page: rework script control
[minimedit.git] / 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;
+