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