summaryrefslogtreecommitdiffstats
path: root/apps/files_sharing/lib/updater.php
blob: 0c35b18c42be3b89df37dbd1ccef8ca760337ae7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?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 {

	/**
	* Correct the parent folders' ETags for all users shared the file at $target
	*
	* @param string $target
	*/
	static public function correctFolders($target) {
		$uid = \OCP\User::getUser();
		$uidOwner = \OC\Files\Filesystem::getOwner($target);
		$info = \OC\Files\Filesystem::getFileInfo($target);
		$checkedUser = array($uidOwner);
		// Correct Shared folders of other users shared with
		$users = \OCP\Share::getUsersItemShared('file', $info['fileid'], $uidOwner, true);
		if (!empty($users)) {
			while (!empty($users)) {
				$reshareUsers = array();
				foreach ($users as $user) {
					if ( !in_array($user, $checkedUser) ) {
						$etag = \OC\Files\Filesystem::getETag('');
						\OCP\Config::setUserValue($user, 'files_sharing', 'etag', $etag);
						// Look for reshares
						$reshareUsers = array_merge($reshareUsers, \OCP\Share::getUsersItemShared('file', $info['fileid'], $user, true));
						$checkedUser[] = $user;
					}
				}
				$users = $reshareUsers;
			}
		}
	}

	/**
	 * @brief remove all shares for a given file if the file was deleted
	 *
	 * @param string $path
	 */
	private static function removeShare($path) {
		$fileInfo = \OC\Files\Filesystem::getFileInfo($path);
		$fileSource = $fileInfo['fileid'];

		$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);
		}
	}

	/**
	 * @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));
	}

	/**
	 * @param array $params
	 */
	static public function deleteHook($params) {
		self::correctFolders($params['path']);
	}

	/**
	 * @param array $params
	 */
	static public function postDeleteHook($params) {
		self::removeShare($params['path']);
	}

	/**
	 * @param array $params
	 */
	static public function shareHook($params) {
		if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') {
			$uidOwner = \OCP\User::getUser();
			$users = \OCP\Share::getUsersItemShared($params['itemType'], $params['fileSource'], $uidOwner, true);
			if (!empty($users)) {
				while (!empty($users)) {
					$reshareUsers = array();
					foreach ($users as $user) {
						if ($user !== $uidOwner) {
							$etag = \OC\Files\Filesystem::getETag('');
							\OCP\Config::setUserValue($user, 'files_sharing', 'etag', $etag);
							// Look for reshares
							$reshareUsers = array_merge($reshareUsers, \OCP\Share::getUsersItemShared('file', $params['fileSource'], $user, true));
						}
					}
					$users = $reshareUsers;
				}
			}
		}
	}

}