PageRenderTime 57ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/cforms/cforms-captcha.php

https://bitbucket.org/spyros_l/khome.portfolio
PHP | 167 lines | 118 code | 42 blank | 7 comment | 15 complexity | 3280227782092bca40a80ae25a64d7b7 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0
  1. <?php
  2. $min = prep( $_REQUEST['c1'],4 );
  3. $max = prep( $_REQUEST['c2'],5 );
  4. $src = prep( $_REQUEST['ac'], 'abcdefghijkmnpqrstuvwxyz23456789');
  5. $img_sz_type = 0;
  6. $img_sz_width = prep($_REQUEST['w'],115);
  7. $img_sz_height = prep($_REQUEST['h'],25);
  8. $im_bg = 0;
  9. $im_bg_type = 1;
  10. $im_bg_url = 'captchabg/' . ( prep($_REQUEST['b'],'1.gif') );
  11. $fontUsed = 0;
  12. $font_url = 'captchafonts/' . ( prep($_REQUEST['f'],'font4.ttf') );
  13. $fonts_dir = 'captchafonts';
  14. $min_font_size = prep($_REQUEST['f1'],17);
  15. $max_font_size = prep($_REQUEST['f2'],19);
  16. $min_angle = prep($_REQUEST['a1'],-12);
  17. $max_angle = prep($_REQUEST['a2'],12);
  18. $col_txt_type = 4;
  19. $col = prep($_REQUEST['c'],'000066');
  20. $col_txt_r = hexdec(substr($col,0,2));
  21. $col_txt_g = hexdec(substr($col,2,2));
  22. $col_txt_b = hexdec(substr($col,4,2));
  23. $border = prep($_REQUEST['l'],'000066');
  24. $border_r = hexdec(substr($border,0,2));
  25. $border_g = hexdec(substr($border,2,2));
  26. $border_b = hexdec(substr($border,4,2));
  27. $char_padding = 2;
  28. $output_type = 'png';
  29. $no = prep($_REQUEST['ts'],'');
  30. ### captcha random code
  31. $srclen = strlen($src)-1;
  32. $length = mt_rand($min,$max);
  33. $turing = '';
  34. for($i=0; $i<$length; $i++)
  35. $turing .= substr($src, mt_rand(0, $srclen), 1);
  36. $tu = ($_REQUEST['i']=='i')?strtolower($turing):$turing;
  37. setcookie('turing_string_'.$no, $_REQUEST['i'].'+'.md5($tu),(time()+60*60*5),"/");
  38. if ($fontUsed == 1 ) {
  39. $fontno = mt_rand(1,34);
  40. $font = $fonts_dir . '/font' . $fontno . '.ttf';
  41. }
  42. else $font = $font_url;
  43. ### initialize variables
  44. $length = strlen($turing);
  45. $data = array();
  46. $image_width = $image_height = 0;
  47. $codelen = 0;
  48. ### build the data array of the characters, size, placement, etc.
  49. for($i=0; $i<$length; $i++) {
  50. $char = substr($turing, $i, 1);
  51. $size = mt_rand($min_font_size, $max_font_size);
  52. $angle = mt_rand($min_angle, $max_angle);
  53. $bbox = ImageTTFBBox( $size, $angle, $font, $char );
  54. $char_width = max($bbox[2], $bbox[4]) - min($bbox[0], $bbox[6]);
  55. $char_height = max($bbox[1], $bbox[3]) - min($bbox[7], $bbox[5]);
  56. $codelen = $codelen + $char_width + $char_padding;
  57. $image_width += $char_width + $char_padding;
  58. $image_height = max($image_height, $char_height);
  59. $data[] = array('char'=>$char,'size'=>$size,'angle'=>$angle,'height'=>$char_height,'width'=>$char_width);
  60. }
  61. ### calculate the final image size, adding some padding
  62. $x_padding = 12;
  63. if ( $img_sz_type == 1 ) {
  64. $image_width += ($x_padding * 2);
  65. $image_height = ($image_height * 1.5) + 2;
  66. } else {
  67. $image_width = $img_sz_width;
  68. $image_height = $img_sz_height;
  69. }
  70. ### build the image, and allocte the colors
  71. $im = ImageCreate($image_width, $image_height);
  72. $cs = mt_rand(1,3);
  73. $d1 = $d2 = $d3 = 0;
  74. while ( ($d1<50) AND ($d2<50) AND ($d3<50) ) {
  75. $r = mt_rand(200,255); $g = mt_rand(200,255); $b = mt_rand(200,255);
  76. $d1 = abs($r-$g); $d2 = abs($r-$b); $d3 = abs($g-$b);
  77. }
  78. $color_bg = ImageColorAllocate($im, $r, $g, $b );
  79. $color_border = ImageColorAllocate($im, $border_r, $border_g, $border_b);
  80. $color_line0 = ImageColorAllocate($im, round($r*0.85), round($g*0.85), round($b*0.85) );
  81. $color_elipse0 = ImageColorAllocate($im, round($r*0.95), round($g*0.95), round($b*0.95) );
  82. $color_elipse1 = ImageColorAllocate($im, round($r*0.90), round($g*0.90), round($b*0.90) );
  83. $d1 = mt_rand(0,50); $d2 = mt_rand(0,50); $d3 = mt_rand(0,50);
  84. $color_line1 = ImageColorAllocate($im, $r-$d1, $g-$d2, $b-$d3 );
  85. $d1 = $d2 = $d3 = 0;
  86. while ( ($d1<100) AND ($d2<100) AND ($d3<100) ) {
  87. $r = mt_rand(0,150); $g = mt_rand(0,150); $b = mt_rand(0,150);
  88. $d1 = abs($r-$g); $d2 = abs($r-$b); $d3 = abs($g-$b);
  89. }
  90. switch ( $col_txt_type ) {
  91. case 1 : $col_txt = ImageColorAllocate($im, $r, $g, $b ); break;
  92. case 2 : $col_txt = ImageColorAllocate($im, 0, 0, 0 ); break;
  93. case 3 : $col_txt = ImageColorAllocate($im, 255, 255, 255 ); break;
  94. case 4 : $col_txt = ImageColorAllocate($im, $col_txt_r, $col_txt_g, $col_txt_b ); break;
  95. }
  96. $noiset = mt_rand(1,2);
  97. $image_data=getimagesize($im_bg_url);
  98. $image_type=$image_data[2];
  99. if($image_type==1) $img_src=imagecreatefromgif($im_bg_url);
  100. elseif($image_type==2) $img_src=imagecreatefromjpeg($im_bg_url);
  101. elseif($image_type==3) $img_src=imagecreatefrompng($im_bg_url);
  102. if ( $im_bg_type == 1 ) {
  103. imagesettile($im,$img_src);
  104. imageFilledRectangle ($im, 0, 0, $image_width, $image_height, IMG_COLOR_TILED);
  105. } else
  106. imagecopyresampled($im,$img_src,0,0,0,0,$image_width,$image_height,$image_data[0],$image_data[1]);
  107. $pos_x = ($image_width - $codelen) / 2;
  108. foreach($data as $d) {
  109. $pos_y = ( ( $image_height + $d['height'] ) / 2 );
  110. ImageTTFText($im, $d['size'], $d['angle'], $pos_x, $pos_y, $col_txt, $font, $d['char'] );
  111. $pos_x += $d['width'] + $char_padding;
  112. }
  113. ### a nice border
  114. ImageRectangle($im, 0, 0, $image_width-1, $image_height-1, $color_border);
  115. switch ($output_type) {
  116. case 'jpeg': Header('Content-type: image/jpeg'); ImageJPEG($im,NULL,100); break;
  117. case 'png':
  118. default: Header('Content-type: image/png'); ImagePNG($im); break;
  119. }
  120. ImageDestroy($im);
  121. ### strip stuff
  122. function prep($v,$d) {
  123. return ($v<>'') ? stripslashes($v) : $d;
  124. }
  125. ?>