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 13KB

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