tidy up api/index.php

This commit is contained in:
Ben Harris 2021-02-28 16:58:56 -05:00
parent d7d097af0e
commit 69c4029b0e
1 changed files with 14 additions and 7 deletions

View File

@ -1,33 +1,40 @@
<?php
if (isset($_GET["count"])) { // get number of taglines
$tags = file("../taglines.txt");
echo count($tags);
header("Content-Type: text/plain");
$tags = new SplFileObject("../taglines.txt", "r");
$tags->seek(PHP_INT_MAX);
echo $tags->key();
}
elseif (isset($_GET["key"])) { // get specified tagline (with bounds checking)
header("Content-Type: text/plain");
$tags = file("../taglines.txt");
$i = intval($_GET["key"]);
echo $tags[$i >= count($tags) ? array_rand($tags) : $i];
if (array_key_exists($i, $tags))
echo $tags[$i];
else
die("$i not found");
}
elseif (isset($_GET["fonts"])) { // get list of fonts
$fonts = __DIR__."/fonts/";
header("Content-Type: application/json");
echo json_encode(array_map(function ($f) {
return basename($f, ".flf");
}, glob("$fonts*.flf")));
}, glob("fonts/*.flf")));
}
elseif (isset($_GET["text"])) { // run figlet
$fonts = __DIR__."/fonts/";
header("Content-Type: text/plain");
echo shell_exec(
"/usr/bin/figlet -d $fonts -w 80 -f " .
"/usr/bin/figlet -d fonts -w 80 -f " .
escapeshellarg($_GET["font"] ?? "standard") . " -- " .
escapeshellarg($_GET["text"])
);
}
else { // get a random tagline
header("Content-Type: text/plain");
$tags = file("../taglines.txt");
echo $tags[array_rand($tags)];
}