Split CAPTCHA check and loading methods for easy integration into other parts of the site

This commit is contained in:
Kijin Sung 2023-06-25 19:56:00 +09:00
parent c0f9c77902
commit 4c9b14f077
2 changed files with 54 additions and 17 deletions

View file

@ -225,23 +225,7 @@ class spamfilterController extends spamfilter
function triggerCheckCaptcha(&$obj)
{
$config = ModuleModel::getModuleConfig('spamfilter');
if (!isset($config) || !isset($config->captcha) || !in_array($config->captcha->type, ['recaptcha', 'turnstile']) || !$config->captcha->site_key || !$config->captcha->secret_key)
{
return;
}
if ($this->user->is_admin === 'Y')
{
return;
}
if ($config->captcha->target_users !== 'everyone' && $this->user->member_srl)
{
return;
}
if ($config->captcha->target_frequency !== 'every_time' && isset($_SESSION['recaptcha_authenticated']) && $_SESSION['recaptcha_authenticated'])
{
return;
}
if (!$config->captcha->target_devices[Mobile::isFromMobilePhone() ? 'mobile' : 'pc'])
if (!SpamfilterModel::isCaptchaEnabled())
{
return;
}

View file

@ -208,6 +208,59 @@ class spamfilterModel extends spamfilter
return new BaseObject();
}
/**
* Check if CAPTCHA is enabled
*
* @return bool
*/
public static function isCaptchaEnabled($target_action = null)
{
$config = ModuleModel::getModuleConfig('spamfilter');
$user = Context::get('logged_info');
if (!isset($config) || !isset($config->captcha) || !in_array($config->captcha->type, ['recaptcha', 'turnstile']) || !$config->captcha->site_key || !$config->captcha->secret_key)
{
return false;
}
if ($user->is_admin === 'Y')
{
return false;
}
if ($config->captcha->target_users !== 'everyone' && $user->member_srl)
{
return false;
}
if ($config->captcha->target_frequency !== 'every_time' && isset($_SESSION['recaptcha_authenticated']) && $_SESSION['recaptcha_authenticated'])
{
return false;
}
if (!$config->captcha->target_devices[Mobile::isFromMobilePhone() ? 'mobile' : 'pc'])
{
return false;
}
if ($target_action && !$config->captcha->target_actions[$target_action])
{
return false;
}
return true;
}
/**
* Get a CAPTCHA instance.
*
* @return object
*/
public static function getCaptcha($target_action)
{
$config = ModuleModel::getModuleConfig('spamfilter');
$captcha_class = 'Rhymix\\Modules\\Spamfilter\\Captcha\\' . $config->captcha->type;
$captcha_class::init($config->captcha);
$captcha = new $captcha_class();
$captcha->setTargetActions([$target_action => true]);
$captcha->addScripts();
return $captcha;
}
/**
* @brief Check if the trackbacks have already been registered to a particular article
*/