From f9885acde90a64cef5c7cc98d19d4fa5a76ec71e Mon Sep 17 00:00:00 2001 From: Jeremy Livingston Date: Sat, 30 Jun 2012 12:18:05 -0400 Subject: [PATCH] Add bypass_code configuration parameter to force captcha validation for development/automation. --- DependencyInjection/Configuration.php | 1 + README.md | 1 + Type/CaptchaType.php | 5 ++++- Validator/CaptchaValidator.php | 26 ++++++++++++++++++++------ 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 1878e26..3b0da0d 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -32,6 +32,7 @@ class Configuration implements ConfigurationInterface ->scalarNode('expiration')->defaultValue(60)->end() ->scalarNode('quality')->defaultValue(15)->end() ->scalarNode('invalid_message')->defaultValue('Bad code value')->end() + ->scalarNode('bypass_code')->defaultValue(null)->end() ->end() ; return $treeBuilder; diff --git a/README.md b/README.md index f0fd05e..f739bd7 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,7 @@ You can define the following type option : * **gc_freq**: frequency of garbage collection in fractions of 1 (default=100) * **expiration**: maximum lifetime of captcha image files in minutes (default=60) * **invalid_message**: error message displayed when an non-matching code is submitted (default="Bad code value") +* **bypass_code**: code that will always validate the captcha (default=null) Example : diff --git a/Type/CaptchaType.php b/Type/CaptchaType.php index b768cb2..c138dbf 100644 --- a/Type/CaptchaType.php +++ b/Type/CaptchaType.php @@ -47,7 +47,10 @@ class CaptchaType extends AbstractType $this->key = $builder->getForm()->getName(); $builder->addValidator( - new CaptchaValidator($this->session, $this->key, $options['invalid_message']) + new CaptchaValidator($this->session, + $this->key, + $options['invalid_message'], + $options['bypass_code']) ); } diff --git a/Validator/CaptchaValidator.php b/Validator/CaptchaValidator.php index 5ab6cd0..d9a0fb1 100644 --- a/Validator/CaptchaValidator.php +++ b/Validator/CaptchaValidator.php @@ -29,20 +29,26 @@ class CaptchaValidator implements FormValidatorInterface */ private $invalidMessage; - public function __construct(Session $session, $key, $invalidMessage) + /** + * Configuration parameter used to bypass a required code match + */ + private $bypassCode; + + public function __construct(Session $session, $key, $invalidMessage, $bypassCode) { $this->session = $session; $this->key = $key; $this->invalidMessage = $invalidMessage; + $this->bypassCode = $bypassCode; } public function validate(FormInterface $form) { $code = $form->getData(); - $excepted_code = $this->getExceptedCode(); + $expectedCode = $this->getExpectedCode(); - if (!($code && $excepted_code && is_string($code) && is_string($excepted_code) - && $this->niceize($code) == $this->niceize($excepted_code))) { + if (!($code && is_string($code) + && ($this->compare($code, $expectedCode) || $this->compare($code, $this->bypassCode)))) { $form->addError(new FormError($this->invalidMessage)); } @@ -54,9 +60,9 @@ class CaptchaValidator implements FormValidatorInterface } /** - * Retrieve the excepted CAPTCHA code + * Retrieve the expected CAPTCHA code */ - private function getExceptedCode() + private function getExpectedCode() { if ($this->session->has($this->key)) { return $this->session->get($this->key); @@ -71,4 +77,12 @@ class CaptchaValidator implements FormValidatorInterface { return strtr(strtolower($code), 'oil', '01l'); } + + /** + * Run a match comparison on the provided code and the expected code + */ + private function compare($code, $expectedCode) + { + return ($expectedCode && is_string($expectedCode) && $this->niceize($code) == $this->niceize($expectedCode)); + } }