Révision f76f30e1
Ajouté par Assos Assos il y a environ 9 ans
drupal7/sites/all/modules/recaptcha/recaptcha.module | ||
---|---|---|
63 | 63 |
/** |
64 | 64 |
* Implements hook_captcha(). |
65 | 65 |
*/ |
66 |
function recaptcha_captcha() { |
|
67 |
$args = func_get_args(); |
|
68 |
$op = array_shift($args); |
|
66 |
function recaptcha_captcha($op, $captcha_type = '') { |
|
69 | 67 |
switch ($op) { |
70 | 68 |
case 'list': |
71 | 69 |
return array('reCAPTCHA'); |
72 | 70 |
|
73 | 71 |
case 'generate': |
74 |
$captcha_type = array_shift($args); |
|
75 | 72 |
$captcha = array(); |
76 | 73 |
if ($captcha_type == 'reCAPTCHA') { |
77 | 74 |
// Retrieve configuration variables. |
... | ... | |
82 | 79 |
|
83 | 80 |
// Test if reCAPTCHA can be used, falling back to Math if it is not |
84 | 81 |
// 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);
|
|
82 |
if (!$recaptcha_public_key || !_recaptcha_load_library() || !_recaptcha_server_is_up()) {
|
|
83 |
return captcha_captcha('generate', 'Math'); |
|
87 | 84 |
} |
88 | 85 |
|
89 | 86 |
if ($recaptcha_ajax_api) { |
... | ... | |
169 | 166 |
} |
170 | 167 |
|
171 | 168 |
/** |
172 |
* @return boolean
|
|
173 |
* Whether or not the reCAPTCHA server is up.
|
|
169 |
* @return bool |
|
170 |
* TRUE if the reCAPTCHA server is up, FALSE otherwise.
|
|
174 | 171 |
*/ |
175 | 172 |
function _recaptcha_test_recaptcha_server() { |
176 |
$fs = @fsockopen(RECAPTCHA_VERIFY_SERVER, 80, $errno, $errstr, 10); |
|
177 |
if ($fs) { |
|
178 |
fclose($fs); |
|
173 |
$response = drupal_http_request('https://' . RECAPTCHA_VERIFY_SERVER); |
|
174 |
if (empty($response->error)) { |
|
179 | 175 |
return TRUE; |
180 | 176 |
} |
181 | 177 |
else { |
182 |
drupal_set_message(t('Unable to connect with the reCAPTCHA server (@server): @errno: @errstr', array(
|
|
178 |
watchdog('reCAPTCHA', 'Unable to connect with the reCAPTCHA server (@server): @errno: @errstr', array(
|
|
183 | 179 |
'@server' => RECAPTCHA_VERIFY_SERVER, |
184 |
'@errno' => $errno,
|
|
185 |
'@errstr' => $errstr,
|
|
186 |
)), 'error');
|
|
180 |
'@errno' => $response->code,
|
|
181 |
'@errstr' => $response->error,
|
|
182 |
), WATCHDOG_ERROR);
|
|
187 | 183 |
return FALSE; |
188 | 184 |
} |
189 | 185 |
} |
190 | 186 |
|
187 |
/** |
|
188 |
* Return the cached output of _recaptcha_test_recaptcha_server(). |
|
189 |
* |
|
190 |
* @return bool |
|
191 |
* TRUE if the reCAPTCHA server was up last time it was checked, FALSE |
|
192 |
* otherwise. |
|
193 |
*/ |
|
194 |
function _recaptcha_server_is_up() { |
|
195 |
static $server_status; |
|
196 |
// Use static cache value, if available. |
|
197 |
if (isset($server_status)) { |
|
198 |
return $server_status; |
|
199 |
} |
|
200 |
// Check if the status is cached already. |
|
201 |
$status_cache = cache_get('recaptcha_server_status'); |
|
202 |
if ($status_cache !== FALSE) { |
|
203 |
$server_status = (bool) $status_cache; |
|
204 |
return $server_status; |
|
205 |
} |
|
206 |
// Status is not cached. Check if server is up. |
|
207 |
$server_status = _recaptcha_test_recaptcha_server(); |
|
208 |
|
|
209 |
// Set to 5 minutes, if interval is not set. |
|
210 |
$cache_period = variable_get('recaptcha_server_status_check_interval', 5); |
|
211 |
// Save it as integer as cache_get returns FALSE, if not found. |
|
212 |
cache_set('recaptcha_server_status', (int) $server_status, 'cache', time() + ($cache_period * 60)); |
|
213 |
return $server_status; |
|
214 |
} |
|
215 |
|
|
191 | 216 |
/** |
192 | 217 |
* CAPTCHA Callback; Validates the reCAPTCHA code. |
193 | 218 |
*/ |
... | ... | |
195 | 220 |
global $user; |
196 | 221 |
$recaptcha_private_key = variable_get('recaptcha_private_key', FALSE); |
197 | 222 |
$ip_address = ip_address(); |
198 |
if ($recaptcha_private_key && $ip_address && $response === 'reCAPTCHA' && !empty($_POST['recaptcha_challenge_field']) && !empty($_POST['recaptcha_response_field']) && _recaptcha_test_recaptcha_server()) {
|
|
223 |
if ($recaptcha_private_key && $ip_address && $response === 'reCAPTCHA' && !empty($_POST['recaptcha_challenge_field']) && !empty($_POST['recaptcha_response_field']) && _recaptcha_server_is_up()) {
|
|
199 | 224 |
$resp = recaptcha_check_answer( |
200 | 225 |
$recaptcha_private_key, |
201 | 226 |
$ip_address, |
Formats disponibles : Unified diff
Weekly update of contrib modules