misuzu/public-legacy/manage/news/post.php

81 lines
2.5 KiB
PHP

<?php
namespace Misuzu;
use RuntimeException;
if(!isset($msz) || !($msz instanceof \Misuzu\MisuzuContext))
die('Script must be called through the Misuzu route dispatcher.');
if(!$msz->authInfo->getPerms('global')->check(Perm::G_NEWS_POSTS_MANAGE))
Template::throwError(403);
$postId = (string)filter_input(INPUT_GET, 'p', FILTER_SANITIZE_NUMBER_INT);
$loadPostInfo = fn() => $msz->news->getPost($postId);
if(empty($postId))
$isNew = true;
else
try {
$isNew = false;
$postInfo = $loadPostInfo();
} catch(RuntimeException $ex) {
Template::throwError(404);
}
if($_SERVER['REQUEST_METHOD'] === 'GET' && !empty($_GET['delete'])) {
if(!CSRF::validateRequest())
Template::throwError(403);
$msz->news->deletePost($postInfo);
$msz->createAuditLog('NEWS_POST_DELETE', [$postInfo->id]);
Tools::redirect($msz->urls->format('manage-news-posts'));
return;
}
while($_SERVER['REQUEST_METHOD'] === 'POST' && CSRF::validateRequest()) {
$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'));
if($isNew) {
$postInfo = $msz->news->createPost($category, $title, $body, $featured, $msz->authInfo->userInfo);
} else {
if($category === $postInfo->categoryId)
$category = null;
if($title === $postInfo->title)
$title = null;
if($body === $postInfo->body)
$body = null;
if($featured === $postInfo->featured)
$featured = null;
if($category !== null || $title !== null || $body !== null || $featured !== null)
$msz->news->updatePost($postInfo, $category, $title, $body, $featured);
}
$msz->createAuditLog(
$isNew ? 'NEWS_POST_CREATE' : 'NEWS_POST_EDIT',
[$postInfo->id]
);
if($isNew) {
if($postInfo->featured) {
// Twitter integration used to be here, replace with Railgun Pulse integration
}
Tools::redirect($msz->urls->format('manage-news-post', ['post' => $postInfo->id]));
return;
} else $postInfo = $loadPostInfo();
break;
}
$categories = [];
foreach($msz->news->getCategories() as $categoryInfo)
$categories[$categoryInfo->id] = $categoryInfo->name;
Template::render('manage.news.post', [
'categories' => $categories,
'post_new' => $isNew,
'post_info' => $postInfo ?? null,
]);