. // }}} namespace Plugin\WebHooks\Controller; use App\Core\Controller; use App\Core\DB; use App\Core\Form; use function App\Core\I18n\_m; use App\Core\Router\Router; use App\Util\Common; use App\Util\Exception\ClientException; use Functional as F; use Plugin\WebHooks as P; use Plugin\WebHooks\Entity as E; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\HttpFoundation\Request; class WebHooks extends Controller { public static function setup() { $user = Common::ensureLoggedIn(); $hooks = F\reindex(DB::findBy(E\WebHook::class, ['actor_id' => $user->getId()]), fn (E\WebHook $wh) => $wh->getEvent()); $form = Form::create([ ['notifications', TextType::class, ['label' => _m('Trigger this hook when I recieve a notification'), 'data' => ($hooks['notifications'] ?? null)?->getTarget()]], ['subscriptions', TextType::class, ['label' => _m('Trigger this hook when someone subscribes to me'), 'data' => ($hooks['subscriptions'] ?? null)?->getTarget()]], ['save_webhooks', SubmitType::class, ['label' => _m('Submit')]], ], form_options: ['action' => Router::url(P\WebHooks::controller_route)]); return [ '_template' => 'webhooks/settings.html.twig', 'form_view' => $form->createView(), 'form' => $form, 'hooks' => $hooks, ]; } public function onPost(Request $request) { $get_response = self::setup(); $form = $get_response['form']; $hooks = $get_response['hooks']; $user = Common::user(); if (\is_null($user)) { return Form::forceRedirect($form, $request); } $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $data = $form->getData(); unset($data['_next']); $error = false; foreach ($data as $key => $value) { if ($value !== '') { $parts = parse_url($value); if ($parts === false || ($parts['scheme'] ?? null) !== 'https' || ($parts['host'] ?? null) === Common::config('site', 'server')) { $error = true; break; } else { if (!isset($hooks[$key])) { DB::persist(E\WebHook::create([ 'actor_id' => $user->getId(), 'event' => $key, 'target' => $value, ])); } else { $hooks[$key]->setTarget($value); } } } else { $error = true; } } if (!$error) { DB::flush(); return Form::forceRedirect($form, $request); } else { throw new ClientException(_m('Invalid form submission')); } } throw new ClientException(_m('Don\'t GET this page')); } }