v1.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. require_once __DIR__ . '/../lib/versioncheck.php';
  9. require_once __DIR__ . '/../lib/base.php';
  10. use OC\OCS\ApiHelper;
  11. use OCP\AppFramework\Http;
  12. use OCP\AppFramework\OCSController;
  13. use OCP\Security\Bruteforce\MaxDelayReached;
  14. use OCP\Util;
  15. use Psr\Log\LoggerInterface;
  16. use Symfony\Component\Routing\Exception\MethodNotAllowedException;
  17. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  18. if (Util::needUpgrade()
  19. || \OC::$server->getConfig()->getSystemValueBool('maintenance')) {
  20. // since the behavior of apps or remotes are unpredictable during
  21. // an upgrade, return a 503 directly
  22. ApiHelper::respond(503, 'Service unavailable', ['X-Nextcloud-Maintenance-Mode' => '1'], 503);
  23. exit;
  24. }
  25. /*
  26. * Try the appframework routes
  27. */
  28. try {
  29. OC_App::loadApps(['session']);
  30. OC_App::loadApps(['authentication']);
  31. OC_App::loadApps(['extended_authentication']);
  32. // load all apps to get all api routes properly setup
  33. // FIXME: this should ideally appear after handleLogin but will cause
  34. // side effects in existing apps
  35. OC_App::loadApps();
  36. if (!\OC::$server->getUserSession()->isLoggedIn()) {
  37. OC::handleLogin(\OC::$server->getRequest());
  38. }
  39. OC::$server->get(\OC\Route\Router::class)->match('/ocsapp' . \OC::$server->getRequest()->getRawPathInfo());
  40. } catch (MaxDelayReached $ex) {
  41. ApiHelper::respond(Http::STATUS_TOO_MANY_REQUESTS, $ex->getMessage());
  42. } catch (ResourceNotFoundException $e) {
  43. $txt = 'Invalid query, please check the syntax. API specifications are here:'
  44. . ' http://www.freedesktop.org/wiki/Specifications/open-collaboration-services.' . "\n";
  45. ApiHelper::respond(OCSController::RESPOND_NOT_FOUND, $txt);
  46. } catch (MethodNotAllowedException $e) {
  47. ApiHelper::setContentType();
  48. http_response_code(405);
  49. } catch (\OC\User\LoginException $e) {
  50. ApiHelper::respond(OCSController::RESPOND_UNAUTHORISED, 'Unauthorised');
  51. } catch (\Exception $e) {
  52. \OCP\Server::get(LoggerInterface::class)->error($e->getMessage(), ['exception' => $e]);
  53. $txt = 'Internal Server Error' . "\n";
  54. try {
  55. if (\OC::$server->getSystemConfig()->getValue('debug', false)) {
  56. $txt .= $e->getMessage();
  57. }
  58. } catch (\Throwable $e) {
  59. // Just to be save
  60. }
  61. ApiHelper::respond(OCSController::RESPOND_SERVER_ERROR, $txt);
  62. }