PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/www/captcha.php

https://bitbucket.org/wayfarer/verse
PHP | 237 lines | 150 code | 43 blank | 44 comment | 23 complexity | 5e8edb105b23fefe7034ce67aa020c90 MD5 | raw file
Possible License(s): ISC, AGPL-3.0, LGPL-2.1, BSD-3-Clause, LGPL-3.0
  1. <?php
  2. // generates captcha image and stores the key in the session
  3. // used for candles
  4. include("inc/verse.inc.php"); //main header - initializes Verse environment
  5. captchaimage();
  6. function captchaimage() {
  7. // start session before!
  8. // ##### IMAGE SETTINGS
  9. $width = 200;
  10. $height = 100;
  11. // ##### SET UP IMAGE AND COLORS
  12. $image = imagecreatetruecolor($width, $height);
  13. imagesetthickness($image, 1);
  14. imagealphablending($image, true);
  15. $color_black = imagecolorallocatealpha($image, 0, 0, 0, 0);
  16. $color_black_semi = imagecolorallocatealpha($image, 0, 0, 0, 115);
  17. $color_white = imagecolorallocatealpha($image, 255, 255, 255, 0);
  18. imagefill($image, 0, 0, $color_white);
  19. imagecolortransparent($image, $color_white);
  20. // ##### BUILD RANDOM PASSWORD
  21. $acceptedCharsV = "AEIOUY";
  22. $acceptedCharsC = "BCDFGHJKLMNPQRSTVWXZ";
  23. $wordbuild = array(
  24. "cvcc", "ccvc", "ccvcc", "cvccc", // monosyllabic nominal stems
  25. "cvcvc", "cvcv", "cvccv", "ccvcv" // disyllabic nominal stems
  26. );
  27. $thisword = $wordbuild[mt_rand(0, sizeof($wordbuild) - 1)];
  28. $stringlength = strlen($thisword);
  29. for ($i = 0; $i < $stringlength; $i++) {
  30. if ($thisword[$i] == "c") {
  31. $password .= $acceptedCharsC{mt_rand(0, strlen($acceptedCharsC) - 1)};
  32. }
  33. if ($thisword[$i] == "v") {
  34. $password .= $acceptedCharsV{mt_rand(0, strlen($acceptedCharsV) - 1)};
  35. }
  36. }
  37. // ##### DRAW RANDOM LETTERS
  38. for ($i = 0; $i < 50; $i++) {
  39. $color = imagecolorallocatealpha($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255), 110);
  40. imagestring($image, mt_rand(1, 3), mt_rand(-$width * 0.25, $width * 1.25), mt_rand(-$height * 0.25, $height * 1.25),
  41. $acceptedCharsC{mt_rand(0, strlen($acceptedCharsC) - 1)}, $color);
  42. imagestring($image, mt_rand(1, 3), mt_rand(-$width * 0.25, $width * 1.25), mt_rand(-$height * 0.25, $height * 1.25),
  43. $acceptedCharsV{mt_rand(0, strlen($acceptedCharsV) - 1)}, $color);
  44. }
  45. // ##### DRAW PASSWORD
  46. for ($i = 0; $i < $stringlength; $i++) {
  47. $buffer = imagecreatetruecolor(40, 40);
  48. imagefill($buffer, 0, 0, $color_white);
  49. imagecolortransparent($buffer, $color_white);
  50. $buffer2 = imagecreatetruecolor(40, 40);
  51. imagefill($buffer2, 0, 0, $color_white);
  52. imagecolortransparent($buffer2, $color_white);
  53. $red = 0;
  54. $green = 0;
  55. $blue = 0;
  56. while ($red + $green + $blue < 400 || $red + $green + $blue > 450) {
  57. $red = mt_rand(0, 255);
  58. $green = mt_rand(0, 255);
  59. $blue = mt_rand(0, 255);
  60. }
  61. $color = imagecolorallocate($buffer, $red, $green, $blue);
  62. imagestring($buffer, 2, 0, 0, substr($password, $i, 1), $color);
  63. imagecopyresized($buffer2, $buffer, 2, -5, 0, 0, mt_rand(30, 40), mt_rand(30, 40), 10, 14);
  64. // $buffer=imagerotate($buffer2,mt_rand(-25,25),$color_white);
  65. $buffer = imageRotateBicubic($buffer2, mt_rand(-25, 25));
  66. $xpos = $i / $stringlength * ($width - 30) + (($width - 30) / $stringlength / 2) + 5 + mt_rand(-8, 8);
  67. $ypos = (($height - 50) / 2) + 5 + mt_rand(-8, 8);
  68. imagecolortransparent($buffer, $color_white);
  69. imagecopymerge($image, $buffer, $xpos, $ypos, 0, 0, imagesx($buffer), imagesy($buffer), 100);
  70. imagedestroy($buffer);
  71. imagedestroy($buffer2);
  72. }
  73. // ##### DRAW ELLIPSES
  74. for ($i = 0; $i < 12; $i++) {
  75. $color = imagecolorallocatealpha($image, mt_rand(0, 200), mt_rand(0, 200), mt_rand(0, 200), 110);
  76. imagefilledellipse($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(10, 40), mt_rand(10, 40), $color);
  77. }
  78. // ##### DRAW LINES
  79. for ($i = 0; $i < 12; $i++) {
  80. $color = imagecolorallocatealpha($image, mt_rand(0, 200), mt_rand(0, 200), mt_rand(0, 200), 110);
  81. imagesetthickness($image, mt_rand(8, 20));
  82. imageline($image, mt_rand(-$width * 0.25, $width * 1.25), mt_rand(-$height * 0.25, $height * 1.25),
  83. mt_rand(-$width * 0.25, $width * 1.25), mt_rand(-$height * 0.25, $height * 1.25), $color);
  84. imagesetthickness($image, 1);
  85. }
  86. /*
  87. // ##### WOBBLE HORIZONTALLY
  88. $sindivide=mt_rand(1,20);
  89. $sinwidth=mt_rand(1,$sindivide)/4;
  90. for ($i=0;$i<$height;$i++) {
  91. $buffer=imagecreatetruecolor($width,1);
  92. imagecopy($buffer,$image,0,0,0,$i,$width,1);
  93. imageline($image,0,$i,$width,$i,$color_white);
  94. imagecopy($image,$buffer,(sin($i/$sindivide)-.5)*2*$sinwidth,$i,0,0,$width,1);
  95. imagedestroy($buffer);
  96. }
  97. // ##### WOBBLE VERTICALLY
  98. $sindivide=mt_rand(1,20);
  99. $sinwidth=mt_rand(1,$sindivide)/4;
  100. for ($i=0;$i<$width;$i++) {
  101. $buffer=imagecreatetruecolor(1,$height);
  102. imagecopy($buffer,$image,0,0,$i,0,1,$height);
  103. imageline($image,$i,0,$i,$width,$color_white);
  104. imagecopy($image,$buffer,$i,(sin($i/$sindivide)-.5)*2*$sinwidth,0,0,1,$height);
  105. imagedestroy($buffer);
  106. }
  107. */
  108. // ##### GRADIENT BACKGROUND HORIZONTALLY
  109. $red_from = mt_rand(0, 255);
  110. $red_to = mt_rand(0, 255);
  111. $green_from = mt_rand(0, 255);
  112. $green_to = mt_rand(0, 255);
  113. $blue_from = mt_rand(0, 255);
  114. $blue_to = mt_rand(0, 255);
  115. for ($i = 0; $i < $height; $i++) {
  116. $color = imagecolorallocatealpha($image, $red_from + ($red_to - $red_from) / $height * $i, $green_from + ($green_to - $green_from) / $height * $i, $blue_from + ($blue_to - $blue_from) / $height * $i, 100);
  117. imageline($image, 0, $i, $width, $i, $color);
  118. }
  119. // ##### GRADIENT BACKGROUND VERTICALLY
  120. $red_from = mt_rand(0, 255);
  121. $red_to = mt_rand(0, 255);
  122. $green_from = mt_rand(0, 255);
  123. $green_to = mt_rand(0, 255);
  124. $blue_from = mt_rand(0, 255);
  125. $blue_to = mt_rand(0, 255);
  126. for ($i = 0; $i < $width; $i++) {
  127. $color = imagecolorallocatealpha($image, $red_from + ($red_to - $red_from) / $width * $i, $green_from + ($green_to - $green_from) / $width * $i, $blue_from + ($blue_to - $blue_from) / $width * $i, 100);
  128. imageline($image, $i, 0, $i, $height, $color);
  129. }
  130. // ##### TAG
  131. $color = imagecolorallocatealpha($image, 255, 255, 255, 90);
  132. imagefilledrectangle($image, 1, 1, 146, 8, $color);
  133. $color = imagecolorallocatealpha($image, 0, 0, 0, 100);
  134. // imagestring($image,1,2,1,"jhfCAPTCHA: find ".strlen($password)." characters",$color);
  135. // ##### STORE PASSWORD
  136. $_SESSION["captcha"] = $password;
  137. // ##### OUTPUT
  138. header('Content-Type: image/png');
  139. imagepng($image);
  140. imagedestroy($image);
  141. }
  142. function imageRotateBicubic($src_img, $angle, $bicubic = false) {
  143. // convert degrees to radians
  144. $angle = $angle + 180;
  145. $angle = deg2rad($angle);
  146. $src_x = imagesx($src_img);
  147. $src_y = imagesy($src_img);
  148. $center_x = floor($src_x / 2);
  149. $center_y = floor($src_y / 2);
  150. $rotate = imagecreatetruecolor($src_x, $src_y);
  151. imagealphablending($rotate, false);
  152. imagesavealpha($rotate, true);
  153. $cosangle = cos($angle);
  154. $sinangle = sin($angle);
  155. for ($y = 0; $y < $src_y; $y++) {
  156. for ($x = 0; $x < $src_x; $x++) {
  157. // rotate...
  158. $old_x = (($center_x - $x) * $cosangle + ($center_y - $y) * $sinangle)
  159. + $center_x;
  160. $old_y = (($center_y - $y) * $cosangle - ($center_x - $x) * $sinangle)
  161. + $center_y;
  162. if ($old_x >= 0 && $old_x < $src_x
  163. && $old_y >= 0 && $old_y < $src_y
  164. ) {
  165. if ($bicubic == true) {
  166. $sY = $old_y + 1;
  167. $siY = $old_y;
  168. $siY2 = $old_y - 1;
  169. $sX = $old_x + 1;
  170. $siX = $old_x;
  171. $siX2 = $old_x - 1;
  172. $c1 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX, $siY2));
  173. $c2 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX, $siY));
  174. $c3 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX2, $siY2));
  175. $c4 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX2, $siY));
  176. $r = ($c1['red'] + $c2['red'] + $c3['red'] + $c4['red']) << 14;
  177. $g = ($c1['green'] + $c2['green'] + $c3['green'] + $c4['green']) << 6;
  178. $b = ($c1['blue'] + $c2['blue'] + $c3['blue'] + $c4['blue']) >> 2;
  179. $a = ($c1['alpha'] + $c2['alpha'] + $c3['alpha'] + $c4['alpha']) >> 2;
  180. $color = imagecolorallocatealpha($src_img, $r, $g, $b, $a);
  181. } else {
  182. $color = imagecolorat($src_img, $old_x, $old_y);
  183. }
  184. } else {
  185. // this line sets the background colour
  186. $color = imagecolorallocatealpha($src_img, 255, 255, 255, 127);
  187. }
  188. imagesetpixel($rotate, $x, $y, $color);
  189. }
  190. }
  191. return $rotate;
  192. }