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

/include/includes/libs/captcha/captcha.php

https://bitbucket.org/GeCk0/ilchshop
PHP | 368 lines | 317 code | 36 blank | 15 comment | 33 complexity | d1d54f5607263c3f1c960d1f7595cbaf MD5 | raw file
  1. <?php
  2. # www.ilch.de
  3. # Author: T0P0LIN0
  4. # thanks to uwe slick! http://www.deruwe.de/captcha.html - his thoughts
  5. class Captcha
  6. {
  7. var $memory;
  8. var $passphrase = null;
  9. var $width = 170;
  10. var $height = 60;
  11. var $fontsPath = "fonts";
  12. var $fontColor = array( );
  13. var $font_type = 5;
  14. var $useRandomColors = true;
  15. var $bgColor = array( );
  16. var $passphraselenght = 5;
  17. var $fontSize = 20;
  18. var $image = null;
  19. var $angle = 45;
  20. var $scratches = true;
  21. var $background_intensity = 50;
  22. var $addhorizontallines = true;
  23. var $image_font_width;
  24. var $image_font_height;
  25. var $scraches_amount = 25;
  26. var $minsize = 20;
  27. var $maxsize = 30;
  28. var $addagrid = true;
  29. var $character = array( 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'Q', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'U', 'V', 'Y', 'W', '2', '3', '4', '5', '6', '7' );
  30. function Captcha( )
  31. {
  32. include 'memory.php';
  33. $this->memory = new Memory();
  34. }
  35. function isValid( $number )
  36. {
  37. return $this->memory->numberExists( $number );
  38. }
  39. function check_type_support( )
  40. {
  41. $get_info = @gd_info();
  42. $Version = @ereg_replace( '[[:alpha:][:space:]()]+', '', $get_info[ 'GD Version' ] );
  43. $get_info[ 'GD VERSION' ] = $Version;
  44. if ( !is_array( $get_info ) ) {
  45. return false;
  46. } else {
  47. return $get_info;
  48. }
  49. }
  50. function open_captcha_image( )
  51. {
  52. // Grafik anlegen
  53. $gd_lib_version = $this->check_type_support();
  54. if ( ( is_array( $gd_lib_version ) ) && ( $gd_lib_version[ 'GD VERSION' ] > "2.0.0" ) )
  55. return ImageCreateTrueColor( $this->width, $this->height );
  56. else
  57. return ImageCreate( $this->width, $this->height );
  58. }
  59. function setUseRandomColors( $useRandomColors = false )
  60. {
  61. $this->fontColor[ 'r' ] = 0;
  62. $this->fontColor[ 'g' ] = 0;
  63. $this->fontColor[ 'b' ] = 0;
  64. $this->bgColor[ 'r' ] = 225;
  65. $this->bgColor[ 'g' ] = 225;
  66. $this->bgColor[ 'b' ] = 225;
  67. $this->useRandomColors = $useRandomColors;
  68. }
  69. function setFontColor( $r, $g, $b )
  70. {
  71. $this->fontColor[ 'r' ] = $r;
  72. $this->fontColor[ 'g' ] = $g;
  73. $this->fontColor[ 'b' ] = $b;
  74. }
  75. function setBgColor( $r, $g, $b )
  76. {
  77. $this->bgColor[ 'r' ] = $r;
  78. $this->bgColor[ 'g' ] = $g;
  79. $this->bgColor[ 'b' ] = $b;
  80. }
  81. function set_character( $character )
  82. {
  83. $this->character = $character;
  84. }
  85. function set_background_intensity( $background_intensity = 50 )
  86. {
  87. $this->background_intensity = $background_intensity;
  88. }
  89. function set_angle( $angle = 45 )
  90. {
  91. $this->angle = $angle;
  92. }
  93. function setImageWidth( $width )
  94. {
  95. $this->width = $width;
  96. }
  97. function setImageHeight( $height )
  98. {
  99. $this->height = $height;
  100. }
  101. function setFontsPath( $fontsPath )
  102. {
  103. $this->fontsPath = $fontsPath;
  104. }
  105. function setFontSize( $size )
  106. {
  107. $this->fontSize = $size;
  108. }
  109. function set_minmax_size( $minsize = 20, $maxsize = 30 )
  110. {
  111. $this->minsize = $minsize;
  112. $this->maxsize = $maxsize;
  113. }
  114. function set_font_type( $font_type = 5 )
  115. {
  116. $this->font_type = $font_type;
  117. }
  118. function enable_scratches( $scratches = true )
  119. {
  120. $this->scratches = $scratches;
  121. }
  122. function set_scratches_amount( $amount = 25 )
  123. {
  124. $this->scraches_amount = $amount;
  125. }
  126. function set_showgrid( $what = true )
  127. {
  128. $this->addagrid = $what;
  129. }
  130. function set_showcoloredlines( $what = true )
  131. {
  132. $this->addhorizontallines = $what;
  133. }
  134. function setPassPhraselenght( $passphraselenght )
  135. {
  136. $this->passphraselenght = $passphraselenght;
  137. }
  138. function getRandomFont( )
  139. {
  140. static $fonts = array( );
  141. if ( count( $fonts ) == 0 ) {
  142. $dh = opendir( $this->fontsPath );
  143. while ( $font = readdir( $dh ) ) {
  144. if ( ( $font != "." ) && ( $font != ".." ) ) {
  145. if ( substr( strtolower( $font ), -3 ) == "ttf" ) {
  146. $fonts[ ] = sprintf( "%s/%s", $this->fontsPath, $font );
  147. }
  148. }
  149. }
  150. closedir( $dh );
  151. }
  152. return $fonts[ rand( 0, count( $fonts ) - 1 ) ];
  153. }
  154. function inRgbTolerance( $originalColors, $newColors )
  155. {
  156. $matches = 0;
  157. foreach ( $originalColors as $rgbIdx => $value ) {
  158. if ( abs( $newColors[ $rgbIdx ] - $value ) < 60 ) {
  159. return false;
  160. }
  161. }
  162. return true;
  163. }
  164. function create_captcha_background( )
  165. {
  166. // Breite eines Zeichens
  167. $this->image_font_width = ImageFontWidth( $this->font_type ) + 2;
  168. // Hoehe eines Zeichens
  169. $this->image_font_height = ImageFontHeight( $this->font_type ) + 2;
  170. // Zufallswerte für hintergrundfarbe
  171. if ( $this->useRandomColors ) {
  172. $this->setBgColor( intval( rand( 225, 255 ) ), intval( rand( 225, 255 ) ), intval( rand( 225, 255 ) ) );
  173. } else {
  174. $this->setBgColor( 225, 225, 225 );
  175. }
  176. // Hintergrund-Farbe stzen
  177. $captcha_background_color = ImageColorAllocate( $this->image, $this->bgColor[ 'r' ], $this->bgColor[ 'g' ], $this->bgColor[ 'b' ] );
  178. // Flaeche fuellen
  179. ImageFilledRectangle( $this->image, 0, 0, $this->width, $this->height, $captcha_background_color );
  180. // Zufallsstrings durchloopen
  181. for ( $x = 0; $x < $this->background_intensity; $x++ ) {
  182. // Zufallsstring-Farbe
  183. $random_string_color = ImageColorAllocate( $this->image, intval( rand( 164, 254 ) ), intval( rand( 164, 254 ) ), intval( rand( 164, 254 ) ) );
  184. // Zufalls-String generieren
  185. $random_string = chr( intval( rand( 65, 122 ) ) );
  186. // X-Position
  187. $x_position = intval( rand( 0, $this->width - $this->image_font_width * strlen( $random_string ) ) );
  188. // Y-Position
  189. $y_position = intval( rand( 0, $this->height - $this->image_font_height ) );
  190. // Zufalls-String
  191. ImageString( $this->image, $this->font_type, $x_position, $y_position, $random_string, $random_string_color );
  192. }
  193. if ( $this->addagrid ) {
  194. $this->addgrid();
  195. }
  196. if ( $this->addhorizontallines ) {
  197. $this->addhorlines();
  198. }
  199. }
  200. function create_scratches( )
  201. {
  202. for ( $i = 1; $i < $this->scraches_amount; $i++ ) {
  203. $randPixSpaceLeft = mt_rand( 0, $this->width );
  204. $randPixSpaceTop = mt_rand( 0, $this->height );
  205. $style = mt_rand( 0, 2 );
  206. if ( 0 == $style ) {
  207. $txtColor = array(
  208. mt_rand( 0, 255 ),
  209. mt_rand( 0, 255 ),
  210. mt_rand( 0, 255 )
  211. );
  212. ImageLine( $this->image, $randPixSpaceLeft, $randPixSpaceTop, $randPixSpaceLeft + 10, $randPixSpaceTop + 7, $txtColor );
  213. } elseif ( 1 == $style ) {
  214. $noiseColor = array(
  215. mt_rand( 0, 255 ),
  216. mt_rand( 0, 255 ),
  217. mt_rand( 0, 255 )
  218. );
  219. ImageLine( $this->image, $randPixSpaceLeft, $randPixSpaceTop, $randPixSpaceLeft - 3, $randPixSpaceTop + 7, $noiseColor );
  220. } else {
  221. $bgColor = array(
  222. mt_rand( 0, 255 ),
  223. mt_rand( 0, 255 ),
  224. mt_rand( 0, 255 )
  225. );
  226. ImageLine( $this->image, $randPixSpaceLeft, $randPixSpaceTop, $randPixSpaceLeft - 5, $randPixSpaceTop - 5, $bgColor );
  227. }
  228. }
  229. }
  230. function displayImage( )
  231. {
  232. $this->image = $this->open_captcha_image();
  233. $this->create_captcha_background();
  234. $numbers = '';
  235. $phraseLength = $this->passphraselenght;
  236. $widthPerChar = $this->width / $phraseLength;
  237. $heightPerChar = $this->height - 2; //2pix spacing...
  238. $color = imagecolorallocate( $this->image, $this->fontColor[ 'r' ], $this->fontColor[ 'g' ], $this->fontColor[ 'b' ] );
  239. for ( $idx = 0; $idx < $phraseLength; $idx++ ) {
  240. $number = $this->character[ rand( 0, count( $this->character ) - 1 ) ];
  241. $currentFont = $this->getRandomFont();
  242. $disangle = rand( -$this->angle, $this->angle );
  243. $charInfo = imageftbbox( $this->fontSize, $disangle, $currentFont, $number );
  244. $charWidth = $charInfo[ 4 ] - $charInfo[ 6 ];
  245. if ( $charWidth > $widthPerChar ) {
  246. echo "Please increase image width or use a smaller fon/font size";
  247. exit( 1 );
  248. }
  249. $xMargin = ( $widthPerChar - $charWidth ) / 2;
  250. $x = ( $idx * $widthPerChar ) + $xMargin;
  251. $charHeight = $charInfo[ 1 ] - $charInfo[ 7 ];
  252. if ( $charHeight > $heightPerChar ) {
  253. echo "Please increase image height or use a smaller fon/font size";
  254. exit( 1 );
  255. }
  256. $baseline = ( $heightPerChar - $charHeight ) / 2;
  257. $y = $baseline + $charHeight;
  258. if ( $this->useRandomColors ) {
  259. do {
  260. $r = rand( 0, 255 );
  261. $g = rand( 0, 255 );
  262. $b = rand( 0, 255 );
  263. } while ( !$this->inRgbTolerance( $this->bgColor, array(
  264. "r" => $r,
  265. "g" => $g,
  266. "b" => $b
  267. ) ) );
  268. $color = imagecolorallocate( $this->image, $r, $g, $b );
  269. }
  270. $numbers .= $number;
  271. imagettftext( $this->image, $this->fontSize, $disangle, $x, $y, $color, $currentFont, $number );
  272. }
  273. if ( $this->scratches ) {
  274. $this->create_scratches( $color );
  275. }
  276. header( "Content-type: image/jpeg" );
  277. imagejpeg( $this->image );
  278. $this->memory->saveNumber( $numbers );
  279. }
  280. function addhorlines( )
  281. {
  282. $grey = imagecolorallocate( $this->image, 235, 235, 235 );
  283. $white = imagecolorallocate( $this->image, 255, 255, 255 );
  284. $black = imagecolorallocate( $this->image, 0, 0, 0 );
  285. $red = imagecolorallocatealpha( $this->image, 255, 0, 0, 75 );
  286. $green = imagecolorallocatealpha( $this->image, 0, 255, 0, 75 );
  287. $blue = imagecolorallocatealpha( $this->image, 0, 0, 255, 75 );
  288. imageline( $this->image, rand( 1, $this->width ), rand( 1, $this->height ), rand( 101, $this->width ), rand( 26, $this->height ), $red );
  289. imageline( $this->image, rand( 1, $this->width ), rand( 1, $this->height ), rand( 101, $this->width ), rand( 26, $this->height ), $green );
  290. imageline( $this->image, rand( 1, $this->width ), rand( 1, $this->height ), rand( 101, $this->width ), rand( 26, $this->height ), $blue );
  291. imageline( $this->image, rand( 1, $this->width ), rand( 1, $this->height ), rand( 101, $this->width ), rand( 26, $this->height ), $red );
  292. imageline( $this->image, rand( 1, $this->width ), rand( 1, $this->height ), rand( 101, $this->width ), rand( 26, $this->height ), $green );
  293. imageline( $this->image, rand( 1, $this->width ), rand( 1, $this->height ), rand( 101, $this->width ), rand( 26, $this->height ), $blue );
  294. }
  295. function random_color( $min, $max )
  296. {
  297. $randcol[ 'r' ] = intval( rand( $min, $max ) );
  298. $randcol[ 'g' ] = intval( rand( $min, $max ) );
  299. $randcol[ 'b' ] = intval( rand( $min, $max ) );
  300. return $randcol;
  301. }
  302. function addgrid( )
  303. {
  304. for ( $i = 0; $i < $this->width; $i += (int) ( $this->minsize / 1.5 ) ) {
  305. $randcol = $this->random_color( 160, 224 );
  306. $color = imagecolorallocate( $this->image, $randcol[ 'r' ], $randcol[ 'g' ], $randcol[ 'b' ] );
  307. @imageline( $this->image, $i, 0, $i, $this->height, $color );
  308. }
  309. for ( $i = 0; $i < $this->height; $i += (int) ( $this->minsize / 1.8 ) ) {
  310. $randcol = $this->random_color( 160, 224 );
  311. $color = imagecolorallocate( $this->image, $randcol[ 'r' ], $randcol[ 'g' ], $randcol[ 'b' ] );
  312. @imageline( $this->image, 0, $i, $this->width, $i, $color );
  313. }
  314. @imageline( $this->image, $this->width, 0, $this->width, $this->height, $color );
  315. @imageline( $this->image, 0, $this->height, $this->width, $this->height, $color );
  316. }
  317. function pickRandomBackground( )
  318. {
  319. $bg_color = imagecolorallocate( $this->image, 255, 255, 255 );
  320. imagefill( $this->image, 0, 0, $bg_color );
  321. for ( $i = 0; $i < $this->height; $i++ ) {
  322. $c = rand( 140, 170 );
  323. $d = rand( 0, 10 );
  324. $e = rand( 0, 10 );
  325. $f = rand( 0, 10 );
  326. $line_color = imagecolorallocate( $this->image, $c + $d, $c + $e, $c + $f );
  327. imagesetthickness( $this->image, rand( 1, 5 ) );
  328. imageline( $this->image, 0, $i + rand( -15, 15 ), $this->width, $i + rand( -15, 15 ), $line_color );
  329. }
  330. }
  331. }
  332. ?>