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