summaryrefslogtreecommitdiff
path: root/includes/refresh.php
diff options
context:
space:
mode:
authorMinteck <contact@minteck.org>2022-10-10 20:51:39 +0200
committerMinteck <contact@minteck.org>2022-10-10 20:51:39 +0200
commit108525534c28013cfe1897c30e4565f9893f3766 (patch)
treedd3e5132971f96ab5f05e7f3f8f6dbbf379a19bd /includes/refresh.php
parent2162eaa06f7e4764eb3dcfe130ec2c711d0c62ab (diff)
downloadpluralconnect-108525534c28013cfe1897c30e4565f9893f3766.tar.gz
pluralconnect-108525534c28013cfe1897c30e4565f9893f3766.tar.bz2
pluralconnect-108525534c28013cfe1897c30e4565f9893f3766.zip
Update
Diffstat (limited to 'includes/refresh.php')
-rw-r--r--includes/refresh.php144
1 files changed, 128 insertions, 16 deletions
diff --git a/includes/refresh.php b/includes/refresh.php
index 8f9da79..e8e12dd 100644
--- a/includes/refresh.php
+++ b/includes/refresh.php
@@ -1,5 +1,7 @@
<?php
+$_SERVER['DOCUMENT_ROOT'] = "..";
+
$app = json_decode(file_get_contents("./app.json"), true);
$start = microtime(true);
@mkdir("./data");
@@ -10,11 +12,11 @@ $restored = [];
$times = [];
$currentOpStart = microtime(true);
-file_put_contents("./data/backup/actions_" . date('Y-m-d_H-i-s') . ".json", @file_get_contents("./data/actions.json"));
-file_put_contents("./data/backup/toys_" . date('Y-m-d_H-i-s') . ".json", @file_get_contents("./data/toys.json"));
+file_put_contents("./data/actions/" . date('Y-m-d_H-i-s') . ".json", @file_get_contents("./data/actions/actions.json"));
+file_put_contents("./data/toys/" . date('Y-m-d_H-i-s') . ".json", @file_get_contents("./data/toys/toys.json"));
-exec('bash -c "cd /nas/web/ponies.equestria.horse/includes/data/backup; ls -tp toys_* | grep -v \'/$\' | tail -n +20 | xargs -I {} rm -- {}"');
-exec('bash -c "cd /nas/web/ponies.equestria.horse/includes/data/backup; ls -tp actions_* | grep -v \'/$\' | tail -n +20 | xargs -I {} rm -- {}"');
+exec('bash -c "cd /nas/web/ponies.equestria.horse/includes/data; ls -tp toys/20* | grep -v \'/$\' | tail -n +20 | xargs -I {} rm -- {}"');
+exec('bash -c "cd /nas/web/ponies.equestria.horse/includes/data; ls -tp actions/20* | grep -v \'/$\' | tail -n +20 | xargs -I {} rm -- {}"');
$times["important_backup"] = microtime(true) - $currentOpStart;
@@ -40,10 +42,83 @@ foreach ($deletable as $item) {
$times["docs_cleanup"] = microtime(true) - $currentOpStart;
$currentOpStart = microtime(true);
+$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",
+];
+
+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));
+$times["version"] = microtime(true) - $currentOpStart;
+
+$currentOpStart = microtime(true);
foreach (array_filter(scandir("./data"), function ($i) {
- return !str_starts_with($i, ".") && $i !== "backup" && is_file("./data/" . $i);
+ return !str_starts_with($i, ".") && $i !== "backup" && (is_file("./data/" . $i) || is_dir("./data/" . $i));
}) as $file) {
- $files[] = $file;
+ if (is_dir("./data/" . $file)) {
+ foreach (array_filter(scandir("./data"), function ($i) use ($file) {
+ return !str_starts_with($i, ".") && $i !== "backup" && is_file("./data/" . $file . "/" . $i);
+ }) as $file2) {
+ @mkdir("./data/backup/" . $file);
+ $files[] = $file . "/" . $file2;
+ }
+ } else {
+ $files[] = $file;
+ }
}
@mkdir("./data/backup");
@@ -57,17 +132,28 @@ function getSystem(string $id) {
echo(" Base system info\n");
$currentOpStart = microtime(true);
- file_put_contents("./data/$id-general.json", file_get_contents("https://pluralkit.equestria.dev/v2/systems/$id"));
+ $data = file_get_contents("https://pluralkit.equestria.dev/v2/systems/$id");
+
+ if (trim($data) !== "" && $data !== false) {
+ file_put_contents("./data/$id/general.json", $data);
+ }
$times["system-general-$id"] = microtime(true) - $currentOpStart;
echo(" System members\n");
$currentOpStart = microtime(true);
- file_put_contents("./data/$id-members.json", file_get_contents("https://pluralkit.equestria.dev/v2/systems/$id/members"));
+ $data = file_get_contents("https://pluralkit.equestria.dev/v2/systems/$id/members");
+
+ if (trim($data) !== "" && $data !== false) {
+ file_put_contents("./data/$id/members.json", $data);
+ }
$times["system-members-$id"] = microtime(true) - $currentOpStart;
echo(" Fronters\n");
- $currentOpStart = microtime(true);
- file_put_contents("./data/$id-fronters.json", file_get_contents("https://pluralkit.equestria.dev/v2/systems/$id/fronters"));
+ $data = file_get_contents("https://pluralkit.equestria.dev/v2/systems/$id/fronters");
+
+ if (trim($data) !== "" && $data !== false) {
+ file_put_contents("./data/$id/fronters.json", $data);
+ }
$times["system-fronters-$id"] = microtime(true) - $currentOpStart;
echo(" Switches\n");
@@ -84,8 +170,10 @@ function getSystem(string $id) {
echo(" Part 3/3\n");
$switches3 = json_decode(file_get_contents("https://pluralkit.equestria.dev/v2/systems/$id/switches?before=$oldest"), true);
- file_put_contents("./data/$id-switches.json", json_encode([...$switches1, ...$switches2, ...$switches3], JSON_PRETTY_PRINT));
- $times["system-switches-$id"] = microtime(true) - $currentOpStart;
+ if ($switches1 !== null && $switches2 !== null && $switches3 !== null) {
+ file_put_contents("./data/$id/switches.json", json_encode([...$switches1, ...$switches2, ...$switches3], JSON_PRETTY_PRINT));
+ $times["system-switches-$id"] = microtime(true) - $currentOpStart;
+ }
}
getSystem("gdapd"); // Raindrops
@@ -93,13 +181,13 @@ getSystem("ynmuc"); // Cloudburst
echo("Calendar\n");
$currentOpStart = microtime(true);
-file_put_contents("./data/calendar.ics", file_get_contents($app["calendar"]));
+file_put_contents("./data/calendar/calendar.ics", file_get_contents($app["calendar"]));
$times["calendar"] = microtime(true) - $currentOpStart;
echo("Downloading images.\n");
if (!file_exists("./data/images")) mkdir("./data/images");
-foreach (json_decode(file_get_contents("./data/gdapd-members.json"), true) as $member) {
+foreach (json_decode(file_get_contents("./data/gdapd/members.json"), true) as $member) {
$currentOpStart2 = microtime(true);
echo(" " . $member['id'] . "\n");
@@ -117,7 +205,7 @@ foreach (json_decode(file_get_contents("./data/gdapd-members.json"), true) as $m
$times["images-gdapd-" . $member['id']] = microtime(true) - $currentOpStart2;
}
-foreach (json_decode(file_get_contents("./data/ynmuc-members.json"), true) as $member) {
+foreach (json_decode(file_get_contents("./data/ynmuc/members.json"), true) as $member) {
$currentOpStart2 = microtime(true);
echo(" " . $member['id'] . "\n");
@@ -152,12 +240,36 @@ $times["restore"] = microtime(true) - $currentOpStart;
echo("Backing up...\n");
if (date('i') === "00") {
- require_once "./backup.php";
+ require_once "./backup.inc";
echo("Backup completed\n");
} else {
echo("Backup skipped\n");
}
+echo("Generating logo...\n");
+
+$currentOpStart = microtime(true);
+require_once "./logo.inc";
+$times["logo"] = microtime(true) - $currentOpStart;
+
+echo("Generated logo\n");
+
+echo("Generating favicons...\n");
+
+$currentOpStart = microtime(true);
+require_once "./icons.inc";
+$times["favicons"] = microtime(true) - $currentOpStart;
+
+echo("Generated favicons\n");
+
+echo("Downloading assets...\n");
+
+$currentOpStart = microtime(true);
+require_once "./assets.inc";
+$times["assets"] = microtime(true) - $currentOpStart;
+
+echo("Downloading assets\n");
+
$time = array_sum($times);
echo("Completed in " . $time . " seconds.\n");