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().'/');
73 http_response_code(500);
74 include_once 'page.inc.php';
76 require_once DOCROOT.'500.html';
77 print getoutput(['debug' => $error]);
80 set_exception_handler('fail');
82 define('E_FATAL', E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR);
84 set_error_handler(function ($level, $error, $file, $line) {
85 if ($level & E_FATAL) {
92 register_shutdown_function(function () {
93 # display failure page for fatal exceptions
94 $error = error_get_last();
95 if (!($error['type'] & E_FATAL)) return;
96 fail("Fatal: $error[message] in $error[file]:$error[line]");
99 error_reporting(error_reporting() & ~E_FATAL);
101 # user login and control
103 include_once 'auth.inc.php';
104 $Edit = isset($_GET['edit']);
106 # distinguish subpage Args from topmost Page script
109 $Page = preg_replace('/\?.*/', '', @$_SERVER['PATH_INFO'] ?: $_SERVER['REQUEST_URI']);
110 $Page = urldecode(trim($Page, '/')) ?: 'index';
112 if (file_exists("$Page/.private")) {
115 http_response_code(303);
116 $target = urlencode($_SERVER['REQUEST_URI']);
117 header("Location: /login?goto=$target");
123 if (file_exists("$Page/index.php")) {
127 $up = strrpos($Page, '/');
128 $Args = substr($Page, $up) . $Args;
129 $Page = substr($Page, 0, $up);
135 # load static contents
137 ob_start(); # page body
138 ob_start(); # inner html
139 print '<div class="static">'."\n\n";
142 if (file_exists("$Page$Args.html")) {
143 $found = include "./$Page$Args.html";
145 elseif (file_exists("$Page$Args/index.html")) {
146 $found = include "./$Page$Args/index.html";
148 elseif (!empty($User['admin'])) {
149 $found = include (file_exists("$Page/template.html") ? "$Page/template.html" : './template.html');
154 # execute dynamic code
159 $found |= require "./$Page/index.php";
163 'user' => empty($User) ? '' : $User['name'],
164 'url' => htmlspecialchars($_SERVER['REQUEST_URI']),
170 # no resulting output
171 http_response_code(404);
172 @require "./404.html";
175 include_once 'page.inc.php';