mirror of
https://github.com/phpbb/phpbb.git
synced 2025-07-30 21:40:43 +02:00
[ticket/15937] Google reCAPTCHA v3 Plugin
PHPBB3-15937
This commit is contained in:
263
phpBB/phpbb/captcha/plugins/recaptcha_v3.php
Normal file
263
phpBB/phpbb/captcha/plugins/recaptcha_v3.php
Normal file
@@ -0,0 +1,263 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* This file is part of the phpBB Forum Software package.
|
||||
*
|
||||
* @copyright (c) phpBB Limited <https://www.phpbb.com>
|
||||
* @license GNU General Public License, version 2 (GPL-2.0)
|
||||
*
|
||||
* For full copyright and license information, please see
|
||||
* the docs/CREDITS.txt file.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace phpbb\captcha\plugins;
|
||||
|
||||
/**
|
||||
* Google reCaptcha v3 plugin.
|
||||
*/
|
||||
class recaptcha_v3 extends captcha_abstract
|
||||
{
|
||||
const CURL = 'curl';
|
||||
const POST = 'post';
|
||||
const SOCKET = 'socket';
|
||||
|
||||
const GOOGLE = 'google.com';
|
||||
const RECAPTCHA = 'recaptcha.net';
|
||||
|
||||
static protected $action = 'default';
|
||||
static protected $actions = [
|
||||
CONFIRM_REG => 'register',
|
||||
CONFIRM_LOGIN => 'login',
|
||||
CONFIRM_POST => 'post',
|
||||
CONFIRM_REPORT => 'report',
|
||||
];
|
||||
|
||||
public function execute()
|
||||
{
|
||||
}
|
||||
|
||||
public function execute_demo()
|
||||
{
|
||||
}
|
||||
|
||||
public function get_generator_class()
|
||||
{
|
||||
throw new \Exception('No generator class given.');
|
||||
}
|
||||
|
||||
public function get_name()
|
||||
{
|
||||
return 'CAPTCHA_RECAPTCHA_V3';
|
||||
}
|
||||
|
||||
public function has_config()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function init($type)
|
||||
{
|
||||
/**
|
||||
* @var \phpbb\language\language $language Language object
|
||||
*/
|
||||
global $language;
|
||||
|
||||
$language->add_lang('captcha_recaptcha');
|
||||
|
||||
parent::init($type);
|
||||
}
|
||||
|
||||
public function is_available()
|
||||
{
|
||||
/**
|
||||
* @var \phpbb\config\config $config Config object
|
||||
* @var \phpbb\language\language $language Language object
|
||||
*/
|
||||
global $config, $language;
|
||||
|
||||
$language->add_lang('captcha_recaptcha');
|
||||
|
||||
return ($config->offsetGet('recaptcha_v3_key') ?? false)
|
||||
&& ($config->offsetGet('recaptcha_v3_secret') ?? false);
|
||||
}
|
||||
|
||||
public function acp_page($id, $module)
|
||||
{
|
||||
/**
|
||||
* @var \phpbb\config\config $config Config object
|
||||
* @var \phpbb\language\language $language Language object
|
||||
* @var \phpbb\log\log $phpbb_log Log object
|
||||
* @var \phpbb\request\request $request Request object
|
||||
* @var \phpbb\template\template $template Template object
|
||||
* @var \phpbb\user $user User object
|
||||
*/
|
||||
global $config, $language, $phpbb_log, $request, $template, $user;
|
||||
|
||||
$module->tpl_name = 'captcha_recaptcha_v3_acp';
|
||||
$module->page_title = 'ACP_VC_SETTINGS';
|
||||
|
||||
$form_key = 'acp_captcha';
|
||||
add_form_key($form_key);
|
||||
|
||||
if ($request->is_set_post('submit'))
|
||||
{
|
||||
if (!check_form_key($form_key))
|
||||
{
|
||||
trigger_error($language->lang('FORM_INVALID') . adm_back_link($module->u_action), E_USER_WARNING);
|
||||
}
|
||||
|
||||
$config->set('recaptcha_v3_key', $request->variable('recaptcha_v3_key', '', true));
|
||||
$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)
|
||||
{
|
||||
$config->set("recaptcha_v3_threshold_{$action}", $request->variable("recaptcha_v3_threshold_{$action}", 0.50));
|
||||
}
|
||||
|
||||
$phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_CONFIG_VISUAL');
|
||||
|
||||
trigger_error($language->lang('CONFIG_UPDATED') . adm_back_link($module->u_action));
|
||||
}
|
||||
|
||||
foreach (self::$actions as $action)
|
||||
{
|
||||
$template->assign_block_vars('thresholds', [
|
||||
'key' => "recaptcha_v3_threshold_{$action}",
|
||||
'value' => $config["recaptcha_v3_threshold_{$action}"] ?? 0.5,
|
||||
]);
|
||||
}
|
||||
|
||||
$template->assign_vars([
|
||||
'CAPTCHA_NAME' => $this->get_service_name(),
|
||||
'CAPTCHA_PREVIEW' => $this->get_demo_template($id),
|
||||
|
||||
'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,
|
||||
],
|
||||
|
||||
'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,
|
||||
]);
|
||||
}
|
||||
|
||||
public function get_demo_template($id)
|
||||
{
|
||||
return $this->get_template();
|
||||
}
|
||||
|
||||
public function get_template()
|
||||
{
|
||||
/**
|
||||
* @var \phpbb\config\config $config Config object
|
||||
* @var \phpbb\language\language $language Language object
|
||||
* @var \phpbb\template\template $template Template object
|
||||
* @var string $phpbb_root_path phpBB root path
|
||||
* @var string $phpEx php File extensions
|
||||
*/
|
||||
global $config, $language, $template, $phpbb_root_path, $phpEx;
|
||||
|
||||
if ($this->is_solved())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$contact = phpbb_get_board_contact_link($config, $phpbb_root_path, $phpEx);
|
||||
$explain = $this->type !== CONFIRM_POST ? 'CONFIRM_EXPLAIN' : 'POST_CONFIRM_EXPLAIN';
|
||||
|
||||
$domain = $config['recaptcha_v3_domain'] ?? self::GOOGLE;
|
||||
$render = $config['recaptcha_v3_key'] ?? '';
|
||||
|
||||
$template->assign_vars([
|
||||
'CONFIRM_EXPLAIN' => $language->lang($explain, '<a href="' . $contact . '">', '</a>'),
|
||||
|
||||
'RECAPTCHA_ACTION' => self::$actions[$this->type] ?? self::$action,
|
||||
'RECAPTCHA_KEY' => $config['recaptcha_v3_key'] ?? '',
|
||||
'U_RECAPTCHA_SCRIPT' => sprintf('//%s/recaptcha/api.js?render=%s', $domain, $render),
|
||||
|
||||
'S_CONFIRM_CODE' => true,
|
||||
'S_RECAPTCHA_AVAILABLE' => $this->is_available(),
|
||||
'S_TYPE' => $this->type,
|
||||
]);
|
||||
|
||||
return 'captcha_recaptcha_v3.html';
|
||||
}
|
||||
|
||||
function validate()
|
||||
{
|
||||
if (!parent::validate())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->recaptcha_check_answer();
|
||||
}
|
||||
|
||||
function recaptcha_check_answer()
|
||||
{
|
||||
/**
|
||||
* @var \phpbb\config\config $config Config object
|
||||
* @var \phpbb\language\language $language Language object
|
||||
* @var \phpbb\request\request $request Request object
|
||||
* @var \phpbb\user $user User object
|
||||
*/
|
||||
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;
|
||||
|
||||
// Discard spam submissions
|
||||
if (empty($token))
|
||||
{
|
||||
return $language->lang('RECAPTCHA_INCORRECT');
|
||||
}
|
||||
|
||||
switch ($config['recaptcha_v3_method'] ?? '')
|
||||
{
|
||||
case self::CURL:
|
||||
$method = new \ReCaptcha\RequestMethod\CurlPost();
|
||||
break;
|
||||
|
||||
case self::SOCKET:
|
||||
$method = new \ReCaptcha\RequestMethod\SocketPost();
|
||||
break;
|
||||
|
||||
case self::POST:
|
||||
default:
|
||||
$method = new \ReCaptcha\RequestMethod\Post();
|
||||
break;
|
||||
}
|
||||
|
||||
$recaptcha = new \ReCaptcha\ReCaptcha($config['recaptcha_v3_secret'], $method);
|
||||
|
||||
$result = $recaptcha->setExpectedAction($action)
|
||||
->setScoreThreshold($threshold)
|
||||
->verify($token, $user->ip);
|
||||
|
||||
if ($result->isSuccess())
|
||||
{
|
||||
$this->solved = true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return $language->lang('RECAPTCHA_INCORRECT');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user