PageRenderTime 70ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/wordpress/plugins/cforms/cforms-captcha.php

https://github.com/prabhu/desistartups
PHP | 221 lines | 151 code | 54 blank | 16 comment | 24 complexity | 67661c22ad4029f78386465b3d9c51b2 MD5 | raw file
  1. <?php
  2. $img_sz_type = 0;
  3. $img_sz_width = ($_REQUEST['w']<>'')?$_REQUEST['w']:115;
  4. $img_sz_height = ($_REQUEST['h']<>'')?$_REQUEST['h']:25;
  5. $im_bg = 0;
  6. $im_bg_type = 1;
  7. $im_bg_url = 'captchabg/' . ( ($_REQUEST['b']<>'')?$_REQUEST['b']:'1.gif' );
  8. $fontUsed = 0;
  9. $font_url = 'captchafonts/' . ( ($_REQUEST['f']<>'')?$_REQUEST['f']:'font4.ttf' );
  10. $fonts_dir = 'captchafonts';
  11. $min_font_size = ($_REQUEST['f1']<>'')?$_REQUEST['f1']:17;
  12. $max_font_size = ($_REQUEST['f2']<>'')?$_REQUEST['f2']:19;
  13. $min_angle = ($_REQUEST['a1']<>'')?$_REQUEST['a1']:-12;
  14. $max_angle = ($_REQUEST['a2']<>'')?$_REQUEST['a2']:12;
  15. $col_txt_type = 4;
  16. $col = ($_REQUEST['c']<>'')?$_REQUEST['c']:'000066';
  17. $col_txt_r = hexdec(substr($col,0,2));
  18. $col_txt_g = hexdec(substr($col,2,2));
  19. $col_txt_b = hexdec(substr($col,4,2));
  20. $border = ($_REQUEST['l']<>'')?$_REQUEST['l']:'000066';
  21. $border_r = hexdec(substr($border,0,2));
  22. $border_g = hexdec(substr($border,2,2));
  23. $border_b = hexdec(substr($border,4,2));
  24. $char_padding = 2;
  25. # $output_type='jpeg';
  26. $output_type='png';
  27. //
  28. // dont't change anything below this comment
  29. //
  30. @session_start();
  31. $no = ($_REQUEST['ts']<>'')?$_REQUEST['ts']:'';
  32. $turing = $_SESSION['turing_string_'.$no];
  33. if ($fontUsed == 1 ) {
  34. $fontno = mt_rand(1,34);
  35. $font = $fonts_dir . '/font' . $fontno . '.ttf';
  36. }
  37. else $font = $font_url;
  38. /* initialize variables */
  39. $length = strlen($turing);
  40. $data = array();
  41. $image_width = $image_height = 0;
  42. $codelen = 0;
  43. /* build the data array of the characters, size, placement, etc. */
  44. for($i=0; $i<$length; $i++) {
  45. $char = substr($turing, $i, 1);
  46. $size = mt_rand($min_font_size, $max_font_size);
  47. $angle = mt_rand($min_angle, $max_angle);
  48. $bbox = ImageTTFBBox( $size, $angle, $font, $char );
  49. $char_width = max($bbox[2], $bbox[4]) - min($bbox[0], $bbox[6]);
  50. $char_height = max($bbox[1], $bbox[3]) - min($bbox[7], $bbox[5]);
  51. $codelen = $codelen + $char_width + $char_padding;
  52. $image_width += $char_width + $char_padding;
  53. $image_height = max($image_height, $char_height);
  54. $data[] = array(
  55. 'char' => $char,
  56. 'size' => $size,
  57. 'angle' => $angle,
  58. 'height' => $char_height,
  59. 'width' => $char_width,
  60. );
  61. }
  62. /* calculate the final image size, adding some padding */
  63. $x_padding = 12;
  64. if ( $img_sz_type == 1 )
  65. {
  66. $image_width += ($x_padding * 2);
  67. $image_height = ($image_height * 1.5) + 2;
  68. }
  69. else {
  70. $image_width = $img_sz_width;
  71. $image_height = $img_sz_height;
  72. }
  73. /* build the image, and allocte the colors */
  74. $im = ImageCreate($image_width, $image_height);
  75. $cs = mt_rand(1,3);
  76. $d1 = $d2 = $d3 = 0;
  77. while ( ($d1<50) AND ($d2<50) AND ($d3<50) )
  78. {
  79. $r = mt_rand(200,255); $g = mt_rand(200,255); $b = mt_rand(200,255);
  80. $d1 = abs($r-$g); $d2 = abs($r-$b); $d3 = abs($g-$b);
  81. }
  82. $color_bg = ImageColorAllocate($im, $r, $g, $b );
  83. $color_border = ImageColorAllocate($im, $border_r, $border_g, $border_b);
  84. $color_line0 = ImageColorAllocate($im, round($r*0.85), round($g*0.85), round($b*0.85) );
  85. $color_elipse0 = ImageColorAllocate($im, round($r*0.95), round($g*0.95), round($b*0.95) );
  86. $color_elipse1 = ImageColorAllocate($im, round($r*0.90), round($g*0.90), round($b*0.90) );
  87. $d1 = mt_rand(0,50); $d2 = mt_rand(0,50); $d3 = mt_rand(0,50);
  88. $color_line1 = ImageColorAllocate($im, $r-$d1, $g-$d2, $b-$d3 );
  89. $d1 = $d2 = $d3 = 0;
  90. while ( ($d1<100) AND ($d2<100) AND ($d3<100) )
  91. {
  92. $r = mt_rand(0,150); $g = mt_rand(0,150); $b = mt_rand(0,150);
  93. $d1 = abs($r-$g); $d2 = abs($r-$b); $d3 = abs($g-$b);
  94. }
  95. switch ( $col_txt_type )
  96. {
  97. case 1 : $col_txt = ImageColorAllocate($im, $r, $g, $b ); break;
  98. case 2 : $col_txt = ImageColorAllocate($im, 0, 0, 0 ); break;
  99. case 3 : $col_txt = ImageColorAllocate($im, 255, 255, 255 ); break;
  100. case 4 : $col_txt = ImageColorAllocate($im, $col_txt_r, $col_txt_g, $col_txt_b ); break;
  101. }
  102. $noiset = mt_rand(1,2);
  103. if ( $im_bg == 1 )
  104. {
  105. switch ($noiset) {
  106. case '1' :
  107. /* make the random background elipses */
  108. for($l=0; $l<10; $l++) {
  109. $c = 'color_elipse' . ($l%2);
  110. $cx = mt_rand(0, $image_width);
  111. $cy = mt_rand(0, $image_width);
  112. $rx = mt_rand(10, $image_width);
  113. $ry = mt_rand(10, $image_width);
  114. ImageFilledEllipse($im, $cx, $cy, $rx, $ry, $$c );
  115. }; break;
  116. case '2' :
  117. for($l=0; $l<10; $l++) {
  118. $c = 'color_line' . ($l%2);
  119. $lx = mt_rand(0, $image_width+$image_height);
  120. $lw = mt_rand(0,3);
  121. if ($lx > $image_width) {
  122. $lx -= $image_width;
  123. ImageFilledRectangle($im, 0, $lx, $image_width-1, $lx+$lw, $$c );
  124. } else ImageFilledRectangle($im, $lx, 0, $lx+$lw, $image_height-1, $$c );
  125. }; break;
  126. }
  127. }
  128. if ( $im_bg == 0 ){
  129. $image_data=getimagesize($im_bg_url);
  130. $image_type=$image_data[2];
  131. if($image_type==1) $img_src=imagecreatefromgif($im_bg_url);
  132. elseif($image_type==2) $img_src=imagecreatefromjpeg($im_bg_url);
  133. elseif($image_type==3) $img_src=imagecreatefrompng($im_bg_url);
  134. if ( $im_bg_type == 1 ) {
  135. imagesettile($im,$img_src);
  136. // imagefill($im,0,0,IMG_COLOR_TILED);
  137. imageFilledRectangle ($im, 0, 0, $image_width, $image_height, IMG_COLOR_TILED);
  138. }
  139. else imagecopyresampled($im,$img_src,0,0,0,0,$image_width,$image_height,$image_data[0],$image_data[1]);
  140. }
  141. $pos_x = ($image_width - $codelen) / 2;
  142. foreach($data as $d) {
  143. $pos_y = ( ( $image_height + $d['height'] ) / 2 );
  144. ImageTTFText($im, $d['size'], $d['angle'], $pos_x, $pos_y, $col_txt, $font, $d['char'] );
  145. $pos_x += $d['width'] + $char_padding;
  146. }
  147. /* a nice border */
  148. ImageRectangle($im, 0, 0, $image_width-1, $image_height-1, $color_border);
  149. /* display it */
  150. switch ($output_type) {
  151. case 'jpeg':
  152. // Header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
  153. // Header('Cache-Control: no-cache, must-revalidate');
  154. Header('Content-type: image/jpeg');
  155. ImageJPEG($im,NULL,100);
  156. break;
  157. case 'png':
  158. default:
  159. // Header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
  160. // Header('Cache-Control: no-cache, must-revalidate');
  161. Header('Content-type: image/png');
  162. ImagePNG($im);
  163. break;
  164. }
  165. ImageDestroy($im);
  166. session_write_close();
  167. ?>