v1.php 3.4 KB

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