CaptchaHelper.php 5.8 KB

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