mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-01-19 08:57:55 +00:00
pages [needs review]
This commit is contained in:
commit
3ea456d4d7
1 changed files with 58 additions and 0 deletions
58
var/www/pages/index.php
Normal file
58
var/www/pages/index.php
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
function error($code, $message) {
|
||||||
|
http_response_code(code);
|
||||||
|
echo("$code : $message");
|
||||||
|
die();
|
||||||
|
}
|
||||||
|
|
||||||
|
$request_url = $_SERVER["PHP_SELF"];
|
||||||
|
|
||||||
|
if (substr($request_url, -1) == "/") {
|
||||||
|
$request_url .= "index.html";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (preg_match("/\/[a-zA-Z0-9_ +\-\/\.]+\z/", $request_url) != 1) {
|
||||||
|
error(404, "invalid request URL '$request_url'");
|
||||||
|
}
|
||||||
|
|
||||||
|
$parts = explode("/", $request_url);
|
||||||
|
array_shift($parts); # remove empty first
|
||||||
|
|
||||||
|
$git_root = "/data/git/gitea-repositories/" . array_shift($parts) . "/pages.git";
|
||||||
|
|
||||||
|
$file_url = implode("/", $parts);
|
||||||
|
|
||||||
|
if (!is_dir($git_root)) {
|
||||||
|
error(404, "this user/organization does not have codeberg pages");
|
||||||
|
}
|
||||||
|
|
||||||
|
$command = "sh -c \"cd '$git_root' && /usr/bin/git show 'master:$file_url'\"";
|
||||||
|
|
||||||
|
## We are executing command twice (first for error-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);
|
||||||
|
if ($retval != 0) {
|
||||||
|
error(404 , "no such file in repo: '$file_url'");
|
||||||
|
}
|
||||||
|
|
||||||
|
$ext = pathinfo($file_url, PATHINFO_EXTENSION);
|
||||||
|
if ($ext == "svg") {
|
||||||
|
header("Content-Type: image/svg+xml");
|
||||||
|
} elseif ($ext == "jpg") {
|
||||||
|
header("Content-Type: image/jpeg");
|
||||||
|
} elseif ($ext == "png") {
|
||||||
|
header("Content-Type: image/png");
|
||||||
|
} elseif ($ext == "gif") {
|
||||||
|
header("Content-Type: image/gif");
|
||||||
|
} elseif ($ext == "js") {
|
||||||
|
header("Content-Type: application/javascript");
|
||||||
|
} elseif ($ext == "css") {
|
||||||
|
header("Content-Type: text/css");
|
||||||
|
}
|
||||||
|
|
||||||
|
## If we could directly implode+echo raw output from above, we wouldn't need to execute command twice:
|
||||||
|
passthru($command);
|
||||||
|
|
||||||
|
?>
|
||||||
|
|
Loading…
Reference in a new issue