PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/inc/stuff/captcha/data.php

https://github.com/Nazg-Gul/e-marsa
PHP | 298 lines | 225 code | 45 blank | 28 comment | 68 complexity | 1e46e2182e591bd10b048f4c8c88c3a2 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * Gate - Wiki engine and web-interface for WebTester Server
  4. *
  5. * CAPTCHA image generator
  6. *
  7. * Copyright (c) 2008-2009 Sergey I. Sharybin <g.ulairi@gmail.com>
  8. *
  9. * This program can be distributed under the terms of the GNU GPL.
  10. * See the file COPYING.
  11. */
  12. include '../../../globals.php';
  13. include $DOCUMENT_ROOT.'/inc/include.php';
  14. include 'config.php';
  15. $cfg = $CAPTCHA_Config;
  16. $w = $cfg['width'];
  17. $h = $cfg['height'];
  18. $dh = (($cfg['signature']['text'])?(-12):(0));
  19. $fonts = array ();
  20. $bg_rgb = $cfg['colors']['backgrounds'][mt_rand (0, count ($cfg['colors']['backgrounds'])-1)];
  21. $fg_rgb = $cfg['colors']['foregrounds'][mt_rand (0, count ($cfg['colors']['foregrounds'])-1)];
  22. // Get list of fonts
  23. $dir = opendir ($DOCUMENT_ROOT.'/pics/captcha/fonts');
  24. while (($file = readdir ($dir)) != false) {
  25. if ($file != '.' && $file != '..') {
  26. if (preg_match ('/.png$/', $file)) {
  27. $fonts[] = $file;
  28. }
  29. }
  30. }
  31. closedir ($dir);
  32. // Generate keycode
  33. while (true) {
  34. $keystring = '';
  35. $l = strlen ($cfg['allowed_symbols']) - 1;
  36. for ($i = 0, $n = $cfg['keystring_length']; $i < $n; $i++) {
  37. $keystring .= $cfg['allowed_symbols'][mt_rand (0, $l)];
  38. }
  39. if (!preg_match('/cp|cb|ck|c6|c9|rn|rm|mm|co|do|cl|db|qp|qb|dp/',
  40. $keystring)) {
  41. break;
  42. }
  43. }
  44. $_SESSION['CAPTCHA_Keystring'] = $keystring;
  45. // Load random font
  46. $font_file = $fonts[mt_rand (0, count ($fonts) - 1)];
  47. $font = imagecreatefrompng ($DOCUMENT_ROOT.'/pics/captcha/fonts/'.$font_file);
  48. imagealphablending ($font, true);
  49. $fontfile_width = imagesx($font);
  50. $fontfile_height = imagesy($font) - 1;
  51. $font_metrics = array();
  52. $reading_symbol=false;
  53. // Look throug font file and fill da metrics
  54. $alphabet_length = strlen ($cfg['alphabet']);
  55. $symbol = 0;
  56. $alphabet = $cfg['alphabet'];
  57. for ($i = 0; $i < $fontfile_width && $alphabet_length; $i++){
  58. $transparent = (imagecolorat ($font, $i, 0) >> 24) == 127;
  59. if (!$reading_symbol && !$transparent) {
  60. $font_metrics[$alphabet{$symbol}] = array('start' => $i);
  61. $reading_symbol = true;
  62. continue;
  63. }
  64. if($reading_symbol && $transparent) {
  65. $font_metrics[$alphabet{$symbol}]['end'] = $i;
  66. $reading_symbol = false;
  67. $symbol++;
  68. continue;
  69. }
  70. }
  71. // Print da texto
  72. $text_img = imagecreatetruecolor ($w * 2, $h * 2);
  73. imagealphablending ($text_img, true);
  74. $white=imagecolorallocate ($text_img, 255, 255, 255);
  75. imagefilledrectangle ($text_img, 0, 0, $w - 1, $h - 1, $white);
  76. // draw text
  77. $x = 1;
  78. for ($i = 0; $i < $cfg['keystring_length']; $i++) {
  79. $m = $font_metrics[$keystring{$i}];
  80. $y = mt_rand(-$cfg['fluctuation_amplitude'],
  81. $cfg['fluctuation_amplitude']) + ($h - $fontfile_height) / 2 + 2;
  82. if ($cfg['no_spaces']) {
  83. $shift = 0;
  84. if ($i > 0) {
  85. $shift = 1000;
  86. for ($sy = 7; $sy < $fontfile_height - 20; ++$sy) {
  87. for ($sx = $m['start'] - 1; $sx < $m['end']; ++$sx) {
  88. $rgb = imagecolorat ($font, $sx, $sy);
  89. $opacity = $rgb >> 24;
  90. if ($opacity < 127) {
  91. $left = $sx - $m['start'] + $x;
  92. $py = $sy + $y;
  93. if ($py > $h) {
  94. break;
  95. }
  96. for ($px = min ($left, $w - 1); $px > $left - 12 &&
  97. $px >= 0; --$px) {
  98. $color = imagecolorat ($text_img, $px, $py) & 0xff;
  99. if ($color + $opacity < 190) {
  100. if ($shift > $left - $px) {
  101. $shift = $left - $px;
  102. }
  103. break;
  104. }
  105. }
  106. break;
  107. }
  108. }
  109. }
  110. if ($shift == 1000) {
  111. $shift = mt_rand (4,6);
  112. }
  113. }
  114. } else {
  115. $shift = 1;
  116. }
  117. imagecopy ($text_img, $font, $x - $shift, $y,
  118. $m['start'], 1, $m['end'] - $m['start'], $fontfile_height);
  119. $x += $m['end'] - $m['start'] - $shift;
  120. }
  121. // periods
  122. $rand1 = mt_rand(750000,1200000)/10000000;
  123. $rand2 = mt_rand(750000,1200000)/10000000;
  124. $rand3 = mt_rand(750000,1200000)/10000000;
  125. $rand4 = mt_rand(750000,1200000)/10000000;
  126. // phases
  127. $rand5 = mt_rand(0,31415926)/10000000;
  128. $rand6 = mt_rand(0,31415926)/10000000;
  129. $rand7 = mt_rand(0,31415926)/10000000;
  130. $rand8 = mt_rand(0,31415926)/10000000;
  131. // amplitudes
  132. $rand9 = mt_rand(330,420)/110;
  133. $rand10 = mt_rand(330,450)/110;
  134. // Main buffer
  135. $new_text_img = imagecreatetruecolor ($w * 2, $h * 2);
  136. imagealphablending ($new_text_img, true);
  137. // Fill work area and draw borders
  138. imagefilledrectangle ($new_text_img, 0, 0, $w * 2, $h * 2,
  139. imagecolorallocate ($new_text_img, $bg_rgb[0],
  140. $bg_rgb[1], $bg_rgb[2]));
  141. $center = $x / 2;
  142. // wave distortion
  143. for ($x = 0; $x < $w; $x++) {
  144. for($y = 0; $y < $h; $y++) {
  145. $sx = $x + (sin ($x * $rand1 + $rand5) + sin ($y * $rand3 + $rand6)) * $rand9
  146. - $w / 2 + $center + 1;
  147. $sy = $y + (sin ($x * $rand2 + $rand7) + sin ($y * $rand4 + $rand8)) * $rand10;
  148. if ($sx < 0 || $sy < 0 || $sx >= $w - 1 || $sy >= $h - 1) {
  149. continue;
  150. } else {
  151. $color = imagecolorat ($text_img, $sx, $sy) & 0xFF;
  152. $color_x = imagecolorat ($text_img, $sx + 1, $sy) & 0xFF;
  153. $color_y = imagecolorat ($text_img, $sx, $sy + 1) & 0xFF;
  154. $color_xy = imagecolorat ($text_img, $sx + 1, $sy + 1) & 0xFF;
  155. }
  156. if ($color == 255 && $color_x == 255 && $color_y == 255 &&
  157. $color_xy == 255) {
  158. continue;
  159. } else if ($color == 0 && $color_x == 0 && $color_y == 0 && $color_xy == 0) {
  160. $newred = $fg_rgb[0];
  161. $newgreen = $fg_rgb[1];
  162. $newblue = $fg_rgb[2];
  163. } else {
  164. $frsx = $sx-floor($sx);
  165. $frsy = $sy-floor($sy);
  166. $frsx1 = 1-$frsx;
  167. $frsy1 = 1-$frsy;
  168. $newcolor = (
  169. $color * $frsx1 * $frsy1+
  170. $color_x * $frsx * $frsy1+
  171. $color_y * $frsx1 * $frsy+
  172. $color_xy * $frsx * $frsy);
  173. if ($newcolor > 255) {
  174. $newcolor = 255;
  175. }
  176. $newcolor = $newcolor/255;
  177. $newcolor0 = 1-$newcolor;
  178. $newred = $newcolor0 * $fg_rgb[0] + $newcolor * $bg_rgb[0];
  179. $newgreen = $newcolor0 * $fg_rgb[1] + $newcolor * $bg_rgb[1];
  180. $newblue = $newcolor0 * $fg_rgb[2] + $newcolor * $bg_rgb[2];
  181. }
  182. imagesetpixel ($new_text_img, $x, $y,
  183. imagecolorallocate ($text_img, $newred, $newgreen, $newblue));
  184. }
  185. }
  186. // Search for text
  187. $sx = -1;
  188. $fx = -1;
  189. $sy = -1;
  190. $fy = -1;
  191. for ($i = 0; $i < $w; $i++) {
  192. for ($j = 0; $j < $h; $j++) {
  193. $color = imagecolorat ($new_text_img, $i, $j);
  194. $r = floor ($color / 0x10000);
  195. $g = floor ($color / 0x100) % 0x100;
  196. $b = $color % 0x100;
  197. if ($r < 230 && $g < 230 && $b < 230) {
  198. if ($sx < 0) {
  199. $sx = $i;
  200. } else if ($i>$fx) {
  201. $fx = $i;
  202. }
  203. if ($sy<0 || $j<$sy) {
  204. $sy = $j;
  205. }
  206. if ($j > $fy) {
  207. $fy = $j;
  208. }
  209. }
  210. }
  211. }
  212. // Main buffer
  213. $img = imagecreatetruecolor ($w, $h);
  214. imagealphablending ($img, true);
  215. $bg = imagecolorallocate ($img, $bg_rgb[0], $bg_rgb[1], $bg_rgb[2]);
  216. $color_border = imagecolorallocate ($img, $cfg['colors']['border'][0],
  217. $cfg['colors']['border'][1],
  218. $cfg['colors']['border'][2]);
  219. // Fill work area and draw borders
  220. imagefilledrectangle ($img, 0, 0, $w - 1, $h - 1, $color_border);
  221. imagefilledrectangle ($img, 1, 1, $w - 2, $h - 2, $bg);
  222. // Draw da signatura
  223. $delta_h = 0;
  224. if ($cfg['signature']['text'] != '') {
  225. $sig_fg = imagecolorallocate ($img, $cfg['signature']['foreground'][0],
  226. $cfg['signature']['foreground'][1],
  227. $cfg['signature']['foreground'][2]);
  228. $sig_bg = imagecolorallocate ($img, $cfg['signature']['background'][0],
  229. $cfg['signature']['background'][1],
  230. $cfg['signature']['background'][2]);
  231. imagefilledrectangle ($img, 0, $h - 12, $w - 1, $h - 12, $color_border);
  232. imagefilledrectangle ($img, 1, $h - 11, $w - 2, $h - 2, $sig_bg);
  233. imagestring ($img, 2, $w / 2 - ImageFontWidth (2) *
  234. mb_strlen($cfg['signature']['text']) / 2,
  235. $h - ImageFontHeight (2), $cfg['signature']['text'], $sig_fg);
  236. $delta_h =- ImageFontHeight(2);
  237. }
  238. $dw = $fx - $sx;
  239. $dh = $fy - $sy;
  240. imagecopy ($img, $new_text_img, ($w - $dw) / 2,
  241. ($h - $dh + $delta_h) / 2, $sx, $sy, $dw, $dh);
  242. // make sure this thing doesn't cache
  243. header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
  244. header ('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
  245. header ('Cache-Control: no-store, no-cache, must-revalidate');
  246. header ('Cache-Control: post-check=0, pre-check=0', false);
  247. header ('Pragma: no-cache');
  248. // Put to output stream
  249. header ("Content-type: image/gif");
  250. imagegif ($img);
  251. imagedestroy ($img);
  252. imagedestroy ($text_img);
  253. ?>