X-Git-Url: http://git.shiar.nl/minimedit.git/blobdiff_plain/0c5ed43f3956281166535f0283a77195af2ae59a..c0f916285fa69edb97a6c8311f567b90b3959ed3:/page.php diff --git a/page.php b/page.php index 1f86e5b..bca7a67 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("$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 "$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,19 +51,42 @@ 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 = empty($User) ? 'login' : 'logout'; + $doc = preg_replace('{\s*<(p|li|span) class="'.$hideclass.'">.*?}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) { http_response_code(500); include_once 'page.inc.php'; ob_start(); - require_once DOCROOT.'500.html'; + require_once '500.html'; print getoutput(['debug' => $error]); } @@ -80,7 +133,7 @@ while (TRUE) { $PageAccess = $Page; } - if (file_exists("$Page.php")) { + if (file_exists("$Page/index.php")) { break; } @@ -92,6 +145,23 @@ while (TRUE) { } } +$staticpage = NULL; +if (file_exists("$Page$Args.html")) { + $staticpage = "$Page$Args.html"; + if (is_link($staticpage)) { + $target = preg_replace('/\.html$/', '', readlink($staticpage)); + header("HTTP/1.1 302 Shorthand"); + header("Location: $target"); + exit; + } +} +elseif (file_exists("$Page$Args/index.html")) { + $staticpage = "$Page$Args/index.html"; +} +elseif (!empty($User['admin'])) { + $staticpage = (file_exists("$Page/template.html") ? "$Page/template.html" : 'template.html'); +} + # load static contents ob_start(); # page body @@ -99,33 +169,32 @@ ob_start(); # inner html print '
'."\n\n"; $found = FALSE; -if (file_exists("$Page$Args.html")) { - $found = include "./$Page$Args.html"; -} -elseif (file_exists("$Page$Args/index.html")) { - $found = include "./$Page$Args/index.html"; -} -elseif (!empty($User['admin'])) { - $found = include (file_exists("$Page/template.html") ? "$Page/template.html" : './template.html'); +if (isset($staticpage)) { + $found = include "./$staticpage"; } print "
\n\n"; # execute dynamic code +$Place = []; + if ($Page) { - $found |= require "./$Page.php"; + $found |= require "./$Page/index.php"; } -# global html +$Place += [ + 'user' => empty($User) ? '' : $User['name'], + 'url' => htmlspecialchars($_SERVER['REQUEST_URI']), +]; -include_once 'page.inc.php'; +# global html if (!$found) { # no resulting output http_response_code(404); - ob_start(); - @require "./404.html"; - print getoutput([ 'url' => htmlspecialchars($_SERVER['REQUEST_URI']) ]); + @require '404.html'; } +include_once 'page.inc.php'; +