2022-09-13 13:14:49 +00:00
|
|
|
<?php
|
|
|
|
namespace Misuzu;
|
|
|
|
|
2023-08-28 01:17:34 +00:00
|
|
|
use RuntimeException;
|
|
|
|
|
2023-09-08 13:22:46 +00:00
|
|
|
$forumCtx = $msz->getForumContext();
|
|
|
|
$forumPosts = $forumCtx->getPosts();
|
2023-09-06 20:06:07 +00:00
|
|
|
$usersCtx = $msz->getUsersContext();
|
2023-08-28 01:17:34 +00:00
|
|
|
|
2022-09-13 13:14:49 +00:00
|
|
|
$postId = !empty($_GET['p']) && is_string($_GET['p']) ? (int)$_GET['p'] : 0;
|
|
|
|
$postMode = !empty($_GET['m']) && is_string($_GET['m']) ? (string)$_GET['m'] : '';
|
|
|
|
$submissionConfirmed = !empty($_GET['confirm']) && is_string($_GET['confirm']) && $_GET['confirm'] === '1';
|
|
|
|
|
|
|
|
$postRequestVerified = CSRF::validateRequest();
|
|
|
|
|
2023-09-06 20:06:07 +00:00
|
|
|
$authInfo = $msz->getAuthInfo();
|
|
|
|
if(!empty($postMode) && !$authInfo->isLoggedIn())
|
2023-08-31 15:59:53 +00:00
|
|
|
Template::displayInfo('You must be logged in to manage posts.', 401);
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-09-06 20:06:07 +00:00
|
|
|
$currentUser = $authInfo->getUserInfo();
|
2023-08-02 22:12:47 +00:00
|
|
|
$currentUserId = $currentUser === null ? '0' : $currentUser->getId();
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-09-06 20:06:07 +00:00
|
|
|
if($postMode !== '' && $usersCtx->hasActiveBan($currentUser))
|
2023-08-31 15:59:53 +00:00
|
|
|
Template::displayInfo('You have been banned, check your profile for more information.', 403);
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-08-28 01:17:34 +00:00
|
|
|
try {
|
2023-09-08 13:22:46 +00:00
|
|
|
$postInfo = $forumPosts->getPost(postId: $postId);
|
2023-08-28 01:17:34 +00:00
|
|
|
} catch(RuntimeException $ex) {
|
2023-08-31 15:59:53 +00:00
|
|
|
Template::throwError(404);
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
|
|
|
|
2023-09-06 20:06:07 +00:00
|
|
|
$perms = $authInfo->getPerms('forum', $postInfo->getCategoryId());
|
2023-08-28 01:17:34 +00:00
|
|
|
|
2023-08-31 15:59:53 +00:00
|
|
|
if(!$perms->check(Perm::F_CATEGORY_VIEW))
|
|
|
|
Template::throwError(403);
|
2023-08-28 01:17:34 +00:00
|
|
|
|
2023-08-30 22:37:21 +00:00
|
|
|
$canDeleteAny = $perms->check(Perm::F_POST_DELETE_ANY);
|
2022-09-13 13:14:49 +00:00
|
|
|
|
|
|
|
switch($postMode) {
|
|
|
|
case 'delete':
|
2023-08-28 01:17:34 +00:00
|
|
|
if($canDeleteAny) {
|
2023-08-31 15:59:53 +00:00
|
|
|
if($postInfo->isDeleted())
|
|
|
|
Template::displayInfo('This post has already been marked as deleted.', 404);
|
2023-08-28 01:17:34 +00:00
|
|
|
} else {
|
2023-08-31 15:59:53 +00:00
|
|
|
if($postInfo->isDeleted())
|
|
|
|
Template::throwError(404);
|
2023-08-28 01:17:34 +00:00
|
|
|
|
2023-08-31 15:59:53 +00:00
|
|
|
if(!$perms->check(Perm::F_POST_DELETE_OWN))
|
|
|
|
Template::displayInfo('You are not allowed to delete posts.', 403);
|
2023-08-28 01:17:34 +00:00
|
|
|
|
2023-08-31 15:59:53 +00:00
|
|
|
if($postInfo->getUserId() !== $currentUser->getId())
|
|
|
|
Template::displayInfo('You can only delete your own posts.', 403);
|
2023-08-28 01:17:34 +00:00
|
|
|
|
|
|
|
// posts may only be deleted within a week of creation, this should be a config value
|
|
|
|
$deleteTimeFrame = 60 * 60 * 24 * 7;
|
2023-08-31 15:59:53 +00:00
|
|
|
if($postInfo->getCreatedTime() < time() - $deleteTimeFrame)
|
|
|
|
Template::displayInfo('This post has existed for too long. Ask a moderator to remove if it absolutely necessary.', 403);
|
2022-09-13 13:14:49 +00:00
|
|
|
}
|
|
|
|
|
2023-09-08 13:22:46 +00:00
|
|
|
$originalPostInfo = $forumPosts->getPost(topicInfo: $postInfo->getTopicId());
|
2023-08-31 15:59:53 +00:00
|
|
|
if($originalPostInfo->getId() === $postInfo->getId())
|
|
|
|
Template::displayInfo('This is the opening post of the topic it belongs to, it may not be deleted without deleting the entire topic as well.', 403);
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-01-02 23:12:23 +00:00
|
|
|
if($postRequestVerified && !$submissionConfirmed) {
|
|
|
|
url_redirect('forum-post', [
|
2023-08-28 01:17:34 +00:00
|
|
|
'post' => $postInfo->getId(),
|
|
|
|
'post_fragment' => 'p' . $postInfo->getId(),
|
2023-01-02 23:12:23 +00:00
|
|
|
]);
|
|
|
|
break;
|
|
|
|
} elseif(!$postRequestVerified) {
|
|
|
|
Template::render('forum.confirm', [
|
|
|
|
'title' => 'Confirm post deletion',
|
|
|
|
'class' => 'far fa-trash-alt',
|
2023-08-28 01:17:34 +00:00
|
|
|
'message' => sprintf('You are about to delete post #%d. Are you sure about that?', $postInfo->getId()),
|
2023-01-02 23:12:23 +00:00
|
|
|
'params' => [
|
2023-08-28 01:17:34 +00:00
|
|
|
'p' => $postInfo->getId(),
|
2023-01-02 23:12:23 +00:00
|
|
|
'm' => 'delete',
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
break;
|
2022-09-13 13:14:49 +00:00
|
|
|
}
|
|
|
|
|
2023-09-08 13:22:46 +00:00
|
|
|
$forumPosts->deletePost($postInfo);
|
2023-08-28 01:17:34 +00:00
|
|
|
$msz->createAuditLog('FORUM_POST_DELETE', [$postInfo->getId()]);
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-08-28 01:17:34 +00:00
|
|
|
url_redirect('forum-topic', ['topic' => $postInfo->getTopicId()]);
|
2022-09-13 13:14:49 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'nuke':
|
2023-08-31 15:59:53 +00:00
|
|
|
if(!$canDeleteAny)
|
|
|
|
Template::throwError(403);
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-01-02 23:12:23 +00:00
|
|
|
if($postRequestVerified && !$submissionConfirmed) {
|
|
|
|
url_redirect('forum-post', [
|
2023-08-28 01:17:34 +00:00
|
|
|
'post' => $postInfo->getId(),
|
|
|
|
'post_fragment' => 'p' . $postInfo->getId(),
|
2023-01-02 23:12:23 +00:00
|
|
|
]);
|
|
|
|
break;
|
|
|
|
} elseif(!$postRequestVerified) {
|
|
|
|
Template::render('forum.confirm', [
|
|
|
|
'title' => 'Confirm post nuke',
|
|
|
|
'class' => 'fas fa-radiation',
|
2023-08-28 01:17:34 +00:00
|
|
|
'message' => sprintf('You are about to PERMANENTLY DELETE post #%d. Are you sure about that?', $postInfo->getId()),
|
2023-01-02 23:12:23 +00:00
|
|
|
'params' => [
|
2023-08-28 01:17:34 +00:00
|
|
|
'p' => $postInfo->getId(),
|
2023-01-02 23:12:23 +00:00
|
|
|
'm' => 'nuke',
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
break;
|
2022-09-13 13:14:49 +00:00
|
|
|
}
|
|
|
|
|
2023-09-08 13:22:46 +00:00
|
|
|
$forumPosts->nukePost($postInfo->getId());
|
2023-08-28 01:17:34 +00:00
|
|
|
$msz->createAuditLog('FORUM_POST_NUKE', [$postInfo->getId()]);
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-08-28 01:17:34 +00:00
|
|
|
url_redirect('forum-topic', ['topic' => $postInfo->getTopicId()]);
|
2022-09-13 13:14:49 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'restore':
|
2023-08-31 15:59:53 +00:00
|
|
|
if(!$canDeleteAny)
|
|
|
|
Template::throwError(403);
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-01-02 23:12:23 +00:00
|
|
|
if($postRequestVerified && !$submissionConfirmed) {
|
|
|
|
url_redirect('forum-post', [
|
2023-08-28 01:17:34 +00:00
|
|
|
'post' => $postInfo->getId(),
|
|
|
|
'post_fragment' => 'p' . $postInfo->getId(),
|
2023-01-02 23:12:23 +00:00
|
|
|
]);
|
|
|
|
break;
|
|
|
|
} elseif(!$postRequestVerified) {
|
|
|
|
Template::render('forum.confirm', [
|
|
|
|
'title' => 'Confirm post restore',
|
|
|
|
'class' => 'fas fa-magic',
|
2023-08-28 01:17:34 +00:00
|
|
|
'message' => sprintf('You are about to restore post #%d. Are you sure about that?', $postInfo->getId()),
|
2023-01-02 23:12:23 +00:00
|
|
|
'params' => [
|
2023-08-28 01:17:34 +00:00
|
|
|
'p' => $postInfo->getId(),
|
2023-01-02 23:12:23 +00:00
|
|
|
'm' => 'restore',
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
break;
|
2022-09-13 13:14:49 +00:00
|
|
|
}
|
|
|
|
|
2023-09-08 13:22:46 +00:00
|
|
|
$forumPosts->restorePost($postInfo->getId());
|
2023-08-28 01:17:34 +00:00
|
|
|
$msz->createAuditLog('FORUM_POST_RESTORE', [$postInfo->getId()]);
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-08-28 01:17:34 +00:00
|
|
|
url_redirect('forum-topic', ['topic' => $postInfo->getTopicId()]);
|
2022-09-13 13:14:49 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default: // function as an alt for topic.php?p= by default
|
2023-07-26 18:19:46 +00:00
|
|
|
url_redirect('forum-post', [
|
2023-08-28 01:17:34 +00:00
|
|
|
'post' => $postInfo->getId(),
|
|
|
|
'post_fragment' => 'p' . $postInfo->getId(),
|
2022-09-13 13:14:49 +00:00
|
|
|
]);
|
2023-01-02 23:12:23 +00:00
|
|
|
break;
|
2022-09-13 13:14:49 +00:00
|
|
|
}
|