From 4e1892fc021f9447bb125b4de24b5abfb72c1209 Mon Sep 17 00:00:00 2001 From: Gregwar Date: Thu, 25 Aug 2011 22:50:59 +0200 Subject: [PATCH] Created captcha type (this works, but still no image generation) --- .../GregwarCaptchaExtension.php | 23 +++++++ GregwarCaptchaBundle.php | 9 +++ Resources/config/services.yml | 8 +++ Resources/views/captcha.html.twig | 4 ++ Type/CaptchaType.php | 67 +++++++++++++++++++ Validator/CaptchaValidator.php | 64 ++++++++++++++++++ 6 files changed, 175 insertions(+) create mode 100755 DependencyInjection/GregwarCaptchaExtension.php create mode 100755 GregwarCaptchaBundle.php create mode 100755 Resources/config/services.yml create mode 100644 Resources/views/captcha.html.twig create mode 100755 Type/CaptchaType.php create mode 100644 Validator/CaptchaValidator.php 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'); + } +}