PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/codegen/kcaptcha.php

https://bitbucket.org/ivangalin/instantcms_mirror
PHP | 238 lines | 178 code | 39 blank | 21 comment | 45 complexity | be7b1006cfc783fd37503016eaa5db2c MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. # KCAPTCHA PROJECT VERSION 2.0
  3. # Automatic test to tell computers and humans apart
  4. # Copyright by Kruglov Sergei, 2006, 2007, 2008, 2011
  5. # www.captcha.ru, www.kruglov.ru
  6. # System requirements: PHP 4.0.6+ w/ GD
  7. # KCAPTCHA is a free software. You can freely use it for developing own site or software.
  8. # If you use this software as a part of own sofware, you must leave copyright notices intact or add KCAPTCHA copyright notices to own.
  9. # As a default configuration, KCAPTCHA has a small credits text at bottom of CAPTCHA image.
  10. # You can remove it, but I would be pleased if you left it. ;)
  11. # See kcaptcha_config.php for customization
  12. class KCAPTCHA{
  13. // generates keystring and image
  14. function KCAPTCHA(){
  15. require(dirname(__FILE__).'/kcaptcha_config.php');
  16. $fonts=array();
  17. $fontsdir_absolute=dirname(__FILE__).'/'.$fontsdir;
  18. if ($handle = opendir($fontsdir_absolute)) {
  19. while (false !== ($file = readdir($handle))) {
  20. if (preg_match('/\.png$/i', $file)) {
  21. $fonts[]=$fontsdir_absolute.'/'.$file;
  22. }
  23. }
  24. closedir($handle);
  25. }
  26. $alphabet_length=mb_strlen($alphabet);
  27. do{
  28. // generating random keystring
  29. while(true){
  30. $this->keystring='';
  31. for($i=0;$i<$length;$i++){
  32. $this->keystring.=$allowed_symbols{mt_rand(0,mb_strlen($allowed_symbols)-1)};
  33. }
  34. if(!preg_match('/cp|cb|ck|c6|c9|rn|rm|mm|co|do|cl|db|qp|qb|dp|ww/', $this->keystring)) break;
  35. }
  36. $font_file=$fonts[mt_rand(0, count($fonts)-1)];
  37. $font=imagecreatefrompng($font_file);
  38. imagealphablending($font, true);
  39. $fontfile_width=imagesx($font);
  40. $fontfile_height=imagesy($font)-1;
  41. $font_metrics=array();
  42. $symbol=0;
  43. $reading_symbol=false;
  44. // loading font
  45. for($i=0;$i<$fontfile_width && $symbol<$alphabet_length;$i++){
  46. $transparent = (imagecolorat($font, $i, 0) >> 24) == 127;
  47. if(!$reading_symbol && !$transparent){
  48. $font_metrics[$alphabet{$symbol}]=array('start'=>$i);
  49. $reading_symbol=true;
  50. continue;
  51. }
  52. if($reading_symbol && $transparent){
  53. $font_metrics[$alphabet{$symbol}]['end']=$i;
  54. $reading_symbol=false;
  55. $symbol++;
  56. continue;
  57. }
  58. }
  59. $img=imagecreatetruecolor($width, $height);
  60. imagealphablending($img, true);
  61. $white=imagecolorallocate($img, 255, 255, 255);
  62. $black=imagecolorallocate($img, 0, 0, 0);
  63. imagefilledrectangle($img, 0, 0, $width-1, $height-1, $white);
  64. // draw text
  65. $x=1;
  66. $odd=mt_rand(0,1);
  67. if($odd==0) $odd=-1;
  68. for($i=0;$i<$length;$i++){
  69. $m=$font_metrics[$this->keystring{$i}];
  70. $y=(($i%2)*$fluctuation_amplitude - $fluctuation_amplitude/2)*$odd
  71. + mt_rand(-round($fluctuation_amplitude/3), round($fluctuation_amplitude/3))
  72. + ($height-$fontfile_height)/2;
  73. if($no_spaces){
  74. $shift=0;
  75. if($i>0){
  76. $shift=10000;
  77. for($sy=3;$sy<$fontfile_height-10;$sy+=1){
  78. for($sx=$m['start']-1;$sx<$m['end'];$sx+=1){
  79. $rgb=imagecolorat($font, $sx, $sy);
  80. $opacity=$rgb>>24;
  81. if($opacity<127){
  82. $left=$sx-$m['start']+$x;
  83. $py=$sy+$y;
  84. if($py>$height) break;
  85. for($px=min($left,$width-1);$px>$left-200 && $px>=0;$px-=1){
  86. $color=imagecolorat($img, $px, $py) & 0xff;
  87. if($color+$opacity<170){ // 170 - threshold
  88. if($shift>$left-$px){
  89. $shift=$left-$px;
  90. }
  91. break;
  92. }
  93. }
  94. break;
  95. }
  96. }
  97. }
  98. if($shift==10000){
  99. $shift=mt_rand(4,6);
  100. }
  101. }
  102. }else{
  103. $shift=1;
  104. }
  105. imagecopy($img, $font, $x-$shift, $y, $m['start'], 1, $m['end']-$m['start'], $fontfile_height);
  106. $x+=$m['end']-$m['start']-$shift;
  107. }
  108. }while($x>=$width-10); // while not fit in canvas
  109. //noise
  110. $white=imagecolorallocate($font, 255, 255, 255);
  111. $black=imagecolorallocate($font, 0, 0, 0);
  112. for($i=0;$i<(($height-30)*$x)*$white_noise_density;$i++){
  113. imagesetpixel($img, mt_rand(0, $x-1), mt_rand(10, $height-15), $white);
  114. }
  115. for($i=0;$i<(($height-30)*$x)*$black_noise_density;$i++){
  116. imagesetpixel($img, mt_rand(0, $x-1), mt_rand(10, $height-15), $black);
  117. }
  118. $center=$x/2;
  119. // credits. To remove, see configuration file
  120. $img2=imagecreatetruecolor($width, $height+($show_credits?12:0));
  121. $foreground=imagecolorallocate($img2, $foreground_color[0], $foreground_color[1], $foreground_color[2]);
  122. $background=imagecolorallocate($img2, $background_color[0], $background_color[1], $background_color[2]);
  123. imagefilledrectangle($img2, 0, 0, $width-1, $height-1, $background);
  124. imagefilledrectangle($img2, 0, $height, $width-1, $height+12, $foreground);
  125. $credits=empty($credits)?$_SERVER['HTTP_HOST']:$credits;
  126. imagestring($img2, 2, $width/2-imagefontwidth(2)*mb_strlen($credits)/2, $height-2, $credits, $background);
  127. // periods
  128. $rand1=mt_rand(750000,1200000)/10000000;
  129. $rand2=mt_rand(750000,1200000)/10000000;
  130. $rand3=mt_rand(750000,1200000)/10000000;
  131. $rand4=mt_rand(750000,1200000)/10000000;
  132. // phases
  133. $rand5=mt_rand(0,31415926)/10000000;
  134. $rand6=mt_rand(0,31415926)/10000000;
  135. $rand7=mt_rand(0,31415926)/10000000;
  136. $rand8=mt_rand(0,31415926)/10000000;
  137. // amplitudes
  138. $rand9=mt_rand(330,420)/110;
  139. $rand10=mt_rand(330,450)/100;
  140. //wave distortion
  141. for($x=0;$x<$width;$x++){
  142. for($y=0;$y<$height;$y++){
  143. $sx=$x+(sin($x*$rand1+$rand5)+sin($y*$rand3+$rand6))*$rand9-$width/2+$center+1;
  144. $sy=$y+(sin($x*$rand2+$rand7)+sin($y*$rand4+$rand8))*$rand10;
  145. if($sx<0 || $sy<0 || $sx>=$width-1 || $sy>=$height-1){
  146. continue;
  147. }else{
  148. $color=imagecolorat($img, $sx, $sy) & 0xFF;
  149. $color_x=imagecolorat($img, $sx+1, $sy) & 0xFF;
  150. $color_y=imagecolorat($img, $sx, $sy+1) & 0xFF;
  151. $color_xy=imagecolorat($img, $sx+1, $sy+1) & 0xFF;
  152. }
  153. if($color==255 && $color_x==255 && $color_y==255 && $color_xy==255){
  154. continue;
  155. }else if($color==0 && $color_x==0 && $color_y==0 && $color_xy==0){
  156. $newred=$foreground_color[0];
  157. $newgreen=$foreground_color[1];
  158. $newblue=$foreground_color[2];
  159. }else{
  160. $frsx=$sx-floor($sx);
  161. $frsy=$sy-floor($sy);
  162. $frsx1=1-$frsx;
  163. $frsy1=1-$frsy;
  164. $newcolor=(
  165. $color*$frsx1*$frsy1+
  166. $color_x*$frsx*$frsy1+
  167. $color_y*$frsx1*$frsy+
  168. $color_xy*$frsx*$frsy);
  169. if($newcolor>255) $newcolor=255;
  170. $newcolor=$newcolor/255;
  171. $newcolor0=1-$newcolor;
  172. $newred=$newcolor0*$foreground_color[0]+$newcolor*$background_color[0];
  173. $newgreen=$newcolor0*$foreground_color[1]+$newcolor*$background_color[1];
  174. $newblue=$newcolor0*$foreground_color[2]+$newcolor*$background_color[2];
  175. }
  176. imagesetpixel($img2, $x, $y, imagecolorallocate($img2, $newred, $newgreen, $newblue));
  177. }
  178. }
  179. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
  180. header('Cache-Control: no-store, no-cache, must-revalidate');
  181. header('Cache-Control: post-check=0, pre-check=0', FALSE);
  182. header('Pragma: no-cache');
  183. if(function_exists("imagejpeg")){
  184. header("Content-Type: image/jpeg");
  185. imagejpeg($img2, null, $jpeg_quality);
  186. }else if(function_exists("imagegif")){
  187. header("Content-Type: image/gif");
  188. imagegif($img2);
  189. }else if(function_exists("imagepng")){
  190. header("Content-Type: image/x-png");
  191. imagepng($img2);
  192. }
  193. }
  194. // returns keystring
  195. function getKeyString(){
  196. return $this->keystring;
  197. }
  198. }
  199. ?>