PageRenderTime 56ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/Captcha/Math.php

https://github.com/bistory/kohana-captcha
PHP | 64 lines | 30 code | 5 blank | 29 comment | 1 complexity | 8d1cf4141a7c16a0af62839defe76544 MD5 | raw file
  1. <?php defined('SYSPATH') OR die('No direct access.');
  2. /**
  3. * Math captcha class.
  4. *
  5. * @package Captcha
  6. * @subpackage Captcha_Math
  7. * @author Michael Lavers
  8. * @author Kohana Team
  9. * @copyright (c) 2008-2010 Kohana Team
  10. * @license http://kohanaphp.com/license.html
  11. */
  12. class Captcha_Math extends Captcha
  13. {
  14. /**
  15. * @var string Captcha math exercise
  16. */
  17. private $math_exercise;
  18. /**
  19. * Generates a new Captcha challenge.
  20. *
  21. * @return string The challenge answer
  22. */
  23. public function generate_challenge()
  24. {
  25. // Easy
  26. if (Captcha::$config['complexity'] < 4)
  27. {
  28. $numbers[] = mt_rand(1, 5);
  29. $numbers[] = mt_rand(1, 4);
  30. }
  31. // Normal
  32. elseif (Captcha::$config['complexity'] < 7)
  33. {
  34. $numbers[] = mt_rand(10, 20);
  35. $numbers[] = mt_rand(1, 10);
  36. }
  37. // Difficult, well, not really ;)
  38. else
  39. {
  40. $numbers[] = mt_rand(100, 200);
  41. $numbers[] = mt_rand(10, 20);
  42. $numbers[] = mt_rand(1, 10);
  43. }
  44. // Store the question for output
  45. $this->math_exercise = implode(' + ', $numbers).' = ';
  46. // Return the answer
  47. return array_sum($numbers);
  48. }
  49. /**
  50. * Outputs the Captcha riddle.
  51. *
  52. * @param boolean $html HTML output
  53. * @return mixed
  54. */
  55. public function render($html = TRUE, $request = NULL)
  56. {
  57. return $this->math_exercise;
  58. }
  59. } // End Captcha Math Driver Class