summaryrefslogtreecommitdiff
path: root/pages/metadata.inc
blob: f3e48d7f622fd8ae8ff298fdd1d803c4c9a68748 (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
<?php

function getSubsystemByID(string $id) {
    global $subsystems;
    $subsystem = null;

    foreach ($subsystems as $ss) {
        if ($ss["source"] === $id) {
            $subsystem = $ss;
        }
    }

    return $subsystem;
}

require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/init.inc"; global $title; global $isLoggedIn; global $lang; global $pages;
require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/functions.inc";
require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/bitset.inc";

if (!isset($_GET['_']) || trim($_GET['_']) === "") header("Location: /?error=Invalid request") and die();

$parts = explode("/", $_GET['_']);
array_shift($parts);
array_shift($parts);
$system = $parts[0];
$member = !isset($parts[1]) || $parts[1] === "" ? null : $parts[1];

if ($system !== "cloudburst" && $system !== "raindrops") header("Location: /?error=Invalid system name: " . $system) and die();
$systemCommonName = $system === "cloudburst" ? "Cloudburst System" : "Raindrops System";
$systemID = $system === "cloudburst" ? "ynmuc" : "gdapd";

$subsystems = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/subsystems/$systemID.json"), true) ?? [];

if ($member === null) {
    require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/sysedit.inc';
} else {
    $isSubsystem = false;
    $members = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/" . $systemID . "/members.json"), true);
    $memberData = null;
    $memberCommonName = null;
    $memberID = null;

    foreach ($members as $m) {
        if ($m['name'] === $member) {
            $memberData = $m;
            $memberCommonName = $m['display_name'] ?? $m['name'];
            $memberID = $m['id'];
        }
    }

    if ($memberData === null) {
        header("Location: /?error=Not found") and die();
    }

    if (isset($_GET["submit"])) {
        if (file_exists($_SERVER['DOCUMENT_ROOT'] . "/includes/data/metadata/" . $memberID . ".json")) {
            $metadata = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/metadata/" . $memberID . ".json"), true);
        } else {
            die();
        }

        $toUpdate = [];

        header("Content-Type: text/plain");

        if (isset($_GET["bitset"]) && is_numeric($_GET["bitset"])) {
            $toUpdate["bitset"] = (int)$_GET["bitset"];
        }

        if (isset($_GET["interest"])) {
            $toUpdate["interest"] = strip_tags($_GET["interest"]);
        }

        if (isset($_GET["marefriends"])) {
            $toUpdate["marefriends"] = array_values(array_filter(array_map(function ($i) { return trim($i); }, explode(",", $_GET["marefriends"])), function ($i) {
                return !!preg_match("/^(ynmuc|gdapd)\/[a-z]{5}$/m", $i);
            }));
        }

        if (isset($_GET["sisters"])) {
            $toUpdate["sisters"] = array_values(array_filter(array_map(function ($i) { return trim($i); }, explode(",", $_GET["sisters"])), function ($i) {
                return !!preg_match("/^(ynmuc|gdapd)\/[a-z]{5}$/m", $i);
            }));
        }

        if (isset($_GET["caretakers"])) {
            $toUpdate["caretakers"] = array_values(array_filter(array_map(function ($i) { return trim($i); }, explode(",", $_GET["caretakers"])), function ($i) {
                return !!preg_match("/^(ynmuc|gdapd)\/[a-z]{5}$/m", $i);
            }));
        }

        if (isset($_GET['regressed'])) {
            if (!!preg_match("/^[a-z]{5}$/m", $_GET['regressed'])) {
                $toUpdate["regression"] = $_GET['regressed'];
            } else {
                $toUpdate["regression"] = null;
            }
        }

        $toUpdate["birth"] = [
            "date" => "01-01",
            "year" => null,
            "age" => 0
        ];

        if (isset($_GET["age"]) && is_numeric($_GET["age"])) {
            $toUpdate["birth"]["age"] = (int)$_GET["age"];
        }

        if (isset($_GET["birth"]) && trim($_GET['birth']) !== "" && !!preg_match("/^\d{4}-\d{2}-\d{2}$/m", $_GET['birth'])) {
            $toUpdate["birth"]["year"] = (int)substr($_GET["birth"],0, 4);
            $toUpdate["birth"]["date"] = substr($_GET["birth"],5);
        }

        if (isset($_GET["membc"])) {
            $toUpdate["code"] = $_GET['membc'];
        }

        foreach ($toUpdate as $item => $value) {
            $metadata[$item] = $value;
        }

        $metadata = [
            "bitset" => $metadata["bitset"] ?? 0,
            "regression" => $metadata["regression"] ?? null,
            "median" => $metadata["median"] ?? null,
            "marefriends" => $metadata["marefriends"] ?? [],
            "sisters" => $metadata["sisters"] ?? [],
            "caretakers" => $metadata["caretakers"] ?? [],
            "heat" => $metadata["heat"] ?? null,
            "birth" => $metadata["birth"] ?? ["age" => 0, "date" => "01-01", "year" => null],
            "code" => $metadata["code"] ?? null,
            "interest" => $metadata["interest"] ?? null
        ];

        file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/metadata/" . $memberID . ".json", json_encode($metadata));

        header("Location: /" . $_GET['_']);
    } else {
        require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/metadata.inc';
    }
}

exit;