PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/captcha.php

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