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