aboutsummaryrefslogtreecommitdiff
path: root/controllers/user.php
blob: ac906a0b372bbe910b53ed6e904b478b25aca543 (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
<?php
namespace Controller;
use Database;
use Exception;

function on_post() {
    global $user_status;
    $user_status = "";

    try {
        Database\User::fromDB($_POST["username"]);
        $user_status = "User \"" . $_POST["username"] . "\" already exists!";
        return;
    }
    catch(Exception $e) {}

    try {
        Database\User::create($_POST["username"], $_POST["password"], "User");
    }
    catch(Exception $e) {
        $user_status = $e;
    }
}

function on_patch() {
    global $TOKEN;
    global $METHOD;
    global $username_status;
    global $password_status;
    $username_status = "";
    $password_status = "";

    $status = null;
    switch ($METHOD['type']) {
        case 'username': $status = 'username_status'; break;
        case 'password': $status = 'password_status'; break;
        default: throw new Exception('Invalid patch type ' . $METHOD['type']);
    }

    $user = null;
    try {
        $user = Database\Cookie::fromDB($TOKEN);
    }
    catch(Exception $e) {
        $$status = "Couldn't retrieve user!";
        return;
    }

    switch ($METHOD['type']) {
        case 'username':
            $user->update($METHOD['username']);
            header('Location: /user/' . $METHOD['username']);
            break;
        case 'password':
            $user->update($user->Username, $METHOD['password']);
            header('Location: /user/' . $user->Username);
            break;
    }
    exit();
}

function on_delete() {
    global $TOKEN;
    global $METHOD;
    global $user_status;
    $user_status = "";

    try {
        Database\Cookie::fromDB($TOKEN);
    }
    catch (Exception $e) {
        $user_status = 'Invalid token!';
        return;
    }

    $to_delete = null;
    try {
        $to_delete = Database\User::fromDBuid($METHOD['uid']);
    }
    catch(Exception $e) {
        $list_status = "The user you're trying to delete doesn't exist!";
        return;
    }

    $to_delete->delete();

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