PageRenderTime 63ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 1ms

/make_captcha_img.php

https://github.com/fusenigk/mantisbt-1
PHP | 308 lines | 209 code | 27 blank | 72 comment | 45 complexity | fde82270304105ef7dbc0bb53f569454 MD5 | raw file
  1. <?php
  2. # MantisBT - A PHP based bugtracking system
  3. # MantisBT is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # MantisBT is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
  15. /**
  16. * @package MantisBT
  17. * @author Marcello Scata' <marcelloscata at users.sourceforge.net> ITALY
  18. * @copyright Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
  19. * @copyright Copyright (C) 2002 - 2011 MantisBT Team - mantisbt-dev@lists.sourceforge.net
  20. * @link http://www.mantisbt.org
  21. *
  22. * @uses core.php
  23. * @uses config_api.php
  24. * @uses crypto_api.php
  25. * @uses gpc_api.php
  26. * @uses utility_api.php
  27. */
  28. /**
  29. * MantisBT Core API's
  30. */
  31. require_once( 'core.php' );
  32. require_api( 'config_api.php' );
  33. require_api( 'crypto_api.php' );
  34. require_api( 'gpc_api.php' );
  35. require_api( 'utility_api.php' );
  36. $f_public_key = gpc_get_string( 'public_key' );
  37. $t_private_key = substr( hash( 'whirlpool', 'captcha' . config_get_global( 'crypto_master_salt' ) . $f_public_key, false ), 0, 5 );
  38. $t_system_font_folder = get_font_path();
  39. $t_font_per_captcha = config_get( 'font_per_captcha' );
  40. $t_captcha_init = array(
  41. 'TTF_folder' => $t_system_font_folder,
  42. 'TTF_RANGE' => array( $t_font_per_captcha )
  43. );
  44. $captcha = new masc_captcha( $t_captcha_init );
  45. $captcha->make_captcha( $t_private_key );
  46. #
  47. # The class below was derived from
  48. # http://www.phpclasses.org/browse/package/1163.html
  49. #
  50. # *** 3.0 Author
  51. # Pascal Rehfeldt
  52. # Pascal@Pascal-Rehfeldt.com
  53. #
  54. # http://www.phpclasses.org/browse.html/author/102754.html
  55. #
  56. #
  57. # *** 3.1 License
  58. # GNU General Public License (Version 2, June 1991)
  59. #
  60. # This program is free software; you can redistribute
  61. # it and/or modify it under the terms of the GNU
  62. # General Public License as published by the Free
  63. # Software Foundation; either version 2 of the License,
  64. # or (at your option) any later version.
  65. #
  66. # This program is distributed in the hope that it will
  67. # be useful, but WITHOUT ANY WARRANTY; without even the
  68. # implied warranty of MERCHANTABILITY or FITNESS FOR A
  69. # PARTICULAR PURPOSE. See the GNU General Public License
  70. # for more details.
  71. #
  72. class masc_captcha
  73. {
  74. var $TTF_folder;
  75. var $TTF_RANGE = array('ARIAL.TTF');
  76. var $chars = 5;
  77. var $minsize = 15;
  78. var $maxsize = 15;
  79. var $maxrotation = 30;
  80. var $noise = FALSE;
  81. var $websafecolors = TRUE;
  82. var $debug = FALSE;
  83. var $lx; // width of picture
  84. var $ly; // height of picture
  85. var $jpegquality = 80; // image quality
  86. var $noisefactor = 9; // this will multiplyed with number of chars
  87. var $nb_noise; // number of background-noise-characters
  88. var $TTF_file; // holds the current selected TrueTypeFont
  89. var $gd_version; // holds the Version Number of GD-Library
  90. var $r;
  91. var $g;
  92. var $b;
  93. function masc_captcha( $config )
  94. {
  95. // Test for GD-Library(-Version)
  96. $this->gd_version = get_gd_version();
  97. if($this->gd_version == 0) die("There is no GD-Library-Support enabled. The Captcha-Class cannot be used!");
  98. if($this->debug) echo "\n<br />-Captcha-Debug: The available GD-Library has major version ".$this->gd_version;
  99. // extracts config array
  100. if(is_array($config))
  101. {
  102. if($this->debug) echo "\n<br />-Captcha-Debug: Extracts Config-Array in unsecure-mode!";
  103. foreach($config as $k=>$v) $this->$k = $v;
  104. }
  105. // check vars for maxtry, secretposition and min-max-size
  106. if($this->minsize > $this->maxsize)
  107. {
  108. $temp = $this->minsize;
  109. $this->minsize = $this->maxsize;
  110. $this->maxsize = $temp;
  111. if($this->debug) echo "<br />-Captcha-Debug: Arrghh! What do you think I mean with min and max? Switch minsize with maxsize.";
  112. }
  113. // check TrueTypeFonts
  114. if(is_array($this->TTF_RANGE))
  115. {
  116. if($this->debug) echo "\n<br />-Captcha-Debug: Check given TrueType-Array! (".count($this->TTF_RANGE).")";
  117. $temp = array();
  118. foreach($this->TTF_RANGE as $k=>$v)
  119. {
  120. if(is_readable($this->TTF_folder.$v)) $temp[] = $v;
  121. }
  122. $this->TTF_RANGE = $temp;
  123. if($this->debug) echo "\n<br />-Captcha-Debug: Valid TrueType-files: (".count($this->TTF_RANGE).")";
  124. //if(count($this->TTF_RANGE) < 1) die('No Truetypefont available for the CaptchaClass.');
  125. }
  126. else
  127. {
  128. if($this->debug) echo "\n<br />-Captcha-Debug: Check given TrueType-File! (".$this->TTF_RANGE.")";
  129. if(!is_readable($this->TTF_folder.$this->TTF_RANGE)) die('No Truetypefont available for the CaptchaClass.');
  130. }
  131. // select first TrueTypeFont
  132. $this->change_TTF();
  133. if($this->debug) echo "\n<br />-Captcha-Debug: Set current TrueType-File: (".$this->TTF_file.")";
  134. // get number of noise-chars for background if is enabled
  135. $this->nb_noise = $this->noise ? ($this->chars * $this->noisefactor) : 0;
  136. if($this->debug) echo "\n<br />-Captcha-Debug: Set number of noise characters to: (".$this->nb_noise.")";
  137. // set dimension of image
  138. $this->lx = ($this->chars + 1) * (int)(($this->maxsize + $this->minsize) / 1.5);
  139. $this->ly = (int)(2.4 * $this->maxsize);
  140. if($this->debug) echo "\n<br />-Captcha-Debug: Set image dimension to: (".$this->lx." x ".$this->ly.")";
  141. }
  142. function make_captcha( $private_key )
  143. {
  144. if($this->debug) echo "\n<br />-Captcha-Debug: Generate private key: ($private_key)";
  145. // create Image and set the apropriate function depending on GD-Version & websafecolor-value
  146. if($this->gd_version >= 2 && !$this->websafecolors)
  147. {
  148. $func1 = 'imagecreatetruecolor';
  149. $func2 = 'imagecolorallocate';
  150. }
  151. else
  152. {
  153. $func1 = 'imageCreate';
  154. $func2 = 'imagecolorclosest';
  155. }
  156. $image = $func1($this->lx,$this->ly);
  157. if($this->debug) echo "\n<br />-Captcha-Debug: Generate ImageStream with: ($func1())";
  158. if($this->debug) echo "\n<br />-Captcha-Debug: For colordefinitions we use: ($func2())";
  159. // Set Backgroundcolor
  160. $this->random_color(224, 255);
  161. $back = @imagecolorallocate($image, $this->r, $this->g, $this->b);
  162. @ImageFilledRectangle($image,0,0,$this->lx,$this->ly,$back);
  163. if($this->debug) echo "\n<br />-Captcha-Debug: We allocate one color for Background: (".$this->r."-".$this->g."-".$this->b.")";
  164. // allocates the 216 websafe color palette to the image
  165. if($this->gd_version < 2 || $this->websafecolors) $this->makeWebsafeColors($image);
  166. // fill with noise or grid
  167. if($this->nb_noise > 0)
  168. {
  169. // random characters in background with random position, angle, color
  170. if($this->debug) echo "\n<br />-Captcha-Debug: Fill background with noise: (".$this->nb_noise.")";
  171. for($i=0; $i < $this->nb_noise; $i++)
  172. {
  173. srand((double)microtime()*1000000);
  174. $size = intval(rand((int)($this->minsize / 2.3), (int)($this->maxsize / 1.7)));
  175. srand((double)microtime()*1000000);
  176. $angle = intval(rand(0, 360));
  177. srand((double)microtime()*1000000);
  178. $x = intval(rand(0, $this->lx));
  179. srand((double)microtime()*1000000);
  180. $y = intval(rand(0, (int)($this->ly - ($size / 5))));
  181. $this->random_color(160, 224);
  182. $color = $func2($image, $this->r, $this->g, $this->b);
  183. srand((double)microtime()*1000000);
  184. $text = chr(intval(rand(45,250)));
  185. if(count ($this->TTF_RANGE)>0){
  186. @ImageTTFText($image, $size, $angle, $x, $y, $color, $this->change_TTF(), $text);
  187. } else {
  188. imagestring($image,5,$x,$y,$text,$color);
  189. }
  190. }
  191. }
  192. else
  193. {
  194. // generate grid
  195. if($this->debug) echo "\n<br />-Captcha-Debug: Fill background with x-gridlines: (".(int)($this->lx / (int)($this->minsize / 1.5)).")";
  196. for($i=0; $i < $this->lx; $i += (int)($this->minsize / 1.5))
  197. {
  198. $this->random_color(160, 224);
  199. $color = $func2($image, $this->r, $this->g, $this->b);
  200. @imageline($image, $i, 0, $i, $this->ly, $color);
  201. }
  202. if($this->debug) echo "\n<br />-Captcha-Debug: Fill background with y-gridlines: (".(int)($this->ly / (int)(($this->minsize / 1.8))).")";
  203. for($i=0 ; $i < $this->ly; $i += (int)($this->minsize / 1.8))
  204. {
  205. $this->random_color(160, 224);
  206. $color = $func2($image, $this->r, $this->g, $this->b);
  207. @imageline($image, 0, $i, $this->lx, $i, $color);
  208. }
  209. }
  210. // generate Text
  211. if($this->debug) echo "\n<br />-Captcha-Debug: Fill forground with chars and shadows: (".$this->chars.")";
  212. for($i=0, $x = intval(rand($this->minsize,$this->maxsize)); $i < $this->chars; $i++)
  213. {
  214. $text = utf8_strtoupper(substr($private_key, $i, 1));
  215. srand((double)microtime()*1000000);
  216. $angle = intval(rand(($this->maxrotation * -1), $this->maxrotation));
  217. srand((double)microtime()*1000000);
  218. $size = intval(rand($this->minsize, $this->maxsize));
  219. srand((double)microtime()*1000000);
  220. $y = intval(rand((int)($size * 1.5), (int)($this->ly - ($size / 7))));
  221. $this->random_color(0, 127);
  222. $color = $func2($image, $this->r, $this->g, $this->b);
  223. $this->random_color(0, 127);
  224. $shadow = $func2($image, $this->r + 127, $this->g + 127, $this->b + 127);
  225. if(count($this->TTF_RANGE) > 0){
  226. @ImageTTFText($image, $size, $angle, $x + (int)($size / 15), $y, $shadow, $this->change_TTF(), $text);
  227. @ImageTTFText($image, $size, $angle, $x, $y - (int)($size / 15), $color, $this->TTF_file, $text);
  228. } else {
  229. $t_font = rand(3,5);
  230. imagestring($image,$t_font,$x + (int)($size / 15),$y-20,$text,$color);
  231. imagestring($image,$t_font,$x,$y - (int)($size / 15)-20,$text,$color);
  232. }
  233. $x += (int)($size + ($this->minsize / 5));
  234. }
  235. header('Content-type: image/jpeg');
  236. @ImageJPEG($image, '', $this->jpegquality);
  237. @ImageDestroy($image);
  238. if($this->debug) echo "\n<br />-Captcha-Debug: Destroy Imagestream.";
  239. }
  240. /** @private **/
  241. function makeWebsafeColors(&$image)
  242. {
  243. for($r = 0; $r <= 255; $r += 51)
  244. {
  245. for($g = 0; $g <= 255; $g += 51)
  246. {
  247. for($b = 0; $b <= 255; $b += 51)
  248. {
  249. $color = imagecolorallocate($image, $r, $g, $b);
  250. //$a[$color] = array('r'=>$r,'g'=>$g,'b'=>$b);
  251. }
  252. }
  253. }
  254. if($this->debug) echo "\n<br />-Captcha-Debug: Allocate 216 websafe colors to image: (".imagecolorstotal($image).")";
  255. }
  256. function random_color($min,$max)
  257. {
  258. srand((double)microtime() * 1000000);
  259. $this->r = intval(rand($min,$max));
  260. srand((double)microtime() * 1000000);
  261. $this->g = intval(rand($min,$max));
  262. srand((double)microtime() * 1000000);
  263. $this->b = intval(rand($min,$max));
  264. }
  265. function change_TTF()
  266. {
  267. if(count($this->TTF_RANGE) > 0){
  268. if(is_array($this->TTF_RANGE))
  269. {
  270. srand((float)microtime() * 10000000);
  271. $key = array_rand($this->TTF_RANGE);
  272. $this->TTF_file = $this->TTF_folder.$this->TTF_RANGE[$key];
  273. }
  274. else
  275. {
  276. $this->TTF_file = $this->TTF_folder.$this->TTF_RANGE;
  277. }
  278. return $this->TTF_file;
  279. }
  280. }
  281. } // END CLASS masc_captcha