remote.php 5.1 KB

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