vendor/gregwar/captcha-bundle/Type/CaptchaType.php line 24

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Gregwar\CaptchaBundle\Type;
  4. use Symfony\Component\Form\Extension\Core\Type\TextType;
  5. use Symfony\Component\Form\FormView;
  6. use Symfony\Component\Form\FormInterface;
  7. use Symfony\Component\Form\AbstractType;
  8. use Symfony\Component\Form\FormBuilderInterface;
  9. use Symfony\Component\OptionsResolver\OptionsResolver;
  10. use Symfony\Component\Form\FormEvents;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. use Gregwar\CaptchaBundle\Validator\CaptchaValidator;
  13. use Gregwar\CaptchaBundle\Generator\CaptchaGenerator;
  14. use Symfony\Component\HttpFoundation\RequestStack
  15. /**
  16.  * Captcha type.
  17.  *
  18.  * @author Gregwar <g.passault@gmail.com>
  19.  */
  20. class CaptchaType extends AbstractType
  21. {
  22.     const SESSION_KEY_PREFIX '_captcha_';
  23.     /** @var SessionInterface */
  24.     protected $session;
  25.     /** @var CaptchaGenerator */
  26.     protected $generator;
  27.     /** @var TranslatorInterface */
  28.     protected $translator;
  29.     /** @var array<mixed> */
  30.     private $options;
  31.     /**
  32.      * @param SessionInterface $session
  33.      * @param CaptchaGenerator $generator
  34.      * @param TranslatorInterface $translator
  35.      * @param array<mixed> $options
  36.      */
  37.     public function __construct(RequestStack $requestStackCaptchaGenerator $generatorTranslatorInterface $translator, array $options)
  38.     {
  39.         $this->session $requestStack->getSession();
  40.         $this->generator $generator;
  41.         $this->translator $translator;
  42.         $this->options $options;
  43.     }
  44.     /**
  45.      * @param FormBuilderInterface<mixed> $builder
  46.      * @param array<mixed> $options
  47.      */
  48.     public function buildForm(FormBuilderInterface $builder, array $options): void
  49.     {
  50.         $validator = new CaptchaValidator(
  51.             $this->translator,
  52.             $this->session,
  53.             sprintf('%s%s'self::SESSION_KEY_PREFIX$options['session_key']),
  54.             $options['invalid_message'],
  55.             $options['bypass_code'],
  56.             $options['humanity']
  57.         );
  58.         $builder->addEventListener(FormEvents::POST_SUBMIT, array($validator'validate'));
  59.     }
  60.     /**
  61.      * @param FormView<mixed> $view
  62.      * @param FormInterface<mixed> $form
  63.      * @param array<mixed> $options
  64.      */
  65.     public function buildView(FormView $viewFormInterface $form, array $options): void
  66.     {
  67.         if ($options['reload'] && !$options['as_url']) {
  68.             throw new \InvalidArgumentException('GregwarCaptcha: The reload option cannot be set without as_url, see the README for more information');
  69.         }
  70.         $sessionKey sprintf('%s%s'self::SESSION_KEY_PREFIX$options['session_key']);
  71.         $isHuman false;
  72.         if ($options['humanity'] > 0) {
  73.             $humanityKey sprintf('%s_humanity'$sessionKey);
  74.             if ($this->session->get($humanityKey0) > 0) {
  75.                 $isHuman true;
  76.             }
  77.         }
  78.         if ($options['as_url']) {
  79.             $keys $this->session->get($options['whitelist_key'], array());
  80.             if (!in_array($sessionKey$keys)) {
  81.                 $keys[] = $sessionKey;
  82.             }
  83.             $this->session->set($options['whitelist_key'], $keys);
  84.             $options['session_key'] = $sessionKey;
  85.         }
  86.         $view->vars array_merge($view->vars, array(
  87.             'captcha_width' => $options['width'],
  88.             'captcha_height' => $options['height'],
  89.             'reload' => $options['reload'],
  90.             'image_id' => uniqid('captcha_'),
  91.             'captcha_code' => $this->generator->getCaptchaCode($options),
  92.             'value' => '',
  93.             'is_human' => $isHuman,
  94.         ));
  95.         $persistOptions = array();
  96.         foreach (array('phrase''width''height''distortion''length',
  97.         'quality''background_color''background_images''text_color', ) as $key) {
  98.             $persistOptions[$key] = $options[$key];
  99.         }
  100.         $this->session->set($sessionKey$persistOptions);
  101.     }
  102.     /**
  103.      * {@inheritdoc}
  104.      */
  105.     public function configureOptions(OptionsResolver $resolver): void
  106.     {
  107.         $this->options['mapped'] = false;
  108.         $resolver->setDefaults($this->options);
  109.     }
  110.     public function getParent(): string
  111.     {
  112.         return TextType::class;
  113.     }
  114.     public function getName(): string
  115.     {
  116.         return $this->getBlockPrefix();
  117.     }
  118.     public function getBlockPrefix(): string
  119.     {
  120.         return 'captcha';
  121.     }
  122. }