source: sipes/modules_contrib/captcha/image_captcha/image_captcha.admin.inc @ 92213c1

stableversion-3.0
Last change on this file since 92213c1 was 177a560, checked in by José Gregorio Puentes <jpuentes@…>, 8 años ago

se agrego el directorio de modulos contribuidos de drupal

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