1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CAPTCHA generator script. |
4
|
|
|
* |
5
|
|
|
* @author Qiang Xue <[email protected]> |
6
|
|
|
* @link https://github.com/pradosoft/prado |
7
|
|
|
* @copyright Copyright © 2005-2016 The PRADO Group |
8
|
|
|
* @license https://github.com/pradosoft/prado/blob/master/LICENSE |
9
|
|
|
* @package Prado\Web\UI\WebControls\assets |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Prado\Web\UI\WebControls\assets; |
13
|
|
|
|
14
|
|
|
define('THEME_OPAQUE_BACKGROUND', 0x0001); |
15
|
|
|
define('THEME_NOISY_BACKGROUND', 0x0002); |
16
|
|
|
define('THEME_HAS_GRID', 0x0004); |
17
|
|
|
define('THEME_HAS_SCRIBBLE', 0x0008); |
18
|
|
|
define('THEME_MORPH_BACKGROUND', 0x0010); |
19
|
|
|
define('THEME_SHADOWED_TEXT', 0x0020); |
20
|
|
|
|
21
|
|
|
require_once(dirname(__FILE__) . '/captcha_key.php'); |
22
|
|
|
|
23
|
|
|
$token = 'error'; |
24
|
|
|
$theme = 0; |
25
|
|
|
|
26
|
|
|
if(isset($_GET['options'])) |
27
|
|
|
{ |
28
|
|
|
$str = base64_decode($_GET['options']); |
29
|
|
|
if(strlen($str) > 32) |
30
|
|
|
{ |
31
|
|
|
$hash = substr($str, 0, 32); |
32
|
|
|
$str = substr($str, 32); |
33
|
|
|
if(md5($privateKey . $str) === $hash) |
34
|
|
|
{ |
35
|
|
|
$options = unserialize($str); |
36
|
|
|
$publicKey = $options['publicKey']; |
37
|
|
|
$tokenLength = $options['tokenLength']; |
38
|
|
|
$caseSensitive = $options['caseSensitive']; |
39
|
|
|
$alphabet = $options['alphabet']; |
40
|
|
|
$fontSize = $options['fontSize']; |
41
|
|
|
$theme = $options['theme']; |
42
|
|
|
if(($randomSeed = $options['randomSeed']) > 0) |
43
|
|
|
srand($randomSeed); |
44
|
|
|
else |
45
|
|
|
srand((int)(microtime() * 1000000)); |
46
|
|
|
$token = generateToken($publicKey, $privateKey, $alphabet, $tokenLength, $caseSensitive); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
displayToken($token, $fontSize, $theme); |
52
|
|
|
|
53
|
|
|
function generateToken($publicKey, $privateKey, $alphabet, $tokenLength, $caseSensitive) |
54
|
|
|
{ |
55
|
|
|
$token = substr(hash2string(md5($publicKey . $privateKey), $alphabet) . hash2string(md5($privateKey . $publicKey), $alphabet), 0, $tokenLength); |
56
|
|
|
return $caseSensitive?$token:strtoupper($token); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
function hash2string($hex, $alphabet) |
60
|
|
|
{ |
61
|
|
|
if(strlen($alphabet) < 2) |
62
|
|
|
$alphabet = '234578adefhijmnrtABDEFGHJLMNRT'; |
63
|
|
|
$hexLength = strlen($hex); |
64
|
|
|
$base = strlen($alphabet); |
65
|
|
|
$result = ''; |
66
|
|
|
for($i = 0;$i < $hexLength;$i += 6) |
67
|
|
|
{ |
68
|
|
|
$number = hexdec(substr($hex, $i, 6)); |
69
|
|
|
while($number) |
70
|
|
|
{ |
71
|
|
|
$result .= $alphabet[$number % $base]; |
72
|
|
|
$number = floor($number / $base); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
return $result; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
function displayToken($token, $fontSize, $theme) |
79
|
|
|
{ |
80
|
|
|
if(($fontSize = (int)$fontSize) < 22) |
81
|
|
|
$fontSize = 22; |
82
|
|
|
if($fontSize > 100) |
83
|
|
|
$fontSize = 100; |
84
|
|
|
$length = strlen($token); |
85
|
|
|
$padding = 10; |
86
|
|
|
$fontWidth = $fontSize; |
87
|
|
|
$fontHeight = floor($fontWidth * 1.5); |
88
|
|
|
$width = $fontWidth * $length + $padding * 2; |
89
|
|
|
$height = $fontHeight; |
90
|
|
|
$image = imagecreatetruecolor($width, $height); |
|
|
|
|
91
|
|
|
|
92
|
|
|
addBackground( |
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
|
|
|
header('Content-Type: image/png'); |
121
|
|
|
imagepng($image); |
122
|
|
|
imagedestroy($image); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
function addBackground($image, $width, $height, $opaque, $noisy, $hasGrid, $hasScribble, $morph) |
126
|
|
|
{ |
127
|
|
|
$background = imagecreatetruecolor($width * 2, $height * 2); |
128
|
|
|
$white = imagecolorallocate($background, 255, 255, 255); |
129
|
|
|
imagefill($background, 0, 0, $white); |
130
|
|
|
|
131
|
|
|
if($opaque) |
132
|
|
|
imagefill($background, 0, 0, imagecolorallocate($background, 100, 100, 100)); |
133
|
|
|
|
134
|
|
|
if($noisy) |
135
|
|
|
addNoise($background, $width * 2, $height * 2); |
136
|
|
|
|
137
|
|
|
if($hasGrid) |
138
|
|
|
addGrid($background, $width * 2, $height * 2); |
139
|
|
|
|
140
|
|
|
if($hasScribble) |
141
|
|
|
addScribble($background, $width * 2, $height * 2); |
142
|
|
|
|
143
|
|
|
if($morph) |
144
|
|
|
morphImage($background, $width * 2, $height * 2); |
145
|
|
|
|
146
|
|
|
imagecopy($image, $background, 0, 0, 30, 30, $width, $height); |
147
|
|
|
|
148
|
|
|
if(!$opaque) |
149
|
|
|
imagecolortransparent($image, $white); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
function addNoise($image, $width, $height) |
153
|
|
|
{ |
154
|
|
|
for($x = 0;$x < $width;++$x) |
155
|
|
|
{ |
156
|
|
|
for($y = 0;$y < $height;++$y) |
157
|
|
|
{ |
158
|
|
|
if(rand(0, 100) < 25) |
159
|
|
|
{ |
160
|
|
|
$color = imagecolorallocate($image, rand(150, 220), rand(150, 220), rand(150, 220)); |
161
|
|
|
imagesetpixel($image, $x, $y, $color); |
162
|
|
|
imagecolordeallocate($image, $color); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
function addGrid($image, $width, $height) |
169
|
|
|
{ |
170
|
|
|
for($i = 0;$i < $width;$i += rand(15, 25)) |
171
|
|
|
{ |
172
|
|
|
imagesetthickness($image, rand(2, 6)); |
173
|
|
|
$color = imagecolorallocate($image, rand(100, 180), rand(100, 180), rand(100, 180)); |
174
|
|
|
imageline($image, $i + rand(-10, 20), 0, $i + rand(-10, 20), $height, $color); |
175
|
|
|
imagecolordeallocate($image, $color); |
176
|
|
|
} |
177
|
|
|
for($i = 0;$i < $height;$i += rand(15, 25)) |
178
|
|
|
{ |
179
|
|
|
imagesetthickness($image, rand(2, 6)); |
180
|
|
|
$color = imagecolorallocate($image, rand(100, 180), rand(100, 180), rand(100, 180)); |
181
|
|
|
imageline($image, 0, $i + rand(-10, 20), $width, $i + rand(-10, 20), $color); |
182
|
|
|
imagecolordeallocate($image, $color); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
function addScribble($image, $width, $height) |
|
|
|
|
187
|
|
|
{ |
188
|
|
|
for($i = 0;$i < 8;$i++) |
189
|
|
|
{ |
190
|
|
|
$color = imagecolorallocate($image, rand(100, 180), rand(100, 180), rand(100, 180)); |
191
|
|
|
$points = []; |
192
|
|
|
for($j = 1;$j < rand(5, 10);$j++) |
193
|
|
|
{ |
194
|
|
|
$points[] = rand(2 * (20 * ($i + 1)), 2 * (50 * ($i + 1))); |
195
|
|
|
$points[] = rand(30, $height + 30); |
196
|
|
|
} |
197
|
|
|
imagesetthickness($image, rand(2, 6)); |
198
|
|
|
imagepolygon($image, $points, intval(count($points) / 2), $color); |
199
|
|
|
imagecolordeallocate($image, $color); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
function morphImage($image, $width, $height) |
204
|
|
|
{ |
205
|
|
|
$tempImage = imagecreatetruecolor($width, $height); |
206
|
|
|
$chunk = rand(1, 5); |
|
|
|
|
207
|
|
|
for($x = $y = 0;$x < $width;$x += $chunk) |
208
|
|
|
{ |
209
|
|
|
$chunk = rand(1, 5); |
210
|
|
|
$y += rand(-1, 1); |
211
|
|
|
if($y >= $height) $y = $height - 5; |
212
|
|
|
if($y < 0) $y = 5; |
213
|
|
|
imagecopy($tempImage, $image, $x, 0, $x, $y, $chunk, $height); |
214
|
|
|
} |
215
|
|
|
for($x = $y = 0;$y < $height;$y += $chunk) |
216
|
|
|
{ |
217
|
|
|
$chunk = rand(1, 5); |
218
|
|
|
$x += rand(-1, 1); |
219
|
|
|
if($x >= $width) $x = $width - 5; |
220
|
|
|
if($x < 0) $x = 5; |
221
|
|
|
imagecopy($image, $tempImage, $x, $y, 0, $y, $width, $chunk); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
|