1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
<?php
/**
* Class used for numeral captchas
*
* PHP version 5
*
* @category Text
* @package Text_CAPTCHA
* @author David Coallier <davidc@agoraproduction.com>
* @author Christian Wenz <wenz@php.net>
* @author Michael Cramer <michael@bigmichi1.de>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Text_CAPTCHA
*/
require_once 'Text/CAPTCHA/Driver/Base.php';
/**
* Class used for numeral captchas
*
* This class is intended to be used to generate numeral captchas as such as:
* Example:
* Give me the answer to "54 + 2" to prove that you are human.
*
* @category Text
* @package Text_CAPTCHA
* @author David Coallier <davidc@agoraproduction.com>
* @author Christian Wenz <wenz@php.net>
* @author Michael Cramer <michael@bigmichi1.de>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Text_CAPTCHA
*/
class Text_CAPTCHA_Driver_Numeral extends Text_CAPTCHA_Driver_Base
{
/**
* This variable holds the minimum range value default set to "1".
*
* @var integer $_minValue The minimum value of the number range.
*/
private $_minValue = 1;
/**
* This variable holds the maximum range value default set to "50".
*
* @var integer $_maxValue The maximum value of the number range.
*/
private $_maxValue = 50;
/**
* The valid operators to use in the numeral captcha. We could use / and * but
* not yet.
*
* @var array $_operators The operations for the captcha.
*/
private $_operators = array('-', '+');
/**
* This variable is basically the operation that we're going to be using in the
* numeral captcha we are about to generate.
*
* @var string $_operator The operation's operator to use.
*/
private $_operator = '';
/**
* This variable holds the first number of the numeral operation we are about to
* generate.
*
* @var integer $_firstNumber The first number of the operation.
*/
private $_firstNumber = 0;
/**
* This variable holds the value of the second variable of the operation we are
* about to generate for the captcha.
*
* @var integer $_secondNumber The second number of the operation.
*/
private $_secondNumber = 0;
/**
* Initialize numeric CAPTCHA.
*
* @param array $options CAPTCHA options with these keys:<br>
* minValue minimum value<br>
* maxValue maximum value
*
* @return void
*/
public function initDriver($options = array())
{
if (isset($options['minValue'])) {
$this->_minValue = (int)$options['minValue'];
} else {
$this->_minValue = 1;
}
if (isset($options['maxValue'])) {
$this->_maxValue = (int)$options['maxValue'];
} else {
$this->_maxValue = 50;
}
if (isset($options['operator'])) {
$this->_operator = $options['operator'];
} else {
$this->_operator = '';
}
if (isset($options['firstValue'])) {
$this->_firstNumber = (int)$options['firstValue'];
} else {
$this->_firstNumber = 0;
}
if (isset($options['secondValue'])) {
$this->_secondNumber = (int)$options['secondValue'];
} else {
$this->_secondNumber = 0;
}
}
/**
* Create the CAPTCHA (the numeral expression).
*
* This function determines a random numeral expression and set the associated
* class properties.
*
* @return void
* @see _generateFirstNumber()
* @see _generateSecondNumber()
* @see _generateOperator()
* @see _generateOperation()
*/
public function createCAPTCHA()
{
if ($this->_firstNumber == 0) {
$this->_firstNumber = $this->_generateNumber();
}
if ($this->_secondNumber == 0) {
$this->_secondNumber = $this->_generateNumber();
}
if (empty($this->_operator)) {
$this->_operator = $this->_operators[array_rand($this->_operators)];
}
$this->_generateOperation();
}
/**
* Set operation.
*
* This variable sets the operation variable by taking the firstNumber,
* secondNumber and operator.
*
* @return void
* @see _operation
* @see _firstNumber
* @see _operator
* @see _secondNumber
*/
private function _setOperation()
{
$this->setCaptcha(
$this->_firstNumber . ' ' . $this->_operator . ' ' . $this->_secondNumber
);
}
/**
* Generate a number.
*
* This function takes the parameters that are in the $this->_maxValue and
* $this->_minValue and get the random number from them using mt_rand().
*
* @return integer Random value between _minValue and _maxValue
* @see _minValue
* @see _maxValue
*/
private function _generateNumber()
{
return mt_rand($this->_minValue, $this->_maxValue);
}
/**
* Adds values.
*
* This function will add the firstNumber and the secondNumber value and then
* call setAnswer to set the answer value.
*
* @return void
* @see _firstNumber
* @see _secondNumber
* @see _setAnswer()
*/
private function _doAdd()
{
$phrase = $this->_firstNumber + $this->_secondNumber;
$this->setPhrase($phrase);
}
/**
* Does a subtract on the values.
*
* This function executes a subtraction on the firstNumber and the secondNumber
* to then call $this->setAnswer to set the answer value.
*
* If the _firstNumber value is smaller than the _secondNumber value then we
* regenerate the first number and regenerate the operation.
*
* @return void
* @see _firstNumber
* @see _secondNumber
* @see _setOperation()
* @see Text_CAPTCHA::setPhrase()
*/
private function _doSubtract()
{
$first = $this->_firstNumber;
$second = $this->_secondNumber;
/**
* Check if firstNumber is smaller than secondNumber
*/
if ($first < $second) {
$this->_firstNumber = $second;
$this->_secondNumber = $first;
$this->_setOperation();
}
$phrase = $this->_firstNumber - $this->_secondNumber;
$this->setPhrase($phrase);
}
/**
* Generate the operation
*
* This function will call the _setOperation() function to set the operation
* string that will be called to display the operation, and call the function
* necessary depending on which operation is set by this->operator.
*
* @return void
* @see _setOperation()
* @see _operator
* @see _doAdd()
* @see _doSubtract()
*/
private function _generateOperation()
{
$this->_setOperation();
switch ($this->_operator) {
case '+':
$this->_doAdd();
break;
case '-':
$this->_doSubtract();
break;
default:
$this->_operator = "+";
$this->_setOperation();
$this->_doAdd();
break;
}
}
/**
* Create random CAPTCHA phrase. This method is a placeholder, since the equation
* is created in createCAPTCHA()
*
* @return string
*/
public function createPhrase()
{
$this->setCaptcha(null);
}
}
|