From e92b657796342df7ade27e7fd8ffafac4cc11dac Mon Sep 17 00:00:00 2001 From: Wojciech Sznapka Date: Mon, 28 Nov 2011 00:51:53 +0100 Subject: [PATCH 1/3] Added ability to store captcha value in session between page redirects --- Type/CaptchaType.php | 17 ++++++++++------- Validator/CaptchaValidator.php | 4 ++-- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/Type/CaptchaType.php b/Type/CaptchaType.php index ffada26..0e720fd 100755 --- a/Type/CaptchaType.php +++ b/Type/CaptchaType.php @@ -94,6 +94,7 @@ class CaptchaType extends AbstractType public function buildForm(FormBuilder $builder, array $options) { + $this->key = $builder->getForm()->getName(); $builder->addValidator( new CaptchaValidator($this->session, $this->key) ); @@ -148,16 +149,18 @@ class CaptchaType extends AbstractType private function generateCaptchaValue() { - $charset = 'abcdefghijklmnopqrstuvwxyz0123456789'; - $value = ''; - $chars = str_split($charset); + $value = $this->session->get($this->key); + if (null == $value || 0 == strlen($value)) { + $charset = 'abcdefhjkmnprstuvwxyz23456789'; + $chars = str_split($charset); - for ($i=0; $i<$this->length; $i++) { - $value.= $chars[array_rand($chars)]; + for ($i=0; $i<$this->length; $i++) { + $value.= $chars[array_rand($chars)]; + } + + $this->session->set($this->key, $value); } - $this->session->set($this->key, $value); - return $value; } } diff --git a/Validator/CaptchaValidator.php b/Validator/CaptchaValidator.php index 47945c1..49707c9 100644 --- a/Validator/CaptchaValidator.php +++ b/Validator/CaptchaValidator.php @@ -38,9 +38,9 @@ class CaptchaValidator implements FormValidatorInterface 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')); + } else { + $this->session->remove($this->key); } - - $this->session->remove($this->key); } /** From 3ecbec4ae6e04919869b155bd99aada49736b374 Mon Sep 17 00:00:00 2001 From: Wojciech Sznapka Date: Mon, 28 Nov 2011 18:47:47 +0100 Subject: [PATCH 2/3] Changed session key checking --- Type/CaptchaType.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Type/CaptchaType.php b/Type/CaptchaType.php index 0e720fd..bf24419 100755 --- a/Type/CaptchaType.php +++ b/Type/CaptchaType.php @@ -149,8 +149,8 @@ class CaptchaType extends AbstractType private function generateCaptchaValue() { - $value = $this->session->get($this->key); - if (null == $value || 0 == strlen($value)) { + if ($this->session->has($this->key)) { + $value = $this->session->get($this->key); $charset = 'abcdefhjkmnprstuvwxyz23456789'; $chars = str_split($charset); From eb97a9c0af10d13165fc71f9a7d817bbba5540f8 Mon Sep 17 00:00:00 2001 From: Wojciech Sznapka Date: Mon, 28 Nov 2011 20:23:10 +0100 Subject: [PATCH 3/3] One more fix in generate method --- Type/CaptchaType.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Type/CaptchaType.php b/Type/CaptchaType.php index bf24419..0bea461 100755 --- a/Type/CaptchaType.php +++ b/Type/CaptchaType.php @@ -149,8 +149,8 @@ class CaptchaType extends AbstractType private function generateCaptchaValue() { - if ($this->session->has($this->key)) { - $value = $this->session->get($this->key); + if (!$this->session->has($this->key)) { + $value = ''; $charset = 'abcdefhjkmnprstuvwxyz23456789'; $chars = str_split($charset); @@ -159,6 +159,8 @@ class CaptchaType extends AbstractType } $this->session->set($this->key, $value); + } else { + $value = $this->session->get($this->key); } return $value;