page: save granted access for admin options
[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 # custom error handling
18
19 define('DOCROOT', getcwd().'/');
20
21 function fail($error)
22 {
23         http_response_code(500);
24         include_once 'page.inc.php';
25         ob_start();
26         require_once DOCROOT.'500.html';
27         print getoutput(['debug' => $error]);
28 }
29
30 set_exception_handler('fail');
31
32 define('E_FATAL', E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR);
33
34 set_error_handler(function ($level, $error, $file, $line) {
35         if ($level & E_FATAL) {
36                 fail($error);
37                 return;
38         }
39         return FALSE;
40 });
41
42 register_shutdown_function(function () {
43         # display failure page for fatal exceptions
44         $error = error_get_last();
45         if (!($error['type'] & E_FATAL)) return;
46         fail("Fatal: $error[message] in $error[file]:$error[line]");
47 });
48
49 error_reporting(error_reporting() & ~E_FATAL);
50
51 # user login and control
52
53 include_once 'auth.inc.php';
54 $Edit = isset($_GET['edit']);
55
56 # distinguish subpage Args from topmost Page script
57
58 $Args = '';
59 $Page = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
60 $Page = urldecode(trim($Page, '/')) ?: 'index';
61 while (TRUE) {
62         if (file_exists("$Page/.private")) {
63                 # access restriction
64                 if (!isset($User)) {
65                         http_response_code(403);
66                         include_once 'page.inc.php';
67                         ob_start();
68                         @require_once './403.html';
69                         exit;
70                 }
71                 $PageAccess = $Page;
72         }
73
74         if (file_exists("$Page.php")) {
75                 break;
76         }
77
78         $up = strrpos($Page, '/');
79         $Args = substr($Page, $up) . $Args;
80         $Page = substr($Page, 0, $up);
81         if ($up === FALSE) {
82                 break;
83         }
84 }
85
86 # load static contents
87
88 ob_start(); # page body
89 ob_start(); # inner html
90 print '<div class="static">'."\n\n";
91
92 $found = FALSE;
93 if (file_exists("$Page$Args/index.html")) {
94         $found = include "./$Page$Args/index.html";
95 }
96 elseif (file_exists("$Page$Args.html")) {
97         $found = include "./$Page$Args.html";
98 }
99 elseif (isset($User) and $User['admin']) {
100         $found = include (file_exists("$Page/template.html") ? "$Page/template.html" : './template.html');
101 }
102
103 print "</div>\n\n";
104
105 # execute dynamic code
106
107 if ($Page) {
108         $found |= require "./$Page.php";
109 }
110
111 # global html
112
113 include_once 'page.inc.php';
114
115 if (!$found) {
116         # no resulting output
117         http_response_code(404);
118         ob_start();
119         @require "./404.html";
120         print getoutput([ 'url' => htmlspecialchars($_SERVER['REQUEST_URI']) ]);
121 }
122