Files
elwig-www/www/clients.php
2023-11-25 20:09:22 +01:00

139 lines
4.4 KiB
PHP

<?php
require "format.inc";
$format = get_fmt();
$cred_file_name = 'credentials.txt';
$clients = [];
$names = [];
$passwords = [];
foreach (scandir('.clients/') as $file) {
if ($file === '.' || $file === '..') continue;
array_push($clients, $file);
$content = file_get_contents(".clients/$file/$cred_file_name");
if ($content) {
$creds = explode(":", explode("\n", $content)[0]);
$names[$file] = $creds[0];
$passwords[$file] = $creds[1];
}
}
$path = $_SERVER['PATH_INFO'];
if ($path == '') {
if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
header('Status: 405');
header('Allow: GET');
if ($format === 'text') {
header('Content-Type: text/plain; charset=UTF-8');
echo "405 Method Not Allowed :(\n";
} else if ($format === 'json') {
header('Content-Type: application/json; charset=UTF-8');
echo "{\"status\": \"error\", \"errors\": [{\"message\": \"Method not allowed\"}]}\n";
} else {
header('Content-Type: text/html; charset=UTF-8');
header('Content-Length: 0');
}
exit();
}
if ($format === 'text' || $format === 'html') {
header('Content-Type: text/plain; charset=UTF-8');
foreach ($clients as $c)
echo "$c\n";
} else if ($format === 'json') {
header('Content-Type: application/json; charset=UTF-8');
echo "{\"status\": \"success\", \"data\": [";
$first = true;
foreach ($clients as $c) {
if (!$first) echo ",";
echo "\n {\"name\": \"$c\"}";
$first = false;
}
echo "\n]}\n";
} else if ($format === 'html') {
header('Content-Type: text/html; charset=UTF-8');
// TODO
}
exit();
}
foreach ($clients as $c) {
if ($path !== "/$c" && !str_starts_with($path, "/$c/"))
continue;
header('Content-Type: text/plain; charset=UTF-8');
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) || $_SERVER['PHP_AUTH_USER'] !== $names[$c] || $_SERVER['PHP_AUTH_PW'] !== $passwords[$c]) {
header('Status: 401');
header('WWW-Authenticate: Basic realm="Elwig"');
exit("401 Unauthorized :(\n");
} elseif ($path === "/$c") {
header("Location: $c/");
header('Status: 303');
exit("303 See Other :)\n");
} elseif ($path === "/$c/") {
if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
header("Status: 405");
header("Allow: GET");
exit("405 Method Not Allowed :(\n");
}
system("ls -Al .clients/$c/");
exit();
}
$file = substr($path, strlen("/$c/"));
$path = ".clients/$c/$file";
if (str_contains($file, '/')) {
header("Status: 400");
exit("400 Bad Request :(\n");
} elseif ($_SERVER['REQUEST_METHOD'] === 'GET') {
$size = filesize($path);
if ($size === false) {
header("Status: 404");
exit("404 Not Found :(\n");
}
$type = mime_content_type($path);
header("Content-Type: $type");
header("Content-Disposition: attachment; filename=\"$file\"");
header("Content-Length: $size");
readfile($path);
} elseif ($_SERVER['REQUEST_METHOD'] === 'PUT') {
$putdata = fopen('php://input', 'r');
$fp = fopen($path, 'wb');
if ($fp === false) {
header("Status: 500");
exit("500 Internal Server Error :(\n");
}
while ($data = fread($putdata, 4096))
fwrite($fp, $data);
fclose($fp);
fclose($putdata);
header("Status: 201");
exit("201 Created :)\n");
} elseif ($_SERVER['REQUEST_METHOD'] === 'DELETE') {
if (unlink($path) === false) {
header("Status: 500");
exit("500 Internal Server Error :(\n");
}
exit("200 OK :)\n");
} else {
header("Status: 405");
header("Allow: GET, PUT, DELETE");
exit("405 Method Not Allowed :(\n");
}
exit();
}
header("Status: 404");
if ($format === 'text') {
header('Content-Type: text/plain; charset=UTF-8');
echo "404 Not Found :(\n";
} else if ($format === 'json') {
header('Content-Type: application/json; charset=UTF-8');
echo "{\"status\": \"error\", \"errors\": [{\"message\": \"Not found\"}]}\n";
} else {
header('Content-Type: text/html; charset=UTF-8');
header('Content-Length: 0');
}
exit();