remote.php 4.2 KB

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