PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / trunk
VikAppointments Services Booking Calendar vtrunk
trunk 1.2.17 1.2.18 1.2.19
vikappointments / admin / controllers / webhook.php
vikappointments / admin / controllers Last commit date
analytics.php 4 years ago apiban.php 4 years ago apilog.php 4 years ago apiplugin.php 4 years ago apiuser.php 4 years ago backup.php 4 years ago calendar.php 4 years ago city.php 4 years ago closure.php 1 month ago configapp.php 4 years ago configcldays.php 2 years ago configcron.php 4 years ago configemp.php 4 years ago configsmsapi.php 4 years ago configuration.php 1 month ago conversion.php 1 year ago country.php 4 years ago coupon.php 4 years ago coupongroup.php 4 years ago cronjob.php 2 years ago cronjoblog.php 4 years ago customer.php 5 months ago customf.php 1 year ago dashboard.php 4 years ago emplocwdays.php 4 years ago employee.php 1 year ago emprates.php 4 years ago export.php 4 years ago exportres.php 4 years ago file.php 5 months ago findreservation.php 1 month ago group.php 4 years ago import.php 4 years ago index.html 4 years ago invoice.php 1 month ago langcustomf.php 4 years ago langemployee.php 4 years ago langgroup.php 4 years ago langmedia.php 4 years ago langoption.php 4 years ago langoptiongroup.php 4 years ago langpackage.php 4 years ago langpackgroup.php 4 years ago langpayment.php 4 years ago langservice.php 4 years ago langstatuscode.php 4 years ago langsubscr.php 4 years ago langtax.php 4 years ago location.php 4 years ago mailtext.php 2 years ago makerecurrence.php 1 month ago media.php 4 years ago multiorder.php 4 years ago option.php 5 months ago optiongroup.php 4 years ago package.php 2 years ago packgroup.php 4 years ago packorder.php 1 year ago payment.php 4 years ago rate.php 4 years ago reportsemp.php 4 years ago reportsser.php 4 years ago reservation.php 1 month ago restriction.php 4 years ago review.php 4 years ago service.php 1 year ago serworkday.php 5 months ago state.php 4 years ago statuscode.php 4 years ago subscription.php 4 years ago subscrorder.php 4 years ago tag.php 4 years ago tax.php 4 years ago usernote.php 4 years ago waitinglist.php 4 years ago webhook.php 4 years ago wizard.php 1 year ago
webhook.php
439 lines
1 <?php
2 /**
3 * @package VikAppointments
4 * @subpackage core
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 VAPLoader::import('libraries.mvc.controllers.admin');
15 VAPLoader::import('libraries.webhook.webhook');
16
17 /**
18 * VikAppointments web hook controller.
19 *
20 * @since 1.7
21 */
22 class VikAppointmentsControllerWebhook extends VAPControllerAdmin
23 {
24 /**
25 * Task used to access the creation page of a new record.
26 *
27 * @return boolean
28 */
29 public function add()
30 {
31 $app = JFactory::getApplication();
32 $user = JFactory::getUser();
33
34 // unset user state for being recovered again
35 $app->setUserState('vap.webhook.data', array());
36
37 // check user permissions
38 if (!$user->authorise('core.create', 'com_vikappointments') || !$user->authorise('core.access.config', 'com_vikappointments'))
39 {
40 // back to main list, not authorised to create records
41 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
42 $this->cancel();
43
44 return false;
45 }
46
47 $this->setRedirect('index.php?option=com_vikappointments&view=managewebhook');
48
49 return true;
50 }
51
52 /**
53 * Task used to access the management page of an existing record.
54 *
55 * @return boolean
56 */
57 public function edit()
58 {
59 $app = JFactory::getApplication();
60 $user = JFactory::getUser();
61
62 // unset user state for being recovered again
63 $app->setUserState('vap.webhook.data', array());
64
65 // check user permissions
66 if (!$user->authorise('core.edit', 'com_vikappointments') || !$user->authorise('core.access.config', 'com_vikappointments'))
67 {
68 // back to main list, not authorised to edit records
69 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
70 $this->cancel();
71
72 return false;
73 }
74
75 $cid = $app->input->getUint('cid', array(0));
76
77 $this->setRedirect('index.php?option=com_vikappointments&view=managewebhook&cid[]=' . $cid[0]);
78
79 return true;
80 }
81
82 /**
83 * Task used to save the record data set in the request.
84 * After saving, the user is redirected to the main list.
85 *
86 * @return void
87 */
88 public function saveclose()
89 {
90 if ($this->save())
91 {
92 $this->cancel();
93 }
94 }
95
96 /**
97 * Task used to save the record data set in the request.
98 * After saving, the user is redirected to the creation
99 * page of a new record.
100 *
101 * @return void
102 */
103 public function savenew()
104 {
105 if ($this->save())
106 {
107 $this->setRedirect('index.php?option=com_vikappointments&task=webhook.add');
108 }
109 }
110
111 /**
112 * Task used to save the record data as a copy of the current item.
113 * After saving, the user is redirected to the management
114 * page of the record that has been saved.
115 *
116 * @return void
117 */
118 public function savecopy()
119 {
120 $this->save(true);
121 }
122
123 /**
124 * Task used to save the record data set in the request.
125 * After saving, the user is redirected to the management
126 * page of the record that has been saved.
127 *
128 * @param boolean $copy True to save the record as a copy.
129 *
130 * @return boolean
131 */
132 public function save($copy = false)
133 {
134 $app = JFactory::getApplication();
135 $input = $app->input;
136 $user = JFactory::getUser();
137
138 /**
139 * Added token validation.
140 *
141 * @since 1.7
142 */
143 if (!JSession::checkToken())
144 {
145 // back to main list, missing CSRF-proof token
146 $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error');
147 $this->cancel();
148
149 return false;
150 }
151
152 $args = array();
153 $args['name'] = $input->getString('name', '');
154 $args['hook'] = $input->getString('hook', '');
155 $args['url'] = $input->getString('url', '');
156 $args['secret'] = $input->getString('secret', '');
157 $args['published'] = $input->getUint('published', 0);
158 $args['id'] = $input->getUint('id', 0);
159
160 $args['params'] = array();
161
162 try
163 {
164 // fetch webhook instance
165 $webhook = VAPWebHook::getInstance($args['hook']);
166
167 // load form parameters from request
168 foreach ($webhook->getForm() as $k => $p)
169 {
170 $args['params'][$k] = $input->get('wh_' . $k, '', 'string');
171 }
172 }
173 catch (Exception $e)
174 {
175 // unset hook event so that the model can raise an error
176 $args['hook'] = '';
177 }
178
179 if ($copy)
180 {
181 // unset ID to create a copy
182 $args['id'] = 0;
183 }
184
185 $rule = 'core.' . ($args['id'] > 0 ? 'edit' : 'create');
186
187 // check user permissions
188 if (!$user->authorise($rule, 'com_vikappointments') || !$user->authorise('core.access.config', 'com_vikappointments'))
189 {
190 // back to main list, not authorised to create/edit records
191 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
192 $this->cancel();
193
194 return false;
195 }
196
197 // get webhook model
198 $webhook = $this->getModel();
199
200 // try to save arguments
201 $id = $webhook->save($args);
202
203 if (!$id)
204 {
205 // get string error
206 $error = $webhook->getError(null, true);
207
208 // display error message
209 $app->enqueueMessage(JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $error), 'error');
210
211 $url = 'index.php?option=com_vikappointments&view=managewebhook';
212
213 if ($args['id'])
214 {
215 $url .= '&cid[]=' . $args['id'];
216 }
217
218 // redirect to new/edit page
219 $this->setRedirect($url);
220
221 return false;
222 }
223
224 // display generic successful message
225 $app->enqueueMessage(JText::translate('JLIB_APPLICATION_SAVE_SUCCESS'));
226
227 // redirect to edit page
228 $this->setRedirect('index.php?option=com_vikappointments&task=webhook.edit&cid[]=' . $id);
229
230 return true;
231 }
232
233 /**
234 * Deletes a list of records set in the request.
235 *
236 * @return boolean
237 */
238 public function delete()
239 {
240 $app = JFactory::getApplication();
241 $user = JFactory::getUser();
242
243 /**
244 * Added token validation.
245 * Both GET and POST are supported.
246 *
247 * @since 1.7
248 */
249 if (!JSession::checkToken() && !JSession::checkToken('get'))
250 {
251 // back to main list, missing CSRF-proof token
252 $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error');
253 $this->cancel();
254
255 return false;
256 }
257
258 $cid = $app->input->get('cid', array(), 'uint');
259
260 // check user permissions
261 if (!$user->authorise('core.delete', 'com_vikappointments') || !$user->authorise('core.access.config', 'com_vikappointments'))
262 {
263 // back to main list, not authorised to delete records
264 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
265 $this->cancel();
266
267 return false;
268 }
269
270 // delete selected records
271 $this->getModel()->delete($cid);
272
273 // back to main list
274 $this->cancel();
275
276 return true;
277 }
278
279 /**
280 * Publishes the selected records.
281 *
282 * @return boolean
283 */
284 public function publish()
285 {
286 $app = JFactory::getApplication();
287 $user = JFactory::getUser();
288
289 /**
290 * Added token validation.
291 * Both GET and POST are supported.
292 *
293 * @since 1.7
294 */
295 if (!JSession::checkToken() && !JSession::checkToken('get'))
296 {
297 // back to main list, missing CSRF-proof token
298 $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error');
299 $this->cancel();
300
301 return false;
302 }
303
304 $cid = $app->input->get('cid', array(), 'uint');
305 $task = $app->input->get('task', null);
306
307 $state = $task == 'unpublish' ? 0 : 1;
308
309 // check user permissions
310 if (!$user->authorise('core.edit.state', 'com_vikappointments') || !$user->authorise('core.access.config', 'com_vikappointments'))
311 {
312 // back to main list, not authorised to edit records
313 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
314 $this->cancel();
315
316 return false;
317 }
318
319 // change state of selected records
320 $this->getModel()->publish($cid, $state);
321
322 // back to main list
323 $this->cancel();
324
325 return true;
326 }
327
328 /**
329 * Redirects the users to the main records list.
330 *
331 * @return void
332 */
333 public function cancel()
334 {
335 $this->setRedirect('index.php?option=com_vikappointments&view=webhooks');
336 }
337
338 /**
339 * AJAX end-point used to load the supported parameters of the given hook.
340 * The task expects the following parameters to be set in request.
341 *
342 * @param string hook The action name.
343 * @param integer id The record ID.
344 *
345 * @return void
346 */
347 public function paramsajax()
348 {
349 $input = JFactory::getApplication()->input;
350
351 /**
352 * Added token validation.
353 *
354 * @since 1.7
355 */
356 if (!JSession::checkToken())
357 {
358 // missing CSRF-proof token
359 UIErrorFactory::raiseError(403, JText::translate('JINVALID_TOKEN'));
360 }
361
362 $hook = $input->getString('hook', '');
363 $id = $input->getUint('id', 0);
364
365 try
366 {
367 // fetch webhook instance
368 $webhook = VAPWebHook::getInstance($hook);
369 }
370 catch (Exception $e)
371 {
372 // something went wrong, safely abort the AJAX request
373 UIErrorFactory::raiseError($e->getCode(), $e->getMessage());
374 }
375
376 // get web hook form
377 $form = $webhook->getForm();
378
379 $params = array();
380
381 if ($id)
382 {
383 // load web hook details
384 $webhook = $this->getModel()->getItem($id);
385
386 if ($webhook)
387 {
388 // use found parameters
389 $params = $webhook->params;
390 }
391 }
392
393 // build display data
394 $data = array(
395 'fields' => $form,
396 'params' => $params,
397 'prefix' => 'wh_',
398 );
399
400 // render web hook form
401 $html = JLayoutHelper::render('form.fields', $data);
402
403 // send HTML form to caller
404 $this->sendJSON(json_encode($html));
405 }
406
407 /**
408 * AJAX end-point used to load the file log of the given hook.
409 * The task expects the following parameters to be set in request.
410 *
411 * @param string file The log file name/path.
412 *
413 * @return void
414 */
415 public function loadlogajax()
416 {
417 $input = JFactory::getApplication()->input;
418
419 /**
420 * Added token validation.
421 *
422 * @since 1.7
423 */
424 if (!JSession::checkToken())
425 {
426 // missing CSRF-proof token
427 UIErrorFactory::raiseError(403, JText::translate('JINVALID_TOKEN'));
428 }
429
430 $file = $input->getString('file', '');
431
432 // load log
433 $log = $this->getModel()->getLog($file);
434
435 // send HTML to caller
436 $this->sendJSON(json_encode(htmlentities($log)));
437 }
438 }
439