page: silence php reporting of handled fatal errors
[minimedit.git] / page.php
1 <?php
2 error_reporting(E_ALL);
3 ini_set('display_errors', TRUE);
4
5 function getoutput($blocks = [])
6 {
7         $rep = [];
8         foreach ($blocks as $name => $html) {
9                 $rep["[[$name]]"] = sprintf('<!--BLOCK:%s-->%s<!--/-->',
10                         is_numeric($name) ? '' : "[[$name]]",
11                         preg_replace('{<!--[^-]*-->}', '', $html)
12                 );
13         }
14         return str_replace(array_keys($rep), array_values($rep), ob_get_clean());
15 }
16
17 function fail($error)
18 {
19         http_response_code(500);
20         include_once 'page.inc.php';
21         ob_start();
22         require_once './500.html';
23         print getoutput(['debug' => $error]);
24 }
25 set_exception_handler('fail');
26
27 define('E_FATAL', E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR);
28 register_shutdown_function(function () {
29         # display failure page for fatal exceptions
30         $error = error_get_last();
31         if (!($error['type'] & E_FATAL)) return;
32         fail("Fatal: $error[message] in $error[file]:$error[line]");
33 });
34 error_reporting(error_reporting() & ~E_FATAL);
35
36 include_once 'auth.inc.php';
37 $Edit = isset($_GET['edit']);
38
39 # distinguish subpage Args from topmost Page script
40
41 $Args = '';
42 $Page = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
43 $Page = urldecode(trim($Page, '/')) ?: 'index';
44 while (TRUE) {
45         if (file_exists("$Page/.private")) {
46                 # access restriction
47                 if (!isset($User)) {
48                         http_response_code(403);
49                         include_once 'page.inc.php';
50                         ob_start();
51                         @require_once './403.html';
52                         exit;
53                 }
54         }
55
56         if (file_exists("$Page.php")) {
57                 break;
58         }
59
60         $up = strrpos($Page, '/');
61         $Args = substr($Page, $up) . $Args;
62         $Page = substr($Page, 0, $up);
63         if ($up === FALSE) {
64                 break;
65         }
66 }
67
68 # load static contents
69
70 ob_start(); # page body
71 ob_start(); # inner html
72 print '<div class="static">'."\n\n";
73
74 $found = FALSE;
75 if (file_exists("$Page$Args/index.html")) {
76         $found = include "./$Page$Args/index.html";
77 }
78 elseif (file_exists("$Page$Args.html")) {
79         $found = include "./$Page$Args.html";
80 }
81 elseif (isset($User) and $User['admin']) {
82         $found = include (file_exists("$Page/template.html") ? "$Page/template.html" : './template.html');
83 }
84
85 print "</div>\n\n";
86
87 # execute dynamic code
88
89 if ($Page) {
90         $found |= @require "./$Page.php";
91 }
92
93 # global html
94
95 include_once 'page.inc.php';
96
97 if (!$found) {
98         # no resulting output
99         http_response_code(404);
100         ob_start();
101         @require "./404.html";
102         print getoutput([ 'url' => htmlspecialchars($_SERVER['REQUEST_URI']) ]);
103 }
104