ZF-9557: Cannot turn off "use numbers" feature for captcha words (bug fix attached)
Description
The {{$_useNumbers}} property of the {{Zend_Captcha_Word}} class cannot be disabled (set to false) because there are no getter or setter methods like {{setUseNumber()}} or {{getUseNumber()}}.
The Zend_Captcha_Word class contains a protected property called {{$_useNumbers}} (line {{81}}) and this property is used to determine whether or not to generate a word that is completely alphabetic with letters only or alphanumeric with letters and numbers (lines {{304}} and {{305}}). There is, however, no method for setting - or getting - this value and because it is a private property it cannot be set manually by the user of the class.
The {{$_useNumbers}} property has a default value of true and the property is tied into the word generation code, so is used for the word-type (alpha vs alphanumeric) decision process already, but the behavior cannot be changed because there is no way to set the property, so the {{Zend_Captcha}} classes are limited to alphanumeric words only - i.e. you cannot generate captchas with words that use letters only if you are using any of the {{Zend_Captcha}} components.
The fix for this is very simple, and until the ZF team implements this fix, users can simply extend the captcha class of their choice, as illustrated below, with the proper getter and setter methods and use that instead. This works successfully for me, but it would be nice to see this fixed in future versions so this workaround, and the extra time it takes, is not required. Perhaps the dev team thought it better not to allow people to use numberless captchas unless they knew what they were doing :)
The Fix: add these two methods to the {{Zend_Captcha_Word}} class. The updated Zend_Captcha_Word class file is already attached to the bug.
public function getUseNumbers()
{
return $this->_useNumbers;
}
public function setUseNumbers($useNumbers)
{
$this->_useNumbers = $useNumbers;
return $this;
}
The Temporary Solution: extend your own captcha class from the appropriate desired base class and add the above two methods
<?php
require_once 'Zend/Captcha/Image.php';
class My_Captcha_Image extends Zend_Captcha_Image
{
public function getUseNumbers()
{
return $this->_useNumbers;
}
public function setUseNumbers($useNumbers)
{
$this->_useNumbers = $useNumbers;
return $this;
}
}
?>
Comments
Posted by Vaughn Draughon (vfdev) on 2010-03-28T09:23:10.000+0000
Here is the updated/fixed Zend_Captcha_Class
Posted by Vaughn Draughon (vfdev) on 2010-03-28T09:26:23.000+0000
Attached the patched class file.
Posted by Peter Urda (purda) on 2010-04-06T06:36:29.000+0000
An easy fix, I was able to add this to my Zend library locally and it work perfectly. Should be an easy commit to trunk by someone with write access.
Posted by Ben Scholzen (dasprid) on 2010-04-06T06:59:28.000+0000
Could you please provide a .diff patch and unit tests for the setter/getter? I can apply it to the trunk then.
Posted by Stanislav Malyshev (stas) on 2010-04-07T17:52:03.000+0000
fixed, added getter/setter in trunk