Projet

Général

Profil

Paste
Télécharger (6,73 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / recaptcha / recaptcha.module @ be58a50c

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file
5 be58a50c Assos Assos
 * Verifies if user is a human without necessity to solve a CAPTCHA.
6 85ad3d82 Assos Assos
 */
7
8 be58a50c Assos Assos
require_once dirname(__FILE__) . '/recaptcha-php/src/ReCaptcha/ReCaptcha.php';
9
require_once dirname(__FILE__) . '/recaptcha-php/src/ReCaptcha/RequestMethod.php';
10
require_once dirname(__FILE__) . '/recaptcha-php/src/ReCaptcha/RequestParameters.php';
11
require_once dirname(__FILE__) . '/recaptcha-php/src/ReCaptcha/Response.php';
12
require_once dirname(__FILE__) . '/src/ReCaptcha/RequestMethod/Drupal7Post.php';
13
14 85ad3d82 Assos Assos
/**
15
 * Implements hook_help().
16
 */
17
function recaptcha_help($path, $arg) {
18
  switch ($path) {
19 be58a50c Assos Assos
    case 'admin/config/people/captcha/recaptcha':
20
      return t('Google <a href="@url">reCAPTCHA</a> is a free service to protect your website from spam and abuse. reCAPTCHA uses an advanced risk analysis engine and adaptive CAPTCHAs to keep automated software from engaging in abusive activities on your site. It does this while letting your valid users pass through with ease.', array('@url' => 'https://www.google.com/recaptcha'));
21 85ad3d82 Assos Assos
  }
22 be58a50c Assos Assos
}
23 4f315dab Assos Assos
24 be58a50c Assos Assos
/**
25
 * Implements hook_theme().
26
 */
27
function recaptcha_theme() {
28
  return array(
29
    'recaptcha_widget_noscript' => array(
30
      'variables' => array(
31
        'widget' => NULL,
32
      ),
33
      'template' => 'recaptcha-widget-noscript',
34
    ),
35
  );
36 85ad3d82 Assos Assos
}
37
38
/**
39
 * Implements hook_menu().
40
 */
41
function recaptcha_menu() {
42
  $items['admin/config/people/captcha/recaptcha'] = array(
43
    'title' => 'reCAPTCHA',
44 be58a50c Assos Assos
    'description' => 'Administer the Google No CAPTCHA reCAPTCHA web service.',
45 85ad3d82 Assos Assos
    'page callback' => 'drupal_get_form',
46
    'page arguments' => array('recaptcha_admin_settings'),
47
    'access arguments' => array('administer recaptcha'),
48
    'type' => MENU_LOCAL_TASK,
49
    'file' => 'recaptcha.admin.inc',
50 be58a50c Assos Assos
    'weight' => 1,
51 85ad3d82 Assos Assos
  );
52 be58a50c Assos Assos
53 85ad3d82 Assos Assos
  return $items;
54
}
55
56
/**
57
 * Implements hook_permission().
58
 */
59
function recaptcha_permission() {
60
  return array(
61
    'administer recaptcha' => array(
62 be58a50c Assos Assos
      'title' => t('Administer reCAPTCHA'),
63
      'description' => t('Administer reCAPTCHA settings'),
64 85ad3d82 Assos Assos
    ),
65
  );
66
}
67
68
/**
69
 * Implements hook_captcha().
70
 */
71 f76f30e1 Assos Assos
function recaptcha_captcha($op, $captcha_type = '') {
72 be58a50c Assos Assos
  global $language;
73
74 85ad3d82 Assos Assos
  switch ($op) {
75
    case 'list':
76
      return array('reCAPTCHA');
77
78
    case 'generate':
79
      $captcha = array();
80
      if ($captcha_type == 'reCAPTCHA') {
81 be58a50c Assos Assos
        $recaptcha_site_key = variable_get('recaptcha_site_key', '');
82
        $recaptcha_secret_key = variable_get('recaptcha_secret_key', '');
83
84
        if (!empty($recaptcha_site_key) && !empty($recaptcha_secret_key)) {
85
          // Build the reCAPTCHA captcha form if site_key and secret_key are
86
          // configured. Captcha requires TRUE to be returned in solution.
87
          $captcha['solution'] = TRUE;
88
          $captcha['captcha_validate'] = 'recaptcha_captcha_validation';
89
          $captcha['form']['captcha_response'] = array(
90
            '#type' => 'hidden',
91
            '#value' => 'Google no captcha',
92
          );
93 85ad3d82 Assos Assos
94 be58a50c Assos Assos
          $noscript = '';
95
          if (variable_get('recaptcha_noscript', 0)) {
96
            $variables = array(
97
              'sitekey' => $recaptcha_site_key,
98
              'language' => $language->language,
99
            );
100
            $noscript = theme('recaptcha_widget_noscript', array('widget' => $variables));
101 85ad3d82 Assos Assos
          }
102
103 be58a50c Assos Assos
          $attributes = array(
104
            'class' => 'g-recaptcha',
105
            'data-sitekey' => $recaptcha_site_key,
106
            'data-theme' => variable_get('recaptcha_theme', 'light'),
107
            'data-type' => variable_get('recaptcha_type', 'image'),
108
            'data-size' => variable_get('recaptcha_size', ''),
109
            'data-tabindex' => variable_get('recaptcha_tabindex', 0),
110
          );
111
          // Filter out empty tabindex/size.
112
          $attributes = array_filter($attributes);
113 85ad3d82 Assos Assos
114 be58a50c Assos Assos
          $captcha['form']['recaptcha_widget'] = array(
115
            '#markup' => '<div' . drupal_attributes($attributes) . '></div>',
116
            '#suffix' => $noscript,
117
          );
118 85ad3d82 Assos Assos
119 be58a50c Assos Assos
          // @todo: #1664602: D7 does not yet support "async" in drupal_add_js().
120
          // drupal_add_js(url('https://www.google.com/recaptcha/api.js', array('query' => array('hl' => $language->language), 'absolute' => TRUE)), array('defer' => TRUE, 'async' => TRUE, 'type' => 'external'));
121
          $data = array(
122
            '#tag' => 'script',
123
            '#value' => '',
124
            '#attributes' => array(
125
              'src' => url('https://www.google.com/recaptcha/api.js', array('query' => array('hl' => $language->language), 'absolute' => TRUE)),
126
              'async' => 'async',
127
              'defer' => 'defer',
128
            ),
129 85ad3d82 Assos Assos
          );
130 be58a50c Assos Assos
          drupal_add_html_head($data, 'recaptcha_api');
131 85ad3d82 Assos Assos
        }
132
        else {
133 be58a50c Assos Assos
          // Fallback to Math captcha as reCAPTCHA is not configured.
134
          $captcha = captcha_captcha('generate', 'Math');
135 85ad3d82 Assos Assos
        }
136
      }
137
      return $captcha;
138
  }
139
}
140
141
/**
142 be58a50c Assos Assos
 * CAPTCHA Callback; Validates the reCAPTCHA code.
143 85ad3d82 Assos Assos
 */
144 be58a50c Assos Assos
function recaptcha_captcha_validation($solution, $response, $element, $form_state) {
145
  $recaptcha_secret_key = variable_get('recaptcha_secret_key', '');
146
  if (empty($_POST['g-recaptcha-response']) || empty($recaptcha_secret_key)) {
147 3753f249 Assos Assos
    return FALSE;
148 85ad3d82 Assos Assos
  }
149
150 be58a50c Assos Assos
  // Use drupal_http_request() to circumvent all issues with the Google library.
151
  $recaptcha = new \ReCaptcha\ReCaptcha($recaptcha_secret_key, new \ReCaptcha\RequestMethod\Drupal7Post());
152 f76f30e1 Assos Assos
153 be58a50c Assos Assos
  $resp = $recaptcha->verify(
154
    $_POST['g-recaptcha-response'],
155
    ip_address()
156
  );
157 f76f30e1 Assos Assos
158 be58a50c Assos Assos
  if ($resp->isSuccess()) {
159
    // Verified!
160
    return TRUE;
161
  }
162
  else {
163
    // Error code reference, https://developers.google.com/recaptcha/docs/verify
164
    $error_codes = array(
165
      'missing-input-secret' => t('The secret parameter is missing.'),
166
      'invalid-input-secret' => t('The secret parameter is invalid or malformed.'),
167
      'missing-input-response' => t('The response parameter is missing.'),
168
      'invalid-input-response' => t('The response parameter is invalid or malformed.'),
169
      'invalid-json' => t('The json response is invalid or malformed.'),
170
      'unknown' => t('Unknown error.'),
171 85ad3d82 Assos Assos
    );
172 be58a50c Assos Assos
    foreach ($resp->getErrorCodes() as $code) {
173
      if (!isset($error_codes[$code])) {
174
        $code = 'unknown';
175
      }
176
      watchdog('reCAPTCHA web service', '@error', array('@error' => $error_codes[$code]), WATCHDOG_ERROR);
177
    }
178 85ad3d82 Assos Assos
  }
179
  return FALSE;
180
}
181
182
/**
183 be58a50c Assos Assos
 * Process variables for recaptcha-widget-noscript.tpl.php.
184 85ad3d82 Assos Assos
 *
185 be58a50c Assos Assos
 * @see recaptcha-widget-noscript.tpl.php
186 85ad3d82 Assos Assos
 */
187 be58a50c Assos Assos
function template_preprocess_recaptcha_widget_noscript(&$variables) {
188
  $variables['sitekey']  = check_plain($variables['widget']['sitekey']);
189
  $variables['language'] = check_plain($variables['widget']['language']);
190
  $variables['url']      = check_url(url('https://www.google.com/recaptcha/api/fallback', array('query' => array('k' => $variables['widget']['sitekey'], 'hl' => $variables['widget']['language']), 'absolute' => TRUE)));
191 85ad3d82 Assos Assos
}