remote.php 5.2 KB

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