summaryrefslogtreecommitdiff
path: root/includes/system/history.inc
blob: b879f6e2ba8e82af735f5e779e453f004ea09b9b (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
<?php global $system; global $lang; global $pages; global $systemCommonName; $pages = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/pages.json"), true); $title = $pages["s:history"]["name"][$lang["_name"]] . " · " . $systemCommonName; global $systemID; global $app; require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/components/header.inc'; global $isLoggedIn; global $isLowerLoggedIn;

function frenchIfRequired($original) {
    return $original;
}

function getSwitchesForDay(int $day) {
    global $switches;

    $filtered = array_values(array_filter($switches, function ($i) use ($day) {
        $diff = strtotime(date("Y-m-d")) - strtotime(explode("T", $i["timestamp"])[0]);
        return $diff <= (86400 * $day) && $diff > (86400 * ($day - 1));
    }));

    uksort($filtered, function ($a, $b) {
        if (isset($b["timestamp"]) && isset($a["timestamp"])) {
            return strtotime($b["timestamp"]) - strtotime($a["timestamp"]);
        } else {
            return null;
        }
    });

    return $filtered;
}

function getSwitchBefore(string $id) {
    global $switches;

    $currentPassed = false;
    $before = null;

    foreach ($switches as $switch) {
        if ($currentPassed) {
            $before = $switch;
            break;
        } else {
            if ($switch["id"] === $id) {
                $currentPassed = true;
            }
        }
    }

    return $before;
}

function isNotToday(int $timestamp, int $offset) {
    if (date('Y-m-d', $timestamp) !== date('Y-m-d', time() - (86400 * $offset))) {
        return true;
    } else {
        return false;
    }
}

if (!file_exists($_SERVER['DOCUMENT_ROOT'] . "/includes/data/cache/history.json")) file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/cache/history.json", "{}");

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

function page() { global $lang; global $systemCommonName; global $systemID; global $app; global $switches; global $isLoggedIn; global $isLowerLoggedIn; ?>
    <br>
    <div class="container" id="page-content">
        <?php

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

        uksort($switches, function ($a, $b) {
            if (isset($b["timestamp"]) && isset($a["timestamp"])) {
                return strtotime($b["timestamp"]) - strtotime($a["timestamp"]);
            } else {
                return null;
            }
        });

        ?>
        <h2><?= $systemID === $app["other"]["id"] ? "Front history in " . $app["other"]["name"] : $lang["history"]["title"] . " " . $systemCommonName ?></h2>
        <div class="alert alert-warning" id="timezone" style="display: none;">
            <?= str_replace("%1", '<span id="timezone-name">-</span>', str_replace("%2", '<span id="day-start">-</span>', $lang["history"]["timezone"])) ?>
            <script>
                document.getElementById("day-start").innerText = new Date(<?= strtotime(date('Y-m-d')) ?> * 1000).toTimeString().substring(0, 5);
                document.getElementById("timezone-name").innerText = new Date().toTimeString().split("(")[1].split(")")[0];
            </script>
        </div>
        <?php foreach ([
                   [ "Today", 0 ],
                   [ "Yesterday", 1 ],
                   [ date('D j M', time() - (86400 * 2)), 2 ],
                   [ date('D j M', time() - (86400 * 3)), 3 ],
                   [ date('D j M', time() - (86400 * 4)), 4 ],
                   [ date('D j M', time() - (86400 * 5)), 5 ],
                   [ date('D j M', time() - (86400 * 6)), 6 ],
                   [ date('D j M', time() - (86400 * 7)), 7 ],
                   [ date('D j M', time() - (86400 * 8)), 8 ],
                   [ date('D j M', time() - (86400 * 9)), 9 ],
               ] as $day):
            $switchesDay = getSwitchesForDay($day[1]);
            if (isset($switchesDay[count($switchesDay) - 1])) {
                $switchesDay[] = getSwitchBefore($switchesDay[count($switchesDay) - 1]["id"]);
            }
            ?>
            <h4 style="margin-top:15px;"><?= frenchIfRequired($day[0]) ?></h4>
            <?php if (count($switchesDay) === 0): ?>
            <p class="text-muted">No switches happened on that day</p>
        <?php endif; ?>
            <?php foreach ($switchesDay as $switch): $switch["timestamp"] = strtotime($switch["timestamp"]); ?>
            <?php if (isset($switch["members"][0])): $member = getMemberWithoutSystem($switch["members"][0]); ?>
                <div class="fronter">
                    <span class="fronter-date" data-date-time="<?= isNotToday($switch["timestamp"], $day[1]) ? strtotime(date('Y-m-d', $switch["timestamp"])) : $switch["timestamp"] ?>" style="opacity:.5;font-family: monospace;font-size:14px;vertical-align: middle;">
                        <?= isNotToday($switch["timestamp"], $day[1]) ? "00:00" : date('H:i', $switch["timestamp"]) ?>
                    </span>
                    <span class="fronter-profile" style="vertical-align: middle;">
                        <a class="member-link" href="/<?= (isset($member) && $member["name"] === "unknown") ? ($systemID === "gdapd" ? "unknown-rd" : ($systemID === "ynmuc" ? "unknown-cb" : $app["other"]["unknown"])) : $member["name"] ?>">
                            <img src="<?= getAsset($systemID, $member["id"], "heads") ?>" style="width:24px;"> <?= $member["display_name"] ?? $member["name"] ?>
                        </a>
                        <?php if (count($switch["members"]) > 1): ?>
                            and
                            <?php if (isset($switch["members"][1])): $member2 = getMember($switch["members"][1]); ?><a class="member-link" href="/<?= $member2["name"] ?>"><img src="<?= getAsset($systemID, $member2["id"], "heads") ?>" style="width:24px;"> <?= $member2["display_name"] ?? $member2["name"] ?></a><?php endif; ?><?php if (isset($switch["members"][2])): $member2 = getMember($switch["members"][2]); ?>, <a class="member-link" href="/<?= $member2["name"] ?>"><img src="<?= getAsset($systemID, $member2["id"], "heads") ?>" style="width:24px;"> <?= $member2["display_name"] ?? $member2["name"] ?></a><?php endif; ?><?php if (isset($switch["members"][3])): $member2 = getMember($switch["members"][3]); ?>, <a class="member-link" href="/<?= $member2["name"] ?>"><img src="<?= getAsset($systemID, $member2["id"], "heads") ?>" style="width:24px;"> <?= $member2["display_name"] ?? $member2["name"] ?></a><?php endif; ?>
                        <?php endif; ?>
                    </span>
                </div>
            <?php else: ?>
                <div class="fronter">
                    <span class="fronter-date" data-date-time="<?= isNotToday($switch["timestamp"], $day[1]) ? strtotime(date('Y-m-d', $switch["timestamp"])) : $switch["timestamp"] ?>" style="opacity:.5;font-family: monospace;font-size:14px;vertical-align: middle;">
                        <?= isNotToday($switch["timestamp"], $day[1]) ? "00:00" : date('H:i', $switch["timestamp"]) ?>
                    </span>
                    <span class="fronter-profile" style="vertical-align: middle;">
                        <span class="text-muted"><?= $lang["history"]["fallback"] ?></span>
                    </span>
                </div>
            <?php endif; ?>
        <?php endforeach; ?>
        <?php endforeach; ?>
    </div>
    <script>
        Array.from(document.getElementsByClassName("fronter-date")).forEach((i) => { i.innerText = new Date(parseInt(i.getAttribute("data-date-time").trim()) * 1000).toTimeString().split(" ")[0].substring(0, 5) + " " });
        document.getElementById("timezone").style.display = "";
    </script>
<?php }

?>

<?php

if (!isset($cache[$systemID])) $cache[$systemID] = [];

if (!isset($cache[$systemID]["private"]) || !isset($cache[$systemID]["public"])) {
    ob_start();
    $isLoggedInOldState = $isLoggedIn;
    $isLowerLoggedInOldState = $isLowerLoggedIn;
    $isLoggedIn = true;
    $isLowerLoggedIn = true;
    page();
    $isLoggedIn = $isLoggedInOldState;
    $isLowerLoggedIn = $isLowerLoggedInOldState;
    $cache[$systemID]["private"] = ob_get_contents();
    ob_end_clean();

    if ($systemID !== $app["other"]["id"]) {
        ob_start();
        $isLoggedInOldState = $isLoggedIn;
        $isLowerLoggedInOldState = $isLowerLoggedIn;
        $isLoggedIn = false;
        $isLowerLoggedIn = false;
        page();
        $isLoggedIn = $isLoggedInOldState;
        $isLowerLoggedIn = $isLowerLoggedInOldState;
        $cache[$systemID]["public"] = ob_get_contents();
        ob_end_clean();
    } else {
        $cache[$systemID]["public"] = "";
    }
}

echo($isLoggedIn ? $cache[$systemID]["private"] : $cache[$systemID]["public"]); file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/cache/history.json", json_encode($cache)); ?>

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