login/pass: error messages below page title
[minimedit.git] / page.php
index c88852416c9ccc531b015ce25eec163157d1e452..30f0e3c59c2e645bd492b1b6f900b50d82a60f8c 100644 (file)
--- a/page.php
+++ b/page.php
@@ -2,33 +2,99 @@
 error_reporting(E_ALL);
 ini_set('display_errors', TRUE);
 
-$Args = '';
-$Page = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
-$Page = urldecode(trim($Page, '/')) ?: 'index';
-while (TRUE) {
-       if (file_exists("$Page/index.html")) {
-               $Page .= '/index';
-               break;
+define('DOCROOT', getcwd());
+set_include_path(implode(PATH_SEPARATOR, [ DOCROOT, __DIR__ ]));
+
+include_once 'error.inc.php';
+
+# setup requested page
+
+$request = preg_replace('/\?.*/', '', @$_SERVER['PATH_INFO'] ?: $_SERVER['REQUEST_URI']);
+$request = urldecode(trim($request, '/')) ?: 'index';
+
+$staticpage = "$request.html";
+if (file_exists($staticpage)) {
+       if (is_link($staticpage)) {
+               $target = preg_replace('/\.html$/', '', readlink($staticpage));
+               abort($target, '307 Shorthand');
        }
-       if (file_exists("$Page.html")) {
-               break;
+}
+elseif (file_exists("$request/index.html")) {
+       $staticpage = "$request/index.html";
+}
+
+require_once('article.inc.php');
+$Page = new ArchiveArticle($staticpage);
+
+if (@$_SERVER['HTTP_ACCEPT'] === 'text/plain') {
+       $Page->api = TRUE;
+}
+
+# user login and control
+
+include_once 'auth.inc.php'; // sets global $User
+
+if ($Page->restricted) {
+       # access restriction
+       if (!$User->login) {
+               $target = urlencode($Page->link);
+               abort("/login?goto=$target", '303 Eerst inloggen');
        }
-       if (file_exists("$Page.php")) {
-               # unformatted script override
-               require "$Page.php";
-               exit;
+       elseif ($check = file_get_contents("{$Page->restricted}/.private")
+       and !$User->admin(trim($check))) {
+               http_response_code(403);
+               $Page->raw('403.inc.html');
        }
+}
 
-       $up = strrpos($Page, '/');
-       if ($up === FALSE) {
+# prepare page contents
+
+header(sprintf('Content-Security-Policy: %s', implode('; ', [
+       "default-src 'self' 'unsafe-inline' http://cdn.ckeditor.com", # some overrides remain
+       "img-src 'self' data: blob: http://cdn.ckeditor.com", # inline svg (in css)
+       "base-uri 'self'", # only local pages
+       "frame-ancestors 'none'", # prevent malicious embedding
+])));
+header('Referrer-Policy: no-referrer-when-downgrade');
+
+$Page->place += [
+       'user'  => $User->login ?: '',
+       'url'   => htmlspecialchars($_SERVER['REQUEST_URI']),
+];
+
+if ($Page->editable = $User->admin("edit {$Page->link}")) {
+       include_once 'edit/head.inc.php';
+}
+
+if (isset($Page->raw)
+and @$_SERVER['HTTP_ACCEPT'] !== 'application/xml') {
+       $Page->raw = '<div class="static">'."\n\n".$Page->raw."</div>\n\n";
+}
+
+# output dynamic and/or static html
+
+include_once 'format.inc.php';
+
+ob_start();
+if ($Page->handler and !$Page->index($Page->api)) {
+       # replace contents by code output on false return
+       $Page->raw = ob_get_clean();
+}
+else {
+       # keep article contents
+       if (!isset($Page->body)) {
+               # no resulting output
                http_response_code(404);
-               $Page = '404';
-               break;
+               @require '404.inc.html';
+               $Page->raw = ob_get_clean() . $Page->raw;
        }
-       $Args = substr($Page, $up) . $Args;
-       $Page = substr($Page, 0, $up);
 }
 
-$Edit = isset($_GET['edit']);
+if (@$_SERVER['HTTP_ACCEPT'] === 'application/xml') {
+       header('Access-Control-Allow-Origin: *');
+}
+elseif (!$Page->api) {
+       include_once 'page.inc.php';
+}
+print $Page->render();
 
-include 'page.inc.php';