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

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