summaryrefslogtreecommitdiff
path: root/includes/cleanup.inc
blob: 1d0208193d5eff5a0c82291a5cf985c4ffffcb9e (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
<?php

$screens = array_values(array_filter(scandir($_SERVER['DOCUMENT_ROOT'] . "/includes/data/computers/screens"), function ($i) {
    return !str_starts_with($i, ".");
}));

$windows = array_values(array_filter(scandir($_SERVER['DOCUMENT_ROOT'] . "/includes/data/computers/windows"), 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);
}

foreach ($windows as $window) {
    $delete = true;
    $id = explode(".", explode("-", $window)[2])[0];

    foreach ($computers as $file) {
        $computer = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/computers/metadata/" . $file), true);

        foreach ($computer["windows"] as $sel) {
            $id2 = sha1($sel["gid"]);

            if ($id2 === $id) $delete = false;
        }
    }

    if ($delete) unlink($_SERVER['DOCUMENT_ROOT'] . "/includes/data/computers/windows/" . $window);
}