sharedmount.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OCA\Files_Sharing;
  9. use OC\Files\Mount\Mount;
  10. use OC\Files\Mount\MoveableMount;
  11. /**
  12. * Shared mount points can be moved by the user
  13. */
  14. class SharedMount extends Mount implements MoveableMount {
  15. /**
  16. * @var \OC\Files\Storage\Shared $storage
  17. */
  18. protected $storage = null;
  19. public function __construct($storage, $mountpoint, $arguments = null, $loader = null) {
  20. // first update the mount point before creating the parent
  21. $newMountPoint = self::verifyMountPoint($arguments['share']);
  22. $absMountPoint = '/' . \OCP\User::getUser() . '/files' . $newMountPoint;
  23. parent::__construct($storage, $absMountPoint, $arguments, $loader);
  24. }
  25. /**
  26. * check if the parent folder exists otherwise move the mount point up
  27. */
  28. private static function verifyMountPoint(&$share) {
  29. $mountPoint = basename($share['file_target']);
  30. $parent = dirname($share['file_target']);
  31. while (!\OC\Files\Filesystem::is_dir($parent)) {
  32. $parent = dirname($parent);
  33. }
  34. $newMountPoint = \OCA\Files_Sharing\Helper::generateUniqueTarget(
  35. \OC\Files\Filesystem::normalizePath($parent . '/' . $mountPoint),
  36. array(),
  37. new \OC\Files\View('/' . \OCP\User::getUser() . '/files')
  38. );
  39. if($newMountPoint !== $share['file_target']) {
  40. self::updateFileTarget($newMountPoint, $share);
  41. $share['file_target'] = $newMountPoint;
  42. $share['unique_name'] = true;
  43. }
  44. return $newMountPoint;
  45. }
  46. /**
  47. * update fileTarget in the database if the mount point changed
  48. * @param string $newPath
  49. * @param array $share reference to the share which should be modified
  50. * @return type
  51. */
  52. private static function updateFileTarget($newPath, &$share) {
  53. // if the user renames a mount point from a group share we need to create a new db entry
  54. // for the unique name
  55. if ($share['share_type'] === \OCP\Share::SHARE_TYPE_GROUP && empty($share['unique_name'])) {
  56. $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (`item_type`, `item_source`, `item_target`,'
  57. .' `share_type`, `share_with`, `uid_owner`, `permissions`, `stime`, `file_source`,'
  58. .' `file_target`, `token`, `parent`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)');
  59. $arguments = array($share['item_type'], $share['item_source'], $share['item_target'],
  60. 2, \OCP\User::getUser(), $share['uid_owner'], $share['permissions'], $share['stime'], $share['file_source'],
  61. $newPath, $share['token'], $share['id']);
  62. } else {
  63. // rename mount point
  64. $query = \OC_DB::prepare(
  65. 'Update `*PREFIX*share`
  66. SET `file_target` = ?
  67. WHERE `id` = ?'
  68. );
  69. $arguments = array($newPath, $share['id']);
  70. }
  71. $result = $query->execute($arguments);
  72. return $result === 1 ? true : false;
  73. }
  74. /**
  75. * Format a path to be relative to the /user/files/ directory
  76. *
  77. * @param string $path the absolute path
  78. * @return string e.g. turns '/admin/files/test.txt' into '/test.txt'
  79. */
  80. private function stripUserFilesPath($path) {
  81. $trimmed = ltrim($path, '/');
  82. $split = explode('/', $trimmed);
  83. // it is not a file relative to data/user/files
  84. if (count($split) < 3 || $split[1] !== 'files') {
  85. \OCP\Util::writeLog('file sharing',
  86. 'Can not strip userid and "files/" from path: ' . $path,
  87. \OCP\Util::DEBUG);
  88. return false;
  89. }
  90. // skip 'user' and 'files'
  91. $sliced = array_slice($split, 2);
  92. $relPath = implode('/', $sliced);
  93. return '/' . $relPath;
  94. }
  95. /**
  96. * Move the mount point to $target
  97. *
  98. * @param string $target the target mount point
  99. * @return bool
  100. */
  101. public function moveMount($target) {
  102. $relTargetPath = $this->stripUserFilesPath($target);
  103. $share = $this->storage->getShare();
  104. $result = $this->updateFileTarget($relTargetPath, $share);
  105. if ($result) {
  106. $this->setMountPoint($target);
  107. $this->storage->setUniqueName();
  108. $this->storage->setMountPoint($relTargetPath);
  109. } else {
  110. \OCP\Util::writeLog('file sharing',
  111. 'Could not rename mount point for shared folder "' . $this->getMountPoint() . '" to "' . $target . '"',
  112. \OCP\Util::ERROR);
  113. }
  114. return $result;
  115. }
  116. /**
  117. * Remove the mount points
  118. *
  119. * @return bool
  120. */
  121. public function removeMount() {
  122. $mountManager = \OC\Files\Filesystem::getMountManager();
  123. $storage = $this->getStorage();
  124. $result = \OCP\Share::unshareFromSelf($storage->getItemType(), $storage->getMountPoint());
  125. $mountManager->removeMount($this->mountPoint);
  126. return $result;
  127. }
  128. }