PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Signup/captcha/captcha_gd.php

https://github.com/xrg/a2billing
PHP | 161 lines | 115 code | 23 blank | 23 comment | 16 complexity | 17bef7a096eb3a6491d4697a35174af4 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package VC
  5. * @version $Id: captcha_gd.php,v 1.19 2007/01/26 16:07:43 acydburn Exp $
  6. * @copyright (c) 2006 phpBB Group
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. * Based on PHP-Class hn_captcha Version 1.3, released 11-Apr-2006
  12. * Original Author - Horst Nogajski, horst@nogajski.de
  13. *
  14. * @package VC
  15. */
  16. class captcha
  17. {
  18. var $width = 240;
  19. var $height = 60;
  20. var $captcha_gd_noise = 1;
  21. function execute($code, $seed)
  22. {
  23. global $config;
  24. $stats = gd_info();
  25. $bundled = (substr($stats['GD Version'], 0, 7) === 'bundled') ? true : false;
  26. preg_match('/[\\d.]+/', $stats['GD Version'], $version);
  27. $gd_version = (version_compare($version[0], '2.0.1', '>=')) ? 2 : 1;
  28. // create the image, stay compat with older versions of GD
  29. if ($gd_version === 2)
  30. {
  31. $func1 = 'imagecreatetruecolor';
  32. $func2 = 'imagecolorallocate';
  33. }
  34. else
  35. {
  36. $func1 = 'imagecreate';
  37. $func2 = 'imagecolorclosest';
  38. }
  39. $image = $func1($this->width, $this->height);
  40. if ($bundled)
  41. {
  42. imageantialias($image, true);
  43. }
  44. // seed the random generator
  45. mt_srand($seed);
  46. // set background color
  47. $back = imagecolorallocate($image, mt_rand(224, 255), mt_rand(224, 255), mt_rand(224, 255));
  48. imagefilledrectangle($image, 0, 0, $this->width, $this->height, $back);
  49. // allocates the 216 websafe color palette to the image
  50. if ($gd_version === 1)
  51. {
  52. for ($r = 0; $r <= 255; $r += 51)
  53. {
  54. for ($g = 0; $g <= 255; $g += 51)
  55. {
  56. for ($b = 0; $b <= 255; $b += 51)
  57. {
  58. imagecolorallocate($image, $r, $g, $b);
  59. }
  60. }
  61. }
  62. }
  63. // fill with noise or grid
  64. if ($this -> captcha_gd_noise)
  65. {
  66. $chars_allowed = array_merge(range('1', '9'), range('A', 'Z'));
  67. // random characters in background with random position, angle, color
  68. for ($i = 0 ; $i < 72; $i++)
  69. {
  70. $size = mt_rand(8, 23);
  71. $angle = mt_rand(0, 360);
  72. $x = mt_rand(0, 360);
  73. $y = mt_rand(0, (int)($this->height - ($size / 5)));
  74. $color = $func2($image, mt_rand(160, 224), mt_rand(160, 224), mt_rand(160, 224));
  75. $text = $chars_allowed[mt_rand(0, sizeof($chars_allowed) - 1)];
  76. imagettftext($image, $size, $angle, $x, $y, $color, $this->get_font(), $text);
  77. }
  78. unset($chars_allowed);
  79. }
  80. else
  81. {
  82. // generate grid
  83. for ($i = 0; $i < $this->width; $i += 13)
  84. {
  85. $color = $func2($image, mt_rand(160, 224), mt_rand(160, 224), mt_rand(160, 224));
  86. imageline($image, $i, 0, $i, $this->height, $color);
  87. }
  88. for ($i = 0; $i < $this->height; $i += 11)
  89. {
  90. $color = $func2($image, mt_rand(160, 224), mt_rand(160, 224), mt_rand(160, 224));
  91. imageline($image, 0, $i, $this->width, $i, $color);
  92. }
  93. }
  94. $len = strlen($code);
  95. for ($i = 0, $x = mt_rand(20, 40); $i < $len; $i++)
  96. {
  97. $text = strtoupper($code[$i]);
  98. //echo "text=$text ; code=$code -".strlen($code); exit;
  99. $angle = mt_rand(-30, 30);
  100. $size = mt_rand(20, 40);
  101. $y = mt_rand((int)($size * 1.5), (int)($this->height - ($size / 7)));
  102. $color = $func2($image, mt_rand(0, 127), mt_rand(0, 127), mt_rand(0, 127));
  103. $shadow = $func2($image, mt_rand(127, 254), mt_rand(127, 254), mt_rand(127, 254));
  104. $font = $this->get_font();
  105. imagettftext($image, $size, $angle, $x + (int)($size / 15), $y, $shadow, $font, $text);
  106. imagettftext($image, $size, $angle, $x, $y - (int)($size / 15), $color, $font, $text);
  107. $x += $size + 4;
  108. }
  109. // Output image
  110. header('Content-Type: image/png');
  111. header('Cache-control: no-cache, no-store');
  112. imagepng($image);
  113. imagedestroy($image);
  114. }
  115. function get_font()
  116. {
  117. static $fonts = array();
  118. if (!sizeof($fonts))
  119. {
  120. global $phpbb_root_path;
  121. $dr = @opendir(FSROOT.'/signup/captcha/fonts/');
  122. if (!$dr)
  123. {
  124. trigger_error('Unable to open fonts directory.', E_USER_ERROR);
  125. }
  126. while (false !== ($entry = readdir($dr)))
  127. {
  128. if (strtolower(pathinfo($entry, PATHINFO_EXTENSION)) == 'ttf')
  129. {
  130. $fonts[] = FSROOT.'/signup/captcha/fonts/' . $entry;
  131. }
  132. }
  133. closedir($dr);
  134. }
  135. return $fonts[mt_rand(0, sizeof($fonts) - 1)];
  136. }
  137. }
  138. ?>