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 # keep either login or logout parts depending on user level
27 $hideclass = $User && property_exists($User, 'login') && $User->login ? 'logout' : 'login';
28 $doc = preg_replace('{\s*<([a-z]+) class="'.$hideclass.'">.*?</\1>}s', '', $doc);
30 return preg_replace_callback(
31 '{ \[\[ ([^] ]+) ([^]]*) \]\] }x',
32 function ($sub) use ($blocks) {
33 list ($placeholder, $name, $params) = $sub;
34 $html = $blocks[$name] ??
35 placeholder_include($name, explode(' ', $params));
36 if (empty($html) or $html[0] != '<') {
37 $html = "<span>$html</span>";
39 $attr = sprintf(' data-dyn="%s"', is_numeric($name) ? '' : $name.$params);
40 # contents with identifier in first tag
41 return preg_replace( '/(?=>)/', $attr, $html, 1);
47 # custom error handling
49 define('DOCROOT', getcwd());
50 set_include_path(implode(PATH_SEPARATOR, [ DOCROOT, __DIR__ ]));
55 http_response_code(500);
57 require_once('article.inc.php');
58 $Page = new ArchiveArticle(NULL);
59 $Page->title = 'Fout';
61 include_once 'page.inc.php';
63 require '500.inc.html';
64 print getoutput(['debug' => htmlspecialchars($error)]);
67 set_exception_handler('fail');
69 define('E_FATAL', E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR);
71 set_error_handler(function ($level, $error, $file, $line) {
72 if ($level & E_FATAL) {
79 register_shutdown_function(function () {
80 # display failure page for fatal exceptions
81 $error = error_get_last();
82 if (!($error['type'] & E_FATAL)) return;
83 fail("Fatal: $error[message] in $error[file]:$error[line]");
86 error_reporting(error_reporting() & ~E_FATAL);
88 # user login and control
90 include_once 'auth.inc.php'; // sets global $User
91 $Edit = isset($_GET['edit']);
93 # setup requested page
95 $request = preg_replace('/\?.*/', '', @$_SERVER['PATH_INFO'] ?: $_SERVER['REQUEST_URI']);
96 $request = urldecode(trim($request, '/')) ?: 'index';
98 $staticpage = "$request.html";
99 if (file_exists($staticpage)) {
100 if (is_link($staticpage)) {
101 $target = preg_replace('/\.html$/', '', readlink($staticpage));
102 header("HTTP/1.1 302 Shorthand");
103 header("Location: $target");
107 elseif (file_exists("$request/index.html")) {
108 $staticpage = "$request/index.html";
111 require_once('article.inc.php');
112 $Page = new ArchiveArticle($staticpage);
114 if ($Page->restricted) {
117 http_response_code(303);
118 $target = urlencode($Page->link);
119 header("Location: /login?goto=$target");
124 # prepare page contents
126 header(sprintf('Content-Security-Policy: %s', implode('; ', [
127 "default-src 'self' 'unsafe-inline' http://cdn.ckeditor.com", # some overrides remain
128 "img-src 'self' data: http://cdn.ckeditor.com", # inline svg (in css)
129 "base-uri 'self'", # only local pages
130 "frame-ancestors 'none'", # prevent malicious embedding
133 ob_start(); # page body
135 'user' => $User->login ?: '',
136 'url' => htmlspecialchars($_SERVER['REQUEST_URI']),
139 if (!isset($Page->raw) and $User->admin("edit {$Page->link}")) {
140 # open bottom template as initial contents
141 $template = 'template.inc.html';
142 if ($Page->handler and file_exists("{$Page->handler}/$template")) {
143 $template = "{$Page->handler}/$template";
145 $Page->raw($template);
146 $Page->meta['article:published_time'] = date('Y-m-d h:i:s O');
147 $Page->meta['article:author'] = '/' . $User->dir;
150 if (isset($Page->raw)) {
151 if ($User->admin("edit {$Page->link}")) {
152 # restore meta tags in static contents for editing
153 foreach (array_reverse($Page->meta) as $metaprop => $val) {
154 $Page->raw = sprintf(
155 '<meta property="%s" content="%s">'."\n",
160 $Page->raw = '<div class="static">'."\n\n".$Page->raw."</div>\n\n";
163 # output dynamic and/or static html
165 if (!$Page->handler or require("./{$Page->handler}/index.php")) {
167 if (isset($Page->raw)) {
171 # no resulting output
172 http_response_code(404);
173 @require '404.inc.html';
177 include_once 'page.inc.php';