2016-09-01 23:13:23 +02:00
|
|
|
<?php
|
|
|
|
/*****************************************************************************
|
|
|
|
* Dispatcher.php
|
|
|
|
* Contains key class Dispatcher.
|
|
|
|
*
|
|
|
|
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
class Dispatcher
|
|
|
|
{
|
|
|
|
public static function dispatch()
|
|
|
|
{
|
|
|
|
// Let's try to find our bearings!
|
|
|
|
try
|
|
|
|
{
|
2023-01-01 19:48:12 +01:00
|
|
|
$page = Router::route();
|
2016-09-01 23:13:23 +02:00
|
|
|
$page->showContent();
|
|
|
|
}
|
|
|
|
// Something wasn't found?
|
|
|
|
catch (NotFoundException $e)
|
|
|
|
{
|
|
|
|
self::trigger404();
|
|
|
|
}
|
|
|
|
// Or are they just sneaking into areas they don't belong?
|
|
|
|
catch (NotAllowedException $e)
|
|
|
|
{
|
|
|
|
if (Registry::get('user')->isGuest())
|
|
|
|
self::kickGuest();
|
|
|
|
else
|
|
|
|
self::trigger403();
|
|
|
|
}
|
2016-09-02 11:17:10 +02:00
|
|
|
catch (UserFacingException $e)
|
|
|
|
{
|
|
|
|
$debug_info = ErrorHandler::getDebugInfo($e->getTrace());
|
|
|
|
ErrorHandler::display($e->getMessage(), $debug_info, false);
|
|
|
|
}
|
2016-09-01 23:13:23 +02:00
|
|
|
catch (Exception $e)
|
|
|
|
{
|
|
|
|
ErrorHandler::handleError(E_USER_ERROR, 'Unspecified exception: ' . $e->getMessage(), $e->getFile(), $e->getLine());
|
|
|
|
}
|
|
|
|
catch (Error $e)
|
|
|
|
{
|
|
|
|
ErrorHandler::handleError(E_USER_ERROR, 'Fatal error: ' . $e->getMessage(), $e->getFile(), $e->getLine());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-26 15:33:18 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2016-09-01 23:13:23 +02:00
|
|
|
/**
|
|
|
|
* Kicks a guest to a login form, redirecting them back to this page upon login.
|
|
|
|
*/
|
2020-03-11 22:23:43 +01:00
|
|
|
public static function kickGuest($title = null, $message = null)
|
2016-09-01 23:13:23 +02:00
|
|
|
{
|
|
|
|
$form = new LogInForm('Log in');
|
2023-03-11 13:30:02 +01:00
|
|
|
$form->adopt(new Alert($title ?? '', $message ?? 'You need to be logged in to view this page.', 'danger'));
|
2016-09-01 23:13:23 +02:00
|
|
|
$form->setRedirectUrl($_SERVER['REQUEST_URI']);
|
|
|
|
|
|
|
|
$page = new MainTemplate('Login required');
|
|
|
|
$page->appendStylesheet(BASEURL . '/css/admin.css');
|
|
|
|
$page->adopt($form);
|
|
|
|
$page->html_main();
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2025-02-26 15:33:18 +01:00
|
|
|
private static function trigger400()
|
2016-09-01 23:13:23 +02:00
|
|
|
{
|
2025-02-26 15:33:18 +01:00
|
|
|
http_response_code(400);
|
|
|
|
self::errorPage('Bad request', 'The server does not understand your request.');
|
2016-09-01 23:13:23 +02:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2025-02-26 15:33:18 +01:00
|
|
|
private static function trigger403()
|
2016-09-01 23:13:23 +02:00
|
|
|
{
|
2025-02-26 15:33:18 +01:00
|
|
|
http_response_code(403);
|
|
|
|
self::errorPage('Forbidden', 'You do not have access to this page.');
|
2016-09-01 23:13:23 +02:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2025-02-26 15:33:18 +01:00
|
|
|
private static function trigger404()
|
2016-09-01 23:13:23 +02:00
|
|
|
{
|
2025-02-26 15:33:18 +01:00
|
|
|
http_response_code(404);
|
|
|
|
$page = new ViewErrorPage('Page not found!');
|
|
|
|
$page->showContent();
|
2016-09-01 23:13:23 +02:00
|
|
|
}
|
|
|
|
}
|