diff options
-rw-r--r-- | lib/format.php | 33 | ||||
-rw-r--r-- | mirrors/status.php | 8 |
2 files changed, 35 insertions, 6 deletions
diff --git a/lib/format.php b/lib/format.php new file mode 100644 index 0000000..c250c8a --- /dev/null +++ b/lib/format.php @@ -0,0 +1,33 @@ +<?php + +# do not include twice +if (function_exists("export_as_requested")) + return; + +require_once "../init.php"; +include_once BASE . "/lib/http.php"; + +function export_as_requested($content) { + if (isset($_GET["json"])) { + header ("Content-type: application/json"); + print json_encode( + $content, + JSON_UNESCAPED_SLASHES + ); + } elseif (isset($_GET["tsv"])) { + header ("Content-type: text/tab-separated-values"); + if (! isset($_GET["no-headers"])) + print implode("\t",array_keys($content[0])) . "\n"; + print implode( + "\n", + array_map( + function($row){ + return implode("\t",$row); + }, + $content + ) + ); + } else { + throw_http_error(406,"Not Acceptable","Unknown output format."); + } +} diff --git a/mirrors/status.php b/mirrors/status.php index 79ca3ee..d9f3274 100644 --- a/mirrors/status.php +++ b/mirrors/status.php @@ -2,6 +2,7 @@ require_once "../init.php"; require_once BASE . "/lib/mysql.php"; +require_once BASE . "/lib/format.php"; $cutoff = 86400; @@ -78,9 +79,4 @@ $content = array( "urls" => $urls ); -if (isset($_GET["json"])) { - header ("Content-type: application/json"); - print json_encode($content,JSON_UNESCAPED_SLASHES); -} else { - print "Unknown output format."; -} +export_as_requested($content); |