Mercurial > hg > rr-repo
comparison sites/all/modules/captcha/image_captcha/image_captcha.admin.inc @ 2:b74b41bb73f0
-- Google analytics module
author | danieleb <danielebarchiesi@me.com> |
---|---|
date | Thu, 22 Aug 2013 17:22:54 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1:67ce89da90df | 2:b74b41bb73f0 |
---|---|
1 <?php | |
2 | |
3 /** | |
4 * @file | |
5 * Functions for administration/settings interface. | |
6 * | |
7 */ | |
8 | |
9 | |
10 /** | |
11 * Configuration form for image_captcha. | |
12 */ | |
13 function image_captcha_settings_form() { | |
14 | |
15 $form = array(); | |
16 | |
17 // Add CSS for theming of admin form. | |
18 $form['#attached']['css'] = array(drupal_get_path('module', 'image_captcha') . '/image_captcha.css'); | |
19 // Use javascript for some added usability on admin form. | |
20 $form['#attached']['js'] = array(drupal_get_path('module', 'image_captcha') . '/image_captcha.js'); | |
21 | |
22 | |
23 // First some error checking. | |
24 $setup_status = _image_captcha_check_setup(FALSE); | |
25 if ($setup_status & IMAGE_CAPTCHA_ERROR_NO_GDLIB) { | |
26 drupal_set_message(t( | |
27 'The Image CAPTCHA module can not generate images because your PHP setup does not support it (no <a href="!gdlib">GD library</a> with JPEG support).', | |
28 array('!gdlib' => 'http://php.net/manual/en/book.image.php') | |
29 ), 'error'); | |
30 // It is no use to continue building the rest of the settings form. | |
31 return $form; | |
32 } | |
33 | |
34 $form['image_captcha_example'] = array( | |
35 '#type' => 'fieldset', | |
36 '#title' => t('Example'), | |
37 '#description' => t('Presolved image CAPTCHA example, generated with the current settings.'), | |
38 ); | |
39 $form['image_captcha_example']['image'] = array( | |
40 '#type' => 'captcha', | |
41 '#captcha_type' => 'image_captcha/Image', | |
42 '#captcha_admin_mode' => TRUE, | |
43 ); | |
44 | |
45 // General code settings. | |
46 $form['image_captcha_code_settings'] = array( | |
47 '#type' => 'fieldset', | |
48 '#title' => t('Code settings'), | |
49 ); | |
50 $form['image_captcha_code_settings']['image_captcha_image_allowed_chars'] = array( | |
51 '#type' => 'textfield', | |
52 '#title' => t('Characters to use in the code'), | |
53 '#default_value' => variable_get('image_captcha_image_allowed_chars', IMAGE_CAPTCHA_ALLOWED_CHARACTERS), | |
54 ); | |
55 $form['image_captcha_code_settings']['image_captcha_code_length'] = array( | |
56 '#type' => 'select', | |
57 '#title' => t('Code length'), | |
58 '#options' => array(2 => 2, 3, 4, 5, 6, 7, 8, 9, 10), | |
59 '#default_value' => (int) variable_get('image_captcha_code_length', 5), | |
60 '#description' => t('The code length influences the size of the image. Note that larger values make the image generation more CPU intensive.'), | |
61 ); | |
62 // RTL support option (only show this option when there are RTL languages). | |
63 $languages = language_list('direction'); | |
64 if (isset($languages[LANGUAGE_RTL])) { | |
65 $form['image_captcha_code_settings']['image_captcha_rtl_support'] = array( | |
66 '#type' => 'checkbox', | |
67 '#title' => t('RTL support'), | |
68 '#default_value' => variable_get('image_captcha_rtl_support', 0), | |
69 '#description' => t('Enable this option to render the code from right to left for right to left languages.'), | |
70 ); | |
71 } | |
72 | |
73 | |
74 // Font related stuff. | |
75 $form['image_captcha_font_settings'] = _image_captcha_settings_form_font_section(); | |
76 | |
77 // Color and file format settings. | |
78 $form['image_captcha_color_settings'] = array( | |
79 '#type' => 'fieldset', | |
80 '#title' => t('Color and image settings'), | |
81 '#description' => t('Configuration of the background, text colors and file format of the image CAPTCHA.'), | |
82 ); | |
83 $form['image_captcha_color_settings']['image_captcha_background_color'] = array( | |
84 '#type' => 'textfield', | |
85 '#title' => t('Background color'), | |
86 '#description' => t('Enter the hexadecimal code for the background color (e.g. #FFF or #FFCE90). When using the PNG file format with transparent background, it is recommended to set this close to the underlying background color.'), | |
87 '#default_value' => variable_get('image_captcha_background_color', '#ffffff'), | |
88 '#maxlength' => 7, | |
89 '#size' => 8, | |
90 ); | |
91 $form['image_captcha_color_settings']['image_captcha_foreground_color'] = array( | |
92 '#type' => 'textfield', | |
93 '#title' => t('Text color'), | |
94 '#description' => t('Enter the hexadecimal code for the text color (e.g. #000 or #004283).'), | |
95 '#default_value' => variable_get('image_captcha_foreground_color', '#000000'), | |
96 '#maxlength' => 7, | |
97 '#size' => 8, | |
98 ); | |
99 $form['image_captcha_color_settings']['image_captcha_foreground_color_randomness'] = array( | |
100 '#type' => 'select', | |
101 '#title' => t('Additional variation of text color'), | |
102 '#options' => array( | |
103 0 => t('No variation'), | |
104 50 => t('Little variation'), | |
105 100 => t('Medium variation'), | |
106 150 => t('High variation'), | |
107 200 => t('Very high variation'), | |
108 ), | |
109 '#default_value' => (int) variable_get('image_captcha_foreground_color_randomness', 100), | |
110 '#description' => t('The different characters will have randomized colors in the specified range around the text color.'), | |
111 ); | |
112 $form['image_captcha_color_settings']['image_captcha_file_format'] = array( | |
113 '#type' => 'select', | |
114 '#title' => t('File format'), | |
115 '#description' => t('Select the file format for the image. JPEG usually results in smaller files, PNG allows tranparency.'), | |
116 '#default_value' => variable_get('image_captcha_file_format', IMAGE_CAPTCHA_FILE_FORMAT_JPG), | |
117 '#options' => array( | |
118 IMAGE_CAPTCHA_FILE_FORMAT_JPG => t('JPEG'), | |
119 IMAGE_CAPTCHA_FILE_FORMAT_PNG => t('PNG'), | |
120 IMAGE_CAPTCHA_FILE_FORMAT_TRANSPARENT_PNG => t('PNG with transparent background'), | |
121 ), | |
122 ); | |
123 | |
124 // distortion and noise settings | |
125 $form['image_captcha_distortion_and_noise'] = array( | |
126 '#type' => 'fieldset', | |
127 '#title' => t('Distortion and noise'), | |
128 '#description' => t('With these settings you can control the degree of obfuscation by distortion and added noise. Do not exaggerate the obfuscation and assure that the code in the image is reasonably readable. For example, do not combine high levels of distortion and noise.'), | |
129 ); | |
130 // distortion | |
131 $form['image_captcha_distortion_and_noise']['image_captcha_distortion_amplitude'] = array( | |
132 '#type' => 'select', | |
133 '#title' => t('Distortion level'), | |
134 '#options' => array( | |
135 0 => t('@level - no distortion', array('@level' => '0')), | |
136 1 => t('@level - low', array('@level' => '1')), | |
137 2 => '2', | |
138 3 => '3', | |
139 4 => '4', | |
140 5 => t('@level - medium', array('@level' => '5')), | |
141 6 => '6', | |
142 7 => '7', | |
143 8 => '8', | |
144 9 => '9', | |
145 10 => t('@level - high', array('@level' => '10')), | |
146 ), | |
147 '#default_value' => (int) variable_get('image_captcha_distortion_amplitude', 0), | |
148 '#description' => t('Set the degree of wave distortion in the image.'), | |
149 ); | |
150 $form['image_captcha_distortion_and_noise']['image_captcha_bilinear_interpolation'] = array( | |
151 '#type' => 'checkbox', | |
152 '#title' => t('Smooth distortion'), | |
153 '#default_value' => variable_get('image_captcha_bilinear_interpolation', FALSE), | |
154 '#description' => t('This option enables bilinear interpolation of the distortion which makes the image look smoother, but it is more CPU intensive.'), | |
155 ); | |
156 // noise | |
157 $form['image_captcha_distortion_and_noise']['image_captcha_dot_noise'] = array( | |
158 '#type' => 'checkbox', | |
159 '#title' => t('Add salt and pepper noise'), | |
160 '#default_value' => variable_get('image_captcha_dot_noise', 0), | |
161 '#description' => t('This option adds randomly colored point noise.'), | |
162 ); | |
163 $form['image_captcha_distortion_and_noise']['image_captcha_line_noise'] = array( | |
164 '#type' => 'checkbox', | |
165 '#title' => t('Add line noise'), | |
166 '#default_value' => variable_get('image_captcha_line_noise', 0), | |
167 '#description' => t('This option enables lines randomly drawn on top of the text code.'), | |
168 ); | |
169 $form['image_captcha_distortion_and_noise']['image_captcha_noise_level'] = array( | |
170 '#type' => 'select', | |
171 '#title' => t('Noise level'), | |
172 '#options' => array( | |
173 1 => '1 - ' . t('low'), | |
174 2 => '2', | |
175 3 => '3 - ' . t('medium'), | |
176 4 => '4', | |
177 5 => '5 - ' . t('high'), | |
178 7 => '7', | |
179 10 => '10 - ' . t('severe'), | |
180 ), | |
181 '#default_value' => (int) variable_get('image_captcha_noise_level', 5), | |
182 ); | |
183 | |
184 // Add a validation handler. | |
185 $form['#validate'] = array('image_captcha_settings_form_validate'); | |
186 | |
187 // Make it a settings form. | |
188 $form = system_settings_form($form); | |
189 // But also do some custom submission handling. | |
190 $form['#submit'][] = 'image_captcha_settings_form_submit'; | |
191 | |
192 return $form; | |
193 } | |
194 | |
195 | |
196 /** | |
197 * Form elements for the font specific setting. | |
198 * | |
199 * This is refactored to a separate function to avoid poluting the | |
200 * general form function image_captcha_settings_form with some | |
201 * specific logic. | |
202 * | |
203 * @return $form, the font settings specific form elements. | |
204 */ | |
205 function _image_captcha_settings_form_font_section() { | |
206 // Put it all in a fieldset. | |
207 $form = array( | |
208 '#type' => 'fieldset', | |
209 '#title' => t('Font settings'), | |
210 ); | |
211 | |
212 // First check if there is TrueType support. | |
213 $setup_status = _image_captcha_check_setup(FALSE); | |
214 if ($setup_status & IMAGE_CAPTCHA_ERROR_NO_TTF_SUPPORT) { | |
215 // Show a warning that there is no TrueType support | |
216 $form['no_ttf_support'] = array( | |
217 '#type' => 'item', | |
218 '#title' => t('No TrueType support'), | |
219 '#markup' => t('The Image CAPTCHA module can not use TrueType fonts because your PHP setup does not support it. You can only use a PHP built-in bitmap font of fixed size.'), | |
220 ); | |
221 | |
222 } | |
223 else { | |
224 | |
225 // Build a list of all available fonts. | |
226 $available_fonts = array(); | |
227 | |
228 // List of folders to search through for TrueType fonts. | |
229 $fonts = _image_captcha_get_available_fonts_from_directories(); | |
230 // Cache the list of previewable fonts. All the previews are done | |
231 // in separate requests, and we don't want to rescan the filesystem | |
232 // every time, so we cache the result. | |
233 variable_set('image_captcha_fonts_preview_map_cache', $fonts); | |
234 // Put these fonts with preview image in the list | |
235 foreach ($fonts as $token => $font) { | |
236 $img_src = check_url(url('admin/config/people/captcha/image_captcha/font_preview/' . $token)); | |
237 $title = t('Font preview of @font (@file)', array('@font' => $font->name, '@file' => $font->uri)); | |
238 $available_fonts[$font->uri] = '<img src="' . $img_src . '" alt="' . $title . '" title="' . $title . '" />'; | |
239 } | |
240 | |
241 // Append the PHP built-in font at the end. | |
242 $img_src = check_url(url('admin/config/people/captcha/image_captcha/font_preview/BUILTIN')); | |
243 $title = t('Preview of built-in font'); | |
244 $available_fonts['BUILTIN'] = t('PHP built-in font: !font_preview', | |
245 array('!font_preview' => '<img src="' . $img_src . '" alt="' . $title . '" title="' . $title . '" />') | |
246 ); | |
247 | |
248 $default_fonts = _image_captcha_get_enabled_fonts(); | |
249 $form['image_captcha_fonts'] = array( | |
250 '#type' => 'checkboxes', | |
251 '#title' => t('Fonts'), | |
252 '#default_value' => $default_fonts, | |
253 '#description' => t('Select the fonts to use for the text in the image CAPTCHA. Apart from the provided defaults, you can also use your own TrueType fonts (filename extension .ttf) by putting them in %fonts_library_general or %fonts_library_specific.', | |
254 array( | |
255 '%fonts_library_general' => 'sites/all/libraries/fonts', | |
256 '%fonts_library_specific' => conf_path() . '/libraries/fonts', | |
257 ) | |
258 ), | |
259 '#options' => $available_fonts, | |
260 '#attributes' => array('class' => array('image_captcha_admin_fonts_selection')), | |
261 '#process' => array('form_process_checkboxes'), | |
262 ); | |
263 | |
264 | |
265 // Font size. | |
266 $form['image_captcha_font_size'] = array( | |
267 '#type' => 'select', | |
268 '#title' => t('Font size'), | |
269 '#options' => array( | |
270 9 => '9 pt - ' . t('tiny'), | |
271 12 => '12 pt - ' . t('small'), | |
272 18 => '18 pt', | |
273 24 => '24 pt - ' . t('normal'), | |
274 30 => '30 pt', | |
275 36 => '36 pt - ' . t('large'), | |
276 48 => '48 pt', | |
277 64 => '64 pt - ' . t('extra large'), | |
278 ), | |
279 '#default_value' => (int) variable_get('image_captcha_font_size', 30), | |
280 '#description' => t('The font size influences the size of the image. Note that larger values make the image generation more CPU intensive.'), | |
281 ); | |
282 | |
283 } | |
284 | |
285 // Character spacing (available for both the TrueType fonts and the builtin font. | |
286 $form['image_captcha_font_settings']['image_captcha_character_spacing'] = array( | |
287 '#type' => 'select', | |
288 '#title' => t('Character spacing'), | |
289 '#description' => t('Define the average spacing between characters. Note that larger values make the image generation more CPU intensive.'), | |
290 '#default_value' => variable_get('image_captcha_character_spacing', '1.2'), | |
291 '#options' => array( | |
292 '0.75' => t('tight'), | |
293 '1' => t('normal'), | |
294 '1.2' => t('wide'), | |
295 '1.5' => t('extra wide'), | |
296 ), | |
297 ); | |
298 | |
299 return $form; | |
300 } | |
301 | |
302 /** | |
303 * Helper function to get fonts from the given directories. | |
304 * | |
305 * @param $directories (optional) an array of directories | |
306 * to recursively search through, if not given, the default | |
307 * directories will be used. | |
308 * | |
309 * @return an array of fonts file objects (with fields 'name', | |
310 * 'basename' and 'filename'), keyed on the md5 hash of the font | |
311 * path (to have an easy token that can be used in an url | |
312 * without en/decoding issues). | |
313 */ | |
314 function _image_captcha_get_available_fonts_from_directories($directories=NULL) { | |
315 // If no fonts directories are given: use the default. | |
316 if ($directories === NULL) { | |
317 $directories = array( | |
318 drupal_get_path('module', 'image_captcha') . '/fonts', | |
319 'sites/all/libraries/fonts', | |
320 conf_path() . '/libraries/fonts', | |
321 ); | |
322 } | |
323 // Collect the font information. | |
324 $fonts = array(); | |
325 foreach ($directories as $directory) { | |
326 foreach (file_scan_directory($directory, '/\.[tT][tT][fF]$/') as $filename => $font) { | |
327 $fonts[md5($filename)] = $font; | |
328 } | |
329 } | |
330 | |
331 return $fonts; | |
332 } | |
333 | |
334 | |
335 /** | |
336 * Validation function for image_captcha configuration form | |
337 */ | |
338 function image_captcha_settings_form_validate($form, &$form_state) { | |
339 // Check image_captcha_image_allowed_chars for spaces. | |
340 if (preg_match('/\s/', $form_state['values']['image_captcha_image_allowed_chars'])) { | |
341 form_set_error('image_captcha_image_allowed_chars', t('The list of characters to use should not contain spaces.')); | |
342 } | |
343 | |
344 if (!isset($form['image_captcha_font_settings']['no_ttf_support'])) { | |
345 // Check the selected fonts. | |
346 // Filter the image_captcha fonts array to pick out the selected ones. | |
347 $fonts = array_filter($form_state['values']['image_captcha_fonts']); | |
348 if (count($fonts) < 1) { | |
349 form_set_error('image_captcha_fonts', t('You need to select at least one font.')); | |
350 } | |
351 if ($form_state['values']['image_captcha_fonts']['BUILTIN']) { | |
352 // With the built in font, only latin2 characters should be used. | |
353 if (preg_match('/[^a-zA-Z0-9]/', $form_state['values']['image_captcha_image_allowed_chars'])) { | |
354 form_set_error('image_captcha_image_allowed_chars', t('The built-in font only supports Latin2 characters. Only use "a" to "z" and numbers.')); | |
355 } | |
356 } | |
357 list($readable_fonts, $problem_fonts) = _image_captcha_check_fonts($fonts); | |
358 if (count($problem_fonts) > 0) { | |
359 form_set_error('image_captcha_fonts', t('The following fonts are not readable: %fonts.', array('%fonts' => implode(', ', $problem_fonts)))); | |
360 } | |
361 } | |
362 | |
363 // check color settings | |
364 if (!preg_match('/^#([0-9a-fA-F]{3}){1,2}$/', $form_state['values']['image_captcha_background_color'])) { | |
365 form_set_error('image_captcha_background_color', t('Background color is not a valid hexadecimal color value.')); | |
366 } | |
367 if (!preg_match('/^#([0-9a-fA-F]{3}){1,2}$/', $form_state['values']['image_captcha_foreground_color'])) { | |
368 form_set_error('image_captcha_foreground_color', t('Text color is not a valid hexadecimal color value.')); | |
369 } | |
370 } | |
371 | |
372 /** | |
373 * Submit function for image_captcha configuration form. | |
374 */ | |
375 function image_captcha_settings_form_submit($form, &$form_state) { | |
376 if (!isset($form['image_captcha_font_settings']['no_ttf_support'])) { | |
377 // Filter the image_captcha fonts array to pick out the selected ones. | |
378 $fonts = array_filter($form_state['values']['image_captcha_fonts']); | |
379 variable_set('image_captcha_fonts', $fonts); | |
380 } | |
381 } | |
382 | |
383 /** | |
384 * Menu handler for font preview request. | |
385 * | |
386 */ | |
387 function image_captcha_font_preview($font_token) { | |
388 | |
389 // Get the font from the given font token. | |
390 if ($font_token == 'BUILTIN') { | |
391 $font = 'BUILTIN'; | |
392 } | |
393 else { | |
394 // Get the mapping of font tokens to font file objects. | |
395 $fonts = variable_get('image_captcha_fonts_preview_map_cache', array()); | |
396 if (!isset($fonts[$font_token])) { | |
397 echo('bad token'); | |
398 exit(); | |
399 } | |
400 // Get the font path. | |
401 $font = $fonts[$font_token]->uri; | |
402 // Some sanity checks if the given font is valid. | |
403 if (!is_file($font) || !is_readable($font)) { | |
404 echo('bad font'); | |
405 exit(); | |
406 } | |
407 } | |
408 | |
409 // Settings of the font preview. | |
410 $width = 120; | |
411 $text = 'AaBbCc123'; | |
412 $font_size = 14; | |
413 $height = 2 * $font_size; | |
414 | |
415 // Allocate image resource. | |
416 $image = imagecreatetruecolor($width, $height); | |
417 if (!$image) { | |
418 exit(); | |
419 } | |
420 // White background and black foreground. | |
421 $background_color = imagecolorallocate($image, 255, 255, 255); | |
422 $color = imagecolorallocate($image, 0, 0, 0); | |
423 imagefilledrectangle($image, 0, 0, $width, $height, $background_color); | |
424 | |
425 // Draw preview text | |
426 if ($font == 'BUILTIN') { | |
427 imagestring($image, 5, 1, .5*$height-10, $text, $color); | |
428 } | |
429 else { | |
430 imagettftext($image, $font_size, 0, 1, 1.5*$font_size, $color, realpath($font), $text); | |
431 } | |
432 | |
433 // Set content type. | |
434 drupal_add_http_header('Content-Type', 'image/png'); | |
435 // Dump image data to client. | |
436 imagepng($image); | |
437 // Release image memory. | |
438 imagedestroy($image); | |
439 | |
440 // Close connection. | |
441 exit(); | |
442 } |