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 placeholder_include($name, $params = [])
13 $path = stream_resolve_include_path("$name.php");
14 if (!file_exists($path)) {
15 return '<strong class="warn"><em>'.$name.'</em> ontbreekt</strong>';
19 $Page = $GLOBALS['Page'] . $GLOBALS['Args'];
21 $Place = $GLOBALS['Place'];
22 foreach ($params as $param) {
23 if ($set = strpos($param, '=')) {
24 $Place[ substr($param, 0, $set) ] = substr($param, $set + 1);
26 elseif (!empty($param)) {
32 return ob_get_clean();
34 catch (Exception $e) {
35 return sprintf('<strong class="warn">%s</strong>',
36 "fout in <em>$name</em>: {$e->getMessage()}"
41 function getoutput($blocks = [])
43 $doc = ob_get_clean();
45 if (!empty($blocks['warn'])) {
46 $warn = '<p class="warn">[[warn]]</p>';
47 if ($offset = strpos($doc, '</h2>')) {
48 $doc = substr_replace($doc, "\n\n".$warn, $offset + 5, 0);
51 $doc = $warn . "\n\n" . $doc;
55 return preg_replace_callback(
56 '< \[\[ ([^] ]+) ([^]]*) \]\] >x',
57 function ($sub) use ($blocks) {
58 list ($placeholder, $name, $params) = $sub;
59 if (isset($blocks[$name])) {
60 $html = $blocks[$name];
63 $html = placeholder_include($name, explode(' ', $params));
65 return sprintf('<!--BLOCK:%s-->%s<!--/-->',
66 is_numeric($name) ? '' : $placeholder, # edit replacement
67 preg_replace('{<!--[^-]*-->}', '', $html) # contents
74 # custom error handling
76 define('DOCROOT', getcwd());
77 set_include_path(implode(PATH_SEPARATOR, [ DOCROOT, __DIR__ ]));
81 http_response_code(500);
82 include_once 'page.inc.php';
84 require_once '500.html';
85 print getoutput(['debug' => $error]);
88 set_exception_handler('fail');
90 define('E_FATAL', E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR);
92 set_error_handler(function ($level, $error, $file, $line) {
93 if ($level & E_FATAL) {
100 register_shutdown_function(function () {
101 # display failure page for fatal exceptions
102 $error = error_get_last();
103 if (!($error['type'] & E_FATAL)) return;
104 fail("Fatal: $error[message] in $error[file]:$error[line]");
107 error_reporting(error_reporting() & ~E_FATAL);
109 # user login and control
111 include_once 'auth.inc.php';
112 $Edit = isset($_GET['edit']);
114 # distinguish subpage Args from topmost Page script
117 $Page = preg_replace('/\?.*/', '', @$_SERVER['PATH_INFO'] ?: $_SERVER['REQUEST_URI']);
118 $Page = urldecode(trim($Page, '/')) ?: 'index';
120 if (file_exists("$Page/.private")) {
123 http_response_code(303);
124 $target = urlencode($_SERVER['REQUEST_URI']);
125 header("Location: /login?goto=$target");
131 if (file_exists("$Page/index.php")) {
135 $up = strrpos($Page, '/');
136 $Args = substr($Page, $up) . $Args;
137 $Page = substr($Page, 0, $up);
144 if (file_exists("$Page$Args.html")) {
145 $staticpage = "$Page$Args.html";
146 if (is_link($staticpage)) {
147 $target = preg_replace('/\.html$/', '', readlink($staticpage));
148 header("HTTP/1.1 302 Shorthand");
149 header("Location: $target");
153 elseif (file_exists("$Page$Args/index.html")) {
154 $staticpage = "$Page$Args/index.html";
156 elseif (!empty($User['admin'])) {
157 $staticpage = (file_exists("$Page/template.html") ? "$Page/template.html" : 'template.html');
160 # load static contents
162 ob_start(); # page body
163 ob_start(); # inner html
164 print '<div class="static">'."\n\n";
167 if (isset($staticpage)) {
168 $found = include "./$staticpage";
173 # execute dynamic code
178 $found |= require "./$Page/index.php";
182 'user' => empty($User) ? '' : $User['name'],
183 'url' => htmlspecialchars($_SERVER['REQUEST_URI']),
189 # no resulting output
190 http_response_code(404);
194 include_once 'page.inc.php';