Created captcha type (this works, but still no image generation)
This commit is contained in:
commit
4e1892fc02
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
namespace Gregwar\CaptchaBundle\DependencyInjection;
|
||||
|
||||
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Definition;
|
||||
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
|
||||
use Symfony\Component\Config\FileLocator;
|
||||
|
||||
class GregwarCaptchaExtension extends Extension
|
||||
{
|
||||
public function load(array $configs, ContainerBuilder $container)
|
||||
{
|
||||
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
|
||||
$loader->load('services.yml');
|
||||
|
||||
$resources = $container->getParameter('twig.form.resources');
|
||||
$resources[] = 'GregwarCaptchaBundle::captcha.html.twig';
|
||||
$container->setParameter('twig.form.resources', $resources);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
namespace Gregwar\CaptchaBundle;
|
||||
|
||||
use Symfony\Component\HttpKernel\Bundle\Bundle;
|
||||
|
||||
class GregwarCaptchaBundle extends Bundle
|
||||
{
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
|
||||
services:
|
||||
# captcha type
|
||||
captcha.type:
|
||||
class: Gregwar\CaptchaBundle\Type\CaptchaType
|
||||
arguments: [@session]
|
||||
tags:
|
||||
- { name: form.type, alias: captcha }
|
|
@ -0,0 +1,4 @@
|
|||
{% block captcha_widget %}
|
||||
[{{ captcha_code }}]<br />
|
||||
{{ form_widget(form) }}
|
||||
{% endblock %}
|
|
@ -0,0 +1,67 @@
|
|||
<?php
|
||||
|
||||
namespace Gregwar\CaptchaBundle\Type;
|
||||
|
||||
use Symfony\Component\Form\FormView;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilder;
|
||||
use Symfony\Bridge\Doctrine\RegistryInterface;
|
||||
use Symfony\Component\Form\Exception\FormException;
|
||||
use Symfony\Component\HttpFoundation\Session;
|
||||
|
||||
use Gregwar\CaptchaBundle\Validator\CaptchaValidator;
|
||||
|
||||
/**
|
||||
* Captcha type
|
||||
*
|
||||
* @author Gregwar <g.passault@gmail.com>
|
||||
*/
|
||||
class CaptchaType extends AbstractType
|
||||
{
|
||||
private $key = 'captcha';
|
||||
|
||||
protected $session;
|
||||
|
||||
public function __construct(Session $session)
|
||||
{
|
||||
$this->session = $session;
|
||||
}
|
||||
|
||||
public function buildForm(FormBuilder $builder, array $options)
|
||||
{
|
||||
$builder->addValidator(
|
||||
new CaptchaValidator($this->session, $this->key)
|
||||
);
|
||||
}
|
||||
|
||||
public function buildView(FormView $view, FormInterface $form)
|
||||
{
|
||||
$view->set('captcha_code', $this->generateCaptchaValue());
|
||||
}
|
||||
|
||||
public function getParent(array $options)
|
||||
{
|
||||
return 'text';
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return 'captcha';
|
||||
}
|
||||
|
||||
private function generateCaptchaValue()
|
||||
{
|
||||
$charset = 'abcdefghijklmnopqrstuvwxyz0123456789';
|
||||
$value = '';
|
||||
$chars = str_split($charset);
|
||||
|
||||
for ($i=0; $i<5; $i++) {
|
||||
$value.= $chars[array_rand($chars)];
|
||||
}
|
||||
|
||||
$this->session->set($this->key, $value);
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
<?php
|
||||
|
||||
namespace Gregwar\CaptchaBundle\Validator;
|
||||
|
||||
use Symfony\Component\Form\FormValidatorInterface;
|
||||
use Symfony\Component\HttpFoundation\Session;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\Form\FormError;
|
||||
|
||||
/**
|
||||
* Captcha validator
|
||||
*
|
||||
* @author Gregwar <g.passault@gmail.com>
|
||||
*/
|
||||
class CaptchaValidator implements FormValidatorInterface
|
||||
{
|
||||
/**
|
||||
* Session
|
||||
*/
|
||||
private $session;
|
||||
|
||||
/**
|
||||
* Session key to store the code
|
||||
*/
|
||||
private $key;
|
||||
|
||||
public function __construct(Session $session, $key)
|
||||
{
|
||||
$this->session = $session;
|
||||
$this->key = $key;
|
||||
}
|
||||
|
||||
public function validate(FormInterface $form)
|
||||
{
|
||||
$code = $form->getData();
|
||||
$excepted_code = $this->getExceptedCode();
|
||||
|
||||
if (!($code && $excepted_code && is_string($code) && is_string($excepted_code)
|
||||
&& $this->niceize($code) == $this->niceize($excepted_code))) {
|
||||
$form->addError(new FormError('Bad code value'));
|
||||
}
|
||||
|
||||
$this->session->remove($this->key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the excepted CAPTCHA code
|
||||
*/
|
||||
private function getExceptedCode()
|
||||
{
|
||||
if ($this->session->has($this->key)) {
|
||||
return $this->session->get($this->key);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the codes
|
||||
*/
|
||||
private function niceize($code)
|
||||
{
|
||||
return strtr($code, 'oil', '01l');
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue