Enable reCAPTCHA in document and comment write forms, and auto-insert reCAPTCHA if the form does not contain a placeholder

This commit is contained in:
Kijin Sung 2016-06-08 00:52:20 +09:00
parent fe63f24617
commit ae27414230
3 changed files with 63 additions and 23 deletions

View file

@ -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');
}

View file

@ -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('<script src="https://www.google.com/recaptcha/api.js?render=explicit&amp;onload=reCaptchaCallback" async defer></script>');
$html = '<div id="recaptcha-config" data-sitekey="%s" data-theme="%s" data-size="%s"></div>';
$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('<script src="https://www.google.com/recaptcha/api.js?render=explicit&onload=reCaptchaCallback" async defer></script>');
self::$script_added = true;
}
$html = '<div id="recaptcha-instance-%d" class="g-recaptcha" data-sitekey="%s" data-theme="%s" data-size="%s"></div>';
$html = sprintf($html, self::$sequence++, escape(self::$config->site_key), self::$config->theme ?: 'light', self::$config->size ?: 'normal');
return $html;
self::$instances_inserted++;
return '<div id="recaptcha-instance-%d" class="g-recaptcha"></div>';
}
}

View file

@ -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 = $('<div class="g-recaptcha"></div>');
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")
});
});
}