-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.php
309 lines (264 loc) · 8.07 KB
/
webhook.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
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
<?php
/**
* @package Joomla.Plugin
* @subpackage Workflow.Webhook
*
* @copyright Copyright (C) 2015 - 2021 Codextrous Software Pvt. Ltd., Inc. All rights reserved.
* @license GNU General Public License version 2 or later
*/
defined('_JEXEC') or die;
use Joomla\CMS\Application\CMSApplicationInterface;
use Joomla\CMS\Categories\Categories;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Event\Workflow\WorkflowTransitionEvent;
use Joomla\CMS\Factory;
use Joomla\CMS\Form\Form;
use Joomla\CMS\Language\LanguageFactoryInterface;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\User\UserFactoryInterface;
use Joomla\CMS\Workflow\WorkflowPluginTrait;
use Joomla\CMS\Workflow\WorkflowServiceInterface;
use Joomla\Event\EventInterface;
use Joomla\Event\SubscriberInterface;
use Joomla\Utilities\ArrayHelper;
/**
* Workflow Webhook Plugin
*
* @since 1.0.0
*/
class PlgWorkflowWebhook extends CMSPlugin implements SubscriberInterface
{
use WorkflowPluginTrait;
/**
* Load the language file on instantiation.
*
* @var boolean
* @since 4.0.0
*/
protected $autoloadLanguage = true;
/**
* Loads the CMS Application for direct access
*
* @var CMSApplicationInterface
* @since 4.0.0
*/
protected $app;
/**
* Database object.
*
* @var JDatabaseDriver
* @since 3.9.0
*/
protected $db;
/**
* Returns an array of events this subscriber will listen to.
*
* @return array
*
* @since 4.0.0
*/
public static function getSubscribedEvents(): array
{
return [
'onContentPrepareForm' => 'onContentPrepareForm',
'onWorkflowAfterTransition' => 'onWorkflowAfterTransition',
];
}
/**
* The form event.
*
* @param Form $form The form
* @param stdClass $data The data
*
* @return boolean
*
* @since 4.0.0
*/
public function onContentPrepareForm(EventInterface $event)
{
$form = $event->getArgument('0');
$data = $event->getArgument('1');
$context = $form->getName();
// Extend the transition form
if ($context === 'com_workflow.transition')
{
$this->enhanceWorkflowTransitionForm($form, $data);
}
return true;
}
/**
* Send a Notification to defined users a transion is performed
*
* @param string $context The context for the content passed to the plugin.
* @param array $pks A list of primary key ids of the content that has changed stage.
* @param object $data Object containing data about the transition
*
* @return boolean
*
* @since 4.0.0
*/
public function onWorkflowAfterTransition(WorkflowTransitionEvent $event)
{
$context = $event->getArgument('extension');
$extensionName = $event->getArgument('extensionName');
$transition = $event->getArgument('transition');
$pks = $event->getArgument('pks');
if (!$this->isSupported($context))
{
return;
}
$component = $this->app->bootComponent($extensionName);
// Check if send-mail is active
if (empty($transition->options['webhook_send_notification']))
{
return;
}
// ID of the items whose state has changed.
$pks = ArrayHelper::toInteger($pks);
if (empty($pks))
{
return;
}
// The active user
$user = $this->app->getIdentity();
// Prepare Language for messages
$defaultLanguage = ComponentHelper::getParams('com_languages')->get('administrator');
$debug = $this->app->get('debug_lang');
$modelName = $component->getModelName($context);
$model = $component->getMVCFactory()->createModel($modelName, $this->app->getName(), ['ignore_request' => true]);
$categories = Categories::getInstance('Content', ['com_content']);
// Get the title of the stage
$model_stage = $this->app->bootComponent('com_workflow')
->getMVCFactory()->createModel('Stage', 'Administrator');
$toStage = $model_stage->getItem($transition->to_stage_id)->title;
$hasGetItem = method_exists($model, 'getItem');
$container = Factory::getContainer();
foreach ($pks as $pk)
{
// Get the title of the item which has changed
$title = '';
$category = '';
if ($hasGetItem)
{
$item = $model->getItem($pk);
$title = $item->title;
$category = $categories->get($item->catid)->title;
}
// Load language for messaging
$lang = $container->get(LanguageFactoryInterface::class)->createLanguage($user->getParam('admin_language', $defaultLanguage), $debug);
$lang->load('plg_workflow_webhook');
$messageText = '';
if (!empty($transition->options['webhook_text']))
{
$messageText = $transition->options['webhook_text'];
$tokens = array('{TITLE}' => $title,
'{USER}' => $user->email,
'{STATE}' => $lang->_($toStage),
'{CATEGORY}' => $category
);
foreach($tokens as $key => $value){
$messageText = str_replace($key, $value, $messageText);
}
}
if (!empty($method = $transition->options['webhook_method'])) {
$method = strtolower($method).'Call';
if (method_exists($this, $method)) {
$this->$method($transition, json_decode($messageText));
$this->app->enqueueMessage(Text::_('PLG_WORKFLOW_WEBHOOK_SENT'), 'message');
}
}
}
}
/**
* Check if the current plugin should execute workflow related activities
*
* @param string $context
*
* @return boolean
*
* @since 4.0.0
*/
protected function isSupported($context)
{
//if (!$this->checkWhiteAndBlacklist($context))
if (!$this->checkAllowedAndForbiddenlist($context))
{
return false;
}
$parts = explode('.', $context);
// We need at least the extension + view for loading the table fields
if (count($parts) < 2)
{
return false;
}
$component = $this->app->bootComponent($parts[0]);
if (!$component instanceof WorkflowServiceInterface
|| !$component->isWorkflowActive($context))
{
return false;
}
return true;
}
protected function getCall($transition, $body)
{
if (!empty($url = $transition->options['webhook_url'])) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
return $result;
}
}
protected function postCall($transition, $data_string)
{
if (!empty($url = $transition->options['webhook_url'])) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getHeaders($transition));
$result = curl_exec($ch);
return $result;
}
}
protected function putCall($transition, $data_string)
{
if (!empty($url = $transition->options['webhook_url'])) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getHeaders($transition));
$result = curl_exec($ch);
return $result;
}
}
protected function deleteCall($transition, $data_string)
{
if (!empty($url = $transition->options['webhook_url'])) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getHeaders($transition));
$result = curl_exec($ch);
return $result;
}
}
public function getHeaders($transition)
{
$headers = [];
if (!empty($transition->options['webhook_headers'])) {
foreach ($transition->options['webhook_headers'] as $header) {
if (!empty($header->name)) {
$headers[] = $header->name .': '. $header->value;
}
}
}
return $headers;
}
}