You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.9 KiB

  1. <?php
  2. chdir(dirname(__FILE__).'/..');
  3. include('vendor/autoload.php');
  4. register_shutdown_function('shutdown');
  5. // Load config file if present, otherwise use default
  6. if(file_exists(dirname(__FILE__).'/../config.php')) {
  7. require dirname(__FILE__).'/../config.php';
  8. } else {
  9. class Config {
  10. public static $cache = false;
  11. }
  12. }
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. $router = new League\Route\RouteCollection;
  16. $templates = new League\Plates\Engine(dirname(__FILE__).'/../views');
  17. $router->addRoute('GET', '/', 'Main::index');
  18. $router->addRoute('GET', '/parse', 'Parse::parse');
  19. $router->addRoute('POST', '/parse', 'Parse::parse');
  20. $router->addRoute('POST', '/token', 'Token::token');
  21. $dispatcher = $router->getDispatcher();
  22. $request = Request::createFromGlobals();
  23. try {
  24. $response = $dispatcher->dispatch($request->getMethod(), $request->getPathInfo());
  25. $response->send();
  26. } catch(League\Route\Http\Exception\NotFoundException $e) {
  27. $response = new Response;
  28. $response->setStatusCode(404);
  29. $response->setContent("Not Found\n");
  30. $response->send();
  31. } catch(League\Route\Http\Exception\MethodNotAllowedException $e) {
  32. $response = new Response;
  33. $response->setStatusCode(405);
  34. $response->setContent("Method not allowed\n");
  35. $response->send();
  36. }
  37. function shutdown() {
  38. $error = error_get_last();
  39. if($error['type'] === E_ERROR) {
  40. header('HTTP/1.1 500 Server Error');
  41. header('X-PHP-Error-Type: '.$error['type']);
  42. header('X-PHP-Error-Message: '.$error['message']);
  43. header('Content-Type: application/json');
  44. echo json_encode([
  45. 'error' => 'internal_error',
  46. 'error_code' => 500,
  47. 'error_description' => $error['message'],
  48. 'debug' => 'Please file an issue with any information you have about what caused this error: https://github.com/aaronpk/XRay/issues'
  49. ]);
  50. die();
  51. }
  52. }