var/www/pages/index.php : properly handle directories, pipe unused output to /dev/null

This commit is contained in:
codeberg.org 2020-05-04 21:14:53 +02:00
parent 972c0879c3
commit 981c0dd904

View File

@ -44,13 +44,19 @@ if (substr($git_root, 0, strlen($git_prefix)) !== $git_prefix) {
} }
if (end($parts) === '') { if (end($parts) === '') {
array_shift($parts); array_pop($parts);
} }
$file_url = implode("/", $parts); $file_url = implode("/", $parts);
if ($file_url === "") { $command = "sh -c \"cd '$git_root' && /usr/bin/git ls-tree 'master:$file_url' > /dev/null\"";
$file_url = "index.html"; exec($command, $output, $retval);
if ($retval == 0) {
if ($file_url == "" ) {
$file_url = "index.html";
} else {
$file_url .= "/index.html";
}
} }
$ext = pathinfo($file_url, PATHINFO_EXTENSION); $ext = pathinfo($file_url, PATHINFO_EXTENSION);
@ -83,20 +89,15 @@ $command = "sh -c \"cd '$git_root' && /usr/bin/git show 'master:$file_url'\"";
## We are executing command twice (first for send_response-checking, then for actual raw output to stream), ## We are executing command twice (first for send_response-checking, then for actual raw output to stream),
## which seems wasteful, but it seems exec+echo cannot do raw binary output? Is this true? ## which seems wasteful, but it seems exec+echo cannot do raw binary output? Is this true?
exec($command, $output, $retval); exec($command . " > /dev/null", $output, $retval);
if ($retval != 0) { if ($retval != 0) {
# Try to append index.html to path, in case it was an abbreviated folder. Whatever happens from now on, we will always render HTML: # Render user-provided 404.html if exists, generic 404 message if not:
http_response_code(404);
header("Content-Type: text/html"); header("Content-Type: text/html");
$command = "sh -c \"cd '$git_root' && /usr/bin/git show 'master:$file_url/index.html'\""; $command = "sh -c \"cd '$git_root' && /usr/bin/git show 'master:404.html'\"";
exec($command, $output, $retval); exec($command . " > /dev/null", $output, $retval);
if ($retval != 0) { if ($retval != 0) {
# Render user-provided 404.html if exists, generic 404 message if not: send_response(404 , "no such file in repo: '" . htmlspecialchars($file_url) . "'");
$command = "sh -c \"cd '$git_root' && /usr/bin/git show 'master:404.html'\"";
exec($command, $output, $retval);
if ($retval != 0) {
send_response(404 , "no such file in repo: '" . htmlspecialchars($file_url) . "'");
}
http_response_code(404);
} }
} }