1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Uses the reCAPTCHA web service to improve the CAPTCHA system.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* Implements hook_help().
|
10 |
|
|
*/
|
11 |
|
|
function recaptcha_help($path, $arg) {
|
12 |
|
|
$output = '';
|
13 |
|
|
switch ($path) {
|
14 |
|
|
case 'admin/modules#name':
|
15 |
|
|
$output .= t('reCAPTCHA');
|
16 |
|
|
break;
|
17 |
|
|
case 'admin/modules#description':
|
18 |
|
|
case 'admin/user/captcha/recaptcha':
|
19 |
|
|
$output .= t('Uses the <a href="@url" target="_blank">reCAPTCHA</a> web service to improve the CAPTCHA system and protect email addresses.', array('@url' => url('https://www.google.com/recaptcha')));
|
20 |
|
|
break;
|
21 |
|
|
case 'admin/help#recaptcha':
|
22 |
|
|
$output .= '<p>' .
|
23 |
|
|
t('Uses the reCAPTCHA web service to improve the CAPTCHA module and protect email addresses. For more information on what reCAPTCHA is, visit <a href="@url" target="_blank">the official website</a>.', array('@url' => url('https://www.google.com/recaptcha'))) .
|
24 |
|
|
'</p><h3>' .
|
25 |
|
|
t('Configuration') .
|
26 |
|
|
'</h3><p>' .
|
27 |
|
|
t('The settings associated with reCAPTCHA can be found in the <a href="@recaptchatab">reCAPTCHA tab</a>, in the <a href="@captchasettings">CAPTCHA settings</a>. You must set your public and private reCAPTCHA keys in order to use the module. Once the public and private keys are set, visit the <a href="@captchasettings">CAPTCHA settings</a>, where you can choose where reCAPTCHA should be displayed.', array('@recaptchatab' => url('admin/user/captcha/recaptcha'), '@captchasettings' => url('admin/user/captcha'))) .
|
28 |
|
|
'</p>';
|
29 |
|
|
break;
|
30 |
|
|
}
|
31 |
|
|
return $output;
|
32 |
|
|
}
|
33 |
|
|
|
34 |
|
|
/**
|
35 |
|
|
* Implements hook_menu().
|
36 |
|
|
*/
|
37 |
|
|
function recaptcha_menu() {
|
38 |
|
|
$items = array();
|
39 |
|
|
$items['admin/config/people/captcha/recaptcha'] = array(
|
40 |
|
|
'title' => 'reCAPTCHA',
|
41 |
|
|
'description' => 'Administer the reCAPTCHA web service.',
|
42 |
|
|
'page callback' => 'drupal_get_form',
|
43 |
|
|
'page arguments' => array('recaptcha_admin_settings'),
|
44 |
|
|
'access arguments' => array('administer recaptcha'),
|
45 |
|
|
'type' => MENU_LOCAL_TASK,
|
46 |
|
|
'file' => 'recaptcha.admin.inc',
|
47 |
|
|
);
|
48 |
|
|
return $items;
|
49 |
|
|
}
|
50 |
|
|
|
51 |
|
|
/**
|
52 |
|
|
* Implements hook_permission().
|
53 |
|
|
*/
|
54 |
|
|
function recaptcha_permission() {
|
55 |
|
|
return array(
|
56 |
|
|
'administer recaptcha' => array(
|
57 |
|
|
'title' => t('reCaptcha Administration'),
|
58 |
|
|
'description' => t('Administer reCaptcha settings'),
|
59 |
|
|
),
|
60 |
|
|
);
|
61 |
|
|
}
|
62 |
|
|
|
63 |
|
|
/**
|
64 |
|
|
* Implements hook_captcha().
|
65 |
|
|
*/
|
66 |
|
|
function recaptcha_captcha() {
|
67 |
|
|
$args = func_get_args();
|
68 |
|
|
$op = array_shift($args);
|
69 |
|
|
switch ($op) {
|
70 |
|
|
case 'list':
|
71 |
|
|
return array('reCAPTCHA');
|
72 |
|
|
|
73 |
|
|
case 'generate':
|
74 |
|
|
$captcha_type = array_shift($args);
|
75 |
|
|
$captcha = array();
|
76 |
|
|
if ($captcha_type == 'reCAPTCHA') {
|
77 |
|
|
// Retrieve configuration variables.
|
78 |
|
|
$recaptcha_theme = variable_get('recaptcha_theme', 'red');
|
79 |
|
|
$recaptcha_tabindex = variable_get('recaptcha_tabindex', NULL);
|
80 |
|
|
$recaptcha_public_key = variable_get('recaptcha_public_key', FALSE);
|
81 |
|
|
$recaptcha_ajax_api = variable_get('recaptcha_ajax_api', FALSE);
|
82 |
|
|
|
83 |
|
|
// Test if reCAPTCHA can be used, falling back to Math if it is not
|
84 |
|
|
// configured, the library won't load, or the server is down.
|
85 |
|
|
if (!$recaptcha_public_key || !_recaptcha_load_library() || !_recaptcha_test_recaptcha_server()) {
|
86 |
|
|
return captcha_captcha('generate', 'Math', $args);
|
87 |
|
|
}
|
88 |
|
|
|
89 |
|
|
if ($recaptcha_ajax_api) {
|
90 |
|
|
// By default CAPTCHA turns off page caching on any page where a
|
91 |
|
|
// CAPTCHA challenge appears. If recaptcha is using AJAX API, set
|
92 |
|
|
// caching back to its old state as stored in DB.
|
93 |
|
|
global $conf;
|
94 |
|
|
$conf['cache'] = unserialize(db_query("SELECT value FROM {variable} WHERE name = 'cache'")->fetchField());
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
$recaptcha_options = array(
|
98 |
|
|
'theme' => $recaptcha_theme,
|
99 |
|
|
);
|
100 |
|
|
|
101 |
|
|
// Localization support.
|
102 |
|
|
global $language;
|
103 |
|
|
if (isset($language->language)) {
|
104 |
|
|
// reCAPTCHA uses two-character language codes, so 'pt-br' must be
|
105 |
|
|
// passed as 'pt'; cf. https://developers.google.com/recaptcha/docs/customization#i18n
|
106 |
|
|
$recaptcha_options['lang'] = drupal_substr($language->language, 0, 2);
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
// Construct the Javascript, but only display it once.
|
110 |
|
|
static $_recaptcha_jsadded = FALSE;
|
111 |
|
|
if ($_recaptcha_jsadded == FALSE && $recaptcha_ajax_api == FALSE) {
|
112 |
|
|
$_recaptcha_jsadded = TRUE;
|
113 |
|
|
|
114 |
|
|
// Add support to display the custom theme.
|
115 |
|
|
if ($recaptcha_theme == 'custom') {
|
116 |
|
|
$recaptcha_options['custom_theme_widget'] = 'recaptcha_custom_theme_widget';
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
// Set the default tab index.
|
120 |
|
|
if (!empty($recaptcha_tabindex)) {
|
121 |
|
|
$recaptcha_options['tabindex'] = $recaptcha_tabindex;
|
122 |
|
|
}
|
123 |
|
|
drupal_add_js('var RecaptchaOptions = ' . drupal_json_encode($recaptcha_options) . ';', array('type' => 'inline'));
|
124 |
|
|
}
|
125 |
|
|
|
126 |
|
|
// Create the form. Captcha requires TRUE to be returned in solution.
|
127 |
|
|
$captcha['solution'] = TRUE;
|
128 |
|
|
$captcha['captcha_validate'] = 'recaptcha_captcha_validation';
|
129 |
|
|
|
130 |
|
|
// If 'Disable Client-Side Cookies' is set, then add query string to
|
131 |
|
|
// end of the public key string before passing to recaptchalib.
|
132 |
|
|
if (variable_get('recaptcha_nocookies', FALSE)) {
|
133 |
|
|
$recaptcha_public_key .= '&nocookie=1';
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
$captcha['form']['captcha_response'] = array(
|
137 |
|
|
'#type' => 'hidden',
|
138 |
|
|
'#value' => 'reCAPTCHA',
|
139 |
|
|
);
|
140 |
|
|
|
141 |
|
|
// Expose the form, either straight HTML, or using the AJAX API.
|
142 |
|
|
// Build the custom theme HTML if necessary.
|
143 |
|
|
$recaptcha_custom_html = ($recaptcha_theme == 'custom') ? theme('recaptcha_custom_widget') : '';
|
144 |
|
|
if ($recaptcha_ajax_api == FALSE) {
|
145 |
|
|
// Only generate recaptcha_get_html() if we're not using the AJAX API.
|
146 |
|
|
$html = recaptcha_get_html($recaptcha_public_key, NULL, TRUE);
|
147 |
|
|
$captcha['form']['captcha_form'] = array(
|
148 |
|
|
'#type' => 'item',
|
149 |
|
|
'#markup' => (!empty($recaptcha_custom_html) ? '<div id="recaptcha_custom_theme_widget">' . $recaptcha_custom_html . '</div>' : '') . $html,
|
150 |
|
|
);
|
151 |
|
|
}
|
152 |
|
|
else {
|
153 |
|
|
$captcha['form']['captcha_form'] = array(
|
154 |
|
|
'#type' => 'item',
|
155 |
|
|
// Create the destination container, inserting any custom theme HTML
|
156 |
|
|
// necessary ($recaptcha_custom_html will be empty if we are not
|
157 |
|
|
// using custom theme).
|
158 |
|
|
'#markup' => '<div id="recaptcha_ajax_api_container">' . $recaptcha_custom_html . '</div>',
|
159 |
|
|
);
|
160 |
|
|
drupal_add_js('https://www.google.com/recaptcha/api/js/recaptcha_ajax.js', array('type' => 'external'));
|
161 |
|
|
$recaptcha_options['public_key'] = $recaptcha_public_key;
|
162 |
|
|
$recaptcha_options['container'] = 'recaptcha_ajax_api_container';
|
163 |
|
|
drupal_add_js(array('recaptcha' => $recaptcha_options), 'setting');
|
164 |
|
|
drupal_add_js(drupal_get_path('module', 'recaptcha') . '/recaptcha.js');
|
165 |
|
|
}
|
166 |
|
|
}
|
167 |
|
|
return $captcha;
|
168 |
|
|
}
|
169 |
|
|
}
|
170 |
|
|
|
171 |
|
|
/**
|
172 |
|
|
* @return boolean
|
173 |
|
|
* Whether or not the reCAPTCHA server is up.
|
174 |
|
|
*/
|
175 |
|
|
function _recaptcha_test_recaptcha_server() {
|
176 |
|
|
$test = TRUE;
|
177 |
|
|
$fs = @fsockopen(RECAPTCHA_VERIFY_SERVER, 80, $errno, $errstr, 10);
|
178 |
|
|
if (!$fs) {
|
179 |
|
|
$test = FALSE;
|
180 |
|
|
drupal_set_message(t('Unable to connect with the reCAPTCHA server (@server): @errno: @errstr', array(
|
181 |
|
|
'@server' => RECAPTCHA_VERIFY_SERVER,
|
182 |
|
|
'@errno' => $errno,
|
183 |
|
|
'@errstr' => $errstr,
|
184 |
|
|
)), 'error');
|
185 |
|
|
}
|
186 |
|
|
fclose($fs);
|
187 |
|
|
return $test;
|
188 |
|
|
}
|
189 |
|
|
|
190 |
|
|
/**
|
191 |
|
|
* CAPTCHA Callback; Validates the reCAPTCHA code.
|
192 |
|
|
*/
|
193 |
|
|
function recaptcha_captcha_validation($solution = NULL, $response = NULL) {
|
194 |
|
|
global $user;
|
195 |
|
|
$recaptcha_private_key = variable_get('recaptcha_private_key', FALSE);
|
196 |
|
|
$ip_address = ip_address();
|
197 |
|
|
if ($recaptcha_private_key && $ip_address && $response === 'reCAPTCHA' && !empty($_POST['recaptcha_challenge_field']) && !empty($_POST['recaptcha_response_field']) && _recaptcha_test_recaptcha_server()) {
|
198 |
|
|
$resp = recaptcha_check_answer(
|
199 |
|
|
$recaptcha_private_key,
|
200 |
|
|
$ip_address,
|
201 |
|
|
check_plain($_POST['recaptcha_challenge_field']),
|
202 |
|
|
check_plain($_POST['recaptcha_response_field'])
|
203 |
|
|
);
|
204 |
|
|
return $resp->is_valid;
|
205 |
|
|
}
|
206 |
|
|
return FALSE;
|
207 |
|
|
}
|
208 |
|
|
|
209 |
|
|
/**
|
210 |
|
|
* Implements hook_theme().
|
211 |
|
|
*/
|
212 |
|
|
function recaptcha_theme() {
|
213 |
|
|
return array(
|
214 |
|
|
'recaptcha_custom_widget' => array(
|
215 |
|
|
'arguments' => array(),
|
216 |
|
|
),
|
217 |
|
|
);
|
218 |
|
|
}
|
219 |
|
|
|
220 |
|
|
/**
|
221 |
|
|
* Theme function: creates the custom themed recaptcha widget.
|
222 |
|
|
*
|
223 |
|
|
* @ingroup themeable
|
224 |
|
|
*/
|
225 |
|
|
function theme_recaptcha_custom_widget() {
|
226 |
|
|
$recaptcha_only_if_incorrect_sol = t('Incorrect please try again');
|
227 |
|
|
$recaptcha_only_if_image_enter = t('Enter the words above:');
|
228 |
|
|
$recaptcha_only_if_audio_enter = t('Enter the words you hear:');
|
229 |
|
|
$recaptcha_get_another_captcha = t('Get another CAPTCHA');
|
230 |
|
|
$recaptcha_only_if_image = t('Get an audio CAPTCHA');
|
231 |
|
|
$recaptcha_only_if_audio = t('Get an image CAPTCHA');
|
232 |
|
|
$help = t('Help');
|
233 |
|
|
return <<<EOT
|
234 |
|
|
<div id="recaptcha_image"></div>
|
235 |
|
|
<div class="recaptcha_only_if_incorrect_sol" style="color:red">$recaptcha_only_if_incorrect_sol</div>
|
236 |
|
|
<span class="recaptcha_only_if_image">$recaptcha_only_if_image_enter</span>
|
237 |
|
|
<span class="recaptcha_only_if_audio">$recaptcha_only_if_audio_enter</span>
|
238 |
|
|
<input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
|
239 |
|
|
<div class="recaptcha_get_another_captcha"><a href="javascript:Recaptcha.reload()">$recaptcha_get_another_captcha</a></div>
|
240 |
|
|
<div class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')">$recaptcha_only_if_image</a></div>
|
241 |
|
|
<div class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type('image')">$recaptcha_only_if_audio</a></div>
|
242 |
|
|
<div class="recaptcha_help"><a href="javascript:Recaptcha.showhelp()">$help</a></div>
|
243 |
|
|
EOT;
|
244 |
|
|
}
|
245 |
|
|
|
246 |
|
|
/**
|
247 |
|
|
* Load the recaptcha library.
|
248 |
|
|
*/
|
249 |
|
|
function _recaptcha_load_library() {
|
250 |
|
|
return module_load_include('php', 'recaptcha', 'recaptcha-php-1.11/recaptchalib');
|
251 |
|
|
} |