publicwebdav.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Julius Härtl <jus@bitgrid.net>
  10. * @author Lukas Reschke <lukas@statuscode.ch>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Roeland Jago Douma <roeland@famdouma.nl>
  14. * @author Thomas Müller <thomas.mueller@tmit.eu>
  15. * @author Vincent Petry <vincent@nextcloud.com>
  16. *
  17. * @license AGPL-3.0
  18. *
  19. * This code is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Affero General Public License, version 3,
  21. * as published by the Free Software Foundation.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License, version 3,
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>
  30. *
  31. */
  32. use OCP\BeforeSabrePubliclyLoadedEvent;
  33. use OCP\EventDispatcher\IEventDispatcher;
  34. use Psr\Log\LoggerInterface;
  35. // load needed apps
  36. $RUNTIME_APPTYPES = ['filesystem', 'authentication', 'logging'];
  37. OC_App::loadApps($RUNTIME_APPTYPES);
  38. OC_Util::obEnd();
  39. \OC::$server->getSession()->close();
  40. // Backends
  41. $authBackend = new OCA\DAV\Connector\LegacyPublicAuth(
  42. \OC::$server->getRequest(),
  43. \OC::$server->getShareManager(),
  44. \OC::$server->getSession(),
  45. \OC::$server->getBruteForceThrottler()
  46. );
  47. $authPlugin = new \Sabre\DAV\Auth\Plugin($authBackend);
  48. /** @var IEventDispatcher $eventDispatcher */
  49. $eventDispatcher = \OC::$server->get(IEventDispatcher::class);
  50. $serverFactory = new OCA\DAV\Connector\Sabre\ServerFactory(
  51. \OC::$server->getConfig(),
  52. \OC::$server->get(LoggerInterface::class),
  53. \OC::$server->getDatabaseConnection(),
  54. \OC::$server->getUserSession(),
  55. \OC::$server->getMountManager(),
  56. \OC::$server->getTagManager(),
  57. \OC::$server->getRequest(),
  58. \OC::$server->getPreviewManager(),
  59. $eventDispatcher,
  60. \OC::$server->getL10N('dav')
  61. );
  62. $requestUri = \OC::$server->getRequest()->getRequestUri();
  63. $linkCheckPlugin = new \OCA\DAV\Files\Sharing\PublicLinkCheckPlugin();
  64. $filesDropPlugin = new \OCA\DAV\Files\Sharing\FilesDropPlugin();
  65. $server = $serverFactory->createServer($baseuri, $requestUri, $authPlugin, function (\Sabre\DAV\Server $server) use ($authBackend, $linkCheckPlugin, $filesDropPlugin) {
  66. $isAjax = in_array('XMLHttpRequest', explode(',', $_SERVER['HTTP_X_REQUESTED_WITH'] ?? ''));
  67. /** @var \OCA\FederatedFileSharing\FederatedShareProvider $shareProvider */
  68. $federatedShareProvider = \OC::$server->query(\OCA\FederatedFileSharing\FederatedShareProvider::class);
  69. if ($federatedShareProvider->isOutgoingServer2serverShareEnabled() === false && !$isAjax) {
  70. // this is what is thrown when trying to access a non-existing share
  71. throw new \Sabre\DAV\Exception\NotAuthenticated();
  72. }
  73. $share = $authBackend->getShare();
  74. $owner = $share->getShareOwner();
  75. $isReadable = $share->getPermissions() & \OCP\Constants::PERMISSION_READ;
  76. $fileId = $share->getNodeId();
  77. // FIXME: should not add storage wrappers outside of preSetup, need to find a better way
  78. $previousLog = \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(false);
  79. \OC\Files\Filesystem::addStorageWrapper('sharePermissions', function ($mountPoint, $storage) use ($share) {
  80. return new \OC\Files\Storage\Wrapper\PermissionsMask(['storage' => $storage, 'mask' => $share->getPermissions() | \OCP\Constants::PERMISSION_SHARE]);
  81. });
  82. \OC\Files\Filesystem::addStorageWrapper('shareOwner', function ($mountPoint, $storage) use ($share) {
  83. return new \OCA\DAV\Storage\PublicOwnerWrapper(['storage' => $storage, 'owner' => $share->getShareOwner()]);
  84. });
  85. \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper($previousLog);
  86. OC_Util::tearDownFS();
  87. OC_Util::setupFS($owner);
  88. $ownerView = new \OC\Files\View('/'. $owner . '/files');
  89. $path = $ownerView->getPath($fileId);
  90. $fileInfo = $ownerView->getFileInfo($path);
  91. $linkCheckPlugin->setFileInfo($fileInfo);
  92. // If not readable (files_drop) enable the filesdrop plugin
  93. if (!$isReadable) {
  94. $filesDropPlugin->enable();
  95. }
  96. $view = new \OC\Files\View($ownerView->getAbsolutePath($path));
  97. $filesDropPlugin->setView($view);
  98. return $view;
  99. });
  100. $server->addPlugin($linkCheckPlugin);
  101. $server->addPlugin($filesDropPlugin);
  102. // allow setup of additional plugins
  103. $event = new BeforeSabrePubliclyLoadedEvent($server);
  104. $eventDispatcher->dispatchTyped($event);
  105. // And off we go!
  106. $server->exec();