page: redirect location option in abort()
[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, '302 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 # user login and control
30
31 include_once 'auth.inc.php'; // sets global $User
32
33 if ($Page->restricted) {
34         # access restriction
35         if (!$User->login) {
36                 $target = urlencode($Page->link);
37                 abort("/login?goto=$target", '303 Eerst inloggen');
38         }
39 }
40
41 # prepare page contents
42
43 header(sprintf('Content-Security-Policy: %s', implode('; ', [
44         "default-src 'self' 'unsafe-inline' http://cdn.ckeditor.com", # some overrides remain
45         "img-src 'self' data: blob: http://cdn.ckeditor.com", # inline svg (in css)
46         "base-uri 'self'", # only local pages
47         "frame-ancestors 'none'", # prevent malicious embedding
48 ])));
49
50 $Page->place += [
51         'user'  => $User->login ?: '',
52         'url'   => htmlspecialchars($_SERVER['REQUEST_URI']),
53 ];
54
55 if ($User->admin("edit {$Page->link}")) {
56         include_once 'edit/head.inc.php';
57 }
58
59 if (isset($Page->raw)) {
60         $Page->raw = '<div class="static">'."\n\n".$Page->raw."</div>\n\n";
61 }
62
63 # output dynamic and/or static html
64
65 include_once 'format.inc.php';
66
67 ob_start();
68 if ($Page->handler and !require("./{$Page->handler}/index.php")) {
69         # replace contents by code output on false return
70         $Page->raw = ob_get_clean();
71 }
72 else {
73         # keep article contents
74         if (!isset($Page->body)) {
75                 # no resulting output
76                 http_response_code(404);
77                 @require '404.inc.html';
78                 $Page->raw = ob_get_clean() . $Page->raw;
79         }
80 }
81
82 include_once 'page.inc.php';
83