1
0

publicremote.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Julius Härtl <jus@bitgrid.net>
  10. * @author Lukas Reschke <lukas@statuscode.ch>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Roeland Jago Douma <roeland@famdouma.nl>
  14. * @author Thomas Müller <thomas.mueller@tmit.eu>
  15. * @author Vincent Petry <vincent@nextcloud.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. use OC\Files\Filesystem;
  33. use OC\Files\Storage\Wrapper\PermissionsMask;
  34. use OC\Files\View;
  35. use OCA\DAV\Storage\PublicOwnerWrapper;
  36. use OCA\FederatedFileSharing\FederatedShareProvider;
  37. use OCP\EventDispatcher\IEventDispatcher;
  38. use OCP\Files\Mount\IMountManager;
  39. use OCP\IConfig;
  40. use OCP\IDBConnection;
  41. use OCP\IPreview;
  42. use OCP\IRequest;
  43. use OCP\ISession;
  44. use OCP\ITagManager;
  45. use OCP\IUserSession;
  46. use OCP\L10N\IFactory;
  47. use OCP\Security\Bruteforce\IThrottler;
  48. use OCP\Share\IManager;
  49. use Psr\Log\LoggerInterface;
  50. use Sabre\DAV\Exception\NotAuthenticated;
  51. use Sabre\DAV\Exception\NotFound;
  52. // load needed apps
  53. $RUNTIME_APPTYPES = ['filesystem', 'authentication', 'logging'];
  54. OC_App::loadApps($RUNTIME_APPTYPES);
  55. OC_Util::obEnd();
  56. $session = \OCP\Server::get(ISession::class);
  57. $request = \OCP\Server::get(IRequest::class);
  58. $session->close();
  59. $requestUri = $request->getRequestUri();
  60. // Backends
  61. $authBackend = new OCA\DAV\Connector\Sabre\PublicAuth(
  62. $request,
  63. \OCP\Server::get(IManager::class),
  64. $session,
  65. \OCP\Server::get(IThrottler::class),
  66. \OCP\Server::get(LoggerInterface::class)
  67. );
  68. $authPlugin = new \Sabre\DAV\Auth\Plugin($authBackend);
  69. $l10nFactory = \OCP\Server::get(IFactory::class);
  70. $serverFactory = new OCA\DAV\Connector\Sabre\ServerFactory(
  71. \OCP\Server::get(IConfig::class),
  72. \OCP\Server::get(LoggerInterface::class),
  73. \OCP\Server::get(IDBConnection::class),
  74. \OCP\Server::get(IUserSession::class),
  75. \OCP\Server::get(IMountManager::class),
  76. \OCP\Server::get(ITagManager::class),
  77. $request,
  78. \OCP\Server::get(IPreview::class),
  79. \OCP\Server::get(IEventDispatcher::class),
  80. $l10nFactory->get('dav'),
  81. );
  82. $linkCheckPlugin = new \OCA\DAV\Files\Sharing\PublicLinkCheckPlugin();
  83. $filesDropPlugin = new \OCA\DAV\Files\Sharing\FilesDropPlugin();
  84. // Define root url with /public.php/dav/files/TOKEN
  85. /** @var string $baseuri defined in public.php */
  86. preg_match('/(^files\/\w+)/i', substr($requestUri, strlen($baseuri)), $match);
  87. $baseuri = $baseuri . $match[0];
  88. $server = $serverFactory->createServer($baseuri, $requestUri, $authPlugin, function (\Sabre\DAV\Server $server) use ($authBackend, $linkCheckPlugin, $filesDropPlugin) {
  89. $isAjax = in_array('XMLHttpRequest', explode(',', $_SERVER['HTTP_X_REQUESTED_WITH'] ?? ''));
  90. $federatedShareProvider = \OCP\Server::get(FederatedShareProvider::class);
  91. if ($federatedShareProvider->isOutgoingServer2serverShareEnabled() === false && !$isAjax) {
  92. // this is what is thrown when trying to access a non-existing share
  93. throw new NotAuthenticated();
  94. }
  95. $share = $authBackend->getShare();
  96. $owner = $share->getShareOwner();
  97. $isReadable = $share->getPermissions() & \OCP\Constants::PERMISSION_READ;
  98. $fileId = $share->getNodeId();
  99. // FIXME: should not add storage wrappers outside of preSetup, need to find a better way
  100. /** @psalm-suppress InternalMethod */
  101. $previousLog = Filesystem::logWarningWhenAddingStorageWrapper(false);
  102. /** @psalm-suppress MissingClosureParamType */
  103. Filesystem::addStorageWrapper('sharePermissions', function ($mountPoint, $storage) use ($share) {
  104. return new PermissionsMask(['storage' => $storage, 'mask' => $share->getPermissions() | \OCP\Constants::PERMISSION_SHARE]);
  105. });
  106. /** @psalm-suppress MissingClosureParamType */
  107. Filesystem::addStorageWrapper('shareOwner', function ($mountPoint, $storage) use ($share) {
  108. return new PublicOwnerWrapper(['storage' => $storage, 'owner' => $share->getShareOwner()]);
  109. });
  110. /** @psalm-suppress InternalMethod */
  111. Filesystem::logWarningWhenAddingStorageWrapper($previousLog);
  112. OC_Util::tearDownFS();
  113. OC_Util::setupFS($owner);
  114. $ownerView = new View('/'. $owner . '/files');
  115. $path = $ownerView->getPath($fileId);
  116. $fileInfo = $ownerView->getFileInfo($path);
  117. if ($fileInfo === false) {
  118. throw new NotFound();
  119. }
  120. $linkCheckPlugin->setFileInfo($fileInfo);
  121. // If not readble (files_drop) enable the filesdrop plugin
  122. if (!$isReadable) {
  123. $filesDropPlugin->enable();
  124. }
  125. $view = new View($ownerView->getAbsolutePath($path));
  126. $filesDropPlugin->setView($view);
  127. return $view;
  128. });
  129. $server->addPlugin($linkCheckPlugin);
  130. $server->addPlugin($filesDropPlugin);
  131. // And off we go!
  132. $server->exec();