page: include controllers from index.php
[minimedit.git] / page.php
1 <?php
2 error_reporting(E_ALL);
3 ini_set('display_errors', TRUE);
4
5 function abort($body, $status = NULL) {
6         if ($status) header("HTTP/1.1 $status");
7         print "$body\n";
8         exit;
9 }
10
11 function getoutput($blocks = [])
12 {
13         $doc = ob_get_clean();
14
15         if (!empty($blocks['warn'])) {
16                 $warn = '<p class="warn">[[warn]]</p>';
17                 if ($offset = strpos($doc, '</h2>')) {
18                         $doc = substr_replace($doc, "\n\n".$warn, $offset + 5, 0);
19                 }
20                 else {
21                         $doc = $warn . "\n\n" . $doc;
22                 }
23         }
24
25         return preg_replace_callback(
26                 '< \[\[ ([^]]*) \]\] >x',
27                 function ($sub) use ($blocks) {
28                         list ($placeholder, $name) = $sub;
29                         if (isset($blocks[$name])) {
30                                 $html = $blocks[$name];
31                         }
32                         else {
33                                 $html = '<strong class="warn"><em>'.$name.'</em> ontbreekt</strong>';
34                         }
35                         return sprintf('<!--BLOCK:%s-->%s<!--/-->',
36                                 is_numeric($name) ? '' : $placeholder, # edit replacement
37                                 preg_replace('{<!--[^-]*-->}', '', $html) # contents
38                         );
39                 },
40                 $doc
41         );
42 }
43
44 # custom error handling
45
46 define('DOCROOT', getcwd().'/');
47
48 function fail($error)
49 {
50         http_response_code(500);
51         include_once 'page.inc.php';
52         ob_start();
53         require_once DOCROOT.'500.html';
54         print getoutput(['debug' => $error]);
55 }
56
57 set_exception_handler('fail');
58
59 define('E_FATAL', E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR);
60
61 set_error_handler(function ($level, $error, $file, $line) {
62         if ($level & E_FATAL) {
63                 fail($error);
64                 return;
65         }
66         return FALSE;
67 });
68
69 register_shutdown_function(function () {
70         # display failure page for fatal exceptions
71         $error = error_get_last();
72         if (!($error['type'] & E_FATAL)) return;
73         fail("Fatal: $error[message] in $error[file]:$error[line]");
74 });
75
76 error_reporting(error_reporting() & ~E_FATAL);
77
78 # user login and control
79
80 include_once 'auth.inc.php';
81 $Edit = isset($_GET['edit']);
82
83 # distinguish subpage Args from topmost Page script
84
85 $Args = '';
86 $Page = preg_replace('/\?.*/', '', @$_SERVER['PATH_INFO'] ?: $_SERVER['REQUEST_URI']);
87 $Page = urldecode(trim($Page, '/')) ?: 'index';
88 while (TRUE) {
89         if (file_exists("$Page/.private")) {
90                 # access restriction
91                 if (empty($User)) {
92                         http_response_code(303);
93                         $target = urlencode($_SERVER['REQUEST_URI']);
94                         header("Location: /login?goto=$target");
95                         exit;
96                 }
97                 $PageAccess = $Page;
98         }
99
100         if (file_exists("$Page/index.php")) {
101                 break;
102         }
103
104         $up = strrpos($Page, '/');
105         $Args = substr($Page, $up) . $Args;
106         $Page = substr($Page, 0, $up);
107         if ($up === FALSE) {
108                 break;
109         }
110 }
111
112 # load static contents
113
114 ob_start(); # page body
115 ob_start(); # inner html
116 print '<div class="static">'."\n\n";
117
118 $found = FALSE;
119 if (file_exists("$Page$Args.html")) {
120         $found = include "./$Page$Args.html";
121 }
122 elseif (file_exists("$Page$Args/index.html")) {
123         $found = include "./$Page$Args/index.html";
124 }
125 elseif (!empty($User['admin'])) {
126         $found = include (file_exists("$Page/template.html") ? "$Page/template.html" : './template.html');
127 }
128
129 print "</div>\n\n";
130
131 # execute dynamic code
132
133 if ($Page) {
134         $found |= require "./$Page/index.php";
135 }
136
137 # global html
138
139 include_once 'page.inc.php';
140
141 if (!$found) {
142         # no resulting output
143         http_response_code(404);
144         ob_start();
145         @require "./404.html";
146         print getoutput([ 'url' => htmlspecialchars($_SERVER['REQUEST_URI']) ]);
147 }
148