2 error_reporting(E_ALL);
3 ini_set('display_errors', TRUE);
5 function abort($body, $status = NULL) {
6 if ($status) header("HTTP/1.1 $status");
11 function getoutput($blocks = [])
14 foreach ($blocks as $name => $html) {
15 $rep["[[$name]]"] = sprintf('<!--BLOCK:%s-->%s<!--/-->',
16 is_numeric($name) ? '' : "[[$name]]",
17 preg_replace('{<!--[^-]*-->}', '', $html)
20 $doc = ob_get_clean();
21 if (!empty($blocks['warn'])) {
22 $warn = '<p class="warn">[[warn]]</p>';
23 if ($offset = strpos($doc, '</h2>')) {
24 $doc = substr_replace($doc, "\n\n".$warn, $offset + 5, 0);
27 $doc = $warn . "\n\n" . $doc;
30 return str_replace(array_keys($rep), array_values($rep), $doc);
33 # custom error handling
35 define('DOCROOT', getcwd().'/');
39 http_response_code(500);
40 include_once 'page.inc.php';
42 require_once DOCROOT.'500.html';
43 print getoutput(['debug' => $error]);
46 set_exception_handler('fail');
48 define('E_FATAL', E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR);
50 set_error_handler(function ($level, $error, $file, $line) {
51 if ($level & E_FATAL) {
58 register_shutdown_function(function () {
59 # display failure page for fatal exceptions
60 $error = error_get_last();
61 if (!($error['type'] & E_FATAL)) return;
62 fail("Fatal: $error[message] in $error[file]:$error[line]");
65 error_reporting(error_reporting() & ~E_FATAL);
67 # user login and control
69 include_once 'auth.inc.php';
70 $Edit = isset($_GET['edit']);
72 # distinguish subpage Args from topmost Page script
75 $Page = preg_replace('/\?.*/', '', @$_SERVER['PATH_INFO'] ?: $_SERVER['REQUEST_URI']);
76 $Page = urldecode(trim($Page, '/')) ?: 'index';
78 if (file_exists("$Page/.private")) {
81 http_response_code(303);
82 $target = urlencode($_SERVER['REQUEST_URI']);
83 header("Location: /login?goto=$target");
89 if (file_exists("$Page.php")) {
93 $up = strrpos($Page, '/');
94 $Args = substr($Page, $up) . $Args;
95 $Page = substr($Page, 0, $up);
101 # load static contents
103 ob_start(); # page body
104 ob_start(); # inner html
105 print '<div class="static">'."\n\n";
108 if (file_exists("$Page$Args.html")) {
109 $found = include "./$Page$Args.html";
111 elseif (file_exists("$Page$Args/index.html")) {
112 $found = include "./$Page$Args/index.html";
114 elseif (!empty($User['admin'])) {
115 $found = include (file_exists("$Page/template.html") ? "$Page/template.html" : './template.html');
120 # execute dynamic code
123 $found |= require "./$Page.php";
128 include_once 'page.inc.php';
131 # no resulting output
132 http_response_code(404);
134 @require "./404.html";
135 print getoutput([ 'url' => htmlspecialchars($_SERVER['REQUEST_URI']) ]);