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 return str_replace(array_keys($rep), array_values($rep), ob_get_clean());
17 # custom error handling
21 http_response_code(500);
22 include_once 'page.inc.php';
24 require_once './500.html';
25 print getoutput(['debug' => $error]);
28 set_exception_handler('fail');
30 define('E_FATAL', E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR);
32 set_error_handler(function ($level, $error, $file, $line) {
33 if ($level & E_FATAL) {
40 register_shutdown_function(function () {
41 # display failure page for fatal exceptions
42 $error = error_get_last();
43 if (!($error['type'] & E_FATAL)) return;
44 fail("Fatal: $error[message] in $error[file]:$error[line]");
47 error_reporting(error_reporting() & ~E_FATAL);
49 # user login and control
51 include_once 'auth.inc.php';
52 $Edit = isset($_GET['edit']);
54 # distinguish subpage Args from topmost Page script
57 $Page = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
58 $Page = urldecode(trim($Page, '/')) ?: 'index';
60 if (file_exists("$Page/.private")) {
63 http_response_code(403);
64 include_once 'page.inc.php';
66 @require_once './403.html';
71 if (file_exists("$Page.php")) {
75 $up = strrpos($Page, '/');
76 $Args = substr($Page, $up) . $Args;
77 $Page = substr($Page, 0, $up);
83 # load static contents
85 ob_start(); # page body
86 ob_start(); # inner html
87 print '<div class="static">'."\n\n";
90 if (file_exists("$Page$Args/index.html")) {
91 $found = include "./$Page$Args/index.html";
93 elseif (file_exists("$Page$Args.html")) {
94 $found = include "./$Page$Args.html";
96 elseif (isset($User) and $User['admin']) {
97 $found = include (file_exists("$Page/template.html") ? "$Page/template.html" : './template.html');
102 # execute dynamic code
105 $found |= @require "./$Page.php";
110 include_once 'page.inc.php';
113 # no resulting output
114 http_response_code(404);
116 @require "./404.html";
117 print getoutput([ 'url' => htmlspecialchars($_SERVER['REQUEST_URI']) ]);