commit 4e1892fc021f9447bb125b4de24b5abfb72c1209 Author: Gregwar Date: Thu Aug 25 22:50:59 2011 +0200 Created captcha type (this works, but still no image generation) diff --git a/DependencyInjection/GregwarCaptchaExtension.php b/DependencyInjection/GregwarCaptchaExtension.php new file mode 100755 index 0000000..5292533 --- /dev/null +++ b/DependencyInjection/GregwarCaptchaExtension.php @@ -0,0 +1,23 @@ +load('services.yml'); + + $resources = $container->getParameter('twig.form.resources'); + $resources[] = 'GregwarCaptchaBundle::captcha.html.twig'; + $container->setParameter('twig.form.resources', $resources); + } +} + diff --git a/GregwarCaptchaBundle.php b/GregwarCaptchaBundle.php new file mode 100755 index 0000000..9ad5967 --- /dev/null +++ b/GregwarCaptchaBundle.php @@ -0,0 +1,9 @@ + + {{ form_widget(form) }} +{% endblock %} diff --git a/Type/CaptchaType.php b/Type/CaptchaType.php new file mode 100755 index 0000000..a5deca5 --- /dev/null +++ b/Type/CaptchaType.php @@ -0,0 +1,67 @@ + + */ +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; + } +} diff --git a/Validator/CaptchaValidator.php b/Validator/CaptchaValidator.php new file mode 100644 index 0000000..47945c1 --- /dev/null +++ b/Validator/CaptchaValidator.php @@ -0,0 +1,64 @@ + + */ +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'); + } +}