diff --git a/addons/recaptcha/recaptcha.addon.php b/addons/recaptcha/recaptcha.addon.php index 3d01c82a8..a04d8b2a4 100644 --- a/addons/recaptcha/recaptcha.addon.php +++ b/addons/recaptcha/recaptcha.addon.php @@ -5,11 +5,21 @@ if (!defined('RX_BASEDIR') || !$addon_info->site_key || !$addon_info->secret_key return; } -if ($addon_info->use_signup === 'Y' && preg_match('/^(?:disp|proc)Member(?:SignUp|Insert)/i', Context::get('act'))) +$current_action = Context::get('act'); + +if ($addon_info->use_signup === 'Y' && preg_match('/^(?:disp|proc)Member(?:SignUp|Insert)/i', $current_action)) { $enable_captcha = true; } -elseif ($addon_info->use_recovery === 'Y' && preg_match('/^(?:disp|proc)Member(?:FindAccount|ResendAuthMail)/i', Context::get('act'))) +elseif ($addon_info->use_recovery === 'Y' && preg_match('/^(?:disp|proc)Member(?:FindAccount|ResendAuthMail)/i', $current_action)) +{ + $enable_captcha = true; +} +elseif ($addon_info->use_document === 'Y' && preg_match('/^(?:disp|proc)Board(Write|InsertDocument)/i', $current_action)) +{ + $enable_captcha = true; +} +elseif ($addon_info->use_comment === 'Y' && (preg_match('/^(?:disp|proc)Board(Content|InsertComment)/i', $current_action) || (!$current_action && Context::get('document_srl')))) { $enable_captcha = true; } @@ -23,7 +33,7 @@ if ($enable_captcha) include_once __DIR__ . '/recaptcha.class.php'; reCAPTCHA::init($addon_info); - if (strncasecmp('proc', Context::get('act'), 4) === 0) + if (strncasecmp('proc', $current_action, 4) === 0) { getController('module')->addTriggerFunction('moduleObject.proc', 'before', 'reCAPTCHA::check'); } diff --git a/addons/recaptcha/recaptcha.class.php b/addons/recaptcha/recaptcha.class.php index 2945eb1b7..46bcbd511 100644 --- a/addons/recaptcha/recaptcha.class.php +++ b/addons/recaptcha/recaptcha.class.php @@ -4,7 +4,8 @@ class reCAPTCHA { protected static $verify = 'https://www.google.com/recaptcha/api/siteverify'; protected static $config = null; - protected static $script_added = false; + protected static $scripts_added = false; + protected static $instances_inserted = 0; protected static $sequence = 1; public static function init($config) @@ -41,22 +42,22 @@ class reCAPTCHA } } + public function __construct() + { + if (!self::$scripts_added) + { + self::$scripts_added = true; + Context::loadFile(array('./addons/recaptcha/recaptcha.js', 'body')); + Context::addHtmlFooter(''); + $html = '
'; + $html = sprintf($html, escape(self::$config->site_key), self::$config->theme ?: 'light', self::$config->size ?: 'normal'); + Context::addHtmlFooter($html); + } + } + public function __toString() { - if (!self::$config) - { - return ''; - } - - if (!self::$script_added) - { - Context::loadFile(array('./addons/recaptcha/recaptcha.js', 'body')); - Context::addHtmlFooter(''); - self::$script_added = true; - } - - $html = '
'; - $html = sprintf($html, self::$sequence++, escape(self::$config->site_key), self::$config->theme ?: 'light', self::$config->size ?: 'normal'); - return $html; + self::$instances_inserted++; + return '
'; } } diff --git a/addons/recaptcha/recaptcha.js b/addons/recaptcha/recaptcha.js index 7d74a9234..9c634b778 100644 --- a/addons/recaptcha/recaptcha.js +++ b/addons/recaptcha/recaptcha.js @@ -1,11 +1,40 @@ function reCaptchaCallback() { - $(".g-recaptcha").each(function() { + var recaptcha_config = $("#recaptcha-config"); + var recaptcha_instances = $(".g-recaptcha"); + var recaptcha_instance_id = 1; + + if (recaptcha_instances.size() === 0) { + var autoinsert_candidates = $("form").filter(function() { + var actinput = $("input[name='act']", this); + if (actinput.size() && actinput.val() && actinput.val().match(/^proc.+Insert(Document|Comment)/i)) { + return true; + } + var procfilter = $(this).attr("onsubmit"); + if (procfilter && procfilter.match(/procFilter\b.+\binsert/i)) { + return true; + } + return false; + }); + autoinsert_candidates.each(function() { + var new_instance = $('
'); + new_instance.attr("id", "recaptcha-instance-" + recaptcha_instance_id++); + var autoinsert_point = $(this).find("button[type='submit'],input[type='submit']").parent(); + if (autoinsert_point.size()) { + new_instance.insertBefore(autoinsert_point); + } else { + new_instance.appendTo($(this)); + } + }); + var recaptcha_instances = $(".g-recaptcha"); + } + + recaptcha_instances.each(function() { var instance = $(this); grecaptcha.render(instance.attr("id"), { - sitekey: instance.data("sitekey"), - size: instance.data("size"), - theme: instance.data("theme") + sitekey: recaptcha_config.data("sitekey"), + size: recaptcha_config.data("size"), + theme: recaptcha_config.data("theme") }); }); }