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