1
0

publicremote.php 5.0 KB

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