page: replace error scripts by editable html with placeholders
[minimedit.git] / page.php
1 <?php
2 error_reporting(E_ALL);
3 ini_set('display_errors', TRUE);
4
5 set_exception_handler(function ($error) {
6         http_response_code(500);
7         include_once 'page.inc.php';
8         ob_start();
9         require_once './500.html';
10         print str_replace('[[debug]]', $error, ob_get_clean());
11 });
12
13 include_once 'auth.inc.php';
14 $Edit = isset($_GET['edit']);
15
16 # distinguish subpage Args from topmost Page script
17
18 $Args = '';
19 $Page = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
20 $Page = urldecode(trim($Page, '/')) ?: 'index';
21 while (TRUE) {
22         if (file_exists("$Page.php")) {
23                 break;
24         }
25
26         $up = strrpos($Page, '/');
27         $Args = substr($Page, $up) . $Args;
28         $Page = substr($Page, 0, $up);
29         if ($up === FALSE) {
30                 break;
31         }
32 }
33
34 # load static contents
35
36 ob_start(); # page body
37 ob_start(); # inner html
38 print '<div class="static">'."\n\n";
39
40 $found = FALSE;
41 if (file_exists("$Page$Args/index.html")) {
42         $found = include "./$Page$Args/index.html";
43 }
44 elseif (file_exists("$Page$Args.html")) {
45         $found = include "./$Page$Args.html";
46 }
47
48 print "</div>\n\n";
49
50 # execute dynamic code
51
52 if ($Page) {
53         $found |= require "./$Page.php";
54 }
55
56 # global html
57
58 include_once 'page.inc.php';
59
60 if (!$found) {
61         # no resulting output
62         if (isset($User) and $User['admin']) {
63                 require './template.html';
64         }
65         else {
66                 http_response_code(404);
67                 ob_start();
68                 require "./404.html";
69                 $url = htmlspecialchars($_SERVER['REQUEST_URI']);
70                 print str_replace('[[url]]', $url, ob_get_clean());
71         }
72 }
73