aboutsummaryrefslogtreecommitdiff
path: root/handler/admin/PageAdd.php
blob: 8754f0f2da583a8d2aff58ef25af82ea0c94b9a3 (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
57
58
59
60
61
62
63
64
65
66
<?php

namespace handler\admin;

use csrf;
use NotFoundException;
use pages;
use RedirectResponse;
use Response;

class PageAdd extends AutoAddOrEdit {

    public function get(): Response {
        list($name) = $this->input('short_name');
        $page = pages::getPageByName($name);
        if ($page)
            throw new NotFoundException();

        return $this->_get_pageAdd($name);
    }

    public function post(): Response {
        csrf::check('addpage');

        list($name) = $this->input('short_name');
        $page = pages::getPageByName($name);
        if ($page)
            throw new NotFoundException();

        $text = trim($_POST['text'] ?? '');
        $title = trim($_POST['title'] ?? '');
        $error_code = null;

        if (!$title) {
            $error_code = 'no_title';
        } else if (!$text) {
            $error_code = 'no_text';
        }

        if ($error_code) {
            return $this->_get_pageAdd(
                name: $name,
                text: $text,
                title: $title,
                error_code: $error_code
            );
        }

        if (!pages::add([
            'short_name' => $name,
            'title' => $title,
            'md' => $text
        ])) {
            return $this->_get_pageAdd(
                name: $name,
                text: $text,
                title: $title,
                error_code: 'db_err'
            );
        }

        $page = pages::getPageByName($name);
        return new RedirectResponse($page->getUrl());
    }

}