CaptchaHelper.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. if (!defined('BR')) {
  3. define('BR', '<br />');
  4. }
  5. /**
  6. * PHP5 / CakePHP2
  7. *
  8. * @author Mark Scherer
  9. * @link http://www.dereuromark.de
  10. * @package tools plugin
  11. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  12. */
  13. /**
  14. * works togehter with captcha behaviour/component
  15. * 2011-06-11 ms
  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 $settings = null;
  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. App::uses('CaptchaLib', 'Tools.Lib');
  33. $this->settings = array_merge(CaptchaLib::$defaults, $this->defaults);
  34. $settings = (array)Configure::read('Captcha');
  35. if (!empty($settings)) {
  36. $this->settings = array_merge($this->settings, $settings);
  37. }
  38. }
  39. /**
  40. * //TODO: move to Lib
  41. * shows the statusText of Relations
  42. * @param int $difficulty: not build in yet
  43. * 2008-12-12 ms
  44. */
  45. protected function _generate($difficulty = null) {
  46. # Choose the first number randomly between 6 and 10. This is to stop the answer being negative.
  47. $numberOne = mt_rand(6, 10);
  48. # Choose the second number randomly between 0 and 5.
  49. $numberTwo = mt_rand(0, 5);
  50. # Choose the operator randomly from the array.
  51. $captchaOperatorSelection = $this->operatorConvert[mt_rand(0, 1)];
  52. $captchaOperator = $captchaOperatorSelection[mt_rand(0, 1)];
  53. # Get the equation in textual form to show to the user.
  54. $code = (mt_rand(0, 1) == 1 ? __($this->numberConvert[$numberOne]) : $numberOne) . ' ' . $captchaOperator . ' ' . (mt_rand(0, 1) == 1 ? __($this->numberConvert[$numberTwo]) : $numberTwo);
  55. # Evaluate the equation and get the result.
  56. eval('$result = ' . $numberOne . ' ' . $captchaOperatorSelection[0] . ' ' . $numberTwo . ';');
  57. return array('code'=>$code, 'result'=>$result);
  58. }
  59. /**
  60. * main captcha output (usually called from $this->input() automatically)
  61. * - hash-based
  62. * - session-based (not impl.)
  63. * - db-based (not impl.)
  64. * 2009-12-18 ms
  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. * @param bool between: [default: true]
  96. * 2010-01-08 ms
  97. */
  98. public function input($modelName = null, $options = array()) {
  99. $defaultOptions = array(
  100. 'type'=>'text',
  101. 'class'=>'captcha',
  102. 'value'=>'',
  103. 'maxlength'=>3,
  104. 'label'=>__('Captcha').BR.__('captchaExplained'),
  105. 'combined'=>true,
  106. 'autocomplete'=>'off',
  107. //'after' => __('captchaTip'),
  108. );
  109. $options = array_merge($defaultOptions, $options);
  110. if ($options['combined'] === true) {
  111. $options['between'] = $this->captcha($modelName);
  112. if (in_array($this->settings['type'], array('active', 'both'))) {
  113. $options['between'] .= ' = ';
  114. }
  115. }
  116. unset($options['combined']);
  117. $field = $this->_fieldName($modelName);
  118. return $this->Form->input($field.'', $options); // TODO: rename: _code
  119. }
  120. /**
  121. * passive captcha
  122. * 2010-01-08 ms
  123. */
  124. public function passive($modelName = null, $options = array()) {
  125. return $this->captcha($modelName);
  126. }
  127. /**
  128. * active captcha
  129. * (+ passive captcha right now)
  130. * 2010-01-08 ms
  131. */
  132. public function active($modelName = null, $options = array()) {
  133. return $this->input($modelName, $options);
  134. }
  135. /**
  136. * @param array $captchaCode
  137. */
  138. protected function _buildHash($data) {
  139. return CaptchaLib::buildHash($data, $this->settings, true);
  140. }
  141. protected function _fieldName($modelName = null) {
  142. $fieldName = 'captcha';
  143. if (isSet($modelName)) {
  144. $fieldName = $modelName.'.'.$fieldName;
  145. }
  146. return $fieldName;
  147. }
  148. }