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