page: api attribute indicate index requests
[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         print "$body\n";
15         exit;
16 }
17
18 function fail($error)
19 {
20         global $User, $Page;
21         http_response_code(500);
22         if (!isset($Page)) {
23                 require_once('article.inc.php');
24                 $Page = new ArchiveArticle(NULL);
25                 $Page->title = 'Fout';
26         }
27         include_once 'page.inc.php';
28
29         ob_start();
30         require '500.inc.html';
31         $Page->place['debug'] = htmlspecialchars($error);
32         $Page->raw = ob_get_clean();
33         print $Page->render();
34 }
35
36 set_exception_handler('fail');
37
38 define('E_FATAL', E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR);
39
40 set_error_handler(function ($level, $error, $file, $line) {
41         if ($level & E_FATAL) {
42                 fail($error);
43                 return;
44         }
45         return FALSE;
46 });
47
48 register_shutdown_function(function () {
49         # display failure page for fatal exceptions
50         $error = error_get_last();
51         if (!($error['type'] & E_FATAL)) return;
52         fail("Fatal: $error[message] in $error[file]:$error[line]");
53 });
54
55 error_reporting(error_reporting() & ~E_FATAL);
56