v1.php 3.4 KB

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