2
// $Id: image_captcha.admin.inc,v 1.19 2009/05/14 22:47:38 soxofaan Exp $
6
* Functions for administration/settings interface.
10
* function to get a list of available fonts
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(),
18
foreach ($fontsdirectories as $fontsdirectory) {
19
foreach (file_scan_directory($fontsdirectory, '\.[tT][tT][fF]$') as $filename => $font) {
20
$available_fonts[$filename] = "{$font->basename} ($filename)";
23
return $available_fonts;
27
* Configuration form for image_captcha
29
function image_captcha_settings_form() {
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) {
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')
39
// It is no use to continue building the rest of the settings form.
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.'),
48
$form['image_captcha_example']['image'] = array(
50
'#captcha_type' => 'image_captcha/Image',
51
'#captcha_admin_mode' => TRUE,
54
// General code settings.
55
$form['image_captcha_code_settings'] = array(
56
'#type' => 'fieldset',
57
'#title' => t('Code settings'),
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),
64
$form['image_captcha_code_settings']['image_captcha_code_length'] = array(
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.'),
73
$form['image_captcha_font_settings'] = array(
74
'#type' => 'fieldset',
75
'#title' => t('Font settings'),
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(
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,
86
// add a prerender procedure for checking that a font should be set.
87
$form['#pre_render'] = array('image_captcha_settings_form_pre_render');
89
if ($default_font != 'BUILTIN') {
90
$form['image_captcha_font_settings']['image_captcha_font_size'] = array(
92
'#title' => t('Font size'),
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.'),
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';
110
$form['image_captcha_font_settings']['image_captcha_character_spacing'] = array(
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'),
117
'1.2' => t('normal'),
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.'),
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'),
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'),
144
$form['image_captcha_color_settings']['image_captcha_foreground_color_randomness'] = array(
146
'#title' => t('Additional variation of text color'),
150
100 => t('moderate'),
152
200 => t('very high'),
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.'),
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.'),
166
$form['image_captcha_distortion_and_noise']['image_captcha_distortion_amplitude'] = array(
168
'#title' => t('Distortion level'),
177
'#default_value' => (int) variable_get('image_captcha_distortion_amplitude', 6),
178
'#description' => t('Set the degree of wave distortion in the image.'),
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.'),
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.'),
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.'),
199
$form['image_captcha_distortion_and_noise']['image_captcha_noise_level'] = array(
201
'#title' => t('Noise level'),
205
3 => t('3 - medium'),
209
10 => t('10 - severe'),
211
'#default_value' => (int) variable_get('image_captcha_noise_level', 5),
213
$form['#validate'] = array('image_captcha_settings_form_validate');
214
return system_settings_form($form);
218
* Pre render function for image_captcha_settings_form for providing
219
* extra info about TTF fonts.
221
function image_captcha_settings_form_pre_render($form) {
222
$errno = _image_captcha_check_setup();
224
if ($errno & IMAGE_CAPTCHA_ERROR_NO_TTF_SUPPORT) {
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.'),
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');
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');
240
* Validation function for image_captcha configuration form
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.'));
248
$font = $form_state['values']['image_captcha_font'];
250
form_set_error('image_captcha_font', t('You need to select a font'));
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.'));
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.'));
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.'));