1
0

sharedmount.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. * @author Robin Appelman <icewind@owncloud.com>
  6. *
  7. * @copyright Copyright (c) 2015, ownCloud, Inc.
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program 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 License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\Files_Sharing;
  24. use OC\Files\Mount\MountPoint;
  25. use OC\Files\Mount\MoveableMount;
  26. /**
  27. * Shared mount points can be moved by the user
  28. */
  29. class SharedMount extends MountPoint implements MoveableMount {
  30. /**
  31. * @var \OC\Files\Storage\Shared $storage
  32. */
  33. protected $storage = null;
  34. public function __construct($storage, $mountpoint, $arguments = null, $loader = null) {
  35. // first update the mount point before creating the parent
  36. $newMountPoint = $this->verifyMountPoint($arguments['share'], $arguments['user']);
  37. $absMountPoint = '/' . $arguments['user'] . '/files' . $newMountPoint;
  38. parent::__construct($storage, $absMountPoint, $arguments, $loader);
  39. }
  40. /**
  41. * check if the parent folder exists otherwise move the mount point up
  42. */
  43. private function verifyMountPoint(&$share, $user) {
  44. $mountPoint = basename($share['file_target']);
  45. $parent = dirname($share['file_target']);
  46. if (!\OC\Files\Filesystem::is_dir($parent)) {
  47. $parent = Helper::getShareFolder();
  48. }
  49. $newMountPoint = \OCA\Files_Sharing\Helper::generateUniqueTarget(
  50. \OC\Files\Filesystem::normalizePath($parent . '/' . $mountPoint),
  51. array(),
  52. new \OC\Files\View('/' . $user . '/files')
  53. );
  54. if($newMountPoint !== $share['file_target']) {
  55. self::updateFileTarget($newMountPoint, $share);
  56. $share['file_target'] = $newMountPoint;
  57. $share['unique_name'] = true;
  58. }
  59. return $newMountPoint;
  60. }
  61. /**
  62. * update fileTarget in the database if the mount point changed
  63. * @param string $newPath
  64. * @param array $share reference to the share which should be modified
  65. * @return bool
  66. */
  67. private static function updateFileTarget($newPath, &$share) {
  68. // if the user renames a mount point from a group share we need to create a new db entry
  69. // for the unique name
  70. if ($share['share_type'] === \OCP\Share::SHARE_TYPE_GROUP && empty($share['unique_name'])) {
  71. $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (`item_type`, `item_source`, `item_target`,'
  72. .' `share_type`, `share_with`, `uid_owner`, `permissions`, `stime`, `file_source`,'
  73. .' `file_target`, `token`, `parent`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)');
  74. $arguments = array($share['item_type'], $share['item_source'], $share['item_target'],
  75. 2, \OCP\User::getUser(), $share['uid_owner'], $share['permissions'], $share['stime'], $share['file_source'],
  76. $newPath, $share['token'], $share['id']);
  77. } else {
  78. // rename mount point
  79. $query = \OC_DB::prepare(
  80. 'Update `*PREFIX*share`
  81. SET `file_target` = ?
  82. WHERE `id` = ?'
  83. );
  84. $arguments = array($newPath, $share['id']);
  85. }
  86. $result = $query->execute($arguments);
  87. return $result === 1 ? true : false;
  88. }
  89. /**
  90. * Format a path to be relative to the /user/files/ directory
  91. *
  92. * @param string $path the absolute path
  93. * @return string e.g. turns '/admin/files/test.txt' into '/test.txt'
  94. */
  95. protected function stripUserFilesPath($path) {
  96. $trimmed = ltrim($path, '/');
  97. $split = explode('/', $trimmed);
  98. // it is not a file relative to data/user/files
  99. if (count($split) < 3 || $split[1] !== 'files') {
  100. \OCP\Util::writeLog('file sharing',
  101. 'Can not strip userid and "files/" from path: ' . $path,
  102. \OCP\Util::ERROR);
  103. throw new \OCA\Files_Sharing\Exceptions\BrokenPath('Path does not start with /user/files', 10);
  104. }
  105. // skip 'user' and 'files'
  106. $sliced = array_slice($split, 2);
  107. $relPath = implode('/', $sliced);
  108. return '/' . $relPath;
  109. }
  110. /**
  111. * Move the mount point to $target
  112. *
  113. * @param string $target the target mount point
  114. * @return bool
  115. */
  116. public function moveMount($target) {
  117. $relTargetPath = $this->stripUserFilesPath($target);
  118. $share = $this->storage->getShare();
  119. $result = true;
  120. if (!empty($share['grouped'])) {
  121. foreach ($share['grouped'] as $s) {
  122. $result = $this->updateFileTarget($relTargetPath, $s) && $result;
  123. }
  124. } else {
  125. $result = $this->updateFileTarget($relTargetPath, $share) && $result;
  126. }
  127. if ($result) {
  128. $this->setMountPoint($target);
  129. $this->storage->setUniqueName();
  130. $this->storage->setMountPoint($relTargetPath);
  131. } else {
  132. \OCP\Util::writeLog('file sharing',
  133. 'Could not rename mount point for shared folder "' . $this->getMountPoint() . '" to "' . $target . '"',
  134. \OCP\Util::ERROR);
  135. }
  136. return $result;
  137. }
  138. /**
  139. * Remove the mount points
  140. *
  141. * @return bool
  142. */
  143. public function removeMount() {
  144. $mountManager = \OC\Files\Filesystem::getMountManager();
  145. /** @var \OC\Files\Storage\Shared */
  146. $storage = $this->getStorage();
  147. $result = $storage->unshareStorage();
  148. $mountManager->removeMount($this->mountPoint);
  149. return $result;
  150. }
  151. }