vendor/shopware/storefront/Controller/CaptchaController.php line 39

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Controller;
  3. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  4. use Shopware\Core\Framework\Routing\Annotation\Since;
  5. use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
  6. use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException;
  7. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  8. use Shopware\Storefront\Framework\Captcha\AbstractCaptcha;
  9. use Shopware\Storefront\Framework\Captcha\BasicCaptcha;
  10. use Shopware\Storefront\Pagelet\Captcha\AbstractBasicCaptchaPageletLoader;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. /**
  16.  * @Route(defaults={"_routeScope"={"storefront"}})
  17.  */
  18. class CaptchaController extends StorefrontController
  19. {
  20.     private AbstractBasicCaptchaPageletLoader $basicCaptchaPageletLoader;
  21.     private AbstractCaptcha $basicCaptcha;
  22.     public function __construct(
  23.         AbstractBasicCaptchaPageletLoader $basicCaptchaPageletLoader,
  24.         AbstractCaptcha $basicCaptcha
  25.     ) {
  26.         $this->basicCaptchaPageletLoader $basicCaptchaPageletLoader;
  27.         $this->basicCaptcha $basicCaptcha;
  28.     }
  29.     /**
  30.      * @Since("6.4.2.0")
  31.      * @Route("/basic-captcha", name="frontend.captcha.basic-captcha.load", methods={"GET"}, defaults={"auth_required"=false}, defaults={"XmlHttpRequest"=true})
  32.      */
  33.     public function loadBasicCaptcha(Request $requestSalesChannelContext $context): Response
  34.     {
  35.         $formId $request->get('formId');
  36.         $page $this->basicCaptchaPageletLoader->load($request$context);
  37.         $request->getSession()->set($formId BasicCaptcha::BASIC_CAPTCHA_SESSION$page->getCaptcha()->getCode());
  38.         return $this->renderStorefront('@Storefront/storefront/component/captcha/basicCaptchaImage.html.twig', [
  39.             'page' => $page,
  40.             'formId' => $formId,
  41.         ]);
  42.     }
  43.     /**
  44.      * @Since("6.4.0.0")
  45.      * @Route("/basic-captcha-validate", name="frontend.captcha.basic-captcha.validate", methods={"POST"}, defaults={"XmlHttpRequest"=true})
  46.      */
  47.     public function validate(Request $request): JsonResponse
  48.     {
  49.         $formId $request->get('formId');
  50.         if (!$formId) {
  51.             throw new MissingRequestParameterException('formId');
  52.         }
  53.         if ($this->basicCaptcha->isValid($request)) {
  54.             $fakeSession $request->get(BasicCaptcha::CAPTCHA_REQUEST_PARAMETER);
  55.             $request->getSession()->set($formId BasicCaptcha::BASIC_CAPTCHA_SESSION$fakeSession);
  56.             return new JsonResponse(['session' => $fakeSession]);
  57.         }
  58.         $violations $this->basicCaptcha->getViolations();
  59.         $formViolations = new ConstraintViolationException($violations, []);
  60.         $response[] = [
  61.             'type' => 'danger',
  62.             'error' => 'invalid_captcha',
  63.             'input' => $this->renderView('@Storefront/storefront/component/captcha/basicCaptchaFields.html.twig', [
  64.                 'formId' => $request->get('formId'),
  65.                 'formViolations' => $formViolations,
  66.             ]),
  67.         ];
  68.         return new JsonResponse($response);
  69.     }
  70. }