1
0

remote.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Brice Maron <brice@bmaron.net>
  6. * @author Christopher Schäpers <kondou@ts.unde.re>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Georg Ehrke <oc.list@georgehrke.com>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Robin Appelman <robin@icewind.nl>
  14. * @author Robin McCorkell <robin@mccorkell.me.uk>
  15. * @author Roeland Jago Douma <roeland@famdouma.nl>
  16. * @author Thomas Müller <thomas.mueller@tmit.eu>
  17. * @author Vincent Petry <vincent@nextcloud.com>
  18. *
  19. * @license AGPL-3.0
  20. *
  21. * This code is free software: you can redistribute it and/or modify
  22. * it under the terms of the GNU Affero General Public License, version 3,
  23. * as published by the Free Software Foundation.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU Affero General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU Affero General Public License, version 3,
  31. * along with this program. If not, see <http://www.gnu.org/licenses/>
  32. *
  33. */
  34. require_once __DIR__ . '/lib/versioncheck.php';
  35. use OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin;
  36. use Sabre\DAV\Exception\ServiceUnavailable;
  37. use Sabre\DAV\Server;
  38. use Psr\Log\LoggerInterface;
  39. /**
  40. * Class RemoteException
  41. * Dummy exception class to be use locally to identify certain conditions
  42. * Will not be logged to avoid DoS
  43. */
  44. class RemoteException extends Exception {
  45. }
  46. /**
  47. * @param Exception|Error $e
  48. */
  49. function handleException($e) {
  50. $request = \OC::$server->getRequest();
  51. // in case the request content type is text/xml - we assume it's a WebDAV request
  52. $isXmlContentType = strpos($request->getHeader('Content-Type'), 'text/xml');
  53. if ($isXmlContentType === 0) {
  54. // fire up a simple server to properly process the exception
  55. $server = new Server();
  56. if (!($e instanceof RemoteException)) {
  57. // we shall not log on RemoteException
  58. $server->addPlugin(new ExceptionLoggerPlugin('webdav', \OC::$server->get(LoggerInterface::class)));
  59. }
  60. $server->on('beforeMethod:*', function () use ($e) {
  61. if ($e instanceof RemoteException) {
  62. switch ($e->getCode()) {
  63. case 503:
  64. throw new ServiceUnavailable($e->getMessage());
  65. case 404:
  66. throw new \Sabre\DAV\Exception\NotFound($e->getMessage());
  67. }
  68. }
  69. $class = get_class($e);
  70. $msg = $e->getMessage();
  71. throw new ServiceUnavailable("$class: $msg");
  72. });
  73. $server->exec();
  74. } else {
  75. $statusCode = 500;
  76. if ($e instanceof \OC\ServiceUnavailableException) {
  77. $statusCode = 503;
  78. }
  79. if ($e instanceof RemoteException) {
  80. // we shall not log on RemoteException
  81. OC_Template::printErrorPage($e->getMessage(), '', $e->getCode());
  82. } else {
  83. \OC::$server->get(LoggerInterface::class)->error($e->getMessage(), ['app' => 'remote','exception' => $e]);
  84. OC_Template::printExceptionErrorPage($e, $statusCode);
  85. }
  86. }
  87. }
  88. /**
  89. * @param $service
  90. * @return string
  91. */
  92. function resolveService($service) {
  93. $services = [
  94. 'webdav' => 'dav/appinfo/v1/webdav.php',
  95. 'dav' => 'dav/appinfo/v2/remote.php',
  96. 'caldav' => 'dav/appinfo/v1/caldav.php',
  97. 'calendar' => 'dav/appinfo/v1/caldav.php',
  98. 'carddav' => 'dav/appinfo/v1/carddav.php',
  99. 'contacts' => 'dav/appinfo/v1/carddav.php',
  100. 'files' => 'dav/appinfo/v1/webdav.php',
  101. 'direct' => 'dav/appinfo/v2/direct.php',
  102. ];
  103. if (isset($services[$service])) {
  104. return $services[$service];
  105. }
  106. return \OC::$server->getConfig()->getAppValue('core', 'remote_' . $service);
  107. }
  108. try {
  109. require_once __DIR__ . '/lib/base.php';
  110. // All resources served via the DAV endpoint should have the strictest possible
  111. // policy. Exempted from this is the SabreDAV browser plugin which overwrites
  112. // this policy with a softer one if debug mode is enabled.
  113. header("Content-Security-Policy: default-src 'none';");
  114. if (\OCP\Util::needUpgrade()) {
  115. // since the behavior of apps or remotes are unpredictable during
  116. // an upgrade, return a 503 directly
  117. throw new RemoteException('Service unavailable', 503);
  118. }
  119. $request = \OC::$server->getRequest();
  120. $pathInfo = $request->getPathInfo();
  121. if ($pathInfo === false || $pathInfo === '') {
  122. throw new RemoteException('Path not found', 404);
  123. }
  124. if (!$pos = strpos($pathInfo, '/', 1)) {
  125. $pos = strlen($pathInfo);
  126. }
  127. $service = substr($pathInfo, 1, $pos - 1);
  128. $file = resolveService($service);
  129. if (is_null($file)) {
  130. throw new RemoteException('Path not found', 404);
  131. }
  132. $file = ltrim($file, '/');
  133. $parts = explode('/', $file, 2);
  134. $app = $parts[0];
  135. // Load all required applications
  136. \OC::$REQUESTEDAPP = $app;
  137. OC_App::loadApps(['authentication']);
  138. OC_App::loadApps(['filesystem', 'logging']);
  139. switch ($app) {
  140. case 'core':
  141. $file = OC::$SERVERROOT .'/'. $file;
  142. break;
  143. default:
  144. if (!\OC::$server->getAppManager()->isInstalled($app)) {
  145. throw new RemoteException('App not installed: ' . $app);
  146. }
  147. OC_App::loadApp($app);
  148. $file = OC_App::getAppPath($app) .'/'. $parts[1];
  149. break;
  150. }
  151. $baseuri = OC::$WEBROOT . '/remote.php/'.$service.'/';
  152. require_once $file;
  153. } catch (Exception $ex) {
  154. handleException($ex);
  155. } catch (Error $e) {
  156. handleException($e);
  157. }