sharedmount.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. use OC\Files\View;
  27. /**
  28. * Shared mount points can be moved by the user
  29. */
  30. class SharedMount extends MountPoint implements MoveableMount {
  31. /**
  32. * @var \OC\Files\Storage\Shared $storage
  33. */
  34. protected $storage = null;
  35. /**
  36. * @var \OC\Files\Cache\ChangePropagator
  37. */
  38. protected $ownerPropagator;
  39. public function __construct($storage, $mountpoint, $arguments = null, $loader = null) {
  40. // first update the mount point before creating the parent
  41. $this->ownerPropagator = $arguments['propagator'];
  42. $newMountPoint = $this->verifyMountPoint($arguments['share'], $arguments['user']);
  43. $absMountPoint = '/' . $arguments['user'] . '/files' . $newMountPoint;
  44. parent::__construct($storage, $absMountPoint, $arguments, $loader);
  45. }
  46. /**
  47. * check if the parent folder exists otherwise move the mount point up
  48. */
  49. private function verifyMountPoint(&$share, $user) {
  50. $mountPoint = basename($share['file_target']);
  51. $parent = dirname($share['file_target']);
  52. $view = new View('/' . $user . '/files');
  53. if (!$view->is_dir($parent)) {
  54. $parent = Helper::getShareFolder();
  55. }
  56. $newMountPoint = \OCA\Files_Sharing\Helper::generateUniqueTarget(
  57. \OC\Files\Filesystem::normalizePath($parent . '/' . $mountPoint),
  58. array(),
  59. new \OC\Files\View('/' . $user . '/files')
  60. );
  61. if($newMountPoint !== $share['file_target']) {
  62. self::updateFileTarget($newMountPoint, $share);
  63. $share['file_target'] = $newMountPoint;
  64. $share['unique_name'] = true;
  65. }
  66. return $newMountPoint;
  67. }
  68. /**
  69. * update fileTarget in the database if the mount point changed
  70. * @param string $newPath
  71. * @param array $share reference to the share which should be modified
  72. * @return bool
  73. */
  74. private static function updateFileTarget($newPath, &$share) {
  75. // if the user renames a mount point from a group share we need to create a new db entry
  76. // for the unique name
  77. if ($share['share_type'] === \OCP\Share::SHARE_TYPE_GROUP && empty($share['unique_name'])) {
  78. $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (`item_type`, `item_source`, `item_target`,'
  79. .' `share_type`, `share_with`, `uid_owner`, `permissions`, `stime`, `file_source`,'
  80. .' `file_target`, `token`, `parent`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)');
  81. $arguments = array($share['item_type'], $share['item_source'], $share['item_target'],
  82. 2, \OCP\User::getUser(), $share['uid_owner'], $share['permissions'], $share['stime'], $share['file_source'],
  83. $newPath, $share['token'], $share['id']);
  84. } else {
  85. // rename mount point
  86. $query = \OC_DB::prepare(
  87. 'Update `*PREFIX*share`
  88. SET `file_target` = ?
  89. WHERE `id` = ?'
  90. );
  91. $arguments = array($newPath, $share['id']);
  92. }
  93. $result = $query->execute($arguments);
  94. return $result === 1 ? true : false;
  95. }
  96. /**
  97. * Format a path to be relative to the /user/files/ directory
  98. *
  99. * @param string $path the absolute path
  100. * @return string e.g. turns '/admin/files/test.txt' into '/test.txt'
  101. */
  102. protected function stripUserFilesPath($path) {
  103. $trimmed = ltrim($path, '/');
  104. $split = explode('/', $trimmed);
  105. // it is not a file relative to data/user/files
  106. if (count($split) < 3 || $split[1] !== 'files') {
  107. \OCP\Util::writeLog('file sharing',
  108. 'Can not strip userid and "files/" from path: ' . $path,
  109. \OCP\Util::ERROR);
  110. throw new \OCA\Files_Sharing\Exceptions\BrokenPath('Path does not start with /user/files', 10);
  111. }
  112. // skip 'user' and 'files'
  113. $sliced = array_slice($split, 2);
  114. $relPath = implode('/', $sliced);
  115. return '/' . $relPath;
  116. }
  117. /**
  118. * Move the mount point to $target
  119. *
  120. * @param string $target the target mount point
  121. * @return bool
  122. */
  123. public function moveMount($target) {
  124. $relTargetPath = $this->stripUserFilesPath($target);
  125. $share = $this->storage->getShare();
  126. $result = true;
  127. if (!empty($share['grouped'])) {
  128. foreach ($share['grouped'] as $s) {
  129. $result = $this->updateFileTarget($relTargetPath, $s) && $result;
  130. }
  131. } else {
  132. $result = $this->updateFileTarget($relTargetPath, $share) && $result;
  133. }
  134. if ($result) {
  135. $this->setMountPoint($target);
  136. $this->storage->setUniqueName();
  137. $this->storage->setMountPoint($relTargetPath);
  138. } else {
  139. \OCP\Util::writeLog('file sharing',
  140. 'Could not rename mount point for shared folder "' . $this->getMountPoint() . '" to "' . $target . '"',
  141. \OCP\Util::ERROR);
  142. }
  143. return $result;
  144. }
  145. /**
  146. * Remove the mount points
  147. *
  148. * @return bool
  149. */
  150. public function removeMount() {
  151. $mountManager = \OC\Files\Filesystem::getMountManager();
  152. /** @var \OC\Files\Storage\Shared */
  153. $storage = $this->getStorage();
  154. $result = $storage->unshareStorage();
  155. $mountManager->removeMount($this->mountPoint);
  156. return $result;
  157. }
  158. public function getShare() {
  159. return $this->getStorage()->getShare();
  160. }
  161. /**
  162. * @return \OC\Files\Cache\ChangePropagator
  163. */
  164. public function getOwnerPropagator() {
  165. return $this->ownerPropagator;
  166. }
  167. }