Add bypass_code configuration parameter to force captcha validation for development/automation.

This commit is contained in:
Jeremy Livingston
2012-06-30 12:18:05 -04:00
parent 9980108880
commit f9885acde9
4 changed files with 26 additions and 7 deletions

View File

@ -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));
}
}