選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

SharedStorage.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author J0WI <J0WI@users.noreply.github.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Robin McCorkell <robin@mccorkell.me.uk>
  13. * @author Roeland Jago Douma <roeland@famdouma.nl>
  14. * @author scambra <sergio@entrecables.com>
  15. * @author Thomas Müller <thomas.mueller@tmit.eu>
  16. * @author Vincent Petry <vincent@nextcloud.com>
  17. *
  18. * @license AGPL-3.0
  19. *
  20. * This code is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU Affero General Public License, version 3,
  22. * as published by the Free Software Foundation.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU Affero General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Affero General Public License, version 3,
  30. * along with this program. If not, see <http://www.gnu.org/licenses/>
  31. *
  32. */
  33. namespace OCA\Files_Sharing;
  34. use OC\Files\Cache\CacheDependencies;
  35. use OC\Files\Cache\FailedCache;
  36. use OC\Files\Cache\NullWatcher;
  37. use OC\Files\Cache\Watcher;
  38. use OC\Files\ObjectStore\HomeObjectStoreStorage;
  39. use OC\Files\Storage\Common;
  40. use OC\Files\Storage\FailedStorage;
  41. use OC\Files\Storage\Home;
  42. use OC\Files\Storage\Wrapper\PermissionsMask;
  43. use OC\User\NoUserException;
  44. use OCA\Files_External\Config\ConfigAdapter;
  45. use OCP\Constants;
  46. use OCP\Files\Cache\ICacheEntry;
  47. use OCP\Files\Config\IUserMountCache;
  48. use OCP\Files\Folder;
  49. use OCP\Files\IHomeStorage;
  50. use OCP\Files\IRootFolder;
  51. use OCP\Files\Node;
  52. use OCP\Files\NotFoundException;
  53. use OCP\Files\Storage\IDisableEncryptionStorage;
  54. use OCP\Files\Storage\IStorage;
  55. use OCP\Lock\ILockingProvider;
  56. use OCP\Share\IShare;
  57. use Psr\Log\LoggerInterface;
  58. /**
  59. * Convert target path to source path and pass the function call to the correct storage provider
  60. */
  61. class SharedStorage extends \OC\Files\Storage\Wrapper\Jail implements ISharedStorage, IDisableEncryptionStorage {
  62. /** @var \OCP\Share\IShare */
  63. private $superShare;
  64. /** @var \OCP\Share\IShare[] */
  65. private $groupedShares;
  66. /**
  67. * @var \OC\Files\View
  68. */
  69. private $ownerView;
  70. private $initialized = false;
  71. /**
  72. * @var ICacheEntry
  73. */
  74. private $sourceRootInfo;
  75. /** @var string */
  76. private $user;
  77. private LoggerInterface $logger;
  78. /** @var IStorage */
  79. private $nonMaskedStorage;
  80. private array $mountOptions = [];
  81. /** @var boolean */
  82. private $sharingDisabledForUser;
  83. /** @var ?Folder $ownerUserFolder */
  84. private $ownerUserFolder = null;
  85. private string $sourcePath = '';
  86. public function __construct($arguments) {
  87. $this->ownerView = $arguments['ownerView'];
  88. $this->logger = \OC::$server->get(LoggerInterface::class);
  89. $this->superShare = $arguments['superShare'];
  90. $this->groupedShares = $arguments['groupedShares'];
  91. $this->user = $arguments['user'];
  92. if (isset($arguments['sharingDisabledForUser'])) {
  93. $this->sharingDisabledForUser = $arguments['sharingDisabledForUser'];
  94. } else {
  95. $this->sharingDisabledForUser = false;
  96. }
  97. parent::__construct([
  98. 'storage' => null,
  99. 'root' => null,
  100. ]);
  101. }
  102. /**
  103. * @return ICacheEntry
  104. */
  105. private function getSourceRootInfo() {
  106. if (is_null($this->sourceRootInfo)) {
  107. if (is_null($this->superShare->getNodeCacheEntry())) {
  108. $this->init();
  109. $this->sourceRootInfo = $this->nonMaskedStorage->getCache()->get($this->rootPath);
  110. } else {
  111. $this->sourceRootInfo = $this->superShare->getNodeCacheEntry();
  112. }
  113. }
  114. return $this->sourceRootInfo;
  115. }
  116. private function init() {
  117. if ($this->initialized) {
  118. return;
  119. }
  120. $this->initialized = true;
  121. try {
  122. /** @var IRootFolder $rootFolder */
  123. $rootFolder = \OC::$server->get(IRootFolder::class);
  124. $this->ownerUserFolder = $rootFolder->getUserFolder($this->superShare->getShareOwner());
  125. $sourceId = $this->superShare->getNodeId();
  126. $ownerNode = $this->ownerUserFolder->getFirstNodeById($sourceId);
  127. if (!$ownerNode) {
  128. $this->storage = new FailedStorage(['exception' => new NotFoundException("File by id $sourceId not found")]);
  129. $this->cache = new FailedCache();
  130. $this->rootPath = '';
  131. } else {
  132. $this->nonMaskedStorage = $ownerNode->getStorage();
  133. $this->sourcePath = $ownerNode->getPath();
  134. $this->rootPath = $ownerNode->getInternalPath();
  135. $this->storage = new PermissionsMask([
  136. 'storage' => $this->nonMaskedStorage,
  137. 'mask' => $this->superShare->getPermissions(),
  138. ]);
  139. }
  140. } catch (NotFoundException $e) {
  141. // original file not accessible or deleted, set FailedStorage
  142. $this->storage = new FailedStorage(['exception' => $e]);
  143. $this->cache = new FailedCache();
  144. $this->rootPath = '';
  145. } catch (NoUserException $e) {
  146. // sharer user deleted, set FailedStorage
  147. $this->storage = new FailedStorage(['exception' => $e]);
  148. $this->cache = new FailedCache();
  149. $this->rootPath = '';
  150. } catch (\Exception $e) {
  151. $this->storage = new FailedStorage(['exception' => $e]);
  152. $this->cache = new FailedCache();
  153. $this->rootPath = '';
  154. $this->logger->error($e->getMessage(), ['exception' => $e]);
  155. }
  156. if (!$this->nonMaskedStorage) {
  157. $this->nonMaskedStorage = $this->storage;
  158. }
  159. }
  160. /**
  161. * @inheritdoc
  162. */
  163. public function instanceOfStorage($class): bool {
  164. if ($class === '\OC\Files\Storage\Common' || $class == Common::class) {
  165. return true;
  166. }
  167. if (in_array($class, [
  168. '\OC\Files\Storage\Home',
  169. '\OC\Files\ObjectStore\HomeObjectStoreStorage',
  170. '\OCP\Files\IHomeStorage',
  171. Home::class,
  172. HomeObjectStoreStorage::class,
  173. IHomeStorage::class
  174. ])) {
  175. return false;
  176. }
  177. return parent::instanceOfStorage($class);
  178. }
  179. /**
  180. * @return string
  181. */
  182. public function getShareId() {
  183. return $this->superShare->getId();
  184. }
  185. private function isValid(): bool {
  186. return $this->getSourceRootInfo() && ($this->getSourceRootInfo()->getPermissions() & Constants::PERMISSION_SHARE) === Constants::PERMISSION_SHARE;
  187. }
  188. /**
  189. * get id of the mount point
  190. *
  191. * @return string
  192. */
  193. public function getId(): string {
  194. return 'shared::' . $this->getMountPoint();
  195. }
  196. /**
  197. * Get the permissions granted for a shared file
  198. *
  199. * @param string $path Shared target file path
  200. * @return int CRUDS permissions granted
  201. */
  202. public function getPermissions($path = ''): int {
  203. if (!$this->isValid()) {
  204. return 0;
  205. }
  206. $permissions = parent::getPermissions($path) & $this->superShare->getPermissions();
  207. // part files and the mount point always have delete permissions
  208. if ($path === '' || pathinfo($path, PATHINFO_EXTENSION) === 'part') {
  209. $permissions |= \OCP\Constants::PERMISSION_DELETE;
  210. }
  211. if ($this->sharingDisabledForUser) {
  212. $permissions &= ~\OCP\Constants::PERMISSION_SHARE;
  213. }
  214. return $permissions;
  215. }
  216. public function isCreatable($path): bool {
  217. return (bool)($this->getPermissions($path) & \OCP\Constants::PERMISSION_CREATE);
  218. }
  219. public function isReadable($path): bool {
  220. if (!$this->isValid()) {
  221. return false;
  222. }
  223. if (!$this->file_exists($path)) {
  224. return false;
  225. }
  226. /** @var IStorage $storage */
  227. /** @var string $internalPath */
  228. [$storage, $internalPath] = $this->resolvePath($path);
  229. return $storage->isReadable($internalPath);
  230. }
  231. public function isUpdatable($path): bool {
  232. return (bool)($this->getPermissions($path) & \OCP\Constants::PERMISSION_UPDATE);
  233. }
  234. public function isDeletable($path): bool {
  235. return (bool)($this->getPermissions($path) & \OCP\Constants::PERMISSION_DELETE);
  236. }
  237. public function isSharable($path): bool {
  238. if (\OCP\Util::isSharingDisabledForUser() || !\OC\Share\Share::isResharingAllowed()) {
  239. return false;
  240. }
  241. return (bool)($this->getPermissions($path) & \OCP\Constants::PERMISSION_SHARE);
  242. }
  243. public function fopen($path, $mode) {
  244. $source = $this->getUnjailedPath($path);
  245. switch ($mode) {
  246. case 'r+':
  247. case 'rb+':
  248. case 'w+':
  249. case 'wb+':
  250. case 'x+':
  251. case 'xb+':
  252. case 'a+':
  253. case 'ab+':
  254. case 'w':
  255. case 'wb':
  256. case 'x':
  257. case 'xb':
  258. case 'a':
  259. case 'ab':
  260. $creatable = $this->isCreatable(dirname($path));
  261. $updatable = $this->isUpdatable($path);
  262. // if neither permissions given, no need to continue
  263. if (!$creatable && !$updatable) {
  264. if (pathinfo($path, PATHINFO_EXTENSION) === 'part') {
  265. $updatable = $this->isUpdatable(dirname($path));
  266. }
  267. if (!$updatable) {
  268. return false;
  269. }
  270. }
  271. $exists = $this->file_exists($path);
  272. // if a file exists, updatable permissions are required
  273. if ($exists && !$updatable) {
  274. return false;
  275. }
  276. // part file is allowed if !$creatable but the final file is $updatable
  277. if (pathinfo($path, PATHINFO_EXTENSION) !== 'part') {
  278. if (!$exists && !$creatable) {
  279. return false;
  280. }
  281. }
  282. }
  283. $info = [
  284. 'target' => $this->getMountPoint() . '/' . $path,
  285. 'source' => $source,
  286. 'mode' => $mode,
  287. ];
  288. \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'fopen', $info);
  289. return $this->nonMaskedStorage->fopen($this->getUnjailedPath($path), $mode);
  290. }
  291. /**
  292. * see https://www.php.net/manual/en/function.rename.php
  293. *
  294. * @param string $source
  295. * @param string $target
  296. * @return bool
  297. */
  298. public function rename($source, $target): bool {
  299. $this->init();
  300. $isPartFile = pathinfo($source, PATHINFO_EXTENSION) === 'part';
  301. $targetExists = $this->file_exists($target);
  302. $sameFolder = dirname($source) === dirname($target);
  303. if ($targetExists || ($sameFolder && !$isPartFile)) {
  304. if (!$this->isUpdatable('')) {
  305. return false;
  306. }
  307. } else {
  308. if (!$this->isCreatable('')) {
  309. return false;
  310. }
  311. }
  312. return $this->nonMaskedStorage->rename($this->getUnjailedPath($source), $this->getUnjailedPath($target));
  313. }
  314. /**
  315. * return mount point of share, relative to data/user/files
  316. *
  317. * @return string
  318. */
  319. public function getMountPoint(): string {
  320. return $this->superShare->getTarget();
  321. }
  322. /**
  323. * @param string $path
  324. */
  325. public function setMountPoint($path): void {
  326. $this->superShare->setTarget($path);
  327. foreach ($this->groupedShares as $share) {
  328. $share->setTarget($path);
  329. }
  330. }
  331. /**
  332. * get the user who shared the file
  333. *
  334. * @return string
  335. */
  336. public function getSharedFrom(): string {
  337. return $this->superShare->getShareOwner();
  338. }
  339. /**
  340. * @return \OCP\Share\IShare
  341. */
  342. public function getShare(): IShare {
  343. return $this->superShare;
  344. }
  345. /**
  346. * return share type, can be "file" or "folder"
  347. *
  348. * @return string
  349. */
  350. public function getItemType(): string {
  351. return $this->superShare->getNodeType();
  352. }
  353. public function getCache($path = '', $storage = null) {
  354. if ($this->cache) {
  355. return $this->cache;
  356. }
  357. if (!$storage) {
  358. $storage = $this;
  359. }
  360. $sourceRoot = $this->getSourceRootInfo();
  361. if ($this->storage instanceof FailedStorage) {
  362. return new FailedCache();
  363. }
  364. $this->cache = new Cache(
  365. $storage,
  366. $sourceRoot,
  367. \OC::$server->get(CacheDependencies::class),
  368. $this->getShare()
  369. );
  370. return $this->cache;
  371. }
  372. public function getScanner($path = '', $storage = null) {
  373. if (!$storage) {
  374. $storage = $this;
  375. }
  376. return new \OCA\Files_Sharing\Scanner($storage);
  377. }
  378. public function getOwner($path): string {
  379. return $this->superShare->getShareOwner();
  380. }
  381. public function getWatcher($path = '', $storage = null): Watcher {
  382. if ($this->watcher) {
  383. return $this->watcher;
  384. }
  385. // Get node information
  386. $node = $this->getShare()->getNodeCacheEntry();
  387. if ($node) {
  388. /** @var IUserMountCache $userMountCache */
  389. $userMountCache = \OC::$server->get(IUserMountCache::class);
  390. $mounts = $userMountCache->getMountsForStorageId($node->getStorageId());
  391. foreach ($mounts as $mount) {
  392. // If the share is originating from an external storage
  393. if ($mount->getMountProvider() === ConfigAdapter::class) {
  394. // Propagate original storage scan
  395. $this->watcher = parent::getWatcher($path, $storage);
  396. return $this->watcher;
  397. }
  398. }
  399. }
  400. // cache updating is handled by the share source
  401. $this->watcher = new NullWatcher();
  402. return $this->watcher;
  403. }
  404. /**
  405. * unshare complete storage, also the grouped shares
  406. *
  407. * @return bool
  408. */
  409. public function unshareStorage(): bool {
  410. foreach ($this->groupedShares as $share) {
  411. \OC::$server->getShareManager()->deleteFromSelf($share, $this->user);
  412. }
  413. return true;
  414. }
  415. /**
  416. * @param string $path
  417. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  418. * @param \OCP\Lock\ILockingProvider $provider
  419. * @throws \OCP\Lock\LockedException
  420. */
  421. public function acquireLock($path, $type, ILockingProvider $provider) {
  422. /** @var \OCP\Files\Storage $targetStorage */
  423. [$targetStorage, $targetInternalPath] = $this->resolvePath($path);
  424. $targetStorage->acquireLock($targetInternalPath, $type, $provider);
  425. // lock the parent folders of the owner when locking the share as recipient
  426. if ($path === '') {
  427. $sourcePath = $this->ownerUserFolder->getRelativePath($this->sourcePath);
  428. $this->ownerView->lockFile(dirname($sourcePath), ILockingProvider::LOCK_SHARED, true);
  429. }
  430. }
  431. /**
  432. * @param string $path
  433. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  434. * @param \OCP\Lock\ILockingProvider $provider
  435. */
  436. public function releaseLock($path, $type, ILockingProvider $provider) {
  437. /** @var \OCP\Files\Storage $targetStorage */
  438. [$targetStorage, $targetInternalPath] = $this->resolvePath($path);
  439. $targetStorage->releaseLock($targetInternalPath, $type, $provider);
  440. // unlock the parent folders of the owner when unlocking the share as recipient
  441. if ($path === '') {
  442. $sourcePath = $this->ownerUserFolder->getRelativePath($this->sourcePath);
  443. $this->ownerView->unlockFile(dirname($sourcePath), ILockingProvider::LOCK_SHARED, true);
  444. }
  445. }
  446. /**
  447. * @param string $path
  448. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  449. * @param \OCP\Lock\ILockingProvider $provider
  450. */
  451. public function changeLock($path, $type, ILockingProvider $provider) {
  452. /** @var \OCP\Files\Storage $targetStorage */
  453. [$targetStorage, $targetInternalPath] = $this->resolvePath($path);
  454. $targetStorage->changeLock($targetInternalPath, $type, $provider);
  455. }
  456. /**
  457. * @return array [ available, last_checked ]
  458. */
  459. public function getAvailability() {
  460. // shares do not participate in availability logic
  461. return [
  462. 'available' => true,
  463. 'last_checked' => 0,
  464. ];
  465. }
  466. /**
  467. * @param bool $isAvailable
  468. */
  469. public function setAvailability($isAvailable) {
  470. // shares do not participate in availability logic
  471. }
  472. public function getSourceStorage() {
  473. $this->init();
  474. return $this->nonMaskedStorage;
  475. }
  476. public function getWrapperStorage() {
  477. $this->init();
  478. return $this->storage;
  479. }
  480. public function file_get_contents($path) {
  481. $info = [
  482. 'target' => $this->getMountPoint() . '/' . $path,
  483. 'source' => $this->getUnjailedPath($path),
  484. ];
  485. \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_get_contents', $info);
  486. return parent::file_get_contents($path);
  487. }
  488. public function file_put_contents($path, $data) {
  489. $info = [
  490. 'target' => $this->getMountPoint() . '/' . $path,
  491. 'source' => $this->getUnjailedPath($path),
  492. ];
  493. \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_put_contents', $info);
  494. return parent::file_put_contents($path, $data);
  495. }
  496. /**
  497. * @return void
  498. */
  499. public function setMountOptions(array $options) {
  500. /* Note: This value is never read */
  501. $this->mountOptions = $options;
  502. }
  503. public function getUnjailedPath($path) {
  504. $this->init();
  505. return parent::getUnjailedPath($path);
  506. }
  507. }