v1.php 2.9 KB

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