diff --git a/models/Dispatcher.php b/models/Dispatcher.php index 3a6b529..136b039 100644 --- a/models/Dispatcher.php +++ b/models/Dispatcher.php @@ -44,6 +44,19 @@ class Dispatcher } } + public static function errorPage($title, $body) + { + $page = new MainTemplate($title); + $page->adopt(new ErrorPage($title, $body)); + + if (Registry::get('user')->isAdmin()) + { + $page->appendStylesheet(BASEURL . '/css/admin.css'); + } + + $page->html_main(); + } + /** * Kicks a guest to a login form, redirecting them back to this page upon login. */ @@ -60,37 +73,24 @@ class Dispatcher exit; } - public static function trigger400() + private static function trigger400() { - header('HTTP/1.1 400 Bad Request'); - $page = new MainTemplate('Bad request'); - $page->adopt(new DummyBox('Bad request', '
The server does not understand your request.
')); - $page->html_main(); + http_response_code(400); + self::errorPage('Bad request', 'The server does not understand your request.'); exit; } - public static function trigger403() + private static function trigger403() { - header('HTTP/1.1 403 Forbidden'); - $page = new MainTemplate('Access denied'); - $page->adopt(new DummyBox('Forbidden', 'You do not have access to the page you requested.
')); - $page->html_main(); + http_response_code(403); + self::errorPage('Forbidden', 'You do not have access to this page.'); exit; } - public static function trigger404() + private static function trigger404() { - header('HTTP/1.1 404 Not Found'); - $page = new MainTemplate('Page not found'); - - if (Registry::has('user') && Registry::get('user')->isAdmin()) - { - $page->appendStylesheet(BASEURL . '/css/admin.css'); - } - - $page->adopt(new DummyBox('Well, this is a bit embarrassing!', 'The page you requested could not be found. Don\'t worry, it\'s probably not your fault. You\'re welcome to browse the website, though!
', 'errormsg')); - $page->addClass('errorpage'); - $page->html_main(); - exit; + http_response_code(404); + $page = new ViewErrorPage('Page not found!'); + $page->showContent(); } } diff --git a/models/ErrorHandler.php b/models/ErrorHandler.php index 254291a..12afbd6 100644 --- a/models/ErrorHandler.php +++ b/models/ErrorHandler.php @@ -3,7 +3,7 @@ * ErrorHandler.php * Contains key class ErrorHandler. * - * Kabuki CMS (C) 2013-2016, Aaron van Geffen + * Kabuki CMS (C) 2013-2025, Aaron van Geffen *****************************************************************************/ class ErrorHandler @@ -47,10 +47,8 @@ class ErrorHandler // Log the error in the database. self::logError($error_message, $debug_info, $file, $line); - // Are we considering this fatal? Then display and exit. - // !!! TODO: should we consider warnings fatal? - if (true) // DEBUG || (!DEBUG && $error_level === E_WARNING || $error_level === E_USER_WARNING)) - self::display($file . ' (' . $line . ')Our software could not connect to the database. We apologise for any inconvenience and ask you to check back later.
'; exit; } @@ -138,7 +136,7 @@ class ErrorHandler return $error_message; } - public static function display($message, $debug_info, $is_sensitive = true) + public static function display($message, $file, $line, $debug_info, $is_sensitive = true) { $is_admin = Registry::has('user') && Registry::get('user')->isAdmin(); @@ -167,7 +165,8 @@ class ErrorHandler $is_admin = Registry::has('user') && Registry::get('user')->isAdmin(); if (DEBUG || $is_admin) { - $page->adopt(new DummyBox('An error occurred!', '' . $message . '
' . $debug_info . '')); + $debug_info = sprintf("Trigger point:\n%s (L%d)\n\n%s", $file, $line, $debug_info); + $page->adopt(new ErrorPage('An error occurred!', $message, $debug_info)); // Let's provide the admin navigation despite it all! if ($is_admin) @@ -176,9 +175,9 @@ class ErrorHandler } } elseif (!$is_sensitive) - $page->adopt(new DummyBox('An error occurred!', '
' . $message . '
')); + $page->adopt(new ErrorPage('An error occurred!', '' . $message . '
')); else - $page->adopt(new DummyBox('An error occurred!', 'Our apologies, an error occurred while we were processing your request. Please try again later, or contact us if the problem persists.
')); + $page->adopt(new ErrorPage('An error occurred!', 'Our apologies, an error occurred while we were processing your request. Please try again later, or contact us if the problem persists.')); // If we got this far, make sure we're not showing stuff twice. ob_end_clean(); diff --git a/templates/ErrorPage.php b/templates/ErrorPage.php new file mode 100644 index 0000000..ee27f21 --- /dev/null +++ b/templates/ErrorPage.php @@ -0,0 +1,41 @@ +title = $title; + $this->message = $message; + $this->debug_info = $debug_info; + } + + public function html_main() + { + echo ' +', nl2br(htmlspecialchars($this->message)), '
'; + + if (isset($this->debug_info)) + { + echo ' +', htmlspecialchars($this->debug_info), ''; + } + + echo ' +