89 lines
2.8 KiB
PHP
Raw Normal View History

2017-01-16 22:21:11 -05:00
<?php
2017-01-25 21:55:13 -05:00
// Properly handle error logging, as well as a fatal error workaround
require_once(__DIR__ . '/autoload.php');
error_reporting(0);
2017-01-25 21:55:13 -05:00
set_error_handler('errorHandler');
set_exception_handler('exceptionHandler');
2017-01-25 21:55:13 -05:00
register_shutdown_function('fatalErrorShutdownHandler');
$userContext = null;
2017-01-18 21:56:12 -05:00
function handle404() {
http_response_code(404);
print json_encode('404 found');
}
function before() {
assertApiIsEnabled();
$token = \BusinessLogic\Helpers\Helpers::getHeader('X-AUTH-TOKEN');
buildUserContext($token);
}
2017-01-20 07:19:39 -05:00
function assertApiIsEnabled() {
return true;
}
function buildUserContext($xAuthToken) {
global $applicationContext, $userContext, $hesk_settings;
/* @var $userContextBuilder \BusinessLogic\Security\UserContextBuilder */
$userContextBuilder = $applicationContext->get['UserContextBuilder'];
$userContext = $userContextBuilder->buildUserContext($xAuthToken, $hesk_settings);
2017-01-25 21:55:13 -05:00
}
function errorHandler($errorNumber, $errorMessage, $errorFile, $errorLine) {
throw new Exception(sprintf("%s:%d\n\n%s", $errorFile, $errorLine, $errorMessage));
}
/**
* @param $exception Exception
*/
function exceptionHandler($exception) {
2017-01-30 22:10:14 -05:00
if (exceptionIsOfType($exception, 'ApiFriendlyException')) {
/* @var $castedException \BusinessLogic\Exceptions\ApiFriendlyException */
$castedException = $exception;
print_error($castedException->title, $castedException->getMessage(), $castedException->httpResponseCode);
} else {
2017-01-30 22:10:14 -05:00
if (exceptionIsOfType($exception, 'SQLException')) {
/* @var $castedException \Core\Exceptions\SQLException */
$castedException = $exception;
print_error("Fought an uncaught exception", sprintf("%s\n\n%s", $castedException->failingQuery, $exception->getTraceAsString()));
} else {
print_error("Fought an uncaught exception", sprintf("%s\n\n%s", $exception->getMessage(), $exception->getTraceAsString()));
}
}
// Log more stuff to logging table if possible; we'll catch any exceptions from this
2017-01-25 21:55:13 -05:00
die();
}
/**
* @param $exception Exception thrown exception
* @param $class string The name of the expected exception type
* @return bool
*/
function exceptionIsOfType($exception, $class) {
return strpos(get_class($exception), $class) !== false;
}
2017-01-25 21:55:13 -05:00
function fatalErrorShutdownHandler() {
$last_error = error_get_last();
if ($last_error['type'] === E_ERROR) {
// fatal error
errorHandler(E_ERROR, $last_error['message'], $last_error['file'], $last_error['line']);
}
2017-01-20 07:19:39 -05:00
}
Link::before('before');
2017-01-20 07:19:39 -05:00
2017-01-18 17:22:24 -05:00
Link::all(array(
2017-01-21 22:09:29 -05:00
// Categories
'/v1/categories' => '\Controllers\Category\CategoryController::printAllCategories',
'/v1/categories/{i}' => '\Controllers\Category\CategoryController',
// Any URL that doesn't match goes to the 404 handler
2017-01-18 21:56:12 -05:00
'404' => 'handle404'
2017-01-17 22:12:45 -05:00
));