summaryrefslogtreecommitdiff
path: root/lib/accounts.php
blob: a7a230ea5e5efdbd52e98b53ca31cf6b2d32290a (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
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
<?php
include_once "{$_SERVER['DOCUMENT_ROOT']}/lib/config.php";

function authorize_user(bool $required = false): bool
{
    session_start();

    if (!isset($_COOKIE["secret_key"]) && !isset($_SERVER["HTTP_AUTHORIZATION"])) {
        if (isset($_SESSION["user_id"])) {
            session_unset();
        }

        if ($required) {
            if (isset($_SERVER["HTTP_ACCEPT"]) && $_SERVER["HTTP_ACCEPT"] == "application/json") {
                http_response_code(401);
                echo json_encode([
                    "status_code" => 401,
                    "message" => "Unauthorized",
                    "data" => null
                ]);
            } else {
                header("Location: /account");
            }
        }

        return false;
    }

    $db = new PDO(CONFIG['database']['url'], CONFIG['database']['user'], CONFIG['database']['pass']);

    $key = $_SERVER["HTTP_AUTHORIZATION"] ?? $_COOKIE["secret_key"];

    $stmt = $db->prepare("SELECT id, username FROM users WHERE secret_key = ?");
    $stmt->execute([$key]);

    if ($row = $stmt->fetch()) {
        $_SESSION["user_id"] = $row["id"];
        $_SESSION["user_name"] = $row["username"];

        $stmt = $db->prepare("UPDATE users SET last_active_at = UTC_TIMESTAMP WHERE id = ?");
        $stmt->execute([$row["id"]]);

        // fetching role
        $stmt = $db->prepare("SELECT * FROM roles r
        INNER JOIN role_assigns ra ON ra.user_id = ?
        WHERE r.id = ra.role_id
        ");
        $stmt->execute([$row["id"]]);

        $_SESSION["user_role"] = null;

        if ($role = $stmt->fetch(PDO::FETCH_ASSOC)) {
            if ($role["permission_admin"]) {
                foreach ($role as $k => &$v) {
                    if (str_starts_with($k, 'permission_')) {
                        $v = 1;
                    }
                }
                unset($v);
            }

            $_SESSION["user_role"] = $role;
        }

        $stmt = $db->prepare("SELECT es.*, aes.is_default FROM emote_sets es
            INNER JOIN acquired_emote_sets aes ON aes.emote_set_id = es.id
            WHERE aes.user_id = ?
            ORDER BY
                CASE WHEN es.id = ? THEN 0 ELSE 1 END,
                es.id
            ");
        $stmt->execute([$row["id"], $_SESSION["user_active_emote_set_id"] ?? ""]);

        $emote_sets = $stmt->fetchAll(PDO::FETCH_ASSOC);

        if (!isset($_SESSION["user_active_emote_set_id"])) {
            foreach ($emote_sets as $es) {
                if ($es["is_default"]) {
                    $_SESSION["user_active_emote_set"] = $es;
                    $_SESSION["user_active_emote_set_id"] = $es["id"];
                }
            }
        }

        $_SESSION["user_emote_sets"] = $emote_sets;
    } else {
        session_regenerate_id();
        session_unset();
        setcookie("secret_key", "", time() - 1000);

        if ($required) {
            if (isset($_SERVER["HTTP_ACCEPT"]) && $_SERVER["HTTP_ACCEPT"] == "application/json") {
                http_response_code(401);
                echo json_encode([
                    "status_code" => 401,
                    "message" => "Unauthorized",
                    "data" => null
                ]);
            } else {
                header("Location: /account");
            }
        }
    }

    $db = null;
    $stmt = null;
    return isset($_SESSION["user_name"]);
}