nieuws/feed: atom content type and link attributes
[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 placeholder_include($name, $params = [])
12 {
13         $path = stream_resolve_include_path("$name.php");
14         if (!file_exists($path)) {
15                 return '<strong class="warn"><em>'.$name.'</em> ontbreekt</strong>';
16         }
17
18         ob_start();
19         $Page = $GLOBALS['Page'] . $GLOBALS['Args'];
20         $Args = '';
21         $Place = $GLOBALS['Place'];
22         foreach ($params as $param) {
23                 if ($set = strpos($param, '=')) {
24                         $Place[ substr($param, 0, $set) ] = substr($param, $set + 1);
25                 }
26                 elseif (!empty($param)) {
27                         $Args .= '/'.$param;
28                 }
29         }
30         try {
31                 include "$name.php";
32                 return ob_get_clean();
33         }
34         catch (Exception $e) {
35                 return sprintf('<strong class="warn">%s</strong>',
36                         "fout in <em>$name</em>: {$e->getMessage()}"
37                 );
38         }
39 }
40
41 function getoutput($blocks = [])
42 {
43         $doc = ob_get_clean();
44
45         if (!empty($blocks['warn'])) {
46                 $warn = '<p class="warn">[[warn]]</p>';
47                 if ($offset = strpos($doc, '</h2>')) {
48                         $doc = substr_replace($doc, "\n\n".$warn, $offset + 5, 0);
49                 }
50                 else {
51                         $doc = $warn . "\n\n" . $doc;
52                 }
53         }
54
55         return preg_replace_callback(
56                 '< \[\[ ([^] ]+) ([^]]*) \]\] >x',
57                 function ($sub) use ($blocks) {
58                         list ($placeholder, $name, $params) = $sub;
59                         if (isset($blocks[$name])) {
60                                 $html = $blocks[$name];
61                         }
62                         else {
63                                 $html = placeholder_include($name, explode(' ', $params));
64                         }
65                         return sprintf('<!--BLOCK:%s-->%s<!--/-->',
66                                 is_numeric($name) ? '' : $placeholder, # edit replacement
67                                 preg_replace('{<!--[^-]*-->}', '', $html) # contents
68                         );
69                 },
70                 $doc
71         );
72 }
73
74 # custom error handling
75
76 define('DOCROOT', getcwd());
77 set_include_path(implode(PATH_SEPARATOR, [ DOCROOT, __DIR__ ]));
78
79 function fail($error)
80 {
81         http_response_code(500);
82         include_once 'page.inc.php';
83         ob_start();
84         require_once '500.html';
85         print getoutput(['debug' => $error]);
86 }
87
88 set_exception_handler('fail');
89
90 define('E_FATAL', E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR);
91
92 set_error_handler(function ($level, $error, $file, $line) {
93         if ($level & E_FATAL) {
94                 fail($error);
95                 return;
96         }
97         return FALSE;
98 });
99
100 register_shutdown_function(function () {
101         # display failure page for fatal exceptions
102         $error = error_get_last();
103         if (!($error['type'] & E_FATAL)) return;
104         fail("Fatal: $error[message] in $error[file]:$error[line]");
105 });
106
107 error_reporting(error_reporting() & ~E_FATAL);
108
109 # user login and control
110
111 include_once 'auth.inc.php';
112 $Edit = isset($_GET['edit']);
113
114 # distinguish subpage Args from topmost Page script
115
116 $Args = '';
117 $Page = preg_replace('/\?.*/', '', @$_SERVER['PATH_INFO'] ?: $_SERVER['REQUEST_URI']);
118 $Page = urldecode(trim($Page, '/')) ?: 'index';
119 while (TRUE) {
120         if (file_exists("$Page/.private")) {
121                 # access restriction
122                 if (empty($User)) {
123                         http_response_code(303);
124                         $target = urlencode($_SERVER['REQUEST_URI']);
125                         header("Location: /login?goto=$target");
126                         exit;
127                 }
128                 $PageAccess = $Page;
129         }
130
131         if (file_exists("$Page/index.php")) {
132                 break;
133         }
134
135         $up = strrpos($Page, '/');
136         $Args = substr($Page, $up) . $Args;
137         $Page = substr($Page, 0, $up);
138         if ($up === FALSE) {
139                 break;
140         }
141 }
142
143 $staticpage = NULL;
144 if (file_exists("$Page$Args.html")) {
145         $staticpage = "$Page$Args.html";
146         if (is_link($staticpage)) {
147                 $target = preg_replace('/\.html$/', '', readlink($staticpage));
148                 header("HTTP/1.1 302 Shorthand");
149                 header("Location: $target");
150                 exit;
151         }
152 }
153 elseif (file_exists("$Page$Args/index.html")) {
154         $staticpage = "$Page$Args/index.html";
155 }
156 elseif (!empty($User['admin'])) {
157         $staticpage = (file_exists("$Page/template.html") ? "$Page/template.html" : 'template.html');
158 }
159
160 # load static contents
161
162 ob_start(); # page body
163 ob_start(); # inner html
164 print '<div class="static">'."\n\n";
165
166 $found = FALSE;
167 if (isset($staticpage)) {
168         $found = include "./$staticpage";
169 }
170
171 print "</div>\n\n";
172
173 # execute dynamic code
174
175 $Place = [];
176
177 if ($Page) {
178         $found |= require "./$Page/index.php";
179 }
180
181 $Place += [
182         'user'  => empty($User) ? '' : $User['name'],
183         'url'   => htmlspecialchars($_SERVER['REQUEST_URI']),
184 ];
185
186 # global html
187
188 if (!$found) {
189         # no resulting output
190         http_response_code(404);
191         @require '404.html';
192 }
193
194 include_once 'page.inc.php';
195