2011-08-25 22:50:59 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Gregwar\CaptchaBundle\Type;
|
|
|
|
|
2011-09-08 17:07:04 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Session;
|
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
|
|
|
2011-08-25 22:50:59 +02:00
|
|
|
use Symfony\Component\Form\FormView;
|
|
|
|
use Symfony\Component\Form\FormInterface;
|
|
|
|
use Symfony\Component\Form\AbstractType;
|
|
|
|
use Symfony\Component\Form\FormBuilder;
|
|
|
|
use Symfony\Component\Form\Exception\FormException;
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2011-09-08 17:07:04 +02:00
|
|
|
/**
|
|
|
|
* The image height
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
protected $height;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The image width
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
protected $width;
|
2011-08-25 22:50:59 +02:00
|
|
|
|
2011-09-08 17:07:04 +02:00
|
|
|
/**
|
|
|
|
* The session
|
|
|
|
* @var Symfony\Component\HttpFoundation\Session
|
|
|
|
*/
|
2011-08-25 22:50:59 +02:00
|
|
|
protected $session;
|
2011-09-08 17:07:04 +02:00
|
|
|
|
|
|
|
private $key = 'captcha';
|
|
|
|
|
2011-08-25 22:50:59 +02:00
|
|
|
|
2011-09-08 17:07:04 +02:00
|
|
|
public function __construct(Session $session, ContainerInterface $container)
|
2011-08-25 22:50:59 +02:00
|
|
|
{
|
|
|
|
$this->session = $session;
|
2011-09-08 17:07:04 +02:00
|
|
|
$this->height = $container->getParameter('gregwar_captcha.image.height');
|
|
|
|
$this->width = $container->getParameter('gregwar_captcha.image.width');
|
|
|
|
|
2011-08-25 22:50:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function buildForm(FormBuilder $builder, array $options)
|
|
|
|
{
|
|
|
|
$builder->addValidator(
|
|
|
|
new CaptchaValidator($this->session, $this->key)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function buildView(FormView $view, FormInterface $form)
|
|
|
|
{
|
2011-08-25 23:10:24 +02:00
|
|
|
$generator = new CaptchaGenerator($this->generateCaptchaValue());
|
|
|
|
|
2011-09-08 17:07:04 +02:00
|
|
|
$view->set('captcha_code', $generator->getCode($this->width, $this->height));
|
|
|
|
$view->set('captcha_width', $this->width);
|
|
|
|
$view->set('captcha_height', $this->height);
|
2011-08-25 22:50:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getParent(array $options)
|
|
|
|
{
|
|
|
|
return 'text';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return 'captcha';
|
|
|
|
}
|
|
|
|
|
|
|
|
private function generateCaptchaValue()
|
|
|
|
{
|
|
|
|
$charset = 'abcdefghijklmnopqrstuvwxyz0123456789';
|
|
|
|
$value = '';
|
|
|
|
$chars = str_split($charset);
|
|
|
|
|
|
|
|
for ($i=0; $i<5; $i++) {
|
|
|
|
$value.= $chars[array_rand($chars)];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->session->set($this->key, $value);
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
}
|