Add target actions to CAPTCHA object #1815

This commit is contained in:
Kijin Sung 2022-02-07 20:52:45 +09:00
parent dba78c46fe
commit 37aa3dea3b
2 changed files with 17 additions and 4 deletions

View file

@ -239,19 +239,19 @@ class spamfilterController extends spamfilter
return; return;
} }
$enable = false; $target_actions = [];
foreach (['signup', 'login', 'recovery', 'document', 'comment'] as $action) foreach (['signup', 'login', 'recovery', 'document', 'comment'] as $action)
{ {
if ($config->captcha->target_actions[$action]) if ($config->captcha->target_actions[$action])
{ {
if (preg_match(self::$_captcha_actions[$action], $obj->act) || ($action === 'comment' && (!$obj->act || $obj->act === 'dispBoardContent') && Context::get('document_srl'))) if (preg_match(self::$_captcha_actions[$action], $obj->act) || ($action === 'comment' && (!$obj->act || $obj->act === 'dispBoardContent') && Context::get('document_srl')))
{ {
$enable = true; $target_actions[$action] = true;
} }
} }
} }
if ($enable) if (count($target_actions))
{ {
include_once __DIR__ . '/spamfilter.lib.php'; include_once __DIR__ . '/spamfilter.lib.php';
spamfilter_reCAPTCHA::init($config->captcha); spamfilter_reCAPTCHA::init($config->captcha);
@ -262,7 +262,9 @@ class spamfilterController extends spamfilter
} }
else else
{ {
Context::set('captcha', new spamfilter_reCAPTCHA()); $captcha = new spamfilter_reCAPTCHA();
$captcha->setTargetActions($target_actions);
Context::set('captcha', $captcha);
} }
} }
} }

View file

@ -7,6 +7,7 @@ class spamfilter_reCAPTCHA
protected static $scripts_added = false; protected static $scripts_added = false;
protected static $instances_inserted = 0; protected static $instances_inserted = 0;
protected static $sequence = 1; protected static $sequence = 1;
protected $_target_actions = [];
public static function init($config) public static function init($config)
{ {
@ -60,6 +61,16 @@ class spamfilter_reCAPTCHA
} }
} }
public function setTargetActions(array $target_actions)
{
$this->_target_actions = $target_actions;
}
public function isTargetAction(string $action): bool
{
return isset($this->_target_actions[$action]);
}
public function __toString() public function __toString()
{ {
return sprintf('<div id="recaptcha-instance-%d" class="g-recaptcha"></div>', self::$instances_inserted++); return sprintf('<div id="recaptcha-instance-%d" class="g-recaptcha"></div>', self::$instances_inserted++);