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

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