[ticket/17414] Create abstract base class for captchas

PHPBB-17414
This commit is contained in:
Marc Alexander 2024-10-13 08:53:11 +02:00
parent 2500f722ab
commit 01dd0b168a
No known key found for this signature in database
GPG key ID: 50E0D2423696F995
3 changed files with 71 additions and 3 deletions

View file

@ -60,6 +60,7 @@ services:
shared: false shared: false
arguments: arguments:
- '@config' - '@config'
- '@dbal.conn'
- '@language' - '@language'
- '@log' - '@log'
- '@request' - '@request'

View file

@ -0,0 +1,62 @@
<?php
namespace phpbb\captcha\plugins;
use phpbb\captcha\plugins\plugin_interface;
use phpbb\db\driver\driver_interface;
abstract class base implements plugin_interface
{
/** @var driver_interface */
protected driver_interface $db;
/** @var bool Resolved state of captcha */
protected bool $solved = false;
/** @var string Confirm code */
protected string $confirm_code = '';
/** @var string Confirm id hash */
protected string $confirm_id = '';
/**
* Constructor for abstract captcha base class
*
* @param driver_interface $db
*/
public function __construct(driver_interface $db)
{
$this->db = $db;
}
/**
* @inheritDoc
*/
public function garbage_collect(int $confirm_type = 0): void
{
$sql = 'SELECT DISTINCT c.session_id
FROM ' . CONFIRM_TABLE . ' c
LEFT JOIN ' . SESSIONS_TABLE . ' s ON (c.session_id = s.session_id)
WHERE s.session_id IS NULL' .
((empty($type)) ? '' : ' AND c.confirm_type = ' . (int) $type);
$result = $this->db->sql_query($sql);
if ($row = $this->db->sql_fetchrow($result))
{
$sql_in = [];
do
{
$sql_in[] = (string) $row['session_id'];
}
while ($row = $this->db->sql_fetchrow($result));
if (count($sql_in))
{
$sql = 'DELETE FROM ' . CONFIRM_TABLE . '
WHERE ' . $this->db->sql_in_set('session_id', $sql_in);
$this->db->sql_query($sql);
}
}
$this->db->sql_freeresult($result);
}
}

View file

@ -14,13 +14,15 @@
namespace phpbb\captcha\plugins; namespace phpbb\captcha\plugins;
use phpbb\config\config; use phpbb\config\config;
use phpbb\db\driver\driver;
use phpbb\db\driver\driver_interface;
use phpbb\language\language; use phpbb\language\language;
use phpbb\log\log_interface; use phpbb\log\log_interface;
use phpbb\request\request_interface; use phpbb\request\request_interface;
use phpbb\template\template; use phpbb\template\template;
use phpbb\user; use phpbb\user;
class turnstile implements plugin_interface class turnstile extends base
{ {
private const API_ENDPOINT = 'https://api.cloudflare.com/client/v4/captcha/validate'; private const API_ENDPOINT = 'https://api.cloudflare.com/client/v4/captcha/validate';
@ -49,14 +51,17 @@ class turnstile implements plugin_interface
* Constructor for turnstile captcha plugin * Constructor for turnstile captcha plugin
* *
* @param config $config * @param config $config
* @param driver_interface $db
* @param language $language * @param language $language
* @param log_interface $log * @param log_interface $log
* @param request_interface $request * @param request_interface $request
* @param template $template * @param template $template
* @param user $user * @param user $user
*/ */
public function __construct(config $config, language $language, log_interface $log, request_interface $request, template $template, user $user) public function __construct(config $config, driver_interface $db, language $language, log_interface $log, request_interface $request, template $template, user $user)
{ {
parent::__construct($db);
$this->config = $config; $this->config = $config;
$this->language = $language; $this->language = $language;
$this->log = $log; $this->log = $log;
@ -103,7 +108,7 @@ class turnstile implements plugin_interface
// Required for posting page to store solved state // Required for posting page to store solved state
if ($this->solved) if ($this->solved)
{ {
$hidden_fields['confirm_code'] = $this->code; $hidden_fields['confirm_code'] = $this->confirm_code;
} }
$hidden_fields['confirm_id'] = $this->confirm_id; $hidden_fields['confirm_id'] = $this->confirm_id;
return $hidden_fields; return $hidden_fields;