updater.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. * Correct the parent folders' ETags for all users shared the file at $target
  27. *
  28. * @param string $target
  29. */
  30. static public function correctFolders($target) {
  31. $uid = \OCP\User::getUser();
  32. $uidOwner = \OC\Files\Filesystem::getOwner($target);
  33. $info = \OC\Files\Filesystem::getFileInfo($target);
  34. $checkedUser = array($uidOwner);
  35. // Correct Shared folders of other users shared with
  36. $users = \OCP\Share::getUsersItemShared('file', $info['fileid'], $uidOwner, true);
  37. if (!empty($users)) {
  38. while (!empty($users)) {
  39. $reshareUsers = array();
  40. foreach ($users as $user) {
  41. if ( !in_array($user, $checkedUser) ) {
  42. $etag = \OC\Files\Filesystem::getETag('');
  43. \OCP\Config::setUserValue($user, 'files_sharing', 'etag', $etag);
  44. // Look for reshares
  45. $reshareUsers = array_merge($reshareUsers, \OCP\Share::getUsersItemShared('file', $info['fileid'], $user, true));
  46. $checkedUser[] = $user;
  47. }
  48. }
  49. $users = $reshareUsers;
  50. }
  51. }
  52. }
  53. /**
  54. * @brief remove all shares for a given file if the file was deleted
  55. *
  56. * @param string $path
  57. */
  58. private static function removeShare($path) {
  59. $fileSource = self::$toRemove[$path];
  60. if (!\OC\Files\Filesystem::file_exists($path)) {
  61. $query = \OC_DB::prepare('DELETE FROM `*PREFIX*share` WHERE `file_source`=?');
  62. try {
  63. \OC_DB::executeAudited($query, array($fileSource));
  64. } catch (\Exception $e) {
  65. \OCP\Util::writeLog('files_sharing', "can't remove share: " . $e->getMessage(), \OCP\Util::WARN);
  66. }
  67. }
  68. unset(self::$toRemove[$path]);
  69. }
  70. /**
  71. * @param array $params
  72. */
  73. static public function writeHook($params) {
  74. self::correctFolders($params['path']);
  75. }
  76. /**
  77. * @param array $params
  78. */
  79. static public function renameHook($params) {
  80. self::correctFolders($params['newpath']);
  81. self::correctFolders(pathinfo($params['oldpath'], PATHINFO_DIRNAME));
  82. }
  83. /**
  84. * @param array $params
  85. */
  86. static public function deleteHook($params) {
  87. self::correctFolders($params['path']);
  88. $fileInfo = \OC\Files\Filesystem::getFileInfo($params['path']);
  89. // mark file as deleted so that we can clean up the share table if
  90. // the file was deleted successfully
  91. self::$toRemove[$params['path']] = $fileInfo['fileid'];
  92. }
  93. /**
  94. * @param array $params
  95. */
  96. static public function postDeleteHook($params) {
  97. self::removeShare($params['path']);
  98. }
  99. /**
  100. * @param array $params
  101. */
  102. static public function shareHook($params) {
  103. if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') {
  104. if (isset($params['uidOwner'])) {
  105. $uidOwner = $params['uidOwner'];
  106. } else {
  107. $uidOwner = \OCP\User::getUser();
  108. }
  109. $users = \OCP\Share::getUsersItemShared($params['itemType'], $params['fileSource'], $uidOwner, true, false);
  110. if (!empty($users)) {
  111. while (!empty($users)) {
  112. $reshareUsers = array();
  113. foreach ($users as $user) {
  114. if ($user !== $uidOwner) {
  115. $etag = \OC\Files\Filesystem::getETag('');
  116. \OCP\Config::setUserValue($user, 'files_sharing', 'etag', $etag);
  117. // Look for reshares
  118. $reshareUsers = array_merge($reshareUsers, \OCP\Share::getUsersItemShared('file', $params['fileSource'], $user, true));
  119. }
  120. }
  121. $users = $reshareUsers;
  122. }
  123. }
  124. }
  125. }
  126. }