remote.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * @author Brice Maron <brice@bmaron.net>
  4. * @author Christopher Schäpers <kondou@ts.unde.re>
  5. * @author Georg Ehrke <georg@owncloud.com>
  6. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  7. * @author Lukas Reschke <lukas@owncloud.com>
  8. * @author Robin Appelman <icewind@owncloud.com>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. * @author Vincent Petry <pvince81@owncloud.com>
  11. *
  12. * @copyright Copyright (c) 2015, ownCloud, Inc.
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. use OC\Connector\Sabre\ExceptionLoggerPlugin;
  29. use Sabre\DAV\Exception\ServiceUnavailable;
  30. use Sabre\DAV\Server;
  31. /**
  32. * Class RemoteException
  33. * Dummy exception class to be use locally to identify certain conditions
  34. * Will not be logged to avoid DoS
  35. */
  36. class RemoteException extends Exception {
  37. }
  38. /**
  39. * @param Exception $e
  40. */
  41. function handleException(Exception $e) {
  42. $request = \OC::$server->getRequest();
  43. // in case the request content type is text/xml - we assume it's a WebDAV request
  44. $isXmlContentType = strpos($request->getHeader('Content-Type'), 'text/xml');
  45. if ($isXmlContentType === 0) {
  46. // fire up a simple server to properly process the exception
  47. $server = new Server();
  48. if (!($e instanceof RemoteException)) {
  49. // we shall not log on RemoteException
  50. $server->addPlugin(new ExceptionLoggerPlugin('webdav', \OC::$server->getLogger()));
  51. }
  52. $server->on('beforeMethod', function () use ($e) {
  53. if ($e instanceof RemoteException) {
  54. switch ($e->getCode()) {
  55. case OC_Response::STATUS_SERVICE_UNAVAILABLE:
  56. throw new ServiceUnavailable($e->getMessage());
  57. case OC_Response::STATUS_NOT_FOUND:
  58. throw new \Sabre\DAV\Exception\NotFound($e->getMessage());
  59. }
  60. }
  61. $class = get_class($e);
  62. $msg = $e->getMessage();
  63. throw new ServiceUnavailable("$class: $msg");
  64. });
  65. $server->exec();
  66. } else {
  67. $statusCode = OC_Response::STATUS_INTERNAL_SERVER_ERROR;
  68. if ($e instanceof \OC\ServiceUnavailableException ) {
  69. $statusCode = OC_Response::STATUS_SERVICE_UNAVAILABLE;
  70. }
  71. if ($e instanceof RemoteException) {
  72. // we shall not log on RemoteException
  73. OC_Response::setStatus($e->getCode());
  74. OC_Template::printErrorPage($e->getMessage());
  75. } else {
  76. \OCP\Util::writeLog('remote', $e->getMessage(), \OCP\Util::FATAL);
  77. OC_Response::setStatus($statusCode);
  78. OC_Template::printExceptionErrorPage($e);
  79. }
  80. }
  81. }
  82. try {
  83. require_once 'lib/base.php';
  84. if (\OCP\Util::needUpgrade()) {
  85. // since the behavior of apps or remotes are unpredictable during
  86. // an upgrade, return a 503 directly
  87. throw new RemoteException('Service unavailable', OC_Response::STATUS_SERVICE_UNAVAILABLE);
  88. }
  89. $request = \OC::$server->getRequest();
  90. $pathInfo = $request->getPathInfo();
  91. if ($pathInfo === false || $pathInfo === '') {
  92. throw new RemoteException('Path not found', OC_Response::STATUS_NOT_FOUND);
  93. }
  94. if (!$pos = strpos($pathInfo, '/', 1)) {
  95. $pos = strlen($pathInfo);
  96. }
  97. $service=substr($pathInfo, 1, $pos-1);
  98. $file = \OC::$server->getConfig()->getAppValue('core', 'remote_' . $service);
  99. if(is_null($file)) {
  100. throw new RemoteException('Path not found', OC_Response::STATUS_NOT_FOUND);
  101. }
  102. // force language as given in the http request
  103. \OC_L10N::setLanguageFromRequest();
  104. $file=ltrim($file, '/');
  105. $parts=explode('/', $file, 2);
  106. $app=$parts[0];
  107. // Load all required applications
  108. \OC::$REQUESTEDAPP = $app;
  109. OC_App::loadApps(array('authentication'));
  110. OC_App::loadApps(array('filesystem', 'logging'));
  111. switch ($app) {
  112. case 'core':
  113. $file = OC::$SERVERROOT .'/'. $file;
  114. break;
  115. default:
  116. if (!\OC::$server->getAppManager()->isInstalled($app)) {
  117. throw new RemoteException('App not installed: ' . $app);
  118. }
  119. OC_App::loadApp($app);
  120. $file = OC_App::getAppPath($app) .'/'. $parts[1];
  121. break;
  122. }
  123. $baseuri = OC::$WEBROOT . '/remote.php/'.$service.'/';
  124. require_once $file;
  125. } catch (Exception $ex) {
  126. handleException($ex);
  127. }