CaptchaHelper.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. App::uses('AppHelper', 'View/Helper');
  3. App::uses('CaptchaLib', 'Tools.Lib');
  4. if (!defined('BR')) {
  5. define('BR', '<br />');
  6. }
  7. /**
  8. * PHP5 / CakePHP2
  9. *
  10. * @author Mark Scherer
  11. * @link http://www.dereuromark.de
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. /**
  15. * Works togehter with captcha behaviour/component
  16. */
  17. class CaptchaHelper extends AppHelper {
  18. public $helpers = array('Form');
  19. protected $_defaults = array(
  20. 'difficulty' => 1, # initial diff. level (@see operator: + = 0, +- = 1, +-* = 2)
  21. 'raiseDifficulty' => 2, # number of failed trails, after the x. one the following one it will be more difficult
  22. );
  23. protected $numberConvert = null;
  24. protected $operatorConvert = null;
  25. public function __construct($View = null, $settings = array()) {
  26. parent::__construct($View, $settings);
  27. // First of all we are going to set up an array with the text equivalents of all the numbers we will be using.
  28. $this->numberConvert = array(0 => 'zero', 1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five', 6 => 'six', 7 => 'seven', 8 => 'eight', 9 => 'nine', 10 => 'ten');
  29. // Set up an array with the operators that we want to use. With difficulty=1 it is only subtraction and addition.
  30. $this->operatorConvert = array(0 => array('+', __('calcPlus')), 1 => array('-', __('calcMinus')), 2 => '*', __('calcTimes'));
  31. $this->settings = array_merge(CaptchaLib::$defaults, $this->_defaults);
  32. $settings = (array)Configure::read('Captcha');
  33. if (!empty($settings)) {
  34. $this->settings = array_merge($this->settings, $settings);
  35. }
  36. }
  37. /**
  38. * //TODO: move to Lib
  39. * shows the statusText of Relations
  40. *
  41. * @param int $difficulty: not build in yet
  42. * @return array
  43. */
  44. protected function _generate($difficulty = null) {
  45. // Choose the first number randomly between 6 and 10. This is to stop the answer being negative.
  46. $numberOne = mt_rand(6, 10);
  47. // Choose the second number randomly between 0 and 5.
  48. $numberTwo = mt_rand(0, 5);
  49. // Choose the operator randomly from the array.
  50. $captchaOperatorSelection = $this->operatorConvert[mt_rand(0, 1)];
  51. $captchaOperator = $captchaOperatorSelection[mt_rand(0, 1)];
  52. // Get the equation in textual form to show to the user.
  53. $code = (mt_rand(0, 1) == 1 ? __($this->numberConvert[$numberOne]) : $numberOne) . ' ' . $captchaOperator . ' ' . (mt_rand(0, 1) == 1 ? __($this->numberConvert[$numberTwo]) : $numberTwo);
  54. // Evaluate the equation and get the result.
  55. eval('$result = ' . $numberOne . ' ' . $captchaOperatorSelection[0] . ' ' . $numberTwo . ';');
  56. return array('code' => $code, 'result' => $result);
  57. }
  58. /**
  59. * Main captcha output (usually called from $this->input() automatically)
  60. * - hash-based
  61. * - session-based (not impl.)
  62. * - db-based (not impl.)
  63. *
  64. * @return string HTML
  65. */
  66. public function captcha($modelName = null) {
  67. $captchaCode = $this->_generate();
  68. // Session-Way (only one form at a time) - must be a component then
  69. //$this->Session->write('Captcha.result', $result);
  70. // DB-Way (several forms possible, high security via IP-Based max limits)
  71. // the following should be done in a component and passed to the view/helper
  72. // $Captcha = ClassRegistry::init('Captcha');
  73. // $this->Captcha->new(); $this->Captcha->update(); etc
  74. // Timestamp-SessionID-Hash-Way (several forms possible, not as secure)
  75. $hash = $this->_buildHash($captchaCode);
  76. $return = '';
  77. if (in_array($this->settings['type'], array('active', 'both'))) {
  78. // //todo obscure html here?
  79. $fill = ''; //'<span></span>';
  80. $return .= '<span id="captchaCode">' . $fill . '' . $captchaCode['code'] . '</span>';
  81. }
  82. $field = $this->_fieldName($modelName);
  83. // add passive part on active forms as well
  84. $return .= '<div style="display:none">' .
  85. $this->Form->input($field . '_hash', array('value' => $hash)) .
  86. $this->Form->input($field . '_time', array('value' => time())) .
  87. $this->Form->input((!empty($modelName) ? $modelName . '.' : '') . $this->settings['dummyField'], array('value' => '')) .
  88. '</div>';
  89. return $return;
  90. }
  91. /**
  92. * Active math captcha
  93. * either combined with between=true (all in this one funtion)
  94. * or seperated by =false (needs input(false) and captcha() calls then)
  95. *
  96. * @param bool between: [default: true]
  97. * @return string HTML
  98. */
  99. public function input($modelName = null, $options = array()) {
  100. $defaultOptions = array(
  101. 'type' => 'text',
  102. 'class' => 'captcha',
  103. 'value' => '',
  104. 'maxlength' => 3,
  105. 'label' => __('Captcha') . BR . __('captchaExplained'),
  106. 'combined' => true,
  107. 'autocomplete' => 'off',
  108. 'after' => __('captchaTip'),
  109. );
  110. $options = array_merge($defaultOptions, $options);
  111. if ($options['combined'] === true) {
  112. $options['between'] = $this->captcha($modelName);
  113. if (in_array($this->settings['type'], array('active', 'both'))) {
  114. $options['between'] .= ' = ';
  115. }
  116. }
  117. unset($options['combined']);
  118. $field = $this->_fieldName($modelName);
  119. return $this->Form->input($field . '', $options); // TODO: rename: _code
  120. }
  121. /**
  122. * Passive captcha
  123. *
  124. * @return string HTML
  125. */
  126. public function passive($modelName = null, $options = array()) {
  127. $tmp = $this->settings['type'];
  128. $this->settings['type'] = 'passive';
  129. $res = $this->captcha($modelName);
  130. $this->settings['type'] = $tmp;
  131. return $res;
  132. }
  133. /**
  134. * Active captcha
  135. * (+ passive captcha right now)
  136. *
  137. * @return string Form input
  138. */
  139. public function active($modelName = null, $options = array()) {
  140. return $this->input($modelName, $options);
  141. }
  142. /**
  143. * @param array $captchaCode
  144. * @return string Hash
  145. */
  146. protected function _buildHash($data) {
  147. return CaptchaLib::buildHash($data, $this->settings, true);
  148. }
  149. /**
  150. * @return string Field name
  151. */
  152. protected function _fieldName($modelName = null) {
  153. $fieldName = 'captcha';
  154. if (isSet($modelName)) {
  155. $fieldName = $modelName . '.' . $fieldName;
  156. }
  157. return $fieldName;
  158. }
  159. }