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
19 define('DOCROOT', getcwd().'/');
23 http_response_code(500);
24 include_once 'page.inc.php';
26 require_once DOCROOT.'500.html';
27 print getoutput(['debug' => $error]);
30 set_exception_handler('fail');
32 define('E_FATAL', E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR);
34 set_error_handler(function ($level, $error, $file, $line) {
35 if ($level & E_FATAL) {
42 register_shutdown_function(function () {
43 # display failure page for fatal exceptions
44 $error = error_get_last();
45 if (!($error['type'] & E_FATAL)) return;
46 fail("Fatal: $error[message] in $error[file]:$error[line]");
49 error_reporting(error_reporting() & ~E_FATAL);
51 # user login and control
53 include_once 'auth.inc.php';
54 $Edit = isset($_GET['edit']);
56 # distinguish subpage Args from topmost Page script
59 $Page = preg_replace('/\?.*/', '', @$_SERVER['PATH_INFO'] ?: $_SERVER['REQUEST_URI']);
60 $Page = urldecode(trim($Page, '/')) ?: 'index';
62 if (file_exists("$Page/.private")) {
65 http_response_code(303);
66 $target = urlencode($_SERVER['REQUEST_URI']);
67 header("Location: /login?goto=$target");
73 if (file_exists("$Page.php")) {
77 $up = strrpos($Page, '/');
78 $Args = substr($Page, $up) . $Args;
79 $Page = substr($Page, 0, $up);
85 # load static contents
87 ob_start(); # page body
88 ob_start(); # inner html
89 print '<div class="static">'."\n\n";
92 if (file_exists("$Page$Args/index.html")) {
93 $found = include "./$Page$Args/index.html";
95 elseif (file_exists("$Page$Args.html")) {
96 $found = include "./$Page$Args.html";
98 elseif (isset($User) and $User['admin']) {
99 $found = include (file_exists("$Page/template.html") ? "$Page/template.html" : './template.html');
104 # execute dynamic code
107 $found |= require "./$Page.php";
112 include_once 'page.inc.php';
115 # no resulting output
116 http_response_code(404);
118 @require "./404.html";
119 print getoutput([ 'url' => htmlspecialchars($_SERVER['REQUEST_URI']) ]);