307 lines
6.6 KiB
PHP
307 lines
6.6 KiB
PHP
<?php
|
|
|
|
/* Aquinas v. 0.1 */
|
|
|
|
require "parsedown.php";
|
|
|
|
$tryuri = ltrim("$_SERVER[REQUEST_URI]", '/');
|
|
$tryuri = strtok($tryuri, "?");
|
|
|
|
if (page_locked($tryuri)) {
|
|
load_page("404");
|
|
}
|
|
|
|
$site = file_get_contents("site.json");
|
|
if (!$site) {
|
|
echo "Need a site.json file to work!";
|
|
return false;
|
|
}
|
|
$site = json_decode($site, true);
|
|
|
|
$banned_ext = "php|json|md";
|
|
|
|
if (is_file($tryuri)) {
|
|
if (preg_match("/\.(?:$banned_ext)/", $tryuri))
|
|
load_page("404");
|
|
else
|
|
return false;
|
|
}
|
|
|
|
load_page($tryuri);
|
|
|
|
|
|
/* HELPER FUNCTIONS START */
|
|
/* Grab json from top */
|
|
$c = file_get_contents($parsef);
|
|
if ($c[0] == '{') {
|
|
$json = substr($c, 0, strpos($c, '}')+1);
|
|
$c = substr($c, strpos($c, '}')+1);
|
|
|
|
$obj = json_decode($json);
|
|
}
|
|
$parse = $pd->text($c);
|
|
|
|
function get_md_jsonhead($md) {
|
|
$c = file_get_contents($md);
|
|
|
|
if ($c[0] != '{') return false;
|
|
|
|
return substr($c, 0, strpos($c, '}')+1);
|
|
}
|
|
|
|
function get_path_content($path) {
|
|
$fs = scandir($path);
|
|
$titles;
|
|
|
|
for ($i = 2; $i < count($fs); $i++) {
|
|
$f = "$path/$fs[$i]";
|
|
if (!is_dir($f)) {
|
|
$ext = substr($f, strrpos($f, '.')+1);
|
|
if ($ext == "json") {
|
|
$titles[] = ["c" => json_decode(file_get_contents($f), true), "link" => uri_from_file($f)];
|
|
} else if ($ext == "md") {
|
|
$json = get_md_jsonhead($f);
|
|
|
|
if ($json) {
|
|
$titles[] = ["c" => json_decode($json, 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)];
|
|
}
|
|
}
|
|
|
|
return $titles;
|
|
}
|
|
|
|
function uri_from_file($file) {
|
|
$name = substr($file, strrpos($file, '/')+1);
|
|
if ($name == "index.json") {
|
|
return substr($file, 0, -strlen($name)-1);
|
|
}
|
|
|
|
return "/".substr($file, 0, strrpos($file, '.'));
|
|
}
|
|
|
|
function try_file($uri, $ext) {
|
|
$tryuri = ltrim($uri, '/');
|
|
$tryf = ltrim("$tryuri/index", '/');
|
|
|
|
if (is_file("$tryf.$ext")) return "$tryf.$ext";
|
|
else if (is_file("$tryuri.$ext")) return "$tryuri.$ext";
|
|
}
|
|
|
|
|
|
function load_page($tryuri) {
|
|
global $site;
|
|
|
|
$mainphp = "";
|
|
$prehp = "";
|
|
$jsonf = "";
|
|
$parsef = "";
|
|
|
|
/* Looking for 3 things: a base, a content, and a template */
|
|
|
|
$jsonf = try_file($tryuri, "json");
|
|
$mainphp = try_file($tryuri, "php");
|
|
$parsef = try_file($tryuri, "md");
|
|
$prehp = try_file($tryuri, "prehp");
|
|
|
|
if (empty($mainphp) && empty($jsonf) && empty($parsef) && empty($prehp)) {
|
|
load_page("404");
|
|
return;
|
|
}
|
|
|
|
if (empty($mainphp)) {
|
|
|
|
while ($tryuri != "") {
|
|
if (is_file("$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();
|
|
|
|
/* Grab json from top */
|
|
$c = file_get_contents($parsef);
|
|
if ($c[0] == '{') {
|
|
$json = substr($c, 0, strpos($c, '}')+1);
|
|
$c = substr($c, strpos($c, '}')+1);
|
|
|
|
$obj = json_decode($json);
|
|
}
|
|
$parse = $pd->text($c);
|
|
}
|
|
|
|
$page['title'] = $obj->title ?? $site['title'];
|
|
$page['desc'] = $obj->desc ?? $site['desc'];
|
|
|
|
if (!empty($prehp)) include $prehp;
|
|
|
|
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 (is_file("$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 = 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";
|
|
}
|
|
|
|
/* 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 = "", $lossy=0) {
|
|
$ext = substr($src, strrpos($src, '.')+1);
|
|
$psrc = ltrim($src, '/');
|
|
|
|
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`;
|
|
|
|
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`;
|
|
|
|
`convert -define webp:lossless=true $opts $psrc $we`;
|
|
|
|
return;
|
|
}
|
|
|
|
}
|
|
|
|
/* If lossy is true, makes lossless images lossy; does nothing for already lossy images */
|
|
function make_img($src, $opts="", $lossy=0) {
|
|
$p = img_src($src);
|
|
gen_images($p, $opts, $lossy);
|
|
|
|
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");
|
|
|
|
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 = $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)";
|
|
}
|
|
|
|
|
|
?>
|