2022-09-13 13:14:49 +00:00
|
|
|
<?php
|
|
|
|
namespace Misuzu;
|
|
|
|
|
2023-07-15 17:02:46 +00:00
|
|
|
use RuntimeException;
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2024-12-02 02:28:08 +00:00
|
|
|
if(!isset($msz) || !($msz instanceof \Misuzu\MisuzuContext))
|
|
|
|
die('Script must be called through the Misuzu route dispatcher.');
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
if(!$msz->authInfo->getPerms('global')->check(Perm::G_NEWS_POSTS_MANAGE))
|
2023-08-31 15:59:53 +00:00
|
|
|
Template::throwError(403);
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-07-15 17:02:46 +00:00
|
|
|
$postId = (string)filter_input(INPUT_GET, 'p', FILTER_SANITIZE_NUMBER_INT);
|
2024-11-30 04:09:29 +00:00
|
|
|
$loadPostInfo = fn() => $msz->news->getPost($postId);
|
2023-07-15 17:02:46 +00:00
|
|
|
|
|
|
|
if(empty($postId))
|
|
|
|
$isNew = true;
|
|
|
|
else
|
2022-09-13 13:14:49 +00:00
|
|
|
try {
|
2023-07-15 17:02:46 +00:00
|
|
|
$isNew = false;
|
|
|
|
$postInfo = $loadPostInfo();
|
|
|
|
} catch(RuntimeException $ex) {
|
2023-08-31 15:59:53 +00:00
|
|
|
Template::throwError(404);
|
2022-09-13 13:14:49 +00:00
|
|
|
}
|
|
|
|
|
2023-07-15 17:02:46 +00:00
|
|
|
if($_SERVER['REQUEST_METHOD'] === 'GET' && !empty($_GET['delete'])) {
|
2023-08-31 15:59:53 +00:00
|
|
|
if(!CSRF::validateRequest())
|
|
|
|
Template::throwError(403);
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
$msz->news->deletePost($postInfo);
|
|
|
|
$msz->createAuditLog('NEWS_POST_DELETE', [$postInfo->id]);
|
|
|
|
Tools::redirect($msz->urls->format('manage-news-posts'));
|
2023-07-15 17:02:46 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2024-12-02 02:28:08 +00:00
|
|
|
while($_SERVER['REQUEST_METHOD'] === 'POST' && CSRF::validateRequest()) { // @phpstan-ignore-line: this while is just weird, i don't blame it
|
2023-07-15 17:02:46 +00:00
|
|
|
$title = trim((string)filter_input(INPUT_POST, 'np_title'));
|
|
|
|
$category = (string)filter_input(INPUT_POST, 'np_category', FILTER_SANITIZE_NUMBER_INT);
|
|
|
|
$featured = !empty($_POST['np_featured']);
|
|
|
|
$body = trim((string)filter_input(INPUT_POST, 'np_body'));
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-07-15 17:02:46 +00:00
|
|
|
if($isNew) {
|
2024-11-30 04:09:29 +00:00
|
|
|
$postInfo = $msz->news->createPost($category, $title, $body, $featured, $msz->authInfo->userInfo);
|
2023-07-15 17:02:46 +00:00
|
|
|
} else {
|
2024-11-30 04:09:29 +00:00
|
|
|
if($category === $postInfo->categoryId)
|
2023-07-15 17:02:46 +00:00
|
|
|
$category = null;
|
2024-11-30 04:09:29 +00:00
|
|
|
if($title === $postInfo->title)
|
2023-07-15 17:02:46 +00:00
|
|
|
$title = null;
|
2024-11-30 04:09:29 +00:00
|
|
|
if($body === $postInfo->body)
|
2023-07-15 17:02:46 +00:00
|
|
|
$body = null;
|
2024-11-30 04:09:29 +00:00
|
|
|
if($featured === $postInfo->featured)
|
2023-07-15 17:02:46 +00:00
|
|
|
$featured = null;
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-07-15 17:02:46 +00:00
|
|
|
if($category !== null || $title !== null || $body !== null || $featured !== null)
|
2024-11-30 04:09:29 +00:00
|
|
|
$msz->news->updatePost($postInfo, $category, $title, $body, $featured);
|
2023-07-15 17:02:46 +00:00
|
|
|
}
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-07-17 17:43:17 +00:00
|
|
|
$msz->createAuditLog(
|
|
|
|
$isNew ? 'NEWS_POST_CREATE' : 'NEWS_POST_EDIT',
|
2024-11-30 04:09:29 +00:00
|
|
|
[$postInfo->id]
|
2022-09-13 13:14:49 +00:00
|
|
|
);
|
|
|
|
|
2023-07-15 17:02:46 +00:00
|
|
|
if($isNew) {
|
2024-11-30 04:09:29 +00:00
|
|
|
if($postInfo->featured) {
|
2023-03-09 21:38:03 +00:00
|
|
|
// Twitter integration used to be here, replace with Railgun Pulse integration
|
2022-09-13 13:14:49 +00:00
|
|
|
}
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
Tools::redirect($msz->urls->format('manage-news-post', ['post' => $postInfo->id]));
|
2022-09-13 13:14:49 +00:00
|
|
|
return;
|
2023-07-15 17:02:46 +00:00
|
|
|
} else $postInfo = $loadPostInfo();
|
|
|
|
break;
|
2022-09-13 13:14:49 +00:00
|
|
|
}
|
|
|
|
|
2023-07-15 17:02:46 +00:00
|
|
|
$categories = [];
|
2024-11-30 04:09:29 +00:00
|
|
|
foreach($msz->news->getCategories() as $categoryInfo)
|
|
|
|
$categories[$categoryInfo->id] = $categoryInfo->name;
|
2023-07-15 17:02:46 +00:00
|
|
|
|
2022-09-13 13:14:49 +00:00
|
|
|
Template::render('manage.news.post', [
|
|
|
|
'categories' => $categories,
|
2023-07-15 17:02:46 +00:00
|
|
|
'post_new' => $isNew,
|
|
|
|
'post_info' => $postInfo ?? null,
|
2022-09-13 13:14:49 +00:00
|
|
|
]);
|