You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.4 KiB

  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage captcha
  7. * @since 2010-04-24
  8. * @version SVN: $Id$
  9. * @filesource $URL$
  10. */
  11. class Captcha
  12. {
  13. protected $font_size = 38;
  14. protected $width = 200;
  15. protected $height = 70;
  16. public function getImage($token)
  17. {
  18. $font = dirname(__FILE__) . '/poh.ttf';
  19. $image = imagecreatetruecolor($this->width, $this->height);
  20. // белый фон
  21. $background = imagecolorallocate($image, 255, 255, 255);
  22. imagefilledrectangle($image, 0, 0, $this->width, $this->height, $background);
  23. // основная магия тут
  24. if (Session::get('_ctoken') == $token && Session::get('_ccode')) {
  25. $code = Session::get('_ccode');
  26. $color = imagecolorallocate($image, 81, 81, 81);
  27. for ($j = 0; $j < 5; $j++) {
  28. imageellipse($image, rand(-$this->width, $this->width * 2),
  29. rand(-$this->height, $this->height * 2),
  30. $this->width, $this->width, $color);
  31. }
  32. $letters = preg_split("//u", strtoupper($code));
  33. $length = count($letters);
  34. $step = floor($this->width / $length);
  35. $i = 2;
  36. foreach ($letters as $key => $letter) {
  37. imagettftext($image, $this->font_size, 0,
  38. rand($i + 2, $i + 4), ($this->font_size + $this->height) / 2,
  39. $color, $font, $letter);
  40. $i = $i + $step ;
  41. }
  42. }
  43. ob_start();
  44. header("Cache-Control: no-store, no-cache, must-revalidate");
  45. header("Content-Type: image/jpg");
  46. imagejpeg($image, '', 100);
  47. imagedestroy($image);
  48. return ob_get_clean();
  49. }
  50. public function getToken()
  51. {
  52. $token = md5(microtime() . uniqid());
  53. $code = strtoupper(substr(str_ireplace(array('0', 'O'), '', md5(time() . 'captcha' . rand(0, 1000))), 1, 5));
  54. Session::set('_ctoken', $token);
  55. Session::set('_ccode', $code);
  56. return $token;
  57. }
  58. public function checkCode($token, $code)
  59. {
  60. if (Session::get('_ctoken') == $token && Session::get('_ccode') == strtoupper($code)){
  61. return true;
  62. }
  63. return false;
  64. }
  65. }