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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Folder to save captcha images in,
|
|
|
|
* relative to public web folder
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $imageFolder;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Public web folder
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $webPath;
|
|
|
|
|
|
|
|
/**
|
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-09-08 17:07:04 +02:00
|
|
|
private $key = 'captcha';
|
|
|
|
|
2011-08-25 22:50:59 +02:00
|
|
|
|
2011-11-09 14:43:25 +01:00
|
|
|
public function __construct(Session $session, $width, $height, $length, $asFile, $imageFolder, $webPath)
|
2011-08-25 22:50:59 +02:00
|
|
|
{
|
|
|
|
$this->session = $session;
|
2011-09-09 19:46:12 +02:00
|
|
|
$this->width = $width;
|
|
|
|
$this->height = $height;
|
2011-09-09 20:02:47 +02:00
|
|
|
$this->length = $length;
|
2011-11-09 14:43:25 +01:00
|
|
|
$this->asFile = $asFile;
|
|
|
|
$this->imageFolder = $imageFolder;
|
|
|
|
$this->webPath = $webPath;
|
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-11-09 14:43:25 +01:00
|
|
|
$generator = new CaptchaGenerator($this->generateCaptchaValue(), $this->imageFolder, $this->webPath);
|
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()
|
|
|
|
{
|
|
|
|
$charset = 'abcdefghijklmnopqrstuvwxyz0123456789';
|
|
|
|
$value = '';
|
|
|
|
$chars = str_split($charset);
|
|
|
|
|
2011-09-09 20:02:47 +02:00
|
|
|
for ($i=0; $i<$this->length; $i++) {
|
2011-08-25 22:50:59 +02:00
|
|
|
$value.= $chars[array_rand($chars)];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->session->set($this->key, $value);
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
}
|