PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/mod2/kcaptcha/includes/kcaptcha.php

https://bitbucket.org/dynamis/phpbb
PHP | 271 lines | 197 code | 43 blank | 31 comment | 49 complexity | 14efce0c5940da9a9cd8277abe5f9028 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. # KCAPTCHA for phpBB
  3. # customized by dynamis based on:
  4. # KCAPTCHA PROJECT VERSION 1.2.4
  5. # Automatic test to tell computers and humans apart
  6. # Copyright by Kruglov Sergei, 2006
  7. # www.captcha.ru, www.kruglov.ru
  8. # System requirements: PHP 4.0.6+ w/ GD
  9. # KCAPTCHA is a free software. You can freely use it for building own site or software.
  10. # If you use this software as a part of own sofware, you must leave copyright notices intact or add KCAPTCHA copyright notices to own.
  11. # As a default configuration, KCAPTCHA has a small credits text at bottom of CAPTCHA image.
  12. # You can remove it, but I would be pleased if you left it. ;)
  13. class KCAPTCHA {
  14. var $keystring;
  15. # symbols used to draw CAPTCHA
  16. //var $allowed_symbols = "0123456789"; #digits
  17. var $allowed_symbols = "23456789abcdeghkmnpqsuvxyz"; # without similar symbols (o=0, 1=l, i=j, t=f)
  18. var $min_length = 5;
  19. var $max_length = 6;
  20. # folder with fonts
  21. var $fontsdir = 'fonts';
  22. # CAPTCHA image size
  23. var $width = 150;
  24. var $height = 60;
  25. # CAPTCHA image colors (RGB, 0-255)
  26. //var $foreground_color = array(0, 0, 0);
  27. //var $background_color = array(220, 230, 255);
  28. var $foreground_color; // set in constructor
  29. var $background_color; // set in constructor
  30. # symbol's vertical fluctuation amplitude divided by 2
  31. var $fluctuation_amplitude = 5;
  32. # increase safety by prevention of spaces between symbols
  33. var $no_spaces = true;
  34. # JPEG quality of CAPTCHA image
  35. var $jpeg_quality = 90;
  36. function getKeyString() {
  37. return $this->keystring;
  38. }
  39. function setKeyString($newstring = '') {
  40. $invalidpattern = '/cp|cb|ck|c6|c9|rn|rm|mm|mn|nm|co|do|cl|db|qp|qb|dp/';
  41. if($newstring){
  42. $length = strlen($newstring);
  43. if($length < $this->min_length || $length > $this->max_length) return false;
  44. for($i=0;$i<$length;$i++){
  45. if(strpos($this->allowed_symbols, substr($newstring, $i, 1)) === false) return false;
  46. }
  47. if(preg_match($invalidpattern, $newstring)) return false;
  48. $this->keystring = $newstring;
  49. }else {
  50. // generating random keystring
  51. $length = mt_rand($this->min_length, $this->max_length);
  52. while(true){
  53. $this->keystring='';
  54. for($i=0;$i<$length;$i++){
  55. $this->keystring.=$this->allowed_symbols{mt_rand(0,strlen($this->allowed_symbols)-1)};
  56. }
  57. if(!preg_match($invalidpattern, $this->keystring)) break;
  58. }
  59. }
  60. return $this->keystring;
  61. }
  62. // generates keystring and image
  63. function KCAPTCHA() {
  64. # CAPTCHA image colors (RGB, 0-255)
  65. $this->foreground_color = array(mt_rand(0,100), mt_rand(0,100), mt_rand(0,100));
  66. $this->background_color = array(mt_rand(200,255), mt_rand(200,255), mt_rand(200,255));
  67. $this->keystring = $this->setKeyString();
  68. }
  69. function genCaptcha() {
  70. $alphabet = "0123456789abcdefghijklmnopqrstuvwxyz"; # do not change without changing font files!
  71. $fonts=array();
  72. $fontsdir_absolute=dirname(__FILE__).'/'.$this->fontsdir;
  73. if ($handle = opendir($fontsdir_absolute)) {
  74. while (false !== ($file = readdir($handle))) {
  75. if (preg_match('/\.png$/i', $file)) {
  76. $fonts[]=$fontsdir_absolute.'/'.$file;
  77. }
  78. }
  79. closedir($handle);
  80. }
  81. $alphabet_length=strlen($alphabet);
  82. while(true){
  83. $font_file=$fonts[mt_rand(0, count($fonts)-1)];
  84. $font=imagecreatefrompng($font_file);
  85. imagealphablending($font, true);
  86. $fontfile_width=imagesx($font);
  87. $fontfile_height=imagesy($font)-1;
  88. $font_metrics=array();
  89. $symbol=0;
  90. $reading_symbol=false;
  91. // loading font
  92. for($i=0;$i<$fontfile_width && $symbol<$alphabet_length;$i++){
  93. $transparent = (imagecolorat($font, $i, 0) >> 24) == 127;
  94. if(!$reading_symbol && !$transparent){
  95. $font_metrics[$alphabet{$symbol}]=array('start'=>$i);
  96. $reading_symbol=true;
  97. continue;
  98. }
  99. if($reading_symbol && $transparent){
  100. $font_metrics[$alphabet{$symbol}]['end']=$i;
  101. $reading_symbol=false;
  102. $symbol++;
  103. continue;
  104. }
  105. }
  106. $img=imagecreatetruecolor($this->width, $this->height);
  107. imagealphablending($img, true);
  108. $white=imagecolorallocate($img, 255, 255, 255);
  109. $black=imagecolorallocate($img, 0, 0, 0);
  110. imagefilledrectangle($img, 0, 0, $this->width-1, $this->height-1, $white);
  111. // draw text
  112. $x=1;
  113. $length = strlen($this->keystring);
  114. for($i=0;$i<$length;$i++){
  115. $m=$font_metrics[$this->keystring{$i}];
  116. $y=mt_rand(-$this->fluctuation_amplitude, $this->fluctuation_amplitude)+($this->height-$fontfile_height)/2+2;
  117. if($this->no_spaces){
  118. $shift=0;
  119. if($i>0){
  120. $shift=1000;
  121. for($sy=7;$sy<$fontfile_height-20;$sy+=1){
  122. //for($sx=$m['start']-1;$sx<$m['end'];$sx+=1){
  123. for($sx=$m['start']-1;$sx<$m['end'];$sx+=1){
  124. $rgb=imagecolorat($font, $sx, $sy);
  125. $opacity=$rgb>>24;
  126. if($opacity<127){
  127. $left=$sx-$m['start']+$x;
  128. $py=$sy+$y;
  129. if($py>$this->height) break;
  130. for($px=min($left,$this->width-1);$px>$left-12 && $px>=0;$px-=1){
  131. $color=imagecolorat($img, $px, $py) & 0xff;
  132. if($color+$opacity<190){
  133. if($shift>$left-$px){
  134. $shift=$left-$px;
  135. }
  136. break;
  137. }
  138. }
  139. break;
  140. }
  141. }
  142. }
  143. if($shift==1000){
  144. $shift=mt_rand(4,6);
  145. }
  146. }
  147. }else{
  148. $shift=1;
  149. }
  150. imagecopy($img,$font,$x-$shift,$y,$m['start'],1,$m['end']-$m['start'],$fontfile_height);
  151. $x+=$m['end']-$m['start']-$shift;
  152. }
  153. if($x<$this->width-10) break; // fit in canvas
  154. }
  155. $center=$x/2;
  156. $img2=imagecreatetruecolor($this->width, $this->height);
  157. $foreground=imagecolorallocate($img2, $this->foreground_color[0], $this->foreground_color[1], $this->foreground_color[2]);
  158. $background=imagecolorallocate($img2, $this->background_color[0], $this->background_color[1], $this->background_color[2]);
  159. // periods
  160. $rand1=mt_rand(750000,1200000)/10000000;
  161. $rand2=mt_rand(750000,1200000)/10000000;
  162. $rand3=mt_rand(750000,1200000)/10000000;
  163. $rand4=mt_rand(750000,1200000)/10000000;
  164. // phases
  165. $rand5=mt_rand(0,3141592)/500000;
  166. $rand6=mt_rand(0,3141592)/500000;
  167. $rand7=mt_rand(0,3141592)/500000;
  168. $rand8=mt_rand(0,3141592)/500000;
  169. // amplitudes
  170. $rand9=mt_rand(330,420)/110;
  171. $rand10=mt_rand(330,450)/110;
  172. //wave distortion
  173. for($x=0;$x<$this->width;$x++){
  174. for($y=0;$y<$this->height;$y++){
  175. $sx=$x+(sin($x*$rand1+$rand5)+sin($y*$rand3+$rand6))*$rand9-$this->width/2+$center+1;
  176. $sy=$y+(sin($x*$rand2+$rand7)+sin($y*$rand4+$rand8))*$rand10;
  177. if($sx<0 || $sy<0 || $sx>=$this->width-1 || $sy>=$this->height-1){
  178. $color=255;
  179. $color_x=255;
  180. $color_y=255;
  181. $color_xy=255;
  182. }else{
  183. $color=imagecolorat($img, $sx, $sy) & 0xFF;
  184. $color_x=imagecolorat($img, $sx+1, $sy) & 0xFF;
  185. $color_y=imagecolorat($img, $sx, $sy+1) & 0xFF;
  186. $color_xy=imagecolorat($img, $sx+1, $sy+1) & 0xFF;
  187. }
  188. if($color==0 && $color_x==0 && $color_y==0 && $color_xy==0){
  189. $newred=$this->foreground_color[0];
  190. $newgreen=$this->foreground_color[1];
  191. $newblue=$this->foreground_color[2];
  192. }else if($color==255 && $color_x==255 && $color_y==255 && $color_xy==255){
  193. $newred=$this->background_color[0];
  194. $newgreen=$this->background_color[1];
  195. $newblue=$this->background_color[2];
  196. }else{
  197. $frsx=$sx-floor($sx);
  198. $frsy=$sy-floor($sy);
  199. $frsx1=1-$frsx;
  200. $frsy1=1-$frsy;
  201. $newcolor=(
  202. $color*$frsx1*$frsy1+
  203. $color_x*$frsx*$frsy1+
  204. $color_y*$frsx1*$frsy+
  205. $color_xy*$frsx*$frsy);
  206. if($newcolor>255) $newcolor=255;
  207. $newcolor=$newcolor/255;
  208. $newcolor0=1-$newcolor;
  209. $newred=$newcolor0*$this->foreground_color[0]+$newcolor*$this->background_color[0];
  210. $newgreen=$newcolor0*$this->foreground_color[1]+$newcolor*$this->background_color[1];
  211. $newblue=$newcolor0*$this->foreground_color[2]+$newcolor*$this->background_color[2];
  212. }
  213. imagesetpixel($img2, $x, $y, imagecolorallocate($img2, $newred, $newgreen, $newblue));
  214. }
  215. }
  216. if(function_exists("imagepng")){
  217. header("Content-Type: image/x-png");
  218. imagepng($img2);
  219. }else if(function_exists("imagejpeg")){
  220. header("Content-Type: image/jpeg");
  221. imagejpeg($img2, null, $this->jpeg_quality);
  222. }else if(function_exists("imagegif")){
  223. header("Content-Type: image/gif");
  224. imagegif($img2);
  225. }
  226. }
  227. }
  228. ?>