/[drupal]/contributions/modules/captcha/captcha.admin.inc
ViewVC logotype

Contents of /contributions/modules/captcha/captcha.admin.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.32 - (show annotations) (download) (as text)
Tue Oct 27 16:11:17 2009 UTC (4 weeks, 3 days ago) by soxofaan
Branch: MAIN
CVS Tags: HEAD
Changes since 1.31: +2 -2 lines
File MIME type: text/x-php
#586232: call_user_func_array() call problem
1 <?php
2 // $Id: captcha.admin.inc,v 1.31 2009/09/24 07:31:26 soxofaan Exp $
3
4 /**
5 * Return an array with the available CAPTCHA types, for use as options array
6 * for a select form elements.
7 *
8 * @param $add_special_options if true: also add a 'none' and 'default' option
9 *
10 * @return an associative array mapping "$module/$type" to
11 * "$type (from module $module)" with $module the module name implementing the CAPTCHA
12 * and $type the name of the CAPTCHA type.
13 */
14 function _captcha_available_challenge_types($add_special_options=TRUE) {
15 if ($add_special_options) {
16 $captcha_types['none'] = '['. t('none') .']';
17 $captcha_types['default'] = '['. t('default challenge type') .']';
18 }
19 // We do our own version of Drupal's module_invoke_all() here because
20 // we want to build an array with custom keys and values.
21 foreach (module_implements('captcha') as $module) {
22 $result = call_user_func_array($module .'_captcha', array('list'));
23 if (is_array($result)) {
24 foreach ($result as $type) {
25 $captcha_types["$module/$type"] = t('@type (from module @module)', array('@type' => $type, '@module' => $module));
26 }
27 }
28 }
29 return $captcha_types;
30 }
31
32 /**
33 * Form builder function for the general CAPTCHA configuration
34 */
35 function captcha_admin_settings() {
36 module_load_include('inc', 'captcha');
37
38 // Use javascript for some added usability on admin form.
39 drupal_add_js(drupal_get_path('module', 'captcha') .'/captcha.js');
40
41 // Configuration of which forms to protect, with what challenge.
42 $form['captcha_form_protection'] = array(
43 '#type' => 'fieldset',
44 '#title' => t('Form protection'),
45 '#description' => t('Select the challenge type you want for each of the listed forms (identified by their so called <em>form_id</em>\'s). You can easily add arbitrary forms with textfield at the bottom of the table or with the help of the \'%CAPTCHA_admin_links\' option below.',
46 array('%CAPTCHA_admin_links' => t('Add CAPTCHA administration links to forms'))
47 ),
48 );
49 $form['captcha_form_protection']['captcha_default_challenge'] = array(
50 '#type' => 'select',
51 '#title' => t('Default challenge type'),
52 '#description' => t('Select the default challenge type for CAPTCHAs. This can be overriden for each form if desired.'),
53 '#options' => _captcha_available_challenge_types(FALSE),
54 '#default_value' => variable_get('captcha_default_challenge', 'captcha/Math'),
55 );
56 // List known form_ids.
57 $form['captcha_form_protection']['captcha_form_id_overview'] = array(
58 '#theme' => 'captcha_admin_settings_captcha_points',
59 '#tree' => TRUE,
60 );
61 $form['captcha_form_protection']['captcha_form_id_overview']['captcha_captcha_points'] = array();
62 $captcha_type_options = _captcha_available_challenge_types();
63 // TODO: make a wrapper for this query to make it a bit cleaner?
64 $result = db_query("SELECT * FROM {captcha_points} ORDER BY form_id");
65 while ($captcha_point = db_fetch_object($result)) {
66 $form['captcha_form_protection']['captcha_form_id_overview']['captcha_captcha_points'][$captcha_point->form_id] = array();
67 $form['captcha_form_protection']['captcha_form_id_overview']['captcha_captcha_points'][$captcha_point->form_id]['form_id'] = array(
68 '#value' => $captcha_point->form_id,
69 );
70 // Select widget for CAPTCHA type.
71 if (isset($captcha_point->module) && $captcha_point->module) {
72 $captcha_type = $captcha_point->module .'/'. $captcha_point->type;
73 }
74 elseif ($captcha_point->type == 'default') {
75 $captcha_type = 'default';
76 }
77 else {
78 $captcha_type = 'none';
79 }
80 $form['captcha_form_protection']['captcha_form_id_overview']['captcha_captcha_points'][$captcha_point->form_id]['captcha_type'] = array(
81 '#type' => 'select',
82 '#default_value' => $captcha_type,
83 '#options' => $captcha_type_options,
84 );
85 // Additional operations.
86 $form['captcha_form_protection']['captcha_form_id_overview']['captcha_captcha_points'][$captcha_point->form_id]['operations'] = array(
87 '#value' => implode(", ", array(
88 l(t('delete'), "admin/user/captcha/captcha/captcha_point/{$captcha_point->form_id}/delete"),
89 ))
90 );
91 }
92
93 // Form items for new form_id.
94 $form['captcha_form_protection']['captcha_form_id_overview']['captcha_new_captcha_point'] = array();
95 // Textfield for form_id.
96 $form['captcha_form_protection']['captcha_form_id_overview']['captcha_new_captcha_point']['form_id'] = array(
97 '#type' => 'textfield',
98 '#size' => 16,
99 );
100 // Select widget for CAPTCHA type.
101 $form['captcha_form_protection']['captcha_form_id_overview']['captcha_new_captcha_point']['captcha_type'] = array(
102 '#type' => 'select',
103 '#default_value' => 'none',
104 '#options' => $captcha_type_options,
105 );
106
107 // Field for the CAPTCHA administration mode.
108 $form['captcha_form_protection']['captcha_administration_mode'] = array(
109 '#type' => 'checkbox',
110 '#title' => t('Add CAPTCHA administration links to forms'),
111 '#default_value' => variable_get('captcha_administration_mode', FALSE),
112 '#description' => t('This option makes it easy to manage CAPTCHA settings on forms. When enabled, users with the "%admincaptcha" permission will see a fieldset with CAPTCHA administration links on all forms, except on administrative pages.', array('%admincaptcha' => t('administer CAPTCHA settings'))),
113 );
114 // Field for the CAPTCHAs on admin pages.
115 $form['captcha_form_protection']['captcha_allow_on_admin_pages'] = array(
116 '#type' => 'checkbox',
117 '#title' => t('Allow CAPTCHAs and CAPTCHA administration links on administrative pages'),
118 '#default_value' => variable_get('captcha_allow_on_admin_pages', FALSE),
119 '#description' => t('This option makes it possible to add CAPTCHAs to forms on administrative pages. CAPTCHAs are disabled by default on administrative pages (which shouldn\'t be accessible to untrusted users normally) to avoid the related overhead. In some situations, e.g. in the case of demo sites, it can be usefull to allow CAPTCHAs on administrative pages.'),
120 );
121
122 // Configuration option for adding a CAPTCHA description.
123 $form['captcha_add_captcha_description'] = array(
124 '#type' => 'checkbox',
125 '#title' => t('Add a description to the CAPTCHA'),
126 '#description' => t('Add a configurable description to explain the purpose of the CAPTCHA to the visitor.'),
127 '#default_value' => variable_get('captcha_add_captcha_description', TRUE),
128 );
129 // Textfield(s) for the CAPTCHA description.
130 if (module_exists('locale')) {
131 $langs = locale_language_list();
132 $form['captcha_descriptions'] = array(
133 '#type' => 'fieldset',
134 '#title' => t('CAPTCHA description'),
135 '#description' => t('Configurable description of the CAPTCHA. An empty entry will reset the description.'),
136 '#attributes' => array('id' => 'edit-captcha-description-wrapper'),
137 );
138 foreach ($langs as $lang_code => $lang_name) {
139 $form['captcha_descriptions']["captcha_description_$lang_code"] = array(
140 '#type' => 'textfield',
141 '#title' => t('For language %lang_name (code %lang_code)', array('%lang_name' => $lang_name, '%lang_code' => $lang_code)),
142 '#default_value' => _captcha_get_description($lang_code),
143 '#maxlength' => 256,
144 );
145 }
146 }
147 else {
148 $form['captcha_description'] = array(
149 '#type' => 'textfield',
150 '#title' => t('Challenge description'),
151 '#description' => t('Configurable description of the CAPTCHA. An empty entry will reset the description.'),
152 '#default_value' => _captcha_get_description(),
153 '#maxlength' => 256,
154 '#attributes' => array('id' => 'edit-captcha-description-wrapper'),
155 );
156 }
157
158 // Option for case sensitive/insensitive validation of the responses.
159 $form['captcha_default_validation'] = array(
160 '#type' => 'radios',
161 '#title' => t('Default CAPTCHA validation'),
162 '#description' => t('Define how the response should be processed by default. Note that the modules that provide the actual challenges can override or ignore this.'),
163 '#options' => array(
164 CAPTCHA_DEFAULT_VALIDATION_CASE_SENSITIVE => t('Case sensitive validation: the response has to exactly match the solution.'),
165 CAPTCHA_DEFAULT_VALIDATION_CASE_INSENSITIVE => t('Case insensitive validation: lowercase/uppercase errors are ignored.'),
166 ),
167 '#default_value' => variable_get('captcha_default_validation', CAPTCHA_DEFAULT_VALIDATION_CASE_INSENSITIVE),
168 );
169
170 // Field for CAPTCHA persistence.
171 $form['captcha_persistence'] = array(
172 '#type' => 'radios',
173 '#title' => t('Persistence'),
174 '#default_value' => variable_get('captcha_persistence', CAPTCHA_PERSISTENCE_SHOW_ALWAYS),
175 '#options' => array(
176 CAPTCHA_PERSISTENCE_SHOW_ALWAYS => t('Always add a challenge.'),
177 CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM => t('Omit challenges for a form once the user has successfully responded to a challenge for that form.'),
178 CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL => t('Omit challenges for all forms once the user has successfully responded to a challenge.'),
179 ),
180 '#description' => t('Define if challenges should be omitted during the rest of a session once the user successfully responses to a challenge.'),
181 );
182
183 // Option for logging wrong responses.
184 $form['captcha_log_wrong_responses'] = array(
185 '#type' => 'checkbox',
186 '#title' => t('Log wrong responses'),
187 '#description' => t('Report information about wrong responses to the !log.', array('!log' => l(t('log'), 'admin/reports/dblog'))),
188 '#default_value' => variable_get('captcha_log_wrong_responses', FALSE),
189 );
190
191 // Submit button.
192 $form['submit'] = array(
193 '#type' => 'submit',
194 '#value' => t('Save configuration'),
195 );
196
197 return $form;
198 }
199
200 /**
201 * Custom theme function for a table of (form_id -> CAPTCHA type) settings
202 */
203 function theme_captcha_admin_settings_captcha_points($form) {
204 $header = array('form_id', t('Challenge type'), t('Operations'));
205 $rows = array();
206 // Existing CAPTCHA points.
207 foreach (element_children($form['captcha_captcha_points']) as $key) {
208 $row = array();
209 $row[] = drupal_render($form['captcha_captcha_points'][$key]['form_id']);
210 $row[] = drupal_render($form['captcha_captcha_points'][$key]['captcha_type']);
211 $row[] = drupal_render($form['captcha_captcha_points'][$key]['operations']);
212 $rows[] = $row;
213 }
214 // For new CAPTCHA point.
215 $row = array();
216 $row[] = drupal_render($form['captcha_new_captcha_point']['form_id']);
217 $row[] = drupal_render($form['captcha_new_captcha_point']['captcha_type']);
218 $row[] = '';
219 $rows[] = $row;
220
221 $output = theme('table', $header, $rows);
222 return $output;
223 }
224
225 /**
226 * Validation handler for captcha_admin_settings form.
227 */
228 function captcha_admin_settings_validate($form, $form_state) {
229 $form_id = $form_state['values']['captcha_form_id_overview']['captcha_new_captcha_point']['form_id'];
230 if (!preg_match('/^[a-z0-9_]*$/', $form_id)) {
231 form_set_error('captcha_form_id_overview][captcha_new_captcha_point][form_id', t('Illegal form_id'));
232 }
233 }
234
235 /**
236 * Submission function for captcha_admin_settings form.
237 */
238 function captcha_admin_settings_submit($form, &$form_state) {
239
240 variable_set('captcha_administration_mode', $form_state['values']['captcha_administration_mode']);
241 variable_set('captcha_allow_on_admin_pages', $form_state['values']['captcha_allow_on_admin_pages']);
242
243 variable_set('captcha_default_challenge', $form_state['values']['captcha_default_challenge']);
244
245 // Process CAPTCHA points
246 if (isset($form_state['values']['captcha_form_id_overview']['captcha_captcha_points'])) {
247 foreach ($form_state['values']['captcha_form_id_overview']['captcha_captcha_points'] as $captcha_point_form_id => $data) {
248 captcha_set_form_id_setting($captcha_point_form_id, $data['captcha_type']);
249 }
250 }
251
252 // Add new CAPTCHA point?
253 $captcha_point_form_id = $form_state['values']['captcha_form_id_overview']['captcha_new_captcha_point']['form_id'];
254 if (!empty($captcha_point_form_id)) {
255 $captcha_type = $form_state['values']['captcha_form_id_overview']['captcha_new_captcha_point']['captcha_type'];
256 captcha_set_form_id_setting($captcha_point_form_id, $captcha_type);
257 drupal_set_message(t('Added CAPTCHA point.'), 'status');
258 }
259
260 // CAPTCHA description stuff.
261 variable_set('captcha_add_captcha_description', $form_state['values']['captcha_add_captcha_description']);
262 // Save (or reset) the CAPTCHA descriptions.
263 if (module_exists('locale')) {
264 $langs = locale_language_list();
265 foreach ($langs as $lang_code => $lang_name) {
266 $description = $form_state['values']["captcha_description_$lang_code"];
267 if ($description) {
268 variable_set("captcha_description_$lang_code", $description);
269 }
270 else {
271 variable_del("captcha_description_$lang_code");
272 drupal_set_message(t('Reset of CAPTCHA description for language %language.', array('%language' => $lang_name)), 'status');
273 }
274 }
275 }
276 else {
277 $description = $form_state['values']['captcha_description'];
278 if ($description) {
279 variable_set('captcha_description', $description);
280 }
281 else {
282 variable_del('captcha_description');
283 drupal_set_message(t('Reset of CAPTCHA description.'), 'status');
284 }
285 }
286
287 variable_set('captcha_default_validation', $form_state['values']['captcha_default_validation']);
288 variable_set('captcha_persistence', $form_state['values']['captcha_persistence']);
289 variable_set('captcha_log_wrong_responses', $form_state['values']['captcha_log_wrong_responses']);
290
291 drupal_set_message(t('The CAPTCHA settings were saved.'), 'status');
292 }
293
294
295 /**
296 * Central handler for CAPTCHA point administration (adding, disabling, deleting)
297 */
298 function captcha_point_admin($captcha_point_form_id=NULL, $op=NULL) {
299 module_load_include('inc', 'captcha');
300
301 // if $captcha_point_form_id and action $op given: do the action
302 if ($captcha_point_form_id) {
303 switch ($op) {
304 case 'disable':
305 return drupal_get_form('captcha_point_disable_confirm', $captcha_point_form_id, FALSE);
306 case 'delete':
307 return drupal_get_form('captcha_point_disable_confirm', $captcha_point_form_id, TRUE);
308 }
309 // return edit form for CAPTCHA point
310 return drupal_get_form('captcha_point_admin_form', $captcha_point_form_id);
311 }
312 // return add form for CAPTCHA point
313 return drupal_get_form('captcha_point_admin_form');
314 }
315
316 function captcha_point_admin_form($form_state, $captcha_point_form_id=NULL) {
317 $form = array();
318 $default_captcha_type = 'none';
319 if (isset($captcha_point_form_id)) {
320 // use given CAPTCHA point form_id
321 $form['captcha_point_form_id'] = array(
322 '#type' => 'textfield',
323 '#title' => t('Form ID'),
324 '#description' => t('The Drupal form_id of the form to add the CAPTCHA to.'),
325 '#value' => check_plain($captcha_point_form_id),
326 '#disabled' => TRUE,
327 );
328 $captcha_point = captcha_get_form_id_setting($captcha_point_form_id);
329 if ($captcha_point) {
330 $default_captcha_type = "{$captcha_point->module}/{$captcha_point->type}";
331 }
332 }
333 else {
334 // textfield for CAPTCHA point form_id
335 $form['captcha_point_form_id'] = array(
336 '#type' => 'textfield',
337 '#title' => t('Form ID'),
338 '#description' => t('The Drupal form_id of the form to add the CAPTCHA to.'),
339 );
340 }
341 // select widget for CAPTCHA type
342 $form['captcha_type'] = array(
343 '#type' => 'select',
344 '#title' => t('Challenge type'),
345 '#description' => t('The CAPTCHA type to use for this form'),
346 '#default_value' => $default_captcha_type,
347 '#options' => _captcha_available_challenge_types(),
348 );
349 // redirect to general CAPTCHA settings page after submission
350 $form['#redirect'] = 'admin/user/captcha';
351 // submit button
352 $form['submit'] = array(
353 '#type' => 'submit',
354 '#value' => t('Save'),
355 );
356 return $form;
357 }
358
359
360 /**
361 * validation function for captcha_point_admin_form
362 */
363 function captcha_point_admin_form_validate($form, $form_state) {
364 if (!preg_match('/^[a-z0-9_]+$/', $form_state['values']['captcha_point_form_id'])) {
365 form_set_error('captcha_point_form_id', t('Illegal form_id'));
366 }
367 }
368
369
370 /**
371 * Submit function for captcha_point_admin_form.
372 */
373 function captcha_point_admin_form_submit($form, $form_state) {
374 $captcha_point_form_id = $form_state['values']['captcha_point_form_id'];
375 $captcha_type = $form_state['values']['captcha_type'];
376 captcha_set_form_id_setting($captcha_point_form_id, $captcha_type);
377 drupal_set_message(t('Saved CAPTCHA point settings.'), 'status');
378 }
379
380 /**
381 * Confirm dialog for disabling/deleting a CAPTCHA point
382 */
383 function captcha_point_disable_confirm(&$form_state, $captcha_point_form_id, $delete) {
384 $form = array();
385 $form['captcha_point_form_id'] = array(
386 '#type' => 'value',
387 '#value' => $captcha_point_form_id,
388 );
389 $form['captcha_point_delete'] = array(
390 '#type' => 'value',
391 '#value' => $delete,
392 );
393 if ($delete) {
394 $message = t('Are you sure you want to delete the CAPTCHA for form_id %form_id?', array('%form_id' => $captcha_point_form_id));
395 $yes = t('Delete');
396 }
397 else {
398 $message = t('Are you sure you want to disable the CAPTCHA for form_id %form_id?', array('%form_id' => $captcha_point_form_id));
399 $yes = t('Disable');
400 }
401 return confirm_form($form, $message, 'admin/user/captcha/captcha', '', $yes);
402 }
403
404 /**
405 * Submission handler of CAPTCHA point disabling/deleting confirm_form.
406 */
407 function captcha_point_disable_confirm_submit($form, &$form_state) {
408 $captcha_point_form_id = $form_state['values']['captcha_point_form_id'];
409 $delete = $form_state['values']['captcha_point_delete'];
410 if ($delete) {
411 captcha_set_form_id_setting($captcha_point_form_id, NULL);
412 drupal_set_message(t('Deleted CAPTCHA for form %form_id.', array('%form_id' => $captcha_point_form_id)));
413 }
414 else {
415 captcha_set_form_id_setting($captcha_point_form_id, 'none');
416 drupal_set_message(t('Disabled CAPTCHA for form %form_id.', array('%form_id' => $captcha_point_form_id)));
417 }
418 $form_state['redirect'] = 'admin/user/captcha/captcha';
419 }
420
421 /**
422 * Helper function for generating an example challenge.
423 */
424 function _captcha_generate_example_challenge($module, $type) {
425 return array(
426 '#type' => 'captcha',
427 '#captcha_type' => $module .'/'. $type,
428 '#captcha_admin_mode' => TRUE,
429 );
430 }
431
432 /**
433 * Funtion for generating a page with CAPTCHA examples.
434 *
435 * If the arguments $module and $challenge are not set, generate a list with
436 * examples of the available CAPTCHA types.
437 * If $module and $challenge are set, generate 10 examples of the concerning
438 * CAPTCHA.
439 */
440 function captcha_examples($form_state, $module, $challenge) {
441 module_load_include('inc', 'captcha');
442
443 $form = array();
444 if ($module && $challenge) {
445 // Generate 10 example challenges.
446 for ($i=0; $i<10; $i++) {
447 $form["challenge_{$i}"] = _captcha_generate_example_challenge($module, $challenge);
448 }
449 }
450 else {
451 // generate a list with examples of the available CAPTCHA types
452 $form['info'] = array(
453 '#value' => t('This page gives an overview of all available challenge types, generated with their current settings.'),
454 );
455 foreach (module_implements('captcha') as $mkey => $module) {
456 $challenges = call_user_func_array($module .'_captcha', array('list'));
457 if ($challenges) {
458 foreach ($challenges as $ckey => $challenge) {
459 $form["captcha_{$mkey}_{$ckey}"] = array(
460 '#type' => 'fieldset',
461 '#title' => t('Challenge "%challenge" by module "%module"', array('%challenge' => $challenge, '%module' => $module)),
462 'challenge' => _captcha_generate_example_challenge($module, $challenge),
463 'more_examples' => array(
464 '#value' => l(t('10 more examples of this challenge.'), "admin/user/captcha/captcha/examples/$module/$challenge"),
465 ),
466 );
467 }
468 }
469 }
470 }
471 return $form;
472 }

  ViewVC Help
Powered by ViewVC 1.1.2