login/pass: error messages below page title
[minimedit.git] / error.inc.php
1 <?php
2 # custom error handling for MinimEdit
3
4 function abort($body, string $status = NULL)
5 {
6         if ($status) {
7                 header("HTTP/1.1 $status");
8                 if ($status[0] === '3') {
9                         # redirection (body specifies target)
10                         header("Location: $body");
11                         exit;
12                 }
13         }
14         header('Content-Type: text/plain; charset=utf-8');
15         print "$body\n";
16         exit;
17 }
18
19 function fail($error)
20 {
21         global $User, $Page;
22         http_response_code(500);
23         if (!isset($Page)) {
24                 require_once('article.inc.php');
25                 $Page = new ArchiveArticle(NULL);
26                 $Page->title = 'Fout';
27         }
28         include_once 'page.inc.php';
29
30         ob_start();
31         require '500.inc.html';
32         $Page->place['debug'] = htmlspecialchars($error);
33         $Page->raw = ob_get_clean();
34         print $Page->render();
35 }
36
37 set_exception_handler('fail');
38
39 define('E_FATAL', E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR);
40
41 set_error_handler(function ($level, $error, $file, $line) {
42         if ($level & E_FATAL) {
43                 fail($error);
44                 return;
45         }
46         return FALSE;
47 });
48
49 register_shutdown_function(function () {
50         # display failure page for fatal exceptions
51         $error = error_get_last();
52         if (!($error['type'] & E_FATAL)) return;
53         fail("Fatal: $error[message] in $error[file]:$error[line]");
54 });
55
56 error_reporting(error_reporting() & ~E_FATAL);
57