publicwebdav.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-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\LegacyPublicAuth;
  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\FederatedFileSharing\FederatedShareProvider;
  16. use OCP\BeforeSabrePubliclyLoadedEvent;
  17. use OCP\Constants;
  18. use OCP\EventDispatcher\IEventDispatcher;
  19. use OCP\Files\IRootFolder;
  20. use OCP\Server;
  21. use Psr\Log\LoggerInterface;
  22. // load needed apps
  23. $RUNTIME_APPTYPES = ['filesystem', 'authentication', 'logging'];
  24. OC_App::loadApps($RUNTIME_APPTYPES);
  25. OC_Util::obEnd();
  26. \OC::$server->getSession()->close();
  27. // Backends
  28. $authBackend = new LegacyPublicAuth(
  29. \OC::$server->getRequest(),
  30. \OC::$server->getShareManager(),
  31. \OC::$server->getSession(),
  32. \OC::$server->getBruteForceThrottler()
  33. );
  34. $authPlugin = new \Sabre\DAV\Auth\Plugin($authBackend);
  35. /** @var IEventDispatcher $eventDispatcher */
  36. $eventDispatcher = \OC::$server->get(IEventDispatcher::class);
  37. $serverFactory = new ServerFactory(
  38. \OC::$server->getConfig(),
  39. \OC::$server->get(LoggerInterface::class),
  40. \OC::$server->getDatabaseConnection(),
  41. \OC::$server->getUserSession(),
  42. \OC::$server->getMountManager(),
  43. \OC::$server->getTagManager(),
  44. \OC::$server->getRequest(),
  45. \OC::$server->getPreviewManager(),
  46. $eventDispatcher,
  47. \OC::$server->getL10N('dav')
  48. );
  49. $requestUri = \OC::$server->getRequest()->getRequestUri();
  50. $linkCheckPlugin = new PublicLinkCheckPlugin();
  51. $filesDropPlugin = new FilesDropPlugin();
  52. $server = $serverFactory->createServer($baseuri, $requestUri, $authPlugin, function (\Sabre\DAV\Server $server) use ($authBackend, $linkCheckPlugin, $filesDropPlugin) {
  53. $isAjax = in_array('XMLHttpRequest', explode(',', $_SERVER['HTTP_X_REQUESTED_WITH'] ?? ''));
  54. /** @var FederatedShareProvider $shareProvider */
  55. $federatedShareProvider = \OC::$server->query(FederatedShareProvider::class);
  56. if ($federatedShareProvider->isOutgoingServer2serverShareEnabled() === false && !$isAjax) {
  57. // this is what is thrown when trying to access a non-existing share
  58. throw new \Sabre\DAV\Exception\NotAuthenticated();
  59. }
  60. $share = $authBackend->getShare();
  61. $owner = $share->getShareOwner();
  62. $isReadable = $share->getPermissions() & Constants::PERMISSION_READ;
  63. $fileId = $share->getNodeId();
  64. // FIXME: should not add storage wrappers outside of preSetup, need to find a better way
  65. $previousLog = Filesystem::logWarningWhenAddingStorageWrapper(false);
  66. Filesystem::addStorageWrapper('sharePermissions', function ($mountPoint, $storage) use ($share) {
  67. return new PermissionsMask(['storage' => $storage, 'mask' => $share->getPermissions() | Constants::PERMISSION_SHARE]);
  68. });
  69. Filesystem::addStorageWrapper('shareOwner', function ($mountPoint, $storage) use ($share) {
  70. return new PublicOwnerWrapper(['storage' => $storage, 'owner' => $share->getShareOwner()]);
  71. });
  72. Filesystem::logWarningWhenAddingStorageWrapper($previousLog);
  73. $rootFolder = Server::get(IRootFolder::class);
  74. $userFolder = $rootFolder->getUserFolder($owner);
  75. $node = $userFolder->getFirstNodeById($fileId);
  76. if (!$node) {
  77. throw new \Sabre\DAV\Exception\NotFound();
  78. }
  79. $linkCheckPlugin->setFileInfo($node);
  80. // If not readable (files_drop) enable the filesdrop plugin
  81. if (!$isReadable) {
  82. $filesDropPlugin->enable();
  83. }
  84. $view = new View($node->getPath());
  85. $filesDropPlugin->setView($view);
  86. $filesDropPlugin->setShare($share);
  87. return $view;
  88. });
  89. $server->addPlugin($linkCheckPlugin);
  90. $server->addPlugin($filesDropPlugin);
  91. // allow setup of additional plugins
  92. $event = new BeforeSabrePubliclyLoadedEvent($server);
  93. $eventDispatcher->dispatchTyped($event);
  94. // And off we go!
  95. $server->exec();