X-Git-Url: http://git.shiar.nl/minimedit.git/blobdiff_plain/903ef7aadee5cee917ce10f775a47bcf77658bae..4486f69521f03fe56549394f13f7c529f57fa962:/page.php diff --git a/page.php b/page.php index 856a08a..35a810a 100644 --- a/page.php +++ b/page.php @@ -2,72 +2,86 @@ error_reporting(E_ALL); ini_set('display_errors', TRUE); -set_exception_handler(function ($error) { - http_response_code(500); - include_once 'page.inc.php'; - ob_start(); - require_once './500.html'; - print str_replace('[[debug]]', $error, ob_get_clean()); -}); - -include_once 'auth.inc.php'; -$Edit = isset($_GET['edit']); - -# distinguish subpage Args from topmost Page script - -$Args = ''; -$Page = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']); -$Page = urldecode(trim($Page, '/')) ?: 'index'; -while (TRUE) { - if (file_exists("$Page.php")) { - break; - } +define('DOCROOT', getcwd()); +set_include_path(implode(PATH_SEPARATOR, [ DOCROOT, __DIR__ ])); + +include_once 'error.inc.php'; + +# setup requested page + +$request = preg_replace('/\?.*/', '', @$_SERVER['PATH_INFO'] ?: $_SERVER['REQUEST_URI']); +$request = urldecode(trim($request, '/')) ?: 'index'; - $up = strrpos($Page, '/'); - $Args = substr($Page, $up) . $Args; - $Page = substr($Page, 0, $up); - if ($up === FALSE) { - break; +$staticpage = "$request.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("$request/index.html")) { + $staticpage = "$request/index.html"; +} -# load static contents +require_once('article.inc.php'); +$Page = new ArchiveArticle($staticpage); -ob_start(); # page body -ob_start(); # inner html -print '
'."\n\n"; +# user login and control -$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"; +include_once 'auth.inc.php'; // sets global $User + +if ($Page->restricted) { + # access restriction + if (!$User->login) { + http_response_code(303); + $target = urlencode($Page->link); + header("Location: /login?goto=$target"); + exit; + } } -print "
\n\n"; +# prepare page contents -# execute dynamic code +header(sprintf('Content-Security-Policy: %s', implode('; ', [ + "default-src 'self' 'unsafe-inline' http://cdn.ckeditor.com", # some overrides remain + "img-src 'self' data: blob: http://cdn.ckeditor.com", # inline svg (in css) + "base-uri 'self'", # only local pages + "frame-ancestors 'none'", # prevent malicious embedding +]))); -if ($Page) { - $found |= require "./$Page.php"; +$Page->place += [ + 'user' => $User->login ?: '', + 'url' => htmlspecialchars($_SERVER['REQUEST_URI']), +]; + +if ($User->admin("edit {$Page->link}")) { + include_once 'edit/head.inc.php'; } -# global html +if (isset($Page->raw)) { + $Page->raw = '
'."\n\n".$Page->raw."
\n\n"; +} -include_once 'page.inc.php'; +# output dynamic and/or static html -if (!$found) { - # no resulting output - if (isset($User) and $User['admin']) { - require './template.html'; - } - else { +include_once 'format.inc.php'; + +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); - ob_start(); - require "./404.html"; - $url = htmlspecialchars($_SERVER['REQUEST_URI']); - print str_replace('[[url]]', $url, ob_get_clean()); + @require '404.inc.html'; + $Page->raw = ob_get_clean() . $Page->raw; } } +include_once 'page.inc.php'; +