WebHooks.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. namespace Plugin\WebHooks\Controller;
  20. use App\Core\Controller;
  21. use App\Core\DB;
  22. use App\Core\Form;
  23. use function App\Core\I18n\_m;
  24. use App\Core\Router;
  25. use App\Util\Common;
  26. use App\Util\Exception\ClientException;
  27. use Functional as F;
  28. use Plugin\WebHooks as P;
  29. use Plugin\WebHooks\Entity as E;
  30. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  31. use Symfony\Component\Form\Extension\Core\Type\TextType;
  32. use Symfony\Component\HttpFoundation\Request;
  33. class WebHooks extends Controller
  34. {
  35. public static function setup()
  36. {
  37. $user = Common::ensureLoggedIn();
  38. $hooks = F\reindex(DB::findBy(E\WebHook::class, ['actor_id' => $user->getId()]), fn (E\WebHook $wh) => $wh->getEvent());
  39. $form = Form::create([
  40. ['notifications', TextType::class, ['label' => _m('Trigger this hook when I recieve a notification'), 'data' => ($hooks['notifications'] ?? null)?->getTarget()]],
  41. ['subscriptions', TextType::class, ['label' => _m('Trigger this hook when someone subscribes to me'), 'data' => ($hooks['subscriptions'] ?? null)?->getTarget()]],
  42. ['save_webhooks', SubmitType::class, ['label' => _m('Submit')]],
  43. ], form_options: ['action' => Router::url(P\WebHooks::controller_route)]);
  44. return [
  45. '_template' => 'webhooks/settings.html.twig',
  46. 'form_view' => $form->createView(),
  47. 'form' => $form,
  48. 'hooks' => $hooks,
  49. ];
  50. }
  51. public function onPost(Request $request)
  52. {
  53. $get_response = self::setup();
  54. $form = $get_response['form'];
  55. $hooks = $get_response['hooks'];
  56. $user = Common::user();
  57. if (\is_null($user)) {
  58. return Form::forceRedirect($form, $request);
  59. }
  60. $form->handleRequest($request);
  61. if ($form->isSubmitted() && $form->isValid()) {
  62. $data = $form->getData();
  63. unset($data['_next']);
  64. $error = false;
  65. foreach ($data as $key => $value) {
  66. if ($value !== '') {
  67. $parts = parse_url($value);
  68. if ($parts === false || ($parts['scheme'] ?? null) !== 'https' || ($parts['host'] ?? null) === Common::config('site', 'server')) {
  69. $error = true;
  70. break;
  71. } else {
  72. if (!isset($hooks[$key])) {
  73. DB::persist(E\WebHook::create([
  74. 'actor_id' => $user->getId(),
  75. 'event' => $key,
  76. 'target' => $value,
  77. ]));
  78. } else {
  79. $hooks[$key]->setTarget($value);
  80. }
  81. }
  82. } else {
  83. $error = true;
  84. }
  85. }
  86. if (!$error) {
  87. DB::flush();
  88. return Form::forceRedirect($form, $request);
  89. } else {
  90. throw new ClientException(_m('Invalid form submission'));
  91. }
  92. }
  93. throw new ClientException(_m('Don\'t GET this page'));
  94. }
  95. }