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
|
|
|
/**
|
2011-09-09 19:46:12 +02:00
|
|
|
* The image width
|
2011-09-08 17:07:04 +02:00
|
|
|
* @var integer
|
|
|
|
*/
|
2011-09-09 19:46:12 +02:00
|
|
|
protected $width;
|
|
|
|
|
2011-09-08 17:07:04 +02:00
|
|
|
/**
|
2011-09-09 19:46:12 +02:00
|
|
|
* The image height
|
2011-09-08 17:07:04 +02:00
|
|
|
* @var integer
|
|
|
|
*/
|
2011-09-09 19:46:12 +02:00
|
|
|
protected $height;
|
2011-08-25 22:50:59 +02:00
|
|
|
|
2011-09-09 20:02:47 +02:00
|
|
|
/**
|
|
|
|
* The code length
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
protected $length;
|
|
|
|
|
2011-11-09 14:43:25 +01:00
|
|
|
/**
|
|
|
|
* Generate image or data
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $asFile;
|
|
|
|
|
2011-12-02 18:54:42 +01:00
|
|
|
/**
|
|
|
|
* Keep value between two requests ?
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $keepValue;
|
|
|
|
|
2011-12-02 18:41:16 +01:00
|
|
|
/**
|
|
|
|
* Charset used
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $charset;
|
|
|
|
|
2011-11-09 14:43:25 +01:00
|
|
|
/**
|
|
|
|
* Folder to save captcha images in,
|
|
|
|
* relative to public web folder
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $imageFolder;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Public web folder
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $webPath;
|
|
|
|
|
2011-11-09 17:58:53 +01:00
|
|
|
/**
|
|
|
|
* Frequence of garbage collection in fractions of 1
|
|
|
|
* @var int
|
|
|
|
*/
|
2011-12-02 19:07:28 +01:00
|
|
|
protected $gcFreq;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Captcha font
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $font;
|
2011-11-09 17:58:53 +01:00
|
|
|
|
2011-12-14 10:35:10 +01:00
|
|
|
/**
|
|
|
|
* Captcha quality
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $quality;
|
|
|
|
|
2011-11-09 17:58:53 +01:00
|
|
|
/**
|
|
|
|
* Maximum age of images in minutes
|
|
|
|
* @var int
|
|
|
|
*/
|
2011-12-02 19:07:28 +01:00
|
|
|
protected $expiration;
|
2011-11-09 17:58:53 +01:00
|
|
|
|
2011-11-09 14:43:25 +01: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-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-08-25 22:50:59 +02:00
|
|
|
|
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;
|
2011-12-02 18:36:34 +01:00
|
|
|
|
|
|
|
$this->width = $config['width'];
|
|
|
|
$this->height = $config['height'];
|
|
|
|
$this->length = $config['length'];
|
2011-12-02 18:41:16 +01:00
|
|
|
$this->charset = $config['charset'];
|
2011-12-02 18:54:42 +01:00
|
|
|
$this->keepValue = $config['keep_value'];
|
|
|
|
$this->asFile = $config['as_file'];
|
2011-12-02 18:36:34 +01:00
|
|
|
$this->imageFolder = $config['image_folder'];
|
|
|
|
$this->webPath = $config['web_path'];
|
|
|
|
$this->gcFreq = $config['gc_freq'];
|
|
|
|
$this->expiration = $config['expiration'];
|
2011-12-02 19:07:28 +01:00
|
|
|
$this->font = $config['font'];
|
2011-12-14 10:35:10 +01:00
|
|
|
$this->quality = $config['quality'];
|
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
|
|
|
|
2011-12-02 19:26:31 +01:00
|
|
|
if ($this->session->has($this->key.'_fingerprint')) {
|
|
|
|
$fingerprint = $this->session->get($this->key.'_fingerprint');
|
|
|
|
}
|
|
|
|
|
2011-12-14 10:35:10 +01:00
|
|
|
$generator = new CaptchaGenerator($this->generateCaptchaValue(), $this->imageFolder, $this->webPath, $this->gcFreq, $this->expiration, $this->font, $fingerprint, $this->quality);
|
2011-08-25 23:10:24 +02:00
|
|
|
|
2011-11-09 14:43:25 +01:00
|
|
|
if ($this->asFile) {
|
|
|
|
$view->set('captcha_code', $generator->getFile($this->width, $this->height));
|
|
|
|
} else {
|
|
|
|
$view->set('captcha_code', $generator->getCode($this->width, $this->height));
|
|
|
|
}
|
2011-09-08 17:07:04 +02:00
|
|
|
$view->set('captcha_width', $this->width);
|
|
|
|
$view->set('captcha_height', $this->height);
|
2011-12-02 19:26:31 +01:00
|
|
|
|
|
|
|
if ($this->keepValue) {
|
|
|
|
$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
|
|
|
{
|
|
|
|
if (isset($options['width'])) {
|
|
|
|
$this->width = $options['width'];
|
2011-09-09 20:02:47 +02:00
|
|
|
}
|
|
|
|
if (isset($options['height'])) {
|
2011-09-09 19:46:12 +02:00
|
|
|
$this->height = $options['height'];
|
|
|
|
}
|
2011-09-09 20:02:47 +02:00
|
|
|
if (isset($options['length'])) {
|
|
|
|
$this->length = $options['length'];
|
|
|
|
}
|
2011-11-09 14:43:25 +01:00
|
|
|
if (isset($options['as_file'])) {
|
|
|
|
$this->asFile = $options['as_file'];
|
|
|
|
}
|
2011-09-09 19:46:12 +02:00
|
|
|
|
|
|
|
return array(
|
|
|
|
'width' => $this->width,
|
2011-09-09 20:02:47 +02:00
|
|
|
'height' => $this->height,
|
2011-09-11 21:11:13 +02:00
|
|
|
'length' => $this->length,
|
2011-11-09 14:43:25 +01:00
|
|
|
'as_file' => $this->asFile,
|
2011-09-11 21:11:13 +02:00
|
|
|
'property_path' => false,
|
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()
|
|
|
|
{
|
2011-12-02 18:54:42 +01:00
|
|
|
if (!$this->keepValue || !$this->session->has($this->key)) {
|
2011-11-28 20:23:10 +01:00
|
|
|
$value = '';
|
2011-12-02 18:41:16 +01:00
|
|
|
$chars = str_split($this->charset);
|
2011-08-25 22:50:59 +02:00
|
|
|
|
2011-11-28 00:51:53 +01:00
|
|
|
for ($i=0; $i<$this->length; $i++) {
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|