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
2017-01-28 01:28:53 -05:00
require_once ( __DIR__ . '/autoload.php' );
2017-01-26 22:00:45 -05:00
error_reporting ( 0 );
2017-01-25 21:55:13 -05:00
set_error_handler ( 'errorHandler' );
2017-01-28 22:35:42 -05:00
set_exception_handler ( 'exceptionHandler' );
2017-01-25 21:55:13 -05:00
register_shutdown_function ( 'fatalErrorShutdownHandler' );
2017-01-28 22:35:42 -05:00
$userContext = null ;
2017-01-18 21:56:12 -05:00
function handle404 () {
http_response_code ( 404 );
print json_encode ( '404 found' );
}
2017-01-28 22:35:42 -05:00
function before () {
assertApiIsEnabled ();
2017-02-01 22:04:52 -05:00
$token = \BusinessLogic\Helpers :: getHeader ( 'X-AUTH-TOKEN' );
2017-01-28 22:35:42 -05:00
buildUserContext ( $token );
}
2017-01-20 07:19:39 -05:00
function assertApiIsEnabled () {
2017-01-28 22:35:42 -05:00
return true ;
}
function buildUserContext ( $xAuthToken ) {
global $applicationContext , $userContext , $hesk_settings ;
2017-01-26 22:00:45 -05:00
2017-01-28 22:35:42 -05:00
/* @var $userContextBuilder \BusinessLogic\Security\UserContextBuilder */
2017-02-13 22:34:15 -05:00
$userContextBuilder = $applicationContext -> get [ \BusinessLogic\Security\UserContextBuilder :: class ];
2017-01-28 22:35:42 -05:00
$userContext = $userContextBuilder -> buildUserContext ( $xAuthToken , $hesk_settings );
2017-01-25 21:55:13 -05:00
}
function errorHandler ( $errorNumber , $errorMessage , $errorFile , $errorLine ) {
2017-02-02 21:22:04 -05:00
exceptionHandler ( new Exception ( sprintf ( " %s:%d \n \n %s " , $errorFile , $errorLine , $errorMessage )));
2017-01-28 22:35:42 -05:00
}
/**
* @ 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 );
2017-01-28 22:35:42 -05:00
} else {
2017-01-30 22:10:14 -05:00
if ( exceptionIsOfType ( $exception , 'SQLException' )) {
/* @var $castedException \Core\Exceptions\SQLException */
$castedException = $exception ;
2017-02-02 20:21:25 -05:00
print_error ( " Fought an uncaught exception " , sprintf ( " %s \n \n %s " , $castedException -> failingQuery , $exception -> getTraceAsString ()));
2017-01-30 22:10:14 -05:00
} else {
2017-02-13 22:34:15 -05:00
print_error ( " Fought an uncaught exception of type " . get_class ( $exception ), sprintf ( " %s \n \n %s " , $exception -> getMessage (), $exception -> getTraceAsString ()));
2017-01-30 22:10:14 -05:00
}
2017-01-28 22:35:42 -05:00
}
// Log more stuff to logging table if possible; we'll catch any exceptions from this
2017-01-25 21:55:13 -05:00
die ();
}
2017-01-28 22:35:42 -05:00
/**
2017-02-02 20:21:25 -05:00
* @ param $exception Exception thrown exception
* @ param $class string The name of the expected exception type
2017-01-28 22:35:42 -05:00
* @ 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
}
2017-01-28 22:35:42 -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' ,
2017-01-31 22:26:46 -05:00
// Tickets
'/v1/tickets/{i}' => '\Controllers\Tickets\TicketController' ,
2017-02-13 22:34:15 -05:00
'/v1/tickets' => '\Controllers\Tickets\TicketController' ,
2017-01-28 01:28:53 -05:00
// 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
));