page: store placeholder values in $Page object
[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         ob_start();
27         require '500.inc.html';
28         $Page->place['debug'] = htmlspecialchars($error);
29         print $Page->render();
30 }
31
32 set_exception_handler('fail');
33
34 define('E_FATAL', E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR);
35
36 set_error_handler(function ($level, $error, $file, $line) {
37         if ($level & E_FATAL) {
38                 fail($error);
39                 return;
40         }
41         return FALSE;
42 });
43
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]");
49 });
50
51 error_reporting(error_reporting() & ~E_FATAL);
52
53 # user login and control
54
55 include_once 'auth.inc.php'; // sets global $User
56 $Edit = isset($_GET['edit']);
57
58 # setup requested page
59
60 $request = preg_replace('/\?.*/', '', @$_SERVER['PATH_INFO'] ?: $_SERVER['REQUEST_URI']);
61 $request = urldecode(trim($request, '/')) ?: 'index';
62
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");
69                 exit;
70         }
71 }
72 elseif (file_exists("$request/index.html")) {
73         $staticpage = "$request/index.html";
74 }
75
76 require_once('article.inc.php');
77 $Page = new ArchiveArticle($staticpage);
78
79 if ($Page->restricted) {
80         # access restriction
81         if (!$User->login) {
82                 http_response_code(303);
83                 $target = urlencode($Page->link);
84                 header("Location: /login?goto=$target");
85                 exit;
86         }
87 }
88
89 # prepare page contents
90
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
96 ])));
97
98 ob_start(); # page body
99 $Page->place += [
100         'user'  => $User->login ?: '',
101         'url'   => htmlspecialchars($_SERVER['REQUEST_URI']),
102 ];
103
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";
109         }
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;
113 }
114
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",
121                                 $metaprop, $val
122                         ) . $Page->raw;
123                 }
124         }
125         $Page->raw = '<div class="static">'."\n\n".$Page->raw."</div>\n\n";
126 }
127
128 # output dynamic and/or static html
129
130 if (!$Page->handler or require("./{$Page->handler}/index.php")) {
131         # static contents
132         if (isset($Page->raw)) {
133                 print $Page->raw;
134         }
135         else {
136                 # no resulting output
137                 http_response_code(404);
138                 @require '404.inc.html';
139         }
140 }
141
142 include_once 'page.inc.php';
143