SharedStorage.php 13 KB

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