<?php

if (time() >= 1688169600) {
    peh_error("Page not found: computers", 404);
}

require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/init.inc"; global $title; global $isLoggedIn; global $lang; global $pages; global $pagename; global $_USER; global $_PROFILE;
$computer = [];
$parts = explode("/", $pagename);

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

if (isset($parts[1])) {
    if (file_exists($_SERVER['DOCUMENT_ROOT'] . "/includes/data/computers/metadata/" . $parts[1] . ".json")) {
        $computer = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/computers/metadata/" . $parts[1] . ".json"), true);
        $id = $parts[1];
        $owner = explode("-", $id)[0];

        if (count($parts) > 2) {
            $title = "Remote control · " . ($names[$id] ?? $computer["host"]) . " · " . $title;
        } else {
            $title = ($names[$id] ?? $computer["host"]) . " · " . $title;
        }
    } else {
        header("Location: /-/computers");
        die();
    }
}

if (isset($parts[2]) && $parts[2] !== "control") {
    header("Location: /-/computers/" . $parts[1]);
    die();
}

$parts = array_values(array_filter($parts, function ($i) {
    return $i !== "-";
}));

if (count($parts) === 2 || count($parts) === 3) {
    array_unshift($parts, null);
}

if (isset($computer["os"]) && str_ends_with($computer["os"], "unknown")) {
    $computer["os"] = trim(substr($computer["os"], 0, -7));
}

if (isset($computer["os"]) && str_ends_with($computer["os"], "rolling")) {
    $computer["os"] = trim(substr($computer["os"], 0, -7));
}

require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/components/header.inc';

?>

<script>
    window.userName = "<?php

    $fronters = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/" . ($_PROFILE["login"] === "raindrops" ? "gdapd" : "ynmuc") . "/fronters.json"), true);
    $name = $_PROFILE["login"] === "raindrops" ? "Raindrops System" : "Cloudburst Sys";

    if (count($fronters["members"]) > 0) {
        $name = $fronters["members"][0]["display_name"] ?? $fronters["members"][0]["name"];
    }

    echo($name);

    ?>";
</script>

<br>
<div class="container">
    <?php if (isset($parts[2]) && !isset($parts[3])): $id = $parts[2]; ?>
        <div>
            <h2>
                <script>
                    async function updateComputerName() {
                        let name = document.getElementById("computer_name").innerText.trim();

                        if (name === "") {
                            name = "<?= $computer["host"] ?>";
                            document.getElementById("computer_name").innerText = name;
                        } else {
                            await fetch("/api/computername?id=<?= $id ?>&name=" + encodeURIComponent(name));
                        }
                    }
                </script>
                <span id="computer_name" style="vertical-align: middle;" contenteditable="true" onkeyup="updateComputerName();"><?= $names[$id] ?? $computer["host"] ?></span>
                <a href="/-/computers" class="small btn btn-outline-light" style="float:right;margin-top:5px;vertical-align:middle;opacity:1 !important; color:white;">Back</a>
            </h2>

            <?php if ($computer["luna_version"] === "1.0.0"): ?>
            <div class="alert alert-danger">
                <p><b>We are unable to give you access to this device.</b></p>
                <p>This computer is running Luna version 1.0.0. This version of Luna is considered malware because of inconspicuous behavior and is therefore not usable anymore and has been disabled. This means you cannot access this computer at the moment.</p>
                Update this device to run Luna 1.1.0 or newer and try again.
            </div>
            <?php else: ?>
            <h4 style="margin-top: 20px;margin-bottom: 10px;">Overview</h4>
            <style>
                @media (max-width: 700px) {
                    #g-0 {
                        grid-template-columns: 1fr !important;
                    }

                    #g-0 img, #g-0 table {
                        display: inline-block;
                        margin-left: auto;
                        margin-right: auto;
                    }

                    #g-0 table {
                        margin-top: 20px;
                    }
                }
            </style>
            <div style="display: grid; grid-template-columns: 256px 1fr; grid-column-gap: 30px;" id="g-0">
                <div style="display: flex; align-items: center;">
                    <img style="width: 256px;" src="<?php

                    if (isset($computer["dsb"])) {
                        switch ($computer["dsb"]["platform"]) {
                            case "chrome":
                                echo("https://upload.wikimedia.org/wikipedia/en/3/3e/ChromeOS_screenshot.png");
                                break;

                            default:
                                echo("https://ponies.equestria.horse/api/data?f=computers/screens/" . $id . "-" . $computer["screens"][0]["id"] . ".jpg");
                                break;
                        }
                    } else {
                        echo("https://ponies.equestria.horse/api/data?f=computers/screens/" . $id . "-" . $computer["screens"][0]["id"] . ".jpg");
                    }

                    ?>">
                </div>
                <div style="display: flex; align-items: center;">
                    <table>
                        <tbody>
                            <tr>
                                <td style="padding-right: 10px; text-align: right;"><b>Owner:</b></td>
                                <td><?= $owner === "raindrops" ? "Raindrops System" : "Cloudburst System" ?></td>
                            </tr>
                            <tr>
                                <td style="padding-right: 10px; text-align: right;"><b>Luna version:</b></td>
                                <td><?= $computer["luna_version"] ?><?php if (isset($computer["dsb"])): ?> (<?php switch ($computer["dsb"]["platform"]) {
                                        case "chrome":
                                            echo("Chrome ");
                                            break;

                                        case "desktop":
                                            echo("Desktop ");
                                            break;

                                        case "android":
                                            echo("Android ");
                                            break;

                                        case "wear":
                                            echo("Wear OS ");
                                            break;

                                        case "server":
                                            echo("Server ");
                                            break;

                                        default:
                                            break;
                                    } ?> DSB <?= $computer["dsb"]["version"] ?? 0 ?>)<?php else: ?> (legacy)<?php endif; ?></td>
                            </tr>
                            <tr>
                                <td style="padding-right: 10px; text-align: right;"><b>System:</b></td>
                                <td><?= $computer["os"] ?></td>
                            </tr>
                            <tr>
                                <td style="padding-right: 10px; text-align: right;"><b>OS kernel:</b></td>
                                <td><?= $computer["kernel"] ?></td>
                            </tr>
                            <tr>
                                <td style="padding-right: 10px; text-align: right;"><b>Serial:</b></td>
                                <?php if (trim($computer["serial"]) !== ""): ?><td><?= $computer["serial"] ?> (<?= $computer["serial_source"] === "hardware" ? "logic board" : "OS" ?>)</td><?php else: ?><td>-</td><?php endif; ?>
                            </tr>
                            <tr>
                                <td style="padding-right: 10px; text-align: right;"><b>Status:</b></td>
                                <?php if (timeAgo($computer["date"]) === "now" || str_contains(timeAgo($computer["date"]), " sec ")): ?>
                                <td>Online</td>
                                <?php else: ?>
                                <td>Last seen <?= timeAgo($computer["date"]) ?>: <?= date('j M, g:ia (T)', strtotime($computer["date"])) ?></td>
                                <?php endif; ?>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>

            <table><tbody>
                <tr>
                    <td colspan="2"><h4 style="margin-top: 20px;margin-bottom: 10px;">Processors and graphics</h4></td>
                </tr>
                <tr>
                    <td style="padding-right: 10px; text-align: right;"><b>Main processor:</b></td>
                    <td><?= trim(($computer["cpu"]["manufacturer"] ?? "") . " " . ($computer["cpu"]["brand"] ?? "") . " " . ($computer["cpu"]["model"] ?? "")) ?> (<?= $computer["cpu"]["speed"] ?? "" ?> GHz)</td>
                </tr>
                <tr>
                    <td style="padding-right: 10px; text-align: right;"><b>Processor threading:</b></td>
                    <td>
                        <?php if ($computer["cpu"]["processors"] > 1): ?><?= $computer["cpu"]["processors"] ?> processor<?= $computer["cpu"]["processors"] > 1 ? "s" : "" ?>,
                        <?php endif; ?><?= $computer["cpu"]["physicalCores"] ?> core<?= $computer["cpu"]["physicalCores"] > 1 ? "s" : "" ?><?= isset($computer["cpu"]["efficiencyCores"]) && isset($computer["cpu"]["performanceCores"]) && $computer["cpu"]["efficiencyCores"] !== 0 && $computer["cpu"]["performanceCores"] !== 0 ? " (" . $computer["cpu"]["efficiencyCores"] . " efficiency, " . $computer["cpu"]["performanceCores"] . " performance)" : "" ?><?php if ($computer["cpu"]["cores"] !== $computer["cpu"]["physicalCores"]): ?>,
                        <?= $computer["cpu"]["cores"] ?> thread<?= $computer["cpu"]["cores"] > 1 ? "s" : "" ?><?php endif; ?></td>
                </tr>
                <tr>
                    <td style="padding-right: 10px; text-align: right;"><b>Hardware virtualization:</b></td>
                    <td><?= isset($computer["cpu"]["virtualization"]) ? ($computer["cpu"]["virtualization"] ? "Capable" : "Disabled") : "Unknown" ?></td>
                </tr>
                <?php if (isset($computer["cpu"]["cache"])): ?>
                <tr>
                    <td style="padding-right: 10px; text-align: right;"><b>Level 1 cache:</b></td>
                    <td><?= round($computer["cpu"]["cache"]["l1i"] / 1024, 2) ?> KiB (instruction), <?= round($computer["cpu"]["cache"]["l1d"] / 1024, 2) ?> KiB (data)</td>
                </tr>
                <tr>
                    <td style="padding-right: 10px; text-align: right;"><b>Level 2 cache:</b></td>
                    <td><?= isset($computer["cpu"]["cache"]["l2"]) && is_numeric($computer["cpu"]["cache"]["l2"]) ? round($computer["cpu"]["cache"]["l2"] / 1024**2, 2) . " MiB" : "Not supported" ?></td>
                </tr>
                <tr>
                    <td style="padding-right: 10px; text-align: right;"><b>Level 3 cache:</b></td>
                    <td><?= isset($computer["cpu"]["cache"]["l3"]) && is_numeric($computer["cpu"]["cache"]["l3"]) ? round($computer["cpu"]["cache"]["l3"] / 1024**2, 2) . " MiB" : "Not supported" ?></td>
                </tr>
                <?php endif; ?>
                <?php if (isset($computer["gpu"])): ?>
                <?php if (isset($computer["gpu"]["controllers"]) && isset($computer["gpu"]["controllers"][0])): ?><tr>
                    <td style="padding-right: 10px; text-align: right;"><b>Graphics processor<?= count($computer["gpu"]["controllers"]) > 1 ? "s" : "" ?>:</b></td>
                    <td>
                        <?php $index = 0; foreach ($computer["gpu"]["controllers"] as $controller): ?>
                            <?= $controller["bus"] ?> <?= $controller["model"] ?> (<?php if (isset($controller["cores"])): ?><?= $controller["cores"] ?> cores, <?php endif; ?><?= $controller["vramDynamic"] ? "dynamic graphics memory" : $controller["vram"] . " MiB" ?>)
                            <?= $index < count($computer["gpu"]["controllers"]) - 1 ? "<br>" : "" ?>
                        <?php $index++; endforeach; ?>
                    </td>
                </tr><?php endif; ?>
                <tr>
                    <td style="padding-right: 10px; text-align: right;"><b>Display<?= count($computer["gpu"]["displays"]) > 1 ? "s" : "" ?>:</b></td>
                    <td>
                        <?php $index = 0; foreach ($computer["gpu"]["displays"] as $display): ?>
                            <?= $display["connection"] ?> <?= $display["vendor"] ?> <?= $display["model"] ?> (<?= $display["currentResX"] ?>x<?= $display["currentResY"] ?><?= $display["currentResX"] !== $display["resolutionX"] ? ", native " . $display["resolutionX"] . "x" . $display["resolutionY"] : "" ?>, <?= $display["currentRefreshRate"] ?> Hz)
                            <?= $index < count($computer["gpu"]["displays"]) - 1 ? "<br>" : "" ?>
                        <?php $index++; endforeach; ?>
                    </td>
                </tr>
                <?php endif; ?>

                <tr>
                    <td colspan="2"><h4 style="margin-top: 20px;margin-bottom: 10px;">System memory</h4></td>
                </tr>
                <tr>
                    <td style="padding-right: 10px; text-align: right;"><b>Physical memory:</b></td>
                    <td><?= round($computer["ram"]["total"] / 1024**3, 2) ?> GiB (<?= round($computer["ram"]["used"] / 1024**2, 2) ?> MiB used, <?= round($computer["ram"]["free"] / 1024**2, 2) ?> MiB free)</td>
                </tr>
                <?php if (isset($computer["ram"]["active"]) && isset($computer["ram"]["available"]) && isset($computer["ram"]["buffcache"])): ?><tr>
                    <td style="padding-right: 10px; text-align: right;"><b>Memory usage:</b></td>
                    <td><?= round($computer["ram"]["active"] / 1024**2, 2) ?> MiB active, <?= round($computer["ram"]["available"] / 1024**2, 2) ?> MiB available, <?= round($computer["ram"]["buffcache"] / 1024**2, 2) ?> MiB cache</td>
                </tr><?php endif; ?>
                <?php if (isset($computer["ram"]["swaptotal"]) && isset($computer["ram"]["swapused"])): ?><tr>
                    <td style="padding-right: 10px; text-align: right;"><b>Swap memory:</b></td>
                    <td><?= round($computer["ram"]["swaptotal"] / 1024**3, 2) ?> GiB (<?= round($computer["ram"]["swapused"] / 1024**2, 2) ?> MiB used, <?= round($computer["ram"]["swapfree"] / 1024**2, 2) ?> MiB free)</td>
                </tr><?php endif; ?>
                <?php if (isset($computer["ram_chips"])): ?><tr>
                    <td style="padding-right: 10px; text-align: right;"><b>RAM chips:</b></td>
                    <td>
                        <?php $index = 0; foreach ($computer["ram_chips"] as $chip): ?>
                            <?php if (isset($chip["manufacturer"])): ?><?= $chip["manufacturer"] ?> <?php endif; ?><?= round($chip["size"] / 1024**2, 2) ?> MiB <?= $chip["type"] ?>
                            <?= $index < count($computer["ram_chips"]) - 1 ? "<br>" : "" ?>
                        <?php $index++; endforeach; ?>
                    </td>
                </tr><?php endif; ?>

                <tr>
                    <td colspan="2"><h4 style="margin-top: 20px;margin-bottom: 10px;">Battery</h4></td>
                </tr>
                <?php if (isset($computer["battery"])): ?>
                <?php if ($computer["battery"]["hasBattery"]): ?>
                <tr>
                    <td style="padding-right: 10px; text-align: right;"><b>Battery model:</b></td>
                    <td><?= preg_replace("/ +/", " ", trim($computer["battery"]["manufacturer"] . " " . $computer["battery"]["type"] . " " . $computer["battery"]["model"])) ?></td>
                </tr>
                <tr>
                    <td style="padding-right: 10px; text-align: right;"><b>Charge level:</b></td>
                    <td><?= $computer["battery"]["percent"] ?>%<?= $computer["battery"]["acConnected"] ? ", plugged in" : "" ?> (<?= round($computer["battery"]["voltage"], 2) ?> V)</td>
                </tr>
                <?php if (isset($computer["battery"]["cycleCount"] ) && $computer["battery"]["cycleCount"] > 0): ?>
                <tr>
                    <td style="padding-right: 10px; text-align: right;"><b>Cycles:</b></td>
                    <td><?= $computer["battery"]["cycleCount"] ?></td>
                </tr>
                <?php endif; ?>
                <tr>
                    <td style="padding-right: 10px; text-align: right;"><b>Remaining capacity:</b></td>
                    <td><?= round($computer["battery"]["currentCapacity"], 2) ?>/<?= round($computer["battery"]["maxCapacity"], 2) ?> <?= $computer["battery"]["capacityUnit"] ?><?php if (isset($computer["battery"]["designedCapacity"]) && $computer["battery"]["designedCapacity"] > 0): ?> (designed for <?= $computer["battery"]["designedCapacity"] ?> <?= $computer["battery"]["capacityUnit"] ?><?php if ($computer["battery"]["designedCapacity"] > 0): ?>, <?= round(($computer["battery"]["maxCapacity"] / $computer["battery"]["designedCapacity"]) * 100, 2) ?>% left<?php endif; ?>)<?php endif; ?></td>
                </tr>
                <?php else: ?><tr><td colspan="2">This device does not contain a battery.</td></tr><?php endif; ?><?php else: ?><tr><td colspan="2">This operating system does not support reporting battery info.</td></tr><?php endif; ?>

                <tr>
                    <td colspan="2"><h4 style="margin-top: 20px;margin-bottom: 10px;">Operating system</h4></td>
                </tr>
                <tr>
                    <td style="padding-right: 10px; text-align: right;"><b>System:</b></td>
                        <td><?= $computer["os_info"]["distro"] ?> <?= $computer["os_info"]["release"] === "unknown" || $computer["os_info"]["release"] === "rolling" ? "" : $computer["os_info"]["release"] ?></td>
                </tr>
                <tr>
                    <td style="padding-right: 10px; text-align: right;"><b>CPU architecture:</b></td>
                    <td><?= $computer["os_info"]["arch"] ?></td>
                </tr>
                <tr>
                    <td style="padding-right: 10px; text-align: right;"><b>Kernel version:</b></td>
                    <td><?= $computer["os_info"]["kernel"] ?></td>
                </tr>
                <?php if (isset($computer["os_info"]["codepage"])): ?>
                <tr>
                    <td style="padding-right: 10px; text-align: right;"><b>Code page:</b></td>
                    <td><?= $computer["os_info"]["codepage"] ?></td>
                </tr>
                <?php endif; ?>
                <tr>
                    <td style="padding-right: 10px; text-align: right;"><b>Build number:</b></td>
                    <td><?= $computer["os_info"]["build"] === "rolling" ? "Rolling release" : $computer["os_info"]["build"] ?></td>
                </tr>
                <tr>
                    <td style="padding-right: 10px; text-align: right;"><b>Serial number:</b></td>
                    <td><?= $computer["os_info"]["serial"] ?></td>
                </tr>
                <?php if (isset($computer["os_info"]["uefi"])): ?>
                <tr>
                    <td style="padding-right: 10px; text-align: right;"><b>Startup interface:</b></td>
                    <td><?= $computer["os_info"]["uefi"] ? "UEFI" : "BIOS (legacy)" ?></td>
                </tr>
                <?php endif; ?>

                <?php if (count(array_filter($computer["versions"], function ($version, $software) {
                    return trim($version) !== "" && $software !== "systemOpensslLib" && $software !== "openssl" && $software !== "node" && $software !== "v8";
                }, ARRAY_FILTER_USE_BOTH)) > 0): ?>
                <tr>
                    <td colspan="2"><h4 style="margin-top: 20px;margin-bottom: 10px;">Software versions</h4></td>
                </tr>
                <?php foreach ($computer["versions"] as $software => $version): if (trim($version) !== "" && $software !== "systemOpensslLib" && $software !== "openssl" && $software !== "node" && $software !== "v8"): ?>
                <tr>
                    <td style="padding-right: 10px; text-align: right;"><b><?=
                            match ($software) {
                                "kernel" => "OS kernel",
                                "systemOpenssl" => $computer["versions"]["systemOpensslLib"],
                                "npm" => "NPM",
                                "yarn" => "Yarn",
                                "gulp" => "Gulp",
                                "grunt" => "Grunt",
                                "git" => "Git",
                                "tsc" => "TypeScript",
                                "mysql" => "MySQL",
                                "redis" => "Redis",
                                "mongodb" => "MongoDB",
                                "apache" => "Apache HTTPD",
                                "php" => "PHP",
                                "docker" => "Docker",
                                "postfix" => "Postfix SMTP Server",
                                "postgresql" => "PostgreSQL",
                                "perl" => "Perl",
                                "python" => "Python (legacy)",
                                "python3" => "Python",
                                "pip" => "PIP (legacy)",
                                "pip3" => "PIP",
                                "java" => "Java",
                                "gcc" => "C compiler",
                                "virtualbox" => "VirtualBox",
                                "bash" => "Bash",
                                "zsh" => "zsh",
                                "fish" => "Fish",
                                "powershell" => "PowerShell",
                                "dotnet" => ".NET",
                                default => $software,
                            }
                    ?>:</b></td>
                    <td><?= $version ?></td>
                </tr>
                <?php endif; endforeach; endif; ?>
                </tbody>
            </table>

            <?php if (!isset($computer["remote_control"]) || $computer["remote_control"] === true): ?>
            <h4 style="margin-top: 20px;margin-bottom: 10px;" id="screens">Remote control</h4>
            <p id="page-content">
                You can remotely control this device. <a href="./<?= $id ?>/control">Open remote device.</a><br>
                <small class="text-muted">Note: The user of this device will need to approve your connection request before you can see and control their screen.</small>
            </p>
            <?php endif; ?>

            <?php if (count($computer["users"]) > 0): ?>
            <h4 style="margin-top: 20px;margin-bottom: 10px;">Sessions</h4>
            <table class="table">
                <thead>
                <tr>
                    <th scope="col">TTY</th>
                    <th scope="col">Command</th>
                    <th scope="col">User</th>
                    <th scope="col">Open</th>
                    <th scope="col">Type</th>
                </tr>
                </thead>
                <tbody>
                <?php foreach ($computer["users"] as $user): ?>
                    <tr>
                        <th scope="row"><?= $user["tty"] ?></th>
                        <td><?= str_replace("�", "", $user["command"]) ?></td>
                        <td><?= $user["user"] ?></td>
                        <td><?= $user["date"] . " " . $user["time"] ?></td>
                        <td><?= trim($user["ip"]) === "" || trim($user["ip"]) === ":1" || trim($user["ip"]) === "::1" ? "Local" : "Remote (" . $user["ip"] . ")" ?></td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
            <?php endif; ?>

            <?php if (count($computer["filesystems"]) > 0): ?>
            <h4 style="margin-top: 20px;margin-bottom: 10px;">Filesystems</h4>
            <table class="table">
                <thead>
                <tr>
                    <th scope="col">Device</th>
                    <th scope="col">Mount point</th>
                    <th scope="col">Type</th>
                    <th scope="col">Used</th>
                    <th scope="col">Free</th>
                    <th scope="col">Total</th>
                </tr>
                </thead>
                <tbody>
                <?php

                uasort($computer["filesystems"], function ($a, $b) {
                    return $b["use"] - $a["use"];
                });

                foreach ($computer["filesystems"] as $fs): ?>
                    <tr>
                        <th scope="row"><?= $fs["fs"] ?></th>
                        <td><?= $fs["mount"] ?></td>
                        <td><?= $fs["type"] ?></td>
                        <td><?= prettySize($fs["used"]) ?> (<?= $fs["use"] ?>%)</td>
                        <td><?= prettySize($fs["available"]) ?></td>
                        <td><?= prettySize($fs["size"]) ?></td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
            <?php endif; ?>

            <?php if (isset($computer["audio"])): ?>
            <h4 style="margin-top: 20px;margin-bottom: 10px;">Audio devices</h4>
            <table class="table">
                <thead>
                <tr>
                    <th scope="col">Type</th>
                    <th scope="col">Manufacturer</th>
                    <th scope="col">Name</th>
                    <th scope="col">Default</th>
                    <th scope="col">Connection</th>
                </tr>
                </thead>
                <tbody>
                <?php foreach ($computer["audio"] as $device): ?>
                    <tr>
                        <th scope="row"><?= trim($device["type"]) === "" ? "Device" : $device["type"] ?></th>
                        <td><?= $device["manufacturer"] ?></td>
                        <td><?= $device["name"] ?></td>
                        <td><?= $device["default"] ? "Yes" : "" ?></td>
                        <td><?= trim($device["channel"]) === "" ? "Virtual" : $device["channel"] ?></td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
            <?php endif; ?>

            <?php if (isset($computer["network"])): ?>
            <h4 style="margin-top: 20px;margin-bottom: 10px;">Network cards</h4>
            <table class="table">
                <thead>
                <tr>
                    <th scope="col">Interface</th>
                    <th scope="col">State</th>
                    <th scope="col">IP addresses</th>
                    <th scope="col">MAC address</th>
                    <th scope="col">Type</th>
                    <th scope="col">Internal</th>
                    <th scope="col">Virtual</th>
                    <th scope="col">DHCP</th>
                </tr>
                </thead>
                <tbody>
                <?php

                uasort($computer["network"], function ($a, $b) {
                    $statA = match ($a["operstate"]) {
                        "up" => 300,
                        "down" => 200,
                        default => 100
                    };
                    $statB = match ($b["operstate"]) {
                        "up" => 300,
                        "down" => 200,
                        default => 100
                    };

                    return $statB - $statA;
                });

                foreach ($computer["network"] as $iface): ?>
                <tr <?= $iface["operstate"] !== "up" ? 'style="opacity:.5;"' : "" ?>>
                    <th scope="row"><?= $iface["ifaceName"] ?></th>
                    <td><?= $iface["operstate"] === "up" ? "Active" : ($iface["operstate"] === "down" ? "Inactive" : "") ?></td>
                    <td><?= trim($iface["ip4"] !== "") ? $iface["ip4"] : "" ?><?= trim($iface["ip6"] !== "") ? (trim($iface["ip4"] !== "") ? ", " : "") . $iface["ip6"] : "" ?></td>
                    <td><?= trim($iface["mac"] !== "") ? $iface["mac"] : "" ?></td>
                    <td><?= $iface["type"] === "wireless" ? "Wireless" : "Wired" ?></td>
                    <td><?= isset($iface["internal"]) && $iface["internal"] ? "Yes" : "" ?></td>
                    <td><?= $iface["virtual"] ? "Yes" : "" ?></td>
                    <td><?= $iface["dhcp"] ? "Yes" : "" ?></td>
                </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
            <?php endif; ?>

            <?php if (isset($computer["connections"])): ?>
            <h4 style="margin-top: 20px;margin-bottom: 10px;">Network connections</h4>
            <table class="table">
                <thead>
                <tr>
                    <th scope="col">State</th>
                    <th scope="col">Protocol</th>
                    <th scope="col">Source</th>
                    <th scope="col">Destination</th>
                    <th scope="col">Process</th>
                </tr>
                </thead>
                <tbody>
                <?php foreach ($computer["connections"] as $connection): ?>
                    <tr>
                        <th scope="row"><?= match ($connection["state"]) {
                            "ESTABLISHED" => "Connected",
                            "LISTEN" => "Listening",
                            "CLOSING" => "Terminating",
                            "UNKNOWN" => "?",
                            "LAST_ACK", "FIN_WAIT2", "TIME_WAIT" => "Waiting",
                            "FIN_WAIT1", "CLOSE_WAIT" => "Closing",
                            "SYN_RECV" => "Received",
                            "SYN_SENT" => "Attempting",
                            default => $connection["state"],
                        } ?></th>
                        <td><?= match ($connection["protocol"]) {
                            "tcp4" => "TCP over IPv4",
                            "tcp6" => "TCP over IPv6",
                            "tcp46" => "TCP over IPv4 and IPv6",
                            "udp4" => "UDP over IPv4",
                            "udp6" => "UDP over IPv6",
                            "udp46" => "UDP over IPv4 and IPv6",
                            default => $connection["protocol"],
                        } ?></td>
                        <td><?= $connection["localAddress"] === "*" ? "&lt;all&gt;" : match ($connection["localAddress"]) {
                            "127.0.0.1" => "&lt;local IPv4&gt;",
                            "::1" => "&lt;local IPv6&gt;",
                            default => $connection["localAddress"],
                        } ?> (port <?= $connection["localPort"] ?>)</td>
                        <td><?php if ($connection["state"] !== "LISTEN"): ?><?= $connection["peerAddress"] === "*" ? "&lt;all&gt;" : match ($connection["peerAddress"]) {
                                "127.0.0.1" => "&lt;local IPv4&gt;",
                                "::1" => "&lt;local IPv6&gt;",
                                default => $connection["localAddress"],
                            } ?> (<?= $connection["peerPort"] ?>)<?php endif; ?></td>
                        <td><?php

                            $f = array_values(array_filter($computer["processes"], function ($i) use ($connection) {
                                return $i["pid"] === $connection["pid"];
                            }));

                            if (count($f) > 0) {
                                echo($f[0]["name"] . " [" . $connection["pid"] . "]");
                            } else {
                                echo($connection["pid"]);
                            }

                            ?></td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
            <?php endif; ?>

            <?php if (isset($computer["processes"]) && count($computer["processes"]) > 0): ?>
            <h4 style="margin-top: 20px;margin-bottom: 10px;">Processes</h4>
            <table class="table">
                <thead>
                    <tr>
                        <th scope="col">PID</th>
                        <th scope="col">Name</th>
                        <th scope="col">CPU</th>
                        <th scope="col">RAM</th>
                        <th scope="col">User</th>
                        <th scope="col">Started</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($computer["processes"] as $process): ?>
                        <tr>
                            <th scope="row"><?= $process["pid"] ?></th>
                            <td><?= $process["name"] ?></td>
                            <td><?= round($process["cpu"] ?? 0, 2) ?>%</td>
                            <td><?= round($process["ram"], 2) ?>%</td>
                            <td><?= $process["user"] ?></td>
                            <td><?= isset($process["date"]) ? timeAgo($process["date"]) : "" ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
            <?php endif; ?>
        <?php endif; ?>
        </div>
    <?php elseif (isset($parts[3]) && $parts[3] === "control"): $id = $parts[2]; ?>
    <?php if ($computer["luna_version"] !== "1.0.0"): ?>
    <div>
        <h2>
            <span style="vertical-align: middle;">Remote controlling <?= $computer["host"] ?></span>
            <a href="/-/computers/<?= $id ?>" class="small btn btn-outline-light" style="float:right;margin-top:5px;vertical-align:middle;opacity:1 !important; color:white;">Back</a>
        </h2>

        <div id="remote-control-load">
            <img src="/assets/editor/load.svg" style="width: 36px; vertical-align: middle;" id="rc-loader-img" alt=""><span style="vertical-align: middle;" id="remote-control-load-msg">Initialising connection...</span></div>
        </div>

        <div class="alert alert-danger" id="remote-control-error" style="display:none;">
            <b>Unable to connect:</b> <span id="remote-control-error-message">Error</span>
        </div>

        <div id="remote-control-app" style="display: none;">
            <div style="display: flex; align-items: center;">
                <img id="display" style="max-width: 100%; max-height: 100%; height: 100%; width: 100%; opacity: .5" src="https://ponies.equestria.horse/api/data?f=computers/screens/<?= $id . "-" . $computer["screens"][0]["id"] ?>.jpg">
            </div>

            <div id="commands" style="background: #151515; border: 1px solid #333; margin-top: 10px; border-radius: 10px; font-family: 'JetBrains Mono', monospace; font-size: 14px;">
                <div id="output" style="height: 200px; padding: 10px 20px; overflow: auto;">
                    <pre id="output-text"></pre>
                    <div id="loader" style="display: none;">
                        <img src="/assets/editor/load.svg" style="width: 36px; vertical-align: middle; margin-left: -8px; margin-top: -8px;" id="loader-img" alt="">
                        <span id="loader-time" style="vertical-align: middle; opacity: .5; display: inline-block; margin-top: -8px; margin-left: -8px;">
                        <span id="loader-time-inner"></span>
                        <span id="loader-time-stopper">
                            | <a id="loader-time-stopper-link" style="cursor: pointer; text-decoration: underline;" onclick="stopCommand();">Stop</a>
                        </span>
                    </span>
                    </div>
                </div>
                <div id="entry" style="border-top: 1px solid #333;">
                    <input type="text" id="command-entry" placeholder="Enter a command" style="width: 100%; color: white; background: transparent; border: none; padding: 10px 20px; outline: none;">
                </div>
            </div>
        </div>

        <script>
            function setLoadMessage(msg) {
                document.getElementById("remote-control-load-msg").innerText = msg;
            }

            function stopCommand() {
                ws.send(JSON.stringify({
                    type: "halt"
                }));
            }
            
            let rejected = false;

            function scrollToBottom() {
                document.getElementById("output").scrollTop = document.getElementById("output").scrollHeight;
            }

            window.addEventListener("load", () => {
                document.getElementById("command-entry").value = "";
            });

            document.getElementById("command-entry").onkeydown = (event) => {
                if (event.key === "Enter") {
                    event.preventDefault();
                    let command = document.getElementById("command-entry").value;
                    document.getElementById("command-entry").value = "";
                    document.getElementById("command-entry").disabled = true;
                    document.getElementById("loader-time").style.display = "none";
                    document.getElementById("loader").style.display = "";
                    document.getElementById("output-text").innerHTML += "\n$ " + command.replaceAll(">", "&gt;").replaceAll("<", "&lt;") + "\n";
                    scrollToBottom();
                    ws.send(JSON.stringify({
                        type: "command",
                        value: btoa(command)
                    }));
                }
            }

            window.controlling = false;
            window.commandRunInterval = null;

            function connect() {
                setLoadMessage("Connecting to server...");
                window.ws = new WebSocket("wss://ponies.equestria.horse/_Computers-RemoteControl-EntryPoint/socket");
                window.baseLatency = null;

                ws.addEventListener('open', (data) => {
                    setLoadMessage("Authenticating...");
                    console.log(data);

                    ws.send(JSON.stringify({
                        type: "client",
                        id: "<?= $id ?>",
                        name: window.userName,
                        token: "<?= $_COOKIE['PEH2_SESSION_TOKEN'] ?>"
                    }));

                    setLoadMessage("Waiting for the user to accept...");
                });

                ws.addEventListener('message', async (_data) => {
                    setLoadMessage("Loading...");
                    let data = JSON.parse(await (_data.data.text()));

                    if (data.rejected) {
                        document.getElementById("remote-control-load").style.display = "none";
                        document.getElementById("remote-control-error").style.display = "";
                        document.getElementById("remote-control-error-message").innerText = "The user has rejected the connection request.";
                        rejected = true;
                    }

                    if (data.type === "image") {
                        setLoadMessage("Loading initial image...");

                        document.getElementById("display").style.opacity = "1";
                        document.getElementById("display").src = data.url;

                        if (baseLatency) {
                            ws.send(JSON.stringify({
                                type: "set_latency",
                                value: Math.abs((new Date().getTime() - data.time) - baseLatency)
                            }));

                            if (Math.abs((new Date().getTime() - data.time) - baseLatency) > 200) {
                                ws.send(JSON.stringify({
                                    type: "reduce_quality"
                                }));
                            } else if (Math.abs((new Date().getTime() - data.time) - baseLatency) < 150) {
                                ws.send(JSON.stringify({
                                    type: "increase_quality"
                                }));
                            }
                        } else {
                            window.baseLatency = Math.abs(new Date().getTime() - data.time);
                        }

                        document.getElementById("remote-control-load").style.display = "none";
                        document.getElementById("remote-control-app").style.display = "";
                    } else if (data.type === "command_stop") {
                        if (data.code !== 0) {
                            if (data.signal) {
                                if (data.code) {
                                    document.getElementById("output-text").innerHTML += "<span style='color: #ff8c8c;'>Command exited with code " + data.code.toString().replaceAll(">", "&gt;").replaceAll("<", "&lt;") + " (caused by " + data.signal.toString().replaceAll(">", "&gt;").replaceAll("<", "&lt;") + ")</span>";
                                } else {
                                    document.getElementById("output-text").innerHTML += "<span style='color: #ff8c8c;'>Command exited with signal " + data.signal.toString().replaceAll(">", "&gt;").replaceAll("<", "&lt;");
                                }
                            } else {
                                document.getElementById("output-text").innerHTML += "<span style='color: #ff8c8c;'>Command exited with code " + data.code.toString().replaceAll(">", "&gt;").replaceAll("<", "&lt;") + "</span>";
                            }
                        }

                        clearInterval(window.commandRunInterval);
                        document.getElementById("command-entry").disabled = false;
                        document.getElementById("loader").style.display = "none";
                        scrollToBottom();
                        document.getElementById("command-entry").focus();
                    } else if (data.type === "command_start") {
                        document.getElementById("loader-time").style.display = "inline-block";
                        document.getElementById("loader-time-inner").innerText = "";
                        window.commandStartTime = new Date().getTime();
                        window.commandRunInterval = setInterval(() => {
                            document.getElementById("loader-time-inner").innerText = ((new Date().getTime() - window.commandStartTime) / 1000).toFixed(2) + "s";
                        }, 10);
                        scrollToBottom();
                    } else if (data.type === "command_stdout" || data.type === "command_stderr") {
                        if (data.type === "command_stderr") {
                            document.getElementById("output-text").innerHTML += "<span style='color: #ffec8c;'>" + atob(data.value).replaceAll(">", "&gt;").replaceAll("<", "&lt;") + "</span>";
                        } else {
                            document.getElementById("output-text").innerHTML += atob(data.value).replaceAll(">", "&gt;").replaceAll("<", "&lt;");
                        }

                        scrollToBottom();
                    } else {
                        console.log(data);
                    }
                });

                ws.addEventListener('close', (data) => {
                    document.getElementById("display").style.opacity = "0.5";
                    console.log(data);

                    if (!rejected) {
                        setLoadMessage("Connection error, retrying in 1 second.");

                        setTimeout(() => {
                            setLoadMessage("Retrying connection...");
                            connect();
                        }, 1000);
                    }
                });
            }

            connect();

            document.getElementById("display").addEventListener("mousemove", (event) => {
                event.preventDefault();

                if (window.controlling) ws.send(JSON.stringify({
                    type: "move_cursor",
                    x: event.offsetX / document.getElementById("display").width,
                    y: event.offsetY / document.getElementById("display").height
                }));
            });

            document.getElementById("display").addEventListener("click", (event) => {
                event.preventDefault();

                if (window.controlling) ws.send(JSON.stringify({
                    type: "click",
                    button: 0
                }));
            });

            document.getElementById("display").addEventListener("contextmenu", (event) => {
                event.preventDefault();

                if (window.controlling) ws.send(JSON.stringify({
                    type: "click",
                    button: 1
                }));
            });

            document.getElementById("display").addEventListener("auxclick", (event) => {
                event.preventDefault();

                if (window.controlling) ws.send(JSON.stringify({
                    type: "click",
                    button: 2
                }));
            });

            document.body.onkeydown = document.getElementById("display").onkeydown = (event) => {
                if (window.controlling) event.preventDefault();

                if (window.controlling) ws.send(JSON.stringify({
                    type: "keyboard",
                    shift: event.shiftKey,
                    ctrl: event.ctrlKey,
                    meta: event.metaKey,
                    alt: event.altKey,
                    key: event.key
                }));
            }

            document.getElementById("display").ondragstart = (event) => {
                event.preventDefault();

                if (window.controlling) ws.send(JSON.stringify({
                    type: "move_cursor",
                    x: event.offsetX / document.getElementById("display").width,
                    y: event.offsetY / document.getElementById("display").height
                }));
                if (window.controlling) ws.send(JSON.stringify({
                    type: "start_drag"
                }));
            }

            document.getElementById("display").ondragend = (event) => {
                event.preventDefault();

                if (window.controlling) ws.send(JSON.stringify({
                    type: "move_cursor",
                    x: event.offsetX / document.getElementById("display").width,
                    y: event.offsetY / document.getElementById("display").height
                }));
                if (window.controlling) ws.send(JSON.stringify({
                    type: "stop_drag"
                }));
            }

            function enableControl() {
                window.controlling = true;
            }

            function disableControl() {
                window.controlling = false;
            }
        </script>
        <?php endif; ?>
    <?php else: ?>
        <div>
            <h2>Devices</h2>
            <p>Click on a device to view information about it</p>

            <div class="alert alert-warning">
                <b>Luna and the Cold Haze Devices software are deprecated.</b> These applications are now deprecated and monitoring devices through Luna is not supported anymore. Basic support and servers will be shut down on July 1<sup>st</sup>.
            </div>

            <ul class="list-group">
                <?php

                $list = array_filter(scandir($_SERVER['DOCUMENT_ROOT'] . "/includes/data/computers/metadata"), function ($i) {
                    return !str_starts_with($i, ".");
                });
                uasort($list, function ($a, $b) {
                    $ca = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/computers/metadata/" . $a), true);
                    $cb = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/computers/metadata/" . $b), true);
                    return strtotime($cb["date"]) - strtotime($ca["date"]);
                });

                foreach ($list as $file): $computer = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/computers/metadata/" . $file), true); $id = substr($file, 0, -5); $owner = explode("-", $id)[0];

                    if (str_ends_with($computer["os"], "unknown")) {
                        $computer["os"] = trim(substr($computer["os"], 0, -7));
                    }

                ?>
                    <a href="/-/computers/<?= $id ?>" class="list-group-item list-group-item-action" style="display: grid; grid-template-columns: 100px 1fr; grid-column-gap: 10px; <?php if (timeAgo($computer["date"]) !== "now" && !str_contains(timeAgo($computer["date"]), " sec ")): ?>opacity: .5;<?php endif; ?>">
                        <div style="display: flex; align-items: center;">
                            <div style="aspect-ratio: 16/10; width: 100px; background-image: url('<?php

                            if (isset($computer["dsb"])) {
                                switch ($computer["dsb"]["platform"]) {
                                    case "chrome":
                                        echo("https://upload.wikimedia.org/wikipedia/en/3/3e/ChromeOS_screenshot.png");
                                        break;

                                    default:
                                        echo("https://ponies.equestria.horse/api/data?f=computers/screens/" . $id . "-" . $computer["screens"][0]["id"] . ".jpg");
                                        break;
                                }
                            } else {
                                echo("https://ponies.equestria.horse/api/data?f=computers/screens/" . $id . "-" . $computer["screens"][0]["id"] . ".jpg");
                            }

                            ?>'); background-size: cover; background-position: center;"></div>
                        </div>
                        <div style="display: flex; align-items: center;">
                            <div>
                                <b><?= $names[$id] ?? $computer["host"] ?></b><br>
                                <?php

                                if (str_starts_with($computer["os"], "Google ")) {
                                    echo(substr($computer["os"], 7));
                                } else if (str_starts_with($computer["os"], "Microsoft ")) {
                                    echo(substr($computer["os"], 10));
                                } else {
                                    echo($computer["os"]);
                                }

                                ?><br>
                                <?php
                                
                                if (isset($computer["dsb"]) && isset($computer["dsb"]["platform"])) {
                                    switch ($computer["dsb"]["platform"]) {
                                        case "desktop":
                                        case "chrome":
                                            echo("Computer");
                                            break;

                                        case "android":
                                            echo("Phone");
                                            break;

                                        case "wear":
                                            echo("Watch");
                                            break;

                                        case "server":
                                            echo("Server");
                                            break;

                                        default:
                                            echo("Device");
                                            break;
                                    }
                                } else {
                                    echo("Legacy device");
                                }
                                
                                ?> · Owned by <?= $owner === "raindrops" ? "Raindrops" : "Cloudburst"; ?><br>
                                <?php if (timeAgo($computer["date"]) === "now" || str_contains(timeAgo($computer["date"]), " sec ")): ?>Online<?php else: ?>Last seen <?= timeAgo($computer["date"]) ?><?php endif; ?>
                            </div>
                        </div>
                    </a>
                <?php endforeach; ?>
            </ul>
        </div>
    <?php endif; ?>
</div>

<style>
    .list-group-item {
        color: #fff;
        background-color: #222;
        border: 1px solid rgba(255, 255, 255, .125);
    }

    .list-group-item.disabled {
        color: #fff;
        background-color: #222;
        border-color: rgba(255, 255, 255, .125);
        opacity: .75;
    }

    .list-group-item:hover {
        background-color: #252525;
        color: #ddd;
    }

    .list-group-item:active, .list-group-item:focus {
        background-color: #272727;
        color: #bbb;
    }

    .modal-header {
        border-bottom: 1px solid #353738;
    }

    .modal-content {
        border: 1px solid rgba(255, 255, 255, .2);
        background-color: #111;
    }

    .btn-close {
        filter: invert(1);
    }

    .btn-outline-light:hover {
        color: black !important;
    }

    table {
        color: white !important;
    }

    table * {
        border-color: rgba(255, 255, 255, .25) !important;
    }
</style>

<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/components/footer.inc'; ?>