Compare commits
No commits in common. "e04f1205bea1225c24a52ab08e3cb038726844e5" and "d3eb3720f73bef88ed4a821036bb0e1ba2f6e055" have entirely different histories.
e04f1205be
...
d3eb3720f7
1
jsmpeg.min.js
vendored
1
jsmpeg.min.js
vendored
File diff suppressed because one or more lines are too long
159
router.php
159
router.php
|
@ -1,59 +1,31 @@
|
|||
<?php
|
||||
|
||||
/* Aquinas v. 0.1 */
|
||||
|
||||
require "parsedown.php";
|
||||
|
||||
$tryuri = ltrim("$_SERVER[REQUEST_URI]", '/');
|
||||
$tryuri = strtok($tryuri, "?");
|
||||
|
||||
|
||||
$site = file_get_contents("site.json");
|
||||
if (!$site) {
|
||||
echo "Need a site.json file to work!";
|
||||
return false;
|
||||
}
|
||||
|
||||
$site = json_decode($site, true);
|
||||
|
||||
if (page_locked($tryuri)) {
|
||||
load_page("404");
|
||||
}
|
||||
|
||||
if ($tryuri == "router" || $tryuri == "parsedown") {
|
||||
load_page("404");
|
||||
}
|
||||
|
||||
$banned_ext = "php|json|md";
|
||||
|
||||
if (is_file($tryuri)) {
|
||||
if (preg_match("/\.(?:$banned_ext)/", $tryuri))
|
||||
load_page("404");
|
||||
else
|
||||
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 */
|
||||
/* 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);
|
||||
|
@ -62,19 +34,12 @@ function get_path_content($path) {
|
|||
for ($i = 2; $i < count($fs); $i++) {
|
||||
$f = "$path/$fs[$i]";
|
||||
if (!is_dir($f)) {
|
||||
$ext = substr($f, strrpos($f, '.')+1);
|
||||
if ($ext == "json") {
|
||||
if (pathinfo($f, PATHINFO_EXTENSION) == "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)];
|
||||
if (file_exists($fy) && !page_locked($fy)) $titles[] = ["c" => json_decode(file_get_contents($fy), true), "link" => uri_from_file($fy)];
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -87,34 +52,44 @@ function uri_from_file($file) {
|
|||
return substr($file, 0, -strlen($name)-1);
|
||||
}
|
||||
|
||||
return "/".substr($file, 0, strrpos($file, '.'));
|
||||
if (!strcmp(substr($file, strrpos($file, '.')+1), "json")) {
|
||||
|
||||
return substr($file, 0, -5);
|
||||
}
|
||||
|
||||
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");
|
||||
$tryf = "$tryuri/index";
|
||||
$tryf = ltrim($tryf, '/');
|
||||
|
||||
if (empty($mainphp) && empty($jsonf) && empty($parsef) && empty($prehp)) {
|
||||
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;
|
||||
}
|
||||
|
@ -122,7 +97,7 @@ if (empty($mainphp) && empty($jsonf) && empty($parsef) && empty($prehp)) {
|
|||
if (empty($mainphp)) {
|
||||
|
||||
while ($tryuri != "") {
|
||||
if (is_file("$tryuri/temp.php")) {
|
||||
if (file_exists("$tryuri/temp.php")) {
|
||||
$mainphp = "$tryuri/temp.php";
|
||||
goto endmain;
|
||||
}
|
||||
|
@ -143,23 +118,12 @@ $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);
|
||||
$parse = $pd->text(file_get_contents($parsef));
|
||||
}
|
||||
|
||||
$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'];
|
||||
|
@ -186,7 +150,7 @@ return $write;
|
|||
}
|
||||
|
||||
function page_locked($page) {
|
||||
if (is_file("$page.lock" ))
|
||||
if (file_exists("$page.lock" ))
|
||||
return true;
|
||||
|
||||
while ($page != "" && is_dir($page)) {
|
||||
|
@ -210,54 +174,52 @@ function page_locked($page) {
|
|||
|
||||
function p_img($src, $opts, $ext) {
|
||||
$src = ltrim($src, '/');
|
||||
$p = substr($src, 0, strrpos($src, '.'));
|
||||
$p = pathinfo($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";
|
||||
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 = "", $lossy=0) {
|
||||
$ext = strtolower(substr($src, strrpos($src, '.')+1));
|
||||
$psrc = ltrim($src, '/');
|
||||
$path = pathinfo($psrc);
|
||||
|
||||
if ($lossy || in_array($ext, ["jpg", "jpeg"])) {
|
||||
if ($lossy || in_array($path['extension'], ["jpg", "jpeg"])) {
|
||||
$je = ltrim(p_img($src, $opts, "jpg"), '/');
|
||||
|
||||
if (is_file($je)) return;
|
||||
|
||||
$gpath = substr($je, 0, strrpos($je, '/'));
|
||||
|
||||
`mkdir -p $gpath`;
|
||||
$jpath = pathinfo($je);
|
||||
$genp = $jpath['dirname'];
|
||||
`mkdir -p $genp`;
|
||||
|
||||
`convert -quality 80 $opts $psrc $je`;
|
||||
|
||||
$we = sub_ext($je, "webp");
|
||||
$we = ltrim(p_img($src, $opts, "webp"), '/');
|
||||
if (!file_exists($we)) {
|
||||
`convert -quality 80 $opts $psrc $we`;
|
||||
}
|
||||
|
||||
$ae = sub_ext($je, "avif");
|
||||
$ae = ltrim(p_img($src, $opts, "avif"), '/');
|
||||
if (!file_exists($ae)) {
|
||||
`convert -quality 80 $opts $psrc $ae`;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if ($ext == "png") {
|
||||
if ($path['extension'] == "png") {
|
||||
$we = ltrim(p_img($src, $opts, "webp"), '/');
|
||||
|
||||
if (is_file($we)) return;
|
||||
|
||||
$gpath = substr($we, 0, strrpos($we, '/'));
|
||||
`mkdir -p $gpath`;
|
||||
$wpath = pathinfo($we);
|
||||
$genp = $wpath['dirname'];
|
||||
`mkdir -p $genp`;
|
||||
|
||||
if (!file_exists($we)) {
|
||||
`convert -define webp:lossless=true $opts $psrc $we`;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
@ -298,7 +260,7 @@ function make_img($src, $opts="", $lossy=0) {
|
|||
|
||||
|
||||
function make_bkgd_img($src, $opts = "", $q=80) {
|
||||
$p = $src;
|
||||
$p = img_src($src);
|
||||
|
||||
gen_images($p, $opts);
|
||||
|
||||
|
@ -310,3 +272,4 @@ function make_bkgd_img($src, $opts = "", $q=80) {
|
|||
|
||||
|
||||
?>
|
||||
|
||||
|
|
Loading…
Reference in a new issue