summaryrefslogtreecommitdiff
path: root/pages/api/pluralkit-integration.php
blob: 0793873dab8ea64e1fb2d6aa8763ac99d82fbca8 (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
<?php

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

$app = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/app.json"), true);
$user = $_GET['user'] ?? null;
$inputJSON = file_get_contents('php://input');
$input = json_decode($inputJSON, true);
$data = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/app.json"), true)["webhook"];

if (isset(json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/app.json"), true)["ponytown"][$user])) {
    $ponytown = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/app.json"), true)["ponytown"][$user];
} else {
    header("HTTP/1.1 404 Not Found") and die();
}

if ($user === null) {
    header("HTTP/1.1 500 Internal Server Error") and die();
}

if (!in_array($user, array_keys($data))) {
    header("HTTP/1.1 404 Not Found") and die();
}

if ($input["signing_token"] !== $data[$user]) {
    header("HTTP/1.1 401 Unauthorized") and die();
}

if ($input['system_id'] === "7d9f543e-f742-40f6-9d07-86c3f2983124") {
    $system = "gdapd";
    $name = "Raindrops System";
} elseif ($input['system_id'] === "ade46823-206b-4b0c-ad3c-caae934a5f3b") {
    $system = "ynmuc";
    $name = "Cloudburst System";
} elseif (isset($app["other"]) && $input["system_id"] === $app["other"]["uuid"]) {
    $system = $app["other"]["id"];
    $name = $app["other"]["name"];
} else {
    die();
}

$lastFronter = json_decode(@file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/$system/last.json"), true) ?? "";

if ($input["type"] === "CREATE_SWITCH" || $input["type"] === "UPDATE_SWITCH" || $input["type"] === "DELETE_SWITCH") {
    if ($system === $app["other"]["id"]) {
        file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/$system/fronters.json", file_get_contents("https://pluralkit.equestria.dev/v2/systems/$input[system_id]/fronters"), false, stream_context_create([
            'http' => [
                'method' => 'GET',
                'header' =>
                    "Authorization: " . $app["other"]["token"] . "\r\n"
            ]
        ]));
        file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/$system/switches.json", file_get_contents("https://pluralkit.equestria.dev/v2/systems/$input[system_id]/switches"), false, stream_context_create([
            'http' => [
                'method' => 'GET',
                'header' =>
                    "Authorization: " . $app["other"]["token"] . "\r\n"
            ]
        ]));
    } else {
        file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/$system/fronters.json", file_get_contents("https://pluralkit.equestria.dev/v2/systems/$input[system_id]/fronters"));
        file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/$system/switches.json", file_get_contents("https://pluralkit.equestria.dev/v2/systems/$input[system_id]/switches"));
    }

    $fronters = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/$system/fronters.json"), true);
    $currentFronter = $fronters["members"][0]["id"];
    file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/$system/last.json", json_encode($currentFronter));

    $ntfy = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/app.json"), true)["ntfy"];

    if (count($fronters["members"]) > 0 && ($system !== $app["other"]["id"] || !isset($app["other"]))) {
        foreach ($fronters["members"] as $member) {
            if (file_exists($_SERVER['DOCUMENT_ROOT'] . "/includes/data/metadata/" . $member["id"] . ".json")) {
                $metadata = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/metadata/" . $member["id"] . ".json"), true);

                if (!isset($metadata["birth"])) {
                    showWarning($member["display_name"] ?? $member["name"], $member["name"], $system);
                } else if (isset($metadata['birth']["age"]) && $metadata["birth"]["age"] === 0 && (!isset($metadata['birth']["year"]) || $metadata["birth"]["year"] < 1900)) {
                    showWarning($member["display_name"] ?? $member["name"], $member["name"], $system);
                } else if (isset($metadata['birth']["year"]) && $metadata["birth"]["year"] < 1900) {
                    showWarning($member["display_name"] ?? $member["name"], $member["name"], $system);
                } else if (!isset($metadata['birth']["year"]) && !isset($metadata['birth']["age"])) {
                    showWarning($member["display_name"] ?? $member["name"], $member["name"], $system);
                }
            }
        }
    }

    if ($system !== $app["other"]["id"] || !isset($app["other"])) {
        if (count($fronters["members"]) > 1) {
            $context = stream_context_create([
                'http' => [
                    'method' => 'POST',
                    'header' =>
                        "Content-Type: text/plain\r\n" .
                        "Title: 🐴 Switch occurred in the $name\r\n" .
                        "Priority: default\r\n" .
                        "Tags: switch\r\n" .
                        "Actions: view, Open " . $fronters["members"][0]["display_name"] . " on Cold Haze, https://ponies.equestria.horse/" . $fronters["members"][0]["name"] . "/, clear=true;view, Open " . $fronters["members"][1]["display_name"] . " on Cold Haze, https://ponies.equestria.horse/" . $fronters["members"][1]["name"] . "/, clear=true\r\n" .
                        "Authorization: Basic " . base64_encode($ntfy["user"] . ":" . $ntfy["password"]),
                    'content' => ($fronters["members"][0]["display_name"] ?? $fronters["members"][0]["name"]) . " and " . ($fronters["members"][1]["display_name"] ?? $fronters["members"][1]["name"]) . " switched in just now"
                ]
            ]);
        } else if (count($fronters["members"]) > 0) {
            $context = stream_context_create([
                'http' => [
                    'method' => 'POST',
                    'header' =>
                        "Content-Type: text/plain\r\n" .
                        "Title: 🐴 Switch occurred in the $name\r\n" .
                        "Priority: default\r\n" .
                        "Tags: switch\r\n" .
                        "Actions: view, Open on Cold Haze, https://ponies.equestria.horse/" . $fronters["members"][0]["name"] . "/, clear=true\r\n" .
                        "Authorization: Basic " . base64_encode($ntfy["user"] . ":" . $ntfy["password"]),
                    'content' => ($fronters["members"][0]["display_name"] ?? $fronters["members"][0]["name"]) . " switched in just now"
                ]
            ]);
        } else {
            $context = stream_context_create([
                'http' => [
                    'method' => 'POST',
                    'header' =>
                        "Content-Type: text/plain\r\n" .
                        "Title: 🐴 Switch occurred in the $name\r\n" .
                        "Priority: default\r\n" .
                        "Tags: switch\r\n" .
                        "Actions: view, Open on Cold Haze, https://ponies.equestria.horse/, clear=true\r\n" .
                        "Authorization: Basic " . base64_encode($ntfy["user"] . ":" . $ntfy["password"]),
                    'content' => "The fallback pony switched in just now"
                ]
            ]);
        }

        file_get_contents('https://' . $ntfy["server"] . '/public-switches', false, $context);
    }

    if ($system === "gdapd") {
        $topic = "public-switches-raindrops";

        if (count($fronters["members"]) > 0) {
            file_get_contents('https://' . $ntfy["server"] . '/public-switches-fr', false, stream_context_create([
                'http' => [
                    'method' => 'POST',
                    'header' =>
                        "Content-Type: text/plain\r\n" .
                        "Title: 🐴 $name vient de switch\r\n" .
                        "Priority: default\r\n" .
                        "Tags: switch\r\n" .
                        "Actions: view, Voir sur Cold Haze, https://fr.equestria.horse/" . $fronters["members"][0]["name"] . "/, clear=true\r\n" .
                        "Authorization: Basic " . base64_encode($ntfy["user"] . ":" . $ntfy["password"]),
                    'content' => ($fronters["members"][0]["display_name"] ?? $fronters["members"][0]["name"]) . " est désormais au front"
                ]
            ]));
        } else {
            file_get_contents('https://' . $ntfy["server"] . '/public-switches-fr', false, stream_context_create([
                'http' => [
                    'method' => 'POST',
                    'header' =>
                        "Content-Type: text/plain\r\n" .
                        "Title: 🐴 $name vient de switch\r\n" .
                        "Priority: default\r\n" .
                        "Tags: switch\r\n" .
                        "Actions: view, Voir sur Cold Haze, https://fr.equestria.horse/, clear=true\r\n" .
                        "Authorization: Basic " . base64_encode($ntfy["user"] . ":" . $ntfy["password"]),
                    'content' => "Le poney de trait est désormais au front"
                ]
            ]));
        }
    } else if ($system === "ynmuc") {
        $topic = "public-switches-cloudburst";
    } else {
        $topic = "main";
    }

    file_get_contents('https://' . $ntfy["server"] . '/' . $topic, false, $context);
}