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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 Christoph Wurst <christoph@winzerhof-wurst.at>
  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 <pvince81@owncloud.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\Filesystem;
  36. use OC\Files\Storage\FailedStorage;
  37. use OC\Files\Storage\Wrapper\PermissionsMask;
  38. use OC\User\NoUserException;
  39. use OCP\Constants;
  40. use OCP\Files\Cache\ICacheEntry;
  41. use OCP\Files\NotFoundException;
  42. use OCP\Files\Storage\IDisableEncryptionStorage;
  43. use OCP\Files\Storage\IStorage;
  44. use OCP\Lock\ILockingProvider;
  45. /**
  46. * Convert target path to source path and pass the function call to the correct storage provider
  47. */
  48. class SharedStorage extends \OC\Files\Storage\Wrapper\Jail implements ISharedStorage, IDisableEncryptionStorage {
  49. /** @var \OCP\Share\IShare */
  50. private $superShare;
  51. /** @var \OCP\Share\IShare[] */
  52. private $groupedShares;
  53. /**
  54. * @var \OC\Files\View
  55. */
  56. private $ownerView;
  57. private $initialized = false;
  58. /**
  59. * @var ICacheEntry
  60. */
  61. private $sourceRootInfo;
  62. /** @var string */
  63. private $user;
  64. /**
  65. * @var \OCP\ILogger
  66. */
  67. private $logger;
  68. /** @var IStorage */
  69. private $nonMaskedStorage;
  70. private $options;
  71. /** @var boolean */
  72. private $sharingDisabledForUser;
  73. public function __construct($arguments) {
  74. $this->ownerView = $arguments['ownerView'];
  75. $this->logger = \OC::$server->getLogger();
  76. $this->superShare = $arguments['superShare'];
  77. $this->groupedShares = $arguments['groupedShares'];
  78. $this->user = $arguments['user'];
  79. if (isset($arguments['sharingDisabledForUser'])) {
  80. $this->sharingDisabledForUser = $arguments['sharingDisabledForUser'];
  81. } else {
  82. $this->sharingDisabledForUser = false;
  83. }
  84. parent::__construct([
  85. 'storage' => null,
  86. 'root' => null,
  87. ]);
  88. }
  89. /**
  90. * @return ICacheEntry
  91. */
  92. private function getSourceRootInfo() {
  93. if (is_null($this->sourceRootInfo)) {
  94. if (is_null($this->superShare->getNodeCacheEntry())) {
  95. $this->init();
  96. $this->sourceRootInfo = $this->nonMaskedStorage->getCache()->get($this->rootPath);
  97. } else {
  98. $this->sourceRootInfo = $this->superShare->getNodeCacheEntry();
  99. }
  100. }
  101. return $this->sourceRootInfo;
  102. }
  103. private function init() {
  104. if ($this->initialized) {
  105. return;
  106. }
  107. $this->initialized = true;
  108. try {
  109. Filesystem::initMountPoints($this->superShare->getShareOwner());
  110. $sourcePath = $this->ownerView->getPath($this->superShare->getNodeId());
  111. list($this->nonMaskedStorage, $this->rootPath) = $this->ownerView->resolvePath($sourcePath);
  112. $this->storage = new PermissionsMask([
  113. 'storage' => $this->nonMaskedStorage,
  114. 'mask' => $this->superShare->getPermissions()
  115. ]);
  116. } catch (NotFoundException $e) {
  117. // original file not accessible or deleted, set FailedStorage
  118. $this->storage = new FailedStorage(['exception' => $e]);
  119. $this->cache = new FailedCache();
  120. $this->rootPath = '';
  121. } catch (NoUserException $e) {
  122. // sharer user deleted, set FailedStorage
  123. $this->storage = new FailedStorage(['exception' => $e]);
  124. $this->cache = new FailedCache();
  125. $this->rootPath = '';
  126. } catch (\Exception $e) {
  127. $this->storage = new FailedStorage(['exception' => $e]);
  128. $this->cache = new FailedCache();
  129. $this->rootPath = '';
  130. $this->logger->logException($e);
  131. }
  132. if (!$this->nonMaskedStorage) {
  133. $this->nonMaskedStorage = $this->storage;
  134. }
  135. }
  136. /**
  137. * @inheritdoc
  138. */
  139. public function instanceOfStorage($class) {
  140. if ($class === '\OC\Files\Storage\Common') {
  141. return true;
  142. }
  143. if (in_array($class, ['\OC\Files\Storage\Home', '\OC\Files\ObjectStore\HomeObjectStoreStorage'])) {
  144. return false;
  145. }
  146. return parent::instanceOfStorage($class);
  147. }
  148. /**
  149. * @return string
  150. */
  151. public function getShareId() {
  152. return $this->superShare->getId();
  153. }
  154. private function isValid() {
  155. return $this->getSourceRootInfo() && ($this->getSourceRootInfo()->getPermissions() & Constants::PERMISSION_SHARE) === Constants::PERMISSION_SHARE;
  156. }
  157. /**
  158. * get id of the mount point
  159. *
  160. * @return string
  161. */
  162. public function getId() {
  163. return 'shared::' . $this->getMountPoint();
  164. }
  165. /**
  166. * Get the permissions granted for a shared file
  167. *
  168. * @param string $target Shared target file path
  169. * @return int CRUDS permissions granted
  170. */
  171. public function getPermissions($target = '') {
  172. if (!$this->isValid()) {
  173. return 0;
  174. }
  175. $permissions = parent::getPermissions($target) & $this->superShare->getPermissions();
  176. // part files and the mount point always have delete permissions
  177. if ($target === '' || pathinfo($target, PATHINFO_EXTENSION) === 'part') {
  178. $permissions |= \OCP\Constants::PERMISSION_DELETE;
  179. }
  180. if ($this->sharingDisabledForUser) {
  181. $permissions &= ~\OCP\Constants::PERMISSION_SHARE;
  182. }
  183. return $permissions;
  184. }
  185. public function isCreatable($path) {
  186. return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_CREATE);
  187. }
  188. public function isReadable($path) {
  189. if (!$this->isValid()) {
  190. return false;
  191. }
  192. if (!$this->file_exists($path)) {
  193. return false;
  194. }
  195. /** @var IStorage $storage */
  196. /** @var string $internalPath */
  197. list($storage, $internalPath) = $this->resolvePath($path);
  198. return $storage->isReadable($internalPath);
  199. }
  200. public function isUpdatable($path) {
  201. return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_UPDATE);
  202. }
  203. public function isDeletable($path) {
  204. return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_DELETE);
  205. }
  206. public function isSharable($path) {
  207. if (\OCP\Util::isSharingDisabledForUser() || !\OC\Share\Share::isResharingAllowed()) {
  208. return false;
  209. }
  210. return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_SHARE);
  211. }
  212. public function fopen($path, $mode) {
  213. if ($source = $this->getUnjailedPath($path)) {
  214. switch ($mode) {
  215. case 'r+':
  216. case 'rb+':
  217. case 'w+':
  218. case 'wb+':
  219. case 'x+':
  220. case 'xb+':
  221. case 'a+':
  222. case 'ab+':
  223. case 'w':
  224. case 'wb':
  225. case 'x':
  226. case 'xb':
  227. case 'a':
  228. case 'ab':
  229. $creatable = $this->isCreatable(dirname($path));
  230. $updatable = $this->isUpdatable($path);
  231. // if neither permissions given, no need to continue
  232. if (!$creatable && !$updatable) {
  233. if (pathinfo($path, PATHINFO_EXTENSION) === 'part') {
  234. $updatable = $this->isUpdatable(dirname($path));
  235. }
  236. if (!$updatable) {
  237. return false;
  238. }
  239. }
  240. $exists = $this->file_exists($path);
  241. // if a file exists, updatable permissions are required
  242. if ($exists && !$updatable) {
  243. return false;
  244. }
  245. // part file is allowed if !$creatable but the final file is $updatable
  246. if (pathinfo($path, PATHINFO_EXTENSION) !== 'part') {
  247. if (!$exists && !$creatable) {
  248. return false;
  249. }
  250. }
  251. }
  252. $info = [
  253. 'target' => $this->getMountPoint() . $path,
  254. 'source' => $source,
  255. 'mode' => $mode,
  256. ];
  257. \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'fopen', $info);
  258. return $this->nonMaskedStorage->fopen($this->getUnjailedPath($path), $mode);
  259. }
  260. return false;
  261. }
  262. /**
  263. * see http://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. $sameFodler = dirname($path1) === dirname($path2);
  274. if ($targetExists || ($sameFodler && !$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. /**
  353. * unshare complete storage, also the grouped shares
  354. *
  355. * @return bool
  356. */
  357. public function unshareStorage() {
  358. foreach ($this->groupedShares as $share) {
  359. \OC::$server->getShareManager()->deleteFromSelf($share, $this->user);
  360. }
  361. return true;
  362. }
  363. /**
  364. * @param string $path
  365. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  366. * @param \OCP\Lock\ILockingProvider $provider
  367. * @throws \OCP\Lock\LockedException
  368. */
  369. public function acquireLock($path, $type, ILockingProvider $provider) {
  370. /** @var \OCP\Files\Storage $targetStorage */
  371. list($targetStorage, $targetInternalPath) = $this->resolvePath($path);
  372. $targetStorage->acquireLock($targetInternalPath, $type, $provider);
  373. // lock the parent folders of the owner when locking the share as recipient
  374. if ($path === '') {
  375. $sourcePath = $this->ownerView->getPath($this->superShare->getNodeId());
  376. $this->ownerView->lockFile(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 releaseLock($path, $type, ILockingProvider $provider) {
  385. /** @var \OCP\Files\Storage $targetStorage */
  386. list($targetStorage, $targetInternalPath) = $this->resolvePath($path);
  387. $targetStorage->releaseLock($targetInternalPath, $type, $provider);
  388. // unlock the parent folders of the owner when unlocking the share as recipient
  389. if ($path === '') {
  390. $sourcePath = $this->ownerView->getPath($this->superShare->getNodeId());
  391. $this->ownerView->unlockFile(dirname($sourcePath), ILockingProvider::LOCK_SHARED, true);
  392. }
  393. }
  394. /**
  395. * @param string $path
  396. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  397. * @param \OCP\Lock\ILockingProvider $provider
  398. */
  399. public function changeLock($path, $type, ILockingProvider $provider) {
  400. /** @var \OCP\Files\Storage $targetStorage */
  401. list($targetStorage, $targetInternalPath) = $this->resolvePath($path);
  402. $targetStorage->changeLock($targetInternalPath, $type, $provider);
  403. }
  404. /**
  405. * @return array [ available, last_checked ]
  406. */
  407. public function getAvailability() {
  408. // shares do not participate in availability logic
  409. return [
  410. 'available' => true,
  411. 'last_checked' => 0
  412. ];
  413. }
  414. /**
  415. * @param bool $available
  416. */
  417. public function setAvailability($available) {
  418. // shares do not participate in availability logic
  419. }
  420. public function getSourceStorage() {
  421. $this->init();
  422. return $this->nonMaskedStorage;
  423. }
  424. public function getWrapperStorage() {
  425. $this->init();
  426. return $this->storage;
  427. }
  428. public function file_get_contents($path) {
  429. $info = [
  430. 'target' => $this->getMountPoint() . '/' . $path,
  431. 'source' => $this->getUnjailedPath($path),
  432. ];
  433. \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_get_contents', $info);
  434. return parent::file_get_contents($path);
  435. }
  436. public function file_put_contents($path, $data) {
  437. $info = [
  438. 'target' => $this->getMountPoint() . '/' . $path,
  439. 'source' => $this->getUnjailedPath($path),
  440. ];
  441. \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_put_contents', $info);
  442. return parent::file_put_contents($path, $data);
  443. }
  444. public function setMountOptions(array $options) {
  445. $this->mountOptions = $options;
  446. }
  447. public function getUnjailedPath($path) {
  448. $this->init();
  449. return parent::getUnjailedPath($path);
  450. }
  451. }