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
|
|
|
/**
|
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: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
|
|
|
|
*/
|
|
|
|
public $gcFreq;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maximum age of images in minutes
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public $expiration;
|
|
|
|
|
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'];
|
|
|
|
$this->asFile = $config['as_file'];
|
2011-12-02 18:41:16 +01:00
|
|
|
$this->charset = $config['charset'];
|
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-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-08-25 22:50:59 +02:00
|
|
|
$builder->addValidator(
|
|
|
|
new CaptchaValidator($this->session, $this->key)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function buildView(FormView $view, FormInterface $form)
|
|
|
|
{
|
2011-11-09 17:58:53 +01:00
|
|
|
$generator = new CaptchaGenerator($this->generateCaptchaValue(), $this->imageFolder, $this->webPath, $this->gcFreq, $this->expiration);
|
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-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-11-28 20:23:10 +01:00
|
|
|
if (!$this->session->has($this->key)) {
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|