index.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. '/api/v2/oidc/providers'
  50. );
  51. $GLOBALS['responseCode'] = 200;
  52. function jsonE($json)
  53. {
  54. return safe_json_encode($json, JSON_HEX_QUOT | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); // JSON_HEX_TAG
  55. }
  56. function getBasePath()
  57. {
  58. $uri = $_SERVER['REQUEST_URI'];
  59. $uriUse = str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME']));
  60. if (stripos($uri, 'api/v2/') !== false) {
  61. return $uriUse;
  62. } else {
  63. return '';
  64. }
  65. }
  66. function overWriteURI()
  67. {
  68. $uri = $_SERVER['REQUEST_URI'];
  69. $query = $_SERVER['QUERY_STRING'];
  70. if (stripos($query, 'group=') !== false) {
  71. $group = explode('group=', $query);
  72. $_SERVER['REQUEST_URI'] = 'auth-' . $group[1];
  73. }
  74. }
  75. overWriteURI();
  76. // Instantiate App
  77. $app = AppFactory::create();
  78. // Add error middleware
  79. $app->addRoutingMiddleware();
  80. $app->addErrorMiddleware(true, true, true);
  81. $app->setBasePath(getBasePath());
  82. $app->add(function ($request, $handler) {
  83. // add the organizr to your request as [READ-ONLY]
  84. $Organizr = new Organizr();
  85. $request = $request->withAttribute('Organizr', $Organizr);
  86. // set custom error handler
  87. set_error_handler([$Organizr, 'setAPIErrorResponse']);
  88. return $handler->handle($request);
  89. });
  90. //$app->add(new Lowercase());
  91. /*
  92. * Include all routes
  93. */
  94. foreach (glob(__DIR__ . DIRECTORY_SEPARATOR . 'routes' . DIRECTORY_SEPARATOR . '*.php') as $filename) {
  95. require_once $filename;
  96. }
  97. /*
  98. * Include all custom routes
  99. */
  100. if (file_exists(dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'routes')) {
  101. foreach (glob(dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'routes' . DIRECTORY_SEPARATOR . '*.php') as $filename) {
  102. require_once $filename;
  103. }
  104. }
  105. /*
  106. * Include all Plugin routes
  107. */
  108. $folder = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins';
  109. $directoryIterator = new RecursiveDirectoryIterator($folder, FilesystemIterator::SKIP_DOTS);
  110. $iteratorIterator = new RecursiveIteratorIterator($directoryIterator);
  111. foreach ($iteratorIterator as $info) {
  112. if ($info->getFilename() == 'api.php') {
  113. require_once $info->getPathname();
  114. }
  115. }
  116. /*
  117. * Include all custom Plugin routes
  118. */
  119. if (file_exists(dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'plugins')) {
  120. $folder = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'plugins';
  121. $directoryIterator = new RecursiveDirectoryIterator($folder, FilesystemIterator::SKIP_DOTS);
  122. $iteratorIterator = new RecursiveIteratorIterator($directoryIterator);
  123. foreach ($iteratorIterator as $info) {
  124. if ($info->getFilename() == 'api.php') {
  125. require_once $info->getPathname();
  126. }
  127. }
  128. }
  129. /*
  130. * Include Plugin routes from plugins/ directory (for external git repos)
  131. */
  132. if (file_exists(dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins')) {
  133. $folder = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins';
  134. $directoryIterator = new RecursiveDirectoryIterator($folder, FilesystemIterator::SKIP_DOTS);
  135. $iteratorIterator = new RecursiveIteratorIterator($directoryIterator);
  136. foreach ($iteratorIterator as $info) {
  137. if ($info->getFilename() == 'routes.php' || ($info->getFilename() == 'api.php' && strpos($info->getPathname(), '/api/') !== false)) {
  138. require_once $info->getPathname();
  139. }
  140. }
  141. }
  142. /*
  143. *
  144. * This is the last defined api endpoint to catch all undefined endpoints
  145. *
  146. */
  147. $app->any('{route:.*}', function ($request, $response) {
  148. $GLOBALS['api']['response']['data'] = array(
  149. 'endpoint' => $request->getUri()->getPath(),
  150. 'method' => $request->getMethod(),
  151. );
  152. $GLOBALS['api']['response']['result'] = 'error';
  153. $GLOBALS['api']['response']['message'] = 'Endpoint Not Found or Defined';
  154. $GLOBALS['responseCode'] = 404;
  155. $response->getBody()->write(jsonE($GLOBALS['api']));
  156. return $response
  157. ->withHeader('Content-Type', 'application/json;charset=UTF-8')
  158. ->withStatus($GLOBALS['responseCode']);
  159. });
  160. $app->run();