summaryrefslogtreecommitdiff
path: root/includes/maintenance/deleteUnusedAssets.php
blob: bd7cd3827e29c82ff6fd56e58ab1ce6802d6e06b (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
<?php

global $action;
if (!isset($_SERVER['DOCUMENT_ROOT'])) $_SERVER['DOCUMENT_ROOT'] = "../..";
if (!isset($action)) $action = "list";

if (isset($_SERVER["argv"][1])) {
    if ($_SERVER['argv'][1] === "archive") {
        $action = "archive";
    } elseif ($_SERVER['argv'][1] === "delete") {
        $action = "delete";
    }
}

$list = [
    str_replace("-", "", json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/gdapd/general.json"), true)["uuid"]),
    str_replace("-", "", json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/hrbom/general.json"), true)["uuid"]),
    str_replace("-", "", json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/other/general.json"), true)["uuid"]),
    ...array_map(function ($i) {
        return str_replace("-", "", json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/gdapd/general.json"), true)["uuid"]) . str_replace("-", "", $i["uuid"]);
    }, json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/gdapd/members.json"), true)),
    ...array_map(function ($i) {
        return str_replace("-", "", json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/hrbom/general.json"), true)["uuid"]) . str_replace("-", "", $i["uuid"]);
    }, json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/hrbom/members.json"), true)),
    ...array_map(function ($i) {
        return str_replace("-", "", json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/other/general.json"), true)["uuid"]) . str_replace("-", "", $i["uuid"]);
    }, json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/other/members.json"), true))
];

$count = 0;
$left = 0;

$list = [
    ...array_map(function ($i) {
        return $i . ".webp";
    }, $list),
    ...array_map(function ($i) {
        return $i . ".png";
    }, $list)
];

if ($action === "archive") {
    if (!file_exists($_SERVER['DOCUMENT_ROOT'] . "/assets/archive")) mkdir($_SERVER["DOCUMENT_ROOT"] . "/assets/archive");
    if (!file_exists($_SERVER['DOCUMENT_ROOT'] . "/assets/archive/heads")) mkdir($_SERVER["DOCUMENT_ROOT"] . "/assets/archive/heads");
    if (!file_exists($_SERVER['DOCUMENT_ROOT'] . "/assets/archive/bodies")) mkdir($_SERVER["DOCUMENT_ROOT"] . "/assets/archive/bodies");
    if (!file_exists($_SERVER['DOCUMENT_ROOT'] . "/assets/archive/banners")) mkdir($_SERVER["DOCUMENT_ROOT"] . "/assets/archive/banners");
    if (!file_exists($_SERVER['DOCUMENT_ROOT'] . "/assets/archive/avatars")) mkdir($_SERVER["DOCUMENT_ROOT"] . "/assets/archive/avatars");
}

foreach (["heads", "bodies", "banners", "avatars"] as $t) {
    foreach (array_filter(scandir($_SERVER['DOCUMENT_ROOT'] . "/assets/$t"), function ($i) {
        return !str_starts_with($i, ".");
    }) as $file) {
        if (!in_array($file, $list)) {
            echo($file . "\n");
            $count++;

            if ($action === "archive") {
                rename($_SERVER['DOCUMENT_ROOT'] . "/assets/$t/" . $file, $_SERVER['DOCUMENT_ROOT'] . "/assets/archive/$t/" . $file);
            } elseif ($action === "delete") {
                unlink($_SERVER['DOCUMENT_ROOT'] . "/assets/$t/" . $file);
            }
        } else {
            $left++;
        }
    }
}

echo($count . " file(s), leaving " . $left . " file(s), action: " . $action . "\n");