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

/tools/captcha/captchac_code.php

https://bitbucket.org/ElecTriNo/bbbweb
PHP | 263 lines | 168 code | 69 blank | 26 comment | 33 complexity | b960aec571524e49f96cc5ed8ca101d0 MD5 | raw file
  1. <?php
  2. // -------------------------------------------------------------------------------------
  3. // Captcha Creator PHP Image Displaying Script.
  4. // Copyright (c) 2007 Alexandru Marias. All rights reserved
  5. // Web: http://www.captchacreator.com
  6. // Phone: +40722486348
  7. // ----------------------------------------------------------------
  8. // Define the characters that can make up the Turing string
  9. // Mixed-case is apparently a bit of a problem,
  10. // so lets pick one or the other
  11. include_once('captchac_config.php'); // Security functions
  12. include_once('captchac_lib.php'); // Security functions
  13. session_start();
  14. $turing = RandomCode($CMinSize,$CMaxSize);
  15. // Put it in session
  16. $_SESSION['turing_string'] = $turing;
  17. switch ($captcha_type) {
  18. case 1:
  19. if ($CFontUsed == 1 ) {
  20. $i=0;
  21. if ($handle = opendir($fonts_dir))
  22. {
  23. while (false !== ($file = readdir($handle)))
  24. if ($file != "." && $file != "..") {
  25. $fontl[$i] = $fonts_dir . '/' . $file;
  26. $i++;
  27. }
  28. closedir($handle);
  29. }
  30. $FontsNo=$i;
  31. $fontno = mt_rand(0,$FontsNo-1);
  32. $font = $fontl[$fontno];
  33. }
  34. else $font = $CFontURL;
  35. /* initialize variables */
  36. $length = strlen($turing);
  37. $data = array();
  38. $image_width = $image_height = 0;
  39. /* build the data array of the characters, size, placement, etc. */
  40. for($i=0; $i<$length; $i++) {
  41. $char = substr($turing, $i, 1);
  42. $size = mt_rand($CFontSizeMin, $CFontSizeMax);
  43. $angle = mt_rand($CFontRotMin, $CFontRotMax);
  44. $bbox = ImageTTFBBox( $size, $angle, $font, $char );
  45. $char_width = max($bbox[2], $bbox[4]) - min($bbox[0], $bbox[6]);
  46. $char_height = max($bbox[1], $bbox[3]) - min($bbox[7], $bbox[5]);
  47. $image_width += $char_width + $CFontPadding;
  48. $image_height = max($image_height, $char_height);
  49. $data[] = array(
  50. 'char' => $char,
  51. 'size' => $size,
  52. 'angle' => $angle,
  53. 'height' => $char_height,
  54. 'width' => $char_width,
  55. );
  56. }
  57. /* calculate the final image size, adding some padding */
  58. $x_padding = 12;
  59. if ( $CSize == 1 )
  60. {
  61. $image_width += ($x_padding * 2);
  62. $image_height = ($image_height * 1.5) + 2;
  63. }
  64. else {
  65. $image_width = $CSizeWidth;
  66. $image_height = $CSizeHeight;
  67. }
  68. /* build the image, and allocte the colors */
  69. $im = ImageCreate($image_width, $image_height);
  70. $cs = mt_rand(1,3);
  71. if ($CBackgroundType == 2)
  72. {
  73. $r = hexdec(substr($CBackgroundColor,1,2));
  74. $g = hexdec(substr($CBackgroundColor,3,2));
  75. $b = hexdec(substr($CBackgroundColor,5,2));
  76. }
  77. else
  78. {
  79. $d1 = $d2 = $d3 = 0;
  80. while ( ($d1<50) AND ($d2<50) AND ($d3<50) )
  81. {
  82. $r = mt_rand(200,255);
  83. $g = mt_rand(200,255);
  84. $b = mt_rand(200,255);
  85. $d1 = abs($r-$g);
  86. $d2 = abs($r-$b);
  87. $d3 = abs($g-$b);
  88. }
  89. }
  90. $color_bg = ImageColorAllocate($im, $r, $g, $b );
  91. $color_border = ImageColorAllocate($im, round($r/2), round($g/2), round($b/2));
  92. $color_line0 = ImageColorAllocate($im, round($r*0.85), round($g*0.85), round($b*0.85) );
  93. $color_elipse0 = ImageColorAllocate($im, round($r*0.95), round($g*0.95), round($b*0.95) );
  94. $color_elipse1 = ImageColorAllocate($im, round($r*0.90), round($g*0.90), round($b*0.90) );
  95. $d1 = mt_rand(0,50);
  96. $d2 = mt_rand(0,50);
  97. $d3 = mt_rand(0,50);
  98. $color_line1 = ImageColorAllocate($im, $r-$d1, $g-$d2, $b-$d3 );
  99. $d1 = $d2 = $d3 = 0;
  100. while ( ($d1<100) AND ($d2<100) AND (d3<100) )
  101. {
  102. $r = mt_rand(0,150);
  103. $g = mt_rand(0,150);
  104. $b = mt_rand(0,150);
  105. $d1 = abs($r-$g);
  106. $d2 = abs($r-$b);
  107. $d3 = abs($g-$b);
  108. }
  109. switch ( $CFontColorType )
  110. {
  111. case 1 : $color_text = ImageColorAllocate($im, $r, $g, $b );
  112. break;
  113. case 2 : $color_text = ImageColorAllocate($im, 0, 0, 0 );
  114. break;
  115. case 3 : $color_text = ImageColorAllocate($im, 255, 255, 255 );
  116. break;
  117. case 4 : $color_text = ImageColorAllocate($im, $color_text_r, $color_text_g, $color_text_b );
  118. break;
  119. }
  120. $noiset = mt_rand(1,2);
  121. if ( $CBackgroundType == 1 )
  122. {
  123. switch ($noiset) {
  124. case '1' :
  125. /* make the random background elipses */
  126. for($l=0; $l<10; $l++) {
  127. $c = 'color_elipse' . ($l%2);
  128. $cx = mt_rand(0, $image_width);
  129. $cy = mt_rand(0, $image_width);
  130. $rx = mt_rand(10, $image_width);
  131. $ry = mt_rand(10, $image_width);
  132. ImageFilledEllipse($im, $cx, $cy, $rx, $ry, $$c );
  133. }; break;
  134. case '2' :
  135. /* make the random background lines */
  136. for($l=0; $l<10; $l++) {
  137. $c = 'color_line' . ($l%2);
  138. $lx = mt_rand(0, $image_width+$image_height);
  139. $lw = mt_rand(0,3);
  140. if ($lx > $image_width) {
  141. $lx -= $image_width;
  142. ImageFilledRectangle($im, 0, $lx, $image_width-1, $lx+$lw, $c );
  143. } else ImageFilledRectangle($im, $lx, 0, $lx+$lw, $image_height-1, $c );
  144. }; break;
  145. } // end switch
  146. }
  147. if ( $CBackgroundType == 0 )
  148. {
  149. $image_data=getimagesize($CBackgroundFile);
  150. $image_type=$image_data[2];
  151. if($image_type==1) $img_src=imagecreatefromgif($CBackgroundFile);
  152. elseif($image_type==2) $img_src=imagecreatefromjpeg($CBackgroundFile);
  153. elseif($image_type==3) $img_src=imagecreatefrompng($CBackgroundFile);
  154. if ( $CBackgroundFillType == 1 ) {
  155. imagesettile($im,$img_src);
  156. imagefill($im,0,0,IMG_COLOR_TILED);
  157. }
  158. else imagecopyresampled($im,$img_src,0,0,0,0,$image_width,$image_height,$image_data[0],$image_data[1]);
  159. }
  160. /* output each character */
  161. $pos_x = $x_padding + ($CFontPadding / 2);
  162. foreach($data as $d) {
  163. $pos_y = ( ( $image_height + $d['height'] ) / 2 );
  164. ImageTTFText($im, $d['size'], $d['angle'], $pos_x, $pos_y, $color_text, $font, $d['char'] );
  165. $pos_x += $d['width'] + $CFontPadding;
  166. }
  167. /*
  168. $sr = array('68', '101', '109', '111', '32', '86', '101', '114', '115', '105', '111', '110');
  169. for ($i=0;$i<=count($sr);$i++)
  170. ImageTTFText($im, 12, 0, 10+ ($i*10), 10, $color_text, $font, chr($sr[$i]) );
  171. */
  172. /* a nice border */
  173. ImageRectangle($im, 0, 0, $image_width-1, $image_height-1, $color_border);
  174. /* display it */
  175. switch ($output_type) {
  176. case 'jpeg':
  177. Header('Content-type: image/jpeg');
  178. ImageJPEG($im,NULL,100);
  179. break;
  180. case 'png':
  181. default:
  182. Header('Content-type: image/png');
  183. ImagePNG($im);
  184. break;
  185. }
  186. ImageDestroy($im);
  187. break;
  188. case 2:
  189. break;
  190. }
  191. session_write_close();
  192. ?>