1
0

remote.php 5.2 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. /**
  39. * Class RemoteException
  40. * Dummy exception class to be use locally to identify certain conditions
  41. * Will not be logged to avoid DoS
  42. */
  43. class RemoteException extends Exception {
  44. }
  45. /**
  46. * @param Exception|Error $e
  47. */
  48. function handleException($e) {
  49. $request = \OC::$server->getRequest();
  50. // in case the request content type is text/xml - we assume it's a WebDAV request
  51. $isXmlContentType = strpos($request->getHeader('Content-Type'), 'text/xml');
  52. if ($isXmlContentType === 0) {
  53. // fire up a simple server to properly process the exception
  54. $server = new Server();
  55. if (!($e instanceof RemoteException)) {
  56. // we shall not log on RemoteException
  57. $server->addPlugin(new ExceptionLoggerPlugin('webdav', \OC::$server->getLogger()));
  58. }
  59. $server->on('beforeMethod:*', function () use ($e) {
  60. if ($e instanceof RemoteException) {
  61. switch ($e->getCode()) {
  62. case 503:
  63. throw new ServiceUnavailable($e->getMessage());
  64. case 404:
  65. throw new \Sabre\DAV\Exception\NotFound($e->getMessage());
  66. }
  67. }
  68. $class = get_class($e);
  69. $msg = $e->getMessage();
  70. throw new ServiceUnavailable("$class: $msg");
  71. });
  72. $server->exec();
  73. } else {
  74. $statusCode = 500;
  75. if ($e instanceof \OC\ServiceUnavailableException) {
  76. $statusCode = 503;
  77. }
  78. if ($e instanceof RemoteException) {
  79. // we shall not log on RemoteException
  80. OC_Template::printErrorPage($e->getMessage(), '', $e->getCode());
  81. } else {
  82. \OC::$server->getLogger()->logException($e, ['app' => 'remote']);
  83. OC_Template::printExceptionErrorPage($e, $statusCode);
  84. }
  85. }
  86. }
  87. /**
  88. * @param $service
  89. * @return string
  90. */
  91. function resolveService($service) {
  92. $services = [
  93. 'webdav' => 'dav/appinfo/v1/webdav.php',
  94. 'dav' => 'dav/appinfo/v2/remote.php',
  95. 'caldav' => 'dav/appinfo/v1/caldav.php',
  96. 'calendar' => 'dav/appinfo/v1/caldav.php',
  97. 'carddav' => 'dav/appinfo/v1/carddav.php',
  98. 'contacts' => 'dav/appinfo/v1/carddav.php',
  99. 'files' => 'dav/appinfo/v1/webdav.php',
  100. 'direct' => 'dav/appinfo/v2/direct.php',
  101. ];
  102. if (isset($services[$service])) {
  103. return $services[$service];
  104. }
  105. return \OC::$server->getConfig()->getAppValue('core', 'remote_' . $service);
  106. }
  107. try {
  108. require_once __DIR__ . '/lib/base.php';
  109. // All resources served via the DAV endpoint should have the strictest possible
  110. // policy. Exempted from this is the SabreDAV browser plugin which overwrites
  111. // this policy with a softer one if debug mode is enabled.
  112. header("Content-Security-Policy: default-src 'none';");
  113. if (\OCP\Util::needUpgrade()) {
  114. // since the behavior of apps or remotes are unpredictable during
  115. // an upgrade, return a 503 directly
  116. throw new RemoteException('Service unavailable', 503);
  117. }
  118. $request = \OC::$server->getRequest();
  119. $pathInfo = $request->getPathInfo();
  120. if ($pathInfo === false || $pathInfo === '') {
  121. throw new RemoteException('Path not found', 404);
  122. }
  123. if (!$pos = strpos($pathInfo, '/', 1)) {
  124. $pos = strlen($pathInfo);
  125. }
  126. $service = substr($pathInfo, 1, $pos - 1);
  127. $file = resolveService($service);
  128. if (is_null($file)) {
  129. throw new RemoteException('Path not found', 404);
  130. }
  131. $file = ltrim($file, '/');
  132. $parts = explode('/', $file, 2);
  133. $app = $parts[0];
  134. // Load all required applications
  135. \OC::$REQUESTEDAPP = $app;
  136. OC_App::loadApps(['authentication']);
  137. OC_App::loadApps(['filesystem', 'logging']);
  138. switch ($app) {
  139. case 'core':
  140. $file = OC::$SERVERROOT .'/'. $file;
  141. break;
  142. default:
  143. if (!\OC::$server->getAppManager()->isInstalled($app)) {
  144. throw new RemoteException('App not installed: ' . $app);
  145. }
  146. OC_App::loadApp($app);
  147. $file = OC_App::getAppPath($app) .'/'. $parts[1];
  148. break;
  149. }
  150. $baseuri = OC::$WEBROOT . '/remote.php/'.$service.'/';
  151. require_once $file;
  152. } catch (Exception $ex) {
  153. handleException($ex);
  154. } catch (Error $e) {
  155. handleException($e);
  156. }