page: move custom error handling to include
[minimedit.git] / page.php
1 <?php
2 error_reporting(E_ALL);
3 ini_set('display_errors', TRUE);
4
5 define('DOCROOT', getcwd());
6 set_include_path(implode(PATH_SEPARATOR, [ DOCROOT, __DIR__ ]));
7
8 include_once 'error.inc.php';
9
10 # user login and control
11
12 include_once 'auth.inc.php'; // sets global $User
13 $Edit = isset($_GET['edit']);
14
15 # setup requested page
16
17 $request = preg_replace('/\?.*/', '', @$_SERVER['PATH_INFO'] ?: $_SERVER['REQUEST_URI']);
18 $request = urldecode(trim($request, '/')) ?: 'index';
19
20 $staticpage = "$request.html";
21 if (file_exists($staticpage)) {
22         if (is_link($staticpage)) {
23                 $target = preg_replace('/\.html$/', '', readlink($staticpage));
24                 header("HTTP/1.1 302 Shorthand");
25                 header("Location: $target");
26                 exit;
27         }
28 }
29 elseif (file_exists("$request/index.html")) {
30         $staticpage = "$request/index.html";
31 }
32
33 require_once('article.inc.php');
34 $Page = new ArchiveArticle($staticpage);
35
36 if ($Page->restricted) {
37         # access restriction
38         if (!$User->login) {
39                 http_response_code(303);
40                 $target = urlencode($Page->link);
41                 header("Location: /login?goto=$target");
42                 exit;
43         }
44 }
45
46 # prepare page contents
47
48 header(sprintf('Content-Security-Policy: %s', implode('; ', [
49         "default-src 'self' 'unsafe-inline' http://cdn.ckeditor.com", # some overrides remain
50         "img-src 'self' data: http://cdn.ckeditor.com", # inline svg (in css)
51         "base-uri 'self'", # only local pages
52         "frame-ancestors 'none'", # prevent malicious embedding
53 ])));
54
55 $Page->place += [
56         'user'  => $User->login ?: '',
57         'url'   => htmlspecialchars($_SERVER['REQUEST_URI']),
58 ];
59
60 if (!isset($Page->raw) and $User->admin("edit {$Page->link}")) {
61         # open bottom template as initial contents
62         $template = 'template.inc.html';
63         if ($Page->handler and file_exists("{$Page->handler}/$template")) {
64                 $template = "{$Page->handler}/$template";
65         }
66         $Page->raw($template);
67         $Page->meta['article:published_time'] = date('Y-m-d h:i:s O');
68         $Page->meta['article:author'] = '/' . $User->dir;
69         $Page->body = NULL;
70 }
71
72 if (isset($Page->raw)) {
73         if ($User->admin("edit {$Page->link}")) {
74                 # restore meta tags in static contents for editing
75                 foreach (array_reverse($Page->meta) as $metaprop => $val) {
76                         $Page->raw = sprintf(
77                                 '<meta property="%s" content="%s">'."\n",
78                                 $metaprop, $val
79                         ) . $Page->raw;
80                 }
81         }
82         $Page->raw = '<div class="static">'."\n\n".$Page->raw."</div>\n\n";
83 }
84
85 # output dynamic and/or static html
86
87 ob_start();
88 if ($Page->handler and !require("./{$Page->handler}/index.php")) {
89         # replace contents by code output on false return
90         $Page->raw = ob_get_clean();
91 }
92 else {
93         # keep article contents
94         if (!isset($Page->body)) {
95                 # no resulting output
96                 http_response_code(404);
97                 @require '404.inc.html';
98                 $Page->raw = ob_get_clean() . $Page->raw;
99         }
100 }
101
102 include_once 'page.inc.php';
103