X-Git-Url: http://git.shiar.nl/minimedit.git/blobdiff_plain/2b56d0fa69a4608fb1b582fca5f08d343f5b90eb..c173b349324dff9933e38a62ca14342430c75e51:/page.php diff --git a/page.php b/page.php index f6c2346..8a37533 100644 --- a/page.php +++ b/page.php @@ -2,65 +2,93 @@ error_reporting(E_ALL); ini_set('display_errors', TRUE); -include_once 'auth.inc.php'; -$Edit = isset($_GET['edit']); +define('DOCROOT', getcwd()); +set_include_path(implode(PATH_SEPARATOR, [ DOCROOT, __DIR__ ])); -# distinguish subpage Args from topmost Page script +include_once 'error.inc.php'; -$Args = ''; -$Page = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']); -$Page = urldecode(trim($Page, '/')) ?: 'index'; -while (TRUE) { - if (file_exists("$Page.php")) { - break; - } +# 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)); + abort($target, '302 Shorthand'); } } +elseif (file_exists("$request/index.html")) { + $staticpage = "$request/index.html"; +} -# execute dynamic code +require_once('article.inc.php'); +$Page = new ArchiveArticle($staticpage); -$prepend = $append = ''; -if ($Page) { - require "./$Page.php"; - if (ob_get_level() > 1) $append = ob_get_clean(); - if (ob_get_level() > 0) $prepend = ob_get_clean(); +if (@$_SERVER['HTTP_ACCEPT'] === 'text/plain') { + $Page->api = TRUE; } -# prepare static contents +# user login and control -include_once 'page.inc.php'; # global html +include_once 'auth.inc.php'; // sets global $User -if (file_exists("$Page$Args/index.html")) { - $Args .= '/index'; +if ($Page->restricted) { + # access restriction + if (!$User->login) { + $target = urlencode($Page->link); + abort("/login?goto=$target", '303 Eerst inloggen'); + } } -if (!$Page and !file_exists("$Page$Args.html")) { - # include not found - $Args = ''; - if (isset($User) and $User['admin']) { - $Page = 'template'; - } - else { - $Page = '404'; - require "./$Page.php"; - } +# prepare page contents + +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 +]))); + +$Page->place += [ + 'user' => $User->login ?: '', + 'url' => htmlspecialchars($_SERVER['REQUEST_URI']), +]; + +if ($Page->editable = $User->admin("edit {$Page->link}")) { + include_once 'edit/head.inc.php'; +} + +if (isset($Page->raw) +and @$_SERVER['HTTP_ACCEPT'] !== 'application/xml') { + $Page->raw = '
'."\n\n".$Page->raw."
\n\n"; } -# output prepared html +# output dynamic and/or static html -print $prepend; +include_once 'format.inc.php'; -print '
'."\n\n"; -if (file_exists("$Page$Args.html")) { -include "./$Page$Args.html"; # static contents +ob_start(); +if ($Page->handler and !$Page->index($Page->api)) { + # 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; + } } -print "
\n\n"; -print $append; +if (@$_SERVER['HTTP_ACCEPT'] === 'application/xml') { + header('Access-Control-Allow-Origin: *'); +} +elseif (!$Page->api) { + include_once 'page.inc.php'; +} +print $Page->render();