ofborg/log-api/index.php

53 lines
1 KiB
PHP
Raw Normal View History

2018-01-27 02:55:08 +00:00
<?php
header('Content-Type: application/json');
$d = array('attempts' => []);
$root = "/var/lib/nginx/ofborg/";
function abrt($msg) {
echo $msg;
exit;
}
if (!is_dir($root)) {
abrt("root missing");
}
if (!isset($_SERVER['REQUEST_URI']) || empty($_SERVER['REQUEST_URI'])) {
abrt("uri missing");
}
2018-01-27 19:40:16 +00:00
$reqd = substr($_SERVER['REQUEST_URI'], strlen("/logs/"));
$req = realpath("$root/logs/$reqd");
$serve_root = "https://logs.nix.gsc.io/logfile/$reqd";
2018-01-27 02:55:08 +00:00
if ($req === false) {
abrt("absent");
}
if (strpos($req, $root) !== 0) {
abrt("bad path");
}
if (!is_dir($req)) {
abrt("non dir");
}
if ($handle = opendir($req)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
if (is_dir($req . '/' . $entry)) {
abrt("dir found");
}
if (is_file($req . '/' . $entry)) {
$d['attempts'][$entry] = [ "log_url" => "$serve_root/$entry" ];
}
}
}
closedir($handle);
}
echo json_encode($d);