publicwebdav.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. OC_App::loadApps(['authentication']);
  37. OC_App::loadApps(['extended_authentication']);
  38. OC_App::loadApps(['filesystem', 'logging']);
  39. OC_Util::obEnd();
  40. \OC::$server->getSession()->close();
  41. // Backends
  42. $authBackend = new OCA\DAV\Connector\PublicAuth(
  43. \OC::$server->getRequest(),
  44. \OC::$server->getShareManager(),
  45. \OC::$server->getSession(),
  46. \OC::$server->getBruteForceThrottler()
  47. );
  48. $authPlugin = new \Sabre\DAV\Auth\Plugin($authBackend);
  49. $serverFactory = new OCA\DAV\Connector\Sabre\ServerFactory(
  50. \OC::$server->getConfig(),
  51. \OC::$server->get(LoggerInterface::class),
  52. \OC::$server->getDatabaseConnection(),
  53. \OC::$server->getUserSession(),
  54. \OC::$server->getMountManager(),
  55. \OC::$server->getTagManager(),
  56. \OC::$server->getRequest(),
  57. \OC::$server->getPreviewManager(),
  58. \OC::$server->getEventDispatcher(),
  59. \OC::$server->getL10N('dav')
  60. );
  61. $requestUri = \OC::$server->getRequest()->getRequestUri();
  62. $linkCheckPlugin = new \OCA\DAV\Files\Sharing\PublicLinkCheckPlugin();
  63. $filesDropPlugin = new \OCA\DAV\Files\Sharing\FilesDropPlugin();
  64. $server = $serverFactory->createServer($baseuri, $requestUri, $authPlugin, function (\Sabre\DAV\Server $server) use ($authBackend, $linkCheckPlugin, $filesDropPlugin) {
  65. $isAjax = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest');
  66. /** @var \OCA\FederatedFileSharing\FederatedShareProvider $shareProvider */
  67. $federatedShareProvider = \OC::$server->query(\OCA\FederatedFileSharing\FederatedShareProvider::class);
  68. if ($federatedShareProvider->isOutgoingServer2serverShareEnabled() === false && !$isAjax) {
  69. // this is what is thrown when trying to access a non-existing share
  70. throw new \Sabre\DAV\Exception\NotAuthenticated();
  71. }
  72. $share = $authBackend->getShare();
  73. $owner = $share->getShareOwner();
  74. $isReadable = $share->getPermissions() & \OCP\Constants::PERMISSION_READ;
  75. $fileId = $share->getNodeId();
  76. // FIXME: should not add storage wrappers outside of preSetup, need to find a better way
  77. $previousLog = \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(false);
  78. \OC\Files\Filesystem::addStorageWrapper('sharePermissions', function ($mountPoint, $storage) use ($share) {
  79. return new \OC\Files\Storage\Wrapper\PermissionsMask(['storage' => $storage, 'mask' => $share->getPermissions() | \OCP\Constants::PERMISSION_SHARE]);
  80. });
  81. \OC\Files\Filesystem::addStorageWrapper('shareOwner', function ($mountPoint, $storage) use ($share) {
  82. return new \OCA\DAV\Storage\PublicOwnerWrapper(['storage' => $storage, 'owner' => $share->getShareOwner()]);
  83. });
  84. \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper($previousLog);
  85. OC_Util::tearDownFS();
  86. OC_Util::setupFS($owner);
  87. $ownerView = new \OC\Files\View('/'. $owner . '/files');
  88. $path = $ownerView->getPath($fileId);
  89. $fileInfo = $ownerView->getFileInfo($path);
  90. $linkCheckPlugin->setFileInfo($fileInfo);
  91. // If not readable (files_drop) enable the filesdrop plugin
  92. if (!$isReadable) {
  93. $filesDropPlugin->enable();
  94. }
  95. $view = new \OC\Files\View($ownerView->getAbsolutePath($path));
  96. $filesDropPlugin->setView($view);
  97. return $view;
  98. });
  99. $server->addPlugin($linkCheckPlugin);
  100. $server->addPlugin($filesDropPlugin);
  101. // allow setup of additional plugins
  102. $event = new BeforeSabrePubliclyLoadedEvent($server);
  103. /** @var IEventDispatcher $eventDispatcher */
  104. $eventDispatcher = \OC::$server->get(IEventDispatcher::class);
  105. $eventDispatcher->dispatchTyped($event);
  106. // And off we go!
  107. $server->exec();