<?php
function print_uptime()
{
    $now = new DateTimeImmutable();
    $uptime_output = trim(shell_exec("uptime -s"));
    $since = DateTimeImmutable::createFromFormat("Y-m-d H:i:s", $uptime_output);
    echo $now->diff($since)
        ->format("%yy%mm%dd %Hh%im")
         . ", since " . $uptime_output;
}

$conf_dir = "/etc/apache2/sites-enabled/";

function name($conf_name) 
{
    return substr($conf_name, 0, -5 /* strlen(".conf") */);
}

function domain($conf_name)
{
    global $conf_dir;
    $conf_contents = file_get_contents($conf_dir . $conf_name);
    $name_directive = strpos($conf_contents, "ServerName");
    $eol = strpos($conf_contents, PHP_EOL, $name_directive);
    return trim(substr(
        $conf_contents,
        $name_directive + strlen("ServerName"),
        $eol - ($name_directive + strlen("ServerName"))));
}

function since($conf_name)
{
    global $conf_dir;
    return date("Y-m-d H:i", lstat($conf_dir . $conf_name)["mtime"]);
}
?>
<html>
    <head>
        <title>Panda's Site</title>
        <link rel="stylesheet" href="index.css" />
    </head>
    <body>
        <div>
            <h1>Bonjour!</h1>
            <p>Welcome to my development server!</p>
            <br />
            <h2>System Uptime</h2>
            <p><?php print_uptime(); ?></p>
            <br />
            <h2>Sites</h2>
            <table>
                <tr><th>Site</th><th>Local Domain</th><th>Up Since</th></tr>
<?php
$sites_confs = array_values(array_diff(scandir($conf_dir), [".", ".."]));
foreach ($sites_confs as $conf)
{
    echo str_repeat(" ", 4 * 4) . "<tr><td>" . name($conf) . "</td><td>" . domain($conf) . "</td><td>" . since($conf) . "</td></tr>\n";
}
?>
            </table>
        </div>
    </body>
</html>
