/include/Jaws/Captcha/Math.php

https://github.com/jaws-project/jaws · PHP · 125 lines · 68 code · 14 blank · 43 comment · 7 complexity · b3e969360ac59eca6fffcab4c33d750f MD5 · raw file

  1. <?php
  2. /**
  3. * Math captcha
  4. *
  5. * @category Captcha
  6. * @package Core
  7. * @author Pablo Fischer <pablo@pablo.com.mx>
  8. * @author Ali Fazelzadeh <afz@php.net>
  9. * @copyright 2006-2022 Jaws Development Group
  10. * @license http://www.gnu.org/copyleft/lesser.html
  11. */
  12. class Jaws_Captcha_Math extends Jaws_Captcha
  13. {
  14. /**
  15. * Captcha entry label
  16. *
  17. * @var string
  18. * @access private
  19. */
  20. var $_label = 'GLOBAL_CAPTCHA_QUESTION';
  21. /**
  22. * Captcha entry description
  23. *
  24. * @var string
  25. * @access private
  26. */
  27. var $_description = 'GLOBAL_CAPTCHA_QUESTION_DESC';
  28. /**
  29. * Install captcha driver
  30. *
  31. * @access public
  32. * @return mixed True on success otherwise Jaws_Error on failure
  33. */
  34. function install()
  35. {
  36. if (is_null($this->app->registry->fetch('captcha_math_offset', 'Policy'))) {
  37. $this->app->registry->insert('captcha_math_offset', 48, false, 'Policy');
  38. }
  39. return true;
  40. }
  41. /**
  42. * Generate a random mathematics equation
  43. *
  44. * @access private
  45. * @return string random mathematics equation
  46. */
  47. function randomEquation()
  48. {
  49. $fnum = mt_rand(1, 9);
  50. $snum = mt_rand(1, 9);
  51. $oprt = mt_rand(0, 2);
  52. $utf8_offset = $this->app->registry->fetch('captcha_math_offset', 'Policy');
  53. $operations = array(
  54. 0 => '+',
  55. 1 => '-',
  56. 2 => '*'
  57. );
  58. switch ($oprt) {
  59. case 0:
  60. $result = $fnum + $snum;
  61. break;
  62. case 1:
  63. // first & second numbers must different
  64. while ($fnum == $snum) {
  65. $snum = mt_rand(1, 9);
  66. }
  67. // exchange value of variables
  68. if ($fnum < $snum) {
  69. list($fnum, $snum) = array($snum, $fnum);
  70. }
  71. $result = $fnum - $snum;
  72. break;
  73. case 2:
  74. $result = $fnum * $snum;
  75. }
  76. $equation = Jaws_UTF8::chr($utf8_offset + $fnum). $operations[$oprt]. Jaws_UTF8::chr($utf8_offset + $snum);
  77. return array($equation, $result);
  78. }
  79. /**
  80. * Displays the captcha image
  81. *
  82. * @access public
  83. */
  84. function image($key)
  85. {
  86. $value = $this->randomEquation();
  87. $result = $this->update($key, $value[1]);
  88. if (Jaws_Error::IsError($result)) {
  89. $value = '';
  90. } else {
  91. $value = $value[0];
  92. }
  93. $value .= ' = ?';
  94. $bg = dirname(__FILE__) . '/resources/math.bg.png';
  95. $im = imagecreatefrompng($bg);
  96. imagecolortransparent($im, imagecolorallocate($im, 255, 255, 255));
  97. $font = dirname(__FILE__) . '/resources/courbd.ttf';
  98. $grey = imagecolorallocate($im, 0x7f, 0x7f, 0x7f);
  99. // shadow
  100. imagettftext($im, 18, 0, 8, 22, $grey, $font, $value);
  101. // text
  102. imagettftext($im, 18, 0, 11, 25, $grey, $font, $value);
  103. ob_start();
  104. imagepng($im);
  105. $content = ob_get_contents();
  106. ob_end_clean();
  107. imagedestroy($im);
  108. header('Content-Type: image/png');
  109. return $content;
  110. }
  111. }