diff --git a/CHANGELOG.md b/CHANGELOG.md index fa86462..33013b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,3 +6,4 @@ - 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. +- 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. diff --git a/README.md b/README.md index 24b02d9..d1b1f83 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 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. +Status: phase 4 (writing). 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. Moderation from the front end (move, merge, split, lock, approve, reports) comes in phase 5. Categories, forums, moderators, permissions, reports, suspensions, digests, the archive and pruning are managed from the admin area. ## Requirements diff --git a/phpstan.neon.dist b/phpstan.neon.dist index eb54d78..9efbe05 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -26,6 +26,10 @@ parameters: # many core docblocks use "str" and "unknown_type", which are not types message: '#^Parameter \#\d \$\w+ of (method|class) icms_\w+(::\w+\(\)| constructor) expects (str|unknown_type), #' identifier: argument.type + - + # icms_ipf_form_Secure::__construct() documents the IPF object as a string + message: '#^Parameter \#1 \$target of class icms_ipf_form_Secure constructor expects string, mod_imforum_\w+ given\.$#' + identifier: argument.type - # 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\.$#' diff --git a/src/assets/css/imforum.css b/src/assets/css/imforum.css index 58ecebd..1409b86 100644 --- a/src/assets/css/imforum.css +++ b/src/assets/css/imforum.css @@ -56,3 +56,16 @@ .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; } } + +/* writing */ +.imforum .imforum-actions { margin: 0.75em 0; } +.imforum .imforum-button { display: inline-block; padding: 0.3em 0.9em; border: 1px solid currentColor; border-radius: 3px; text-decoration: none; font-weight: bold; } +.imforum .imforum-prefix { font-weight: bold; } +.imforum .imforum-post-actions { margin-top: 0.75em; text-align: right; font-size: 0.9em; } +.imforum .imforum-post-actions a { margin-left: 0.9em; } +.imforum .imforum-attachments ul { margin: 0.25em 0 0 1.25em; padding: 0; } +.imforum .imforum-quickreply { margin-top: 1.5em; } +.imforum .imforum-quickreply textarea { width: 100%; box-sizing: border-box; } +.imforum .imforum-errors p { margin: 0.25em 0; } +.imforum .imforum-disclaimer { margin: 0.75em 0; padding: 0.5em 0.75em; border-left: 3px solid currentColor; opacity: 0.85; } +.imforum .imforum-confirm { margin: 1em 0; } diff --git a/src/attachment.php b/src/attachment.php new file mode 100644 index 0000000..7a0510a --- /dev/null +++ b/src/attachment.php @@ -0,0 +1,56 @@ +get(mod_imforum_View::intParam('id')); +$post = $attachment->isNew() ? null : icms_getModuleHandler('post', IMFORUM_DIRNAME)->get((int) $attachment->getVar('post_id', 'e')); +$forum = ($post !== null && !$post->isNew()) ? $permissions->getForum((int) $post->getVar('forum_id', 'e')) : null; + +$allowed = $forum !== null + && $permissions->canReadForum($forum) + && (int) $attachment->getVar('online', 'e') === 1 + && ((int) $post->getVar('approved', 'e') === 1 || $permissions->isModerator((int) $forum->id())); +if ($allowed) { + $karma = new mod_imforum_KarmaService($permissions->getUid()); + $allowed = $karma->getLock($post, $permissions->isModerator((int) $forum->id())) === mod_imforum_KarmaService::LOCK_NONE; +} +$path = $allowed ? $attachment->getFilePath() : ''; +if (!$allowed || !is_file($path)) { + http_response_code($allowed ? 404 : 403); + header('Content-Type: text/plain; charset=utf-8'); + echo $allowed ? 'Not found' : _NOPERM; + exit; +} + +icms_getModuleHandler('attachment', IMFORUM_DIRNAME)->incrementDownloads((int) $attachment->id()); + +$name = (string) $attachment->getVar('name_display', 'n'); +$safeName = preg_replace('/[^A-Za-z0-9._-]+/', '_', $name) ?: 'attachment'; +$mime = (string) $attachment->getVar('mimetype', 'e'); +$inline = in_array($mime, ['image/gif', 'image/jpeg', 'image/png', 'image/webp'], true) && isset($_GET['inline']); + +while (ob_get_level() > 0) { + ob_end_clean(); +} +header('Content-Type: ' . ($inline ? $mime : 'application/octet-stream')); +header('Content-Disposition: ' . ($inline ? 'inline' : 'attachment') . '; filename="' . $safeName . '"; filename*=UTF-8\'\'' . rawurlencode($name)); +header('Content-Length: ' . (int) filesize($path)); +header('X-Content-Type-Options: nosniff'); +header("Content-Security-Policy: default-src 'none'; sandbox"); +readfile($path); +exit; diff --git a/src/class/AttachmentHandler.php b/src/class/AttachmentHandler.php index e1b27b0..90ea0f7 100644 --- a/src/class/AttachmentHandler.php +++ b/src/class/AttachmentHandler.php @@ -18,6 +18,36 @@ public function __construct(icms_db_legacy_Database $db) { parent::__construct($db, 'attachment', 'attach_id', 'name_display', '', IMFORUM_DIRNAME); } + /** + * The attachments of these posts that are online + * + * @param list $postIds + * @return array> post id => attachments, oldest first + */ + public function getByPosts(array $postIds): array { + $postIds = array_values(array_filter(array_map('intval', $postIds))); + if (!$postIds) { + return []; + } + $criteria = new icms_db_criteria_Compo(new icms_db_criteria_Item('post_id', '(' . implode(',', $postIds) . ')', 'IN')); + $criteria->add(new icms_db_criteria_Item('online', 1)); + $criteria->setSort('attach_id'); + $criteria->setOrder('ASC'); + $byPost = []; + foreach ($this->getObjects($criteria) as $attachment) { + $byPost[(int) $attachment->getVar('post_id', 'e')][] = $attachment; + } + return $byPost; + } + + public function incrementDownloads(int $attachId): bool { + return (bool) $this->db->queryF("UPDATE {$this->table} SET downloads = downloads + 1 WHERE attach_id = $attachId"); + } + + public function countForPost(int $postId): int { + return $this->getCount(new icms_db_criteria_Item('post_id', $postId)); + } + protected function beforeInsert(mod_imforum_Attachment $attachment): bool { if ((int) $attachment->getVar('attach_time', 'e') === 0) { $attachment->setVar('attach_time', time()); diff --git a/src/class/Notifier.php b/src/class/Notifier.php new file mode 100644 index 0000000..919e53d --- /dev/null +++ b/src/class/Notifier.php @@ -0,0 +1,110 @@ +getModuleInfo()->getVar('mid'); + $forumId = (int) $forum->id(); + $topicId = (int) $topic->id(); + + $events = []; + if ($newTopic) { + $events[] = ['forum', $forumId, 'new_thread']; + $events[] = ['global', 0, 'new_thread']; + } else { + $events[] = ['thread', $topicId, 'new_post']; + } + $events[] = ['forum', $forumId, 'new_post']; + $events[] = ['global', 0, 'new_post']; + + $tags = [ + 'X_POST_URL' => mod_imforum_View::url('topic', ['topic' => $topicId, 'post' => (int) $post->id()]) . '#post-' . (int) $post->id(), + 'X_POST_SUBJECT' => (string) $post->getVar('subject', 'n'), + 'X_TOPIC_TITLE' => (string) $topic->getVar('topic_title', 'n'), + 'X_FORUM_NAME' => (string) $forum->getVar('forum_name', 'n'), + 'X_POSTER' => $this->posterName($post), + ]; + $author = (int) $post->getVar('uid', 'e'); + $told = []; + foreach ($events as [$category, $itemId, $event]) { + $subscribers = $this->getSubscribers($moduleId, $category, $itemId, $event); + // a member who gets the mail for one event is not told again for another + $readers = array_values(array_diff($this->filterReaders($subscribers, $forum), $told, [$author])); + if (!$readers) { + continue; + } + $told = array_merge($told, $readers); + $notifications->triggerEvent($category, $itemId, $event, $tags, $readers, $moduleId, $author); + } + } + + /** + * Of these members, those who may read the forum + * + * @param list $uids + * @param (callable(int): list)|null $groupsOf the groups of a member; the core member handler by default + * @return list + */ + public function filterReaders(array $uids, mod_imforum_Forum $forum, ?callable $groupsOf = null): array { + $groupsOf ??= static fn (int $uid): array => array_map('intval', (array) icms::handler('icms_member')->getGroupsByUser($uid)); + $readers = []; + foreach (array_unique($uids) as $uid) { + $groups = $groupsOf($uid); + $permissions = new mod_imforum_PermissionService($groups, $uid, in_array((int) ICMS_GROUP_ADMIN, $groups, true)); + if ($permissions->canReadForum($forum)) { + $readers[] = $uid; + } + } + return $readers; + } + + /** + * The category and event are names from icms_version.php, never request input + * + * @return list + */ + private function getSubscribers(int $moduleId, string $category, int $itemId, string $event): array { + $criteria = new icms_db_criteria_Compo(new icms_db_criteria_Item('not_modid', $moduleId)); + $criteria->add(new icms_db_criteria_Item('not_category', $category)); + $criteria->add(new icms_db_criteria_Item('not_itemid', $itemId)); + $criteria->add(new icms_db_criteria_Item('not_event', $event)); + $uids = []; + foreach (icms::handler('icms_data_notification')->getObjects($criteria) as $notification) { + $uids[] = (int) $notification->getVar('not_uid'); + } + return $uids; + } + + private function posterName(mod_imforum_Post $post): string { + $uid = (int) $post->getVar('uid', 'e'); + if ($uid > 0) { + $name = icms_member_user_Handler::getUserLink($uid); + return trim(strip_tags((string) $name)); + } + global $icmsConfig; + $name = trim((string) $post->getVar('poster_name', 'n')); + return $name !== '' ? $name : (string) ($icmsConfig['anonymous'] ?? ''); + } +} diff --git a/src/class/PermissionService.php b/src/class/PermissionService.php index 08caa37..f9f55c7 100644 --- a/src/class/PermissionService.php +++ b/src/class/PermissionService.php @@ -105,6 +105,91 @@ public function canReadForum(mod_imforum_Forum $forum): bool { return $this->webmaster || ($this->canSeeForum($forum) && $this->has('forum_view', (int) $forum->id())); } + /** + * Start a topic in the forum + */ + public function canStartTopic(mod_imforum_Forum $forum): bool { + return $this->canReadForum($forum) && $this->has('forum_post', (int) $forum->id()) && !$this->isSuspended((int) $forum->id()); + } + + /** + * Reply in a topic; a locked topic takes replies from moderators only + */ + public function canReply(mod_imforum_Forum $forum, mod_imforum_Topic $topic): bool { + $forumId = (int) $forum->id(); + if (!$this->canReadForum($forum) || !$this->has('forum_reply', $forumId) || $this->isSuspended($forumId)) { + return false; + } + return !$topic->isLocked() || $this->isModerator($forumId); + } + + /** + * Members change their own posts for a while; moderators change any post + */ + public function canEdit(mod_imforum_Forum $forum, mod_imforum_Topic $topic, mod_imforum_Post $post): bool { + $forumId = (int) $forum->id(); + if (!$this->canReadForum($forum)) { + return false; + } + if ($this->isModerator($forumId)) { + return true; + } + return $this->isAuthor($post) && $this->has('forum_edit', $forumId) && !$topic->isLocked() + && !$this->isSuspended($forumId) && $this->isWithinTimeLimit($post); + } + + /** + * Members delete their own last post of a topic for a while, so no reply loses what it answered + */ + public function canDelete(mod_imforum_Forum $forum, mod_imforum_Topic $topic, mod_imforum_Post $post): bool { + $forumId = (int) $forum->id(); + if (!$this->canReadForum($forum)) { + return false; + } + if ($this->isModerator($forumId)) { + return true; + } + return $this->isAuthor($post) && $this->has('forum_delete', $forumId) && !$topic->isLocked() + && !$this->isSuspended($forumId) && $this->isWithinTimeLimit($post) + && (int) $topic->getVar('topic_last_post_id', 'e') === (int) $post->id(); + } + + public function canAttach(mod_imforum_Forum $forum): bool { + return $this->has('forum_attach', (int) $forum->id()); + } + + /** + * Posts of these members appear at once instead of waiting for a moderator + */ + public function skipsApproval(mod_imforum_Forum $forum): bool { + return $this->isModerator((int) $forum->id()) || $this->has('forum_noapprove', (int) $forum->id()); + } + + /** + * A suspension on the member or the address, for this forum or every forum; moderators are exempt + */ + public function isSuspended(int $forumId): bool { + if ($this->webmaster || $this->isModerator($forumId)) { + return false; + } + return icms_getModuleHandler('suspension', IMFORUM_DIRNAME)->isSuspended($this->uid, mod_imforum_View::clientIp(), $forumId); + } + + /** + * Guests never own a post: their name is only text + */ + private function isAuthor(mod_imforum_Post $post): bool { + return $this->uid > 0 && (int) $post->getVar('uid', 'e') === $this->uid; + } + + /** + * 0 minutes means no limit + */ + private function isWithinTimeLimit(mod_imforum_Post $post): bool { + $minutes = (int) mod_imforum_Settings::get('edit_timelimit'); + return $minutes <= 0 || time() - (int) $post->getVar('post_time', 'e') <= $minutes * 60; + } + public function isModerator(int $forumId): bool { if ($this->webmaster) { return true; diff --git a/src/class/Post.php b/src/class/Post.php index 40273c7..86d1852 100644 --- a/src/class/Post.php +++ b/src/class/Post.php @@ -30,7 +30,11 @@ public function __construct(mod_imforum_PostHandler $handler) { $this->quickInitVar('poster_ip', XOBJ_DTYPE_TXTBOX); $this->quickInitVar('post_time', XOBJ_DTYPE_LTIME, false, '', '', 0); $this->quickInitVar('subject', XOBJ_DTYPE_TXTBOX, true); - $this->quickInitVar('post_text', XOBJ_DTYPE_TXTAREA, true); + // Stored as typed. XOBJ_DTYPE_TXTAREA would run the text through HTMLPurifier + // when it is saved, which turns BBCode into HTML for good and makes the + // switches of the post (HTML, smilies, line breaks) meaningless. The text + // is made safe when it is shown, in getFormattedText(). + $this->quickInitVar('post_text', XOBJ_DTYPE_OTHER, true); // JSON list of edits; not a PHP array var, so nothing is ever unserialised $this->quickInitVar('post_edit', XOBJ_DTYPE_OTHER); $this->quickInitVar('icon', XOBJ_DTYPE_TXTBOX); diff --git a/src/class/PostForm.php b/src/class/PostForm.php new file mode 100644 index 0000000..f881aa5 --- /dev/null +++ b/src/class/PostForm.php @@ -0,0 +1,129 @@ + $values what the form starts with + */ + public static function build( + mod_imforum_Post $form_post, + mod_imforum_Forum $forum, + ?mod_imforum_Topic $topic, + ?mod_imforum_Post $existing, + string $mode, + array $values, + mod_imforum_PermissionService $permissions, + bool $captcha + ): icms_ipf_form_Secure { + $member = $permissions->getUid() > 0; + $startsTopic = $mode === 'new' || ($mode === 'edit' && $topic !== null && $existing !== null + && (int) $topic->getVar('topic_first_post_id', 'e') === (int) $existing->id()); + + $hidden = ['icon']; + if ($member || $mode === 'edit') { + $hidden[] = 'poster_name'; + } + if (!(int) $forum->getVar('allow_html', 'e')) { + $hidden[] = 'dohtml'; + } + if (!$member || !(int) $forum->getVar('allow_sig', 'e')) { + $hidden[] = 'attachsig'; + } + if (!$member) { + array_push($hidden, 'post_karma', 'require_reply'); + } + $form_post->hideFieldFromForm($hidden); + + foreach (['subject', 'post_text', 'poster_name'] as $field) { + $form_post->setVar($field, is_string($values[$field] ?? null) ? $values[$field] : ''); + } + // the text is stored as typed, so the form must escape it or a post could close the textarea + $form_post->setVar('post_text', htmlspecialchars((string) $form_post->getVar('post_text', 'n'), ENT_QUOTES, 'UTF-8')); + foreach (['attachsig', 'require_reply', 'dohtml', 'dosmiley', 'doxcode', 'doimage', 'dobr'] as $field) { + $form_post->setVar($field, !empty($values[$field]) ? 1 : 0); + } + $form_post->setVar('post_karma', max(0, (int) ($values['post_karma'] ?? 0))); + + $caption = match ($mode) { + 'new' => _MD_IMFORUM_NEW_TOPIC, + 'reply' => _MD_IMFORUM_REPLY, + default => _MD_IMFORUM_EDIT, + }; + $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); + + 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) { + self::add($form, new icms_form_elements_Hidden('topic', (string) $topic->id())); + } + if ($existing !== null && $mode === 'edit') { + self::add($form, new icms_form_elements_Hidden('post', (string) $existing->id())); + } + if ($mode === 'reply' && !empty($values['pid'])) { + self::add($form, new icms_form_elements_Hidden('pid', (string) (int) $values['pid'])); + } + + $prefixes = mod_imforum_PostService::getPrefixes(); + if ($startsTopic && $prefixes && (int) $forum->getVar('allow_subject_prefix', 'e') === 1) { + $select = new icms_form_elements_Select(_MD_IMFORUM_PREFIX, 'prefix', (int) ($values['prefix'] ?? 0)); + $select->addOption('0', _MD_IMFORUM_PREFIX_NONE); + $select->addOptionArray($prefixes); + self::add($form, $select); + } + + if ($permissions->canAttach($forum) && (int) mod_imforum_Settings::get('attach_max_files') > 0) { + $uploader = new mod_imforum_Uploader($forum); + $note = sprintf(_MD_IMFORUM_ATTACH_NOTE, (int) $forum->getVar('attach_maxkb', 'e'), htmlspecialchars(implode(', ', $uploader->getAcceptedExtensions()), ENT_QUOTES, 'UTF-8')); + $limit = max(1, (int) mod_imforum_Settings::get('attach_max_files')); + $kept = 0; + if ($existing !== null && $mode === 'edit') { + $attachments = icms_getModuleHandler('attachment', IMFORUM_DIRNAME)->getByPosts([(int) $existing->id()])[(int) $existing->id()] ?? []; + $kept = count($attachments); + if ($attachments) { + $box = new icms_form_elements_Checkbox(_MD_IMFORUM_ATTACH_REMOVE, 'remove_attachment'); + foreach ($attachments as $attachment) { + $box->addOption((string) $attachment->id(), htmlspecialchars((string) $attachment->getVar('name_display', 'n'), ENT_QUOTES, 'UTF-8')); + } + self::add($form, $box); + } + } + for ($i = 0; $i < max(0, $limit - $kept); $i++) { + $file = new icms_form_elements_File($i === 0 ? _MD_IMFORUM_ATTACH : '', 'attachment_' . $i, (int) $forum->getVar('attach_maxkb', 'e') * 1024); + if ($i === 0) { + $file->setDescription($note); + } + self::add($form, $file); + } + } + + if ($captcha) { + self::add($form, new icms_form_elements_Captcha(_MD_IMFORUM_CAPTCHA, 'icmscaptcha', true)); + } + return $form; + } + + /** + * icms_ipf_form_Base::addElement() takes its element by reference + */ + private static function add(icms_ipf_form_Base $form, icms_form_Element $element): void { + $form->addElement($element); + } +} diff --git a/src/class/PostHandler.php b/src/class/PostHandler.php index 59c0ea7..5fb7853 100644 --- a/src/class/PostHandler.php +++ b/src/class/PostHandler.php @@ -213,6 +213,27 @@ public function getPosition(int $topicId, int $postId): int { return (int) $this->getCount($criteria); } + /** + * When the member or the address posted last, for the flood check; 0 when never + */ + public function getLastPostTime(int $uid, string $ip): int { + $who = new icms_db_criteria_Compo(); + if ($uid > 0) { + $who->add(new icms_db_criteria_Item('uid', $uid)); + } elseif (filter_var($ip, FILTER_VALIDATE_IP) !== false) { + // criteria values are not escaped by the core, so only a valid address goes in + $who->add(new icms_db_criteria_Item('poster_ip', $ip)); + $who->add(new icms_db_criteria_Item('uid', 0)); + } else { + return 0; + } + $who->setSort('post_time'); + $who->setOrder('DESC'); + $who->setLimit(1); + $posts = $this->getObjects($who); + return $posts ? (int) reset($posts)->getVar('post_time', 'e') : 0; + } + protected function beforeInsert(mod_imforum_Post $post): bool { if ((int) $post->getVar('post_time', 'e') === 0) { $post->setVar('post_time', time()); diff --git a/src/class/PostService.php b/src/class/PostService.php new file mode 100644 index 0000000..af35e20 --- /dev/null +++ b/src/class/PostService.php @@ -0,0 +1,328 @@ +permissions = $permissions; + $this->notifier = $notifier; + } + + /** + * The topic prefixes of the preferences, numbered from 1 as topic_subject stores them + * + * @return array + */ + public static function getPrefixes(): array { + $prefixes = []; + foreach (explode(',', (string) mod_imforum_Settings::get('subject_prefix')) as $prefix) { + $prefix = trim($prefix); + if ($prefix !== '') { + $prefixes[count($prefixes) + 1] = $prefix; + } + } + return $prefixes; + } + + /** + * @param array $input the request, unfiltered + * @return array{post: ?mod_imforum_Post, topic: ?mod_imforum_Topic, errors: list, pending: bool} + */ + public function createTopic(mod_imforum_Forum $forum, array $input): array { + $result = ['post' => null, 'topic' => null, 'errors' => [], 'pending' => false]; + if (!$this->permissions->canStartTopic($forum)) { + $result['errors'][] = _NOPERM; + return $result; + } + [$values, $errors] = $this->clean($input, $forum, true); + $errors = array_merge($errors, $this->checkFlood($forum)); + if ($errors) { + $result['errors'] = $errors; + return $result; + } + + $approved = $this->permissions->skipsApproval($forum) ? 1 : 0; + $topicHandler = icms_getModuleHandler('topic', IMFORUM_DIRNAME); + $topic = $topicHandler->create(); + $topic->setVar('forum_id', (int) $forum->id()); + $topic->setVar('topic_title', $values['subject']); + $topic->setVar('topic_subject', $values['prefix']); + $topic->setVar('topic_poster', $this->permissions->getUid()); + $topic->setVar('poster_name', $values['poster_name']); + $topic->setVar('approved', $approved); + if (!$topicHandler->insert($topic, true)) { + $result['errors'][] = _MD_IMFORUM_ERR_SAVE_FAILED; + return $result; + } + + $post = $this->savePost($topic, $forum, $values, 0, $approved); + if ($post === null) { + $topicHandler->delete($topic, true); + $result['errors'][] = _MD_IMFORUM_ERR_SAVE_FAILED; + return $result; + } + return $this->finish($result, $post, $topic, $forum, true, $approved); + } + + /** + * @param array $input + * @return array{post: ?mod_imforum_Post, topic: ?mod_imforum_Topic, errors: list, pending: bool} + */ + public function reply(mod_imforum_Forum $forum, mod_imforum_Topic $topic, array $input): array { + $result = ['post' => null, 'topic' => $topic, 'errors' => [], 'pending' => false]; + if ((int) $topic->getVar('forum_id', 'e') !== (int) $forum->id() || !$this->permissions->canReply($forum, $topic)) { + $result['errors'][] = _NOPERM; + return $result; + } + [$values, $errors] = $this->clean($input, $forum, false); + $errors = array_merge($errors, $this->checkFlood($forum)); + if ($errors) { + $result['errors'] = $errors; + return $result; + } + + $approved = $this->permissions->skipsApproval($forum) ? 1 : 0; + $parent = $this->resolveParent((int) ($input['pid'] ?? 0), $topic); + $post = $this->savePost($topic, $forum, $values, $parent, $approved); + if ($post === null) { + $result['errors'][] = _MD_IMFORUM_ERR_SAVE_FAILED; + return $result; + } + return $this->finish($result, $post, $topic, $forum, false, $approved); + } + + /** + * @param array $input + * @return array{post: ?mod_imforum_Post, topic: ?mod_imforum_Topic, errors: list, pending: bool} + */ + public function edit(mod_imforum_Forum $forum, mod_imforum_Topic $topic, mod_imforum_Post $post, array $input): array { + $result = ['post' => null, 'topic' => $topic, 'errors' => [], 'pending' => false]; + if ((int) $post->getVar('topic_id', 'e') !== (int) $topic->id() || !$this->permissions->canEdit($forum, $topic, $post)) { + $result['errors'][] = _NOPERM; + return $result; + } + $isFirst = (int) $topic->getVar('topic_first_post_id', 'e') === (int) $post->id() || (int) $post->getVar('pid', 'e') === 0 && $this->isOnlyPost($post); + [$values, $errors] = $this->clean($input, $forum, $isFirst, $post); + if ($errors) { + $result['errors'] = $errors; + return $result; + } + + $this->applyValues($post, $values); + $name = icms::$user ? (string) icms::$user->getVar('uname', 'n') : ''; + $post->addEdit($this->permissions->getUid(), $name); + if (!icms_getModuleHandler('post', IMFORUM_DIRNAME)->insert($post, true)) { + $result['errors'][] = _MD_IMFORUM_ERR_SAVE_FAILED; + return $result; + } + if ($isFirst) { + $topic->setVar('topic_title', $values['subject']); + $topic->setVar('topic_subject', $values['prefix']); + icms_getModuleHandler('topic', IMFORUM_DIRNAME)->insert($topic, true); + } + $result['post'] = $post; + $result['pending'] = (int) $post->getVar('approved', 'e') === 0; + return $result; + } + + /** + * @return array{ok: bool, errors: list, topic_gone: bool} + */ + public function delete(mod_imforum_Forum $forum, mod_imforum_Topic $topic, mod_imforum_Post $post): array { + if ((int) $post->getVar('topic_id', 'e') !== (int) $topic->id() || !$this->permissions->canDelete($forum, $topic, $post)) { + return ['ok' => false, 'errors' => [_NOPERM], 'topic_gone' => false]; + } + $topicId = (int) $topic->id(); + if (!icms_getModuleHandler('post', IMFORUM_DIRNAME)->delete($post, true)) { + return ['ok' => false, 'errors' => [_MD_IMFORUM_ERR_SAVE_FAILED], 'topic_gone' => false]; + } + return ['ok' => true, 'errors' => [], 'topic_gone' => icms_getModuleHandler('topic', IMFORUM_DIRNAME)->get($topicId)->isNew()]; + } + + /** + * Store the uploaded files of the form and remove the ones the author ticked + * + * @param list $remove ids of attachments to remove + * @return list messages for files that were refused + */ + public function handleAttachments(mod_imforum_Forum $forum, mod_imforum_Post $post, array $remove = []): array { + $handler = icms_getModuleHandler('attachment', IMFORUM_DIRNAME); + foreach ($remove as $attachId) { + $attachment = $handler->get((int) $attachId); + if (!$attachment->isNew() && (int) $attachment->getVar('post_id', 'e') === (int) $post->id()) { + $handler->delete($attachment, true); + } + } + + $errors = []; + if (!$this->permissions->canAttach($forum)) { + return $errors; + } + $uploader = new mod_imforum_Uploader($forum); + $limit = max(1, (int) mod_imforum_Settings::get('attach_max_files')); + for ($i = 0; $i < $limit; $i++) { + if (!isset($_FILES['attachment_' . $i])) { + continue; + } + if ($handler->countForPost((int) $post->id()) >= $limit) { + $errors[] = sprintf(_MD_IMFORUM_ERR_ATTACH_LIMIT, $limit); + break; + } + $stored = $uploader->store('attachment_' . $i, (int) $post->id()); + if ($stored['error'] !== null) { + $errors[] = $stored['error']; + } + } + return $errors; + } + + /** + * Filter and check what the form sent + * + * @param array $input + * @return array{0: array, 1: list} + */ + private function clean(array $input, mod_imforum_Forum $forum, bool $startsTopic, ?mod_imforum_Post $existing = null): array { + $errors = []; + $text = static fn (string $key): string => is_string($input[$key] ?? null) ? $input[$key] : ''; + $flag = static fn (string $key): int => !empty($input[$key]) ? 1 : 0; + + $subject = trim(preg_replace('/\s+/u', ' ', $text('subject')) ?? ''); + if ($subject === '') { + $errors[] = _MD_IMFORUM_ERR_NO_SUBJECT; + } elseif (mb_strlen($subject) > self::MAX_SUBJECT) { + $errors[] = sprintf(_MD_IMFORUM_ERR_SUBJECT_LONG, self::MAX_SUBJECT); + } + $body = str_replace("\r\n", "\n", $text('post_text')); + if (trim($body) === '') { + $errors[] = _MD_IMFORUM_ERR_NO_TEXT; + } + + $member = $this->permissions->getUid() > 0; + $guestName = ''; + if (!$member && $existing === null) { + $guestName = mb_substr(trim(strip_tags($text('poster_name'))), 0, self::MAX_NAME); + } + $prefix = 0; + $prefixes = self::getPrefixes(); + if ($startsTopic && (int) $forum->getVar('allow_subject_prefix', 'e') === 1) { + $prefix = (int) ($input['prefix'] ?? 0); + if (!isset($prefixes[$prefix])) { + $prefix = 0; + } + } + $karma = $member ? max(0, min(self::MAX_KARMA, (int) ($input['post_karma'] ?? 0))) : 0; + + return [[ + 'subject' => $subject, + 'post_text' => $body, + 'poster_name' => $guestName, + 'prefix' => $prefix, + 'icon' => preg_match('/^[A-Za-z0-9_.-]{1,50}$/', $text('icon')) === 1 ? $text('icon') : '', + '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, + 'dosmiley' => $flag('dosmiley'), + 'doxcode' => $flag('doxcode'), + 'doimage' => $flag('doimage'), + 'dobr' => $flag('dobr'), + ], $errors]; + } + + /** + * @return list + */ + private function checkFlood(mod_imforum_Forum $forum): array { + $seconds = (int) mod_imforum_Settings::get('flood_seconds'); + if ($seconds <= 0 || $this->permissions->isModerator((int) $forum->id())) { + return []; + } + $last = icms_getModuleHandler('post', IMFORUM_DIRNAME)->getLastPostTime($this->permissions->getUid(), mod_imforum_View::clientIp()); + $wait = $last + $seconds - time(); + return $wait > 0 ? [sprintf(_MD_IMFORUM_ERR_FLOOD, $wait)] : []; + } + + /** + * The post a reply answers, or the first post of the topic + */ + private function resolveParent(int $pid, mod_imforum_Topic $topic): int { + if ($pid > 0) { + $parent = icms_getModuleHandler('post', IMFORUM_DIRNAME)->get($pid); + if (!$parent->isNew() && (int) $parent->getVar('topic_id', 'e') === (int) $topic->id() && (int) $parent->getVar('approved', 'e') === 1) { + return $pid; + } + } + return (int) $topic->getVar('topic_first_post_id', 'e'); + } + + private function isOnlyPost(mod_imforum_Post $post): bool { + return icms_getModuleHandler('post', IMFORUM_DIRNAME)->getCount(new icms_db_criteria_Item('topic_id', (int) $post->getVar('topic_id', 'e'))) === 1; + } + + /** + * @param array $values + */ + private function savePost(mod_imforum_Topic $topic, mod_imforum_Forum $forum, array $values, int $pid, int $approved): ?mod_imforum_Post { + $handler = icms_getModuleHandler('post', IMFORUM_DIRNAME); + $post = $handler->create(); + $post->setVar('topic_id', (int) $topic->id()); + $post->setVar('forum_id', (int) $forum->id()); + $post->setVar('pid', $pid); + $post->setVar('uid', $this->permissions->getUid()); + $post->setVar('poster_ip', mod_imforum_View::clientIp()); + $post->setVar('approved', $approved); + $post->setVar('poster_name', $values['poster_name']); + $this->applyValues($post, $values); + return $handler->insert($post, true) ? $post : null; + } + + /** + * @param array $values + */ + private function applyValues(mod_imforum_Post $post, array $values): void { + foreach (['subject', 'post_text', 'icon', 'attachsig', 'post_karma', 'require_reply', 'dohtml', 'dosmiley', 'doxcode', 'doimage', 'dobr'] as $field) { + $post->setVar($field, $values[$field]); + } + } + + /** + * @param array{post: ?mod_imforum_Post, topic: ?mod_imforum_Topic, errors: list, pending: bool} $result + * @return array{post: ?mod_imforum_Post, topic: ?mod_imforum_Topic, errors: list, pending: bool} + */ + private function finish(array $result, mod_imforum_Post $post, mod_imforum_Topic $topic, mod_imforum_Forum $forum, bool $newTopic, int $approved): array { + // reload: the counters of the topic changed with the post + $topic = icms_getModuleHandler('topic', IMFORUM_DIRNAME)->get((int) $topic->id()); + $result['post'] = $post; + $result['topic'] = $topic; + $result['pending'] = $approved === 0; + if ($approved === 1) { + ($this->notifier ?? new mod_imforum_Notifier())->newPost($post, $topic, $forum, $newTopic); + } + return $result; + } +} diff --git a/src/class/Settings.php b/src/class/Settings.php index d84c7c4..fb2c2f7 100644 --- a/src/class/Settings.php +++ b/src/class/Settings.php @@ -24,6 +24,14 @@ class mod_imforum_Settings { 'show_online' => 1, 'online_minutes' => 15, 'rss_items' => 20, + 'edit_timelimit' => 30, + 'flood_seconds' => 30, + 'quickreply_enabled' => 1, + 'captcha_guests' => 1, + 'attach_max_files' => 3, + 'subject_prefix' => '', + 'disclaimer_show' => 0, + 'disclaimer' => '', ]; /** @var array|null */ diff --git a/src/class/TopicList.php b/src/class/TopicList.php index df48410..7933034 100644 --- a/src/class/TopicList.php +++ b/src/class/TopicList.php @@ -55,6 +55,7 @@ public static function rows(array $topics, array $forums, mod_imforum_ReadTracke $rows[] = [ 'id' => $topicId, 'url' => mod_imforum_View::link('topic', ['topic' => $topicId]), + 'prefix' => mod_imforum_View::prefix((int) $topic->getVar('topic_subject', 'e')), 'title' => $topic->getVar('topic_title'), 'poster' => mod_imforum_View::poster((int) $topic->getVar('topic_poster', 'e'), (string) $topic->getVar('poster_name', 'n')), 'time' => mod_imforum_View::time((int) $topic->getVar('topic_time', 'e')), diff --git a/src/class/Uploader.php b/src/class/Uploader.php new file mode 100644 index 0000000..d3489de --- /dev/null +++ b/src/class/Uploader.php @@ -0,0 +1,128 @@ +forum = $forum; + } + + /** + * Why a file is refused, or null when it is fine + * + * Does not touch the file: the name and size come from the upload. + */ + public function check(string $name, int $size): ?string { + $extension = self::extensionOf($name); + if ($extension === '' || in_array($extension, self::BLOCKED, true) || !in_array($extension, $this->forum->getAllowedExtensions(), true)) { + return sprintf(_MD_IMFORUM_ERR_ATTACH_EXT, htmlspecialchars($name, ENT_QUOTES, 'UTF-8'), htmlspecialchars(implode(', ', $this->getAcceptedExtensions()), ENT_QUOTES, 'UTF-8')); + } + $maxKb = (int) $this->forum->getVar('attach_maxkb', 'e'); + if ($size <= 0 || $size > $maxKb * 1024) { + return sprintf(_MD_IMFORUM_ERR_ATTACH_SIZE, htmlspecialchars($name, ENT_QUOTES, 'UTF-8'), $maxKb); + } + return null; + } + + /** + * The extensions of the forum that are not blocked + * + * @return list + */ + public function getAcceptedExtensions(): array { + return array_values(array_diff($this->forum->getAllowedExtensions(), self::BLOCKED)); + } + + public static function extensionOf(string $name): string { + $dot = strrpos($name, '.'); + return $dot === false ? '' : strtolower(substr($name, $dot + 1)); + } + + /** + * Store one uploaded file for a post + * + * @param string $field name of the file input in $_FILES + * @return array{attachment: ?mod_imforum_Attachment, error: ?string} + */ + public function store(string $field, int $postId): array { + $upload = $_FILES[$field] ?? null; + if (!is_array($upload) || !is_string($upload['name'] ?? null)) { + return ['attachment' => null, 'error' => null]; + } + if ((int) ($upload['error'] ?? 0) === UPLOAD_ERR_NO_FILE || $upload['name'] === '') { + return ['attachment' => null, 'error' => null]; + } + $name = basename(str_replace('\\', '/', $upload['name'])); + if ($error = $this->check($name, (int) ($upload['size'] ?? 0))) { + return ['attachment' => null, 'error' => $error]; + } + if (!imforum_create_upload_folder()) { + return ['attachment' => null, 'error' => _MD_IMFORUM_ERR_ATTACH_FAILED]; + } + + $extension = self::extensionOf($name); + $mimeTypes = icms_Utils::mimetypes(); + $uploader = new icms_file_MediaUploadHandler(rtrim(IMFORUM_UPLOAD_PATH, '/'), [(string) ($mimeTypes[$extension] ?? '')], (int) $this->forum->getVar('attach_maxkb', 'e') * 1024); + $saved = bin2hex(random_bytes(16)); + if (!$uploader->fetchMedia($field)) { + return ['attachment' => null, 'error' => _MD_IMFORUM_ERR_ATTACH_FAILED]; + } + $uploader->setTargetFileName($saved); + if (!$uploader->upload()) { + return ['attachment' => null, 'error' => _MD_IMFORUM_ERR_ATTACH_FAILED]; + } + + $path = IMFORUM_UPLOAD_PATH . $saved; + $type = in_array($extension, ['gif', 'jpg', 'jpeg', 'png', 'webp'], true) ? self::imageMime($path) : null; + if (in_array($extension, ['gif', 'jpg', 'jpeg', 'png', 'webp'], true) && $type === null) { + @unlink($path); + return ['attachment' => null, 'error' => sprintf(_MD_IMFORUM_ERR_ATTACH_IMAGE, htmlspecialchars($name, ENT_QUOTES, 'UTF-8'))]; + } + + $handler = icms_getModuleHandler('attachment', IMFORUM_DIRNAME); + $attachment = $handler->create(); + $attachment->setVar('post_id', $postId); + $attachment->setVar('name_saved', $saved); + $attachment->setVar('name_display', $name); + $attachment->setVar('mimetype', $type ?? 'application/octet-stream'); + $attachment->setVar('filesize', (int) filesize($path)); + if (!$handler->insert($attachment, true)) { + @unlink($path); + return ['attachment' => null, 'error' => _MD_IMFORUM_ERR_ATTACH_FAILED]; + } + return ['attachment' => $attachment, 'error' => null]; + } + + /** + * The type of an image by its content, or null when it is not a picture we serve + */ + private static function imageMime(string $path): ?string { + $info = @getimagesize($path); + $mime = is_array($info) ? $info['mime'] : ''; + return in_array($mime, ['image/gif', 'image/jpeg', 'image/png', 'image/webp'], true) ? $mime : null; + } +} diff --git a/src/class/View.php b/src/class/View.php index 1000089..df16cca 100644 --- a/src/class/View.php +++ b/src/class/View.php @@ -65,6 +65,22 @@ public static function online(array $snapshot): array { return ['members' => $members, 'member_count' => count($members), 'guests' => $snapshot['guests']]; } + /** + * The address of the visitor, or an empty string when the server does not give a valid one + */ + public static function clientIp(): string { + $ip = $_SERVER['REMOTE_ADDR'] ?? ''; + return is_string($ip) && filter_var($ip, FILTER_VALIDATE_IP) !== false ? $ip : ''; + } + + /** + * The prefix of a topic ("Question") ready to print, or an empty string + */ + public static function prefix(int $index): string { + $prefixes = mod_imforum_PostService::getPrefixes(); + return isset($prefixes[$index]) ? self::escape($prefixes[$index]) : ''; + } + public static function escape(string $text): string { return htmlspecialchars($text, ENT_QUOTES, 'UTF-8'); } diff --git a/src/forum.php b/src/forum.php index 1d5d20f..12408e2 100644 --- a/src/forum.php +++ b/src/forum.php @@ -84,6 +84,7 @@ $online->track($permissions->getUid(), icms::$user ? (string) icms::$user->getVar('uname', 'n') : '', (string) ($_SERVER['REMOTE_ADDR'] ?? ''), $forumId, 0, $minutes); } +$icmsTpl->assign('imforum_new_topic_url', $permissions->canStartTopic($forum) ? mod_imforum_View::link('post', ['forum' => $forumId]) : ''); $icmsTpl->assign('imforum_breadcrumb', mod_imforum_View::breadcrumb($trail)); $icmsTpl->assign('imforum_forum', [ 'id' => $forumId, diff --git a/src/icms_version.php b/src/icms_version.php index 5a7921a..ec68a92 100644 --- a/src/icms_version.php +++ b/src/icms_version.php @@ -59,7 +59,62 @@ 'hasSearch' => 1, 'search' => ['file' => 'include/search.inc.php', 'func' => 'imforum_search'], 'hasComments' => 0, - 'hasNotification' => 0, + 'hasNotification' => 1, + 'notification' => [ + 'lookup_file' => 'include/notification.inc.php', + 'lookup_func' => 'imforum_notify_iteminfo', + 'category' => [ + [ + 'name' => 'global', + 'title' => _MI_IMFORUM_NOTIFY_GLOBAL, + 'description' => _MI_IMFORUM_NOTIFY_GLOBAL_DSC, + 'subscribe_from' => ['index.php', 'viewall.php'], + ], + [ + 'name' => 'forum', + 'title' => _MI_IMFORUM_NOTIFY_FORUM, + 'description' => _MI_IMFORUM_NOTIFY_FORUM_DSC, + 'subscribe_from' => ['forum.php'], + 'item_name' => 'forum', + 'allow_bookmark' => 1, + ], + [ + 'name' => 'thread', + 'title' => _MI_IMFORUM_NOTIFY_THREAD, + 'description' => _MI_IMFORUM_NOTIFY_THREAD_DSC, + 'subscribe_from' => ['topic.php'], + 'item_name' => 'topic', + 'allow_bookmark' => 1, + ], + ], + 'event' => [ + [ + 'name' => 'new_post', 'category' => 'thread', + 'title' => _MI_IMFORUM_NOTIFY_THREAD_NEWPOST, 'caption' => _MI_IMFORUM_NOTIFY_THREAD_NEWPOST_CAP, + 'description' => _MI_IMFORUM_NOTIFY_THREAD_NEWPOST_DSC, 'mail_template' => 'thread_newpost', 'mail_subject' => _MI_IMFORUM_NOTIFY_THREAD_NEWPOST_SBJ, + ], + [ + 'name' => 'new_thread', 'category' => 'forum', + 'title' => _MI_IMFORUM_NOTIFY_FORUM_NEWTHREAD, 'caption' => _MI_IMFORUM_NOTIFY_FORUM_NEWTHREAD_CAP, + 'description' => _MI_IMFORUM_NOTIFY_FORUM_NEWTHREAD_DSC, 'mail_template' => 'forum_newthread', 'mail_subject' => _MI_IMFORUM_NOTIFY_FORUM_NEWTHREAD_SBJ, + ], + [ + 'name' => 'new_post', 'category' => 'forum', + 'title' => _MI_IMFORUM_NOTIFY_FORUM_NEWPOST, 'caption' => _MI_IMFORUM_NOTIFY_FORUM_NEWPOST_CAP, + 'description' => _MI_IMFORUM_NOTIFY_FORUM_NEWPOST_DSC, 'mail_template' => 'forum_newpost', 'mail_subject' => _MI_IMFORUM_NOTIFY_FORUM_NEWPOST_SBJ, + ], + [ + 'name' => 'new_thread', 'category' => 'global', + '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' => 'new_post', 'category' => 'global', + 'title' => _MI_IMFORUM_NOTIFY_GLOBAL_NEWPOST, 'caption' => _MI_IMFORUM_NOTIFY_GLOBAL_NEWPOST_CAP, + 'description' => _MI_IMFORUM_NOTIFY_GLOBAL_NEWPOST_DSC, 'mail_template' => 'global_newpost', 'mail_subject' => _MI_IMFORUM_NOTIFY_GLOBAL_NEWPOST_SBJ, + ], + ], + ], 'templates' => [ ['file' => 'imforum_index.html', 'description' => 'Forum index'], @@ -67,6 +122,7 @@ ['file' => 'imforum_admin.html', 'description' => 'Administration page'], ['file' => 'imforum_forum.html', 'description' => 'Forum with its topics'], ['file' => 'imforum_topic.html', 'description' => 'Topic with its posts'], + ['file' => 'imforum_post.html', 'description' => 'Form to write, edit or delete a post'], ['file' => 'imforum_viewall.html', 'description' => 'Topics of all forums'], ['file' => 'imforum_archive.html', 'description' => 'Archived topics'], ['file' => 'imforum_breadcrumb.html', 'description' => 'Path above a page'], @@ -156,5 +212,69 @@ 'valuetype' => 'int', 'default' => 20, ], + [ + 'name' => 'edit_timelimit', + 'title' => '_MI_IMFORUM_CFG_EDIT_TIMELIMIT', + 'description' => '_MI_IMFORUM_CFG_EDIT_TIMELIMIT_DSC', + 'formtype' => 'textbox', + 'valuetype' => 'int', + 'default' => 30, + ], + [ + 'name' => 'flood_seconds', + 'title' => '_MI_IMFORUM_CFG_FLOOD_SECONDS', + 'description' => '_MI_IMFORUM_CFG_FLOOD_SECONDS_DSC', + 'formtype' => 'textbox', + 'valuetype' => 'int', + 'default' => 30, + ], + [ + 'name' => 'quickreply_enabled', + 'title' => '_MI_IMFORUM_CFG_QUICKREPLY', + 'description' => '_MI_IMFORUM_CFG_QUICKREPLY_DSC', + 'formtype' => 'yesno', + 'valuetype' => 'int', + 'default' => 1, + ], + [ + 'name' => 'captcha_guests', + 'title' => '_MI_IMFORUM_CFG_CAPTCHA_GUESTS', + 'description' => '_MI_IMFORUM_CFG_CAPTCHA_GUESTS_DSC', + 'formtype' => 'yesno', + 'valuetype' => 'int', + 'default' => 1, + ], + [ + 'name' => 'attach_max_files', + 'title' => '_MI_IMFORUM_CFG_ATTACH_MAX_FILES', + 'description' => '_MI_IMFORUM_CFG_ATTACH_MAX_FILES_DSC', + 'formtype' => 'textbox', + 'valuetype' => 'int', + 'default' => 3, + ], + [ + 'name' => 'subject_prefix', + 'title' => '_MI_IMFORUM_CFG_SUBJECT_PREFIX', + 'description' => '_MI_IMFORUM_CFG_SUBJECT_PREFIX_DSC', + 'formtype' => 'textbox', + 'valuetype' => 'text', + 'default' => '', + ], + [ + 'name' => 'disclaimer_show', + 'title' => '_MI_IMFORUM_CFG_DISCLAIMER_SHOW', + 'description' => '_MI_IMFORUM_CFG_DISCLAIMER_SHOW_DSC', + 'formtype' => 'yesno', + 'valuetype' => 'int', + 'default' => 0, + ], + [ + 'name' => 'disclaimer', + 'title' => '_MI_IMFORUM_CFG_DISCLAIMER', + 'description' => '_MI_IMFORUM_CFG_DISCLAIMER_DSC', + 'formtype' => 'textarea', + 'valuetype' => 'text', + 'default' => '', + ], ], ]; diff --git a/src/include/common.php b/src/include/common.php index d8fa2be..27d6d22 100644 --- a/src/include/common.php +++ b/src/include/common.php @@ -29,4 +29,25 @@ // common.php by itself, and the handlers need it as soon as they are built. if (function_exists('icms_loadLanguageFile')) { icms_loadLanguageFile(IMFORUM_DIRNAME, 'common'); + // the notification forms and the block list show the titles of icms_version.php + icms_loadLanguageFile(IMFORUM_DIRNAME, 'modinfo'); +} + +/** + * Make sure uploads/imforum exists and cannot be browsed or run, without granting world-write access + * + * Attachments are stored under random names without extension and only + * served by attachment.php, so the web server needs no direct access. + */ +function imforum_create_upload_folder(): bool { + if (!is_dir(IMFORUM_UPLOAD_PATH) && !mkdir(IMFORUM_UPLOAD_PATH, 0755, true) && !is_dir(IMFORUM_UPLOAD_PATH)) { + return false; + } + if (!is_file(IMFORUM_UPLOAD_PATH . '.htaccess')) { + file_put_contents(IMFORUM_UPLOAD_PATH . '.htaccess', "Require all denied\n\nDeny from all\n\n"); + } + if (!is_file(IMFORUM_UPLOAD_PATH . 'index.html')) { + file_put_contents(IMFORUM_UPLOAD_PATH . 'index.html', ''); + } + return true; } diff --git a/src/include/notification.inc.php b/src/include/notification.inc.php new file mode 100644 index 0000000..b7d4acf --- /dev/null +++ b/src/include/notification.inc.php @@ -0,0 +1,36 @@ +get($itemId); + if (!$forum->isNew()) { + return ['name' => (string) $forum->getVar('forum_name', 'n'), 'url' => mod_imforum_View::url('forum', ['forum' => $itemId])]; + } + break; + case 'thread': + $topic = icms_getModuleHandler('topic', IMFORUM_DIRNAME)->get($itemId); + if (!$topic->isNew()) { + return ['name' => (string) $topic->getVar('topic_title', 'n'), 'url' => mod_imforum_View::url('topic', ['topic' => $itemId])]; + } + break; + } + return ['name' => '', 'url' => IMFORUM_URL]; +} diff --git a/src/include/onupdate.php b/src/include/onupdate.php index 75703f6..c6a471b 100644 --- a/src/include/onupdate.php +++ b/src/include/onupdate.php @@ -132,13 +132,3 @@ function icms_module_install_imforum(icms_module_Object $module): bool { function icms_module_update_imforum(icms_module_Object $module, ?string $oldversion = null, ?int $olddbversion = null): bool { return imforum_create_upload_folder(); } - -/** - * Make sure uploads/imforum exists, without granting world-write access - */ -function imforum_create_upload_folder(): bool { - if (is_dir(IMFORUM_UPLOAD_PATH)) { - return true; - } - return mkdir(IMFORUM_UPLOAD_PATH, 0755, true); -} diff --git a/src/language/english/mail_template/forum_newpost.tpl b/src/language/english/mail_template/forum_newpost.tpl new file mode 100644 index 0000000..5e1e82e --- /dev/null +++ b/src/language/english/mail_template/forum_newpost.tpl @@ -0,0 +1,18 @@ +Hello {X_UNAME}, + +{X_POSTER} wrote a new post in the topic "{X_TOPIC_TITLE}" in the forum {X_FORUM_NAME} on {X_SITENAME}. + +Read it here: +{X_POST_URL} + +----------- + +You receive this message because you follow new posts in this forum. 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/mail_template/forum_newthread.tpl b/src/language/english/mail_template/forum_newthread.tpl new file mode 100644 index 0000000..9c165b6 --- /dev/null +++ b/src/language/english/mail_template/forum_newthread.tpl @@ -0,0 +1,18 @@ +Hello {X_UNAME}, + +{X_POSTER} started the topic "{X_TOPIC_TITLE}" in the forum {X_FORUM_NAME} on {X_SITENAME}. + +Read it here: +{X_POST_URL} + +----------- + +You receive this message because you follow new topics in this forum. 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/mail_template/global_newpost.tpl b/src/language/english/mail_template/global_newpost.tpl new file mode 100644 index 0000000..359c0c6 --- /dev/null +++ b/src/language/english/mail_template/global_newpost.tpl @@ -0,0 +1,18 @@ +Hello {X_UNAME}, + +{X_POSTER} wrote a new post in the topic "{X_TOPIC_TITLE}" in the forum {X_FORUM_NAME} on {X_SITENAME}. + +Read it here: +{X_POST_URL} + +----------- + +You receive this message because you follow new posts in 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/mail_template/global_newthread.tpl b/src/language/english/mail_template/global_newthread.tpl new file mode 100644 index 0000000..6386aab --- /dev/null +++ b/src/language/english/mail_template/global_newthread.tpl @@ -0,0 +1,18 @@ +Hello {X_UNAME}, + +{X_POSTER} started the topic "{X_TOPIC_TITLE}" in the forum {X_FORUM_NAME} on {X_SITENAME}. + +Read it here: +{X_POST_URL} + +----------- + +You receive this message because you follow new topics in 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/mail_template/thread_newpost.tpl b/src/language/english/mail_template/thread_newpost.tpl new file mode 100644 index 0000000..adde031 --- /dev/null +++ b/src/language/english/mail_template/thread_newpost.tpl @@ -0,0 +1,18 @@ +Hello {X_UNAME}, + +{X_POSTER} wrote a new post in the topic "{X_TOPIC_TITLE}" (forum {X_FORUM_NAME}) on {X_SITENAME}. + +Read it here: +{X_POST_URL} + +----------- + +You receive this message because you follow this topic. 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 b0688f7..ba9170a 100644 --- a/src/language/english/main.php +++ b/src/language/english/main.php @@ -45,3 +45,39 @@ define('_MD_IMFORUM_EDITED_BY', 'Edited by'); define('_MD_IMFORUM_LOCK_KARMA', 'The author asks for more karma than you have to read this post.'); define('_MD_IMFORUM_LOCK_REPLY', 'The author asks you to reply to this topic before you can read this post.'); +define('_MD_IMFORUM_NEW_TOPIC', 'New topic'); +define('_MD_IMFORUM_REPLY', 'Reply'); +define('_MD_IMFORUM_QUOTE', 'Quote'); +define('_MD_IMFORUM_EDIT', 'Edit'); +define('_MD_IMFORUM_DELETE', 'Delete'); +define('_MD_IMFORUM_CANCEL', 'Cancel'); +define('_MD_IMFORUM_POST', 'Post'); +define('_MD_IMFORUM_SAVE', 'Save'); +define('_MD_IMFORUM_PREVIEW', 'Preview'); +define('_MD_IMFORUM_QUICKREPLY', 'Quick reply'); +define('_MD_IMFORUM_YOUR_NAME', 'Your name'); +define('_MD_IMFORUM_PREFIX', 'Prefix'); +define('_MD_IMFORUM_PREFIX_NONE', 'None'); +define('_MD_IMFORUM_CAPTCHA', 'Type the characters you see'); +define('_MD_IMFORUM_ATTACH', 'Attach a file'); +define('_MD_IMFORUM_ATTACH_NOTE', 'At most %d KB per file. Allowed types: %s.'); +define('_MD_IMFORUM_ATTACH_REMOVE', 'Remove attached files'); +define('_MD_IMFORUM_ATTACHMENTS', 'Attachments'); +define('_MD_IMFORUM_DOWNLOADS', 'downloads'); +define('_MD_IMFORUM_POST_SAVED', 'Your post was saved.'); +define('_MD_IMFORUM_POST_PENDING', 'Your post was saved and appears when a moderator has approved it.'); +define('_MD_IMFORUM_POST_DELETED', 'The post was deleted.'); +define('_MD_IMFORUM_DELETE_CONFIRM', 'Do you want to delete this post?'); +define('_MD_IMFORUM_DELETE_TOPIC', 'This is the only post, so the topic is deleted with it.'); +define('_MD_IMFORUM_ERR_NO_SUBJECT', 'Give your post a subject.'); +define('_MD_IMFORUM_ERR_SUBJECT_LONG', 'The subject can have at most %d characters.'); +define('_MD_IMFORUM_ERR_NO_TEXT', 'Write a text.'); +define('_MD_IMFORUM_ERR_FLOOD', 'Please wait %d more seconds before you post again.'); +define('_MD_IMFORUM_ERR_SAVE_FAILED', 'The post could not be saved.'); +define('_MD_IMFORUM_ERR_ATTACH_EXT', 'The file %s is not allowed. Allowed types: %s.'); +define('_MD_IMFORUM_ERR_ATTACH_SIZE', 'The file %s is empty or larger than %d KB.'); +define('_MD_IMFORUM_ERR_ATTACH_LIMIT', 'A post can have at most %d files.'); +define('_MD_IMFORUM_ERR_ATTACH_IMAGE', 'The file %s is not a valid picture.'); +define('_MD_IMFORUM_ERR_ATTACH_FAILED', 'A file could not be uploaded.'); +define('_MD_IMFORUM_QUOTE_WROTE', '%s wrote:'); +define('_MD_IMFORUM_ANONYMOUS', 'a guest'); diff --git a/src/language/english/modinfo.php b/src/language/english/modinfo.php index 7b5e995..53f4cb0 100644 --- a/src/language/english/modinfo.php +++ b/src/language/english/modinfo.php @@ -36,3 +36,45 @@ define('_MI_IMFORUM_CFG_ONLINE_MINUTES_DSC', 'A visitor who has not opened a page for this long is no longer shown.'); define('_MI_IMFORUM_CFG_RSS_ITEMS', 'Items in the RSS feed'); define('_MI_IMFORUM_CFG_RSS_ITEMS_DSC', 'The number of topics in the feed.'); +define('_MI_IMFORUM_CFG_EDIT_TIMELIMIT', 'Minutes members may edit or delete their posts'); +define('_MI_IMFORUM_CFG_EDIT_TIMELIMIT_DSC', 'Moderators are not limited. 0 means no limit.'); +define('_MI_IMFORUM_CFG_FLOOD_SECONDS', 'Seconds between two posts'); +define('_MI_IMFORUM_CFG_FLOOD_SECONDS_DSC', 'Protects against flooding. Moderators are not limited. 0 switches it off.'); +define('_MI_IMFORUM_CFG_QUICKREPLY', 'Quick reply under a topic'); +define('_MI_IMFORUM_CFG_QUICKREPLY_DSC', 'Shows a short reply form below the posts. Guests only get it when the captcha is off.'); +define('_MI_IMFORUM_CFG_CAPTCHA_GUESTS', 'Captcha for guests'); +define('_MI_IMFORUM_CFG_CAPTCHA_GUESTS_DSC', 'Guests solve a captcha before they can post. The type of captcha is set in the user preferences of the system.'); +define('_MI_IMFORUM_CFG_ATTACH_MAX_FILES', 'Files per post'); +define('_MI_IMFORUM_CFG_ATTACH_MAX_FILES_DSC', 'The most files one post can carry. The allowed types and the size are set per forum.'); +define('_MI_IMFORUM_CFG_SUBJECT_PREFIX', 'Topic prefixes'); +define('_MI_IMFORUM_CFG_SUBJECT_PREFIX_DSC', 'Separate with commas, for example Question,Idea,Solved. Forums that allow prefixes let the author pick one.'); +define('_MI_IMFORUM_CFG_DISCLAIMER_SHOW', 'Show a disclaimer when posting'); +define('_MI_IMFORUM_CFG_DISCLAIMER_SHOW_DSC', 'The text below appears above the form to write a post.'); +define('_MI_IMFORUM_CFG_DISCLAIMER', 'Disclaimer'); +define('_MI_IMFORUM_CFG_DISCLAIMER_DSC', 'Shown with the text filter, so BBCode works.'); +define('_MI_IMFORUM_NOTIFY_GLOBAL', 'All forums'); +define('_MI_IMFORUM_NOTIFY_GLOBAL_DSC', 'Notifications about every forum you may read'); +define('_MI_IMFORUM_NOTIFY_FORUM', 'This forum'); +define('_MI_IMFORUM_NOTIFY_FORUM_DSC', 'Notifications about this forum'); +define('_MI_IMFORUM_NOTIFY_THREAD', 'This topic'); +define('_MI_IMFORUM_NOTIFY_THREAD_DSC', 'Notifications about this topic'); +define('_MI_IMFORUM_NOTIFY_THREAD_NEWPOST', 'New post'); +define('_MI_IMFORUM_NOTIFY_THREAD_NEWPOST_CAP', 'Tell me about new posts in this topic'); +define('_MI_IMFORUM_NOTIFY_THREAD_NEWPOST_DSC', 'Tell me about new posts in this topic'); +define('_MI_IMFORUM_NOTIFY_THREAD_NEWPOST_SBJ', '[{X_SITENAME}] {X_MODULE}: new post in a topic you follow'); +define('_MI_IMFORUM_NOTIFY_FORUM_NEWTHREAD', 'New topic'); +define('_MI_IMFORUM_NOTIFY_FORUM_NEWTHREAD_CAP', 'Tell me about new topics in this forum'); +define('_MI_IMFORUM_NOTIFY_FORUM_NEWTHREAD_DSC', 'Tell me about new topics in this forum'); +define('_MI_IMFORUM_NOTIFY_FORUM_NEWTHREAD_SBJ', '[{X_SITENAME}] {X_MODULE}: new topic in a forum you follow'); +define('_MI_IMFORUM_NOTIFY_FORUM_NEWPOST', 'New post'); +define('_MI_IMFORUM_NOTIFY_FORUM_NEWPOST_CAP', 'Tell me about new posts in this forum'); +define('_MI_IMFORUM_NOTIFY_FORUM_NEWPOST_DSC', 'Tell me about new posts in this forum'); +define('_MI_IMFORUM_NOTIFY_FORUM_NEWPOST_SBJ', '[{X_SITENAME}] {X_MODULE}: new post in a forum you follow'); +define('_MI_IMFORUM_NOTIFY_GLOBAL_NEWTHREAD', 'New topic'); +define('_MI_IMFORUM_NOTIFY_GLOBAL_NEWTHREAD_CAP', 'Tell me about new topics in all forums'); +define('_MI_IMFORUM_NOTIFY_GLOBAL_NEWTHREAD_DSC', 'Tell me about new topics in all forums'); +define('_MI_IMFORUM_NOTIFY_GLOBAL_NEWTHREAD_SBJ', '[{X_SITENAME}] {X_MODULE}: new topic'); +define('_MI_IMFORUM_NOTIFY_GLOBAL_NEWPOST', 'New post'); +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'); diff --git a/src/language/nederlands/mail_template/forum_newpost.tpl b/src/language/nederlands/mail_template/forum_newpost.tpl new file mode 100644 index 0000000..f0d2996 --- /dev/null +++ b/src/language/nederlands/mail_template/forum_newpost.tpl @@ -0,0 +1,18 @@ +Hallo {X_UNAME}, + +{X_POSTER} schreef een nieuw bericht in het onderwerp "{X_TOPIC_TITLE}" in het forum {X_FORUM_NAME} op {X_SITENAME}. + +Lees het hier: +{X_POST_URL} + +----------- + +Je ontvangt dit bericht omdat je nieuwe berichten in dit forum volgt. Om te stoppen pas je hier je abonnementen aan: +{X_UNSUBSCRIBE_URL} + +Antwoord niet op dit bericht. + +----------- +{X_SITENAME} ({X_SITEURL}) +webmaster +{X_ADMINMAIL} diff --git a/src/language/nederlands/mail_template/forum_newthread.tpl b/src/language/nederlands/mail_template/forum_newthread.tpl new file mode 100644 index 0000000..ccc86c7 --- /dev/null +++ b/src/language/nederlands/mail_template/forum_newthread.tpl @@ -0,0 +1,18 @@ +Hallo {X_UNAME}, + +{X_POSTER} startte het onderwerp "{X_TOPIC_TITLE}" in het forum {X_FORUM_NAME} op {X_SITENAME}. + +Lees het hier: +{X_POST_URL} + +----------- + +Je ontvangt dit bericht omdat je nieuwe onderwerpen in dit forum volgt. Om te stoppen pas je hier je abonnementen aan: +{X_UNSUBSCRIBE_URL} + +Antwoord niet op dit bericht. + +----------- +{X_SITENAME} ({X_SITEURL}) +webmaster +{X_ADMINMAIL} diff --git a/src/language/nederlands/mail_template/global_newpost.tpl b/src/language/nederlands/mail_template/global_newpost.tpl new file mode 100644 index 0000000..212cdd3 --- /dev/null +++ b/src/language/nederlands/mail_template/global_newpost.tpl @@ -0,0 +1,18 @@ +Hallo {X_UNAME}, + +{X_POSTER} schreef een nieuw bericht in het onderwerp "{X_TOPIC_TITLE}" in het forum {X_FORUM_NAME} op {X_SITENAME}. + +Lees het hier: +{X_POST_URL} + +----------- + +Je ontvangt dit bericht omdat je nieuwe berichten in alle forums volgt. Om te stoppen pas je hier je abonnementen aan: +{X_UNSUBSCRIBE_URL} + +Antwoord niet op dit bericht. + +----------- +{X_SITENAME} ({X_SITEURL}) +webmaster +{X_ADMINMAIL} diff --git a/src/language/nederlands/mail_template/global_newthread.tpl b/src/language/nederlands/mail_template/global_newthread.tpl new file mode 100644 index 0000000..1142afb --- /dev/null +++ b/src/language/nederlands/mail_template/global_newthread.tpl @@ -0,0 +1,18 @@ +Hallo {X_UNAME}, + +{X_POSTER} startte het onderwerp "{X_TOPIC_TITLE}" in het forum {X_FORUM_NAME} op {X_SITENAME}. + +Lees het hier: +{X_POST_URL} + +----------- + +Je ontvangt dit bericht omdat je nieuwe onderwerpen in alle forums volgt. Om te stoppen pas je hier je abonnementen aan: +{X_UNSUBSCRIBE_URL} + +Antwoord niet op dit bericht. + +----------- +{X_SITENAME} ({X_SITEURL}) +webmaster +{X_ADMINMAIL} diff --git a/src/language/nederlands/mail_template/thread_newpost.tpl b/src/language/nederlands/mail_template/thread_newpost.tpl new file mode 100644 index 0000000..9bed6a4 --- /dev/null +++ b/src/language/nederlands/mail_template/thread_newpost.tpl @@ -0,0 +1,18 @@ +Hallo {X_UNAME}, + +{X_POSTER} schreef een nieuw bericht in het onderwerp "{X_TOPIC_TITLE}" (forum {X_FORUM_NAME}) op {X_SITENAME}. + +Lees het hier: +{X_POST_URL} + +----------- + +Je ontvangt dit bericht omdat je dit onderwerp volgt. Om te stoppen pas je hier je abonnementen aan: +{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 247f16f..5efd6f0 100644 --- a/src/language/nederlands/main.php +++ b/src/language/nederlands/main.php @@ -45,3 +45,39 @@ define('_MD_IMFORUM_EDITED_BY', 'Bewerkt door'); define('_MD_IMFORUM_LOCK_KARMA', 'De auteur vraagt meer karma dan je hebt om dit bericht te lezen.'); define('_MD_IMFORUM_LOCK_REPLY', 'De auteur vraagt dat je eerst op dit onderwerp reageert voordat je dit bericht kunt lezen.'); +define('_MD_IMFORUM_NEW_TOPIC', 'Nieuw onderwerp'); +define('_MD_IMFORUM_REPLY', 'Reageren'); +define('_MD_IMFORUM_QUOTE', 'Citeren'); +define('_MD_IMFORUM_EDIT', 'Bewerken'); +define('_MD_IMFORUM_DELETE', 'Verwijderen'); +define('_MD_IMFORUM_CANCEL', 'Annuleren'); +define('_MD_IMFORUM_POST', 'Plaatsen'); +define('_MD_IMFORUM_SAVE', 'Opslaan'); +define('_MD_IMFORUM_PREVIEW', 'Voorbeeld'); +define('_MD_IMFORUM_QUICKREPLY', 'Snel reageren'); +define('_MD_IMFORUM_YOUR_NAME', 'Je naam'); +define('_MD_IMFORUM_PREFIX', 'Voorvoegsel'); +define('_MD_IMFORUM_PREFIX_NONE', 'Geen'); +define('_MD_IMFORUM_CAPTCHA', 'Typ de tekens die je ziet'); +define('_MD_IMFORUM_ATTACH', 'Bestand toevoegen'); +define('_MD_IMFORUM_ATTACH_NOTE', 'Maximaal %d KB per bestand. Toegestane types: %s.'); +define('_MD_IMFORUM_ATTACH_REMOVE', 'Bijlagen verwijderen'); +define('_MD_IMFORUM_ATTACHMENTS', 'Bijlagen'); +define('_MD_IMFORUM_DOWNLOADS', 'keer gedownload'); +define('_MD_IMFORUM_POST_SAVED', 'Je bericht is opgeslagen.'); +define('_MD_IMFORUM_POST_PENDING', 'Je bericht is opgeslagen en verschijnt zodra een moderator het goedkeurt.'); +define('_MD_IMFORUM_POST_DELETED', 'Het bericht is verwijderd.'); +define('_MD_IMFORUM_DELETE_CONFIRM', 'Wil je dit bericht verwijderen?'); +define('_MD_IMFORUM_DELETE_TOPIC', 'Dit is het enige bericht, dus het onderwerp wordt mee verwijderd.'); +define('_MD_IMFORUM_ERR_NO_SUBJECT', 'Geef je bericht een onderwerp.'); +define('_MD_IMFORUM_ERR_SUBJECT_LONG', 'Het onderwerp mag hoogstens %d tekens hebben.'); +define('_MD_IMFORUM_ERR_NO_TEXT', 'Schrijf een tekst.'); +define('_MD_IMFORUM_ERR_FLOOD', 'Wacht nog %d seconden voordat je opnieuw post.'); +define('_MD_IMFORUM_ERR_SAVE_FAILED', 'Het bericht kon niet worden opgeslagen.'); +define('_MD_IMFORUM_ERR_ATTACH_EXT', 'Het bestand %s is niet toegestaan. Toegestane types: %s.'); +define('_MD_IMFORUM_ERR_ATTACH_SIZE', 'Het bestand %s is leeg of groter dan %d KB.'); +define('_MD_IMFORUM_ERR_ATTACH_LIMIT', 'Een bericht kan hoogstens %d bestanden hebben.'); +define('_MD_IMFORUM_ERR_ATTACH_IMAGE', 'Het bestand %s is geen geldige afbeelding.'); +define('_MD_IMFORUM_ERR_ATTACH_FAILED', 'Een bestand kon niet worden geüpload.'); +define('_MD_IMFORUM_QUOTE_WROTE', '%s schreef:'); +define('_MD_IMFORUM_ANONYMOUS', 'een gast'); diff --git a/src/language/nederlands/modinfo.php b/src/language/nederlands/modinfo.php index 7a2a2e7..bd07221 100644 --- a/src/language/nederlands/modinfo.php +++ b/src/language/nederlands/modinfo.php @@ -36,3 +36,45 @@ define('_MI_IMFORUM_CFG_ONLINE_MINUTES_DSC', 'Een bezoeker die zo lang geen pagina opende, wordt niet meer getoond.'); define('_MI_IMFORUM_CFG_RSS_ITEMS', 'Items in de RSS-feed'); define('_MI_IMFORUM_CFG_RSS_ITEMS_DSC', 'Het aantal onderwerpen in de feed.'); +define('_MI_IMFORUM_CFG_EDIT_TIMELIMIT', 'Minuten dat leden hun berichten mogen bewerken of verwijderen'); +define('_MI_IMFORUM_CFG_EDIT_TIMELIMIT_DSC', 'Moderators zijn niet beperkt. 0 betekent geen limiet.'); +define('_MI_IMFORUM_CFG_FLOOD_SECONDS', 'Seconden tussen twee berichten'); +define('_MI_IMFORUM_CFG_FLOOD_SECONDS_DSC', 'Beschermt tegen overspoelen. Moderators zijn niet beperkt. 0 zet het uit.'); +define('_MI_IMFORUM_CFG_QUICKREPLY', 'Snel reageren onder een onderwerp'); +define('_MI_IMFORUM_CFG_QUICKREPLY_DSC', 'Toont een kort antwoordformulier onder de berichten. Gasten krijgen het alleen als de captcha uit staat.'); +define('_MI_IMFORUM_CFG_CAPTCHA_GUESTS', 'Captcha voor gasten'); +define('_MI_IMFORUM_CFG_CAPTCHA_GUESTS_DSC', 'Gasten lossen een captcha op voordat ze kunnen posten. Het soort captcha stel je in bij de gebruikersvoorkeuren van het systeem.'); +define('_MI_IMFORUM_CFG_ATTACH_MAX_FILES', 'Bestanden per bericht'); +define('_MI_IMFORUM_CFG_ATTACH_MAX_FILES_DSC', 'Het maximum aantal bestanden bij één bericht. De toegestane types en de grootte stel je per forum in.'); +define('_MI_IMFORUM_CFG_SUBJECT_PREFIX', 'Voorvoegsels voor onderwerpen'); +define('_MI_IMFORUM_CFG_SUBJECT_PREFIX_DSC', 'Scheid met komma\'s, bijvoorbeeld Vraag,Idee,Opgelost. Forums die voorvoegsels toestaan laten de auteur er één kiezen.'); +define('_MI_IMFORUM_CFG_DISCLAIMER_SHOW', 'Toon een disclaimer bij het posten'); +define('_MI_IMFORUM_CFG_DISCLAIMER_SHOW_DSC', 'De tekst hieronder staat boven het formulier om een bericht te schrijven.'); +define('_MI_IMFORUM_CFG_DISCLAIMER', 'Disclaimer'); +define('_MI_IMFORUM_CFG_DISCLAIMER_DSC', 'Getoond met de tekstfilter, dus BBCode werkt.'); +define('_MI_IMFORUM_NOTIFY_GLOBAL', 'Alle forums'); +define('_MI_IMFORUM_NOTIFY_GLOBAL_DSC', 'Meldingen over elk forum dat je mag lezen'); +define('_MI_IMFORUM_NOTIFY_FORUM', 'Dit forum'); +define('_MI_IMFORUM_NOTIFY_FORUM_DSC', 'Meldingen over dit forum'); +define('_MI_IMFORUM_NOTIFY_THREAD', 'Dit onderwerp'); +define('_MI_IMFORUM_NOTIFY_THREAD_DSC', 'Meldingen over dit onderwerp'); +define('_MI_IMFORUM_NOTIFY_THREAD_NEWPOST', 'Nieuw bericht'); +define('_MI_IMFORUM_NOTIFY_THREAD_NEWPOST_CAP', 'Meld mij nieuwe berichten in dit onderwerp'); +define('_MI_IMFORUM_NOTIFY_THREAD_NEWPOST_DSC', 'Meld mij nieuwe berichten in dit onderwerp'); +define('_MI_IMFORUM_NOTIFY_THREAD_NEWPOST_SBJ', '[{X_SITENAME}] {X_MODULE}: nieuw bericht in een onderwerp dat je volgt'); +define('_MI_IMFORUM_NOTIFY_FORUM_NEWTHREAD', 'Nieuw onderwerp'); +define('_MI_IMFORUM_NOTIFY_FORUM_NEWTHREAD_CAP', 'Meld mij nieuwe onderwerpen in dit forum'); +define('_MI_IMFORUM_NOTIFY_FORUM_NEWTHREAD_DSC', 'Meld mij nieuwe onderwerpen in dit forum'); +define('_MI_IMFORUM_NOTIFY_FORUM_NEWTHREAD_SBJ', '[{X_SITENAME}] {X_MODULE}: nieuw onderwerp in een forum dat je volgt'); +define('_MI_IMFORUM_NOTIFY_FORUM_NEWPOST', 'Nieuw bericht'); +define('_MI_IMFORUM_NOTIFY_FORUM_NEWPOST_CAP', 'Meld mij nieuwe berichten in dit forum'); +define('_MI_IMFORUM_NOTIFY_FORUM_NEWPOST_DSC', 'Meld mij nieuwe berichten in dit forum'); +define('_MI_IMFORUM_NOTIFY_FORUM_NEWPOST_SBJ', '[{X_SITENAME}] {X_MODULE}: nieuw bericht in een forum dat je volgt'); +define('_MI_IMFORUM_NOTIFY_GLOBAL_NEWTHREAD', 'Nieuw onderwerp'); +define('_MI_IMFORUM_NOTIFY_GLOBAL_NEWTHREAD_CAP', 'Meld mij nieuwe onderwerpen in alle forums'); +define('_MI_IMFORUM_NOTIFY_GLOBAL_NEWTHREAD_DSC', 'Meld mij nieuwe onderwerpen in alle forums'); +define('_MI_IMFORUM_NOTIFY_GLOBAL_NEWTHREAD_SBJ', '[{X_SITENAME}] {X_MODULE}: nieuw onderwerp'); +define('_MI_IMFORUM_NOTIFY_GLOBAL_NEWPOST', 'Nieuw bericht'); +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'); diff --git a/src/notification_update.php b/src/notification_update.php new file mode 100644 index 0000000..eb02485 --- /dev/null +++ b/src/notification_update.php @@ -0,0 +1,14 @@ + is_scalar($request[$name] ?? null) ? max(0, (int) $request[$name]) : 0; +$do = is_string($request['do'] ?? null) ? $request['do'] : ''; + +// what the request is about: a post to change, a topic to answer or a forum to start in +$post = null; +$topic = null; +$mode = 'new'; +if ($param('post') > 0 && in_array($do, ['edit', 'delete'], true)) { + $post = $postHandler->get($param('post')); + $topic = $post->isNew() ? null : $topicHandler->get((int) $post->getVar('topic_id', 'e')); + $mode = $do; +} elseif ($param('topic') > 0) { + $topic = $topicHandler->get($param('topic')); + $mode = 'reply'; +} +if ($topic !== null && $topic->isNew()) { + $topic = null; +} +$forumId = $topic !== null ? (int) $topic->getVar('forum_id', 'e') : $param('forum'); +$forum = $permissions->getForum($forumId); +$back = mod_imforum_View::url($topic !== null ? 'topic' : 'index', $topic !== null ? ['topic' => (int) $topic->id()] : []); + +if ($forum === null || ($mode !== 'new' && $topic === null) || ($mode === 'delete' || $mode === 'edit') && $post === null) { + redirect_header(mod_imforum_View::url('index'), 3, _NOPERM); + exit; +} + +$allowed = match ($mode) { + 'new' => $permissions->canStartTopic($forum), + 'reply' => $permissions->canReply($forum, $topic), + 'edit' => $permissions->canEdit($forum, $topic, $post), + default => $permissions->canDelete($forum, $topic, $post), +}; +if (!$allowed) { + if ($permissions->getUid() === 0) { + redirect_header(ICMS_URL . '/user.php?xoops_redirect=' . rawurlencode($_SERVER['REQUEST_URI'] ?? ''), 3, _NOPERM); + } else { + redirect_header($topic !== null ? $back : mod_imforum_View::url('forum', ['forum' => $forumId]), 3, _NOPERM); + } + exit; +} + +$service = new mod_imforum_PostService($permissions); +$errors = []; +$captcha = $permissions->getUid() === 0 && (int) mod_imforum_Settings::get('captcha_guests') === 1; +$preview = null; +$values = []; + +// deleting: the confirmation is a form, the delete itself a POST with a token +if ($mode === 'delete' && $isPost) { + if (!icms::$security->check()) { + redirect_header($back, 3, implode('
', icms::$security->getErrors())); + exit; + } + $outcome = $service->delete($forum, $topic, $post); + if (!$outcome['ok']) { + redirect_header($back, 3, implode('
', $outcome['errors'])); + exit; + } + redirect_header($outcome['topic_gone'] ? mod_imforum_View::url('forum', ['forum' => $forumId]) : $back, 2, _MD_IMFORUM_POST_DELETED); + exit; +} + +// saving or previewing +if ($isPost) { + $values = $_POST; + if (!icms::$security->check()) { + $errors = array_merge($errors, (array) icms::$security->getErrors()); + } elseif ($captcha && !icms_form_elements_captcha_Object::instance()->verify(true)) { + $errors[] = icms_form_elements_captcha_Object::instance()->getMessage(); + } elseif (isset($_POST['preview_button'])) { + $preview = true; + } else { + if ($mode === 'new') { + $outcome = $service->createTopic($forum, $_POST); + } elseif ($mode === 'reply') { + $outcome = $service->reply($forum, $topic, $_POST); + } else { + $outcome = $service->edit($forum, $topic, $post, $_POST); + } + if (!$outcome['errors']) { + $saved = $outcome['post']; + $remove = isset($_POST['remove_attachment']) && is_array($_POST['remove_attachment']) ? array_map('intval', $_POST['remove_attachment']) : []; + $attachErrors = $service->handleAttachments($forum, $saved, $remove); + $target = mod_imforum_View::url('topic', ['topic' => (int) $outcome['topic']->id(), 'post' => (int) $saved->id()]) . '#post-' . (int) $saved->id(); + if ($outcome['pending']) { + $target = mod_imforum_View::url('forum', ['forum' => $forumId]); + } + $message = $outcome['pending'] ? _MD_IMFORUM_POST_PENDING : _MD_IMFORUM_POST_SAVED; + redirect_header($target, $attachErrors ? 5 : 2, implode('
', array_merge([$message], $attachErrors))); + exit; + } + $errors = $outcome['errors']; + } +} + +// the values the form starts with: the request after an error, otherwise the post or a quote +if (!$values) { + $values = ['subject' => '', 'post_text' => '', 'attachsig' => 1, 'dosmiley' => 1, 'doxcode' => 1, 'doimage' => 1, 'dobr' => 1, 'post_karma' => 0, 'require_reply' => 0, 'prefix' => 0]; + if ($mode === 'edit') { + foreach (['subject', 'post_text', 'attachsig', 'post_karma', 'require_reply', 'dohtml', 'dosmiley', 'doxcode', 'doimage', 'dobr'] as $field) { + $values[$field] = $post->getVar($field, 'n'); + } + $values['prefix'] = (int) $topic->getVar('topic_subject', 'e'); + } elseif ($mode === 'reply') { + $parent = $param('post') > 0 ? $postHandler->get($param('post')) : null; + $values['subject'] = preg_replace('/^(Re:\s*)+/i', '', (string) $topic->getVar('topic_title', 'n')); + $values['subject'] = 'Re: ' . $values['subject']; + if ($parent !== null && !$parent->isNew() && (int) $parent->getVar('topic_id', 'e') === (int) $topic->id() && (int) $parent->getVar('approved', 'e') === 1) { + $values['pid'] = (int) $parent->id(); + // a quote of a locked post would hand out the text the reader may not see + $karma = new mod_imforum_KarmaService($permissions->getUid()); + if (!isset($_GET['quote']) || $karma->getLock($parent, $permissions->isModerator($forumId)) === mod_imforum_KarmaService::LOCK_NONE) { + if (isset($_GET['quote'])) { + $author = (int) $parent->getVar('uid', 'e') > 0 ? (string) icms_member_user_Handler::getUserLink((int) $parent->getVar('uid', 'e')) : (string) $parent->getVar('poster_name', 'n'); + $author = trim(html_entity_decode(strip_tags($author), ENT_QUOTES, 'UTF-8')); + $author = $author !== '' ? $author : _MD_IMFORUM_ANONYMOUS; + $values['post_text'] = '[quote]' . sprintf(_MD_IMFORUM_QUOTE_WROTE, $author) . "\n" . (string) $parent->getVar('post_text', 'n') . "[/quote]\n"; + } + } + } + } +} + +$xoopsOption['template_main'] = 'imforum_post.html'; +include_once ICMS_ROOT_PATH . '/header.php'; +global $icmsTpl, $xoTheme; +$xoTheme->addStylesheet(IMFORUM_URL . 'assets/css/imforum.css'); + +$trail = []; +for ($parent = $forumId, $guard = 0; $parent > 0 && $guard < 10; $guard++) { + $step = $permissions->getForum($parent); + if ($step === null || !$permissions->canSeeForum($step)) { + break; + } + array_unshift($trail, [$step->getVar('forum_name', 'n'), mod_imforum_View::url('forum', ['forum' => $parent])]); + $parent = (int) $step->getVar('parent_forum', 'e'); +} +if ($topic !== null) { + $trail[] = [$topic->getVar('topic_title', 'n'), mod_imforum_View::url('topic', ['topic' => (int) $topic->id()])]; +} +$trail[] = [match ($mode) { + 'new' => _MD_IMFORUM_NEW_TOPIC, + 'reply' => _MD_IMFORUM_REPLY, + 'edit' => _MD_IMFORUM_EDIT, + default => _MD_IMFORUM_DELETE, +}]; + +$icmsTpl->assign('imforum_breadcrumb', mod_imforum_View::breadcrumb($trail)); +$icmsTpl->assign('imforum_mode', $mode); +$icmsTpl->assign('imforum_errors', $errors); +$icmsTpl->assign('imforum_back', mod_imforum_View::escape($back)); +$disclaimer = (int) mod_imforum_Settings::get('disclaimer_show') === 1 ? trim((string) mod_imforum_Settings::get('disclaimer')) : ''; +$icmsTpl->assign('imforum_disclaimer', $disclaimer !== '' ? icms_core_DataFilter::filterTextareaDisplay($disclaimer, 0, 1, 0, 1) : ''); + +if ($mode === 'delete') { + $icmsTpl->assign('imforum_delete', [ + 'subject' => $post->getVar('subject'), + 'action' => mod_imforum_View::link('post'), + 'token' => icms::$security->getTokenHTML(), + 'post' => (int) $post->id(), + 'topic_deleted' => (int) $topic->getVar('topic_replies', 'e') === 0 && (int) $topic->getVar('topic_first_post_id', 'e') === (int) $post->id(), + ]); +} else { + if ($preview) { + $sample = $postHandler->create(); + 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); + foreach (['dosmiley', 'doxcode', 'doimage', 'dobr'] as $field) { + $sample->setVar($field, !empty($values[$field]) ? 1 : 0); + } + $sample->unlock(); + $icmsTpl->assign('imforum_preview', $sample->toArray()); + } + + $post_form = $postHandler->create(); + $form = mod_imforum_PostForm::build($post_form, $forum, $topic, $post, $mode, $values, $permissions, $captcha); + $icmsTpl->assign('imforum_form', $form->render()); +} + +include_once ICMS_ROOT_PATH . '/footer.php'; diff --git a/src/templates/imforum_forum.html b/src/templates/imforum_forum.html index a98eb4a..ff0d085 100644 --- a/src/templates/imforum_forum.html +++ b/src/templates/imforum_forum.html @@ -28,6 +28,8 @@

<{$imforum_forum.name}>

<{/if}> + <{if $imforum_new_topic_url}>

<{$smarty.const._MD_IMFORUM_NEW_TOPIC}>

<{/if}> + <{if $imforum_topics}> <{$imforum_pager}> <{include file="db:imforum_topiclist.html"}> @@ -44,5 +46,6 @@

<{$imforum_forum.name}>

<{/if}> + <{include file="db:system_notification_select.html"}> <{include file="db:imforum_online.html"}> diff --git a/src/templates/imforum_index.html b/src/templates/imforum_index.html index b8c8cc1..c185bc7 100644 --- a/src/templates/imforum_index.html +++ b/src/templates/imforum_index.html @@ -57,5 +57,6 @@

<{$smarty.const._MD_IMFORUM_TITLE}>

<{$smarty.const._MD_IMFORUM_NO_FORUMS}>

<{/if}> + <{include file="db:system_notification_select.html"}> <{include file="db:imforum_online.html"}> diff --git a/src/templates/imforum_post.html b/src/templates/imforum_post.html new file mode 100644 index 0000000..3523066 --- /dev/null +++ b/src/templates/imforum_post.html @@ -0,0 +1,32 @@ +
+ <{include file="db:imforum_breadcrumb.html"}> + <{if $imforum_errors}> +
+ <{foreach from=$imforum_errors item=error}>

<{$error}>

<{/foreach}> +
+ <{/if}> + + <{if $imforum_mode == 'delete'}> +

<{$smarty.const._MD_IMFORUM_DELETE}>

+
+ <{$imforum_delete.token}> + + +

<{$smarty.const._MD_IMFORUM_DELETE_CONFIRM}> “<{$imforum_delete.subject}>”

+ <{if $imforum_delete.topic_deleted}>

<{$smarty.const._MD_IMFORUM_DELETE_TOPIC}>

<{/if}> +

<{$smarty.const._MD_IMFORUM_CANCEL}>

+
+ <{else}> + <{if $imforum_disclaimer}>
<{$imforum_disclaimer}>
<{/if}> + <{if $imforum_preview}> +

<{$smarty.const._MD_IMFORUM_PREVIEW}>

+
+
+
<{$imforum_preview.subject}>
+
<{$imforum_preview.text}>
+
+
+ <{/if}> + <{$imforum_form}> + <{/if}> +
diff --git a/src/templates/imforum_topic.html b/src/templates/imforum_topic.html index 3dd10cd..278f758 100644 --- a/src/templates/imforum_topic.html +++ b/src/templates/imforum_topic.html @@ -1,6 +1,6 @@
<{include file="db:imforum_breadcrumb.html"}> -

<{$imforum_topic.title}><{if $imforum_topic.locked}> <{$smarty.const._MD_IMFORUM_LOCKED}><{/if}>

+

<{if $imforum_topic.prefix}>[<{$imforum_topic.prefix}>] <{/if}><{$imforum_topic.title}><{if $imforum_topic.locked}> <{$smarty.const._MD_IMFORUM_LOCKED}><{/if}>

<{$imforum_topic.replies}> <{$smarty.const._MD_IMFORUM_REPLIES}>, <{$imforum_topic.views}> <{$smarty.const._MD_IMFORUM_VIEWS}> · @@ -8,6 +8,7 @@

<{$imforum_topic.title}><{if $imforum_topic.locked}> "><{$smarty.const._MD_IMFORUM_VIEW_TREE}><{/if}>

+ <{if $imforum_reply_url}>

<{$smarty.const._MD_IMFORUM_REPLY}>

<{/if}> <{$imforum_pager}> <{foreach from=$imforum_posts item=post}>
@@ -37,17 +38,51 @@

<{$imforum_topic.title}><{if $imforum_topic.locked}> <{$smarty.const._MD_IMFORUM_ATTACHMENTS}>: +
    + <{foreach from=$post.attachments item=attachment}> +
  • <{$attachment.name}> (<{$attachment.size}>, <{$attachment.downloads}> <{$smarty.const._MD_IMFORUM_DOWNLOADS}>)
  • + <{/foreach}> +
+

+ <{/if}> <{if $post.signature}>
<{$post.signature}>
<{/if}> <{if $post.edits}>
<{$smarty.const._MD_IMFORUM_EDITED_BY}> <{foreach from=$post.edits item=edit name=edits}><{$edit.name}> (<{$edit.time}>)<{if !$smarty.foreach.edits.last}>, <{/if}><{/foreach}>
<{/if}> + <{if $post.reply_url || $post.edit_url || $post.delete_url}> +
+ <{if $post.reply_url}><{$smarty.const._MD_IMFORUM_REPLY}><{/if}> + <{if $post.quote_url}><{$smarty.const._MD_IMFORUM_QUOTE}><{/if}> + <{if $post.edit_url}><{$smarty.const._MD_IMFORUM_EDIT}><{/if}> + <{if $post.delete_url}><{$smarty.const._MD_IMFORUM_DELETE}><{/if}> +
+ <{/if}> <{/foreach}> <{$imforum_pager}> + <{if $imforum_quickreply}> +
+

<{$smarty.const._MD_IMFORUM_QUICKREPLY}>

+ <{$imforum_quickreply.token}> + + + + + + <{if $imforum_quickreply.signature}><{/if}> + <{if $imforum_quickreply.guest}>

<{/if}> +

+

+
+ <{/if}> +

« <{$imforum_topic.forum_name}>

+ <{include file="db:system_notification_select.html"}> <{include file="db:imforum_online.html"}> diff --git a/src/templates/imforum_topiclist.html b/src/templates/imforum_topiclist.html index 58b4e6d..4022520 100644 --- a/src/templates/imforum_topiclist.html +++ b/src/templates/imforum_topiclist.html @@ -19,7 +19,7 @@ <{if $topic.sticky}><{$smarty.const._MD_IMFORUM_STICKY}><{/if}> <{if $topic.digest}><{$smarty.const._MD_IMFORUM_FEATURED}><{/if}> <{if $topic.hot}><{$smarty.const._MD_IMFORUM_HOT}><{/if}> - <{$topic.title}> + <{if $topic.prefix}>[<{$topic.prefix}>] <{/if}><{$topic.title}> <{if $topic.pages}> [<{foreach from=$topic.pages item=page}><{$page.number}> <{/foreach}><{if $topic.more_pages}>…<{/if}>] <{/if}> diff --git a/src/topic.php b/src/topic.php index 78ebbff..18cc479 100644 --- a/src/topic.php +++ b/src/topic.php @@ -61,6 +61,7 @@ } $karma = new mod_imforum_KarmaService($permissions->getUid()); +$canReply = $permissions->canReply($forum, $topic); $minutes = mod_imforum_Settings::getCount('online_minutes'); $posters = mod_imforum_PosterInfo::load(array_map(static fn (mod_imforum_Post $post): int => (int) $post->getVar('uid', 'e'), $posts), $minutes); @@ -81,9 +82,32 @@ 'name' => mod_imforum_View::poster((int) $edit['uid'], (string) $edit['name']), 'time' => mod_imforum_View::time((int) $edit['time']), ], $row['edits']); + $row['attachments'] = []; + $row['can_edit'] = $permissions->canEdit($forum, $topic, $post); + $row['can_delete'] = $permissions->canDelete($forum, $topic, $post); + $row['reply_url'] = $canReply ? mod_imforum_View::link('post', ['topic' => $topicId, 'post' => $postId]) : ''; + $row['quote_url'] = ($canReply && !$row['locked']) ? mod_imforum_View::link('post', ['topic' => $topicId, 'post' => $postId, 'quote' => 1]) : ''; + $row['edit_url'] = $row['can_edit'] ? mod_imforum_View::link('post', ['do' => 'edit', 'post' => $postId]) : ''; + $row['delete_url'] = $row['can_delete'] ? mod_imforum_View::link('post', ['do' => 'delete', 'post' => $postId]) : ''; $rows[] = $row; } +// the files of the posts the reader may open +$attachments = icms_getModuleHandler('attachment', IMFORUM_DIRNAME)->getByPosts(array_keys($posts)); +foreach ($rows as &$row) { + if (!$row['locked']) { + foreach ($attachments[$row['post_id']] ?? [] as $attachment) { + $row['attachments'][] = [ + 'url' => mod_imforum_View::link('attachment', ['id' => (int) $attachment->id()]), + 'name' => $attachment->getVar('name_display'), + 'size' => round((int) $attachment->getVar('filesize', 'e') / 1024) . ' KB', + 'downloads' => (int) $attachment->getVar('downloads', 'e'), + ]; + } + } +} +unset($row); + // reading a page marks the topic as read up to the newest post seen; the last page reads it to the end $tracker = new mod_imforum_ReadTracker($permissions->getUid()); if ($posts) { @@ -121,6 +145,7 @@ $icmsTpl->assign('imforum_breadcrumb', mod_imforum_View::breadcrumb($trail)); $icmsTpl->assign('imforum_topic', [ 'id' => $topicId, + 'prefix' => mod_imforum_View::prefix((int) $topic->getVar('topic_subject', 'e')), 'title' => $topic->getVar('topic_title'), 'views' => (int) $topic->getVar('topic_views', 'e'), 'replies' => (int) $topic->getVar('topic_replies', 'e'), @@ -129,6 +154,19 @@ 'forum_name' => $forum->getVar('forum_name'), ]); $icmsTpl->assign('imforum_posts', $rows); +$icmsTpl->assign('imforum_reply_url', $canReply ? mod_imforum_View::link('post', ['topic' => $topicId]) : ''); +// the quick reply is a short form on the page; with a captcha the full form is needed +$quick = $canReply && (int) mod_imforum_Settings::get('quickreply_enabled') === 1 + && !($permissions->getUid() === 0 && (int) mod_imforum_Settings::get('captcha_guests') === 1); +$icmsTpl->assign('imforum_quickreply', $quick ? [ + 'action' => mod_imforum_View::link('post'), + 'token' => icms::$security->getTokenHTML(), + 'topic' => $topicId, + 'forum' => $forumId, + 'subject' => mod_imforum_View::escape('Re: ' . preg_replace('/^(Re:\s*)+/i', '', (string) $topic->getVar('topic_title', 'n'))), + 'guest' => $permissions->getUid() === 0, + 'signature' => $permissions->getUid() > 0 && (int) $forum->getVar('allow_sig', 'e') === 1, +] : null); $icmsTpl->assign('imforum_mode', $mode); $icmsTpl->assign('imforum_flat_url', mod_imforum_View::link('topic', ['topic' => $topicId, 'mode' => 'flat'])); $icmsTpl->assign('imforum_tree_url', mod_imforum_View::link('topic', ['topic' => $topicId, 'mode' => 'tree'])); diff --git a/tests/Integration/WritingTest.php b/tests/Integration/WritingTest.php new file mode 100644 index 0000000..bc58cea --- /dev/null +++ b/tests/Integration/WritingTest.php @@ -0,0 +1,407 @@ + 0, 'edit_timelimit' => 30, 'subject_prefix' => 'Question, Idea']); + $category = self::category(); + $this->forumId = (int) self::forum((int) $category->id())->id(); + self::grantCategory((int) $category->id(), [2, 3]); + self::grant($this->forumId, ['forum_access', 'forum_view'], [2, 3]); + self::grant($this->forumId, ['forum_post', 'forum_reply', 'forum_edit', 'forum_delete', 'forum_noapprove'], self::MEMBERS); + } + + protected function tearDown(): void + { + Settings::override(null); + } + + private function forumObject(): \mod_imforum_Forum + { + return self::reload('forum', $this->forumId); + } + + private function member(int $uid = 5): Permissions + { + return new Permissions(self::MEMBERS, $uid); + } + + private function service(?Permissions $permissions = null, ?RecordingNotifier $notifier = null): Service + { + return new Service($permissions ?? $this->member(), $notifier ?? new RecordingNotifier()); + } + + /** @return array */ + private static function input(string $subject = 'A subject', string $text = 'Some text'): array + { + return ['subject' => $subject, 'post_text' => $text, 'dosmiley' => 1, 'doxcode' => 1, 'dobr' => 1]; + } + + public function testPermissionsForWritingFollowTheGroups(): void + { + $forum = $this->forumObject(); + $topic = self::topic($this->forumId); + + self::assertTrue($this->member()->canStartTopic($forum)); + self::assertTrue($this->member()->canReply($forum, $topic)); + self::assertFalse((new Permissions(self::GUESTS))->canStartTopic($forum)); + self::assertFalse((new Permissions(self::GUESTS))->canReply($forum, $topic)); + self::assertFalse($this->member()->canAttach($forum), 'no forum_attach given'); + self::assertTrue($this->member()->skipsApproval($forum)); + + self::grant($this->forumId, ['forum_post'], [3]); + self::assertTrue((new Permissions(self::GUESTS))->canStartTopic($forum), 'guests can be given the right'); + } + + public function testLockedTopicsTakeRepliesFromModeratorsOnly(): void + { + $forum = $this->forumObject(); + $topic = self::topic($this->forumId); + \Imforum\Tests\Integration\Database::exec('UPDATE ' . self::handler('topic')->table . ' SET topic_status = 1'); + $locked = self::reload('topic', (int) $topic->id()); + + self::assertFalse($this->member()->canReply($forum, $locked)); + self::handler('moderator')->setModerators($this->forumId, [5]); + self::assertTrue((new Permissions(self::MEMBERS, 5))->canReply($forum, $locked)); + } + + public function testSuspendedMembersCannotWrite(): void + { + $forum = $this->forumObject(); + $topic = self::topic($this->forumId); + self::make('suspension', ['uid' => 5, 'forum_id' => 0, 'start_time' => time() - 10, 'end_time' => time() + 3600]); + + self::assertFalse($this->member(5)->canStartTopic($forum)); + self::assertFalse($this->member(5)->canReply($forum, $topic)); + self::assertTrue($this->member(6)->canReply($forum, $topic), 'other members are fine'); + self::assertTrue($this->member(5)->canReadForum($forum), 'a suspended member can still read'); + } + + public function testStartingATopicCreatesTopicAndFirstPost(): void + { + $notifier = new RecordingNotifier(); + $result = $this->service(null, $notifier)->createTopic($this->forumObject(), self::input('First topic', 'Hello world')); + + self::assertSame([], $result['errors']); + self::assertFalse($result['pending']); + $topic = self::reload('topic', (int) $result['topic']->id()); + $post = self::reload('post', (int) $result['post']->id()); + self::assertSame('First topic', $topic->getVar('topic_title', 'n')); + self::assertSame(5, (int) $topic->getVar('topic_poster', 'e')); + self::assertSame((int) $post->id(), (int) $topic->getVar('topic_first_post_id', 'e')); + self::assertSame(0, (int) $topic->getVar('topic_replies', 'e')); + self::assertSame('Hello world', $post->getVar('post_text', 'n')); + self::assertSame(5, (int) $post->getVar('uid', 'e')); + self::assertSame(1, (int) self::reload('forum', $this->forumId)->getVar('forum_topics', 'e')); + self::assertSame([[true, (int) $post->id()]], $notifier->calls); + } + + public function testReplyCountsAndAnswersAPost(): void + { + $topic = self::topic($this->forumId); + $first = (int) $topic->getVar('topic_first_post_id', 'e'); + $notifier = new RecordingNotifier(); + $result = $this->service(null, $notifier)->reply($this->forumObject(), $topic, self::input('Re: Hello') + ['pid' => $first]); + + self::assertSame([], $result['errors']); + self::assertSame($first, (int) $result['post']->getVar('pid', 'e')); + self::assertSame(1, (int) self::reload('topic', (int) $topic->id())->getVar('topic_replies', 'e')); + self::assertSame([[false, (int) $result['post']->id()]], $notifier->calls); + + // a parent from another topic is ignored, the reply goes under the first post + $other = self::topic($this->forumId, 'Other'); + $stray = (int) $other->getVar('topic_first_post_id', 'e'); + $again = $this->service()->reply($this->forumObject(), self::reload('topic', (int) $topic->id()), self::input() + ['pid' => $stray]); + self::assertSame($first, (int) $again['post']->getVar('pid', 'e')); + } + + public function testPostsWaitForApprovalWithoutTheRight(): void + { + self::grant($this->forumId, [], self::MEMBERS); + \Imforum\Tests\Integration\Database::exec('DELETE FROM ' . \icms::$xoopsDB->prefix('group_permission') . " WHERE gperm_name = 'forum_noapprove'"); + $notifier = new RecordingNotifier(); + $result = $this->service(null, $notifier)->createTopic($this->forumObject(), self::input('Needs approval')); + + self::assertSame([], $result['errors']); + self::assertTrue($result['pending']); + self::assertSame(0, (int) self::reload('topic', (int) $result['topic']->id())->getVar('approved', 'e')); + self::assertSame(0, (int) self::reload('post', (int) $result['post']->id())->getVar('approved', 'e')); + self::assertSame(0, (int) self::reload('forum', $this->forumId)->getVar('forum_topics', 'e'), 'not counted yet'); + self::assertSame([], $notifier->calls, 'nobody is told about a post nobody can read'); + } + + 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]; + $post = $this->service()->createTopic($this->forumObject(), $input)['post']; + + self::assertSame(0, (int) $post->getVar('dohtml', 'e'), 'the forum does not allow HTML'); + self::assertSame(1, (int) $post->getVar('attachsig', 'e'), 'signatures are allowed by default'); + self::assertSame(Service::MAX_KARMA, (int) $post->getVar('post_karma', 'e')); + self::assertSame(5, (int) $post->getVar('uid', 'e')); + self::assertSame(1, (int) $post->getVar('approved', 'e')); + self::assertSame($this->forumId, (int) $post->getVar('forum_id', 'e')); + } + + public function testTheTextIsStoredAsTypedAndMadeSafeWhenShown(): void + { + $text = "a < b [b]bold[/b]"; + $post = $this->service()->createTopic($this->forumObject(), self::input('Raw', $text))['post']; + + $stored = self::reload('post', (int) $post->id()); + self::assertSame($text, $stored->getVar('post_text', 'n'), 'no filter at save: the migrated posts and the new ones look alike'); + $shown = $stored->toArray()['text']; + self::assertStringNotContainsString('