aquinas/router.php

137 lines
2.7 KiB
PHP
Raw Normal View History

2022-08-02 07:48:43 -05:00
<?php
2022-11-02 18:12:43 -05:00
require "parsedown.php";
2022-08-02 07:48:43 -05:00
2022-11-02 18:12:43 -05:00
if (file_exists("site.json")) {
$site = json_decode(file_get_contents("site.json"), true);
}
if (preg_match('/\.(?:jpg|png|css|ico|scss|js|ttf|woff|txt)/', $_SERVER["REQUEST_URI"])) {
return false;
}
$tryuri = ltrim("$_SERVER[REQUEST_URI]", '/');
$tryuri = strtok($tryuri, "?");
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 (file_exists($fy)) $titles[] = ["c" => json_decode(file_get_contents($fy), true), "link" => uri_from_file($fy)];
}
}
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 (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";
2022-08-02 07:48:43 -05:00
}
2022-11-02 18:12:43 -05:00
if (file_exists("$tryf.md")) {
$parsef = "$tryf.md";
} else if (file_exists("$tryuri.md")) {
$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 (file_exists("$tryuri/temp.php")) {
$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));
}
$page['title'] = $obj->title;
$page['desc'] = $obj->desc;
if (empty($page['title'])) $page['title'] = $site['title'];
if (empty($page['desc'])) $page['desc'] = $site['desc'];
2022-08-02 07:48:43 -05:00
include "base.php";
2022-11-02 18:12:43 -05:00
}
function img_src($src, $root = "" ) {
if (empty($root)) $root = $_SERVER['REQUEST_URI'];
if ($src[0] != '/') {
$write = "$root/$src";
} else $write = $src;
return $write;
}
?>
2022-08-02 07:48:43 -05:00