consistently use empty() to check user existence
[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['PATH_INFO'] ?: $_SERVER['REQUEST_URI']);
60 $Page = urldecode(trim($Page, '/')) ?: 'index';
61 while (TRUE) {
62         if (file_exists("$Page/.private")) {
63                 # access restriction
64                 if (empty($User)) {
65                         http_response_code(303);
66                         $target = urlencode($_SERVER['REQUEST_URI']);
67                         header("Location: /login?goto=$target");
68                         exit;
69                 }
70                 $PageAccess = $Page;
71         }
72
73         if (file_exists("$Page.php")) {
74                 break;
75         }
76
77         $up = strrpos($Page, '/');
78         $Args = substr($Page, $up) . $Args;
79         $Page = substr($Page, 0, $up);
80         if ($up === FALSE) {
81                 break;
82         }
83 }
84
85 # load static contents
86
87 ob_start(); # page body
88 ob_start(); # inner html
89 print '<div class="static">'."\n\n";
90
91 $found = FALSE;
92 if (file_exists("$Page$Args/index.html")) {
93         $found = include "./$Page$Args/index.html";
94 }
95 elseif (file_exists("$Page$Args.html")) {
96         $found = include "./$Page$Args.html";
97 }
98 elseif (!empty($User['admin'])) {
99         $found = include (file_exists("$Page/template.html") ? "$Page/template.html" : './template.html');
100 }
101
102 print "</div>\n\n";
103
104 # execute dynamic code
105
106 if ($Page) {
107         $found |= require "./$Page.php";
108 }
109
110 # global html
111
112 include_once 'page.inc.php';
113
114 if (!$found) {
115         # no resulting output
116         http_response_code(404);
117         ob_start();
118         @require "./404.html";
119         print getoutput([ 'url' => htmlspecialchars($_SERVER['REQUEST_URI']) ]);
120 }
121