X-Git-Url: http://git.shiar.net/minimedit.git/blobdiff_plain/8302e2ddeac5e241ad3f8bfaea0dbdfd069f26ef..088e48c44ae79498f459e79dcf52ff02b2467772:/page.php diff --git a/page.php b/page.php index edb074e..ed0ec1b 100644 --- a/page.php +++ b/page.php @@ -2,16 +2,46 @@ error_reporting(E_ALL); ini_set('display_errors', TRUE); -function getoutput($blocks = []) +function abort($body, $status = NULL) { + if ($status) header("HTTP/1.1 $status"); + print "$body\n"; + exit; +} + +function placeholder_include($name, $params = []) { - $rep = []; - foreach ($blocks as $name => $html) { - $rep["[[$name]]"] = sprintf('%s', - is_numeric($name) ? '' : "[[$name]]", - preg_replace('{}', '', $html) + $path = stream_resolve_include_path("widget/$name.php"); + if (!file_exists($path)) { + return ''.$name.' ontbreekt'; + } + + ob_start(); + $Page = $GLOBALS['Page'] . $GLOBALS['Args']; + $Args = ''; + $Place = $GLOBALS['Place']; + foreach ($params as $param) { + if ($set = strpos($param, '=')) { + $Place[ substr($param, 0, $set) ] = substr($param, $set + 1); + } + elseif (!empty($param)) { + $Args .= '/'.$param; + } + } + 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, '')) { @@ -21,20 +51,49 @@ function getoutput($blocks = []) $doc = $warn . "\n\n" . $doc; } } - return str_replace(array_keys($rep), array_values($rep), $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( + '{ (?%s', + is_numeric($name) ? '' : $placeholder, # edit replacement + preg_replace('{}', '', $html) # contents + ); + }, + $doc + ); } # custom error handling -define('DOCROOT', getcwd().'/'); +define('DOCROOT', getcwd()); +set_include_path(implode(PATH_SEPARATOR, [ DOCROOT, __DIR__ ])); function fail($error) { + global $User, $Page, $Args; http_response_code(500); + if (!isset($Article)) { + require_once('article.inc.php'); + $Article = new ArchiveArticle(NULL); + $Article->title = 'Fout'; + } include_once 'page.inc.php'; ob_start(); - require_once DOCROOT.'500.html'; - print getoutput(['debug' => $error]); + require '500.inc.html'; + print getoutput(['debug' => htmlspecialchars($error)]); } set_exception_handler('fail'); @@ -60,72 +119,90 @@ error_reporting(error_reporting() & ~E_FATAL); # user login and control -include_once 'auth.inc.php'; +include_once 'auth.inc.php'; // sets global $User $Edit = isset($_GET['edit']); -# distinguish subpage Args from topmost Page script +# setup requested page $Args = ''; $Page = preg_replace('/\?.*/', '', @$_SERVER['PATH_INFO'] ?: $_SERVER['REQUEST_URI']); $Page = urldecode(trim($Page, '/')) ?: 'index'; -while (TRUE) { - if (file_exists("$Page/.private")) { - # access restriction - if (empty($User)) { - http_response_code(303); - $target = urlencode($_SERVER['REQUEST_URI']); - header("Location: /login?goto=$target"); - exit; - } - $PageAccess = $Page; - } - if (file_exists("$Page.php")) { - break; +$staticpage = "$Page.html"; +if (file_exists($staticpage)) { + if (is_link($staticpage)) { + $target = preg_replace('/\.html$/', '', readlink($staticpage)); + header("HTTP/1.1 302 Shorthand"); + header("Location: $target"); + exit; } +} +elseif (file_exists("$Page/index.html")) { + $staticpage = "$Page/index.html"; +} + +require_once('article.inc.php'); +$Article = new ArchiveArticle($staticpage); - $up = strrpos($Page, '/'); - $Args = substr($Page, $up) . $Args; - $Page = substr($Page, 0, $up); - if ($up === FALSE) { - break; +$Page = $Article->handler; +$Args = $Article->path; + +if ($PageAccess = $Article->restricted) { + # access restriction + if (!$User->login) { + http_response_code(303); + $target = urlencode($Article->link); + header("Location: /login?goto=$target"); + exit; } } -# load static contents +# prepare page contents -ob_start(); # page body -ob_start(); # inner html -print '