misuzu/public-legacy/forum/forum.php

168 lines
6.1 KiB
PHP

<?php
namespace Misuzu;
use stdClass;
use RuntimeException;
$categoryId = (int)filter_input(INPUT_GET, 'f', FILTER_SANITIZE_NUMBER_INT);
try {
$categoryInfo = $msz->forumCtx->categories->getCategory(categoryId: $categoryId);
} catch(RuntimeException $ex) {
Template::throwError(404);
}
$perms = $msz->authInfo->getPerms('forum', $categoryInfo);
$currentUser = $msz->authInfo->userInfo;
$currentUserId = $currentUser === null ? '0' : $currentUser->getId();
if(!$perms->check(Perm::F_CATEGORY_VIEW))
Template::throwError(403);
if($msz->usersCtx->hasActiveBan($currentUser))
$perms = $perms->apply(fn($calc) => $calc & (Perm::F_CATEGORY_LIST | Perm::F_CATEGORY_VIEW));
if($categoryInfo->isLink) {
$msz->forumCtx->categories->incrementCategoryClicks($categoryInfo);
Tools::redirect($categoryInfo->linkTarget ?? '/');
return;
}
$forumPagination = new Pagination($msz->forumCtx->topics->countTopics(
categoryInfo: $categoryInfo,
global: true,
deleted: $perms->check(Perm::F_POST_DELETE_ANY) ? null : false
), 20);
if(!$forumPagination->hasValidOffset())
Template::throwError(404);
$children = [];
$topics = [];
if($categoryInfo->mayHaveChildren) {
$children = $msz->forumCtx->categories->getCategoryChildren($categoryInfo, hidden: false, asTree: true);
foreach($children as $childId => $child) {
$childPerms = $msz->authInfo->getPerms('forum', $child->info);
if(!$childPerms->check(Perm::F_CATEGORY_LIST)) {
unset($category->children[$childId]);
continue;
}
$childUnread = false;
if($child->info->mayHaveChildren) {
foreach($child->children as $grandChildId => $grandChild) {
$grandChildPerms = $msz->authInfo->getPerms('forum', $grandChild->info);
if(!$grandChildPerms->check(Perm::F_CATEGORY_LIST)) {
unset($child->children[$grandChildId]);
continue;
}
$grandChildUnread = false;
if($grandChild->info->mayHaveTopics) {
$catIds = [$grandChild->info->id];
foreach($grandChild->childIds as $greatGrandChildId) {
$greatGrandChildPerms = $msz->authInfo->getPerms('forum', $greatGrandChildId);
if(!$greatGrandChildPerms->check(Perm::F_CATEGORY_LIST))
$catIds[] = $greatGrandChildId;
}
$grandChildUnread = $msz->forumCtx->categories->checkCategoryUnread($catIds, $currentUser);
if($grandChildUnread)
$childUnread = true;
}
$grandChild->perms = $grandChildPerms;
$grandChild->unread = $grandChildUnread;
}
}
if($child->info->mayHaveChildren || $child->info->mayHaveTopics) {
$catIds = [$child->info->id];
foreach($child->childIds as $grandChildId) {
$grandChildPerms = $msz->authInfo->getPerms('forum', $grandChildId);
if($grandChildPerms->check(Perm::F_CATEGORY_LIST))
$catIds[] = $grandChildId;
}
try {
$lastPostInfo = $msz->forumCtx->posts->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 = $msz->forumCtx->topics->getTopic(postInfo: $lastPostInfo);
if($lastPostInfo->userId !== null) {
$child->lastPost->user = $msz->usersCtx->getUserInfo($lastPostInfo->userId);
$child->lastPost->colour = $msz->usersCtx->getUserColour($child->lastPost->user);
}
}
}
if($child->info->mayHaveTopics && !$childUnread)
$childUnread = $msz->forumCtx->categories->checkCategoryUnread($child->info, $currentUser);
$child->perms = $childPerms;
$child->unread = $childUnread;
}
}
if($categoryInfo->mayHaveTopics) {
$topicInfos = $msz->forumCtx->topics->getTopics(
categoryInfo: $categoryInfo,
global: true,
deleted: $perms->check(Perm::F_POST_DELETE_ANY) ? null : false,
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);
if($topicInfo->userId !== null) {
$topic->user = $msz->usersCtx->getUserInfo($topicInfo->userId);
$topic->colour = $msz->usersCtx->getUserColour($topic->user);
}
try {
$topic->lastPost = new stdClass;
$topic->lastPost->info = $lastPostInfo = $msz->forumCtx->posts->getPost(
topicInfo: $topicInfo,
getLast: true,
deleted: $topicInfo->deleted ? null : false,
);
if($lastPostInfo->userId !== null) {
$topic->lastPost->user = $msz->usersCtx->getUserInfo($lastPostInfo->userId);
$topic->lastPost->colour = $msz->usersCtx->getUserColour($topic->lastPost->user);
}
} catch(RuntimeException $ex) {
$topic->lastPost = null;
}
}
}
$perms = $perms->checkMany([
'can_create_topic' => Perm::F_TOPIC_CREATE,
]);
Template::render('forum.forum', [
'forum_breadcrumbs' => iterator_to_array($msz->forumCtx->categories->getCategoryAncestry($categoryInfo)),
'global_accent_colour' => $msz->forumCtx->categories->getCategoryColour($categoryInfo),
'forum_info' => $categoryInfo,
'forum_children' => $children,
'forum_topics' => $topics,
'forum_pagination' => $forumPagination,
'forum_show_mark_as_read' => $currentUser !== null,
'forum_perms' => $perms,
]);