v1.php 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 Symfony\Component\Routing\Exception\MethodNotAllowedException;
  44. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  45. /*
  46. * Try the appframework routes
  47. */
  48. try {
  49. OC_App::loadApps(['session']);
  50. OC_App::loadApps(['authentication']);
  51. OC_App::loadApps(['extended_authentication']);
  52. // load all apps to get all api routes properly setup
  53. // FIXME: this should ideally appear after handleLogin but will cause
  54. // side effects in existing apps
  55. OC_App::loadApps();
  56. if (!\OC::$server->getUserSession()->isLoggedIn()) {
  57. OC::handleLogin(\OC::$server->getRequest());
  58. }
  59. OC::$server->get(\OC\Route\Router::class)->match('/ocsapp'.\OC::$server->getRequest()->getRawPathInfo());
  60. } catch (MaxDelayReached $ex) {
  61. $format = \OC::$server->getRequest()->getParam('format', 'xml');
  62. OC_API::respond(new \OC\OCS\Result(null, OCP\AppFramework\Http::STATUS_TOO_MANY_REQUESTS, $ex->getMessage()), $format);
  63. } catch (ResourceNotFoundException $e) {
  64. OC_API::setContentType();
  65. $format = \OC::$server->getRequest()->getParam('format', 'xml');
  66. $txt = 'Invalid query, please check the syntax. API specifications are here:'
  67. .' http://www.freedesktop.org/wiki/Specifications/open-collaboration-services.'."\n";
  68. OC_API::respond(new \OC\OCS\Result(null, \OCP\AppFramework\OCSController::RESPOND_NOT_FOUND, $txt), $format);
  69. } catch (MethodNotAllowedException $e) {
  70. OC_API::setContentType();
  71. http_response_code(405);
  72. } catch (\OC\OCS\Exception $ex) {
  73. OC_API::respond($ex->getResult(), OC_API::requestedFormat());
  74. } catch (\OC\User\LoginException $e) {
  75. OC_API::respond(new \OC\OCS\Result(null, \OCP\AppFramework\OCSController::RESPOND_UNAUTHORISED, 'Unauthorised'));
  76. } catch (\Exception $e) {
  77. \OC::$server->getLogger()->logException($e);
  78. OC_API::setContentType();
  79. $format = \OC::$server->getRequest()->getParam('format', 'xml');
  80. $txt = 'Internal Server Error'."\n";
  81. try {
  82. if (\OC::$server->getSystemConfig()->getValue('debug', false)) {
  83. $txt .= $e->getMessage();
  84. }
  85. } catch (\Throwable $e) {
  86. // Just to be save
  87. }
  88. OC_API::respond(new \OC\OCS\Result(null, \OCP\AppFramework\OCSController::RESPOND_SERVER_ERROR, $txt), $format);
  89. }