summaryrefslogtreecommitdiff
path: root/includes/util/session.inc
blob: e3af58bd83dc1dabc4c97183ed735d3a90ee6350 (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
109
110
111
112
113
114
<?php

require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/util/functions.inc";

global $isLoggedIn;
global $isLowerLoggedIn;
global $_PROFILE;

$isLoggedIn = false;
$isLowerLoggedIn = false;

if (!function_exists("formatPonypush")) {
    function formatPonypush($message) {
        return "Update to Ponypush 3.1.0 or later — (\$PA1$\$" . base64_encode($message) . "\$\$)";
    }
}

$authorization = null;
$post = null;

if ($_SERVER['REQUEST_METHOD'] === "POST") {
    $request_raw = file_get_contents('php://input');
    $json_object = $data = json_decode($request_raw, true);

    if (json_last_error() === JSON_ERROR_NONE) {
        $post = $data["_session"] ?? null;
    }
}

if (isset($_SERVER['HTTP_AUTHORIZATION']) && str_starts_with(trim($_SERVER['HTTP_AUTHORIZATION']), "Bearer ")) {
    $authorization = trim(substr($_SERVER['HTTP_AUTHORIZATION'], 7));
}

$token = $authorization ?? $post ?? $_POST["_session"] ?? $_GET["_session"] ?? $_COOKIE['PEH2_SESSION_TOKEN'] ?? null;

if (isset($token)) {
    if (!(str_contains($token, "/") || trim($token) === "" || trim($token) === "." || trim($token) === "..")) {
        if (file_exists($_SERVER['DOCUMENT_ROOT'] . "/includes/tokens/" . str_replace("/", "", $token))) {
            $data = json_decode(pf_utf8_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/tokens/" . str_replace("/", "", $token))), true);

            if (isset($data["profile"])) {
                $_PROFILE = $data["profile"];

                if (time() - $data["last"] > 86400 * 30) {
                    unlink($_SERVER['DOCUMENT_ROOT'] . "/includes/tokens/" . str_replace("/", "", $token));
                    unset($_PROFILE);
                    $isLoggedIn = false;
                    $isLowerLoggedIn = false;
                }

                $data["last"] = time();
                if (!isset($data["addresses"])) $data["addresses"] = [];
                $data["addresses"][$_SERVER["HTTP_X_FORWARDED_FOR"] ?? $_SERVER["REMOTE_ADDR"]] = time();

                copy($_SERVER['DOCUMENT_ROOT'] . "/includes/tokens/" . str_replace("/", "", $token), $_SERVER['DOCUMENT_ROOT'] . "/includes/tokens/" . str_replace("/", "", $token) . ".old");

                file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/tokens/" . str_replace("/", "", $token), pf_utf8_encode(json_encode($data)));

                if (trim(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/tokens/" . str_replace("/", "", $token)) === "")) {
                    unlink($_SERVER['DOCUMENT_ROOT'] . "/includes/tokens/" . str_replace("/", "", $token));
                    copy($_SERVER['DOCUMENT_ROOT'] . "/includes/tokens/" . str_replace("/", "", $token) . ".old", $_SERVER['DOCUMENT_ROOT'] . "/includes/tokens/" . str_replace("/", "", $token));
                }

                unlink($_SERVER['DOCUMENT_ROOT'] . "/includes/tokens/" . str_replace("/", "", $token) . ".old");
            } else {
                $_PROFILE = $data;
            }

            if (isset($_GET['invert'])) {
                $_PROFILE["login"] = $_PROFILE["login"] === "raindrops" ? "cloudburst" : "raindrops";
                $_PROFILE["name"] = $_PROFILE["name"] === "Raindrops System" ? "Cloudburst System" : "Raindrops System";
            }

            $isLoggedIn = true;
        } elseif (file_exists($_SERVER['DOCUMENT_ROOT'] . "/includes/lowertokens/" . str_replace("/", "", $token))) {
            $data = json_decode(pf_utf8_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/lowertokens/" . str_replace("/", "", $token))), true);

            if (isset($data["profile"])) {
                $_PROFILE = $data["profile"];

                if (time() - $data["last"] > 86400 * 30) {
                    unlink($_SERVER['DOCUMENT_ROOT'] . "/includes/tokens/" . str_replace("/", "", $token));
                    unset($_PROFILE);
                    $isLoggedIn = false;
                    $isLowerLoggedIn = false;
                }

                $data["last"] = time();
                if (!isset($data["addresses"])) $data["addresses"] = [];
                $data["addresses"][$_SERVER["HTTP_X_FORWARDED_FOR"] ?? $_SERVER["REMOTE_ADDR"]] = time();

                copy($_SERVER['DOCUMENT_ROOT'] . "/includes/lowertokens/" . str_replace("/", "", $token), $_SERVER['DOCUMENT_ROOT'] . "/includes/lowertokens/" . str_replace("/", "", $token) . ".old");

                file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/lowertokens/" . str_replace("/", "", $token), pf_utf8_encode(json_encode($data)));

                if (trim(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/lowertokens/" . str_replace("/", "", $token)) === "")) {
                    unlink($_SERVER['DOCUMENT_ROOT'] . "/includes/lowertokens/" . str_replace("/", "", $token));
                    copy($_SERVER['DOCUMENT_ROOT'] . "/includes/lowertokens/" . str_replace("/", "", $token) . ".old", $_SERVER['DOCUMENT_ROOT'] . "/includes/lowertokens/" . str_replace("/", "", $token));
                }

                unlink($_SERVER['DOCUMENT_ROOT'] . "/includes/lowertokens/" . str_replace("/", "", $token) . ".old");
            } else {
                $_PROFILE = $data;
            }

            $isLowerLoggedIn = true;
        }
    }
} else if ($_SERVER["REMOTE_ADDR"] === "127.0.0.1") {
    $isLowerLoggedIn = false;
    $isLoggedIn = true;
}

unset($data);