Projet

Général

Profil

Paste
Télécharger (25,4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / captcha / captcha.admin.inc @ a8cee257

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