page: prepare static output before dynamic code
authorMischa POSLAWSKY <perl@shiar.org>
Wed, 13 Sep 2017 19:51:47 +0000 (21:51 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Mon, 18 Sep 2017 18:21:28 +0000 (20:21 +0200)
Give script includes full access to prepared html, greatly simplifying
buffer logic and allowing potential substitutions.  Assumes small pages
since all data flushes are delayed (can be worked around if ever needed).

404.php
500.php
edit.php
page.inc.php
page.php

diff --git a/404.php b/404.php
index a6c11bfaa83ebf3d60ae9ecb7a6f1605da7cb327..f6e1489a4ab4db5743e8df6f36b125c459869be0 100644 (file)
--- a/404.php
+++ b/404.php
@@ -1,5 +1,7 @@
 <?php
+$extra = ob_get_clean();
 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";
+echo $extra;
diff --git a/500.php b/500.php
index a3e261c1aa8334d860008d05d4727afe76b4abec..c2ae65538721f95e309de96cb84a448df787c40e 100644 (file)
--- a/500.php
+++ b/500.php
@@ -1,4 +1,5 @@
 <?php
+$extra = ob_get_clean();
 http_response_code(500);
 ?>
 <h2>Scriptfout</h2>
@@ -6,3 +7,5 @@ http_response_code(500);
 <p>Er ging iets mis: <pre class="warn"><?php print $error; ?></pre>
 Waarschijnlijk wordt hier aan gewerkt, maar als het probleem aanhoudt
 geef het dan aan <a href="/contact">ons</a> door.
+<?php
+print $extra;
index cf9b6c07e9f79e95ce7830ba1a56e774fd762fd1..2a63bd2d4f865d1fd1c9dba95689ce40e2a15039 100644 (file)
--- a/edit.php
+++ b/edit.php
@@ -1,4 +1,6 @@
 <?php
+ob_clean();
+
 function abort($status, $body) {
        header("HTTP/1.1 $status");
        print "$body\n";
index 39d2008dc88823c799159b8a9c96494ba2a2d84c..bc2c57d5de5df8ad8f92138e234d8d8d2803cab7 100644 (file)
@@ -1,4 +1,6 @@
 <?php
+$body = ob_get_clean();
+
 include_once './head.inc.php';
 
 print "<header>\n";
@@ -13,6 +15,8 @@ $nav = preg_replace_callback('{<a href="([^"]+)">(.*?)</a>}', function ($m) {
 print $nav;
 print "</header>\n\n";
 
+print $body;
+
 register_shutdown_function(function () {
        include 'foot.inc.php';
        print "</body></html>\n";
index 9acacc2a949a87cf39e23079615ecb71d8125e66..cb201f2f46fe32516fbdd61611a84fe0ce9cc0d2 100644 (file)
--- a/page.php
+++ b/page.php
@@ -28,44 +28,39 @@ while (TRUE) {
        }
 }
 
-# execute dynamic code
+# load static contents
 
-$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();
+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";
 }
 
-# prepare static contents
+print "</div>\n\n";
 
-include_once 'page.inc.php'; # global html
+# execute dynamic code
 
-if (file_exists("$Page$Args/index.html")) {
-       $Args .= '/index';
+if ($Page) {
+       $found |= require "./$Page.php";
 }
 
-if (!$Page and !file_exists("$Page$Args.html")) {
-       # include not found
-       $Args = '';
+# global html
+
+include_once 'page.inc.php';
+
+if (!$found) {
+       # no resulting output
        if (isset($User) and $User['admin']) {
-               $Page = 'template';
+               require './template.html';
        }
        else {
-               $Page = '404';
-               require "./$Page.php";
+               require "./404.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;
-