From 4c1848d751c1fd2b5d4b91576b633cc4dc189d50 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 26 Oct 2026 12:37:34 +0000 Subject: [PATCH 1/5] Phase 6: background tasks Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01MZy45V5RV9nx6Buq8vqKHr --- CHANGELOG.md | 1 + README.md | 2 +- src/class/Cleanup.php | 35 +++ src/class/DigestMailer.php | 178 ++++++++++++ src/class/Notifier.php | 10 + src/class/PostHandler.php | 20 ++ src/class/ReadmarkHandler.php | 47 +++- src/class/Settings.php | 1 + src/class/SuspensionHandler.php | 9 + src/icms_version.php | 24 +- src/include/autotask_cleanup.php | 17 ++ src/include/autotask_digest.php | 17 ++ .../english/mail_template/global_digest.tpl | 17 ++ src/language/english/main.php | 3 + src/language/english/modinfo.php | 8 + .../mail_template/global_digest.tpl | 17 ++ src/language/nederlands/main.php | 3 + src/language/nederlands/modinfo.php | 8 + tests/Integration/BackgroundTasksTest.php | 263 ++++++++++++++++++ 19 files changed, 675 insertions(+), 5 deletions(-) create mode 100644 src/class/Cleanup.php create mode 100644 src/class/DigestMailer.php create mode 100644 src/include/autotask_cleanup.php create mode 100644 src/include/autotask_digest.php create mode 100644 src/language/english/mail_template/global_digest.tpl create mode 100644 src/language/nederlands/mail_template/global_digest.tpl create mode 100644 tests/Integration/BackgroundTasksTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 4aaf7c6..47e4837 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,3 +10,4 @@ While the module is in development the status carries the phase: 1.0.0 Alpha 5 i - Phase 3: reading. `index.php`, `forum.php`, `topic.php` (flat and threaded), `viewall.php` (latest, unread, featured), `archive.php`, `rss.php`, search, who is online, signatures, ranks and the poster bar, read marks per topic and per forum ("mark all as read" is a POST with a token), karma and required-reply locks that hide the text until unlocked, three blocks (recent, hot, top posters) and six preferences. Permissions are checked in one `PermissionService`: category access, forum access and view, switched-off forums for moderators and webmasters only. Our own pager replaces the core's, which prints `PHP_SELF` unescaped. Integration tests cover the permission checks, read marks, thread order, karma, online tracking, the archive and search. - Phase 4: writing. `post.php` starts topics, replies, edits and deletes through one `PostService` that holds all rules: the permissions forum_post, forum_reply, forum_edit, forum_delete, forum_attach and forum_noapprove, suspensions, a flood limit, a time limit for members, locked topics, what the forum allows (HTML, signature, topic prefix), approval and the edit history. Members delete only their own last post of a topic. The form is built on the IPF secure form, with a preview, a disclaimer and a captcha for guests. Attachments go through `Uploader` (extension list per forum, a list of extensions that is never accepted, size limit, a random file name without extension in a folder the web server cannot reach) and are served by `attachment.php`, which checks the read permission and the karma lock and always sends a download. Subscriptions use the core notification system for topics, forums and all forums; the mail is only sent to members who may read the forum and never holds the text of a post. Post text is now stored as typed (`XOBJ_DTYPE_OTHER`): the IPF textarea type ran every text through HTMLPurifier when saving, which turned BBCode into HTML for good and made the HTML, smiley and line break switches of a post meaningless. The text is made safe when it is shown. The welcome post is left for a later release: the core has no event for a new registration to hook it on. - Phase 5: moderation. `moderate.php` and one `ModerationService` that holds all rules: lock and unlock, pin, feature, move (the moderator must moderate both forums), merge (counters, last post, views and read marks follow), split from a post onward (replies are re-attached to the new first post), delete a topic, approve waiting posts (subscribers are told only then), a queue of waiting posts and a list of open reports per forum, and suspensions with a number of days (reading stays possible; moderators and webmasters cannot be suspended). `report.php` lets members report a post, one open report per member and post. Every action is a confirmation form followed by a POST with a security token. Restoring pruned topics from the archive stays an admin task for now. Integration tests cover each action, the counters after merge and split, and a suspended member who can read but not write. +- Phase 6: background tasks. Two autotasks, registered by the core when the module is installed or updated: the digest mail and a cleanup. Members subscribe to the digest with the notification "Digest" for all forums. `DigestMailer` covers the time since the last digest (one day for the first), lists titles, counts and links but never the text of a post, sends each member only the forums that member may read, and stores the complete digest for the administrators before it sends, so a failure halfway never causes a second mail. The cleanup removes visitors that are no longer online, read marks older than a preference (90 days; 0 keeps them) and suspensions that ended more than 30 days ago. A topic without a read mark counts as unread, so posts older than the limit first count as read for members who have marks, and only then the old marks go. diff --git a/README.md b/README.md index 5d22073..6f78741 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Community forum module for ImpressCMS 2.0, built on the ImpressCMS Persistable Framework (IPF). imforum is the successor of iForum: same features, new code, and an importer that takes over the data of a running iForum installation. -Status: phase 5 (moderation). Members start topics, reply (with a quick reply under the topic), quote, edit and delete their own posts, attach files and follow topics and forums by notification; guests can post where the group permissions allow it, behind a captcha. Posts wait for a moderator when the group lacks forum_noapprove. Moderators of a forum lock, pin and feature topics, move, merge and split them, delete topics, approve waiting posts, handle reports from members and suspend members, all from the front end through `moderate.php`; members report posts through `report.php`. Categories, forums, moderators, permissions, reports, suspensions, digests, the archive and pruning are managed from the admin area. +Status: phase 6 (background tasks), module version 1.0.0 Alpha 6. Members start topics, reply (with a quick reply under the topic), quote, edit and delete their own posts, attach files and follow topics and forums by notification; guests can post where the group permissions allow it, behind a captcha. Posts wait for a moderator when the group lacks forum_noapprove. Moderators of a forum lock, pin and feature topics, move, merge and split them, delete topics, approve waiting posts, handle reports from members and suspend members, all from the front end through `moderate.php`; members report posts through `report.php`. Two autotasks (Systeem > Autotasks, daily by default) send a digest of new topics and posts to members who subscribe to it and clean up old read marks, the visitors list and ended suspensions. Categories, forums, moderators, permissions, reports, suspensions, digests, the archive and pruning are managed from the admin area. ## Requirements diff --git a/src/class/Cleanup.php b/src/class/Cleanup.php new file mode 100644 index 0000000..34c9ca6 --- /dev/null +++ b/src/class/Cleanup.php @@ -0,0 +1,35 @@ +purgeOlderThan($now - $minutes * 60); + + // 0 days: read marks never expire + $days = (int) mod_imforum_Settings::get('read_expire_days'); + $marks = $days > 0 ? icms_getModuleHandler('readmark', IMFORUM_DIRNAME)->purgeOlderThan($now - $days * 86400) : 0; + + $ended = icms_getModuleHandler('suspension', IMFORUM_DIRNAME)->purgeEndedBefore($now - self::KEEP_SUSPENSIONS_DAYS * 86400); + return ['online' => $online, 'read_marks' => $marks, 'suspensions' => $ended]; + } +} diff --git a/src/class/DigestMailer.php b/src/class/DigestMailer.php new file mode 100644 index 0000000..8108497 --- /dev/null +++ b/src/class/DigestMailer.php @@ -0,0 +1,178 @@ +notifier = $notifier; + } + + /** + * What happened between two moments, per forum + * + * @return array> by forum id, most recent activity first within a forum + */ + public function collect(int $since, int $until): array { + $found = icms_getModuleHandler('post', IMFORUM_DIRNAME)->getActivity($since, $until); + if (!$found) { + return []; + } + + $topicObjects = icms_getModuleHandler('topic', IMFORUM_DIRNAME)->getByIds(array_column($found, 1)); + $byForum = []; + foreach ($found as [$forumId, $topicId, $count, $firstNew]) { + if (!isset($topicObjects[$topicId])) { + continue; + } + $byForum[$forumId][] = [ + 'topic_id' => $topicId, + 'title' => trim((string) preg_replace('/\s+/', ' ', (string) $topicObjects[$topicId]->getVar('topic_title', 'n'))), + 'posts' => $count, + 'is_new' => (int) $topicObjects[$topicId]->getVar('topic_first_post_id', 'e') === $firstNew, + ]; + } + return $byForum; + } + + /** + * The mail text for some forums; plain text with links + * + * @param array> $byForum + * @param list $forumIds the forums that may appear, in the order they are listed + * @param array $forumNames by forum id + * @return array{text: string, topics: int} + */ + public static function render(array $byForum, array $forumIds, array $forumNames): array { + $lines = []; + $topicCount = 0; + $left = 0; + foreach ($forumIds as $forumId) { + if (empty($byForum[$forumId])) { + continue; + } + $section = []; + foreach ($byForum[$forumId] as $topic) { + $topicCount++; + if ($topicCount > self::MAX_TOPICS) { + $left++; + continue; + } + $section[] = '* ' . $topic['title'] . ' (' . ($topic['is_new'] ? _MD_IMFORUM_DIGEST_NEW_TOPIC . ', ' : '') + . sprintf(_MD_IMFORUM_DIGEST_POSTS, $topic['posts']) . ')' . "\n " + . mod_imforum_View::url('topic', ['topic' => $topic['topic_id']]); + } + if ($section) { + $lines[] = '== ' . ($forumNames[$forumId] ?? '#' . $forumId) . ' =='; + $lines = array_merge($lines, $section, ['']); + } + } + if ($left > 0) { + $lines[] = sprintf(_MD_IMFORUM_DIGEST_MORE, $left) . ' ' . mod_imforum_View::url('viewall'); + } + return ['text' => rtrim(implode("\n", $lines)), 'topics' => $topicCount]; + } + + /** + * Build and send the digest for the time since the last one + * + * The digest is stored before the mails go out, so a failure halfway never + * leads to a second mail for the same period. + * + * @param (callable(int): list)|null $groupsOf the groups of a member; the core member handler by default + * @return array{topics: int, recipients: int, digest_id: int} + */ + public function run(?int $now = null, ?callable $groupsOf = null): array { + $now ??= time(); + $digests = icms_getModuleHandler('digest', IMFORUM_DIRNAME); + $last = $digests->getLastDigestTime(); + $since = $last > 0 ? $last : $now - self::FIRST_PERIOD; + $empty = ['topics' => 0, 'recipients' => 0, 'digest_id' => 0]; + if ($since >= $now) { + return $empty; + } + + $byForum = $this->collect($since, $now); + if (!$byForum) { + return $empty; + } + + $permissions = new mod_imforum_PermissionService([(int) ICMS_GROUP_ADMIN], 1, true); + $forums = $permissions->getAllForums(); + $forumNames = []; + foreach ($forums as $forumId => $forum) { + $forumNames[(int) $forumId] = (string) $forum->getVar('forum_name', 'n'); + } + $allForumIds = array_map('intval', array_keys($forums)); + $all = self::render($byForum, $allForumIds, $forumNames); + + $digest = $digests->create(); + $digest->setVar('digest_time', $now); + $digest->setVar('digest_content', $all['text']); + if (!$digests->insert($digest, true)) { + return $empty; + } + + $notifier = $this->notifier ?? new mod_imforum_Notifier(); + $groupsOf ??= static fn (int $uid): array => array_map('intval', (array) icms::handler('icms_member')->getGroupsByUser($uid)); + $readable = []; + $byContent = []; + foreach ($notifier->getGlobalSubscribers('digest') as $uid) { + $groups = $groupsOf($uid); + sort($groups); + $key = implode(',', $groups); + if (!isset($readable[$key])) { + $visitor = new mod_imforum_PermissionService($groups, $uid, in_array((int) ICMS_GROUP_ADMIN, $groups, true)); + $readable[$key] = array_values(array_intersect($allForumIds, $visitor->getReadableForumIds())); + } + $mail = self::render($byForum, $readable[$key], $forumNames); + if ($mail['topics'] > 0) { + $byContent[md5($mail['text'])]['text'] = $mail['text']; + $byContent[md5($mail['text'])]['uids'][] = $uid; + } + } + + $recipients = 0; + foreach ($byContent as $content) { + $recipients += count($content['uids']); + $this->send($content['text'], $content['uids'], $since, $now); + } + return ['topics' => $all['topics'], 'recipients' => $recipients, 'digest_id' => (int) $digest->id()]; + } + + /** + * @param list $uids + */ + protected function send(string $text, array $uids, int $since, int $until): void { + $moduleId = (int) icms_getModuleHandler('forum', IMFORUM_DIRNAME)->getModuleInfo()->getVar('mid'); + icms::handler('icms_data_notification')->triggerEvent('global', 0, 'digest', [ + 'X_DIGEST_TEXT' => $text, + 'X_DIGEST_FROM' => (string) formatTimestamp($since, 'm'), + 'X_DIGEST_TO' => (string) formatTimestamp($until, 'm'), + 'X_DIGEST_URL' => mod_imforum_View::url('viewall'), + ], $uids, $moduleId, 0); + } +} diff --git a/src/class/Notifier.php b/src/class/Notifier.php index 919e53d..d73507e 100644 --- a/src/class/Notifier.php +++ b/src/class/Notifier.php @@ -80,6 +80,16 @@ public function filterReaders(array $uids, mod_imforum_Forum $forum, ?callable $ return $readers; } + /** + * Members who follow all forums for an event of the global category + * + * @return list + */ + public function getGlobalSubscribers(string $event): array { + $moduleId = (int) icms_getModuleHandler('forum', IMFORUM_DIRNAME)->getModuleInfo()->getVar('mid'); + return array_values(array_unique($this->getSubscribers($moduleId, 'global', 0, $event))); + } + /** * The category and event are names from icms_version.php, never request input * diff --git a/src/class/PostHandler.php b/src/class/PostHandler.php index efc5611..a4b88c2 100644 --- a/src/class/PostHandler.php +++ b/src/class/PostHandler.php @@ -56,6 +56,26 @@ private function approvedIn(int $topicId): icms_db_criteria_Compo { return $criteria; } + /** + * Approved posts written in a period, counted per topic, for the digest + * + * @return list forum id, topic id, number of posts, id of the first new post; by forum, busiest topic first + */ + public function getActivity(int $since, int $until): array { + $topics = $this->db->prefix(IMFORUM_DIRNAME . '_topic'); + $result = $this->db->queryF( + 'SELECT p.forum_id, p.topic_id, COUNT(*) AS n, MIN(p.post_id) AS first_new, MAX(p.post_id) AS last_new' + . " FROM {$this->table} p JOIN $topics t ON t.topic_id = p.topic_id" + . ' WHERE p.approved = 1 AND t.approved = 1 AND p.post_time >= ' . $since . ' AND p.post_time < ' . $until + . ' GROUP BY p.forum_id, p.topic_id ORDER BY p.forum_id, last_new DESC' + ); + $rows = []; + while ($result && ($row = $this->db->fetchRow($result))) { + $rows[] = [(int) $row[0], (int) $row[1], (int) $row[2], (int) $row[3]]; + } + return $rows; + } + /** * One page of the approved posts of a topic, oldest first * diff --git a/src/class/ReadmarkHandler.php b/src/class/ReadmarkHandler.php index afdcb45..d62992e 100644 --- a/src/class/ReadmarkHandler.php +++ b/src/class/ReadmarkHandler.php @@ -72,9 +72,52 @@ public function deleteByItem(int $type, int $itemId): bool { /** * Drop read marks older than a moment; used by the cleanup task (phase 6) + * + * A topic without a mark counts as unread, so deleting marks by age alone + * would turn every old topic into an unread one. Posts older than the + * moment are therefore counted as read first: a member who has marks in a + * forum gets a forum mark up to the newest post from before the moment. + * Then the old marks go, together with the marks of topics that no longer + * exist and the topic marks the forum mark already covers. + * + * @return int the number of topic marks removed */ - public function purgeOlderThan(int $time): bool { - return (bool) $this->db->queryF(sprintf('DELETE FROM %s WHERE read_time < %d', $this->table, $time)); + public function purgeOlderThan(int $time): int { + $posts = $this->db->prefix(IMFORUM_DIRNAME . '_post'); + $topics = $this->db->prefix(IMFORUM_DIRNAME . '_topic'); + $topicType = self::TYPE_TOPIC; + $forumType = self::TYPE_FORUM; + + $newestOld = []; + $result = $this->db->queryF("SELECT forum_id, MAX(post_id) FROM $posts WHERE approved = 1 AND post_time < $time GROUP BY forum_id"); + while ($result && ($row = $this->db->fetchRow($result))) { + $newestOld[(int) $row[0]] = (int) $row[1]; + } + + $pairs = []; + $result = $this->db->queryF("SELECT DISTINCT m.uid, t.forum_id FROM {$this->table} m JOIN $topics t ON t.topic_id = m.item_id WHERE m.read_type = $topicType AND m.read_time < $time"); + while ($result && ($row = $this->db->fetchRow($result))) { + $pairs[] = [(int) $row[0], (int) $row[1]]; + } + foreach ($pairs as [$uid, $forumId]) { + $upTo = $newestOld[$forumId] ?? 0; + $seen = $this->getLastRead($forumType, $uid, [$forumId])[$forumId] ?? 0; + if ($upTo > $seen) { + $this->markRead($forumType, $uid, $forumId, $upTo); + } + } + + $removed = 0; + foreach ([ + "DELETE FROM {$this->table} WHERE read_type = $topicType AND read_time < $time", + "DELETE m FROM {$this->table} m LEFT JOIN $topics t ON t.topic_id = m.item_id WHERE m.read_type = $topicType AND t.topic_id IS NULL", + "DELETE m FROM {$this->table} m JOIN $topics t ON t.topic_id = m.item_id JOIN {$this->table} f ON f.read_type = $forumType AND f.uid = m.uid AND f.item_id = t.forum_id WHERE m.read_type = $topicType AND m.post_id <= f.post_id", + ] as $sql) { + if ($this->db->queryF($sql)) { + $removed += max(0, (int) $this->db->getAffectedRows()); + } + } + return $removed; } /** diff --git a/src/class/Settings.php b/src/class/Settings.php index fb2c2f7..e75867d 100644 --- a/src/class/Settings.php +++ b/src/class/Settings.php @@ -32,6 +32,7 @@ class mod_imforum_Settings { 'subject_prefix' => '', 'disclaimer_show' => 0, 'disclaimer' => '', + 'read_expire_days' => 90, ]; /** @var array|null */ diff --git a/src/class/SuspensionHandler.php b/src/class/SuspensionHandler.php index e8f86d5..1a094ab 100644 --- a/src/class/SuspensionHandler.php +++ b/src/class/SuspensionHandler.php @@ -47,6 +47,15 @@ public function isSuspended(int $uid, string $ip, int $forumId, ?int $now = null return $this->getCount($where) > 0; } + /** + * Remove suspensions that ended before a moment + */ + public function purgeEndedBefore(int $time): int { + $count = $this->getCount(new icms_db_criteria_Item('end_time', $time, '<')); + $this->deleteAll(new icms_db_criteria_Item('end_time', $time, '<')); + return (int) $count; + } + protected function beforeInsert(mod_imforum_Suspension $suspension): bool { if ((int) $suspension->getVar('start_time', 'e') === 0) { $suspension->setVar('start_time', time()); diff --git a/src/icms_version.php b/src/icms_version.php index 6d88f43..ab3c9cd 100644 --- a/src/icms_version.php +++ b/src/icms_version.php @@ -19,8 +19,8 @@ $modversion = [ 'name' => _MI_IMFORUM_NAME, 'version' => '1.0.0', - 'status' => 'Alpha 5', - 'status_version' => '1.0.0 Alpha 5', + 'status' => 'Alpha 6', + 'status_version' => '1.0.0 Alpha 6', 'date' => '25 October 2026', 'description' => _MI_IMFORUM_DESC, 'author' => 'fiammybe', @@ -60,6 +60,13 @@ 'search' => ['file' => 'include/search.inc.php', 'func' => 'imforum_search'], 'hasComments' => 0, 'hasNotification' => 1, + // The core adds these to Systeem -> Autotasks when the module is installed or + // updated; the interval is in minutes and can be changed there. + 'autotasks' => [ + ['enabled' => true, 'name' => _MI_IMFORUM_TASK_DIGEST, 'code' => 'include/autotask_digest.php', 'interval' => 1440], + ['enabled' => true, 'name' => _MI_IMFORUM_TASK_CLEANUP, 'code' => 'include/autotask_cleanup.php', 'interval' => 1440], + ], + 'notification' => [ 'lookup_file' => 'include/notification.inc.php', 'lookup_func' => 'imforum_notify_iteminfo', @@ -108,6 +115,11 @@ 'title' => _MI_IMFORUM_NOTIFY_GLOBAL_NEWTHREAD, 'caption' => _MI_IMFORUM_NOTIFY_GLOBAL_NEWTHREAD_CAP, 'description' => _MI_IMFORUM_NOTIFY_GLOBAL_NEWTHREAD_DSC, 'mail_template' => 'global_newthread', 'mail_subject' => _MI_IMFORUM_NOTIFY_GLOBAL_NEWTHREAD_SBJ, ], + [ + 'name' => 'digest', 'category' => 'global', + 'title' => _MI_IMFORUM_NOTIFY_GLOBAL_DIGEST, 'caption' => _MI_IMFORUM_NOTIFY_GLOBAL_DIGEST_CAP, + 'description' => _MI_IMFORUM_NOTIFY_GLOBAL_DIGEST_DSC, 'mail_template' => 'global_digest', 'mail_subject' => _MI_IMFORUM_NOTIFY_GLOBAL_DIGEST_SBJ, + ], [ 'name' => 'new_post', 'category' => 'global', 'title' => _MI_IMFORUM_NOTIFY_GLOBAL_NEWPOST, 'caption' => _MI_IMFORUM_NOTIFY_GLOBAL_NEWPOST_CAP, @@ -277,5 +289,13 @@ 'valuetype' => 'text', 'default' => '', ], + [ + 'name' => 'read_expire_days', + 'title' => '_MI_IMFORUM_CFG_READ_EXPIRE', + 'description' => '_MI_IMFORUM_CFG_READ_EXPIRE_DSC', + 'formtype' => 'textbox', + 'valuetype' => 'int', + 'default' => 90, + ], ], ]; diff --git a/src/include/autotask_cleanup.php b/src/include/autotask_cleanup.php new file mode 100644 index 0000000..d8907ea --- /dev/null +++ b/src/include/autotask_cleanup.php @@ -0,0 +1,17 @@ +run(); diff --git a/src/language/english/mail_template/global_digest.tpl b/src/language/english/mail_template/global_digest.tpl new file mode 100644 index 0000000..c9a29e3 --- /dev/null +++ b/src/language/english/mail_template/global_digest.tpl @@ -0,0 +1,17 @@ +Hello {X_UNAME}, + +New topics and posts in the forums on {X_SITENAME} between {X_DIGEST_FROM} and {X_DIGEST_TO}: + +{X_DIGEST_TEXT} + +----------- + +You receive this message because you asked for a digest of all forums. To stop, change your subscriptions here: +{X_UNSUBSCRIBE_URL} + +Please do not reply to this message. + +----------- +{X_SITENAME} ({X_SITEURL}) +webmaster +{X_ADMINMAIL} diff --git a/src/language/english/main.php b/src/language/english/main.php index 5cdb2b1..a050c13 100644 --- a/src/language/english/main.php +++ b/src/language/english/main.php @@ -128,3 +128,6 @@ define('_MD_IMFORUM_MOD_ERR_SUSPEND_WHO', 'Choose another member than yourself.'); define('_MD_IMFORUM_MOD_ERR_SUSPEND_DAYS', 'Choose between 1 and %d days.'); define('_MD_IMFORUM_MOD_ERR_SUSPEND_MOD', 'Moderators and webmasters cannot be suspended.'); +define('_MD_IMFORUM_DIGEST_NEW_TOPIC', 'new topic'); +define('_MD_IMFORUM_DIGEST_POSTS', '%d new posts'); +define('_MD_IMFORUM_DIGEST_MORE', 'And %d more topics:'); diff --git a/src/language/english/modinfo.php b/src/language/english/modinfo.php index 53f4cb0..3270b1f 100644 --- a/src/language/english/modinfo.php +++ b/src/language/english/modinfo.php @@ -78,3 +78,11 @@ define('_MI_IMFORUM_NOTIFY_GLOBAL_NEWPOST_CAP', 'Tell me about new posts in all forums'); define('_MI_IMFORUM_NOTIFY_GLOBAL_NEWPOST_DSC', 'Tell me about new posts in all forums'); define('_MI_IMFORUM_NOTIFY_GLOBAL_NEWPOST_SBJ', '[{X_SITENAME}] {X_MODULE}: new post'); +define('_MI_IMFORUM_NOTIFY_GLOBAL_DIGEST', 'Digest'); +define('_MI_IMFORUM_NOTIFY_GLOBAL_DIGEST_CAP', 'Send me a digest of the new topics and posts in all forums'); +define('_MI_IMFORUM_NOTIFY_GLOBAL_DIGEST_DSC', 'Send me a digest of the new topics and posts in all forums'); +define('_MI_IMFORUM_NOTIFY_GLOBAL_DIGEST_SBJ', '[{X_SITENAME}] {X_MODULE}: digest'); +define('_MI_IMFORUM_TASK_DIGEST', 'Forum digest mail'); +define('_MI_IMFORUM_TASK_CLEANUP', 'Forum cleanup'); +define('_MI_IMFORUM_CFG_READ_EXPIRE', 'Days before read marks expire'); +define('_MI_IMFORUM_CFG_READ_EXPIRE_DSC', 'Posts older than this count as read for members who have read marks, and the old marks are removed. 0 keeps all marks.'); diff --git a/src/language/nederlands/mail_template/global_digest.tpl b/src/language/nederlands/mail_template/global_digest.tpl new file mode 100644 index 0000000..af8f795 --- /dev/null +++ b/src/language/nederlands/mail_template/global_digest.tpl @@ -0,0 +1,17 @@ +Hallo {X_UNAME}, + +Nieuwe onderwerpen en berichten in de forums van {X_SITENAME} tussen {X_DIGEST_FROM} en {X_DIGEST_TO}: + +{X_DIGEST_TEXT} + +----------- + +U ontvangt dit bericht omdat u een overzicht van alle forums hebt gevraagd. Om te stoppen wijzigt u uw abonnementen hier: +{X_UNSUBSCRIBE_URL} + +Antwoord niet op dit bericht. + +----------- +{X_SITENAME} ({X_SITEURL}) +webmaster +{X_ADMINMAIL} diff --git a/src/language/nederlands/main.php b/src/language/nederlands/main.php index 26a2245..4bb9a0a 100644 --- a/src/language/nederlands/main.php +++ b/src/language/nederlands/main.php @@ -128,3 +128,6 @@ define('_MD_IMFORUM_MOD_ERR_SUSPEND_WHO', 'Kies een ander lid dan uzelf.'); define('_MD_IMFORUM_MOD_ERR_SUSPEND_DAYS', 'Kies tussen 1 en %d dagen.'); define('_MD_IMFORUM_MOD_ERR_SUSPEND_MOD', 'Moderatoren en webmasters kunnen niet geschorst worden.'); +define('_MD_IMFORUM_DIGEST_NEW_TOPIC', 'nieuw onderwerp'); +define('_MD_IMFORUM_DIGEST_POSTS', '%d nieuwe berichten'); +define('_MD_IMFORUM_DIGEST_MORE', 'En nog %d onderwerpen:'); diff --git a/src/language/nederlands/modinfo.php b/src/language/nederlands/modinfo.php index bd07221..9e5b99c 100644 --- a/src/language/nederlands/modinfo.php +++ b/src/language/nederlands/modinfo.php @@ -78,3 +78,11 @@ define('_MI_IMFORUM_NOTIFY_GLOBAL_NEWPOST_CAP', 'Meld mij nieuwe berichten in alle forums'); define('_MI_IMFORUM_NOTIFY_GLOBAL_NEWPOST_DSC', 'Meld mij nieuwe berichten in alle forums'); define('_MI_IMFORUM_NOTIFY_GLOBAL_NEWPOST_SBJ', '[{X_SITENAME}] {X_MODULE}: nieuw bericht'); +define('_MI_IMFORUM_NOTIFY_GLOBAL_DIGEST', 'Overzicht'); +define('_MI_IMFORUM_NOTIFY_GLOBAL_DIGEST_CAP', 'Stuur mij een overzicht van nieuwe onderwerpen en berichten in alle forums'); +define('_MI_IMFORUM_NOTIFY_GLOBAL_DIGEST_DSC', 'Stuur mij een overzicht van nieuwe onderwerpen en berichten in alle forums'); +define('_MI_IMFORUM_NOTIFY_GLOBAL_DIGEST_SBJ', '[{X_SITENAME}] {X_MODULE}: overzicht'); +define('_MI_IMFORUM_TASK_DIGEST', 'Forum: overzichtsmail'); +define('_MI_IMFORUM_TASK_CLEANUP', 'Forum: opruimen'); +define('_MI_IMFORUM_CFG_READ_EXPIRE', 'Dagen voordat leesmarkeringen vervallen'); +define('_MI_IMFORUM_CFG_READ_EXPIRE_DSC', 'Berichten die ouder zijn tellen als gelezen voor leden met leesmarkeringen, en de oude markeringen verdwijnen. 0 bewaart alle markeringen.'); diff --git a/tests/Integration/BackgroundTasksTest.php b/tests/Integration/BackgroundTasksTest.php new file mode 100644 index 0000000..d26b4c8 --- /dev/null +++ b/tests/Integration/BackgroundTasksTest.php @@ -0,0 +1,263 @@ + 90, 'online_minutes' => 15]); + } + + protected function tearDown(): void + { + Settings::override(null); + } + + /** + * A topic whose posts were written at the given moments + * + * @param list $times + */ + private static function topicAt(int $forumId, string $title, array $times, int $uid = 5): \mod_imforum_Topic + { + $topic = self::make('topic', ['forum_id' => $forumId, 'topic_title' => $title, 'topic_poster' => $uid, 'topic_time' => $times[0]]); + foreach ($times as $i => $time) { + self::make('post', ['topic_id' => (int) $topic->id(), 'subject' => $title . ' ' . $i, 'post_text' => 'Text', 'uid' => $uid, 'post_time' => $time]); + } + return self::reload('topic', (int) $topic->id()); + } + + private function forumId(): int + { + return (int) self::forum((int) self::category()->id())->id(); + } + + public function testOldReadMarksGoButReadTopicsStayRead(): void + { + $now = time(); + $forumId = $this->forumId(); + $old = self::topicAt($forumId, 'Old', [$now - 200 * self::DAY, $now - 199 * self::DAY]); + $partly = self::topicAt($forumId, 'Partly', [$now - 200 * self::DAY, $now - 5 * self::DAY]); + $recent = self::topicAt($forumId, 'Recent', [$now - 3 * self::DAY]); + $marks = self::handler('readmark'); + $marks->markRead(Marks::TYPE_TOPIC, 5, (int) $old->id(), (int) $old->getVar('topic_last_post_id', 'e'), $now - 198 * self::DAY); + $marks->markRead(Marks::TYPE_TOPIC, 5, (int) $partly->id(), (int) $partly->getVar('topic_first_post_id', 'e'), $now - 190 * self::DAY); + $marks->markRead(Marks::TYPE_TOPIC, 5, (int) $recent->id(), (int) $recent->getVar('topic_last_post_id', 'e'), $now - 2 * self::DAY); + + $result = Cleanup::run($now); + self::assertSame(2, $result['read_marks']); + + $progress = []; + foreach ([$old, $partly, $recent] as $topic) { + $topic = self::reload('topic', (int) $topic->id()); + $progress[(int) $topic->id()] = ['forum_id' => $forumId, 'last_post_id' => (int) $topic->getVar('topic_last_post_id', 'e')]; + } + $unread = (new Tracker(5))->getUnreadTopics($progress); + self::assertArrayNotHasKey((int) $old->id(), $unread, 'a topic read long ago stays read without its mark'); + self::assertArrayHasKey((int) $partly->id(), $unread, 'a reply since the mark keeps the topic unread'); + self::assertArrayNotHasKey((int) $recent->id(), $unread, 'a recent mark stays'); + self::assertSame(1, self::rows('readmark') - 1, 'the recent topic mark stays next to the forum mark'); + } + + public function testMarksOfDeletedTopicsAndCoveredMarksAreRemoved(): void + { + $now = time(); + $forumId = $this->forumId(); + $topic = self::topicAt($forumId, 'Covered', [$now - 3 * self::DAY]); + $marks = self::handler('readmark'); + $marks->markRead(Marks::TYPE_TOPIC, 5, (int) $topic->id(), (int) $topic->getVar('topic_last_post_id', 'e'), $now - 3 * self::DAY); + $marks->markRead(Marks::TYPE_FORUM, 5, $forumId, (int) $topic->getVar('topic_last_post_id', 'e'), $now - 2 * self::DAY); + $marks->markRead(Marks::TYPE_TOPIC, 5, 9999, 3, $now - self::DAY); + + self::assertSame(2, Cleanup::run($now)['read_marks']); + self::assertSame(1, self::rows('readmark'), 'only the forum mark is left'); + } + + public function testReadMarksNeverExpireWithZeroDays(): void + { + Settings::override(['read_expire_days' => 0, 'online_minutes' => 15]); + $now = time(); + $topic = self::topicAt($this->forumId(), 'Old', [$now - 400 * self::DAY]); + self::handler('readmark')->markRead(Marks::TYPE_TOPIC, 5, (int) $topic->id(), (int) $topic->getVar('topic_last_post_id', 'e'), $now - 399 * self::DAY); + self::assertSame(0, Cleanup::run($now)['read_marks']); + self::assertSame(1, self::rows('readmark')); + } + + public function testVisitorsAndEndedSuspensionsAreCleanedUp(): void + { + $now = time(); + $online = self::handler('online'); + $online->track(5, 'five', '203.0.113.5', 0, 0, 15); + self::make('online', ['uid' => 6, 'uname' => 'six', 'ip' => '203.0.113.6', 'updated' => $now - 3600]); + self::make('suspension', ['uid' => 7, 'forum_id' => 0, 'start_time' => $now - 90 * self::DAY, 'end_time' => $now - 60 * self::DAY]); + self::make('suspension', ['uid' => 8, 'forum_id' => 0, 'start_time' => $now - 40 * self::DAY, 'end_time' => $now - 10 * self::DAY]); + self::make('suspension', ['uid' => 9, 'forum_id' => 0, 'start_time' => $now - self::DAY, 'end_time' => $now + 5 * self::DAY]); + + $result = Cleanup::run($now); + self::assertSame(1, $result['suspensions'], 'only the one that ended more than 30 days ago'); + self::assertSame(2, self::rows('suspension')); + self::assertSame(1, self::rows('online'), 'the visitor from an hour ago is gone'); + } + + // ---- the digest + + private function digestSetup(): array + { + $category = (int) self::category()->id(); + $open = (int) self::forum($category, 'Open')->id(); + $staff = (int) self::forum($category, 'Staff only')->id(); + self::grantCategory($category, [1, 2, 3, 4]); + self::grant($open, ['forum_access', 'forum_view'], [2, 3]); + self::grant($staff, ['forum_access', 'forum_view'], [4]); + return [$open, $staff]; + } + + private function mailer(array $subscribers, array &$sent): mod_imforum_DigestMailer + { + $notifier = new class($subscribers) extends mod_imforum_Notifier { + /** @param list $subscribers */ + public function __construct(private array $subscribers) + { + } + + public function getGlobalSubscribers(string $event): array + { + return $event === 'digest' ? $this->subscribers : []; + } + }; + return new class($notifier, $sent) extends mod_imforum_DigestMailer { + /** @param array, text: string}> $sent */ + public function __construct(mod_imforum_Notifier $notifier, private array &$sent) + { + parent::__construct($notifier); + } + + protected function send(string $text, array $uids, int $since, int $until): void + { + $this->sent[] = ['uids' => $uids, 'text' => $text]; + } + }; + } + + public function testEachRecipientGetsOnlyTheForumsTheyMayRead(): void + { + [$open, $staff] = $this->digestSetup(); + $now = time(); + self::topicAt($open, 'Public news', [$now - 3600, $now - 1800]); + self::topicAt($staff, 'Secret plans', [$now - 3000]); + $groups = [10 => [2], 11 => [4, 2], 12 => [1], 13 => [3]]; + + $sent = []; + $mailer = $this->mailer([10, 11, 12], $sent); + $result = $mailer->run($now, static fn (int $uid): array => $groups[$uid]); + + self::assertSame(2, $result['topics']); + self::assertSame(3, $result['recipients']); + $byUid = []; + foreach ($sent as $mail) { + foreach ($mail['uids'] as $uid) { + $byUid[$uid] = $mail['text']; + } + } + self::assertStringContainsString('Public news', $byUid[10]); + self::assertStringContainsString('new topic, 2 new posts', $byUid[10]); + self::assertStringNotContainsString('Secret plans', $byUid[10], 'a member does not hear about forums they cannot read'); + self::assertStringContainsString('Secret plans', $byUid[11]); + self::assertStringContainsString('Secret plans', $byUid[12]); + self::assertStringContainsString('topic.php?topic=', $byUid[10]); + self::assertStringNotContainsString('Text', $byUid[10], 'the text of a post is never in the mail'); + + $digests = self::handler('digest')->getObjects(); + self::assertCount(1, $digests); + self::assertStringContainsString('Secret plans', (string) reset($digests)->getVar('digest_content', 'n'), 'the stored digest is complete'); + } + + public function testTheDigestCoversTheTimeSinceTheLastOneOnly(): void + { + [$open] = $this->digestSetup(); + $now = time(); + self::topicAt($open, 'Yesterday', [$now - 2 * self::DAY]); + self::topicAt($open, 'Today', [$now - 600]); + $waiting = self::topicAt($open, 'Waiting', [$now - 500]); + foreach (['topic' => (int) $waiting->id(), 'post' => (int) $waiting->getVar('topic_first_post_id', 'e')] as $item => $id) { + $object = self::reload($item, $id); + $object->setVar('approved', 0); + self::assertTrue(self::handler($item)->insert($object, true)); + } + + $sent = []; + $mailer = $this->mailer([10], $sent); + $first = $mailer->run($now, static fn (): array => [2]); + self::assertSame(1, $first['topics'], 'a first digest covers one day, without posts that wait for approval'); + self::assertStringContainsString('Today', $sent[0]['text']); + self::assertStringNotContainsString('Yesterday', $sent[0]['text']); + self::assertStringNotContainsString('Waiting', $sent[0]['text']); + + $again = $mailer->run($now + 1, static fn (): array => [2]); + self::assertSame(0, $again['topics'], 'nothing new since the last digest'); + self::assertCount(1, $sent); + self::assertSame(1, self::rows('digest')); + + self::topicAt($open, 'Later', [$now + 100]); + $next = $mailer->run($now + 200, static fn (): array => [2]); + self::assertSame(1, $next['topics']); + self::assertStringContainsString('Later', $sent[1]['text']); + self::assertStringNotContainsString('Today', $sent[1]['text']); + } + + public function testNothingIsSentToMembersWithoutAnythingToRead(): void + { + [, $staff] = $this->digestSetup(); + $now = time(); + self::topicAt($staff, 'Secret plans', [$now - 100]); + + $sent = []; + $result = $this->mailer([10], $sent)->run($now, static fn (): array => [2]); + self::assertSame(1, $result['topics']); + self::assertSame(0, $result['recipients']); + self::assertSame([], $sent); + self::assertSame(1, self::rows('digest'), 'the digest is kept for the administrators'); + } + + public function testALongDigestIsCutOff(): void + { + [$open] = $this->digestSetup(); + $now = time(); + for ($i = 0; $i < mod_imforum_DigestMailer::MAX_TOPICS + 3; $i++) { + self::topicAt($open, "Topic $i", [$now - 100 - $i]); + } + $sent = []; + $this->mailer([10], $sent)->run($now, static fn (): array => [2]); + self::assertSame(mod_imforum_DigestMailer::MAX_TOPICS, substr_count($sent[0]['text'], 'topic.php?topic=')); + self::assertStringContainsString('And 3 more topics:', $sent[0]['text']); + } + + public function testTheAutotaskFilesTheModuleDeclaresExist(): void + { + $version = (string) file_get_contents(dirname(__DIR__, 2) . '/src/icms_version.php'); + self::assertSame(2, preg_match_all("/'code' => '([^']+)'/", $version, $matches)); + self::assertCount(2, $matches[1]); + foreach ($matches[1] as $file) { + self::assertFileExists(dirname(__DIR__, 2) . '/src/' . $file); + } + self::assertStringContainsString("'name' => 'digest', 'category' => 'global'", $version); + self::assertFileExists(dirname(__DIR__, 2) . '/src/language/english/mail_template/global_digest.tpl'); + self::assertFileExists(dirname(__DIR__, 2) . '/src/language/nederlands/mail_template/global_digest.tpl'); + } +} From d51ef460a85bc0964abc6a3f87e81f59e55e8a50 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 26 Oct 2026 13:35:30 +0000 Subject: [PATCH 2/5] Load the front end strings in the digest, which runs from the admin area Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01MZy45V5RV9nx6Buq8vqKHr --- src/class/DigestMailer.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/class/DigestMailer.php b/src/class/DigestMailer.php index 8108497..71ce3bb 100644 --- a/src/class/DigestMailer.php +++ b/src/class/DigestMailer.php @@ -67,6 +67,10 @@ public function collect(int $since, int $until): array { * @return array{text: string, topics: int} */ public static function render(array $byForum, array $forumIds, array $forumNames): array { + // an autotask runs in any request, also in the admin area, where the front end strings are not loaded + if (function_exists('icms_loadLanguageFile')) { + icms_loadLanguageFile(IMFORUM_DIRNAME, 'main'); + } $lines = []; $topicCount = 0; $left = 0; From 7bbb4cb661255cb2b016bfe211ff010145300279 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 26 Oct 2026 21:04:16 +0000 Subject: [PATCH 3/5] Use the core PDO connection (icms::$db) with bound parameters for all raw SQL Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01MZy45V5RV9nx6Buq8vqKHr --- CHANGELOG.md | 1 + phpstan.neon.dist | 4 - src/class/ArchiveHandler.php | 16 ++-- src/class/AttachmentHandler.php | 2 +- src/class/Db.php | 119 ++++++++++++++++++++++++++++++ src/class/DigestHandler.php | 4 +- src/class/ForumHandler.php | 41 +++------- src/class/ModerationService.php | 37 +++------- src/class/OnlineHandler.php | 2 +- src/class/PermtemplateHandler.php | 2 +- src/class/PostHandler.php | 42 +++++------ src/class/ReadmarkHandler.php | 50 ++++++------- src/class/ReportHandler.php | 8 +- src/class/TopicHandler.php | 78 ++++++++------------ src/include/onupdate.php | 15 ++-- src/include/search.inc.php | 24 +++--- tests/Integration/DbTest.php | 98 ++++++++++++++++++++++++ tests/integration-bootstrap.php | 1 + tests/phpstan-bootstrap.php | 1 + 19 files changed, 348 insertions(+), 197 deletions(-) create mode 100644 src/class/Db.php create mode 100644 tests/Integration/DbTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 47e4837..531328c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,3 +11,4 @@ While the module is in development the status carries the phase: 1.0.0 Alpha 5 i - Phase 4: writing. `post.php` starts topics, replies, edits and deletes through one `PostService` that holds all rules: the permissions forum_post, forum_reply, forum_edit, forum_delete, forum_attach and forum_noapprove, suspensions, a flood limit, a time limit for members, locked topics, what the forum allows (HTML, signature, topic prefix), approval and the edit history. Members delete only their own last post of a topic. The form is built on the IPF secure form, with a preview, a disclaimer and a captcha for guests. Attachments go through `Uploader` (extension list per forum, a list of extensions that is never accepted, size limit, a random file name without extension in a folder the web server cannot reach) and are served by `attachment.php`, which checks the read permission and the karma lock and always sends a download. Subscriptions use the core notification system for topics, forums and all forums; the mail is only sent to members who may read the forum and never holds the text of a post. Post text is now stored as typed (`XOBJ_DTYPE_OTHER`): the IPF textarea type ran every text through HTMLPurifier when saving, which turned BBCode into HTML for good and made the HTML, smiley and line break switches of a post meaningless. The text is made safe when it is shown. The welcome post is left for a later release: the core has no event for a new registration to hook it on. - Phase 5: moderation. `moderate.php` and one `ModerationService` that holds all rules: lock and unlock, pin, feature, move (the moderator must moderate both forums), merge (counters, last post, views and read marks follow), split from a post onward (replies are re-attached to the new first post), delete a topic, approve waiting posts (subscribers are told only then), a queue of waiting posts and a list of open reports per forum, and suspensions with a number of days (reading stays possible; moderators and webmasters cannot be suspended). `report.php` lets members report a post, one open report per member and post. Every action is a confirmation form followed by a POST with a security token. Restoring pruned topics from the archive stays an admin task for now. Integration tests cover each action, the counters after merge and split, and a suspended member who can read but not write. - Phase 6: background tasks. Two autotasks, registered by the core when the module is installed or updated: the digest mail and a cleanup. Members subscribe to the digest with the notification "Digest" for all forums. `DigestMailer` covers the time since the last digest (one day for the first), lists titles, counts and links but never the text of a post, sends each member only the forums that member may read, and stores the complete digest for the administrators before it sends, so a failure halfway never causes a second mail. The cleanup removes visitors that are no longer online, read marks older than a preference (90 days; 0 keeps them) and suspensions that ended more than 30 days ago. A topic without a read mark counts as unread, so posts older than the limit first count as read for members who have marks, and only then the old marks go. +- Database access: all hand-written SQL now goes through `mod_imforum_Db` over the core's PDO connection (`icms::$db`) with bound parameters, instead of `queryF()` on the legacy adapter (`icms::$xoopsDB`) with values glued into the SQL. The IPF handlers keep the legacy adapter the core gives them for their own queries; both share one PDO connection. Without PDO (`XOOPS_DB_TYPE` not `pdo.*`) the module stops with a clear message. Prepared statements do not show up in the debug console of the core, which only logs `query()`. diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 9efbe05..078a5bd 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -34,10 +34,6 @@ parameters: # icms_ipf_Tree::getFirstChild() documents string; the tree is keyed by integer ids message: '#^Parameter \#1 \$key of method icms_ipf_Tree::getFirstChild\(\) expects string, int given\.$#' identifier: argument.type - - - # icms_db_legacy_IDatabase::queryF() documents a resource, but returns false when the query fails - identifier: ternary.alwaysTrue - path: src/include/onupdate.php - # icms_core_DataFilter::filterTextareaDisplay() documents bool switches but compares them # with != 0 and !== 0, so false would still convert line breaks: the post's 0/1 flags go in as they are diff --git a/src/class/ArchiveHandler.php b/src/class/ArchiveHandler.php index 0242ab9..01e5e70 100644 --- a/src/class/ArchiveHandler.php +++ b/src/class/ArchiveHandler.php @@ -29,17 +29,15 @@ public function getTopicsPage(array $forumIds, int $start, int $limit): array { if (!$forumIds) { return ['rows' => [], 'total' => 0]; } - $topics = $this->db->prefix(IMFORUM_DIRNAME . '_topic'); - $from = " FROM {$this->table} a JOIN $topics t ON t.topic_id = a.topic_id WHERE t.forum_id IN (" . implode(',', $forumIds) . ')'; + $topics = mod_imforum_Db::table('topic'); + $from = " FROM {$this->table} a JOIN $topics t ON t.topic_id = a.topic_id WHERE t.forum_id IN (" . mod_imforum_Db::marks($forumIds) . ')'; - $total = 0; - $result = $this->db->queryF("SELECT COUNT(DISTINCT a.topic_id)$from"); - if ($result && ($row = $this->db->fetchRow($result))) { - $total = (int) $row[0]; - } + $total = mod_imforum_Db::value("SELECT COUNT(DISTINCT a.topic_id)$from", $forumIds); $rows = []; - $result = $this->db->queryF("SELECT a.topic_id, COUNT(*), MAX(a.archived_time)$from GROUP BY a.topic_id ORDER BY MAX(a.archived_time) DESC, a.topic_id DESC LIMIT " . max(0, $start) . ', ' . max(1, $limit)); - while ($result && ($row = $this->db->fetchRow($result))) { + foreach (mod_imforum_Db::rows( + "SELECT a.topic_id, COUNT(*), MAX(a.archived_time)$from GROUP BY a.topic_id ORDER BY MAX(a.archived_time) DESC, a.topic_id DESC LIMIT ?, ?", + [...$forumIds, max(0, $start), max(1, $limit)] + ) as $row) { $rows[] = ['topic_id' => (int) $row[0], 'posts' => (int) $row[1], 'archived_time' => (int) $row[2]]; } return ['rows' => $rows, 'total' => $total]; diff --git a/src/class/AttachmentHandler.php b/src/class/AttachmentHandler.php index 90ea0f7..df48208 100644 --- a/src/class/AttachmentHandler.php +++ b/src/class/AttachmentHandler.php @@ -41,7 +41,7 @@ public function getByPosts(array $postIds): array { } public function incrementDownloads(int $attachId): bool { - return (bool) $this->db->queryF("UPDATE {$this->table} SET downloads = downloads + 1 WHERE attach_id = $attachId"); + return mod_imforum_Db::ok("UPDATE {$this->table} SET downloads = downloads + 1 WHERE attach_id = ?", [$attachId]); } public function countForPost(int $postId): int { diff --git a/src/class/Db.php b/src/class/Db.php new file mode 100644 index 0000000..985ad04 --- /dev/null +++ b/src/class/Db.php @@ -0,0 +1,119 @@ + $values + */ + public static function marks(array $values): string { + return implode(',', array_fill(0, max(1, count($values)), '?')); + } + + /** + * Run a statement that changes data + * + * @param list $params + * @return int|null rows changed, null when the statement failed + */ + public static function execute(string $sql, array $params = []): ?int { + $statement = self::run($sql, $params); + return $statement === null ? null : $statement->rowCount(); + } + + /** + * Run a statement that changes data and say whether it worked + * + * @param list $params + */ + public static function ok(string $sql, array $params = []): bool { + return self::execute($sql, $params) !== null; + } + + /** + * All rows of a query, each as a list of values + * + * @param list $params + * @return list> the driver's own types, so cast what you use + */ + public static function rows(string $sql, array $params = []): array { + $statement = self::run($sql, $params); + return $statement === null ? [] : array_values($statement->fetchAll(PDO::FETCH_NUM)); + } + + /** + * The first row of a query, or an empty list + * + * @param list $params + * @return list + */ + public static function row(string $sql, array $params = []): array { + return self::rows($sql, $params)[0] ?? []; + } + + /** + * The first value of the first row, 0 when there is none + * + * @param list $params + */ + public static function value(string $sql, array $params = []): int { + return (int) (self::row($sql, $params)[0] ?? 0); + } + + /** + * Integers go in as integers, so a LIMIT keeps working + * + * @param list $params + */ + private static function run(string $sql, array $params): ?PDOStatement { + $statement = self::connection()->prepare($sql); + if ($statement === false) { + return null; + } + foreach ($params as $i => $value) { + $statement->bindValue($i + 1, $value, is_int($value) ? PDO::PARAM_INT : ($value === null ? PDO::PARAM_NULL : PDO::PARAM_STR)); + } + return $statement->execute() ? $statement : null; + } +} diff --git a/src/class/DigestHandler.php b/src/class/DigestHandler.php index e8fdcb5..824e627 100644 --- a/src/class/DigestHandler.php +++ b/src/class/DigestHandler.php @@ -22,9 +22,7 @@ public function __construct(icms_db_legacy_Database $db) { * Time of the most recent digest, 0 if none was sent yet */ public function getLastDigestTime(): int { - $result = $this->db->queryF("SELECT MAX(digest_time) FROM {$this->table}"); - $row = $result ? $this->db->fetchRow($result) : false; - return $row ? (int) $row[0] : 0; + return mod_imforum_Db::value("SELECT MAX(digest_time) FROM {$this->table}"); } protected function beforeInsert(mod_imforum_Digest $digest): bool { diff --git a/src/class/ForumHandler.php b/src/class/ForumHandler.php index 817ed90..9743cc7 100644 --- a/src/class/ForumHandler.php +++ b/src/class/ForumHandler.php @@ -91,43 +91,30 @@ private function addTreeOptions(icms_ipf_Tree $tree, int $parent, int $depth, ar * requests, and stays cheap thanks to the indexes from imforum_db_upgrade_1(). */ public function synchronise(int $forumId): bool { - $topics = $this->db->prefix(IMFORUM_DIRNAME . '_topic'); - $posts = $this->db->prefix(IMFORUM_DIRNAME . '_post'); + $topicCount = mod_imforum_Db::value('SELECT COUNT(*) FROM ' . mod_imforum_Db::table('topic') . ' WHERE forum_id = ? AND approved = 1', [$forumId]); + [$postCount, $lastPost] = mod_imforum_Db::row('SELECT COUNT(*), MAX(post_id) FROM ' . mod_imforum_Db::table('post') . ' WHERE forum_id = ? AND approved = 1', [$forumId]) ?: [0, 0]; - [$topicCount] = $this->fetchRow("SELECT COUNT(*) FROM $topics WHERE forum_id = $forumId AND approved = 1"); - [$postCount, $lastPost] = $this->fetchRow("SELECT COUNT(*), MAX(post_id) FROM $posts WHERE forum_id = $forumId AND approved = 1"); - - // queryF: the counters follow a write that has already been allowed - return (bool) $this->db->queryF(sprintf( - 'UPDATE %s SET forum_topics = %d, forum_posts = %d, forum_last_post_id = %d WHERE forum_id = %d', - $this->table, (int) $topicCount, (int) $postCount, (int) $lastPost, $forumId - )); + return mod_imforum_Db::ok( + "UPDATE {$this->table} SET forum_topics = ?, forum_posts = ?, forum_last_post_id = ? WHERE forum_id = ?", + [$topicCount, (int) $postCount, (int) $lastPost, $forumId] + ); } /** * Recount every forum in two queries; used after an import or a prune */ public function synchroniseAll(): bool { - $topics = $this->db->prefix(IMFORUM_DIRNAME . '_topic'); - $posts = $this->db->prefix(IMFORUM_DIRNAME . '_post'); + $topics = mod_imforum_Db::table('topic'); + $posts = mod_imforum_Db::table('post'); - $ok = (bool) $this->db->queryF("UPDATE {$this->table} f" + $ok = mod_imforum_Db::ok("UPDATE {$this->table} f" . " LEFT JOIN (SELECT forum_id, COUNT(*) AS n FROM $topics WHERE approved = 1 GROUP BY forum_id) t ON t.forum_id = f.forum_id" . ' SET f.forum_topics = COALESCE(t.n, 0)'); - return (bool) $this->db->queryF("UPDATE {$this->table} f" + return mod_imforum_Db::ok("UPDATE {$this->table} f" . " LEFT JOIN (SELECT forum_id, COUNT(*) AS n, MAX(post_id) AS last FROM $posts WHERE approved = 1 GROUP BY forum_id) p ON p.forum_id = f.forum_id" . ' SET f.forum_posts = COALESCE(p.n, 0), f.forum_last_post_id = COALESCE(p.last, 0)') && $ok; } - /** - * @return list - */ - private function fetchRow(string $sql): array { - $result = $this->db->queryF($sql); - $row = $result ? $this->db->fetchRow($result) : false; - return $row ? array_values($row) : [0, 0]; - } - /** * A forum may not become its own parent or the child of one of its children */ @@ -159,8 +146,7 @@ protected function afterSave(mod_imforum_Forum $forum): bool { } private function getParentId(int $forumId): int { - [$parent] = $this->fetchRow("SELECT parent_forum FROM {$this->table} WHERE forum_id = $forumId"); - return (int) $parent; + return mod_imforum_Db::value("SELECT parent_forum FROM {$this->table} WHERE forum_id = ?", [$forumId]); } /** @@ -181,9 +167,6 @@ protected function afterDelete(mod_imforum_Forum $forum): bool { icms_getModuleHandler('online', IMFORUM_DIRNAME)->deleteAll($byForum); icms_getModuleHandler('readmark', IMFORUM_DIRNAME)->deleteByItem(mod_imforum_ReadmarkHandler::TYPE_FORUM, $forumId); - return (bool) $this->db->queryF(sprintf( - 'UPDATE %s SET parent_forum = %d WHERE parent_forum = %d', - $this->table, (int) $forum->getVar('parent_forum', 'e'), $forumId - )); + return mod_imforum_Db::ok("UPDATE {$this->table} SET parent_forum = ? WHERE parent_forum = ?", [(int) $forum->getVar('parent_forum', 'e'), $forumId]); } } diff --git a/src/class/ModerationService.php b/src/class/ModerationService.php index 31c7fbd..55f3596 100644 --- a/src/class/ModerationService.php +++ b/src/class/ModerationService.php @@ -38,13 +38,6 @@ private static function result(bool $ok, string $error = ''): array { return ['ok' => $ok, 'errors' => $ok || $error === '' ? [] : [$error]]; } - /** - * A write that has already been allowed - */ - private function run(string $sql): bool { - return (bool) icms::$xoopsDB->queryF($sql); - } - private function mayModerate(int $forumId): bool { $forum = $this->permissions->getForum($forumId); return $forum !== null && $this->permissions->canModerate($forum); @@ -142,13 +135,11 @@ public function merge(mod_imforum_Topic $source, mod_imforum_Topic $target): arr return $fail(_MD_IMFORUM_MOD_ERR_SAME_TOPIC); } - $db = icms::$xoopsDB; - $posts = $db->prefix(IMFORUM_DIRNAME . '_post'); - $topicTable = $db->prefix(IMFORUM_DIRNAME . '_topic'); - $ok = $this->run("UPDATE $posts SET topic_id = $targetId, forum_id = $targetForum WHERE topic_id = $sourceId"); - $views = (int) $source->getVar('topic_views', 'e'); - $ok = $this->run("UPDATE $topicTable SET topic_views = topic_views + $views WHERE topic_id = $targetId") && $ok; - $ok = $this->run("DELETE FROM $topicTable WHERE topic_id = $sourceId") && $ok; + $posts = mod_imforum_Db::table('post'); + $topicTable = mod_imforum_Db::table('topic'); + $ok = mod_imforum_Db::ok("UPDATE $posts SET topic_id = ?, forum_id = ? WHERE topic_id = ?", [$targetId, $targetForum, $sourceId]); + $ok = mod_imforum_Db::ok("UPDATE $topicTable SET topic_views = topic_views + ? WHERE topic_id = ?", [(int) $source->getVar('topic_views', 'e'), $targetId]) && $ok; + $ok = mod_imforum_Db::ok("DELETE FROM $topicTable WHERE topic_id = ?", [$sourceId]) && $ok; icms_getModuleHandler('readmark', IMFORUM_DIRNAME)->deleteByItem(mod_imforum_ReadmarkHandler::TYPE_TOPIC, $sourceId); $ok = self::topics()->synchronise($targetId) && $ok; @@ -184,11 +175,8 @@ public function split(mod_imforum_Post $post, string $title): array { $postId = (int) $post->id(); $oldId = (int) $oldTopic->id(); - $db = icms::$xoopsDB; - $posts = $db->prefix(IMFORUM_DIRNAME . '_post'); - $result = $db->queryF("SELECT MIN(post_id) FROM $posts WHERE topic_id = $oldId"); - $row = $result ? $db->fetchRow($result) : false; - if ($row && (int) $row[0] === $postId) { + $posts = mod_imforum_Db::table('post'); + if (mod_imforum_Db::value("SELECT MIN(post_id) FROM $posts WHERE topic_id = ?", [$oldId]) === $postId) { return $fail(_MD_IMFORUM_MOD_ERR_SPLIT_FIRST); } @@ -204,9 +192,9 @@ public function split(mod_imforum_Post $post, string $title): array { } $newId = (int) $topic->id(); - $ok = $this->run("UPDATE $posts SET topic_id = $newId WHERE topic_id = $oldId AND post_id >= $postId"); - $ok = $this->run("UPDATE $posts SET pid = 0 WHERE post_id = $postId") && $ok; - $ok = $this->run("UPDATE $posts SET pid = $postId WHERE topic_id = $newId AND post_id > $postId AND pid < $postId") && $ok; + $ok = mod_imforum_Db::ok("UPDATE $posts SET topic_id = ? WHERE topic_id = ? AND post_id >= ?", [$newId, $oldId, $postId]); + $ok = mod_imforum_Db::ok("UPDATE $posts SET pid = 0 WHERE post_id = ?", [$postId]) && $ok; + $ok = mod_imforum_Db::ok("UPDATE $posts SET pid = ? WHERE topic_id = ? AND post_id > ? AND pid < ?", [$postId, $newId, $postId, $postId]) && $ok; $ok = self::topics()->synchronise($oldId) && $ok; $ok = self::topics()->synchronise($newId) && $ok; $ok = self::forums()->synchronise($forumId) && $ok; @@ -264,10 +252,7 @@ public function approve(mod_imforum_Post $post): array { } private function firstPostId(int $topicId): int { - $db = icms::$xoopsDB; - $result = $db->queryF('SELECT MIN(post_id) FROM ' . $db->prefix(IMFORUM_DIRNAME . '_post') . " WHERE topic_id = $topicId"); - $row = $result ? $db->fetchRow($result) : false; - return $row ? (int) $row[0] : 0; + return mod_imforum_Db::value('SELECT MIN(post_id) FROM ' . mod_imforum_Db::table('post') . ' WHERE topic_id = ?', [$topicId]); } /** diff --git a/src/class/OnlineHandler.php b/src/class/OnlineHandler.php index 1335e0d..040d12c 100644 --- a/src/class/OnlineHandler.php +++ b/src/class/OnlineHandler.php @@ -98,6 +98,6 @@ public function getOnlineMembers(array $uids, int $minutes = 15): array { * Drop rows that were not updated since a moment */ public function purgeOlderThan(int $time): bool { - return (bool) $this->db->queryF(sprintf('DELETE FROM %s WHERE updated < %d', $this->table, $time)); + return mod_imforum_Db::ok("DELETE FROM {$this->table} WHERE updated < ?", [$time]); } } diff --git a/src/class/PermtemplateHandler.php b/src/class/PermtemplateHandler.php index fb1cea0..5dbfefb 100644 --- a/src/class/PermtemplateHandler.php +++ b/src/class/PermtemplateHandler.php @@ -40,7 +40,7 @@ public function getTemplate(): array { * @param array> $template */ public function saveTemplate(array $template): bool { - $ok = (bool) $this->db->queryF("DELETE FROM {$this->table}"); + $ok = mod_imforum_Db::ok("DELETE FROM {$this->table}"); foreach (array_intersect_key($template, array_flip(mod_imforum_ForumHandler::PERMISSIONS)) as $name => $groups) { foreach (array_unique(array_map('intval', $groups)) as $groupId) { $row = $this->create(); diff --git a/src/class/PostHandler.php b/src/class/PostHandler.php index a4b88c2..068b0ea 100644 --- a/src/class/PostHandler.php +++ b/src/class/PostHandler.php @@ -25,18 +25,14 @@ public function __construct(icms_db_legacy_Database $db) { * recount per post: the topic handler recounts the forum once afterwards. */ public function deleteByTopic(int $topicId): bool { - $postIds = []; - $result = $this->db->queryF("SELECT post_id FROM {$this->table} WHERE topic_id = $topicId"); - while ($result && ($row = $this->db->fetchRow($result))) { - $postIds[] = (int) $row[0]; - } + $postIds = array_map(static fn (array $row): int => (int) $row[0], mod_imforum_Db::rows("SELECT post_id FROM {$this->table} WHERE topic_id = ?", [$topicId])); if (!$postIds) { return true; } foreach (array_chunk($postIds, 500) as $chunk) { $this->deletePostChildren($chunk); } - return (bool) $this->db->queryF("DELETE FROM {$this->table} WHERE topic_id = $topicId"); + return mod_imforum_Db::ok("DELETE FROM {$this->table} WHERE topic_id = ?", [$topicId]); } /** @@ -62,15 +58,15 @@ private function approvedIn(int $topicId): icms_db_criteria_Compo { * @return list forum id, topic id, number of posts, id of the first new post; by forum, busiest topic first */ public function getActivity(int $since, int $until): array { - $topics = $this->db->prefix(IMFORUM_DIRNAME . '_topic'); - $result = $this->db->queryF( + $topics = mod_imforum_Db::table('topic'); + $rows = []; + foreach (mod_imforum_Db::rows( 'SELECT p.forum_id, p.topic_id, COUNT(*) AS n, MIN(p.post_id) AS first_new, MAX(p.post_id) AS last_new' . " FROM {$this->table} p JOIN $topics t ON t.topic_id = p.topic_id" - . ' WHERE p.approved = 1 AND t.approved = 1 AND p.post_time >= ' . $since . ' AND p.post_time < ' . $until - . ' GROUP BY p.forum_id, p.topic_id ORDER BY p.forum_id, last_new DESC' - ); - $rows = []; - while ($result && ($row = $this->db->fetchRow($result))) { + . ' WHERE p.approved = 1 AND t.approved = 1 AND p.post_time >= ? AND p.post_time < ?' + . ' GROUP BY p.forum_id, p.topic_id ORDER BY p.forum_id, last_new DESC', + [$since, $until] + ) as $row) { $rows[] = [(int) $row[0], (int) $row[1], (int) $row[2], (int) $row[3]]; } return $rows; @@ -113,8 +109,7 @@ public function countApproved(int $topicId): int { */ public function getThreadOrder(int $topicId): array { $parentOf = []; - $result = $this->db->queryF("SELECT post_id, pid FROM {$this->table} WHERE topic_id = $topicId AND approved = 1 ORDER BY post_id"); - while ($result && ($row = $this->db->fetchRow($result))) { + foreach (mod_imforum_Db::rows("SELECT post_id, pid FROM {$this->table} WHERE topic_id = ? AND approved = 1 ORDER BY post_id", [$topicId]) as $row) { $parentOf[(int) $row[0]] = (int) $row[1]; } @@ -180,8 +175,7 @@ public function countByUsers(array $uids): array { return []; } $counts = []; - $result = $this->db->queryF('SELECT uid, COUNT(*) FROM ' . $this->table . ' WHERE approved = 1 AND uid IN (' . implode(',', $uids) . ') GROUP BY uid'); - while ($result && ($row = $this->db->fetchRow($result))) { + foreach (mod_imforum_Db::rows('SELECT uid, COUNT(*) FROM ' . $this->table . ' WHERE approved = 1 AND uid IN (' . mod_imforum_Db::marks($uids) . ') GROUP BY uid', $uids) as $row) { $counts[(int) $row[0]] = (int) $row[1]; } return $counts; @@ -199,8 +193,10 @@ public function getTopPosters(array $forumIds, int $limit): array { return []; } $top = []; - $result = $this->db->queryF('SELECT uid, COUNT(*) AS n FROM ' . $this->table . ' WHERE approved = 1 AND uid > 0 AND forum_id IN (' . implode(',', $forumIds) . ') GROUP BY uid ORDER BY n DESC, uid ASC LIMIT ' . max(1, $limit)); - while ($result && ($row = $this->db->fetchRow($result))) { + foreach (mod_imforum_Db::rows( + 'SELECT uid, COUNT(*) AS n FROM ' . $this->table . ' WHERE approved = 1 AND uid > 0 AND forum_id IN (' . mod_imforum_Db::marks($forumIds) . ') GROUP BY uid ORDER BY n DESC, uid ASC LIMIT ?', + [...$forumIds, max(1, $limit)] + ) as $row) { $top[] = ['uid' => (int) $row[0], 'posts' => (int) $row[1]]; } return $top; @@ -222,8 +218,7 @@ public function getSummaries(array $postIds): array { $postIds = array_values(array_filter(array_map('intval', $postIds))); $summaries = []; foreach (array_chunk($postIds, 500) as $chunk) { - $result = $this->db->queryF('SELECT post_id, topic_id, uid, poster_name, post_time, subject FROM ' . $this->table . ' WHERE post_id IN (' . implode(',', $chunk) . ')'); - while ($result && ($row = $this->db->fetchRow($result))) { + foreach (mod_imforum_Db::rows('SELECT post_id, topic_id, uid, poster_name, post_time, subject FROM ' . $this->table . ' WHERE post_id IN (' . mod_imforum_Db::marks($chunk) . ')', $chunk) as $row) { $summaries[(int) $row[0]] = [ 'post_id' => (int) $row[0], 'topic_id' => (int) $row[1], 'uid' => (int) $row[2], 'poster_name' => (string) $row[3], 'post_time' => (int) $row[4], 'subject' => (string) $row[5], @@ -291,10 +286,7 @@ protected function afterSave(mod_imforum_Post $post): bool { protected function afterDelete(mod_imforum_Post $post): bool { $postId = (int) $post->id(); $this->deletePostChildren([$postId]); - $this->db->queryF(sprintf( - 'UPDATE %s SET pid = %d WHERE pid = %d', - $this->table, (int) $post->getVar('pid', 'e'), $postId - )); + mod_imforum_Db::execute("UPDATE {$this->table} SET pid = ? WHERE pid = ?", [(int) $post->getVar('pid', 'e'), $postId]); $topicId = (int) $post->getVar('topic_id', 'e'); if ($this->getCount(new icms_db_criteria_Item('topic_id', $topicId)) === 0) { diff --git a/src/class/ReadmarkHandler.php b/src/class/ReadmarkHandler.php index d62992e..500c48b 100644 --- a/src/class/ReadmarkHandler.php +++ b/src/class/ReadmarkHandler.php @@ -64,10 +64,7 @@ public function getLastRead(int $type, int $uid, array $itemIds): array { * Forget all read marks for a forum or topic that is deleted */ public function deleteByItem(int $type, int $itemId): bool { - return (bool) $this->db->queryF(sprintf( - 'DELETE FROM %s WHERE read_type = %d AND item_id = %d', - $this->table, $type, $itemId - )); + return mod_imforum_Db::ok("DELETE FROM {$this->table} WHERE read_type = ? AND item_id = ?", [$type, $itemId]); } /** @@ -83,20 +80,18 @@ public function deleteByItem(int $type, int $itemId): bool { * @return int the number of topic marks removed */ public function purgeOlderThan(int $time): int { - $posts = $this->db->prefix(IMFORUM_DIRNAME . '_post'); - $topics = $this->db->prefix(IMFORUM_DIRNAME . '_topic'); + $posts = mod_imforum_Db::table('post'); + $topics = mod_imforum_Db::table('topic'); $topicType = self::TYPE_TOPIC; $forumType = self::TYPE_FORUM; $newestOld = []; - $result = $this->db->queryF("SELECT forum_id, MAX(post_id) FROM $posts WHERE approved = 1 AND post_time < $time GROUP BY forum_id"); - while ($result && ($row = $this->db->fetchRow($result))) { + foreach (mod_imforum_Db::rows("SELECT forum_id, MAX(post_id) FROM $posts WHERE approved = 1 AND post_time < ? GROUP BY forum_id", [$time]) as $row) { $newestOld[(int) $row[0]] = (int) $row[1]; } $pairs = []; - $result = $this->db->queryF("SELECT DISTINCT m.uid, t.forum_id FROM {$this->table} m JOIN $topics t ON t.topic_id = m.item_id WHERE m.read_type = $topicType AND m.read_time < $time"); - while ($result && ($row = $this->db->fetchRow($result))) { + foreach (mod_imforum_Db::rows("SELECT DISTINCT m.uid, t.forum_id FROM {$this->table} m JOIN $topics t ON t.topic_id = m.item_id WHERE m.read_type = ? AND m.read_time < ?", [$topicType, $time]) as $row) { $pairs[] = [(int) $row[0], (int) $row[1]]; } foreach ($pairs as [$uid, $forumId]) { @@ -109,13 +104,11 @@ public function purgeOlderThan(int $time): int { $removed = 0; foreach ([ - "DELETE FROM {$this->table} WHERE read_type = $topicType AND read_time < $time", - "DELETE m FROM {$this->table} m LEFT JOIN $topics t ON t.topic_id = m.item_id WHERE m.read_type = $topicType AND t.topic_id IS NULL", - "DELETE m FROM {$this->table} m JOIN $topics t ON t.topic_id = m.item_id JOIN {$this->table} f ON f.read_type = $forumType AND f.uid = m.uid AND f.item_id = t.forum_id WHERE m.read_type = $topicType AND m.post_id <= f.post_id", - ] as $sql) { - if ($this->db->queryF($sql)) { - $removed += max(0, (int) $this->db->getAffectedRows()); - } + ["DELETE FROM {$this->table} WHERE read_type = ? AND read_time < ?", [$topicType, $time]], + ["DELETE m FROM {$this->table} m LEFT JOIN $topics t ON t.topic_id = m.item_id WHERE m.read_type = ? AND t.topic_id IS NULL", [$topicType]], + ["DELETE m FROM {$this->table} m JOIN $topics t ON t.topic_id = m.item_id JOIN {$this->table} f ON f.read_type = ? AND f.uid = m.uid AND f.item_id = t.forum_id WHERE m.read_type = ? AND m.post_id <= f.post_id", [$forumType, $topicType]], + ] as [$sql, $params]) { + $removed += mod_imforum_Db::execute($sql, $params) ?? 0; } return $removed; } @@ -124,11 +117,10 @@ public function purgeOlderThan(int $time): int { * Drop a member's marks on the topics of a forum, once the forum itself is marked */ public function deleteTopicMarksInForum(int $uid, int $forumId): bool { - $topics = $this->db->prefix(IMFORUM_DIRNAME . '_topic'); - return (bool) $this->db->queryF(sprintf( - 'DELETE FROM %s WHERE read_type = %d AND uid = %d AND item_id IN (SELECT topic_id FROM %s WHERE forum_id = %d)', - $this->table, self::TYPE_TOPIC, $uid, $topics, $forumId - )); + return mod_imforum_Db::ok( + "DELETE FROM {$this->table} WHERE read_type = ? AND uid = ? AND item_id IN (SELECT topic_id FROM " . mod_imforum_Db::table('topic') . ' WHERE forum_id = ?)', + [self::TYPE_TOPIC, $uid, $forumId] + ); } /** @@ -138,14 +130,15 @@ public function deleteTopicMarksInForum(int $uid, int $forumId): bool { * mark on the topic and the mark on its forum ("mark all as read"). The * topic table must be aliased t. * - * @return array{joins: string, where: string} + * @return array{joins: string, where: string, params: list} the values for the placeholders of the joins, in order */ public function getUnreadSql(int $uid): array { $read = $this->table; return [ - 'joins' => " LEFT JOIN $read rt ON rt.read_type = " . self::TYPE_TOPIC . " AND rt.uid = $uid AND rt.item_id = t.topic_id" - . " LEFT JOIN $read rf ON rf.read_type = " . self::TYPE_FORUM . " AND rf.uid = $uid AND rf.item_id = t.forum_id", + 'joins' => " LEFT JOIN $read rt ON rt.read_type = ? AND rt.uid = ? AND rt.item_id = t.topic_id" + . " LEFT JOIN $read rf ON rf.read_type = ? AND rf.uid = ? AND rf.item_id = t.forum_id", 'where' => ' AND t.topic_last_post_id > GREATEST(COALESCE(rt.post_id, 0), COALESCE(rf.post_id, 0))', + 'params' => [self::TYPE_TOPIC, $uid, self::TYPE_FORUM, $uid], ]; } @@ -161,11 +154,10 @@ public function countUnreadByForum(int $uid, array $forumIds): array { return []; } $unread = $this->getUnreadSql($uid); - $sql = 'SELECT t.forum_id, COUNT(*) FROM ' . $this->db->prefix(IMFORUM_DIRNAME . '_topic') . ' t' . $unread['joins'] - . ' WHERE t.approved = 1 AND t.forum_id IN (' . implode(',', $forumIds) . ')' . $unread['where'] . ' GROUP BY t.forum_id'; + $sql = 'SELECT t.forum_id, COUNT(*) FROM ' . mod_imforum_Db::table('topic') . ' t' . $unread['joins'] + . ' WHERE t.approved = 1 AND t.forum_id IN (' . mod_imforum_Db::marks($forumIds) . ')' . $unread['where'] . ' GROUP BY t.forum_id'; $counts = []; - $result = $this->db->queryF($sql); - while ($result && ($row = $this->db->fetchRow($result))) { + foreach (mod_imforum_Db::rows($sql, [...$unread['params'], ...$forumIds]) as $row) { $counts[(int) $row[0]] = (int) $row[1]; } return $counts; diff --git a/src/class/ReportHandler.php b/src/class/ReportHandler.php index 77e4ddb..ab76929 100644 --- a/src/class/ReportHandler.php +++ b/src/class/ReportHandler.php @@ -26,10 +26,10 @@ public function getOpenCount(): int { * Open reports about posts of one forum */ public function countOpenInForum(int $forumId): int { - $posts = $this->db->prefix(IMFORUM_DIRNAME . '_post'); - $result = $this->db->queryF("SELECT COUNT(*) FROM {$this->table} r JOIN $posts p ON p.post_id = r.post_id WHERE r.report_result = " . mod_imforum_Report::RESULT_OPEN . " AND p.forum_id = $forumId"); - $row = $result ? $this->db->fetchRow($result) : false; - return $row ? (int) $row[0] : 0; + return mod_imforum_Db::value( + "SELECT COUNT(*) FROM {$this->table} r JOIN " . mod_imforum_Db::table('post') . ' p ON p.post_id = r.post_id WHERE r.report_result = ? AND p.forum_id = ?', + [mod_imforum_Report::RESULT_OPEN, $forumId] + ); } protected function beforeInsert(mod_imforum_Report $report): bool { diff --git a/src/class/TopicHandler.php b/src/class/TopicHandler.php index a1eb181..ac0b2ff 100644 --- a/src/class/TopicHandler.php +++ b/src/class/TopicHandler.php @@ -24,24 +24,22 @@ public function __construct(icms_db_legacy_Database $db) { * Only approved posts count; the first post is not a reply. */ public function synchronise(int $topicId): bool { - $posts = $this->db->prefix(IMFORUM_DIRNAME . '_post'); - $result = $this->db->queryF("SELECT COUNT(*), MIN(post_id), MAX(post_id) FROM $posts WHERE topic_id = $topicId AND approved = 1"); - $row = $result ? $this->db->fetchRow($result) : false; - [$count, $first, $last] = $row ? array_values($row) : [0, 0, 0]; - - // queryF: the counters follow a write that has already been allowed - return (bool) $this->db->queryF(sprintf( - 'UPDATE %s SET topic_replies = %d, topic_first_post_id = %d, topic_last_post_id = %d WHERE topic_id = %d', - $this->table, max((int) $count - 1, 0), (int) $first, (int) $last, $topicId - )); + $posts = mod_imforum_Db::table('post'); + $row = mod_imforum_Db::row("SELECT COUNT(*), MIN(post_id), MAX(post_id) FROM $posts WHERE topic_id = ? AND approved = 1", [$topicId]); + [$count, $first, $last] = $row ?: [0, 0, 0]; + + return mod_imforum_Db::ok( + "UPDATE {$this->table} SET topic_replies = ?, topic_first_post_id = ?, topic_last_post_id = ? WHERE topic_id = ?", + [max((int) $count - 1, 0), (int) $first, (int) $last, $topicId] + ); } /** * Recount every topic in one query; used after an import or a prune */ public function synchroniseAll(): bool { - $posts = $this->db->prefix(IMFORUM_DIRNAME . '_post'); - return (bool) $this->db->queryF("UPDATE {$this->table} t" + $posts = mod_imforum_Db::table('post'); + return mod_imforum_Db::ok("UPDATE {$this->table} t" . " LEFT JOIN (SELECT topic_id, COUNT(*) AS n, MIN(post_id) AS first, MAX(post_id) AS last FROM $posts WHERE approved = 1 GROUP BY topic_id) p ON p.topic_id = t.topic_id" . ' SET t.topic_replies = GREATEST(COALESCE(p.n, 0) - 1, 0), t.topic_first_post_id = COALESCE(p.first, 0), t.topic_last_post_id = COALESCE(p.last, 0)'); } @@ -57,20 +55,15 @@ public function findInactive(array $forumIds, int $before, bool $keepSticky = tr if (!$forumIds) { return []; } - $posts = $this->db->prefix(IMFORUM_DIRNAME . '_post'); + $posts = mod_imforum_Db::table('post'); $sql = "SELECT t.topic_id FROM {$this->table} t" . " LEFT JOIN $posts p ON p.post_id = t.topic_last_post_id" - . ' WHERE t.forum_id IN (' . implode(',', $forumIds) . ')' - . ' AND COALESCE(p.post_time, t.topic_time) < ' . $before + . ' WHERE t.forum_id IN (' . mod_imforum_Db::marks($forumIds) . ')' + . ' AND COALESCE(p.post_time, t.topic_time) < ?' . ($keepSticky ? ' AND t.topic_sticky = 0' : '') . ($keepDigest ? ' AND t.topic_digest = 0' : ''); - $ids = []; - $result = $this->db->queryF($sql); - while ($result && ($row = $this->db->fetchRow($result))) { - $ids[] = (int) $row[0]; - } - return $ids; + return array_map(static fn (array $row): int => (int) $row[0], mod_imforum_Db::rows($sql, [...$forumIds, $before])); } /** @@ -87,8 +80,7 @@ public function moveToForum(int $topicId, int $forumId): bool { return true; } - $posts = $this->db->prefix(IMFORUM_DIRNAME . '_post'); - $ok = (bool) $this->db->queryF("UPDATE $posts SET forum_id = $forumId WHERE topic_id = $topicId"); + $ok = mod_imforum_Db::ok('UPDATE ' . mod_imforum_Db::table('post') . ' SET forum_id = ? WHERE topic_id = ?', [$forumId, $topicId]); $topic->setVar('forum_id', $forumId); $ok = $this->insert($topic, true) && $ok; return $forumHandler->synchronise($oldForumId) && $ok; @@ -152,7 +144,8 @@ public function findRecent(array $forumIds, int $start, int $limit, string $filt return ['ids' => [], 'total' => 0]; } $joins = ''; - $where = ' WHERE t.approved = 1 AND t.forum_id IN (' . implode(',', $forumIds) . ')'; + $joinParams = []; + $where = ' WHERE t.approved = 1 AND t.forum_id IN (' . mod_imforum_Db::marks($forumIds) . ')'; if ($filter === 'digest') { $where .= ' AND t.topic_digest = 1'; } elseif ($filter === 'unread') { @@ -161,20 +154,17 @@ public function findRecent(array $forumIds, int $start, int $limit, string $filt } $unread = icms_getModuleHandler('readmark', IMFORUM_DIRNAME)->getUnreadSql($uid); $joins = $unread['joins']; + $joinParams = $unread['params']; $where .= $unread['where']; } - $total = 0; - $result = $this->db->queryF("SELECT COUNT(*) FROM {$this->table} t$joins$where"); - if ($result && ($row = $this->db->fetchRow($result))) { - $total = (int) $row[0]; - } - - $ids = []; - $result = $this->db->queryF("SELECT t.topic_id FROM {$this->table} t$joins$where ORDER BY t.topic_last_post_id DESC LIMIT " . max(0, $start) . ', ' . max(1, $limit)); - while ($result && ($row = $this->db->fetchRow($result))) { - $ids[] = (int) $row[0]; - } + // the placeholders of the joins come first in the statement + $params = [...$joinParams, ...$forumIds]; + $total = mod_imforum_Db::value("SELECT COUNT(*) FROM {$this->table} t$joins$where", $params); + $ids = array_map( + static fn (array $row): int => (int) $row[0], + mod_imforum_Db::rows("SELECT t.topic_id FROM {$this->table} t$joins$where ORDER BY t.topic_last_post_id DESC LIMIT ?, ?", [...$params, max(0, $start), max(1, $limit)]) + ); return ['ids' => $ids, 'total' => $total]; } @@ -189,24 +179,20 @@ public function findHot(array $forumIds, int $since, int $limit): array { if (!$forumIds) { return []; } - $posts = $this->db->prefix(IMFORUM_DIRNAME . '_post'); - $ids = []; - $result = $this->db->queryF( + $posts = mod_imforum_Db::table('post'); + return array_map(static fn (array $row): int => (int) $row[0], mod_imforum_Db::rows( "SELECT t.topic_id FROM {$this->table} t JOIN $posts p ON p.post_id = t.topic_last_post_id" - . ' WHERE t.approved = 1 AND t.topic_replies > 0 AND t.forum_id IN (' . implode(',', $forumIds) . ') AND p.post_time >= ' . max(0, $since) - . ' ORDER BY t.topic_replies DESC, t.topic_last_post_id DESC LIMIT ' . max(1, $limit) - ); - while ($result && ($row = $this->db->fetchRow($result))) { - $ids[] = (int) $row[0]; - } - return $ids; + . ' WHERE t.approved = 1 AND t.topic_replies > 0 AND t.forum_id IN (' . mod_imforum_Db::marks($forumIds) . ') AND p.post_time >= ?' + . ' ORDER BY t.topic_replies DESC, t.topic_last_post_id DESC LIMIT ?', + [...$forumIds, max(0, $since), max(1, $limit)] + )); } /** * Count a view */ public function incrementViews(int $topicId): bool { - return (bool) $this->db->queryF("UPDATE {$this->table} SET topic_views = topic_views + 1 WHERE topic_id = $topicId"); + return mod_imforum_Db::ok("UPDATE {$this->table} SET topic_views = topic_views + 1 WHERE topic_id = ?", [$topicId]); } protected function beforeInsert(mod_imforum_Topic $topic): bool { diff --git a/src/include/onupdate.php b/src/include/onupdate.php index c6a471b..1ee2d21 100644 --- a/src/include/onupdate.php +++ b/src/include/onupdate.php @@ -104,16 +104,15 @@ function imforum_indexes(): array { * @param string $type '' for a regular index, 'UNIQUE' or 'FULLTEXT' */ function imforum_add_index(string $item, string $name, array $columns, string $type = ''): bool { - $db = icms::$xoopsDB; - $table = $db->prefix(IMFORUM_DIRNAME . '_' . $item); + $table = mod_imforum_Db::table($item); - $result = $db->queryF("SHOW INDEX FROM `$table` WHERE Key_name = " . $db->quoteString($name)); - $exists = $result ? $db->getRowsNum($result) > 0 : false; - if ($exists) { - return true; + // column 2 of SHOW INDEX is the name of the index + foreach (mod_imforum_Db::rows("SHOW INDEX FROM `$table`") as $index) { + if ($index[2] === $name) { + return true; + } } - $sql = sprintf('ALTER TABLE `%s` ADD %s INDEX `%s` (`%s`)', $table, $type, $name, implode('`, `', $columns)); - return (bool) $db->queryF($sql); + return mod_imforum_Db::ok(sprintf('ALTER TABLE `%s` ADD %s INDEX `%s` (`%s`)', $table, $type, $name, implode('`, `', $columns))); } /** diff --git a/src/include/search.inc.php b/src/include/search.inc.php index 23758ad..2885fb2 100644 --- a/src/include/search.inc.php +++ b/src/include/search.inc.php @@ -30,30 +30,32 @@ function imforum_search($queryarray, $andor, $limit, $offset, $userid) { if (!$forumIds) { return []; } - $db = icms::$xoopsDB; - $posts = $db->prefix(IMFORUM_DIRNAME . '_post'); + $posts = mod_imforum_Db::table('post'); - $where = 'p.approved = 1 AND p.forum_id IN (' . implode(',', array_map('intval', $forumIds)) . ')'; + $forumIds = array_map('intval', $forumIds); + $params = $forumIds; + $where = 'p.approved = 1 AND p.forum_id IN (' . mod_imforum_Db::marks($forumIds) . ')'; if ((int) $userid > 0) { - $where .= ' AND p.uid = ' . (int) $userid; + $where .= ' AND p.uid = ?'; + $params[] = (int) $userid; } if (is_array($queryarray) && $queryarray) { $conditions = []; foreach ($queryarray as $word) { // % and _ in a word are plain characters - $like = $db->quoteString('%' . addcslashes((string) $word, '\\%_') . '%'); - $conditions[] = "(p.subject LIKE $like OR (p.post_karma = 0 AND p.require_reply = 0 AND p.post_text LIKE $like))"; + $like = '%' . addcslashes((string) $word, '\\%_') . '%'; + $conditions[] = '(p.subject LIKE ? OR (p.post_karma = 0 AND p.require_reply = 0 AND p.post_text LIKE ?))'; + array_push($params, $like, $like); } $where .= ' AND (' . implode($andor === 'OR' ? ' OR ' : ' AND ', $conditions) . ')'; } $hits = []; - $result = $db->query( - "SELECT p.post_id, p.topic_id, p.uid, p.post_time, p.subject FROM $posts p WHERE $where ORDER BY p.post_id DESC", - max(1, (int) $limit), - max(0, (int) $offset) + $rows = mod_imforum_Db::rows( + "SELECT p.post_id, p.topic_id, p.uid, p.post_time, p.subject FROM $posts p WHERE $where ORDER BY p.post_id DESC LIMIT ?, ?", + [...$params, max(0, (int) $offset), max(1, (int) $limit)] ); - while ($result && ($row = $db->fetchRow($result))) { + foreach ($rows as $row) { $hits[] = [ 'image' => 'images/imforum_iconsmall.png', 'link' => 'topic.php?topic=' . (int) $row[1] . '&post=' . (int) $row[0] . '#post-' . (int) $row[0], diff --git a/tests/Integration/DbTest.php b/tests/Integration/DbTest.php new file mode 100644 index 0000000..765485d --- /dev/null +++ b/tests/Integration/DbTest.php @@ -0,0 +1,98 @@ +table, Db::table('topic')); + } + + public function testValuesAreBoundAndNeverGluedIntoTheSql(): void + { + $forumId = (int) self::forum((int) self::category()->id())->id(); + $evil = "x' OR '1'='1"; + $topic = self::make('topic', ['forum_id' => $forumId, 'topic_title' => $evil, 'topic_poster' => 1]); + + self::assertSame(1, Db::value('SELECT COUNT(*) FROM ' . Db::table('topic') . ' WHERE topic_title = ?', [$evil])); + self::assertSame(0, Db::value('SELECT COUNT(*) FROM ' . Db::table('topic') . ' WHERE topic_title = ?', ["nothing' OR '1'='1"])); + self::assertSame([(int) $topic->id()], array_map('intval', Db::row('SELECT topic_id FROM ' . Db::table('topic') . ' WHERE topic_title = ?', [$evil]))); + } + + public function testIntegersWorkInALimitAndListsGetOnePlaceholderPerValue(): void + { + $forumId = (int) self::forum((int) self::category()->id())->id(); + $ids = []; + for ($i = 0; $i < 5; $i++) { + $ids[] = (int) self::topic($forumId, "Topic $i")->id(); + } + $table = Db::table('topic'); + self::assertSame('?,?,?', Db::marks([1, 2, 3])); + $rows = Db::rows("SELECT topic_id FROM $table WHERE topic_id IN (" . Db::marks($ids) . ') ORDER BY topic_id LIMIT ?, ?', [...$ids, 1, 2]); + self::assertSame([$ids[1], $ids[2]], array_map(static fn (array $row): int => (int) $row[0], $rows)); + } + + public function testWritesReportTheRowsChangedAndFailuresAreVisible(): void + { + $forumId = (int) self::forum((int) self::category()->id())->id(); + self::topic($forumId); + self::topic($forumId, 'Second'); + $table = Db::table('topic'); + self::assertSame(2, Db::execute("UPDATE $table SET topic_views = ? WHERE forum_id = ?", [7, $forumId])); + self::assertSame(0, Db::execute("UPDATE $table SET topic_views = ? WHERE forum_id = ?", [7, 99999])); + self::assertNull(Db::execute('UPDATE imforum_no_such_table SET a = 1')); + self::assertFalse(Db::ok('DELETE FROM imforum_no_such_table')); + self::assertTrue(Db::ok("DELETE FROM $table WHERE forum_id = ?", [99999])); + self::assertSame([], Db::rows('SELECT * FROM imforum_no_such_table')); + } + + public function testItNeedsThePdoConnection(): void + { + $saved = \icms::$db; + \icms::$db = null; + try { + $this->expectException(\RuntimeException::class); + Db::connection(); + } finally { + \icms::$db = $saved; + } + } + + public function testSearchKeepsWorkingWithBoundWords(): void + { + $category = (int) self::category()->id(); + $forumId = (int) self::forum($category)->id(); + self::grantCategory($category, [3]); + self::grant($forumId, ['forum_access', 'forum_view'], [3]); + Settings::override([]); + $topic = self::topic($forumId, '100% sure'); + self::topic($forumId, 'Other thing'); + + require_once ICMS_ROOT_PATH . '/modules/imforum/include/search.inc.php'; + $permissionsGuest = new \mod_imforum_PermissionService([3], 0); + self::assertSame([$forumId], $permissionsGuest->getReadableForumIds()); + $user = \icms::$user; + \icms::$user = null; + try { + $hits = imforum_search(['100%'], 'AND', 10, 0, 0); + self::assertCount(1, $hits, '% in a word is a plain character'); + self::assertStringContainsString('topic=' . $topic->id(), $hits[0]['link']); + self::assertCount(0, imforum_search(["' OR '1'='1"], 'AND', 10, 0, 0)); + self::assertCount(2, imforum_search(['sure', 'thing'], 'OR', 10, 0, 0)); + self::assertCount(1, imforum_search(['sure', 'thing'], 'OR', 1, 1, 0), 'limit and offset'); + } finally { + \icms::$user = $user; + Settings::override(null); + } + } +} diff --git a/tests/integration-bootstrap.php b/tests/integration-bootstrap.php index 2ea7ddc..0a462db 100644 --- a/tests/integration-bootstrap.php +++ b/tests/integration-bootstrap.php @@ -79,6 +79,7 @@ class_exists('icms_config_item_Handler'); // defines the ICMS_CONF_* constants icms::$config = new \Imforum\Tests\Integration\CoreConfig(); icms::$preload = icms_preload_Handler::getInstance(); icms::$logger = icms_core_Logger::instance(); +icms::$db = icms_db_Factory::pdoInstance(); icms::$xoopsDB = icms_db_Factory::instance(); foreach (['global', 'core', 'databaseupdater'] as $file) { diff --git a/tests/phpstan-bootstrap.php b/tests/phpstan-bootstrap.php index 488bef3..850030f 100644 --- a/tests/phpstan-bootstrap.php +++ b/tests/phpstan-bootstrap.php @@ -11,6 +11,7 @@ define('ICMS_URL', 'http://localhost'); define('ICMS_UPLOAD_PATH', $core . '/uploads'); define('ICMS_UPLOAD_URL', 'http://localhost/uploads'); +define('XOOPS_DB_PREFIX', 'icms'); define('ICMS_GROUP_ADMIN', 1); define('ICMS_GROUP_USERS', 2); define('ICMS_GROUP_ANONYMOUS', 3); From 79d37e7b57f20490f329929293ba0ecb2d9f2c28 Mon Sep 17 00:00:00 2001 From: David Janssens Date: Mon, 26 Oct 2026 22:12:17 +0100 Subject: [PATCH 4/5] Use the dhtmltextarea control so the WYSIWYG editor loads in the post, forum and category forms Co-Authored-By: Claude Sonnet 5.5 --- src/class/Category.php | 2 +- src/class/Forum.php | 2 +- src/class/Post.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/class/Category.php b/src/class/Category.php index f33432e..216c804 100644 --- a/src/class/Category.php +++ b/src/class/Category.php @@ -25,7 +25,7 @@ public function __construct(mod_imforum_CategoryHandler $handler) { $this->initCommonVar('weight'); $this->initiateSEO(); - $this->setControl('cat_description', ['name' => 'textarea', 'form_editor' => 'default']); + $this->setControl('cat_description', 'dhtmltextarea'); $this->setControl('cat_image', ['name' => 'image']); } diff --git a/src/class/Forum.php b/src/class/Forum.php index 72bfe33..3526505 100644 --- a/src/class/Forum.php +++ b/src/class/Forum.php @@ -46,7 +46,7 @@ public function __construct(mod_imforum_ForumHandler $handler) { $this->setControl('cat_id', ['itemHandler' => 'category', 'method' => 'getCategorySelectList', 'module' => IMFORUM_DIRNAME]); $this->setControl('parent_forum', ['itemHandler' => 'forum', 'method' => 'getParentSelectList', 'module' => IMFORUM_DIRNAME]); - $this->setControl('forum_desc', ['name' => 'textarea', 'form_editor' => 'default']); + $this->setControl('forum_desc', 'dhtmltextarea'); foreach (['forum_active', 'allow_html', 'allow_sig', 'allow_subject_prefix'] as $key) { $this->setControl($key, ['name' => 'yesno']); } diff --git a/src/class/Post.php b/src/class/Post.php index 86d1852..d4451c8 100644 --- a/src/class/Post.php +++ b/src/class/Post.php @@ -50,7 +50,7 @@ public function __construct(mod_imforum_PostHandler $handler) { $this->hideFieldFromForm(['topic_id', 'forum_id', 'pid', 'uid', 'poster_ip', 'post_time', 'post_edit', 'approved']); - $this->setControl('post_text', ['name' => 'textarea', 'form_editor' => 'default']); + $this->setControl('post_text', 'dhtmltextarea'); foreach (['attachsig', 'require_reply'] as $key) { $this->setControl($key, ['name' => 'yesno']); } From 47428968206cdda19c89c8936b7f9fafd39c3f95 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 26 Oct 2026 21:13:48 +0000 Subject: [PATCH 5/5] Store posts written in an editor as HTML and keep the dhtmltextarea control typed The WYSIWYG editor sends HTML: such a post gets dohtml on and dobr off and is shown through the purifier. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01MZy45V5RV9nx6Buq8vqKHr --- CHANGELOG.md | 1 + src/class/Category.php | 2 +- src/class/Forum.php | 2 +- src/class/Post.php | 2 +- src/class/PostForm.php | 7 +++++++ src/class/PostService.php | 4 ++-- src/post.php | 3 ++- tests/Integration/WritingTest.php | 8 ++++++++ 8 files changed, 23 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 531328c..d264849 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,3 +12,4 @@ While the module is in development the status carries the phase: 1.0.0 Alpha 5 i - Phase 5: moderation. `moderate.php` and one `ModerationService` that holds all rules: lock and unlock, pin, feature, move (the moderator must moderate both forums), merge (counters, last post, views and read marks follow), split from a post onward (replies are re-attached to the new first post), delete a topic, approve waiting posts (subscribers are told only then), a queue of waiting posts and a list of open reports per forum, and suspensions with a number of days (reading stays possible; moderators and webmasters cannot be suspended). `report.php` lets members report a post, one open report per member and post. Every action is a confirmation form followed by a POST with a security token. Restoring pruned topics from the archive stays an admin task for now. Integration tests cover each action, the counters after merge and split, and a suspended member who can read but not write. - Phase 6: background tasks. Two autotasks, registered by the core when the module is installed or updated: the digest mail and a cleanup. Members subscribe to the digest with the notification "Digest" for all forums. `DigestMailer` covers the time since the last digest (one day for the first), lists titles, counts and links but never the text of a post, sends each member only the forums that member may read, and stores the complete digest for the administrators before it sends, so a failure halfway never causes a second mail. The cleanup removes visitors that are no longer online, read marks older than a preference (90 days; 0 keeps them) and suspensions that ended more than 30 days ago. A topic without a read mark counts as unread, so posts older than the limit first count as read for members who have marks, and only then the old marks go. - Database access: all hand-written SQL now goes through `mod_imforum_Db` over the core's PDO connection (`icms::$db`) with bound parameters, instead of `queryF()` on the legacy adapter (`icms::$xoopsDB`) with values glued into the SQL. The IPF handlers keep the legacy adapter the core gives them for their own queries; both share one PDO connection. Without PDO (`XOOPS_DB_TYPE` not `pdo.*`) the module stops with a clear message. Prepared statements do not show up in the debug console of the core, which only logs `query()`. +- Editor: the post, forum and category forms use the `dhtmltextarea` control, as the content module does, so the WYSIWYG editor chosen in the site preferences loads (the plain `textarea` control of the IPF ignores the editor). A post written in an editor is stored as HTML (`dohtml` on, line breaks off) and is shown through the purifier; without an editor the text still stays as typed. The quick reply and the moderation forms stay plain textareas. diff --git a/src/class/Category.php b/src/class/Category.php index 216c804..067e812 100644 --- a/src/class/Category.php +++ b/src/class/Category.php @@ -25,7 +25,7 @@ public function __construct(mod_imforum_CategoryHandler $handler) { $this->initCommonVar('weight'); $this->initiateSEO(); - $this->setControl('cat_description', 'dhtmltextarea'); + $this->setControl('cat_description', ['name' => 'dhtmltextarea']); $this->setControl('cat_image', ['name' => 'image']); } diff --git a/src/class/Forum.php b/src/class/Forum.php index 3526505..5085c8b 100644 --- a/src/class/Forum.php +++ b/src/class/Forum.php @@ -46,7 +46,7 @@ public function __construct(mod_imforum_ForumHandler $handler) { $this->setControl('cat_id', ['itemHandler' => 'category', 'method' => 'getCategorySelectList', 'module' => IMFORUM_DIRNAME]); $this->setControl('parent_forum', ['itemHandler' => 'forum', 'method' => 'getParentSelectList', 'module' => IMFORUM_DIRNAME]); - $this->setControl('forum_desc', 'dhtmltextarea'); + $this->setControl('forum_desc', ['name' => 'dhtmltextarea']); foreach (['forum_active', 'allow_html', 'allow_sig', 'allow_subject_prefix'] as $key) { $this->setControl($key, ['name' => 'yesno']); } diff --git a/src/class/Post.php b/src/class/Post.php index d4451c8..2ff5ff6 100644 --- a/src/class/Post.php +++ b/src/class/Post.php @@ -50,7 +50,7 @@ public function __construct(mod_imforum_PostHandler $handler) { $this->hideFieldFromForm(['topic_id', 'forum_id', 'pid', 'uid', 'poster_ip', 'post_time', 'post_edit', 'approved']); - $this->setControl('post_text', 'dhtmltextarea'); + $this->setControl('post_text', ['name' => 'dhtmltextarea']); foreach (['attachsig', 'require_reply'] as $key) { $this->setControl($key, ['name' => 'yesno']); } diff --git a/src/class/PostForm.php b/src/class/PostForm.php index f881aa5..18aa014 100644 --- a/src/class/PostForm.php +++ b/src/class/PostForm.php @@ -69,6 +69,13 @@ public static function build( $form = new icms_ipf_form_Secure($form_post, 'imforum_post', $caption, mod_imforum_View::url('post'), null, $mode === 'edit' ? _MD_IMFORUM_SAVE : _MD_IMFORUM_POST); $form->addCustomButton('preview_button', _MD_IMFORUM_PREVIEW); + // a WYSIWYG editor sends HTML: the post is then stored as HTML (shown through the purifier), without line break conversion + foreach ($form->getElements() as $element) { + if ($element instanceof icms_form_elements_Dhtmltextarea && $element->getName() === 'post_text' && is_object($element->htmlEditor)) { + self::add($form, new icms_form_elements_Hidden('wysiwyg', '1')); + break; + } + } self::add($form, new icms_form_elements_Hidden('do', $mode)); self::add($form, new icms_form_elements_Hidden('forum', (string) $forum->id())); if ($topic !== null) { diff --git a/src/class/PostService.php b/src/class/PostService.php index af35e20..ecfe6f1 100644 --- a/src/class/PostService.php +++ b/src/class/PostService.php @@ -246,11 +246,11 @@ private function clean(array $input, mod_imforum_Forum $forum, bool $startsTopic 'attachsig' => $member && (int) $forum->getVar('allow_sig', 'e') === 1 ? $flag('attachsig') : 0, 'post_karma' => $karma, 'require_reply' => $member ? $flag('require_reply') : 0, - 'dohtml' => (int) $forum->getVar('allow_html', 'e') === 1 ? $flag('dohtml') : 0, + 'dohtml' => $flag('wysiwyg') === 1 || ((int) $forum->getVar('allow_html', 'e') === 1 && $flag('dohtml') === 1) ? 1 : 0, 'dosmiley' => $flag('dosmiley'), 'doxcode' => $flag('doxcode'), 'doimage' => $flag('doimage'), - 'dobr' => $flag('dobr'), + 'dobr' => $flag('wysiwyg') === 1 ? 0 : $flag('dobr'), ], $errors]; } diff --git a/src/post.php b/src/post.php index f805745..44c43cd 100644 --- a/src/post.php +++ b/src/post.php @@ -191,7 +191,8 @@ foreach (['subject', 'post_text'] as $field) { $sample->setVar($field, is_string($values[$field] ?? null) ? $values[$field] : ''); } - $sample->setVar('dohtml', (int) $forum->getVar('allow_html', 'e') === 1 && !empty($values['dohtml']) ? 1 : 0); + $sample->setVar('dohtml', !empty($values['wysiwyg']) || ((int) $forum->getVar('allow_html', 'e') === 1 && !empty($values['dohtml'])) ? 1 : 0); + $values['dobr'] = empty($values['wysiwyg']) ? ($values['dobr'] ?? 0) : 0; foreach (['dosmiley', 'doxcode', 'doimage', 'dobr'] as $field) { $sample->setVar($field, !empty($values[$field]) ? 1 : 0); } diff --git a/tests/Integration/WritingTest.php b/tests/Integration/WritingTest.php index bc58cea..836c768 100644 --- a/tests/Integration/WritingTest.php +++ b/tests/Integration/WritingTest.php @@ -150,6 +150,14 @@ public function testPostsWaitForApprovalWithoutTheRight(): void self::assertSame([], $notifier->calls, 'nobody is told about a post nobody can read'); } + public function testAnEditorPostIsStoredAsHtmlWithoutLineBreakConversion(): void + { + $post = $this->service()->createTopic($this->forumObject(), self::input() + ['wysiwyg' => 1, 'dobr' => 1])['post']; + + self::assertSame(1, (int) $post->getVar('dohtml', 'e'), 'the editor sends HTML, shown through the purifier'); + self::assertSame(0, (int) $post->getVar('dobr', 'e')); + } + public function testWhatTheFormMayNotSetIsIgnored(): void { $input = self::input() + ['dohtml' => 1, 'attachsig' => 1, 'post_karma' => 999999, 'require_reply' => 1, 'uid' => 99, 'approved' => 0, 'forum_id' => 77, 'topic_id' => 77];