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