publicremote.php 4.5 KB

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