diff --git a/js/recaptcha-customize.js b/js/recaptcha-customize.js deleted file mode 100644 index 718d60d..0000000 --- a/js/recaptcha-customize.js +++ /dev/null @@ -1,112 +0,0 @@ -/** - * @author Yan Dong (2017/05/15) - * @requires jQuery v1.8.3 or later - * @file recaptcha customize js file - * Can re-write below two function: - * - recaptchaFrontEndVali - * - recaptchaBeforeSubmit - */ - -RecaptchaCustomize = {}; - -/** - * Invisible reCAPTCHA api onload callback, render the widget explicitly. - */ -var onRecaptchaApiReady = function () { - jQuery('form').each(function() { - if (jQuery(this).find('.g-recaptcha[data-size="invisible"]').length > 0) { - var widgetId = grecaptcha.render(jQuery(this).find('.g-recaptcha')[0], {}, true); - jQuery(this).data('recaptcha-id', widgetId); - } - }); -}; - -/** - * Google reCAPTCHA callback, when the user submits a successful CAPTCHA response. - */ -var recaptchaValiCallback = function() { - RecaptchaCustomize.submitForm(); -}; - -/** - * Re-write this function if have front end validation. - * @param {Obj} $form The form element will execute invisible reCAPTCHA. - * @return {Boolean} If front end validation pass will return true, otherwise return false. - */ -var recaptchaFrontEndVali = function($form) { - return true; -} - -/** - * Re-write this function if need do something before submit form after execute invisible reCAPTCHA successfully. - * @param {[type]} $from The form element will execute invisible reCAPTCHA. - */ -var recaptchaBeforeSubmit = function($from) {}; - -(function($) { - - // Add reCAPTCHA API. - (function (d, s, id) { - var js, - fjs = d.getElementsByTagName(s)[0], - lang = $('html').attr('xml:lang'); - lang = lang ? lang : $('html').prop('lang'); - // drupal_add_js('https://apis.google.com/js/api:client.js', array('type' => 'external', 'weight' => 60)); - if (d.getElementById(id)) { - return; - } - js = d.createElement(s); - js.id = id; - js.async = true; - js.defer = true; - js.src = '//www.google.com/recaptcha/api.js?hl=' + lang + '&onload=onRecaptchaApiReady&render=explicit'; - fjs.parentNode.insertBefore(js, fjs); - }(document, 'script', 'recaptcha-api')); - - /** - * Please put here the function before form submit. - */ - RecaptchaCustomize.submitForm = function() { - var $this_form = $('.clicked-submit'); - recaptchaBeforeSubmit($this_form); - $this_form.submit(); - }; - - /** - * Execute reCAPTCHA. - */ - RecaptchaCustomize.exeCaptcha = function() { - $('form').on('click', '.form-submit', function(e) { - var $this_form = $(this).parents('form'); - if ($this_form.find('.g-recaptcha[data-size="invisible"]').length > 0) { - e.preventDefault(); - $('form').removeClass('clicked-submit'); - $this_form.addClass('clicked-submit'); - var widgetId = $this_form.data('recaptcha-id'); - if (recaptchaFrontEndVali($this_form)) { - if (grecaptcha.getResponse(widgetId)) { - RecaptchaCustomize.submitForm(); - } else { - grecaptcha.execute(widgetId); - } - } - } - }); - }; - - /** - * Add invisible reCAPTCHA class in body to define captcha layout. - */ - RecaptchaCustomize.addInvisibleRecClass = function() { - if ($('.g-recaptcha[data-size="invisible"]').length > 0) { - $('body').addClass('use-invisible-recaptcha'); - } - } - - // Document ready. - $(function() { - RecaptchaCustomize.addInvisibleRecClass(); - RecaptchaCustomize.exeCaptcha(); - }) - -}(jQuery)); \ No newline at end of file diff --git a/recaptcha.admin.inc b/recaptcha.admin.inc index 7951602..bd2dc23 100644 --- a/recaptcha.admin.inc +++ b/recaptcha.admin.inc @@ -13,14 +13,7 @@ function recaptcha_admin_settings() { '#type' => 'fieldset', '#title' => t('General settings'), ); - - $form['recaptcha_general_settings']['general'] = array( - '#type' => 'fieldset', - '#title' => t('General reCaptcha settings'), - '#collapsible' => TRUE, - '#collapsed' => FALSE, - ); - $form['recaptcha_general_settings']['general']['recaptcha_site_key'] = array( + $form['recaptcha_general_settings']['recaptcha_site_key'] = array( '#type' => 'textfield', '#title' => t('Site key'), '#default_value' => variable_get('recaptcha_site_key', ''), @@ -28,7 +21,7 @@ function recaptcha_admin_settings() { '#description' => t('The site key given to you when you register for reCAPTCHA.', array('@url' => 'http://www.google.com/recaptcha/admin')), '#required' => TRUE, ); - $form['recaptcha_general_settings']['general']['recaptcha_secret_key'] = array( + $form['recaptcha_general_settings']['recaptcha_secret_key'] = array( '#type' => 'textfield', '#title' => t('Secret key'), '#default_value' => variable_get('recaptcha_secret_key', ''), @@ -37,29 +30,6 @@ function recaptcha_admin_settings() { '#required' => TRUE, ); - $form['recaptcha_general_settings']['invisible'] = array( - '#type' => 'fieldset', - '#title' => t('Invisible reCaptcha settings'), - '#collapsible' => TRUE, - '#collapsed' => FALSE, - ); - $form['recaptcha_general_settings']['invisible']['invisible_recaptcha_site_key'] = array( - '#type' => 'textfield', - '#title' => t('Site key'), - '#default_value' => variable_get('invisible_recaptcha_site_key', ''), - '#maxlength' => 40, - '#description' => t('The site key given to you when you register for reCAPTCHA.', array('@url' => 'http://www.google.com/recaptcha/admin')), - '#required' => TRUE, - ); - $form['recaptcha_general_settings']['invisible']['invisible_recaptcha_secret_key'] = array( - '#type' => 'textfield', - '#title' => t('Secret key'), - '#default_value' => variable_get('invisible_recaptcha_secret_key', ''), - '#maxlength' => 40, - '#description' => t('The secret key given to you when you register for reCAPTCHA.', array('@url' => 'http://www.google.com/recaptcha/admin')), - '#required' => TRUE, - ); - $form['recaptcha_widget_settings'] = array( '#type' => 'fieldset', '#title' => t('Widget settings'), @@ -92,7 +62,6 @@ function recaptcha_admin_settings() { '#options' => array( '' => t('Normal (default)'), 'compact' => t('Compact'), - 'invisible' => 'Invisible', ), '#title' => t('Size'), '#type' => 'select', diff --git a/recaptcha.module b/recaptcha.module index 361c727..1e65aab 100644 --- a/recaptcha.module +++ b/recaptcha.module @@ -73,7 +73,7 @@ function recaptcha_captcha($op, $captcha_type = '') { switch ($op) { case 'list': - return array('reCAPTCHA', 'Invisible'); + return array('reCAPTCHA'); case 'generate': $captcha = array(); @@ -107,8 +107,6 @@ function recaptcha_captcha($op, $captcha_type = '') { 'data-type' => variable_get('recaptcha_type', 'image'), 'data-size' => variable_get('recaptcha_size', ''), 'data-tabindex' => variable_get('recaptcha_tabindex', 0), - 'data-callback' => 'recaptchaValiCallback', - 'data-expired-callback' => 'recaptchaExpiredCallback', ); // Filter out empty tabindex/size. $attributes = array_filter($attributes); @@ -136,59 +134,6 @@ function recaptcha_captcha($op, $captcha_type = '') { $captcha = captcha_captcha('generate', 'Math'); } } - elseif ($captcha_type == 'Invisible') { - $recaptcha_site_key = variable_get('invisible_recaptcha_site_key', ''); - $recaptcha_secret_key = variable_get('invisible_recaptcha_secret_key', ''); - - if (!empty($recaptcha_site_key) && !empty($recaptcha_secret_key)) { - // Build the reCAPTCHA captcha form if site_key and secret_key are - // configured. Captcha requires TRUE to be returned in solution. - $captcha['solution'] = TRUE; - $captcha['captcha_validate'] = 'recaptcha_invisible_captcha_validation'; - $captcha['form']['captcha_response'] = array( - '#type' => 'hidden', - '#value' => 'Google no captcha', - ); - - $noscript = ''; - if (variable_get('recaptcha_noscript', 0)) { - $variables = array( - 'sitekey' => $recaptcha_site_key, - 'language' => $language->language, - ); - $noscript = theme('recaptcha_widget_noscript', array('widget' => $variables)); - } - - $attributes = array( - 'class' => 'g-recaptcha', - 'data-sitekey' => $recaptcha_site_key, - 'data-theme' => variable_get('recaptcha_theme', 'light'), - 'data-type' => variable_get('recaptcha_type', 'image'), - 'data-size' => 'invisible', - 'data-tabindex' => variable_get('recaptcha_tabindex', 0), - 'data-callback' => 'recaptchaValiCallback', - ); - // Filter out empty tabindex/size. - $attributes = array_filter($attributes); - - $captcha['form']['recaptcha_widget'] = array( - '#markup' => '', - '#suffix' => $noscript, - ); - - drupal_add_js( - drupal_get_path('module', 'recaptcha') . '/js/recaptcha-customize.js', - array( - 'type' => 'file', - 'scope' => 'footer', - ) - ); - } - else { - // Fallback to Math captcha as reCAPTCHA is not configured. - $captcha = captcha_captcha('generate', 'Math'); - } - } return $captcha; } } @@ -235,47 +180,6 @@ function recaptcha_captcha_validation($solution, $response, $element, $form_stat } /** - * CAPTCHA Callback; Validates the reCAPTCHA code. - */ -function recaptcha_invisible_captcha_validation($solution, $response, $element, $form_state) { - $recaptcha_secret_key = variable_get('invisible_recaptcha_secret_key', ''); - if (empty($_POST['g-recaptcha-response']) || empty($recaptcha_secret_key)) { - return FALSE; - } - - // Use drupal_http_request() to circumvent all issues with the Google library. - $recaptcha = new \ReCaptcha\ReCaptcha($recaptcha_secret_key, new \ReCaptcha\RequestMethod\Drupal7Post()); - - $resp = $recaptcha->verify( - $_POST['g-recaptcha-response'], - ip_address() - ); - - if ($resp->isSuccess()) { - // Verified! - return TRUE; - } - else { - // Error code reference, https://developers.google.com/recaptcha/docs/verify - $error_codes = array( - 'missing-input-secret' => t('The secret parameter is missing.'), - 'invalid-input-secret' => t('The secret parameter is invalid or malformed.'), - 'missing-input-response' => t('The response parameter is missing.'), - 'invalid-input-response' => t('The response parameter is invalid or malformed.'), - 'invalid-json' => t('The json response is invalid or malformed.'), - 'unknown' => t('Unknown error.'), - ); - foreach ($resp->getErrorCodes() as $code) { - if (!isset($error_codes[$code])) { - $code = 'unknown'; - } - watchdog('reCAPTCHA web service', '@error', array('@error' => $error_codes[$code]), WATCHDOG_ERROR); - } - } - return FALSE; -} - -/** * Process variables for recaptcha-widget-noscript.tpl.php. * * @see recaptcha-widget-noscript.tpl.php @@ -285,14 +189,3 @@ function template_preprocess_recaptcha_widget_noscript(&$variables) { $variables['language'] = check_plain($variables['widget']['language']); $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))); } - - -function recaptcha_variable_group_info() { - $groups['recaptcha'] = array( - 'title' => 'recaptcha', - 'access' => 'access administration pages', - 'path' => array('admin/config'), - ); - - return $groups; -} \ No newline at end of file