1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
99781f3b
|
Assos Assos
|
* @file
|
5 |
|
|
* All code for the Foo Bar module, except installation-related code.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* Implements hook_help().
|
10 |
85ad3d82
|
Assos Assos
|
*/
|
11 |
|
|
function hidden_captcha_help($path, $arg) {
|
12 |
|
|
switch ($path) {
|
13 |
|
|
case 'admin/config/people/captcha/hidden_captcha':
|
14 |
99781f3b
|
Assos Assos
|
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 |
85ad3d82
|
Assos Assos
|
}
|
16 |
|
|
}
|
17 |
|
|
|
18 |
|
|
/**
|
19 |
99781f3b
|
Assos Assos
|
* Implements hook_menu().
|
20 |
85ad3d82
|
Assos Assos
|
*/
|
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 |
99781f3b
|
Assos Assos
|
"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 |
85ad3d82
|
Assos Assos
|
"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 |
99781f3b
|
Assos Assos
|
* Implements hook_captcha().
|
55 |
85ad3d82
|
Assos Assos
|
*/
|
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 |
99781f3b
|
Assos Assos
|
'#required' => FALSE,
|
67 |
|
|
'#attributes' => array('tabindex' => '-1'),
|
68 |
85ad3d82
|
Assos Assos
|
);
|
69 |
|
|
return $captcha;
|
70 |
|
|
}
|
71 |
|
|
break;
|
72 |
|
|
}
|
73 |
|
|
}
|
74 |
|
|
|
75 |
|
|
/**
|
76 |
99781f3b
|
Assos Assos
|
* Implements hook_theme().
|
77 |
85ad3d82
|
Assos Assos
|
*/
|
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 |
99781f3b
|
Assos Assos
|
* Implements theme_hook().
|
89 |
85ad3d82
|
Assos Assos
|
*/
|
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 |
99781f3b
|
Assos Assos
|
drupal_add_css(".$class{width:0;height:0;overflow:hidden;}", "inline"); // TODO: move the random class to an external file
|
99 |
85ad3d82
|
Assos Assos
|
//html for the captcha
|
100 |
|
|
$captcha = "<div class=\"$class\">" . $captcha . "</div>";
|
101 |
|
|
}
|
102 |
|
|
return $captcha;
|
103 |
|
|
} |