ryogit/index.php

77 行
2.5 KiB
PHP

<?php
// Define shit.
define("YOMAMA", "ryo");
define("PDIR", "/home/".YOMAMA."/dev");
// Prevent accessing stuff you're not supposed to access.
$path = htmlentities((isset($_GET["path"]) ? $_GET["path"] : ""));
if ($path == "/") $path = "";
foreach (explode("/", $path) as $e) { if ($e == "." || $e == ".." || $e == ".git") die("fuck off."); }
// Show URL based on where you're browsing from.
$url = (str_contains($_SERVER['HTTP_HOST'], ".i2p") ? "http://ryocafe.i2p" : (str_contains($_SERVER["HTTP_HOST"], ".onion") ? "http://asc7ewkcvat2wsoi5yuwkej5ukyrqqnpnzpj4u34r2jxnoxhnbx6yqad.onion" : "https://ryocafe.site"));
// Get the Git info and whether it's a Git repo at all.
$cgp = PDIR;
$proot = "";
foreach (explode("/", $path) as $cg) {
$cgp .= $cg."/";
if (file_exists($cgp.".git")) $proot = $cgp;
}
unset($cgp);
// Functions
function git_branch (string $fp) : string {
$res = explode("/", str_replace("ref: ", "", file_get_contents($fp.".git/HEAD")));
return trim($res[array_key_last($res)]);
}
function ls (string $fp = "") : array|bool {
return array_diff(scandir(PDIR.$fp), ["..", ".", ".git"]);
}
function cat (string $fp) : string {
if (!isset($fp)) die("Missing filename.");
return str_starts_with(mime_content_type(PDIR."/".$fp), "text/") ? htmlspecialchars(file_get_contents(PDIR."/".$fp)) : "";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja">
<head>
<meta content="text/html; charset=utf-8" http-equiv="content-type" />
<title><?= YOMAMA ?>'s git repo</title>
<link rel="stylesheet" type="text/css" href="/style.css" />
</head>
<body>
<p>Made by <a href="<?= $url ?>">寮</a>.</p>
<h1>
<?php
unset($url);
$ex = explode("/", $path);
$ph = "";
foreach ($ex as $i => $pt) {
if ($i != 0) $ph .= "/".$pt;
else echo "<a href=\"/\">root</a>";
echo ($i != array_key_last($ex) ? "<a href=\"/?path=".$ph."\">".$pt."</a>" : $pt);
if ($i != array_key_last($ex)) echo "/";
}
unset($ex);
unset($ph);
?>
</h1>
<?php
if ($proot != "") echo "<h2>Branch: ".git_branch($proot)."</h2>";
unset($proot);
if (!is_dir(PDIR.$path)) {
echo "<pre>".cat($path)."</pre>";
}
else {
echo "<ul>";
foreach (ls($path."/") as $p) echo "<li><a href=\"/?path=".$path."/".$p."\">".$p."</a></li>";
echo "</ul>";
}
unset($path);
?>
</body>
</html>