login/pass: error messages below page title
[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                 abort($target, '307 Shorthand');
20         }
21 }
22 elseif (file_exists("$request/index.html")) {
23         $staticpage = "$request/index.html";
24 }
25
26 require_once('article.inc.php');
27 $Page = new ArchiveArticle($staticpage);
28
29 if (@$_SERVER['HTTP_ACCEPT'] === 'text/plain') {
30         $Page->api = TRUE;
31 }
32
33 # user login and control
34
35 include_once 'auth.inc.php'; // sets global $User
36
37 if ($Page->restricted) {
38         # access restriction
39         if (!$User->login) {
40                 $target = urlencode($Page->link);
41                 abort("/login?goto=$target", '303 Eerst inloggen');
42         }
43         elseif ($check = file_get_contents("{$Page->restricted}/.private")
44         and !$User->admin(trim($check))) {
45                 http_response_code(403);
46                 $Page->raw('403.inc.html');
47         }
48 }
49
50 # prepare page contents
51
52 header(sprintf('Content-Security-Policy: %s', implode('; ', [
53         "default-src 'self' 'unsafe-inline' http://cdn.ckeditor.com", # some overrides remain
54         "img-src 'self' data: blob: http://cdn.ckeditor.com", # inline svg (in css)
55         "base-uri 'self'", # only local pages
56         "frame-ancestors 'none'", # prevent malicious embedding
57 ])));
58 header('Referrer-Policy: no-referrer-when-downgrade');
59
60 $Page->place += [
61         'user'  => $User->login ?: '',
62         'url'   => htmlspecialchars($_SERVER['REQUEST_URI']),
63 ];
64
65 if ($Page->editable = $User->admin("edit {$Page->link}")) {
66         include_once 'edit/head.inc.php';
67 }
68
69 if (isset($Page->raw)
70 and @$_SERVER['HTTP_ACCEPT'] !== 'application/xml') {
71         $Page->raw = '<div class="static">'."\n\n".$Page->raw."</div>\n\n";
72 }
73
74 # output dynamic and/or static html
75
76 include_once 'format.inc.php';
77
78 ob_start();
79 if ($Page->handler and !$Page->index($Page->api)) {
80         # replace contents by code output on false return
81         $Page->raw = ob_get_clean();
82 }
83 else {
84         # keep article contents
85         if (!isset($Page->body)) {
86                 # no resulting output
87                 http_response_code(404);
88                 @require '404.inc.html';
89                 $Page->raw = ob_get_clean() . $Page->raw;
90         }
91 }
92
93 if (@$_SERVER['HTTP_ACCEPT'] === 'application/xml') {
94         header('Access-Control-Allow-Origin: *');
95 }
96 elseif (!$Page->api) {
97         include_once 'page.inc.php';
98 }
99 print $Page->render();
100