2011-08-25 22:50:59 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Gregwar\CaptchaBundle\Type;
|
|
|
|
|
2012-11-14 04:33:36 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
2011-08-25 22:50:59 +02:00
|
|
|
use Symfony\Component\Form\FormView;
|
|
|
|
use Symfony\Component\Form\FormInterface;
|
|
|
|
use Symfony\Component\Form\AbstractType;
|
2012-06-19 21:14:52 +02:00
|
|
|
use Symfony\Component\Form\FormBuilderInterface;
|
2015-07-09 18:19:21 +02:00
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
2012-06-19 21:14:52 +02:00
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
|
2012-11-14 04:33:36 +01:00
|
|
|
use Symfony\Component\Form\FormEvents;
|
2013-03-03 13:54:31 +01:00
|
|
|
use Symfony\Component\Translation\TranslatorInterface;
|
2011-08-25 22:50:59 +02:00
|
|
|
|
|
|
|
use Gregwar\CaptchaBundle\Validator\CaptchaValidator;
|
2011-08-25 23:10:24 +02:00
|
|
|
use Gregwar\CaptchaBundle\Generator\CaptchaGenerator;
|
2011-08-25 22:50:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Captcha type
|
|
|
|
*
|
|
|
|
* @author Gregwar <g.passault@gmail.com>
|
|
|
|
*/
|
|
|
|
class CaptchaType extends AbstractType
|
|
|
|
{
|
2012-11-14 04:33:36 +01:00
|
|
|
/**
|
2014-07-21 14:33:31 +02:00
|
|
|
* @var SessionInterface
|
2012-11-14 04:33:36 +01:00
|
|
|
*/
|
|
|
|
protected $session;
|
|
|
|
|
|
|
|
/**
|
2014-07-21 14:33:31 +02:00
|
|
|
* @var CaptchaGenerator
|
2012-11-14 04:33:36 +01:00
|
|
|
*/
|
|
|
|
protected $generator;
|
|
|
|
|
2013-03-03 13:54:31 +01:00
|
|
|
/**
|
|
|
|
* @var TranslatorInterface
|
|
|
|
*/
|
|
|
|
protected $translator;
|
|
|
|
|
2011-09-08 17:07:04 +02:00
|
|
|
/**
|
2012-02-08 19:53:22 +01:00
|
|
|
* Options
|
|
|
|
* @var array
|
2011-09-08 17:07:04 +02:00
|
|
|
*/
|
2012-02-08 19:53:22 +01:00
|
|
|
private $options = array();
|
2012-06-29 16:23:05 +02:00
|
|
|
|
2012-11-14 04:33:36 +01:00
|
|
|
/**
|
2014-07-21 14:33:31 +02:00
|
|
|
* @param SessionInterface $session
|
|
|
|
* @param CaptchaGenerator $generator
|
|
|
|
* @param TranslatorInterface $translator
|
|
|
|
* @param array $options
|
2012-11-14 04:33:36 +01:00
|
|
|
*/
|
2013-03-03 13:54:31 +01:00
|
|
|
public function __construct(SessionInterface $session, CaptchaGenerator $generator, TranslatorInterface $translator, $options)
|
2011-08-25 22:50:59 +02:00
|
|
|
{
|
2012-11-14 04:33:36 +01:00
|
|
|
$this->session = $session;
|
|
|
|
$this->generator = $generator;
|
2013-03-03 13:54:31 +01:00
|
|
|
$this->translator = $translator;
|
2012-11-14 04:33:36 +01:00
|
|
|
$this->options = $options;
|
2011-08-25 22:50:59 +02:00
|
|
|
}
|
|
|
|
|
2012-11-14 04:33:36 +01:00
|
|
|
/**
|
2014-07-21 14:33:31 +02:00
|
|
|
* {@inheritdoc}
|
2012-11-14 04:33:36 +01:00
|
|
|
*/
|
2012-06-19 21:14:52 +02:00
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options)
|
2011-08-25 22:50:59 +02:00
|
|
|
{
|
2012-12-04 00:54:39 +01:00
|
|
|
$validator = new CaptchaValidator(
|
2013-03-03 13:54:31 +01:00
|
|
|
$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
|
|
|
);
|
2015-08-26 10:26:01 +02:00
|
|
|
$event = \Symfony\Component\HttpKernel\Kernel::VERSION >= 2.3 ? FormEvents::POST_SUBMIT : FormEvents::POST_BIND;
|
|
|
|
$builder->addEventListener($event, array($validator, 'validate'));
|
2011-08-25 22:50:59 +02:00
|
|
|
}
|
|
|
|
|
2012-11-14 04:33:36 +01:00
|
|
|
/**
|
2014-07-21 14:33:31 +02:00
|
|
|
* {@inheritdoc}
|
2012-11-14 04:33:36 +01:00
|
|
|
*/
|
2012-07-23 23:02:37 +02:00
|
|
|
public function buildView(FormView $view, FormInterface $form, array $options)
|
2011-08-25 22:50:59 +02:00
|
|
|
{
|
2013-08-07 10:56:49 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-21 15:18:36 +01:00
|
|
|
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;
|
2013-01-21 15:18:36 +01:00
|
|
|
}
|
|
|
|
$this->session->set($options['whitelist_key'], $keys);
|
2014-07-29 19:03:41 +02:00
|
|
|
$options['session_key'] = $sessionKey;
|
2013-01-21 15:18:36 +01:00
|
|
|
}
|
|
|
|
|
2012-11-14 04:33:36 +01:00
|
|
|
$view->vars = array_merge($view->vars, array(
|
2012-06-19 21:14:52 +02:00
|
|
|
'captcha_width' => $options['width'],
|
|
|
|
'captcha_height' => $options['height'],
|
2012-12-25 20:07:00 +01:00
|
|
|
'reload' => $options['reload'],
|
2013-02-13 18:29:40 +01:00
|
|
|
'image_id' => uniqid('captcha_'),
|
2013-01-21 15:18:36 +01:00
|
|
|
'captcha_code' => $this->generator->getCaptchaCode($options),
|
2012-06-19 21:14:52 +02:00
|
|
|
'value' => '',
|
2012-12-04 12:20:23 +01:00
|
|
|
'is_human' => $isHuman
|
2012-11-14 04:33:36 +01:00
|
|
|
));
|
2013-01-21 15:18:36 +01:00
|
|
|
|
|
|
|
$persistOptions = array();
|
2014-05-07 17:18:57 +02:00
|
|
|
foreach (array('phrase', 'width', 'height', 'distortion', 'length', 'quality', 'background_color', 'text_color') as $key) {
|
2013-01-21 15:18:36 +01:00
|
|
|
$persistOptions[$key] = $options[$key];
|
|
|
|
}
|
|
|
|
|
2014-07-29 19:03:41 +02:00
|
|
|
$this->session->set($sessionKey, $persistOptions);
|
2011-09-09 19:46:12 +02:00
|
|
|
}
|
|
|
|
|
2012-11-14 04:33:36 +01:00
|
|
|
/**
|
2014-07-21 14:33:31 +02:00
|
|
|
* {@inheritdoc}
|
2012-11-14 04:33:36 +01:00
|
|
|
*/
|
2015-07-09 18:19:21 +02:00
|
|
|
public function configureOptions(OptionsResolver $resolver)
|
2011-09-09 19:46:12 +02:00
|
|
|
{
|
2013-02-13 18:29:40 +01:00
|
|
|
$this->options['mapped'] = false;
|
2012-06-19 21:14:52 +02:00
|
|
|
$resolver->setDefaults($this->options);
|
2011-09-09 19:46:12 +02:00
|
|
|
}
|
2011-08-25 22:50:59 +02:00
|
|
|
|
2015-07-09 18:19:21 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
* BC for SF < 2.7
|
|
|
|
*/
|
|
|
|
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
|
|
|
{
|
|
|
|
$this->configureOptions($resolver);
|
|
|
|
}
|
|
|
|
|
2012-11-14 04:33:36 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-06-19 21:14:52 +02:00
|
|
|
public function getParent()
|
2011-08-25 22:50:59 +02:00
|
|
|
{
|
2015-12-13 12:10:14 +01:00
|
|
|
// Not using ::class to support Symfony 2.8 w/ php<5.5
|
|
|
|
return 'Symfony\Component\Form\Extension\Core\Type\TextType';
|
2011-08-25 22:50:59 +02:00
|
|
|
}
|
|
|
|
|
2012-11-14 04:33:36 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2011-08-25 22:50:59 +02:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return 'captcha';
|
|
|
|
}
|
2012-12-04 11:33:54 +01:00
|
|
|
}
|