Projet

Général

Profil

Paste
Télécharger (3 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / hidden_captcha / hidden_captcha.module @ 87dbc3bf

1
<?php
2

    
3
/**
4
 * Implementation of hook_help().
5
 */
6
function hidden_captcha_help($path, $arg) {
7
  switch ($path) {
8
    case 'admin/config/people/captcha/hidden_captcha':
9
      return '<p>'. t('This CAPTCHA presents a text field that we expect no one to fill. The text field can be given any name and will be hidden from view using CSS.') .'</p>';
10
  }
11
}
12

    
13
/**
14
 * Implementation of hook_menu().
15
 */
16
function hidden_captcha_menu() {
17
  $items = array();
18
  $items['admin/config/people/captcha/hidden_captcha'] = array(
19
    'title' => 'Hidden CAPTCHA',
20
    'page callback' => 'drupal_get_form',
21
    'page arguments' => array('hidden_captcha_settings_form'),
22
    'access arguments' => array('administer CAPTCHA settings'),
23
    'type' => MENU_LOCAL_TASK,
24
    'weight' => -10
25
  );
26
  return $items;
27
}
28

    
29
/**
30
 * Function for the hidden captcha settings form.
31
 */
32
function hidden_captcha_settings_form() {
33
  $form = array();
34
  $form['hidden_captcha_label'] = array(
35
    '#type' => 'textfield',
36
    '#title' => t('Hidden text field label'),
37
    '#description' => t(
38
      "This is the hidden captcha form field's label, describing the expected input.<br />".
39
      "<strong>It is highly recommended to change it!</strong><br />".
40
      "The label should be as \"machine-readable\" as possible, encouraging spambots to fill in the field. An example might be a simple math challenge.<br />".
41
      "The label will only be visible to people who do not have CSS enabled and to robots."
42
    ),
43
    '#default_value' => variable_get('hidden_captcha_label', 'Website URL'),
44
  );
45
  return system_settings_form($form);
46
}
47

    
48
/**
49
 * Implementation of hook_captcha().
50
 */
51
function hidden_captcha_captcha($op, $captcha_type = '') {
52
  switch ($op) {
53
    case 'list': return array('Hidden CAPTCHA');
54
    case 'generate':
55
      if ($captcha_type == 'Hidden CAPTCHA') {
56
        $captcha = array();
57
        $captcha['solution'] = '';
58
        $captcha['form']['captcha_response'] = array(
59
          '#type' => 'textfield',
60
          '#title' => variable_get('hidden_captcha_label', 'Website URL'),
61
          '#required' => FALSE
62
        );
63
        return $captcha;
64
      }
65
      break;
66
  }
67
}
68

    
69
/**
70
 * Implementation of hook_theme().
71
 */
72
function hidden_captcha_theme() {
73
  return array(
74
    'captcha' => array(
75
      'arguments' => array('element' => NULL),
76
      'function' => 'theme_hidden_captcha_captcha'
77
    )
78
  );
79
}
80

    
81
/**
82
 * Implementation of theme_hook().
83
 */
84
function theme_hidden_captcha_captcha($element) {
85
  $captcha = theme_captcha($element);
86
  if (strncmp($element["element"]["#captcha_type"], "hidden_captcha/", 15) == 0) {
87
    //generate a random class name
88
    $chars = "abcdfghjkmnpqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
89
    $class = "";
90
    for ($i = 0; $i < 64; ++$i) $class .= substr($chars, rand(0, strlen($chars)-1), 1);
91
    //hide the random class via css
92
    drupal_add_css(".$class{width:0;height:0;overflow:hidden;}","inline"); // TODO: move the random class to an external file
93
    //html for the captcha
94
    $captcha = "<div class=\"$class\">" . $captcha . "</div>";
95
  }
96
  return $captcha;
97
}