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 placeholder_include($name, $params = [])
13 $path = stream_resolve_include_path("widget/$name.php");
14 if (!file_exists($path)) {
15 return '<strong class="warn"><em>'.$name.'</em> ontbreekt</strong>';
19 $Page = $GLOBALS['Page'] . $GLOBALS['Args'];
21 $Place = $GLOBALS['Place'];
22 foreach ($params as $param) {
23 if ($set = strpos($param, '=')) {
24 $Place[ substr($param, 0, $set) ] = substr($param, $set + 1);
26 elseif (!empty($param)) {
31 include "widget/$name.php";
32 return ob_get_clean();
34 catch (Exception $e) {
35 return sprintf('<strong class="warn">%s</strong>',
36 "fout in <em>$name</em>: {$e->getMessage()}"
41 function getoutput($blocks = [])
43 $doc = ob_get_clean();
45 if (!empty($blocks['warn'])) {
46 $warn = '<p class="warn">[[warn]]</p>';
47 if ($offset = strpos($doc, '</h2>')) {
48 $doc = substr_replace($doc, "\n\n".$warn, $offset + 5, 0);
51 $doc = $warn . "\n\n" . $doc;
55 # keep either login or logout parts depending on user level
57 $hideclass = $User && property_exists($User, 'login') && $User->login ? 'logout' : 'login';
58 $doc = preg_replace('{\s*<([a-z]+) class="'.$hideclass.'">.*?</\1>}s', '', $doc);
60 return preg_replace_callback(
61 '{ \[\[ ([^] ]+) ([^]]*) \]\] }x',
62 function ($sub) use ($blocks) {
63 list ($placeholder, $name, $params) = $sub;
64 $html = $blocks[$name] ??
65 placeholder_include($name, explode(' ', $params));
66 if (empty($html) or $html[0] != '<') {
67 $html = "<span>$html</span>";
69 $attr = sprintf(' data-dyn="%s"', is_numeric($name) ? '' : $name.$params);
70 # contents with identifier in first tag
71 return preg_replace( '/(?=>)/', $attr, $html, 1);
77 # custom error handling
79 define('DOCROOT', getcwd());
80 set_include_path(implode(PATH_SEPARATOR, [ DOCROOT, __DIR__ ]));
84 global $User, $Page, $Args;
85 http_response_code(500);
86 if (!isset($Article)) {
87 require_once('article.inc.php');
88 $Article = new ArchiveArticle(NULL);
89 $Article->title = 'Fout';
91 include_once 'page.inc.php';
93 require '500.inc.html';
94 print getoutput(['debug' => htmlspecialchars($error)]);
97 set_exception_handler('fail');
99 define('E_FATAL', E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR);
101 set_error_handler(function ($level, $error, $file, $line) {
102 if ($level & E_FATAL) {
109 register_shutdown_function(function () {
110 # display failure page for fatal exceptions
111 $error = error_get_last();
112 if (!($error['type'] & E_FATAL)) return;
113 fail("Fatal: $error[message] in $error[file]:$error[line]");
116 error_reporting(error_reporting() & ~E_FATAL);
118 # user login and control
120 include_once 'auth.inc.php'; // sets global $User
121 $Edit = isset($_GET['edit']);
123 # setup requested page
126 $Page = preg_replace('/\?.*/', '', @$_SERVER['PATH_INFO'] ?: $_SERVER['REQUEST_URI']);
127 $Page = urldecode(trim($Page, '/')) ?: 'index';
129 $staticpage = "$Page.html";
130 if (file_exists($staticpage)) {
131 if (is_link($staticpage)) {
132 $target = preg_replace('/\.html$/', '', readlink($staticpage));
133 header("HTTP/1.1 302 Shorthand");
134 header("Location: $target");
138 elseif (file_exists("$Page/index.html")) {
139 $staticpage = "$Page/index.html";
142 require_once('article.inc.php');
143 $Article = new ArchiveArticle($staticpage);
145 $Page = $Article->handler;
146 $Args = $Article->path;
148 if ($PageAccess = $Article->restricted) {
151 http_response_code(303);
152 $target = urlencode($Article->link);
153 header("Location: /login?goto=$target");
158 # prepare page contents
160 header(sprintf('Content-Security-Policy: %s', implode('; ', [
161 "default-src 'self' 'unsafe-inline' http://cdn.ckeditor.com", # some overrides remain
162 "img-src 'self' data: http://cdn.ckeditor.com", # inline svg (in css)
163 "base-uri 'self'", # only local pages
164 "frame-ancestors 'none'", # prevent malicious embedding
167 ob_start(); # page body
169 'user' => $User->login ?: '',
170 'url' => htmlspecialchars($_SERVER['REQUEST_URI']),
173 if (!isset($Article->raw) and $User->admin("edit {$Article->link}")) {
174 # open template as initial contents
175 $Article->raw(file_exists("$Page/template.inc.html") ? "$Page/template.inc.html" : 'template.inc.html');
176 $Article->meta['article:published_time'] = date('Y-m-d h:i:s O');
177 $Article->meta['article:author'] = '/' . $User->dir;
180 if (isset($Article->raw)) {
181 if ($User->admin("edit $Page$Args")) {
182 # restore meta tags in static contents for editing
183 foreach (array_reverse($Article->meta) as $metaprop => $val) {
184 $Article->raw = sprintf(
185 '<meta property="%s" content="%s">'."\n",
190 $Article->raw = '<div class="static">'."\n\n".$Article->raw."</div>\n\n";
193 # output dynamic and/or static html
195 if (!$Page or require("./$Page/index.php")) {
197 if (isset($Article->raw)) {
201 # no resulting output
202 http_response_code(404);
203 @require '404.inc.html';
207 include_once 'page.inc.php';