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.

81 lines
2.4 KiB

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