X-Git-Url: http://git.shiar.net/minimedit.git/blobdiff_plain/769e3ba9e234db9019a096e1d6590f902b5560e0..467fdea52c27dcf01e79cb96068134403afff660:/page.php?ds=inline diff --git a/page.php b/page.php index c34b11c..5eefadb 100644 --- a/page.php +++ b/page.php @@ -2,88 +2,142 @@ error_reporting(E_ALL); ini_set('display_errors', TRUE); +function abort($body, $status = NULL) { + if ($status) header("HTTP/1.1 $status"); + print "$body\n"; + exit; +} + +# custom error handling + +define('DOCROOT', getcwd()); +set_include_path(implode(PATH_SEPARATOR, [ DOCROOT, __DIR__ ])); + function fail($error) { + global $User, $Page; http_response_code(500); + if (!isset($Page)) { + require_once('article.inc.php'); + $Page = new ArchiveArticle(NULL); + $Page->title = 'Fout'; + } include_once 'page.inc.php'; ob_start(); - require_once './500.html'; - print getoutput(['debug' => $error]); + require '500.inc.html'; + $Page->place['debug'] = htmlspecialchars($error); + print $Page->render(); } + set_exception_handler('fail'); + +define('E_FATAL', E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR); + +set_error_handler(function ($level, $error, $file, $line) { + if ($level & E_FATAL) { + fail($error); + return; + } + return FALSE; +}); + register_shutdown_function(function () { # display failure page for fatal exceptions $error = error_get_last(); - if (!($error['type'] & (E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR))) return; + if (!($error['type'] & E_FATAL)) return; fail("Fatal: $error[message] in $error[file]:$error[line]"); }); -include_once 'auth.inc.php'; +error_reporting(error_reporting() & ~E_FATAL); + +# user login and control + +include_once 'auth.inc.php'; // sets global $User $Edit = isset($_GET['edit']); -# distinguish subpage Args from topmost Page script +# setup requested page -$Args = ''; -$Page = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']); -$Page = urldecode(trim($Page, '/')) ?: 'index'; -while (TRUE) { - if (file_exists("$Page.php")) { - break; - } +$request = preg_replace('/\?.*/', '', @$_SERVER['PATH_INFO'] ?: $_SERVER['REQUEST_URI']); +$request = urldecode(trim($request, '/')) ?: 'index'; - $up = strrpos($Page, '/'); - $Args = substr($Page, $up) . $Args; - $Page = substr($Page, 0, $up); - if ($up === FALSE) { - break; +$staticpage = "$request.html"; +if (file_exists($staticpage)) { + if (is_link($staticpage)) { + $target = preg_replace('/\.html$/', '', readlink($staticpage)); + header("HTTP/1.1 302 Shorthand"); + header("Location: $target"); + exit; } } +elseif (file_exists("$request/index.html")) { + $staticpage = "$request/index.html"; +} -# load static contents - -ob_start(); # page body -ob_start(); # inner html -print '