[ticket/15937] Google reCAPTCHA v3 Plugin migration and clean up

PHPBB3-15937
This commit is contained in:
mrgoldy 2020-03-14 16:03:02 +01:00
parent 01b966c664
commit c8e5c36c1d
3 changed files with 43 additions and 41 deletions

View file

@ -48,13 +48,13 @@
<br><span>{{ lang('RECAPTCHA_V3_METHOD_EXPLAIN') }}</span>
</dt>
<dd>
{% for method, constant in RECAPTCHA_V3_METHODS %}
{% for method, available in RECAPTCHA_V3_METHODS %}
<label>
{% set disabled = not attribute(_context, 'S_RECAPTCHA_V3_' ~ method) ? ' disabled' %}
{% set checked = constant == RECAPTCHA_V3_METHOD ? ' checked' %}
{% set checked = method == RECAPTCHA_V3_METHOD ? ' checked' %}
{% set disabled = not available ? ' disabled' %}
<input class="radio" name="recaptcha_v3_method" type="radio" value="{{ constant }}"{{ checked ~ disabled }}>
<span>{{ lang('RECAPTCHA_V3_METHOD_' ~ method) }}</span>
<input class="radio" name="recaptcha_v3_method" type="radio" value="{{ method }}"{{ checked ~ disabled }}>
<span>{{ lang('RECAPTCHA_V3_METHOD_' ~ method|upper) }}</span>
</label>
{% endfor %}
</dd>
@ -65,17 +65,14 @@
<legend>{{ lang('RECAPTCHA_V3_THRESHOLDS') }}</legend>
<p>{{ lang('RECAPTCHA_V3_THRESHOLDS_EXPLAIN') }}</p>
<dl>
<dt>
<label for="recaptcha_v3_threshold">{{ lang('RECAPTCHA_V3_THRESHOLD') ~ lang('COLON') }}</label>
<br><span class="explain">{{ lang('RECAPTCHA_V3_THRESHOLD_EXPLAIN') }}</span>
</dt>
<dd><input id="recaptcha_v3_threshold" name="recaptcha_v3_threshold" type="number" value="{{ RECAPTCHA_V3_THRESHOLD }}" min="0" max="1" step="0.1"></dd>
</dl>
{% for threshold in thresholds %}
<dl>
<dt><label for="{{ threshold.key }}">{{ lang(threshold.key|upper) ~ lang('COLON') }}</label></dt>
<dt>
<label for="{{ threshold.key }}">{{ lang(threshold.key|upper) ~ lang('COLON') }}</label>
{% if lang_defined(threshold.key|upper ~ '_EXPLAIN') %}
<br><span>{{ lang(threshold.key|upper ~ '_EXPLAIN') }}</span>
{% endif %}
</dt>
<dd><input id="{{ threshold.key }}" name="{{ threshold.key }}" type="number" value="{{ threshold.value }}" min="0" max="1" step="0.1"></dd>
</dl>
{% endfor %}

View file

@ -47,6 +47,7 @@ $lang = array_merge($lang, array(
'RECAPTCHA_INCORRECT' => 'The solution you provided was incorrect',
'RECAPTCHA_NOSCRIPT' => 'Please enable JavaScript in your browser to load the challenge.',
'RECAPTCHA_NOT_AVAILABLE' => 'In order to use reCaptcha, you must create an account on <a href="http://www.google.com/recaptcha">www.google.com/recaptcha</a>.',
'RECAPTCHA_INVISIBLE' => 'This CAPTCHA is actually invisible. To verify that it works, a small icon should appear in right bottom corner of this page.',
'RECAPTCHA_PUBLIC' => 'Site key',
'RECAPTCHA_PUBLIC_EXPLAIN' => 'Your site reCAPTCHA key. Keys can be obtained on <a href="http://www.google.com/recaptcha">www.google.com/recaptcha</a>. Please, use reCAPTCHA v2 &gt; Invisible reCAPTCHA badge type.',
@ -55,17 +56,17 @@ $lang = array_merge($lang, array(
'RECAPTCHA_PRIVATE_EXPLAIN' => 'Your secret reCAPTCHA key. Keys can be obtained on <a href="http://www.google.com/recaptcha">www.google.com/recaptcha</a>. Please, use reCAPTCHA v2 &gt; Invisible reCAPTCHA badge type.',
'RECAPTCHA_V3_PRIVATE_EXPLAIN' => 'Your secret reCAPTCHA key. Keys can be obtained on <a href="http://www.google.com/recaptcha">www.google.com/recaptcha</a>. Please, use reCAPTCHA v3.',
'RECAPTCHA_INVISIBLE' => 'This CAPTCHA is actually invisible. To verify that it works, a small icon should appear in right bottom corner of this page.',
'RECAPTCHA_V3_DOMAIN' => 'Request domain',
'RECAPTCHA_V3_DOMAIN_EXPLAIN' => 'The domain to fetch the script from and to use when verifying the request.<br>Use <samp>recaptcha.net</samp> when <samp>google.com</samp> is not accessible.',
'RECAPTCHA_V3_METHOD' => 'Request method',
'RECAPTCHA_V3_METHOD_EXPLAIN' => 'The method to use when verifying the request.<br>Disabled options are not available within your setup.',
'RECAPTCHA_V3_METHOD_CURL' => 'cURL',
'RECAPTCHA_V3_METHOD_POST' => 'POST',
'RECAPTCHA_V3_METHOD_SOCKET' => 'Socket',
'RECAPTCHA_V3_THRESHOLD' => 'Default threshold',
'RECAPTCHA_V3_THRESHOLD_EXPLAIN' => 'Used when none of the other actions are applicable.',
'RECAPTCHA_V3_THRESHOLD_DEFAULT' => 'Default threshold',
'RECAPTCHA_V3_THRESHOLD_DEFAULT_EXPLAIN' => 'Used when none of the other actions are applicable.',
'RECAPTCHA_V3_THRESHOLD_LOGIN' => 'Login threshold',
'RECAPTCHA_V3_THRESHOLD_POST' => 'Post threshold',
'RECAPTCHA_V3_THRESHOLD_REGISTER' => 'Register threshold',

View file

@ -31,17 +31,26 @@ class recaptcha_v3 extends captcha_abstract
const GOOGLE = 'google.com';
const RECAPTCHA = 'recaptcha.net';
/** @var string Default action when no other applies */
static protected $action = 'default';
/** @var array CAPTCHA types mapped to their action with threshold */
/** @var array CAPTCHA types mapped to their action */
static protected $actions = [
0 => 'default',
CONFIRM_REG => 'register',
CONFIRM_LOGIN => 'login',
CONFIRM_POST => 'post',
CONFIRM_REPORT => 'report',
];
/**
* Get CAPTCHA types mapped to their action.
*
* @static
* @return array
*/
static public function get_actions()
{
return self::$actions;
}
/**
* Execute.
*
@ -116,7 +125,7 @@ class recaptcha_v3 extends captcha_abstract
}
/**
* Whether or not this CAPTCHA plugin is available.
* Whether or not this CAPTCHA plugin is available and setup.
*
* @return bool
*/
@ -170,7 +179,6 @@ class recaptcha_v3 extends captcha_abstract
$config->set('recaptcha_v3_secret', $request->variable('recaptcha_v3_secret', '', true));
$config->set('recaptcha_v3_domain', $request->variable('recaptcha_v3_domain', '', true));
$config->set('recaptcha_v3_method', $request->variable('recaptcha_v3_method', '', true));
$config->set('recaptcha_v3_threshold', $request->variable('recaptcha_v3_threshold', 0.50));
foreach (self::$actions as $action)
{
@ -196,22 +204,17 @@ class recaptcha_v3 extends captcha_abstract
'RECAPTCHA_V3_KEY' => $config['recaptcha_v3_key'] ?? '',
'RECAPTCHA_V3_SECRET' => $config['recaptcha_v3_secret'] ?? '',
'RECAPTCHA_V3_THRESHOLD' => $config['recaptcha_v3_threshold'] ?? 0.5,
'RECAPTCHA_V3_DOMAIN' => $config['recaptcha_v3_domain'] ?? self::GOOGLE,
'RECAPTCHA_V3_DOMAINS' => [self::GOOGLE, self::RECAPTCHA],
'RECAPTCHA_V3_METHOD' => $config['recaptcha_v3_method'] ?? self::POST,
'RECAPTCHA_V3_METHODS' => [
'POST' => self::POST,
'CURL' => self::CURL,
'SOCKET' => self::SOCKET,
self::POST => ini_get('allow_url_fopen') && function_exists('file_get_contents'),
self::CURL => extension_loaded('curl') && function_exists('curl_init'),
self::SOCKET => function_exists('fsockopen'),
],
'S_RECAPTCHA_V3_CURL' => extension_loaded('curl') && function_exists('curl_init'),
'S_RECAPTCHA_V3_POST' => ini_get('allow_url_fopen') && function_exists('file_get_contents'),
'S_RECAPTCHA_V3_SOCKET' => function_exists('fsockopen'),
'U_ACTION' => $module->u_action,
]);
}
@ -257,7 +260,7 @@ class recaptcha_v3 extends captcha_abstract
$template->assign_vars([
'CONFIRM_EXPLAIN' => $language->lang($explain, '<a href="' . $contact . '">', '</a>'),
'RECAPTCHA_ACTION' => self::$actions[$this->type] ?? self::$action,
'RECAPTCHA_ACTION' => self::$actions[$this->type] ?? reset(self::$actions),
'RECAPTCHA_KEY' => $config['recaptcha_v3_key'] ?? '',
'U_RECAPTCHA_SCRIPT' => sprintf('//%s/recaptcha/api.js?render=%s', $domain, $render),
@ -299,9 +302,10 @@ class recaptcha_v3 extends captcha_abstract
*/
global $config, $language, $request, $user;
$action = $request->variable('recaptcha_action', self::$action, true);
$token = $request->variable('recaptcha_token', '', true);
$threshold = (double) $config["recaptcha_v3_threshold_{$action}"] ?? $config['recaptcha_v3_threshold'] ?? 0.5;
$action = $request->variable('recaptcha_action', '', true);
$action = in_array($action, self::$actions) ? $action : reset(self::$actions);
$threshold = (double) $config["recaptcha_v3_threshold_{$action}"] ?? 0.5;
// No token was provided, discard spam submissions
if (empty($token))