updater.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Vincent Petry <pvince81@owncloud.com>
  8. *
  9. * @copyright Copyright (c) 2015, ownCloud, Inc.
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Files\Cache;
  26. class Shared_Updater {
  27. /**
  28. * Walk up the users file tree and update the etags.
  29. *
  30. * @param string $user user id
  31. * @param string $path share mount point path, relative to the user's "files" folder
  32. */
  33. static private function correctUsersFolder($user, $path) {
  34. // $path points to the mount point which is a virtual folder, so we start with
  35. // the parent
  36. $path = '/' . ltrim($path, '/');
  37. $path = '/files' . dirname($path);
  38. \OC\Files\Filesystem::initMountPoints($user);
  39. $view = new \OC\Files\View('/' . $user);
  40. if ($view->file_exists($path)) {
  41. while ($path !== dirname($path)) {
  42. $etag = $view->getETag($path);
  43. $view->putFileInfo($path, array('etag' => $etag));
  44. $path = dirname($path);
  45. }
  46. } else {
  47. \OCP\Util::writeLog('files_sharing', 'can not update etags on ' . $path . ' for user ' . $user . '. Path does not exists', \OCP\Util::DEBUG);
  48. }
  49. }
  50. /**
  51. * @param array $params
  52. */
  53. static public function renameHook($params) {
  54. self::renameChildren($params['oldpath'], $params['newpath']);
  55. }
  56. /**
  57. * @param array $params
  58. */
  59. static public function deleteHook($params) {
  60. $path = $params['path'];
  61. }
  62. /**
  63. * update etags if a file was shared
  64. * @param array $params
  65. */
  66. static public function postShareHook($params) {
  67. if ($params['itemType'] === 'folder' || $params['itemType'] === 'file') {
  68. $shareWith = $params['shareWith'];
  69. $shareType = $params['shareType'];
  70. if ($shareType === \OCP\Share::SHARE_TYPE_USER) {
  71. self::correctUsersFolder($shareWith, $params['fileTarget']);
  72. } elseif ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
  73. foreach (\OC_Group::usersInGroup($shareWith) as $user) {
  74. self::correctUsersFolder($user, $params['fileTarget']);
  75. }
  76. }
  77. }
  78. }
  79. /**
  80. * update etags if a file was unshared
  81. *
  82. * @param array $params
  83. */
  84. static public function postUnshareHook($params) {
  85. // only update etags for file/folders shared to local users/groups
  86. if (($params['itemType'] === 'file' || $params['itemType'] === 'folder') &&
  87. $params['shareType'] !== \OCP\Share::SHARE_TYPE_LINK &&
  88. $params['shareType'] !== \OCP\Share::SHARE_TYPE_REMOTE) {
  89. $deletedShares = isset($params['deletedShares']) ? $params['deletedShares'] : array();
  90. foreach ($deletedShares as $share) {
  91. if ($share['shareType'] === \OCP\Share::SHARE_TYPE_GROUP) {
  92. foreach (\OC_Group::usersInGroup($share['shareWith']) as $user) {
  93. self::correctUsersFolder($user, $share['fileTarget']);
  94. }
  95. } else {
  96. self::correctUsersFolder($share['shareWith'], $share['fileTarget']);
  97. }
  98. }
  99. }
  100. }
  101. /**
  102. * update etags if file was unshared from self
  103. * @param array $params
  104. */
  105. static public function postUnshareFromSelfHook($params) {
  106. if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') {
  107. foreach ($params['unsharedItems'] as $item) {
  108. if ($item['shareType'] === \OCP\Share::SHARE_TYPE_GROUP) {
  109. foreach (\OC_Group::usersInGroup($item['shareWith']) as $user) {
  110. self::correctUsersFolder($user, $item['fileTarget']);
  111. }
  112. } else {
  113. self::correctUsersFolder($item['shareWith'], $item['fileTarget']);
  114. }
  115. }
  116. }
  117. }
  118. /**
  119. * clean up oc_share table from files which are no longer exists
  120. *
  121. * This fixes issues from updates from files_sharing < 0.3.5.6 (ownCloud 4.5)
  122. * It will just be called during the update of the app
  123. */
  124. static public function fixBrokenSharesOnAppUpdate() {
  125. // delete all shares where the original file no longer exists
  126. $findAndRemoveShares = \OCP\DB::prepare('DELETE FROM `*PREFIX*share` ' .
  127. 'WHERE `item_type` IN (\'file\', \'folder\') ' .
  128. 'AND `file_source` NOT IN (SELECT `fileid` FROM `*PREFIX*filecache`)'
  129. );
  130. $findAndRemoveShares->execute(array());
  131. }
  132. /**
  133. * rename mount point from the children if the parent was renamed
  134. *
  135. * @param string $oldPath old path relative to data/user/files
  136. * @param string $newPath new path relative to data/user/files
  137. */
  138. static private function renameChildren($oldPath, $newPath) {
  139. $absNewPath = \OC\Files\Filesystem::normalizePath('/' . \OCP\User::getUser() . '/files/' . $newPath);
  140. $absOldPath = \OC\Files\Filesystem::normalizePath('/' . \OCP\User::getUser() . '/files/' . $oldPath);
  141. $mountManager = \OC\Files\Filesystem::getMountManager();
  142. $mountedShares = $mountManager->findIn('/' . \OCP\User::getUser() . '/files/' . $oldPath);
  143. foreach ($mountedShares as $mount) {
  144. if ($mount->getStorage()->instanceOfStorage('OCA\Files_Sharing\ISharedStorage')) {
  145. $mountPoint = $mount->getMountPoint();
  146. $target = str_replace($absOldPath, $absNewPath, $mountPoint);
  147. $mount->moveMount($target);
  148. }
  149. }
  150. }
  151. }