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 = [])
13 $doc = ob_get_clean();
15 if (!empty($blocks['warn'])) {
16 $warn = '<p class="warn">[[warn]]</p>';
17 if ($offset = strpos($doc, '</h2>')) {
18 $doc = substr_replace($doc, "\n\n".$warn, $offset + 5, 0);
21 $doc = $warn . "\n\n" . $doc;
25 return preg_replace_callback(
26 '< \[\[ ([^]/]+) ([^]]*) \]\] >x',
27 function ($sub) use ($blocks) {
28 list ($placeholder, $name, $params) = $sub;
29 if (isset($blocks[$name])) {
30 $html = $blocks[$name];
32 elseif (file_exists("$name.php")) {
34 $Page = $GLOBALS['Page'];
35 $Args = $GLOBALS['Args'] . $params;
37 $html = ob_get_clean();
40 $html = '<strong class="warn"><em>'.$name.'</em> ontbreekt</strong>';
42 return sprintf('<!--BLOCK:%s-->%s<!--/-->',
43 is_numeric($name) ? '' : $placeholder, # edit replacement
44 preg_replace('{<!--[^-]*-->}', '', $html) # contents
51 # custom error handling
53 define('DOCROOT', getcwd().'/');
57 http_response_code(500);
58 include_once 'page.inc.php';
60 require_once DOCROOT.'500.html';
61 print getoutput(['debug' => $error]);
64 set_exception_handler('fail');
66 define('E_FATAL', E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR);
68 set_error_handler(function ($level, $error, $file, $line) {
69 if ($level & E_FATAL) {
76 register_shutdown_function(function () {
77 # display failure page for fatal exceptions
78 $error = error_get_last();
79 if (!($error['type'] & E_FATAL)) return;
80 fail("Fatal: $error[message] in $error[file]:$error[line]");
83 error_reporting(error_reporting() & ~E_FATAL);
85 # user login and control
87 include_once 'auth.inc.php';
88 $Edit = isset($_GET['edit']);
90 # distinguish subpage Args from topmost Page script
93 $Page = preg_replace('/\?.*/', '', @$_SERVER['PATH_INFO'] ?: $_SERVER['REQUEST_URI']);
94 $Page = urldecode(trim($Page, '/')) ?: 'index';
96 if (file_exists("$Page/.private")) {
99 http_response_code(303);
100 $target = urlencode($_SERVER['REQUEST_URI']);
101 header("Location: /login?goto=$target");
107 if (file_exists("$Page/index.php")) {
111 $up = strrpos($Page, '/');
112 $Args = substr($Page, $up) . $Args;
113 $Page = substr($Page, 0, $up);
119 # load static contents
121 ob_start(); # page body
122 ob_start(); # inner html
123 print '<div class="static">'."\n\n";
126 if (file_exists("$Page$Args.html")) {
127 $found = include "./$Page$Args.html";
129 elseif (file_exists("$Page$Args/index.html")) {
130 $found = include "./$Page$Args/index.html";
132 elseif (!empty($User['admin'])) {
133 $found = include (file_exists("$Page/template.html") ? "$Page/template.html" : './template.html');
138 # execute dynamic code
141 'user' => $User['name'],
142 'url' => htmlspecialchars($_SERVER['REQUEST_URI']),
146 $found |= require "./$Page/index.php";
152 # no resulting output
153 http_response_code(404);
154 @require "./404.html";
157 include_once 'page.inc.php';