CaptchaBundle/Type/CaptchaType.php

145 lines
3.9 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\OptionsResolverInterface;
use Symfony\Component\Form\FormEvents;
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 \Symfony\Component\HttpFoundation\Session\SessionInterface
*/
protected $session;
/**
* The session key
* @var string
*/
protected $key = null;
/**
* @var \Gregwar\CaptchaBundle\Generator\CaptchaGenerator
*/
protected $generator;
/**
* Options
* @var array
*/
private $options = array();
/**
* @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session
* @param \Gregwar\CaptchaBundle\Generator\CaptchaGenerator $generator
* @param array $options
*/
public function __construct(SessionInterface $session, CaptchaGenerator $generator, $options)
{
$this->session = $session;
$this->generator = $generator;
$this->options = $options;
}
/**
* @param \Symfony\Component\Form\FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$this->key = 'gcb_'.$builder->getForm()->getName();
2012-12-04 00:54:39 +01:00
$validator = new CaptchaValidator(
$this->session,
$this->key,
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
);
$builder->addEventListener(FormEvents::POST_BIND, array($validator, 'validate'));
}
/**
* @param \Symfony\Component\Form\FormView $view
* @param \Symfony\Component\Form\FormInterface $form
* @param array $options
*/
2012-07-23 23:02:37 +02:00
public function buildView(FormView $view, FormInterface $form, array $options)
{
2012-12-04 12:20:23 +01:00
$isHuman = false;
if ($options['humanity'] > 0) {
$humanityKey = $this->key.'_humanity';
if ($this->session->get($humanityKey, 0) > 0) {
$isHuman = true;
}
}
if ($options['as_url']) {
$key = $this->key;
$keys = $this->session->get($options['whitelist_key'], array());
if (!in_array($key, $keys)) {
$keys[] = $key;
}
$this->session->set($options['whitelist_key'], $keys);
$options['session_key'] = $this->key;
}
$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') as $key) {
$persistOptions[$key] = $options[$key];
}
$this->session->set($this->key, $persistOptions);
}
/**
* @param \Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
2013-02-13 18:29:40 +01:00
$this->options['mapped'] = false;
$resolver->setDefaults($this->options);
}
/**
* @return string
*/
public function getParent()
{
return 'text';
}
/**
* @return string
*/
public function getName()
{
return 'captcha';
}
}