X-Git-Url: http://git.shiar.net/minimedit.git/blobdiff_plain/772961fd4e9be46ad340cad1e55ee5f28cc5b968..v4.5-6-g50f9b29cc2:/page.php?ds=sidebyside diff --git a/page.php b/page.php index a469a9d..8d2d01c 100644 --- a/page.php +++ b/page.php @@ -8,74 +8,6 @@ function abort($body, $status = NULL) { exit; } -function placeholder_include($name, $params = []) -{ - $path = stream_resolve_include_path("widget/$name.php"); - if (!file_exists($path)) { - return ''.$name.' ontbreekt'; - } - - ob_start(); - $Page = clone $GLOBALS['Page']; - $Page->handler = $Page->handler . $Page->path; // .= with explicit getter - $Page->path = ''; - $Place = $GLOBALS['Place']; - foreach ($params as $param) { - if ($set = strpos($param, '=')) { - $Place[ substr($param, 0, $set) ] = substr($param, $set + 1); - } - elseif (!empty($param)) { - $Page->path .= '/'.$param; - } - } - $Page->link .= $Page->path; - try { - include "widget/$name.php"; - return ob_get_clean(); - } - catch (Exception $e) { - return sprintf('%s', - "fout in $name: {$e->getMessage()}" - ); - } -} - -function getoutput($blocks = []) -{ - $doc = ob_get_clean(); - - if (!empty($blocks['warn'])) { - $warn = '

[[warn]]

'; - if ($offset = strpos($doc, '')) { - $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.'">.*?}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 = "$html"; - } - $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()); @@ -91,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'); @@ -162,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']), ]; @@ -194,15 +128,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->raw)) { # no resulting output http_response_code(404); @require '404.inc.html'; + $Page->raw = ob_get_clean(); } }