publicremote.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2020-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. use OC\Files\Filesystem;
  8. use OC\Files\Storage\Wrapper\PermissionsMask;
  9. use OC\Files\View;
  10. use OCA\DAV\Storage\PublicOwnerWrapper;
  11. use OCA\FederatedFileSharing\FederatedShareProvider;
  12. use OCP\EventDispatcher\IEventDispatcher;
  13. use OCP\Files\Mount\IMountManager;
  14. use OCP\IConfig;
  15. use OCP\IDBConnection;
  16. use OCP\IPreview;
  17. use OCP\IRequest;
  18. use OCP\ISession;
  19. use OCP\ITagManager;
  20. use OCP\IUserSession;
  21. use OCP\L10N\IFactory;
  22. use OCP\Security\Bruteforce\IThrottler;
  23. use OCP\Share\IManager;
  24. use Psr\Log\LoggerInterface;
  25. use Sabre\DAV\Exception\NotAuthenticated;
  26. use Sabre\DAV\Exception\NotFound;
  27. // load needed apps
  28. $RUNTIME_APPTYPES = ['filesystem', 'authentication', 'logging'];
  29. OC_App::loadApps($RUNTIME_APPTYPES);
  30. OC_Util::obEnd();
  31. $session = \OCP\Server::get(ISession::class);
  32. $request = \OCP\Server::get(IRequest::class);
  33. $session->close();
  34. $requestUri = $request->getRequestUri();
  35. // Backends
  36. $authBackend = new OCA\DAV\Connector\Sabre\PublicAuth(
  37. $request,
  38. \OCP\Server::get(IManager::class),
  39. $session,
  40. \OCP\Server::get(IThrottler::class),
  41. \OCP\Server::get(LoggerInterface::class)
  42. );
  43. $authPlugin = new \Sabre\DAV\Auth\Plugin($authBackend);
  44. $l10nFactory = \OCP\Server::get(IFactory::class);
  45. $serverFactory = new OCA\DAV\Connector\Sabre\ServerFactory(
  46. \OCP\Server::get(IConfig::class),
  47. \OCP\Server::get(LoggerInterface::class),
  48. \OCP\Server::get(IDBConnection::class),
  49. \OCP\Server::get(IUserSession::class),
  50. \OCP\Server::get(IMountManager::class),
  51. \OCP\Server::get(ITagManager::class),
  52. $request,
  53. \OCP\Server::get(IPreview::class),
  54. \OCP\Server::get(IEventDispatcher::class),
  55. $l10nFactory->get('dav'),
  56. );
  57. $linkCheckPlugin = new \OCA\DAV\Files\Sharing\PublicLinkCheckPlugin();
  58. $filesDropPlugin = new \OCA\DAV\Files\Sharing\FilesDropPlugin();
  59. // Define root url with /public.php/dav/files/TOKEN
  60. /** @var string $baseuri defined in public.php */
  61. preg_match('/(^files\/\w+)/i', substr($requestUri, strlen($baseuri)), $match);
  62. $baseuri = $baseuri . $match[0];
  63. $server = $serverFactory->createServer($baseuri, $requestUri, $authPlugin, function (\Sabre\DAV\Server $server) use ($authBackend, $linkCheckPlugin, $filesDropPlugin) {
  64. $isAjax = in_array('XMLHttpRequest', explode(',', $_SERVER['HTTP_X_REQUESTED_WITH'] ?? ''));
  65. $federatedShareProvider = \OCP\Server::get(FederatedShareProvider::class);
  66. if ($federatedShareProvider->isOutgoingServer2serverShareEnabled() === false && !$isAjax) {
  67. // this is what is thrown when trying to access a non-existing share
  68. throw new NotAuthenticated();
  69. }
  70. $share = $authBackend->getShare();
  71. $owner = $share->getShareOwner();
  72. $isReadable = $share->getPermissions() & \OCP\Constants::PERMISSION_READ;
  73. $fileId = $share->getNodeId();
  74. // FIXME: should not add storage wrappers outside of preSetup, need to find a better way
  75. /** @psalm-suppress InternalMethod */
  76. $previousLog = Filesystem::logWarningWhenAddingStorageWrapper(false);
  77. /** @psalm-suppress MissingClosureParamType */
  78. Filesystem::addStorageWrapper('sharePermissions', function ($mountPoint, $storage) use ($share) {
  79. return new PermissionsMask(['storage' => $storage, 'mask' => $share->getPermissions() | \OCP\Constants::PERMISSION_SHARE]);
  80. });
  81. /** @psalm-suppress MissingClosureParamType */
  82. Filesystem::addStorageWrapper('shareOwner', function ($mountPoint, $storage) use ($share) {
  83. return new PublicOwnerWrapper(['storage' => $storage, 'owner' => $share->getShareOwner()]);
  84. });
  85. /** @psalm-suppress InternalMethod */
  86. Filesystem::logWarningWhenAddingStorageWrapper($previousLog);
  87. $rootFolder = \OCP\Server::get(\OCP\Files\IRootFolder::class);
  88. $userFolder = $rootFolder->getUserFolder($owner);
  89. $node = $userFolder->getFirstNodeById($fileId);
  90. if (!$node) {
  91. throw new NotFound();
  92. }
  93. $linkCheckPlugin->setFileInfo($node);
  94. // If not readable (files_drop) enable the filesdrop plugin
  95. if (!$isReadable) {
  96. $filesDropPlugin->enable();
  97. }
  98. $view = new View($node->getPath());
  99. $filesDropPlugin->setView($view);
  100. return $view;
  101. });
  102. $server->addPlugin($linkCheckPlugin);
  103. $server->addPlugin($filesDropPlugin);
  104. // And off we go!
  105. $server->exec();