vendor/shopware/storefront/Framework/Captcha/HoneypotCaptcha.php line 11

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Framework\Captcha;
  3. use Shopware\Core\System\SystemConfig\SystemConfigService;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\Validator\Constraints\Blank;
  6. use Symfony\Component\Validator\Mapping\ClassMetadata;
  7. use Symfony\Component\Validator\Validator\ValidatorInterface;
  8. class HoneypotCaptcha extends AbstractCaptcha
  9. {
  10.     public const CAPTCHA_NAME 'honeypot';
  11.     public const CAPTCHA_REQUEST_PARAMETER 'shopware_surname_confirm';
  12.     /**
  13.      * @var ValidatorInterface
  14.      */
  15.     private $validator;
  16.     /**
  17.      * @var SystemConfigService
  18.      */
  19.     private $systemConfigService;
  20.     /**
  21.      * @var string
  22.      */
  23.     private $honeypotValue;
  24.     public function __construct(ValidatorInterface $validatorSystemConfigService $systemConfigService)
  25.     {
  26.         $this->validator $validator;
  27.         $this->systemConfigService $systemConfigService;
  28.     }
  29.     /**
  30.      * Default method for determining constraints when using the Symfony validator.
  31.      */
  32.     public static function loadValidatorMetadata(ClassMetadata $metadata): void
  33.     {
  34.         $metadata->addPropertyConstraint('honeypotValue', new Blank());
  35.     }
  36.     /**
  37.      * {@inheritdoc}
  38.      */
  39.     public function supports(Request $request): bool
  40.     {
  41.         $activeCaptchas $this->systemConfigService->get('core.basicInformation.activeCaptchas');
  42.         if (empty($activeCaptchas) || !is_array($activeCaptchas)) {
  43.             return false;
  44.         }
  45.         return $request->isMethod(Request::METHOD_POST)
  46.             && in_array(self::CAPTCHA_NAME$activeCaptchastrue);
  47.     }
  48.     /**
  49.      * {@inheritdoc}
  50.      */
  51.     public function isValid(Request $request): bool
  52.     {
  53.         $this->honeypotValue $request->get(self::CAPTCHA_REQUEST_PARAMETER);
  54.         return count($this->validator->validate($this)) < 1;
  55.     }
  56.     /**
  57.      * {@inheritdoc}
  58.      */
  59.     public function getName(): string
  60.     {
  61.         return self::CAPTCHA_NAME;
  62.     }
  63. }