page: prepare static output before dynamic code
[minimedit.git] / page.php
index 38f54ecc285fe38381d898138da4eaf10b167ec0..cb201f2f46fe32516fbdd61611a84fe0ce9cc0d2 100644 (file)
--- a/page.php
+++ b/page.php
@@ -2,38 +2,65 @@
 error_reporting(E_ALL);
 ini_set('display_errors', TRUE);
 
+set_exception_handler(function ($error) {
+       include_once 'page.inc.php';
+       include_once '500.php';
+});
+
 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';
+# load static contents
+
+ob_start(); # page body
+ob_start(); # inner html
+print '<div class="static">'."\n\n";
+
+$found = FALSE;
+if (file_exists("$Page$Args/index.html")) {
+       $found = include "./$Page$Args/index.html";
+}
+elseif (file_exists("$Page$Args.html")) {
+       $found = include "./$Page$Args.html";
+}
+
+print "</div>\n\n";
+
+# execute dynamic code
+
+if ($Page) {
+       $found |= require "./$Page.php";
+}
+
+# global html
+
+include_once 'page.inc.php';
+
+if (!$found) {
+       # no resulting output
+       if (isset($User) and $User['admin']) {
+               require './template.html';
+       }
+       else {
+               require "./404.php";
+       }
+}
+