~spreadubuntu/spreadubuntu/devel-drupal6

« back to all changes in this revision

Viewing changes to sites/all/modules/captcha/image_captcha/image_captcha.admin.inc

  • Committer: ruben
  • Date: 2009-06-08 09:38:49 UTC
  • Revision ID: ruben@captive-20090608093849-s1qtsyctv2vwp1x1
SpreadUbuntu moving to Drupal6. Based on ubuntu-drupal theme and adding our modules

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
// $Id: image_captcha.admin.inc,v 1.19 2009/05/14 22:47:38 soxofaan Exp $
 
3
 
 
4
/**
 
5
 * @file
 
6
 * Functions for administration/settings interface.
 
7
 */
 
8
 
 
9
/**
 
10
 * function to get a list of available fonts
 
11
 */
 
12
function _image_captcha_available_fonts() {
 
13
  $available_fonts = array('BUILTIN' => t('Built-in font'));
 
14
  $fontsdirectories = array(
 
15
    drupal_get_path('module', 'image_captcha') .'/fonts',
 
16
    file_directory_path(),
 
17
  );
 
18
  foreach ($fontsdirectories as $fontsdirectory) {
 
19
    foreach (file_scan_directory($fontsdirectory, '\.[tT][tT][fF]$') as $filename => $font) {
 
20
      $available_fonts[$filename] = "{$font->basename} ($filename)";
 
21
    }
 
22
  }
 
23
  return $available_fonts;
 
24
}
 
25
 
 
26
/**
 
27
 * Configuration form for image_captcha
 
28
 */
 
29
function image_captcha_settings_form() {
 
30
  $form = array();
 
31
 
 
32
  // First some error checking.
 
33
  $image_captcha_setup_errno = _image_captcha_check_setup();
 
34
  if ($image_captcha_setup_errno & IMAGE_CAPTCHA_ERROR_NO_GDLIB) {
 
35
    drupal_set_message(t(
 
36
      'The Image CAPTCHA module can not generate images because your PHP setup does not support it (no <a href="!gdlib">GD library</a>).',
 
37
      array('!gdlib' => 'http://php.net/manual/en/book.image.php')
 
38
    ), 'error');
 
39
    // It is no use to continue building the rest of the settings form.
 
40
    return $form;
 
41
  }
 
42
 
 
43
  $form['image_captcha_example'] = array(
 
44
    '#type' => 'fieldset',
 
45
    '#title' => t('Example'),
 
46
    '#description' => t('Presolved image CAPTCHA example, generated with the current settings.'),
 
47
  );
 
48
  $form['image_captcha_example']['image'] = array(
 
49
    '#type' => 'captcha',
 
50
    '#captcha_type' => 'image_captcha/Image',
 
51
    '#captcha_admin_mode' => TRUE,
 
52
  );
 
53
 
 
54
  // General code settings.
 
55
  $form['image_captcha_code_settings'] = array(
 
56
    '#type' => 'fieldset',
 
57
    '#title' => t('Code settings'),
 
58
  );
 
59
  $form['image_captcha_code_settings']['image_captcha_image_allowed_chars'] = array(
 
60
    '#type' => 'textfield',
 
61
    '#title' => t('Characters to use in the code'),
 
62
    '#default_value' => variable_get('image_captcha_image_allowed_chars', IMAGE_CAPTCHA_ALLOWED_CHARACTERS),
 
63
  );
 
64
  $form['image_captcha_code_settings']['image_captcha_code_length'] = array(
 
65
    '#type' => 'select',
 
66
    '#title' => t('Code length'),
 
67
    '#options' => array(2 => 2, 3, 4, 5, 6, 7, 8, 9, 10),
 
68
    '#default_value' => (int) variable_get('image_captcha_code_length', 5),
 
69
    '#description' => t('The code length influences the size of the image. Note that larger values make the image generation more CPU intensive.'),
 
70
  );
 
71
 
 
72
  // font related stuff
 
73
  $form['image_captcha_font_settings'] = array(
 
74
    '#type' => 'fieldset',
 
75
    '#title' => t('Font settings'),
 
76
  );
 
77
  $available_fonts = _image_captcha_available_fonts();
 
78
  list($default_font, $errno) = _image_captcha_get_font();
 
79
  $form['image_captcha_font_settings']['image_captcha_font'] = array(
 
80
    '#type' => 'select',
 
81
    '#title' => t('Font'),
 
82
    '#default_value' => $default_font,
 
83
    '#description' => t('The TrueType font (.ttf) to use for the text in the image CAPTCHA.'),
 
84
    '#options' => $available_fonts,
 
85
  );
 
86
  // add a prerender procedure for checking that a font should be set.
 
87
  $form['#pre_render'] = array('image_captcha_settings_form_pre_render');
 
88
  // font size
 
89
  if ($default_font != 'BUILTIN') {
 
90
    $form['image_captcha_font_settings']['image_captcha_font_size'] = array(
 
91
      '#type' => 'select',
 
92
      '#title' => t('Font size'),
 
93
      '#options' => array(
 
94
        12 => t('tiny'),
 
95
        20 => t('small'),
 
96
        30 => t('normal'),
 
97
        40 => t('large'),
 
98
      ),
 
99
      '#default_value' => (int) variable_get('image_captcha_font_size', 30),
 
100
      '#description' => t('The font size influences the size of the image. Note that larger values make the image generation more CPU intensive.'),
 
101
    );
 
102
  }
 
103
  // Disable some options when there is no TTF support
 
104
  if ($image_captcha_setup_errno & IMAGE_CAPTCHA_ERROR_NO_TTF_SUPPORT) {
 
105
    $form['image_captcha_font_settings']['image_captcha_font']['#disabled'] = TRUE;
 
106
    $form['image_captcha_font_settings']['image_captcha_font']['#default_value'] = 'BUILTIN';
 
107
  }
 
108
 
 
109
  // character spacing
 
110
  $form['image_captcha_font_settings']['image_captcha_character_spacing'] = array(
 
111
    '#type' => 'select',
 
112
    '#title' => t('Character spacing'),
 
113
    '#description' => t('Define the average spacing between characters. Note that larger values make the image generation more CPU intensive.'),
 
114
    '#default_value' => variable_get('image_captcha_character_spacing', '1.2'),
 
115
    '#options' => array(
 
116
      '1' => t('small'),
 
117
      '1.2' => t('normal'),
 
118
      '1.5' => t('large'),
 
119
    ),
 
120
  );
 
121
 
 
122
    // color settings
 
123
  $form['image_captcha_color_settings'] = array(
 
124
    '#type' => 'fieldset',
 
125
    '#title' => t('Color settings'),
 
126
    '#description' => t('Configuration of the background and text colors in the image CAPTCHA.'),
 
127
  );
 
128
  $form['image_captcha_color_settings']['image_captcha_background_color'] = array(
 
129
    '#type' => 'textfield',
 
130
    '#title' => t('Background color'),
 
131
    '#description' => t('Enter the hexadecimal code for the background color (e.g. #FFF or #FFCE90).'),
 
132
    '#default_value' => variable_get('image_captcha_background_color', '#ffffff'),
 
133
    '#maxlength' => 7,
 
134
    '#size' => 8,
 
135
  );
 
136
  $form['image_captcha_color_settings']['image_captcha_foreground_color'] = array(
 
137
    '#type' => 'textfield',
 
138
    '#title' => t('Text color'),
 
139
    '#description' => t('Enter the hexadecimal code for the text color (e.g. #000 or #004283).'),
 
140
    '#default_value' => variable_get('image_captcha_foreground_color', '#000000'),
 
141
    '#maxlength' => 7,
 
142
    '#size' => 8,
 
143
  );
 
144
  $form['image_captcha_color_settings']['image_captcha_foreground_color_randomness'] = array(
 
145
    '#type' => 'select',
 
146
    '#title' => t('Additional variation of text color'),
 
147
    '#options' => array(
 
148
      0 => t('none'),
 
149
      50 => t('small'),
 
150
      100 => t('moderate'),
 
151
      150 => t('high'),
 
152
      200 => t('very high'),
 
153
    ),
 
154
    '#default_value' => (int) variable_get('image_captcha_foreground_color_randomness', 100),
 
155
    '#description' => t('The different characters will have randomized colors in the specified range around the text color.'),
 
156
  );
 
157
 
 
158
 
 
159
  // distortion and noise settings
 
160
  $form['image_captcha_distortion_and_noise'] = array(
 
161
    '#type' => 'fieldset',
 
162
    '#title' => t('Distortion and noise'),
 
163
    '#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.'),
 
164
  );
 
165
  // distortion
 
166
  $form['image_captcha_distortion_and_noise']['image_captcha_distortion_amplitude'] = array(
 
167
    '#type' => 'select',
 
168
    '#title' => t('Distortion level'),
 
169
    '#options' => array(
 
170
      0 => t('none'),
 
171
      2 => t('low'),
 
172
      4 => t('moderate'),
 
173
      6 => t('normal'),
 
174
      8 => t('high'),
 
175
      10 => t('severe'),
 
176
    ),
 
177
    '#default_value' => (int) variable_get('image_captcha_distortion_amplitude', 6),
 
178
    '#description' => t('Set the degree of wave distortion in the image.'),
 
179
  );
 
180
  $form['image_captcha_distortion_and_noise']['image_captcha_bilinear_interpolation'] = array(
 
181
    '#type' => 'checkbox',
 
182
    '#title' => t('Smooth distortion'),
 
183
    '#default_value' => variable_get('image_captcha_bilinear_interpolation', FALSE),
 
184
    '#description' => t('This option enables bilinear interpolation of the distortion which makes the image look smoother, but it is more CPU intensive.'),
 
185
  );
 
186
  // noise
 
187
  $form['image_captcha_distortion_and_noise']['image_captcha_dot_noise'] = array(
 
188
    '#type' => 'checkbox',
 
189
    '#title' => t('Add salt and pepper noise'),
 
190
    '#default_value' => variable_get('image_captcha_dot_noise', 0),
 
191
    '#description' => t('This option adds randomly colored point noise.'),
 
192
  );
 
193
  $form['image_captcha_distortion_and_noise']['image_captcha_line_noise'] = array(
 
194
    '#type' => 'checkbox',
 
195
    '#title' => t('Add line noise'),
 
196
    '#default_value' => variable_get('image_captcha_line_noise', 0),
 
197
    '#description' => t('This option enables lines randomly drawn on top of the text code.'),
 
198
  );
 
199
  $form['image_captcha_distortion_and_noise']['image_captcha_noise_level'] = array(
 
200
    '#type' => 'select',
 
201
    '#title' => t('Noise level'),
 
202
    '#options' => array(
 
203
      1 => t('1 - low'),
 
204
      2 => '2',
 
205
      3 => t('3 - medium'),
 
206
      4 => '4',
 
207
      5 => t('5 - high'),
 
208
      7 => '7',
 
209
      10 => t('10 - severe'),
 
210
    ),
 
211
    '#default_value' => (int) variable_get('image_captcha_noise_level', 5),
 
212
  );
 
213
  $form['#validate'] = array('image_captcha_settings_form_validate');
 
214
  return system_settings_form($form);
 
215
}
 
216
 
 
217
/**
 
218
 * Pre render function for image_captcha_settings_form for providing
 
219
 * extra info about TTF fonts.
 
220
 */
 
221
function image_captcha_settings_form_pre_render($form) {
 
222
  $errno = _image_captcha_check_setup();
 
223
 
 
224
  if ($errno & IMAGE_CAPTCHA_ERROR_NO_TTF_SUPPORT) {
 
225
    drupal_set_message(
 
226
      t('The Image CAPTCHA module can not use True Type fonts because your PHP setup does not support it. There is only a built-in bitmap font available.'),
 
227
      'warning'
 
228
    );
 
229
  }
 
230
  else if (count($form['image_captcha_font_settings']['image_captcha_font']['#options']) == 1) {
 
231
    drupal_set_message(t('The image CAPTCHA module works best with a TrueType font file (TTF) for generating the images, but because of licencing issues it is not allowed to package fonts with the module. A simple built-in bitmap font is provided as default instead. You can install TrueType fonts yourself by putting them in the fonts directory of the image CAPTCHA module (directory "%fontsdir") or by uploading them to your Drupal file system (directory "%filesdir") with for example the upload module.', array('%fontsdir' => drupal_get_path('module', 'image_captcha') .'/fonts', '%filesdir' => file_directory_path())), 'warning');
 
232
  }
 
233
  if ($form['image_captcha_font_settings']['image_captcha_font']['#default_value'] == 'BUILTIN') {
 
234
    drupal_set_message(t('The usage of the built-in bitmap font it is not recommended because of its small size and missing UTF-8 support.'), 'warning');
 
235
  }
 
236
  return $form;
 
237
}
 
238
 
 
239
/**
 
240
 * Validation function for image_captcha configuration form
 
241
 */
 
242
function image_captcha_settings_form_validate($form, &$form_state) {
 
243
  // check image_captcha_image_allowed_chars for spaces
 
244
  if (preg_match('/\s/', $form_state['values']['image_captcha_image_allowed_chars'])) {
 
245
    form_set_error('image_captcha_image_allowed_chars', t('The list of characters to use should not contain spaces.'));
 
246
  }
 
247
  // check font
 
248
  $font = $form_state['values']['image_captcha_font'];
 
249
  if ($font == '0') {
 
250
    form_set_error('image_captcha_font', t('You need to select a font'));
 
251
  }
 
252
  elseif ($font != 'BUILTIN' && (!is_file($font) || !is_readable($font))) {
 
253
    form_set_error('image_captcha_font', t('Font does not exist or is not readable.'));
 
254
  }
 
255
 
 
256
  // check color settings
 
257
  if (!preg_match('/^#([0-9a-fA-F]{3}){1,2}$/', $form_state['values']['image_captcha_background_color'])) {
 
258
    form_set_error('image_captcha_background_color', t('Background color is not a valid hexadecimal color value.'));
 
259
  }
 
260
  if (!preg_match('/^#([0-9a-fA-F]{3}){1,2}$/', $form_state['values']['image_captcha_foreground_color'])) {
 
261
    form_set_error('image_captcha_foreground_color', t('Text color is not a valid hexadecimal color value.'));
 
262
  }
 
263
}