publicwebdav.php 3.6 KB

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