Projet

Général

Profil

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

root / drupal7 / sites / all / modules / hidden_captcha / hidden_captcha.module @ 99781f3b

1
<?php
2

    
3
/**
4
 * @file
5
 * All code for the Foo Bar module, except installation-related code.
6
 */
7

    
8
/**
9
 * Implements hook_help().
10
 */
11
function hidden_captcha_help($path, $arg) {
12
  switch ($path) {
13
    case 'admin/config/people/captcha/hidden_captcha':
14
      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>';
15
  }
16
}
17

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

    
34
/**
35
 * Function for the hidden captcha settings form.
36
 */
37
function hidden_captcha_settings_form() {
38
  $form = array();
39
  $form['hidden_captcha_label'] = array(
40
    '#type' => 'textfield',
41
    '#title' => t('Hidden text field label'),
42
    '#description' => t(
43
      "This is the hidden captcha form field's label, describing the expected input.<br />" .
44
      "<strong>It is highly recommended to change it!</strong><br />" .
45
      "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 />" .
46
      "The label will only be visible to people who do not have CSS enabled and to robots."
47
    ),
48
    '#default_value' => variable_get('hidden_captcha_label', 'Website URL'),
49
  );
50
  return system_settings_form($form);
51
}
52

    
53
/**
54
 * Implements hook_captcha().
55
 */
56
function hidden_captcha_captcha($op, $captcha_type = '') {
57
  switch ($op) {
58
    case 'list': return array('Hidden CAPTCHA');
59
    case 'generate':
60
      if ($captcha_type == 'Hidden CAPTCHA') {
61
        $captcha = array();
62
        $captcha['solution'] = '';
63
        $captcha['form']['captcha_response'] = array(
64
          '#type' => 'textfield',
65
          '#title' => variable_get('hidden_captcha_label', 'Website URL'),
66
          '#required' => FALSE,
67
          '#attributes' => array('tabindex' => '-1'),
68
        );
69
        return $captcha;
70
      }
71
      break;
72
  }
73
}
74

    
75
/**
76
 * Implements hook_theme().
77
 */
78
function hidden_captcha_theme() {
79
  return array(
80
    'captcha' => array(
81
      'arguments' => array('element' => NULL),
82
      'function' => 'theme_hidden_captcha_captcha'
83
    )
84
  );
85
}
86

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