page: rework script control
authorMischa POSLAWSKY <perl@shiar.org>
Wed, 13 Sep 2017 11:44:27 +0000 (13:44 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Mon, 18 Sep 2017 18:21:28 +0000 (20:21 +0200)
Do not match *.html contents to subrequests, only traverse parents for *.php
scripts which now support an additional output layer for appended output.
This allows parent code to prepare how its subpages will be displayed
(for example, a news directory can surround static articles with metadata).

404.php [changed mode: 0755->0644]
edit.js
login.php
page.inc.php
page.php

diff --git a/404.php b/404.php
old mode 100755 (executable)
new mode 100644 (file)
index 0410799..a6c11bf
--- a/404.php
+++ b/404.php
@@ -1,8 +1,5 @@
-<?php include 'head.inc.php'; ?>
-
-<h2>Niet gevonden</h2>
-
-<p>
-De gevraagde pagina <em><?php echo htmlspecialchars($_SERVER['REQUEST_URI']); ?></em> bestaat niet.
-</p>
-
+<?php
+http_response_code(404);
+$request = htmlspecialchars($_SERVER['REQUEST_URI']);
+echo "<h2>Pagina niet gevonden</h2>\n\n";
+echo "<p>De gevraagde pagina <em>$request</em> bestaat niet.</p>\n\n";
diff --git a/edit.js b/edit.js
index 0db437427c0305dc980dc1697d5e290fabf58aa4..c5d6ce3198610da8a490456c0a479c9a61d888cb 100644 (file)
--- a/edit.js
+++ b/edit.js
@@ -75,7 +75,7 @@ CKEDITOR.on('instanceCreated', function (event) {
        CKEDITOR.disableAutoInline = true;
 
 // add edit link to menu
-var pagebody = document.getElementsByClassName('article')[0];
+var pagebody = document.getElementsByClassName('static')[0];
 if (pagebody) {
        var editlink = document.createElement('a');
        editlink.style.cursor = 'pointer';
index 369a01358f34276c3eb5dd7e4ec0d02e24c2a396..490422f1e11ca6dc5b6b07cf51ebdd52aae3ad62 100644 (file)
--- a/login.php
+++ b/login.php
@@ -1,3 +1,6 @@
+<?php
+include 'page.inc.php';
+?>
 <h2>Inloggen</h2>
 
 <?php
index 2c1fc6c7e201f160dbc4642a8e8d1b8fc09a0dc2..39d2008dc88823c799159b8a9c96494ba2a2d84c 100644 (file)
@@ -18,9 +18,3 @@ register_shutdown_function(function () {
        print "</body></html>\n";
 });
 
-print '<div class="article">'."\n\n";
-include "./$Page.html";
-print "</div>\n\n";
-
-if (file_exists("$Page.php")) include_once("./$Page.php");
-
index 38f54ecc285fe38381d898138da4eaf10b167ec0..f6c2346e17edbfa31ac7c654c3b603425e8675f6 100644 (file)
--- a/page.php
+++ b/page.php
@@ -5,35 +5,62 @@ ini_set('display_errors', TRUE);
 include_once 'auth.inc.php';
 $Edit = isset($_GET['edit']);
 
+# distinguish subpage Args from topmost Page script
+
 $Args = '';
 $Page = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
 $Page = urldecode(trim($Page, '/')) ?: 'index';
 while (TRUE) {
-       if (file_exists("$Page/index.html")) {
-               $Page .= '/index';
-               break;
-       }
-       if (file_exists("$Page.html")) {
-               break;
-       }
        if (file_exists("$Page.php")) {
-               # unformatted script override
-               require "$Page.php";
-               exit;
+               break;
        }
 
        $up = strrpos($Page, '/');
+       $Args = substr($Page, $up) . $Args;
+       $Page = substr($Page, 0, $up);
        if ($up === FALSE) {
-               if ($User['admin']) {
-                       $Page = 'template';
-                       break;
-               }
-               http_response_code(404);
-               $Page = '404';
                break;
        }
-       $Args = substr($Page, $up) . $Args;
-       $Page = substr($Page, 0, $up);
 }
 
-include 'page.inc.php';
+# execute dynamic code
+
+$prepend = $append = '';
+if ($Page) {
+       require "./$Page.php";
+       if (ob_get_level() > 1) $append  = ob_get_clean();
+       if (ob_get_level() > 0) $prepend = ob_get_clean();
+}
+
+# prepare static contents
+
+include_once 'page.inc.php'; # global html
+
+if (file_exists("$Page$Args/index.html")) {
+       $Args .= '/index';
+}
+
+if (!$Page and !file_exists("$Page$Args.html")) {
+       # include not found
+       $Args = '';
+       if (isset($User) and $User['admin']) {
+               $Page = 'template';
+       }
+       else {
+               $Page = '404';
+               require "./$Page.php";
+       }
+}
+
+# output prepared html
+
+print $prepend;
+
+print '<div class="static">'."\n\n";
+if (file_exists("$Page$Args.html")) {
+include "./$Page$Args.html"; # static contents
+}
+print "</div>\n\n";
+
+print $append;
+