remote.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. require_once __DIR__ . '/lib/versioncheck.php';
  8. use OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin;
  9. use Psr\Log\LoggerInterface;
  10. use Sabre\DAV\Exception\ServiceUnavailable;
  11. use Sabre\DAV\Server;
  12. /**
  13. * Class RemoteException
  14. * Dummy exception class to be use locally to identify certain conditions
  15. * Will not be logged to avoid DoS
  16. */
  17. class RemoteException extends \Exception {
  18. }
  19. /**
  20. * @param Exception|Error $e
  21. */
  22. function handleException($e) {
  23. try {
  24. $request = \OC::$server->getRequest();
  25. $isError = $e instanceof Error;
  26. // in case the request content type is text/xml - we assume it's a WebDAV request
  27. $isXmlContentType = strpos($request->getHeader('Content-Type'), 'text/xml');
  28. if ($isError === false && $isXmlContentType === 0) {
  29. // fire up a simple server to properly process the exception
  30. $server = new Server();
  31. if (!($e instanceof RemoteException)) {
  32. // we shall not log on RemoteException
  33. $server->addPlugin(new ExceptionLoggerPlugin('webdav', \OC::$server->get(LoggerInterface::class)));
  34. }
  35. $server->on('beforeMethod:*', function () use ($e) {
  36. if ($e instanceof RemoteException) {
  37. switch ($e->getCode()) {
  38. case 503:
  39. throw new ServiceUnavailable($e->getMessage());
  40. case 404:
  41. throw new \Sabre\DAV\Exception\NotFound($e->getMessage());
  42. }
  43. }
  44. $class = get_class($e);
  45. $msg = $e->getMessage();
  46. throw new ServiceUnavailable("$class: $msg");
  47. });
  48. $server->exec();
  49. } else {
  50. $statusCode = 500;
  51. if ($e instanceof \OC\ServiceUnavailableException) {
  52. $statusCode = 503;
  53. }
  54. if ($e instanceof RemoteException) {
  55. // we shall not log on RemoteException
  56. OC_Template::printErrorPage($e->getMessage(), '', $e->getCode());
  57. } else {
  58. \OC::$server->get(LoggerInterface::class)->error($e->getMessage(), ['app' => 'remote','exception' => $e]);
  59. OC_Template::printExceptionErrorPage($e, $statusCode);
  60. }
  61. }
  62. } catch (\Exception $e) {
  63. OC_Template::printExceptionErrorPage($e, 500);
  64. }
  65. }
  66. /**
  67. * @param $service
  68. * @return string
  69. */
  70. function resolveService($service) {
  71. $services = [
  72. 'webdav' => 'dav/appinfo/v1/webdav.php',
  73. 'dav' => 'dav/appinfo/v2/remote.php',
  74. 'caldav' => 'dav/appinfo/v1/caldav.php',
  75. 'calendar' => 'dav/appinfo/v1/caldav.php',
  76. 'carddav' => 'dav/appinfo/v1/carddav.php',
  77. 'contacts' => 'dav/appinfo/v1/carddav.php',
  78. 'files' => 'dav/appinfo/v1/webdav.php',
  79. 'direct' => 'dav/appinfo/v2/direct.php',
  80. ];
  81. if (isset($services[$service])) {
  82. return $services[$service];
  83. }
  84. return \OC::$server->getConfig()->getAppValue('core', 'remote_' . $service);
  85. }
  86. try {
  87. require_once __DIR__ . '/lib/base.php';
  88. // All resources served via the DAV endpoint should have the strictest possible
  89. // policy. Exempted from this is the SabreDAV browser plugin which overwrites
  90. // this policy with a softer one if debug mode is enabled.
  91. header("Content-Security-Policy: default-src 'none';");
  92. if (\OCP\Util::needUpgrade()) {
  93. // since the behavior of apps or remotes are unpredictable during
  94. // an upgrade, return a 503 directly
  95. throw new RemoteException('Service unavailable', 503);
  96. }
  97. $request = \OC::$server->getRequest();
  98. $pathInfo = $request->getPathInfo();
  99. if ($pathInfo === false || $pathInfo === '') {
  100. throw new RemoteException('Path not found', 404);
  101. }
  102. if (!$pos = strpos($pathInfo, '/', 1)) {
  103. $pos = strlen($pathInfo);
  104. }
  105. $service = substr($pathInfo, 1, $pos - 1);
  106. $file = resolveService($service);
  107. if (is_null($file)) {
  108. throw new RemoteException('Path not found', 404);
  109. }
  110. $file = ltrim($file, '/');
  111. $parts = explode('/', $file, 2);
  112. $app = $parts[0];
  113. // Load all required applications
  114. \OC::$REQUESTEDAPP = $app;
  115. OC_App::loadApps(['authentication']);
  116. OC_App::loadApps(['extended_authentication']);
  117. OC_App::loadApps(['filesystem', 'logging']);
  118. switch ($app) {
  119. case 'core':
  120. $file = OC::$SERVERROOT .'/'. $file;
  121. break;
  122. default:
  123. if (!\OC::$server->getAppManager()->isInstalled($app)) {
  124. throw new RemoteException('App not installed: ' . $app);
  125. }
  126. OC_App::loadApp($app);
  127. $file = OC_App::getAppPath($app) .'/'. $parts[1];
  128. break;
  129. }
  130. $baseuri = OC::$WEBROOT . '/remote.php/'.$service.'/';
  131. require_once $file;
  132. } catch (Exception $ex) {
  133. handleException($ex);
  134. } catch (Error $e) {
  135. handleException($e);
  136. }