index.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * @OA\Info(title="Organizr API", description="Organizr - Accept no others", version="2.0")
  4. * @OA\Server(url=API_HOST,description="This Organizr Install")
  5. * @OA\Server(url="https://demo.organizr.app",description="Organizr Demo API")
  6. * @OA\Server(url="{schema}://{hostPath}",description="Custom Organizr API",
  7. * @OA\ServerVariable(
  8. * serverVariable="schema",
  9. * enum={"https", "http"},
  10. * default="http"
  11. * ),
  12. * @OA\ServerVariable(
  13. * serverVariable="hostPath",
  14. * description="Your Organizr URL",
  15. * default="localhost"
  16. * )
  17. * )
  18. * @OA\SecurityScheme(
  19. * securityScheme="api_key",
  20. * type="apiKey",
  21. * in="header",
  22. * name="Token"
  23. * )
  24. */
  25. require_once '../functions.php';
  26. use Psr\Http\Message\ResponseInterface as Response;
  27. use Psr\Http\Message\ServerRequestInterface as Request;
  28. use Psr\Http\Message\StreamInterface;
  29. use Slim\Factory\AppFactory;
  30. use Psr\Http\Server\MiddlewareInterface;
  31. use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
  32. $GLOBALS['api'] = array(
  33. 'response' => array(
  34. 'result' => 'success',
  35. 'message' => null,
  36. 'data' => null
  37. )
  38. );
  39. $GLOBALS['bypass'] = array(
  40. '/api/v2/upgrade',
  41. '/api/v2/update',
  42. '/api/v2/force',
  43. '/api/v2/auth',
  44. '/api/v2/wizard',
  45. '/api/v2/login',
  46. '/api/v2/wizard/path',
  47. '/api/v2/login/api',
  48. '/api/v2/plex/register'
  49. );
  50. $GLOBALS['responseCode'] = 200;
  51. function jsonE($json)
  52. {
  53. return safe_json_encode($json, JSON_HEX_QUOT | JSON_HEX_TAG | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
  54. }
  55. function getBasePath()
  56. {
  57. $uri = $_SERVER['REQUEST_URI'];
  58. $uriUse = str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME']));
  59. if (stripos($uri, 'api/v2/') !== false) {
  60. return $uriUse;
  61. } else {
  62. return '';
  63. }
  64. }
  65. function overWriteURI()
  66. {
  67. $uri = $_SERVER['REQUEST_URI'];
  68. $query = $_SERVER['QUERY_STRING'];
  69. if (stripos($query, 'group=') !== false) {
  70. $group = explode('group=', $query);
  71. $_SERVER['REQUEST_URI'] = 'auth-' . $group[1];
  72. }
  73. }
  74. overWriteURI();
  75. // Instantiate App
  76. $app = AppFactory::create();
  77. // Add error middleware
  78. $app->addRoutingMiddleware();
  79. $app->addErrorMiddleware(true, true, true);
  80. $app->setBasePath(getBasePath());
  81. $app->add(function ($request, $handler) {
  82. // add the organizr to your request as [READ-ONLY]
  83. $Organizr = new Organizr();
  84. $request = $request->withAttribute('Organizr', $Organizr);
  85. return $handler->handle($request);
  86. });
  87. //$app->add(new Lowercase());
  88. /*
  89. * Include all routes
  90. */
  91. foreach (glob(__DIR__ . DIRECTORY_SEPARATOR . 'routes' . DIRECTORY_SEPARATOR . '*.php') as $filename) {
  92. require_once $filename;
  93. }
  94. /*
  95. * Include all custom routes
  96. */
  97. foreach (glob(__DIR__ . DIRECTORY_SEPARATOR . 'routes' . DIRECTORY_SEPARATOR . 'custom' . DIRECTORY_SEPARATOR . '*.php') as $filename) {
  98. require_once $filename;
  99. }
  100. /*
  101. * Include all Plugin routes
  102. */
  103. foreach (glob(dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . "*.php") as $filename) {
  104. require_once $filename;
  105. }
  106. /*
  107. *
  108. * This is the last defined api endpoint to catch all undefined endpoints
  109. *
  110. */
  111. $app->any('{route:.*}', function ($request, $response) {
  112. $GLOBALS['api']['response']['data'] = array(
  113. 'endpoint' => $request->getUri()->getPath(),
  114. 'method' => $request->getMethod(),
  115. );
  116. $GLOBALS['api']['response']['result'] = 'error';
  117. $GLOBALS['api']['response']['message'] = 'Endpoint Not Found or Defined';
  118. $GLOBALS['responseCode'] = 404;
  119. $response->getBody()->write(jsonE($GLOBALS['api']));
  120. return $response
  121. ->withHeader('Content-Type', 'application/json;charset=UTF-8')
  122. ->withStatus($GLOBALS['responseCode']);
  123. });
  124. $app->run();