2011-08-25 22:50:59 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Gregwar\CaptchaBundle\Type;
|
|
|
|
|
2012-04-26 04:54:51 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Session\Session;
|
2011-09-08 17:07:04 +02:00
|
|
|
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\Exception\FormException;
|
2012-06-19 21:14:52 +02:00
|
|
|
use Symfony\Component\Form\FormBuilderInterface;
|
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
|
|
|
|
use Symfony\Component\Form\FormViewInterface;
|
2011-08-25 22:50:59 +02:00
|
|
|
|
|
|
|
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();
|
2012-06-29 16:23:05 +02: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
|
|
|
}
|
|
|
|
|
2012-06-19 21:14:52 +02:00
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options)
|
2011-08-25 22:50:59 +02:00
|
|
|
{
|
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(
|
2012-06-30 18:18:05 +02:00
|
|
|
new CaptchaValidator($this->session,
|
|
|
|
$this->key,
|
|
|
|
$options['invalid_message'],
|
|
|
|
$options['bypass_code'])
|
2011-08-25 22:50:59 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-06-19 21:14:52 +02:00
|
|
|
public function buildView(FormViewInterface $view, FormInterface $form, array $options)
|
2011-08-25 22:50:59 +02:00
|
|
|
{
|
2011-12-02 19:26:31 +01:00
|
|
|
$fingerprint = null;
|
2011-12-14 10:28:21 +01:00
|
|
|
|
2012-06-19 21:14:52 +02:00
|
|
|
if ($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-06-29 16:23:05 +02:00
|
|
|
$generator = new CaptchaGenerator($this->generateCaptchaValue(),
|
|
|
|
$options['image_folder'],
|
|
|
|
$options['web_path'],
|
|
|
|
$options['gc_freq'],
|
|
|
|
$options['expiration'],
|
|
|
|
$options['font'],
|
|
|
|
$fingerprint,
|
2012-06-19 21:14:52 +02:00
|
|
|
$options['quality']);
|
2012-02-08 19:53:22 +01:00
|
|
|
|
2012-06-19 21:14:52 +02:00
|
|
|
if ($options['as_file']) {
|
|
|
|
$captchaCode = $generator->getFile($options['width'], $options['height']);
|
2011-11-09 14:43:25 +01:00
|
|
|
} else {
|
2012-06-19 21:14:52 +02:00
|
|
|
$captchaCode = $generator->getCode($options['width'], $options['height']);
|
2011-11-09 14:43:25 +01:00
|
|
|
}
|
2011-12-02 19:26:31 +01:00
|
|
|
|
2012-06-19 21:14:52 +02:00
|
|
|
if ($options['keep_value']) {
|
2011-12-02 19:26:31 +01:00
|
|
|
$this->session->set($this->key.'_fingerprint', $generator->getFingerprint());
|
|
|
|
}
|
2012-06-29 16:23:05 +02:00
|
|
|
|
2012-06-19 21:14:52 +02:00
|
|
|
$view->addVars(array(
|
|
|
|
'captcha_width' => $options['width'],
|
|
|
|
'captcha_height' => $options['height'],
|
|
|
|
'captcha_code' => $captchaCode,
|
|
|
|
'value' => '',
|
|
|
|
));
|
2011-09-09 19:46:12 +02:00
|
|
|
}
|
|
|
|
|
2012-06-19 21:14:52 +02:00
|
|
|
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
2011-09-09 19:46:12 +02:00
|
|
|
{
|
2012-06-29 16:23:05 +02:00
|
|
|
$this->options['property_path'] = false;
|
2012-06-19 21:14:52 +02:00
|
|
|
$resolver->setDefaults($this->options);
|
2011-09-09 19:46:12 +02:00
|
|
|
}
|
2011-08-25 22:50:59 +02:00
|
|
|
|
2012-06-19 21:14:52 +02:00
|
|
|
public function getParent()
|
2011-08-25 22:50:59 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2012-06-19 21:14:52 +02:00
|
|
|
}
|