v1.php 3.5 KB

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