sharedstorage.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. <?php
  2. /**
  3. * @author Bart Visscher <bartv@thisnet.nl>
  4. * @author Björn Schießle <schiessle@owncloud.com>
  5. * @author Joas Schilling <nickvergessen@owncloud.com>
  6. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <icewind@owncloud.com>
  9. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  10. * @author scambra <sergio@entrecables.com>
  11. * @author Vincent Petry <pvince81@owncloud.com>
  12. *
  13. * @copyright Copyright (c) 2015, ownCloud, Inc.
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OC\Files\Storage;
  30. use OC\Files\Cache\ChangePropagator;
  31. use OC\Files\Filesystem;
  32. use OC\Files\View;
  33. use OCA\Files_Sharing\ISharedStorage;
  34. use OCA\Files_Sharing\Propagator;
  35. use OCA\Files_Sharing\SharedMount;
  36. /**
  37. * Convert target path to source path and pass the function call to the correct storage provider
  38. */
  39. class Shared extends \OC\Files\Storage\Common implements ISharedStorage {
  40. private $share; // the shared resource
  41. private $files = array();
  42. private static $isInitialized = array();
  43. public function __construct($arguments) {
  44. $this->share = $arguments['share'];
  45. }
  46. /**
  47. * get id of the mount point
  48. *
  49. * @return string
  50. */
  51. public function getId() {
  52. return 'shared::' . $this->getMountPoint();
  53. }
  54. /**
  55. * get file cache of the shared item source
  56. *
  57. * @return int
  58. */
  59. public function getSourceId() {
  60. return (int)$this->share['file_source'];
  61. }
  62. /**
  63. * Get the source file path, permissions, and owner for a shared file
  64. *
  65. * @param string $target Shared target file path
  66. * @return Returns array with the keys path, permissions, and owner or false if not found
  67. */
  68. public function getFile($target) {
  69. if (!isset($this->files[$target])) {
  70. // Check for partial files
  71. if (pathinfo($target, PATHINFO_EXTENSION) === 'part') {
  72. $source = \OC_Share_Backend_File::getSource(substr($target, 0, -5), $this->getMountPoint(), $this->getItemType());
  73. if ($source) {
  74. $source['path'] .= '.part';
  75. // All partial files have delete permission
  76. $source['permissions'] |= \OCP\Constants::PERMISSION_DELETE;
  77. }
  78. } else {
  79. $source = \OC_Share_Backend_File::getSource($target, $this->getMountPoint(), $this->getItemType());
  80. }
  81. $this->files[$target] = $source;
  82. }
  83. return $this->files[$target];
  84. }
  85. /**
  86. * Get the source file path for a shared file
  87. *
  88. * @param string $target Shared target file path
  89. * @return string|false source file path or false if not found
  90. */
  91. public function getSourcePath($target) {
  92. $source = $this->getFile($target);
  93. if ($source) {
  94. if (!isset($source['fullPath'])) {
  95. \OC\Files\Filesystem::initMountPoints($source['fileOwner']);
  96. $mount = \OC\Files\Filesystem::getMountByNumericId($source['storage']);
  97. if (is_array($mount) && !empty($mount)) {
  98. $this->files[$target]['fullPath'] = $mount[key($mount)]->getMountPoint() . $source['path'];
  99. } else {
  100. $this->files[$target]['fullPath'] = false;
  101. \OCP\Util::writeLog('files_sharing', "Unable to get mount for shared storage '" . $source['storage'] . "' user '" . $source['fileOwner'] . "'", \OCP\Util::ERROR);
  102. }
  103. }
  104. return $this->files[$target]['fullPath'];
  105. }
  106. return false;
  107. }
  108. /**
  109. * Get the permissions granted for a shared file
  110. *
  111. * @param string $target Shared target file path
  112. * @return int CRUDS permissions granted
  113. */
  114. public function getPermissions($target = '') {
  115. $permissions = $this->share['permissions'];
  116. // part files and the mount point always have delete permissions
  117. if ($target === '' || pathinfo($target, PATHINFO_EXTENSION) === 'part') {
  118. $permissions |= \OCP\Constants::PERMISSION_DELETE;
  119. }
  120. if (\OCP\Util::isSharingDisabledForUser()) {
  121. $permissions &= ~\OCP\Constants::PERMISSION_SHARE;
  122. }
  123. return $permissions;
  124. }
  125. public function mkdir($path) {
  126. if ($path == '' || $path == '/' || !$this->isCreatable(dirname($path))) {
  127. return false;
  128. } else if ($source = $this->getSourcePath($path)) {
  129. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  130. return $storage->mkdir($internalPath);
  131. }
  132. return false;
  133. }
  134. /**
  135. * Delete the directory if DELETE permission is granted
  136. *
  137. * @param string $path
  138. * @return boolean
  139. */
  140. public function rmdir($path) {
  141. // never delete a share mount point
  142. if (empty($path)) {
  143. return false;
  144. }
  145. if (($source = $this->getSourcePath($path)) && $this->isDeletable($path)) {
  146. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  147. return $storage->rmdir($internalPath);
  148. }
  149. return false;
  150. }
  151. public function opendir($path) {
  152. $source = $this->getSourcePath($path);
  153. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  154. return $storage->opendir($internalPath);
  155. }
  156. public function is_dir($path) {
  157. $source = $this->getSourcePath($path);
  158. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  159. return $storage->is_dir($internalPath);
  160. }
  161. public function is_file($path) {
  162. if ($source = $this->getSourcePath($path)) {
  163. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  164. return $storage->is_file($internalPath);
  165. }
  166. return false;
  167. }
  168. public function stat($path) {
  169. if ($path == '' || $path == '/') {
  170. $stat['size'] = $this->filesize($path);
  171. $stat['mtime'] = $this->filemtime($path);
  172. return $stat;
  173. } else if ($source = $this->getSourcePath($path)) {
  174. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  175. return $storage->stat($internalPath);
  176. }
  177. return false;
  178. }
  179. public function filetype($path) {
  180. if ($path == '' || $path == '/') {
  181. return 'dir';
  182. } else if ($source = $this->getSourcePath($path)) {
  183. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  184. return $storage->filetype($internalPath);
  185. }
  186. return false;
  187. }
  188. public function filesize($path) {
  189. $source = $this->getSourcePath($path);
  190. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  191. return $storage->filesize($internalPath);
  192. }
  193. public function isCreatable($path) {
  194. return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_CREATE);
  195. }
  196. public function isReadable($path) {
  197. return $this->file_exists($path);
  198. }
  199. public function isUpdatable($path) {
  200. return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_UPDATE);
  201. }
  202. public function isDeletable($path) {
  203. return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_DELETE);
  204. }
  205. public function isSharable($path) {
  206. if (\OCP\Util::isSharingDisabledForUser()) {
  207. return false;
  208. }
  209. return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_SHARE);
  210. }
  211. public function file_exists($path) {
  212. if ($path == '' || $path == '/') {
  213. return true;
  214. } else if ($source = $this->getSourcePath($path)) {
  215. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  216. return $storage->file_exists($internalPath);
  217. }
  218. return false;
  219. }
  220. public function filemtime($path) {
  221. $source = $this->getSourcePath($path);
  222. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  223. return $storage->filemtime($internalPath);
  224. }
  225. public function file_get_contents($path) {
  226. $source = $this->getSourcePath($path);
  227. if ($source) {
  228. $info = array(
  229. 'target' => $this->getMountPoint() . $path,
  230. 'source' => $source,
  231. );
  232. \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_get_contents', $info);
  233. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  234. return $storage->file_get_contents($internalPath);
  235. }
  236. }
  237. public function file_put_contents($path, $data) {
  238. if ($source = $this->getSourcePath($path)) {
  239. // Check if permission is granted
  240. if (($this->file_exists($path) && !$this->isUpdatable($path))
  241. || ($this->is_dir($path) && !$this->isCreatable($path))
  242. ) {
  243. return false;
  244. }
  245. $info = array(
  246. 'target' => $this->getMountPoint() . '/' . $path,
  247. 'source' => $source,
  248. );
  249. \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_put_contents', $info);
  250. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  251. $result = $storage->file_put_contents($internalPath, $data);
  252. return $result;
  253. }
  254. return false;
  255. }
  256. /**
  257. * Delete the file if DELETE permission is granted
  258. *
  259. * @param string $path
  260. * @return boolean
  261. */
  262. public function unlink($path) {
  263. // never delete a share mount point
  264. if (empty($path)) {
  265. return false;
  266. }
  267. if ($source = $this->getSourcePath($path)) {
  268. if ($this->isDeletable($path)) {
  269. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  270. return $storage->unlink($internalPath);
  271. }
  272. }
  273. return false;
  274. }
  275. public function rename($path1, $path2) {
  276. // we need the paths relative to data/user/files
  277. $relPath1 = $this->getMountPoint() . '/' . $path1;
  278. $relPath2 = $this->getMountPoint() . '/' . $path2;
  279. $pathinfo = pathinfo($relPath1);
  280. $isPartFile = (isset($pathinfo['extension']) && $pathinfo['extension'] === 'part');
  281. $targetExists = $this->file_exists($path2);
  282. $sameFolder = (dirname($relPath1) === dirname($relPath2));
  283. if ($targetExists || ($sameFolder && !$isPartFile)) {
  284. // note that renaming a share mount point is always allowed
  285. if (!$this->isUpdatable('')) {
  286. return false;
  287. }
  288. } else {
  289. if (!$this->isCreatable('')) {
  290. return false;
  291. }
  292. }
  293. // for part files we need to ask for the owner and path from the parent directory because
  294. // the file cache doesn't return any results for part files
  295. if ($isPartFile) {
  296. list($user1, $path1) = \OCA\Files_Sharing\Helper::getUidAndFilename($pathinfo['dirname']);
  297. $path1 = $path1 . '/' . $pathinfo['basename'];
  298. } else {
  299. list($user1, $path1) = \OCA\Files_Sharing\Helper::getUidAndFilename($relPath1);
  300. }
  301. $targetFilename = basename($relPath2);
  302. list($user2, $path2) = \OCA\Files_Sharing\Helper::getUidAndFilename(dirname($relPath2));
  303. $rootView = new \OC\Files\View('');
  304. $rootView->getUpdater()->disable(); // dont update the cache here
  305. $result = $rootView->rename('/' . $user1 . '/files/' . $path1, '/' . $user2 . '/files/' . $path2 . '/' . $targetFilename);
  306. $rootView->getUpdater()->enable();
  307. return $result;
  308. }
  309. public function copy($path1, $path2) {
  310. // Copy the file if CREATE permission is granted
  311. if ($this->isCreatable(dirname($path2))) {
  312. $oldSource = $this->getSourcePath($path1);
  313. $newSource = $this->getSourcePath(dirname($path2)) . '/' . basename($path2);
  314. $rootView = new \OC\Files\View('');
  315. return $rootView->copy($oldSource, $newSource);
  316. }
  317. return false;
  318. }
  319. public function fopen($path, $mode) {
  320. if ($source = $this->getSourcePath($path)) {
  321. switch ($mode) {
  322. case 'r+':
  323. case 'rb+':
  324. case 'w+':
  325. case 'wb+':
  326. case 'x+':
  327. case 'xb+':
  328. case 'a+':
  329. case 'ab+':
  330. case 'w':
  331. case 'wb':
  332. case 'x':
  333. case 'xb':
  334. case 'a':
  335. case 'ab':
  336. $creatable = $this->isCreatable($path);
  337. $updatable = $this->isUpdatable($path);
  338. // if neither permissions given, no need to continue
  339. if (!$creatable && !$updatable) {
  340. return false;
  341. }
  342. $exists = $this->file_exists($path);
  343. // if a file exists, updatable permissions are required
  344. if ($exists && !$updatable) {
  345. return false;
  346. }
  347. // part file is allowed if !$creatable but the final file is $updatable
  348. if (pathinfo($path, PATHINFO_EXTENSION) !== 'part') {
  349. if (!$exists && !$creatable) {
  350. return false;
  351. }
  352. }
  353. }
  354. $info = array(
  355. 'target' => $this->getMountPoint() . $path,
  356. 'source' => $source,
  357. 'mode' => $mode,
  358. );
  359. \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'fopen', $info);
  360. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  361. return $storage->fopen($internalPath, $mode);
  362. }
  363. return false;
  364. }
  365. public function getMimeType($path) {
  366. if ($source = $this->getSourcePath($path)) {
  367. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  368. return $storage->getMimeType($internalPath);
  369. }
  370. return false;
  371. }
  372. public function free_space($path) {
  373. $source = $this->getSourcePath($path);
  374. if ($source) {
  375. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  376. return $storage->free_space($internalPath);
  377. }
  378. return \OCP\Files\FileInfo::SPACE_UNKNOWN;
  379. }
  380. public function getLocalFile($path) {
  381. if ($source = $this->getSourcePath($path)) {
  382. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  383. return $storage->getLocalFile($internalPath);
  384. }
  385. return false;
  386. }
  387. public function touch($path, $mtime = null) {
  388. if ($source = $this->getSourcePath($path)) {
  389. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  390. return $storage->touch($internalPath, $mtime);
  391. }
  392. return false;
  393. }
  394. /**
  395. * return mount point of share, relative to data/user/files
  396. *
  397. * @return string
  398. */
  399. public function getMountPoint() {
  400. return $this->share['file_target'];
  401. }
  402. public function setMountPoint($path) {
  403. $this->share['file_target'] = $path;
  404. }
  405. public function getShareType() {
  406. return $this->share['share_type'];
  407. }
  408. /**
  409. * does the group share already has a user specific unique name
  410. *
  411. * @return bool
  412. */
  413. public function uniqueNameSet() {
  414. return (isset($this->share['unique_name']) && $this->share['unique_name']);
  415. }
  416. /**
  417. * the share now uses a unique name of this user
  418. *
  419. * @brief the share now uses a unique name of this user
  420. */
  421. public function setUniqueName() {
  422. $this->share['unique_name'] = true;
  423. }
  424. /**
  425. * get share ID
  426. *
  427. * @return integer unique share ID
  428. */
  429. public function getShareId() {
  430. return $this->share['id'];
  431. }
  432. /**
  433. * get the user who shared the file
  434. *
  435. * @return string
  436. */
  437. public function getSharedFrom() {
  438. return $this->share['uid_owner'];
  439. }
  440. /**
  441. * @return array
  442. */
  443. public function getShare() {
  444. return $this->share;
  445. }
  446. /**
  447. * return share type, can be "file" or "folder"
  448. *
  449. * @return string
  450. */
  451. public function getItemType() {
  452. return $this->share['item_type'];
  453. }
  454. public function hasUpdated($path, $time) {
  455. return $this->filemtime($path) > $time;
  456. }
  457. public function getCache($path = '', $storage = null) {
  458. if (!$storage) {
  459. $storage = $this;
  460. }
  461. return new \OC\Files\Cache\Shared_Cache($storage);
  462. }
  463. public function getScanner($path = '', $storage = null) {
  464. if (!$storage) {
  465. $storage = $this;
  466. }
  467. return new \OC\Files\Cache\SharedScanner($storage);
  468. }
  469. public function getWatcher($path = '', $storage = null) {
  470. if (!$storage) {
  471. $storage = $this;
  472. }
  473. return new \OC\Files\Cache\Shared_Watcher($storage);
  474. }
  475. public function getOwner($path) {
  476. if ($path == '') {
  477. $path = $this->getMountPoint();
  478. }
  479. $source = $this->getFile($path);
  480. if ($source) {
  481. return $source['fileOwner'];
  482. }
  483. return false;
  484. }
  485. public function getETag($path) {
  486. if ($path == '') {
  487. $path = $this->getMountPoint();
  488. }
  489. if ($source = $this->getSourcePath($path)) {
  490. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  491. return $storage->getETag($internalPath);
  492. }
  493. return null;
  494. }
  495. /**
  496. * unshare complete storage, also the grouped shares
  497. *
  498. * @return bool
  499. */
  500. public function unshareStorage() {
  501. $result = true;
  502. if (!empty($this->share['grouped'])) {
  503. foreach ($this->share['grouped'] as $share) {
  504. $result = $result && \OCP\Share::unshareFromSelf($share['item_type'], $share['file_target']);
  505. }
  506. }
  507. $result = $result && \OCP\Share::unshareFromSelf($this->getItemType(), $this->getMountPoint());
  508. return $result;
  509. }
  510. /**
  511. * Resolve the path for the source of the share
  512. *
  513. * @param string $path
  514. * @return array
  515. */
  516. private function resolvePath($path) {
  517. $source = $this->getSourcePath($path);
  518. return \OC\Files\Filesystem::resolvePath($source);
  519. }
  520. /**
  521. * @param \OCP\Files\Storage $sourceStorage
  522. * @param string $sourceInternalPath
  523. * @param string $targetInternalPath
  524. * @return bool
  525. */
  526. public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  527. /** @var \OCP\Files\Storage $targetStorage */
  528. list($targetStorage, $targetInternalPath) = $this->resolvePath($targetInternalPath);
  529. return $targetStorage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  530. }
  531. /**
  532. * @param \OCP\Files\Storage $sourceStorage
  533. * @param string $sourceInternalPath
  534. * @param string $targetInternalPath
  535. * @return bool
  536. */
  537. public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  538. /** @var \OCP\Files\Storage $targetStorage */
  539. list($targetStorage, $targetInternalPath) = $this->resolvePath($targetInternalPath);
  540. return $targetStorage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  541. }
  542. }