aquinas/router.php

277 lines
5.7 KiB
PHP
Raw Normal View History

2022-08-02 07:48:43 -05:00
<?php
2022-11-04 03:44:50 -05:00
2022-11-02 18:12:43 -05:00
require "parsedown.php";
2022-08-02 07:48:43 -05:00
2022-11-04 03:44:50 -05:00
$tryuri = ltrim("$_SERVER[REQUEST_URI]", '/');
$tryuri = strtok($tryuri, "?");
2022-11-04 17:55:34 -05:00
if (page_locked($tryuri)) {
load_page("404");
2022-11-04 03:44:50 -05:00
}
2022-11-03 12:55:29 -05:00
$site = file_get_contents("site.json");
if (!$site) {
2022-11-07 13:01:35 -06:00
echo "Need a site.json file to work!";
return false;
2022-11-02 18:12:43 -05:00
}
$site = json_decode($site, true);
2022-11-02 18:12:43 -05:00
if (preg_match('/\.(?:jpg|png|webp|css|ico|js|ttf|woff|txt)/', $_SERVER["REQUEST_URI"])) {
2022-11-02 18:12:43 -05:00
return false;
}
2022-11-04 03:44:50 -05:00
2022-11-02 18:12:43 -05:00
load_page($tryuri);
/* HELPER FUNCTIONS START */
2022-08-02 07:48:43 -05:00
2022-11-02 18:12:43 -05:00
function get_path_content($path) {
$fs = scandir($path);
$titles;
2022-08-02 07:48:43 -05:00
2022-11-02 18:12:43 -05:00
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 (is_file($fy) && !page_locked($fy)) $titles[] = ["c" => json_decode(file_get_contents($fy), true), "link" => uri_from_file($fy)];
2022-11-02 18:12:43 -05:00
}
}
2022-08-02 07:48:43 -05:00
2022-11-02 18:12:43 -05:00
return $titles;
2022-08-02 07:48:43 -05:00
}
2022-11-02 18:12:43 -05:00
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);
}
}
2022-08-02 07:48:43 -05:00
2022-11-02 18:12:43 -05:00
function load_page($tryuri) {
global $site;
2022-08-02 07:48:43 -05:00
$mainphp = "";
2022-11-02 18:12:43 -05:00
$jsonf = "";
$parsef = "";
2022-08-02 07:48:43 -05:00
2022-11-02 18:12:43 -05:00
/* Looking for 3 things: a base, a content, and a template */
$tryf = "$tryuri/index";
$tryf = ltrim($tryf, '/');
if (is_file("$tryf.json")) {
2022-11-02 18:12:43 -05:00
$jsonf = "$tryf.json";
} else if (is_file("$tryuri.json")) {
2022-11-02 18:12:43 -05:00
$jsonf = "$tryuri.json";
}
if (is_file("$tryf.php")) {
2022-11-02 18:12:43 -05:00
$mainphp = "$tryf.php";
}
else if (is_file("$tryuri.php")) {
2022-11-02 18:12:43 -05:00
$mainphp = "$tryuri.php";
2022-08-02 07:48:43 -05:00
}
if (is_file("$tryf.md")) {
2022-11-02 18:12:43 -05:00
$parsef = "$tryf.md";
} else if (is_file("$tryuri.md")) {
2022-11-02 18:12:43 -05:00
$parsef = "$tryuri.md";
}
2022-08-02 07:48:43 -05:00
2022-11-02 18:12:43 -05:00
if (empty($mainphp) && empty($jsonf) && empty($parsef)) {
load_page("404");
return;
}
2022-08-02 07:48:43 -05:00
2022-11-02 18:12:43 -05:00
if (empty($mainphp)) {
2022-08-02 07:48:43 -05:00
2022-11-02 18:12:43 -05:00
while ($tryuri != "") {
if (is_file("$tryuri/temp.php")) {
2022-11-02 18:12:43 -05:00
$mainphp = "$tryuri/temp.php";
goto endmain;
}
2022-08-02 07:48:43 -05:00
2022-11-02 18:12:43 -05:00
$tryuri = substr($tryuri, 0, strrpos($tryuri, '/'));
2022-08-02 07:48:43 -05:00
}
2022-11-02 18:12:43 -05:00
$mainphp = "temp.php";
}
2022-08-02 07:48:43 -05:00
2022-11-02 18:12:43 -05:00
endmain:
2022-08-02 07:48:43 -05:00
2022-11-02 18:12:43 -05:00
if (!empty($jsonf)) {
2022-08-02 07:48:43 -05:00
$file = file_get_contents($jsonf);
$obj = json_decode($file);
2022-11-02 18:12:43 -05:00
}
if (!empty($parsef)) {
$pd = new Parsedown();
$parse = $pd->text(file_get_contents($parsef));
}
2022-11-07 13:01:35 -06:00
$page['title'] = $obj->title ?? $site['title'];
$page['desc'] = $obj->desc ?? $site['desc'];
2022-08-02 07:48:43 -05:00
include "base.php";
2022-11-04 03:59:34 -05:00
$tt = microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'];
echo <<<END
<script>
console.log("Generated page in " + $tt.toFixed(5) + " seconds.");
</script>
END;
die();
2022-11-02 18:12:43 -05:00
}
function img_src($src, $root = "" ) {
if (empty($root)) $root = $_SERVER['REQUEST_URI'];
2022-11-07 16:07:29 -06:00
if ($root == "/") $root = "";
if (substr($root, -1) == '/') $root = substr($root, 0, -1);
2022-11-02 18:12:43 -05:00
if ($src[0] != '/') {
$write = "$root/$src";
} else $write = $src;
return $write;
}
2022-11-04 17:55:34 -05:00
function page_locked($page) {
if (is_file("$page.lock" ))
2022-11-04 17:55:34 -05:00
return true;
2022-11-05 18:06:34 -05:00
while ($page != "" && is_dir($page)) {
2022-11-04 17:55:34 -05:00
$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;
}
2022-11-04 03:59:34 -05:00
function p_img($src, $opts, $ext) {
$src = ltrim($src, '/');
$p = substr($src, 0, strrpos($src, '.'));
$o = str_replace([" ", "-"], "", $opts);
if ($o == "") $o = "gen";
return "/gen/$p.$o.$ext";
}
function sub_ext($src, $ext) {
$stem = substr($src, 0, strrpos($src, '.'));
return "$stem.$ext";
}
2022-11-07 16:07:29 -06:00
/* Given a source image and options, creates the appropriate images
* jpeg, webp, and avif for lossy, and png and lossless webp for lossless
*/
2022-11-08 16:59:19 -06:00
function gen_images($src, $opts = "", $lossy=0) {
$ext = substr($src, strrpos($src, '.')+1);
2022-11-07 16:07:29 -06:00
if ($lossy || in_array($ext, ["jpg", "jpeg"])) {
$je = ltrim(p_img($src, $opts, "jpg"), '/');
if (is_file($je)) return;
$gpath = substr($je, 0, strrpos($je, '/'));
`mkdir -p $gpath`;
`convert -quality 80 $opts $psrc $je`;
$we = sub_ext($je, "webp");
`convert -quality 80 $opts $psrc $we`;
$ae = sub_ext($je, "avif");
`convert -quality 80 $opts $psrc $ae`;
2022-11-08 16:59:19 -06:00
return;
}
if ($ext == "png") {
$we = ltrim(p_img($src, $opts, "webp"), '/');
if (is_file($we)) return;
$gpath = substr($we, 0, strrpos($we, '/'));
`mkdir -p $gpath`;
2022-11-07 16:07:29 -06:00
`convert -define webp:lossless=true $opts $psrc $we`;
2022-11-08 16:59:19 -06:00
return;
2022-11-07 16:07:29 -06:00
}
2022-11-07 16:07:29 -06:00
}
2022-11-08 16:59:19 -06:00
/* If lossy is true, makes lossless images lossy; does nothing for already lossy images */
function make_img($src, $opts="", $lossy=0) {
2022-11-07 16:07:29 -06:00
$p = img_src($src);
2022-11-08 16:59:19 -06:00
gen_images($p, $opts, $lossy);
2022-11-08 16:59:19 -06:00
if (!$lossy && 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");
2022-11-07 16:07:29 -06:00
echo <<<END
<picture>
<source srcset="$we" type="image/webp" />
<source srcset="$je" type="image/jpeg" />
<img src="$je" />
2022-11-07 16:07:29 -06:00
</picture>
END;
}
2022-11-07 16:07:29 -06:00
}
function make_bkgd_img($src, $opts = "", $q=80) {
2022-11-07 16:07:29 -06:00
$p = img_src($src);
gen_images($p, $opts);
2022-11-07 16:07:29 -06:00
$we = p_img($p, $opts, "webp");
$je = p_img($p, $opts, "jpg");
2022-11-07 16:07:29 -06:00
return "background-image: url($je); background-image: -webkit-image-set(url($we) 1x, url($je) 1x)";
2022-11-07 16:07:29 -06:00
}
2022-11-02 18:12:43 -05:00
?>
2022-08-02 07:48:43 -05:00