updater.php 3.5 KB

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