index.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. // Instantiate App
  98. $app = AppFactory::create();
  99. // Add error middleware
  100. $app->addRoutingMiddleware();
  101. $app->addErrorMiddleware(true, true, true);
  102. $app->setBasePath('/api/v2');
  103. $app->add(function ($request, $handler) {
  104. // add the organizr to your request as [READ-ONLY]
  105. $Organizr = new Organizr();
  106. $request = $request->withAttribute('Organizr', $Organizr);
  107. return $handler->handle($request);
  108. });
  109. $app->add(new Lowercase());
  110. /*
  111. * Include all routes
  112. */
  113. foreach (glob(__DIR__ . DIRECTORY_SEPARATOR . 'routes' . DIRECTORY_SEPARATOR . '*.php') as $filename) {
  114. require_once $filename;
  115. }
  116. /*
  117. * Include all Plugin routes
  118. */
  119. foreach (glob(dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . "*.php") as $filename) {
  120. require_once $filename;
  121. }
  122. /*
  123. *
  124. * This is the last defined api endpoint to catch all undefined endpoints
  125. *
  126. */
  127. $app->any('{route:.*}', function ($request, $response) {
  128. $GLOBALS['api']['response']['data'] = array(
  129. 'endpoint' => $request->getUri()->getPath(),
  130. 'method' => $request->getMethod(),
  131. );
  132. $GLOBALS['api']['response']['result'] = 'error';
  133. $GLOBALS['api']['response']['message'] = 'Endpoint Not Found or Defined';
  134. $GLOBALS['responseCode'] = 404;
  135. $response->getBody()->write(jsonE($GLOBALS['api']));
  136. return $response
  137. ->withHeader('Content-Type', 'application/json;charset=UTF-8')
  138. ->withStatus($GLOBALS['responseCode']);
  139. });
  140. $app->run();