PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/system/controllers/captcha.php

https://github.com/MHordecki/milionkostek
PHP | 61 lines | 33 code | 9 blank | 19 comment | 3 complexity | 7c9bdb68f53e3a4096f18513c6e9835f MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Allows a CAPTCHA image to be displayed dynamically.
  4. * Captcha library is called to output the image.
  5. *
  6. *
  7. * Usage: Call the captcha controller from a view.
  8. * `echo html::image(url::site().'captcha');`
  9. *
  10. * $Id: captcha.php 2206 2008-03-01 20:43:47Z armen $
  11. *
  12. * @package Core
  13. * @author Kohana Team
  14. * @copyright (c) 2007-2008 Kohana Team
  15. * @license http://kohanaphp.com/license.html
  16. */
  17. class Captcha_Controller extends Controller {
  18. public $captcha;
  19. public $session;
  20. protected $captcha_code;
  21. public function index()
  22. {
  23. $this->session = Session::instance();
  24. $this->captcha = new Captcha;
  25. // Create a random text string for captcha code.
  26. $this->captcha_code = $this->create_code();
  27. $this->captcha->set_code($this->captcha_code) ;
  28. // Set the session to store the security code
  29. $this->session->set('captcha_code', $this->captcha_code);
  30. // Call the library to output the image
  31. $this->captcha->render();
  32. }
  33. private function create_code()
  34. {
  35. $num_chars = Config::item('captcha.num_chars');
  36. if (Config::item('captcha.style') == 'math')
  37. {
  38. $code = (string) mt_rand(101, 991);
  39. }
  40. else
  41. {
  42. // Character set to use, similar characters removed.
  43. $charset = '@2345#6BCDF$GH789KMNPQRT%VWXYZ';
  44. $code = '';
  45. for ($i = 0; $i < $num_chars; $i++)
  46. {
  47. $code .= substr($charset, mt_rand(0, strlen($charset)-1), 1);
  48. }
  49. }
  50. return $code;
  51. }
  52. }