1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Functions for the generation of the CAPTCHA image.
|
6 |
|
|
*
|
7 |
|
|
* Loosely Based on MyCaptcha by Heine Deelstra
|
8 |
|
|
* (http://heine.familiedeelstra.com/mycaptcha-download)
|
9 |
|
|
*/
|
10 |
|
|
|
11 |
|
|
/**
|
12 |
|
|
* Menu callback function that generates the CAPTCHA image.
|
13 |
|
|
*/
|
14 |
|
|
function image_captcha_image() {
|
15 |
|
|
// If output buffering is on: discard current content and disable further buffering
|
16 |
|
|
if (ob_get_level()) {
|
17 |
|
|
ob_end_clean();
|
18 |
|
|
}
|
19 |
|
|
|
20 |
|
|
if (!isset($_GET['sid'])) {
|
21 |
|
|
exit();
|
22 |
|
|
}
|
23 |
|
|
$captcha_sid = $_GET['sid'];
|
24 |
|
|
|
25 |
|
|
// Get solution (the code to show).
|
26 |
|
|
$code = db_query("SELECT solution FROM {captcha_sessions} WHERE csid = :csid",
|
27 |
|
|
array(':csid' => $captcha_sid)
|
28 |
|
|
)->fetchField();
|
29 |
|
|
|
30 |
|
|
// Only generate captcha if code exists in the session.
|
31 |
|
|
if ($code !== FALSE) {
|
32 |
66b5cbf6
|
Assos Assos
|
// Seed the random generators used for image CAPTCHA distortion based on session and code
|
33 |
|
|
// to counter attacks that re-request the same challenge and pick the simplest image one or combine info.
|
34 |
|
|
$seed = hexdec(substr(md5($captcha_sid . $code), 0, 8));
|
35 |
|
|
srand($seed);
|
36 |
|
|
mt_srand($seed);
|
37 |
85ad3d82
|
Assos Assos
|
// generate the image
|
38 |
|
|
$image = @_image_captcha_generate_image($code);
|
39 |
|
|
// check of generation was successful
|
40 |
|
|
if (!$image) {
|
41 |
|
|
watchdog('CAPTCHA', 'Generation of image CAPTCHA failed. Check your image CAPTCHA configuration and especially the used font.', array(), WATCHDOG_ERROR);
|
42 |
|
|
exit();
|
43 |
|
|
}
|
44 |
|
|
// Send the image resource as an image file to the client.
|
45 |
|
|
$file_format = variable_get('image_captcha_file_format', IMAGE_CAPTCHA_FILE_FORMAT_JPG);
|
46 |
|
|
if ($file_format == IMAGE_CAPTCHA_FILE_FORMAT_JPG) {
|
47 |
|
|
drupal_add_http_header('Content-Type', 'image/jpeg');
|
48 |
|
|
imagejpeg($image);
|
49 |
|
|
}
|
50 |
|
|
else {
|
51 |
|
|
drupal_add_http_header('Content-Type', 'image/png');
|
52 |
|
|
imagepng($image);
|
53 |
|
|
}
|
54 |
|
|
// Clean up the image resource.
|
55 |
|
|
imagedestroy($image);
|
56 |
|
|
}
|
57 |
|
|
exit();
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
|
61 |
|
|
/**
|
62 |
|
|
* Small helper function for parsing a hexadecimal color to a RGB tuple.
|
63 |
|
|
*/
|
64 |
|
|
function _image_captcha_hex_to_rgb($hex) {
|
65 |
|
|
// handle #RGB format
|
66 |
|
|
if (strlen($hex) == 4) {
|
67 |
|
|
$hex = $hex[1] . $hex[1] . $hex[2] . $hex[2] . $hex[3] . $hex[3];
|
68 |
|
|
}
|
69 |
|
|
$c = hexdec($hex);
|
70 |
|
|
$rgb = array();
|
71 |
|
|
for ($i = 16; $i >= 0; $i -= 8) {
|
72 |
|
|
$rgb[] = ($c >> $i) & 0xFF;
|
73 |
|
|
}
|
74 |
|
|
return $rgb;
|
75 |
|
|
}
|
76 |
|
|
|
77 |
|
|
|
78 |
|
|
/**
|
79 |
|
|
* Base function for generating a image CAPTCHA.
|
80 |
|
|
*/
|
81 |
|
|
function _image_captcha_generate_image($code) {
|
82 |
|
|
// Get font.
|
83 |
|
|
$fonts = _image_captcha_get_enabled_fonts();
|
84 |
|
|
|
85 |
|
|
// get other settings
|
86 |
|
|
$font_size = (int) variable_get('image_captcha_font_size', 30);
|
87 |
|
|
list($width, $height) = _image_captcha_image_size($code);
|
88 |
|
|
|
89 |
|
|
// create image resource
|
90 |
|
|
$image = imagecreatetruecolor($width, $height);
|
91 |
|
|
if (!$image) {
|
92 |
|
|
return FALSE;
|
93 |
|
|
}
|
94 |
|
|
|
95 |
|
|
// Get the background color and paint the background.
|
96 |
|
|
$background_rgb = _image_captcha_hex_to_rgb(variable_get('image_captcha_background_color', '#ffffff'));
|
97 |
|
|
$background_color = imagecolorallocate($image, $background_rgb[0], $background_rgb[1], $background_rgb[2]);
|
98 |
|
|
// Set transparency if needed.
|
99 |
|
|
$file_format = variable_get('image_captcha_file_format', IMAGE_CAPTCHA_FILE_FORMAT_JPG);
|
100 |
|
|
if ($file_format == IMAGE_CAPTCHA_FILE_FORMAT_TRANSPARENT_PNG) {
|
101 |
|
|
imagecolortransparent($image, $background_color);
|
102 |
|
|
}
|
103 |
|
|
imagefilledrectangle($image, 0, 0, $width, $height, $background_color);
|
104 |
|
|
|
105 |
|
|
// Do we need to draw in RTL mode?
|
106 |
|
|
global $language;
|
107 |
|
|
$rtl = $language->direction && ((bool) variable_get('image_captcha_rtl_support', 0));
|
108 |
|
|
|
109 |
|
|
// draw text
|
110 |
|
|
$result = _image_captcha_image_generator_print_string($image, $width, $height, $fonts, $font_size, $code, $rtl);
|
111 |
|
|
if (!$result) {
|
112 |
|
|
return FALSE;
|
113 |
|
|
}
|
114 |
|
|
|
115 |
|
|
// add noise
|
116 |
|
|
$noise_colors = array();
|
117 |
|
|
for ($i = 0; $i < 20; $i++) {
|
118 |
|
|
$noise_colors[] = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
|
119 |
|
|
}
|
120 |
|
|
// Add additional noise.
|
121 |
|
|
if (variable_get('image_captcha_dot_noise', 0)) {
|
122 |
|
|
_image_captcha_image_generator_add_dots($image, $width, $height, $noise_colors);
|
123 |
|
|
}
|
124 |
|
|
if (variable_get('image_captcha_line_noise', 0)) {
|
125 |
|
|
_image_captcha_image_generator_add_lines($image, $width, $height, $noise_colors);
|
126 |
|
|
}
|
127 |
|
|
|
128 |
|
|
// Distort the image.
|
129 |
|
|
$distortion_amplitude = .25 * $font_size * variable_get('image_captcha_distortion_amplitude', 0) / 10.0;
|
130 |
|
|
if ($distortion_amplitude > 1) {
|
131 |
|
|
// distortion parameters
|
132 |
66b5cbf6
|
Assos Assos
|
$wavelength_xr = (2 + 3 * mt_rand(0, 1000) / 1000) * $font_size;
|
133 |
|
|
$wavelength_yr = (2 + 3 * mt_rand(0, 1000) / 1000) * $font_size;
|
134 |
85ad3d82
|
Assos Assos
|
$freq_xr = 2 * 3.141592 / $wavelength_xr;
|
135 |
|
|
$freq_yr = 2 * 3.141592 / $wavelength_yr;
|
136 |
66b5cbf6
|
Assos Assos
|
$wavelength_xt = (2 + 3 * mt_rand(0, 1000) / 1000) * $font_size;
|
137 |
|
|
$wavelength_yt = (2 + 3 * mt_rand(0, 1000) / 1000) * $font_size;
|
138 |
85ad3d82
|
Assos Assos
|
$freq_xt = 2 * 3.141592 / $wavelength_xt;
|
139 |
|
|
$freq_yt = 2 * 3.141592 / $wavelength_yt;
|
140 |
|
|
|
141 |
|
|
$distorted_image = imagecreatetruecolor($width, $height);
|
142 |
|
|
if ($file_format == IMAGE_CAPTCHA_FILE_FORMAT_TRANSPARENT_PNG) {
|
143 |
|
|
imagecolortransparent($distorted_image, $background_color);
|
144 |
|
|
}
|
145 |
|
|
if (!$distorted_image) {
|
146 |
|
|
return FALSE;
|
147 |
|
|
}
|
148 |
|
|
|
149 |
|
|
if (variable_get('image_captcha_bilinear_interpolation', FALSE)) {
|
150 |
|
|
// distortion with bilinear interpolation
|
151 |
|
|
for ($x = 0; $x < $width; $x++) {
|
152 |
|
|
for ($y = 0; $y < $height; $y++) {
|
153 |
|
|
// get distorted sample point in source image
|
154 |
|
|
$r = $distortion_amplitude * sin($x * $freq_xr + $y * $freq_yr);
|
155 |
|
|
$theta = $x * $freq_xt + $y * $freq_yt;
|
156 |
|
|
$sx = $x + $r * cos($theta);
|
157 |
|
|
$sy = $y + $r * sin($theta);
|
158 |
|
|
$sxf = (int)floor($sx);
|
159 |
|
|
$syf = (int)floor($sy);
|
160 |
|
|
if ($sxf < 0 || $syf < 0 || $sxf >= $width - 1 || $syf >= $height - 1) {
|
161 |
|
|
$color = $background_color;
|
162 |
|
|
}
|
163 |
|
|
else {
|
164 |
|
|
// bilinear interpolation: sample at four corners
|
165 |
|
|
$color_00 = imagecolorat($image, $sxf , $syf );
|
166 |
|
|
$color_00_r = ($color_00 >> 16) & 0xFF;
|
167 |
|
|
$color_00_g = ($color_00 >> 8) & 0xFF;
|
168 |
|
|
$color_00_b = $color_00 & 0xFF;
|
169 |
|
|
$color_10 = imagecolorat($image, $sxf+1, $syf );
|
170 |
|
|
$color_10_r = ($color_10 >> 16) & 0xFF;
|
171 |
|
|
$color_10_g = ($color_10 >> 8) & 0xFF;
|
172 |
|
|
$color_10_b = $color_10 & 0xFF;
|
173 |
|
|
$color_01 = imagecolorat($image, $sxf , $syf+1);
|
174 |
|
|
$color_01_r = ($color_01 >> 16) & 0xFF;
|
175 |
|
|
$color_01_g = ($color_01 >> 8) & 0xFF;
|
176 |
|
|
$color_01_b = $color_01 & 0xFF;
|
177 |
|
|
$color_11 = imagecolorat($image, $sxf+1, $syf+1);
|
178 |
|
|
$color_11_r = ($color_11 >> 16) & 0xFF;
|
179 |
|
|
$color_11_g = ($color_11 >> 8) & 0xFF;
|
180 |
|
|
$color_11_b = $color_11 & 0xFF;
|
181 |
|
|
// interpolation factors
|
182 |
|
|
$u = $sx - $sxf;
|
183 |
|
|
$v = $sy - $syf;
|
184 |
|
|
// interpolate
|
185 |
|
|
$r = (int)((1-$v)*((1-$u)*$color_00_r + $u*$color_10_r) + $v*((1-$u)*$color_01_r + $u*$color_11_r));
|
186 |
|
|
$g = (int)((1-$v)*((1-$u)*$color_00_g + $u*$color_10_g) + $v*((1-$u)*$color_01_g + $u*$color_11_g));
|
187 |
|
|
$b = (int)((1-$v)*((1-$u)*$color_00_b + $u*$color_10_b) + $v*((1-$u)*$color_01_b + $u*$color_11_b));
|
188 |
|
|
// build color
|
189 |
|
|
$color = ($r<<16) + ($g<<8) + $b;
|
190 |
|
|
}
|
191 |
|
|
imagesetpixel($distorted_image, $x, $y, $color);
|
192 |
|
|
}
|
193 |
|
|
}
|
194 |
|
|
}
|
195 |
|
|
else {
|
196 |
|
|
// distortion with nearest neighbor interpolation
|
197 |
|
|
for ($x = 0; $x < $width; $x++) {
|
198 |
|
|
for ($y = 0; $y < $height; $y++) {
|
199 |
|
|
// get distorted sample point in source image
|
200 |
|
|
$r = $distortion_amplitude * sin($x * $freq_xr + $y * $freq_yr);
|
201 |
|
|
$theta = $x * $freq_xt + $y * $freq_yt;
|
202 |
|
|
$sx = $x + $r * cos($theta);
|
203 |
|
|
$sy = $y + $r * sin($theta);
|
204 |
|
|
$sxf = (int)floor($sx);
|
205 |
|
|
$syf = (int)floor($sy);
|
206 |
|
|
if ($sxf < 0 || $syf < 0 || $sxf >= $width - 1 || $syf >= $height - 1) {
|
207 |
|
|
$color = $background_color;
|
208 |
|
|
}
|
209 |
|
|
else {
|
210 |
|
|
$color = imagecolorat($image, $sxf, $syf);
|
211 |
|
|
}
|
212 |
|
|
imagesetpixel($distorted_image, $x, $y, $color);
|
213 |
|
|
}
|
214 |
|
|
}
|
215 |
|
|
}
|
216 |
|
|
// release undistorted image
|
217 |
|
|
imagedestroy($image);
|
218 |
|
|
// return distorted image
|
219 |
|
|
return $distorted_image;
|
220 |
|
|
}
|
221 |
|
|
else {
|
222 |
|
|
return $image;
|
223 |
|
|
}
|
224 |
|
|
}
|
225 |
|
|
|
226 |
|
|
function _image_captcha_image_generator_add_lines(&$image, $width, $height, $colors) {
|
227 |
|
|
$line_quantity = $width * $height/200.0 * ((int) variable_get('image_captcha_noise_level', 5)) / 10.0;
|
228 |
|
|
for ($i = 0; $i < $line_quantity; $i++) {
|
229 |
|
|
imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $colors[array_rand($colors)]);
|
230 |
|
|
}
|
231 |
|
|
}
|
232 |
|
|
|
233 |
|
|
function _image_captcha_image_generator_add_dots(&$image, $width, $height, $colors) {
|
234 |
|
|
$noise_quantity = $width * $height * ((int) variable_get('image_captcha_noise_level', 5)) / 10.0;
|
235 |
|
|
for ($i = 0; $i < $noise_quantity; $i++ ) {
|
236 |
|
|
imagesetpixel($image, mt_rand(0, $width), mt_rand(0, $height), $colors[array_rand($colors)]);
|
237 |
|
|
}
|
238 |
|
|
}
|
239 |
|
|
|
240 |
|
|
/**
|
241 |
|
|
* Helper function for drawing text on the image.
|
242 |
|
|
*/
|
243 |
|
|
function _image_captcha_image_generator_print_string(&$image, $width, $height, $fonts, $font_size, $text, $rtl=FALSE) {
|
244 |
|
|
// get characters
|
245 |
|
|
$characters = _image_captcha_utf8_split($text);
|
246 |
|
|
$character_quantity = count($characters);
|
247 |
|
|
|
248 |
|
|
// get colors
|
249 |
|
|
$background_rgb = _image_captcha_hex_to_rgb(variable_get('image_captcha_background_color', '#ffffff'));
|
250 |
|
|
$foreground_rgb = _image_captcha_hex_to_rgb(variable_get('image_captcha_foreground_color', '#000000'));
|
251 |
|
|
$background_color = imagecolorallocate($image, $background_rgb[0], $background_rgb[1], $background_rgb[2]);
|
252 |
|
|
$foreground_color = imagecolorallocate($image, $foreground_rgb[0], $foreground_rgb[1], $foreground_rgb[2]);
|
253 |
|
|
// precalculate the value ranges for color randomness
|
254 |
|
|
$foreground_randomness = (int)(variable_get('image_captcha_foreground_color_randomness', 100));
|
255 |
|
|
if ($foreground_randomness) {
|
256 |
|
|
$foreground_color_range = array();
|
257 |
|
|
for ($i=0; $i<3; $i++) {
|
258 |
|
|
$foreground_color_range[$i] = array(max(0, $foreground_rgb[$i] - $foreground_randomness), min(255, $foreground_rgb[$i] + $foreground_randomness));
|
259 |
|
|
}
|
260 |
|
|
}
|
261 |
|
|
|
262 |
|
|
// set default text color
|
263 |
|
|
$color = $foreground_color;
|
264 |
|
|
|
265 |
|
|
// the image is seperated in different character cages, one for each character
|
266 |
|
|
// each character will be somewhere inside that cage
|
267 |
|
|
$ccage_width = $width / $character_quantity;
|
268 |
|
|
$ccage_height = $height;
|
269 |
|
|
|
270 |
|
|
foreach ($characters as $c => $character) {
|
271 |
|
|
// initial position of character: in the center of its cage
|
272 |
|
|
$center_x = ($c + 0.5) * $ccage_width;
|
273 |
|
|
if ($rtl) {
|
274 |
|
|
$center_x = $width - $center_x;
|
275 |
|
|
}
|
276 |
|
|
$center_y = 0.5 * $height;
|
277 |
|
|
|
278 |
|
|
// Pick a random font from the list.
|
279 |
|
|
$font = $fonts[array_rand($fonts)];
|
280 |
|
|
|
281 |
|
|
// Get character dimensions for TrueType fonts.
|
282 |
|
|
if ($font != 'BUILTIN') {
|
283 |
|
|
$bbox = imagettfbbox($font_size, 0, drupal_realpath($font), $character);
|
284 |
|
|
// In very rare cases with some versions of the GD library, the x-value
|
285 |
|
|
// of the left side of the bounding box as returned by the first call of
|
286 |
|
|
// imagettfbbox is corrupt (value -2147483648 = 0x80000000).
|
287 |
|
|
// The weird thing is that calling the function a second time
|
288 |
|
|
// can be used as workaround.
|
289 |
|
|
// This issue is discussed at http://drupal.org/node/349218.
|
290 |
|
|
if ($bbox[2] < 0) {
|
291 |
|
|
$bbox = imagettfbbox($font_size, 0, drupal_realpath($font), $character);
|
292 |
|
|
}
|
293 |
|
|
}
|
294 |
|
|
else {
|
295 |
|
|
$character_width = imagefontwidth(5);
|
296 |
|
|
$character_height = imagefontheight(5);
|
297 |
|
|
$bbox = array(0, $character_height, $character_width, $character_height, $character_width, 0, 0, 0);
|
298 |
|
|
}
|
299 |
|
|
|
300 |
|
|
// Random (but small) rotation of the character.
|
301 |
|
|
// TODO: add a setting for this?
|
302 |
|
|
$angle = mt_rand(-10, 10);
|
303 |
|
|
|
304 |
|
|
// Determine print position: at what coordinate should the character be
|
305 |
|
|
// printed so that the bounding box would be nicely centered in the cage?
|
306 |
|
|
$bb_center_x = .5 * ($bbox[0] + $bbox[2]);
|
307 |
|
|
$bb_center_y = .5 * ($bbox[1] + $bbox[7]);
|
308 |
|
|
$angle_cos = cos($angle*3.1415/180);
|
309 |
|
|
$angle_sin = sin($angle*3.1415/180);
|
310 |
|
|
$pos_x = $center_x - ($angle_cos * $bb_center_x + $angle_sin * $bb_center_y);
|
311 |
|
|
$pos_y = $center_y - (-$angle_sin * $bb_center_x + $angle_cos * $bb_center_y);
|
312 |
|
|
|
313 |
|
|
// Calculate available room to jitter: how much can the character be moved
|
314 |
|
|
// so that it stays inside its cage?
|
315 |
|
|
$bb_width = $bbox[2] - $bbox[0];
|
316 |
|
|
$bb_height = $bbox[1] - $bbox[7];
|
317 |
|
|
$dev_x = .5 * max(0, $ccage_width - abs($angle_cos) * $bb_width - abs($angle_sin) * $bb_height);
|
318 |
|
|
$dev_y = .5 * max(0, $ccage_height - abs($angle_cos) * $bb_height - abs($angle_sin) * $bb_width);
|
319 |
|
|
|
320 |
|
|
// add jitter to position
|
321 |
|
|
$pos_x = $pos_x + mt_rand(-$dev_x, $dev_x);
|
322 |
|
|
$pos_y = $pos_y + mt_rand(-$dev_y, $dev_y);
|
323 |
|
|
|
324 |
|
|
// calculate text color in case of randomness
|
325 |
|
|
if ($foreground_randomness) {
|
326 |
|
|
$color = imagecolorallocate($image,
|
327 |
|
|
mt_rand($foreground_color_range[0][0], $foreground_color_range[0][1]),
|
328 |
|
|
mt_rand($foreground_color_range[1][0], $foreground_color_range[1][1]),
|
329 |
|
|
mt_rand($foreground_color_range[2][0], $foreground_color_range[2][1])
|
330 |
|
|
);
|
331 |
|
|
}
|
332 |
|
|
|
333 |
|
|
// draw character
|
334 |
|
|
if ($font == 'BUILTIN') {
|
335 |
|
|
imagestring($image, 5, $pos_x, $pos_y, $character, $color);
|
336 |
|
|
}
|
337 |
|
|
else {
|
338 |
|
|
imagettftext($image, $font_size, $angle, $pos_x, $pos_y, $color, drupal_realpath($font), $character);
|
339 |
|
|
}
|
340 |
|
|
|
341 |
|
|
// For debugging purposes: draw character bounding box (only valid when rotation is disabled).
|
342 |
|
|
// imagerectangle($image, $pos_x + $bbox[0], $pos_y + $bbox[1], $pos_x + $bbox[2], $pos_y + $bbox[7], $color);
|
343 |
|
|
|
344 |
|
|
}
|
345 |
|
|
|
346 |
|
|
// return a sign of success
|
347 |
|
|
return TRUE;
|
348 |
|
|
} |