You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SharedStorage.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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\FailedCache;
  35. use OC\Files\Cache\NullWatcher;
  36. use OC\Files\Filesystem;
  37. use OC\Files\Storage\FailedStorage;
  38. use OC\Files\Storage\Wrapper\PermissionsMask;
  39. use OC\User\NoUserException;
  40. use OCP\Constants;
  41. use OCP\Files\Cache\ICacheEntry;
  42. use OCP\Files\NotFoundException;
  43. use OCP\Files\Storage\IDisableEncryptionStorage;
  44. use OCP\Files\Storage\IStorage;
  45. use OCP\Lock\ILockingProvider;
  46. /**
  47. * Convert target path to source path and pass the function call to the correct storage provider
  48. */
  49. class SharedStorage extends \OC\Files\Storage\Wrapper\Jail implements ISharedStorage, IDisableEncryptionStorage {
  50. /** @var \OCP\Share\IShare */
  51. private $superShare;
  52. /** @var \OCP\Share\IShare[] */
  53. private $groupedShares;
  54. /**
  55. * @var \OC\Files\View
  56. */
  57. private $ownerView;
  58. private $initialized = false;
  59. /**
  60. * @var ICacheEntry
  61. */
  62. private $sourceRootInfo;
  63. /** @var string */
  64. private $user;
  65. /**
  66. * @var \OCP\ILogger
  67. */
  68. private $logger;
  69. /** @var IStorage */
  70. private $nonMaskedStorage;
  71. private $options;
  72. /** @var boolean */
  73. private $sharingDisabledForUser;
  74. public function __construct($arguments) {
  75. $this->ownerView = $arguments['ownerView'];
  76. $this->logger = \OC::$server->getLogger();
  77. $this->superShare = $arguments['superShare'];
  78. $this->groupedShares = $arguments['groupedShares'];
  79. $this->user = $arguments['user'];
  80. if (isset($arguments['sharingDisabledForUser'])) {
  81. $this->sharingDisabledForUser = $arguments['sharingDisabledForUser'];
  82. } else {
  83. $this->sharingDisabledForUser = false;
  84. }
  85. parent::__construct([
  86. 'storage' => null,
  87. 'root' => null,
  88. ]);
  89. }
  90. /**
  91. * @return ICacheEntry
  92. */
  93. private function getSourceRootInfo() {
  94. if (is_null($this->sourceRootInfo)) {
  95. if (is_null($this->superShare->getNodeCacheEntry())) {
  96. $this->init();
  97. $this->sourceRootInfo = $this->nonMaskedStorage->getCache()->get($this->rootPath);
  98. } else {
  99. $this->sourceRootInfo = $this->superShare->getNodeCacheEntry();
  100. }
  101. }
  102. return $this->sourceRootInfo;
  103. }
  104. private function init() {
  105. if ($this->initialized) {
  106. return;
  107. }
  108. $this->initialized = true;
  109. try {
  110. Filesystem::initMountPoints($this->superShare->getShareOwner());
  111. $storageId = $this->superShare->getNodeCacheEntry() ? $this->superShare->getNodeCacheEntry()->getStorageId() : null;
  112. $sourcePath = $this->ownerView->getPath($this->superShare->getNodeId(), $storageId);
  113. [$this->nonMaskedStorage, $this->rootPath] = $this->ownerView->resolvePath($sourcePath);
  114. $this->storage = new PermissionsMask([
  115. 'storage' => $this->nonMaskedStorage,
  116. 'mask' => $this->superShare->getPermissions(),
  117. ]);
  118. } catch (NotFoundException $e) {
  119. // original file not accessible or deleted, set FailedStorage
  120. $this->storage = new FailedStorage(['exception' => $e]);
  121. $this->cache = new FailedCache();
  122. $this->rootPath = '';
  123. } catch (NoUserException $e) {
  124. // sharer user deleted, set FailedStorage
  125. $this->storage = new FailedStorage(['exception' => $e]);
  126. $this->cache = new FailedCache();
  127. $this->rootPath = '';
  128. } catch (\Exception $e) {
  129. $this->storage = new FailedStorage(['exception' => $e]);
  130. $this->cache = new FailedCache();
  131. $this->rootPath = '';
  132. $this->logger->logException($e);
  133. }
  134. if (!$this->nonMaskedStorage) {
  135. $this->nonMaskedStorage = $this->storage;
  136. }
  137. }
  138. /**
  139. * @inheritdoc
  140. */
  141. public function instanceOfStorage($class) {
  142. if ($class === '\OC\Files\Storage\Common') {
  143. return true;
  144. }
  145. if (in_array($class, ['\OC\Files\Storage\Home', '\OC\Files\ObjectStore\HomeObjectStoreStorage', '\OCP\Files\IHomeStorage'])) {
  146. return false;
  147. }
  148. return parent::instanceOfStorage($class);
  149. }
  150. /**
  151. * @return string
  152. */
  153. public function getShareId() {
  154. return $this->superShare->getId();
  155. }
  156. private function isValid() {
  157. return $this->getSourceRootInfo() && ($this->getSourceRootInfo()->getPermissions() & Constants::PERMISSION_SHARE) === Constants::PERMISSION_SHARE;
  158. }
  159. /**
  160. * get id of the mount point
  161. *
  162. * @return string
  163. */
  164. public function getId() {
  165. return 'shared::' . $this->getMountPoint();
  166. }
  167. /**
  168. * Get the permissions granted for a shared file
  169. *
  170. * @param string $target Shared target file path
  171. * @return int CRUDS permissions granted
  172. */
  173. public function getPermissions($target = '') {
  174. if (!$this->isValid()) {
  175. return 0;
  176. }
  177. $permissions = parent::getPermissions($target) & $this->superShare->getPermissions();
  178. // part files and the mount point always have delete permissions
  179. if ($target === '' || pathinfo($target, PATHINFO_EXTENSION) === 'part') {
  180. $permissions |= \OCP\Constants::PERMISSION_DELETE;
  181. }
  182. if ($this->sharingDisabledForUser) {
  183. $permissions &= ~\OCP\Constants::PERMISSION_SHARE;
  184. }
  185. return $permissions;
  186. }
  187. public function isCreatable($path) {
  188. return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_CREATE);
  189. }
  190. public function isReadable($path) {
  191. if (!$this->isValid()) {
  192. return false;
  193. }
  194. if (!$this->file_exists($path)) {
  195. return false;
  196. }
  197. /** @var IStorage $storage */
  198. /** @var string $internalPath */
  199. [$storage, $internalPath] = $this->resolvePath($path);
  200. return $storage->isReadable($internalPath);
  201. }
  202. public function isUpdatable($path) {
  203. return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_UPDATE);
  204. }
  205. public function isDeletable($path) {
  206. return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_DELETE);
  207. }
  208. public function isSharable($path) {
  209. if (\OCP\Util::isSharingDisabledForUser() || !\OC\Share\Share::isResharingAllowed()) {
  210. return false;
  211. }
  212. return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_SHARE);
  213. }
  214. public function fopen($path, $mode) {
  215. $source = $this->getUnjailedPath($path);
  216. switch ($mode) {
  217. case 'r+':
  218. case 'rb+':
  219. case 'w+':
  220. case 'wb+':
  221. case 'x+':
  222. case 'xb+':
  223. case 'a+':
  224. case 'ab+':
  225. case 'w':
  226. case 'wb':
  227. case 'x':
  228. case 'xb':
  229. case 'a':
  230. case 'ab':
  231. $creatable = $this->isCreatable(dirname($path));
  232. $updatable = $this->isUpdatable($path);
  233. // if neither permissions given, no need to continue
  234. if (!$creatable && !$updatable) {
  235. if (pathinfo($path, PATHINFO_EXTENSION) === 'part') {
  236. $updatable = $this->isUpdatable(dirname($path));
  237. }
  238. if (!$updatable) {
  239. return false;
  240. }
  241. }
  242. $exists = $this->file_exists($path);
  243. // if a file exists, updatable permissions are required
  244. if ($exists && !$updatable) {
  245. return false;
  246. }
  247. // part file is allowed if !$creatable but the final file is $updatable
  248. if (pathinfo($path, PATHINFO_EXTENSION) !== 'part') {
  249. if (!$exists && !$creatable) {
  250. return false;
  251. }
  252. }
  253. }
  254. $info = [
  255. 'target' => $this->getMountPoint() . '/' . $path,
  256. 'source' => $source,
  257. 'mode' => $mode,
  258. ];
  259. \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'fopen', $info);
  260. return $this->nonMaskedStorage->fopen($this->getUnjailedPath($path), $mode);
  261. }
  262. /**
  263. * see https://www.php.net/manual/en/function.rename.php
  264. *
  265. * @param string $path1
  266. * @param string $path2
  267. * @return bool
  268. */
  269. public function rename($path1, $path2) {
  270. $this->init();
  271. $isPartFile = pathinfo($path1, PATHINFO_EXTENSION) === 'part';
  272. $targetExists = $this->file_exists($path2);
  273. $sameFolder = dirname($path1) === dirname($path2);
  274. if ($targetExists || ($sameFolder && !$isPartFile)) {
  275. if (!$this->isUpdatable('')) {
  276. return false;
  277. }
  278. } else {
  279. if (!$this->isCreatable('')) {
  280. return false;
  281. }
  282. }
  283. return $this->nonMaskedStorage->rename($this->getUnjailedPath($path1), $this->getUnjailedPath($path2));
  284. }
  285. /**
  286. * return mount point of share, relative to data/user/files
  287. *
  288. * @return string
  289. */
  290. public function getMountPoint() {
  291. return $this->superShare->getTarget();
  292. }
  293. /**
  294. * @param string $path
  295. */
  296. public function setMountPoint($path) {
  297. $this->superShare->setTarget($path);
  298. foreach ($this->groupedShares as $share) {
  299. $share->setTarget($path);
  300. }
  301. }
  302. /**
  303. * get the user who shared the file
  304. *
  305. * @return string
  306. */
  307. public function getSharedFrom() {
  308. return $this->superShare->getShareOwner();
  309. }
  310. /**
  311. * @return \OCP\Share\IShare
  312. */
  313. public function getShare() {
  314. return $this->superShare;
  315. }
  316. /**
  317. * return share type, can be "file" or "folder"
  318. *
  319. * @return string
  320. */
  321. public function getItemType() {
  322. return $this->superShare->getNodeType();
  323. }
  324. /**
  325. * @param string $path
  326. * @param null $storage
  327. * @return Cache
  328. */
  329. public function getCache($path = '', $storage = null) {
  330. if ($this->cache) {
  331. return $this->cache;
  332. }
  333. if (!$storage) {
  334. $storage = $this;
  335. }
  336. $sourceRoot = $this->getSourceRootInfo();
  337. if ($this->storage instanceof FailedStorage) {
  338. return new FailedCache();
  339. }
  340. $this->cache = new \OCA\Files_Sharing\Cache($storage, $sourceRoot, $this->superShare);
  341. return $this->cache;
  342. }
  343. public function getScanner($path = '', $storage = null) {
  344. if (!$storage) {
  345. $storage = $this;
  346. }
  347. return new \OCA\Files_Sharing\Scanner($storage);
  348. }
  349. public function getOwner($path) {
  350. return $this->superShare->getShareOwner();
  351. }
  352. public function getWatcher($path = '', $storage = null) {
  353. // cache updating is handled by the share source
  354. return new NullWatcher();
  355. }
  356. /**
  357. * unshare complete storage, also the grouped shares
  358. *
  359. * @return bool
  360. */
  361. public function unshareStorage() {
  362. foreach ($this->groupedShares as $share) {
  363. \OC::$server->getShareManager()->deleteFromSelf($share, $this->user);
  364. }
  365. return true;
  366. }
  367. /**
  368. * @param string $path
  369. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  370. * @param \OCP\Lock\ILockingProvider $provider
  371. * @throws \OCP\Lock\LockedException
  372. */
  373. public function acquireLock($path, $type, ILockingProvider $provider) {
  374. /** @var \OCP\Files\Storage $targetStorage */
  375. [$targetStorage, $targetInternalPath] = $this->resolvePath($path);
  376. $targetStorage->acquireLock($targetInternalPath, $type, $provider);
  377. // lock the parent folders of the owner when locking the share as recipient
  378. if ($path === '') {
  379. $sourcePath = $this->ownerView->getPath($this->superShare->getNodeId());
  380. $this->ownerView->lockFile(dirname($sourcePath), ILockingProvider::LOCK_SHARED, true);
  381. }
  382. }
  383. /**
  384. * @param string $path
  385. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  386. * @param \OCP\Lock\ILockingProvider $provider
  387. */
  388. public function releaseLock($path, $type, ILockingProvider $provider) {
  389. /** @var \OCP\Files\Storage $targetStorage */
  390. [$targetStorage, $targetInternalPath] = $this->resolvePath($path);
  391. $targetStorage->releaseLock($targetInternalPath, $type, $provider);
  392. // unlock the parent folders of the owner when unlocking the share as recipient
  393. if ($path === '') {
  394. $sourcePath = $this->ownerView->getPath($this->superShare->getNodeId());
  395. $this->ownerView->unlockFile(dirname($sourcePath), ILockingProvider::LOCK_SHARED, true);
  396. }
  397. }
  398. /**
  399. * @param string $path
  400. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  401. * @param \OCP\Lock\ILockingProvider $provider
  402. */
  403. public function changeLock($path, $type, ILockingProvider $provider) {
  404. /** @var \OCP\Files\Storage $targetStorage */
  405. [$targetStorage, $targetInternalPath] = $this->resolvePath($path);
  406. $targetStorage->changeLock($targetInternalPath, $type, $provider);
  407. }
  408. /**
  409. * @return array [ available, last_checked ]
  410. */
  411. public function getAvailability() {
  412. // shares do not participate in availability logic
  413. return [
  414. 'available' => true,
  415. 'last_checked' => 0,
  416. ];
  417. }
  418. /**
  419. * @param bool $available
  420. */
  421. public function setAvailability($available) {
  422. // shares do not participate in availability logic
  423. }
  424. public function getSourceStorage() {
  425. $this->init();
  426. return $this->nonMaskedStorage;
  427. }
  428. public function getWrapperStorage() {
  429. $this->init();
  430. return $this->storage;
  431. }
  432. public function file_get_contents($path) {
  433. $info = [
  434. 'target' => $this->getMountPoint() . '/' . $path,
  435. 'source' => $this->getUnjailedPath($path),
  436. ];
  437. \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_get_contents', $info);
  438. return parent::file_get_contents($path);
  439. }
  440. public function file_put_contents($path, $data) {
  441. $info = [
  442. 'target' => $this->getMountPoint() . '/' . $path,
  443. 'source' => $this->getUnjailedPath($path),
  444. ];
  445. \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_put_contents', $info);
  446. return parent::file_put_contents($path, $data);
  447. }
  448. public function setMountOptions(array $options) {
  449. $this->mountOptions = $options;
  450. }
  451. public function getUnjailedPath($path) {
  452. $this->init();
  453. return parent::getUnjailedPath($path);
  454. }
  455. }