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
|
|
|
|
2023-08-28 01:17:34 +00:00
|
|
|
$forum = $msz->getForum();
|
2023-09-06 13:50:19 +00:00
|
|
|
$usersCtx = $msz->getUsersContext();
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-08-28 01:17:34 +00:00
|
|
|
$categoryId = (int)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 = $forum->getCategory(categoryId: $categoryId);
|
|
|
|
} catch(RuntimeException $ex) {
|
2023-08-31 15:59:53 +00:00
|
|
|
Template::throwError(404);
|
2022-09-13 13:14:49 +00:00
|
|
|
}
|
|
|
|
|
2023-09-06 20:06:07 +00:00
|
|
|
$authInfo = $msz->getAuthInfo();
|
|
|
|
$perms = $authInfo->getPerms('forum', $categoryInfo);
|
2023-08-30 22:37:21 +00:00
|
|
|
|
2023-09-06 20:06:07 +00:00
|
|
|
$currentUser = $authInfo->getUserInfo();
|
2023-08-28 01:17:34 +00:00
|
|
|
$currentUserId = $currentUser === null ? '0' : $currentUser->getId();
|
|
|
|
|
2023-08-31 15:59:53 +00:00
|
|
|
if(!$perms->check(Perm::F_CATEGORY_VIEW))
|
|
|
|
Template::throwError(403);
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-09-06 20:06:07 +00:00
|
|
|
if($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
|
|
|
|
2023-08-28 01:17:34 +00:00
|
|
|
if($categoryInfo->isLink()) {
|
|
|
|
if($categoryInfo->hasLinkTarget()) {
|
|
|
|
$forum->incrementCategoryClicks($categoryInfo);
|
|
|
|
redirect($categoryInfo->getLinkTarget());
|
2023-08-31 15:59:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Template::throwError(404);
|
2022-09-13 13:14:49 +00:00
|
|
|
}
|
|
|
|
|
2023-08-28 01:17:34 +00:00
|
|
|
$forumPagination = new Pagination($forum->countTopics(
|
|
|
|
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
|
|
|
|
2023-08-31 15:59:53 +00:00
|
|
|
if(!$forumPagination->hasValidOffset())
|
|
|
|
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 = $forum->getCategoryChildren($categoryInfo, hidden: false, asTree: true);
|
|
|
|
|
2023-08-30 22:37:21 +00:00
|
|
|
foreach($children as $childId => $child) {
|
2023-09-06 20:06:07 +00:00
|
|
|
$childPerms = $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()) {
|
|
|
|
foreach($child->children as $grandChildId => $grandChild) {
|
2023-09-06 20:06:07 +00:00
|
|
|
$grandChildPerms = $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->getId()];
|
|
|
|
foreach($grandChild->childIds as $greatGrandChildId) {
|
2023-09-06 20:06:07 +00:00
|
|
|
$greatGrandChildPerms = $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 = $forum->checkCategoryUnread($catIds, $currentUser);
|
|
|
|
if($grandChildUnread)
|
|
|
|
$childUnread = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$grandChild->perms = $grandChildPerms;
|
|
|
|
$grandChild->unread = $grandChildUnread;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($child->info->mayHaveChildren() || $child->info->mayHaveTopics()) {
|
|
|
|
$catIds = [$child->info->getId()];
|
|
|
|
foreach($child->childIds as $grandChildId) {
|
2023-09-06 20:06:07 +00:00
|
|
|
$grandChildPerms = $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 = $forum->getPost(categoryInfos: $catIds, getLast: true, deleted: false);
|
|
|
|
} catch(RuntimeException $ex) {
|
|
|
|
$lastPostInfo = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($lastPostInfo !== null) {
|
|
|
|
$child->lastPost = new stdClass;
|
|
|
|
$child->lastPost->info = $lastPostInfo;
|
|
|
|
$child->lastPost->topicInfo = $forum->getTopic(postInfo: $lastPostInfo);
|
|
|
|
|
|
|
|
if($lastPostInfo->hasUserId()) {
|
2023-09-06 13:50:19 +00:00
|
|
|
$child->lastPost->user = $usersCtx->getUserInfo($lastPostInfo->getUserId());
|
|
|
|
$child->lastPost->colour = $usersCtx->getUserColour($child->lastPost->user);
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($child->info->mayHaveTopics() && !$childUnread)
|
|
|
|
$childUnread = $forum->checkCategoryUnread($child->info, $currentUser);
|
|
|
|
|
|
|
|
$child->perms = $childPerms;
|
|
|
|
$child->unread = $childUnread;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($categoryInfo->mayHaveTopics()) {
|
|
|
|
$topicInfos = $forum->getTopics(
|
|
|
|
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 = $forum->checkTopicUnread($topicInfo, $currentUser);
|
|
|
|
$topic->participated = $forum->checkTopicParticipated($topicInfo, $currentUser);
|
|
|
|
$topic->lastPost = new stdClass;
|
|
|
|
|
|
|
|
if($topicInfo->hasUserId()) {
|
2023-09-06 13:50:19 +00:00
|
|
|
$topic->user = $usersCtx->getUserInfo($topicInfo->getUserId());
|
|
|
|
$topic->colour = $usersCtx->getUserColour($topic->user);
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$topic->lastPost->info = $lastPostInfo = $forum->getPost(
|
|
|
|
topicInfo: $topicInfo,
|
|
|
|
getLast: true,
|
|
|
|
deleted: $topicInfo->isDeleted() ? null : false,
|
|
|
|
);
|
|
|
|
|
|
|
|
if($lastPostInfo->hasUserId()) {
|
2023-09-06 13:50:19 +00:00
|
|
|
$topic->lastPost->user = $usersCtx->getUserInfo($lastPostInfo->getUserId());
|
|
|
|
$topic->lastPost->colour = $usersCtx->getUserColour($topic->lastPost->user);
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
|
|
|
} catch(RuntimeException $ex) {}
|
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', [
|
2023-08-28 01:17:34 +00:00
|
|
|
'forum_breadcrumbs' => $forum->getCategoryAncestry($categoryInfo),
|
|
|
|
'global_accent_colour' => $forum->getCategoryColour($categoryInfo),
|
|
|
|
'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
|
|
|
]);
|