misuzu/public-legacy/forum/forum.php

172 lines
6.2 KiB
PHP
Raw Normal View History

2022-09-13 13:14:49 +00:00
<?php
namespace Misuzu;
2023-08-28 01:17:34 +00:00
use stdClass;
use RuntimeException;
2022-09-13 13:14:49 +00:00
2024-12-02 02:28:08 +00:00
if(!isset($msz) || !($msz instanceof \Misuzu\MisuzuContext))
die('Script must be called through the Misuzu route dispatcher.');
$categoryId = (string)filter_input(INPUT_GET, 'f', FILTER_SANITIZE_NUMBER_INT);
2022-09-13 13:14:49 +00:00
2023-08-28 01:17:34 +00:00
try {
$categoryInfo = $msz->forumCtx->categories->getCategory(categoryId: $categoryId);
2023-08-28 01:17:34 +00:00
} catch(RuntimeException $ex) {
Template::throwError(404);
2022-09-13 13:14:49 +00:00
}
$perms = $msz->authInfo->getPerms('forum', $categoryInfo);
2023-08-30 22:37:21 +00:00
$currentUser = $msz->authInfo->userInfo;
2024-11-30 04:20:20 +00:00
$currentUserId = $currentUser === null ? '0' : $currentUser->id;
2023-08-28 01:17:34 +00:00
if(!$perms->check(Perm::F_CATEGORY_VIEW))
Template::throwError(403);
2022-09-13 13:14:49 +00:00
if($msz->usersCtx->hasActiveBan($currentUser))
2023-08-30 22:37:21 +00:00
$perms = $perms->apply(fn($calc) => $calc & (Perm::F_CATEGORY_LIST | Perm::F_CATEGORY_VIEW));
2022-09-13 13:14:49 +00:00
if($categoryInfo->isLink) {
$msz->forumCtx->categories->incrementCategoryClicks($categoryInfo);
Tools::redirect($categoryInfo->linkTarget ?? '/');
return;
2022-09-13 13:14:49 +00:00
}
2024-12-19 01:22:26 +00:00
$forumPagination = Pagination::fromInput($msz->forumCtx->topics->countTopics(
2023-08-28 01:17:34 +00:00
categoryInfo: $categoryInfo,
global: true,
2023-08-30 22:37:21 +00:00
deleted: $perms->check(Perm::F_POST_DELETE_ANY) ? null : false
2023-08-28 01:17:34 +00:00
), 20);
2022-09-13 13:14:49 +00:00
2024-12-19 01:22:26 +00:00
if(!$forumPagination->validOffset)
Template::throwError(404);
2022-09-13 13:14:49 +00:00
2023-08-28 01:17:34 +00:00
$children = [];
$topics = [];
if($categoryInfo->mayHaveChildren) {
$children = $msz->forumCtx->categories->getCategoryChildren($categoryInfo, hidden: false, asTree: true);
2023-08-28 01:17:34 +00:00
2023-08-30 22:37:21 +00:00
foreach($children as $childId => $child) {
$childPerms = $msz->authInfo->getPerms('forum', $child->info);
2023-08-30 22:37:21 +00:00
if(!$childPerms->check(Perm::F_CATEGORY_LIST)) {
2023-08-28 01:17:34 +00:00
unset($category->children[$childId]);
continue;
}
$childUnread = false;
if($child->info->mayHaveChildren) {
2023-08-28 01:17:34 +00:00
foreach($child->children as $grandChildId => $grandChild) {
$grandChildPerms = $msz->authInfo->getPerms('forum', $grandChild->info);
2023-08-30 22:37:21 +00:00
if(!$grandChildPerms->check(Perm::F_CATEGORY_LIST)) {
2023-08-28 01:17:34 +00:00
unset($child->children[$grandChildId]);
continue;
}
$grandChildUnread = false;
if($grandChild->info->mayHaveTopics) {
$catIds = [$grandChild->info->id];
2023-08-28 01:17:34 +00:00
foreach($grandChild->childIds as $greatGrandChildId) {
$greatGrandChildPerms = $msz->authInfo->getPerms('forum', $greatGrandChildId);
2023-08-30 22:37:21 +00:00
if(!$greatGrandChildPerms->check(Perm::F_CATEGORY_LIST))
2023-08-28 01:17:34 +00:00
$catIds[] = $greatGrandChildId;
}
$grandChildUnread = $msz->forumCtx->categories->checkCategoryUnread($catIds, $currentUser);
2023-08-28 01:17:34 +00:00
if($grandChildUnread)
$childUnread = true;
}
$grandChild->perms = $grandChildPerms;
$grandChild->unread = $grandChildUnread;
}
}
if($child->info->mayHaveChildren || $child->info->mayHaveTopics) {
$catIds = [$child->info->id];
2023-08-28 01:17:34 +00:00
foreach($child->childIds as $grandChildId) {
$grandChildPerms = $msz->authInfo->getPerms('forum', $grandChildId);
2023-08-30 22:37:21 +00:00
if($grandChildPerms->check(Perm::F_CATEGORY_LIST))
2023-08-28 01:17:34 +00:00
$catIds[] = $grandChildId;
}
try {
$lastPostInfo = $msz->forumCtx->posts->getPost(categoryInfos: $catIds, getLast: true, deleted: false);
2023-08-28 01:17:34 +00:00
} catch(RuntimeException $ex) {
$lastPostInfo = null;
}
if($lastPostInfo !== null) {
$child->lastPost = new stdClass;
$child->lastPost->info = $lastPostInfo;
$child->lastPost->topicInfo = $msz->forumCtx->topics->getTopic(postInfo: $lastPostInfo);
2023-08-28 01:17:34 +00:00
if($lastPostInfo->userId !== null) {
$child->lastPost->user = $msz->usersCtx->getUserInfo($lastPostInfo->userId);
$child->lastPost->colour = $msz->usersCtx->getUserColour($child->lastPost->user);
2023-08-28 01:17:34 +00:00
}
}
}
if($child->info->mayHaveTopics && !$childUnread)
$childUnread = $msz->forumCtx->categories->checkCategoryUnread($child->info, $currentUser);
2023-08-28 01:17:34 +00:00
$child->perms = $childPerms;
$child->unread = $childUnread;
}
}
if($categoryInfo->mayHaveTopics) {
$topicInfos = $msz->forumCtx->topics->getTopics(
2023-08-28 01:17:34 +00:00
categoryInfo: $categoryInfo,
global: true,
2023-08-30 22:37:21 +00:00
deleted: $perms->check(Perm::F_POST_DELETE_ANY) ? null : false,
2023-08-28 01:17:34 +00:00
pagination: $forumPagination,
);
foreach($topicInfos as $topicInfo) {
$topics[] = $topic = new stdClass;
$topic->info = $topicInfo;
$topic->unread = $msz->forumCtx->topics->checkTopicUnread($topicInfo, $currentUser);
$topic->participated = $msz->forumCtx->topics->checkTopicParticipated($topicInfo, $currentUser);
2023-08-28 01:17:34 +00:00
if($topicInfo->userId !== null) {
$topic->user = $msz->usersCtx->getUserInfo($topicInfo->userId);
$topic->colour = $msz->usersCtx->getUserColour($topic->user);
2023-08-28 01:17:34 +00:00
}
try {
2023-09-10 19:13:36 +00:00
$topic->lastPost = new stdClass;
$topic->lastPost->info = $lastPostInfo = $msz->forumCtx->posts->getPost(
2023-08-28 01:17:34 +00:00
topicInfo: $topicInfo,
getLast: true,
deleted: $topicInfo->deleted ? null : false,
2023-08-28 01:17:34 +00:00
);
if($lastPostInfo->userId !== null) {
$topic->lastPost->user = $msz->usersCtx->getUserInfo($lastPostInfo->userId);
$topic->lastPost->colour = $msz->usersCtx->getUserColour($topic->lastPost->user);
2023-08-28 01:17:34 +00:00
}
2023-09-10 19:13:36 +00:00
} catch(RuntimeException $ex) {
$topic->lastPost = null;
}
2022-09-13 13:14:49 +00:00
}
}
2023-08-30 22:37:21 +00:00
$perms = $perms->checkMany([
'can_create_topic' => Perm::F_TOPIC_CREATE,
2023-08-28 01:17:34 +00:00
]);
2022-09-13 13:14:49 +00:00
Template::render('forum.forum', [
'forum_breadcrumbs' => iterator_to_array($msz->forumCtx->categories->getCategoryAncestry($categoryInfo)),
'global_accent_colour' => $msz->forumCtx->categories->getCategoryColour($categoryInfo),
2023-08-28 01:17:34 +00:00
'forum_info' => $categoryInfo,
'forum_children' => $children,
2022-09-13 13:14:49 +00:00
'forum_topics' => $topics,
'forum_pagination' => $forumPagination,
2023-08-28 01:17:34 +00:00
'forum_show_mark_as_read' => $currentUser !== null,
'forum_perms' => $perms,
2022-09-13 13:14:49 +00:00
]);