aboutsummaryrefslogtreecommitdiff
path: root/controllers/list.php
blob: c2e72a0f5aca719c77d20a4775b8dc9f46b6ed46 (plain) (blame)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace Controller;
use Database;
use Exception;

function on_post() {
    global $TOKEN;
    global $list_status;
    global $lid;
    $list_status = "";
    $lid = 0;

    try {
        $uid = Database\Cookie::fromDB($TOKEN)->UID;
        $lid = Database\ArchiveList::create($uid, $_POST["name"], $_POST["description"]);
    }
    catch(Exception $e) {
        $list_status = $e;
    }
}

function on_patch() {
    global $TOKEN;
    global $METHOD;

    try {
        $user = Database\Cookie::fromDB($TOKEN);
    }
    catch(Exception $e) {
        return;
    }

    $list = null;
    try {
        $list = Database\ArchiveList::fromDB($METHOD['lid']);
    }
    catch(Exception $e) {
        return;
    }

    switch ($METHOD['type']) {
        case 'add': $list->addItem($METHOD['wid']); break;

        default: throw new Exception('Unknown type ' . $METHOD['type']);
    }

    header('Location: /list/' . $list->LID);
    exit();
}

function on_put() {
    global $TOKEN;
    global $METHOD;
    global $list_status;

    $list = null;
    try {
        $list = Database\ArchiveList::fromDB($METHOD['lid']);
    }
    catch(Exception $e) {
        $list_status = "This list doesn't exist!";
        return;
    }

    try {
        $user = Database\Cookie::fromDB($TOKEN);
        $author = Database\User::fromDBuid($list->AuthorUID);
        if ($author->UID !== $user->UID) {
            $list_status = "You're not the owner of this list! You have no permission to edit it!";
            return;
        }
    }
    catch(Exception $e) {
        $list_status = "Either your cookie is invalid or the author of this list has deleted their account!";
        return;
    }

    $list->update($METHOD['name'], $METHOD['description']);

    header('Location: /list/' . $list->LID);
    exit();
}

function on_delete() {
    global $TOKEN;
    global $METHOD;
    global $list_status;

    $list = null;
    try {
        $list = Database\ArchiveList::fromDB($METHOD['lid']);
    }
    catch(Exception $e) {
        $list_status = "This list doesn't exist!";
        return;
    }

    try {
        $user = Database\Cookie::fromDB($TOKEN);
        $author = Database\User::fromDBuid($list->AuthorUID);
        if ($author->UID !== $user->UID) {
            $list_status = "You're not the owner of this list! You have no permission to delete it!";
            return;
        }
    }
    catch(Exception $e) {
        $list_status = "Either your cookie is invalid or the author of this list has deleted their account!";
        return;
    }

    $list->delete();

    header('Location: /');
    exit();
}