| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* CAPTCHA generator script.
|
|
|
4 |
*
|
|
|
5 |
* @author Qiang Xue <qiang.xue@gmail.com>
|
|
|
6 |
* @link http://www.pradosoft.com/
|
|
|
7 |
* @copyright Copyright © 2005-2008 PradoSoft
|
|
|
8 |
* @license http://www.pradosoft.com/license/
|
|
|
9 |
* @version $Id: captcha.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
10 |
* @package System.Web.UI.WebControls.assets
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
define('THEME_OPAQUE_BACKGROUND',0x0001);
|
|
|
14 |
define('THEME_NOISY_BACKGROUND',0x0002);
|
|
|
15 |
define('THEME_HAS_GRID',0x0004);
|
|
|
16 |
define('THEME_HAS_SCRIBBLE',0x0008);
|
|
|
17 |
define('THEME_MORPH_BACKGROUND',0x0010);
|
|
|
18 |
define('THEME_SHADOWED_TEXT',0x0020);
|
|
|
19 |
|
|
|
20 |
require_once(dirname(__FILE__).'/captcha_key.php');
|
|
|
21 |
|
|
|
22 |
$token='error';
|
|
|
23 |
$theme=0;
|
|
|
24 |
|
|
|
25 |
if(isset($_GET['options']))
|
|
|
26 |
{
|
|
|
27 |
$str=base64_decode($_GET['options']);
|
|
|
28 |
if(strlen($str)>32)
|
|
|
29 |
{
|
|
|
30 |
$hash=substr($str,0,32);
|
|
|
31 |
$str=substr($str,32);
|
|
|
32 |
if(md5($privateKey.$str)===$hash)
|
|
|
33 |
{
|
|
|
34 |
$options=unserialize($str);
|
|
|
35 |
$publicKey=$options['publicKey'];
|
|
|
36 |
$tokenLength=$options['tokenLength'];
|
|
|
37 |
$caseSensitive=$options['caseSensitive'];
|
|
|
38 |
$alphabet=$options['alphabet'];
|
|
|
39 |
$fontSize=$options['fontSize'];
|
|
|
40 |
$theme=$options['theme'];
|
|
|
41 |
if(($randomSeed=$options['randomSeed'])>0)
|
|
|
42 |
srand($randomSeed);
|
|
|
43 |
else
|
|
|
44 |
srand((int)(microtime()*1000000));
|
|
|
45 |
$token=generateToken($publicKey,$privateKey,$alphabet,$tokenLength,$caseSensitive);
|
|
|
46 |
}
|
|
|
47 |
}
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
displayToken($token,$fontSize,$theme);
|
|
|
51 |
|
|
|
52 |
function generateToken($publicKey,$privateKey,$alphabet,$tokenLength,$caseSensitive)
|
|
|
53 |
{
|
|
|
54 |
$token=substr(hash2string(md5($publicKey.$privateKey),$alphabet).hash2string(md5($privateKey.$publicKey),$alphabet),0,$tokenLength);
|
|
|
55 |
return $caseSensitive?$token:strtoupper($token);
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
function hash2string($hex,$alphabet)
|
|
|
59 |
{
|
|
|
60 |
if(strlen($alphabet)<2)
|
|
|
61 |
$alphabet='234578adefhijmnrtABDEFGHJLMNRT';
|
|
|
62 |
$hexLength=strlen($hex);
|
|
|
63 |
$base=strlen($alphabet);
|
|
|
64 |
$result='';
|
|
|
65 |
for($i=0;$i<$hexLength;$i+=6)
|
|
|
66 |
{
|
|
|
67 |
$number=hexdec(substr($hex,$i,6));
|
|
|
68 |
while($number)
|
|
|
69 |
{
|
|
|
70 |
$result.=$alphabet[$number%$base];
|
|
|
71 |
$number=floor($number/$base);
|
|
|
72 |
}
|
|
|
73 |
}
|
|
|
74 |
return $result;
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
function displayToken($token,$fontSize,$theme)
|
|
|
78 |
{
|
|
|
79 |
if(($fontSize=(int)$fontSize)<22)
|
|
|
80 |
$fontSize=22;
|
|
|
81 |
if($fontSize>100)
|
|
|
82 |
$fontSize=100;
|
|
|
83 |
$length=strlen($token);
|
|
|
84 |
$padding=10;
|
|
|
85 |
$fontWidth=$fontSize;
|
|
|
86 |
$fontHeight=floor($fontWidth*1.5);
|
|
|
87 |
$width=$fontWidth*$length+$padding*2;
|
|
|
88 |
$height=$fontHeight;
|
|
|
89 |
$image=imagecreatetruecolor($width,$height);
|
|
|
90 |
|
|
|
91 |
addBackground
|
|
|
92 |
(
|
|
|
93 |
$image, $width, $height,
|
|
|
94 |
$theme&THEME_OPAQUE_BACKGROUND,
|
|
|
95 |
$theme&THEME_NOISY_BACKGROUND,
|
|
|
96 |
$theme&THEME_HAS_GRID,
|
|
|
97 |
$theme&THEME_HAS_SCRIBBLE,
|
|
|
98 |
$theme&THEME_MORPH_BACKGROUND
|
|
|
99 |
);
|
|
|
100 |
|
|
|
101 |
$font=dirname(__FILE__).DIRECTORY_SEPARATOR.'verase.ttf';
|
|
|
102 |
|
|
|
103 |
if(function_exists('imagefilter'))
|
|
|
104 |
imagefilter($image,IMG_FILTER_GAUSSIAN_BLUR);
|
|
|
105 |
|
|
|
106 |
$hasShadow=($theme&THEME_SHADOWED_TEXT);
|
|
|
107 |
for($i=0;$i<$length;$i++)
|
|
|
108 |
{
|
|
|
109 |
$color=imagecolorallocate($image,rand(150,220),rand(150,220),rand(150,220));
|
|
|
110 |
$size=rand($fontWidth-10,$fontWidth);
|
|
|
111 |
$angle=rand(-30,30);
|
|
|
112 |
$x=$padding+$i*$fontWidth;
|
|
|
113 |
$y=rand($fontHeight-15,$fontHeight-10);
|
|
|
114 |
imagettftext($image,$size,$angle,$x,$y,$color,$font,$token[$i]);
|
|
|
115 |
if($hasShadow)
|
|
|
116 |
imagettftext($image,$size,$angle,$x+2,$y+2,$color,$font,$token[$i]);
|
|
|
117 |
imagecolordeallocate($image,$color);
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
imagepng($image);
|
|
|
121 |
imagedestroy($image);
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
function addBackground($image,$width,$height,$opaque,$noisy,$hasGrid,$hasScribble,$morph)
|
|
|
125 |
{
|
|
|
126 |
$background=imagecreatetruecolor($width*2,$height*2);
|
|
|
127 |
$white=imagecolorallocate($background,255,255,255);
|
|
|
128 |
imagefill($background,0,0,$white);
|
|
|
129 |
|
|
|
130 |
if($opaque)
|
|
|
131 |
imagefill($background,0,0,imagecolorallocate($background,100,100,100));
|
|
|
132 |
|
|
|
133 |
if($noisy)
|
|
|
134 |
addNoise($background,$width*2,$height*2);
|
|
|
135 |
|
|
|
136 |
if($hasGrid)
|
|
|
137 |
addGrid($background,$width*2,$height*2);
|
|
|
138 |
|
|
|
139 |
if($hasScribble)
|
|
|
140 |
addScribble($background,$width*2,$height*2);
|
|
|
141 |
|
|
|
142 |
if($morph)
|
|
|
143 |
morphImage($background,$width*2,$height*2);
|
|
|
144 |
|
|
|
145 |
imagecopy($image,$background,0,0,30,30,$width,$height);
|
|
|
146 |
|
|
|
147 |
if(!$opaque)
|
|
|
148 |
imagecolortransparent($image,$white);
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
function addNoise($image,$width,$height)
|
|
|
152 |
{
|
|
|
153 |
for($x=0;$x<$width;++$x)
|
|
|
154 |
{
|
|
|
155 |
for($y=0;$y<$height;++$y)
|
|
|
156 |
{
|
|
|
157 |
if(rand(0,100)<25)
|
|
|
158 |
{
|
|
|
159 |
$color=imagecolorallocate($image,rand(150,220),rand(150,220),rand(150,220));
|
|
|
160 |
imagesetpixel($image,$x,$y,$color);
|
|
|
161 |
imagecolordeallocate($image,$color);
|
|
|
162 |
}
|
|
|
163 |
}
|
|
|
164 |
}
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
function addGrid($image,$width,$height)
|
|
|
168 |
{
|
|
|
169 |
for($i=0;$i<$width;$i+=rand(15,25))
|
|
|
170 |
{
|
|
|
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);
|
|
|
175 |
}
|
|
|
176 |
for($i=0;$i<$height;$i+=rand(15,25))
|
|
|
177 |
{
|
|
|
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);
|
|
|
182 |
}
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
function addScribble($image,$width,$height)
|
|
|
186 |
{
|
|
|
187 |
for($i=0;$i<8;$i++)
|
|
|
188 |
{
|
|
|
189 |
$color=imagecolorallocate($image,rand(100,180),rand(100,180),rand(100,180));
|
|
|
190 |
$points=array();
|
|
|
191 |
for($j=1;$j<rand(5,10);$j++)
|
|
|
192 |
{
|
|
|
193 |
$points[]=rand(2*(20*($i+1)),2*(50*($i+1)));
|
|
|
194 |
$points[]=rand(30,$height+30);
|
|
|
195 |
}
|
|
|
196 |
imagesetthickness($image,rand(2,6));
|
|
|
197 |
imagepolygon($image,$points,intval(sizeof($points)/2),$color);
|
|
|
198 |
imagecolordeallocate($image,$color);
|
|
|
199 |
}
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
function morphImage($image,$width,$height)
|
|
|
203 |
{
|
|
|
204 |
$tempImage=imagecreatetruecolor($width,$height);
|
|
|
205 |
$chunk=rand(1,5);
|
|
|
206 |
for($x=$y=0;$x<$width;$x+=$chunk)
|
|
|
207 |
{
|
|
|
208 |
$chunk=rand(1,5);
|
|
|
209 |
$y+=rand(-1,1);
|
|
|
210 |
if($y>=$height) $y=$height-5;
|
|
|
211 |
if($y<0) $y=5;
|
|
|
212 |
imagecopy($tempImage,$image,$x,0,$x,$y,$chunk,$height);
|
|
|
213 |
}
|
|
|
214 |
for($x=$y=0;$y<$height;$y+=$chunk)
|
|
|
215 |
{
|
|
|
216 |
$chunk=rand(1,5);
|
|
|
217 |
$x+=rand(-1,1);
|
|
|
218 |
if($x>=$width) $x=$width-5;
|
|
|
219 |
if($x<0) $x=5;
|
|
|
220 |
imagecopy($image,$tempImage,$x,$y,0,$y,$width,$chunk);
|
|
|
221 |
}
|
|
|
222 |
}
|
|
|
223 |
|