Project

General

Profile

Paste
Download (13.5 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / captcha / image_captcha / image_captcha.user.inc @ 620f9137

1
<?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']) || is_array($_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
    // 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 = (int) hexdec(substr(md5($captcha_sid . $code), 0, 8));
35
    srand($seed);
36
    mt_srand($seed);
37
    // 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
 * Small helper function for parsing a hexadecimal color to a RGB tuple.
62
 */
63
function _image_captcha_hex_to_rgb($hex) {
64
  // Handle #RGB format/
65
  if (strlen($hex) == 4) {
66
    $hex = $hex[1] . $hex[1] . $hex[2] . $hex[2] . $hex[3] . $hex[3];
67
  }
68
  $c = hexdec($hex);
69
  $rgb = array();
70
  for ($i = 16; $i >= 0; $i -= 8) {
71
    $rgb[] = ($c >> $i) & 0xFF;
72
  }
73
  return $rgb;
74
}
75

    
76
/**
77
 * Base function for generating a image CAPTCHA.
78
 */
79
function _image_captcha_generate_image($code) {
80
  // Get font.
81
  $fonts = _image_captcha_get_enabled_fonts();
82

    
83
  // Get other settings.
84
  $font_size = (int) variable_get('image_captcha_font_size', 30);
85
  list($width, $height) = _image_captcha_image_size($code);
86

    
87
  // Create image resource.
88
  $image = imagecreatetruecolor($width, $height);
89
  if (!$image) {
90
    return FALSE;
91
  }
92

    
93
  // Get the background color and paint the background.
94
  $background_rgb = _image_captcha_hex_to_rgb(variable_get('image_captcha_background_color', '#ffffff'));
95
  $background_color = imagecolorallocate($image, $background_rgb[0], $background_rgb[1], $background_rgb[2]);
96
  // Set transparency if needed.
97
  $file_format = variable_get('image_captcha_file_format', IMAGE_CAPTCHA_FILE_FORMAT_JPG);
98
  if ($file_format == IMAGE_CAPTCHA_FILE_FORMAT_TRANSPARENT_PNG) {
99
    imagecolortransparent($image, $background_color);
100
  }
101
  imagefilledrectangle($image, 0, 0, $width, $height, $background_color);
102

    
103
  // Do we need to draw in RTL mode?
104
  global $language;
105
  $rtl = $language->direction && ((bool) variable_get('image_captcha_rtl_support', 0));
106

    
107
  // Draw text.
108
  $result = _image_captcha_image_generator_print_string($image, $width, $height, $fonts, $font_size, $code, $rtl);
109
  if (!$result) {
110
    return FALSE;
111
  }
112

    
113
  // Add noise.
114
  $noise_colors = array();
115
  for ($i = 0; $i < 20; $i++) {
116
    $noise_colors[] = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
117
  }
118
  // Add additional noise.
119
  if (variable_get('image_captcha_dot_noise', 0)) {
120
    _image_captcha_image_generator_add_dots($image, $width, $height, $noise_colors);
121
  }
122
  if (variable_get('image_captcha_line_noise', 0)) {
123
    _image_captcha_image_generator_add_lines($image, $width, $height, $noise_colors);
124
  }
125

    
126
  // Distort the image.
127
  $distortion_amplitude = .25 * $font_size * variable_get('image_captcha_distortion_amplitude', 0) / 10.0;
128
  if ($distortion_amplitude > 1) {
129
    // Distortion parameters.
130
    $wavelength_xr = (2 + 3 * mt_rand(0, 1000) / 1000) * $font_size;
131
    $wavelength_yr = (2 + 3 * mt_rand(0, 1000) / 1000) * $font_size;
132
    $freq_xr = 2 * 3.141592 / $wavelength_xr;
133
    $freq_yr = 2 * 3.141592 / $wavelength_yr;
134
    $wavelength_xt = (2 + 3 * mt_rand(0, 1000) / 1000) * $font_size;
135
    $wavelength_yt = (2 + 3 * mt_rand(0, 1000) / 1000) * $font_size;
136
    $freq_xt = 2 * 3.141592 / $wavelength_xt;
137
    $freq_yt = 2 * 3.141592 / $wavelength_yt;
138

    
139
    $distorted_image = imagecreatetruecolor($width, $height);
140
    if ($file_format == IMAGE_CAPTCHA_FILE_FORMAT_TRANSPARENT_PNG) {
141
      imagecolortransparent($distorted_image, $background_color);
142
    }
143
    if (!$distorted_image) {
144
      return FALSE;
145
    }
146

    
147
    if (variable_get('image_captcha_bilinear_interpolation', FALSE)) {
148
      // Distortion with bilinear interpolation.
149
      for ($x = 0; $x < $width; $x++) {
150
        for ($y = 0; $y < $height; $y++) {
151
          // Get distorted sample point in source image.
152
          $r = $distortion_amplitude * sin($x * $freq_xr + $y * $freq_yr);
153
          $theta = $x * $freq_xt + $y * $freq_yt;
154
          $sx = $x + $r * cos($theta);
155
          $sy = $y + $r * sin($theta);
156
          $sxf = (int) floor($sx);
157
          $syf = (int) floor($sy);
158
          if ($sxf < 0 || $syf < 0 || $sxf >= $width - 1 || $syf >= $height - 1) {
159
            $color = $background_color;
160
          }
161
          else {
162
            // Bilinear interpolation: sample at four corners.
163
            $color_00 = imagecolorat($image, $sxf, $syf);
164
            $color_00_r = ($color_00 >> 16) & 0xFF;
165
            $color_00_g = ($color_00 >> 8) & 0xFF;
166
            $color_00_b = $color_00 & 0xFF;
167
            $color_10 = imagecolorat($image, $sxf + 1, $syf);
168
            $color_10_r = ($color_10 >> 16) & 0xFF;
169
            $color_10_g = ($color_10 >> 8) & 0xFF;
170
            $color_10_b = $color_10 & 0xFF;
171
            $color_01 = imagecolorat($image, $sxf, $syf + 1);
172
            $color_01_r = ($color_01 >> 16) & 0xFF;
173
            $color_01_g = ($color_01 >> 8) & 0xFF;
174
            $color_01_b = $color_01 & 0xFF;
175
            $color_11 = imagecolorat($image, $sxf + 1, $syf + 1);
176
            $color_11_r = ($color_11 >> 16) & 0xFF;
177
            $color_11_g = ($color_11 >> 8) & 0xFF;
178
            $color_11_b = $color_11 & 0xFF;
179
            // Interpolation factors.
180
            $u  = $sx - $sxf;
181
            $v  = $sy - $syf;
182
            // Interpolate.
183
            $r = (int) ((1 - $v) * ((1 - $u) * $color_00_r + $u * $color_10_r) + $v * ((1 - $u) * $color_01_r + $u * $color_11_r));
184
            $g = (int) ((1 - $v) * ((1 - $u) * $color_00_g + $u * $color_10_g) + $v * ((1 - $u) * $color_01_g + $u * $color_11_g));
185
            $b = (int) ((1 - $v) * ((1 - $u) * $color_00_b + $u * $color_10_b) + $v * ((1 - $u) * $color_01_b + $u * $color_11_b));
186
            // Build color.
187
            $color = ($r<<16) + ($g<<8) + $b;
188
          }
189
          imagesetpixel($distorted_image, $x, $y, $color);
190
        }
191
      }
192
    }
193
    else {
194
      // Distortion with nearest neighbor interpolation.
195
      for ($x = 0; $x < $width; $x++) {
196
        for ($y = 0; $y < $height; $y++) {
197
          // Get distorted sample point in source image.
198
          $r = $distortion_amplitude * sin($x * $freq_xr + $y * $freq_yr);
199
          $theta = $x * $freq_xt + $y * $freq_yt;
200
          $sx = $x + $r * cos($theta);
201
          $sy = $y + $r * sin($theta);
202
          $sxf = (int) floor($sx);
203
          $syf = (int) floor($sy);
204
          if ($sxf < 0 || $syf < 0 || $sxf >= $width - 1 || $syf >= $height - 1) {
205
            $color = $background_color;
206
          }
207
          else {
208
            $color = imagecolorat($image, $sxf, $syf);
209
          }
210
          imagesetpixel($distorted_image, $x, $y, $color);
211
        }
212
      }
213
    }
214
    // Release undistorted image.
215
    imagedestroy($image);
216
    // Return distorted image.
217
    return $distorted_image;
218
  }
219
  else {
220
    return $image;
221
  }
222
}
223

    
224
/**
225
 * Add lines.
226
 */
227
function _image_captcha_image_generator_add_lines(&$image, $width, $height, $colors) {
228
  $line_quantity = $width * $height / 200.0 * ((int) variable_get('image_captcha_noise_level', 5)) / 10.0;
229
  for ($i = 0; $i < $line_quantity; $i++) {
230
    imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $colors[array_rand($colors)]);
231
  }
232
}
233

    
234
/**
235
 * Add dots.
236
 */
237
function _image_captcha_image_generator_add_dots(&$image, $width, $height, $colors) {
238
  $noise_quantity = $width * $height * ((int) variable_get('image_captcha_noise_level', 5)) / 10.0;
239
  for ($i = 0; $i < $noise_quantity; $i++) {
240
    imagesetpixel($image, mt_rand(0, $width), mt_rand(0, $height), $colors[array_rand($colors)]);
241
  }
242
}
243

    
244
/**
245
 * Helper function for drawing text on the image.
246
 */
247
function _image_captcha_image_generator_print_string(&$image, $width, $height, $fonts, $font_size, $text, $rtl = FALSE) {
248
  // Get characters.
249
  $characters = _image_captcha_utf8_split($text);
250
  $character_quantity = count($characters);
251

    
252
  // Get colors.
253
  $background_rgb = _image_captcha_hex_to_rgb(variable_get('image_captcha_background_color', '#ffffff'));
254
  $foreground_rgb = _image_captcha_hex_to_rgb(variable_get('image_captcha_foreground_color', '#000000'));
255
  $background_color = imagecolorallocate($image, $background_rgb[0], $background_rgb[1], $background_rgb[2]);
256
  $foreground_color = imagecolorallocate($image, $foreground_rgb[0], $foreground_rgb[1], $foreground_rgb[2]);
257
  // Precalculate the value ranges for color randomness.
258
  $foreground_randomness = (int) (variable_get('image_captcha_foreground_color_randomness', 100));
259
  if ($foreground_randomness) {
260
    $foreground_color_range = array();
261
    for ($i = 0; $i < 3; $i++) {
262
      $foreground_color_range[$i] = array(
263
        max(0, $foreground_rgb[$i] - $foreground_randomness),
264
        min(255, $foreground_rgb[$i] + $foreground_randomness),
265
      );
266
    }
267
  }
268

    
269
  // Set default text color.
270
  $color = $foreground_color;
271

    
272
  // The image is seperated in different character cages, one for each character,
273
  // each character will be somewhere inside that cage.
274
  $ccage_width = $width / $character_quantity;
275
  $ccage_height = $height;
276

    
277
  foreach ($characters as $c => $character) {
278
    // Initial position of character: in the center of its cage.
279
    $center_x = ($c + 0.5) * $ccage_width;
280
    if ($rtl) {
281
      $center_x = $width - $center_x;
282
    }
283
    $center_y = 0.5 * $height;
284

    
285
    // Pick a random font from the list.
286
    $font = $fonts[array_rand($fonts)];
287

    
288
    // Get character dimensions for TrueType fonts.
289
    if ($font != 'BUILTIN') {
290
      $bbox = imagettfbbox($font_size, 0, drupal_realpath($font), $character);
291
      // In very rare cases with some versions of the GD library, the x-value
292
      // of the left side of the bounding box as returned by the first call of
293
      // imagettfbbox is corrupt (value -2147483648 = 0x80000000).
294
      // The weird thing is that calling the function a second time
295
      // can be used as workaround.
296
      // This issue is discussed at http://drupal.org/node/349218.
297
      if ($bbox[2] < 0) {
298
        $bbox = imagettfbbox($font_size, 0, drupal_realpath($font), $character);
299
      }
300
    }
301
    else {
302
      $character_width = imagefontwidth(5);
303
      $character_height = imagefontheight(5);
304
      $bbox = array(
305
        0,
306
        $character_height,
307
        $character_width,
308
        $character_height,
309
        $character_width,
310
        0,
311
        0,
312
        0,
313
      );
314
    }
315

    
316
    // Random (but small) rotation of the character.
317
    // TODO: add a setting for this?
318
    $angle = mt_rand(-10, 10);
319

    
320
    // Determine print position: at what coordinate should the character be
321
    // printed so that the bounding box would be nicely centered in the cage?
322
    $bb_center_x = .5 * ($bbox[0] + $bbox[2]);
323
    $bb_center_y = .5 * ($bbox[1] + $bbox[7]);
324
    $angle_cos = cos($angle * 3.1415 / 180);
325
    $angle_sin = sin($angle * 3.1415 / 180);
326
    $pos_x = $center_x - ($angle_cos * $bb_center_x + $angle_sin * $bb_center_y);
327
    $pos_y = $center_y - (-$angle_sin * $bb_center_x + $angle_cos * $bb_center_y);
328

    
329
    // Calculate available room to jitter: how much can the character be moved
330
    // so that it stays inside its cage?
331
    $bb_width = $bbox[2] - $bbox[0];
332
    $bb_height = $bbox[1] - $bbox[7];
333
    $dev_x = .5 * max(0, $ccage_width - abs($angle_cos) * $bb_width - abs($angle_sin) * $bb_height);
334
    $dev_y = .5 * max(0, $ccage_height - abs($angle_cos) * $bb_height - abs($angle_sin) * $bb_width);
335

    
336
    // Add jitter to position.
337
    $pos_x = $pos_x + mt_rand(-$dev_x, $dev_x);
338
    $pos_y = $pos_y + mt_rand(-$dev_y, $dev_y);
339

    
340
    // Calculate text color in case of randomness.
341
    if ($foreground_randomness) {
342
      $color = imagecolorallocate($image,
343
        mt_rand($foreground_color_range[0][0], $foreground_color_range[0][1]),
344
        mt_rand($foreground_color_range[1][0], $foreground_color_range[1][1]),
345
        mt_rand($foreground_color_range[2][0], $foreground_color_range[2][1])
346
      );
347
    }
348

    
349
    // Draw character.
350
    if ($font == 'BUILTIN') {
351
      imagestring($image, 5, $pos_x, $pos_y, $character, $color);
352
    }
353
    else {
354
      imagettftext($image, $font_size, $angle, $pos_x, $pos_y, $color, drupal_realpath($font), $character);
355
    }
356

    
357
    // For debugging purposes: draw character bounding box (only valid when rotation is disabled).
358
    // imagerectangle($image, $pos_x + $bbox[0], $pos_y + $bbox[1], $pos_x + $bbox[2], $pos_y + $bbox[7], $color);
359
  }
360

    
361
  // Return a sign of success.
362
  return TRUE;
363
}