Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ 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.
- 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.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 0 additions & 4 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 7 additions & 9 deletions src/class/ArchiveHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
2 changes: 1 addition & 1 deletion src/class/AttachmentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/class/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', ['name' => 'dhtmltextarea']);
$this->setControl('cat_image', ['name' => 'image']);
}

Expand Down
35 changes: 35 additions & 0 deletions src/class/Cleanup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Periodic housekeeping for the autotask: visitors list, read marks, ended suspensions
*
* @copyright The iForum project
* @license MIT, see LICENSE
* @package imforum
* @since 1.0
*/

defined('ICMS_ROOT_PATH') || die('ImpressCMS root path not defined');

include_once dirname(__DIR__) . '/include/common.php';

class mod_imforum_Cleanup {

/** Suspensions stay in the admin list this long after they ended */
public const KEEP_SUSPENSIONS_DAYS = 30;

/**
* @return array{online: bool, read_marks: int, suspensions: int}
*/
public static function run(?int $now = null): array {
$now ??= time();
$minutes = mod_imforum_Settings::getCount('online_minutes');
$online = icms_getModuleHandler('online', IMFORUM_DIRNAME)->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];
}
}
119 changes: 119 additions & 0 deletions src/class/Db.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php
/**
* Raw SQL for the module, through the core's PDO connection (icms::$db)
*
* The IPF handlers keep using the legacy adapter they are given for their own
* queries. Everything written by hand in this module goes through here,
* with bound parameters instead of values glued into the SQL. Both adapters
* share one PDO connection, so they see each other's writes at once.
*
* @copyright The iForum project
* @license MIT, see LICENSE
* @package imforum
* @since 1.0
*/

defined('ICMS_ROOT_PATH') || die('ImpressCMS root path not defined');

include_once dirname(__DIR__) . '/include/common.php';

class mod_imforum_Db {

/**
* The PDO connection of the core
*
* icms::$db is empty when the site still runs the legacy mysql adapter,
* which PHP 8 does not have anymore.
*
* @throws RuntimeException
*/
public static function connection(): PDO {
$connection = icms::$db;
if (!$connection instanceof PDO) {
throw new RuntimeException('imforum needs the PDO database connection: set XOOPS_DB_TYPE to pdo.mysql in mainfile.php.');
}
return $connection;
}

/**
* Full name of a table of this module: prefix, module and item
*/
public static function table(string $item): string {
return XOOPS_DB_PREFIX . '_' . IMFORUM_DIRNAME . '_' . $item;
}

/**
* Placeholders for a list: "?,?,?"
*
* @param list<mixed> $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<int|string|null> $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<int|string|null> $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<int|string|null> $params
* @return list<list<int|string|null>> 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<int|string|null> $params
* @return list<int|string|null>
*/
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<int|string|null> $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<int|string|null> $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;
}
}
4 changes: 1 addition & 3 deletions src/class/DigestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading
Loading