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)) {
46 $html = ob_get_clean();
49 $html = '<strong class="warn"><em>'.$name.'</em> ontbreekt</strong>';
51 return sprintf('<!--BLOCK:%s-->%s<!--/-->',
52 is_numeric($name) ? '' : $placeholder, # edit replacement
53 preg_replace('{<!--[^-]*-->}', '', $html) # contents
60 # custom error handling
62 define('DOCROOT', getcwd().'/');
66 http_response_code(500);
67 include_once 'page.inc.php';
69 require_once DOCROOT.'500.html';
70 print getoutput(['debug' => $error]);
73 set_exception_handler('fail');
75 define('E_FATAL', E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR);
77 set_error_handler(function ($level, $error, $file, $line) {
78 if ($level & E_FATAL) {
85 register_shutdown_function(function () {
86 # display failure page for fatal exceptions
87 $error = error_get_last();
88 if (!($error['type'] & E_FATAL)) return;
89 fail("Fatal: $error[message] in $error[file]:$error[line]");
92 error_reporting(error_reporting() & ~E_FATAL);
94 # user login and control
96 include_once 'auth.inc.php';
97 $Edit = isset($_GET['edit']);
99 # distinguish subpage Args from topmost Page script
102 $Page = preg_replace('/\?.*/', '', @$_SERVER['PATH_INFO'] ?: $_SERVER['REQUEST_URI']);
103 $Page = urldecode(trim($Page, '/')) ?: 'index';
105 if (file_exists("$Page/.private")) {
108 http_response_code(303);
109 $target = urlencode($_SERVER['REQUEST_URI']);
110 header("Location: /login?goto=$target");
116 if (file_exists("$Page/index.php")) {
120 $up = strrpos($Page, '/');
121 $Args = substr($Page, $up) . $Args;
122 $Page = substr($Page, 0, $up);
128 # load static contents
130 ob_start(); # page body
131 ob_start(); # inner html
132 print '<div class="static">'."\n\n";
135 if (file_exists("$Page$Args.html")) {
136 $found = include "./$Page$Args.html";
138 elseif (file_exists("$Page$Args/index.html")) {
139 $found = include "./$Page$Args/index.html";
141 elseif (!empty($User['admin'])) {
142 $found = include (file_exists("$Page/template.html") ? "$Page/template.html" : './template.html');
147 # execute dynamic code
152 $found |= require "./$Page/index.php";
156 'user' => empty($User) ? '' : $User['name'],
157 'url' => htmlspecialchars($_SERVER['REQUEST_URI']),
163 # no resulting output
164 http_response_code(404);
165 @require "./404.html";
168 include_once 'page.inc.php';