sharedmount.php 5.9 KB

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