You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

updater.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Michael Gapczynski
  6. * @copyright 2013 Michael Gapczynski mtgap@owncloud.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. namespace OC\Files\Cache;
  22. class Shared_Updater {
  23. // shares which can be removed from oc_share after the delete operation was successful
  24. static private $toRemove = array();
  25. /**
  26. * @brief walk up the users file tree and update the etags
  27. * @param string $user
  28. * @param string $path
  29. */
  30. static private function correctUsersFolder($user, $path) {
  31. // $path points to the mount point which is a virtual folder, so we start with
  32. // the parent
  33. $path = '/files' . dirname($path);
  34. \OC\Files\Filesystem::initMountPoints($user);
  35. $view = new \OC\Files\View('/' . $user);
  36. if ($view->file_exists($path)) {
  37. while ($path !== dirname($path)) {
  38. $etag = $view->getETag($path);
  39. $view->putFileInfo($path, array('etag' => $etag));
  40. $path = dirname($path);
  41. }
  42. } else {
  43. \OCP\Util::writeLog('files_sharing', 'can not update etags on ' . $path . ' for user ' . $user . '. Path does not exists', \OCP\Util::DEBUG);
  44. }
  45. }
  46. /**
  47. * Correct the parent folders' ETags for all users shared the file at $target
  48. *
  49. * @param string $target
  50. */
  51. static public function correctFolders($target) {
  52. // ignore part files
  53. if (pathinfo($target, PATHINFO_EXTENSION) === 'part') {
  54. return false;
  55. }
  56. // Correct Shared folders of other users shared with
  57. $shares = \OCA\Files_Sharing\Helper::getSharesFromItem($target);
  58. foreach ($shares as $share) {
  59. if ((int)$share['share_type'] === \OCP\Share::SHARE_TYPE_USER) {
  60. self::correctUsersFolder($share['share_with'], $share['file_target']);
  61. } elseif ((int)$share['share_type'] === \OCP\Share::SHARE_TYPE_GROUP) {
  62. $users = \OC_Group::usersInGroup($share['share_with']);
  63. foreach ($users as $user) {
  64. self::correctUsersFolder($user, $share['file_target']);
  65. }
  66. } else { //unique name for group share
  67. self::correctUsersFolder($share['share_with'], $share['file_target']);
  68. }
  69. }
  70. }
  71. /**
  72. * @brief remove all shares for a given file if the file was deleted
  73. *
  74. * @param string $path
  75. */
  76. private static function removeShare($path) {
  77. $fileSource = self::$toRemove[$path];
  78. if (!\OC\Files\Filesystem::file_exists($path)) {
  79. $query = \OC_DB::prepare('DELETE FROM `*PREFIX*share` WHERE `file_source`=?');
  80. try {
  81. \OC_DB::executeAudited($query, array($fileSource));
  82. } catch (\Exception $e) {
  83. \OCP\Util::writeLog('files_sharing', "can't remove share: " . $e->getMessage(), \OCP\Util::WARN);
  84. }
  85. }
  86. unset(self::$toRemove[$path]);
  87. }
  88. /**
  89. * @param array $params
  90. */
  91. static public function writeHook($params) {
  92. self::correctFolders($params['path']);
  93. }
  94. /**
  95. * @param array $params
  96. */
  97. static public function renameHook($params) {
  98. self::correctFolders($params['newpath']);
  99. self::correctFolders(pathinfo($params['oldpath'], PATHINFO_DIRNAME));
  100. }
  101. /**
  102. * @param array $params
  103. */
  104. static public function deleteHook($params) {
  105. self::correctFolders($params['path']);
  106. $fileInfo = \OC\Files\Filesystem::getFileInfo($params['path']);
  107. // mark file as deleted so that we can clean up the share table if
  108. // the file was deleted successfully
  109. self::$toRemove[$params['path']] = $fileInfo['fileid'];
  110. }
  111. /**
  112. * @param array $params
  113. */
  114. static public function postDeleteHook($params) {
  115. self::removeShare($params['path']);
  116. }
  117. /**
  118. * clean up oc_share table from files which are no longer exists
  119. *
  120. * This fixes issues from updates from files_sharing < 0.3.5.6 (ownCloud 4.5)
  121. * It will just be called during the update of the app
  122. */
  123. static public function fixBrokenSharesOnAppUpdate() {
  124. // delete all shares where the original file no longer exists
  125. $findAndRemoveShares = \OC_DB::prepare('DELETE FROM `*PREFIX*share` ' .
  126. 'WHERE `file_source` NOT IN ( ' .
  127. 'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `item_type` IN (\'file\', \'folder\'))'
  128. );
  129. $findAndRemoveShares->execute(array());
  130. }
  131. }