Completed
Push — master ( 736366...88c351 )
by Joas
02:53
created

FilesHooks::shareFileOrFolderWithGroup()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 4.0406

Importance

Changes 6
Bugs 0 Features 3
Metric Value
c 6
b 0
f 3
dl 0
loc 29
ccs 19
cts 22
cp 0.8636
rs 8.5806
cc 4
eloc 20
nc 6
nop 6
crap 4.0406
1
<?php
2
3
/**
4
 * ownCloud - Activities App
5
 *
6
 * @author Frank Karlitschek, Joas Schilling
7
 * @copyright 2013 Frank Karlitschek [email protected]
8
 *
9
 * This library is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
11
 * License as published by the Free Software Foundation; either
12
 * version 3 of the License, or any later version.
13
 *
14
 * This library is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public
20
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
namespace OCA\Activity;
24
25
use OC\Files\Filesystem;
26
use OC\Files\View;
27
use OCA\Activity\Extension\Files;
28
use OCA\Activity\Extension\Files_Sharing;
29
use OCP\Activity\IManager;
30
use OCP\Files\Mount\IMountPoint;
31
use OCP\Files\NotFoundException;
32
use OCP\IDBConnection;
33
use OCP\IGroup;
34
use OCP\IGroupManager;
35
use OCP\IUser;
36
use OCP\Share;
37
use OCP\Util;
38
39
/**
40
 * The class to handle the filesystem hooks
41
 */
42
class FilesHooks {
43
	const USER_BATCH_SIZE = 50;
44
45
	/** @var \OCP\Activity\IManager */
46
	protected $manager;
47
48
	/** @var \OCA\Activity\Data */
49
	protected $activityData;
50
51
	/** @var \OCA\Activity\UserSettings */
52
	protected $userSettings;
53
54
	/** @var \OCP\IGroupManager */
55
	protected $groupManager;
56
57
	/** @var \OCP\IDBConnection */
58
	protected $connection;
59
60
	/** @var \OC\Files\View */
61
	protected $view;
62
63
	/** @var string|false */
64
	protected $currentUser;
65
66
	/**
67
	 * Constructor
68
	 *
69
	 * @param IManager $manager
70
	 * @param Data $activityData
71
	 * @param UserSettings $userSettings
72
	 * @param IGroupManager $groupManager
73
	 * @param View $view
74
	 * @param IDBConnection $connection
75
	 * @param string|false $currentUser
76
	 */
77 47
	public function __construct(IManager $manager, Data $activityData, UserSettings $userSettings, IGroupManager $groupManager, View $view, IDBConnection $connection, $currentUser) {
78 47
		$this->manager = $manager;
79 47
		$this->activityData = $activityData;
80 47
		$this->userSettings = $userSettings;
81 47
		$this->groupManager = $groupManager;
82 47
		$this->view = $view;
83 47
		$this->connection = $connection;
84 47
		$this->currentUser = $currentUser;
85 47
	}
86
87
	/**
88
	 * @return string|false Current UserID if logged in, false otherwise
89
	 */
90 2
	protected function getCurrentUser() {
91 2
		return $this->currentUser;
92
	}
93
94
	/**
95
	 * Store the create hook events
96
	 * @param string $path Path of the file that has been created
97
	 */
98 2
	public function fileCreate($path) {
99 2
		if ($this->getCurrentUser() !== false) {
100 1
			$this->addNotificationsForFileAction($path, Files::TYPE_SHARE_CREATED, 'created_self', 'created_by');
101 1
		} else {
102 1
			$this->addNotificationsForFileAction($path, Files::TYPE_SHARE_CREATED, '', 'created_public');
103
		}
104 2
	}
105
106
	/**
107
	 * Store the update hook events
108
	 * @param string $path Path of the file that has been modified
109
	 */
110 1
	public function fileUpdate($path) {
111 1
		$this->addNotificationsForFileAction($path, Files::TYPE_SHARE_CHANGED, 'changed_self', 'changed_by');
112 1
	}
113
114
	/**
115
	 * Store the delete hook events
116
	 * @param string $path Path of the file that has been deleted
117
	 */
118 1
	public function fileDelete($path) {
119 1
		$this->addNotificationsForFileAction($path, Files::TYPE_SHARE_DELETED, 'deleted_self', 'deleted_by');
120 1
	}
121
122
	/**
123
	 * Store the restore hook events
124
	 * @param string $path Path of the file that has been restored
125
	 */
126 1
	public function fileRestore($path) {
127 1
		$this->addNotificationsForFileAction($path, Files::TYPE_SHARE_RESTORED, 'restored_self', 'restored_by');
128 1
	}
129
130
	/**
131
	 * Creates the entries for file actions on $file_path
132
	 *
133
	 * @param string $filePath         The file that is being changed
134
	 * @param int    $activityType     The activity type
135
	 * @param string $subject          The subject for the actor
136
	 * @param string $subjectBy        The subject for other users (with "by $actor")
137
	 */
138 3
	protected function addNotificationsForFileAction($filePath, $activityType, $subject, $subjectBy) {
139
		// Do not add activities for .part-files
140 3
		if (substr($filePath, -5) === '.part') {
141 1
			return;
142
		}
143
144 2
		list($filePath, $uidOwner, $fileId) = $this->getSourcePathAndOwner($filePath);
145 2
		$affectedUsers = $this->getUserPathsFromPath($filePath, $uidOwner);
146 2
		$filteredStreamUsers = $this->userSettings->filterUsersBySetting(array_keys($affectedUsers), 'stream', $activityType);
147 2
		$filteredEmailUsers = $this->userSettings->filterUsersBySetting(array_keys($affectedUsers), 'email', $activityType);
148
149 2
		foreach ($affectedUsers as $user => $path) {
150 2
			if (empty($filteredStreamUsers[$user]) && empty($filteredEmailUsers[$user])) {
151 2
				continue;
152
			}
153
154 2
			if ($user === $this->currentUser) {
155 1
				$userSubject = $subject;
156 1
				$userParams = [[$fileId => $path]];
157 1
			} else {
158 1
				$userSubject = $subjectBy;
159 1
				$userParams = [[$fileId => $path], $this->currentUser];
160
			}
161
162 2
			$this->addNotificationsForUser(
163 2
				$user, $userSubject, $userParams,
164 2
				$fileId, $path, true,
165 2
				!empty($filteredStreamUsers[$user]),
166 2
				!empty($filteredEmailUsers[$user]) ? $filteredEmailUsers[$user] : 0,
167
				$activityType
168 2
			);
169 2
		}
170 2
	}
171
172
	/**
173
	 * Returns a "username => path" map for all affected users
174
	 *
175
	 * @param string $path
176
	 * @param string $uidOwner
177
	 * @return array
178
	 */
179
	protected function getUserPathsFromPath($path, $uidOwner) {
180
		return Share::getUsersSharingFile($path, $uidOwner, true, true);
181
	}
182
183
	/**
184
	 * Return the source
185
	 *
186
	 * @param string $path
187
	 * @return array
188
	 */
189
	protected function getSourcePathAndOwner($path) {
190
		$uidOwner = Filesystem::getOwner($path);
191
		$fileId = 0;
192
193
		if ($uidOwner !== $this->currentUser) {
194
			Filesystem::initMountPoints($uidOwner);
195
		}
196
		$info = Filesystem::getFileInfo($path);
197
		if ($info !== false) {
198
			$ownerView = new View('/' . $uidOwner . '/files');
199
			$fileId = (int) $info['fileid'];
200
			$path = $ownerView->getPath($fileId);
201
		}
202
203
		return array($path, $uidOwner, $fileId);
204
	}
205
206
	/**
207
	 * Manage sharing events
208
	 * @param array $params The hook params
209
	 */
210 3 View Code Duplication
	public function share($params) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
211 3
		if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') {
212 3
			if ((int) $params['shareType'] === Share::SHARE_TYPE_USER) {
213 1
				$this->shareFileOrFolderWithUser($params['shareWith'], (int) $params['fileSource'], $params['itemType'], $params['fileTarget'], true);
214 3
			} else if ((int) $params['shareType'] === Share::SHARE_TYPE_GROUP) {
215 1
				$this->shareFileOrFolderWithGroup($params['shareWith'], (int) $params['fileSource'], $params['itemType'], $params['fileTarget'], (int) $params['id'], true);
216 2
			} else if ((int) $params['shareType'] === Share::SHARE_TYPE_LINK) {
217 1
				$this->shareFileOrFolderByLink((int) $params['fileSource'], $params['itemType'], $params['uidOwner'], true);
218 1
			}
219 3
		}
220 3
	}
221
222
	/**
223
	 * Manage sharing events
224
	 * @param array $params The hook params
225
	 */
226 View Code Duplication
	public function unShare($params) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
227
		if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') {
228
			if ((int) $params['shareType'] === Share::SHARE_TYPE_USER) {
229
				$this->shareFileOrFolderWithUser($params['shareWith'], (int) $params['fileSource'], $params['itemType'], $params['fileTarget'], false);
230
			} else if ((int) $params['shareType'] === Share::SHARE_TYPE_GROUP) {
231
				$this->shareFileOrFolderWithGroup($params['shareWith'], (int) $params['fileSource'], $params['itemType'], $params['fileTarget'], (int) $params['id'], false);
232
			} else if ((int) $params['shareType'] === Share::SHARE_TYPE_LINK) {
233
				$this->shareFileOrFolderByLink((int) $params['fileSource'], $params['itemType'], $params['uidOwner'], false);
234
			}
235
		}
236
	}
237
238
	/**
239
	 * Sharing a file or folder with a user
240
	 *
241
	 * @param string $shareWith
242
	 * @param int $fileSource File ID that is being shared
243
	 * @param string $itemType File type that is being shared (file or folder)
244
	 * @param string $fileTarget File path
245
	 * @param bool $isSharing True if sharing, false if unsharing
246
	 */
247 2
	protected function shareFileOrFolderWithUser($shareWith, $fileSource, $itemType, $fileTarget, $isSharing) {
248 2
		if ($isSharing) {
249 2
			$actionSharer = 'shared_user_self';
250 2
			$actionOwner = 'reshared_user_by';
251 2
			$actionUser = 'shared_with_by';
252 2
		} else {
253
			$actionSharer = 'unshared_user_self';
254
			$actionOwner = 'unshared_user_by';
255
			$actionUser = 'unshared_by';
256
		}
257
258
		// User performing the share
259 2
		$this->shareNotificationForSharer($actionSharer, $shareWith, $fileSource, $itemType);
260 2
		$this->shareNotificationForOriginalOwners($this->currentUser, $actionOwner, $shareWith, $fileSource, $itemType);
0 ignored issues
show
Security Bug introduced by
It seems like $this->currentUser can also be of type false; however, OCA\Activity\FilesHooks:...tionForOriginalOwners() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
261
262
		// New shared user
263 2
		$this->addNotificationsForUser(
264 2
			$shareWith, $actionUser, [[$fileSource => $fileTarget], $this->currentUser],
265 2
			(int) $fileSource, $fileTarget, ($itemType === 'file'),
266 2
			$this->userSettings->getUserSetting($shareWith, 'stream', Files_Sharing::TYPE_SHARED),
267 2
			$this->userSettings->getUserSetting($shareWith, 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($shareWith, 'setting', 'batchtime') : 0
268 2
		);
269 2
	}
270
271
	/**
272
	 * Sharing a file or folder with a group
273
	 *
274
	 * @param string $shareWith
275
	 * @param int $fileSource File ID that is being shared
276
	 * @param string $itemType File type that is being shared (file or folder)
277
	 * @param string $fileTarget File path
278
	 * @param int $shareId The Share ID of this share
279
	 * @param bool $isSharing True if sharing, false if unsharing
280
	 */
281 6
	protected function shareFileOrFolderWithGroup($shareWith, $fileSource, $itemType, $fileTarget, $shareId, $isSharing) {
282 6
		if ($isSharing) {
283 6
			$actionSharer = 'shared_group_self';
284 6
			$actionOwner = 'reshared_group_by';
285 6
			$actionUser = 'shared_with_by';
286 6
		} else {
287
			$actionSharer = 'unshared_group_self';
288
			$actionOwner = 'unshared_group_by';
289
			$actionUser = 'unshared_by';
290
		}
291
292
		// Members of the new group
293 6
		$group = $this->groupManager->get($shareWith);
294 6
		if (!($group instanceof IGroup)) {
0 ignored issues
show
Bug introduced by
The class OCP\IGroup does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
295 1
			return;
296
		}
297
298
		// User performing the share
299 5
		$this->shareNotificationForSharer($actionSharer, $shareWith, $fileSource, $itemType);
300 5
		$this->shareNotificationForOriginalOwners($this->currentUser, $actionOwner, $shareWith, $fileSource, $itemType);
0 ignored issues
show
Security Bug introduced by
It seems like $this->currentUser can also be of type false; however, OCA\Activity\FilesHooks:...tionForOriginalOwners() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
301
302 5
		$offset = 0;
303 5
		$users = $group->searchUsers('', self::USER_BATCH_SIZE, $offset);
304 5
		while (!empty($users)) {
305 4
			$this->addNotificationsForGroupUsers($users, $actionUser, $fileSource, $itemType, $fileTarget, $shareId);
306 4
			$offset += self::USER_BATCH_SIZE;
307 4
			$users = $group->searchUsers('', self::USER_BATCH_SIZE, $offset);
308 4
		}
309 5
	}
310
311
	/**
312
	 * @param IUser[] $usersInGroup
313
	 * @param string $actionUser
314
	 * @param int $fileSource File ID that is being shared
315
	 * @param string $itemType File type that is being shared (file or folder)
316
	 * @param string $fileTarget File path
317
	 * @param int $shareId The Share ID of this share
318
	 */
319 4
	protected function addNotificationsForGroupUsers(array $usersInGroup, $actionUser, $fileSource, $itemType, $fileTarget, $shareId) {
320 4
		$affectedUsers = [];
321
322 4
		foreach ($usersInGroup as $user) {
323 4
			$affectedUsers[$user->getUID()] = $fileTarget;
324 4
		}
325
326
		// Remove the triggering user, we already managed his notifications
327 4
		unset($affectedUsers[$this->currentUser]);
328
329 4
		if (empty($affectedUsers)) {
330 1
			return;
331
		}
332
333 3
		$userIds = array_keys($affectedUsers);
334 3
		$filteredStreamUsersInGroup = $this->userSettings->filterUsersBySetting($userIds, 'stream', Files_Sharing::TYPE_SHARED);
335 3
		$filteredEmailUsersInGroup = $this->userSettings->filterUsersBySetting($userIds, 'email', Files_Sharing::TYPE_SHARED);
336
337 3
		$affectedUsers = $this->fixPathsForShareExceptions($affectedUsers, $shareId);
338 3
		foreach ($affectedUsers as $user => $path) {
339 3
			if (empty($filteredStreamUsersInGroup[$user]) && empty($filteredEmailUsersInGroup[$user])) {
340 2
				continue;
341
			}
342
343 1
			$this->addNotificationsForUser(
344 1
				$user, $actionUser, [[$fileSource => $path], $this->currentUser],
345 1
				$fileSource, $path, ($itemType === 'file'),
346 1
				!empty($filteredStreamUsersInGroup[$user]),
347 1
				!empty($filteredEmailUsersInGroup[$user]) ? $filteredEmailUsersInGroup[$user] : 0
348 1
			);
349 3
		}
350 3
	}
351
352
	/**
353
	 * Check when there was a naming conflict and the target is different
354
	 * for some of the users
355
	 *
356
	 * @param array $affectedUsers
357
	 * @param int $shareId
358
	 * @return mixed
359
	 */
360
	protected function fixPathsForShareExceptions(array $affectedUsers, $shareId) {
361
		$queryBuilder = $this->connection->getQueryBuilder();
362
		$queryBuilder->select(['share_with', 'file_target'])
363
			->from('share')
364
			->where($queryBuilder->expr()->eq('parent', $queryBuilder->createParameter('parent')))
365
			->setParameter('parent', (int) $shareId);
366
		$query = $queryBuilder->execute();
367
368
		while ($row = $query->fetch()) {
369
			$affectedUsers[$row['share_with']] = $row['file_target'];
370
		}
371
372
		return $affectedUsers;
373
	}
374
375
	/**
376
	 * Sharing a file or folder via link/public
377
	 *
378
	 * @param int $fileSource File ID that is being shared
379
	 * @param string $itemType File type that is being shared (file or folder)
380
	 * @param string $linkOwner
381
	 * @param bool $isSharing True if sharing, false if unsharing
382
	 */
383 2
	protected function shareFileOrFolderByLink($fileSource, $itemType, $linkOwner, $isSharing) {
384 2
		if ($isSharing) {
385 2
			$actionSharer = 'shared_link_self';
386 2
			$actionOwner = 'reshared_link_by';
387 2
		} else if ($this->currentUser !== $linkOwner) {
388
			// Link expired
389
			$actionSharer = 'link_expired';
390
			$actionOwner = 'link_by_expired';
391
			$this->currentUser = $linkOwner;
392
			\OC::$server->getUserFolder($linkOwner);
393
		} else {
394
			$actionSharer = 'unshared_link_self';
395
			$actionOwner = 'unshared_link_by';
396
		}
397
398 2
		$this->view->chroot('/' . $this->currentUser . '/files');
399
400
		try {
401 2
			$path = $this->view->getPath($fileSource);
402 2
		} catch (NotFoundException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\NotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
403 1
			return;
404
		}
405
406 1
		$this->shareNotificationForOriginalOwners($this->currentUser, $actionOwner, '', $fileSource, $itemType);
0 ignored issues
show
Security Bug introduced by
It seems like $this->currentUser can also be of type false; however, OCA\Activity\FilesHooks:...tionForOriginalOwners() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
407
408 1
		$this->addNotificationsForUser(
409 1
			$this->currentUser, $actionSharer, [[$fileSource => $path]],
0 ignored issues
show
Security Bug introduced by
It seems like $this->currentUser can also be of type false; however, OCA\Activity\FilesHooks::addNotificationsForUser() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
410 1
			(int) $fileSource, $path, ($itemType === 'file'),
411 1
			$this->userSettings->getUserSetting($this->currentUser, 'stream', Files_Sharing::TYPE_SHARED),
0 ignored issues
show
Security Bug introduced by
It seems like $this->currentUser can also be of type false; however, OCA\Activity\UserSettings::getUserSetting() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
412 1
			$this->userSettings->getUserSetting($this->currentUser, 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($this->currentUser, 'setting', 'batchtime') : 0
0 ignored issues
show
Security Bug introduced by
It seems like $this->currentUser can also be of type false; however, OCA\Activity\UserSettings::getUserSetting() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
413 1
		);
414 1
	}
415
416
	/**
417
	 * Add notifications for the user that shares a file/folder
418
	 *
419
	 * @param string $subject
420
	 * @param string $shareWith
421
	 * @param int $fileSource
422
	 * @param string $itemType
423
	 */
424 2
	protected function shareNotificationForSharer($subject, $shareWith, $fileSource, $itemType) {
425 2
		$this->view->chroot('/' . $this->currentUser . '/files');
426
427
		try {
428 2
			$path = $this->view->getPath($fileSource);
429 2
		} catch (NotFoundException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\NotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
430 1
			return;
431
		}
432
433 1
		$this->addNotificationsForUser(
434 1
			$this->currentUser, $subject, [[$fileSource => $path], $shareWith],
0 ignored issues
show
Security Bug introduced by
It seems like $this->currentUser can also be of type false; however, OCA\Activity\FilesHooks::addNotificationsForUser() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
435 1
			$fileSource, $path, ($itemType === 'file'),
436 1
			$this->userSettings->getUserSetting($this->currentUser, 'stream', Files_Sharing::TYPE_SHARED),
0 ignored issues
show
Security Bug introduced by
It seems like $this->currentUser can also be of type false; however, OCA\Activity\UserSettings::getUserSetting() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
437 1
			$this->userSettings->getUserSetting($this->currentUser, 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($this->currentUser, 'setting', 'batchtime') : 0
0 ignored issues
show
Security Bug introduced by
It seems like $this->currentUser can also be of type false; however, OCA\Activity\UserSettings::getUserSetting() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
438 1
		);
439 1
	}
440
441
	/**
442
	 * Add notifications for the user that shares a file/folder
443
	 *
444
	 * @param string $owner
445
	 * @param string $subject
446
	 * @param string $shareWith
447
	 * @param int $fileSource
448
	 * @param string $itemType
449
	 */
450 2
	protected function reshareNotificationForSharer($owner, $subject, $shareWith, $fileSource, $itemType) {
451 2
		$this->view->chroot('/' . $owner . '/files');
452
453
		try {
454 2
			$path = $this->view->getPath($fileSource);
455 2
		} catch (NotFoundException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\NotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
456 1
			return;
457
		}
458
459 1
		$this->addNotificationsForUser(
460 1
			$owner, $subject, [[$fileSource => $path], $this->currentUser, $shareWith],
461 1
			$fileSource, $path, ($itemType === 'file'),
462 1
			$this->userSettings->getUserSetting($owner, 'stream', Files_Sharing::TYPE_SHARED),
463 1
			$this->userSettings->getUserSetting($owner, 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($owner, 'setting', 'batchtime') : 0
464 1
		);
465 1
	}
466
467
	/**
468
	 * Add notifications for the owners whose files have been reshared
469
	 *
470
	 * @param string $currentOwner
471
	 * @param string $subject
472
	 * @param string $shareWith
473
	 * @param int $fileSource
474
	 * @param string $itemType
475
	 */
476 10
	protected function shareNotificationForOriginalOwners($currentOwner, $subject, $shareWith, $fileSource, $itemType) {
477
		// Get the full path of the current user
478 10
		$this->view->chroot('/' . $currentOwner . '/files');
479
480
		try {
481 10
			$path = $this->view->getPath($fileSource);
482 10
		} catch (NotFoundException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\NotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
483 1
			return;
484
		}
485
486
		/**
487
		 * Get the original owner and his path
488
		 */
489 9
		$owner = $this->view->getOwner($path);
490 9
		if ($owner !== $currentOwner) {
491 7
			$this->reshareNotificationForSharer($owner, $subject, $shareWith, $fileSource, $itemType);
492 7
		}
493
494
		/**
495
		 * Get the sharee who shared the item with the currentUser
496
		 */
497 9
		$this->view->chroot('/' . $currentOwner . '/files');
498 9
		$mount = $this->view->getMount($path);
499 9
		if (!($mount instanceof IMountPoint)) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\Mount\IMountPoint does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
500 1
			return;
501
		}
502
503 8
		$storage = $mount->getStorage();
504 8
		if (!$storage->instanceOfStorage('OC\Files\Storage\Shared')) {
505 1
			return;
506
		}
507
508
		/** @var \OC\Files\Storage\Shared $storage */
509 7
		$shareOwner = $storage->getSharedFrom();
510 7
		if ($shareOwner === '' || $shareOwner === null || $shareOwner === $owner || $shareOwner === $currentOwner) {
511 5
			return;
512
		}
513
514 2
		$this->reshareNotificationForSharer($shareOwner, $subject, $shareWith, $fileSource, $itemType);
515 2
	}
516
517
	/**
518
	 * Adds the activity and email for a user when the settings require it
519
	 *
520
	 * @param string $user
521
	 * @param string $subject
522
	 * @param array $subjectParams
523
	 * @param int $fileId
524
	 * @param string $path
525
	 * @param bool $isFile If the item is a file, we link to the parent directory
526
	 * @param bool $streamSetting
527
	 * @param int $emailSetting
528
	 * @param string $type
529
	 */
530 9
	protected function addNotificationsForUser($user, $subject, $subjectParams, $fileId, $path, $isFile, $streamSetting, $emailSetting, $type = Files_Sharing::TYPE_SHARED) {
531 9
		if (!$streamSetting && !$emailSetting) {
532 1
			return;
533
		}
534
535 8
		$selfAction = $user === $this->currentUser;
536 8
		$app = $type === Files_Sharing::TYPE_SHARED ? 'files_sharing' : 'files';
537 8
		$link = Util::linkToAbsolute('files', 'index.php', array(
538 8
			'dir' => ($isFile) ? dirname($path) : $path,
539 8
		));
540
541 8
		$objectType = ($fileId) ? 'files' : '';
542
543 8
		$event = $this->manager->generateEvent();
544 8
		$event->setApp($app)
545 8
			->setType($type)
546 8
			->setAffectedUser($user)
547 8
			->setAuthor($this->currentUser)
548 8
			->setTimestamp(time())
549 8
			->setSubject($subject, $subjectParams)
550 8
			->setObject($objectType, $fileId, $path)
551 8
			->setLink($link);
552
553
		// Add activity to stream
554 8
		if ($streamSetting && (!$selfAction || $this->userSettings->getUserSetting($this->currentUser, 'setting', 'self'))) {
555 3
			$this->activityData->send($event);
556 3
		}
557
558
		// Add activity to mail queue
559 8
		if ($emailSetting && (!$selfAction || $this->userSettings->getUserSetting($this->currentUser, 'setting', 'selfemail'))) {
560 3
			$latestSend = time() + $emailSetting;
561 3
			$this->activityData->storeMail($event, $latestSend);
562 3
		}
563 8
	}
564
}
565