index.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); // JSON_HEX_TAG
  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. // set custom error handler
  86. set_error_handler([$Organizr, 'setAPIErrorResponse']);
  87. return $handler->handle($request);
  88. });
  89. //$app->add(new Lowercase());
  90. /*
  91. * Include all routes
  92. */
  93. foreach (glob(__DIR__ . DIRECTORY_SEPARATOR . 'routes' . DIRECTORY_SEPARATOR . '*.php') as $filename) {
  94. require_once $filename;
  95. }
  96. /*
  97. * Include all custom routes
  98. */
  99. if (file_exists(dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'routes')) {
  100. foreach (glob(dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'routes' . DIRECTORY_SEPARATOR . '*.php') as $filename) {
  101. require_once $filename;
  102. }
  103. }
  104. /*
  105. * Include all Plugin routes
  106. */
  107. $folder = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins';
  108. $directoryIterator = new RecursiveDirectoryIterator($folder, FilesystemIterator::SKIP_DOTS);
  109. $iteratorIterator = new RecursiveIteratorIterator($directoryIterator);
  110. foreach ($iteratorIterator as $info) {
  111. if ($info->getFilename() == 'api.php') {
  112. require_once $info->getPathname();
  113. }
  114. }
  115. /*
  116. * Include all custom Plugin routes
  117. */
  118. if (file_exists(dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'plugins')) {
  119. $folder = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'plugins';
  120. $directoryIterator = new RecursiveDirectoryIterator($folder, FilesystemIterator::SKIP_DOTS);
  121. $iteratorIterator = new RecursiveIteratorIterator($directoryIterator);
  122. foreach ($iteratorIterator as $info) {
  123. if ($info->getFilename() == 'api.php') {
  124. require_once $info->getPathname();
  125. }
  126. }
  127. }
  128. /*
  129. *
  130. * This is the last defined api endpoint to catch all undefined endpoints
  131. *
  132. */
  133. $app->any('{route:.*}', function ($request, $response) {
  134. $GLOBALS['api']['response']['data'] = array(
  135. 'endpoint' => $request->getUri()->getPath(),
  136. 'method' => $request->getMethod(),
  137. );
  138. $GLOBALS['api']['response']['result'] = 'error';
  139. $GLOBALS['api']['response']['message'] = 'Endpoint Not Found or Defined';
  140. $GLOBALS['responseCode'] = 404;
  141. $response->getBody()->write(jsonE($GLOBALS['api']));
  142. return $response
  143. ->withHeader('Content-Type', 'application/json;charset=UTF-8')
  144. ->withStatus($GLOBALS['responseCode']);
  145. });
  146. $app->run();