blob: 886fc81ad629eaafc8242a20667aaabddfbb3977 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
<?php
use Digitigrade\FormattingProvider\CommonMark;
use Digitigrade\HttpResponseStatus\BadRequest;
use Digitigrade\HttpResponseStatus\Forbidden;
use Digitigrade\HttpResponseStatus\PolicyRejected;
use Digitigrade\Model\Note;
use Digitigrade\Model\UserAccount;
use Digitigrade\Notification\AdminEditedNoteNotif;
use Digitigrade\PolicyManager;
use Digitigrade\Router;
Router::getInstance()->mount('/fragment/page/:id', function (array $args) {
$note = Note::find($args['id']);
render_template('pages/page_item', ['note' => $note, 'page' => $note->getPage()]);
});
Router::getInstance()->mount('/fragment/page/:id/edit', function (array $args) {
// edit form for a page
$note = Note::find($args['id']);
$user = UserAccount::requireByCurrentSession();
if ($note->author != $user->actor && !$user->isAdmin) {
throw new Forbidden('you may not edit that page!');
}
if (!$note->author->isLocal) {
throw new BadRequest("can't edit a non-local page");
}
if (isset($_POST['title'])) {
$title = trim($_POST['title']);
$body = trim($_POST['body']);
$page = $note->getPage();
$page->title = $title;
$page->content = [
'text/markdown' => $body,
'text/html' => CommonMark::getInstance()->renderToHtml($body)
];
$note->setPage($page);
$note->modified = new DateTimeImmutable();
if (!PolicyManager::getInstance()->check($note)) {
throw new PolicyRejected();
}
$note->save();
$note->publish();
if ($user->actor != $note->author) {
(new AdminEditedNoteNotif($user, $note))->processNotifications();
}
render_template('pages/page_item', ['note' => $note, 'page' => $page]);
} else {
render_template('pages/editable', ['note' => $note, 'page' => $note->getPage()]);
}
});
|