mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-01-19 08:57:55 +00:00
var/www/pages/index.php : work around browser issues with relative links and directory URIs not ending on '/'
This commit is contained in:
parent
1b10dec06f
commit
ee47caf42d
1 changed files with 11 additions and 8 deletions
|
@ -36,26 +36,29 @@ if (preg_match("/^\/[a-zA-Z0-9_ +\-\/\.]+\$/", $request_url) != 1) {
|
||||||
|
|
||||||
$git_prefix = "/data/git/gitea-repositories";
|
$git_prefix = "/data/git/gitea-repositories";
|
||||||
$parts = explode("/", $request_url);
|
$parts = explode("/", $request_url);
|
||||||
|
$parts = array_diff($parts, array("")); # Remove empty parts in URL
|
||||||
# Remove empty first, potentially empty parts between //. If URL ends on "/", last entry in array is empty too. Remove it:
|
|
||||||
$parts = array_diff($parts, array(""));
|
|
||||||
|
|
||||||
$owner = strtolower(array_shift($parts));
|
$owner = strtolower(array_shift($parts));
|
||||||
$git_root = realpath("$git_prefix/$owner/pages.git");
|
$git_root = realpath("$git_prefix/$owner/pages.git");
|
||||||
|
$file_url = implode("/", $parts);
|
||||||
|
|
||||||
# Ensure that only files within the user's pages repository are accessed:
|
# Ensure that only files within the user's pages repository are accessed:
|
||||||
if (substr($git_root, 0, strlen($git_prefix)) !== $git_prefix) {
|
if (substr($git_root, 0, strlen($git_prefix)) !== $git_prefix) {
|
||||||
send_response(404, "this user/organization does not have codeberg pages");
|
send_response(404, "this user/organization does not have codeberg pages");
|
||||||
}
|
}
|
||||||
|
|
||||||
$file_url = implode("/", $parts);
|
# If this is a folder, we explicitly redirect to folder URL, otherwise browsers will construct invalid relative links:
|
||||||
|
|
||||||
# If this is a folder or just empty, we add 'index.html' to the URL:
|
|
||||||
$command = "sh -c \"cd '$git_root' && /usr/bin/git ls-tree 'master:$file_url' > /dev/null\"";
|
$command = "sh -c \"cd '$git_root' && /usr/bin/git ls-tree 'master:$file_url' > /dev/null\"";
|
||||||
exec($command, $output, $retval);
|
exec($command, $output, $retval);
|
||||||
if ($retval === 0) {
|
if ($retval === 0) {
|
||||||
|
if (substr($request_url, -1) !== "/") {
|
||||||
|
$h = "Location: https://" . $_SERVER["HTTP_HOST"] . "/" . $owner . "/" . $file_url . "/";
|
||||||
|
if ($_SERVER['QUERY_STRING'] !== "")
|
||||||
|
$h .= "?" . $_SERVER['QUERY_STRING'];
|
||||||
|
header($h);
|
||||||
|
exit();
|
||||||
|
}
|
||||||
if ($file_url !== "")
|
if ($file_url !== "")
|
||||||
$file_url .= "/";
|
$file_url .= "/";
|
||||||
$file_url .= "index.html";
|
$file_url .= "index.html";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue