From f9a0e4d6065eb216822e103f658d2bc67ab28dec Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Sun, 30 Jul 2023 18:27:25 +0200 Subject: [PATCH] [ticket/9687] Separate querie for ban options of users PHPBB3-9687 --- phpBB/phpbb/ban/type/base.php | 4 ++-- phpBB/phpbb/ban/type/user.php | 37 +++++++++++++++++++++++++---------- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/phpBB/phpbb/ban/type/base.php b/phpBB/phpbb/ban/type/base.php index cb442b983d..92b324f9b3 100644 --- a/phpBB/phpbb/ban/type/base.php +++ b/phpBB/phpbb/ban/type/base.php @@ -50,6 +50,7 @@ abstract class base implements type_interface public function __construct(driver_interface $db, string $bans_table, string $users_table, string $sessions_table, string $sessions_keys_table) { $this->db = $db; + $this->bans_table = $bans_table; $this->users_table = $users_table; $this->sessions_table = $sessions_table; $this->sessions_keys_table = $sessions_keys_table; @@ -109,9 +110,8 @@ abstract class base implements type_interface */ public function get_ban_options(): array { - // @todo replace table constant by string $sql = 'SELECT * - FROM ' . BANS_TABLE . ' + FROM ' . $this->bans_table . ' WHERE (ban_end >= ' . time() . " OR ban_end = 0) AND ban_mode = '{$this->get_type()}' diff --git a/phpBB/phpbb/ban/type/user.php b/phpBB/phpbb/ban/type/user.php index da07b2c16b..7dadbf2e43 100644 --- a/phpBB/phpbb/ban/type/user.php +++ b/phpBB/phpbb/ban/type/user.php @@ -73,23 +73,40 @@ class user extends base public function get_ban_options(): array { $ban_options = []; + $ban_data = []; + $user_ids = []; - // @todo replace table constant by string - $sql = 'SELECT b.*, u.user_id, u.username, u.username_clean - FROM ' . BANS_TABLE . ' b, ' . $this->users_table . ' u - WHERE (b.ban_end >= ' . time() . " - OR b.ban_end = 0) - AND b.ban_mode = '{$this->get_type()}' - AND u.user_id = b.ban_item - ORDER BY u.username_clean ASC"; + $sql = 'SELECT b.* + FROM ' . $this->bans_table . ' b + WHERE (b.ban_end >= ' . time() . " + OR b.ban_end = 0) + AND b.ban_mode = '{$this->get_type()}'"; $result = $this->db->sql_query($sql); while ($row = $this->db->sql_fetchrow($result)) { - $row['ban_item'] = $row['username'] ?: $row['ban_item']; - $ban_options[] = $row; + $user_ids[] = $row['ban_item']; + $ban_data[$row['ban_item']] = $row; } $this->db->sql_freeresult($result); + if (count($user_ids)) + { + // Grab usernames for banned user IDs + $sql = 'SELECT user_id, username, username_clean + FROM ' . $this->users_table . ' + WHERE ' . $this->db->sql_in_set('user_id', $user_ids) . ' + ORDER BY username_clean'; + $result = $this->db->sql_query($sql); + while ($row = $this->db->sql_fetchrow($result)) + { + $ban_options[] = array_merge( + $ban_data[$row['user_id']], + $row + ); + } + $this->db->sql_freeresult($result); + } + return $ban_options; }