[ticket/17414] Extend plugin interface and wrapper

PHPBB-17414
This commit is contained in:
Marc Alexander 2024-10-04 13:42:45 +02:00
parent 15b51e3b8f
commit e84e9cace4
No known key found for this signature in database
GPG key ID: 50E0D2423696F995
5 changed files with 119 additions and 16 deletions

View file

@ -235,8 +235,10 @@ class ucp_register
// The CAPTCHA kicks in here. We can't help that the information gets lost on language change. // The CAPTCHA kicks in here. We can't help that the information gets lost on language change.
if ($config['enable_confirm']) if ($config['enable_confirm'])
{ {
$captcha = $phpbb_container->get('captcha.factory')->get_instance($config['captcha_plugin']); /** @var \phpbb\captcha\factory $captcha_factory */
$captcha->init(CONFIRM_REG); $captcha_factory = $phpbb_container->get('captcha.factory');
$captcha = $captcha_factory->get_instance($config['captcha_plugin']);
$captcha->init(\phpbb\captcha\plugins\plugin_interface::CONFIRM_REGISTRATION);
} }
$timezone = $config['board_timezone']; $timezone = $config['board_timezone'];
@ -426,7 +428,7 @@ class ucp_register
} }
// Okay, captcha, your job is done. // Okay, captcha, your job is done.
if ($config['enable_confirm'] && isset($captcha)) if ($config['enable_confirm'])
{ {
$captcha->reset(); $captcha->reset();
} }

View file

@ -13,6 +13,9 @@
namespace phpbb\captcha; namespace phpbb\captcha;
use phpbb\captcha\plugins\legacy_wrapper;
use phpbb\captcha\plugins\plugin_interface;
class factory class factory
{ {
/** /**
@ -41,11 +44,17 @@ class factory
* Return a new instance of a given plugin * Return a new instance of a given plugin
* *
* @param $name * @param $name
* @return object|null * @return plugin_interface
*/ */
public function get_instance($name) public function get_instance($name): plugin_interface
{ {
return $this->container->get($name); $captcha = $this->container->get($name);
if ($captcha instanceof plugin_interface)
{
return $captcha;
}
return new legacy_wrapper($name);
} }
/** /**

View file

@ -17,6 +17,8 @@ class legacy_wrapper implements plugin_interface
{ {
private $legacy_captcha; private $legacy_captcha;
private string $last_error;
public function __construct($legacy_captcha) public function __construct($legacy_captcha)
{ {
$this->legacy_captcha = $legacy_captcha; $this->legacy_captcha = $legacy_captcha;
@ -72,11 +74,69 @@ class legacy_wrapper implements plugin_interface
* @param int $type Type of captcha, should be one of the CONFIRMATION_* constants * @param int $type Type of captcha, should be one of the CONFIRMATION_* constants
* @return void * @return void
*/ */
public function show(int $type): void public function init(int $type): void
{ {
if (method_exists($this->legacy_captcha, 'init')) if (method_exists($this->legacy_captcha, 'init'))
{ {
$this->legacy_captcha->init($type); $this->legacy_captcha->init($type);
} }
} }
}
/**
* {@inheritDoc}
*/
public function validate(array $request_data): bool
{
if (method_exists($this->legacy_captcha, 'validate'))
{
$error = $this->legacy_captcha->validate($request_data);
if ($error)
{
$this->last_error = $error;
return false;
}
return true;
}
return false;
}
/**
* {@inheritDoc}
*/
public function is_solved(): bool
{
if (method_exists($this->legacy_captcha, 'is_solved'))
{
return $this->legacy_captcha->is_solved();
}
return false;
}
/**
* {@inheritDoc}
*/
public function reset(): void
{
if (method_exists($this->legacy_captcha, 'reset'))
{
$this->legacy_captcha->reset();
}
}
/**
* {@inheritDoc}
*/
public function get_attempt_count(): int
{
if (method_exists($this->legacy_captcha, 'get_attempt_count'))
{
return $this->legacy_captcha->get_attempt_count();
}
// Ensure this is deemed as too many attempts
return PHP_INT_MAX;
}
}

View file

@ -15,16 +15,17 @@ namespace phpbb\captcha\plugins;
interface plugin_interface interface plugin_interface
{ {
const CONFIRMATION_REGISTRATION = 1; const CONFIRM_REGISTRATION = 1;
const CONFIRMATION_LOGIN = 2; const CONFIRM_LOGIN = 2;
const CONFIRMATION_POST = 3; const CONFIRM_POST = 3;
const CONFIRMATION_REPORT = 4; const CONFIRM_REPORT = 4;
/** /**
* Check if the plugin is available * Check if the plugin is available
*
* @return bool True if the plugin is available, false if not * @return bool True if the plugin is available, false if not
*/ */
public function is_available(): bool; public function is_available(): bool;
@ -49,5 +50,34 @@ interface plugin_interface
* @param int $type Type of captcha, should be one of the CONFIRMATION_* constants * @param int $type Type of captcha, should be one of the CONFIRMATION_* constants
* @return void * @return void
*/ */
public function show(int $type): void; public function init(int $type): void;
/**
* Validate the captcha with the given request data
*
* @param array $request_data Request data for the captcha
* @return bool True if request data was valid captcha reply, false if not
*/
public function validate(array $request_data): bool;
/**
* Return whether captcha was solved
*
* @return bool True if captcha was solved, false if not
*/
public function is_solved(): bool;
/**
* Reset captcha state, e.g. after checking if it's valid
*
* @return void
*/
public function reset(): void;
/**
* Get attempt count for this captcha and user
*
* @return int Number of attempts
*/
public function get_attempt_count(): int;
} }

View file

@ -455,8 +455,10 @@ if (!$is_authed || !empty($error))
if ($config['enable_post_confirm'] && !$user->data['is_registered']) if ($config['enable_post_confirm'] && !$user->data['is_registered'])
{ {
$captcha = $phpbb_container->get('captcha.factory')->get_instance($config['captcha_plugin']); /** @var \phpbb\captcha\factory $captcha_factory */
$captcha->init(CONFIRM_POST); $captcha_factory = $phpbb_container->get('captcha.factory');
$captcha = $captcha_factory->get_instance($config['captcha_plugin']);
$captcha->init(\phpbb\captcha\plugins\plugin_interface::CONFIRM_POST);
} }
// Is the user able to post within this forum? // Is the user able to post within this forum?
@ -1600,7 +1602,7 @@ if ($submit || $preview || $refresh)
); );
extract($phpbb_dispatcher->trigger_event('core.posting_modify_submit_post_after', compact($vars))); extract($phpbb_dispatcher->trigger_event('core.posting_modify_submit_post_after', compact($vars)));
if ($config['enable_post_confirm'] && !$user->data['is_registered'] && (isset($captcha) && $captcha->is_solved() === true) && ($mode == 'post' || $mode == 'reply' || $mode == 'quote')) if ($config['enable_post_confirm'] && !$user->data['is_registered'] && $captcha->is_solved() === true && ($mode == 'post' || $mode == 'reply' || $mode == 'quote'))
{ {
$captcha->reset(); $captcha->reset();
} }