2019-10-18 00:54:11 +02:00
|
|
|
<?php
|
|
|
|
|
2020-09-02 13:53:43 +02:00
|
|
|
function send_response($code, $message = "") {
|
2019-10-23 23:50:06 +02:00
|
|
|
http_response_code($code);
|
|
|
|
echo $message;
|
|
|
|
exit();
|
2019-10-18 00:54:11 +02:00
|
|
|
}
|
|
|
|
|
2020-08-31 00:23:31 +02:00
|
|
|
$request_uri = explode("?", $_SERVER["REQUEST_URI"])[0];
|
|
|
|
$request_url = filter_var($request_uri, FILTER_SANITIZE_URL);
|
|
|
|
$request_url = str_replace("%20", " ", $request_url);
|
2019-10-18 00:54:11 +02:00
|
|
|
|
2020-06-17 23:06:02 +02:00
|
|
|
if ($request_url === "/" and $_SERVER["HTTP_HOST"] === "codeberg.eu") {
|
2020-06-18 21:12:42 +02:00
|
|
|
send_response(200, file_get_contents("./default-page.html"));
|
2019-10-18 00:54:11 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 22:24:56 +02:00
|
|
|
# Restrict allowed characters in request URI:
|
2020-06-17 23:06:02 +02:00
|
|
|
if (preg_match("/^\/[a-zA-Z0-9_ +\-\/\.]*\$/", $request_url) != 1) {
|
2019-10-23 23:50:06 +02:00
|
|
|
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);
|
2020-05-04 23:43:52 +02:00
|
|
|
$parts = array_diff($parts, array("")); # Remove empty parts in URL
|
2020-06-17 23:06:02 +02:00
|
|
|
|
|
|
|
$parts_dot = explode(".",$_SERVER["HTTP_HOST"]);
|
2020-08-31 00:23:31 +02:00
|
|
|
if (count($parts_dot) != 3) {
|
2020-06-17 23:06:02 +02:00
|
|
|
send_response(404, "invalid subdomain");
|
|
|
|
}
|
|
|
|
$owner = $parts_dot[0];
|
|
|
|
|
2019-10-23 23:50:06 +02:00
|
|
|
$git_root = realpath("$git_prefix/$owner/pages.git");
|
2020-05-04 23:43:52 +02:00
|
|
|
$file_url = implode("/", $parts);
|
2019-10-23 23:50:06 +02:00
|
|
|
|
2020-05-04 23:59:54 +02:00
|
|
|
# Ensure that only files within $git_root are accessed:
|
2019-10-23 23:50:06 +02:00
|
|
|
if (substr($git_root, 0, strlen($git_prefix)) !== $git_prefix) {
|
|
|
|
send_response(404, "this user/organization does not have codeberg pages");
|
|
|
|
}
|
|
|
|
|
2020-05-04 23:43:52 +02:00
|
|
|
# If this is a folder, we explicitly redirect to folder URL, otherwise browsers will construct invalid relative links:
|
2020-06-18 13:17:48 +02:00
|
|
|
$command = "sh -c \"cd '$git_root' && /usr/bin/git ls-tree 'HEAD:$file_url' > /dev/null\"";
|
2020-05-04 21:14:53 +02:00
|
|
|
exec($command, $output, $retval);
|
2020-05-04 22:00:19 +02:00
|
|
|
if ($retval === 0) {
|
2020-05-04 23:43:52 +02:00
|
|
|
if (substr($request_url, -1) !== "/") {
|
2020-05-04 23:51:46 +02:00
|
|
|
$h = "Location: " . $request_url . "/";
|
|
|
|
if ($_SERVER['QUERY_STRING'] !== "")
|
|
|
|
$h .= "?" . $_SERVER['QUERY_STRING'];
|
2020-05-04 23:43:52 +02:00
|
|
|
header($h);
|
|
|
|
exit();
|
|
|
|
}
|
2020-05-04 22:13:36 +02:00
|
|
|
if ($file_url !== "")
|
2020-05-04 23:51:46 +02:00
|
|
|
$file_url .= "/";
|
2020-05-04 22:00:19 +02:00
|
|
|
$file_url .= "index.html";
|
2020-05-04 20:15:28 +02:00
|
|
|
}
|
|
|
|
|
2020-05-02 17:56:52 +02:00
|
|
|
$ext = pathinfo($file_url, PATHINFO_EXTENSION);
|
|
|
|
$ext = strtolower($ext);
|
2019-10-18 00:54:11 +02:00
|
|
|
|
2019-10-23 23:50:06 +02:00
|
|
|
$mime_types = array(
|
2020-05-04 22:00:19 +02:00
|
|
|
"css" => "text/css",
|
|
|
|
"csv" => "text/csv",
|
2019-10-23 23:50:06 +02:00
|
|
|
"gif" => "image/gif",
|
|
|
|
"html" => "text/html",
|
2019-12-21 10:07:13 +01:00
|
|
|
"ico" => "image/x-icon",
|
2020-05-04 22:00:19 +02:00
|
|
|
"ics" => "text/calendar",
|
|
|
|
"jpg" => "image/jpeg",
|
|
|
|
"jpeg" => "image/jpeg",
|
|
|
|
"js" => "application/javascript",
|
|
|
|
"json" => "application/json",
|
|
|
|
"pdf" => "application/pdf",
|
|
|
|
"png" => "image/png",
|
|
|
|
"svg" => "image/svg+xml",
|
|
|
|
"ttf" => "font/ttf",
|
|
|
|
"txt" => "text/plain",
|
2019-12-21 10:07:13 +01:00
|
|
|
"woff" => "font/woff",
|
|
|
|
"woff2" => "font/woff2",
|
2020-05-04 22:00:19 +02:00
|
|
|
"xml" => "text/xml"
|
2019-10-23 23:50:06 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
if (array_key_exists($ext, $mime_types)) {
|
2020-05-04 22:00:19 +02:00
|
|
|
header("Content-Type: " . $mime_types[$ext]);
|
2019-10-23 23:50:06 +02:00
|
|
|
} else {
|
2020-05-04 22:00:19 +02:00
|
|
|
header("Content-Type: application/octet-stream");
|
2019-10-18 00:54:11 +02:00
|
|
|
}
|
|
|
|
|
2020-09-02 13:53:43 +02:00
|
|
|
#header("Cache-Control: public, max-age=10, immutable");
|
|
|
|
|
|
|
|
$command = "sh -c \"cd '$git_root' && /usr/bin/git log --format='%H' -1\"";
|
|
|
|
exec($command, $output, $retval);
|
|
|
|
if ($retval == 0 && count($output)) {
|
|
|
|
$revision=$output[0];
|
|
|
|
header('ETag: "' . $revision . '"');
|
|
|
|
if (isset($_SERVER["HTTP_IF_NONE_MATCH"])) {
|
|
|
|
$req_revision = str_replace('"', '', str_replace('W/"', '', $_SERVER["HTTP_IF_NONE_MATCH"]));
|
|
|
|
if ($req_revision === $revision) {
|
|
|
|
send_response(304);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-02 11:55:36 +02:00
|
|
|
|
2020-05-02 17:56:52 +02:00
|
|
|
## 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?
|
2020-06-18 13:17:48 +02:00
|
|
|
$command = "sh -c \"cd '$git_root' && /usr/bin/git show 'HEAD:$file_url'\"";
|
2020-05-04 21:14:53 +02:00
|
|
|
exec($command . " > /dev/null", $output, $retval);
|
2020-05-02 17:56:52 +02:00
|
|
|
if ($retval != 0) {
|
2020-05-11 09:06:23 +02:00
|
|
|
# Try adding '.html' suffix, if this does not work either, report error
|
2020-06-18 13:17:48 +02:00
|
|
|
$command = "sh -c \"cd '$git_root' && /usr/bin/git show 'HEAD:$file_url.html'\"";
|
2020-05-04 21:14:53 +02:00
|
|
|
exec($command . " > /dev/null", $output, $retval);
|
2020-05-11 09:06:23 +02:00
|
|
|
header("Content-Type: text/html");
|
|
|
|
if ($retval != 0) {
|
|
|
|
# Render user-provided 404.html if exists, generic 404 message if not:
|
|
|
|
http_response_code(404);
|
2020-06-18 13:17:48 +02:00
|
|
|
$command = "sh -c \"cd '$git_root' && /usr/bin/git show 'HEAD:404.html'\"";
|
2020-05-11 09:06:23 +02:00
|
|
|
exec($command . " > /dev/null", $output, $retval);
|
|
|
|
if ($retval != 0)
|
|
|
|
send_response(404 , "no such file in repo: '" . htmlspecialchars($file_url) . "'");
|
|
|
|
}
|
2020-05-02 17:56:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
## If we could directly exec+echo raw output from above, we wouldn't need to execute command twice:
|
2019-10-18 00:54:11 +02:00
|
|
|
passthru($command);
|