PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/Cockpit/Controller/Webhooks.php

https://github.com/agentejo/cockpit
PHP | 125 lines | 82 code | 33 blank | 10 comment | 8 complexity | 2260e53832ba74bc56f0de53fd1593c9 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, Apache-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * This file is part of the Cockpit project.
  4. *
  5. * (c) Artur Heinze - πŸ…°πŸ…ΆπŸ…΄πŸ…½πŸ†ƒπŸ…΄πŸ…ΉπŸ…Ύ, http://agentejo.com
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Cockpit\Controller;
  11. class Webhooks extends \Cockpit\AuthController {
  12. public function __construct($app) {
  13. parent::__construct($app);
  14. if (!$this->module('cockpit')->hasaccess('cockpit', 'webhooks')) {
  15. return $this->helper('admin')->denyRequest();
  16. }
  17. }
  18. public function index() {
  19. $webhooks = $this->app->storage->find('cockpit/webhooks', [
  20. 'sort' => ['name' => 1]
  21. ])->toArray();
  22. return $this->render('cockpit:views/webhooks/index.php', compact('webhooks'));
  23. }
  24. public function webhook($id = null) {
  25. $webhook = [
  26. 'name' => '',
  27. 'url' => '',
  28. 'auth' => ['user'=>'', 'pass'=>''],
  29. 'headers' => [],
  30. 'events' => [],
  31. 'active' => true
  32. ];
  33. if ($id) {
  34. $webhook = $this->app->storage->findOne('cockpit/webhooks', ['_id' => $id]);
  35. if (!$webhook) {
  36. return false;
  37. }
  38. }
  39. $triggers = new \ArrayObject([
  40. 'admin.init',
  41. 'app.{$controller}.init',
  42. 'cockpit.account.login',
  43. 'cockpit.account.logout',
  44. 'cockpit.api.authenticate',
  45. 'cockpit.api.erroronrequest',
  46. 'cockpit.assets.list',
  47. 'cockpit.assets.remove',
  48. 'cockpit.assets.save',
  49. 'cockpit.bootstrap',
  50. 'cockpit.clearcache',
  51. 'cockpit.export',
  52. 'cockpit.import',
  53. 'cockpit.media.removefiles',
  54. 'cockpit.media.rename',
  55. 'cockpit.media.upload',
  56. 'cockpit.request.error',
  57. 'cockpit.rest.init',
  58. 'cockpit.update.after',
  59. 'cockpit.update.before',
  60. 'shutdown',
  61. ]);
  62. $this->app->trigger('cockpit.webhook.events', [$triggers]);
  63. $triggers = $triggers->getArrayCopy();
  64. return $this->render('cockpit:views/webhooks/webhook.php', compact('webhook', 'triggers'));
  65. }
  66. public function save() {
  67. if ($data = $this->param('webhook', false)) {
  68. $data['_modified'] = time();
  69. if (!isset($data['_id'])) {
  70. $data['_created'] = $data['_modified'];
  71. }
  72. $this->app->storage->save('cockpit/webhooks', $data);
  73. // invalidate cache
  74. if ($cache = $this->app->path('#tmp:webhooks.cache.php')) {
  75. @unlink($cache);
  76. }
  77. return json_encode($data);
  78. }
  79. return false;
  80. }
  81. public function remove() {
  82. if ($data = $this->param('webhook', false)) {
  83. $this->app->storage->remove('cockpit/webhooks', ['_id'=>$data['_id']]);
  84. // invalidate cache
  85. if ($cache = $this->app->path('#tmp:webhooks.cache.php')) {
  86. @unlink($cache);
  87. }
  88. return true;
  89. }
  90. return false;
  91. }
  92. }