1
0

v1.php 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Julius Härtl <jus@bitgrid.net>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. * @author Vincent Petry <vincent@nextcloud.com>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. require_once __DIR__ . '/../lib/versioncheck.php';
  31. require_once __DIR__ . '/../lib/base.php';
  32. if (\OCP\Util::needUpgrade()
  33. || \OC::$server->getConfig()->getSystemValueBool('maintenance')) {
  34. // since the behavior of apps or remotes are unpredictable during
  35. // an upgrade, return a 503 directly
  36. http_response_code(503);
  37. header('X-Nextcloud-Maintenance-Mode: 1');
  38. $response = new \OC\OCS\Result(null, 503, 'Service unavailable');
  39. OC_API::respond($response, OC_API::requestedFormat());
  40. exit;
  41. }
  42. use OCP\Security\Bruteforce\MaxDelayReached;
  43. use Psr\Log\LoggerInterface;
  44. use Symfony\Component\Routing\Exception\MethodNotAllowedException;
  45. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  46. /*
  47. * Try the appframework routes
  48. */
  49. try {
  50. OC_App::loadApps(['session']);
  51. OC_App::loadApps(['authentication']);
  52. OC_App::loadApps(['extended_authentication']);
  53. // load all apps to get all api routes properly setup
  54. // FIXME: this should ideally appear after handleLogin but will cause
  55. // side effects in existing apps
  56. OC_App::loadApps();
  57. if (!\OC::$server->getUserSession()->isLoggedIn()) {
  58. OC::handleLogin(\OC::$server->getRequest());
  59. }
  60. OC::$server->get(\OC\Route\Router::class)->match('/ocsapp'.\OC::$server->getRequest()->getRawPathInfo());
  61. } catch (MaxDelayReached $ex) {
  62. $format = \OC::$server->getRequest()->getParam('format', 'xml');
  63. OC_API::respond(new \OC\OCS\Result(null, OCP\AppFramework\Http::STATUS_TOO_MANY_REQUESTS, $ex->getMessage()), $format);
  64. } catch (ResourceNotFoundException $e) {
  65. OC_API::setContentType();
  66. $format = \OC::$server->getRequest()->getParam('format', 'xml');
  67. $txt = 'Invalid query, please check the syntax. API specifications are here:'
  68. .' http://www.freedesktop.org/wiki/Specifications/open-collaboration-services.'."\n";
  69. OC_API::respond(new \OC\OCS\Result(null, \OCP\AppFramework\OCSController::RESPOND_NOT_FOUND, $txt), $format);
  70. } catch (MethodNotAllowedException $e) {
  71. OC_API::setContentType();
  72. http_response_code(405);
  73. } catch (\OC\OCS\Exception $ex) {
  74. OC_API::respond($ex->getResult(), OC_API::requestedFormat());
  75. } catch (\OC\User\LoginException $e) {
  76. OC_API::respond(new \OC\OCS\Result(null, \OCP\AppFramework\OCSController::RESPOND_UNAUTHORISED, 'Unauthorised'));
  77. } catch (\Exception $e) {
  78. \OCP\Server::get(LoggerInterface::class)->error($e->getMessage(), ['exception' => $e]);
  79. OC_API::setContentType();
  80. $format = \OC::$server->getRequest()->getParam('format', 'xml');
  81. $txt = 'Internal Server Error'."\n";
  82. try {
  83. if (\OC::$server->getSystemConfig()->getValue('debug', false)) {
  84. $txt .= $e->getMessage();
  85. }
  86. } catch (\Throwable $e) {
  87. // Just to be save
  88. }
  89. OC_API::respond(new \OC\OCS\Result(null, \OCP\AppFramework\OCSController::RESPOND_SERVER_ERROR, $txt), $format);
  90. }