-
Notifications
You must be signed in to change notification settings - Fork 2
/
FidusWriterGatewayPlugin.inc.php
257 lines (226 loc) · 5.36 KB
/
FidusWriterGatewayPlugin.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
/**
* Copyright (c) 2015-2017 Afshin Sadehghi
* Copyright (c) 2017 Firas Kassawat
* Copyright (c) 2016-2018 Johannes Wilm
* Copyright (c) 2014-2017 Simon Fraser University
* Copyright (c) 2000-2017 John Willinsky
* License: GNU GPL v2. See LICENSE.md for details.
*/
import('lib.pkp.classes.security.authorization.PolicySet');
import('lib.pkp.classes.plugins.GatewayPlugin');
class FidusWriterGatewayPlugin extends GatewayPlugin
{
// BEGIN STANDARD PLUGIN FUNCTIONS
protected $parentPlugin;
/**
* Constructor
* @param $parentPlugin FidusWriterPlugin
*/
function __construct($parentPlugin)
{
$this->parentPlugin = $parentPlugin;
parent::__construct();
}
/**
* Get the name of this plugin. The name must be unique within
* its category.
* @return String name of plugin
*/
public function getName()
{
return 'FidusWriterGatewayPlugin';
}
/**
* Hide this plugin from the management interface (it's subsidiary)
*/
public function getHideManagement()
{
return true;
}
/**
* @copydoc Plugin::getDisplayName()
*/
public function getDisplayName()
{
return __('plugins.generic.fidusWriter.displayName');
}
/**
* @copydoc Plugin::getDescription()
*/
public function getDescription()
{
return __('plugins.generic.fidusWriter.description');
}
/**
* Override the builtin to get the correct plugin path.
*/
public function getPluginPath()
{
return $this->parentPlugin->getPluginPath();
}
/**
* Get whether or not this plugin is enabled. (Should always return true, as the
* parent plugin will take care of loading this one when needed)
* @return boolean
*/
public function getEnabled()
{
return $this->parentPlugin->getEnabled();
}
/**
* Override the builtin to get the correct template path.
* @return string
*/
public function getTemplatePath($inCore = false)
{
return $this->parentPlugin->getTemplatePath($inCore);
}
/**
* @see Plugin::isSitePlugin()
*/
function isSitePlugin()
{
return true;
}
// END STANDARD PLUGIN FUNCTIONS
/**
* @return mixed|null
*/
public function getApiKey()
{
return $this->parentPlugin->getApiKey();
}
/**
* @return string
*/
public function getApiVersion()
{
return "1.0";
}
/**
* Handle all requests for this plugin.
* @param $args array
* @param $request PKPRequest Request object
* @return bool
*/
public function fetch($args, $request)
{
if (!$this->getEnabled()) {
return false;
}
ignore_user_abort(true);
set_time_limit(0);
ob_start();
try {
$restCallType = $this->getRESTRequestType();
$operator = array_shift($args);
if ($restCallType === "GET") {
$this->parentPlugin->import('gateways.FidusWriterGetRequestHandler');
$requestHandler = new FidusWriterGetRequestHandler($request, $this->getApiKey(), $this->getApiVersion());
$handlerName = "get_{$operator}";
$response = false;
if (method_exists($requestHandler, $handlerName)) {
$response = $requestHandler->$handlerName();
}
if ($response) {
$this->sendJsonResponse($response);
} else {
$error = "Not a valid request";
$this->sendErrorResponse($error);
}
}
if ($restCallType === "POST") {
$key = $_GET['key'];
if ($this->getApiKey() !== $key) {
// Not correct api key.
$error = "Incorrect API Key";
$this->sendErrorResponse($error);
}
$this->parentPlugin->import('gateways.FidusWriterSubmissionHandler');
$submissionHandler = new FidusWriterSubmissionHandler($request, $this->getApiKey(), $this->getApiVersion());
$handlerName = "post_{$operator}";
$response = false;
if (method_exists($submissionHandler, $handlerName)) {
$response = $submissionHandler->$handlerName();
}
if ($response) {
$this->sendJsonResponse($response);
} else {
$error = "Not a valid request";
$this->sendErrorResponse($error);
}
}
if ($restCallType === "PUT") {
$response = [
"message" => "PUT response",
"version" => $this->getApiVersion()
];
$this->sendJsonResponse($response);
}
if ($restCallType === "DELETE") {
$response = [
"message" => "DELETE response",
"version" => $this->getApiVersion()
];
$this->sendJsonResponse($response);
}
return true;
} catch (Exception $e) {
$this->sendErrorResponse($e->getMessage());
return true;
}
}
/**
* @return string
*/
function getRESTRequestType()
{
$callType = $_SERVER['REQUEST_METHOD'];
switch ($callType) {
case 'PUT':
case 'DELETE':
case 'GET':
case 'POST':
$result = $callType;
break;
default:
$result = "";
}
return $result;
}
/**
* @param array $response
*/
function sendJsonResponse($response)
{
header("Content-Type: application/json;charset=utf-8");
http_response_code(200);
echo json_encode($response);
header('Connection: close');
header('Content-Length: ' . ob_get_length());
ob_flush();
flush();
ob_end_flush();
}
/**
* Display an error message and exit
* @param $errorMessage
*/
public function sendErrorResponse($errorMessage)
{
header("HTTP/1.0 500 Internal Server Error");
http_response_code(500);
$response = [
"error" => "internal server error",
"errorMessage" => $errorMessage,
"code" => "500"
];
echo json_encode($response);
header('Connection: close');
header('Content-Length: ' . ob_get_length());
ob_flush();
flush();
ob_end_flush();
}
}