CaptchaHelper.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. * @package tools plugin
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. /**
  16. * works togehter with captcha behaviour/component
  17. * 2011-06-11 ms
  18. */
  19. class CaptchaHelper extends AppHelper {
  20. public $helpers = array('Form');
  21. protected $defaults = array(
  22. 'difficulty' => 1, # initial diff. level (@see operator: + = 0, +- = 1, +-* = 2)
  23. 'raiseDifficulty' => 2, # number of failed trails, after the x. one the following one it will be more difficult
  24. );
  25. protected $settings = null;
  26. protected $numberConvert = null;
  27. protected $operatorConvert = null;
  28. public function __construct($View = null, $settings = array()) {
  29. parent::__construct($View, $settings);
  30. # First of all we are going to set up an array with the text equivalents of all the numbers we will be using.
  31. $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');
  32. # Set up an array with the operators that we want to use. With difficulty=1 it is only subtraction and addition.
  33. $this->operatorConvert = array(0=>array('+',__('calcPlus')), 1=>array('-',__('calcMinus')), 2=>'*',__('calcTimes'));
  34. $this->settings = array_merge(CaptchaLib::$defaults, $this->defaults);
  35. $settings = (array)Configure::read('Captcha');
  36. if (!empty($settings)) {
  37. $this->settings = array_merge($this->settings, $settings);
  38. }
  39. }
  40. /**
  41. * //TODO: move to Lib
  42. * shows the statusText of Relations
  43. * @param int $difficulty: not build in yet
  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. * 2009-12-18 ms
  66. */
  67. public function captcha($modelName = null) {
  68. $captchaCode = $this->_generate();
  69. # Session-Way (only one form at a time) - must be a component then
  70. //$this->Session->write('Captcha.result', $result);
  71. # DB-Way (several forms possible, high security via IP-Based max limits)
  72. // the following should be done in a component and passed to the view/helper
  73. // $Captcha = ClassRegistry::init('Captcha');
  74. // $this->Captcha->new(); $this->Captcha->update(); etc
  75. # Timestamp-SessionID-Hash-Way (several forms possible, not as secure)
  76. $hash = $this->_buildHash($captchaCode);
  77. $return = '';
  78. if (in_array($this->settings['type'], array('active', 'both'))) {
  79. # //todo obscure html here?
  80. $fill = ''; //'<span></span>';
  81. $return .= '<span id="captchaCode">'.$fill.''.$captchaCode['code'].'</span>';
  82. }
  83. $field = $this->_fieldName($modelName);
  84. # add passive part on active forms as well
  85. $return .= '<div style="display:none">'.
  86. $this->Form->input($field.'_hash', array('value'=>$hash)).
  87. $this->Form->input($field.'_time', array('value'=>time())).
  88. $this->Form->input((!empty($modelName)?$modelName.'.':'').$this->settings['dummyField'], array('value'=>'')).
  89. '</div>';
  90. return $return;
  91. }
  92. /**
  93. * active math captcha
  94. * either combined with between=true (all in this one funtion)
  95. * or seperated by =false (needs input(false) and captcha() calls then)
  96. * @param bool between: [default: true]
  97. * 2010-01-08 ms
  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. * 2010-01-08 ms
  124. */
  125. public function passive($modelName = null, $options = array()) {
  126. $tmp = $this->settings['type'];
  127. $this->settings['type'] = 'passive';
  128. $res = $this->captcha($modelName);
  129. $this->settings['type'] = $tmp;
  130. return $res;
  131. }
  132. /**
  133. * active captcha
  134. * (+ passive captcha right now)
  135. * 2010-01-08 ms
  136. */
  137. public function active($modelName = null, $options = array()) {
  138. return $this->input($modelName, $options);
  139. }
  140. /**
  141. * @param array $captchaCode
  142. */
  143. protected function _buildHash($data) {
  144. return CaptchaLib::buildHash($data, $this->settings, true);
  145. }
  146. protected function _fieldName($modelName = null) {
  147. $fieldName = 'captcha';
  148. if (isSet($modelName)) {
  149. $fieldName = $modelName.'.'.$fieldName;
  150. }
  151. return $fieldName;
  152. }
  153. }