error_reporting(E_ALL);
ini_set('display_errors', TRUE);
-set_exception_handler(function ($error) {
- include_once 'page.inc.php';
- include_once '500.php';
-});
-
-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";
+}
+
+require_once('article.inc.php');
+$Page = new ArchiveArticle($staticpage);
+
+# user login and control
-# execute dynamic code
+include_once 'auth.inc.php'; // sets global $User
-$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 ($Page->restricted) {
+ # access restriction
+ if (!$User->login) {
+ http_response_code(303);
+ $target = urlencode($Page->link);
+ header("Location: /login?goto=$target");
+ exit;
+ }
}
-# prepare static contents
+# 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: http://cdn.ckeditor.com", # inline svg (in css)
+ "base-uri 'self'", # only local pages
+ "frame-ancestors 'none'", # prevent malicious embedding
+])));
-include_once 'page.inc.php'; # global html
+$Page->place += [
+ 'user' => $User->login ?: '',
+ 'url' => htmlspecialchars($_SERVER['REQUEST_URI']),
+];
-if (file_exists("$Page$Args/index.html")) {
- $Args .= '/index';
+if ($User->admin("edit {$Page->link}")) {
+ include_once 'edit/head.inc.php';
}
-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";
- }
+if (isset($Page->raw)) {
+ $Page->raw = '<div class="static">'."\n\n".$Page->raw."</div>\n\n";
}
-# output prepared html
+# output dynamic and/or static html
-print $prepend;
+include_once 'format.inc.php';
-print '<div class="static">'."\n\n";
-if (file_exists("$Page$Args.html")) {
-include "./$Page$Args.html"; # static contents
+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);
+ @require '404.inc.html';
+ $Page->raw = ob_get_clean() . $Page->raw;
+ }
}
-print "</div>\n\n";
-print $append;
+include_once 'page.inc.php';