misuzu/public-legacy/forum/forum.php

200 lines
7.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
2023-08-28 01:17:34 +00:00
$forum = $msz->getForum();
$users = $msz->getUsers();
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) {
2022-09-13 13:14:49 +00:00
echo render_error(404);
return;
}
2023-08-28 01:17:34 +00:00
$currentUser = $msz->getActiveUser();
$currentUserId = $currentUser === null ? '0' : $currentUser->getId();
$perms = forum_perms_get_user($categoryInfo->getId(), $currentUserId)[MSZ_FORUM_PERMS_GENERAL];
2022-09-13 13:14:49 +00:00
if(!perms_check($perms, MSZ_FORUM_PERM_VIEW_FORUM)) {
echo render_error(403);
return;
}
2023-08-28 01:17:34 +00:00
if(isset($currentUser) && $msz->hasActiveBan($currentUser))
$perms &= MSZ_FORUM_PERM_LIST_FORUM | MSZ_FORUM_PERM_VIEW_FORUM;
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());
} else render_error(404);
2022-09-13 13:14:49 +00:00
return;
}
2023-08-28 01:17:34 +00:00
$forumPagination = new Pagination($forum->countTopics(
categoryInfo: $categoryInfo,
global: true,
deleted: perms_check($perms, MSZ_FORUM_PERM_DELETE_ANY_POST) ? null : false
), 20);
2022-09-13 13:14:49 +00:00
2023-08-28 01:17:34 +00:00
if(!$forumPagination->hasValidOffset()) {
2022-09-13 13:14:49 +00:00
echo render_error(404);
return;
}
2023-08-28 01:17:34 +00:00
$userInfos = [];
$userColours = [];
$children = [];
$topics = [];
if($categoryInfo->mayHaveChildren()) {
$children = $forum->getCategoryChildren($categoryInfo, hidden: false, asTree: true);
foreach($children as $child) {
$childPerms = forum_perms_get_user($child->info->getId(), (int)$currentUserId)[MSZ_FORUM_PERMS_GENERAL];
if(!perms_check($childPerms, MSZ_FORUM_PERM_LIST_FORUM)) {
unset($category->children[$childId]);
continue;
}
$childUnread = false;
if($child->info->mayHaveChildren()) {
foreach($child->children as $grandChildId => $grandChild) {
$grandChildPerms = forum_perms_get_user($grandChild->info->getId(), (int)$currentUserId)[MSZ_FORUM_PERMS_GENERAL];
if(!perms_check($grandChildPerms, MSZ_FORUM_PERM_LIST_FORUM)) {
unset($child->children[$grandChildId]);
continue;
}
$grandChildUnread = false;
if($grandChild->info->mayHaveTopics()) {
$catIds = [$grandChild->info->getId()];
foreach($grandChild->childIds as $greatGrandChildId) {
$greatGrandChildPerms = forum_perms_get_user($greatGrandChildId, (int)$currentUserId)[MSZ_FORUM_PERMS_GENERAL];
if(perms_check($greatGrandChildPerms, MSZ_FORUM_PERM_LIST_FORUM))
$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) {
$grandChildPerms = forum_perms_get_user($grandChildId, (int)$currentUserId)[MSZ_FORUM_PERMS_GENERAL];
if(perms_check($grandChildPerms, MSZ_FORUM_PERM_LIST_FORUM))
$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()) {
$lastPostUserId = $lastPostInfo->getUserId();
if(!array_key_exists($lastPostUserId, $userInfos)) {
$userInfo = $users->getUser($lastPostUserId, 'id');
$userInfos[$lastPostUserId] = $userInfo;
$userColours[$lastPostUserId] = $users->getUserColour($userInfo);
}
$child->lastPost->user = $userInfos[$lastPostUserId];
$child->lastPost->colour = $userColours[$lastPostUserId];
}
}
}
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,
deleted: perms_check($perms, MSZ_FORUM_PERM_DELETE_ANY_POST) ? null : false,
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()) {
$lastTopicUserId = $topicInfo->getUserId();
if(!array_key_exists($lastTopicUserId, $userInfos)) {
$userInfo = $users->getUser($lastTopicUserId, 'id');
$userInfos[$lastTopicUserId] = $userInfo;
$userColours[$lastTopicUserId] = $users->getUserColour($userInfo);
}
$topic->user = $userInfos[$lastTopicUserId];
$topic->colour = $userColours[$lastTopicUserId];
}
try {
$topic->lastPost->info = $lastPostInfo = $forum->getPost(
topicInfo: $topicInfo,
getLast: true,
deleted: $topicInfo->isDeleted() ? null : false,
);
if($lastPostInfo->hasUserId()) {
$lastPostUserId = $lastPostInfo->getUserId();
if(!array_key_exists($lastPostUserId, $userInfos)) {
$userInfo = $users->getUser($lastPostUserId, 'id');
$userInfos[$lastPostUserId] = $userInfo;
$userColours[$lastPostUserId] = $users->getUserColour($userInfo);
}
$topic->lastPost->user = $userInfos[$lastPostUserId];
$topic->lastPost->colour = $userColours[$lastPostUserId];
}
} catch(RuntimeException $ex) {}
2022-09-13 13:14:49 +00:00
}
}
2023-08-28 01:17:34 +00:00
$perms = perms_check_bulk($perms, [
'can_create_topic' => MSZ_FORUM_PERM_CREATE_TOPIC,
]);
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
]);