Try to unify API responses

This commit is contained in:
2024-07-21 16:10:02 +02:00
parent a01515aa9c
commit b76c434216
5 changed files with 59 additions and 38 deletions

View File

@ -1,12 +1,10 @@
<?php
require "../.php/auth.inc";
require "../.php/credentials.inc";
global $GITEA_TOKEN;
if ($_SERVER['PHP_AUTH_USER'] !== 'elwig' || $_SERVER['PHP_AUTH_PW'] !== 'ganzGeheim123!') {
header('Status: 401');
header('WWW-Authenticate: Basic realm="Elwig"');
header('Content-Type: text/plain');
exit("401 Unauthorized :(\n");
http_401_unauthorized();
}
$repo = "winzer/elwig-misc.git";
@ -15,11 +13,18 @@ $schema_version = $_GET['v'];
shell_exec("cd .repos; git clone $url; cd elwig-misc; git checkout main; git pull -f --rebase");
if ($schema_version) {
if (!$schema_version || !ctype_digit($schema_version)) {
header('Status: 400');
header('Content-Type: text/plain; charset=UTF-8');
header('Content-Length: 16');
exit("400 Bad Request\n");
} else {
$commit = shell_exec("cd .repos/elwig-misc; git log -p sql/v01/99.schema_version.sql | grep '+PRAGMA schema_version = ${schema_version}00;' -B 14 | head -n 1 | grep -oE '[^ ]+$'");
if (!$commit) {
header('Status: 404');
exit("404 Specified schema version not found :(\n");
header('Content-Type: text/plain; charset=UTF-8');
header('Content-Length: 39');
exit("404 Specified Schema Version Not Found\n");
}
shell_exec("cd .repos/elwig-misc; git checkout $commit");
}

View File

@ -8,9 +8,9 @@ if ($_SERVER['REQUEST_METHOD'] === 'PUT') {
$name = substr($_SERVER['PATH_INFO'], 1);
if (str_contains($name, "..") || str_contains($name, "/")) {
header('Status: 403');
header('Content-Type: text/plain');
header('Content-Length: 17');
exit("403 Forbidden :(\n");
header('Content-Type: text/plain; charset=UTF-8');
header('Content-Length: 14');
exit("403 Forbidden\n");
}
$upload = fopen("php://input", "r");
@ -19,16 +19,16 @@ if ($_SERVER['REQUEST_METHOD'] === 'PUT') {
fclose($fp);
fclose($upload);
header('Status: 200');
header('Content-Type: text/plain');
header('Content-Length: 10');
exit("200 OK :)\n");
header('Status: 201');
header('Content-Type: text/plain; charset=UTF-8');
header('Content-Length: 12');
exit("201 Created\n");
} else if ($_SERVER['REQUEST_METHOD'] !== 'GET' && $_SERVER['REQUEST_METHOD'] !== 'HEAD') {
header('Status: 405');
header('Allow: GET, HEAD, PUT');
header('Content-Type: text/plain');
header('Content-Length: 26');
exit("405 Method Not Allowed :(\n");
header('Content-Type: text/plain; charset=UTF-8');
header('Content-Length: 23');
exit("405 Method Not Allowed\n");
}
global $getProd;
@ -95,7 +95,7 @@ $entities = array_filter($entities, function($e) {
$format = get_fmt();
if ($format === 'json') {
header('Content-Type: application/json; charset=UTF-8');
echo "{\"status\": \"success\", \"data\": [\n";
echo "{\"data\": [\n";
$first = true;
foreach ($entities as $name => [$prod, $vers, $url, $size, $mtime, $ctime, $mod, $cre]) {
if (!$first) echo ",\n";
@ -112,6 +112,7 @@ if ($format === 'json') {
if (isset($getProd) && isset($getVers) && sizeof($entities) === 1) {
header('Status: 303');
header('Location: ' . $entities[array_key_first($entities)][2]);
header('Content-Length: 0');
exit();
}
header('Content-Type: application/xhtml+xml; charset=UTF-8');