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

/wwwroot/mantis/make_captcha_img.php

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