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 # custom error handling
13 define('DOCROOT', getcwd());
14 set_include_path(implode(PATH_SEPARATOR, [ DOCROOT, __DIR__ ]));
19 http_response_code(500);
21 require_once('article.inc.php');
22 $Page = new ArchiveArticle(NULL);
23 $Page->title = 'Fout';
25 include_once 'page.inc.php';
27 require '500.inc.html';
28 $Page->place['debug'] = htmlspecialchars($error);
29 print $Page->render();
32 set_exception_handler('fail');
34 define('E_FATAL', E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR);
36 set_error_handler(function ($level, $error, $file, $line) {
37 if ($level & E_FATAL) {
44 register_shutdown_function(function () {
45 # display failure page for fatal exceptions
46 $error = error_get_last();
47 if (!($error['type'] & E_FATAL)) return;
48 fail("Fatal: $error[message] in $error[file]:$error[line]");
51 error_reporting(error_reporting() & ~E_FATAL);
53 # user login and control
55 include_once 'auth.inc.php'; // sets global $User
56 $Edit = isset($_GET['edit']);
58 # setup requested page
60 $request = preg_replace('/\?.*/', '', @$_SERVER['PATH_INFO'] ?: $_SERVER['REQUEST_URI']);
61 $request = urldecode(trim($request, '/')) ?: 'index';
63 $staticpage = "$request.html";
64 if (file_exists($staticpage)) {
65 if (is_link($staticpage)) {
66 $target = preg_replace('/\.html$/', '', readlink($staticpage));
67 header("HTTP/1.1 302 Shorthand");
68 header("Location: $target");
72 elseif (file_exists("$request/index.html")) {
73 $staticpage = "$request/index.html";
76 require_once('article.inc.php');
77 $Page = new ArchiveArticle($staticpage);
79 if ($Page->restricted) {
82 http_response_code(303);
83 $target = urlencode($Page->link);
84 header("Location: /login?goto=$target");
89 # prepare page contents
91 header(sprintf('Content-Security-Policy: %s', implode('; ', [
92 "default-src 'self' 'unsafe-inline' http://cdn.ckeditor.com", # some overrides remain
93 "img-src 'self' data: http://cdn.ckeditor.com", # inline svg (in css)
94 "base-uri 'self'", # only local pages
95 "frame-ancestors 'none'", # prevent malicious embedding
98 ob_start(); # page body
100 'user' => $User->login ?: '',
101 'url' => htmlspecialchars($_SERVER['REQUEST_URI']),
104 if (!isset($Page->raw) and $User->admin("edit {$Page->link}")) {
105 # open bottom template as initial contents
106 $template = 'template.inc.html';
107 if ($Page->handler and file_exists("{$Page->handler}/$template")) {
108 $template = "{$Page->handler}/$template";
110 $Page->raw($template);
111 $Page->meta['article:published_time'] = date('Y-m-d h:i:s O');
112 $Page->meta['article:author'] = '/' . $User->dir;
115 if (isset($Page->raw)) {
116 if ($User->admin("edit {$Page->link}")) {
117 # restore meta tags in static contents for editing
118 foreach (array_reverse($Page->meta) as $metaprop => $val) {
119 $Page->raw = sprintf(
120 '<meta property="%s" content="%s">'."\n",
125 $Page->raw = '<div class="static">'."\n\n".$Page->raw."</div>\n\n";
128 # output dynamic and/or static html
130 if (!$Page->handler or require("./{$Page->handler}/index.php")) {
132 if (isset($Page->raw)) {
136 # no resulting output
137 http_response_code(404);
138 @require '404.inc.html';
142 include_once 'page.inc.php';