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