Mercurial > hg > rr-repo
comparison sites/all/modules/captcha/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 * Functionality and helper functions for CAPTCHA administration. | |
6 */ | |
7 | |
8 /** | |
9 * Return an array with the available CAPTCHA types, for use as options array | |
10 * for a select form elements. | |
11 * | |
12 * @param $add_special_options if true: also add a 'none' and 'default' option | |
13 * | |
14 * @return an associative array mapping "$module/$type" to | |
15 * "$type (from module $module)" with $module the module name implementing the CAPTCHA | |
16 * and $type the name of the CAPTCHA type. | |
17 */ | |
18 function _captcha_available_challenge_types($add_special_options=TRUE) { | |
19 $captcha_types = array(); | |
20 if ($add_special_options) { | |
21 $captcha_types['none'] = t('- No challenge -'); | |
22 $captcha_types['default'] = t('Default challenge type'); | |
23 } | |
24 // We do our own version of Drupal's module_invoke_all() here because | |
25 // we want to build an array with custom keys and values. | |
26 foreach (module_implements('captcha') as $module) { | |
27 $result = call_user_func_array($module . '_captcha', array('list')); | |
28 if (is_array($result)) { | |
29 foreach ($result as $type) { | |
30 $captcha_types["$module/$type"] = t('@type (from module @module)', array('@type' => $type, '@module' => $module)); | |
31 } | |
32 } | |
33 } | |
34 return $captcha_types; | |
35 } | |
36 | |
37 /** | |
38 * Form builder function for the general CAPTCHA configuration | |
39 */ | |
40 function captcha_admin_settings() { | |
41 module_load_include('inc', 'captcha'); | |
42 | |
43 // Use javascript for some added usability on admin form. | |
44 drupal_add_js(drupal_get_path('module', 'captcha') . '/captcha.js'); | |
45 | |
46 // Configuration of which forms to protect, with what challenge. | |
47 $form['captcha_form_protection'] = array( | |
48 '#type' => 'fieldset', | |
49 '#title' => t('Form protection'), | |
50 '#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 the textfield at the bottom of the table or with the help of the option <em>Add CAPTCHA administration links to forms</em> below."), | |
51 ); | |
52 $form['captcha_form_protection']['captcha_default_challenge'] = array( | |
53 '#type' => 'select', | |
54 '#title' => t('Default challenge type'), | |
55 '#description' => t('Select the default challenge type for CAPTCHAs. This can be overriden for each form if desired.'), | |
56 '#options' => _captcha_available_challenge_types(FALSE), | |
57 '#default_value' => variable_get('captcha_default_challenge', 'captcha/Math'), | |
58 ); | |
59 // List known form_ids. | |
60 $form['captcha_form_protection']['captcha_form_id_overview'] = array( | |
61 '#theme' => 'captcha_admin_settings_captcha_points', | |
62 '#tree' => TRUE, | |
63 ); | |
64 $form['captcha_form_protection']['captcha_form_id_overview']['captcha_captcha_points'] = array(); | |
65 $captcha_type_options = _captcha_available_challenge_types(); | |
66 $result = db_select('captcha_points', 'cp')->fields('cp')->orderBy('form_id')->execute(); | |
67 foreach ($result as $captcha_point) { | |
68 $form['captcha_form_protection']['captcha_form_id_overview']['captcha_captcha_points'][$captcha_point->form_id] = array(); | |
69 $form['captcha_form_protection']['captcha_form_id_overview']['captcha_captcha_points'][$captcha_point->form_id]['form_id'] = array( | |
70 '#markup' => $captcha_point->form_id, | |
71 ); | |
72 // Select widget for CAPTCHA type. | |
73 if (isset($captcha_point->module) && $captcha_point->module) { | |
74 $captcha_type = $captcha_point->module . '/' . $captcha_point->captcha_type; | |
75 } | |
76 elseif (isset($captcha_point->captcha_type) && ($captcha_point->captcha_type == 'default')) { | |
77 $captcha_type = 'default'; | |
78 } | |
79 else { | |
80 $captcha_type = 'none'; | |
81 } | |
82 $form['captcha_form_protection']['captcha_form_id_overview']['captcha_captcha_points'][$captcha_point->form_id]['captcha_type'] = array( | |
83 '#type' => 'select', | |
84 '#default_value' => $captcha_type, | |
85 '#options' => $captcha_type_options, | |
86 ); | |
87 // Additional operations. | |
88 $form['captcha_form_protection']['captcha_form_id_overview']['captcha_captcha_points'][$captcha_point->form_id]['operations'] = array( | |
89 '#markup' => implode(", ", array( | |
90 l(t('delete'), "admin/config/people/captcha/captcha/captcha_point/{$captcha_point->form_id}/delete"), | |
91 )) | |
92 ); | |
93 } | |
94 | |
95 // Form items for new form_id. | |
96 $form['captcha_form_protection']['captcha_form_id_overview']['captcha_new_captcha_point'] = array(); | |
97 // Textfield for form_id. | |
98 $form['captcha_form_protection']['captcha_form_id_overview']['captcha_new_captcha_point']['form_id'] = array( | |
99 '#type' => 'textfield', | |
100 '#size' => 16, | |
101 ); | |
102 // Select widget for CAPTCHA type. | |
103 $form['captcha_form_protection']['captcha_form_id_overview']['captcha_new_captcha_point']['captcha_type'] = array( | |
104 '#type' => 'select', | |
105 '#default_value' => 'none', | |
106 '#options' => $captcha_type_options, | |
107 ); | |
108 | |
109 // Field for the CAPTCHA administration mode. | |
110 $form['captcha_form_protection']['captcha_administration_mode'] = array( | |
111 '#type' => 'checkbox', | |
112 '#title' => t('Add CAPTCHA administration links to forms'), | |
113 '#default_value' => variable_get('captcha_administration_mode', FALSE), | |
114 '#description' => t('This option makes it easy to manage CAPTCHA settings on forms. When enabled, users with the <em>administer CAPTCHA settings</em> permission will see a fieldset with CAPTCHA administration links on all forms, except on administrative pages.'), | |
115 ); | |
116 // Field for the CAPTCHAs on admin pages. | |
117 $form['captcha_form_protection']['captcha_allow_on_admin_pages'] = array( | |
118 '#type' => 'checkbox', | |
119 '#title' => t('Allow CAPTCHAs and CAPTCHA administration links on administrative pages'), | |
120 '#default_value' => variable_get('captcha_allow_on_admin_pages', FALSE), | |
121 '#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."), | |
122 ); | |
123 | |
124 // Button for clearing the CAPTCHA placement cache. | |
125 // Based on Drupal core's "Clear all caches" (performance settings page). | |
126 $form['captcha_form_protection']['captcha_placement_caching'] = array( | |
127 '#type' => 'item', | |
128 '#title' => t('CAPTCHA placement caching'), | |
129 '#description' => t('For efficiency, the positions of the CAPTCHA elements in each of the configured forms are cached. Most of the time, the structure of a form does not change and it would be a waste to recalculate the positions every time. Occasionally however, the form structure can change (e.g. during site building) and clearing the CAPTCHA placement cache can be required to fix the CAPTCHA placement.'), | |
130 ); | |
131 $form['captcha_form_protection']['captcha_placement_caching']['captcha_placement_cache_clear'] = array( | |
132 '#type' => 'submit', | |
133 '#value' => t('Clear the CAPTCHA placement cache'), | |
134 '#submit' => array('captcha_clear_captcha_placement_cache_submit'), | |
135 ); | |
136 | |
137 // Configuration option for adding a CAPTCHA description. | |
138 $form['captcha_add_captcha_description'] = array( | |
139 '#type' => 'checkbox', | |
140 '#title' => t('Add a description to the CAPTCHA'), | |
141 '#description' => t('Add a configurable description to explain the purpose of the CAPTCHA to the visitor.'), | |
142 '#default_value' => variable_get('captcha_add_captcha_description', TRUE), | |
143 ); | |
144 // Textfield(s) for the CAPTCHA description. | |
145 if (module_exists('locale')) { | |
146 $langs = locale_language_list(); | |
147 $form['captcha_descriptions'] = array( | |
148 '#type' => 'fieldset', | |
149 '#title' => t('CAPTCHA description'), | |
150 '#description' => t('Configurable description of the CAPTCHA. An empty entry will reset the description to default.'), | |
151 '#attributes' => array('id' => 'edit-captcha-description-wrapper'), | |
152 ); | |
153 foreach ($langs as $lang_code => $lang_name) { | |
154 $form['captcha_descriptions']["captcha_description_$lang_code"] = array( | |
155 '#type' => 'textfield', | |
156 '#title' => t('For language %lang_name (code %lang_code)', array('%lang_name' => $lang_name, '%lang_code' => $lang_code)), | |
157 '#default_value' => _captcha_get_description($lang_code), | |
158 '#maxlength' => 256, | |
159 ); | |
160 } | |
161 } | |
162 else { | |
163 $form['captcha_description'] = array( | |
164 '#type' => 'textfield', | |
165 '#title' => t('Challenge description'), | |
166 '#description' => t('Configurable description of the CAPTCHA. An empty entry will reset the description to default.'), | |
167 '#default_value' => _captcha_get_description(), | |
168 '#maxlength' => 256, | |
169 '#attributes' => array('id' => 'edit-captcha-description-wrapper'), | |
170 ); | |
171 } | |
172 | |
173 // Option for case sensitive/insensitive validation of the responses. | |
174 $form['captcha_default_validation'] = array( | |
175 '#type' => 'radios', | |
176 '#title' => t('Default CAPTCHA validation'), | |
177 '#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.'), | |
178 '#options' => array( | |
179 CAPTCHA_DEFAULT_VALIDATION_CASE_SENSITIVE => t('Case sensitive validation: the response has to exactly match the solution.'), | |
180 CAPTCHA_DEFAULT_VALIDATION_CASE_INSENSITIVE => t('Case insensitive validation: lowercase/uppercase errors are ignored.'), | |
181 ), | |
182 '#default_value' => variable_get('captcha_default_validation', CAPTCHA_DEFAULT_VALIDATION_CASE_INSENSITIVE), | |
183 ); | |
184 | |
185 // Field for CAPTCHA persistence. | |
186 // TODO for D7: Rethink/simplify the explanation and UI strings. | |
187 $form['captcha_persistence'] = array( | |
188 '#type' => 'radios', | |
189 '#title' => t('Persistence'), | |
190 '#default_value' => variable_get('captcha_persistence', CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM_INSTANCE), | |
191 '#options' => array( | |
192 CAPTCHA_PERSISTENCE_SHOW_ALWAYS => | |
193 t('Always add a challenge.'), | |
194 CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM_INSTANCE => | |
195 t('Omit challenges in a multi-step/preview workflow once the user successfully responds to a challenge.'), | |
196 CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM_TYPE => | |
197 t('Omit challenges on a form type once the user successfully responds to a challenge on a form of that type.'), | |
198 CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL => | |
199 t('Omit challenges on all forms once the user successfully responds to any challenge on the site.'), | |
200 ), | |
201 '#description' => t('Define if challenges should be omitted during the rest of a session once the user successfully responds to a challenge.'), | |
202 ); | |
203 | |
204 // Enable wrong response counter. | |
205 $form['captcha_enable_stats'] = array( | |
206 '#type' => 'checkbox', | |
207 '#title' => t('Enable statistics'), | |
208 '#description' => t('Keep CAPTCHA related counters in the <a href="!statusreport">status report</a>. Note that this comes with a performance penalty as updating the counters results in clearing the variable cache.', array('!statusreport' => url('admin/reports/status'))), | |
209 '#default_value' => variable_get('captcha_enable_stats', FALSE), | |
210 ); | |
211 | |
212 // Option for logging wrong responses. | |
213 $form['captcha_log_wrong_responses'] = array( | |
214 '#type' => 'checkbox', | |
215 '#title' => t('Log wrong responses'), | |
216 '#description' => t('Report information about wrong responses to the <a href="!dblog">log</a>.', array('!dblog' => url('admin/reports/dblog'))), | |
217 '#default_value' => variable_get('captcha_log_wrong_responses', FALSE), | |
218 ); | |
219 | |
220 // Submit button. | |
221 $form['actions'] = array('#type' => 'actions'); | |
222 $form['actions']['submit'] = array( | |
223 '#type' => 'submit', | |
224 '#value' => t('Save configuration'), | |
225 ); | |
226 | |
227 return $form; | |
228 } | |
229 | |
230 /** | |
231 * Custom theme function for a table of (form_id -> CAPTCHA type) settings | |
232 */ | |
233 function theme_captcha_admin_settings_captcha_points($variables) { | |
234 $form = $variables['form']; | |
235 $header = array('form_id', t('Challenge type'), t('Operations')); | |
236 $rows = array(); | |
237 // Existing CAPTCHA points. | |
238 foreach (element_children($form['captcha_captcha_points']) as $key) { | |
239 $row = array(); | |
240 $row[] = drupal_render($form['captcha_captcha_points'][$key]['form_id']); | |
241 $row[] = drupal_render($form['captcha_captcha_points'][$key]['captcha_type']); | |
242 $row[] = drupal_render($form['captcha_captcha_points'][$key]['operations']); | |
243 $rows[] = $row; | |
244 } | |
245 // For new CAPTCHA point. | |
246 $row = array(); | |
247 $row[] = drupal_render($form['captcha_new_captcha_point']['form_id']); | |
248 $row[] = drupal_render($form['captcha_new_captcha_point']['captcha_type']); | |
249 $row[] = ''; | |
250 $rows[] = $row; | |
251 | |
252 $output = theme('table', array('header' => $header, 'rows' => $rows)); | |
253 return $output; | |
254 } | |
255 | |
256 /** | |
257 * Validation handler for captcha_admin_settings form. | |
258 */ | |
259 function captcha_admin_settings_validate($form, $form_state) { | |
260 $form_id = $form_state['values']['captcha_form_id_overview']['captcha_new_captcha_point']['form_id']; | |
261 if (!preg_match('/^[a-z0-9_]*$/', $form_id)) { | |
262 form_set_error('captcha_form_id_overview][captcha_new_captcha_point][form_id', t('Illegal form_id')); | |
263 } | |
264 } | |
265 | |
266 /** | |
267 * Submission function for captcha_admin_settings form. | |
268 */ | |
269 function captcha_admin_settings_submit($form, &$form_state) { | |
270 | |
271 variable_set('captcha_administration_mode', $form_state['values']['captcha_administration_mode']); | |
272 variable_set('captcha_allow_on_admin_pages', $form_state['values']['captcha_allow_on_admin_pages']); | |
273 | |
274 variable_set('captcha_default_challenge', $form_state['values']['captcha_default_challenge']); | |
275 | |
276 // Process CAPTCHA points | |
277 if (isset($form_state['values']['captcha_form_id_overview']['captcha_captcha_points'])) { | |
278 foreach ($form_state['values']['captcha_form_id_overview']['captcha_captcha_points'] as $captcha_point_form_id => $data) { | |
279 captcha_set_form_id_setting($captcha_point_form_id, $data['captcha_type']); | |
280 } | |
281 } | |
282 | |
283 // Add new CAPTCHA point? | |
284 $captcha_point_form_id = $form_state['values']['captcha_form_id_overview']['captcha_new_captcha_point']['form_id']; | |
285 if (!empty($captcha_point_form_id)) { | |
286 $captcha_type = $form_state['values']['captcha_form_id_overview']['captcha_new_captcha_point']['captcha_type']; | |
287 captcha_set_form_id_setting($captcha_point_form_id, $captcha_type); | |
288 drupal_set_message(t('Added CAPTCHA point.'), 'status'); | |
289 } | |
290 | |
291 // CAPTCHA description stuff. | |
292 variable_set('captcha_add_captcha_description', $form_state['values']['captcha_add_captcha_description']); | |
293 // Save (or reset) the CAPTCHA descriptions. | |
294 if (module_exists('locale')) { | |
295 $langs = locale_language_list(); | |
296 foreach ($langs as $lang_code => $lang_name) { | |
297 $description = $form_state['values']["captcha_description_$lang_code"]; | |
298 if ($description) { | |
299 variable_set("captcha_description_$lang_code", $description); | |
300 } | |
301 else { | |
302 variable_del("captcha_description_$lang_code"); | |
303 drupal_set_message(t('Reset of CAPTCHA description for language %language.', array('%language' => $lang_name)), 'status'); | |
304 } | |
305 } | |
306 } | |
307 else { | |
308 $description = $form_state['values']['captcha_description']; | |
309 if ($description) { | |
310 variable_set('captcha_description', $description); | |
311 } | |
312 else { | |
313 variable_del('captcha_description'); | |
314 drupal_set_message(t('Reset of CAPTCHA description.'), 'status'); | |
315 } | |
316 } | |
317 | |
318 variable_set('captcha_default_validation', $form_state['values']['captcha_default_validation']); | |
319 variable_set('captcha_persistence', $form_state['values']['captcha_persistence']); | |
320 variable_set('captcha_enable_stats', $form_state['values']['captcha_enable_stats']); | |
321 variable_set('captcha_log_wrong_responses', $form_state['values']['captcha_log_wrong_responses']); | |
322 | |
323 drupal_set_message(t('The CAPTCHA settings have been saved.'), 'status'); | |
324 } | |
325 | |
326 | |
327 | |
328 | |
329 /** | |
330 * Submit callback; clear CAPTCHA placement cache. | |
331 */ | |
332 function captcha_clear_captcha_placement_cache_submit($form, &$form_state) { | |
333 variable_del('captcha_placement_map_cache'); | |
334 drupal_set_message(t('Cleared the CAPTCHA placement cache.')); | |
335 } | |
336 | |
337 | |
338 /** | |
339 * Central handler for CAPTCHA point administration (adding, disabling, deleting) | |
340 */ | |
341 function captcha_point_admin($captcha_point_form_id=NULL, $op=NULL) { | |
342 module_load_include('inc', 'captcha'); | |
343 | |
344 // if $captcha_point_form_id and action $op given: do the action | |
345 if ($captcha_point_form_id) { | |
346 switch ($op) { | |
347 case 'disable': | |
348 return drupal_get_form('captcha_point_disable_confirm', $captcha_point_form_id, FALSE); | |
349 case 'delete': | |
350 return drupal_get_form('captcha_point_disable_confirm', $captcha_point_form_id, TRUE); | |
351 } | |
352 // return edit form for CAPTCHA point | |
353 return drupal_get_form('captcha_point_admin_form', $captcha_point_form_id); | |
354 } | |
355 // return add form for CAPTCHA point | |
356 return drupal_get_form('captcha_point_admin_form'); | |
357 } | |
358 | |
359 function captcha_point_admin_form($form, $form_state, $captcha_point_form_id=NULL) { | |
360 $form = array(); | |
361 $default_captcha_type = 'none'; | |
362 if (isset($captcha_point_form_id)) { | |
363 // use given CAPTCHA point form_id | |
364 $form['captcha_point_form_id'] = array( | |
365 '#type' => 'textfield', | |
366 '#title' => t('Form ID'), | |
367 '#description' => t('The Drupal form_id of the form to add the CAPTCHA to.'), | |
368 '#value' => check_plain($captcha_point_form_id), | |
369 '#disabled' => TRUE, | |
370 ); | |
371 $captcha_point = captcha_get_form_id_setting($captcha_point_form_id); | |
372 if ($captcha_point) { | |
373 $default_captcha_type = "{$captcha_point->module}/{$captcha_point->captcha_type}"; | |
374 } | |
375 } | |
376 else { | |
377 // textfield for CAPTCHA point form_id | |
378 $form['captcha_point_form_id'] = array( | |
379 '#type' => 'textfield', | |
380 '#title' => t('Form ID'), | |
381 '#description' => t('The Drupal form_id of the form to add the CAPTCHA to.'), | |
382 ); | |
383 } | |
384 // select widget for CAPTCHA type | |
385 $form['captcha_type'] = array( | |
386 '#type' => 'select', | |
387 '#title' => t('Challenge type'), | |
388 '#description' => t('The CAPTCHA type to use for this form.'), | |
389 '#default_value' => $default_captcha_type, | |
390 '#options' => _captcha_available_challenge_types(), | |
391 ); | |
392 // redirect to general CAPTCHA settings page after submission | |
393 $form['#redirect'] = 'admin/config/people/captcha'; | |
394 // submit button | |
395 $form['actions'] = array('#type' => 'actions'); | |
396 $form['actions']['submit'] = array( | |
397 '#type' => 'submit', | |
398 '#value' => t('Save'), | |
399 ); | |
400 return $form; | |
401 } | |
402 | |
403 | |
404 /** | |
405 * validation function for captcha_point_admin_form | |
406 */ | |
407 function captcha_point_admin_form_validate($form, $form_state) { | |
408 if (!preg_match('/^[a-z0-9_]+$/', $form_state['values']['captcha_point_form_id'])) { | |
409 form_set_error('captcha_point_form_id', t('Illegal form_id')); | |
410 } | |
411 } | |
412 | |
413 | |
414 /** | |
415 * Submit function for captcha_point_admin_form. | |
416 */ | |
417 function captcha_point_admin_form_submit($form, $form_state) { | |
418 $captcha_point_form_id = $form_state['values']['captcha_point_form_id']; | |
419 $captcha_type = $form_state['values']['captcha_type']; | |
420 captcha_set_form_id_setting($captcha_point_form_id, $captcha_type); | |
421 drupal_set_message(t('Saved CAPTCHA point settings.'), 'status'); | |
422 } | |
423 | |
424 /** | |
425 * Confirm dialog for disabling/deleting a CAPTCHA point | |
426 */ | |
427 function captcha_point_disable_confirm($form, &$form_state, $captcha_point_form_id, $delete) { | |
428 $form = array(); | |
429 $form['captcha_point_form_id'] = array( | |
430 '#type' => 'value', | |
431 '#value' => $captcha_point_form_id, | |
432 ); | |
433 $form['captcha_point_delete'] = array( | |
434 '#type' => 'value', | |
435 '#value' => $delete, | |
436 ); | |
437 if ($delete) { | |
438 $message = t('Are you sure you want to delete the CAPTCHA for form_id %form_id?', array('%form_id' => $captcha_point_form_id)); | |
439 $yes = t('Delete'); | |
440 } | |
441 else { | |
442 $message = t('Are you sure you want to disable the CAPTCHA for form_id %form_id?', array('%form_id' => $captcha_point_form_id)); | |
443 $yes = t('Disable'); | |
444 } | |
445 return confirm_form($form, $message, 'admin/config/people/captcha/captcha', '', $yes); | |
446 } | |
447 | |
448 /** | |
449 * Submission handler of CAPTCHA point disabling/deleting confirm_form. | |
450 */ | |
451 function captcha_point_disable_confirm_submit($form, &$form_state) { | |
452 $captcha_point_form_id = $form_state['values']['captcha_point_form_id']; | |
453 $delete = $form_state['values']['captcha_point_delete']; | |
454 if ($delete) { | |
455 captcha_set_form_id_setting($captcha_point_form_id, NULL); | |
456 drupal_set_message(t('Deleted CAPTCHA for form %form_id.', array('%form_id' => $captcha_point_form_id))); | |
457 } | |
458 else { | |
459 captcha_set_form_id_setting($captcha_point_form_id, 'none'); | |
460 drupal_set_message(t('Disabled CAPTCHA for form %form_id.', array('%form_id' => $captcha_point_form_id))); | |
461 } | |
462 $form_state['redirect'] = 'admin/config/people/captcha/captcha'; | |
463 } | |
464 | |
465 /** | |
466 * Helper function for generating an example challenge. | |
467 */ | |
468 function _captcha_generate_example_challenge($module, $type) { | |
469 return array( | |
470 '#type' => 'captcha', | |
471 '#captcha_type' => $module . '/' . $type, | |
472 '#captcha_admin_mode' => TRUE, | |
473 ); | |
474 } | |
475 | |
476 /** | |
477 * Funtion for generating a page with CAPTCHA examples. | |
478 * | |
479 * If the arguments $module and $challenge are not set, generate a list with | |
480 * examples of the available CAPTCHA types. | |
481 * If $module and $challenge are set, generate 10 examples of the concerning | |
482 * CAPTCHA. | |
483 */ | |
484 function captcha_examples($form, $form_state, $module, $challenge) { | |
485 module_load_include('inc', 'captcha'); | |
486 | |
487 $form = array(); | |
488 if ($module && $challenge) { | |
489 // Generate 10 example challenges. | |
490 for ($i=0; $i<10; $i++) { | |
491 $form["challenge_{$i}"] = _captcha_generate_example_challenge($module, $challenge); | |
492 } | |
493 } | |
494 else { | |
495 // generate a list with examples of the available CAPTCHA types | |
496 $form['info'] = array( | |
497 '#markup' => t('This page gives an overview of all available challenge types, generated with their current settings.'), | |
498 ); | |
499 foreach (module_implements('captcha') as $mkey => $module) { | |
500 $challenges = call_user_func_array($module . '_captcha', array('list')); | |
501 if ($challenges) { | |
502 foreach ($challenges as $ckey => $challenge) { | |
503 $form["captcha_{$mkey}_{$ckey}"] = array( | |
504 '#type' => 'fieldset', | |
505 '#title' => t('Challenge %challenge by module %module', array('%challenge' => $challenge, '%module' => $module)), | |
506 'challenge' => _captcha_generate_example_challenge($module, $challenge), | |
507 'more_examples' => array( | |
508 '#markup' => l(t('10 more examples of this challenge.'), "admin/config/people/captcha/captcha/examples/$module/$challenge"), | |
509 ), | |
510 ); | |
511 } | |
512 } | |
513 } | |
514 } | |
515 return $form; | |
516 } |