Passed
Push — master ( ef6806...fd475d )
by Roeland
09:13 queued 10s
created

DefaultShareProvider   F

Complexity

Total Complexity 134

Size/Duplication

Total Lines 1351
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 727
dl 0
loc 1351
rs 1.873
c 0
b 0
f 0
wmc 134

26 Methods

Rating   Name   Duplication   Size   Complexity  
A identifier() 0 2 1
A __construct() 0 17 1
B create() 0 89 10
A getSharesByPath() 0 25 2
A isAccessibleResult() 0 14 4
B deleteFromSelf() 0 74 8
B update() 0 83 7
A move() 0 56 4
A delete() 0 14 2
A getShareByToken() 0 26 3
A getSharesInFolder() 0 42 3
B getSharesBy() 0 46 6
A restore() 0 27 1
A getShareById() 0 41 5
A getChildren() 0 30 2
C getSharedWith() 0 123 14
A userDeleted() 0 56 4
A groupDeleted() 0 35 4
B sendNote() 0 64 8
A userDeletedFromGroup() 0 28 4
B resolveGroupShares() 0 50 6
A propagateNote() 0 8 3
C getAccessList() 0 70 15
B filterSharesOfUser() 0 29 7
B createShare() 0 51 7
A getAllShares() 0 24 3

How to fix   Complexity   

Complex Class

Complex classes like DefaultShareProvider often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use DefaultShareProvider, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Andrius <[email protected]>
6
 * @author Bjoern Schiessle <[email protected]>
7
 * @author Björn Schießle <[email protected]>
8
 * @author Jan-Philipp Litza <[email protected]>
9
 * @author Joas Schilling <[email protected]>
10
 * @author phisch <[email protected]>
11
 * @author Robin Appelman <[email protected]>
12
 * @author Roeland Jago Douma <[email protected]>
13
 * @author Vincent Petry <[email protected]>
14
 *
15
 * @license AGPL-3.0
16
 *
17
 * This code is free software: you can redistribute it and/or modify
18
 * it under the terms of the GNU Affero General Public License, version 3,
19
 * as published by the Free Software Foundation.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License, version 3,
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
28
 *
29
 */
30
namespace OC\Share20;
31
32
use OC\Files\Cache\Cache;
33
use OCP\Defaults;
34
use OCP\Files\Folder;
35
use OCP\IL10N;
36
use OCP\IURLGenerator;
37
use OCP\IUser;
38
use OCP\Mail\IMailer;
39
use OCP\Share\IShare;
40
use OCP\Share\IShareHelper;
41
use OCP\Share\IShareProvider;
42
use OC\Share20\Exception\InvalidShare;
43
use OC\Share20\Exception\ProviderException;
44
use OCP\Share\Exceptions\ShareNotFound;
45
use OC\Share20\Exception\BackendError;
46
use OCP\DB\QueryBuilder\IQueryBuilder;
47
use OCP\IGroup;
48
use OCP\IGroupManager;
49
use OCP\IUserManager;
50
use OCP\Files\IRootFolder;
51
use OCP\IDBConnection;
52
use OCP\Files\Node;
53
54
/**
55
 * Class DefaultShareProvider
56
 *
57
 * @package OC\Share20
58
 */
59
class DefaultShareProvider implements IShareProvider {
60
61
	// Special share type for user modified group shares
62
	const SHARE_TYPE_USERGROUP = 2;
63
64
	/** @var IDBConnection */
65
	private $dbConn;
66
67
	/** @var IUserManager */
68
	private $userManager;
69
70
	/** @var IGroupManager */
71
	private $groupManager;
72
73
	/** @var IRootFolder */
74
	private $rootFolder;
75
76
	/** @var IMailer */
77
	private $mailer;
78
79
	/** @var Defaults */
80
	private $defaults;
81
82
	/** @var IL10N */
83
	private $l;
84
85
	/** @var IURLGenerator */
86
	private $urlGenerator;
87
88
	/**
89
	 * DefaultShareProvider constructor.
90
	 *
91
	 * @param IDBConnection $connection
92
	 * @param IUserManager $userManager
93
	 * @param IGroupManager $groupManager
94
	 * @param IRootFolder $rootFolder
95
	 * @param IMailer $mailer ;
96
	 * @param Defaults $defaults
97
	 * @param IL10N $l
98
	 * @param IURLGenerator $urlGenerator
99
	 */
100
	public function __construct(
101
			IDBConnection $connection,
102
			IUserManager $userManager,
103
			IGroupManager $groupManager,
104
			IRootFolder $rootFolder,
105
			IMailer $mailer,
106
			Defaults $defaults,
107
			IL10N $l,
108
			IURLGenerator $urlGenerator) {
109
		$this->dbConn = $connection;
110
		$this->userManager = $userManager;
111
		$this->groupManager = $groupManager;
112
		$this->rootFolder = $rootFolder;
113
		$this->mailer = $mailer;
114
		$this->defaults = $defaults;
115
		$this->l = $l;
116
		$this->urlGenerator = $urlGenerator;
117
	}
118
119
	/**
120
	 * Return the identifier of this provider.
121
	 *
122
	 * @return string Containing only [a-zA-Z0-9]
123
	 */
124
	public function identifier() {
125
		return 'ocinternal';
126
	}
127
128
	/**
129
	 * Share a path
130
	 *
131
	 * @param \OCP\Share\IShare $share
132
	 * @return \OCP\Share\IShare The share object
133
	 * @throws ShareNotFound
134
	 * @throws \Exception
135
	 */
136
	public function create(\OCP\Share\IShare $share) {
137
		$qb = $this->dbConn->getQueryBuilder();
138
139
		$qb->insert('share');
140
		$qb->setValue('share_type', $qb->createNamedParameter($share->getShareType()));
141
142
		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_USER has been deprecated: 17.0.0 - use IShare::TYPE_USER instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

142
		if ($share->getShareType() === /** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_USER) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
143
			//Set the UID of the user we share with
144
			$qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith()));
145
		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_GROUP has been deprecated: 17.0.0 - use IShare::TYPE_GROUP instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

145
		} else if ($share->getShareType() === /** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_GROUP) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
146
			//Set the GID of the group we share with
147
			$qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith()));
148
		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_LINK has been deprecated: 17.0.0 - use IShare::TYPE_LINK instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

148
		} else if ($share->getShareType() === /** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_LINK) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
149
			//set label for public link
150
			$qb->setValue('label', $qb->createNamedParameter($share->getLabel()));
151
			//Set the token of the share
152
			$qb->setValue('token', $qb->createNamedParameter($share->getToken()));
153
154
			//If a password is set store it
155
			if ($share->getPassword() !== null) {
0 ignored issues
show
introduced by
The condition $share->getPassword() !== null is always true.
Loading history...
156
				$qb->setValue('password', $qb->createNamedParameter($share->getPassword()));
157
			}
158
159
			$qb->setValue('password_by_talk', $qb->createNamedParameter($share->getSendPasswordByTalk(), IQueryBuilder::PARAM_BOOL));
160
161
			//If an expiration date is set store it
162
			if ($share->getExpirationDate() !== null) {
163
				$qb->setValue('expiration', $qb->createNamedParameter($share->getExpirationDate(), 'datetime'));
164
			}
165
166
			if (method_exists($share, 'getParent')) {
167
				$qb->setValue('parent', $qb->createNamedParameter($share->getParent()));
168
			}
169
		} else {
170
			throw new \Exception('invalid share type!');
171
		}
172
173
		// Set what is shares
174
		$qb->setValue('item_type', $qb->createParameter('itemType'));
175
		if ($share->getNode() instanceof \OCP\Files\File) {
176
			$qb->setParameter('itemType', 'file');
177
		} else {
178
			$qb->setParameter('itemType', 'folder');
179
		}
180
181
		// Set the file id
182
		$qb->setValue('item_source', $qb->createNamedParameter($share->getNode()->getId()));
183
		$qb->setValue('file_source', $qb->createNamedParameter($share->getNode()->getId()));
184
185
		// set the permissions
186
		$qb->setValue('permissions', $qb->createNamedParameter($share->getPermissions()));
187
188
		// Set who created this share
189
		$qb->setValue('uid_initiator', $qb->createNamedParameter($share->getSharedBy()));
190
191
		// Set who is the owner of this file/folder (and this the owner of the share)
192
		$qb->setValue('uid_owner', $qb->createNamedParameter($share->getShareOwner()));
193
194
		// Set the file target
195
		$qb->setValue('file_target', $qb->createNamedParameter($share->getTarget()));
196
197
		// Set the time this share was created
198
		$qb->setValue('stime', $qb->createNamedParameter(time()));
199
200
		// insert the data and fetch the id of the share
201
		$this->dbConn->beginTransaction();
202
		$qb->execute();
203
		$id = $this->dbConn->lastInsertId('*PREFIX*share');
204
205
		// Now fetch the inserted share and create a complete share object
206
		$qb = $this->dbConn->getQueryBuilder();
207
		$qb->select('*')
208
			->from('share')
209
			->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
210
211
		$cursor = $qb->execute();
212
		$data = $cursor->fetch();
213
		$this->dbConn->commit();
214
		$cursor->closeCursor();
215
216
		if ($data === false) {
217
			throw new ShareNotFound();
218
		}
219
220
		$mailSendValue = $share->getMailSend();
221
		$data['mail_send'] = ($mailSendValue === null) ? true : $mailSendValue;
0 ignored issues
show
introduced by
The condition $mailSendValue === null is always false.
Loading history...
222
223
		$share = $this->createShare($data);
224
		return $share;
225
	}
226
227
	/**
228
	 * Update a share
229
	 *
230
	 * @param \OCP\Share\IShare $share
231
	 * @return \OCP\Share\IShare The share object
232
	 * @throws ShareNotFound
233
	 * @throws \OCP\Files\InvalidPathException
234
	 * @throws \OCP\Files\NotFoundException
235
	 */
236
	public function update(\OCP\Share\IShare $share) {
237
238
		$originalShare = $this->getShareById($share->getId());
239
240
		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_USER has been deprecated: 17.0.0 - use IShare::TYPE_USER instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

240
		if ($share->getShareType() === /** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_USER) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
241
			/*
242
			 * We allow updating the recipient on user shares.
243
			 */
244
			$qb = $this->dbConn->getQueryBuilder();
245
			$qb->update('share')
246
				->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
247
				->set('share_with', $qb->createNamedParameter($share->getSharedWith()))
248
				->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
249
				->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
250
				->set('permissions', $qb->createNamedParameter($share->getPermissions()))
251
				->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
252
				->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
253
				->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
254
				->set('note', $qb->createNamedParameter($share->getNote()))
255
				->execute();
256
		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_GROUP has been deprecated: 17.0.0 - use IShare::TYPE_GROUP instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

256
		} else if ($share->getShareType() === /** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_GROUP) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
257
			$qb = $this->dbConn->getQueryBuilder();
258
			$qb->update('share')
259
				->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
260
				->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
261
				->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
262
				->set('permissions', $qb->createNamedParameter($share->getPermissions()))
263
				->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
264
				->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
265
				->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
266
				->set('note', $qb->createNamedParameter($share->getNote()))
267
				->execute();
268
269
			/*
270
			 * Update all user defined group shares
271
			 */
272
			$qb = $this->dbConn->getQueryBuilder();
273
			$qb->update('share')
274
				->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
275
				->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
276
				->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
277
				->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
278
				->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
279
				->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
280
				->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
281
				->set('note', $qb->createNamedParameter($share->getNote()))
282
				->execute();
283
284
			/*
285
			 * Now update the permissions for all children that have not set it to 0
286
			 */
287
			$qb = $this->dbConn->getQueryBuilder();
288
			$qb->update('share')
289
				->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
290
				->andWhere($qb->expr()->neq('permissions', $qb->createNamedParameter(0)))
291
				->set('permissions', $qb->createNamedParameter($share->getPermissions()))
292
				->execute();
293
294
		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_LINK has been deprecated: 17.0.0 - use IShare::TYPE_LINK instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

294
		} else if ($share->getShareType() === /** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_LINK) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
295
			$qb = $this->dbConn->getQueryBuilder();
296
			$qb->update('share')
297
				->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
298
				->set('password', $qb->createNamedParameter($share->getPassword()))
299
				->set('password_by_talk', $qb->createNamedParameter($share->getSendPasswordByTalk(), IQueryBuilder::PARAM_BOOL))
300
				->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
301
				->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
302
				->set('permissions', $qb->createNamedParameter($share->getPermissions()))
303
				->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
304
				->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
305
				->set('token', $qb->createNamedParameter($share->getToken()))
306
				->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
307
				->set('note', $qb->createNamedParameter($share->getNote()))
308
				->set('label', $qb->createNamedParameter($share->getLabel()))
309
				->set('hide_download', $qb->createNamedParameter($share->getHideDownload() ? 1 : 0), IQueryBuilder::PARAM_INT)
0 ignored issues
show
Unused Code introduced by
The call to OCP\DB\QueryBuilder\IQueryBuilder::set() has too many arguments starting with OCP\DB\QueryBuilder\IQueryBuilder::PARAM_INT. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

309
				->/** @scrutinizer ignore-call */ set('hide_download', $qb->createNamedParameter($share->getHideDownload() ? 1 : 0), IQueryBuilder::PARAM_INT)

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
310
				->execute();
311
		}
312
313
		if ($originalShare->getNote() !== $share->getNote() && $share->getNote() !== '') {
314
			$this->propagateNote($share);
315
		}
316
317
318
		return $share;
319
	}
320
321
	/**
322
	 * Get all children of this share
323
	 * FIXME: remove once https://github.com/owncloud/core/pull/21660 is in
324
	 *
325
	 * @param \OCP\Share\IShare $parent
326
	 * @return \OCP\Share\IShare[]
327
	 */
328
	public function getChildren(\OCP\Share\IShare $parent) {
329
		$children = [];
330
331
		$qb = $this->dbConn->getQueryBuilder();
332
		$qb->select('*')
333
			->from('share')
334
			->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId())))
335
			->andWhere(
336
				$qb->expr()->in(
337
					'share_type',
338
					$qb->createNamedParameter([
339
						\OCP\Share::SHARE_TYPE_USER,
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_USER has been deprecated: 17.0.0 - use IShare::TYPE_USER instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

339
						/** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_USER,

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
340
						\OCP\Share::SHARE_TYPE_GROUP,
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_GROUP has been deprecated: 17.0.0 - use IShare::TYPE_GROUP instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

340
						/** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_GROUP,

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
341
						\OCP\Share::SHARE_TYPE_LINK,
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_LINK has been deprecated: 17.0.0 - use IShare::TYPE_LINK instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

341
						/** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_LINK,

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
342
					], IQueryBuilder::PARAM_INT_ARRAY)
343
				)
344
			)
345
			->andWhere($qb->expr()->orX(
346
				$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
347
				$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
348
			))
349
			->orderBy('id');
350
351
		$cursor = $qb->execute();
352
		while($data = $cursor->fetch()) {
353
			$children[] = $this->createShare($data);
354
		}
355
		$cursor->closeCursor();
356
357
		return $children;
358
	}
359
360
	/**
361
	 * Delete a share
362
	 *
363
	 * @param \OCP\Share\IShare $share
364
	 */
365
	public function delete(\OCP\Share\IShare $share) {
366
		$qb = $this->dbConn->getQueryBuilder();
367
		$qb->delete('share')
368
			->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())));
369
370
		/*
371
		 * If the share is a group share delete all possible
372
		 * user defined groups shares.
373
		 */
374
		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_GROUP has been deprecated: 17.0.0 - use IShare::TYPE_GROUP instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

374
		if ($share->getShareType() === /** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_GROUP) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
375
			$qb->orWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())));
376
		}
377
378
		$qb->execute();
379
	}
380
381
	/**
382
	 * Unshare a share from the recipient. If this is a group share
383
	 * this means we need a special entry in the share db.
384
	 *
385
	 * @param \OCP\Share\IShare $share
386
	 * @param string $recipient UserId of recipient
387
	 * @throws BackendError
388
	 * @throws ProviderException
389
	 */
390
	public function deleteFromSelf(\OCP\Share\IShare $share, $recipient) {
391
		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_GROUP has been deprecated: 17.0.0 - use IShare::TYPE_GROUP instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

391
		if ($share->getShareType() === /** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_GROUP) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
392
393
			$group = $this->groupManager->get($share->getSharedWith());
394
			$user = $this->userManager->get($recipient);
395
396
			if (is_null($group)) {
397
				throw new ProviderException('Group "' . $share->getSharedWith() . '" does not exist');
398
			}
399
400
			if (!$group->inGroup($user)) {
401
				throw new ProviderException('Recipient not in receiving group');
402
			}
403
404
			// Try to fetch user specific share
405
			$qb = $this->dbConn->getQueryBuilder();
406
			$stmt = $qb->select('*')
407
				->from('share')
408
				->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
409
				->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient)))
410
				->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
411
				->andWhere($qb->expr()->orX(
412
					$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
413
					$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
414
				))
415
				->execute();
416
417
			$data = $stmt->fetch();
418
419
			/*
420
			 * Check if there already is a user specific group share.
421
			 * If there is update it (if required).
422
			 */
423
			if ($data === false) {
424
				$qb = $this->dbConn->getQueryBuilder();
425
426
				$type = $share->getNodeType();
427
428
				//Insert new share
429
				$qb->insert('share')
430
					->values([
431
						'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP),
432
						'share_with' => $qb->createNamedParameter($recipient),
433
						'uid_owner' => $qb->createNamedParameter($share->getShareOwner()),
434
						'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()),
435
						'parent' => $qb->createNamedParameter($share->getId()),
436
						'item_type' => $qb->createNamedParameter($type),
437
						'item_source' => $qb->createNamedParameter($share->getNodeId()),
438
						'file_source' => $qb->createNamedParameter($share->getNodeId()),
439
						'file_target' => $qb->createNamedParameter($share->getTarget()),
440
						'permissions' => $qb->createNamedParameter(0),
441
						'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()),
442
					])->execute();
443
444
			} else if ($data['permissions'] !== 0) {
445
446
				// Update existing usergroup share
447
				$qb = $this->dbConn->getQueryBuilder();
448
				$qb->update('share')
449
					->set('permissions', $qb->createNamedParameter(0))
450
					->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id'])))
451
					->execute();
452
			}
453
454
		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_USER has been deprecated: 17.0.0 - use IShare::TYPE_USER instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

454
		} else if ($share->getShareType() === /** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_USER) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
455
456
			if ($share->getSharedWith() !== $recipient) {
457
				throw new ProviderException('Recipient does not match');
458
			}
459
460
			// We can just delete user and link shares
461
			$this->delete($share);
462
		} else {
463
			throw new ProviderException('Invalid shareType');
464
		}
465
	}
466
467
	/**
468
	 * @inheritdoc
469
	 *
470
	 * For now this only works for group shares
471
	 * If this gets implemented for normal shares we have to extend it
472
	 */
473
	public function restore(IShare $share, string $recipient): IShare {
474
		$qb = $this->dbConn->getQueryBuilder();
475
		$qb->select('permissions')
476
			->from('share')
477
			->where(
478
				$qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))
479
			);
480
		$cursor = $qb->execute();
481
		$data = $cursor->fetch();
482
		$cursor->closeCursor();
483
484
		$originalPermission = $data['permissions'];
485
486
		$qb = $this->dbConn->getQueryBuilder();
487
		$qb->update('share')
488
			->set('permissions', $qb->createNamedParameter($originalPermission))
489
			->where(
490
				$qb->expr()->eq('parent', $qb->createNamedParameter($share->getParent()))
0 ignored issues
show
Bug introduced by
The method getParent() does not exist on OCP\Share\IShare. Since it exists in all sub-types, consider adding an abstract or default implementation to OCP\Share\IShare. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

490
				$qb->expr()->eq('parent', $qb->createNamedParameter($share->/** @scrutinizer ignore-call */ getParent()))
Loading history...
491
			)->andWhere(
492
				$qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))
493
			)->andWhere(
494
				$qb->expr()->eq('share_with', $qb->createNamedParameter($recipient))
495
			);
496
497
		$qb->execute();
498
499
		return $this->getShareById($share->getId(), $recipient);
500
	}
501
502
	/**
503
	 * @inheritdoc
504
	 */
505
	public function move(\OCP\Share\IShare $share, $recipient) {
506
		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_USER has been deprecated: 17.0.0 - use IShare::TYPE_USER instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

506
		if ($share->getShareType() === /** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_USER) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
507
			// Just update the target
508
			$qb = $this->dbConn->getQueryBuilder();
509
			$qb->update('share')
510
				->set('file_target', $qb->createNamedParameter($share->getTarget()))
511
				->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
512
				->execute();
513
514
		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_GROUP has been deprecated: 17.0.0 - use IShare::TYPE_GROUP instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

514
		} else if ($share->getShareType() === /** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_GROUP) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
515
516
			// Check if there is a usergroup share
517
			$qb = $this->dbConn->getQueryBuilder();
518
			$stmt = $qb->select('id')
519
				->from('share')
520
				->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
521
				->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient)))
522
				->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
523
				->andWhere($qb->expr()->orX(
524
					$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
525
					$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
526
				))
527
				->setMaxResults(1)
528
				->execute();
529
530
			$data = $stmt->fetch();
531
			$stmt->closeCursor();
532
533
			if ($data === false) {
534
				// No usergroup share yet. Create one.
535
				$qb = $this->dbConn->getQueryBuilder();
536
				$qb->insert('share')
537
					->values([
538
						'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP),
539
						'share_with' => $qb->createNamedParameter($recipient),
540
						'uid_owner' => $qb->createNamedParameter($share->getShareOwner()),
541
						'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()),
542
						'parent' => $qb->createNamedParameter($share->getId()),
543
						'item_type' => $qb->createNamedParameter($share->getNodeType()),
544
						'item_source' => $qb->createNamedParameter($share->getNodeId()),
545
						'file_source' => $qb->createNamedParameter($share->getNodeId()),
546
						'file_target' => $qb->createNamedParameter($share->getTarget()),
547
						'permissions' => $qb->createNamedParameter($share->getPermissions()),
548
						'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()),
549
					])->execute();
550
			} else {
551
				// Already a usergroup share. Update it.
552
				$qb = $this->dbConn->getQueryBuilder();
553
				$qb->update('share')
554
					->set('file_target', $qb->createNamedParameter($share->getTarget()))
555
					->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id'])))
556
					->execute();
557
			}
558
		}
559
560
		return $share;
561
	}
562
563
	public function getSharesInFolder($userId, Folder $node, $reshares) {
564
		$qb = $this->dbConn->getQueryBuilder();
565
		$qb->select('*')
566
			->from('share', 's')
567
			->andWhere($qb->expr()->orX(
568
				$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
569
				$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
570
			));
571
572
		$qb->andWhere($qb->expr()->orX(
573
			$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)),
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_USER has been deprecated: 17.0.0 - use IShare::TYPE_USER instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

573
			$qb->expr()->eq('share_type', $qb->createNamedParameter(/** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_USER)),

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
574
			$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)),
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_GROUP has been deprecated: 17.0.0 - use IShare::TYPE_GROUP instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

574
			$qb->expr()->eq('share_type', $qb->createNamedParameter(/** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_GROUP)),

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
575
			$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK))
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_LINK has been deprecated: 17.0.0 - use IShare::TYPE_LINK instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

575
			$qb->expr()->eq('share_type', $qb->createNamedParameter(/** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_LINK))

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
576
		));
577
578
		/**
579
		 * Reshares for this user are shares where they are the owner.
580
		 */
581
		if ($reshares === false) {
582
			$qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
583
		} else {
584
			$qb->andWhere(
585
				$qb->expr()->orX(
586
					$qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
587
					$qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
588
				)
589
			);
590
		}
591
592
		$qb->innerJoin('s', 'filecache' ,'f', $qb->expr()->eq('s.file_source', 'f.fileid'));
593
		$qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())));
594
595
		$qb->orderBy('id');
596
597
		$cursor = $qb->execute();
598
		$shares = [];
599
		while ($data = $cursor->fetch()) {
600
			$shares[$data['fileid']][] = $this->createShare($data);
601
		}
602
		$cursor->closeCursor();
603
604
		return $shares;
605
	}
606
607
	/**
608
	 * @inheritdoc
609
	 */
610
	public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) {
611
		$qb = $this->dbConn->getQueryBuilder();
612
		$qb->select('*')
613
			->from('share')
614
			->andWhere($qb->expr()->orX(
615
				$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
616
				$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
617
			));
618
619
		$qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter($shareType)));
620
621
		/**
622
		 * Reshares for this user are shares where they are the owner.
623
		 */
624
		if ($reshares === false) {
625
			$qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
626
		} else {
627
			if ($node === null) {
628
				$qb->andWhere(
629
					$qb->expr()->orX(
630
						$qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
631
						$qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
632
					)
633
				);
634
			}
635
		}
636
637
		if ($node !== null) {
638
			$qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
639
		}
640
641
		if ($limit !== -1) {
642
			$qb->setMaxResults($limit);
643
		}
644
645
		$qb->setFirstResult($offset);
646
		$qb->orderBy('id');
647
648
		$cursor = $qb->execute();
649
		$shares = [];
650
		while($data = $cursor->fetch()) {
651
			$shares[] = $this->createShare($data);
652
		}
653
		$cursor->closeCursor();
654
655
		return $shares;
656
	}
657
658
	/**
659
	 * @inheritdoc
660
	 */
661
	public function getShareById($id, $recipientId = null) {
662
		$qb = $this->dbConn->getQueryBuilder();
663
664
		$qb->select('*')
665
			->from('share')
666
			->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
667
			->andWhere(
668
				$qb->expr()->in(
669
					'share_type',
670
					$qb->createNamedParameter([
671
						\OCP\Share::SHARE_TYPE_USER,
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_USER has been deprecated: 17.0.0 - use IShare::TYPE_USER instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

671
						/** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_USER,

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
672
						\OCP\Share::SHARE_TYPE_GROUP,
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_GROUP has been deprecated: 17.0.0 - use IShare::TYPE_GROUP instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

672
						/** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_GROUP,

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
673
						\OCP\Share::SHARE_TYPE_LINK,
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_LINK has been deprecated: 17.0.0 - use IShare::TYPE_LINK instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

673
						/** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_LINK,

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
674
					], IQueryBuilder::PARAM_INT_ARRAY)
675
				)
676
			)
677
			->andWhere($qb->expr()->orX(
678
				$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
679
				$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
680
			));
681
682
		$cursor = $qb->execute();
683
		$data = $cursor->fetch();
684
		$cursor->closeCursor();
685
686
		if ($data === false) {
687
			throw new ShareNotFound();
688
		}
689
690
		try {
691
			$share = $this->createShare($data);
692
		} catch (InvalidShare $e) {
693
			throw new ShareNotFound();
694
		}
695
696
		// If the recipient is set for a group share resolve to that user
697
		if ($recipientId !== null && $share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_GROUP has been deprecated: 17.0.0 - use IShare::TYPE_GROUP instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

697
		if ($recipientId !== null && $share->getShareType() === /** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_GROUP) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
698
			$share = $this->resolveGroupShares([$share], $recipientId)[0];
699
		}
700
701
		return $share;
702
	}
703
704
	/**
705
	 * Get shares for a given path
706
	 *
707
	 * @param \OCP\Files\Node $path
708
	 * @return \OCP\Share\IShare[]
709
	 */
710
	public function getSharesByPath(Node $path) {
711
		$qb = $this->dbConn->getQueryBuilder();
712
713
		$cursor = $qb->select('*')
714
			->from('share')
715
			->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId())))
716
			->andWhere(
717
				$qb->expr()->orX(
718
					$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)),
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_USER has been deprecated: 17.0.0 - use IShare::TYPE_USER instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

718
					$qb->expr()->eq('share_type', $qb->createNamedParameter(/** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_USER)),

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
719
					$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_GROUP has been deprecated: 17.0.0 - use IShare::TYPE_GROUP instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

719
					$qb->expr()->eq('share_type', $qb->createNamedParameter(/** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_GROUP))

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
720
				)
721
			)
722
			->andWhere($qb->expr()->orX(
723
				$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
724
				$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
725
			))
726
			->execute();
727
728
		$shares = [];
729
		while($data = $cursor->fetch()) {
730
			$shares[] = $this->createShare($data);
731
		}
732
		$cursor->closeCursor();
733
734
		return $shares;
735
	}
736
737
	/**
738
	 * Returns whether the given database result can be interpreted as
739
	 * a share with accessible file (not trashed, not deleted)
740
	 */
741
	private function isAccessibleResult($data) {
742
		// exclude shares leading to deleted file entries
743
		if ($data['fileid'] === null) {
744
			return false;
745
		}
746
747
		// exclude shares leading to trashbin on home storages
748
		$pathSections = explode('/', $data['path'], 2);
749
		// FIXME: would not detect rare md5'd home storage case properly
750
		if ($pathSections[0] !== 'files'
751
		    	&& in_array(explode(':', $data['storage_string_id'], 2)[0], array('home', 'object'))) {
752
			return false;
753
		}
754
		return true;
755
	}
756
757
	/**
758
	 * @inheritdoc
759
	 */
760
	public function getSharedWith($userId, $shareType, $node, $limit, $offset) {
761
		/** @var Share[] $shares */
762
		$shares = [];
763
764
		if ($shareType === \OCP\Share::SHARE_TYPE_USER) {
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_USER has been deprecated: 17.0.0 - use IShare::TYPE_USER instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

764
		if ($shareType === /** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_USER) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
765
			//Get shares directly with this user
766
			$qb = $this->dbConn->getQueryBuilder();
767
			$qb->select('s.*',
768
				'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash',
769
				'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime',
770
				'f.encrypted', 'f.unencrypted_size', 'f.etag', 'f.checksum'
771
			)
772
				->selectAlias('st.id', 'storage_string_id')
773
				->from('share', 's')
774
				->leftJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid'))
775
				->leftJoin('f', 'storages', 'st', $qb->expr()->eq('f.storage', 'st.numeric_id'));
776
777
			// Order by id
778
			$qb->orderBy('s.id');
779
780
			// Set limit and offset
781
			if ($limit !== -1) {
782
				$qb->setMaxResults($limit);
783
			}
784
			$qb->setFirstResult($offset);
785
786
			$qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)))
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_USER has been deprecated: 17.0.0 - use IShare::TYPE_USER instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

786
			$qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(/** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_USER)))

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
787
				->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)))
788
				->andWhere($qb->expr()->orX(
789
					$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
790
					$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
791
				));
792
793
			// Filter by node if provided
794
			if ($node !== null) {
795
				$qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
796
			}
797
798
			$cursor = $qb->execute();
799
800
			while($data = $cursor->fetch()) {
801
				if ($this->isAccessibleResult($data)) {
802
					$shares[] = $this->createShare($data);
803
				}
804
			}
805
			$cursor->closeCursor();
806
807
		} else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_GROUP has been deprecated: 17.0.0 - use IShare::TYPE_GROUP instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

807
		} else if ($shareType === /** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_GROUP) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
808
			$user = $this->userManager->get($userId);
809
			$allGroups = $this->groupManager->getUserGroups($user);
810
811
			/** @var Share[] $shares2 */
812
			$shares2 = [];
813
814
			$start = 0;
815
			while(true) {
816
				$groups = array_slice($allGroups, $start, 100);
817
				$start += 100;
818
819
				if ($groups === []) {
820
					break;
821
				}
822
823
				$qb = $this->dbConn->getQueryBuilder();
824
				$qb->select('s.*',
825
					'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash',
826
					'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime',
827
					'f.encrypted', 'f.unencrypted_size', 'f.etag', 'f.checksum'
828
				)
829
					->selectAlias('st.id', 'storage_string_id')
830
					->from('share', 's')
831
					->leftJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid'))
832
					->leftJoin('f', 'storages', 'st', $qb->expr()->eq('f.storage', 'st.numeric_id'))
833
					->orderBy('s.id')
834
					->setFirstResult(0);
835
836
				if ($limit !== -1) {
837
					$qb->setMaxResults($limit - count($shares));
838
				}
839
840
				// Filter by node if provided
841
				if ($node !== null) {
842
					$qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
843
				}
844
845
846
				$groups = array_filter($groups, function($group) { return $group instanceof IGroup; });
847
				$groups = array_map(function(IGroup $group) { return $group->getGID(); }, $groups);
848
849
				$qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)))
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_GROUP has been deprecated: 17.0.0 - use IShare::TYPE_GROUP instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

849
				$qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(/** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_GROUP)))

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
850
					->andWhere($qb->expr()->in('share_with', $qb->createNamedParameter(
851
						$groups,
852
						IQueryBuilder::PARAM_STR_ARRAY
853
					)))
854
					->andWhere($qb->expr()->orX(
855
						$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
856
						$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
857
					));
858
859
				$cursor = $qb->execute();
860
				while($data = $cursor->fetch()) {
861
					if ($offset > 0) {
862
						$offset--;
863
						continue;
864
					}
865
866
					if ($this->isAccessibleResult($data)) {
867
						$shares2[] = $this->createShare($data);
868
					}
869
				}
870
				$cursor->closeCursor();
871
			}
872
873
			/*
874
 			 * Resolve all group shares to user specific shares
875
 			 */
876
			$shares = $this->resolveGroupShares($shares2, $userId);
877
		} else {
878
			throw new BackendError('Invalid backend');
879
		}
880
881
882
		return $shares;
883
	}
884
885
	/**
886
	 * Get a share by token
887
	 *
888
	 * @param string $token
889
	 * @return \OCP\Share\IShare
890
	 * @throws ShareNotFound
891
	 */
892
	public function getShareByToken($token) {
893
		$qb = $this->dbConn->getQueryBuilder();
894
895
		$cursor = $qb->select('*')
896
			->from('share')
897
			->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK)))
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_LINK has been deprecated: 17.0.0 - use IShare::TYPE_LINK instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

897
			->where($qb->expr()->eq('share_type', $qb->createNamedParameter(/** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_LINK)))

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
898
			->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token)))
899
			->andWhere($qb->expr()->orX(
900
				$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
901
				$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
902
			))
903
			->execute();
904
905
		$data = $cursor->fetch();
906
907
		if ($data === false) {
908
			throw new ShareNotFound();
909
		}
910
911
		try {
912
			$share = $this->createShare($data);
913
		} catch (InvalidShare $e) {
914
			throw new ShareNotFound();
915
		}
916
917
		return $share;
918
	}
919
920
	/**
921
	 * Create a share object from an database row
922
	 *
923
	 * @param mixed[] $data
924
	 * @return \OCP\Share\IShare
925
	 * @throws InvalidShare
926
	 */
927
	private function createShare($data) {
928
		$share = new Share($this->rootFolder, $this->userManager);
929
		$share->setId((int)$data['id'])
930
			->setShareType((int)$data['share_type'])
931
			->setPermissions((int)$data['permissions'])
932
			->setTarget($data['file_target'])
933
			->setNote($data['note'])
934
			->setMailSend((bool)$data['mail_send'])
935
			->setLabel($data['label']);
936
937
		$shareTime = new \DateTime();
938
		$shareTime->setTimestamp((int)$data['stime']);
939
		$share->setShareTime($shareTime);
940
941
		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_USER has been deprecated: 17.0.0 - use IShare::TYPE_USER instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

941
		if ($share->getShareType() === /** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_USER) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
942
			$share->setSharedWith($data['share_with']);
943
			$user = $this->userManager->get($data['share_with']);
944
			if ($user !== null) {
945
				$share->setSharedWithDisplayName($user->getDisplayName());
946
			}
947
		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_GROUP has been deprecated: 17.0.0 - use IShare::TYPE_GROUP instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

947
		} else if ($share->getShareType() === /** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_GROUP) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
948
			$share->setSharedWith($data['share_with']);
949
		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_LINK has been deprecated: 17.0.0 - use IShare::TYPE_LINK instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

949
		} else if ($share->getShareType() === /** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_LINK) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
950
			$share->setPassword($data['password']);
951
			$share->setSendPasswordByTalk((bool)$data['password_by_talk']);
952
			$share->setToken($data['token']);
953
		}
954
955
		$share->setSharedBy($data['uid_initiator']);
956
		$share->setShareOwner($data['uid_owner']);
957
958
		$share->setNodeId((int)$data['file_source']);
959
		$share->setNodeType($data['item_type']);
960
961
		if ($data['expiration'] !== null) {
962
			$expiration = \DateTime::createFromFormat('Y-m-d H:i:s', $data['expiration']);
963
			$share->setExpirationDate($expiration);
964
		}
965
966
		if (isset($data['f_permissions'])) {
967
			$entryData = $data;
968
			$entryData['permissions'] = $entryData['f_permissions'];
969
			$entryData['parent'] = $entryData['f_parent'];
970
			$share->setNodeCacheEntry(Cache::cacheEntryFromData($entryData,
971
				\OC::$server->getMimeTypeLoader()));
972
		}
973
974
		$share->setProviderId($this->identifier());
975
		$share->setHideDownload((int)$data['hide_download'] === 1);
976
977
		return $share;
978
	}
979
980
	/**
981
	 * @param Share[] $shares
982
	 * @param $userId
983
	 * @return Share[] The updates shares if no update is found for a share return the original
984
	 */
985
	private function resolveGroupShares($shares, $userId) {
986
		$result = [];
987
988
		$start = 0;
989
		while(true) {
990
			/** @var Share[] $shareSlice */
991
			$shareSlice = array_slice($shares, $start, 100);
992
			$start += 100;
993
994
			if ($shareSlice === []) {
995
				break;
996
			}
997
998
			/** @var int[] $ids */
999
			$ids = [];
1000
			/** @var Share[] $shareMap */
1001
			$shareMap = [];
1002
1003
			foreach ($shareSlice as $share) {
1004
				$ids[] = (int)$share->getId();
1005
				$shareMap[$share->getId()] = $share;
1006
			}
1007
1008
			$qb = $this->dbConn->getQueryBuilder();
1009
1010
			$query = $qb->select('*')
1011
				->from('share')
1012
				->where($qb->expr()->in('parent', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
1013
				->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)))
1014
				->andWhere($qb->expr()->orX(
1015
					$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
1016
					$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
1017
				));
1018
1019
			$stmt = $query->execute();
1020
1021
			while($data = $stmt->fetch()) {
1022
				$shareMap[$data['parent']]->setPermissions((int)$data['permissions']);
1023
				$shareMap[$data['parent']]->setTarget($data['file_target']);
1024
				$shareMap[$data['parent']]->setParent($data['parent']);
1025
			}
1026
1027
			$stmt->closeCursor();
1028
1029
			foreach ($shareMap as $share) {
1030
				$result[] = $share;
1031
			}
1032
		}
1033
1034
		return $result;
1035
	}
1036
1037
	/**
1038
	 * A user is deleted from the system
1039
	 * So clean up the relevant shares.
1040
	 *
1041
	 * @param string $uid
1042
	 * @param int $shareType
1043
	 */
1044
	public function userDeleted($uid, $shareType) {
1045
		$qb = $this->dbConn->getQueryBuilder();
1046
1047
		$qb->delete('share');
1048
1049
		if ($shareType === \OCP\Share::SHARE_TYPE_USER) {
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_USER has been deprecated: 17.0.0 - use IShare::TYPE_USER instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1049
		if ($shareType === /** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_USER) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
1050
			/*
1051
			 * Delete all user shares that are owned by this user
1052
			 * or that are received by this user
1053
			 */
1054
1055
			$qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)));
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_USER has been deprecated: 17.0.0 - use IShare::TYPE_USER instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1055
			$qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(/** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_USER)));

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
1056
1057
			$qb->andWhere(
1058
				$qb->expr()->orX(
1059
					$qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)),
1060
					$qb->expr()->eq('share_with', $qb->createNamedParameter($uid))
1061
				)
1062
			);
1063
		} else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_GROUP has been deprecated: 17.0.0 - use IShare::TYPE_GROUP instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1063
		} else if ($shareType === /** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_GROUP) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
1064
			/*
1065
			 * Delete all group shares that are owned by this user
1066
			 * Or special user group shares that are received by this user
1067
			 */
1068
			$qb->where(
1069
				$qb->expr()->andX(
1070
					$qb->expr()->orX(
1071
						$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)),
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_GROUP has been deprecated: 17.0.0 - use IShare::TYPE_GROUP instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1071
						$qb->expr()->eq('share_type', $qb->createNamedParameter(/** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_GROUP)),

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
1072
						$qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))
1073
					),
1074
					$qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid))
1075
				)
1076
			);
1077
1078
			$qb->orWhere(
1079
				$qb->expr()->andX(
1080
					$qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)),
1081
					$qb->expr()->eq('share_with', $qb->createNamedParameter($uid))
1082
				)
1083
			);
1084
		} else if ($shareType === \OCP\Share::SHARE_TYPE_LINK) {
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_LINK has been deprecated: 17.0.0 - use IShare::TYPE_LINK instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1084
		} else if ($shareType === /** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_LINK) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
1085
			/*
1086
			 * Delete all link shares owned by this user.
1087
			 * And all link shares initiated by this user (until #22327 is in)
1088
			 */
1089
			$qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK)));
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_LINK has been deprecated: 17.0.0 - use IShare::TYPE_LINK instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1089
			$qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(/** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_LINK)));

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
1090
1091
			$qb->andWhere(
1092
				$qb->expr()->orX(
1093
					$qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)),
1094
					$qb->expr()->eq('uid_initiator', $qb->createNamedParameter($uid))
1095
				)
1096
			);
1097
		}
1098
1099
		$qb->execute();
1100
	}
1101
1102
	/**
1103
	 * Delete all shares received by this group. As well as any custom group
1104
	 * shares for group members.
1105
	 *
1106
	 * @param string $gid
1107
	 */
1108
	public function groupDeleted($gid) {
1109
		/*
1110
		 * First delete all custom group shares for group members
1111
		 */
1112
		$qb = $this->dbConn->getQueryBuilder();
1113
		$qb->select('id')
1114
			->from('share')
1115
			->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)))
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_GROUP has been deprecated: 17.0.0 - use IShare::TYPE_GROUP instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1115
			->where($qb->expr()->eq('share_type', $qb->createNamedParameter(/** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_GROUP)))

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
1116
			->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid)));
1117
1118
		$cursor = $qb->execute();
1119
		$ids = [];
1120
		while($row = $cursor->fetch()) {
1121
			$ids[] = (int)$row['id'];
1122
		}
1123
		$cursor->closeCursor();
1124
1125
		if (!empty($ids)) {
1126
			$chunks = array_chunk($ids, 100);
1127
			foreach ($chunks as $chunk) {
1128
				$qb->delete('share')
1129
					->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
1130
					->andWhere($qb->expr()->in('parent', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY)));
1131
				$qb->execute();
1132
			}
1133
		}
1134
1135
		/*
1136
		 * Now delete all the group shares
1137
		 */
1138
		$qb = $this->dbConn->getQueryBuilder();
1139
		$qb->delete('share')
1140
			->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)))
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_GROUP has been deprecated: 17.0.0 - use IShare::TYPE_GROUP instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1140
			->where($qb->expr()->eq('share_type', $qb->createNamedParameter(/** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_GROUP)))

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
1141
			->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid)));
1142
		$qb->execute();
1143
	}
1144
1145
	/**
1146
	 * Delete custom group shares to this group for this user
1147
	 *
1148
	 * @param string $uid
1149
	 * @param string $gid
1150
	 */
1151
	public function userDeletedFromGroup($uid, $gid) {
1152
		/*
1153
		 * Get all group shares
1154
		 */
1155
		$qb = $this->dbConn->getQueryBuilder();
1156
		$qb->select('id')
1157
			->from('share')
1158
			->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)))
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_GROUP has been deprecated: 17.0.0 - use IShare::TYPE_GROUP instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1158
			->where($qb->expr()->eq('share_type', $qb->createNamedParameter(/** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_GROUP)))

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
1159
			->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid)));
1160
1161
		$cursor = $qb->execute();
1162
		$ids = [];
1163
		while($row = $cursor->fetch()) {
1164
			$ids[] = (int)$row['id'];
1165
		}
1166
		$cursor->closeCursor();
1167
1168
		if (!empty($ids)) {
1169
			$chunks = array_chunk($ids, 100);
1170
			foreach ($chunks as $chunk) {
1171
				/*
1172
				 * Delete all special shares wit this users for the found group shares
1173
				 */
1174
				$qb->delete('share')
1175
					->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
1176
					->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($uid)))
1177
					->andWhere($qb->expr()->in('parent', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY)));
1178
				$qb->execute();
1179
			}
1180
		}
1181
	}
1182
1183
	/**
1184
	 * @inheritdoc
1185
	 */
1186
	public function getAccessList($nodes, $currentAccess) {
1187
		$ids = [];
1188
		foreach ($nodes as $node) {
1189
			$ids[] = $node->getId();
1190
		}
1191
1192
		$qb = $this->dbConn->getQueryBuilder();
1193
1194
		$or = $qb->expr()->orX(
1195
			$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)),
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_USER has been deprecated: 17.0.0 - use IShare::TYPE_USER instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1195
			$qb->expr()->eq('share_type', $qb->createNamedParameter(/** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_USER)),

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
1196
			$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)),
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_GROUP has been deprecated: 17.0.0 - use IShare::TYPE_GROUP instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1196
			$qb->expr()->eq('share_type', $qb->createNamedParameter(/** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_GROUP)),

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
1197
			$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK))
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_LINK has been deprecated: 17.0.0 - use IShare::TYPE_LINK instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1197
			$qb->expr()->eq('share_type', $qb->createNamedParameter(/** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_LINK))

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
1198
		);
1199
1200
		if ($currentAccess) {
1201
			$or->add($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)));
1202
		}
1203
1204
		$qb->select('id', 'parent', 'share_type', 'share_with', 'file_source', 'file_target', 'permissions')
1205
			->from('share')
1206
			->where(
1207
				$or
1208
			)
1209
			->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
1210
			->andWhere($qb->expr()->orX(
1211
				$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
1212
				$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
1213
			));
1214
		$cursor = $qb->execute();
1215
1216
		$users = [];
1217
		$link = false;
1218
		while($row = $cursor->fetch()) {
1219
			$type = (int)$row['share_type'];
1220
			if ($type === \OCP\Share::SHARE_TYPE_USER) {
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_USER has been deprecated: 17.0.0 - use IShare::TYPE_USER instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1220
			if ($type === /** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_USER) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
1221
				$uid = $row['share_with'];
1222
				$users[$uid] = isset($users[$uid]) ? $users[$uid] : [];
1223
				$users[$uid][$row['id']] = $row;
1224
			} else if ($type === \OCP\Share::SHARE_TYPE_GROUP) {
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_GROUP has been deprecated: 17.0.0 - use IShare::TYPE_GROUP instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1224
			} else if ($type === /** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_GROUP) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
1225
				$gid = $row['share_with'];
1226
				$group = $this->groupManager->get($gid);
1227
1228
				if ($group === null) {
1229
					continue;
1230
				}
1231
1232
				$userList = $group->getUsers();
1233
				foreach ($userList as $user) {
1234
					$uid = $user->getUID();
1235
					$users[$uid] = isset($users[$uid]) ? $users[$uid] : [];
1236
					$users[$uid][$row['id']] = $row;
1237
				}
1238
			} else if ($type === \OCP\Share::SHARE_TYPE_LINK) {
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_LINK has been deprecated: 17.0.0 - use IShare::TYPE_LINK instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1238
			} else if ($type === /** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_LINK) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
1239
				$link = true;
1240
			} else if ($type === self::SHARE_TYPE_USERGROUP && $currentAccess === true) {
1241
				$uid = $row['share_with'];
1242
				$users[$uid] = isset($users[$uid]) ? $users[$uid] : [];
1243
				$users[$uid][$row['id']] = $row;
1244
			}
1245
		}
1246
		$cursor->closeCursor();
1247
1248
		if ($currentAccess === true) {
1249
			$users = array_map([$this, 'filterSharesOfUser'], $users);
1250
			$users = array_filter($users);
1251
		} else {
1252
			$users = array_keys($users);
1253
		}
1254
1255
		return ['users' => $users, 'public' => $link];
1256
	}
1257
1258
	/**
1259
	 * For each user the path with the fewest slashes is returned
1260
	 * @param array $shares
1261
	 * @return array
1262
	 */
1263
	protected function filterSharesOfUser(array $shares) {
1264
		// Group shares when the user has a share exception
1265
		foreach ($shares as $id => $share) {
1266
			$type = (int) $share['share_type'];
1267
			$permissions = (int) $share['permissions'];
1268
1269
			if ($type === self::SHARE_TYPE_USERGROUP) {
1270
				unset($shares[$share['parent']]);
1271
1272
				if ($permissions === 0) {
1273
					unset($shares[$id]);
1274
				}
1275
			}
1276
		}
1277
1278
		$best = [];
1279
		$bestDepth = 0;
1280
		foreach ($shares as $id => $share) {
1281
			$depth = substr_count($share['file_target'], '/');
1282
			if (empty($best) || $depth < $bestDepth) {
1283
				$bestDepth = $depth;
1284
				$best = [
1285
					'node_id' => $share['file_source'],
1286
					'node_path' => $share['file_target'],
1287
				];
1288
			}
1289
		}
1290
1291
		return $best;
1292
	}
1293
1294
	/**
1295
	 * propagate notes to the recipients
1296
	 *
1297
	 * @param IShare $share
1298
	 * @throws \OCP\Files\NotFoundException
1299
	 */
1300
	private function propagateNote(IShare $share) {
1301
		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_USER has been deprecated: 17.0.0 - use IShare::TYPE_USER instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1301
		if ($share->getShareType() === /** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_USER) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
1302
			$user = $this->userManager->get($share->getSharedWith());
1303
			$this->sendNote([$user], $share);
1304
		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
0 ignored issues
show
Deprecated Code introduced by
The constant OC\Share\Constants::SHARE_TYPE_GROUP has been deprecated: 17.0.0 - use IShare::TYPE_GROUP instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1304
		} else if ($share->getShareType() === /** @scrutinizer ignore-deprecated */ \OCP\Share::SHARE_TYPE_GROUP) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
1305
			$group = $this->groupManager->get($share->getSharedWith());
1306
			$groupMembers = $group->getUsers();
1307
			$this->sendNote($groupMembers, $share);
1308
		}
1309
	}
1310
1311
	/**
1312
	 * send note by mail
1313
	 *
1314
	 * @param array $recipients
1315
	 * @param IShare $share
1316
	 * @throws \OCP\Files\NotFoundException
1317
	 */
1318
	private function sendNote(array $recipients, IShare $share) {
1319
1320
		$toList = [];
1321
1322
		foreach ($recipients as $recipient) {
1323
			/** @var IUser $recipient */
1324
			$email = $recipient->getEMailAddress();
1325
			if ($email) {
1326
				$toList[$email] = $recipient->getDisplayName();
1327
			}
1328
		}
1329
1330
		if (!empty($toList)) {
1331
1332
			$filename = $share->getNode()->getName();
1333
			$initiator = $share->getSharedBy();
1334
			$note = $share->getNote();
1335
1336
			$initiatorUser = $this->userManager->get($initiator);
1337
			$initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
1338
			$initiatorEmailAddress = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null;
1339
			$plainHeading = $this->l->t('%1$s shared »%2$s« with you and wants to add:', [$initiatorDisplayName, $filename]);
1340
			$htmlHeading = $this->l->t('%1$s shared »%2$s« with you and wants to add', [$initiatorDisplayName, $filename]);
1341
			$message = $this->mailer->createMessage();
1342
1343
			$emailTemplate = $this->mailer->createEMailTemplate('defaultShareProvider.sendNote');
1344
1345
			$emailTemplate->setSubject($this->l->t('»%s« added a note to a file shared with you', [$initiatorDisplayName]));
1346
			$emailTemplate->addHeader();
1347
			$emailTemplate->addHeading($htmlHeading, $plainHeading);
1348
			$emailTemplate->addBodyText(htmlspecialchars($note), $note);
1349
1350
			$link = $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $share->getNode()->getId()]);
1351
			$emailTemplate->addBodyButton(
1352
				$this->l->t('Open »%s«', [$filename]),
1353
				$link
1354
			);
1355
1356
1357
			// The "From" contains the sharers name
1358
			$instanceName = $this->defaults->getName();
1359
			$senderName = $this->l->t(
1360
				'%1$s via %2$s',
1361
				[
1362
					$initiatorDisplayName,
1363
					$instanceName
1364
				]
1365
			);
1366
			$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
1367
			if ($initiatorEmailAddress !== null) {
1368
				$message->setReplyTo([$initiatorEmailAddress => $initiatorDisplayName]);
1369
				$emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
1370
			} else {
1371
				$emailTemplate->addFooter();
1372
			}
1373
1374
			if (count($toList) === 1) {
1375
				$message->setTo($toList);
1376
			} else {
1377
				$message->setTo([]);
1378
				$message->setBcc($toList);
1379
			}
1380
			$message->useTemplate($emailTemplate);
1381
			$this->mailer->send($message);
1382
		}
1383
1384
	}
1385
1386
	public function getAllShares(): iterable {
1387
		$qb = $this->dbConn->getQueryBuilder();
1388
1389
		$qb->select('*')
1390
			->from('share')
1391
			->where(
1392
				$qb->expr()->orX(
1393
					$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_USER)),
1394
					$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_GROUP)),
1395
					$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_LINK))
1396
				)
1397
			);
1398
1399
		$cursor = $qb->execute();
1400
		while($data = $cursor->fetch()) {
1401
			try {
1402
				$share = $this->createShare($data);
1403
			} catch (InvalidShare $e) {
1404
				continue;
1405
			}
1406
1407
			yield $share;
1408
		}
1409
		$cursor->closeCursor();
1410
	}
1411
}
1412