-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebhookPlugin.inc.php
More file actions
executable file
·430 lines (347 loc) · 11.6 KB
/
WebhookPlugin.inc.php
File metadata and controls
executable file
·430 lines (347 loc) · 11.6 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<?php
/**
* @file plugins/generic/webhook/WebhookPlugin.inc.php
*
* Copyright (c) 2014-2021 Simon Fraser University
* Copyright (c) 2003-2021 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class WebhookPlugin
* @ingroup plugins_block_webhook
*
* @brief Fix SSL plugin class
*/
// define('SUBMISSION_EDITOR_DECISION_EXTERNAL_REVIEW', 8);
// // Submission and review stages decision actions.
// define('SUBMISSION_EDITOR_DECISION_ACCEPT', 1);
// define('SUBMISSION_EDITOR_DECISION_DECLINE', 4);
// // Review stage decisions actions.
// define('SUBMISSION_EDITOR_DECISION_PENDING_REVISIONS', 2);
// define('SUBMISSION_EDITOR_DECISION_RESUBMIT', 3);
// define('SUBMISSION_EDITOR_DECISION_NEW_ROUND', 16);
// // Editorial stage decision actions.
// define('SUBMISSION_EDITOR_DECISION_SEND_TO_PRODUCTION', 7);
import('lib.pkp.classes.plugins.GenericPlugin');
class WebhookPlugin extends GenericPlugin
{
var $submissionMap = array('', 'accept', 'revisions', 'resubmit', 'decline', '', '', 'production', 'review', '', '', '', '', '', '', '', 'round');
var $decisionMap = [
// SUBMISSION_EDITOR_DECISION_ACCEPT
1 => 'accept',
// SUBMISSION_EDITOR_DECISION_PENDING_REVISIONS
2 => 'requestRevisions',
// SUBMISSION_EDITOR_DECISION_RESUBMIT
3 => 'resubmit',
// SUBMISSION_EDITOR_DECISION_DECLINE
4 => 'decline',
// SUBMISSION_EDITOR_DECISION_SEND_TO_PRODUCTION
7 => 'sendToProduction',
// SUBMISSION_EDITOR_DECISION_EXTERNAL_REVIEW
8 => 'skipReview',
// SUBMISSION_EDITOR_DECISION_INITIAL_DECLINE
9 => 'initialDecline',
//SUBMISSION_EDITOR_DECISION_NEW_ROUND
16 => 'newRound',
// SUBMISSION_EDITOR_DECISION_REVERT_DECLINE
17 => 'revertDecline',
];
var $statusMap = [
1 => [
'label' => 'queued',
'text' => 'submissions.queued'
],
3 => [
'label' => 'published',
'text' => 'submission.status.published'
],
4 => [
'label' => 'declined',
'text' => 'submission.status.declined'
],
5 => [
'label' => 'scheduled',
'text' => 'submission.status.scheduled'
]
];
// var $urls = ['https://auto.trialanderror.org/webhook-test/cda048ab-cbb1-42d4-8fa7-3116f20bea48', 'https://auto.trialanderror.org/webhook/cda048ab-cbb1-42d4-8fa7-3116f20bea48', 'https://play.svix.com/in/e_OlxpPyfrm1bj6kOtaAWFX6v2w91/', 'https://typedwebhook.tools/webhook/0d27b246-0df7-49d5-9629-84582558c664'];
/** @var WebhookEventManager */
var $webhookEventManager;
/**
* Get the display name of this plugin
* @return string
*/
public function getDisplayName()
{
return __('plugins.generic.webhook.displayName');
}
/**
* Get the description of this plugin
* @return string
*/
public function getDescription()
{
return __('plugins.generic.webhook.description');
}
/**
* @copydoc Plugin::register()
*/
public function register($category, $path, $mainContextId = null)
{
if (!parent::register($category, $path, $mainContextId))
return false;
if ($this->getEnabled($mainContextId)) {
$this->import('WebhookEventManager');
$this->webhookEventManager = new WebhookEventManager($this);
$defaultEvents = [
['publicationEdit', 'Publication::validate', [$this, 'handleEditPublication']],
['decision', 'EditorAction::recordDecision', [$this, 'editorDecision']],
['add', 'Submission::add', [$this, 'addSubmission']],
['confirmReview', 'ReviewerAction::confirmReview', [$this, 'confirmReview']],
['updateStatus', 'Submission::updateStatus', [$this, 'updateStatus']],
];
HookRegistry::call('Plugin::Webhook::addEvent', [&$defaultEvents]);
foreach ($defaultEvents as $event) {
error_log(json_encode($event[0]));
$this->webhookEventManager->addEvent($event[0], $event[1], $event[2] ?? null);
}
// add the api handler
HookRegistry::register('Dispatcher::dispatch', function (string $hook, \PKPRequest $request): bool {
$path = array_slice(explode('/', trim($request->getRequestPath(), '/')), -1);
$router = $request->getRouter();
if ($router instanceof \APIRouter && $path[0] == 'webhook') {
error_log("Webhook handler");
error_log(json_encode($path));
$this->import('WebhookAPIHandler');
$handler = new WebhookAPIHandler();
$router->setHandler($handler);
$handler->getApp()->run();
exit;
}
return false;
});
}
return true;
}
/**
* Get the name of the settings file to be installed on new context
* creation.
* @return string
*/
public function getContextSpecificPluginSettingsFile()
{
return $this->getPluginPath() . '/settings.xml';
}
public function editorDecision(string $hookName, $args)
{
list($submission, $editorDecision, $result, $recommendation) = $args;
/** @var Submission $submission */
$submission = $args[0];
$editorDecision = $args[1];
$decisionLabel = $this->decisionMap[$editorDecision['decision']] ?? 'unknown';
$decisionText = __('editor.submission.decision.' . $decisionLabel);
$editorDecision['decisionText'] = $decisionText;
$editorDecision['decisionLabel'] = $decisionLabel;
$payload = ["submission" => $submission, "decision" => $editorDecision, "result" => $result, "recommendation" => $recommendation];
error_log(json_encode($payload));
return $payload;
}
public function addSubmission(string $hookName, $args)
{
/** @var Submission $submission */
$submission = $args[0];
/** @var Request $request*/
$request = $args[1];
return ["submission" => $submission, "request" => $request];
}
public function updateStatus(string $hookname, $args)
{
/** @var number $status */
$status = $args[0];
/** @var Submission $submission */
$submission = $args[1];
$newStatus = $status;
if ($status !== STATUS_DECLINED) {
$newStatus = STATUS_QUEUED;
$publication = $submission->getCurrentPublication();
if ($publication && $publication->getData('status') === STATUS_PUBLISHED) {
$newStatus = STATUS_PUBLISHED;
}
if ($publication && $publication->getData('status') === STATUS_SCHEDULED) {
$newStatus = STATUS_SCHEDULED;
}
}
$statusLabel = $this->statusMap[$status] ? $this->statusMap[$status]['label'] : 'unknown';
$statusTextLabel = $statusLabel ? $this->statusMap[$status]['text'] : null;
$statusText = $statusTextLabel ? __($statusTextLabel) : null;
$newStatusLabel = $this->statusMap[$newStatus] ? $this->statusMap[$newStatus]['label'] : 'unknown';
$newStatusTextLabel = $newStatusLabel ? $this->statusMap[$newStatus]['text'] : null;
$newStatusText = $newStatusTextLabel ? __($newStatusTextLabel) : null;
return [
"status" => $status,
"statusLabel" => $statusLabel,
"statusText" => $statusText,
"newStatus" => $newStatus,
"newStatusLabel" => $newStatusLabel,
"newStatusText" => $newStatusText,
"submission" => $submission
];
// return false;
}
public function confirmReview(string $hookName, $args)
{
list($request, $submission, $email, $decline) = $args;
return ["submission" => $submission, "request" => $request, "email" => $email, "decline" => $decline];
// return false;
}
public function handleEditPublication(string $hookName, $args)
{
// list($newPublication, $publication, $params) = $args;
list($errors, $action, $props) = $args;
if ($errors) {
error_log(json_encode($errors));
}
$request = $this->getRequest();
$publicationId = $props['id'];
$publication = Services::get('publication')->get((int) $publicationId);
error_log("Name of hook: " . $hookName);
$userGroupDao = DAORegistry::getDAO('UserGroupDAO'); /* @var $userGroupDao UserGroupDAO */
$data = Services::get('publication')->getFullProperties(
$publication,
[
'request' => $request,
'userGroups' => $userGroupDao->getByContextId($publication->getData('contextId'))->toArray(),
]
);
return ["publication" => $data, "props" => $props];
// return false;
}
/**
* @param $event string The event to fire
* @param $data array The data to send
* @param $urls array The URLs to send to
* @param $headers array The headers to send
*
* @return array The results of the webhook
*/
public function fireWebhook($event, $data, $urls = null, $headers = [])
{
if (!$urls) {
$urls = $this->getSetting($this->getRequest()->getContext()->getId(), 'webhooks');
}
$results = [];
$payload = $this->makeWebhookPayload($event, $data);
$defaultHeaders = [
'Content-type: application/json',
'X-OJS-Webhook-Event: ' . $event
];
error_log("IN fireWebhook" . json_encode($urls));
foreach ($urls as $url) {
$ch = curl_init();
$optArray = [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_HTTPHEADER => array_merge($defaultHeaders, $headers)
];
curl_setopt_array($ch, $optArray);
$result = curl_exec($ch);
curl_close($ch);
$results[$url] = $result;
}
return $results;
}
public function makeWebhookPayload($event, $data)
{
return json_encode(["event" => $event, "data" => $data]);
}
/**
* Add feed links to page <head> on select/all pages.
*/
public function fixBaseUrl($hookName, $args)
{
// Only page requests will be handled
$baseUrl = &$args[0];
$baseUrl = Config::getVar('general', 'base_url');
return true;
}
/**
* @copydoc Plugin::getActions()
*/
public function getActions($request, $actionArgs)
{
// Get the existing actions
$actions = parent::getActions($request, $actionArgs);
if (!$this->getEnabled()) {
return $actions;
}
$router = $request->getRouter();
import('lib.pkp.classes.linkAction.request.AjaxModal');
$pluginActions = ['settings'];
return array_merge(
array_map(
fn($action) =>
new LinkAction(
$action,
new AjaxModal(
$router->url($request, null, null, 'manage', null, array('verb' => $action, 'plugin' => $this->getName(), 'category' => 'generic')),
$this->getDisplayName()
),
__("plugins.generic.webhook.linkactions.$action"),
null
),
$pluginActions
)
);
}
/**
* @copydoc Plugin::manage()
*/
public function manage($args, $request)
{
switch ($request->getUserVar('verb')) {
case 'settings':
AppLocale::requireComponents(LOCALE_COMPONENT_APP_COMMON, LOCALE_COMPONENT_PKP_MANAGER);
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign('webhookEventManager', $this->webhookEventManager);
// $templateMgr->registerPlugin('function', 'plugin_url', array($this, 'smartyPluginUrl'));
$this->import('WebhookSettingsForm');
$form = new WebhookSettingsForm($this, $request->getContext()->getId());
if ($request->getUserVar('save')) {
$form->readInputData();
if ($form->validate()) {
$form->execute($request);
return new JSONMessage(true);
} else {
$templateMgr->assign('formErrors', $form->getErrorsArray());
}
} else {
$form->initData();
}
return new JSONMessage(true, $form->fetch($request));
}
return parent::manage($args, $request);
}
/**
* @return array{url: string, events: array<string, string>[] }[]
*/
public function getRegisteredWebhooks(): array
{
$webhooks = $this->getSetting($this->getRequest()->getContext()->getId(), 'webhooks');
return $webhooks;
}
public function getWebhookUrls(string $event)
{
// Retrieve the registered webhooks
$webhooks = $this->getSetting($this->getRequest()->getContext()->getId(), 'webhooks');
// Filter the webhooks that have the specified event enabled
$filteredWebhooks = array_filter($webhooks, function ($webhook) use ($event) {
return in_array($event, $webhook['events']);
});
// Extract the URLs from the filtered webhooks
$urls = array_map(function ($webhook) {
return $webhook['url'];
}, $filteredWebhooks);
return $urls;
}
}