From 01dd0b168a5829fe4f5c3cfff31828ca7fcceaf1 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Sun, 13 Oct 2024 08:53:11 +0200 Subject: [PATCH] [ticket/17414] Create abstract base class for captchas PHPBB-17414 --- .../default/container/services_captcha.yml | 1 + phpBB/phpbb/captcha/plugins/base.php | 62 +++++++++++++++++++ phpBB/phpbb/captcha/plugins/turnstile.php | 11 +++- 3 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 phpBB/phpbb/captcha/plugins/base.php diff --git a/phpBB/config/default/container/services_captcha.yml b/phpBB/config/default/container/services_captcha.yml index 09742f2eb8..d4f1ad863a 100644 --- a/phpBB/config/default/container/services_captcha.yml +++ b/phpBB/config/default/container/services_captcha.yml @@ -60,6 +60,7 @@ services: shared: false arguments: - '@config' + - '@dbal.conn' - '@language' - '@log' - '@request' diff --git a/phpBB/phpbb/captcha/plugins/base.php b/phpBB/phpbb/captcha/plugins/base.php new file mode 100644 index 0000000000..2b07ccaa64 --- /dev/null +++ b/phpBB/phpbb/captcha/plugins/base.php @@ -0,0 +1,62 @@ +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); + } +} diff --git a/phpBB/phpbb/captcha/plugins/turnstile.php b/phpBB/phpbb/captcha/plugins/turnstile.php index 0bb731d147..210ae03610 100644 --- a/phpBB/phpbb/captcha/plugins/turnstile.php +++ b/phpBB/phpbb/captcha/plugins/turnstile.php @@ -14,13 +14,15 @@ namespace phpbb\captcha\plugins; use phpbb\config\config; +use phpbb\db\driver\driver; +use phpbb\db\driver\driver_interface; use phpbb\language\language; use phpbb\log\log_interface; use phpbb\request\request_interface; use phpbb\template\template; use phpbb\user; -class turnstile implements plugin_interface +class turnstile extends base { 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 * * @param config $config + * @param driver_interface $db * @param language $language * @param log_interface $log * @param request_interface $request * @param template $template * @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->language = $language; $this->log = $log; @@ -103,7 +108,7 @@ class turnstile implements plugin_interface // Required for posting page to store solved state if ($this->solved) { - $hidden_fields['confirm_code'] = $this->code; + $hidden_fields['confirm_code'] = $this->confirm_code; } $hidden_fields['confirm_id'] = $this->confirm_id; return $hidden_fields;