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
|
<?php
require_once './composer/vendor/autoload.php';
$_SERVER['DOCUMENT_ROOT'] = "..";
$app = json_decode(file_get_contents("./app.json"), true);
@mkdir("./data");
file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/calendar.ics", file_get_contents($app["calendar"]));
$version = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/version.json"), true);
$ignore = [
$_SERVER['DOCUMENT_ROOT'] . "/Data",
$_SERVER['DOCUMENT_ROOT'] . "/bits/Data",
$_SERVER['DOCUMENT_ROOT'] . "/Private/Data",
$_SERVER['DOCUMENT_ROOT'] . "/bits/Data",
$_SERVER['DOCUMENT_ROOT'] . "/bits/Private/Data",
$_SERVER['DOCUMENT_ROOT'] . "/includes/data",
$_SERVER['DOCUMENT_ROOT'] . "/includes/tokens",
$_SERVER['DOCUMENT_ROOT'] . "/app",
$_SERVER['DOCUMENT_ROOT'] . "/assets",
];
$screens = array_values(array_filter(scandir($_SERVER['DOCUMENT_ROOT'] . "/includes/data/computers/screens"), function ($i) {
return !str_starts_with($i, ".");
}));
$computers = array_values(array_filter(scandir($_SERVER['DOCUMENT_ROOT'] . "/includes/data/computers/metadata"), function ($i) {
return !str_starts_with($i, ".");
}));
foreach ($screens as $screen) {
$delete = true;
$id = explode(".", explode("-", $screen)[2])[0];
foreach ($computers as $file) {
$computer = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/computers/metadata/" . $file), true);
foreach ($computer["screens"] as $sel) {
if ($sel["id"] === $id) $delete = false;
}
}
if ($delete) unlink($_SERVER['DOCUMENT_ROOT'] . "/includes/data/computers/screens/" . $screen);
}
$documents = array_map(function ($i) {
return [
"id" => substr($i, 0, -5),
...(json_decode(file_get_contents("./data/docs/" . $i), true) ?? [])
];
}, array_filter(scandir("./data/docs"), function ($i) {
return !str_starts_with($i, ".") && str_ends_with($i, ".json");
}));
$deletable = array_values(array_filter($documents, function ($i) {
return str_starts_with(strip_tags($i["contents"]), "/delete");
}));
foreach ($deletable as $item) {
if (time() - $item["last"]["date"] > 86400) {
unlink("./data/docs/" . $item["id"] . ".json");
}
}
function rscandir($dir) {
global $ignore;
$files = [];
foreach (array_filter(scandir($dir), function ($i) {
return !str_starts_with($i, ".");
}) as $file) {
if (in_array($dir . "/" . $file, $ignore)) continue;
if (is_dir($dir . "/" . $file)) {
array_push($files, ...rscandir($dir . "/" . $file));
} else {
$files[] = $dir . "/" . $file;
}
}
return array_unique($files);
}
function calculateVersionNumber() {
$files = rscandir($_SERVER['DOCUMENT_ROOT']);
$hashes = [];
foreach ($files as $file) {
$hashes[] = md5_file($file);
}
$hash = md5(implode("", $hashes));
return [$hash, $hashes];
}
$versionHash = calculateVersionNumber();
if ($versionHash[0] !== $version["hash"]) {
$version["hash"] = $versionHash[0];
$version["timestamp"] = time();
$version["revision"] = 0;
$version["list"] = [
"current" => $versionHash[1],
"old" => isset($version["list"]) ? $version["list"]["current"] ?? null : null
];
$version["build"]++;
} else {
$version["revision"]++;
}
file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/version.json", json_encode($version));
if (!is_link($_SERVER['DOCUMENT_ROOT'] . "/includes/data/" . $app["other"]["id"])) {
link($_SERVER['DOCUMENT_ROOT'] . "/includes/data/other", $_SERVER['DOCUMENT_ROOT'] . "/includes/data/" . $app["other"]["id"]);
}
echo("Backing up...\n");
$lastBackup = (int)trim(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/backup.txt"));
if (time() - $lastBackup >= 3600) {
require_once "./refresh/backup.inc";
file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/backup.txt", time());
echo("Backup completed\n");
} else {
echo("Backup skipped\n");
}
echo("Fetching school information...\n");
file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/school.json", "{}");
chdir("./external/school");
exec("node index.js");
chdir("../..");
echo("Fetched school information\n");
echo("Completed.\n");
|