page: return 500 status and page on php exceptions
[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 # execute dynamic code
32
33 $prepend = $append = '';
34 if ($Page) {
35         require "./$Page.php";
36         if (ob_get_level() > 1) $append  = ob_get_clean();
37         if (ob_get_level() > 0) $prepend = ob_get_clean();
38 }
39
40 # prepare static contents
41
42 include_once 'page.inc.php'; # global html
43
44 if (file_exists("$Page$Args/index.html")) {
45         $Args .= '/index';
46 }
47
48 if (!$Page and !file_exists("$Page$Args.html")) {
49         # include not found
50         $Args = '';
51         if (isset($User) and $User['admin']) {
52                 $Page = 'template';
53         }
54         else {
55                 $Page = '404';
56                 require "./$Page.php";
57         }
58 }
59
60 # output prepared html
61
62 print $prepend;
63
64 print '<div class="static">'."\n\n";
65 if (file_exists("$Page$Args.html")) {
66 include "./$Page$Args.html"; # static contents
67 }
68 print "</div>\n\n";
69
70 print $append;
71