diff --git a/modules/spamfilter/spamfilter.controller.php b/modules/spamfilter/spamfilter.controller.php index 8a1091110..ed6667f42 100644 --- a/modules/spamfilter/spamfilter.controller.php +++ b/modules/spamfilter/spamfilter.controller.php @@ -264,6 +264,7 @@ class spamfilterController extends spamfilter { $captcha = new spamfilter_reCAPTCHA(); $captcha->setTargetActions($target_actions); + $captcha->addScripts(); Context::set('captcha', $captcha); } } diff --git a/modules/spamfilter/spamfilter.lib.php b/modules/spamfilter/spamfilter.lib.php index 02b001f8a..54560ddc1 100644 --- a/modules/spamfilter/spamfilter.lib.php +++ b/modules/spamfilter/spamfilter.lib.php @@ -48,15 +48,15 @@ class spamfilter_reCAPTCHA $_SESSION['recaptcha_authenticated'] = true; } - public function __construct() + public function addScripts() { if (!self::$scripts_added) { self::$scripts_added = true; Context::loadFile(array('./modules/spamfilter/tpl/js/recaptcha.js', 'body')); Context::addHtmlFooter(''); - $html = '
'; - $html = sprintf($html, escape(self::$config->site_key), self::$config->theme ?: 'light', self::$config->size ?: 'normal'); + $html = '
'; + $html = sprintf($html, escape(self::$config->site_key), self::$config->theme ?: 'light', self::$config->size ?: 'normal', implode(',', array_keys($this->_target_actions))); Context::addHtmlFooter($html); } } diff --git a/modules/spamfilter/tpl/js/recaptcha.js b/modules/spamfilter/tpl/js/recaptcha.js index a1e5ae1cb..91a93af9d 100644 --- a/modules/spamfilter/tpl/js/recaptcha.js +++ b/modules/spamfilter/tpl/js/recaptcha.js @@ -3,15 +3,31 @@ function reCaptchaCallback() { var recaptcha_config = $("#recaptcha-config"); var recaptcha_instances = $(".g-recaptcha"); var recaptcha_instance_id = 1; + var recaptcha_targets = String(recaptcha_config.data("targets")).split(","); - if (recaptcha_instances.size() === 0) { + if (recaptcha_instances.length === 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|)|Login|FindAccount|ResendAuthMail)/i)) { - return true; + if (actinput.length && actinput.val()) { + var act = String(actinput.val()); + if (act.match(/^procMemberInsert$/i) && recaptcha_targets.indexOf("signup") > -1) { + return true; + } + if (act.match(/^procMemberLogin$/i) && recaptcha_targets.indexOf("login") > -1) { + return true; + } + if (act.match(/^procMember(FindAccount|ResendAuthMail)$/i) && recaptcha_targets.indexOf("recovery") > -1) { + return true; + } + if (act.match(/^proc[A-Z][a-zA-Z0-9_]+InsertDocument$/i) && recaptcha_targets.indexOf("document") > -1) { + return true; + } + if (act.match(/^proc[A-Z][a-zA-Z0-9_]+InsertComment$/i) && recaptcha_targets.indexOf("comment") > -1) { + return true; + } } var procfilter = $(this).attr("onsubmit"); - if (procfilter && procfilter.match(/procFilter\b.+\binsert/i)) { + if (procfilter && procfilter.match(/procFilter\b.+\binsert/i) && (recaptcha_targets.indexOf("document") > -1 || recaptcha_targets.indexOf("comment") > -1)) { return true; } return false;