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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
- Phase 0: module skeleton for ImpressCMS 2.0 in IPF mode, install and update routines, English and Dutch language files, CI on PHP 8.2 to 8.5.
- Phase 1: data model. 13 IPF objects with handlers (category, forum, moderator, topic, post, attachment, report, suspension, readmark, online, digest, archive, permtemplate), group permissions on categories and forums, counters and cascading deletes through handler events, indexes in `imforum_db_upgrade_1()`, labels in English and Dutch, integration tests against MySQL 5.5, MySQL 5.7 (MyISAM) and MariaDB 10.11.
- Phase 2: administration. Admin screens on `icms_ipf_view_Table` and `getSecureForm()` for categories and forums (moderators and group permissions on the forum form), a permission template that new forums start with and that can be applied to existing forums, reports, suspensions, digests, the archive, prune (with a preview and a confirmation) and a full recount. Every operation that writes runs only on POST with a valid security token, deletion confirmations included. Works around three core issues: a missing admin menu on the first request after the menu cache is rebuilt, `count(null)` when a permission select is left empty, and the date picker failing when a form is built before `icms_cp_header()`.
- 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.
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 2 (administration). Categories, forums, moderators, group permissions, reports, suspensions, digests, the archive and pruning are managed from the admin area. There are no forum pages for visitors yet.
Status: phase 3 (reading). Visitors can read the forum index, forums, topics (flat or threaded), the latest, unread and featured topics, the archive and an RSS feed, and search the forums. Everything is filtered by the group permissions, read marks, karma and required replies are applied, and three blocks are available. Posting, editing and moderating from the front end come in later phases. Categories, forums, moderators, permissions, reports, suspensions, digests, the archive and pruning are managed from the admin area.

## Requirements

Expand Down
16 changes: 16 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,19 @@ parameters:
# 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
message: '#^Parameter \#[2-5] \$(smiley|icode|image|br) of static method icms_core_DataFilter::filterTextareaDisplay\(\) expects bool, int given\.$#'
identifier: argument.type
-
# formatTimestamp() documents a string, but takes the integer timestamps that every caller passes
message: '#^Parameter \#1 \$time of function formatTimestamp expects string, int#'
identifier: argument.type
-
# icms::$user is documented as an object, but it is null for guests
identifier: ternary.alwaysTrue
path: src/*.php
-
identifier: function.alreadyNarrowedType
message: '#^Call to function is_object\(\) with icms_member_user_Object#'
85 changes: 85 additions & 0 deletions src/archive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* Archived topics, read only
*
* @copyright The iForum project
* @license MIT, see LICENSE
* @package imforum
* @since 1.0
*/

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

$permissions = mod_imforum_PermissionService::forCurrentUser();
$archiveHandler = icms_getModuleHandler('archive', IMFORUM_DIRNAME);
$topicHandler = icms_getModuleHandler('topic', IMFORUM_DIRNAME);
$postHandler = icms_getModuleHandler('post', IMFORUM_DIRNAME);

$topicId = mod_imforum_View::intParam('topic');
$topic = null;
if ($topicId > 0) {
$topic = $topicHandler->get($topicId);
$forum = $topic->isNew() ? null : $permissions->getForum((int) $topic->getVar('forum_id', 'e'));
if ($forum === null || !$permissions->canReadForum($forum)) {
redirect_header(mod_imforum_View::url('archive'), 3, _NOPERM);
exit;
}
}

$xoopsOption['template_main'] = 'imforum_archive.html';
include_once ICMS_ROOT_PATH . '/header.php';
global $icmsTpl, $xoTheme;
$xoTheme->addStylesheet(IMFORUM_URL . 'assets/css/imforum.css');

$listUrl = mod_imforum_View::url('archive');
if ($topic !== null) {
$archived = $archiveHandler->getByTopic($topicId);
// the posts still exist: the author's wish for karma or a reply holds for the archived text too
$live = $postHandler->getByIds(array_map(static fn (mod_imforum_Archive $entry): int => (int) $entry->getVar('post_id', 'e'), $archived));
$karma = new mod_imforum_KarmaService($permissions->getUid());
$moderator = $permissions->isModerator((int) $topic->getVar('forum_id', 'e'));
$posts = [];
foreach ($archived as $entry) {
$postId = (int) $entry->getVar('post_id', 'e');
$post = $live[$postId] ?? null;
$lock = $post === null ? mod_imforum_KarmaService::LOCK_NONE : $karma->apply($post, $moderator);
$posts[] = [
'id' => $postId,
'subject' => $post === null ? '' : $post->getVar('subject'),
'author' => $post === null ? '' : mod_imforum_View::poster((int) $post->getVar('uid', 'e'), (string) $post->getVar('poster_name', 'n')),
'time' => $post === null ? '' : mod_imforum_View::time((int) $post->getVar('post_time', 'e')),
'archived' => mod_imforum_View::time((int) $entry->getVar('archived_time', 'e')),
'lock' => $lock,
'text' => $lock !== mod_imforum_KarmaService::LOCK_NONE ? '' : (string) icms_core_DataFilter::filterTextareaDisplay((string) $entry->getVar('post_text', 'n'), 1, 1, 1, 1),
];
}
$icmsTpl->assign('imforum_breadcrumb', mod_imforum_View::breadcrumb([[_MD_IMFORUM_ARCHIVE, $listUrl], [$topic->getVar('topic_title', 'n')]]));
$icmsTpl->assign('imforum_archive_topic', [
'title' => $topic->getVar('topic_title'),
'url' => mod_imforum_View::link('topic', ['topic' => $topicId]),
]);
$icmsTpl->assign('imforum_archive_posts', $posts);
} else {
$perPage = mod_imforum_Settings::getCount('topics_per_page');
$start = mod_imforum_View::intParam('start');
$page = $archiveHandler->getTopicsPage($permissions->getReadableForumIds(), $start, $perPage);
$topics = $topicHandler->getByIds(array_column($page['rows'], 'topic_id'));
$rows = [];
foreach ($page['rows'] as $row) {
if (!isset($topics[$row['topic_id']])) {
continue;
}
$rows[] = [
'url' => mod_imforum_View::link('archive', ['topic' => $row['topic_id']]),
'title' => $topics[$row['topic_id']]->getVar('topic_title'),
'posts' => $row['posts'],
'archived' => mod_imforum_View::time($row['archived_time']),
];
}
$icmsTpl->assign('imforum_breadcrumb', mod_imforum_View::breadcrumb([[_MD_IMFORUM_ARCHIVE]]));
$icmsTpl->assign('imforum_archive_topics', $rows);
$icmsTpl->assign('imforum_pager', mod_imforum_View::pager('archive', [], $page['total'], $perPage, $start));
}

include_once ICMS_ROOT_PATH . '/footer.php';
57 changes: 57 additions & 0 deletions src/assets/css/imforum.css
Original file line number Diff line number Diff line change
@@ -1 +1,58 @@
/* Forum-specific additions on top of the ImpressCMS 2.0 default theme. */

.imforum { margin: 0 0 1.5em; }
.imforum-breadcrumb { margin: 0 0 .75em; font-size: .9em; }
.imforum-sep { margin: 0 .35em; opacity: .6; }
.imforum-tools { margin: 0 0 .75em; }
.imforum-meta { font-size: .85em; opacity: .75; }
.imforum-notice { padding: .5em .75em; border-left: 3px solid currentColor; background: rgba(128, 128, 128, .12); }

.imforum table.outer { width: 100%; margin: 0 0 1em; border-collapse: collapse; }
.imforum table.outer th,
.imforum table.outer td { padding: .5em .65em; vertical-align: top; text-align: left; }
.imforum .imforum-num { width: 5em; text-align: center; white-space: nowrap; }
.imforum .imforum-last { width: 14em; }
.imforum .imforum-flags { width: 2em; text-align: center; }
.imforum .imforum-read { opacity: .35; }
.imforum tr.imforum-unread a { font-weight: bold; }

.imforum-badge { display: inline-block; padding: 0 .4em; margin-right: .3em; border-radius: .6em; font-size: .75em; line-height: 1.6; background: rgba(128, 128, 128, .25); }
.imforum-badge.imforum-new { background: #2a7a3b; color: #fff; }
.imforum-pages { font-size: .8em; }

.imforum-pager { margin: .75em 0; }
.imforum-pager a,
.imforum-pager strong,
.imforum-pager .imforum-gap { display: inline-block; min-width: 1.6em; padding: .15em .4em; text-align: center; }

.imforum-post { display: flex; gap: 1em; margin: 0 0 1em; padding: .75em; border: 1px solid rgba(128, 128, 128, .35); border-radius: 4px; }
.imforum-poster { flex: 0 0 11em; word-break: break-word; }
.imforum-avatar { display: block; max-width: 96px; max-height: 96px; margin: 0 0 .4em; }
.imforum-post-body { flex: 1 1 auto; min-width: 0; }
.imforum-post-head { display: flex; flex-wrap: wrap; gap: .5em; align-items: baseline; margin: 0 0 .5em; padding: 0 0 .4em; border-bottom: 1px solid rgba(128, 128, 128, .25); }
.imforum-permalink { opacity: .6; }
.imforum-post-text { overflow-wrap: anywhere; }
.imforum-signature { margin: .75em 0 0; padding: .4em 0 0; border-top: 1px dashed rgba(128, 128, 128, .4); font-size: .85em; opacity: .8; }

.imforum-depth-1 { margin-left: 1.5em; }
.imforum-depth-2 { margin-left: 3em; }
.imforum-depth-3 { margin-left: 4.5em; }
.imforum-depth-4 { margin-left: 6em; }
.imforum-depth-5 { margin-left: 7.5em; }
.imforum-depth-6 { margin-left: 9em; }
.imforum-depth-7 { margin-left: 10.5em; }
.imforum-depth-8 { margin-left: 12em; }

.imforum-online { margin: 1em 0 0; }
.imforum-online h3 { margin: 0 0 .25em; font-size: 1em; }
.imforum-markread { margin: 1em 0; }
.imforum-block { margin: 0; padding-left: 1.2em; }
.imforum-block li { margin: 0 0 .4em; }

@media (max-width: 40em) {
.imforum-post { flex-direction: column; }
.imforum-poster { flex-basis: auto; }
.imforum .imforum-last { width: auto; }
.imforum-depth-2, .imforum-depth-3, .imforum-depth-4, .imforum-depth-5,
.imforum-depth-6, .imforum-depth-7, .imforum-depth-8 { margin-left: 1.5em; }
}
94 changes: 94 additions & 0 deletions src/blocks/imforum_blocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/**
* Blocks: the latest topics, the busiest topics and the most active members
*
* The blocks show only what the visitor may read.
*
* @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';
icms_loadLanguageFile(IMFORUM_DIRNAME, 'main');

/**
* Rows of topics for a block
*
* @param list<int> $topicIds
* @return list<array<string, mixed>>
*/
function imforum_block_topic_rows(array $topicIds, mod_imforum_PermissionService $permissions): array {
$topics = icms_getModuleHandler('topic', IMFORUM_DIRNAME)->getByIds($topicIds);
$summaries = icms_getModuleHandler('post', IMFORUM_DIRNAME)->getSummaries(array_map(
static fn (mod_imforum_Topic $topic): int => (int) $topic->getVar('topic_last_post_id', 'e'),
$topics
));
$rows = [];
foreach ($topics as $topicId => $topic) {
$lastPostId = (int) $topic->getVar('topic_last_post_id', 'e');
$forum = $permissions->getForum((int) $topic->getVar('forum_id', 'e'));
$rows[] = [
'url' => mod_imforum_View::link('topic', ['topic' => $topicId, 'post' => $lastPostId]) . '#post-' . $lastPostId,
'title' => $topic->getVar('topic_title'),
'forum' => $forum === null ? '' : $forum->getVar('forum_name'),
'replies' => (int) $topic->getVar('topic_replies', 'e'),
'time' => mod_imforum_View::time(isset($summaries[$lastPostId]) ? $summaries[$lastPostId]['post_time'] : (int) $topic->getVar('topic_time', 'e')),
];
}
return $rows;
}

/**
* @param array<int, string|int> $options 0: number of topics
* @return array<string, mixed>|false
*/
function imforum_block_recent($options) {
$permissions = mod_imforum_PermissionService::forCurrentUser();
$found = icms_getModuleHandler('topic', IMFORUM_DIRNAME)->findRecent($permissions->getReadableForumIds(), 0, max(1, (int) ($options[0] ?? 10)));
$rows = imforum_block_topic_rows($found['ids'], $permissions);
return $rows ? ['topics' => $rows, 'all_url' => mod_imforum_View::link('viewall')] : false;
}

/**
* @param array<int, string|int> $options 0: number of topics, 1: days to look back
* @return array<string, mixed>|false
*/
function imforum_block_hot($options) {
$permissions = mod_imforum_PermissionService::forCurrentUser();
$days = max(1, (int) ($options[1] ?? 30));
$ids = icms_getModuleHandler('topic', IMFORUM_DIRNAME)->findHot($permissions->getReadableForumIds(), time() - $days * 86400, max(1, (int) ($options[0] ?? 10)));
$rows = imforum_block_topic_rows($ids, $permissions);
return $rows ? ['topics' => $rows, 'all_url' => mod_imforum_View::link('viewall')] : false;
}

/**
* @param array<int, string|int> $options 0: number of members
* @return array<string, mixed>|false
*/
function imforum_block_topposters($options) {
$permissions = mod_imforum_PermissionService::forCurrentUser();
$rows = [];
foreach (icms_getModuleHandler('post', IMFORUM_DIRNAME)->getTopPosters($permissions->getReadableForumIds(), max(1, (int) ($options[0] ?? 10))) as $poster) {
$rows[] = ['member' => mod_imforum_View::poster($poster['uid'], ''), 'posts' => $poster['posts']];
}
return $rows ? ['posters' => $rows] : false;
}

/**
* @param array<int, string|int> $options
*/
function imforum_block_edit_recent($options): string {
return '<label>' . _MI_IMFORUM_BLOCK_OPT_COUNT . ' <input type="text" name="options[0]" value="' . (int) ($options[0] ?? 10) . '" size="3" /></label>';
}

/**
* @param array<int, string|int> $options
*/
function imforum_block_edit_hot($options): string {
return '<label>' . _MI_IMFORUM_BLOCK_OPT_COUNT . ' <input type="text" name="options[0]" value="' . (int) ($options[0] ?? 10) . '" size="3" /></label><br />'
. '<label>' . _MI_IMFORUM_BLOCK_OPT_DAYS . ' <input type="text" name="options[1]" value="' . (int) ($options[1] ?? 30) . '" size="3" /></label>';
}
39 changes: 39 additions & 0 deletions src/class/ArchiveHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,45 @@ public function __construct(icms_db_legacy_Database $db) {
parent::__construct($db, 'archive', 'archive_id', '', 'post_text', IMFORUM_DIRNAME);
}

/**
* Topics with archived posts, the most recently archived first
*
* @param list<int> $forumIds the forums the reader may open
* @return array{rows: list<array{topic_id: int, posts: int, archived_time: int}>, total: int}
*/
public function getTopicsPage(array $forumIds, int $start, int $limit): array {
$forumIds = array_values(array_filter(array_map('intval', $forumIds)));
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) . ')';

$total = 0;
$result = $this->db->queryF("SELECT COUNT(DISTINCT a.topic_id)$from");
if ($result && ($row = $this->db->fetchRow($result))) {
$total = (int) $row[0];
}
$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))) {
$rows[] = ['topic_id' => (int) $row[0], 'posts' => (int) $row[1], 'archived_time' => (int) $row[2]];
}
return ['rows' => $rows, 'total' => $total];
}

/**
* The archived posts of a topic, oldest first
*
* @return list<mod_imforum_Archive>
*/
public function getByTopic(int $topicId): array {
$criteria = new icms_db_criteria_Compo(new icms_db_criteria_Item('topic_id', $topicId));
$criteria->setSort('post_id');
$criteria->setOrder('ASC');
return array_values($this->getObjects($criteria));
}

protected function beforeInsert(mod_imforum_Archive $archive): bool {
if ((int) $archive->getVar('archived_time', 'e') === 0) {
$archive->setVar('archived_time', time());
Expand Down
Loading
Loading