aquinas/router.php

271 lines
5.7 KiB
PHP

<?php
require "parsedown.php";
$tryuri = ltrim("$_SERVER[REQUEST_URI]", '/');
$tryuri = strtok($tryuri, "?");
if (page_locked($tryuri)) {
load_page("404");
}
if (file_exists("site.json")) {
$site = json_decode(file_get_contents("site.json"), true);
} else {
echo "Need a site.json file to work!";
return false;
}
if (preg_match('/\.(?:jpg|png|webp|css|ico|js|ttf|woff|txt)/', $_SERVER["REQUEST_URI"])) {
return false;
}
load_page($tryuri);
/* HELPER FUNCTIONS START */
function get_path_content($path) {
$fs = scandir($path);
$titles;
for ($i = 2; $i < count($fs); $i++) {
$f = "$path/$fs[$i]";
if (!is_dir($f)) {
if (pathinfo($f, PATHINFO_EXTENSION) == "json") {
$titles[] = ["c" => json_decode(file_get_contents($f), true), "link" => uri_from_file($f)];
}
} else {
$fy = "$f/index.json";
if (file_exists($fy) && !page_locked($fy)) $titles[] = ["c" => json_decode(file_get_contents($fy), true), "link" => uri_from_file($fy)];
}
}
return $titles;
}
function uri_from_file($file) {
$name = substr($file, strrpos($file, '/')+1);
if ($name == "index.json") {
return substr($file, 0, -strlen($name)-1);
}
if (!strcmp(substr($file, strrpos($file, '.')+1), "json")) {
return substr($file, 0, -5);
}
}
function load_page($tryuri) {
global $site;
$mainphp = "";
$jsonf = "";
$parsef = "";
/* Looking for 3 things: a base, a content, and a template */
$tryf = "$tryuri/index";
$tryf = ltrim($tryf, '/');
if (file_exists("$tryf.json")) {
$jsonf = "$tryf.json";
} else if (file_exists("$tryuri.json")) {
$jsonf = "$tryuri.json";
}
if (file_exists("$tryf.php")) {
$mainphp = "$tryf.php";
}
else if (file_exists("$tryuri.php")) {
$mainphp = "$tryuri.php";
}
if (file_exists("$tryf.md")) {
$parsef = "$tryf.md";
} else if (file_exists("$tryuri.md")) {
$parsef = "$tryuri.md";
}
if (empty($mainphp) && empty($jsonf) && empty($parsef)) {
load_page("404");
return;
}
if (empty($mainphp)) {
while ($tryuri != "") {
if (file_exists("$tryuri/temp.php")) {
$mainphp = "$tryuri/temp.php";
goto endmain;
}
$tryuri = substr($tryuri, 0, strrpos($tryuri, '/'));
}
$mainphp = "temp.php";
}
endmain:
if (!empty($jsonf)) {
$file = file_get_contents($jsonf);
$obj = json_decode($file);
}
if (!empty($parsef)) {
$pd = new Parsedown();
$parse = $pd->text(file_get_contents($parsef));
}
$page['title'] = $obj->title ?? $site['title'];
$page['desc'] = $obj->desc ?? $site['desc'];
include "base.php";
$tt = microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'];
echo <<<END
<script>
console.log("Generated page in " + $tt.toFixed(5) + " seconds.");
</script>
END;
die();
}
function img_src($src, $root = "" ) {
if (empty($root)) $root = $_SERVER['REQUEST_URI'];
if ($root == "/") $root = "";
if (substr($root, -1) == '/') $root = substr($root, 0, -1);
if ($src[0] != '/') {
$write = "$root/$src";
} else $write = $src;
return $write;
}
function page_locked($page) {
if (file_exists("$page.lock" ))
return true;
while ($page != "" && is_dir($page)) {
$files = scandir($page);
if (!$files)
goto CONT;
for ($i = 2; $i < count($files); $i++) {
if ($files[$i] == "lock") {
return true;
}
}
CONT:
$page = substr($page, 0, strrpos($page, '/'));
}
return false;
}
function p_img($src, $opts, $ext) {
$src = ltrim($src, '/');
$p = pathinfo($src);
$o = str_replace([" ", "-"], "", $opts);
if ($o == "") $o = "gen";
return "/gen/$p[dirname]/$p[filename].$o.$ext";
}
/* Given a source image and options, creates the appropriate images
* jpeg, webp, and avif for lossy, and png and lossless webp for lossless
*/
function gen_images($src, $opts = "") {
$psrc = ltrim($src, '/');
$path = pathinfo($psrc);
if (in_array($path['extension'], ["jpg", "jpeg"])) {
$je = ltrim(p_img($src, $opts, "jpg"), '/');
$jpath = pathinfo($je);
$genp = $jpath['dirname'];
`mkdir -p $genp`;
`convert -quality 80 $opts $psrc $je`;
$we = ltrim(p_img($src, $opts, "webp"), '/');
if (!file_exists($we)) {
`convert -quality 80 $opts $psrc $we`;
}
$ae = ltrim(p_img($src, $opts, "avif"), '/');
if (!file_exists($ae)) {
`convert -quality 80 $opts $psrc $ae`;
}
}
if ($path['extension'] == "png") {
$we = ltrim(p_img($src, $opts, "webp"), '/');
$wpath = pathinfo($we);
$genp = $wpath['dirname'];
`mkdir -p $genp`;
if (!file_exists($we)) {
`convert -quality 80 -define webp:lossless=true $psrc $we`;
}
}
}
function make_img($src, $opts="", $q=80) {
$p = img_src($src);
gen_images($p, $opts);
if (pathinfo($p)['extension'] == "png") {
$pe = $p;
$we = p_img($p, $opts, "webp");
echo <<<END
<picture>
<source srcset="$we" type="image/webp" />
<source srcset="$pe" type="image/png" />
<img src="$pe" />
</picture>
END;
} else {
$we = p_img($p, $opts, "webp");
$je = p_img($p, $opts, "jpg");
echo <<<END
<picture>
<source srcset="$we" type="image/webp" />
<source srcset="$je" type="image/jpeg" />
<img src="$je" />
</picture>
END;
}
}
function make_bkgd_img($src, $opts = "", $q=80) {
$p = img_src($src);
gen_images($p, $opts);
$we = p_img($p, $opts, "webp");
$je = p_img($p, $opts, "jpg");
return "background-image: url($je); background-image: -webkit-image-set(url($we) 1x, url($je) 1x)";
}
?>