comparison sites/all/modules/recaptcha/recaptcha.module @ 3:b28be78d8160

alpha1.0 version
author danieleb <danielebarchiesi@me.com>
date Thu, 19 Sep 2013 10:33:07 +0100
parents b74b41bb73f0
children
comparison
equal deleted inserted replaced
2:b74b41bb73f0 3:b28be78d8160
72 72
73 case 'generate': 73 case 'generate':
74 $captcha_type = array_shift($args); 74 $captcha_type = array_shift($args);
75 $captcha = array(); 75 $captcha = array();
76 if ($captcha_type == 'reCAPTCHA') { 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. 77 // Retrieve configuration variables.
88 $recaptcha_theme = variable_get('recaptcha_theme', 'red'); 78 $recaptcha_theme = variable_get('recaptcha_theme', 'red');
89 $recaptcha_tabindex = variable_get('recaptcha_tabindex', NULL); 79 $recaptcha_tabindex = variable_get('recaptcha_tabindex', NULL);
90 $recaptcha_public_key = variable_get('recaptcha_public_key', FALSE); 80 $recaptcha_public_key = variable_get('recaptcha_public_key', FALSE);
91 $recaptcha_form_value = NULL;
92 $recaptcha_ajax_api = variable_get('recaptcha_ajax_api', FALSE); 81 $recaptcha_ajax_api = variable_get('recaptcha_ajax_api', FALSE);
93 82
94 // Fallback to Math if reCAPTCHA is not configured. 83 // Test if reCAPTCHA can be used, falling back to Math if it is not
95 if (!$recaptcha_public_key) { 84 // configured, the library won't load, or the server is down.
85 if (!$recaptcha_public_key || !_recaptcha_load_library() || !_recaptcha_test_recaptcha_server()) {
96 return captcha_captcha('generate', 'Math', $args); 86 return captcha_captcha('generate', 'Math', $args);
97 } 87 }
98 88
99 if ($recaptcha_ajax_api) { 89 if ($recaptcha_ajax_api) {
100 // By default CAPTCHA turns off page caching on any page where a 90 // By default CAPTCHA turns off page caching on any page where a
122 $_recaptcha_jsadded = TRUE; 112 $_recaptcha_jsadded = TRUE;
123 113
124 // Add support to display the custom theme. 114 // Add support to display the custom theme.
125 if ($recaptcha_theme == 'custom') { 115 if ($recaptcha_theme == 'custom') {
126 $recaptcha_options['custom_theme_widget'] = 'recaptcha_custom_theme_widget'; 116 $recaptcha_options['custom_theme_widget'] = 'recaptcha_custom_theme_widget';
127 $recaptcha_form_value = theme('recaptcha_custom_widget');
128 } 117 }
129 118
130 // Set the default tab index. 119 // Set the default tab index.
131 if (!empty($recaptcha_tabindex)) { 120 if (!empty($recaptcha_tabindex)) {
132 $recaptcha_options['tabindex'] = $recaptcha_tabindex; 121 $recaptcha_options['tabindex'] = $recaptcha_tabindex;
142 // end of the public key string before passing to recaptchalib. 131 // end of the public key string before passing to recaptchalib.
143 if (variable_get('recaptcha_nocookies', FALSE)) { 132 if (variable_get('recaptcha_nocookies', FALSE)) {
144 $recaptcha_public_key .= '&nocookie=1'; 133 $recaptcha_public_key .= '&nocookie=1';
145 } 134 }
146 135
147 $html = recaptcha_get_html($recaptcha_public_key, NULL, TRUE);
148 $captcha['form']['captcha_response'] = array( 136 $captcha['form']['captcha_response'] = array(
149 '#type' => 'hidden', 137 '#type' => 'hidden',
150 '#value' => 'reCAPTCHA', 138 '#value' => 'reCAPTCHA',
151 ); 139 );
152 140
153 // Expose the form, either straight HTML, or using the AJAX API. 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') : '';
154 if ($recaptcha_ajax_api == FALSE) { 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);
155 $captcha['form']['captcha_form'] = array( 147 $captcha['form']['captcha_form'] = array(
156 '#type' => 'item', 148 '#type' => 'item',
157 '#markup' => ($recaptcha_form_value ? '<div id="recaptcha_custom_theme_widget">' . $recaptcha_form_value . '</div>' : '') . $html, 149 '#markup' => (!empty($recaptcha_custom_html) ? '<div id="recaptcha_custom_theme_widget">' . $recaptcha_custom_html . '</div>' : '') . $html,
158 ); 150 );
159 } 151 }
160 else { 152 else {
161 $html = ($recaptcha_theme == 'custom') ? theme('recaptcha_custom_widget') : '';
162 $captcha['form']['captcha_form'] = array( 153 $captcha['form']['captcha_form'] = array(
163 '#type' => 'item', 154 '#type' => 'item',
164 '#markup' => '<div id="recaptcha_ajax_api_container">' . $html . '</div>', 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>',
165 ); 159 );
166 drupal_add_js('https://www.google.com/recaptcha/api/js/recaptcha.js', array('type' => 'external')); 160 drupal_add_js('https://www.google.com/recaptcha/api/js/recaptcha_ajax.js', array('type' => 'external'));
167 $recaptcha_options['public_key'] = $recaptcha_public_key; 161 $recaptcha_options['public_key'] = $recaptcha_public_key;
168 $recaptcha_options['container'] = 'recaptcha_ajax_api_container'; 162 $recaptcha_options['container'] = 'recaptcha_ajax_api_container';
169 drupal_add_js(array('recaptcha' => $recaptcha_options), 'setting'); 163 drupal_add_js(array('recaptcha' => $recaptcha_options), 'setting');
170 drupal_add_js(drupal_get_path('module', 'recaptcha') . '/recaptcha.js'); 164 drupal_add_js(drupal_get_path('module', 'recaptcha') . '/recaptcha.js');
171 } 165 }
251 245
252 /** 246 /**
253 * Load the recaptcha library. 247 * Load the recaptcha library.
254 */ 248 */
255 function _recaptcha_load_library() { 249 function _recaptcha_load_library() {
256 module_load_include('php', 'recaptcha', 'recaptcha-php-1.11/recaptchalib'); 250 return module_load_include('php', 'recaptcha', 'recaptcha-php-1.11/recaptchalib');
257 } 251 }