From c97582b1c7f2976da19ed1a87311a4bea6e20d72 Mon Sep 17 00:00:00 2001 From: Tadhg Boyle Date: Wed, 18 Jan 2023 10:00:48 -0800 Subject: [PATCH 1/3] Fix MySQL `IN` clause string interpolation in Forum `getLatestDiscussions` --- modules/Forum/classes/Forum.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/Forum/classes/Forum.php b/modules/Forum/classes/Forum.php index 08fa5e19c8..98883bba60 100644 --- a/modules/Forum/classes/Forum.php +++ b/modules/Forum/classes/Forum.php @@ -316,9 +316,9 @@ public function getLatestDiscussions(array $groups = [0], int $user_id = 0, int $own_topics_forums_string = rtrim($own_topics_forums_string, ','); return DB::getInstance()->query( - "(SELECT topics.id as id, topics.forum_id as forum_id, topics.topic_title as topic_title, topics.topic_creator as topic_creator, topics.topic_last_user as topic_last_user, topics.topic_date as topic_date, topics.topic_reply_date as topic_reply_date, topics.topic_views as topic_views, topics.locked as locked, topics.sticky as sticky, topics.label as label, topics.deleted as deleted, posts.id as last_post_id FROM nl2_topics topics LEFT JOIN nl2_posts posts ON topics.id = posts.topic_id AND posts.id = (SELECT MAX(id) FROM nl2_posts p WHERE p.topic_id = topics.id AND p.deleted = 0) WHERE topics.deleted = 0 AND topics.forum_id IN (' . $all_topics_forums_string . ') ORDER BY topics.topic_reply_date DESC LIMIT $limit) + "(SELECT topics.id as id, topics.forum_id as forum_id, topics.topic_title as topic_title, topics.topic_creator as topic_creator, topics.topic_last_user as topic_last_user, topics.topic_date as topic_date, topics.topic_reply_date as topic_reply_date, topics.topic_views as topic_views, topics.locked as locked, topics.sticky as sticky, topics.label as label, topics.deleted as deleted, posts.id as last_post_id FROM nl2_topics topics LEFT JOIN nl2_posts posts ON topics.id = posts.topic_id AND posts.id = (SELECT MAX(id) FROM nl2_posts p WHERE p.topic_id = topics.id AND p.deleted = 0) WHERE topics.deleted = 0 AND topics.forum_id IN ($all_topics_forums_string) ORDER BY topics.topic_reply_date DESC LIMIT $limit) UNION - (SELECT topics.id as id, topics.forum_id as forum_id, topics.topic_title as topic_title, topics.topic_creator as topic_creator, topics.topic_last_user as topic_last_user, topics.topic_date as topic_date, topics.topic_reply_date as topic_reply_date, topics.topic_views as topic_views, topics.locked as locked, topics.sticky as sticky, topics.label as label, topics.deleted as deleted, posts.id as last_post_id FROM nl2_topics topics LEFT JOIN nl2_posts posts ON topics.id = posts.topic_id AND posts.id = (SELECT MAX(id) FROM nl2_posts p WHERE p.topic_id = topics.id AND p.deleted = 0) WHERE topics.deleted = 0 AND ((topics.forum_id IN (' . $own_topics_forums_string . ') AND topics.topic_creator = ?) OR topics.sticky = 1) ORDER BY topics.topic_reply_date DESC LIMIT $limit) + (SELECT topics.id as id, topics.forum_id as forum_id, topics.topic_title as topic_title, topics.topic_creator as topic_creator, topics.topic_last_user as topic_last_user, topics.topic_date as topic_date, topics.topic_reply_date as topic_reply_date, topics.topic_views as topic_views, topics.locked as locked, topics.sticky as sticky, topics.label as label, topics.deleted as deleted, posts.id as last_post_id FROM nl2_topics topics LEFT JOIN nl2_posts posts ON topics.id = posts.topic_id AND posts.id = (SELECT MAX(id) FROM nl2_posts p WHERE p.topic_id = topics.id AND p.deleted = 0) WHERE topics.deleted = 0 AND ((topics.forum_id IN ($own_topics_forums_string) AND topics.topic_creator = ?) OR topics.sticky = 1) ORDER BY topics.topic_reply_date DESC LIMIT $limit) ORDER BY topic_reply_date DESC LIMIT $limit", [$user_id], true @@ -329,7 +329,7 @@ public function getLatestDiscussions(array $groups = [0], int $user_id = 0, int "SELECT topics.id as id, topics.forum_id as forum_id, topics.topic_title as topic_title, topics.topic_creator as topic_creator, topics.topic_last_user as topic_last_user, topics.topic_date as topic_date, topics.topic_reply_date as topic_reply_date, topics.topic_views as topic_views, topics.locked as locked, topics.sticky as sticky, topics.label as label, topics.deleted as deleted, posts.id as last_post_id FROM nl2_topics topics LEFT JOIN nl2_posts posts ON topics.id = posts.topic_id AND posts.id = (SELECT MAX(id) FROM nl2_posts p WHERE p.topic_id = topics.id AND p.deleted = 0) - WHERE topics.deleted = 0 AND topics.forum_id IN (' . $all_topics_forums_string . ') ORDER BY topics.topic_reply_date DESC LIMIT $limit", + WHERE topics.deleted = 0 AND topics.forum_id IN ($all_topics_forums_string) ORDER BY topics.topic_reply_date DESC LIMIT $limit", )->results(); } From 30c86b67c687e45dddf19369f45e124dca021bc2 Mon Sep 17 00:00:00 2001 From: Tadhg Boyle Date: Wed, 18 Jan 2023 10:11:42 -0800 Subject: [PATCH 2/3] Fix $limit being applied --- modules/Forum/widgets/LatestPostsWidget.php | 25 +++++++-------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/modules/Forum/widgets/LatestPostsWidget.php b/modules/Forum/widgets/LatestPostsWidget.php index da341eb965..03f4f2d708 100644 --- a/modules/Forum/widgets/LatestPostsWidget.php +++ b/modules/Forum/widgets/LatestPostsWidget.php @@ -50,25 +50,18 @@ public function initialise(): void { $user_groups = $this->_user->getAllGroupIds(); $this->_cache->setCache('forum_discussions_' . rtrim(implode('-', $user_groups), '-')); - if ($this->_cache->isCached('discussions')) { - $template_array = $this->_cache->retrieve('discussions'); - - } else { +// if ($this->_cache->isCached('discussions')) { +// $template_array = $this->_cache->retrieve('discussions'); +// +// } else { + $limit = (int) Util::getSetting('latest_posts_limit', 5, 'Forum'); // Generate latest posts - $discussions = $forum->getLatestDiscussions($user_groups, ($this->_user->isLoggedIn() ? $this->_user->data()->id : 0), 5); - - $n = 0; - // Calculate the number of discussions to display - $limit = Util::getSetting('latest_posts_limit', 5, 'Forum'); - if (count($discussions) <= $limit) { - $limit = count($discussions); - } + $discussions = $forum->getLatestDiscussions($user_groups, ($this->_user->isLoggedIn() ? $this->_user->data()->id : 0), $limit); $template_array = []; // Generate an array to pass to template - while ($n < $limit) { - $discussion = $discussions[$n]; + foreach ($discussions as $discussion) { // Get the name of the forum from the ID $forum_name = $db->get('forums', ['id', $discussion->forum_id])->results(); $forum_name = Output::getPurified($forum_name[0]->forum_title); @@ -128,12 +121,10 @@ public function initialise(): void { 'last_reply_profile_link' => $last_reply_user->getProfileURL(), 'last_reply_link' => URL::build('/forum/topic/' . $discussion->id . '-' . $forum->titleToURL($discussion->topic_title), 'pid=' . $discussion->last_post_id) ]; - - $n++; } $this->_cache->store('discussions', $template_array, 60); - } + // } // Generate HTML code for widget $this->_smarty->assign('LATEST_POSTS_ARRAY', $template_array); From 4f6d0842ec15f92e6d237a011cd398d19cf29040 Mon Sep 17 00:00:00 2001 From: Tadhg Boyle Date: Wed, 18 Jan 2023 10:13:53 -0800 Subject: [PATCH 3/3] Remove comments --- modules/Forum/widgets/LatestPostsWidget.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/Forum/widgets/LatestPostsWidget.php b/modules/Forum/widgets/LatestPostsWidget.php index 03f4f2d708..8785584caf 100644 --- a/modules/Forum/widgets/LatestPostsWidget.php +++ b/modules/Forum/widgets/LatestPostsWidget.php @@ -50,10 +50,10 @@ public function initialise(): void { $user_groups = $this->_user->getAllGroupIds(); $this->_cache->setCache('forum_discussions_' . rtrim(implode('-', $user_groups), '-')); -// if ($this->_cache->isCached('discussions')) { -// $template_array = $this->_cache->retrieve('discussions'); -// -// } else { + if ($this->_cache->isCached('discussions')) { + $template_array = $this->_cache->retrieve('discussions'); + + } else { $limit = (int) Util::getSetting('latest_posts_limit', 5, 'Forum'); // Generate latest posts $discussions = $forum->getLatestDiscussions($user_groups, ($this->_user->isLoggedIn() ? $this->_user->data()->id : 0), $limit); @@ -124,7 +124,7 @@ public function initialise(): void { } $this->_cache->store('discussions', $template_array, 60); - // } + } // Generate HTML code for widget $this->_smarty->assign('LATEST_POSTS_ARRAY', $template_array);