index.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. class JsonBodyParserMiddleware implements MiddlewareInterface
  33. {
  34. public function process(Request $request, RequestHandler $handler): Response
  35. {
  36. $contentType = $request->getHeaderLine('Content-Type');
  37. if (strstr($contentType, 'application/json')) {
  38. $contents = json_decode(file_get_contents('php://input'), true);
  39. if (json_last_error() === JSON_ERROR_NONE) {
  40. $request = $request->withParsedBody($contents);
  41. }
  42. }
  43. return $handler->handle($request);
  44. }
  45. }
  46. class Lowercase implements MiddlewareInterface
  47. {
  48. /**
  49. * @var ResponseFactoryInterface
  50. */
  51. private $responseFactory;
  52. /*
  53. * Whether returns a 301 response to the new path.
  54. */
  55. public function redirect(ResponseFactoryInterface $responseFactory): self
  56. {
  57. $this->responseFactory = $responseFactory;
  58. return $this;
  59. }
  60. /*
  61. * Process a request and return a response.
  62. */
  63. public function process(Request $request, RequestHandler $handler): Response
  64. {
  65. $uri = $request->getUri();
  66. $path = strtolower($uri->getPath());
  67. if ($this->responseFactory && ($uri->getPath() !== $path)) {
  68. return $this->responseFactory->createResponse(301)
  69. ->withHeader('Location', (string)$uri->withPath($path));
  70. }
  71. return $handler->handle($request->withUri($uri->withPath($path)));
  72. }
  73. }
  74. $GLOBALS['api'] = array(
  75. 'response' => array(
  76. 'result' => 'success',
  77. 'message' => null,
  78. 'data' => null
  79. )
  80. );
  81. $GLOBALS['bypass'] = array(
  82. '/api/v2/upgrade',
  83. '/api/v2/update',
  84. '/api/v2/force',
  85. '/api/v2/auth',
  86. '/api/v2/wizard',
  87. '/api/v2/login',
  88. '/api/v2/wizard/path',
  89. '/api/v2/login/api',
  90. '/api/v2/plex/register'
  91. );
  92. $GLOBALS['responseCode'] = 200;
  93. function jsonE($json)
  94. {
  95. return safe_json_encode($json, JSON_HEX_QUOT | JSON_HEX_TAG | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
  96. }
  97. function getBasePath()
  98. {
  99. $uri = $_SERVER['REQUEST_URI'];
  100. if (stripos($uri, 'api/v2') !== false) {
  101. return '/api/v2';
  102. } else {
  103. return '';
  104. }
  105. }
  106. function overWriteURI()
  107. {
  108. $uri = $_SERVER['REQUEST_URI'];
  109. $query = $_SERVER['QUERY_STRING'];
  110. if (stripos($uri, 'api/v2') === false && stripos($query, 'group=') !== false) {
  111. $group = explode('group=', $query);
  112. $_SERVER['REQUEST_URI'] = 'auth-' . $group[1];
  113. }
  114. }
  115. overWriteURI();
  116. // Instantiate App
  117. $app = AppFactory::create();
  118. // Add error middleware
  119. $app->addRoutingMiddleware();
  120. $app->addErrorMiddleware(true, true, true);
  121. $app->setBasePath(getBasePath());
  122. $app->add(function ($request, $handler) {
  123. // add the organizr to your request as [READ-ONLY]
  124. $Organizr = new Organizr();
  125. $request = $request->withAttribute('Organizr', $Organizr);
  126. return $handler->handle($request);
  127. });
  128. $app->add(new Lowercase());
  129. /*
  130. * Include all routes
  131. */
  132. foreach (glob(__DIR__ . DIRECTORY_SEPARATOR . 'routes' . DIRECTORY_SEPARATOR . '*.php') as $filename) {
  133. require_once $filename;
  134. }
  135. /*
  136. * Include all Plugin routes
  137. */
  138. foreach (glob(dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . "*.php") as $filename) {
  139. require_once $filename;
  140. }
  141. /*
  142. *
  143. * This is the last defined api endpoint to catch all undefined endpoints
  144. *
  145. */
  146. $app->any('{route:.*}', function ($request, $response) {
  147. $GLOBALS['api']['response']['data'] = array(
  148. 'endpoint' => $request->getUri()->getPath(),
  149. 'method' => $request->getMethod(),
  150. );
  151. $GLOBALS['api']['response']['result'] = 'error';
  152. $GLOBALS['api']['response']['message'] = 'Endpoint Not Found or Defined';
  153. $GLOBALS['responseCode'] = 404;
  154. $response->getBody()->write(jsonE($GLOBALS['api']));
  155. return $response
  156. ->withHeader('Content-Type', 'application/json;charset=UTF-8')
  157. ->withStatus($GLOBALS['responseCode']);
  158. });
  159. $app->run();