updater.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. // shares which can be removed from oc_share after the delete operation was successful
  28. static private $toRemove = array();
  29. /**
  30. * walk up the users file tree and update the etags
  31. * @param string $user
  32. * @param string $path
  33. */
  34. static private function correctUsersFolder($user, $path) {
  35. // $path points to the mount point which is a virtual folder, so we start with
  36. // the parent
  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. * Correct the parent folders' ETags for all users shared the file at $target
  52. *
  53. * @param string $target
  54. */
  55. static public function correctFolders($target) {
  56. // ignore part files
  57. if (pathinfo($target, PATHINFO_EXTENSION) === 'part') {
  58. return false;
  59. }
  60. // Correct Shared folders of other users shared with
  61. $shares = \OCA\Files_Sharing\Helper::getSharesFromItem($target);
  62. foreach ($shares as $share) {
  63. if ((int)$share['share_type'] === \OCP\Share::SHARE_TYPE_USER) {
  64. self::correctUsersFolder($share['share_with'], $share['file_target']);
  65. } elseif ((int)$share['share_type'] === \OCP\Share::SHARE_TYPE_GROUP) {
  66. $users = \OC_Group::usersInGroup($share['share_with']);
  67. foreach ($users as $user) {
  68. self::correctUsersFolder($user, $share['file_target']);
  69. }
  70. } else { //unique name for group share
  71. self::correctUsersFolder($share['share_with'], $share['file_target']);
  72. }
  73. }
  74. }
  75. /**
  76. * remove all shares for a given file if the file was deleted
  77. *
  78. * @param string $path
  79. */
  80. private static function removeShare($path) {
  81. $fileSource = self::$toRemove[$path];
  82. if (!\OC\Files\Filesystem::file_exists($path)) {
  83. $query = \OC_DB::prepare('DELETE FROM `*PREFIX*share` WHERE `file_source`=?');
  84. try {
  85. \OC_DB::executeAudited($query, array($fileSource));
  86. } catch (\Exception $e) {
  87. \OCP\Util::writeLog('files_sharing', "can't remove share: " . $e->getMessage(), \OCP\Util::WARN);
  88. }
  89. }
  90. unset(self::$toRemove[$path]);
  91. }
  92. /**
  93. * @param array $params
  94. */
  95. static public function writeHook($params) {
  96. self::correctFolders($params['path']);
  97. }
  98. /**
  99. * @param array $params
  100. */
  101. static public function renameHook($params) {
  102. self::correctFolders($params['newpath']);
  103. self::correctFolders(pathinfo($params['oldpath'], PATHINFO_DIRNAME));
  104. self::renameChildren($params['oldpath'], $params['newpath']);
  105. }
  106. /**
  107. * @param array $params
  108. */
  109. static public function deleteHook($params) {
  110. $path = $params['path'];
  111. self::correctFolders($path);
  112. $fileInfo = \OC\Files\Filesystem::getFileInfo($path);
  113. // mark file as deleted so that we can clean up the share table if
  114. // the file was deleted successfully
  115. self::$toRemove[$path] = $fileInfo['fileid'];
  116. }
  117. /**
  118. * @param array $params
  119. */
  120. static public function postDeleteHook($params) {
  121. self::removeShare($params['path']);
  122. }
  123. /**
  124. * update etags if a file was shared
  125. * @param array $params
  126. */
  127. static public function postShareHook($params) {
  128. if ($params['itemType'] === 'folder' || $params['itemType'] === 'file') {
  129. $shareWith = $params['shareWith'];
  130. $shareType = $params['shareType'];
  131. if ($shareType === \OCP\Share::SHARE_TYPE_USER) {
  132. self::correctUsersFolder($shareWith, '/');
  133. } elseif ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
  134. foreach (\OC_Group::usersInGroup($shareWith) as $user) {
  135. self::correctUsersFolder($user, '/');
  136. }
  137. }
  138. }
  139. }
  140. /**
  141. * update etags if a file was unshared
  142. *
  143. * @param array $params
  144. */
  145. static public function postUnshareHook($params) {
  146. // only update etags for file/folders shared to local users/groups
  147. if (($params['itemType'] === 'file' || $params['itemType'] === 'folder') &&
  148. $params['shareType'] !== \OCP\Share::SHARE_TYPE_LINK &&
  149. $params['shareType'] !== \OCP\Share::SHARE_TYPE_REMOTE) {
  150. $deletedShares = isset($params['deletedShares']) ? $params['deletedShares'] : array();
  151. foreach ($deletedShares as $share) {
  152. if ($share['shareType'] === \OCP\Share::SHARE_TYPE_GROUP) {
  153. foreach (\OC_Group::usersInGroup($share['shareWith']) as $user) {
  154. self::correctUsersFolder($user, dirname($share['fileTarget']));
  155. }
  156. } else {
  157. self::correctUsersFolder($share['shareWith'], dirname($share['fileTarget']));
  158. }
  159. }
  160. }
  161. }
  162. /**
  163. * update etags if file was unshared from self
  164. * @param array $params
  165. */
  166. static public function postUnshareFromSelfHook($params) {
  167. if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') {
  168. foreach ($params['unsharedItems'] as $item) {
  169. if ($item['shareType'] === \OCP\Share::SHARE_TYPE_GROUP) {
  170. foreach (\OC_Group::usersInGroup($item['shareWith']) as $user) {
  171. self::correctUsersFolder($user, dirname($item['fileTarget']));
  172. }
  173. } else {
  174. self::correctUsersFolder($item['shareWith'], dirname($item['fileTarget']));
  175. }
  176. }
  177. }
  178. }
  179. /**
  180. * clean up oc_share table from files which are no longer exists
  181. *
  182. * This fixes issues from updates from files_sharing < 0.3.5.6 (ownCloud 4.5)
  183. * It will just be called during the update of the app
  184. */
  185. static public function fixBrokenSharesOnAppUpdate() {
  186. // delete all shares where the original file no longer exists
  187. $findAndRemoveShares = \OC_DB::prepare('DELETE FROM `*PREFIX*share` ' .
  188. 'WHERE `item_type` IN (\'file\', \'folder\') ' .
  189. 'AND `file_source` NOT IN (SELECT `fileid` FROM `*PREFIX*filecache`)'
  190. );
  191. $findAndRemoveShares->execute(array());
  192. }
  193. /**
  194. * rename mount point from the children if the parent was renamed
  195. *
  196. * @param string $oldPath old path relative to data/user/files
  197. * @param string $newPath new path relative to data/user/files
  198. */
  199. static private function renameChildren($oldPath, $newPath) {
  200. $absNewPath = \OC\Files\Filesystem::normalizePath('/' . \OCP\User::getUser() . '/files/' . $newPath);
  201. $absOldPath = \OC\Files\Filesystem::normalizePath('/' . \OCP\User::getUser() . '/files/' . $oldPath);
  202. $mountManager = \OC\Files\Filesystem::getMountManager();
  203. $mountedShares = $mountManager->findIn('/' . \OCP\User::getUser() . '/files/' . $oldPath);
  204. foreach ($mountedShares as $mount) {
  205. if ($mount->getStorage()->instanceOfStorage('OCA\Files_Sharing\ISharedStorage')) {
  206. $mountPoint = $mount->getMountPoint();
  207. $target = str_replace($absOldPath, $absNewPath, $mountPoint);
  208. $mount->moveMount($target);
  209. }
  210. }
  211. }
  212. }