Blame view

sources/apps/files_sharing/lib/updater.php 7.22 KB
03e52840d   Kload   Init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  <?php
  /**
   * ownCloud
   *
   * @author Michael Gapczynski
   * @copyright 2013 Michael Gapczynski mtgap@owncloud.com
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
   * License as published by the Free Software Foundation; either
   * version 3 of the License, or any later version.
   *
   * This library is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
   *
   * You should have received a copy of the GNU Affero General Public
   * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
   */
  
  namespace OC\Files\Cache;
  
  class Shared_Updater {
31b7f2792   Kload   Upgrade to ownclo...
25
26
  	// shares which can be removed from oc_share after the delete operation was successful
  	static private $toRemove = array();
03e52840d   Kload   Init
27
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  	 * walk up the users file tree and update the etags
  	 * @param string $user
  	 * @param string $path
  	 */
  	static private function correctUsersFolder($user, $path) {
  		// $path points to the mount point which is a virtual folder, so we start with
  		// the parent
  		$path = '/files' . dirname($path);
  		\OC\Files\Filesystem::initMountPoints($user);
  		$view = new \OC\Files\View('/' . $user);
  		if ($view->file_exists($path)) {
  			while ($path !== dirname($path)) {
  				$etag = $view->getETag($path);
  				$view->putFileInfo($path, array('etag' => $etag));
  				$path = dirname($path);
  			}
  		} else {
  			\OCP\Util::writeLog('files_sharing', 'can not update etags on ' . $path . ' for user ' . $user . '. Path does not exists', \OCP\Util::DEBUG);
  		}
  	}
  
  	/**
03e52840d   Kload   Init
50
51
52
53
54
  	* Correct the parent folders' ETags for all users shared the file at $target
  	*
  	* @param string $target
  	*/
  	static public function correctFolders($target) {
6d9380f96   Cédric Dupont   Update sources OC...
55
56
57
58
59
  
  		// ignore part files
  		if (pathinfo($target, PATHINFO_EXTENSION) === 'part') {
  			return false;
  		}
03e52840d   Kload   Init
60
  		// Correct Shared folders of other users shared with
6d9380f96   Cédric Dupont   Update sources OC...
61
62
63
64
65
66
67
  		$shares = \OCA\Files_Sharing\Helper::getSharesFromItem($target);
  
  		foreach ($shares as $share) {
  			if ((int)$share['share_type'] === \OCP\Share::SHARE_TYPE_USER) {
  				self::correctUsersFolder($share['share_with'], $share['file_target']);
  			} elseif ((int)$share['share_type'] === \OCP\Share::SHARE_TYPE_GROUP) {
  				$users = \OC_Group::usersInGroup($share['share_with']);
03e52840d   Kload   Init
68
  				foreach ($users as $user) {
6d9380f96   Cédric Dupont   Update sources OC...
69
  					self::correctUsersFolder($user, $share['file_target']);
03e52840d   Kload   Init
70
  				}
6d9380f96   Cédric Dupont   Update sources OC...
71
72
  			} else { //unique name for group share
  				self::correctUsersFolder($share['share_with'], $share['file_target']);
03e52840d   Kload   Init
73
74
75
76
77
  			}
  		}
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
78
  	 * remove all shares for a given file if the file was deleted
03e52840d   Kload   Init
79
80
81
82
  	 *
  	 * @param string $path
  	 */
  	private static function removeShare($path) {
31b7f2792   Kload   Upgrade to ownclo...
83
  		$fileSource = self::$toRemove[$path];
03e52840d   Kload   Init
84

31b7f2792   Kload   Upgrade to ownclo...
85
86
87
88
89
90
91
  		if (!\OC\Files\Filesystem::file_exists($path)) {
  			$query = \OC_DB::prepare('DELETE FROM `*PREFIX*share` WHERE `file_source`=?');
  			try	{
  				\OC_DB::executeAudited($query, array($fileSource));
  			} catch (\Exception $e) {
  				\OCP\Util::writeLog('files_sharing', "can't remove share: " . $e->getMessage(), \OCP\Util::WARN);
  			}
03e52840d   Kload   Init
92
  		}
31b7f2792   Kload   Upgrade to ownclo...
93
  		unset(self::$toRemove[$path]);
03e52840d   Kload   Init
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  	}
  
  	/**
  	 * @param array $params
  	 */
  	static public function writeHook($params) {
  		self::correctFolders($params['path']);
  	}
  
  	/**
  	 * @param array $params
  	 */
  	static public function renameHook($params) {
  		self::correctFolders($params['newpath']);
  		self::correctFolders(pathinfo($params['oldpath'], PATHINFO_DIRNAME));
6d9380f96   Cédric Dupont   Update sources OC...
109
  		self::renameChildren($params['oldpath'], $params['newpath']);
03e52840d   Kload   Init
110
111
112
113
114
115
  	}
  
  	/**
  	 * @param array $params
  	 */
  	static public function deleteHook($params) {
6d9380f96   Cédric Dupont   Update sources OC...
116
117
118
119
  		$path = $params['path'];
  		self::correctFolders($path);
  
  		$fileInfo = \OC\Files\Filesystem::getFileInfo($path);
31b7f2792   Kload   Upgrade to ownclo...
120
121
  		// mark file as deleted so that we can clean up the share table if
  		// the file was deleted successfully
6d9380f96   Cédric Dupont   Update sources OC...
122
  		self::$toRemove[$path] =  $fileInfo['fileid'];
03e52840d   Kload   Init
123
  	}
31b7f2792   Kload   Upgrade to ownclo...
124
125
126
127
128
129
  	/**
  	 * @param array $params
  	 */
  	static public function postDeleteHook($params) {
  		self::removeShare($params['path']);
  	}
03e52840d   Kload   Init
130
131
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
  	 * update etags if a file was shared
  	 * @param array $params
  	 */
  	static public function postShareHook($params) {
  
  		if ($params['itemType'] === 'folder' || $params['itemType'] === 'file') {
  
  			$shareWith = $params['shareWith'];
  			$shareType = $params['shareType'];
  
  			if ($shareType === \OCP\Share::SHARE_TYPE_USER) {
  				self::correctUsersFolder($shareWith, '/');
  			} elseif ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
  				foreach (\OC_Group::usersInGroup($shareWith) as $user) {
  					self::correctUsersFolder($user, '/');
  				}
  			}
  		}
  	}
  
  	/**
  	 * update etags if a file was unshared
  	 *
03e52840d   Kload   Init
155
156
  	 * @param array $params
  	 */
6d9380f96   Cédric Dupont   Update sources OC...
157
  	static public function postUnshareHook($params) {
03e52840d   Kload   Init
158
  		if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') {
6d9380f96   Cédric Dupont   Update sources OC...
159
160
161
162
163
164
165
166
167
168
169
  
  			$deletedShares = isset($params['deletedShares']) ? $params['deletedShares'] : array();
  
  			foreach ($deletedShares as $share) {
  				if ($share['shareType'] === \OCP\Share::SHARE_TYPE_GROUP) {
  					foreach (\OC_Group::usersInGroup($share['shareWith']) as $user) {
  						self::correctUsersFolder($user, dirname($share['fileTarget']));
  					}
  				} else {
  					self::correctUsersFolder($share['shareWith'], dirname($share['fileTarget']));
  				}
31b7f2792   Kload   Upgrade to ownclo...
170
  			}
6d9380f96   Cédric Dupont   Update sources OC...
171
172
173
174
175
176
177
178
179
180
181
182
183
  		}
  	}
  
  	/**
  	 * update etags if file was unshared from self
  	 * @param array $params
  	 */
  	static public function postUnshareFromSelfHook($params) {
  		if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') {
  			foreach ($params['unsharedItems'] as $item) {
  				if ($item['shareType'] === \OCP\Share::SHARE_TYPE_GROUP) {
  					foreach (\OC_Group::usersInGroup($item['shareWith']) as $user) {
  						self::correctUsersFolder($user, dirname($item['fileTarget']));
03e52840d   Kload   Init
184
  					}
6d9380f96   Cédric Dupont   Update sources OC...
185
186
  				} else {
  					self::correctUsersFolder($item['shareWith'], dirname($item['fileTarget']));
03e52840d   Kload   Init
187
188
189
190
  				}
  			}
  		}
  	}
6d9380f96   Cédric Dupont   Update sources OC...
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
  	/**
  	 * clean up oc_share table from files which are no longer exists
  	 *
  	 * This fixes issues from updates from files_sharing < 0.3.5.6 (ownCloud 4.5)
  	 * It will just be called during the update of the app
  	 */
  	static public function fixBrokenSharesOnAppUpdate() {
  		// delete all shares where the original file no longer exists
  		$findAndRemoveShares = \OC_DB::prepare('DELETE FROM `*PREFIX*share` ' .
  			'WHERE `item_type` IN (\'file\', \'folder\') ' .
  			'AND `file_source` NOT IN (SELECT `fileid` FROM `*PREFIX*filecache`)'
  		);
  		$findAndRemoveShares->execute(array());
  	}
  
  	/**
  	 * rename mount point from the children if the parent was renamed
  	 * 
  	 * @param string $oldPath old path relative to data/user/files
  	 * @param string $newPath new path relative to data/user/files
  	 */
  	static private function renameChildren($oldPath, $newPath) {
  
  		$absNewPath =  \OC\Files\Filesystem::normalizePath('/' . \OCP\User::getUser() . '/files/' . $newPath);
  		$absOldPath =  \OC\Files\Filesystem::normalizePath('/' . \OCP\User::getUser() . '/files/' . $oldPath);
  
  		$mountManager = \OC\Files\Filesystem::getMountManager();
  		$mountedShares = $mountManager->findIn('/' . \OCP\User::getUser() . '/files/' . $oldPath);
  		foreach ($mountedShares as $mount) {
  			if ($mount->getStorage()->instanceOfStorage('OCA\Files_Sharing\ISharedStorage')) {
  				$mountPoint = $mount->getMountPoint();
  				$target = str_replace($absOldPath, $absNewPath, $mountPoint);
  				$mount->moveMount($target);
  			}
  		}
  	}
03e52840d   Kload   Init
227
  }