2 error_reporting(E_ALL);
3 ini_set('display_errors', TRUE);
5 function getoutput($blocks = [])
8 foreach ($blocks as $name => $html) {
9 $rep["[[$name]]"] = sprintf('<!--BLOCK:%s-->%s<!--/-->',
10 is_numeric($name) ? '' : "[[$name]]",
11 preg_replace('{<!--[^-]*-->}', '', $html)
14 $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;
24 return str_replace(array_keys($rep), array_values($rep), $doc);
27 # custom error handling
29 define('DOCROOT', getcwd().'/');
33 http_response_code(500);
34 include_once 'page.inc.php';
36 require_once DOCROOT.'500.html';
37 print getoutput(['debug' => $error]);
40 set_exception_handler('fail');
42 define('E_FATAL', E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR);
44 set_error_handler(function ($level, $error, $file, $line) {
45 if ($level & E_FATAL) {
52 register_shutdown_function(function () {
53 # display failure page for fatal exceptions
54 $error = error_get_last();
55 if (!($error['type'] & E_FATAL)) return;
56 fail("Fatal: $error[message] in $error[file]:$error[line]");
59 error_reporting(error_reporting() & ~E_FATAL);
61 # user login and control
63 include_once 'auth.inc.php';
64 $Edit = isset($_GET['edit']);
66 # distinguish subpage Args from topmost Page script
69 $Page = preg_replace('/\?.*/', '', @$_SERVER['PATH_INFO'] ?: $_SERVER['REQUEST_URI']);
70 $Page = urldecode(trim($Page, '/')) ?: 'index';
72 if (file_exists("$Page/.private")) {
75 http_response_code(303);
76 $target = urlencode($_SERVER['REQUEST_URI']);
77 header("Location: /login?goto=$target");
83 if (file_exists("$Page.php")) {
87 $up = strrpos($Page, '/');
88 $Args = substr($Page, $up) . $Args;
89 $Page = substr($Page, 0, $up);
95 # load static contents
97 ob_start(); # page body
98 ob_start(); # inner html
99 print '<div class="static">'."\n\n";
102 if (file_exists("$Page$Args.html")) {
103 $found = include "./$Page$Args.html";
105 elseif (file_exists("$Page$Args/index.html")) {
106 $found = include "./$Page$Args/index.html";
108 elseif (!empty($User['admin'])) {
109 $found = include (file_exists("$Page/template.html") ? "$Page/template.html" : './template.html');
114 # execute dynamic code
117 $found |= require "./$Page.php";
122 include_once 'page.inc.php';
125 # no resulting output
126 http_response_code(404);
128 @require "./404.html";
129 print getoutput([ 'url' => htmlspecialchars($_SERVER['REQUEST_URI']) ]);