remote.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. use OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin;
  31. use Sabre\DAV\Exception\ServiceUnavailable;
  32. use Sabre\DAV\Server;
  33. /**
  34. * Class RemoteException
  35. * Dummy exception class to be use locally to identify certain conditions
  36. * Will not be logged to avoid DoS
  37. */
  38. class RemoteException extends Exception {
  39. }
  40. /**
  41. * @param Exception $e
  42. */
  43. function handleException(Exception $e) {
  44. $request = \OC::$server->getRequest();
  45. // in case the request content type is text/xml - we assume it's a WebDAV request
  46. $isXmlContentType = strpos($request->getHeader('Content-Type'), 'text/xml');
  47. if ($isXmlContentType === 0) {
  48. // fire up a simple server to properly process the exception
  49. $server = new Server();
  50. if (!($e instanceof RemoteException)) {
  51. // we shall not log on RemoteException
  52. $server->addPlugin(new ExceptionLoggerPlugin('webdav', \OC::$server->getLogger()));
  53. }
  54. $server->on('beforeMethod', function () use ($e) {
  55. if ($e instanceof RemoteException) {
  56. switch ($e->getCode()) {
  57. case OC_Response::STATUS_SERVICE_UNAVAILABLE:
  58. throw new ServiceUnavailable($e->getMessage());
  59. case OC_Response::STATUS_NOT_FOUND:
  60. throw new \Sabre\DAV\Exception\NotFound($e->getMessage());
  61. }
  62. }
  63. $class = get_class($e);
  64. $msg = $e->getMessage();
  65. throw new ServiceUnavailable("$class: $msg");
  66. });
  67. $server->exec();
  68. } else {
  69. $statusCode = OC_Response::STATUS_INTERNAL_SERVER_ERROR;
  70. if ($e instanceof \OC\ServiceUnavailableException ) {
  71. $statusCode = OC_Response::STATUS_SERVICE_UNAVAILABLE;
  72. }
  73. if ($e instanceof RemoteException) {
  74. // we shall not log on RemoteException
  75. OC_Response::setStatus($e->getCode());
  76. OC_Template::printErrorPage($e->getMessage());
  77. } else {
  78. \OCP\Util::writeLog('remote', $e->getMessage(), \OCP\Util::FATAL);
  79. OC_Response::setStatus($statusCode);
  80. OC_Template::printExceptionErrorPage($e);
  81. }
  82. }
  83. }
  84. try {
  85. require_once __DIR__ . '/lib/base.php';
  86. if (\OCP\Util::needUpgrade()) {
  87. // since the behavior of apps or remotes are unpredictable during
  88. // an upgrade, return a 503 directly
  89. throw new RemoteException('Service unavailable', OC_Response::STATUS_SERVICE_UNAVAILABLE);
  90. }
  91. $request = \OC::$server->getRequest();
  92. $pathInfo = $request->getPathInfo();
  93. if ($pathInfo === false || $pathInfo === '') {
  94. throw new RemoteException('Path not found', OC_Response::STATUS_NOT_FOUND);
  95. }
  96. if (!$pos = strpos($pathInfo, '/', 1)) {
  97. $pos = strlen($pathInfo);
  98. }
  99. $service=substr($pathInfo, 1, $pos-1);
  100. $file = \OC::$server->getConfig()->getAppValue('core', 'remote_' . $service);
  101. if(is_null($file)) {
  102. throw new RemoteException('Path not found', OC_Response::STATUS_NOT_FOUND);
  103. }
  104. // force language as given in the http request
  105. \OC::$server->getL10NFactory()->setLanguageFromRequest();
  106. $file=ltrim($file, '/');
  107. $parts=explode('/', $file, 2);
  108. $app=$parts[0];
  109. // Load all required applications
  110. \OC::$REQUESTEDAPP = $app;
  111. OC_App::loadApps(array('authentication'));
  112. OC_App::loadApps(array('filesystem', 'logging'));
  113. switch ($app) {
  114. case 'core':
  115. $file = OC::$SERVERROOT .'/'. $file;
  116. break;
  117. default:
  118. if (!\OC::$server->getAppManager()->isInstalled($app)) {
  119. throw new RemoteException('App not installed: ' . $app);
  120. }
  121. OC_App::loadApp($app);
  122. $file = OC_App::getAppPath($app) .'/'. $parts[1];
  123. break;
  124. }
  125. $baseuri = OC::$WEBROOT . '/remote.php/'.$service.'/';
  126. require_once $file;
  127. } catch (Exception $ex) {
  128. handleException($ex);
  129. }