comparison sites/all/modules/recaptcha/recaptcha.module @ 2:b74b41bb73f0

-- Google analytics module
author danieleb <danielebarchiesi@me.com>
date Thu, 22 Aug 2013 17:22:54 +0100
parents
children b28be78d8160
comparison
equal deleted inserted replaced
1:67ce89da90df 2:b74b41bb73f0
1 <?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 // Load the recaptcha library.
78 _recaptcha_load_library();
79
80 // Check if reCAPTCHA is available and show Math if not.
81 $connect = @fsockopen(RECAPTCHA_VERIFY_SERVER, 80);
82 if (!$connect) {
83 return captcha_captcha('generate', 'Math', $args);
84 }
85 fclose($connect);
86
87 // Retrieve configuration variables.
88 $recaptcha_theme = variable_get('recaptcha_theme', 'red');
89 $recaptcha_tabindex = variable_get('recaptcha_tabindex', NULL);
90 $recaptcha_public_key = variable_get('recaptcha_public_key', FALSE);
91 $recaptcha_form_value = NULL;
92 $recaptcha_ajax_api = variable_get('recaptcha_ajax_api', FALSE);
93
94 // Fallback to Math if reCAPTCHA is not configured.
95 if (!$recaptcha_public_key) {
96 return captcha_captcha('generate', 'Math', $args);
97 }
98
99 if ($recaptcha_ajax_api) {
100 // By default CAPTCHA turns off page caching on any page where a
101 // CAPTCHA challenge appears. If recaptcha is using AJAX API, set
102 // caching back to its old state as stored in DB.
103 global $conf;
104 $conf['cache'] = unserialize(db_query("SELECT value FROM {variable} WHERE name = 'cache'")->fetchField());
105 }
106
107 $recaptcha_options = array(
108 'theme' => $recaptcha_theme,
109 );
110
111 // Localization support.
112 global $language;
113 if (isset($language->language)) {
114 // reCAPTCHA uses two-character language codes, so 'pt-br' must be
115 // passed as 'pt'; cf. https://developers.google.com/recaptcha/docs/customization#i18n
116 $recaptcha_options['lang'] = drupal_substr($language->language, 0, 2);
117 }
118
119 // Construct the Javascript, but only display it once.
120 static $_recaptcha_jsadded = FALSE;
121 if ($_recaptcha_jsadded == FALSE && $recaptcha_ajax_api == FALSE) {
122 $_recaptcha_jsadded = TRUE;
123
124 // Add support to display the custom theme.
125 if ($recaptcha_theme == 'custom') {
126 $recaptcha_options['custom_theme_widget'] = 'recaptcha_custom_theme_widget';
127 $recaptcha_form_value = theme('recaptcha_custom_widget');
128 }
129
130 // Set the default tab index.
131 if (!empty($recaptcha_tabindex)) {
132 $recaptcha_options['tabindex'] = $recaptcha_tabindex;
133 }
134 drupal_add_js('var RecaptchaOptions = ' . drupal_json_encode($recaptcha_options) . ';', array('type' => 'inline'));
135 }
136
137 // Create the form. Captcha requires TRUE to be returned in solution.
138 $captcha['solution'] = TRUE;
139 $captcha['captcha_validate'] = 'recaptcha_captcha_validation';
140
141 // If 'Disable Client-Side Cookies' is set, then add query string to
142 // end of the public key string before passing to recaptchalib.
143 if (variable_get('recaptcha_nocookies', FALSE)) {
144 $recaptcha_public_key .= '&nocookie=1';
145 }
146
147 $html = recaptcha_get_html($recaptcha_public_key, NULL, TRUE);
148 $captcha['form']['captcha_response'] = array(
149 '#type' => 'hidden',
150 '#value' => 'reCAPTCHA',
151 );
152
153 // Expose the form, either straight HTML, or using the AJAX API.
154 if ($recaptcha_ajax_api == FALSE) {
155 $captcha['form']['captcha_form'] = array(
156 '#type' => 'item',
157 '#markup' => ($recaptcha_form_value ? '<div id="recaptcha_custom_theme_widget">' . $recaptcha_form_value . '</div>' : '') . $html,
158 );
159 }
160 else {
161 $html = ($recaptcha_theme == 'custom') ? theme('recaptcha_custom_widget') : '';
162 $captcha['form']['captcha_form'] = array(
163 '#type' => 'item',
164 '#markup' => '<div id="recaptcha_ajax_api_container">' . $html . '</div>',
165 );
166 drupal_add_js('https://www.google.com/recaptcha/api/js/recaptcha.js', array('type' => 'external'));
167 $recaptcha_options['public_key'] = $recaptcha_public_key;
168 $recaptcha_options['container'] = 'recaptcha_ajax_api_container';
169 drupal_add_js(array('recaptcha' => $recaptcha_options), 'setting');
170 drupal_add_js(drupal_get_path('module', 'recaptcha') . '/recaptcha.js');
171 }
172 }
173 return $captcha;
174 }
175 }
176
177 /**
178 * @return boolean
179 * Whether or not the reCAPTCHA server is up.
180 */
181 function _recaptcha_test_recaptcha_server() {
182 $test = TRUE;
183 $fs = @fsockopen(RECAPTCHA_VERIFY_SERVER, 80, $errno, $errstr, 10);
184 if (!$fs) {
185 $test = FALSE;
186 drupal_set_message(t('Unable to connect with the reCAPTCHA server (@server): @errno: @errstr', array(
187 '@server' => RECAPTCHA_VERIFY_SERVER,
188 '@errno' => $errno,
189 '@errstr' => $errstr,
190 )), 'error');
191 }
192 fclose($fs);
193 return $test;
194 }
195
196 /**
197 * CAPTCHA Callback; Validates the reCAPTCHA code.
198 */
199 function recaptcha_captcha_validation($solution = NULL, $response = NULL) {
200 global $user;
201 $recaptcha_private_key = variable_get('recaptcha_private_key', FALSE);
202 $ip_address = ip_address();
203 if ($recaptcha_private_key && $ip_address && $response === 'reCAPTCHA' && !empty($_POST['recaptcha_challenge_field']) && !empty($_POST['recaptcha_response_field']) && _recaptcha_test_recaptcha_server()) {
204 $resp = recaptcha_check_answer(
205 $recaptcha_private_key,
206 $ip_address,
207 check_plain($_POST['recaptcha_challenge_field']),
208 check_plain($_POST['recaptcha_response_field'])
209 );
210 return $resp->is_valid;
211 }
212 return FALSE;
213 }
214
215 /**
216 * Implements hook_theme().
217 */
218 function recaptcha_theme() {
219 return array(
220 'recaptcha_custom_widget' => array(
221 'arguments' => array(),
222 ),
223 );
224 }
225
226 /**
227 * Theme function: creates the custom themed recaptcha widget.
228 *
229 * @ingroup themeable
230 */
231 function theme_recaptcha_custom_widget() {
232 $recaptcha_only_if_incorrect_sol = t('Incorrect please try again');
233 $recaptcha_only_if_image_enter = t('Enter the words above:');
234 $recaptcha_only_if_audio_enter = t('Enter the words you hear:');
235 $recaptcha_get_another_captcha = t('Get another CAPTCHA');
236 $recaptcha_only_if_image = t('Get an audio CAPTCHA');
237 $recaptcha_only_if_audio = t('Get an image CAPTCHA');
238 $help = t('Help');
239 return <<<EOT
240 <div id="recaptcha_image"></div>
241 <div class="recaptcha_only_if_incorrect_sol" style="color:red">$recaptcha_only_if_incorrect_sol</div>
242 <span class="recaptcha_only_if_image">$recaptcha_only_if_image_enter</span>
243 <span class="recaptcha_only_if_audio">$recaptcha_only_if_audio_enter</span>
244 <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
245 <div class="recaptcha_get_another_captcha"><a href="javascript:Recaptcha.reload()">$recaptcha_get_another_captcha</a></div>
246 <div class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')">$recaptcha_only_if_image</a></div>
247 <div class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type('image')">$recaptcha_only_if_audio</a></div>
248 <div class="recaptcha_help"><a href="javascript:Recaptcha.showhelp()">$help</a></div>
249 EOT;
250 }
251
252 /**
253 * Load the recaptcha library.
254 */
255 function _recaptcha_load_library() {
256 module_load_include('php', 'recaptcha', 'recaptcha-php-1.11/recaptchalib');
257 }