2019-10-18 00:54:11 +02:00
|
|
|
<?php
|
|
|
|
|
2019-10-23 23:50:06 +02:00
|
|
|
function send_response($code, $message) {
|
|
|
|
http_response_code($code);
|
|
|
|
echo $message;
|
|
|
|
exit();
|
2019-10-18 00:54:11 +02:00
|
|
|
}
|
|
|
|
|
2019-10-23 23:50:06 +02:00
|
|
|
$request_url = filter_var($_SERVER["PHP_SELF"], FILTER_SANITIZE_URL);
|
2019-10-18 00:54:11 +02:00
|
|
|
|
2019-10-23 23:50:06 +02:00
|
|
|
if ($request_url === "/") {
|
|
|
|
send_response(200, "
|
2019-12-01 10:31:31 +01:00
|
|
|
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
|
2019-10-23 23:50:06 +02:00
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>Codeberg Pages</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
2019-10-24 00:13:31 +02:00
|
|
|
<div style='height: 100%; display: flex; align-items: center; justify-content: center;'>
|
|
|
|
<center>
|
|
|
|
<h1>Codeberg Pages. Static Pages for your Projects.</h1>
|
|
|
|
<p>Create a repo named 'pages' in your user account or org, push static content, HTML, style, fonts, images.</p>
|
|
|
|
<p>Share your rendered content via: <pre>https://" . $_SERVER["HTTP_HOST"] . "/<username>/</pre></p>
|
|
|
|
<p>Welcome to <a href='https://codeberg.org'>Codeberg.org</a>!</p>
|
|
|
|
</center>
|
|
|
|
</div>
|
2019-10-23 23:50:06 +02:00
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
");
|
2019-10-18 00:54:11 +02:00
|
|
|
}
|
|
|
|
|
2019-10-23 23:50:06 +02:00
|
|
|
if (preg_match("/^\/[a-zA-Z0-9_ +\-\/\.]+\$/", $request_url) != 1) {
|
|
|
|
send_response(404, "invalid request URL");
|
2019-10-18 00:54:11 +02:00
|
|
|
}
|
|
|
|
|
2019-10-23 23:50:06 +02:00
|
|
|
$git_prefix = "/data/git/gitea-repositories";
|
2019-10-18 00:54:11 +02:00
|
|
|
$parts = explode("/", $request_url);
|
|
|
|
array_shift($parts); # remove empty first
|
2019-10-18 09:48:43 +02:00
|
|
|
$owner = strtolower(array_shift($parts));
|
2019-10-23 23:50:06 +02:00
|
|
|
$git_root = realpath("$git_prefix/$owner/pages.git");
|
|
|
|
|
|
|
|
if (substr($git_root, 0, strlen($git_prefix)) !== $git_prefix) {
|
|
|
|
send_response(404, "this user/organization does not have codeberg pages");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (end($parts) === '') {
|
|
|
|
array_shift($parts);
|
|
|
|
}
|
2019-10-18 00:54:11 +02:00
|
|
|
|
2019-10-23 23:50:06 +02:00
|
|
|
if (sizeof($parts) === 0 || strpos(end($parts), ".") === false) {
|
|
|
|
$h = "Location: https://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
|
|
|
|
if (substr($h, -1) != "/") {
|
|
|
|
$h .= "/";
|
|
|
|
}
|
|
|
|
$h .= "index.html";
|
|
|
|
header($h);
|
|
|
|
exit();
|
2019-10-18 00:54:11 +02:00
|
|
|
}
|
|
|
|
|
2019-10-23 23:50:06 +02:00
|
|
|
$file_url = implode("/", $parts);
|
|
|
|
|
2019-10-18 00:54:11 +02:00
|
|
|
$command = "sh -c \"cd '$git_root' && /usr/bin/git show 'master:$file_url'\"";
|
|
|
|
|
2019-10-23 23:50:06 +02:00
|
|
|
## We are executing command twice (first for send_response-checking, then for actual raw output to stream),
|
2019-10-18 00:54:11 +02:00
|
|
|
## which seems wasteful, but it seems exec+echo cannot do raw binary output? Is this true?
|
|
|
|
exec($command, $output, $retval);
|
|
|
|
if ($retval != 0) {
|
2019-10-23 23:50:06 +02:00
|
|
|
send_response(404 , "no such file in repo: '" . htmlspecialchars($file_url) . "'");
|
2019-10-18 00:54:11 +02:00
|
|
|
}
|
|
|
|
|
2019-10-23 23:50:06 +02:00
|
|
|
$mime_types = array(
|
|
|
|
"svg" => "image/svg+xml",
|
|
|
|
"png" => "image/png",
|
|
|
|
"jpg" => "image/jpeg",
|
|
|
|
"jpeg" => "image/jpeg",
|
|
|
|
"gif" => "image/gif",
|
|
|
|
"js" => "application/javascript",
|
|
|
|
"html" => "text/html",
|
|
|
|
"css" => "text/css",
|
|
|
|
"ico" => "image/x-icon"
|
|
|
|
);
|
|
|
|
|
2019-10-18 00:54:11 +02:00
|
|
|
$ext = pathinfo($file_url, PATHINFO_EXTENSION);
|
2019-10-18 09:48:43 +02:00
|
|
|
$ext = strtolower($ext);
|
|
|
|
|
2019-10-23 23:50:06 +02:00
|
|
|
if (array_key_exists($ext, $mime_types)) {
|
|
|
|
$mime_type = $mime_types[$ext];
|
|
|
|
} else {
|
|
|
|
$mime_type = "text/plain";
|
2019-10-18 00:54:11 +02:00
|
|
|
}
|
|
|
|
|
2019-10-23 23:50:06 +02:00
|
|
|
header("Content-Type: " . $mime_type);
|
|
|
|
|
2019-10-18 00:54:11 +02:00
|
|
|
## If we could directly implode+echo raw output from above, we wouldn't need to execute command twice:
|
|
|
|
passthru($command);
|
|
|
|
|
|
|
|
?>
|
|
|
|
|