X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=index.php;h=9bdeceaaccb438c086de2c55882072f6dbe43125;hb=78040801011740393dc2059516a98a8381beeab6;hp=160fed53b3418e5a0a30fb4f80e4e594c5fbf86d;hpb=277cfe1f0e09b05e6615ce87603672e0f305520a;p=mdref%2Fmdref diff --git a/index.php b/index.php index 160fed5..9bdecea 100644 --- a/index.php +++ b/index.php @@ -2,8 +2,6 @@ error_reporting(E_ALL &~ E_DEPRECATED); -define("OUTPUT", fopen("php://memory", "w+")); - function cut(array $lines, array $specs) { $delim = "[[:space:]]+"; $bytes = []; @@ -56,111 +54,235 @@ function cut(array $lines, array $specs) { function head($file, $lines = 1) { $ld = []; - if (($fd = fopen($file, "r"))) { + if (is_resource($file) || ($file = fopen($file, "r"))) { while ($lines--) { - $ld[] = fgets($fd); + $ld[] = fgets($file); } } return $ld; } -function ns($file) { +function ns($path) { + $ns = ""; + $parts = explode("/", $path); + $upper = ctype_upper($path[0]); + for ($i = 0; $i < count($parts); ++$i) { + if (!strlen($parts[$i]) || $parts[$i] === ".") { + continue; + } + if (strlen($ns)) { + if ($upper && !ctype_upper($parts[$i][0])) { + $ns .= "::"; + } else { + $ns .= "\\"; + } + } + $ns .= $parts[$i]; + $upper = ctype_upper($parts[$i][0]); + } + return $ns; return str_replace("/", "\\", str_replace("//", "/", trim($file, "/."))); } function urlpath($dir, $file) { - return (strlen($dir) ? $dir . "/" : "") . urlencode($file); + return (strlen($dir) ? $dir . "/" : "") . basename($file, ".md"); } -function ls($dir, $invert = false) { - fprintf(OUTPUT, "
".join(" ", cut(head("./$dir/$file"), ["f"=>"1-"]))."
"; + + $path = "$dir/$file"; + + if (is_file($path)) { + $pi = pathinfo($path); + /* ignore files not ending in .md */ + if (!isset($pi["extension"]) || $pi["extension"] != "md") { + continue; + } + /* ignore files where an accompanying directory exists */ + if (is_dir("$dir/".$pi["filename"])) { + continue; + } + } else { + /* ignore directories where no accompanying file exists */ + if (!is_file("$path.md")) { + continue; + } + } + + printf("%s
%s
%s
%s
%s\n", htmlspecialchars(file_get_contents($file))); + break; + } + } else { + $res->setResponseCode(404); + printf("
", dirname($file), basename($file, ".md")); + } } +chdir(__DIR__); +$t = ["css"=>"text/css", "js"=>"application/javascript"]; $r = new http\Env\Request; $u = new http\Url($r->getRequestUrl()); -$t = ["css"=>"text/css", "js"=>"application/javascript"]; +$s = new http\Env\Response; +$b = dirname($_SERVER["SCRIPT_NAME"]); +$p = ".". substr($u->path, strlen($b)); -switch($u->path) { -case "/index.js": -case "/index.css": - $s = new http\Env\Response; - $s->setHeader("Content-type", $t[pathinfo($u->path, PATHINFO_EXTENSION)]); - $s->setBody(new http\Message\Body(fopen(basename($u->path), "r"))); +switch($p) { +case "./index.php": + exit; +case "./index.js": +case "./index.css": + $s->setHeader("Content-type", $t[pathinfo($p, PATHINFO_EXTENSION)]); + $s->setBody(new http\Message\Body(fopen($p, "r"))); $s->send(); exit; } -if (is_dir(".".$u->path)) { - ls(".".$u->path); -} else { - md(".".$u->path); -} +ob_start($s); ?> - -Sorry, I could not find
%s/%s
.
v
+ + + + + + + + + + + + +send();