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-12-14 10:28:21 +01:00
|
|
|
use Gregwar\CaptchaBundle\DataTransformer\EmptyTransformer;
|
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
|
|
|
/**
|
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();
|
2011-11-09 14:43:25 +01:00
|
|
|
|
2011-12-02 18:36:34 +01:00
|
|
|
/**
|
|
|
|
* Session key
|
2011-12-02 18:41:16 +01:00
|
|
|
* @var string
|
2011-12-02 18:36:34 +01:00
|
|
|
*/
|
2011-09-08 17:07:04 +02:00
|
|
|
private $key = 'captcha';
|
|
|
|
|
2011-12-02 18:36:34 +01:00
|
|
|
public function __construct(Session $session, $config)
|
2011-08-25 22:50:59 +02:00
|
|
|
{
|
|
|
|
$this->session = $session;
|
2012-02-08 19:53:22 +01:00
|
|
|
$this->options = $config;
|
2011-08-25 22:50:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function buildForm(FormBuilder $builder, array $options)
|
|
|
|
{
|
2011-11-28 00:51:53 +01:00
|
|
|
$this->key = $builder->getForm()->getName();
|
2011-12-14 10:28:21 +01:00
|
|
|
|
2011-08-25 22:50:59 +02:00
|
|
|
$builder->addValidator(
|
|
|
|
new CaptchaValidator($this->session, $this->key)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function buildView(FormView $view, FormInterface $form)
|
|
|
|
{
|
2011-12-02 19:26:31 +01:00
|
|
|
$fingerprint = null;
|
2011-12-14 10:28:21 +01:00
|
|
|
|
2012-02-08 19:53:22 +01:00
|
|
|
if ($this->options['keep_value'] && $this->session->has($this->key.'_fingerprint')) {
|
2011-12-02 19:26:31 +01:00
|
|
|
$fingerprint = $this->session->get($this->key.'_fingerprint');
|
|
|
|
}
|
|
|
|
|
2012-02-08 19:53:22 +01:00
|
|
|
$generator = new CaptchaGenerator($this->generateCaptchaValue(),
|
|
|
|
$this->options['image_folder'],
|
|
|
|
$this->options['web_path'],
|
|
|
|
$this->options['gc_freq'],
|
|
|
|
$this->options['expiration'],
|
|
|
|
$this->options['font'],
|
|
|
|
$fingerprint,
|
|
|
|
$this->options['quality']);
|
|
|
|
|
|
|
|
if ($this->options['as_file']) {
|
|
|
|
$view->set('captcha_code', $generator->getFile($this->options['width'], $this->options['height']));
|
2011-11-09 14:43:25 +01:00
|
|
|
} else {
|
2012-02-08 19:53:22 +01:00
|
|
|
$view->set('captcha_code', $generator->getCode($this->options['width'], $this->options['height']));
|
2011-11-09 14:43:25 +01:00
|
|
|
}
|
2012-02-08 19:53:22 +01:00
|
|
|
$view->set('captcha_width', $this->options['width']);
|
|
|
|
$view->set('captcha_height', $this->options['height']);
|
2011-12-02 19:26:31 +01:00
|
|
|
|
2012-02-08 19:53:22 +01:00
|
|
|
if ($this->options['keep_value']) {
|
2011-12-02 19:26:31 +01:00
|
|
|
$this->session->set($this->key.'_fingerprint', $generator->getFingerprint());
|
|
|
|
}
|
2011-12-14 10:28:21 +01:00
|
|
|
|
|
|
|
$view->set('value', '');
|
2011-09-09 19:46:12 +02:00
|
|
|
}
|
|
|
|
|
2011-11-09 14:43:25 +01:00
|
|
|
public function getDefaultOptions(array $options = array())
|
2011-09-09 19:46:12 +02:00
|
|
|
{
|
2012-02-08 19:53:22 +01:00
|
|
|
$this->options = array_replace($this->options, $options);
|
|
|
|
$this->options['property_path'] = false;
|
2011-09-09 19:46:12 +02:00
|
|
|
|
2012-02-08 19:53:22 +01:00
|
|
|
return $this->options;
|
2011-09-09 19:46:12 +02:00
|
|
|
}
|
2011-08-25 22:50:59 +02:00
|
|
|
|
|
|
|
public function getParent(array $options)
|
|
|
|
{
|
|
|
|
return 'text';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return 'captcha';
|
|
|
|
}
|
|
|
|
|
|
|
|
private function generateCaptchaValue()
|
|
|
|
{
|
2012-02-08 19:53:22 +01:00
|
|
|
if (!$this->options['keep_value'] || !$this->session->has($this->key)) {
|
2011-11-28 20:23:10 +01:00
|
|
|
$value = '';
|
2012-02-08 19:53:22 +01:00
|
|
|
$chars = str_split($this->options['charset']);
|
2011-08-25 22:50:59 +02:00
|
|
|
|
2012-02-08 19:53:22 +01:00
|
|
|
for ($i=0; $i<$this->options['length']; $i++) {
|
2011-11-28 00:51:53 +01:00
|
|
|
$value.= $chars[array_rand($chars)];
|
|
|
|
}
|
2011-08-25 22:50:59 +02:00
|
|
|
|
2011-11-28 00:51:53 +01:00
|
|
|
$this->session->set($this->key, $value);
|
2011-11-28 20:23:10 +01:00
|
|
|
} else {
|
|
|
|
$value = $this->session->get($this->key);
|
2011-11-28 00:51:53 +01:00
|
|
|
}
|
2011-08-25 22:50:59 +02:00
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
}
|