Numeral.php 7.5 KB

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