misuzu/public-legacy/forum/index.php

208 lines
8.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;
$forum = $msz->getForum();
$users = $msz->getUsers();
$mode = (string)filter_input(INPUT_GET, 'm');
2022-09-13 13:14:49 +00:00
$currentUser = $msz->getActiveUser();
$currentUserId = $currentUser === null ? '0' : $currentUser->getId();
2022-09-13 13:14:49 +00:00
2023-08-28 01:17:34 +00:00
if($mode === 'mark') {
if(!$msz->isLoggedIn()) {
echo render_error(403);
return;
}
$categoryId = filter_input(INPUT_GET, 'f', FILTER_SANITIZE_NUMBER_INT);
2023-08-28 01:17:34 +00:00
if($_SERVER['REQUEST_METHOD'] === 'POST' && CSRF::validateRequest()) {
$categoryInfos = $categoryId === null
? $forum->getCategories()
: $forum->getCategoryChildren(parentInfo: $categoryId, includeSelf: true);
foreach($categoryInfos as $categoryInfo) {
$perms = forum_perms_get_user($categoryInfo->getId(), (int)$currentUserId)[MSZ_FORUM_PERMS_GENERAL];
if(perms_check($perms, MSZ_FORUM_PERM_LIST_FORUM))
$forum->updateUserReadCategory($userInfo, $categoryInfo);
}
2023-08-28 01:17:34 +00:00
url_redirect($categoryId ? 'forum-category' : 'forum-index', ['forum' => $categoryId]);
return;
}
Template::render('confirm', [
'title' => 'Mark forum as read',
'message' => 'Are you sure you want to mark ' . ($categoryId < 1 ? 'the entire' : 'this') . ' forum as read?',
'return' => url($categoryId ? 'forum-category' : 'forum-index', ['forum' => $categoryId]),
'params' => [
'forum' => $categoryId,
]
]);
return;
}
if($mode !== '') {
echo render_error(404);
return;
}
$userInfos = [];
$userColours = [];
$categories = $forum->getCategories(hidden: false, asTree: true);
foreach($categories as $categoryId => $category) {
$perms = forum_perms_get_user($category->info->getId(), (int)$currentUserId)[MSZ_FORUM_PERMS_GENERAL];
if(!perms_check($perms, MSZ_FORUM_PERM_LIST_FORUM)) {
unset($categories[$categoryId]);
continue;
}
$unread = false;
if($category->info->mayHaveChildren())
foreach($category->children as $childId => $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($category->info->isListing()) {
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];
}
}
2022-09-13 13:14:49 +00:00
}
2023-08-28 01:17:34 +00:00
}
2022-09-13 13:14:49 +00:00
2023-08-28 01:17:34 +00:00
if($child->info->mayHaveTopics() && !$childUnread) {
$childUnread = $forum->checkCategoryUnread($child->info, $currentUser);
if($childUnread)
$unread = true;
2022-09-13 13:14:49 +00:00
}
2023-08-28 01:17:34 +00:00
$child->perms = $childPerms;
$child->unread = $childUnread;
2022-09-13 13:14:49 +00:00
}
2023-08-28 01:17:34 +00:00
if($category->info->mayHaveTopics() && !$unread)
$unread = $forum->checkCategoryUnread($category->info, $currentUser);
if(!$category->info->isListing()) {
if(!array_key_exists('0', $categories)) {
$categories['0'] = $root = new stdClass;
$root->info = null;
$root->perms = 0;
$root->unread = false;
$root->colour = null;
$root->children = [];
}
$categories['0']->children[$categoryId] = $category;
unset($categories[$categoryId]);
if($category->info->mayHaveChildren() || $category->info->mayHaveTopics()) {
$catIds = [$category->info->getId()];
foreach($category->childIds as $childId) {
$childPerms = forum_perms_get_user($childId, (int)$currentUserId)[MSZ_FORUM_PERMS_GENERAL];
if(perms_check($childPerms, MSZ_FORUM_PERM_LIST_FORUM))
$catIds[] = $childId;
}
try {
$lastPostInfo = $forum->getPost(categoryInfos: $catIds, getLast: true, deleted: false);
} catch(RuntimeException $ex) {
$lastPostInfo = null;
}
if($lastPostInfo !== null) {
$category->lastPost = new stdClass;
$category->lastPost->info = $lastPostInfo;
$category->lastPost->topicInfo = $forum->getTopic(postInfo: $lastPostInfo);
if($lastPostInfo->hasUserId()) {
$lastPostUserId = $lastPostInfo->getUserId();
if(!array_key_exists($lastPostUserId, $userInfos)) {
$userInfo = $users->getUser($lastPostInfo->getUserId(), 'id');
$userInfos[$lastPostUserId] = $userInfo;
$userColours[$lastPostUserId] = $users->getUserColour($userInfo);
}
$category->lastPost->user = $userInfos[$lastPostUserId];
$category->lastPost->colour = $userColours[$lastPostUserId];
}
}
}
}
$category->perms = $perms;
$category->unread = $unread;
2022-09-13 13:14:49 +00:00
}
2023-08-28 01:17:34 +00:00
Template::render('forum.index', [
'forum_categories' => $categories,
'forum_empty' => empty($categories),
'forum_show_mark_as_read' => $currentUser !== null,
]);