This repository has been archived on 2017-11-12. You can view files and clone it, but cannot push or open issues or pull requests.
ledcat-www/index.php
2017-11-12 22:00:46 +01:00

65 lines
1.6 KiB
PHP

<?php
$TMPDIR = "/tmp/ledcat-www";
$PIDFILE = "$TMPDIR/program.pid";
$FIFO = "$TMPDIR/fifo";
$PROGDIR = "./programs";
// Report all errors. Errors should never be ignored, it means that our
// application is broken and we need to fix it.
error_reporting(-1);
// Transform PHP's super weird error system to comprehensible exceptions.
set_error_handler(function($errno, $errstr, $errfile, $errline) {
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
// Don't execute PHP internal error handler.
return true;
});
// Make our tmpdir.
try {
mkdir($TMPDIR, $mode=0777, $recursive=true);
} catch (Exception $ex) { }
// Build an index of the programs we can use.
$programs = array_values(array_filter(scandir("./programs"), function($d) {
return $d[0] != ".";
}));
if (!file_exists($FIFO)) {
posix_mkfifo($FIFO, 0777);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$program = $_POST["program"] ?? $programs[0];
// Ok, let's add a bit of security and make sure that the program is known
// in our program index.
if (!in_array($program, $programs)) {
die("no hax, pl0x");
}
try {
$pid = file_get_contents($PIDFILE);
posix_kill($pid, SIGKILL);
// Let ledcat timeout so it clears its input buffers.
sleep(1);
} catch (Exception $ex) { }
$pid = shell_exec("$PROGDIR/$program > $FIFO & echo $!");
file_put_contents($PIDFILE, $pid);
echo "Started $program";
}
?>
<form method="post">
<?php
foreach ($programs as $program) {
echo "<input type=\"radio\" name=\"program\" value=\"$program\">$program<br>\n";
}
?>
<input type="submit" value="Ok" />
</form>