edit/page: copy static contents before dynamic alterations
[minimedit.git] / page.php
index d5c78cdecc9005285a2df589197c6cd087c4453a..ba5830692e0eeaf65705b962c866e57e5a8a2c59 100644 (file)
--- a/page.php
+++ b/page.php
@@ -8,42 +8,6 @@ function abort($body, $status = NULL) {
        exit;
 }
 
-function getoutput($blocks = [])
-{
-       $doc = ob_get_clean();
-
-       if (!empty($blocks['warn'])) {
-               $warn = '<p class="warn">[[warn]]</p>';
-               if ($offset = strpos($doc, '</h2>')) {
-                       $doc = substr_replace($doc, "\n\n".$warn, $offset + 5, 0);
-               }
-               else {
-                       $doc = $warn . "\n\n" . $doc;
-               }
-       }
-
-       # keep either login or logout parts depending on user level
-       global $User;
-       $hideclass = $User && property_exists($User, 'login') && $User->login ? 'logout' : 'login';
-       $doc = preg_replace('{\s*<([a-z]+) class="'.$hideclass.'">.*?</\1>}s', '', $doc);
-
-       return preg_replace_callback(
-               '{ \[\[ ([^] ]+) ([^]]*) \]\] }x',
-               function ($sub) use ($blocks) {
-                       list ($placeholder, $name, $params) = $sub;
-                       $html = $blocks[$name] ??
-                               placeholder_include($name, explode(' ', $params));
-                       if (empty($html) or $html[0] != '<') {
-                               $html = "<span>$html</span>";
-                       }
-                       $attr = sprintf(' data-dyn="%s"', is_numeric($name) ? '' : $name.$params);
-                       # contents with identifier in first tag
-                       return preg_replace( '/(?=>)/', $attr, $html, 1);
-               },
-               $doc
-       );
-}
-
 # custom error handling
 
 define('DOCROOT', getcwd());
@@ -59,9 +23,12 @@ function fail($error)
                $Page->title = 'Fout';
        }
        include_once 'page.inc.php';
+
        ob_start();
        require '500.inc.html';
-       print getoutput(['debug' => htmlspecialchars($error)]);
+       $Page->place['debug'] = htmlspecialchars($error);
+       $Page->raw = ob_get_clean();
+       print $Page->render();
 }
 
 set_exception_handler('fail');
@@ -130,8 +97,7 @@ header(sprintf('Content-Security-Policy: %s', implode('; ', [
        "frame-ancestors 'none'", # prevent malicious embedding
 ])));
 
-ob_start(); # page body
-$Place = [
+$Page->place += [
        'user'  => $User->login ?: '',
        'url'   => htmlspecialchars($_SERVER['REQUEST_URI']),
 ];
@@ -145,6 +111,7 @@ if (!isset($Page->raw) and $User->admin("edit {$Page->link}")) {
        $Page->raw($template);
        $Page->meta['article:published_time'] = date('Y-m-d h:i:s O');
        $Page->meta['article:author'] = '/' . $User->dir;
+       $Page->body = NULL;
 }
 
 if (isset($Page->raw)) {
@@ -162,15 +129,18 @@ if (isset($Page->raw)) {
 
 # output dynamic and/or static html
 
-if (!$Page->handler or require("./{$Page->handler}/index.php")) {
-       # static contents
-       if (isset($Page->raw)) {
-               print $Page->raw;
-       }
-       else {
+ob_start();
+if ($Page->handler and !require("./{$Page->handler}/index.php")) {
+       # 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);
                @require '404.inc.html';
+               $Page->raw = ob_get_clean() . $Page->raw;
        }
 }