sharedstorage.php 15 KB

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