page: prepend 404 contents to admin template
[minimedit.git] / page.php
1 <?php
2 error_reporting(E_ALL);
3 ini_set('display_errors', TRUE);
4
5 function abort($body, $status = NULL) {
6         if ($status) header("HTTP/1.1 $status");
7         print "$body\n";
8         exit;
9 }
10
11 # custom error handling
12
13 define('DOCROOT', getcwd());
14 set_include_path(implode(PATH_SEPARATOR, [ DOCROOT, __DIR__ ]));
15
16 function fail($error)
17 {
18         global $User, $Page;
19         http_response_code(500);
20         if (!isset($Page)) {
21                 require_once('article.inc.php');
22                 $Page = new ArchiveArticle(NULL);
23                 $Page->title = 'Fout';
24         }
25         include_once 'page.inc.php';
26
27         ob_start();
28         require '500.inc.html';
29         $Page->place['debug'] = htmlspecialchars($error);
30         $Page->raw = ob_get_clean();
31         print $Page->render();
32 }
33
34 set_exception_handler('fail');
35
36 define('E_FATAL', E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR);
37
38 set_error_handler(function ($level, $error, $file, $line) {
39         if ($level & E_FATAL) {
40                 fail($error);
41                 return;
42         }
43         return FALSE;
44 });
45
46 register_shutdown_function(function () {
47         # display failure page for fatal exceptions
48         $error = error_get_last();
49         if (!($error['type'] & E_FATAL)) return;
50         fail("Fatal: $error[message] in $error[file]:$error[line]");
51 });
52
53 error_reporting(error_reporting() & ~E_FATAL);
54
55 # user login and control
56
57 include_once 'auth.inc.php'; // sets global $User
58 $Edit = isset($_GET['edit']);
59
60 # setup requested page
61
62 $request = preg_replace('/\?.*/', '', @$_SERVER['PATH_INFO'] ?: $_SERVER['REQUEST_URI']);
63 $request = urldecode(trim($request, '/')) ?: 'index';
64
65 $staticpage = "$request.html";
66 if (file_exists($staticpage)) {
67         if (is_link($staticpage)) {
68                 $target = preg_replace('/\.html$/', '', readlink($staticpage));
69                 header("HTTP/1.1 302 Shorthand");
70                 header("Location: $target");
71                 exit;
72         }
73 }
74 elseif (file_exists("$request/index.html")) {
75         $staticpage = "$request/index.html";
76 }
77
78 require_once('article.inc.php');
79 $Page = new ArchiveArticle($staticpage);
80
81 if ($Page->restricted) {
82         # access restriction
83         if (!$User->login) {
84                 http_response_code(303);
85                 $target = urlencode($Page->link);
86                 header("Location: /login?goto=$target");
87                 exit;
88         }
89 }
90
91 # prepare page contents
92
93 header(sprintf('Content-Security-Policy: %s', implode('; ', [
94         "default-src 'self' 'unsafe-inline' http://cdn.ckeditor.com", # some overrides remain
95         "img-src 'self' data: http://cdn.ckeditor.com", # inline svg (in css)
96         "base-uri 'self'", # only local pages
97         "frame-ancestors 'none'", # prevent malicious embedding
98 ])));
99
100 $Page->place += [
101         'user'  => $User->login ?: '',
102         'url'   => htmlspecialchars($_SERVER['REQUEST_URI']),
103 ];
104
105 if (!isset($Page->raw) and $User->admin("edit {$Page->link}")) {
106         # open bottom template as initial contents
107         $template = 'template.inc.html';
108         if ($Page->handler and file_exists("{$Page->handler}/$template")) {
109                 $template = "{$Page->handler}/$template";
110         }
111         $Page->raw($template);
112         $Page->meta['article:published_time'] = date('Y-m-d h:i:s O');
113         $Page->meta['article:author'] = '/' . $User->dir;
114         $Page->body = NULL;
115 }
116
117 if (isset($Page->raw)) {
118         if ($User->admin("edit {$Page->link}")) {
119                 # restore meta tags in static contents for editing
120                 foreach (array_reverse($Page->meta) as $metaprop => $val) {
121                         $Page->raw = sprintf(
122                                 '<meta property="%s" content="%s">'."\n",
123                                 $metaprop, $val
124                         ) . $Page->raw;
125                 }
126         }
127         $Page->raw = '<div class="static">'."\n\n".$Page->raw."</div>\n\n";
128 }
129
130 # output dynamic and/or static html
131
132 ob_start();
133 if ($Page->handler and !require("./{$Page->handler}/index.php")) {
134         # replace contents by code output on false return
135         $Page->raw = ob_get_clean();
136 }
137 else {
138         # keep article contents
139         if (!isset($Page->body)) {
140                 # no resulting output
141                 http_response_code(404);
142                 @require '404.inc.html';
143                 $Page->raw = ob_get_clean() . $Page->raw;
144         }
145 }
146
147 include_once 'page.inc.php';
148