|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @copyright Copyright (c) 2016, ownCloud, Inc. |
| 4 | + * |
| 5 | + * @author Bjoern Schiessle <[email protected]> |
| 6 | + * @author Björn Schießle <[email protected]> |
| 7 | + * @author Christoph Wurst <[email protected]> |
| 8 | + * @author Joas Schilling <[email protected]> |
| 9 | + * @author Julius Härtl <[email protected]> |
| 10 | + * @author Lukas Reschke <[email protected]> |
| 11 | + * @author Morris Jobke <[email protected]> |
| 12 | + * @author Robin Appelman <[email protected]> |
| 13 | + * @author Roeland Jago Douma <[email protected]> |
| 14 | + * @author Thomas Müller <[email protected]> |
| 15 | + * @author Vincent Petry <[email protected]> |
| 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 | + |
| 33 | +use OC\Files\Filesystem; |
| 34 | +use OC\Files\Storage\Wrapper\PermissionsMask; |
| 35 | +use OC\Files\View; |
| 36 | +use OCA\DAV\Storage\PublicOwnerWrapper; |
| 37 | +use OCA\FederatedFileSharing\FederatedShareProvider; |
| 38 | +use OCP\EventDispatcher\IEventDispatcher; |
| 39 | +use OCP\Files\Mount\IMountManager; |
| 40 | +use OCP\IConfig; |
| 41 | +use OCP\IDBConnection; |
| 42 | +use OCP\IPreview; |
| 43 | +use OCP\IRequest; |
| 44 | +use OCP\ISession; |
| 45 | +use OCP\ITagManager; |
| 46 | +use OCP\IUserSession; |
| 47 | +use OCP\L10N\IFactory; |
| 48 | +use OCP\Security\Bruteforce\IThrottler; |
| 49 | +use OCP\Share\IManager; |
| 50 | +use Psr\Log\LoggerInterface; |
| 51 | +use Sabre\DAV\Exception\NotAuthenticated; |
| 52 | +use Sabre\DAV\Exception\NotFound; |
| 53 | + |
| 54 | +// load needed apps |
| 55 | +$RUNTIME_APPTYPES = ['filesystem', 'authentication', 'logging']; |
| 56 | +OC_App::loadApps($RUNTIME_APPTYPES); |
| 57 | +OC_Util::obEnd(); |
| 58 | + |
| 59 | +$session = \OCP\Server::get(ISession::class); |
| 60 | +$request = \OCP\Server::get(IRequest::class); |
| 61 | + |
| 62 | +$session->close(); |
| 63 | +$requestUri = $request->getRequestUri(); |
| 64 | + |
| 65 | +// Backends |
| 66 | +$authBackend = new OCA\DAV\Connector\Sabre\PublicAuth( |
| 67 | + $request, |
| 68 | + \OCP\Server::get(IManager::class), |
| 69 | + $session, |
| 70 | + \OCP\Server::get(IThrottler::class), |
| 71 | + \OCP\Server::get(LoggerInterface::class) |
| 72 | +); |
| 73 | +$authPlugin = new \Sabre\DAV\Auth\Plugin($authBackend); |
| 74 | + |
| 75 | +$l10nFactory = \OCP\Server::get(IFactory::class); |
| 76 | +$serverFactory = new OCA\DAV\Connector\Sabre\ServerFactory( |
| 77 | + \OCP\Server::get(IConfig::class), |
| 78 | + \OCP\Server::get(LoggerInterface::class), |
| 79 | + \OCP\Server::get(IDBConnection::class), |
| 80 | + \OCP\Server::get(IUserSession::class), |
| 81 | + \OCP\Server::get(IMountManager::class), |
| 82 | + \OCP\Server::get(ITagManager::class), |
| 83 | + $request, |
| 84 | + \OCP\Server::get(IPreview::class), |
| 85 | + \OCP\Server::get(IEventDispatcher::class), |
| 86 | + $l10nFactory->get('dav'), |
| 87 | +); |
| 88 | + |
| 89 | + |
| 90 | +$linkCheckPlugin = new \OCA\DAV\Files\Sharing\PublicLinkCheckPlugin(); |
| 91 | +$filesDropPlugin = new \OCA\DAV\Files\Sharing\FilesDropPlugin(); |
| 92 | + |
| 93 | +// Define root url with /public.php/dav/files/TOKEN |
| 94 | +/** @var string $baseuri defined in public.php */ |
| 95 | +preg_match('/(^files\/\w+)/i', substr($requestUri, strlen($baseuri)), $match); |
| 96 | +$baseuri = $baseuri . $match[0]; |
| 97 | + |
| 98 | +$server = $serverFactory->createServer($baseuri, $requestUri, $authPlugin, function (\Sabre\DAV\Server $server) use ($authBackend, $linkCheckPlugin, $filesDropPlugin) { |
| 99 | + $isAjax = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest'); |
| 100 | + $federatedShareProvider = \OCP\Server::get(FederatedShareProvider::class); |
| 101 | + if ($federatedShareProvider->isOutgoingServer2serverShareEnabled() === false && !$isAjax) { |
| 102 | + // this is what is thrown when trying to access a non-existing share |
| 103 | + throw new NotAuthenticated(); |
| 104 | + } |
| 105 | + |
| 106 | + $share = $authBackend->getShare(); |
| 107 | + $owner = $share->getShareOwner(); |
| 108 | + $isReadable = $share->getPermissions() & \OCP\Constants::PERMISSION_READ; |
| 109 | + $fileId = $share->getNodeId(); |
| 110 | + |
| 111 | + // FIXME: should not add storage wrappers outside of preSetup, need to find a better way |
| 112 | + /** @psalm-suppress InternalMethod */ |
| 113 | + $previousLog = Filesystem::logWarningWhenAddingStorageWrapper(false); |
| 114 | + |
| 115 | + /** @psalm-suppress MissingClosureParamType */ |
| 116 | + Filesystem::addStorageWrapper('sharePermissions', function ($mountPoint, $storage) use ($share) { |
| 117 | + return new PermissionsMask(['storage' => $storage, 'mask' => $share->getPermissions() | \OCP\Constants::PERMISSION_SHARE]); |
| 118 | + }); |
| 119 | + |
| 120 | + /** @psalm-suppress MissingClosureParamType */ |
| 121 | + Filesystem::addStorageWrapper('shareOwner', function ($mountPoint, $storage) use ($share) { |
| 122 | + return new PublicOwnerWrapper(['storage' => $storage, 'owner' => $share->getShareOwner()]); |
| 123 | + }); |
| 124 | + |
| 125 | + /** @psalm-suppress InternalMethod */ |
| 126 | + Filesystem::logWarningWhenAddingStorageWrapper($previousLog); |
| 127 | + |
| 128 | + OC_Util::tearDownFS(); |
| 129 | + OC_Util::setupFS($owner); |
| 130 | + $ownerView = new View('/'. $owner . '/files'); |
| 131 | + $path = $ownerView->getPath($fileId); |
| 132 | + $fileInfo = $ownerView->getFileInfo($path); |
| 133 | + |
| 134 | + if ($fileInfo === false) { |
| 135 | + throw new NotFound(); |
| 136 | + } |
| 137 | + |
| 138 | + $linkCheckPlugin->setFileInfo($fileInfo); |
| 139 | + |
| 140 | + // If not readble (files_drop) enable the filesdrop plugin |
| 141 | + if (!$isReadable) { |
| 142 | + $filesDropPlugin->enable(); |
| 143 | + } |
| 144 | + |
| 145 | + $view = new View($ownerView->getAbsolutePath($path)); |
| 146 | + $filesDropPlugin->setView($view); |
| 147 | + |
| 148 | + return $view; |
| 149 | +}); |
| 150 | + |
| 151 | +$server->addPlugin($linkCheckPlugin); |
| 152 | +$server->addPlugin($filesDropPlugin); |
| 153 | + |
| 154 | +// And off we go! |
| 155 | +$server->exec(); |
0 commit comments