custom/plugins/GbmedForm/src/Storefront/Framework/Captcha/CaptchaRouteListener.php line 78

Open in your IDE?
  1. <?php
  2. /**
  3.  * gb media
  4.  * All Rights Reserved.
  5.  *
  6.  * Unauthorized copying of this file, via any medium is strictly prohibited.
  7.  * The content of this file is proprietary and confidential.
  8.  *
  9.  * @category       Shopware
  10.  * @package        Shopware_Plugins
  11.  * @subpackage     GbmedForm
  12.  * @copyright      Copyright (c) 2020, gb media
  13.  * @license        proprietary
  14.  * @author         Giuseppe Bottino
  15.  * @link           http://www.gb-media.biz
  16.  */
  17. declare(strict_types=1);
  18. namespace Gbmed\Form\Storefront\Framework\Captcha;
  19. use Gbmed\Form\Framework\Exception\CaptchaInvalidException;
  20. use Gbmed\Form\Services\ReCaptcha;
  21. use Gbmed\Form\Framework\Captcha\FormRoutes\Collection;
  22. use Shopware\Core\Framework\Adapter\Translation\Translator;
  23. use Shopware\Core\Framework\Routing\KernelListenerPriorities;
  24. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  25. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  26. use Symfony\Component\HttpFoundation\Request;
  27. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  28. use Symfony\Component\HttpKernel\KernelEvents;
  29. class CaptchaRouteListener implements EventSubscriberInterface
  30. {
  31.     /**
  32.      * @var Collection
  33.      */
  34.     private $formRoutes;
  35.     /**
  36.      * @var ReCaptcha
  37.      */
  38.     private $service;
  39.     /**
  40.      * @var Translator
  41.      */
  42.     private $translator;
  43.     /**
  44.      * CaptchaRouteListener constructor.
  45.      * @param ReCaptcha $service
  46.      * @param Collection $formRoutes
  47.      * @param Translator $translator
  48.      */
  49.     public function __construct(ReCaptcha $serviceCollection $formRoutesTranslator $translator)
  50.     {
  51.         $this->service $service;
  52.         $this->formRoutes $formRoutes;
  53.         $this->translator $translator;
  54.     }
  55.     /**
  56.      * {@inheritdoc}
  57.      */
  58.     public static function getSubscribedEvents()
  59.     {
  60.         return [
  61.             KernelEvents::CONTROLLER => [
  62.                 ['validateCaptcha'KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_SCOPE_VALIDATE],
  63.             ],
  64.         ];
  65.     }
  66.     /**
  67.      * throw CaptchaInvalidException if g-recaptcha-response is not valide
  68.      *
  69.      * @param ControllerEvent $event
  70.      */
  71.     public function validateCaptcha(ControllerEvent $event): void
  72.     {
  73.         $salesChannelContext $this->getSalesChannelContext($event->getRequest());
  74.         $supports $this->supports($event->getRequest(), $salesChannelContext);
  75.         if ($supports === false) {
  76.             return;
  77.         }
  78.         if (!$this->service->validate($event->getRequest()->request$salesChannelContext)) {
  79.             throw new CaptchaInvalidException(
  80.                 $this->translator->trans('gbmed-form.contact.exception')
  81.             );
  82.         }
  83.     }
  84.     /**
  85.      * @param Request $request
  86.      * @param SalesChannelContext|null $salesChannelContext
  87.      * @return bool
  88.      */
  89.     private function supports(Request $request, ?SalesChannelContext $salesChannelContext)
  90.     {
  91.         /** @var bool $isRecaptcha */
  92.         $isRecaptcha $this->service->getSecret($salesChannelContext) && $this->service->getSitekey($salesChannelContext);
  93.         /** @var bool $route */
  94.         $route $this->formRoutes->support($request);
  95.         return $isRecaptcha && $request->isMethod(Request::METHOD_POST) && $route;
  96.     }
  97.     /**
  98.      * helper to get SalesChannelContext
  99.      *
  100.      * @param Request $request
  101.      * @return SalesChannelContext|null
  102.      */
  103.     private function getSalesChannelContext(Request $request): ?SalesChannelContext
  104.     {
  105.         return $request->attributes->get('sw-sales-channel-context');
  106.     }
  107. }