diff --git a/var/www/pages/index.php b/var/www/pages/index.php index 5cf38b7..c804f77 100644 --- a/var/www/pages/index.php +++ b/var/www/pages/index.php @@ -44,13 +44,19 @@ if (substr($git_root, 0, strlen($git_prefix)) !== $git_prefix) { } if (end($parts) === '') { - array_shift($parts); + array_pop($parts); } $file_url = implode("/", $parts); -if ($file_url === "") { - $file_url = "index.html"; +$command = "sh -c \"cd '$git_root' && /usr/bin/git ls-tree 'master:$file_url' > /dev/null\""; +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); @@ -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), ## 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) { - # 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"); - $command = "sh -c \"cd '$git_root' && /usr/bin/git show 'master:$file_url/index.html'\""; - exec($command, $output, $retval); + $command = "sh -c \"cd '$git_root' && /usr/bin/git show 'master:404.html'\""; + exec($command . " > /dev/null", $output, $retval); if ($retval != 0) { - # Render user-provided 404.html if exists, generic 404 message if not: - $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); + send_response(404 , "no such file in repo: '" . htmlspecialchars($file_url) . "'"); } }