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

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