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'] . $GLOBALS['Args'];
36 $Place = $GLOBALS['Place'];
37 foreach (explode(' ', $params) as $param) {
38 if ($set = strpos($param, '=')) {
39 $Place[ substr($param, 0, $set) ] = substr($param, $set + 1);
41 elseif (!empty($param)) {
47 $html = ob_get_clean();
49 catch (Exception $e) {
50 $html = sprintf('<strong class="warn">%s</strong>',
51 "fout in <em>$name</em>: {$e->getMessage()}"
56 $html = '<strong class="warn"><em>'.$name.'</em> ontbreekt</strong>';
58 return sprintf('<!--BLOCK:%s-->%s<!--/-->',
59 is_numeric($name) ? '' : $placeholder, # edit replacement
60 preg_replace('{<!--[^-]*-->}', '', $html) # contents
67 # custom error handling
69 define('DOCROOT', getcwd());
70 set_include_path(implode(PATH_SEPARATOR, [ DOCROOT, __DIR__ ]));
74 http_response_code(500);
75 include_once 'page.inc.php';
77 require_once '500.html';
78 print getoutput(['debug' => $error]);
81 set_exception_handler('fail');
83 define('E_FATAL', E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR);
85 set_error_handler(function ($level, $error, $file, $line) {
86 if ($level & E_FATAL) {
93 register_shutdown_function(function () {
94 # display failure page for fatal exceptions
95 $error = error_get_last();
96 if (!($error['type'] & E_FATAL)) return;
97 fail("Fatal: $error[message] in $error[file]:$error[line]");
100 error_reporting(error_reporting() & ~E_FATAL);
102 # user login and control
104 include_once 'auth.inc.php';
105 $Edit = isset($_GET['edit']);
107 # distinguish subpage Args from topmost Page script
110 $Page = preg_replace('/\?.*/', '', @$_SERVER['PATH_INFO'] ?: $_SERVER['REQUEST_URI']);
111 $Page = urldecode(trim($Page, '/')) ?: 'index';
113 if (file_exists("$Page/.private")) {
116 http_response_code(303);
117 $target = urlencode($_SERVER['REQUEST_URI']);
118 header("Location: /login?goto=$target");
124 if (file_exists("$Page/index.php")) {
128 $up = strrpos($Page, '/');
129 $Args = substr($Page, $up) . $Args;
130 $Page = substr($Page, 0, $up);
136 # load static contents
138 ob_start(); # page body
139 ob_start(); # inner html
140 print '<div class="static">'."\n\n";
143 if (file_exists("$Page$Args.html")) {
144 $found = include "./$Page$Args.html";
146 elseif (file_exists("$Page$Args/index.html")) {
147 $found = include "./$Page$Args/index.html";
149 elseif (!empty($User['admin'])) {
150 $found = include (file_exists("$Page/template.html") ? "$Page/template.html" : './template.html');
155 # execute dynamic code
160 $found |= require "./$Page/index.php";
164 'user' => empty($User) ? '' : $User['name'],
165 'url' => htmlspecialchars($_SERVER['REQUEST_URI']),
171 # no resulting output
172 http_response_code(404);
176 include_once 'page.inc.php';