3 * CAPTCHA generator script.
5 * @author Qiang Xue <qiang.xue@gmail.com>
6 * @link https://github.com/pradosoft/prado
7 * @copyright Copyright © 2005-2016 The PRADO Group
8 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9 * @package System.Web.UI.WebControls.assets
12 define('THEME_OPAQUE_BACKGROUND',0x0001);
13 define('THEME_NOISY_BACKGROUND',0x0002);
14 define('THEME_HAS_GRID',0x0004);
15 define('THEME_HAS_SCRIBBLE',0x0008);
16 define('THEME_MORPH_BACKGROUND',0x0010);
17 define('THEME_SHADOWED_TEXT',0x0020);
19 require_once(dirname(__FILE__).'/captcha_key.php');
24 if(isset($_GET['options']))
26 $str=base64_decode($_GET['options']);
29 $hash=substr($str,0,32);
31 if(md5($privateKey.$str)===$hash)
33 $options=unserialize($str);
34 $publicKey=$options['publicKey'];
35 $tokenLength=$options['tokenLength'];
36 $caseSensitive=$options['caseSensitive'];
37 $alphabet=$options['alphabet'];
38 $fontSize=$options['fontSize'];
39 $theme=$options['theme'];
40 if(($randomSeed=$options['randomSeed'])>0)
43 srand((int)(microtime()*1000000));
44 $token=generateToken($publicKey,$privateKey,$alphabet,$tokenLength,$caseSensitive);
49 displayToken($token,$fontSize,$theme);
51 function generateToken($publicKey,$privateKey,$alphabet,$tokenLength,$caseSensitive)
53 $token=substr(hash2string(md5($publicKey.$privateKey),$alphabet).hash2string(md5($privateKey.$publicKey),$alphabet),0,$tokenLength);
54 return $caseSensitive?$token:strtoupper($token);
57 function hash2string($hex,$alphabet)
59 if(strlen($alphabet)<2)
60 $alphabet='234578adefhijmnrtABDEFGHJLMNRT';
61 $hexLength=strlen($hex);
62 $base=strlen($alphabet);
64 for($i=0;$i<$hexLength;$i+=6)
66 $number=hexdec(substr($hex,$i,6));
69 $result.=$alphabet[$number%$base];
70 $number=floor($number/$base);
76 function displayToken($token,$fontSize,$theme)
78 if(($fontSize=(int)$fontSize)<22)
82 $length=strlen($token);
85 $fontHeight=floor($fontWidth*1.5);
86 $width=$fontWidth*$length+$padding*2;
88 $image=imagecreatetruecolor($width,$height);
92 $image, $width, $height,
93 $theme&THEME_OPAQUE_BACKGROUND,
94 $theme&THEME_NOISY_BACKGROUND,
95 $theme&THEME_HAS_GRID,
96 $theme&THEME_HAS_SCRIBBLE,
97 $theme&THEME_MORPH_BACKGROUND
100 $font=dirname(__FILE__).DIRECTORY_SEPARATOR.'verase.ttf';
102 if(function_exists('imagefilter'))
103 imagefilter($image,IMG_FILTER_GAUSSIAN_BLUR);
105 $hasShadow=($theme&THEME_SHADOWED_TEXT);
106 for($i=0;$i<$length;$i++)
108 $color=imagecolorallocate($image,rand(150,220),rand(150,220),rand(150,220));
109 $size=rand($fontWidth-10,$fontWidth);
111 $x=$padding+$i*$fontWidth;
112 $y=rand($fontHeight-15,$fontHeight-10);
113 imagettftext($image,$size,$angle,$x,$y,$color,$font,$token[$i]);
115 imagettftext($image,$size,$angle,$x+2,$y+2,$color,$font,$token[$i]);
116 imagecolordeallocate($image,$color);
119 header('Content-Type: image/png');
121 imagedestroy($image);
124 function addBackground($image,$width,$height,$opaque,$noisy,$hasGrid,$hasScribble,$morph)
126 $background=imagecreatetruecolor($width*2,$height*2);
127 $white=imagecolorallocate($background,255,255,255);
128 imagefill($background,0,0,$white);
131 imagefill($background,0,0,imagecolorallocate($background,100,100,100));
134 addNoise($background,$width*2,$height*2);
137 addGrid($background,$width*2,$height*2);
140 addScribble($background,$width*2,$height*2);
143 morphImage($background,$width*2,$height*2);
145 imagecopy($image,$background,0,0,30,30,$width,$height);
148 imagecolortransparent($image,$white);
151 function addNoise($image,$width,$height)
153 for($x=0;$x<$width;++$x)
155 for($y=0;$y<$height;++$y)
159 $color=imagecolorallocate($image,rand(150,220),rand(150,220),rand(150,220));
160 imagesetpixel($image,$x,$y,$color);
161 imagecolordeallocate($image,$color);
167 function addGrid($image,$width,$height)
169 for($i=0;$i<$width;$i+=rand(15,25))
171 imagesetthickness($image,rand(2,6));
172 $color=imagecolorallocate($image,rand(100,180),rand(100,180),rand(100,180));
173 imageline($image,$i+rand(-10,20),0,$i+rand(-10,20),$height,$color);
174 imagecolordeallocate($image,$color);
176 for($i=0;$i<$height;$i+=rand(15,25))
178 imagesetthickness($image,rand(2,6));
179 $color=imagecolorallocate($image,rand(100,180),rand(100,180),rand(100,180));
180 imageline($image,0,$i+rand(-10,20),$width,$i+rand(-10,20),$color);
181 imagecolordeallocate($image,$color);
185 function addScribble($image,$width,$height)
189 $color=imagecolorallocate($image,rand(100,180),rand(100,180),rand(100,180));
191 for($j=1;$j<rand(5,10);$j++)
193 $points[]=rand(2*(20*($i+1)),2*(50*($i+1)));
194 $points[]=rand(30,$height+30);
196 imagesetthickness($image,rand(2,6));
197 imagepolygon($image,$points,intval(sizeof($points)/2),$color);
198 imagecolordeallocate($image,$color);
202 function morphImage($image,$width,$height)
204 $tempImage=imagecreatetruecolor($width,$height);
206 for($x=$y=0;$x<$width;$x+=$chunk)
210 if($y>=$height) $y=$height-5;
212 imagecopy($tempImage,$image,$x,0,$x,$y,$chunk,$height);
214 for($x=$y=0;$y<$height;$y+=$chunk)
218 if($x>=$width) $x=$width-5;
220 imagecopy($image,$tempImage,$x,$y,0,$y,$width,$chunk);