PageRenderTime 58ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_listbingo/addons/captcha/libraries/gbengine.php

https://github.com/gobingoo/Listbingo
PHP | 225 lines | 156 code | 33 blank | 36 comment | 12 complexity | 4c535cdc66e172a8501b8e06e6812cb6 MD5 | raw file
  1. <?php
  2. class GBCaptchaEngine
  3. {
  4. var $font;
  5. var $width=160;
  6. var $height=40;
  7. var $text_color;
  8. var $noise_color;
  9. var $bg_color;
  10. var $angle;
  11. var $bg_size;
  12. var $grid_color;
  13. var $first_range1=1;
  14. var $first_range2=5;
  15. var $second_range1=6;
  16. var $second_range2=11;
  17. var $characters=5;
  18. var $captcha_character_set ='23456789bcdfghjkmnpqrstvwxyz';
  19. function hex2rgb($color)
  20. {
  21. if ($color[0] == '#')
  22. $color = substr($color, 1);
  23. if (strlen($color) == 6)
  24. list($r, $g, $b) = array($color[0].$color[1],
  25. $color[2].$color[3],
  26. $color[4].$color[5]);
  27. elseif (strlen($color) == 3)
  28. list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
  29. else
  30. return false;
  31. $r = hexdec($r); $g = hexdec($g); $b = hexdec($b);
  32. return array($r, $g, $b);
  33. }
  34. function generateCode($characters)
  35. {
  36. /* list all possible characters, similar looking characters and vowels have been removed */
  37. $possible = $this->captcha_character_set;
  38. $code = '';
  39. $i = 0;
  40. while ($i < $characters)
  41. {
  42. $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
  43. $i++;
  44. }
  45. return $code;
  46. }
  47. function generateTextCaptcha()
  48. {
  49. $code = self::generateCode($this->characters);
  50. /* font size will be 75% of the image height */
  51. $font_size = $this->height * 0.75;
  52. $bcolor = self::hex2rgb($this->bg_color);
  53. $tcolor = self::hex2rgb($this->text_color);
  54. $ncolor = self::hex2rgb($this->noise_color);
  55. $image = @imagecreate($this->width, $this->height) or die('Cannot initialize new GD image stream');
  56. /* set the colours */
  57. $background_color = imagecolorallocate($image, $bcolor[0],$bcolor[1],$bcolor[2]);
  58. $this->text_color = imagecolorallocate($image, $tcolor[0],$tcolor[1],$tcolor[2]);
  59. $this->noise_color = imagecolorallocate($image, $ncolor[0],$ncolor[1],$ncolor[2]);
  60. imagefill( $image, 0, 0, $background_color );
  61. /* generate random dots in background */
  62. for( $i=0; $i<($this->width*$this->height)/3; $i++ )
  63. {
  64. imagefilledellipse($image, mt_rand(0,$this->width), mt_rand(0,$this->height), 1, 1, $this->noise_color);
  65. }
  66. /* generate random lines in background */
  67. for( $i=0; $i<($this->width*$this->height)/150; $i++ )
  68. {
  69. imageline($image, mt_rand(0,$this->width), mt_rand(0,$this->height), mt_rand(0,$this->width), mt_rand(0,$this->height), $this->noise_color);
  70. }
  71. for( $i=0; $i<($this->width*$this->height)/3; $i++ )
  72. {
  73. imagefilledellipse($image, mt_rand(0,$this->width), mt_rand(0,$this->height), 1, 1, $this->noise_color);
  74. }
  75. /* generate random lines in background */
  76. for( $i=0; $i<($this->width*$this->height)/150; $i++ )
  77. {
  78. imageline($image, mt_rand(0,$this->width), mt_rand(0,$this->height), mt_rand(0,$this->width), mt_rand(0,$this->height), $this->noise_color);
  79. }
  80. /* create textbox and add text */
  81. $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
  82. $x = ($this->width - $textbox[4])/2;
  83. $y = ($this->height - $textbox[5])/2;
  84. imagettftext($image, $font_size, 0, $x, $y, $this->text_color,$this->font , $code) or die('Error in imagettftext function');
  85. imagettftext($image, "10", 0,$this->width-48, $this->height-4, $this->text_color,$this->font , "gobingoo");
  86. /* output captcha image to browser */
  87. header('Content-Type: image/jpeg');
  88. imagejpeg($image);
  89. imagedestroy($image);
  90. $session=JFactory::getSession();
  91. $session->set('security_number',$code);
  92. exit;
  93. }
  94. function generateMathCaptcha()
  95. {
  96. $min_font_size = $this->height * 0.45;
  97. //set maximum font size
  98. $max_font_size = $this->height * 0.45;
  99. $operators=array('+','-','*');
  100. // first number random value; keep it lower than $second_num
  101. $first_num = rand($this->first_range1,$this->first_range2);
  102. // second number random value
  103. $second_num = rand($this->second_range1, $this->second_range2);
  104. //operation result is stored in $session_var
  105. eval("\$session_var=".$second_num.$operators[0].$first_num.";");
  106. // instantiate session
  107. $session=JFactory::getSession();
  108. // save the operation result in session to make verifications
  109. $session->set('security_number',$session_var);
  110. // start the captcha image
  111. $img = imagecreate( $this->width, $this->height );
  112. $bcolor = self::hex2rgb($this->bg_color);
  113. $tcolor = self::hex2rgb($this->text_color);
  114. $gcolor = self::hex2rgb($this->grid_color);
  115. $ncolor = self::hex2rgb($this->noise_color);
  116. // Some colors. Text is $black, background is $white, grid is $grey
  117. /*$black = imagecolorallocate($img,$tcolor[0],$tcolor[1],$tcolor[2]);
  118. $white = imagecolorallocate($img,255,255,255);
  119. $grey = imagecolorallocate($img,215,215,215);*/
  120. //set background color
  121. $background_color = imagecolorallocate($img, $bcolor[0],$bcolor[1],$bcolor[2]);
  122. //set text color
  123. $this->text_color = imagecolorallocate($img, $tcolor[0],$tcolor[1],$tcolor[2]);
  124. //set grid color
  125. $this->grid_color = imagecolorallocate($img,$gcolor[0],$gcolor[1],$gcolor[2]);
  126. //set noise color
  127. $this->noise_color = imagecolorallocate($img, $ncolor[0],$ncolor[1],$ncolor[2]);
  128. //fill the background color
  129. imagefill( $img, 0, 0, $background_color );
  130. // the background grid lines - vertical lines
  131. for ($t = $this->bg_size; $t<$this->width; $t+=$this->bg_size){
  132. imageline($img, $t, 0, $t, $this->height, $this->grid_color);
  133. }
  134. // background grid - horizontal lines
  135. for ($t = $this->bg_size; $t<$this->height; $t+=$this->bg_size){
  136. imageline($img, 0, $t, $this->width, $t, $this->grid_color);
  137. }
  138. /*
  139. this determinates the available space for each operation element
  140. it's used to position each element on the image so that they don't overlap
  141. */
  142. $item_space = $this->width/3;
  143. // first number
  144. imagettftext(
  145. $img,
  146. rand(
  147. $min_font_size,
  148. $max_font_size
  149. ),
  150. rand( -$this->angle , $this->angle ),
  151. rand( 10, $item_space-20 ),
  152. rand( 25, $this->height-25 ),
  153. $this->text_color,
  154. $this->font,
  155. $first_num);
  156. // operator
  157. imagettftext(
  158. $img,
  159. rand(
  160. $min_font_size,
  161. $max_font_size
  162. ),
  163. rand( -$this->angle, $this->angle ),
  164. rand( $item_space, 2*$item_space-20 ),
  165. rand( 25, $this->height-25 ),
  166. $this->text_color,
  167. $this->font,
  168. $operators[0]);
  169. // second number
  170. imagettftext(
  171. $img,
  172. rand(
  173. $min_font_size,
  174. $max_font_size
  175. ),
  176. rand( -$this->angle, $this->angle ),
  177. rand( 2*$item_space, 3*$item_space-20),
  178. rand( 25, $this->height-25 ),
  179. $this->text_color,
  180. $this->font,
  181. $second_num);
  182. imagettftext($img, "10", 0,$this->width-48, $this->height-4, $this->text_color,$this->font , "gobingoo");
  183. // image is .jpg
  184. header("Content-type:image/jpeg");
  185. // name is secure.jpg
  186. header("Content-Disposition:inline ; filename=secure.jpg");
  187. // output image
  188. imagejpeg($img);;
  189. exit;
  190. }
  191. }
  192. ?>