<?php

$tags = file("../taglines.txt");
$fonts = __DIR__."/fonts/";

if (isset($_GET["count"])) { // get number of taglines
    echo count($tags);
}

elseif (isset($_GET["key"])) { // get specified tagline (with bounds checking)
    echo $tags[$_GET["key"] >= count($tags) ? array_rand($tags) : intval($_GET["key"])];
}

elseif (isset($_GET["fonts"])) { // get list of fonts
    echo json_encode(array_map(function ($f) {
        return basename($f, ".flf");
    }, glob("$fonts*.flf")));
}

elseif (isset($_GET["text"])) { // run figlet
    echo shell_exec(
        "/usr/bin/figlet -d $fonts -w 120 -f " .
        escapeshellarg($_GET["font"] ?? "standard") . " " .
        escapeshellarg($_GET["text"])
    );
}

elseif (isset($_GET["users"])) {
    $users = [];
    foreach (scandir("/home") as $user) {
        if (!is_dir("/home/$user/public_html")) continue;
        $users[] = $user;
    }
    // usort($users, function($a, $b) { return filemtime($a) - filemtime($b); } );
    echo json_encode($users);
}

else { // get a random tagline
    echo $tags[array_rand($tags)];
}
