page: prepare static output before dynamic code
[minimedit.git] / page.php
1 <?php
2 error_reporting(E_ALL);
3 ini_set('display_errors', TRUE);
4
5 set_exception_handler(function ($error) {
6         include_once 'page.inc.php';
7         include_once '500.php';
8 });
9
10 include_once 'auth.inc.php';
11 $Edit = isset($_GET['edit']);
12
13 # distinguish subpage Args from topmost Page script
14
15 $Args = '';
16 $Page = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
17 $Page = urldecode(trim($Page, '/')) ?: 'index';
18 while (TRUE) {
19         if (file_exists("$Page.php")) {
20                 break;
21         }
22
23         $up = strrpos($Page, '/');
24         $Args = substr($Page, $up) . $Args;
25         $Page = substr($Page, 0, $up);
26         if ($up === FALSE) {
27                 break;
28         }
29 }
30
31 # load static contents
32
33 ob_start(); # page body
34 ob_start(); # inner html
35 print '<div class="static">'."\n\n";
36
37 $found = FALSE;
38 if (file_exists("$Page$Args/index.html")) {
39         $found = include "./$Page$Args/index.html";
40 }
41 elseif (file_exists("$Page$Args.html")) {
42         $found = include "./$Page$Args.html";
43 }
44
45 print "</div>\n\n";
46
47 # execute dynamic code
48
49 if ($Page) {
50         $found |= require "./$Page.php";
51 }
52
53 # global html
54
55 include_once 'page.inc.php';
56
57 if (!$found) {
58         # no resulting output
59         if (isset($User) and $User['admin']) {
60                 require './template.html';
61         }
62         else {
63                 require "./404.php";
64         }
65 }
66