CaptchaBundle/Type/CaptchaType.php

158 lines
4.4 KiB
PHP
Raw Normal View History

<?php
namespace Gregwar\CaptchaBundle\Type;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Gregwar\CaptchaBundle\Validator\CaptchaValidator;
2011-08-25 23:10:24 +02:00
use Gregwar\CaptchaBundle\Generator\CaptchaGenerator;
/**
* Captcha type
*
* @author Gregwar <g.passault@gmail.com>
*/
class CaptchaType extends AbstractType
{
/**
* @var SessionInterface
*/
protected $session;
/**
* @var CaptchaGenerator
*/
protected $generator;
/**
* @var TranslatorInterface
*/
protected $translator;
/**
* Options
* @var array
*/
private $options = array();
/**
* @param SessionInterface $session
* @param CaptchaGenerator $generator
* @param TranslatorInterface $translator
* @param array $options
*/
public function __construct(SessionInterface $session, CaptchaGenerator $generator, TranslatorInterface $translator, $options)
{
$this->session = $session;
$this->generator = $generator;
$this->translator = $translator;
$this->options = $options;
}
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
2012-12-04 00:54:39 +01:00
$validator = new CaptchaValidator(
$this->translator,
2012-12-04 00:54:39 +01:00
$this->session,
2014-07-31 16:41:27 +02:00
sprintf('gcb_%s', $builder->getForm()->getName()),
2012-12-04 00:54:39 +01:00
$options['invalid_message'],
2012-12-04 12:20:23 +01:00
$options['bypass_code'],
$options['humanity']
2012-12-04 00:54:39 +01:00
);
$event = \Symfony\Component\HttpKernel\Kernel::VERSION >= 2.3 ? FormEvents::POST_SUBMIT : FormEvents::POST_BIND;
$builder->addEventListener($event, array($validator, 'validate'));
}
/**
* {@inheritdoc}
*/
2012-07-23 23:02:37 +02:00
public function buildView(FormView $view, FormInterface $form, array $options)
{
if ($options['reload'] && !$options['as_url']) {
throw new \InvalidArgumentException('GregwarCaptcha: The reload option cannot be set without as_url, see the README for more information');
}
2014-07-31 16:41:27 +02:00
$sessionKey = sprintf('gcb_%s', $form->getName());
2014-07-29 19:03:41 +02:00
$isHuman = false;
2014-07-21 14:46:14 +02:00
2012-12-04 12:20:23 +01:00
if ($options['humanity'] > 0) {
2014-07-29 19:03:41 +02:00
$humanityKey = sprintf('%s_humanity', $sessionKey);
2012-12-04 12:20:23 +01:00
if ($this->session->get($humanityKey, 0) > 0) {
$isHuman = true;
}
}
if ($options['as_url']) {
$keys = $this->session->get($options['whitelist_key'], array());
2014-07-29 19:03:41 +02:00
if (!in_array($sessionKey, $keys)) {
$keys[] = $sessionKey;
}
$this->session->set($options['whitelist_key'], $keys);
2014-07-29 19:03:41 +02:00
$options['session_key'] = $sessionKey;
}
$view->vars = array_merge($view->vars, array(
'captcha_width' => $options['width'],
'captcha_height' => $options['height'],
'reload' => $options['reload'],
2013-02-13 18:29:40 +01:00
'image_id' => uniqid('captcha_'),
'captcha_code' => $this->generator->getCaptchaCode($options),
'value' => '',
2012-12-04 12:20:23 +01:00
'is_human' => $isHuman
));
$persistOptions = array();
foreach (array('phrase', 'width', 'height', 'distortion', 'length', 'quality', 'background_color', 'text_color') as $key) {
$persistOptions[$key] = $options[$key];
}
2014-07-29 19:03:41 +02:00
$this->session->set($sessionKey, $persistOptions);
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
2013-02-13 18:29:40 +01:00
$this->options['mapped'] = false;
$resolver->setDefaults($this->options);
}
/**
* {@inheritdoc}
* BC for SF < 2.7
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$this->configureOptions($resolver);
}
/**
* @return string
*/
public function getParent()
{
return TextType::class;
}
/**
* @return string
*/
public function getName()
{
return 'captcha';
}
}