summaryrefslogtreecommitdiff
path: root/pages/sessions.inc
blob: 444e98152ce72a5011b3933aeffc7cc90774b4c8 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php

require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/init.inc"; global $title; global $isLoggedIn; global $isLowerLoggedIn; global $lang; global $pages;
require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/components/header.inc'; global $_PROFILE; global $app;

$verified = [
    dns_get_record("zephyrheights.equestria.dev", DNS_A)[0]["ip"],
    dns_get_record("maretimebay.equestria.dev", DNS_A)[0]["ip"],
    dns_get_record("bridlewood.equestria.dev", DNS_A)[0]["ip"],
    dns_get_record("manehattan.equestria.dev", DNS_A)[0]["ip"],
    dns_get_record("everfree.equestria.dev", DNS_A)[0]["ip"],
];

$verifiedNames = [
    "Raindrops System",
    "Raindrops System",
    "Equestria.dev bridlewood",
    "Equestria.dev manehattan",
    $app["other"]["name"]
];

?>

<br>
<div class="container">
    <h1>Sessions</h1>
    <script>window.devices = {};</script>

    <p>Here are all the currently open sessions for your account. Clicking on a session will delete it, meaning the device using this session will be logged out.</p>

    <?php $data = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/tokens/" . str_replace("/", "", $_COOKIE['PEH2_SESSION_TOKEN'])), true); if (isset($data["profile"])): ?>
    <div class="list-group">
        <?php

        $list = array_filter([...scandir($_SERVER['DOCUMENT_ROOT'] . "/includes/tokens"), ...scandir($_SERVER['DOCUMENT_ROOT'] . "/includes/lowertokens")], function ($token) {
            if ($token === "." || $token === "..") return false;

            $session = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/sessions/" . $token), true);

            return isset($session["last"]) && isset($session["profile"]);
        });
        usort($list, function ($token1, $token2) {
            $session1 = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/sessions/" . $token1), true);
            $session2 = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/sessions/" . $token2), true);

            if (isset($session1["last"]) && isset($session2["last"])) {
                return $session2["last"] - $session1["last"];
            } else {
                return INF;
            }
        });

        $addressFetchIndex = 0;

        foreach ($list as $token): $session = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/sessions/" . $token), true); if (isset($session["profile"]) && isset($session["name"]) && ($session["profile"]["id"] ?? "") === ($_PROFILE["id"] ?? "")): uasort($session["addresses"], function ($a, $b) {
            return $b - $a;
        }); ?>
        <a class="list-group-item list-group-item-action" onclick="logOut(&quot;<?= bin2hex(substr($token, 0, 50)) ?>&quot;);">
            <b><?= $session["name"] ?></b><?php if ($token === $_COOKIE["PEH2_SESSION_TOKEN"]): ?><span style="margin-left: 10px;" class="badge bg-primary">This device</span><?php endif; ?><script>window.devices["<?= bin2hex(substr($token, 0, 50)) ?>"]=JSON.parse(`<?= json_encode([
                    "name" => trim($session["name"]),
                    "lastIP" => array_keys($session["addresses"])[count(array_keys($session["addresses"])) > 0 ? count(array_keys($session["addresses"])) - 1 : 0] ?? "-",
                    "lastSeen" => timeAgo($session["last"]),
                    "currentDevice" => $token === $_COOKIE["PEH2_SESSION_TOKEN"]
            ]) ?>`);</script><br>
            Logged in <?= timeAgo($session["created"]) ?>, last activity <?= timeAgo($session["last"]) ?>
            <blockquote class="session-bq">
                <?php foreach ($session["addresses"] as $address => $last): ?>
                    <?= $address ?><?php if (in_array($address, $verified)): ?><span class="text-success" style="filter: invert(1) hue-rotate(180deg);"> (verified: <?= $verifiedNames[array_search($address, $verified)] ?? "-" ?>)</span><?php else: ?> <span class="text-muted" style="filter: invert(1) hue-rotate(180deg);" id="aft-<?= $addressFetchIndex ?>">(unverified: …)<script>
                            setTimeout(async () => {
                                let json = JSON.parse(await (await fetch("https://api.iplocation.net/?ip=<?= $address ?>")).text());

                                if (
                                    (json['country_code2'] !== "FR" && json['country_code2'] !== "GB") ||
                                    (json['isp'] === "Academie Orleans-Tours" && json['isp'] === "Free Mobile SAS" && json['isp'] === "Google One Services" && json['isp'] === "Vodafone Ltd" && json['isp'] === "Telefonica UK Limited")
                                ) {
                                    document.getElementById("aft-<?= $addressFetchIndex ?>").classList.remove("text-muted");
                                    document.getElementById("aft-<?= $addressFetchIndex ?>").classList.add("text-danger");
                                    document.getElementById("aft-<?= $addressFetchIndex ?>").innerText = `(unsafe: ${json['isp']}, ${json['country_code2'] === "GB" ? "UK" : json['country_code2']})`;
                                } else {
                                    document.getElementById("aft-<?= $addressFetchIndex ?>").innerText = `(unverified: ${json['isp']}, ${json['country_code2'] === "GB" ? "UK" : json['country_code2']})`;
                                }
                            }, 500 * <?= $addressFetchIndex ?>);
                        </script></span><?php $addressFetchIndex++; endif; ?> · <?= timeAgo($last) ?><br>
                <?php endforeach; ?>
            </blockquote>
        </a>
        <?php endif; endforeach; ?>
    </div>
    <?php else: ?>
    <div class="alert alert-danger">
        <b>Error:</b> You cannot use the session manager because your current session is using the old authentication system. Please log out and log in again to continue.
    </div>
    <?php endif; ?>
</div>

<div class="modal fade" id="confirm">
    <div class="modal-dialog">
        <div class="modal-content">

            <div class="modal-header">
                <h4 class="modal-title">Log out this device?</h4>
                <button type="button" class="btn-close" data-bs-toggle="modal"></button>
            </div>

            <div class="modal-body">
                <p>You are about to log out the following device from your Ponycule account:</p>
                <blockquote id="device-bq">
                    <div>
                        <b>Name:</b> <span id="device-name">-</span><br>
                        <b>Last address:</b> <span id="device-address">-</span><br>
                        <b>Last activity:</b> <span id="device-activity">-</span>
                    </div>
                </blockquote>
                <p class="text-danger" id="device-current" style="display: none;">This is the device you are currently using, which means you will get logged out as soon as you click on confirm.</p>
                <p id="device-normal"></p>
                <span class="btn btn-success" id="modal-button" style="margin-right: 5px;" onclick="confirm();">Confirm</span><span class="btn btn-outline-secondary" data-bs-toggle="modal">Cancel</span>
            </div>
        </div>
    </div>
</div>

<script>
    window.currentSession = null;
    window.currentDevice = null;
    window.modal = new bootstrap.Modal(document.getElementById("confirm"));

    async function confirm() {
        await fetch("/api/disconnect?id=" + window.currentSession);

        if (currentDevice.currentDevice) {
            location.href = "/-/logout";
        } else {
            location.reload();
        }
    }

    function logOut(id) {
        window.currentSession = id;
        window.currentDevice = devices[id];

        if (currentDevice.currentDevice) {
            document.getElementById("device-current").style.display = "";
            document.getElementById("device-normal").style.display = "none";
        } else {
            document.getElementById("device-current").style.display = "none";
            document.getElementById("device-normal").style.display = "";
        }

        document.getElementById("device-name").innerText = currentDevice.name;
        document.getElementById("device-address").innerText = currentDevice.lastIP;
        document.getElementById("device-activity").innerText = currentDevice.lastSeen;

        modal.show();
    }
</script>

<style>
    <?php global $use2023UI; if (!$use2023UI): ?>
    .modal-header {
        border-bottom: 1px solid #353738;
    }

    .modal-content {
        border: 1px solid rgba(255, 255, 255, .2);
        background-color: #111;
    }

    .btn-close {
        filter: invert(1);
    }

    .list-group-item {
        color: #fff;
        background-color: #222;
        border: 1px solid rgba(255, 255, 255, .125);
    }

    .list-group-item.disabled {
        color: #fff;
        background-color: #222;
        border-color: rgba(255, 255, 255, .125);
        opacity: .75;
    }

    .list-group-item:hover {
        background-color: #252525;
        color: #ddd;
    }

    .list-group-item:active, .list-group-item:focus {
        background-color: #272727;
        color: #bbb;
    }

    <?php else: ?>

    .list-group-item .text-muted {
        color: var(--palette-9) !important;
    }

    <?php endif; ?>

    .member-link, .list-group-item-action {
        cursor: pointer !important;
    }

    .alert-dismissible .btn-close {
        filter: none !important;
    }

    .session-bq {
        margin-bottom: 5px;
        margin-top: 10px;
        margin-left: 5px;
        padding-left: 10px;
        border-left: 3px solid var(--palette-5, rgba(255, 255, 255, .25));
    }

    #device-bq {
        margin-left: 5px;
        padding-left: 10px;
        border-left: 3px solid var(--palette-5, rgba(255, 255, 255, .25));
    }
</style>

<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/components/footer.inc'; ?>