84 lines
2.7 KiB
PHP
84 lines
2.7 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('user')->check(Perm::U_NOTES_MANAGE))
|
|
Template::throwError(403);
|
|
|
|
$hasNoteId = !empty($_GET['n']);
|
|
$hasUserId = !empty($_GET['u']);
|
|
|
|
if((!$hasNoteId && !$hasUserId) || ($hasNoteId && $hasUserId))
|
|
Template::throwError(400);
|
|
|
|
if($hasUserId) {
|
|
$isNew = true;
|
|
|
|
try {
|
|
$userInfo = $msz->usersCtx->getUserInfo(!empty($_GET['u']) && is_scalar($_GET['u']) ? (string)$_GET['u'] : '');
|
|
} catch(RuntimeException $ex) {
|
|
Template::throwError(404);
|
|
}
|
|
|
|
$authorInfo = $msz->authInfo->userInfo;
|
|
} elseif($hasNoteId) {
|
|
$isNew = false;
|
|
|
|
try {
|
|
$noteInfo = $msz->usersCtx->modNotes->getNote(!empty($_GET['n']) && is_scalar($_GET['n']) ? (string)$_GET['n'] : '');
|
|
} catch(RuntimeException $ex) {
|
|
Template::throwError(404);
|
|
}
|
|
|
|
if($_SERVER['REQUEST_METHOD'] === 'GET' && !empty($_GET['delete'])) {
|
|
if(!$msz->csrfCtx->verifyLegacy())
|
|
Template::throwError(403);
|
|
|
|
$msz->usersCtx->modNotes->deleteNotes($noteInfo);
|
|
$msz->logsCtx->createAuthedLog('MOD_NOTE_DELETE', [$noteInfo->id, $noteInfo->userId]);
|
|
Tools::redirect($msz->urls->format('manage-users-notes', ['user' => $noteInfo->userId]));
|
|
return;
|
|
}
|
|
|
|
$userInfo = $msz->usersCtx->getUserInfo($noteInfo->userId);
|
|
$authorInfo = $noteInfo->authorId !== null ? $msz->usersCtx->getUserInfo($noteInfo->authorId) : null;
|
|
}
|
|
|
|
while($_SERVER['REQUEST_METHOD'] === 'POST' && $msz->csrfCtx->verifyLegacy()) {
|
|
$title = trim((string)($_POST['mn_title'] ?? ''));
|
|
$body = trim((string)($_POST['mn_body'] ?? ''));
|
|
|
|
if($isNew) {
|
|
$noteInfo = $msz->usersCtx->modNotes->createNote($userInfo, $title, $body, $authorInfo);
|
|
} else {
|
|
if($title === $noteInfo->title)
|
|
$title = null;
|
|
if($body === $noteInfo->body)
|
|
$body = null;
|
|
|
|
if($title !== null || $body !== null)
|
|
$msz->usersCtx->modNotes->updateNote($noteInfo, $title, $body);
|
|
}
|
|
|
|
$msz->logsCtx->createAuthedLog(
|
|
$isNew ? 'MOD_NOTE_CREATE' : 'MOD_NOTE_UPDATE',
|
|
[$noteInfo->id, $userInfo->id],
|
|
);
|
|
|
|
// this is easier
|
|
Tools::redirect($msz->urls->format('manage-users-note', ['note' => $noteInfo->id]));
|
|
return;
|
|
}
|
|
|
|
Template::render('manage.users.note', [
|
|
'note_new' => $isNew,
|
|
'note_info' => $noteInfo ?? null,
|
|
'note_user' => $userInfo,
|
|
'note_user_colour' => $msz->usersCtx->getUserColour($userInfo),
|
|
'note_author' => $authorInfo,
|
|
'note_author_colour' => $msz->usersCtx->getUserColour($authorInfo),
|
|
]);
|