summaryrefslogtreecommitdiff
path: root/includes/refresh.php
blob: cac1fb1c5c3e83c4a26b6a861f814f6da7e75c16 (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
<?php

$_SERVER['DOCUMENT_ROOT'] = "..";

$app = json_decode(file_get_contents("./app.json"), true);
@mkdir("./data");


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

$ignore = [
    $_SERVER['DOCUMENT_ROOT'] . "/includes/data",
    $_SERVER['DOCUMENT_ROOT'] . "/includes/tokens",
    $_SERVER['DOCUMENT_ROOT'] . "/app",
    $_SERVER['DOCUMENT_ROOT'] . "/assets",
];

$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 "./backup.inc";
    file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/backup.txt", time());
    echo("Backup completed\n");
} else {
    echo("Backup skipped\n");
}

echo("Completed.\n");