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.

69 lines
2.3 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. public static $admins = [];
  12. }
  13. }
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. $router = new League\Route\RouteCollection;
  17. $templates = new League\Plates\Engine(dirname(__FILE__).'/../views');
  18. $router->addRoute('GET', '/', 'Main::index');
  19. $router->addRoute('GET', '/parse', 'Parse::parse');
  20. $router->addRoute('POST', '/parse', 'Parse::parse');
  21. $router->addRoute('POST', '/token', 'Token::token');
  22. $router->addRoute('GET', '/rels', 'Rels::fetch');
  23. $router->addRoute('GET', '/cert', 'Certbot::index');
  24. $router->addRoute('GET', '/cert/auth', 'Certbot::start_auth');
  25. $router->addRoute('GET', '/cert/logout', 'Certbot::logout');
  26. $router->addRoute('GET', '/cert/redirect', 'Certbot::redirect');
  27. $router->addRoute('POST', '/cert/save-challenge', 'Certbot::save_challenge');
  28. $router->addRoute('GET', '/.well-known/acme-challenge/{token}', 'Certbot::challenge');
  29. $dispatcher = $router->getDispatcher();
  30. $request = Request::createFromGlobals();
  31. try {
  32. $response = $dispatcher->dispatch($request->getMethod(), $request->getPathInfo());
  33. $response->send();
  34. } catch(League\Route\Http\Exception\NotFoundException $e) {
  35. $response = new Response;
  36. $response->setStatusCode(404);
  37. $response->setContent("Not Found\n");
  38. $response->send();
  39. } catch(League\Route\Http\Exception\MethodNotAllowedException $e) {
  40. $response = new Response;
  41. $response->setStatusCode(405);
  42. $response->setContent("Method not allowed\n");
  43. $response->send();
  44. }
  45. function shutdown() {
  46. $error = error_get_last();
  47. if($error['type'] === E_ERROR) {
  48. header('HTTP/1.1 500 Server Error');
  49. header('X-PHP-Error-Type: '.$error['type']);
  50. header('X-PHP-Error-Message: '.$error['message']);
  51. header('Content-Type: application/json');
  52. echo json_encode([
  53. 'error' => 'internal_error',
  54. 'error_code' => 500,
  55. 'error_description' => $error['message'],
  56. 'debug' => 'Please file an issue with any information you have about what caused this error: https://github.com/aaronpk/XRay/issues'
  57. ]);
  58. die();
  59. }
  60. }