Completed
Pull Request — master (#8062)
by Morris
77:41 queued 61:37
created
lib/private/Share/Share.php 3 patches
Unused Use Statements   -4 removed lines patch added patch discarded remove patch
@@ -41,10 +41,6 @@
 block discarded – undo
41 41
 
42 42
 use OC\Files\Filesystem;
43 43
 use OCP\DB\QueryBuilder\IQueryBuilder;
44
-use OCP\ILogger;
45
-use OCP\IUserManager;
46
-use OCP\IUserSession;
47
-use OCP\IDBConnection;
48 44
 use OCP\IConfig;
49 45
 use OCP\Util;
50 46
 use OCP\Share;
Please login to merge, or discard this patch.
Indentation   +2073 added lines, -2073 removed lines patch added patch discarded remove patch
@@ -58,2087 +58,2087 @@
 block discarded – undo
58 58
  */
59 59
 class Share extends Constants {
60 60
 
61
-	/** CRUDS permissions (Create, Read, Update, Delete, Share) using a bitmask
62
-	 * Construct permissions for share() and setPermissions with Or (|) e.g.
63
-	 * Give user read and update permissions: PERMISSION_READ | PERMISSION_UPDATE
64
-	 *
65
-	 * Check if permission is granted with And (&) e.g. Check if delete is
66
-	 * granted: if ($permissions & PERMISSION_DELETE)
67
-	 *
68
-	 * Remove permissions with And (&) and Not (~) e.g. Remove the update
69
-	 * permission: $permissions &= ~PERMISSION_UPDATE
70
-	 *
71
-	 * Apps are required to handle permissions on their own, this class only
72
-	 * stores and manages the permissions of shares
73
-	 * @see lib/public/constants.php
74
-	 */
75
-
76
-	/**
77
-	 * Register a sharing backend class that implements OCP\Share_Backend for an item type
78
-	 * @param string $itemType Item type
79
-	 * @param string $class Backend class
80
-	 * @param string $collectionOf (optional) Depends on item type
81
-	 * @param array $supportedFileExtensions (optional) List of supported file extensions if this item type depends on files
82
-	 * @return boolean true if backend is registered or false if error
83
-	 */
84
-	public static function registerBackend($itemType, $class, $collectionOf = null, $supportedFileExtensions = null) {
85
-		if (\OC::$server->getConfig()->getAppValue('core', 'shareapi_enabled', 'yes') == 'yes') {
86
-			if (!isset(self::$backendTypes[$itemType])) {
87
-				self::$backendTypes[$itemType] = array(
88
-					'class' => $class,
89
-					'collectionOf' => $collectionOf,
90
-					'supportedFileExtensions' => $supportedFileExtensions
91
-				);
92
-				if(count(self::$backendTypes) === 1) {
93
-					Util::addScript('core', 'merged-share-backend');
94
-					\OC_Util::addStyle('core', 'share');
95
-				}
96
-				return true;
97
-			}
98
-			\OCP\Util::writeLog(Share::class,
99
-				'Sharing backend '.$class.' not registered, '.self::$backendTypes[$itemType]['class']
100
-				.' is already registered for '.$itemType,
101
-				\OCP\Util::WARN);
102
-		}
103
-		return false;
104
-	}
105
-
106
-	/**
107
-	 * Get the items of item type shared with the current user
108
-	 * @param string $itemType
109
-	 * @param int $format (optional) Format type must be defined by the backend
110
-	 * @param mixed $parameters (optional)
111
-	 * @param int $limit Number of items to return (optional) Returns all by default
112
-	 * @param boolean $includeCollections (optional)
113
-	 * @return mixed Return depends on format
114
-	 */
115
-	public static function getItemsSharedWith($itemType, $format = self::FORMAT_NONE,
116
-											  $parameters = null, $limit = -1, $includeCollections = false) {
117
-		return self::getItems($itemType, null, self::$shareTypeUserAndGroups, \OC_User::getUser(), null, $format,
118
-			$parameters, $limit, $includeCollections);
119
-	}
120
-
121
-	/**
122
-	 * Get the items of item type shared with a user
123
-	 * @param string $itemType
124
-	 * @param string $user id for which user we want the shares
125
-	 * @param int $format (optional) Format type must be defined by the backend
126
-	 * @param mixed $parameters (optional)
127
-	 * @param int $limit Number of items to return (optional) Returns all by default
128
-	 * @param boolean $includeCollections (optional)
129
-	 * @return mixed Return depends on format
130
-	 */
131
-	public static function getItemsSharedWithUser($itemType, $user, $format = self::FORMAT_NONE,
132
-												  $parameters = null, $limit = -1, $includeCollections = false) {
133
-		return self::getItems($itemType, null, self::$shareTypeUserAndGroups, $user, null, $format,
134
-			$parameters, $limit, $includeCollections);
135
-	}
136
-
137
-	/**
138
-	 * Get the item of item type shared with a given user by source
139
-	 * @param string $itemType
140
-	 * @param string $itemSource
141
-	 * @param string $user User to whom the item was shared
142
-	 * @param string $owner Owner of the share
143
-	 * @param int $shareType only look for a specific share type
144
-	 * @return array Return list of items with file_target, permissions and expiration
145
-	 */
146
-	public static function getItemSharedWithUser($itemType, $itemSource, $user, $owner = null, $shareType = null) {
147
-		$shares = array();
148
-		$fileDependent = false;
149
-
150
-		$where = 'WHERE';
151
-		$fileDependentWhere = '';
152
-		if ($itemType === 'file' || $itemType === 'folder') {
153
-			$fileDependent = true;
154
-			$column = 'file_source';
155
-			$fileDependentWhere = 'INNER JOIN `*PREFIX*filecache` ON `file_source` = `*PREFIX*filecache`.`fileid` ';
156
-			$fileDependentWhere .= 'INNER JOIN `*PREFIX*storages` ON `numeric_id` = `*PREFIX*filecache`.`storage` ';
157
-		} else {
158
-			$column = 'item_source';
159
-		}
160
-
161
-		$select = self::createSelectStatement(self::FORMAT_NONE, $fileDependent);
162
-
163
-		$where .= ' `' . $column . '` = ? AND `item_type` = ? ';
164
-		$arguments = array($itemSource, $itemType);
165
-		// for link shares $user === null
166
-		if ($user !== null) {
167
-			$where .= ' AND `share_with` = ? ';
168
-			$arguments[] = $user;
169
-		}
170
-
171
-		if ($shareType !== null) {
172
-			$where .= ' AND `share_type` = ? ';
173
-			$arguments[] = $shareType;
174
-		}
175
-
176
-		if ($owner !== null) {
177
-			$where .= ' AND `uid_owner` = ? ';
178
-			$arguments[] = $owner;
179
-		}
180
-
181
-		$query = \OC_DB::prepare('SELECT ' . $select . ' FROM `*PREFIX*share` '. $fileDependentWhere . $where);
182
-
183
-		$result = \OC_DB::executeAudited($query, $arguments);
184
-
185
-		while ($row = $result->fetchRow()) {
186
-			if ($fileDependent && !self::isFileReachable($row['path'], $row['storage_id'])) {
187
-				continue;
188
-			}
189
-			if ($fileDependent && (int)$row['file_parent'] === -1) {
190
-				// if it is a mount point we need to get the path from the mount manager
191
-				$mountManager = \OC\Files\Filesystem::getMountManager();
192
-				$mountPoint = $mountManager->findByStorageId($row['storage_id']);
193
-				if (!empty($mountPoint)) {
194
-					$path = $mountPoint[0]->getMountPoint();
195
-					$path = trim($path, '/');
196
-					$path = substr($path, strlen($owner) + 1); //normalize path to 'files/foo.txt`
197
-					$row['path'] = $path;
198
-				} else {
199
-					\OC::$server->getLogger()->warning(
200
-						'Could not resolve mount point for ' . $row['storage_id'],
201
-						['app' => Share::class]
202
-					);
203
-				}
204
-			}
205
-			$shares[] = $row;
206
-		}
207
-
208
-		//if didn't found a result than let's look for a group share.
209
-		if(empty($shares) && $user !== null) {
210
-			$userObject = \OC::$server->getUserManager()->get($user);
211
-			$groups = [];
212
-			if ($userObject) {
213
-				$groups = \OC::$server->getGroupManager()->getUserGroupIds($userObject);
214
-			}
215
-
216
-			if (!empty($groups)) {
217
-				$where = $fileDependentWhere . ' WHERE `' . $column . '` = ? AND `item_type` = ? AND `share_with` in (?)';
218
-				$arguments = array($itemSource, $itemType, $groups);
219
-				$types = array(null, null, IQueryBuilder::PARAM_STR_ARRAY);
220
-
221
-				if ($owner !== null) {
222
-					$where .= ' AND `uid_owner` = ?';
223
-					$arguments[] = $owner;
224
-					$types[] = null;
225
-				}
226
-
227
-				// TODO: inject connection, hopefully one day in the future when this
228
-				// class isn't static anymore...
229
-				$conn = \OC::$server->getDatabaseConnection();
230
-				$result = $conn->executeQuery(
231
-					'SELECT ' . $select . ' FROM `*PREFIX*share` ' . $where,
232
-					$arguments,
233
-					$types
234
-				);
235
-
236
-				while ($row = $result->fetch()) {
237
-					$shares[] = $row;
238
-				}
239
-			}
240
-		}
241
-
242
-		return $shares;
243
-
244
-	}
245
-
246
-	/**
247
-	 * Get the item of item type shared with the current user by source
248
-	 * @param string $itemType
249
-	 * @param string $itemSource
250
-	 * @param int $format (optional) Format type must be defined by the backend
251
-	 * @param mixed $parameters
252
-	 * @param boolean $includeCollections
253
-	 * @param string $shareWith (optional) define against which user should be checked, default: current user
254
-	 * @return array
255
-	 */
256
-	public static function getItemSharedWithBySource($itemType, $itemSource, $format = self::FORMAT_NONE,
257
-													 $parameters = null, $includeCollections = false, $shareWith = null) {
258
-		$shareWith = ($shareWith === null) ? \OC_User::getUser() : $shareWith;
259
-		return self::getItems($itemType, $itemSource, self::$shareTypeUserAndGroups, $shareWith, null, $format,
260
-			$parameters, 1, $includeCollections, true);
261
-	}
262
-
263
-	/**
264
-	 * Based on the given token the share information will be returned - password protected shares will be verified
265
-	 * @param string $token
266
-	 * @param bool $checkPasswordProtection
267
-	 * @return array|boolean false will be returned in case the token is unknown or unauthorized
268
-	 */
269
-	public static function getShareByToken($token, $checkPasswordProtection = true) {
270
-		$query = \OC_DB::prepare('SELECT * FROM `*PREFIX*share` WHERE `token` = ?', 1);
271
-		$result = $query->execute(array($token));
272
-		if ($result === false) {
273
-			\OCP\Util::writeLog(Share::class, \OC_DB::getErrorMessage() . ', token=' . $token, \OCP\Util::ERROR);
274
-		}
275
-		$row = $result->fetchRow();
276
-		if ($row === false) {
277
-			return false;
278
-		}
279
-		if (is_array($row) and self::expireItem($row)) {
280
-			return false;
281
-		}
282
-
283
-		// password protected shares need to be authenticated
284
-		if ($checkPasswordProtection && !\OC\Share\Share::checkPasswordProtectedShare($row)) {
285
-			return false;
286
-		}
287
-
288
-		return $row;
289
-	}
290
-
291
-	/**
292
-	 * resolves reshares down to the last real share
293
-	 * @param array $linkItem
294
-	 * @return array file owner
295
-	 */
296
-	public static function resolveReShare($linkItem)
297
-	{
298
-		if (isset($linkItem['parent'])) {
299
-			$parent = $linkItem['parent'];
300
-			while (isset($parent)) {
301
-				$query = \OC_DB::prepare('SELECT * FROM `*PREFIX*share` WHERE `id` = ?', 1);
302
-				$item = $query->execute(array($parent))->fetchRow();
303
-				if (isset($item['parent'])) {
304
-					$parent = $item['parent'];
305
-				} else {
306
-					return $item;
307
-				}
308
-			}
309
-		}
310
-		return $linkItem;
311
-	}
312
-
313
-
314
-	/**
315
-	 * Get the shared items of item type owned by the current user
316
-	 * @param string $itemType
317
-	 * @param int $format (optional) Format type must be defined by the backend
318
-	 * @param mixed $parameters
319
-	 * @param int $limit Number of items to return (optional) Returns all by default
320
-	 * @param boolean $includeCollections
321
-	 * @return mixed Return depends on format
322
-	 */
323
-	public static function getItemsShared($itemType, $format = self::FORMAT_NONE, $parameters = null,
324
-										  $limit = -1, $includeCollections = false) {
325
-		return self::getItems($itemType, null, null, null, \OC_User::getUser(), $format,
326
-			$parameters, $limit, $includeCollections);
327
-	}
328
-
329
-	/**
330
-	 * Get the shared item of item type owned by the current user
331
-	 * @param string $itemType
332
-	 * @param string $itemSource
333
-	 * @param int $format (optional) Format type must be defined by the backend
334
-	 * @param mixed $parameters
335
-	 * @param boolean $includeCollections
336
-	 * @return mixed Return depends on format
337
-	 */
338
-	public static function getItemShared($itemType, $itemSource, $format = self::FORMAT_NONE,
339
-										 $parameters = null, $includeCollections = false) {
340
-		return self::getItems($itemType, $itemSource, null, null, \OC_User::getUser(), $format,
341
-			$parameters, -1, $includeCollections);
342
-	}
343
-
344
-	/**
345
-	 * Share an item with a user, group, or via private link
346
-	 * @param string $itemType
347
-	 * @param string $itemSource
348
-	 * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
349
-	 * @param string $shareWith User or group the item is being shared with
350
-	 * @param int $permissions CRUDS
351
-	 * @param string $itemSourceName
352
-	 * @param \DateTime|null $expirationDate
353
-	 * @param bool|null $passwordChanged
354
-	 * @return boolean|string Returns true on success or false on failure, Returns token on success for links
355
-	 * @throws \OC\HintException when the share type is remote and the shareWith is invalid
356
-	 * @throws \Exception
357
-	 * @since 5.0.0 - parameter $itemSourceName was added in 6.0.0, parameter $expirationDate was added in 7.0.0, parameter $passwordChanged added in 9.0.0
358
-	 */
359
-	public static function shareItem($itemType, $itemSource, $shareType, $shareWith, $permissions, $itemSourceName = null, \DateTime $expirationDate = null, $passwordChanged = null) {
360
-
361
-		$backend = self::getBackend($itemType);
362
-		$l = \OC::$server->getL10N('lib');
363
-
364
-		if ($backend->isShareTypeAllowed($shareType) === false) {
365
-			$message = 'Sharing %s failed, because the backend does not allow shares from type %i';
366
-			$message_t = $l->t('Sharing %s failed, because the backend does not allow shares from type %i', array($itemSourceName, $shareType));
367
-			\OCP\Util::writeLog(Share::class, sprintf($message, $itemSourceName, $shareType), \OCP\Util::DEBUG);
368
-			throw new \Exception($message_t);
369
-		}
370
-
371
-		$uidOwner = \OC_User::getUser();
372
-		$shareWithinGroupOnly = self::shareWithGroupMembersOnly();
373
-
374
-		if (is_null($itemSourceName)) {
375
-			$itemSourceName = $itemSource;
376
-		}
377
-		$itemName = $itemSourceName;
378
-
379
-		// check if file can be shared
380
-		if ($itemType === 'file' or $itemType === 'folder') {
381
-			$path = \OC\Files\Filesystem::getPath($itemSource);
382
-			$itemName = $path;
383
-
384
-			// verify that the file exists before we try to share it
385
-			if (!$path) {
386
-				$message = 'Sharing %s failed, because the file does not exist';
387
-				$message_t = $l->t('Sharing %s failed, because the file does not exist', array($itemSourceName));
388
-				\OCP\Util::writeLog(Share::class, sprintf($message, $itemSourceName), \OCP\Util::DEBUG);
389
-				throw new \Exception($message_t);
390
-			}
391
-			// verify that the user has share permission
392
-			if (!\OC\Files\Filesystem::isSharable($path) || \OCP\Util::isSharingDisabledForUser()) {
393
-				$message = 'You are not allowed to share %s';
394
-				$message_t = $l->t('You are not allowed to share %s', [$path]);
395
-				\OCP\Util::writeLog(Share::class, sprintf($message, $path), \OCP\Util::DEBUG);
396
-				throw new \Exception($message_t);
397
-			}
398
-		}
399
-
400
-		//verify that we don't share a folder which already contains a share mount point
401
-		if ($itemType === 'folder') {
402
-			$path = '/' . $uidOwner . '/files' . \OC\Files\Filesystem::getPath($itemSource) . '/';
403
-			$mountManager = \OC\Files\Filesystem::getMountManager();
404
-			$mounts = $mountManager->findIn($path);
405
-			foreach ($mounts as $mount) {
406
-				if ($mount->getStorage()->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) {
407
-					$message = 'Sharing "' . $itemSourceName . '" failed, because it contains files shared with you!';
408
-					\OCP\Util::writeLog(Share::class, $message, \OCP\Util::DEBUG);
409
-					throw new \Exception($message);
410
-				}
411
-
412
-			}
413
-		}
414
-
415
-		// single file shares should never have delete permissions
416
-		if ($itemType === 'file') {
417
-			$permissions = (int)$permissions & ~\OCP\Constants::PERMISSION_DELETE;
418
-		}
419
-
420
-		//Validate expirationDate
421
-		if ($expirationDate !== null) {
422
-			try {
423
-				/*
61
+    /** CRUDS permissions (Create, Read, Update, Delete, Share) using a bitmask
62
+     * Construct permissions for share() and setPermissions with Or (|) e.g.
63
+     * Give user read and update permissions: PERMISSION_READ | PERMISSION_UPDATE
64
+     *
65
+     * Check if permission is granted with And (&) e.g. Check if delete is
66
+     * granted: if ($permissions & PERMISSION_DELETE)
67
+     *
68
+     * Remove permissions with And (&) and Not (~) e.g. Remove the update
69
+     * permission: $permissions &= ~PERMISSION_UPDATE
70
+     *
71
+     * Apps are required to handle permissions on their own, this class only
72
+     * stores and manages the permissions of shares
73
+     * @see lib/public/constants.php
74
+     */
75
+
76
+    /**
77
+     * Register a sharing backend class that implements OCP\Share_Backend for an item type
78
+     * @param string $itemType Item type
79
+     * @param string $class Backend class
80
+     * @param string $collectionOf (optional) Depends on item type
81
+     * @param array $supportedFileExtensions (optional) List of supported file extensions if this item type depends on files
82
+     * @return boolean true if backend is registered or false if error
83
+     */
84
+    public static function registerBackend($itemType, $class, $collectionOf = null, $supportedFileExtensions = null) {
85
+        if (\OC::$server->getConfig()->getAppValue('core', 'shareapi_enabled', 'yes') == 'yes') {
86
+            if (!isset(self::$backendTypes[$itemType])) {
87
+                self::$backendTypes[$itemType] = array(
88
+                    'class' => $class,
89
+                    'collectionOf' => $collectionOf,
90
+                    'supportedFileExtensions' => $supportedFileExtensions
91
+                );
92
+                if(count(self::$backendTypes) === 1) {
93
+                    Util::addScript('core', 'merged-share-backend');
94
+                    \OC_Util::addStyle('core', 'share');
95
+                }
96
+                return true;
97
+            }
98
+            \OCP\Util::writeLog(Share::class,
99
+                'Sharing backend '.$class.' not registered, '.self::$backendTypes[$itemType]['class']
100
+                .' is already registered for '.$itemType,
101
+                \OCP\Util::WARN);
102
+        }
103
+        return false;
104
+    }
105
+
106
+    /**
107
+     * Get the items of item type shared with the current user
108
+     * @param string $itemType
109
+     * @param int $format (optional) Format type must be defined by the backend
110
+     * @param mixed $parameters (optional)
111
+     * @param int $limit Number of items to return (optional) Returns all by default
112
+     * @param boolean $includeCollections (optional)
113
+     * @return mixed Return depends on format
114
+     */
115
+    public static function getItemsSharedWith($itemType, $format = self::FORMAT_NONE,
116
+                                                $parameters = null, $limit = -1, $includeCollections = false) {
117
+        return self::getItems($itemType, null, self::$shareTypeUserAndGroups, \OC_User::getUser(), null, $format,
118
+            $parameters, $limit, $includeCollections);
119
+    }
120
+
121
+    /**
122
+     * Get the items of item type shared with a user
123
+     * @param string $itemType
124
+     * @param string $user id for which user we want the shares
125
+     * @param int $format (optional) Format type must be defined by the backend
126
+     * @param mixed $parameters (optional)
127
+     * @param int $limit Number of items to return (optional) Returns all by default
128
+     * @param boolean $includeCollections (optional)
129
+     * @return mixed Return depends on format
130
+     */
131
+    public static function getItemsSharedWithUser($itemType, $user, $format = self::FORMAT_NONE,
132
+                                                    $parameters = null, $limit = -1, $includeCollections = false) {
133
+        return self::getItems($itemType, null, self::$shareTypeUserAndGroups, $user, null, $format,
134
+            $parameters, $limit, $includeCollections);
135
+    }
136
+
137
+    /**
138
+     * Get the item of item type shared with a given user by source
139
+     * @param string $itemType
140
+     * @param string $itemSource
141
+     * @param string $user User to whom the item was shared
142
+     * @param string $owner Owner of the share
143
+     * @param int $shareType only look for a specific share type
144
+     * @return array Return list of items with file_target, permissions and expiration
145
+     */
146
+    public static function getItemSharedWithUser($itemType, $itemSource, $user, $owner = null, $shareType = null) {
147
+        $shares = array();
148
+        $fileDependent = false;
149
+
150
+        $where = 'WHERE';
151
+        $fileDependentWhere = '';
152
+        if ($itemType === 'file' || $itemType === 'folder') {
153
+            $fileDependent = true;
154
+            $column = 'file_source';
155
+            $fileDependentWhere = 'INNER JOIN `*PREFIX*filecache` ON `file_source` = `*PREFIX*filecache`.`fileid` ';
156
+            $fileDependentWhere .= 'INNER JOIN `*PREFIX*storages` ON `numeric_id` = `*PREFIX*filecache`.`storage` ';
157
+        } else {
158
+            $column = 'item_source';
159
+        }
160
+
161
+        $select = self::createSelectStatement(self::FORMAT_NONE, $fileDependent);
162
+
163
+        $where .= ' `' . $column . '` = ? AND `item_type` = ? ';
164
+        $arguments = array($itemSource, $itemType);
165
+        // for link shares $user === null
166
+        if ($user !== null) {
167
+            $where .= ' AND `share_with` = ? ';
168
+            $arguments[] = $user;
169
+        }
170
+
171
+        if ($shareType !== null) {
172
+            $where .= ' AND `share_type` = ? ';
173
+            $arguments[] = $shareType;
174
+        }
175
+
176
+        if ($owner !== null) {
177
+            $where .= ' AND `uid_owner` = ? ';
178
+            $arguments[] = $owner;
179
+        }
180
+
181
+        $query = \OC_DB::prepare('SELECT ' . $select . ' FROM `*PREFIX*share` '. $fileDependentWhere . $where);
182
+
183
+        $result = \OC_DB::executeAudited($query, $arguments);
184
+
185
+        while ($row = $result->fetchRow()) {
186
+            if ($fileDependent && !self::isFileReachable($row['path'], $row['storage_id'])) {
187
+                continue;
188
+            }
189
+            if ($fileDependent && (int)$row['file_parent'] === -1) {
190
+                // if it is a mount point we need to get the path from the mount manager
191
+                $mountManager = \OC\Files\Filesystem::getMountManager();
192
+                $mountPoint = $mountManager->findByStorageId($row['storage_id']);
193
+                if (!empty($mountPoint)) {
194
+                    $path = $mountPoint[0]->getMountPoint();
195
+                    $path = trim($path, '/');
196
+                    $path = substr($path, strlen($owner) + 1); //normalize path to 'files/foo.txt`
197
+                    $row['path'] = $path;
198
+                } else {
199
+                    \OC::$server->getLogger()->warning(
200
+                        'Could not resolve mount point for ' . $row['storage_id'],
201
+                        ['app' => Share::class]
202
+                    );
203
+                }
204
+            }
205
+            $shares[] = $row;
206
+        }
207
+
208
+        //if didn't found a result than let's look for a group share.
209
+        if(empty($shares) && $user !== null) {
210
+            $userObject = \OC::$server->getUserManager()->get($user);
211
+            $groups = [];
212
+            if ($userObject) {
213
+                $groups = \OC::$server->getGroupManager()->getUserGroupIds($userObject);
214
+            }
215
+
216
+            if (!empty($groups)) {
217
+                $where = $fileDependentWhere . ' WHERE `' . $column . '` = ? AND `item_type` = ? AND `share_with` in (?)';
218
+                $arguments = array($itemSource, $itemType, $groups);
219
+                $types = array(null, null, IQueryBuilder::PARAM_STR_ARRAY);
220
+
221
+                if ($owner !== null) {
222
+                    $where .= ' AND `uid_owner` = ?';
223
+                    $arguments[] = $owner;
224
+                    $types[] = null;
225
+                }
226
+
227
+                // TODO: inject connection, hopefully one day in the future when this
228
+                // class isn't static anymore...
229
+                $conn = \OC::$server->getDatabaseConnection();
230
+                $result = $conn->executeQuery(
231
+                    'SELECT ' . $select . ' FROM `*PREFIX*share` ' . $where,
232
+                    $arguments,
233
+                    $types
234
+                );
235
+
236
+                while ($row = $result->fetch()) {
237
+                    $shares[] = $row;
238
+                }
239
+            }
240
+        }
241
+
242
+        return $shares;
243
+
244
+    }
245
+
246
+    /**
247
+     * Get the item of item type shared with the current user by source
248
+     * @param string $itemType
249
+     * @param string $itemSource
250
+     * @param int $format (optional) Format type must be defined by the backend
251
+     * @param mixed $parameters
252
+     * @param boolean $includeCollections
253
+     * @param string $shareWith (optional) define against which user should be checked, default: current user
254
+     * @return array
255
+     */
256
+    public static function getItemSharedWithBySource($itemType, $itemSource, $format = self::FORMAT_NONE,
257
+                                                        $parameters = null, $includeCollections = false, $shareWith = null) {
258
+        $shareWith = ($shareWith === null) ? \OC_User::getUser() : $shareWith;
259
+        return self::getItems($itemType, $itemSource, self::$shareTypeUserAndGroups, $shareWith, null, $format,
260
+            $parameters, 1, $includeCollections, true);
261
+    }
262
+
263
+    /**
264
+     * Based on the given token the share information will be returned - password protected shares will be verified
265
+     * @param string $token
266
+     * @param bool $checkPasswordProtection
267
+     * @return array|boolean false will be returned in case the token is unknown or unauthorized
268
+     */
269
+    public static function getShareByToken($token, $checkPasswordProtection = true) {
270
+        $query = \OC_DB::prepare('SELECT * FROM `*PREFIX*share` WHERE `token` = ?', 1);
271
+        $result = $query->execute(array($token));
272
+        if ($result === false) {
273
+            \OCP\Util::writeLog(Share::class, \OC_DB::getErrorMessage() . ', token=' . $token, \OCP\Util::ERROR);
274
+        }
275
+        $row = $result->fetchRow();
276
+        if ($row === false) {
277
+            return false;
278
+        }
279
+        if (is_array($row) and self::expireItem($row)) {
280
+            return false;
281
+        }
282
+
283
+        // password protected shares need to be authenticated
284
+        if ($checkPasswordProtection && !\OC\Share\Share::checkPasswordProtectedShare($row)) {
285
+            return false;
286
+        }
287
+
288
+        return $row;
289
+    }
290
+
291
+    /**
292
+     * resolves reshares down to the last real share
293
+     * @param array $linkItem
294
+     * @return array file owner
295
+     */
296
+    public static function resolveReShare($linkItem)
297
+    {
298
+        if (isset($linkItem['parent'])) {
299
+            $parent = $linkItem['parent'];
300
+            while (isset($parent)) {
301
+                $query = \OC_DB::prepare('SELECT * FROM `*PREFIX*share` WHERE `id` = ?', 1);
302
+                $item = $query->execute(array($parent))->fetchRow();
303
+                if (isset($item['parent'])) {
304
+                    $parent = $item['parent'];
305
+                } else {
306
+                    return $item;
307
+                }
308
+            }
309
+        }
310
+        return $linkItem;
311
+    }
312
+
313
+
314
+    /**
315
+     * Get the shared items of item type owned by the current user
316
+     * @param string $itemType
317
+     * @param int $format (optional) Format type must be defined by the backend
318
+     * @param mixed $parameters
319
+     * @param int $limit Number of items to return (optional) Returns all by default
320
+     * @param boolean $includeCollections
321
+     * @return mixed Return depends on format
322
+     */
323
+    public static function getItemsShared($itemType, $format = self::FORMAT_NONE, $parameters = null,
324
+                                            $limit = -1, $includeCollections = false) {
325
+        return self::getItems($itemType, null, null, null, \OC_User::getUser(), $format,
326
+            $parameters, $limit, $includeCollections);
327
+    }
328
+
329
+    /**
330
+     * Get the shared item of item type owned by the current user
331
+     * @param string $itemType
332
+     * @param string $itemSource
333
+     * @param int $format (optional) Format type must be defined by the backend
334
+     * @param mixed $parameters
335
+     * @param boolean $includeCollections
336
+     * @return mixed Return depends on format
337
+     */
338
+    public static function getItemShared($itemType, $itemSource, $format = self::FORMAT_NONE,
339
+                                            $parameters = null, $includeCollections = false) {
340
+        return self::getItems($itemType, $itemSource, null, null, \OC_User::getUser(), $format,
341
+            $parameters, -1, $includeCollections);
342
+    }
343
+
344
+    /**
345
+     * Share an item with a user, group, or via private link
346
+     * @param string $itemType
347
+     * @param string $itemSource
348
+     * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
349
+     * @param string $shareWith User or group the item is being shared with
350
+     * @param int $permissions CRUDS
351
+     * @param string $itemSourceName
352
+     * @param \DateTime|null $expirationDate
353
+     * @param bool|null $passwordChanged
354
+     * @return boolean|string Returns true on success or false on failure, Returns token on success for links
355
+     * @throws \OC\HintException when the share type is remote and the shareWith is invalid
356
+     * @throws \Exception
357
+     * @since 5.0.0 - parameter $itemSourceName was added in 6.0.0, parameter $expirationDate was added in 7.0.0, parameter $passwordChanged added in 9.0.0
358
+     */
359
+    public static function shareItem($itemType, $itemSource, $shareType, $shareWith, $permissions, $itemSourceName = null, \DateTime $expirationDate = null, $passwordChanged = null) {
360
+
361
+        $backend = self::getBackend($itemType);
362
+        $l = \OC::$server->getL10N('lib');
363
+
364
+        if ($backend->isShareTypeAllowed($shareType) === false) {
365
+            $message = 'Sharing %s failed, because the backend does not allow shares from type %i';
366
+            $message_t = $l->t('Sharing %s failed, because the backend does not allow shares from type %i', array($itemSourceName, $shareType));
367
+            \OCP\Util::writeLog(Share::class, sprintf($message, $itemSourceName, $shareType), \OCP\Util::DEBUG);
368
+            throw new \Exception($message_t);
369
+        }
370
+
371
+        $uidOwner = \OC_User::getUser();
372
+        $shareWithinGroupOnly = self::shareWithGroupMembersOnly();
373
+
374
+        if (is_null($itemSourceName)) {
375
+            $itemSourceName = $itemSource;
376
+        }
377
+        $itemName = $itemSourceName;
378
+
379
+        // check if file can be shared
380
+        if ($itemType === 'file' or $itemType === 'folder') {
381
+            $path = \OC\Files\Filesystem::getPath($itemSource);
382
+            $itemName = $path;
383
+
384
+            // verify that the file exists before we try to share it
385
+            if (!$path) {
386
+                $message = 'Sharing %s failed, because the file does not exist';
387
+                $message_t = $l->t('Sharing %s failed, because the file does not exist', array($itemSourceName));
388
+                \OCP\Util::writeLog(Share::class, sprintf($message, $itemSourceName), \OCP\Util::DEBUG);
389
+                throw new \Exception($message_t);
390
+            }
391
+            // verify that the user has share permission
392
+            if (!\OC\Files\Filesystem::isSharable($path) || \OCP\Util::isSharingDisabledForUser()) {
393
+                $message = 'You are not allowed to share %s';
394
+                $message_t = $l->t('You are not allowed to share %s', [$path]);
395
+                \OCP\Util::writeLog(Share::class, sprintf($message, $path), \OCP\Util::DEBUG);
396
+                throw new \Exception($message_t);
397
+            }
398
+        }
399
+
400
+        //verify that we don't share a folder which already contains a share mount point
401
+        if ($itemType === 'folder') {
402
+            $path = '/' . $uidOwner . '/files' . \OC\Files\Filesystem::getPath($itemSource) . '/';
403
+            $mountManager = \OC\Files\Filesystem::getMountManager();
404
+            $mounts = $mountManager->findIn($path);
405
+            foreach ($mounts as $mount) {
406
+                if ($mount->getStorage()->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) {
407
+                    $message = 'Sharing "' . $itemSourceName . '" failed, because it contains files shared with you!';
408
+                    \OCP\Util::writeLog(Share::class, $message, \OCP\Util::DEBUG);
409
+                    throw new \Exception($message);
410
+                }
411
+
412
+            }
413
+        }
414
+
415
+        // single file shares should never have delete permissions
416
+        if ($itemType === 'file') {
417
+            $permissions = (int)$permissions & ~\OCP\Constants::PERMISSION_DELETE;
418
+        }
419
+
420
+        //Validate expirationDate
421
+        if ($expirationDate !== null) {
422
+            try {
423
+                /*
424 424
 				 * Reuse the validateExpireDate.
425 425
 				 * We have to pass time() since the second arg is the time
426 426
 				 * the file was shared, since it is not shared yet we just use
427 427
 				 * the current time.
428 428
 				 */
429
-				$expirationDate = self::validateExpireDate($expirationDate->format('Y-m-d'), time(), $itemType, $itemSource);
430
-			} catch (\Exception $e) {
431
-				throw new \OC\HintException($e->getMessage(), $e->getMessage(), 404);
432
-			}
433
-		}
434
-
435
-		// Verify share type and sharing conditions are met
436
-		if ($shareType === self::SHARE_TYPE_USER) {
437
-			if ($shareWith == $uidOwner) {
438
-				$message = 'Sharing %s failed, because you can not share with yourself';
439
-				$message_t = $l->t('Sharing %s failed, because you can not share with yourself', [$itemName]);
440
-				\OCP\Util::writeLog(Share::class, sprintf($message, $itemSourceName), \OCP\Util::DEBUG);
441
-				throw new \Exception($message_t);
442
-			}
443
-			if (!\OC::$server->getUserManager()->userExists($shareWith)) {
444
-				$message = 'Sharing %s failed, because the user %s does not exist';
445
-				$message_t = $l->t('Sharing %s failed, because the user %s does not exist', array($itemSourceName, $shareWith));
446
-				\OCP\Util::writeLog(Share::class, sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG);
447
-				throw new \Exception($message_t);
448
-			}
449
-			if ($shareWithinGroupOnly) {
450
-				$userManager = \OC::$server->getUserManager();
451
-				$groupManager = \OC::$server->getGroupManager();
452
-				$userOwner = $userManager->get($uidOwner);
453
-				$userShareWith = $userManager->get($shareWith);
454
-				$groupsOwner = [];
455
-				$groupsShareWith = [];
456
-				if ($userOwner) {
457
-					$groupsOwner = $groupManager->getUserGroupIds($userOwner);
458
-				}
459
-				if ($userShareWith) {
460
-					$groupsShareWith = $groupManager->getUserGroupIds($userShareWith);
461
-				}
462
-				$inGroup = array_intersect($groupsOwner, $groupsShareWith);
463
-				if (empty($inGroup)) {
464
-					$message = 'Sharing %s failed, because the user '
465
-						.'%s is not a member of any groups that %s is a member of';
466
-					$message_t = $l->t('Sharing %s failed, because the user %s is not a member of any groups that %s is a member of', array($itemName, $shareWith, $uidOwner));
467
-					\OCP\Util::writeLog(Share::class, sprintf($message, $itemName, $shareWith, $uidOwner), \OCP\Util::DEBUG);
468
-					throw new \Exception($message_t);
469
-				}
470
-			}
471
-			// Check if the item source is already shared with the user, either from the same owner or a different user
472
-			if ($checkExists = self::getItems($itemType, $itemSource, self::$shareTypeUserAndGroups,
473
-				$shareWith, null, self::FORMAT_NONE, null, 1, true, true)) {
474
-				// Only allow the same share to occur again if it is the same
475
-				// owner and is not a user share, this use case is for increasing
476
-				// permissions for a specific user
477
-				if ($checkExists['uid_owner'] != $uidOwner || $checkExists['share_type'] == $shareType) {
478
-					$message = 'Sharing %s failed, because this item is already shared with %s';
479
-					$message_t = $l->t('Sharing %s failed, because this item is already shared with %s', array($itemSourceName, $shareWith));
480
-					\OCP\Util::writeLog(Share::class, sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG);
481
-					throw new \Exception($message_t);
482
-				}
483
-			}
484
-			if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_USER,
485
-				$shareWith, null, self::FORMAT_NONE, null, 1, true, true)) {
486
-				// Only allow the same share to occur again if it is the same
487
-				// owner and is not a user share, this use case is for increasing
488
-				// permissions for a specific user
489
-				if ($checkExists['uid_owner'] != $uidOwner || $checkExists['share_type'] == $shareType) {
490
-					$message = 'Sharing %s failed, because this item is already shared with user %s';
491
-					$message_t = $l->t('Sharing %s failed, because this item is already shared with user %s', array($itemSourceName, $shareWith));
492
-					\OCP\Util::writeLog(Share::class, sprintf($message, $itemSourceName, $shareWith), \OCP\Util::ERROR);
493
-					throw new \Exception($message_t);
494
-				}
495
-			}
496
-		} else if ($shareType === self::SHARE_TYPE_GROUP) {
497
-			if (!\OC::$server->getGroupManager()->groupExists($shareWith)) {
498
-				$message = 'Sharing %s failed, because the group %s does not exist';
499
-				$message_t = $l->t('Sharing %s failed, because the group %s does not exist', array($itemSourceName, $shareWith));
500
-				\OCP\Util::writeLog(Share::class, sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG);
501
-				throw new \Exception($message_t);
502
-			}
503
-			if ($shareWithinGroupOnly) {
504
-				$group = \OC::$server->getGroupManager()->get($shareWith);
505
-				$user = \OC::$server->getUserManager()->get($uidOwner);
506
-				if (!$group || !$user || !$group->inGroup($user)) {
507
-					$message = 'Sharing %s failed, because '
508
-						. '%s is not a member of the group %s';
509
-					$message_t = $l->t('Sharing %s failed, because %s is not a member of the group %s', array($itemSourceName, $uidOwner, $shareWith));
510
-					\OCP\Util::writeLog(Share::class, sprintf($message, $itemSourceName, $uidOwner, $shareWith), \OCP\Util::DEBUG);
511
-					throw new \Exception($message_t);
512
-				}
513
-			}
514
-			// Check if the item source is already shared with the group, either from the same owner or a different user
515
-			// The check for each user in the group is done inside the put() function
516
-			if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_GROUP, $shareWith,
517
-				null, self::FORMAT_NONE, null, 1, true, true)) {
518
-
519
-				if ($checkExists['share_with'] === $shareWith && $checkExists['share_type'] === \OCP\Share::SHARE_TYPE_GROUP) {
520
-					$message = 'Sharing %s failed, because this item is already shared with %s';
521
-					$message_t = $l->t('Sharing %s failed, because this item is already shared with %s', array($itemSourceName, $shareWith));
522
-					\OCP\Util::writeLog(Share::class, sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG);
523
-					throw new \Exception($message_t);
524
-				}
525
-			}
526
-			// Convert share with into an array with the keys group and users
527
-			$group = $shareWith;
528
-			$shareWith = array();
529
-			$shareWith['group'] = $group;
530
-
531
-
532
-			$groupObject = \OC::$server->getGroupManager()->get($group);
533
-			$userIds = [];
534
-			if ($groupObject) {
535
-				$users = $groupObject->searchUsers('', -1, 0);
536
-				foreach ($users as $user) {
537
-					$userIds[] = $user->getUID();
538
-				}
539
-			}
540
-
541
-			$shareWith['users'] = array_diff($userIds, array($uidOwner));
542
-		} else if ($shareType === self::SHARE_TYPE_LINK) {
543
-			$updateExistingShare = false;
544
-			if (\OC::$server->getConfig()->getAppValue('core', 'shareapi_allow_links', 'yes') == 'yes') {
545
-
546
-				// IF the password is changed via the old ajax endpoint verify it before deleting the old share
547
-				if ($passwordChanged === true) {
548
-					self::verifyPassword($shareWith);
549
-				}
550
-
551
-				// when updating a link share
552
-				// FIXME Don't delete link if we update it
553
-				if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_LINK, null,
554
-					$uidOwner, self::FORMAT_NONE, null, 1)) {
555
-					// remember old token
556
-					$oldToken = $checkExists['token'];
557
-					$oldPermissions = $checkExists['permissions'];
558
-					//delete the old share
559
-					Helper::delete($checkExists['id']);
560
-					$updateExistingShare = true;
561
-				}
562
-
563
-				if ($passwordChanged === null) {
564
-					// Generate hash of password - same method as user passwords
565
-					if (is_string($shareWith) && $shareWith !== '') {
566
-						self::verifyPassword($shareWith);
567
-						$shareWith = \OC::$server->getHasher()->hash($shareWith);
568
-					} else {
569
-						// reuse the already set password, but only if we change permissions
570
-						// otherwise the user disabled the password protection
571
-						if ($checkExists && (int)$permissions !== (int)$oldPermissions) {
572
-							$shareWith = $checkExists['share_with'];
573
-						}
574
-					}
575
-				} else {
576
-					if ($passwordChanged === true) {
577
-						if (is_string($shareWith) && $shareWith !== '') {
578
-							self::verifyPassword($shareWith);
579
-							$shareWith = \OC::$server->getHasher()->hash($shareWith);
580
-						}
581
-					} else if ($updateExistingShare) {
582
-						$shareWith = $checkExists['share_with'];
583
-					}
584
-				}
585
-
586
-				if (\OCP\Util::isPublicLinkPasswordRequired() && empty($shareWith)) {
587
-					$message = 'You need to provide a password to create a public link, only protected links are allowed';
588
-					$message_t = $l->t('You need to provide a password to create a public link, only protected links are allowed');
589
-					\OCP\Util::writeLog(Share::class, $message, \OCP\Util::DEBUG);
590
-					throw new \Exception($message_t);
591
-				}
592
-
593
-				if ($updateExistingShare === false &&
594
-					self::isDefaultExpireDateEnabled() &&
595
-					empty($expirationDate)) {
596
-					$expirationDate = Helper::calcExpireDate();
597
-				}
598
-
599
-				// Generate token
600
-				if (isset($oldToken)) {
601
-					$token = $oldToken;
602
-				} else {
603
-					$token = \OC::$server->getSecureRandom()->generate(self::TOKEN_LENGTH,
604
-						\OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE
605
-					);
606
-				}
607
-				$result = self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions,
608
-					null, $token, $itemSourceName, $expirationDate);
609
-				if ($result) {
610
-					return $token;
611
-				} else {
612
-					return false;
613
-				}
614
-			}
615
-			$message = 'Sharing %s failed, because sharing with links is not allowed';
616
-			$message_t = $l->t('Sharing %s failed, because sharing with links is not allowed', array($itemSourceName));
617
-			\OCP\Util::writeLog(Share::class, sprintf($message, $itemSourceName), \OCP\Util::DEBUG);
618
-			throw new \Exception($message_t);
619
-		} else if ($shareType === self::SHARE_TYPE_REMOTE) {
620
-
621
-			/*
429
+                $expirationDate = self::validateExpireDate($expirationDate->format('Y-m-d'), time(), $itemType, $itemSource);
430
+            } catch (\Exception $e) {
431
+                throw new \OC\HintException($e->getMessage(), $e->getMessage(), 404);
432
+            }
433
+        }
434
+
435
+        // Verify share type and sharing conditions are met
436
+        if ($shareType === self::SHARE_TYPE_USER) {
437
+            if ($shareWith == $uidOwner) {
438
+                $message = 'Sharing %s failed, because you can not share with yourself';
439
+                $message_t = $l->t('Sharing %s failed, because you can not share with yourself', [$itemName]);
440
+                \OCP\Util::writeLog(Share::class, sprintf($message, $itemSourceName), \OCP\Util::DEBUG);
441
+                throw new \Exception($message_t);
442
+            }
443
+            if (!\OC::$server->getUserManager()->userExists($shareWith)) {
444
+                $message = 'Sharing %s failed, because the user %s does not exist';
445
+                $message_t = $l->t('Sharing %s failed, because the user %s does not exist', array($itemSourceName, $shareWith));
446
+                \OCP\Util::writeLog(Share::class, sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG);
447
+                throw new \Exception($message_t);
448
+            }
449
+            if ($shareWithinGroupOnly) {
450
+                $userManager = \OC::$server->getUserManager();
451
+                $groupManager = \OC::$server->getGroupManager();
452
+                $userOwner = $userManager->get($uidOwner);
453
+                $userShareWith = $userManager->get($shareWith);
454
+                $groupsOwner = [];
455
+                $groupsShareWith = [];
456
+                if ($userOwner) {
457
+                    $groupsOwner = $groupManager->getUserGroupIds($userOwner);
458
+                }
459
+                if ($userShareWith) {
460
+                    $groupsShareWith = $groupManager->getUserGroupIds($userShareWith);
461
+                }
462
+                $inGroup = array_intersect($groupsOwner, $groupsShareWith);
463
+                if (empty($inGroup)) {
464
+                    $message = 'Sharing %s failed, because the user '
465
+                        .'%s is not a member of any groups that %s is a member of';
466
+                    $message_t = $l->t('Sharing %s failed, because the user %s is not a member of any groups that %s is a member of', array($itemName, $shareWith, $uidOwner));
467
+                    \OCP\Util::writeLog(Share::class, sprintf($message, $itemName, $shareWith, $uidOwner), \OCP\Util::DEBUG);
468
+                    throw new \Exception($message_t);
469
+                }
470
+            }
471
+            // Check if the item source is already shared with the user, either from the same owner or a different user
472
+            if ($checkExists = self::getItems($itemType, $itemSource, self::$shareTypeUserAndGroups,
473
+                $shareWith, null, self::FORMAT_NONE, null, 1, true, true)) {
474
+                // Only allow the same share to occur again if it is the same
475
+                // owner and is not a user share, this use case is for increasing
476
+                // permissions for a specific user
477
+                if ($checkExists['uid_owner'] != $uidOwner || $checkExists['share_type'] == $shareType) {
478
+                    $message = 'Sharing %s failed, because this item is already shared with %s';
479
+                    $message_t = $l->t('Sharing %s failed, because this item is already shared with %s', array($itemSourceName, $shareWith));
480
+                    \OCP\Util::writeLog(Share::class, sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG);
481
+                    throw new \Exception($message_t);
482
+                }
483
+            }
484
+            if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_USER,
485
+                $shareWith, null, self::FORMAT_NONE, null, 1, true, true)) {
486
+                // Only allow the same share to occur again if it is the same
487
+                // owner and is not a user share, this use case is for increasing
488
+                // permissions for a specific user
489
+                if ($checkExists['uid_owner'] != $uidOwner || $checkExists['share_type'] == $shareType) {
490
+                    $message = 'Sharing %s failed, because this item is already shared with user %s';
491
+                    $message_t = $l->t('Sharing %s failed, because this item is already shared with user %s', array($itemSourceName, $shareWith));
492
+                    \OCP\Util::writeLog(Share::class, sprintf($message, $itemSourceName, $shareWith), \OCP\Util::ERROR);
493
+                    throw new \Exception($message_t);
494
+                }
495
+            }
496
+        } else if ($shareType === self::SHARE_TYPE_GROUP) {
497
+            if (!\OC::$server->getGroupManager()->groupExists($shareWith)) {
498
+                $message = 'Sharing %s failed, because the group %s does not exist';
499
+                $message_t = $l->t('Sharing %s failed, because the group %s does not exist', array($itemSourceName, $shareWith));
500
+                \OCP\Util::writeLog(Share::class, sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG);
501
+                throw new \Exception($message_t);
502
+            }
503
+            if ($shareWithinGroupOnly) {
504
+                $group = \OC::$server->getGroupManager()->get($shareWith);
505
+                $user = \OC::$server->getUserManager()->get($uidOwner);
506
+                if (!$group || !$user || !$group->inGroup($user)) {
507
+                    $message = 'Sharing %s failed, because '
508
+                        . '%s is not a member of the group %s';
509
+                    $message_t = $l->t('Sharing %s failed, because %s is not a member of the group %s', array($itemSourceName, $uidOwner, $shareWith));
510
+                    \OCP\Util::writeLog(Share::class, sprintf($message, $itemSourceName, $uidOwner, $shareWith), \OCP\Util::DEBUG);
511
+                    throw new \Exception($message_t);
512
+                }
513
+            }
514
+            // Check if the item source is already shared with the group, either from the same owner or a different user
515
+            // The check for each user in the group is done inside the put() function
516
+            if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_GROUP, $shareWith,
517
+                null, self::FORMAT_NONE, null, 1, true, true)) {
518
+
519
+                if ($checkExists['share_with'] === $shareWith && $checkExists['share_type'] === \OCP\Share::SHARE_TYPE_GROUP) {
520
+                    $message = 'Sharing %s failed, because this item is already shared with %s';
521
+                    $message_t = $l->t('Sharing %s failed, because this item is already shared with %s', array($itemSourceName, $shareWith));
522
+                    \OCP\Util::writeLog(Share::class, sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG);
523
+                    throw new \Exception($message_t);
524
+                }
525
+            }
526
+            // Convert share with into an array with the keys group and users
527
+            $group = $shareWith;
528
+            $shareWith = array();
529
+            $shareWith['group'] = $group;
530
+
531
+
532
+            $groupObject = \OC::$server->getGroupManager()->get($group);
533
+            $userIds = [];
534
+            if ($groupObject) {
535
+                $users = $groupObject->searchUsers('', -1, 0);
536
+                foreach ($users as $user) {
537
+                    $userIds[] = $user->getUID();
538
+                }
539
+            }
540
+
541
+            $shareWith['users'] = array_diff($userIds, array($uidOwner));
542
+        } else if ($shareType === self::SHARE_TYPE_LINK) {
543
+            $updateExistingShare = false;
544
+            if (\OC::$server->getConfig()->getAppValue('core', 'shareapi_allow_links', 'yes') == 'yes') {
545
+
546
+                // IF the password is changed via the old ajax endpoint verify it before deleting the old share
547
+                if ($passwordChanged === true) {
548
+                    self::verifyPassword($shareWith);
549
+                }
550
+
551
+                // when updating a link share
552
+                // FIXME Don't delete link if we update it
553
+                if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_LINK, null,
554
+                    $uidOwner, self::FORMAT_NONE, null, 1)) {
555
+                    // remember old token
556
+                    $oldToken = $checkExists['token'];
557
+                    $oldPermissions = $checkExists['permissions'];
558
+                    //delete the old share
559
+                    Helper::delete($checkExists['id']);
560
+                    $updateExistingShare = true;
561
+                }
562
+
563
+                if ($passwordChanged === null) {
564
+                    // Generate hash of password - same method as user passwords
565
+                    if (is_string($shareWith) && $shareWith !== '') {
566
+                        self::verifyPassword($shareWith);
567
+                        $shareWith = \OC::$server->getHasher()->hash($shareWith);
568
+                    } else {
569
+                        // reuse the already set password, but only if we change permissions
570
+                        // otherwise the user disabled the password protection
571
+                        if ($checkExists && (int)$permissions !== (int)$oldPermissions) {
572
+                            $shareWith = $checkExists['share_with'];
573
+                        }
574
+                    }
575
+                } else {
576
+                    if ($passwordChanged === true) {
577
+                        if (is_string($shareWith) && $shareWith !== '') {
578
+                            self::verifyPassword($shareWith);
579
+                            $shareWith = \OC::$server->getHasher()->hash($shareWith);
580
+                        }
581
+                    } else if ($updateExistingShare) {
582
+                        $shareWith = $checkExists['share_with'];
583
+                    }
584
+                }
585
+
586
+                if (\OCP\Util::isPublicLinkPasswordRequired() && empty($shareWith)) {
587
+                    $message = 'You need to provide a password to create a public link, only protected links are allowed';
588
+                    $message_t = $l->t('You need to provide a password to create a public link, only protected links are allowed');
589
+                    \OCP\Util::writeLog(Share::class, $message, \OCP\Util::DEBUG);
590
+                    throw new \Exception($message_t);
591
+                }
592
+
593
+                if ($updateExistingShare === false &&
594
+                    self::isDefaultExpireDateEnabled() &&
595
+                    empty($expirationDate)) {
596
+                    $expirationDate = Helper::calcExpireDate();
597
+                }
598
+
599
+                // Generate token
600
+                if (isset($oldToken)) {
601
+                    $token = $oldToken;
602
+                } else {
603
+                    $token = \OC::$server->getSecureRandom()->generate(self::TOKEN_LENGTH,
604
+                        \OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE
605
+                    );
606
+                }
607
+                $result = self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions,
608
+                    null, $token, $itemSourceName, $expirationDate);
609
+                if ($result) {
610
+                    return $token;
611
+                } else {
612
+                    return false;
613
+                }
614
+            }
615
+            $message = 'Sharing %s failed, because sharing with links is not allowed';
616
+            $message_t = $l->t('Sharing %s failed, because sharing with links is not allowed', array($itemSourceName));
617
+            \OCP\Util::writeLog(Share::class, sprintf($message, $itemSourceName), \OCP\Util::DEBUG);
618
+            throw new \Exception($message_t);
619
+        } else if ($shareType === self::SHARE_TYPE_REMOTE) {
620
+
621
+            /*
622 622
 			 * Check if file is not already shared with the remote user
623 623
 			 */
624
-			if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_REMOTE,
625
-				$shareWith, $uidOwner, self::FORMAT_NONE, null, 1, true, true)) {
626
-					$message = 'Sharing %s failed, because this item is already shared with %s';
627
-					$message_t = $l->t('Sharing %s failed, because this item is already shared with %s', array($itemSourceName, $shareWith));
628
-					\OCP\Util::writeLog(Share::class, sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG);
629
-					throw new \Exception($message_t);
630
-			}
631
-
632
-			// don't allow federated shares if source and target server are the same
633
-			list($user, $remote) = Helper::splitUserRemote($shareWith);
634
-			$currentServer = self::removeProtocolFromUrl(\OC::$server->getURLGenerator()->getAbsoluteURL('/'));
635
-			$currentUser = \OC::$server->getUserSession()->getUser()->getUID();
636
-			if (Helper::isSameUserOnSameServer($user, $remote, $currentUser, $currentServer)) {
637
-				$message = 'Not allowed to create a federated share with the same user.';
638
-				$message_t = $l->t('Not allowed to create a federated share with the same user');
639
-				\OCP\Util::writeLog(Share::class, $message, \OCP\Util::DEBUG);
640
-				throw new \Exception($message_t);
641
-			}
642
-
643
-			$token = \OC::$server->getSecureRandom()->generate(self::TOKEN_LENGTH, \OCP\Security\ISecureRandom::CHAR_LOWER . \OCP\Security\ISecureRandom::CHAR_UPPER .
644
-				\OCP\Security\ISecureRandom::CHAR_DIGITS);
645
-
646
-			$shareWith = $user . '@' . $remote;
647
-			$shareId = self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, null, $token, $itemSourceName);
648
-
649
-			$send = false;
650
-			if ($shareId) {
651
-				$send = self::sendRemoteShare($token, $shareWith, $itemSourceName, $shareId, $uidOwner);
652
-			}
653
-
654
-			if ($send === false) {
655
-				$currentUser = \OC::$server->getUserSession()->getUser()->getUID();
656
-				self::unshare($itemType, $itemSource, $shareType, $shareWith, $currentUser);
657
-				$message_t = $l->t('Sharing %s failed, could not find %s, maybe the server is currently unreachable.', array($itemSourceName, $shareWith));
658
-				throw new \Exception($message_t);
659
-			}
660
-
661
-			return $send;
662
-		} else {
663
-			// Future share types need to include their own conditions
664
-			$message = 'Share type %s is not valid for %s';
665
-			$message_t = $l->t('Share type %s is not valid for %s', array($shareType, $itemSource));
666
-			\OCP\Util::writeLog(Share::class, sprintf($message, $shareType, $itemSource), \OCP\Util::DEBUG);
667
-			throw new \Exception($message_t);
668
-		}
669
-
670
-		// Put the item into the database
671
-		$result = self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, null, null, $itemSourceName, $expirationDate);
672
-
673
-		return $result ? true : false;
674
-	}
675
-
676
-	/**
677
-	 * Unshare an item from a user, group, or delete a private link
678
-	 * @param string $itemType
679
-	 * @param string $itemSource
680
-	 * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
681
-	 * @param string $shareWith User or group the item is being shared with
682
-	 * @param string $owner owner of the share, if null the current user is used
683
-	 * @return boolean true on success or false on failure
684
-	 */
685
-	public static function unshare($itemType, $itemSource, $shareType, $shareWith, $owner = null) {
686
-
687
-		// check if it is a valid itemType
688
-		self::getBackend($itemType);
689
-
690
-		$items = self::getItemSharedWithUser($itemType, $itemSource, $shareWith, $owner, $shareType);
691
-
692
-		$toDelete = array();
693
-		$newParent = null;
694
-		$currentUser = $owner ? $owner : \OC_User::getUser();
695
-		foreach ($items as $item) {
696
-			// delete the item with the expected share_type and owner
697
-			if ((int)$item['share_type'] === (int)$shareType && $item['uid_owner'] === $currentUser) {
698
-				$toDelete = $item;
699
-				// if there is more then one result we don't have to delete the children
700
-				// but update their parent. For group shares the new parent should always be
701
-				// the original group share and not the db entry with the unique name
702
-			} else if ((int)$item['share_type'] === self::$shareTypeGroupUserUnique) {
703
-				$newParent = $item['parent'];
704
-			} else {
705
-				$newParent = $item['id'];
706
-			}
707
-		}
708
-
709
-		if (!empty($toDelete)) {
710
-			self::unshareItem($toDelete, $newParent);
711
-			return true;
712
-		}
713
-		return false;
714
-	}
715
-
716
-	/**
717
-	 * sent status if users got informed by mail about share
718
-	 * @param string $itemType
719
-	 * @param string $itemSource
720
-	 * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
721
-	 * @param string $recipient with whom was the file shared
722
-	 * @param boolean $status
723
-	 */
724
-	public static function setSendMailStatus($itemType, $itemSource, $shareType, $recipient, $status) {
725
-		$status = $status ? 1 : 0;
726
-
727
-		$query = \OC_DB::prepare(
728
-			'UPDATE `*PREFIX*share`
624
+            if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_REMOTE,
625
+                $shareWith, $uidOwner, self::FORMAT_NONE, null, 1, true, true)) {
626
+                    $message = 'Sharing %s failed, because this item is already shared with %s';
627
+                    $message_t = $l->t('Sharing %s failed, because this item is already shared with %s', array($itemSourceName, $shareWith));
628
+                    \OCP\Util::writeLog(Share::class, sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG);
629
+                    throw new \Exception($message_t);
630
+            }
631
+
632
+            // don't allow federated shares if source and target server are the same
633
+            list($user, $remote) = Helper::splitUserRemote($shareWith);
634
+            $currentServer = self::removeProtocolFromUrl(\OC::$server->getURLGenerator()->getAbsoluteURL('/'));
635
+            $currentUser = \OC::$server->getUserSession()->getUser()->getUID();
636
+            if (Helper::isSameUserOnSameServer($user, $remote, $currentUser, $currentServer)) {
637
+                $message = 'Not allowed to create a federated share with the same user.';
638
+                $message_t = $l->t('Not allowed to create a federated share with the same user');
639
+                \OCP\Util::writeLog(Share::class, $message, \OCP\Util::DEBUG);
640
+                throw new \Exception($message_t);
641
+            }
642
+
643
+            $token = \OC::$server->getSecureRandom()->generate(self::TOKEN_LENGTH, \OCP\Security\ISecureRandom::CHAR_LOWER . \OCP\Security\ISecureRandom::CHAR_UPPER .
644
+                \OCP\Security\ISecureRandom::CHAR_DIGITS);
645
+
646
+            $shareWith = $user . '@' . $remote;
647
+            $shareId = self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, null, $token, $itemSourceName);
648
+
649
+            $send = false;
650
+            if ($shareId) {
651
+                $send = self::sendRemoteShare($token, $shareWith, $itemSourceName, $shareId, $uidOwner);
652
+            }
653
+
654
+            if ($send === false) {
655
+                $currentUser = \OC::$server->getUserSession()->getUser()->getUID();
656
+                self::unshare($itemType, $itemSource, $shareType, $shareWith, $currentUser);
657
+                $message_t = $l->t('Sharing %s failed, could not find %s, maybe the server is currently unreachable.', array($itemSourceName, $shareWith));
658
+                throw new \Exception($message_t);
659
+            }
660
+
661
+            return $send;
662
+        } else {
663
+            // Future share types need to include their own conditions
664
+            $message = 'Share type %s is not valid for %s';
665
+            $message_t = $l->t('Share type %s is not valid for %s', array($shareType, $itemSource));
666
+            \OCP\Util::writeLog(Share::class, sprintf($message, $shareType, $itemSource), \OCP\Util::DEBUG);
667
+            throw new \Exception($message_t);
668
+        }
669
+
670
+        // Put the item into the database
671
+        $result = self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, null, null, $itemSourceName, $expirationDate);
672
+
673
+        return $result ? true : false;
674
+    }
675
+
676
+    /**
677
+     * Unshare an item from a user, group, or delete a private link
678
+     * @param string $itemType
679
+     * @param string $itemSource
680
+     * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
681
+     * @param string $shareWith User or group the item is being shared with
682
+     * @param string $owner owner of the share, if null the current user is used
683
+     * @return boolean true on success or false on failure
684
+     */
685
+    public static function unshare($itemType, $itemSource, $shareType, $shareWith, $owner = null) {
686
+
687
+        // check if it is a valid itemType
688
+        self::getBackend($itemType);
689
+
690
+        $items = self::getItemSharedWithUser($itemType, $itemSource, $shareWith, $owner, $shareType);
691
+
692
+        $toDelete = array();
693
+        $newParent = null;
694
+        $currentUser = $owner ? $owner : \OC_User::getUser();
695
+        foreach ($items as $item) {
696
+            // delete the item with the expected share_type and owner
697
+            if ((int)$item['share_type'] === (int)$shareType && $item['uid_owner'] === $currentUser) {
698
+                $toDelete = $item;
699
+                // if there is more then one result we don't have to delete the children
700
+                // but update their parent. For group shares the new parent should always be
701
+                // the original group share and not the db entry with the unique name
702
+            } else if ((int)$item['share_type'] === self::$shareTypeGroupUserUnique) {
703
+                $newParent = $item['parent'];
704
+            } else {
705
+                $newParent = $item['id'];
706
+            }
707
+        }
708
+
709
+        if (!empty($toDelete)) {
710
+            self::unshareItem($toDelete, $newParent);
711
+            return true;
712
+        }
713
+        return false;
714
+    }
715
+
716
+    /**
717
+     * sent status if users got informed by mail about share
718
+     * @param string $itemType
719
+     * @param string $itemSource
720
+     * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
721
+     * @param string $recipient with whom was the file shared
722
+     * @param boolean $status
723
+     */
724
+    public static function setSendMailStatus($itemType, $itemSource, $shareType, $recipient, $status) {
725
+        $status = $status ? 1 : 0;
726
+
727
+        $query = \OC_DB::prepare(
728
+            'UPDATE `*PREFIX*share`
729 729
 					SET `mail_send` = ?
730 730
 					WHERE `item_type` = ? AND `item_source` = ? AND `share_type` = ? AND `share_with` = ?');
731 731
 
732
-		$result = $query->execute(array($status, $itemType, $itemSource, $shareType, $recipient));
733
-
734
-		if($result === false) {
735
-			\OCP\Util::writeLog(Share::class, 'Couldn\'t set send mail status', \OCP\Util::ERROR);
736
-		}
737
-	}
738
-
739
-	/**
740
-	 * validate expiration date if it meets all constraints
741
-	 *
742
-	 * @param string $expireDate well formatted date string, e.g. "DD-MM-YYYY"
743
-	 * @param string $shareTime timestamp when the file was shared
744
-	 * @param string $itemType
745
-	 * @param string $itemSource
746
-	 * @return \DateTime validated date
747
-	 * @throws \Exception when the expire date is in the past or further in the future then the enforced date
748
-	 */
749
-	private static function validateExpireDate($expireDate, $shareTime, $itemType, $itemSource) {
750
-		$l = \OC::$server->getL10N('lib');
751
-		$date = new \DateTime($expireDate);
752
-		$today = new \DateTime('now');
753
-
754
-		// if the user doesn't provide a share time we need to get it from the database
755
-		// fall-back mode to keep API stable, because the $shareTime parameter was added later
756
-		$defaultExpireDateEnforced = \OCP\Util::isDefaultExpireDateEnforced();
757
-		if ($defaultExpireDateEnforced && $shareTime === null) {
758
-			$items = self::getItemShared($itemType, $itemSource);
759
-			$firstItem = reset($items);
760
-			$shareTime = (int)$firstItem['stime'];
761
-		}
762
-
763
-		if ($defaultExpireDateEnforced) {
764
-			// initialize max date with share time
765
-			$maxDate = new \DateTime();
766
-			$maxDate->setTimestamp($shareTime);
767
-			$maxDays = \OC::$server->getConfig()->getAppValue('core', 'shareapi_expire_after_n_days', '7');
768
-			$maxDate->add(new \DateInterval('P' . $maxDays . 'D'));
769
-			if ($date > $maxDate) {
770
-				$warning = 'Cannot set expiration date. Shares cannot expire later than ' . $maxDays . ' after they have been shared';
771
-				$warning_t = $l->t('Cannot set expiration date. Shares cannot expire later than %s after they have been shared', array($maxDays));
772
-				\OCP\Util::writeLog(Share::class, $warning, \OCP\Util::WARN);
773
-				throw new \Exception($warning_t);
774
-			}
775
-		}
776
-
777
-		if ($date < $today) {
778
-			$message = 'Cannot set expiration date. Expiration date is in the past';
779
-			$message_t = $l->t('Cannot set expiration date. Expiration date is in the past');
780
-			\OCP\Util::writeLog(Share::class, $message, \OCP\Util::WARN);
781
-			throw new \Exception($message_t);
782
-		}
783
-
784
-		return $date;
785
-	}
786
-
787
-	/**
788
-	 * Checks whether a share has expired, calls unshareItem() if yes.
789
-	 * @param array $item Share data (usually database row)
790
-	 * @return boolean True if item was expired, false otherwise.
791
-	 */
792
-	protected static function expireItem(array $item) {
793
-
794
-		$result = false;
795
-
796
-		// only use default expiration date for link shares
797
-		if ((int) $item['share_type'] === self::SHARE_TYPE_LINK) {
798
-
799
-			// calculate expiration date
800
-			if (!empty($item['expiration'])) {
801
-				$userDefinedExpire = new \DateTime($item['expiration']);
802
-				$expires = $userDefinedExpire->getTimestamp();
803
-			} else {
804
-				$expires = null;
805
-			}
806
-
807
-
808
-			// get default expiration settings
809
-			$defaultSettings = Helper::getDefaultExpireSetting();
810
-			$expires = Helper::calculateExpireDate($defaultSettings, $item['stime'], $expires);
811
-
812
-
813
-			if (is_int($expires)) {
814
-				$now = time();
815
-				if ($now > $expires) {
816
-					self::unshareItem($item);
817
-					$result = true;
818
-				}
819
-			}
820
-		}
821
-		return $result;
822
-	}
823
-
824
-	/**
825
-	 * Unshares a share given a share data array
826
-	 * @param array $item Share data (usually database row)
827
-	 * @param int $newParent parent ID
828
-	 * @return null
829
-	 */
830
-	protected static function unshareItem(array $item, $newParent = null) {
831
-
832
-		$shareType = (int)$item['share_type'];
833
-		$shareWith = null;
834
-		if ($shareType !== \OCP\Share::SHARE_TYPE_LINK) {
835
-			$shareWith = $item['share_with'];
836
-		}
837
-
838
-		// Pass all the vars we have for now, they may be useful
839
-		$hookParams = array(
840
-			'id'            => $item['id'],
841
-			'itemType'      => $item['item_type'],
842
-			'itemSource'    => $item['item_source'],
843
-			'shareType'     => $shareType,
844
-			'shareWith'     => $shareWith,
845
-			'itemParent'    => $item['parent'],
846
-			'uidOwner'      => $item['uid_owner'],
847
-		);
848
-		if($item['item_type'] === 'file' || $item['item_type'] === 'folder') {
849
-			$hookParams['fileSource'] = $item['file_source'];
850
-			$hookParams['fileTarget'] = $item['file_target'];
851
-		}
852
-
853
-		\OC_Hook::emit(Share::class, 'pre_unshare', $hookParams);
854
-		$deletedShares = Helper::delete($item['id'], false, null, $newParent);
855
-		$deletedShares[] = $hookParams;
856
-		$hookParams['deletedShares'] = $deletedShares;
857
-		\OC_Hook::emit(Share::class, 'post_unshare', $hookParams);
858
-		if ((int)$item['share_type'] === \OCP\Share::SHARE_TYPE_REMOTE && \OC::$server->getUserSession()->getUser()) {
859
-			list(, $remote) = Helper::splitUserRemote($item['share_with']);
860
-			self::sendRemoteUnshare($remote, $item['id'], $item['token']);
861
-		}
862
-	}
863
-
864
-	/**
865
-	 * Get the backend class for the specified item type
866
-	 * @param string $itemType
867
-	 * @throws \Exception
868
-	 * @return \OCP\Share_Backend
869
-	 */
870
-	public static function getBackend($itemType) {
871
-		$l = \OC::$server->getL10N('lib');
872
-		if (isset(self::$backends[$itemType])) {
873
-			return self::$backends[$itemType];
874
-		} else if (isset(self::$backendTypes[$itemType]['class'])) {
875
-			$class = self::$backendTypes[$itemType]['class'];
876
-			if (class_exists($class)) {
877
-				self::$backends[$itemType] = new $class;
878
-				if (!(self::$backends[$itemType] instanceof \OCP\Share_Backend)) {
879
-					$message = 'Sharing backend %s must implement the interface OCP\Share_Backend';
880
-					$message_t = $l->t('Sharing backend %s must implement the interface OCP\Share_Backend', array($class));
881
-					\OCP\Util::writeLog(Share::class, sprintf($message, $class), \OCP\Util::ERROR);
882
-					throw new \Exception($message_t);
883
-				}
884
-				return self::$backends[$itemType];
885
-			} else {
886
-				$message = 'Sharing backend %s not found';
887
-				$message_t = $l->t('Sharing backend %s not found', array($class));
888
-				\OCP\Util::writeLog(Share::class, sprintf($message, $class), \OCP\Util::ERROR);
889
-				throw new \Exception($message_t);
890
-			}
891
-		}
892
-		$message = 'Sharing backend for %s not found';
893
-		$message_t = $l->t('Sharing backend for %s not found', array($itemType));
894
-		\OCP\Util::writeLog(Share::class, sprintf($message, $itemType), \OCP\Util::ERROR);
895
-		throw new \Exception($message_t);
896
-	}
897
-
898
-	/**
899
-	 * Check if resharing is allowed
900
-	 * @return boolean true if allowed or false
901
-	 *
902
-	 * Resharing is allowed by default if not configured
903
-	 */
904
-	public static function isResharingAllowed() {
905
-		if (!isset(self::$isResharingAllowed)) {
906
-			if (\OC::$server->getConfig()->getAppValue('core', 'shareapi_allow_resharing', 'yes') == 'yes') {
907
-				self::$isResharingAllowed = true;
908
-			} else {
909
-				self::$isResharingAllowed = false;
910
-			}
911
-		}
912
-		return self::$isResharingAllowed;
913
-	}
914
-
915
-	/**
916
-	 * Get a list of collection item types for the specified item type
917
-	 * @param string $itemType
918
-	 * @return array
919
-	 */
920
-	private static function getCollectionItemTypes($itemType) {
921
-		$collectionTypes = array($itemType);
922
-		foreach (self::$backendTypes as $type => $backend) {
923
-			if (in_array($backend['collectionOf'], $collectionTypes)) {
924
-				$collectionTypes[] = $type;
925
-			}
926
-		}
927
-		// TODO Add option for collections to be collection of themselves, only 'folder' does it now...
928
-		if (isset(self::$backendTypes[$itemType]) && (!self::getBackend($itemType) instanceof \OCP\Share_Backend_Collection || $itemType != 'folder')) {
929
-			unset($collectionTypes[0]);
930
-		}
931
-		// Return array if collections were found or the item type is a
932
-		// collection itself - collections can be inside collections
933
-		if (count($collectionTypes) > 0) {
934
-			return $collectionTypes;
935
-		}
936
-		return false;
937
-	}
938
-
939
-	/**
940
-	 * Get the owners of items shared with a user.
941
-	 *
942
-	 * @param string $user The user the items are shared with.
943
-	 * @param string $type The type of the items shared with the user.
944
-	 * @param boolean $includeCollections Include collection item types (optional)
945
-	 * @param boolean $includeOwner include owner in the list of users the item is shared with (optional)
946
-	 * @return array
947
-	 */
948
-	public static function getSharedItemsOwners($user, $type, $includeCollections = false, $includeOwner = false) {
949
-		// First, we find out if $type is part of a collection (and if that collection is part of
950
-		// another one and so on).
951
-		$collectionTypes = array();
952
-		if (!$includeCollections || !$collectionTypes = self::getCollectionItemTypes($type)) {
953
-			$collectionTypes[] = $type;
954
-		}
955
-
956
-		// Of these collection types, along with our original $type, we make a
957
-		// list of the ones for which a sharing backend has been registered.
958
-		// FIXME: Ideally, we wouldn't need to nest getItemsSharedWith in this loop but just call it
959
-		// with its $includeCollections parameter set to true. Unfortunately, this fails currently.
960
-		$allMaybeSharedItems = array();
961
-		foreach ($collectionTypes as $collectionType) {
962
-			if (isset(self::$backends[$collectionType])) {
963
-				$allMaybeSharedItems[$collectionType] = self::getItemsSharedWithUser(
964
-					$collectionType,
965
-					$user,
966
-					self::FORMAT_NONE
967
-				);
968
-			}
969
-		}
970
-
971
-		$owners = array();
972
-		if ($includeOwner) {
973
-			$owners[] = $user;
974
-		}
975
-
976
-		// We take a look at all shared items of the given $type (or of the collections it is part of)
977
-		// and find out their owners. Then, we gather the tags for the original $type from all owners,
978
-		// and return them as elements of a list that look like "Tag (owner)".
979
-		foreach ($allMaybeSharedItems as $collectionType => $maybeSharedItems) {
980
-			foreach ($maybeSharedItems as $sharedItem) {
981
-				if (isset($sharedItem['id'])) { //workaround for https://github.com/owncloud/core/issues/2814
982
-					$owners[] = $sharedItem['uid_owner'];
983
-				}
984
-			}
985
-		}
986
-
987
-		return $owners;
988
-	}
989
-
990
-	/**
991
-	 * Get shared items from the database
992
-	 * @param string $itemType
993
-	 * @param string $item Item source or target (optional)
994
-	 * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, SHARE_TYPE_LINK, $shareTypeUserAndGroups, or $shareTypeGroupUserUnique
995
-	 * @param string $shareWith User or group the item is being shared with
996
-	 * @param string $uidOwner User that is the owner of shared items (optional)
997
-	 * @param int $format Format to convert items to with formatItems() (optional)
998
-	 * @param mixed $parameters to pass to formatItems() (optional)
999
-	 * @param int $limit Number of items to return, -1 to return all matches (optional)
1000
-	 * @param boolean $includeCollections Include collection item types (optional)
1001
-	 * @param boolean $itemShareWithBySource (optional)
1002
-	 * @param boolean $checkExpireDate
1003
-	 * @return array
1004
-	 *
1005
-	 * See public functions getItem(s)... for parameter usage
1006
-	 *
1007
-	 */
1008
-	public static function getItems($itemType, $item = null, $shareType = null, $shareWith = null,
1009
-									$uidOwner = null, $format = self::FORMAT_NONE, $parameters = null, $limit = -1,
1010
-									$includeCollections = false, $itemShareWithBySource = false, $checkExpireDate  = true) {
1011
-		if (\OC::$server->getConfig()->getAppValue('core', 'shareapi_enabled', 'yes') != 'yes') {
1012
-			return array();
1013
-		}
1014
-		$backend = self::getBackend($itemType);
1015
-		$collectionTypes = false;
1016
-		// Get filesystem root to add it to the file target and remove from the
1017
-		// file source, match file_source with the file cache
1018
-		if ($itemType == 'file' || $itemType == 'folder') {
1019
-			if(!is_null($uidOwner)) {
1020
-				$root = \OC\Files\Filesystem::getRoot();
1021
-			} else {
1022
-				$root = '';
1023
-			}
1024
-			$where = 'INNER JOIN `*PREFIX*filecache` ON `file_source` = `*PREFIX*filecache`.`fileid` ';
1025
-			if (!isset($item)) {
1026
-				$where .= ' AND `file_target` IS NOT NULL ';
1027
-			}
1028
-			$where .= 'INNER JOIN `*PREFIX*storages` ON `numeric_id` = `*PREFIX*filecache`.`storage` ';
1029
-			$fileDependent = true;
1030
-			$queryArgs = array();
1031
-		} else {
1032
-			$fileDependent = false;
1033
-			$root = '';
1034
-			$collectionTypes = self::getCollectionItemTypes($itemType);
1035
-			if ($includeCollections && !isset($item) && $collectionTypes) {
1036
-				// If includeCollections is true, find collections of this item type, e.g. a music album contains songs
1037
-				if (!in_array($itemType, $collectionTypes)) {
1038
-					$itemTypes = array_merge(array($itemType), $collectionTypes);
1039
-				} else {
1040
-					$itemTypes = $collectionTypes;
1041
-				}
1042
-				$placeholders = join(',', array_fill(0, count($itemTypes), '?'));
1043
-				$where = ' WHERE `item_type` IN ('.$placeholders.'))';
1044
-				$queryArgs = $itemTypes;
1045
-			} else {
1046
-				$where = ' WHERE `item_type` = ?';
1047
-				$queryArgs = array($itemType);
1048
-			}
1049
-		}
1050
-		if (\OC::$server->getConfig()->getAppValue('core', 'shareapi_allow_links', 'yes') !== 'yes') {
1051
-			$where .= ' AND `share_type` != ?';
1052
-			$queryArgs[] = self::SHARE_TYPE_LINK;
1053
-		}
1054
-		if (isset($shareType)) {
1055
-			// Include all user and group items
1056
-			if ($shareType == self::$shareTypeUserAndGroups && isset($shareWith)) {
1057
-				$where .= ' AND ((`share_type` in (?, ?) AND `share_with` = ?) ';
1058
-				$queryArgs[] = self::SHARE_TYPE_USER;
1059
-				$queryArgs[] = self::$shareTypeGroupUserUnique;
1060
-				$queryArgs[] = $shareWith;
1061
-
1062
-				$user = \OC::$server->getUserManager()->get($shareWith);
1063
-				$groups = [];
1064
-				if ($user) {
1065
-					$groups = \OC::$server->getGroupManager()->getUserGroupIds($user);
1066
-				}
1067
-				if (!empty($groups)) {
1068
-					$placeholders = join(',', array_fill(0, count($groups), '?'));
1069
-					$where .= ' OR (`share_type` = ? AND `share_with` IN ('.$placeholders.')) ';
1070
-					$queryArgs[] = self::SHARE_TYPE_GROUP;
1071
-					$queryArgs = array_merge($queryArgs, $groups);
1072
-				}
1073
-				$where .= ')';
1074
-				// Don't include own group shares
1075
-				$where .= ' AND `uid_owner` != ?';
1076
-				$queryArgs[] = $shareWith;
1077
-			} else {
1078
-				$where .= ' AND `share_type` = ?';
1079
-				$queryArgs[] = $shareType;
1080
-				if (isset($shareWith)) {
1081
-					$where .= ' AND `share_with` = ?';
1082
-					$queryArgs[] = $shareWith;
1083
-				}
1084
-			}
1085
-		}
1086
-		if (isset($uidOwner)) {
1087
-			$where .= ' AND `uid_owner` = ?';
1088
-			$queryArgs[] = $uidOwner;
1089
-			if (!isset($shareType)) {
1090
-				// Prevent unique user targets for group shares from being selected
1091
-				$where .= ' AND `share_type` != ?';
1092
-				$queryArgs[] = self::$shareTypeGroupUserUnique;
1093
-			}
1094
-			if ($fileDependent) {
1095
-				$column = 'file_source';
1096
-			} else {
1097
-				$column = 'item_source';
1098
-			}
1099
-		} else {
1100
-			if ($fileDependent) {
1101
-				$column = 'file_target';
1102
-			} else {
1103
-				$column = 'item_target';
1104
-			}
1105
-		}
1106
-		if (isset($item)) {
1107
-			$collectionTypes = self::getCollectionItemTypes($itemType);
1108
-			if ($includeCollections && $collectionTypes && !in_array('folder', $collectionTypes)) {
1109
-				$where .= ' AND (';
1110
-			} else {
1111
-				$where .= ' AND';
1112
-			}
1113
-			// If looking for own shared items, check item_source else check item_target
1114
-			if (isset($uidOwner) || $itemShareWithBySource) {
1115
-				// If item type is a file, file source needs to be checked in case the item was converted
1116
-				if ($fileDependent) {
1117
-					$where .= ' `file_source` = ?';
1118
-					$column = 'file_source';
1119
-				} else {
1120
-					$where .= ' `item_source` = ?';
1121
-					$column = 'item_source';
1122
-				}
1123
-			} else {
1124
-				if ($fileDependent) {
1125
-					$where .= ' `file_target` = ?';
1126
-					$item = \OC\Files\Filesystem::normalizePath($item);
1127
-				} else {
1128
-					$where .= ' `item_target` = ?';
1129
-				}
1130
-			}
1131
-			$queryArgs[] = $item;
1132
-			if ($includeCollections && $collectionTypes && !in_array('folder', $collectionTypes)) {
1133
-				$placeholders = join(',', array_fill(0, count($collectionTypes), '?'));
1134
-				$where .= ' OR `item_type` IN ('.$placeholders.'))';
1135
-				$queryArgs = array_merge($queryArgs, $collectionTypes);
1136
-			}
1137
-		}
1138
-
1139
-		if ($shareType == self::$shareTypeUserAndGroups && $limit === 1) {
1140
-			// Make sure the unique user target is returned if it exists,
1141
-			// unique targets should follow the group share in the database
1142
-			// If the limit is not 1, the filtering can be done later
1143
-			$where .= ' ORDER BY `*PREFIX*share`.`id` DESC';
1144
-		} else {
1145
-			$where .= ' ORDER BY `*PREFIX*share`.`id` ASC';
1146
-		}
1147
-
1148
-		if ($limit != -1 && !$includeCollections) {
1149
-			// The limit must be at least 3, because filtering needs to be done
1150
-			if ($limit < 3) {
1151
-				$queryLimit = 3;
1152
-			} else {
1153
-				$queryLimit = $limit;
1154
-			}
1155
-		} else {
1156
-			$queryLimit = null;
1157
-		}
1158
-		$select = self::createSelectStatement($format, $fileDependent, $uidOwner);
1159
-		$root = strlen($root);
1160
-		$query = \OC_DB::prepare('SELECT '.$select.' FROM `*PREFIX*share` '.$where, $queryLimit);
1161
-		$result = $query->execute($queryArgs);
1162
-		if ($result === false) {
1163
-			\OCP\Util::writeLog(Share::class,
1164
-				\OC_DB::getErrorMessage() . ', select=' . $select . ' where=',
1165
-				\OCP\Util::ERROR);
1166
-		}
1167
-		$items = array();
1168
-		$targets = array();
1169
-		$switchedItems = array();
1170
-		$mounts = array();
1171
-		while ($row = $result->fetchRow()) {
1172
-			self::transformDBResults($row);
1173
-			// Filter out duplicate group shares for users with unique targets
1174
-			if ($fileDependent && !self::isFileReachable($row['path'], $row['storage_id'])) {
1175
-				continue;
1176
-			}
1177
-			if ($row['share_type'] == self::$shareTypeGroupUserUnique && isset($items[$row['parent']])) {
1178
-				$row['share_type'] = self::SHARE_TYPE_GROUP;
1179
-				$row['unique_name'] = true; // remember that we use a unique name for this user
1180
-				$row['share_with'] = $items[$row['parent']]['share_with'];
1181
-				// if the group share was unshared from the user we keep the permission, otherwise
1182
-				// we take the permission from the parent because this is always the up-to-date
1183
-				// permission for the group share
1184
-				if ($row['permissions'] > 0) {
1185
-					$row['permissions'] = $items[$row['parent']]['permissions'];
1186
-				}
1187
-				// Remove the parent group share
1188
-				unset($items[$row['parent']]);
1189
-				if ($row['permissions'] == 0) {
1190
-					continue;
1191
-				}
1192
-			} else if (!isset($uidOwner)) {
1193
-				// Check if the same target already exists
1194
-				if (isset($targets[$row['id']])) {
1195
-					// Check if the same owner shared with the user twice
1196
-					// through a group and user share - this is allowed
1197
-					$id = $targets[$row['id']];
1198
-					if (isset($items[$id]) && $items[$id]['uid_owner'] == $row['uid_owner']) {
1199
-						// Switch to group share type to ensure resharing conditions aren't bypassed
1200
-						if ($items[$id]['share_type'] != self::SHARE_TYPE_GROUP) {
1201
-							$items[$id]['share_type'] = self::SHARE_TYPE_GROUP;
1202
-							$items[$id]['share_with'] = $row['share_with'];
1203
-						}
1204
-						// Switch ids if sharing permission is granted on only
1205
-						// one share to ensure correct parent is used if resharing
1206
-						if (~(int)$items[$id]['permissions'] & \OCP\Constants::PERMISSION_SHARE
1207
-							&& (int)$row['permissions'] & \OCP\Constants::PERMISSION_SHARE) {
1208
-							$items[$row['id']] = $items[$id];
1209
-							$switchedItems[$id] = $row['id'];
1210
-							unset($items[$id]);
1211
-							$id = $row['id'];
1212
-						}
1213
-						$items[$id]['permissions'] |= (int)$row['permissions'];
1214
-
1215
-					}
1216
-					continue;
1217
-				} elseif (!empty($row['parent'])) {
1218
-					$targets[$row['parent']] = $row['id'];
1219
-				}
1220
-			}
1221
-			// Remove root from file source paths if retrieving own shared items
1222
-			if (isset($uidOwner) && isset($row['path'])) {
1223
-				if (isset($row['parent'])) {
1224
-					$query = \OC_DB::prepare('SELECT `file_target` FROM `*PREFIX*share` WHERE `id` = ?');
1225
-					$parentResult = $query->execute(array($row['parent']));
1226
-					if ($result === false) {
1227
-						\OCP\Util::writeLog(Share::class, 'Can\'t select parent: ' .
1228
-							\OC_DB::getErrorMessage() . ', select=' . $select . ' where=' . $where,
1229
-							\OCP\Util::ERROR);
1230
-					} else {
1231
-						$parentRow = $parentResult->fetchRow();
1232
-						$tmpPath = $parentRow['file_target'];
1233
-						// find the right position where the row path continues from the target path
1234
-						$pos = strrpos($row['path'], $parentRow['file_target']);
1235
-						$subPath = substr($row['path'], $pos);
1236
-						$splitPath = explode('/', $subPath);
1237
-						foreach (array_slice($splitPath, 2) as $pathPart) {
1238
-							$tmpPath = $tmpPath . '/' . $pathPart;
1239
-						}
1240
-						$row['path'] = $tmpPath;
1241
-					}
1242
-				} else {
1243
-					if (!isset($mounts[$row['storage']])) {
1244
-						$mountPoints = \OC\Files\Filesystem::getMountByNumericId($row['storage']);
1245
-						if (is_array($mountPoints) && !empty($mountPoints)) {
1246
-							$mounts[$row['storage']] = current($mountPoints);
1247
-						}
1248
-					}
1249
-					if (!empty($mounts[$row['storage']])) {
1250
-						$path = $mounts[$row['storage']]->getMountPoint().$row['path'];
1251
-						$relPath = substr($path, $root); // path relative to data/user
1252
-						$row['path'] = rtrim($relPath, '/');
1253
-					}
1254
-				}
1255
-			}
1256
-
1257
-			if($checkExpireDate) {
1258
-				if (self::expireItem($row)) {
1259
-					continue;
1260
-				}
1261
-			}
1262
-			// Check if resharing is allowed, if not remove share permission
1263
-			if (isset($row['permissions']) && (!self::isResharingAllowed() | \OCP\Util::isSharingDisabledForUser())) {
1264
-				$row['permissions'] &= ~\OCP\Constants::PERMISSION_SHARE;
1265
-			}
1266
-			// Add display names to result
1267
-			$row['share_with_displayname'] = $row['share_with'];
1268
-			if ( isset($row['share_with']) && $row['share_with'] != '' &&
1269
-				$row['share_type'] === self::SHARE_TYPE_USER) {
1270
-				$row['share_with_displayname'] = \OCP\User::getDisplayName($row['share_with']);
1271
-			} else if(isset($row['share_with']) && $row['share_with'] != '' &&
1272
-				$row['share_type'] === self::SHARE_TYPE_REMOTE) {
1273
-				$addressBookEntries = \OC::$server->getContactsManager()->search($row['share_with'], ['CLOUD']);
1274
-				foreach ($addressBookEntries as $entry) {
1275
-					foreach ($entry['CLOUD'] as $cloudID) {
1276
-						if ($cloudID === $row['share_with']) {
1277
-							$row['share_with_displayname'] = $entry['FN'];
1278
-						}
1279
-					}
1280
-				}
1281
-			}
1282
-			if ( isset($row['uid_owner']) && $row['uid_owner'] != '') {
1283
-				$row['displayname_owner'] = \OCP\User::getDisplayName($row['uid_owner']);
1284
-			}
1285
-
1286
-			if ($row['permissions'] > 0) {
1287
-				$items[$row['id']] = $row;
1288
-			}
1289
-
1290
-		}
1291
-
1292
-		// group items if we are looking for items shared with the current user
1293
-		if (isset($shareWith) && $shareWith === \OCP\User::getUser()) {
1294
-			$items = self::groupItems($items, $itemType);
1295
-		}
1296
-
1297
-		if (!empty($items)) {
1298
-			$collectionItems = array();
1299
-			foreach ($items as &$row) {
1300
-				// Return only the item instead of a 2-dimensional array
1301
-				if ($limit == 1 && $row[$column] == $item && ($row['item_type'] == $itemType || $itemType == 'file')) {
1302
-					if ($format == self::FORMAT_NONE) {
1303
-						return $row;
1304
-					} else {
1305
-						break;
1306
-					}
1307
-				}
1308
-				// Check if this is a collection of the requested item type
1309
-				if ($includeCollections && $collectionTypes && $row['item_type'] !== 'folder' && in_array($row['item_type'], $collectionTypes)) {
1310
-					if (($collectionBackend = self::getBackend($row['item_type']))
1311
-						&& $collectionBackend instanceof \OCP\Share_Backend_Collection) {
1312
-						// Collections can be inside collections, check if the item is a collection
1313
-						if (isset($item) && $row['item_type'] == $itemType && $row[$column] == $item) {
1314
-							$collectionItems[] = $row;
1315
-						} else {
1316
-							$collection = array();
1317
-							$collection['item_type'] = $row['item_type'];
1318
-							if ($row['item_type'] == 'file' || $row['item_type'] == 'folder') {
1319
-								$collection['path'] = basename($row['path']);
1320
-							}
1321
-							$row['collection'] = $collection;
1322
-							// Fetch all of the children sources
1323
-							$children = $collectionBackend->getChildren($row[$column]);
1324
-							foreach ($children as $child) {
1325
-								$childItem = $row;
1326
-								$childItem['item_type'] = $itemType;
1327
-								if ($row['item_type'] != 'file' && $row['item_type'] != 'folder') {
1328
-									$childItem['item_source'] = $child['source'];
1329
-									$childItem['item_target'] = $child['target'];
1330
-								}
1331
-								if ($backend instanceof \OCP\Share_Backend_File_Dependent) {
1332
-									if ($row['item_type'] == 'file' || $row['item_type'] == 'folder') {
1333
-										$childItem['file_source'] = $child['source'];
1334
-									} else { // TODO is this really needed if we already know that we use the file backend?
1335
-										$meta = \OC\Files\Filesystem::getFileInfo($child['file_path']);
1336
-										$childItem['file_source'] = $meta['fileid'];
1337
-									}
1338
-									$childItem['file_target'] =
1339
-										\OC\Files\Filesystem::normalizePath($child['file_path']);
1340
-								}
1341
-								if (isset($item)) {
1342
-									if ($childItem[$column] == $item) {
1343
-										// Return only the item instead of a 2-dimensional array
1344
-										if ($limit == 1) {
1345
-											if ($format == self::FORMAT_NONE) {
1346
-												return $childItem;
1347
-											} else {
1348
-												// Unset the items array and break out of both loops
1349
-												$items = array();
1350
-												$items[] = $childItem;
1351
-												break 2;
1352
-											}
1353
-										} else {
1354
-											$collectionItems[] = $childItem;
1355
-										}
1356
-									}
1357
-								} else {
1358
-									$collectionItems[] = $childItem;
1359
-								}
1360
-							}
1361
-						}
1362
-					}
1363
-					// Remove collection item
1364
-					$toRemove = $row['id'];
1365
-					if (array_key_exists($toRemove, $switchedItems)) {
1366
-						$toRemove = $switchedItems[$toRemove];
1367
-					}
1368
-					unset($items[$toRemove]);
1369
-				} elseif ($includeCollections && $collectionTypes && in_array($row['item_type'], $collectionTypes)) {
1370
-					// FIXME: Thats a dirty hack to improve file sharing performance,
1371
-					// see github issue #10588 for more details
1372
-					// Need to find a solution which works for all back-ends
1373
-					$collectionBackend = self::getBackend($row['item_type']);
1374
-					$sharedParents = $collectionBackend->getParents($row['item_source']);
1375
-					foreach ($sharedParents as $parent) {
1376
-						$collectionItems[] = $parent;
1377
-					}
1378
-				}
1379
-			}
1380
-			if (!empty($collectionItems)) {
1381
-				$collectionItems = array_unique($collectionItems, SORT_REGULAR);
1382
-				$items = array_merge($items, $collectionItems);
1383
-			}
1384
-
1385
-			// filter out invalid items, these can appear when subshare entries exist
1386
-			// for a group in which the requested user isn't a member any more
1387
-			$items = array_filter($items, function($item) {
1388
-				return $item['share_type'] !== self::$shareTypeGroupUserUnique;
1389
-			});
1390
-
1391
-			return self::formatResult($items, $column, $backend, $format, $parameters);
1392
-		} elseif ($includeCollections && $collectionTypes && in_array('folder', $collectionTypes)) {
1393
-			// FIXME: Thats a dirty hack to improve file sharing performance,
1394
-			// see github issue #10588 for more details
1395
-			// Need to find a solution which works for all back-ends
1396
-			$collectionItems = array();
1397
-			$collectionBackend = self::getBackend('folder');
1398
-			$sharedParents = $collectionBackend->getParents($item, $shareWith, $uidOwner);
1399
-			foreach ($sharedParents as $parent) {
1400
-				$collectionItems[] = $parent;
1401
-			}
1402
-			if ($limit === 1) {
1403
-				return reset($collectionItems);
1404
-			}
1405
-			return self::formatResult($collectionItems, $column, $backend, $format, $parameters);
1406
-		}
1407
-
1408
-		return array();
1409
-	}
1410
-
1411
-	/**
1412
-	 * group items with link to the same source
1413
-	 *
1414
-	 * @param array $items
1415
-	 * @param string $itemType
1416
-	 * @return array of grouped items
1417
-	 */
1418
-	protected static function groupItems($items, $itemType) {
1419
-
1420
-		$fileSharing = ($itemType === 'file' || $itemType === 'folder') ? true : false;
1421
-
1422
-		$result = array();
1423
-
1424
-		foreach ($items as $item) {
1425
-			$grouped = false;
1426
-			foreach ($result as $key => $r) {
1427
-				// for file/folder shares we need to compare file_source, otherwise we compare item_source
1428
-				// only group shares if they already point to the same target, otherwise the file where shared
1429
-				// before grouping of shares was added. In this case we don't group them toi avoid confusions
1430
-				if (( $fileSharing && $item['file_source'] === $r['file_source'] && $item['file_target'] === $r['file_target']) ||
1431
-					(!$fileSharing && $item['item_source'] === $r['item_source'] && $item['item_target'] === $r['item_target'])) {
1432
-					// add the first item to the list of grouped shares
1433
-					if (!isset($result[$key]['grouped'])) {
1434
-						$result[$key]['grouped'][] = $result[$key];
1435
-					}
1436
-					$result[$key]['permissions'] = (int) $item['permissions'] | (int) $r['permissions'];
1437
-					$result[$key]['grouped'][] = $item;
1438
-					$grouped = true;
1439
-					break;
1440
-				}
1441
-			}
1442
-
1443
-			if (!$grouped) {
1444
-				$result[] = $item;
1445
-			}
1446
-
1447
-		}
1448
-
1449
-		return $result;
1450
-	}
1451
-
1452
-	/**
1453
-	 * Put shared item into the database
1454
-	 * @param string $itemType Item type
1455
-	 * @param string $itemSource Item source
1456
-	 * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
1457
-	 * @param string $shareWith User or group the item is being shared with
1458
-	 * @param string $uidOwner User that is the owner of shared item
1459
-	 * @param int $permissions CRUDS permissions
1460
-	 * @param boolean|array $parentFolder Parent folder target (optional)
1461
-	 * @param string $token (optional)
1462
-	 * @param string $itemSourceName name of the source item (optional)
1463
-	 * @param \DateTime $expirationDate (optional)
1464
-	 * @throws \Exception
1465
-	 * @return mixed id of the new share or false
1466
-	 */
1467
-	private static function put($itemType, $itemSource, $shareType, $shareWith, $uidOwner,
1468
-								$permissions, $parentFolder = null, $token = null, $itemSourceName = null, \DateTime $expirationDate = null) {
1469
-
1470
-		$queriesToExecute = array();
1471
-		$suggestedItemTarget = null;
1472
-		$groupFileTarget = $fileTarget = $suggestedFileTarget = $filePath = '';
1473
-		$groupItemTarget = $itemTarget = $fileSource = $parent = 0;
1474
-
1475
-		$result = self::checkReshare($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, $itemSourceName, $expirationDate);
1476
-		if(!empty($result)) {
1477
-			$parent = $result['parent'];
1478
-			$itemSource = $result['itemSource'];
1479
-			$fileSource = $result['fileSource'];
1480
-			$suggestedItemTarget = $result['suggestedItemTarget'];
1481
-			$suggestedFileTarget = $result['suggestedFileTarget'];
1482
-			$filePath = $result['filePath'];
1483
-		}
1484
-
1485
-		$isGroupShare = false;
1486
-		if ($shareType == self::SHARE_TYPE_GROUP) {
1487
-			$isGroupShare = true;
1488
-			if (isset($shareWith['users'])) {
1489
-				$users = $shareWith['users'];
1490
-			} else {
1491
-				$group = \OC::$server->getGroupManager()->get($shareWith['group']);
1492
-				if ($group) {
1493
-					$users = $group->searchUsers('', -1, 0);
1494
-					$userIds = [];
1495
-					foreach ($users as $user) {
1496
-						$userIds[] = $user->getUID();
1497
-					}
1498
-					$users = $userIds;
1499
-				} else {
1500
-					$users = [];
1501
-				}
1502
-			}
1503
-			// remove current user from list
1504
-			if (in_array(\OCP\User::getUser(), $users)) {
1505
-				unset($users[array_search(\OCP\User::getUser(), $users)]);
1506
-			}
1507
-			$groupItemTarget = Helper::generateTarget($itemType, $itemSource,
1508
-				$shareType, $shareWith['group'], $uidOwner, $suggestedItemTarget);
1509
-			$groupFileTarget = Helper::generateTarget($itemType, $itemSource,
1510
-				$shareType, $shareWith['group'], $uidOwner, $filePath);
1511
-
1512
-			// add group share to table and remember the id as parent
1513
-			$queriesToExecute['groupShare'] = array(
1514
-				'itemType'			=> $itemType,
1515
-				'itemSource'		=> $itemSource,
1516
-				'itemTarget'		=> $groupItemTarget,
1517
-				'shareType'			=> $shareType,
1518
-				'shareWith'			=> $shareWith['group'],
1519
-				'uidOwner'			=> $uidOwner,
1520
-				'permissions'		=> $permissions,
1521
-				'shareTime'			=> time(),
1522
-				'fileSource'		=> $fileSource,
1523
-				'fileTarget'		=> $groupFileTarget,
1524
-				'token'				=> $token,
1525
-				'parent'			=> $parent,
1526
-				'expiration'		=> $expirationDate,
1527
-			);
1528
-
1529
-		} else {
1530
-			$users = array($shareWith);
1531
-			$itemTarget = Helper::generateTarget($itemType, $itemSource, $shareType, $shareWith, $uidOwner,
1532
-				$suggestedItemTarget);
1533
-		}
1534
-
1535
-		$run = true;
1536
-		$error = '';
1537
-		$preHookData = array(
1538
-			'itemType' => $itemType,
1539
-			'itemSource' => $itemSource,
1540
-			'shareType' => $shareType,
1541
-			'uidOwner' => $uidOwner,
1542
-			'permissions' => $permissions,
1543
-			'fileSource' => $fileSource,
1544
-			'expiration' => $expirationDate,
1545
-			'token' => $token,
1546
-			'run' => &$run,
1547
-			'error' => &$error
1548
-		);
1549
-
1550
-		$preHookData['itemTarget'] = ($isGroupShare) ? $groupItemTarget : $itemTarget;
1551
-		$preHookData['shareWith'] = ($isGroupShare) ? $shareWith['group'] : $shareWith;
1552
-
1553
-		\OC_Hook::emit(Share::class, 'pre_shared', $preHookData);
1554
-
1555
-		if ($run === false) {
1556
-			throw new \Exception($error);
1557
-		}
1558
-
1559
-		foreach ($users as $user) {
1560
-			$sourceId = ($itemType === 'file' || $itemType === 'folder') ? $fileSource : $itemSource;
1561
-			$sourceExists = self::getItemSharedWithBySource($itemType, $sourceId, self::FORMAT_NONE, null, true, $user);
1562
-
1563
-			$userShareType = ($isGroupShare) ? self::$shareTypeGroupUserUnique : $shareType;
1564
-
1565
-			if ($sourceExists && $sourceExists['item_source'] === $itemSource) {
1566
-				$fileTarget = $sourceExists['file_target'];
1567
-				$itemTarget = $sourceExists['item_target'];
1568
-
1569
-				// for group shares we don't need a additional entry if the target is the same
1570
-				if($isGroupShare && $groupItemTarget === $itemTarget) {
1571
-					continue;
1572
-				}
1573
-
1574
-			} elseif(!$sourceExists && !$isGroupShare)  {
1575
-
1576
-				$itemTarget = Helper::generateTarget($itemType, $itemSource, $userShareType, $user,
1577
-					$uidOwner, $suggestedItemTarget, $parent);
1578
-				if (isset($fileSource)) {
1579
-					if ($parentFolder) {
1580
-						if ($parentFolder === true) {
1581
-							$fileTarget = Helper::generateTarget('file', $filePath, $userShareType, $user,
1582
-								$uidOwner, $suggestedFileTarget, $parent);
1583
-							if ($fileTarget != $groupFileTarget) {
1584
-								$parentFolders[$user]['folder'] = $fileTarget;
1585
-							}
1586
-						} else if (isset($parentFolder[$user])) {
1587
-							$fileTarget = $parentFolder[$user]['folder'].$itemSource;
1588
-							$parent = $parentFolder[$user]['id'];
1589
-						}
1590
-					} else {
1591
-						$fileTarget = Helper::generateTarget('file', $filePath, $userShareType,
1592
-							$user, $uidOwner, $suggestedFileTarget, $parent);
1593
-					}
1594
-				} else {
1595
-					$fileTarget = null;
1596
-				}
1597
-
1598
-			} else {
1599
-
1600
-				// group share which doesn't exists until now, check if we need a unique target for this user
1601
-
1602
-				$itemTarget = Helper::generateTarget($itemType, $itemSource, self::SHARE_TYPE_USER, $user,
1603
-					$uidOwner, $suggestedItemTarget, $parent);
1604
-
1605
-				// do we also need a file target
1606
-				if (isset($fileSource)) {
1607
-					$fileTarget = Helper::generateTarget('file', $filePath, self::SHARE_TYPE_USER, $user,
1608
-						$uidOwner, $suggestedFileTarget, $parent);
1609
-				} else {
1610
-					$fileTarget = null;
1611
-				}
1612
-
1613
-				if (($itemTarget === $groupItemTarget) &&
1614
-					(!isset($fileSource) || $fileTarget === $groupFileTarget)) {
1615
-					continue;
1616
-				}
1617
-			}
1618
-
1619
-			$queriesToExecute[] = array(
1620
-				'itemType'			=> $itemType,
1621
-				'itemSource'		=> $itemSource,
1622
-				'itemTarget'		=> $itemTarget,
1623
-				'shareType'			=> $userShareType,
1624
-				'shareWith'			=> $user,
1625
-				'uidOwner'			=> $uidOwner,
1626
-				'permissions'		=> $permissions,
1627
-				'shareTime'			=> time(),
1628
-				'fileSource'		=> $fileSource,
1629
-				'fileTarget'		=> $fileTarget,
1630
-				'token'				=> $token,
1631
-				'parent'			=> $parent,
1632
-				'expiration'		=> $expirationDate,
1633
-			);
1634
-
1635
-		}
1636
-
1637
-		$id = false;
1638
-		if ($isGroupShare) {
1639
-			$id = self::insertShare($queriesToExecute['groupShare']);
1640
-			// Save this id, any extra rows for this group share will need to reference it
1641
-			$parent = \OC::$server->getDatabaseConnection()->lastInsertId('*PREFIX*share');
1642
-			unset($queriesToExecute['groupShare']);
1643
-		}
1644
-
1645
-		foreach ($queriesToExecute as $shareQuery) {
1646
-			$shareQuery['parent'] = $parent;
1647
-			$id = self::insertShare($shareQuery);
1648
-		}
1649
-
1650
-		$postHookData = array(
1651
-			'itemType' => $itemType,
1652
-			'itemSource' => $itemSource,
1653
-			'parent' => $parent,
1654
-			'shareType' => $shareType,
1655
-			'uidOwner' => $uidOwner,
1656
-			'permissions' => $permissions,
1657
-			'fileSource' => $fileSource,
1658
-			'id' => $parent,
1659
-			'token' => $token,
1660
-			'expirationDate' => $expirationDate,
1661
-		);
1662
-
1663
-		$postHookData['shareWith'] = ($isGroupShare) ? $shareWith['group'] : $shareWith;
1664
-		$postHookData['itemTarget'] = ($isGroupShare) ? $groupItemTarget : $itemTarget;
1665
-		$postHookData['fileTarget'] = ($isGroupShare) ? $groupFileTarget : $fileTarget;
1666
-
1667
-		\OC_Hook::emit(Share::class, 'post_shared', $postHookData);
1668
-
1669
-
1670
-		return $id ? $id : false;
1671
-	}
1672
-
1673
-	/**
1674
-	 * @param string $itemType
1675
-	 * @param string $itemSource
1676
-	 * @param int $shareType
1677
-	 * @param string $shareWith
1678
-	 * @param string $uidOwner
1679
-	 * @param int $permissions
1680
-	 * @param string|null $itemSourceName
1681
-	 * @param null|\DateTime $expirationDate
1682
-	 */
1683
-	private static function checkReshare($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, $itemSourceName, $expirationDate) {
1684
-		$backend = self::getBackend($itemType);
1685
-
1686
-		$l = \OC::$server->getL10N('lib');
1687
-		$result = array();
1688
-
1689
-		$column = ($itemType === 'file' || $itemType === 'folder') ? 'file_source' : 'item_source';
1690
-
1691
-		$checkReshare = self::getItemSharedWithBySource($itemType, $itemSource, self::FORMAT_NONE, null, true);
1692
-		if ($checkReshare) {
1693
-			// Check if attempting to share back to owner
1694
-			if ($checkReshare['uid_owner'] == $shareWith && $shareType == self::SHARE_TYPE_USER) {
1695
-				$message = 'Sharing %s failed, because the user %s is the original sharer';
1696
-				$message_t = $l->t('Sharing failed, because the user %s is the original sharer', [$shareWith]);
1697
-
1698
-				\OCP\Util::writeLog(Share::class, sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG);
1699
-				throw new \Exception($message_t);
1700
-			}
1701
-		}
1702
-
1703
-		if ($checkReshare && $checkReshare['uid_owner'] !== \OC_User::getUser()) {
1704
-			// Check if share permissions is granted
1705
-			if (self::isResharingAllowed() && (int)$checkReshare['permissions'] & \OCP\Constants::PERMISSION_SHARE) {
1706
-				if (~(int)$checkReshare['permissions'] & $permissions) {
1707
-					$message = 'Sharing %s failed, because the permissions exceed permissions granted to %s';
1708
-					$message_t = $l->t('Sharing %s failed, because the permissions exceed permissions granted to %s', array($itemSourceName, $uidOwner));
1709
-
1710
-					\OCP\Util::writeLog(Share::class, sprintf($message, $itemSourceName, $uidOwner), \OCP\Util::DEBUG);
1711
-					throw new \Exception($message_t);
1712
-				} else {
1713
-					// TODO Don't check if inside folder
1714
-					$result['parent'] = $checkReshare['id'];
1715
-
1716
-					$result['expirationDate'] = $expirationDate;
1717
-					// $checkReshare['expiration'] could be null and then is always less than any value
1718
-					if(isset($checkReshare['expiration']) && $checkReshare['expiration'] < $expirationDate) {
1719
-						$result['expirationDate'] = $checkReshare['expiration'];
1720
-					}
1721
-
1722
-					// only suggest the same name as new target if it is a reshare of the
1723
-					// same file/folder and not the reshare of a child
1724
-					if ($checkReshare[$column] === $itemSource) {
1725
-						$result['filePath'] = $checkReshare['file_target'];
1726
-						$result['itemSource'] = $checkReshare['item_source'];
1727
-						$result['fileSource'] = $checkReshare['file_source'];
1728
-						$result['suggestedItemTarget'] = $checkReshare['item_target'];
1729
-						$result['suggestedFileTarget'] = $checkReshare['file_target'];
1730
-					} else {
1731
-						$result['filePath'] = ($backend instanceof \OCP\Share_Backend_File_Dependent) ? $backend->getFilePath($itemSource, $uidOwner) : null;
1732
-						$result['suggestedItemTarget'] = null;
1733
-						$result['suggestedFileTarget'] = null;
1734
-						$result['itemSource'] = $itemSource;
1735
-						$result['fileSource'] = ($backend instanceof \OCP\Share_Backend_File_Dependent) ? $itemSource : null;
1736
-					}
1737
-				}
1738
-			} else {
1739
-				$message = 'Sharing %s failed, because resharing is not allowed';
1740
-				$message_t = $l->t('Sharing %s failed, because resharing is not allowed', array($itemSourceName));
1741
-
1742
-				\OCP\Util::writeLog(Share::class, sprintf($message, $itemSourceName), \OCP\Util::DEBUG);
1743
-				throw new \Exception($message_t);
1744
-			}
1745
-		} else {
1746
-			$result['parent'] = null;
1747
-			$result['suggestedItemTarget'] = null;
1748
-			$result['suggestedFileTarget'] = null;
1749
-			$result['itemSource'] = $itemSource;
1750
-			$result['expirationDate'] = $expirationDate;
1751
-			if (!$backend->isValidSource($itemSource, $uidOwner)) {
1752
-				$message = 'Sharing %s failed, because the sharing backend for '
1753
-					.'%s could not find its source';
1754
-				$message_t = $l->t('Sharing %s failed, because the sharing backend for %s could not find its source', array($itemSource, $itemType));
1755
-				\OCP\Util::writeLog(Share::class, sprintf($message, $itemSource, $itemType), \OCP\Util::DEBUG);
1756
-				throw new \Exception($message_t);
1757
-			}
1758
-			if ($backend instanceof \OCP\Share_Backend_File_Dependent) {
1759
-				$result['filePath'] = $backend->getFilePath($itemSource, $uidOwner);
1760
-				if ($itemType == 'file' || $itemType == 'folder') {
1761
-					$result['fileSource'] = $itemSource;
1762
-				} else {
1763
-					$meta = \OC\Files\Filesystem::getFileInfo($result['filePath']);
1764
-					$result['fileSource'] = $meta['fileid'];
1765
-				}
1766
-				if ($result['fileSource'] == -1) {
1767
-					$message = 'Sharing %s failed, because the file could not be found in the file cache';
1768
-					$message_t = $l->t('Sharing %s failed, because the file could not be found in the file cache', array($itemSource));
1769
-
1770
-					\OCP\Util::writeLog(Share::class, sprintf($message, $itemSource), \OCP\Util::DEBUG);
1771
-					throw new \Exception($message_t);
1772
-				}
1773
-			} else {
1774
-				$result['filePath'] = null;
1775
-				$result['fileSource'] = null;
1776
-			}
1777
-		}
1778
-
1779
-		return $result;
1780
-	}
1781
-
1782
-	/**
1783
-	 *
1784
-	 * @param array $shareData
1785
-	 * @return mixed false in case of a failure or the id of the new share
1786
-	 */
1787
-	private static function insertShare(array $shareData) {
1788
-
1789
-		$query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` ('
1790
-			.' `item_type`, `item_source`, `item_target`, `share_type`,'
1791
-			.' `share_with`, `uid_owner`, `permissions`, `stime`, `file_source`,'
1792
-			.' `file_target`, `token`, `parent`, `expiration`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)');
1793
-		$query->bindValue(1, $shareData['itemType']);
1794
-		$query->bindValue(2, $shareData['itemSource']);
1795
-		$query->bindValue(3, $shareData['itemTarget']);
1796
-		$query->bindValue(4, $shareData['shareType']);
1797
-		$query->bindValue(5, $shareData['shareWith']);
1798
-		$query->bindValue(6, $shareData['uidOwner']);
1799
-		$query->bindValue(7, $shareData['permissions']);
1800
-		$query->bindValue(8, $shareData['shareTime']);
1801
-		$query->bindValue(9, $shareData['fileSource']);
1802
-		$query->bindValue(10, $shareData['fileTarget']);
1803
-		$query->bindValue(11, $shareData['token']);
1804
-		$query->bindValue(12, $shareData['parent']);
1805
-		$query->bindValue(13, $shareData['expiration'], 'datetime');
1806
-		$result = $query->execute();
1807
-
1808
-		$id = false;
1809
-		if ($result) {
1810
-			$id =  \OC::$server->getDatabaseConnection()->lastInsertId('*PREFIX*share');
1811
-		}
1812
-
1813
-		return $id;
1814
-
1815
-	}
1816
-
1817
-	/**
1818
-	 * In case a password protected link is not yet authenticated this function will return false
1819
-	 *
1820
-	 * @param array $linkItem
1821
-	 * @return boolean
1822
-	 */
1823
-	public static function checkPasswordProtectedShare(array $linkItem) {
1824
-		if (!isset($linkItem['share_with'])) {
1825
-			return true;
1826
-		}
1827
-		if (!isset($linkItem['share_type'])) {
1828
-			return true;
1829
-		}
1830
-		if (!isset($linkItem['id'])) {
1831
-			return true;
1832
-		}
1833
-
1834
-		if ($linkItem['share_type'] != \OCP\Share::SHARE_TYPE_LINK) {
1835
-			return true;
1836
-		}
1837
-
1838
-		if ( \OC::$server->getSession()->exists('public_link_authenticated')
1839
-			&& \OC::$server->getSession()->get('public_link_authenticated') === (string)$linkItem['id'] ) {
1840
-			return true;
1841
-		}
1842
-
1843
-		return false;
1844
-	}
1845
-
1846
-	/**
1847
-	 * construct select statement
1848
-	 * @param int $format
1849
-	 * @param boolean $fileDependent ist it a file/folder share or a generla share
1850
-	 * @param string $uidOwner
1851
-	 * @return string select statement
1852
-	 */
1853
-	private static function createSelectStatement($format, $fileDependent, $uidOwner = null) {
1854
-		$select = '*';
1855
-		if ($format == self::FORMAT_STATUSES) {
1856
-			if ($fileDependent) {
1857
-				$select = '`*PREFIX*share`.`id`, `*PREFIX*share`.`parent`, `share_type`, `path`, `storage`, '
1858
-					. '`share_with`, `uid_owner` , `file_source`, `stime`, `*PREFIX*share`.`permissions`, '
1859
-					. '`*PREFIX*storages`.`id` AS `storage_id`, `*PREFIX*filecache`.`parent` as `file_parent`, '
1860
-					. '`uid_initiator`';
1861
-			} else {
1862
-				$select = '`id`, `parent`, `share_type`, `share_with`, `uid_owner`, `item_source`, `stime`, `*PREFIX*share`.`permissions`';
1863
-			}
1864
-		} else {
1865
-			if (isset($uidOwner)) {
1866
-				if ($fileDependent) {
1867
-					$select = '`*PREFIX*share`.`id`, `item_type`, `item_source`, `*PREFIX*share`.`parent`,'
1868
-						. ' `share_type`, `share_with`, `file_source`, `file_target`, `path`, `*PREFIX*share`.`permissions`, `stime`,'
1869
-						. ' `expiration`, `token`, `storage`, `mail_send`, `uid_owner`, '
1870
-						. '`*PREFIX*storages`.`id` AS `storage_id`, `*PREFIX*filecache`.`parent` as `file_parent`';
1871
-				} else {
1872
-					$select = '`id`, `item_type`, `item_source`, `parent`, `share_type`, `share_with`, `*PREFIX*share`.`permissions`,'
1873
-						. ' `stime`, `file_source`, `expiration`, `token`, `mail_send`, `uid_owner`';
1874
-				}
1875
-			} else {
1876
-				if ($fileDependent) {
1877
-					if ($format == \OCA\Files_Sharing\ShareBackend\File::FORMAT_GET_FOLDER_CONTENTS || $format == \OCA\Files_Sharing\ShareBackend\File::FORMAT_FILE_APP_ROOT) {
1878
-						$select = '`*PREFIX*share`.`id`, `item_type`, `item_source`, `*PREFIX*share`.`parent`, `uid_owner`, '
1879
-							. '`share_type`, `share_with`, `file_source`, `path`, `file_target`, `stime`, '
1880
-							. '`*PREFIX*share`.`permissions`, `expiration`, `storage`, `*PREFIX*filecache`.`parent` as `file_parent`, '
1881
-							. '`name`, `mtime`, `mimetype`, `mimepart`, `size`, `encrypted`, `etag`, `mail_send`';
1882
-					} else {
1883
-						$select = '`*PREFIX*share`.`id`, `item_type`, `item_source`, `item_target`,'
1884
-							. '`*PREFIX*share`.`parent`, `share_type`, `share_with`, `uid_owner`,'
1885
-							. '`file_source`, `path`, `file_target`, `*PREFIX*share`.`permissions`,'
1886
-						    . '`stime`, `expiration`, `token`, `storage`, `mail_send`,'
1887
-							. '`*PREFIX*storages`.`id` AS `storage_id`, `*PREFIX*filecache`.`parent` as `file_parent`';
1888
-					}
1889
-				}
1890
-			}
1891
-		}
1892
-		return $select;
1893
-	}
1894
-
1895
-
1896
-	/**
1897
-	 * transform db results
1898
-	 * @param array $row result
1899
-	 */
1900
-	private static function transformDBResults(&$row) {
1901
-		if (isset($row['id'])) {
1902
-			$row['id'] = (int) $row['id'];
1903
-		}
1904
-		if (isset($row['share_type'])) {
1905
-			$row['share_type'] = (int) $row['share_type'];
1906
-		}
1907
-		if (isset($row['parent'])) {
1908
-			$row['parent'] = (int) $row['parent'];
1909
-		}
1910
-		if (isset($row['file_parent'])) {
1911
-			$row['file_parent'] = (int) $row['file_parent'];
1912
-		}
1913
-		if (isset($row['file_source'])) {
1914
-			$row['file_source'] = (int) $row['file_source'];
1915
-		}
1916
-		if (isset($row['permissions'])) {
1917
-			$row['permissions'] = (int) $row['permissions'];
1918
-		}
1919
-		if (isset($row['storage'])) {
1920
-			$row['storage'] = (int) $row['storage'];
1921
-		}
1922
-		if (isset($row['stime'])) {
1923
-			$row['stime'] = (int) $row['stime'];
1924
-		}
1925
-		if (isset($row['expiration']) && $row['share_type'] !== self::SHARE_TYPE_LINK) {
1926
-			// discard expiration date for non-link shares, which might have been
1927
-			// set by ancient bugs
1928
-			$row['expiration'] = null;
1929
-		}
1930
-	}
1931
-
1932
-	/**
1933
-	 * format result
1934
-	 * @param array $items result
1935
-	 * @param string $column is it a file share or a general share ('file_target' or 'item_target')
1936
-	 * @param \OCP\Share_Backend $backend sharing backend
1937
-	 * @param int $format
1938
-	 * @param array $parameters additional format parameters
1939
-	 * @return array format result
1940
-	 */
1941
-	private static function formatResult($items, $column, $backend, $format = self::FORMAT_NONE , $parameters = null) {
1942
-		if ($format === self::FORMAT_NONE) {
1943
-			return $items;
1944
-		} else if ($format === self::FORMAT_STATUSES) {
1945
-			$statuses = array();
1946
-			foreach ($items as $item) {
1947
-				if ($item['share_type'] === self::SHARE_TYPE_LINK) {
1948
-					if ($item['uid_initiator'] !== \OC::$server->getUserSession()->getUser()->getUID()) {
1949
-						continue;
1950
-					}
1951
-					$statuses[$item[$column]]['link'] = true;
1952
-				} else if (!isset($statuses[$item[$column]])) {
1953
-					$statuses[$item[$column]]['link'] = false;
1954
-				}
1955
-				if (!empty($item['file_target'])) {
1956
-					$statuses[$item[$column]]['path'] = $item['path'];
1957
-				}
1958
-			}
1959
-			return $statuses;
1960
-		} else {
1961
-			return $backend->formatItems($items, $format, $parameters);
1962
-		}
1963
-	}
1964
-
1965
-	/**
1966
-	 * remove protocol from URL
1967
-	 *
1968
-	 * @param string $url
1969
-	 * @return string
1970
-	 */
1971
-	public static function removeProtocolFromUrl($url) {
1972
-		if (strpos($url, 'https://') === 0) {
1973
-			return substr($url, strlen('https://'));
1974
-		} else if (strpos($url, 'http://') === 0) {
1975
-			return substr($url, strlen('http://'));
1976
-		}
1977
-
1978
-		return $url;
1979
-	}
1980
-
1981
-	/**
1982
-	 * try http post first with https and then with http as a fallback
1983
-	 *
1984
-	 * @param string $remoteDomain
1985
-	 * @param string $urlSuffix
1986
-	 * @param array $fields post parameters
1987
-	 * @return array
1988
-	 */
1989
-	private static function tryHttpPostToShareEndpoint($remoteDomain, $urlSuffix, array $fields) {
1990
-		$protocol = 'https://';
1991
-		$result = [
1992
-			'success' => false,
1993
-			'result' => '',
1994
-		];
1995
-		$try = 0;
1996
-		$discoveryService = \OC::$server->query(\OCP\OCS\IDiscoveryService::class);
1997
-		while ($result['success'] === false && $try < 2) {
1998
-			$federationEndpoints = $discoveryService->discover($protocol . $remoteDomain, 'FEDERATED_SHARING');
1999
-			$endpoint = isset($federationEndpoints['share']) ? $federationEndpoints['share'] : '/ocs/v2.php/cloud/shares';
2000
-			$result = \OC::$server->getHTTPHelper()->post($protocol . $remoteDomain . $endpoint . $urlSuffix . '?format=' . self::RESPONSE_FORMAT, $fields);
2001
-			$try++;
2002
-			$protocol = 'http://';
2003
-		}
2004
-
2005
-		return $result;
2006
-	}
2007
-
2008
-	/**
2009
-	 * send server-to-server share to remote server
2010
-	 *
2011
-	 * @param string $token
2012
-	 * @param string $shareWith
2013
-	 * @param string $name
2014
-	 * @param int $remote_id
2015
-	 * @param string $owner
2016
-	 * @return bool
2017
-	 */
2018
-	private static function sendRemoteShare($token, $shareWith, $name, $remote_id, $owner) {
2019
-
2020
-		list($user, $remote) = Helper::splitUserRemote($shareWith);
2021
-
2022
-		if ($user && $remote) {
2023
-			$url = $remote;
2024
-
2025
-			$local = \OC::$server->getURLGenerator()->getAbsoluteURL('/');
2026
-
2027
-			$fields = array(
2028
-				'shareWith' => $user,
2029
-				'token' => $token,
2030
-				'name' => $name,
2031
-				'remoteId' => $remote_id,
2032
-				'owner' => $owner,
2033
-				'remote' => $local,
2034
-			);
2035
-
2036
-			$url = self::removeProtocolFromUrl($url);
2037
-			$result = self::tryHttpPostToShareEndpoint($url, '', $fields);
2038
-			$status = json_decode($result['result'], true);
2039
-
2040
-			if ($result['success'] && ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200)) {
2041
-				\OC_Hook::emit(Share::class, 'federated_share_added', ['server' => $remote]);
2042
-				return true;
2043
-			}
2044
-
2045
-		}
2046
-
2047
-		return false;
2048
-	}
2049
-
2050
-	/**
2051
-	 * send server-to-server unshare to remote server
2052
-	 *
2053
-	 * @param string $remote url
2054
-	 * @param int $id share id
2055
-	 * @param string $token
2056
-	 * @return bool
2057
-	 */
2058
-	private static function sendRemoteUnshare($remote, $id, $token) {
2059
-		$url = rtrim($remote, '/');
2060
-		$fields = array('token' => $token, 'format' => 'json');
2061
-		$url = self::removeProtocolFromUrl($url);
2062
-		$result = self::tryHttpPostToShareEndpoint($url, '/'.$id.'/unshare', $fields);
2063
-		$status = json_decode($result['result'], true);
2064
-
2065
-		return ($result['success'] && ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200));
2066
-	}
2067
-
2068
-	/**
2069
-	 * check if user can only share with group members
2070
-	 * @return bool
2071
-	 */
2072
-	public static function shareWithGroupMembersOnly() {
2073
-		$value = \OC::$server->getConfig()->getAppValue('core', 'shareapi_only_share_with_group_members', 'no');
2074
-		return ($value === 'yes') ? true : false;
2075
-	}
2076
-
2077
-	/**
2078
-	 * @return bool
2079
-	 */
2080
-	public static function isDefaultExpireDateEnabled() {
2081
-		$defaultExpireDateEnabled = \OC::$server->getConfig()->getAppValue('core', 'shareapi_default_expire_date', 'no');
2082
-		return ($defaultExpireDateEnabled === "yes") ? true : false;
2083
-	}
2084
-
2085
-	/**
2086
-	 * @return int
2087
-	 */
2088
-	public static function getExpireInterval() {
2089
-		return (int)\OC::$server->getConfig()->getAppValue('core', 'shareapi_expire_after_n_days', '7');
2090
-	}
2091
-
2092
-	/**
2093
-	 * Checks whether the given path is reachable for the given owner
2094
-	 *
2095
-	 * @param string $path path relative to files
2096
-	 * @param string $ownerStorageId storage id of the owner
2097
-	 *
2098
-	 * @return boolean true if file is reachable, false otherwise
2099
-	 */
2100
-	private static function isFileReachable($path, $ownerStorageId) {
2101
-		// if outside the home storage, file is always considered reachable
2102
-		if (!(substr($ownerStorageId, 0, 6) === 'home::' ||
2103
-			substr($ownerStorageId, 0, 13) === 'object::user:'
2104
-		)) {
2105
-			return true;
2106
-		}
2107
-
2108
-		// if inside the home storage, the file has to be under "/files/"
2109
-		$path = ltrim($path, '/');
2110
-		if (substr($path, 0, 6) === 'files/') {
2111
-			return true;
2112
-		}
2113
-
2114
-		return false;
2115
-	}
2116
-
2117
-	/**
2118
-	 * @param IConfig $config
2119
-	 * @return bool
2120
-	 */
2121
-	public static function enforcePassword(IConfig $config) {
2122
-		$enforcePassword = $config->getAppValue('core', 'shareapi_enforce_links_password', 'no');
2123
-		return ($enforcePassword === "yes") ? true : false;
2124
-	}
2125
-
2126
-	/**
2127
-	 * @param string $password
2128
-	 * @throws \Exception
2129
-	 */
2130
-	private static function verifyPassword($password) {
2131
-
2132
-		$accepted = true;
2133
-		$message = '';
2134
-		\OCP\Util::emitHook('\OC\Share', 'verifyPassword', [
2135
-			'password' => $password,
2136
-			'accepted' => &$accepted,
2137
-			'message' => &$message
2138
-		]);
2139
-
2140
-		if (!$accepted) {
2141
-			throw new \Exception($message);
2142
-		}
2143
-	}
732
+        $result = $query->execute(array($status, $itemType, $itemSource, $shareType, $recipient));
733
+
734
+        if($result === false) {
735
+            \OCP\Util::writeLog(Share::class, 'Couldn\'t set send mail status', \OCP\Util::ERROR);
736
+        }
737
+    }
738
+
739
+    /**
740
+     * validate expiration date if it meets all constraints
741
+     *
742
+     * @param string $expireDate well formatted date string, e.g. "DD-MM-YYYY"
743
+     * @param string $shareTime timestamp when the file was shared
744
+     * @param string $itemType
745
+     * @param string $itemSource
746
+     * @return \DateTime validated date
747
+     * @throws \Exception when the expire date is in the past or further in the future then the enforced date
748
+     */
749
+    private static function validateExpireDate($expireDate, $shareTime, $itemType, $itemSource) {
750
+        $l = \OC::$server->getL10N('lib');
751
+        $date = new \DateTime($expireDate);
752
+        $today = new \DateTime('now');
753
+
754
+        // if the user doesn't provide a share time we need to get it from the database
755
+        // fall-back mode to keep API stable, because the $shareTime parameter was added later
756
+        $defaultExpireDateEnforced = \OCP\Util::isDefaultExpireDateEnforced();
757
+        if ($defaultExpireDateEnforced && $shareTime === null) {
758
+            $items = self::getItemShared($itemType, $itemSource);
759
+            $firstItem = reset($items);
760
+            $shareTime = (int)$firstItem['stime'];
761
+        }
762
+
763
+        if ($defaultExpireDateEnforced) {
764
+            // initialize max date with share time
765
+            $maxDate = new \DateTime();
766
+            $maxDate->setTimestamp($shareTime);
767
+            $maxDays = \OC::$server->getConfig()->getAppValue('core', 'shareapi_expire_after_n_days', '7');
768
+            $maxDate->add(new \DateInterval('P' . $maxDays . 'D'));
769
+            if ($date > $maxDate) {
770
+                $warning = 'Cannot set expiration date. Shares cannot expire later than ' . $maxDays . ' after they have been shared';
771
+                $warning_t = $l->t('Cannot set expiration date. Shares cannot expire later than %s after they have been shared', array($maxDays));
772
+                \OCP\Util::writeLog(Share::class, $warning, \OCP\Util::WARN);
773
+                throw new \Exception($warning_t);
774
+            }
775
+        }
776
+
777
+        if ($date < $today) {
778
+            $message = 'Cannot set expiration date. Expiration date is in the past';
779
+            $message_t = $l->t('Cannot set expiration date. Expiration date is in the past');
780
+            \OCP\Util::writeLog(Share::class, $message, \OCP\Util::WARN);
781
+            throw new \Exception($message_t);
782
+        }
783
+
784
+        return $date;
785
+    }
786
+
787
+    /**
788
+     * Checks whether a share has expired, calls unshareItem() if yes.
789
+     * @param array $item Share data (usually database row)
790
+     * @return boolean True if item was expired, false otherwise.
791
+     */
792
+    protected static function expireItem(array $item) {
793
+
794
+        $result = false;
795
+
796
+        // only use default expiration date for link shares
797
+        if ((int) $item['share_type'] === self::SHARE_TYPE_LINK) {
798
+
799
+            // calculate expiration date
800
+            if (!empty($item['expiration'])) {
801
+                $userDefinedExpire = new \DateTime($item['expiration']);
802
+                $expires = $userDefinedExpire->getTimestamp();
803
+            } else {
804
+                $expires = null;
805
+            }
806
+
807
+
808
+            // get default expiration settings
809
+            $defaultSettings = Helper::getDefaultExpireSetting();
810
+            $expires = Helper::calculateExpireDate($defaultSettings, $item['stime'], $expires);
811
+
812
+
813
+            if (is_int($expires)) {
814
+                $now = time();
815
+                if ($now > $expires) {
816
+                    self::unshareItem($item);
817
+                    $result = true;
818
+                }
819
+            }
820
+        }
821
+        return $result;
822
+    }
823
+
824
+    /**
825
+     * Unshares a share given a share data array
826
+     * @param array $item Share data (usually database row)
827
+     * @param int $newParent parent ID
828
+     * @return null
829
+     */
830
+    protected static function unshareItem(array $item, $newParent = null) {
831
+
832
+        $shareType = (int)$item['share_type'];
833
+        $shareWith = null;
834
+        if ($shareType !== \OCP\Share::SHARE_TYPE_LINK) {
835
+            $shareWith = $item['share_with'];
836
+        }
837
+
838
+        // Pass all the vars we have for now, they may be useful
839
+        $hookParams = array(
840
+            'id'            => $item['id'],
841
+            'itemType'      => $item['item_type'],
842
+            'itemSource'    => $item['item_source'],
843
+            'shareType'     => $shareType,
844
+            'shareWith'     => $shareWith,
845
+            'itemParent'    => $item['parent'],
846
+            'uidOwner'      => $item['uid_owner'],
847
+        );
848
+        if($item['item_type'] === 'file' || $item['item_type'] === 'folder') {
849
+            $hookParams['fileSource'] = $item['file_source'];
850
+            $hookParams['fileTarget'] = $item['file_target'];
851
+        }
852
+
853
+        \OC_Hook::emit(Share::class, 'pre_unshare', $hookParams);
854
+        $deletedShares = Helper::delete($item['id'], false, null, $newParent);
855
+        $deletedShares[] = $hookParams;
856
+        $hookParams['deletedShares'] = $deletedShares;
857
+        \OC_Hook::emit(Share::class, 'post_unshare', $hookParams);
858
+        if ((int)$item['share_type'] === \OCP\Share::SHARE_TYPE_REMOTE && \OC::$server->getUserSession()->getUser()) {
859
+            list(, $remote) = Helper::splitUserRemote($item['share_with']);
860
+            self::sendRemoteUnshare($remote, $item['id'], $item['token']);
861
+        }
862
+    }
863
+
864
+    /**
865
+     * Get the backend class for the specified item type
866
+     * @param string $itemType
867
+     * @throws \Exception
868
+     * @return \OCP\Share_Backend
869
+     */
870
+    public static function getBackend($itemType) {
871
+        $l = \OC::$server->getL10N('lib');
872
+        if (isset(self::$backends[$itemType])) {
873
+            return self::$backends[$itemType];
874
+        } else if (isset(self::$backendTypes[$itemType]['class'])) {
875
+            $class = self::$backendTypes[$itemType]['class'];
876
+            if (class_exists($class)) {
877
+                self::$backends[$itemType] = new $class;
878
+                if (!(self::$backends[$itemType] instanceof \OCP\Share_Backend)) {
879
+                    $message = 'Sharing backend %s must implement the interface OCP\Share_Backend';
880
+                    $message_t = $l->t('Sharing backend %s must implement the interface OCP\Share_Backend', array($class));
881
+                    \OCP\Util::writeLog(Share::class, sprintf($message, $class), \OCP\Util::ERROR);
882
+                    throw new \Exception($message_t);
883
+                }
884
+                return self::$backends[$itemType];
885
+            } else {
886
+                $message = 'Sharing backend %s not found';
887
+                $message_t = $l->t('Sharing backend %s not found', array($class));
888
+                \OCP\Util::writeLog(Share::class, sprintf($message, $class), \OCP\Util::ERROR);
889
+                throw new \Exception($message_t);
890
+            }
891
+        }
892
+        $message = 'Sharing backend for %s not found';
893
+        $message_t = $l->t('Sharing backend for %s not found', array($itemType));
894
+        \OCP\Util::writeLog(Share::class, sprintf($message, $itemType), \OCP\Util::ERROR);
895
+        throw new \Exception($message_t);
896
+    }
897
+
898
+    /**
899
+     * Check if resharing is allowed
900
+     * @return boolean true if allowed or false
901
+     *
902
+     * Resharing is allowed by default if not configured
903
+     */
904
+    public static function isResharingAllowed() {
905
+        if (!isset(self::$isResharingAllowed)) {
906
+            if (\OC::$server->getConfig()->getAppValue('core', 'shareapi_allow_resharing', 'yes') == 'yes') {
907
+                self::$isResharingAllowed = true;
908
+            } else {
909
+                self::$isResharingAllowed = false;
910
+            }
911
+        }
912
+        return self::$isResharingAllowed;
913
+    }
914
+
915
+    /**
916
+     * Get a list of collection item types for the specified item type
917
+     * @param string $itemType
918
+     * @return array
919
+     */
920
+    private static function getCollectionItemTypes($itemType) {
921
+        $collectionTypes = array($itemType);
922
+        foreach (self::$backendTypes as $type => $backend) {
923
+            if (in_array($backend['collectionOf'], $collectionTypes)) {
924
+                $collectionTypes[] = $type;
925
+            }
926
+        }
927
+        // TODO Add option for collections to be collection of themselves, only 'folder' does it now...
928
+        if (isset(self::$backendTypes[$itemType]) && (!self::getBackend($itemType) instanceof \OCP\Share_Backend_Collection || $itemType != 'folder')) {
929
+            unset($collectionTypes[0]);
930
+        }
931
+        // Return array if collections were found or the item type is a
932
+        // collection itself - collections can be inside collections
933
+        if (count($collectionTypes) > 0) {
934
+            return $collectionTypes;
935
+        }
936
+        return false;
937
+    }
938
+
939
+    /**
940
+     * Get the owners of items shared with a user.
941
+     *
942
+     * @param string $user The user the items are shared with.
943
+     * @param string $type The type of the items shared with the user.
944
+     * @param boolean $includeCollections Include collection item types (optional)
945
+     * @param boolean $includeOwner include owner in the list of users the item is shared with (optional)
946
+     * @return array
947
+     */
948
+    public static function getSharedItemsOwners($user, $type, $includeCollections = false, $includeOwner = false) {
949
+        // First, we find out if $type is part of a collection (and if that collection is part of
950
+        // another one and so on).
951
+        $collectionTypes = array();
952
+        if (!$includeCollections || !$collectionTypes = self::getCollectionItemTypes($type)) {
953
+            $collectionTypes[] = $type;
954
+        }
955
+
956
+        // Of these collection types, along with our original $type, we make a
957
+        // list of the ones for which a sharing backend has been registered.
958
+        // FIXME: Ideally, we wouldn't need to nest getItemsSharedWith in this loop but just call it
959
+        // with its $includeCollections parameter set to true. Unfortunately, this fails currently.
960
+        $allMaybeSharedItems = array();
961
+        foreach ($collectionTypes as $collectionType) {
962
+            if (isset(self::$backends[$collectionType])) {
963
+                $allMaybeSharedItems[$collectionType] = self::getItemsSharedWithUser(
964
+                    $collectionType,
965
+                    $user,
966
+                    self::FORMAT_NONE
967
+                );
968
+            }
969
+        }
970
+
971
+        $owners = array();
972
+        if ($includeOwner) {
973
+            $owners[] = $user;
974
+        }
975
+
976
+        // We take a look at all shared items of the given $type (or of the collections it is part of)
977
+        // and find out their owners. Then, we gather the tags for the original $type from all owners,
978
+        // and return them as elements of a list that look like "Tag (owner)".
979
+        foreach ($allMaybeSharedItems as $collectionType => $maybeSharedItems) {
980
+            foreach ($maybeSharedItems as $sharedItem) {
981
+                if (isset($sharedItem['id'])) { //workaround for https://github.com/owncloud/core/issues/2814
982
+                    $owners[] = $sharedItem['uid_owner'];
983
+                }
984
+            }
985
+        }
986
+
987
+        return $owners;
988
+    }
989
+
990
+    /**
991
+     * Get shared items from the database
992
+     * @param string $itemType
993
+     * @param string $item Item source or target (optional)
994
+     * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, SHARE_TYPE_LINK, $shareTypeUserAndGroups, or $shareTypeGroupUserUnique
995
+     * @param string $shareWith User or group the item is being shared with
996
+     * @param string $uidOwner User that is the owner of shared items (optional)
997
+     * @param int $format Format to convert items to with formatItems() (optional)
998
+     * @param mixed $parameters to pass to formatItems() (optional)
999
+     * @param int $limit Number of items to return, -1 to return all matches (optional)
1000
+     * @param boolean $includeCollections Include collection item types (optional)
1001
+     * @param boolean $itemShareWithBySource (optional)
1002
+     * @param boolean $checkExpireDate
1003
+     * @return array
1004
+     *
1005
+     * See public functions getItem(s)... for parameter usage
1006
+     *
1007
+     */
1008
+    public static function getItems($itemType, $item = null, $shareType = null, $shareWith = null,
1009
+                                    $uidOwner = null, $format = self::FORMAT_NONE, $parameters = null, $limit = -1,
1010
+                                    $includeCollections = false, $itemShareWithBySource = false, $checkExpireDate  = true) {
1011
+        if (\OC::$server->getConfig()->getAppValue('core', 'shareapi_enabled', 'yes') != 'yes') {
1012
+            return array();
1013
+        }
1014
+        $backend = self::getBackend($itemType);
1015
+        $collectionTypes = false;
1016
+        // Get filesystem root to add it to the file target and remove from the
1017
+        // file source, match file_source with the file cache
1018
+        if ($itemType == 'file' || $itemType == 'folder') {
1019
+            if(!is_null($uidOwner)) {
1020
+                $root = \OC\Files\Filesystem::getRoot();
1021
+            } else {
1022
+                $root = '';
1023
+            }
1024
+            $where = 'INNER JOIN `*PREFIX*filecache` ON `file_source` = `*PREFIX*filecache`.`fileid` ';
1025
+            if (!isset($item)) {
1026
+                $where .= ' AND `file_target` IS NOT NULL ';
1027
+            }
1028
+            $where .= 'INNER JOIN `*PREFIX*storages` ON `numeric_id` = `*PREFIX*filecache`.`storage` ';
1029
+            $fileDependent = true;
1030
+            $queryArgs = array();
1031
+        } else {
1032
+            $fileDependent = false;
1033
+            $root = '';
1034
+            $collectionTypes = self::getCollectionItemTypes($itemType);
1035
+            if ($includeCollections && !isset($item) && $collectionTypes) {
1036
+                // If includeCollections is true, find collections of this item type, e.g. a music album contains songs
1037
+                if (!in_array($itemType, $collectionTypes)) {
1038
+                    $itemTypes = array_merge(array($itemType), $collectionTypes);
1039
+                } else {
1040
+                    $itemTypes = $collectionTypes;
1041
+                }
1042
+                $placeholders = join(',', array_fill(0, count($itemTypes), '?'));
1043
+                $where = ' WHERE `item_type` IN ('.$placeholders.'))';
1044
+                $queryArgs = $itemTypes;
1045
+            } else {
1046
+                $where = ' WHERE `item_type` = ?';
1047
+                $queryArgs = array($itemType);
1048
+            }
1049
+        }
1050
+        if (\OC::$server->getConfig()->getAppValue('core', 'shareapi_allow_links', 'yes') !== 'yes') {
1051
+            $where .= ' AND `share_type` != ?';
1052
+            $queryArgs[] = self::SHARE_TYPE_LINK;
1053
+        }
1054
+        if (isset($shareType)) {
1055
+            // Include all user and group items
1056
+            if ($shareType == self::$shareTypeUserAndGroups && isset($shareWith)) {
1057
+                $where .= ' AND ((`share_type` in (?, ?) AND `share_with` = ?) ';
1058
+                $queryArgs[] = self::SHARE_TYPE_USER;
1059
+                $queryArgs[] = self::$shareTypeGroupUserUnique;
1060
+                $queryArgs[] = $shareWith;
1061
+
1062
+                $user = \OC::$server->getUserManager()->get($shareWith);
1063
+                $groups = [];
1064
+                if ($user) {
1065
+                    $groups = \OC::$server->getGroupManager()->getUserGroupIds($user);
1066
+                }
1067
+                if (!empty($groups)) {
1068
+                    $placeholders = join(',', array_fill(0, count($groups), '?'));
1069
+                    $where .= ' OR (`share_type` = ? AND `share_with` IN ('.$placeholders.')) ';
1070
+                    $queryArgs[] = self::SHARE_TYPE_GROUP;
1071
+                    $queryArgs = array_merge($queryArgs, $groups);
1072
+                }
1073
+                $where .= ')';
1074
+                // Don't include own group shares
1075
+                $where .= ' AND `uid_owner` != ?';
1076
+                $queryArgs[] = $shareWith;
1077
+            } else {
1078
+                $where .= ' AND `share_type` = ?';
1079
+                $queryArgs[] = $shareType;
1080
+                if (isset($shareWith)) {
1081
+                    $where .= ' AND `share_with` = ?';
1082
+                    $queryArgs[] = $shareWith;
1083
+                }
1084
+            }
1085
+        }
1086
+        if (isset($uidOwner)) {
1087
+            $where .= ' AND `uid_owner` = ?';
1088
+            $queryArgs[] = $uidOwner;
1089
+            if (!isset($shareType)) {
1090
+                // Prevent unique user targets for group shares from being selected
1091
+                $where .= ' AND `share_type` != ?';
1092
+                $queryArgs[] = self::$shareTypeGroupUserUnique;
1093
+            }
1094
+            if ($fileDependent) {
1095
+                $column = 'file_source';
1096
+            } else {
1097
+                $column = 'item_source';
1098
+            }
1099
+        } else {
1100
+            if ($fileDependent) {
1101
+                $column = 'file_target';
1102
+            } else {
1103
+                $column = 'item_target';
1104
+            }
1105
+        }
1106
+        if (isset($item)) {
1107
+            $collectionTypes = self::getCollectionItemTypes($itemType);
1108
+            if ($includeCollections && $collectionTypes && !in_array('folder', $collectionTypes)) {
1109
+                $where .= ' AND (';
1110
+            } else {
1111
+                $where .= ' AND';
1112
+            }
1113
+            // If looking for own shared items, check item_source else check item_target
1114
+            if (isset($uidOwner) || $itemShareWithBySource) {
1115
+                // If item type is a file, file source needs to be checked in case the item was converted
1116
+                if ($fileDependent) {
1117
+                    $where .= ' `file_source` = ?';
1118
+                    $column = 'file_source';
1119
+                } else {
1120
+                    $where .= ' `item_source` = ?';
1121
+                    $column = 'item_source';
1122
+                }
1123
+            } else {
1124
+                if ($fileDependent) {
1125
+                    $where .= ' `file_target` = ?';
1126
+                    $item = \OC\Files\Filesystem::normalizePath($item);
1127
+                } else {
1128
+                    $where .= ' `item_target` = ?';
1129
+                }
1130
+            }
1131
+            $queryArgs[] = $item;
1132
+            if ($includeCollections && $collectionTypes && !in_array('folder', $collectionTypes)) {
1133
+                $placeholders = join(',', array_fill(0, count($collectionTypes), '?'));
1134
+                $where .= ' OR `item_type` IN ('.$placeholders.'))';
1135
+                $queryArgs = array_merge($queryArgs, $collectionTypes);
1136
+            }
1137
+        }
1138
+
1139
+        if ($shareType == self::$shareTypeUserAndGroups && $limit === 1) {
1140
+            // Make sure the unique user target is returned if it exists,
1141
+            // unique targets should follow the group share in the database
1142
+            // If the limit is not 1, the filtering can be done later
1143
+            $where .= ' ORDER BY `*PREFIX*share`.`id` DESC';
1144
+        } else {
1145
+            $where .= ' ORDER BY `*PREFIX*share`.`id` ASC';
1146
+        }
1147
+
1148
+        if ($limit != -1 && !$includeCollections) {
1149
+            // The limit must be at least 3, because filtering needs to be done
1150
+            if ($limit < 3) {
1151
+                $queryLimit = 3;
1152
+            } else {
1153
+                $queryLimit = $limit;
1154
+            }
1155
+        } else {
1156
+            $queryLimit = null;
1157
+        }
1158
+        $select = self::createSelectStatement($format, $fileDependent, $uidOwner);
1159
+        $root = strlen($root);
1160
+        $query = \OC_DB::prepare('SELECT '.$select.' FROM `*PREFIX*share` '.$where, $queryLimit);
1161
+        $result = $query->execute($queryArgs);
1162
+        if ($result === false) {
1163
+            \OCP\Util::writeLog(Share::class,
1164
+                \OC_DB::getErrorMessage() . ', select=' . $select . ' where=',
1165
+                \OCP\Util::ERROR);
1166
+        }
1167
+        $items = array();
1168
+        $targets = array();
1169
+        $switchedItems = array();
1170
+        $mounts = array();
1171
+        while ($row = $result->fetchRow()) {
1172
+            self::transformDBResults($row);
1173
+            // Filter out duplicate group shares for users with unique targets
1174
+            if ($fileDependent && !self::isFileReachable($row['path'], $row['storage_id'])) {
1175
+                continue;
1176
+            }
1177
+            if ($row['share_type'] == self::$shareTypeGroupUserUnique && isset($items[$row['parent']])) {
1178
+                $row['share_type'] = self::SHARE_TYPE_GROUP;
1179
+                $row['unique_name'] = true; // remember that we use a unique name for this user
1180
+                $row['share_with'] = $items[$row['parent']]['share_with'];
1181
+                // if the group share was unshared from the user we keep the permission, otherwise
1182
+                // we take the permission from the parent because this is always the up-to-date
1183
+                // permission for the group share
1184
+                if ($row['permissions'] > 0) {
1185
+                    $row['permissions'] = $items[$row['parent']]['permissions'];
1186
+                }
1187
+                // Remove the parent group share
1188
+                unset($items[$row['parent']]);
1189
+                if ($row['permissions'] == 0) {
1190
+                    continue;
1191
+                }
1192
+            } else if (!isset($uidOwner)) {
1193
+                // Check if the same target already exists
1194
+                if (isset($targets[$row['id']])) {
1195
+                    // Check if the same owner shared with the user twice
1196
+                    // through a group and user share - this is allowed
1197
+                    $id = $targets[$row['id']];
1198
+                    if (isset($items[$id]) && $items[$id]['uid_owner'] == $row['uid_owner']) {
1199
+                        // Switch to group share type to ensure resharing conditions aren't bypassed
1200
+                        if ($items[$id]['share_type'] != self::SHARE_TYPE_GROUP) {
1201
+                            $items[$id]['share_type'] = self::SHARE_TYPE_GROUP;
1202
+                            $items[$id]['share_with'] = $row['share_with'];
1203
+                        }
1204
+                        // Switch ids if sharing permission is granted on only
1205
+                        // one share to ensure correct parent is used if resharing
1206
+                        if (~(int)$items[$id]['permissions'] & \OCP\Constants::PERMISSION_SHARE
1207
+                            && (int)$row['permissions'] & \OCP\Constants::PERMISSION_SHARE) {
1208
+                            $items[$row['id']] = $items[$id];
1209
+                            $switchedItems[$id] = $row['id'];
1210
+                            unset($items[$id]);
1211
+                            $id = $row['id'];
1212
+                        }
1213
+                        $items[$id]['permissions'] |= (int)$row['permissions'];
1214
+
1215
+                    }
1216
+                    continue;
1217
+                } elseif (!empty($row['parent'])) {
1218
+                    $targets[$row['parent']] = $row['id'];
1219
+                }
1220
+            }
1221
+            // Remove root from file source paths if retrieving own shared items
1222
+            if (isset($uidOwner) && isset($row['path'])) {
1223
+                if (isset($row['parent'])) {
1224
+                    $query = \OC_DB::prepare('SELECT `file_target` FROM `*PREFIX*share` WHERE `id` = ?');
1225
+                    $parentResult = $query->execute(array($row['parent']));
1226
+                    if ($result === false) {
1227
+                        \OCP\Util::writeLog(Share::class, 'Can\'t select parent: ' .
1228
+                            \OC_DB::getErrorMessage() . ', select=' . $select . ' where=' . $where,
1229
+                            \OCP\Util::ERROR);
1230
+                    } else {
1231
+                        $parentRow = $parentResult->fetchRow();
1232
+                        $tmpPath = $parentRow['file_target'];
1233
+                        // find the right position where the row path continues from the target path
1234
+                        $pos = strrpos($row['path'], $parentRow['file_target']);
1235
+                        $subPath = substr($row['path'], $pos);
1236
+                        $splitPath = explode('/', $subPath);
1237
+                        foreach (array_slice($splitPath, 2) as $pathPart) {
1238
+                            $tmpPath = $tmpPath . '/' . $pathPart;
1239
+                        }
1240
+                        $row['path'] = $tmpPath;
1241
+                    }
1242
+                } else {
1243
+                    if (!isset($mounts[$row['storage']])) {
1244
+                        $mountPoints = \OC\Files\Filesystem::getMountByNumericId($row['storage']);
1245
+                        if (is_array($mountPoints) && !empty($mountPoints)) {
1246
+                            $mounts[$row['storage']] = current($mountPoints);
1247
+                        }
1248
+                    }
1249
+                    if (!empty($mounts[$row['storage']])) {
1250
+                        $path = $mounts[$row['storage']]->getMountPoint().$row['path'];
1251
+                        $relPath = substr($path, $root); // path relative to data/user
1252
+                        $row['path'] = rtrim($relPath, '/');
1253
+                    }
1254
+                }
1255
+            }
1256
+
1257
+            if($checkExpireDate) {
1258
+                if (self::expireItem($row)) {
1259
+                    continue;
1260
+                }
1261
+            }
1262
+            // Check if resharing is allowed, if not remove share permission
1263
+            if (isset($row['permissions']) && (!self::isResharingAllowed() | \OCP\Util::isSharingDisabledForUser())) {
1264
+                $row['permissions'] &= ~\OCP\Constants::PERMISSION_SHARE;
1265
+            }
1266
+            // Add display names to result
1267
+            $row['share_with_displayname'] = $row['share_with'];
1268
+            if ( isset($row['share_with']) && $row['share_with'] != '' &&
1269
+                $row['share_type'] === self::SHARE_TYPE_USER) {
1270
+                $row['share_with_displayname'] = \OCP\User::getDisplayName($row['share_with']);
1271
+            } else if(isset($row['share_with']) && $row['share_with'] != '' &&
1272
+                $row['share_type'] === self::SHARE_TYPE_REMOTE) {
1273
+                $addressBookEntries = \OC::$server->getContactsManager()->search($row['share_with'], ['CLOUD']);
1274
+                foreach ($addressBookEntries as $entry) {
1275
+                    foreach ($entry['CLOUD'] as $cloudID) {
1276
+                        if ($cloudID === $row['share_with']) {
1277
+                            $row['share_with_displayname'] = $entry['FN'];
1278
+                        }
1279
+                    }
1280
+                }
1281
+            }
1282
+            if ( isset($row['uid_owner']) && $row['uid_owner'] != '') {
1283
+                $row['displayname_owner'] = \OCP\User::getDisplayName($row['uid_owner']);
1284
+            }
1285
+
1286
+            if ($row['permissions'] > 0) {
1287
+                $items[$row['id']] = $row;
1288
+            }
1289
+
1290
+        }
1291
+
1292
+        // group items if we are looking for items shared with the current user
1293
+        if (isset($shareWith) && $shareWith === \OCP\User::getUser()) {
1294
+            $items = self::groupItems($items, $itemType);
1295
+        }
1296
+
1297
+        if (!empty($items)) {
1298
+            $collectionItems = array();
1299
+            foreach ($items as &$row) {
1300
+                // Return only the item instead of a 2-dimensional array
1301
+                if ($limit == 1 && $row[$column] == $item && ($row['item_type'] == $itemType || $itemType == 'file')) {
1302
+                    if ($format == self::FORMAT_NONE) {
1303
+                        return $row;
1304
+                    } else {
1305
+                        break;
1306
+                    }
1307
+                }
1308
+                // Check if this is a collection of the requested item type
1309
+                if ($includeCollections && $collectionTypes && $row['item_type'] !== 'folder' && in_array($row['item_type'], $collectionTypes)) {
1310
+                    if (($collectionBackend = self::getBackend($row['item_type']))
1311
+                        && $collectionBackend instanceof \OCP\Share_Backend_Collection) {
1312
+                        // Collections can be inside collections, check if the item is a collection
1313
+                        if (isset($item) && $row['item_type'] == $itemType && $row[$column] == $item) {
1314
+                            $collectionItems[] = $row;
1315
+                        } else {
1316
+                            $collection = array();
1317
+                            $collection['item_type'] = $row['item_type'];
1318
+                            if ($row['item_type'] == 'file' || $row['item_type'] == 'folder') {
1319
+                                $collection['path'] = basename($row['path']);
1320
+                            }
1321
+                            $row['collection'] = $collection;
1322
+                            // Fetch all of the children sources
1323
+                            $children = $collectionBackend->getChildren($row[$column]);
1324
+                            foreach ($children as $child) {
1325
+                                $childItem = $row;
1326
+                                $childItem['item_type'] = $itemType;
1327
+                                if ($row['item_type'] != 'file' && $row['item_type'] != 'folder') {
1328
+                                    $childItem['item_source'] = $child['source'];
1329
+                                    $childItem['item_target'] = $child['target'];
1330
+                                }
1331
+                                if ($backend instanceof \OCP\Share_Backend_File_Dependent) {
1332
+                                    if ($row['item_type'] == 'file' || $row['item_type'] == 'folder') {
1333
+                                        $childItem['file_source'] = $child['source'];
1334
+                                    } else { // TODO is this really needed if we already know that we use the file backend?
1335
+                                        $meta = \OC\Files\Filesystem::getFileInfo($child['file_path']);
1336
+                                        $childItem['file_source'] = $meta['fileid'];
1337
+                                    }
1338
+                                    $childItem['file_target'] =
1339
+                                        \OC\Files\Filesystem::normalizePath($child['file_path']);
1340
+                                }
1341
+                                if (isset($item)) {
1342
+                                    if ($childItem[$column] == $item) {
1343
+                                        // Return only the item instead of a 2-dimensional array
1344
+                                        if ($limit == 1) {
1345
+                                            if ($format == self::FORMAT_NONE) {
1346
+                                                return $childItem;
1347
+                                            } else {
1348
+                                                // Unset the items array and break out of both loops
1349
+                                                $items = array();
1350
+                                                $items[] = $childItem;
1351
+                                                break 2;
1352
+                                            }
1353
+                                        } else {
1354
+                                            $collectionItems[] = $childItem;
1355
+                                        }
1356
+                                    }
1357
+                                } else {
1358
+                                    $collectionItems[] = $childItem;
1359
+                                }
1360
+                            }
1361
+                        }
1362
+                    }
1363
+                    // Remove collection item
1364
+                    $toRemove = $row['id'];
1365
+                    if (array_key_exists($toRemove, $switchedItems)) {
1366
+                        $toRemove = $switchedItems[$toRemove];
1367
+                    }
1368
+                    unset($items[$toRemove]);
1369
+                } elseif ($includeCollections && $collectionTypes && in_array($row['item_type'], $collectionTypes)) {
1370
+                    // FIXME: Thats a dirty hack to improve file sharing performance,
1371
+                    // see github issue #10588 for more details
1372
+                    // Need to find a solution which works for all back-ends
1373
+                    $collectionBackend = self::getBackend($row['item_type']);
1374
+                    $sharedParents = $collectionBackend->getParents($row['item_source']);
1375
+                    foreach ($sharedParents as $parent) {
1376
+                        $collectionItems[] = $parent;
1377
+                    }
1378
+                }
1379
+            }
1380
+            if (!empty($collectionItems)) {
1381
+                $collectionItems = array_unique($collectionItems, SORT_REGULAR);
1382
+                $items = array_merge($items, $collectionItems);
1383
+            }
1384
+
1385
+            // filter out invalid items, these can appear when subshare entries exist
1386
+            // for a group in which the requested user isn't a member any more
1387
+            $items = array_filter($items, function($item) {
1388
+                return $item['share_type'] !== self::$shareTypeGroupUserUnique;
1389
+            });
1390
+
1391
+            return self::formatResult($items, $column, $backend, $format, $parameters);
1392
+        } elseif ($includeCollections && $collectionTypes && in_array('folder', $collectionTypes)) {
1393
+            // FIXME: Thats a dirty hack to improve file sharing performance,
1394
+            // see github issue #10588 for more details
1395
+            // Need to find a solution which works for all back-ends
1396
+            $collectionItems = array();
1397
+            $collectionBackend = self::getBackend('folder');
1398
+            $sharedParents = $collectionBackend->getParents($item, $shareWith, $uidOwner);
1399
+            foreach ($sharedParents as $parent) {
1400
+                $collectionItems[] = $parent;
1401
+            }
1402
+            if ($limit === 1) {
1403
+                return reset($collectionItems);
1404
+            }
1405
+            return self::formatResult($collectionItems, $column, $backend, $format, $parameters);
1406
+        }
1407
+
1408
+        return array();
1409
+    }
1410
+
1411
+    /**
1412
+     * group items with link to the same source
1413
+     *
1414
+     * @param array $items
1415
+     * @param string $itemType
1416
+     * @return array of grouped items
1417
+     */
1418
+    protected static function groupItems($items, $itemType) {
1419
+
1420
+        $fileSharing = ($itemType === 'file' || $itemType === 'folder') ? true : false;
1421
+
1422
+        $result = array();
1423
+
1424
+        foreach ($items as $item) {
1425
+            $grouped = false;
1426
+            foreach ($result as $key => $r) {
1427
+                // for file/folder shares we need to compare file_source, otherwise we compare item_source
1428
+                // only group shares if they already point to the same target, otherwise the file where shared
1429
+                // before grouping of shares was added. In this case we don't group them toi avoid confusions
1430
+                if (( $fileSharing && $item['file_source'] === $r['file_source'] && $item['file_target'] === $r['file_target']) ||
1431
+                    (!$fileSharing && $item['item_source'] === $r['item_source'] && $item['item_target'] === $r['item_target'])) {
1432
+                    // add the first item to the list of grouped shares
1433
+                    if (!isset($result[$key]['grouped'])) {
1434
+                        $result[$key]['grouped'][] = $result[$key];
1435
+                    }
1436
+                    $result[$key]['permissions'] = (int) $item['permissions'] | (int) $r['permissions'];
1437
+                    $result[$key]['grouped'][] = $item;
1438
+                    $grouped = true;
1439
+                    break;
1440
+                }
1441
+            }
1442
+
1443
+            if (!$grouped) {
1444
+                $result[] = $item;
1445
+            }
1446
+
1447
+        }
1448
+
1449
+        return $result;
1450
+    }
1451
+
1452
+    /**
1453
+     * Put shared item into the database
1454
+     * @param string $itemType Item type
1455
+     * @param string $itemSource Item source
1456
+     * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
1457
+     * @param string $shareWith User or group the item is being shared with
1458
+     * @param string $uidOwner User that is the owner of shared item
1459
+     * @param int $permissions CRUDS permissions
1460
+     * @param boolean|array $parentFolder Parent folder target (optional)
1461
+     * @param string $token (optional)
1462
+     * @param string $itemSourceName name of the source item (optional)
1463
+     * @param \DateTime $expirationDate (optional)
1464
+     * @throws \Exception
1465
+     * @return mixed id of the new share or false
1466
+     */
1467
+    private static function put($itemType, $itemSource, $shareType, $shareWith, $uidOwner,
1468
+                                $permissions, $parentFolder = null, $token = null, $itemSourceName = null, \DateTime $expirationDate = null) {
1469
+
1470
+        $queriesToExecute = array();
1471
+        $suggestedItemTarget = null;
1472
+        $groupFileTarget = $fileTarget = $suggestedFileTarget = $filePath = '';
1473
+        $groupItemTarget = $itemTarget = $fileSource = $parent = 0;
1474
+
1475
+        $result = self::checkReshare($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, $itemSourceName, $expirationDate);
1476
+        if(!empty($result)) {
1477
+            $parent = $result['parent'];
1478
+            $itemSource = $result['itemSource'];
1479
+            $fileSource = $result['fileSource'];
1480
+            $suggestedItemTarget = $result['suggestedItemTarget'];
1481
+            $suggestedFileTarget = $result['suggestedFileTarget'];
1482
+            $filePath = $result['filePath'];
1483
+        }
1484
+
1485
+        $isGroupShare = false;
1486
+        if ($shareType == self::SHARE_TYPE_GROUP) {
1487
+            $isGroupShare = true;
1488
+            if (isset($shareWith['users'])) {
1489
+                $users = $shareWith['users'];
1490
+            } else {
1491
+                $group = \OC::$server->getGroupManager()->get($shareWith['group']);
1492
+                if ($group) {
1493
+                    $users = $group->searchUsers('', -1, 0);
1494
+                    $userIds = [];
1495
+                    foreach ($users as $user) {
1496
+                        $userIds[] = $user->getUID();
1497
+                    }
1498
+                    $users = $userIds;
1499
+                } else {
1500
+                    $users = [];
1501
+                }
1502
+            }
1503
+            // remove current user from list
1504
+            if (in_array(\OCP\User::getUser(), $users)) {
1505
+                unset($users[array_search(\OCP\User::getUser(), $users)]);
1506
+            }
1507
+            $groupItemTarget = Helper::generateTarget($itemType, $itemSource,
1508
+                $shareType, $shareWith['group'], $uidOwner, $suggestedItemTarget);
1509
+            $groupFileTarget = Helper::generateTarget($itemType, $itemSource,
1510
+                $shareType, $shareWith['group'], $uidOwner, $filePath);
1511
+
1512
+            // add group share to table and remember the id as parent
1513
+            $queriesToExecute['groupShare'] = array(
1514
+                'itemType'			=> $itemType,
1515
+                'itemSource'		=> $itemSource,
1516
+                'itemTarget'		=> $groupItemTarget,
1517
+                'shareType'			=> $shareType,
1518
+                'shareWith'			=> $shareWith['group'],
1519
+                'uidOwner'			=> $uidOwner,
1520
+                'permissions'		=> $permissions,
1521
+                'shareTime'			=> time(),
1522
+                'fileSource'		=> $fileSource,
1523
+                'fileTarget'		=> $groupFileTarget,
1524
+                'token'				=> $token,
1525
+                'parent'			=> $parent,
1526
+                'expiration'		=> $expirationDate,
1527
+            );
1528
+
1529
+        } else {
1530
+            $users = array($shareWith);
1531
+            $itemTarget = Helper::generateTarget($itemType, $itemSource, $shareType, $shareWith, $uidOwner,
1532
+                $suggestedItemTarget);
1533
+        }
1534
+
1535
+        $run = true;
1536
+        $error = '';
1537
+        $preHookData = array(
1538
+            'itemType' => $itemType,
1539
+            'itemSource' => $itemSource,
1540
+            'shareType' => $shareType,
1541
+            'uidOwner' => $uidOwner,
1542
+            'permissions' => $permissions,
1543
+            'fileSource' => $fileSource,
1544
+            'expiration' => $expirationDate,
1545
+            'token' => $token,
1546
+            'run' => &$run,
1547
+            'error' => &$error
1548
+        );
1549
+
1550
+        $preHookData['itemTarget'] = ($isGroupShare) ? $groupItemTarget : $itemTarget;
1551
+        $preHookData['shareWith'] = ($isGroupShare) ? $shareWith['group'] : $shareWith;
1552
+
1553
+        \OC_Hook::emit(Share::class, 'pre_shared', $preHookData);
1554
+
1555
+        if ($run === false) {
1556
+            throw new \Exception($error);
1557
+        }
1558
+
1559
+        foreach ($users as $user) {
1560
+            $sourceId = ($itemType === 'file' || $itemType === 'folder') ? $fileSource : $itemSource;
1561
+            $sourceExists = self::getItemSharedWithBySource($itemType, $sourceId, self::FORMAT_NONE, null, true, $user);
1562
+
1563
+            $userShareType = ($isGroupShare) ? self::$shareTypeGroupUserUnique : $shareType;
1564
+
1565
+            if ($sourceExists && $sourceExists['item_source'] === $itemSource) {
1566
+                $fileTarget = $sourceExists['file_target'];
1567
+                $itemTarget = $sourceExists['item_target'];
1568
+
1569
+                // for group shares we don't need a additional entry if the target is the same
1570
+                if($isGroupShare && $groupItemTarget === $itemTarget) {
1571
+                    continue;
1572
+                }
1573
+
1574
+            } elseif(!$sourceExists && !$isGroupShare)  {
1575
+
1576
+                $itemTarget = Helper::generateTarget($itemType, $itemSource, $userShareType, $user,
1577
+                    $uidOwner, $suggestedItemTarget, $parent);
1578
+                if (isset($fileSource)) {
1579
+                    if ($parentFolder) {
1580
+                        if ($parentFolder === true) {
1581
+                            $fileTarget = Helper::generateTarget('file', $filePath, $userShareType, $user,
1582
+                                $uidOwner, $suggestedFileTarget, $parent);
1583
+                            if ($fileTarget != $groupFileTarget) {
1584
+                                $parentFolders[$user]['folder'] = $fileTarget;
1585
+                            }
1586
+                        } else if (isset($parentFolder[$user])) {
1587
+                            $fileTarget = $parentFolder[$user]['folder'].$itemSource;
1588
+                            $parent = $parentFolder[$user]['id'];
1589
+                        }
1590
+                    } else {
1591
+                        $fileTarget = Helper::generateTarget('file', $filePath, $userShareType,
1592
+                            $user, $uidOwner, $suggestedFileTarget, $parent);
1593
+                    }
1594
+                } else {
1595
+                    $fileTarget = null;
1596
+                }
1597
+
1598
+            } else {
1599
+
1600
+                // group share which doesn't exists until now, check if we need a unique target for this user
1601
+
1602
+                $itemTarget = Helper::generateTarget($itemType, $itemSource, self::SHARE_TYPE_USER, $user,
1603
+                    $uidOwner, $suggestedItemTarget, $parent);
1604
+
1605
+                // do we also need a file target
1606
+                if (isset($fileSource)) {
1607
+                    $fileTarget = Helper::generateTarget('file', $filePath, self::SHARE_TYPE_USER, $user,
1608
+                        $uidOwner, $suggestedFileTarget, $parent);
1609
+                } else {
1610
+                    $fileTarget = null;
1611
+                }
1612
+
1613
+                if (($itemTarget === $groupItemTarget) &&
1614
+                    (!isset($fileSource) || $fileTarget === $groupFileTarget)) {
1615
+                    continue;
1616
+                }
1617
+            }
1618
+
1619
+            $queriesToExecute[] = array(
1620
+                'itemType'			=> $itemType,
1621
+                'itemSource'		=> $itemSource,
1622
+                'itemTarget'		=> $itemTarget,
1623
+                'shareType'			=> $userShareType,
1624
+                'shareWith'			=> $user,
1625
+                'uidOwner'			=> $uidOwner,
1626
+                'permissions'		=> $permissions,
1627
+                'shareTime'			=> time(),
1628
+                'fileSource'		=> $fileSource,
1629
+                'fileTarget'		=> $fileTarget,
1630
+                'token'				=> $token,
1631
+                'parent'			=> $parent,
1632
+                'expiration'		=> $expirationDate,
1633
+            );
1634
+
1635
+        }
1636
+
1637
+        $id = false;
1638
+        if ($isGroupShare) {
1639
+            $id = self::insertShare($queriesToExecute['groupShare']);
1640
+            // Save this id, any extra rows for this group share will need to reference it
1641
+            $parent = \OC::$server->getDatabaseConnection()->lastInsertId('*PREFIX*share');
1642
+            unset($queriesToExecute['groupShare']);
1643
+        }
1644
+
1645
+        foreach ($queriesToExecute as $shareQuery) {
1646
+            $shareQuery['parent'] = $parent;
1647
+            $id = self::insertShare($shareQuery);
1648
+        }
1649
+
1650
+        $postHookData = array(
1651
+            'itemType' => $itemType,
1652
+            'itemSource' => $itemSource,
1653
+            'parent' => $parent,
1654
+            'shareType' => $shareType,
1655
+            'uidOwner' => $uidOwner,
1656
+            'permissions' => $permissions,
1657
+            'fileSource' => $fileSource,
1658
+            'id' => $parent,
1659
+            'token' => $token,
1660
+            'expirationDate' => $expirationDate,
1661
+        );
1662
+
1663
+        $postHookData['shareWith'] = ($isGroupShare) ? $shareWith['group'] : $shareWith;
1664
+        $postHookData['itemTarget'] = ($isGroupShare) ? $groupItemTarget : $itemTarget;
1665
+        $postHookData['fileTarget'] = ($isGroupShare) ? $groupFileTarget : $fileTarget;
1666
+
1667
+        \OC_Hook::emit(Share::class, 'post_shared', $postHookData);
1668
+
1669
+
1670
+        return $id ? $id : false;
1671
+    }
1672
+
1673
+    /**
1674
+     * @param string $itemType
1675
+     * @param string $itemSource
1676
+     * @param int $shareType
1677
+     * @param string $shareWith
1678
+     * @param string $uidOwner
1679
+     * @param int $permissions
1680
+     * @param string|null $itemSourceName
1681
+     * @param null|\DateTime $expirationDate
1682
+     */
1683
+    private static function checkReshare($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, $itemSourceName, $expirationDate) {
1684
+        $backend = self::getBackend($itemType);
1685
+
1686
+        $l = \OC::$server->getL10N('lib');
1687
+        $result = array();
1688
+
1689
+        $column = ($itemType === 'file' || $itemType === 'folder') ? 'file_source' : 'item_source';
1690
+
1691
+        $checkReshare = self::getItemSharedWithBySource($itemType, $itemSource, self::FORMAT_NONE, null, true);
1692
+        if ($checkReshare) {
1693
+            // Check if attempting to share back to owner
1694
+            if ($checkReshare['uid_owner'] == $shareWith && $shareType == self::SHARE_TYPE_USER) {
1695
+                $message = 'Sharing %s failed, because the user %s is the original sharer';
1696
+                $message_t = $l->t('Sharing failed, because the user %s is the original sharer', [$shareWith]);
1697
+
1698
+                \OCP\Util::writeLog(Share::class, sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG);
1699
+                throw new \Exception($message_t);
1700
+            }
1701
+        }
1702
+
1703
+        if ($checkReshare && $checkReshare['uid_owner'] !== \OC_User::getUser()) {
1704
+            // Check if share permissions is granted
1705
+            if (self::isResharingAllowed() && (int)$checkReshare['permissions'] & \OCP\Constants::PERMISSION_SHARE) {
1706
+                if (~(int)$checkReshare['permissions'] & $permissions) {
1707
+                    $message = 'Sharing %s failed, because the permissions exceed permissions granted to %s';
1708
+                    $message_t = $l->t('Sharing %s failed, because the permissions exceed permissions granted to %s', array($itemSourceName, $uidOwner));
1709
+
1710
+                    \OCP\Util::writeLog(Share::class, sprintf($message, $itemSourceName, $uidOwner), \OCP\Util::DEBUG);
1711
+                    throw new \Exception($message_t);
1712
+                } else {
1713
+                    // TODO Don't check if inside folder
1714
+                    $result['parent'] = $checkReshare['id'];
1715
+
1716
+                    $result['expirationDate'] = $expirationDate;
1717
+                    // $checkReshare['expiration'] could be null and then is always less than any value
1718
+                    if(isset($checkReshare['expiration']) && $checkReshare['expiration'] < $expirationDate) {
1719
+                        $result['expirationDate'] = $checkReshare['expiration'];
1720
+                    }
1721
+
1722
+                    // only suggest the same name as new target if it is a reshare of the
1723
+                    // same file/folder and not the reshare of a child
1724
+                    if ($checkReshare[$column] === $itemSource) {
1725
+                        $result['filePath'] = $checkReshare['file_target'];
1726
+                        $result['itemSource'] = $checkReshare['item_source'];
1727
+                        $result['fileSource'] = $checkReshare['file_source'];
1728
+                        $result['suggestedItemTarget'] = $checkReshare['item_target'];
1729
+                        $result['suggestedFileTarget'] = $checkReshare['file_target'];
1730
+                    } else {
1731
+                        $result['filePath'] = ($backend instanceof \OCP\Share_Backend_File_Dependent) ? $backend->getFilePath($itemSource, $uidOwner) : null;
1732
+                        $result['suggestedItemTarget'] = null;
1733
+                        $result['suggestedFileTarget'] = null;
1734
+                        $result['itemSource'] = $itemSource;
1735
+                        $result['fileSource'] = ($backend instanceof \OCP\Share_Backend_File_Dependent) ? $itemSource : null;
1736
+                    }
1737
+                }
1738
+            } else {
1739
+                $message = 'Sharing %s failed, because resharing is not allowed';
1740
+                $message_t = $l->t('Sharing %s failed, because resharing is not allowed', array($itemSourceName));
1741
+
1742
+                \OCP\Util::writeLog(Share::class, sprintf($message, $itemSourceName), \OCP\Util::DEBUG);
1743
+                throw new \Exception($message_t);
1744
+            }
1745
+        } else {
1746
+            $result['parent'] = null;
1747
+            $result['suggestedItemTarget'] = null;
1748
+            $result['suggestedFileTarget'] = null;
1749
+            $result['itemSource'] = $itemSource;
1750
+            $result['expirationDate'] = $expirationDate;
1751
+            if (!$backend->isValidSource($itemSource, $uidOwner)) {
1752
+                $message = 'Sharing %s failed, because the sharing backend for '
1753
+                    .'%s could not find its source';
1754
+                $message_t = $l->t('Sharing %s failed, because the sharing backend for %s could not find its source', array($itemSource, $itemType));
1755
+                \OCP\Util::writeLog(Share::class, sprintf($message, $itemSource, $itemType), \OCP\Util::DEBUG);
1756
+                throw new \Exception($message_t);
1757
+            }
1758
+            if ($backend instanceof \OCP\Share_Backend_File_Dependent) {
1759
+                $result['filePath'] = $backend->getFilePath($itemSource, $uidOwner);
1760
+                if ($itemType == 'file' || $itemType == 'folder') {
1761
+                    $result['fileSource'] = $itemSource;
1762
+                } else {
1763
+                    $meta = \OC\Files\Filesystem::getFileInfo($result['filePath']);
1764
+                    $result['fileSource'] = $meta['fileid'];
1765
+                }
1766
+                if ($result['fileSource'] == -1) {
1767
+                    $message = 'Sharing %s failed, because the file could not be found in the file cache';
1768
+                    $message_t = $l->t('Sharing %s failed, because the file could not be found in the file cache', array($itemSource));
1769
+
1770
+                    \OCP\Util::writeLog(Share::class, sprintf($message, $itemSource), \OCP\Util::DEBUG);
1771
+                    throw new \Exception($message_t);
1772
+                }
1773
+            } else {
1774
+                $result['filePath'] = null;
1775
+                $result['fileSource'] = null;
1776
+            }
1777
+        }
1778
+
1779
+        return $result;
1780
+    }
1781
+
1782
+    /**
1783
+     *
1784
+     * @param array $shareData
1785
+     * @return mixed false in case of a failure or the id of the new share
1786
+     */
1787
+    private static function insertShare(array $shareData) {
1788
+
1789
+        $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` ('
1790
+            .' `item_type`, `item_source`, `item_target`, `share_type`,'
1791
+            .' `share_with`, `uid_owner`, `permissions`, `stime`, `file_source`,'
1792
+            .' `file_target`, `token`, `parent`, `expiration`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)');
1793
+        $query->bindValue(1, $shareData['itemType']);
1794
+        $query->bindValue(2, $shareData['itemSource']);
1795
+        $query->bindValue(3, $shareData['itemTarget']);
1796
+        $query->bindValue(4, $shareData['shareType']);
1797
+        $query->bindValue(5, $shareData['shareWith']);
1798
+        $query->bindValue(6, $shareData['uidOwner']);
1799
+        $query->bindValue(7, $shareData['permissions']);
1800
+        $query->bindValue(8, $shareData['shareTime']);
1801
+        $query->bindValue(9, $shareData['fileSource']);
1802
+        $query->bindValue(10, $shareData['fileTarget']);
1803
+        $query->bindValue(11, $shareData['token']);
1804
+        $query->bindValue(12, $shareData['parent']);
1805
+        $query->bindValue(13, $shareData['expiration'], 'datetime');
1806
+        $result = $query->execute();
1807
+
1808
+        $id = false;
1809
+        if ($result) {
1810
+            $id =  \OC::$server->getDatabaseConnection()->lastInsertId('*PREFIX*share');
1811
+        }
1812
+
1813
+        return $id;
1814
+
1815
+    }
1816
+
1817
+    /**
1818
+     * In case a password protected link is not yet authenticated this function will return false
1819
+     *
1820
+     * @param array $linkItem
1821
+     * @return boolean
1822
+     */
1823
+    public static function checkPasswordProtectedShare(array $linkItem) {
1824
+        if (!isset($linkItem['share_with'])) {
1825
+            return true;
1826
+        }
1827
+        if (!isset($linkItem['share_type'])) {
1828
+            return true;
1829
+        }
1830
+        if (!isset($linkItem['id'])) {
1831
+            return true;
1832
+        }
1833
+
1834
+        if ($linkItem['share_type'] != \OCP\Share::SHARE_TYPE_LINK) {
1835
+            return true;
1836
+        }
1837
+
1838
+        if ( \OC::$server->getSession()->exists('public_link_authenticated')
1839
+            && \OC::$server->getSession()->get('public_link_authenticated') === (string)$linkItem['id'] ) {
1840
+            return true;
1841
+        }
1842
+
1843
+        return false;
1844
+    }
1845
+
1846
+    /**
1847
+     * construct select statement
1848
+     * @param int $format
1849
+     * @param boolean $fileDependent ist it a file/folder share or a generla share
1850
+     * @param string $uidOwner
1851
+     * @return string select statement
1852
+     */
1853
+    private static function createSelectStatement($format, $fileDependent, $uidOwner = null) {
1854
+        $select = '*';
1855
+        if ($format == self::FORMAT_STATUSES) {
1856
+            if ($fileDependent) {
1857
+                $select = '`*PREFIX*share`.`id`, `*PREFIX*share`.`parent`, `share_type`, `path`, `storage`, '
1858
+                    . '`share_with`, `uid_owner` , `file_source`, `stime`, `*PREFIX*share`.`permissions`, '
1859
+                    . '`*PREFIX*storages`.`id` AS `storage_id`, `*PREFIX*filecache`.`parent` as `file_parent`, '
1860
+                    . '`uid_initiator`';
1861
+            } else {
1862
+                $select = '`id`, `parent`, `share_type`, `share_with`, `uid_owner`, `item_source`, `stime`, `*PREFIX*share`.`permissions`';
1863
+            }
1864
+        } else {
1865
+            if (isset($uidOwner)) {
1866
+                if ($fileDependent) {
1867
+                    $select = '`*PREFIX*share`.`id`, `item_type`, `item_source`, `*PREFIX*share`.`parent`,'
1868
+                        . ' `share_type`, `share_with`, `file_source`, `file_target`, `path`, `*PREFIX*share`.`permissions`, `stime`,'
1869
+                        . ' `expiration`, `token`, `storage`, `mail_send`, `uid_owner`, '
1870
+                        . '`*PREFIX*storages`.`id` AS `storage_id`, `*PREFIX*filecache`.`parent` as `file_parent`';
1871
+                } else {
1872
+                    $select = '`id`, `item_type`, `item_source`, `parent`, `share_type`, `share_with`, `*PREFIX*share`.`permissions`,'
1873
+                        . ' `stime`, `file_source`, `expiration`, `token`, `mail_send`, `uid_owner`';
1874
+                }
1875
+            } else {
1876
+                if ($fileDependent) {
1877
+                    if ($format == \OCA\Files_Sharing\ShareBackend\File::FORMAT_GET_FOLDER_CONTENTS || $format == \OCA\Files_Sharing\ShareBackend\File::FORMAT_FILE_APP_ROOT) {
1878
+                        $select = '`*PREFIX*share`.`id`, `item_type`, `item_source`, `*PREFIX*share`.`parent`, `uid_owner`, '
1879
+                            . '`share_type`, `share_with`, `file_source`, `path`, `file_target`, `stime`, '
1880
+                            . '`*PREFIX*share`.`permissions`, `expiration`, `storage`, `*PREFIX*filecache`.`parent` as `file_parent`, '
1881
+                            . '`name`, `mtime`, `mimetype`, `mimepart`, `size`, `encrypted`, `etag`, `mail_send`';
1882
+                    } else {
1883
+                        $select = '`*PREFIX*share`.`id`, `item_type`, `item_source`, `item_target`,'
1884
+                            . '`*PREFIX*share`.`parent`, `share_type`, `share_with`, `uid_owner`,'
1885
+                            . '`file_source`, `path`, `file_target`, `*PREFIX*share`.`permissions`,'
1886
+                            . '`stime`, `expiration`, `token`, `storage`, `mail_send`,'
1887
+                            . '`*PREFIX*storages`.`id` AS `storage_id`, `*PREFIX*filecache`.`parent` as `file_parent`';
1888
+                    }
1889
+                }
1890
+            }
1891
+        }
1892
+        return $select;
1893
+    }
1894
+
1895
+
1896
+    /**
1897
+     * transform db results
1898
+     * @param array $row result
1899
+     */
1900
+    private static function transformDBResults(&$row) {
1901
+        if (isset($row['id'])) {
1902
+            $row['id'] = (int) $row['id'];
1903
+        }
1904
+        if (isset($row['share_type'])) {
1905
+            $row['share_type'] = (int) $row['share_type'];
1906
+        }
1907
+        if (isset($row['parent'])) {
1908
+            $row['parent'] = (int) $row['parent'];
1909
+        }
1910
+        if (isset($row['file_parent'])) {
1911
+            $row['file_parent'] = (int) $row['file_parent'];
1912
+        }
1913
+        if (isset($row['file_source'])) {
1914
+            $row['file_source'] = (int) $row['file_source'];
1915
+        }
1916
+        if (isset($row['permissions'])) {
1917
+            $row['permissions'] = (int) $row['permissions'];
1918
+        }
1919
+        if (isset($row['storage'])) {
1920
+            $row['storage'] = (int) $row['storage'];
1921
+        }
1922
+        if (isset($row['stime'])) {
1923
+            $row['stime'] = (int) $row['stime'];
1924
+        }
1925
+        if (isset($row['expiration']) && $row['share_type'] !== self::SHARE_TYPE_LINK) {
1926
+            // discard expiration date for non-link shares, which might have been
1927
+            // set by ancient bugs
1928
+            $row['expiration'] = null;
1929
+        }
1930
+    }
1931
+
1932
+    /**
1933
+     * format result
1934
+     * @param array $items result
1935
+     * @param string $column is it a file share or a general share ('file_target' or 'item_target')
1936
+     * @param \OCP\Share_Backend $backend sharing backend
1937
+     * @param int $format
1938
+     * @param array $parameters additional format parameters
1939
+     * @return array format result
1940
+     */
1941
+    private static function formatResult($items, $column, $backend, $format = self::FORMAT_NONE , $parameters = null) {
1942
+        if ($format === self::FORMAT_NONE) {
1943
+            return $items;
1944
+        } else if ($format === self::FORMAT_STATUSES) {
1945
+            $statuses = array();
1946
+            foreach ($items as $item) {
1947
+                if ($item['share_type'] === self::SHARE_TYPE_LINK) {
1948
+                    if ($item['uid_initiator'] !== \OC::$server->getUserSession()->getUser()->getUID()) {
1949
+                        continue;
1950
+                    }
1951
+                    $statuses[$item[$column]]['link'] = true;
1952
+                } else if (!isset($statuses[$item[$column]])) {
1953
+                    $statuses[$item[$column]]['link'] = false;
1954
+                }
1955
+                if (!empty($item['file_target'])) {
1956
+                    $statuses[$item[$column]]['path'] = $item['path'];
1957
+                }
1958
+            }
1959
+            return $statuses;
1960
+        } else {
1961
+            return $backend->formatItems($items, $format, $parameters);
1962
+        }
1963
+    }
1964
+
1965
+    /**
1966
+     * remove protocol from URL
1967
+     *
1968
+     * @param string $url
1969
+     * @return string
1970
+     */
1971
+    public static function removeProtocolFromUrl($url) {
1972
+        if (strpos($url, 'https://') === 0) {
1973
+            return substr($url, strlen('https://'));
1974
+        } else if (strpos($url, 'http://') === 0) {
1975
+            return substr($url, strlen('http://'));
1976
+        }
1977
+
1978
+        return $url;
1979
+    }
1980
+
1981
+    /**
1982
+     * try http post first with https and then with http as a fallback
1983
+     *
1984
+     * @param string $remoteDomain
1985
+     * @param string $urlSuffix
1986
+     * @param array $fields post parameters
1987
+     * @return array
1988
+     */
1989
+    private static function tryHttpPostToShareEndpoint($remoteDomain, $urlSuffix, array $fields) {
1990
+        $protocol = 'https://';
1991
+        $result = [
1992
+            'success' => false,
1993
+            'result' => '',
1994
+        ];
1995
+        $try = 0;
1996
+        $discoveryService = \OC::$server->query(\OCP\OCS\IDiscoveryService::class);
1997
+        while ($result['success'] === false && $try < 2) {
1998
+            $federationEndpoints = $discoveryService->discover($protocol . $remoteDomain, 'FEDERATED_SHARING');
1999
+            $endpoint = isset($federationEndpoints['share']) ? $federationEndpoints['share'] : '/ocs/v2.php/cloud/shares';
2000
+            $result = \OC::$server->getHTTPHelper()->post($protocol . $remoteDomain . $endpoint . $urlSuffix . '?format=' . self::RESPONSE_FORMAT, $fields);
2001
+            $try++;
2002
+            $protocol = 'http://';
2003
+        }
2004
+
2005
+        return $result;
2006
+    }
2007
+
2008
+    /**
2009
+     * send server-to-server share to remote server
2010
+     *
2011
+     * @param string $token
2012
+     * @param string $shareWith
2013
+     * @param string $name
2014
+     * @param int $remote_id
2015
+     * @param string $owner
2016
+     * @return bool
2017
+     */
2018
+    private static function sendRemoteShare($token, $shareWith, $name, $remote_id, $owner) {
2019
+
2020
+        list($user, $remote) = Helper::splitUserRemote($shareWith);
2021
+
2022
+        if ($user && $remote) {
2023
+            $url = $remote;
2024
+
2025
+            $local = \OC::$server->getURLGenerator()->getAbsoluteURL('/');
2026
+
2027
+            $fields = array(
2028
+                'shareWith' => $user,
2029
+                'token' => $token,
2030
+                'name' => $name,
2031
+                'remoteId' => $remote_id,
2032
+                'owner' => $owner,
2033
+                'remote' => $local,
2034
+            );
2035
+
2036
+            $url = self::removeProtocolFromUrl($url);
2037
+            $result = self::tryHttpPostToShareEndpoint($url, '', $fields);
2038
+            $status = json_decode($result['result'], true);
2039
+
2040
+            if ($result['success'] && ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200)) {
2041
+                \OC_Hook::emit(Share::class, 'federated_share_added', ['server' => $remote]);
2042
+                return true;
2043
+            }
2044
+
2045
+        }
2046
+
2047
+        return false;
2048
+    }
2049
+
2050
+    /**
2051
+     * send server-to-server unshare to remote server
2052
+     *
2053
+     * @param string $remote url
2054
+     * @param int $id share id
2055
+     * @param string $token
2056
+     * @return bool
2057
+     */
2058
+    private static function sendRemoteUnshare($remote, $id, $token) {
2059
+        $url = rtrim($remote, '/');
2060
+        $fields = array('token' => $token, 'format' => 'json');
2061
+        $url = self::removeProtocolFromUrl($url);
2062
+        $result = self::tryHttpPostToShareEndpoint($url, '/'.$id.'/unshare', $fields);
2063
+        $status = json_decode($result['result'], true);
2064
+
2065
+        return ($result['success'] && ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200));
2066
+    }
2067
+
2068
+    /**
2069
+     * check if user can only share with group members
2070
+     * @return bool
2071
+     */
2072
+    public static function shareWithGroupMembersOnly() {
2073
+        $value = \OC::$server->getConfig()->getAppValue('core', 'shareapi_only_share_with_group_members', 'no');
2074
+        return ($value === 'yes') ? true : false;
2075
+    }
2076
+
2077
+    /**
2078
+     * @return bool
2079
+     */
2080
+    public static function isDefaultExpireDateEnabled() {
2081
+        $defaultExpireDateEnabled = \OC::$server->getConfig()->getAppValue('core', 'shareapi_default_expire_date', 'no');
2082
+        return ($defaultExpireDateEnabled === "yes") ? true : false;
2083
+    }
2084
+
2085
+    /**
2086
+     * @return int
2087
+     */
2088
+    public static function getExpireInterval() {
2089
+        return (int)\OC::$server->getConfig()->getAppValue('core', 'shareapi_expire_after_n_days', '7');
2090
+    }
2091
+
2092
+    /**
2093
+     * Checks whether the given path is reachable for the given owner
2094
+     *
2095
+     * @param string $path path relative to files
2096
+     * @param string $ownerStorageId storage id of the owner
2097
+     *
2098
+     * @return boolean true if file is reachable, false otherwise
2099
+     */
2100
+    private static function isFileReachable($path, $ownerStorageId) {
2101
+        // if outside the home storage, file is always considered reachable
2102
+        if (!(substr($ownerStorageId, 0, 6) === 'home::' ||
2103
+            substr($ownerStorageId, 0, 13) === 'object::user:'
2104
+        )) {
2105
+            return true;
2106
+        }
2107
+
2108
+        // if inside the home storage, the file has to be under "/files/"
2109
+        $path = ltrim($path, '/');
2110
+        if (substr($path, 0, 6) === 'files/') {
2111
+            return true;
2112
+        }
2113
+
2114
+        return false;
2115
+    }
2116
+
2117
+    /**
2118
+     * @param IConfig $config
2119
+     * @return bool
2120
+     */
2121
+    public static function enforcePassword(IConfig $config) {
2122
+        $enforcePassword = $config->getAppValue('core', 'shareapi_enforce_links_password', 'no');
2123
+        return ($enforcePassword === "yes") ? true : false;
2124
+    }
2125
+
2126
+    /**
2127
+     * @param string $password
2128
+     * @throws \Exception
2129
+     */
2130
+    private static function verifyPassword($password) {
2131
+
2132
+        $accepted = true;
2133
+        $message = '';
2134
+        \OCP\Util::emitHook('\OC\Share', 'verifyPassword', [
2135
+            'password' => $password,
2136
+            'accepted' => &$accepted,
2137
+            'message' => &$message
2138
+        ]);
2139
+
2140
+        if (!$accepted) {
2141
+            throw new \Exception($message);
2142
+        }
2143
+    }
2144 2144
 }
Please login to merge, or discard this patch.
Spacing   +50 added lines, -50 removed lines patch added patch discarded remove patch
@@ -89,7 +89,7 @@  discard block
 block discarded – undo
89 89
 					'collectionOf' => $collectionOf,
90 90
 					'supportedFileExtensions' => $supportedFileExtensions
91 91
 				);
92
-				if(count(self::$backendTypes) === 1) {
92
+				if (count(self::$backendTypes) === 1) {
93 93
 					Util::addScript('core', 'merged-share-backend');
94 94
 					\OC_Util::addStyle('core', 'share');
95 95
 				}
@@ -160,7 +160,7 @@  discard block
 block discarded – undo
160 160
 
161 161
 		$select = self::createSelectStatement(self::FORMAT_NONE, $fileDependent);
162 162
 
163
-		$where .= ' `' . $column . '` = ? AND `item_type` = ? ';
163
+		$where .= ' `'.$column.'` = ? AND `item_type` = ? ';
164 164
 		$arguments = array($itemSource, $itemType);
165 165
 		// for link shares $user === null
166 166
 		if ($user !== null) {
@@ -178,7 +178,7 @@  discard block
 block discarded – undo
178 178
 			$arguments[] = $owner;
179 179
 		}
180 180
 
181
-		$query = \OC_DB::prepare('SELECT ' . $select . ' FROM `*PREFIX*share` '. $fileDependentWhere . $where);
181
+		$query = \OC_DB::prepare('SELECT '.$select.' FROM `*PREFIX*share` '.$fileDependentWhere.$where);
182 182
 
183 183
 		$result = \OC_DB::executeAudited($query, $arguments);
184 184
 
@@ -186,7 +186,7 @@  discard block
 block discarded – undo
186 186
 			if ($fileDependent && !self::isFileReachable($row['path'], $row['storage_id'])) {
187 187
 				continue;
188 188
 			}
189
-			if ($fileDependent && (int)$row['file_parent'] === -1) {
189
+			if ($fileDependent && (int) $row['file_parent'] === -1) {
190 190
 				// if it is a mount point we need to get the path from the mount manager
191 191
 				$mountManager = \OC\Files\Filesystem::getMountManager();
192 192
 				$mountPoint = $mountManager->findByStorageId($row['storage_id']);
@@ -197,7 +197,7 @@  discard block
 block discarded – undo
197 197
 					$row['path'] = $path;
198 198
 				} else {
199 199
 					\OC::$server->getLogger()->warning(
200
-						'Could not resolve mount point for ' . $row['storage_id'],
200
+						'Could not resolve mount point for '.$row['storage_id'],
201 201
 						['app' => Share::class]
202 202
 					);
203 203
 				}
@@ -206,7 +206,7 @@  discard block
 block discarded – undo
206 206
 		}
207 207
 
208 208
 		//if didn't found a result than let's look for a group share.
209
-		if(empty($shares) && $user !== null) {
209
+		if (empty($shares) && $user !== null) {
210 210
 			$userObject = \OC::$server->getUserManager()->get($user);
211 211
 			$groups = [];
212 212
 			if ($userObject) {
@@ -214,7 +214,7 @@  discard block
 block discarded – undo
214 214
 			}
215 215
 
216 216
 			if (!empty($groups)) {
217
-				$where = $fileDependentWhere . ' WHERE `' . $column . '` = ? AND `item_type` = ? AND `share_with` in (?)';
217
+				$where = $fileDependentWhere.' WHERE `'.$column.'` = ? AND `item_type` = ? AND `share_with` in (?)';
218 218
 				$arguments = array($itemSource, $itemType, $groups);
219 219
 				$types = array(null, null, IQueryBuilder::PARAM_STR_ARRAY);
220 220
 
@@ -228,7 +228,7 @@  discard block
 block discarded – undo
228 228
 				// class isn't static anymore...
229 229
 				$conn = \OC::$server->getDatabaseConnection();
230 230
 				$result = $conn->executeQuery(
231
-					'SELECT ' . $select . ' FROM `*PREFIX*share` ' . $where,
231
+					'SELECT '.$select.' FROM `*PREFIX*share` '.$where,
232 232
 					$arguments,
233 233
 					$types
234 234
 				);
@@ -270,7 +270,7 @@  discard block
 block discarded – undo
270 270
 		$query = \OC_DB::prepare('SELECT * FROM `*PREFIX*share` WHERE `token` = ?', 1);
271 271
 		$result = $query->execute(array($token));
272 272
 		if ($result === false) {
273
-			\OCP\Util::writeLog(Share::class, \OC_DB::getErrorMessage() . ', token=' . $token, \OCP\Util::ERROR);
273
+			\OCP\Util::writeLog(Share::class, \OC_DB::getErrorMessage().', token='.$token, \OCP\Util::ERROR);
274 274
 		}
275 275
 		$row = $result->fetchRow();
276 276
 		if ($row === false) {
@@ -399,12 +399,12 @@  discard block
 block discarded – undo
399 399
 
400 400
 		//verify that we don't share a folder which already contains a share mount point
401 401
 		if ($itemType === 'folder') {
402
-			$path = '/' . $uidOwner . '/files' . \OC\Files\Filesystem::getPath($itemSource) . '/';
402
+			$path = '/'.$uidOwner.'/files'.\OC\Files\Filesystem::getPath($itemSource).'/';
403 403
 			$mountManager = \OC\Files\Filesystem::getMountManager();
404 404
 			$mounts = $mountManager->findIn($path);
405 405
 			foreach ($mounts as $mount) {
406 406
 				if ($mount->getStorage()->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) {
407
-					$message = 'Sharing "' . $itemSourceName . '" failed, because it contains files shared with you!';
407
+					$message = 'Sharing "'.$itemSourceName.'" failed, because it contains files shared with you!';
408 408
 					\OCP\Util::writeLog(Share::class, $message, \OCP\Util::DEBUG);
409 409
 					throw new \Exception($message);
410 410
 				}
@@ -414,7 +414,7 @@  discard block
 block discarded – undo
414 414
 
415 415
 		// single file shares should never have delete permissions
416 416
 		if ($itemType === 'file') {
417
-			$permissions = (int)$permissions & ~\OCP\Constants::PERMISSION_DELETE;
417
+			$permissions = (int) $permissions & ~\OCP\Constants::PERMISSION_DELETE;
418 418
 		}
419 419
 
420 420
 		//Validate expirationDate
@@ -568,7 +568,7 @@  discard block
 block discarded – undo
568 568
 					} else {
569 569
 						// reuse the already set password, but only if we change permissions
570 570
 						// otherwise the user disabled the password protection
571
-						if ($checkExists && (int)$permissions !== (int)$oldPermissions) {
571
+						if ($checkExists && (int) $permissions !== (int) $oldPermissions) {
572 572
 							$shareWith = $checkExists['share_with'];
573 573
 						}
574 574
 					}
@@ -640,10 +640,10 @@  discard block
 block discarded – undo
640 640
 				throw new \Exception($message_t);
641 641
 			}
642 642
 
643
-			$token = \OC::$server->getSecureRandom()->generate(self::TOKEN_LENGTH, \OCP\Security\ISecureRandom::CHAR_LOWER . \OCP\Security\ISecureRandom::CHAR_UPPER .
643
+			$token = \OC::$server->getSecureRandom()->generate(self::TOKEN_LENGTH, \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_UPPER.
644 644
 				\OCP\Security\ISecureRandom::CHAR_DIGITS);
645 645
 
646
-			$shareWith = $user . '@' . $remote;
646
+			$shareWith = $user.'@'.$remote;
647 647
 			$shareId = self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, null, $token, $itemSourceName);
648 648
 
649 649
 			$send = false;
@@ -694,12 +694,12 @@  discard block
 block discarded – undo
694 694
 		$currentUser = $owner ? $owner : \OC_User::getUser();
695 695
 		foreach ($items as $item) {
696 696
 			// delete the item with the expected share_type and owner
697
-			if ((int)$item['share_type'] === (int)$shareType && $item['uid_owner'] === $currentUser) {
697
+			if ((int) $item['share_type'] === (int) $shareType && $item['uid_owner'] === $currentUser) {
698 698
 				$toDelete = $item;
699 699
 				// if there is more then one result we don't have to delete the children
700 700
 				// but update their parent. For group shares the new parent should always be
701 701
 				// the original group share and not the db entry with the unique name
702
-			} else if ((int)$item['share_type'] === self::$shareTypeGroupUserUnique) {
702
+			} else if ((int) $item['share_type'] === self::$shareTypeGroupUserUnique) {
703 703
 				$newParent = $item['parent'];
704 704
 			} else {
705 705
 				$newParent = $item['id'];
@@ -731,7 +731,7 @@  discard block
 block discarded – undo
731 731
 
732 732
 		$result = $query->execute(array($status, $itemType, $itemSource, $shareType, $recipient));
733 733
 
734
-		if($result === false) {
734
+		if ($result === false) {
735 735
 			\OCP\Util::writeLog(Share::class, 'Couldn\'t set send mail status', \OCP\Util::ERROR);
736 736
 		}
737 737
 	}
@@ -757,7 +757,7 @@  discard block
 block discarded – undo
757 757
 		if ($defaultExpireDateEnforced && $shareTime === null) {
758 758
 			$items = self::getItemShared($itemType, $itemSource);
759 759
 			$firstItem = reset($items);
760
-			$shareTime = (int)$firstItem['stime'];
760
+			$shareTime = (int) $firstItem['stime'];
761 761
 		}
762 762
 
763 763
 		if ($defaultExpireDateEnforced) {
@@ -765,9 +765,9 @@  discard block
 block discarded – undo
765 765
 			$maxDate = new \DateTime();
766 766
 			$maxDate->setTimestamp($shareTime);
767 767
 			$maxDays = \OC::$server->getConfig()->getAppValue('core', 'shareapi_expire_after_n_days', '7');
768
-			$maxDate->add(new \DateInterval('P' . $maxDays . 'D'));
768
+			$maxDate->add(new \DateInterval('P'.$maxDays.'D'));
769 769
 			if ($date > $maxDate) {
770
-				$warning = 'Cannot set expiration date. Shares cannot expire later than ' . $maxDays . ' after they have been shared';
770
+				$warning = 'Cannot set expiration date. Shares cannot expire later than '.$maxDays.' after they have been shared';
771 771
 				$warning_t = $l->t('Cannot set expiration date. Shares cannot expire later than %s after they have been shared', array($maxDays));
772 772
 				\OCP\Util::writeLog(Share::class, $warning, \OCP\Util::WARN);
773 773
 				throw new \Exception($warning_t);
@@ -829,7 +829,7 @@  discard block
 block discarded – undo
829 829
 	 */
830 830
 	protected static function unshareItem(array $item, $newParent = null) {
831 831
 
832
-		$shareType = (int)$item['share_type'];
832
+		$shareType = (int) $item['share_type'];
833 833
 		$shareWith = null;
834 834
 		if ($shareType !== \OCP\Share::SHARE_TYPE_LINK) {
835 835
 			$shareWith = $item['share_with'];
@@ -845,7 +845,7 @@  discard block
 block discarded – undo
845 845
 			'itemParent'    => $item['parent'],
846 846
 			'uidOwner'      => $item['uid_owner'],
847 847
 		);
848
-		if($item['item_type'] === 'file' || $item['item_type'] === 'folder') {
848
+		if ($item['item_type'] === 'file' || $item['item_type'] === 'folder') {
849 849
 			$hookParams['fileSource'] = $item['file_source'];
850 850
 			$hookParams['fileTarget'] = $item['file_target'];
851 851
 		}
@@ -855,7 +855,7 @@  discard block
 block discarded – undo
855 855
 		$deletedShares[] = $hookParams;
856 856
 		$hookParams['deletedShares'] = $deletedShares;
857 857
 		\OC_Hook::emit(Share::class, 'post_unshare', $hookParams);
858
-		if ((int)$item['share_type'] === \OCP\Share::SHARE_TYPE_REMOTE && \OC::$server->getUserSession()->getUser()) {
858
+		if ((int) $item['share_type'] === \OCP\Share::SHARE_TYPE_REMOTE && \OC::$server->getUserSession()->getUser()) {
859 859
 			list(, $remote) = Helper::splitUserRemote($item['share_with']);
860 860
 			self::sendRemoteUnshare($remote, $item['id'], $item['token']);
861 861
 		}
@@ -1016,7 +1016,7 @@  discard block
 block discarded – undo
1016 1016
 		// Get filesystem root to add it to the file target and remove from the
1017 1017
 		// file source, match file_source with the file cache
1018 1018
 		if ($itemType == 'file' || $itemType == 'folder') {
1019
-			if(!is_null($uidOwner)) {
1019
+			if (!is_null($uidOwner)) {
1020 1020
 				$root = \OC\Files\Filesystem::getRoot();
1021 1021
 			} else {
1022 1022
 				$root = '';
@@ -1161,7 +1161,7 @@  discard block
 block discarded – undo
1161 1161
 		$result = $query->execute($queryArgs);
1162 1162
 		if ($result === false) {
1163 1163
 			\OCP\Util::writeLog(Share::class,
1164
-				\OC_DB::getErrorMessage() . ', select=' . $select . ' where=',
1164
+				\OC_DB::getErrorMessage().', select='.$select.' where=',
1165 1165
 				\OCP\Util::ERROR);
1166 1166
 		}
1167 1167
 		$items = array();
@@ -1203,14 +1203,14 @@  discard block
 block discarded – undo
1203 1203
 						}
1204 1204
 						// Switch ids if sharing permission is granted on only
1205 1205
 						// one share to ensure correct parent is used if resharing
1206
-						if (~(int)$items[$id]['permissions'] & \OCP\Constants::PERMISSION_SHARE
1207
-							&& (int)$row['permissions'] & \OCP\Constants::PERMISSION_SHARE) {
1206
+						if (~(int) $items[$id]['permissions'] & \OCP\Constants::PERMISSION_SHARE
1207
+							&& (int) $row['permissions'] & \OCP\Constants::PERMISSION_SHARE) {
1208 1208
 							$items[$row['id']] = $items[$id];
1209 1209
 							$switchedItems[$id] = $row['id'];
1210 1210
 							unset($items[$id]);
1211 1211
 							$id = $row['id'];
1212 1212
 						}
1213
-						$items[$id]['permissions'] |= (int)$row['permissions'];
1213
+						$items[$id]['permissions'] |= (int) $row['permissions'];
1214 1214
 
1215 1215
 					}
1216 1216
 					continue;
@@ -1224,8 +1224,8 @@  discard block
 block discarded – undo
1224 1224
 					$query = \OC_DB::prepare('SELECT `file_target` FROM `*PREFIX*share` WHERE `id` = ?');
1225 1225
 					$parentResult = $query->execute(array($row['parent']));
1226 1226
 					if ($result === false) {
1227
-						\OCP\Util::writeLog(Share::class, 'Can\'t select parent: ' .
1228
-							\OC_DB::getErrorMessage() . ', select=' . $select . ' where=' . $where,
1227
+						\OCP\Util::writeLog(Share::class, 'Can\'t select parent: '.
1228
+							\OC_DB::getErrorMessage().', select='.$select.' where='.$where,
1229 1229
 							\OCP\Util::ERROR);
1230 1230
 					} else {
1231 1231
 						$parentRow = $parentResult->fetchRow();
@@ -1235,7 +1235,7 @@  discard block
 block discarded – undo
1235 1235
 						$subPath = substr($row['path'], $pos);
1236 1236
 						$splitPath = explode('/', $subPath);
1237 1237
 						foreach (array_slice($splitPath, 2) as $pathPart) {
1238
-							$tmpPath = $tmpPath . '/' . $pathPart;
1238
+							$tmpPath = $tmpPath.'/'.$pathPart;
1239 1239
 						}
1240 1240
 						$row['path'] = $tmpPath;
1241 1241
 					}
@@ -1254,7 +1254,7 @@  discard block
 block discarded – undo
1254 1254
 				}
1255 1255
 			}
1256 1256
 
1257
-			if($checkExpireDate) {
1257
+			if ($checkExpireDate) {
1258 1258
 				if (self::expireItem($row)) {
1259 1259
 					continue;
1260 1260
 				}
@@ -1265,10 +1265,10 @@  discard block
 block discarded – undo
1265 1265
 			}
1266 1266
 			// Add display names to result
1267 1267
 			$row['share_with_displayname'] = $row['share_with'];
1268
-			if ( isset($row['share_with']) && $row['share_with'] != '' &&
1268
+			if (isset($row['share_with']) && $row['share_with'] != '' &&
1269 1269
 				$row['share_type'] === self::SHARE_TYPE_USER) {
1270 1270
 				$row['share_with_displayname'] = \OCP\User::getDisplayName($row['share_with']);
1271
-			} else if(isset($row['share_with']) && $row['share_with'] != '' &&
1271
+			} else if (isset($row['share_with']) && $row['share_with'] != '' &&
1272 1272
 				$row['share_type'] === self::SHARE_TYPE_REMOTE) {
1273 1273
 				$addressBookEntries = \OC::$server->getContactsManager()->search($row['share_with'], ['CLOUD']);
1274 1274
 				foreach ($addressBookEntries as $entry) {
@@ -1279,7 +1279,7 @@  discard block
 block discarded – undo
1279 1279
 					}
1280 1280
 				}
1281 1281
 			}
1282
-			if ( isset($row['uid_owner']) && $row['uid_owner'] != '') {
1282
+			if (isset($row['uid_owner']) && $row['uid_owner'] != '') {
1283 1283
 				$row['displayname_owner'] = \OCP\User::getDisplayName($row['uid_owner']);
1284 1284
 			}
1285 1285
 
@@ -1427,7 +1427,7 @@  discard block
 block discarded – undo
1427 1427
 				// for file/folder shares we need to compare file_source, otherwise we compare item_source
1428 1428
 				// only group shares if they already point to the same target, otherwise the file where shared
1429 1429
 				// before grouping of shares was added. In this case we don't group them toi avoid confusions
1430
-				if (( $fileSharing && $item['file_source'] === $r['file_source'] && $item['file_target'] === $r['file_target']) ||
1430
+				if (($fileSharing && $item['file_source'] === $r['file_source'] && $item['file_target'] === $r['file_target']) ||
1431 1431
 					(!$fileSharing && $item['item_source'] === $r['item_source'] && $item['item_target'] === $r['item_target'])) {
1432 1432
 					// add the first item to the list of grouped shares
1433 1433
 					if (!isset($result[$key]['grouped'])) {
@@ -1473,7 +1473,7 @@  discard block
 block discarded – undo
1473 1473
 		$groupItemTarget = $itemTarget = $fileSource = $parent = 0;
1474 1474
 
1475 1475
 		$result = self::checkReshare($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, $itemSourceName, $expirationDate);
1476
-		if(!empty($result)) {
1476
+		if (!empty($result)) {
1477 1477
 			$parent = $result['parent'];
1478 1478
 			$itemSource = $result['itemSource'];
1479 1479
 			$fileSource = $result['fileSource'];
@@ -1567,11 +1567,11 @@  discard block
 block discarded – undo
1567 1567
 				$itemTarget = $sourceExists['item_target'];
1568 1568
 
1569 1569
 				// for group shares we don't need a additional entry if the target is the same
1570
-				if($isGroupShare && $groupItemTarget === $itemTarget) {
1570
+				if ($isGroupShare && $groupItemTarget === $itemTarget) {
1571 1571
 					continue;
1572 1572
 				}
1573 1573
 
1574
-			} elseif(!$sourceExists && !$isGroupShare)  {
1574
+			} elseif (!$sourceExists && !$isGroupShare) {
1575 1575
 
1576 1576
 				$itemTarget = Helper::generateTarget($itemType, $itemSource, $userShareType, $user,
1577 1577
 					$uidOwner, $suggestedItemTarget, $parent);
@@ -1702,8 +1702,8 @@  discard block
 block discarded – undo
1702 1702
 
1703 1703
 		if ($checkReshare && $checkReshare['uid_owner'] !== \OC_User::getUser()) {
1704 1704
 			// Check if share permissions is granted
1705
-			if (self::isResharingAllowed() && (int)$checkReshare['permissions'] & \OCP\Constants::PERMISSION_SHARE) {
1706
-				if (~(int)$checkReshare['permissions'] & $permissions) {
1705
+			if (self::isResharingAllowed() && (int) $checkReshare['permissions'] & \OCP\Constants::PERMISSION_SHARE) {
1706
+				if (~(int) $checkReshare['permissions'] & $permissions) {
1707 1707
 					$message = 'Sharing %s failed, because the permissions exceed permissions granted to %s';
1708 1708
 					$message_t = $l->t('Sharing %s failed, because the permissions exceed permissions granted to %s', array($itemSourceName, $uidOwner));
1709 1709
 
@@ -1715,7 +1715,7 @@  discard block
 block discarded – undo
1715 1715
 
1716 1716
 					$result['expirationDate'] = $expirationDate;
1717 1717
 					// $checkReshare['expiration'] could be null and then is always less than any value
1718
-					if(isset($checkReshare['expiration']) && $checkReshare['expiration'] < $expirationDate) {
1718
+					if (isset($checkReshare['expiration']) && $checkReshare['expiration'] < $expirationDate) {
1719 1719
 						$result['expirationDate'] = $checkReshare['expiration'];
1720 1720
 					}
1721 1721
 
@@ -1807,7 +1807,7 @@  discard block
 block discarded – undo
1807 1807
 
1808 1808
 		$id = false;
1809 1809
 		if ($result) {
1810
-			$id =  \OC::$server->getDatabaseConnection()->lastInsertId('*PREFIX*share');
1810
+			$id = \OC::$server->getDatabaseConnection()->lastInsertId('*PREFIX*share');
1811 1811
 		}
1812 1812
 
1813 1813
 		return $id;
@@ -1835,8 +1835,8 @@  discard block
 block discarded – undo
1835 1835
 			return true;
1836 1836
 		}
1837 1837
 
1838
-		if ( \OC::$server->getSession()->exists('public_link_authenticated')
1839
-			&& \OC::$server->getSession()->get('public_link_authenticated') === (string)$linkItem['id'] ) {
1838
+		if (\OC::$server->getSession()->exists('public_link_authenticated')
1839
+			&& \OC::$server->getSession()->get('public_link_authenticated') === (string) $linkItem['id']) {
1840 1840
 			return true;
1841 1841
 		}
1842 1842
 
@@ -1938,7 +1938,7 @@  discard block
 block discarded – undo
1938 1938
 	 * @param array $parameters additional format parameters
1939 1939
 	 * @return array format result
1940 1940
 	 */
1941
-	private static function formatResult($items, $column, $backend, $format = self::FORMAT_NONE , $parameters = null) {
1941
+	private static function formatResult($items, $column, $backend, $format = self::FORMAT_NONE, $parameters = null) {
1942 1942
 		if ($format === self::FORMAT_NONE) {
1943 1943
 			return $items;
1944 1944
 		} else if ($format === self::FORMAT_STATUSES) {
@@ -1995,9 +1995,9 @@  discard block
 block discarded – undo
1995 1995
 		$try = 0;
1996 1996
 		$discoveryService = \OC::$server->query(\OCP\OCS\IDiscoveryService::class);
1997 1997
 		while ($result['success'] === false && $try < 2) {
1998
-			$federationEndpoints = $discoveryService->discover($protocol . $remoteDomain, 'FEDERATED_SHARING');
1998
+			$federationEndpoints = $discoveryService->discover($protocol.$remoteDomain, 'FEDERATED_SHARING');
1999 1999
 			$endpoint = isset($federationEndpoints['share']) ? $federationEndpoints['share'] : '/ocs/v2.php/cloud/shares';
2000
-			$result = \OC::$server->getHTTPHelper()->post($protocol . $remoteDomain . $endpoint . $urlSuffix . '?format=' . self::RESPONSE_FORMAT, $fields);
2000
+			$result = \OC::$server->getHTTPHelper()->post($protocol.$remoteDomain.$endpoint.$urlSuffix.'?format='.self::RESPONSE_FORMAT, $fields);
2001 2001
 			$try++;
2002 2002
 			$protocol = 'http://';
2003 2003
 		}
@@ -2086,7 +2086,7 @@  discard block
 block discarded – undo
2086 2086
 	 * @return int
2087 2087
 	 */
2088 2088
 	public static function getExpireInterval() {
2089
-		return (int)\OC::$server->getConfig()->getAppValue('core', 'shareapi_expire_after_n_days', '7');
2089
+		return (int) \OC::$server->getConfig()->getAppValue('core', 'shareapi_expire_after_n_days', '7');
2090 2090
 	}
2091 2091
 
2092 2092
 	/**
Please login to merge, or discard this patch.
lib/private/Share20/Manager.php 2 patches
Doc Comments   +5 added lines, -2 removed lines patch added patch discarded remove patch
@@ -575,7 +575,7 @@  discard block
 block discarded – undo
575 575
 	 * Share a path
576 576
 	 *
577 577
 	 * @param \OCP\Share\IShare $share
578
-	 * @return Share The share object
578
+	 * @return Share\IShare The share object
579 579
 	 * @throws \Exception
580 580
 	 *
581 581
 	 * TODO: handle link share permissions or check them
@@ -1161,7 +1161,7 @@  discard block
 block discarded – undo
1161 1161
 	 * Get the share by token possible with password
1162 1162
 	 *
1163 1163
 	 * @param string $token
1164
-	 * @return Share
1164
+	 * @return Share\IShare
1165 1165
 	 *
1166 1166
 	 * @throws ShareNotFound
1167 1167
 	 */
@@ -1223,6 +1223,9 @@  discard block
 block discarded – undo
1223 1223
 		return $share;
1224 1224
 	}
1225 1225
 
1226
+	/**
1227
+	 * @param Share\IShare $share
1228
+	 */
1226 1229
 	protected function checkExpireDate($share) {
1227 1230
 		if ($share->getExpirationDate() !== null &&
1228 1231
 			$share->getExpirationDate() <= new \DateTime()) {
Please login to merge, or discard this patch.
Indentation   +1470 added lines, -1470 removed lines patch added patch discarded remove patch
@@ -71,1498 +71,1498 @@
 block discarded – undo
71 71
  */
72 72
 class Manager implements IManager {
73 73
 
74
-	/** @var IProviderFactory */
75
-	private $factory;
76
-	/** @var ILogger */
77
-	private $logger;
78
-	/** @var IConfig */
79
-	private $config;
80
-	/** @var ISecureRandom */
81
-	private $secureRandom;
82
-	/** @var IHasher */
83
-	private $hasher;
84
-	/** @var IMountManager */
85
-	private $mountManager;
86
-	/** @var IGroupManager */
87
-	private $groupManager;
88
-	/** @var IL10N */
89
-	private $l;
90
-	/** @var IFactory */
91
-	private $l10nFactory;
92
-	/** @var IUserManager */
93
-	private $userManager;
94
-	/** @var IRootFolder */
95
-	private $rootFolder;
96
-	/** @var CappedMemoryCache */
97
-	private $sharingDisabledForUsersCache;
98
-	/** @var EventDispatcher */
99
-	private $eventDispatcher;
100
-	/** @var LegacyHooks */
101
-	private $legacyHooks;
102
-	/** @var IMailer */
103
-	private $mailer;
104
-	/** @var IURLGenerator */
105
-	private $urlGenerator;
106
-	/** @var \OC_Defaults */
107
-	private $defaults;
108
-
109
-
110
-	/**
111
-	 * Manager constructor.
112
-	 *
113
-	 * @param ILogger $logger
114
-	 * @param IConfig $config
115
-	 * @param ISecureRandom $secureRandom
116
-	 * @param IHasher $hasher
117
-	 * @param IMountManager $mountManager
118
-	 * @param IGroupManager $groupManager
119
-	 * @param IL10N $l
120
-	 * @param IFactory $l10nFactory
121
-	 * @param IProviderFactory $factory
122
-	 * @param IUserManager $userManager
123
-	 * @param IRootFolder $rootFolder
124
-	 * @param EventDispatcher $eventDispatcher
125
-	 * @param IMailer $mailer
126
-	 * @param IURLGenerator $urlGenerator
127
-	 * @param \OC_Defaults $defaults
128
-	 */
129
-	public function __construct(
130
-			ILogger $logger,
131
-			IConfig $config,
132
-			ISecureRandom $secureRandom,
133
-			IHasher $hasher,
134
-			IMountManager $mountManager,
135
-			IGroupManager $groupManager,
136
-			IL10N $l,
137
-			IFactory $l10nFactory,
138
-			IProviderFactory $factory,
139
-			IUserManager $userManager,
140
-			IRootFolder $rootFolder,
141
-			EventDispatcher $eventDispatcher,
142
-			IMailer $mailer,
143
-			IURLGenerator $urlGenerator,
144
-			\OC_Defaults $defaults
145
-	) {
146
-		$this->logger = $logger;
147
-		$this->config = $config;
148
-		$this->secureRandom = $secureRandom;
149
-		$this->hasher = $hasher;
150
-		$this->mountManager = $mountManager;
151
-		$this->groupManager = $groupManager;
152
-		$this->l = $l;
153
-		$this->l10nFactory = $l10nFactory;
154
-		$this->factory = $factory;
155
-		$this->userManager = $userManager;
156
-		$this->rootFolder = $rootFolder;
157
-		$this->eventDispatcher = $eventDispatcher;
158
-		$this->sharingDisabledForUsersCache = new CappedMemoryCache();
159
-		$this->legacyHooks = new LegacyHooks($this->eventDispatcher);
160
-		$this->mailer = $mailer;
161
-		$this->urlGenerator = $urlGenerator;
162
-		$this->defaults = $defaults;
163
-	}
164
-
165
-	/**
166
-	 * Convert from a full share id to a tuple (providerId, shareId)
167
-	 *
168
-	 * @param string $id
169
-	 * @return string[]
170
-	 */
171
-	private function splitFullId($id) {
172
-		return explode(':', $id, 2);
173
-	}
174
-
175
-	/**
176
-	 * Verify if a password meets all requirements
177
-	 *
178
-	 * @param string $password
179
-	 * @throws \Exception
180
-	 */
181
-	protected function verifyPassword($password) {
182
-		if ($password === null) {
183
-			// No password is set, check if this is allowed.
184
-			if ($this->shareApiLinkEnforcePassword()) {
185
-				throw new \InvalidArgumentException('Passwords are enforced for link shares');
186
-			}
187
-
188
-			return;
189
-		}
190
-
191
-		// Let others verify the password
192
-		try {
193
-			$event = new GenericEvent($password);
194
-			$this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event);
195
-		} catch (HintException $e) {
196
-			throw new \Exception($e->getHint());
197
-		}
198
-	}
199
-
200
-	/**
201
-	 * Check for generic requirements before creating a share
202
-	 *
203
-	 * @param \OCP\Share\IShare $share
204
-	 * @throws \InvalidArgumentException
205
-	 * @throws GenericShareException
206
-	 *
207
-	 * @suppress PhanUndeclaredClassMethod
208
-	 */
209
-	protected function generalCreateChecks(\OCP\Share\IShare $share) {
210
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
211
-			// We expect a valid user as sharedWith for user shares
212
-			if (!$this->userManager->userExists($share->getSharedWith())) {
213
-				throw new \InvalidArgumentException('SharedWith is not a valid user');
214
-			}
215
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
216
-			// We expect a valid group as sharedWith for group shares
217
-			if (!$this->groupManager->groupExists($share->getSharedWith())) {
218
-				throw new \InvalidArgumentException('SharedWith is not a valid group');
219
-			}
220
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
221
-			if ($share->getSharedWith() !== null) {
222
-				throw new \InvalidArgumentException('SharedWith should be empty');
223
-			}
224
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE) {
225
-			if ($share->getSharedWith() === null) {
226
-				throw new \InvalidArgumentException('SharedWith should not be empty');
227
-			}
228
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
229
-			if ($share->getSharedWith() === null) {
230
-				throw new \InvalidArgumentException('SharedWith should not be empty');
231
-			}
232
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_CIRCLE) {
233
-			$circle = \OCA\Circles\Api\v1\Circles::detailsCircle($share->getSharedWith());
234
-			if ($circle === null) {
235
-				throw new \InvalidArgumentException('SharedWith is not a valid circle');
236
-			}
237
-		} else {
238
-			// We can't handle other types yet
239
-			throw new \InvalidArgumentException('unknown share type');
240
-		}
241
-
242
-		// Verify the initiator of the share is set
243
-		if ($share->getSharedBy() === null) {
244
-			throw new \InvalidArgumentException('SharedBy should be set');
245
-		}
246
-
247
-		// Cannot share with yourself
248
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
249
-			$share->getSharedWith() === $share->getSharedBy()) {
250
-			throw new \InvalidArgumentException('Can’t share with yourself');
251
-		}
252
-
253
-		// The path should be set
254
-		if ($share->getNode() === null) {
255
-			throw new \InvalidArgumentException('Path should be set');
256
-		}
257
-
258
-		// And it should be a file or a folder
259
-		if (!($share->getNode() instanceof \OCP\Files\File) &&
260
-				!($share->getNode() instanceof \OCP\Files\Folder)) {
261
-			throw new \InvalidArgumentException('Path should be either a file or a folder');
262
-		}
263
-
264
-		// And you can't share your rootfolder
265
-		if ($this->userManager->userExists($share->getSharedBy())) {
266
-			$sharedPath = $this->rootFolder->getUserFolder($share->getSharedBy())->getPath();
267
-		} else {
268
-			$sharedPath = $this->rootFolder->getUserFolder($share->getShareOwner())->getPath();
269
-		}
270
-		if ($sharedPath === $share->getNode()->getPath()) {
271
-			throw new \InvalidArgumentException('You can’t share your root folder');
272
-		}
273
-
274
-		// Check if we actually have share permissions
275
-		if (!$share->getNode()->isShareable()) {
276
-			$message_t = $this->l->t('You are not allowed to share %s', [$share->getNode()->getPath()]);
277
-			throw new GenericShareException($message_t, $message_t, 404);
278
-		}
279
-
280
-		// Permissions should be set
281
-		if ($share->getPermissions() === null) {
282
-			throw new \InvalidArgumentException('A share requires permissions');
283
-		}
284
-
285
-		/*
74
+    /** @var IProviderFactory */
75
+    private $factory;
76
+    /** @var ILogger */
77
+    private $logger;
78
+    /** @var IConfig */
79
+    private $config;
80
+    /** @var ISecureRandom */
81
+    private $secureRandom;
82
+    /** @var IHasher */
83
+    private $hasher;
84
+    /** @var IMountManager */
85
+    private $mountManager;
86
+    /** @var IGroupManager */
87
+    private $groupManager;
88
+    /** @var IL10N */
89
+    private $l;
90
+    /** @var IFactory */
91
+    private $l10nFactory;
92
+    /** @var IUserManager */
93
+    private $userManager;
94
+    /** @var IRootFolder */
95
+    private $rootFolder;
96
+    /** @var CappedMemoryCache */
97
+    private $sharingDisabledForUsersCache;
98
+    /** @var EventDispatcher */
99
+    private $eventDispatcher;
100
+    /** @var LegacyHooks */
101
+    private $legacyHooks;
102
+    /** @var IMailer */
103
+    private $mailer;
104
+    /** @var IURLGenerator */
105
+    private $urlGenerator;
106
+    /** @var \OC_Defaults */
107
+    private $defaults;
108
+
109
+
110
+    /**
111
+     * Manager constructor.
112
+     *
113
+     * @param ILogger $logger
114
+     * @param IConfig $config
115
+     * @param ISecureRandom $secureRandom
116
+     * @param IHasher $hasher
117
+     * @param IMountManager $mountManager
118
+     * @param IGroupManager $groupManager
119
+     * @param IL10N $l
120
+     * @param IFactory $l10nFactory
121
+     * @param IProviderFactory $factory
122
+     * @param IUserManager $userManager
123
+     * @param IRootFolder $rootFolder
124
+     * @param EventDispatcher $eventDispatcher
125
+     * @param IMailer $mailer
126
+     * @param IURLGenerator $urlGenerator
127
+     * @param \OC_Defaults $defaults
128
+     */
129
+    public function __construct(
130
+            ILogger $logger,
131
+            IConfig $config,
132
+            ISecureRandom $secureRandom,
133
+            IHasher $hasher,
134
+            IMountManager $mountManager,
135
+            IGroupManager $groupManager,
136
+            IL10N $l,
137
+            IFactory $l10nFactory,
138
+            IProviderFactory $factory,
139
+            IUserManager $userManager,
140
+            IRootFolder $rootFolder,
141
+            EventDispatcher $eventDispatcher,
142
+            IMailer $mailer,
143
+            IURLGenerator $urlGenerator,
144
+            \OC_Defaults $defaults
145
+    ) {
146
+        $this->logger = $logger;
147
+        $this->config = $config;
148
+        $this->secureRandom = $secureRandom;
149
+        $this->hasher = $hasher;
150
+        $this->mountManager = $mountManager;
151
+        $this->groupManager = $groupManager;
152
+        $this->l = $l;
153
+        $this->l10nFactory = $l10nFactory;
154
+        $this->factory = $factory;
155
+        $this->userManager = $userManager;
156
+        $this->rootFolder = $rootFolder;
157
+        $this->eventDispatcher = $eventDispatcher;
158
+        $this->sharingDisabledForUsersCache = new CappedMemoryCache();
159
+        $this->legacyHooks = new LegacyHooks($this->eventDispatcher);
160
+        $this->mailer = $mailer;
161
+        $this->urlGenerator = $urlGenerator;
162
+        $this->defaults = $defaults;
163
+    }
164
+
165
+    /**
166
+     * Convert from a full share id to a tuple (providerId, shareId)
167
+     *
168
+     * @param string $id
169
+     * @return string[]
170
+     */
171
+    private function splitFullId($id) {
172
+        return explode(':', $id, 2);
173
+    }
174
+
175
+    /**
176
+     * Verify if a password meets all requirements
177
+     *
178
+     * @param string $password
179
+     * @throws \Exception
180
+     */
181
+    protected function verifyPassword($password) {
182
+        if ($password === null) {
183
+            // No password is set, check if this is allowed.
184
+            if ($this->shareApiLinkEnforcePassword()) {
185
+                throw new \InvalidArgumentException('Passwords are enforced for link shares');
186
+            }
187
+
188
+            return;
189
+        }
190
+
191
+        // Let others verify the password
192
+        try {
193
+            $event = new GenericEvent($password);
194
+            $this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event);
195
+        } catch (HintException $e) {
196
+            throw new \Exception($e->getHint());
197
+        }
198
+    }
199
+
200
+    /**
201
+     * Check for generic requirements before creating a share
202
+     *
203
+     * @param \OCP\Share\IShare $share
204
+     * @throws \InvalidArgumentException
205
+     * @throws GenericShareException
206
+     *
207
+     * @suppress PhanUndeclaredClassMethod
208
+     */
209
+    protected function generalCreateChecks(\OCP\Share\IShare $share) {
210
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
211
+            // We expect a valid user as sharedWith for user shares
212
+            if (!$this->userManager->userExists($share->getSharedWith())) {
213
+                throw new \InvalidArgumentException('SharedWith is not a valid user');
214
+            }
215
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
216
+            // We expect a valid group as sharedWith for group shares
217
+            if (!$this->groupManager->groupExists($share->getSharedWith())) {
218
+                throw new \InvalidArgumentException('SharedWith is not a valid group');
219
+            }
220
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
221
+            if ($share->getSharedWith() !== null) {
222
+                throw new \InvalidArgumentException('SharedWith should be empty');
223
+            }
224
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE) {
225
+            if ($share->getSharedWith() === null) {
226
+                throw new \InvalidArgumentException('SharedWith should not be empty');
227
+            }
228
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
229
+            if ($share->getSharedWith() === null) {
230
+                throw new \InvalidArgumentException('SharedWith should not be empty');
231
+            }
232
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_CIRCLE) {
233
+            $circle = \OCA\Circles\Api\v1\Circles::detailsCircle($share->getSharedWith());
234
+            if ($circle === null) {
235
+                throw new \InvalidArgumentException('SharedWith is not a valid circle');
236
+            }
237
+        } else {
238
+            // We can't handle other types yet
239
+            throw new \InvalidArgumentException('unknown share type');
240
+        }
241
+
242
+        // Verify the initiator of the share is set
243
+        if ($share->getSharedBy() === null) {
244
+            throw new \InvalidArgumentException('SharedBy should be set');
245
+        }
246
+
247
+        // Cannot share with yourself
248
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
249
+            $share->getSharedWith() === $share->getSharedBy()) {
250
+            throw new \InvalidArgumentException('Can’t share with yourself');
251
+        }
252
+
253
+        // The path should be set
254
+        if ($share->getNode() === null) {
255
+            throw new \InvalidArgumentException('Path should be set');
256
+        }
257
+
258
+        // And it should be a file or a folder
259
+        if (!($share->getNode() instanceof \OCP\Files\File) &&
260
+                !($share->getNode() instanceof \OCP\Files\Folder)) {
261
+            throw new \InvalidArgumentException('Path should be either a file or a folder');
262
+        }
263
+
264
+        // And you can't share your rootfolder
265
+        if ($this->userManager->userExists($share->getSharedBy())) {
266
+            $sharedPath = $this->rootFolder->getUserFolder($share->getSharedBy())->getPath();
267
+        } else {
268
+            $sharedPath = $this->rootFolder->getUserFolder($share->getShareOwner())->getPath();
269
+        }
270
+        if ($sharedPath === $share->getNode()->getPath()) {
271
+            throw new \InvalidArgumentException('You can’t share your root folder');
272
+        }
273
+
274
+        // Check if we actually have share permissions
275
+        if (!$share->getNode()->isShareable()) {
276
+            $message_t = $this->l->t('You are not allowed to share %s', [$share->getNode()->getPath()]);
277
+            throw new GenericShareException($message_t, $message_t, 404);
278
+        }
279
+
280
+        // Permissions should be set
281
+        if ($share->getPermissions() === null) {
282
+            throw new \InvalidArgumentException('A share requires permissions');
283
+        }
284
+
285
+        /*
286 286
 		 * Quick fix for #23536
287 287
 		 * Non moveable mount points do not have update and delete permissions
288 288
 		 * while we 'most likely' do have that on the storage.
289 289
 		 */
290
-		$permissions = $share->getNode()->getPermissions();
291
-		$mount = $share->getNode()->getMountPoint();
292
-		if (!($mount instanceof MoveableMount)) {
293
-			$permissions |= \OCP\Constants::PERMISSION_DELETE | \OCP\Constants::PERMISSION_UPDATE;
294
-		}
295
-
296
-		// Check that we do not share with more permissions than we have
297
-		if ($share->getPermissions() & ~$permissions) {
298
-			$message_t = $this->l->t('Can’t increase permissions of %s', [$share->getNode()->getPath()]);
299
-			throw new GenericShareException($message_t, $message_t, 404);
300
-		}
301
-
302
-
303
-		// Check that read permissions are always set
304
-		// Link shares are allowed to have no read permissions to allow upload to hidden folders
305
-		$noReadPermissionRequired = $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK
306
-			|| $share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL;
307
-		if (!$noReadPermissionRequired &&
308
-			($share->getPermissions() & \OCP\Constants::PERMISSION_READ) === 0) {
309
-			throw new \InvalidArgumentException('Shares need at least read permissions');
310
-		}
311
-
312
-		if ($share->getNode() instanceof \OCP\Files\File) {
313
-			if ($share->getPermissions() & \OCP\Constants::PERMISSION_DELETE) {
314
-				$message_t = $this->l->t('Files can’t be shared with delete permissions');
315
-				throw new GenericShareException($message_t);
316
-			}
317
-			if ($share->getPermissions() & \OCP\Constants::PERMISSION_CREATE) {
318
-				$message_t = $this->l->t('Files can’t be shared with create permissions');
319
-				throw new GenericShareException($message_t);
320
-			}
321
-		}
322
-	}
323
-
324
-	/**
325
-	 * Validate if the expiration date fits the system settings
326
-	 *
327
-	 * @param \OCP\Share\IShare $share The share to validate the expiration date of
328
-	 * @return \OCP\Share\IShare The modified share object
329
-	 * @throws GenericShareException
330
-	 * @throws \InvalidArgumentException
331
-	 * @throws \Exception
332
-	 */
333
-	protected function validateExpirationDate(\OCP\Share\IShare $share) {
334
-
335
-		$expirationDate = $share->getExpirationDate();
336
-
337
-		if ($expirationDate !== null) {
338
-			//Make sure the expiration date is a date
339
-			$expirationDate->setTime(0, 0, 0);
340
-
341
-			$date = new \DateTime();
342
-			$date->setTime(0, 0, 0);
343
-			if ($date >= $expirationDate) {
344
-				$message = $this->l->t('Expiration date is in the past');
345
-				throw new GenericShareException($message, $message, 404);
346
-			}
347
-		}
348
-
349
-		// If expiredate is empty set a default one if there is a default
350
-		$fullId = null;
351
-		try {
352
-			$fullId = $share->getFullId();
353
-		} catch (\UnexpectedValueException $e) {
354
-			// This is a new share
355
-		}
356
-
357
-		if ($fullId === null && $expirationDate === null && $this->shareApiLinkDefaultExpireDate()) {
358
-			$expirationDate = new \DateTime();
359
-			$expirationDate->setTime(0,0,0);
360
-			$expirationDate->add(new \DateInterval('P'.$this->shareApiLinkDefaultExpireDays().'D'));
361
-		}
362
-
363
-		// If we enforce the expiration date check that is does not exceed
364
-		if ($this->shareApiLinkDefaultExpireDateEnforced()) {
365
-			if ($expirationDate === null) {
366
-				throw new \InvalidArgumentException('Expiration date is enforced');
367
-			}
368
-
369
-			$date = new \DateTime();
370
-			$date->setTime(0, 0, 0);
371
-			$date->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D'));
372
-			if ($date < $expirationDate) {
373
-				$message = $this->l->t('Can’t set expiration date more than %s days in the future', [$this->shareApiLinkDefaultExpireDays()]);
374
-				throw new GenericShareException($message, $message, 404);
375
-			}
376
-		}
377
-
378
-		$accepted = true;
379
-		$message = '';
380
-		\OCP\Util::emitHook('\OC\Share', 'verifyExpirationDate', [
381
-			'expirationDate' => &$expirationDate,
382
-			'accepted' => &$accepted,
383
-			'message' => &$message,
384
-			'passwordSet' => $share->getPassword() !== null,
385
-		]);
386
-
387
-		if (!$accepted) {
388
-			throw new \Exception($message);
389
-		}
390
-
391
-		$share->setExpirationDate($expirationDate);
392
-
393
-		return $share;
394
-	}
395
-
396
-	/**
397
-	 * Check for pre share requirements for user shares
398
-	 *
399
-	 * @param \OCP\Share\IShare $share
400
-	 * @throws \Exception
401
-	 */
402
-	protected function userCreateChecks(\OCP\Share\IShare $share) {
403
-		// Check if we can share with group members only
404
-		if ($this->shareWithGroupMembersOnly()) {
405
-			$sharedBy = $this->userManager->get($share->getSharedBy());
406
-			$sharedWith = $this->userManager->get($share->getSharedWith());
407
-			// Verify we can share with this user
408
-			$groups = array_intersect(
409
-					$this->groupManager->getUserGroupIds($sharedBy),
410
-					$this->groupManager->getUserGroupIds($sharedWith)
411
-			);
412
-			if (empty($groups)) {
413
-				throw new \Exception('Sharing is only allowed with group members');
414
-			}
415
-		}
416
-
417
-		/*
290
+        $permissions = $share->getNode()->getPermissions();
291
+        $mount = $share->getNode()->getMountPoint();
292
+        if (!($mount instanceof MoveableMount)) {
293
+            $permissions |= \OCP\Constants::PERMISSION_DELETE | \OCP\Constants::PERMISSION_UPDATE;
294
+        }
295
+
296
+        // Check that we do not share with more permissions than we have
297
+        if ($share->getPermissions() & ~$permissions) {
298
+            $message_t = $this->l->t('Can’t increase permissions of %s', [$share->getNode()->getPath()]);
299
+            throw new GenericShareException($message_t, $message_t, 404);
300
+        }
301
+
302
+
303
+        // Check that read permissions are always set
304
+        // Link shares are allowed to have no read permissions to allow upload to hidden folders
305
+        $noReadPermissionRequired = $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK
306
+            || $share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL;
307
+        if (!$noReadPermissionRequired &&
308
+            ($share->getPermissions() & \OCP\Constants::PERMISSION_READ) === 0) {
309
+            throw new \InvalidArgumentException('Shares need at least read permissions');
310
+        }
311
+
312
+        if ($share->getNode() instanceof \OCP\Files\File) {
313
+            if ($share->getPermissions() & \OCP\Constants::PERMISSION_DELETE) {
314
+                $message_t = $this->l->t('Files can’t be shared with delete permissions');
315
+                throw new GenericShareException($message_t);
316
+            }
317
+            if ($share->getPermissions() & \OCP\Constants::PERMISSION_CREATE) {
318
+                $message_t = $this->l->t('Files can’t be shared with create permissions');
319
+                throw new GenericShareException($message_t);
320
+            }
321
+        }
322
+    }
323
+
324
+    /**
325
+     * Validate if the expiration date fits the system settings
326
+     *
327
+     * @param \OCP\Share\IShare $share The share to validate the expiration date of
328
+     * @return \OCP\Share\IShare The modified share object
329
+     * @throws GenericShareException
330
+     * @throws \InvalidArgumentException
331
+     * @throws \Exception
332
+     */
333
+    protected function validateExpirationDate(\OCP\Share\IShare $share) {
334
+
335
+        $expirationDate = $share->getExpirationDate();
336
+
337
+        if ($expirationDate !== null) {
338
+            //Make sure the expiration date is a date
339
+            $expirationDate->setTime(0, 0, 0);
340
+
341
+            $date = new \DateTime();
342
+            $date->setTime(0, 0, 0);
343
+            if ($date >= $expirationDate) {
344
+                $message = $this->l->t('Expiration date is in the past');
345
+                throw new GenericShareException($message, $message, 404);
346
+            }
347
+        }
348
+
349
+        // If expiredate is empty set a default one if there is a default
350
+        $fullId = null;
351
+        try {
352
+            $fullId = $share->getFullId();
353
+        } catch (\UnexpectedValueException $e) {
354
+            // This is a new share
355
+        }
356
+
357
+        if ($fullId === null && $expirationDate === null && $this->shareApiLinkDefaultExpireDate()) {
358
+            $expirationDate = new \DateTime();
359
+            $expirationDate->setTime(0,0,0);
360
+            $expirationDate->add(new \DateInterval('P'.$this->shareApiLinkDefaultExpireDays().'D'));
361
+        }
362
+
363
+        // If we enforce the expiration date check that is does not exceed
364
+        if ($this->shareApiLinkDefaultExpireDateEnforced()) {
365
+            if ($expirationDate === null) {
366
+                throw new \InvalidArgumentException('Expiration date is enforced');
367
+            }
368
+
369
+            $date = new \DateTime();
370
+            $date->setTime(0, 0, 0);
371
+            $date->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D'));
372
+            if ($date < $expirationDate) {
373
+                $message = $this->l->t('Can’t set expiration date more than %s days in the future', [$this->shareApiLinkDefaultExpireDays()]);
374
+                throw new GenericShareException($message, $message, 404);
375
+            }
376
+        }
377
+
378
+        $accepted = true;
379
+        $message = '';
380
+        \OCP\Util::emitHook('\OC\Share', 'verifyExpirationDate', [
381
+            'expirationDate' => &$expirationDate,
382
+            'accepted' => &$accepted,
383
+            'message' => &$message,
384
+            'passwordSet' => $share->getPassword() !== null,
385
+        ]);
386
+
387
+        if (!$accepted) {
388
+            throw new \Exception($message);
389
+        }
390
+
391
+        $share->setExpirationDate($expirationDate);
392
+
393
+        return $share;
394
+    }
395
+
396
+    /**
397
+     * Check for pre share requirements for user shares
398
+     *
399
+     * @param \OCP\Share\IShare $share
400
+     * @throws \Exception
401
+     */
402
+    protected function userCreateChecks(\OCP\Share\IShare $share) {
403
+        // Check if we can share with group members only
404
+        if ($this->shareWithGroupMembersOnly()) {
405
+            $sharedBy = $this->userManager->get($share->getSharedBy());
406
+            $sharedWith = $this->userManager->get($share->getSharedWith());
407
+            // Verify we can share with this user
408
+            $groups = array_intersect(
409
+                    $this->groupManager->getUserGroupIds($sharedBy),
410
+                    $this->groupManager->getUserGroupIds($sharedWith)
411
+            );
412
+            if (empty($groups)) {
413
+                throw new \Exception('Sharing is only allowed with group members');
414
+            }
415
+        }
416
+
417
+        /*
418 418
 		 * TODO: Could be costly, fix
419 419
 		 *
420 420
 		 * Also this is not what we want in the future.. then we want to squash identical shares.
421 421
 		 */
422
-		$provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_USER);
423
-		$existingShares = $provider->getSharesByPath($share->getNode());
424
-		foreach($existingShares as $existingShare) {
425
-			// Ignore if it is the same share
426
-			try {
427
-				if ($existingShare->getFullId() === $share->getFullId()) {
428
-					continue;
429
-				}
430
-			} catch (\UnexpectedValueException $e) {
431
-				//Shares are not identical
432
-			}
433
-
434
-			// Identical share already existst
435
-			if ($existingShare->getSharedWith() === $share->getSharedWith()) {
436
-				throw new \Exception('Path is already shared with this user');
437
-			}
438
-
439
-			// The share is already shared with this user via a group share
440
-			if ($existingShare->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
441
-				$group = $this->groupManager->get($existingShare->getSharedWith());
442
-				if (!is_null($group)) {
443
-					$user = $this->userManager->get($share->getSharedWith());
444
-
445
-					if ($group->inGroup($user) && $existingShare->getShareOwner() !== $share->getShareOwner()) {
446
-						throw new \Exception('Path is already shared with this user');
447
-					}
448
-				}
449
-			}
450
-		}
451
-	}
452
-
453
-	/**
454
-	 * Check for pre share requirements for group shares
455
-	 *
456
-	 * @param \OCP\Share\IShare $share
457
-	 * @throws \Exception
458
-	 */
459
-	protected function groupCreateChecks(\OCP\Share\IShare $share) {
460
-		// Verify group shares are allowed
461
-		if (!$this->allowGroupSharing()) {
462
-			throw new \Exception('Group sharing is now allowed');
463
-		}
464
-
465
-		// Verify if the user can share with this group
466
-		if ($this->shareWithGroupMembersOnly()) {
467
-			$sharedBy = $this->userManager->get($share->getSharedBy());
468
-			$sharedWith = $this->groupManager->get($share->getSharedWith());
469
-			if (is_null($sharedWith) || !$sharedWith->inGroup($sharedBy)) {
470
-				throw new \Exception('Sharing is only allowed within your own groups');
471
-			}
472
-		}
473
-
474
-		/*
422
+        $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_USER);
423
+        $existingShares = $provider->getSharesByPath($share->getNode());
424
+        foreach($existingShares as $existingShare) {
425
+            // Ignore if it is the same share
426
+            try {
427
+                if ($existingShare->getFullId() === $share->getFullId()) {
428
+                    continue;
429
+                }
430
+            } catch (\UnexpectedValueException $e) {
431
+                //Shares are not identical
432
+            }
433
+
434
+            // Identical share already existst
435
+            if ($existingShare->getSharedWith() === $share->getSharedWith()) {
436
+                throw new \Exception('Path is already shared with this user');
437
+            }
438
+
439
+            // The share is already shared with this user via a group share
440
+            if ($existingShare->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
441
+                $group = $this->groupManager->get($existingShare->getSharedWith());
442
+                if (!is_null($group)) {
443
+                    $user = $this->userManager->get($share->getSharedWith());
444
+
445
+                    if ($group->inGroup($user) && $existingShare->getShareOwner() !== $share->getShareOwner()) {
446
+                        throw new \Exception('Path is already shared with this user');
447
+                    }
448
+                }
449
+            }
450
+        }
451
+    }
452
+
453
+    /**
454
+     * Check for pre share requirements for group shares
455
+     *
456
+     * @param \OCP\Share\IShare $share
457
+     * @throws \Exception
458
+     */
459
+    protected function groupCreateChecks(\OCP\Share\IShare $share) {
460
+        // Verify group shares are allowed
461
+        if (!$this->allowGroupSharing()) {
462
+            throw new \Exception('Group sharing is now allowed');
463
+        }
464
+
465
+        // Verify if the user can share with this group
466
+        if ($this->shareWithGroupMembersOnly()) {
467
+            $sharedBy = $this->userManager->get($share->getSharedBy());
468
+            $sharedWith = $this->groupManager->get($share->getSharedWith());
469
+            if (is_null($sharedWith) || !$sharedWith->inGroup($sharedBy)) {
470
+                throw new \Exception('Sharing is only allowed within your own groups');
471
+            }
472
+        }
473
+
474
+        /*
475 475
 		 * TODO: Could be costly, fix
476 476
 		 *
477 477
 		 * Also this is not what we want in the future.. then we want to squash identical shares.
478 478
 		 */
479
-		$provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP);
480
-		$existingShares = $provider->getSharesByPath($share->getNode());
481
-		foreach($existingShares as $existingShare) {
482
-			try {
483
-				if ($existingShare->getFullId() === $share->getFullId()) {
484
-					continue;
485
-				}
486
-			} catch (\UnexpectedValueException $e) {
487
-				//It is a new share so just continue
488
-			}
489
-
490
-			if ($existingShare->getSharedWith() === $share->getSharedWith()) {
491
-				throw new \Exception('Path is already shared with this group');
492
-			}
493
-		}
494
-	}
495
-
496
-	/**
497
-	 * Check for pre share requirements for link shares
498
-	 *
499
-	 * @param \OCP\Share\IShare $share
500
-	 * @throws \Exception
501
-	 */
502
-	protected function linkCreateChecks(\OCP\Share\IShare $share) {
503
-		// Are link shares allowed?
504
-		if (!$this->shareApiAllowLinks()) {
505
-			throw new \Exception('Link sharing is not allowed');
506
-		}
507
-
508
-		// Link shares by definition can't have share permissions
509
-		if ($share->getPermissions() & \OCP\Constants::PERMISSION_SHARE) {
510
-			throw new \InvalidArgumentException('Link shares can’t have reshare permissions');
511
-		}
512
-
513
-		// Check if public upload is allowed
514
-		if (!$this->shareApiLinkAllowPublicUpload() &&
515
-			($share->getPermissions() & (\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE))) {
516
-			throw new \InvalidArgumentException('Public upload is not allowed');
517
-		}
518
-	}
519
-
520
-	/**
521
-	 * To make sure we don't get invisible link shares we set the parent
522
-	 * of a link if it is a reshare. This is a quick word around
523
-	 * until we can properly display multiple link shares in the UI
524
-	 *
525
-	 * See: https://github.com/owncloud/core/issues/22295
526
-	 *
527
-	 * FIXME: Remove once multiple link shares can be properly displayed
528
-	 *
529
-	 * @param \OCP\Share\IShare $share
530
-	 */
531
-	protected function setLinkParent(\OCP\Share\IShare $share) {
532
-
533
-		// No sense in checking if the method is not there.
534
-		if (method_exists($share, 'setParent')) {
535
-			$storage = $share->getNode()->getStorage();
536
-			if ($storage->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) {
537
-				/** @var \OCA\Files_Sharing\SharedStorage $storage */
538
-				$share->setParent($storage->getShareId());
539
-			}
540
-		};
541
-	}
542
-
543
-	/**
544
-	 * @param File|Folder $path
545
-	 */
546
-	protected function pathCreateChecks($path) {
547
-		// Make sure that we do not share a path that contains a shared mountpoint
548
-		if ($path instanceof \OCP\Files\Folder) {
549
-			$mounts = $this->mountManager->findIn($path->getPath());
550
-			foreach($mounts as $mount) {
551
-				if ($mount->getStorage()->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) {
552
-					throw new \InvalidArgumentException('Path contains files shared with you');
553
-				}
554
-			}
555
-		}
556
-	}
557
-
558
-	/**
559
-	 * Check if the user that is sharing can actually share
560
-	 *
561
-	 * @param \OCP\Share\IShare $share
562
-	 * @throws \Exception
563
-	 */
564
-	protected function canShare(\OCP\Share\IShare $share) {
565
-		if (!$this->shareApiEnabled()) {
566
-			throw new \Exception('Sharing is disabled');
567
-		}
568
-
569
-		if ($this->sharingDisabledForUser($share->getSharedBy())) {
570
-			throw new \Exception('Sharing is disabled for you');
571
-		}
572
-	}
573
-
574
-	/**
575
-	 * Share a path
576
-	 *
577
-	 * @param \OCP\Share\IShare $share
578
-	 * @return Share The share object
579
-	 * @throws \Exception
580
-	 *
581
-	 * TODO: handle link share permissions or check them
582
-	 */
583
-	public function createShare(\OCP\Share\IShare $share) {
584
-		$this->canShare($share);
585
-
586
-		$this->generalCreateChecks($share);
587
-
588
-		// Verify if there are any issues with the path
589
-		$this->pathCreateChecks($share->getNode());
590
-
591
-		/*
479
+        $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP);
480
+        $existingShares = $provider->getSharesByPath($share->getNode());
481
+        foreach($existingShares as $existingShare) {
482
+            try {
483
+                if ($existingShare->getFullId() === $share->getFullId()) {
484
+                    continue;
485
+                }
486
+            } catch (\UnexpectedValueException $e) {
487
+                //It is a new share so just continue
488
+            }
489
+
490
+            if ($existingShare->getSharedWith() === $share->getSharedWith()) {
491
+                throw new \Exception('Path is already shared with this group');
492
+            }
493
+        }
494
+    }
495
+
496
+    /**
497
+     * Check for pre share requirements for link shares
498
+     *
499
+     * @param \OCP\Share\IShare $share
500
+     * @throws \Exception
501
+     */
502
+    protected function linkCreateChecks(\OCP\Share\IShare $share) {
503
+        // Are link shares allowed?
504
+        if (!$this->shareApiAllowLinks()) {
505
+            throw new \Exception('Link sharing is not allowed');
506
+        }
507
+
508
+        // Link shares by definition can't have share permissions
509
+        if ($share->getPermissions() & \OCP\Constants::PERMISSION_SHARE) {
510
+            throw new \InvalidArgumentException('Link shares can’t have reshare permissions');
511
+        }
512
+
513
+        // Check if public upload is allowed
514
+        if (!$this->shareApiLinkAllowPublicUpload() &&
515
+            ($share->getPermissions() & (\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE))) {
516
+            throw new \InvalidArgumentException('Public upload is not allowed');
517
+        }
518
+    }
519
+
520
+    /**
521
+     * To make sure we don't get invisible link shares we set the parent
522
+     * of a link if it is a reshare. This is a quick word around
523
+     * until we can properly display multiple link shares in the UI
524
+     *
525
+     * See: https://github.com/owncloud/core/issues/22295
526
+     *
527
+     * FIXME: Remove once multiple link shares can be properly displayed
528
+     *
529
+     * @param \OCP\Share\IShare $share
530
+     */
531
+    protected function setLinkParent(\OCP\Share\IShare $share) {
532
+
533
+        // No sense in checking if the method is not there.
534
+        if (method_exists($share, 'setParent')) {
535
+            $storage = $share->getNode()->getStorage();
536
+            if ($storage->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) {
537
+                /** @var \OCA\Files_Sharing\SharedStorage $storage */
538
+                $share->setParent($storage->getShareId());
539
+            }
540
+        };
541
+    }
542
+
543
+    /**
544
+     * @param File|Folder $path
545
+     */
546
+    protected function pathCreateChecks($path) {
547
+        // Make sure that we do not share a path that contains a shared mountpoint
548
+        if ($path instanceof \OCP\Files\Folder) {
549
+            $mounts = $this->mountManager->findIn($path->getPath());
550
+            foreach($mounts as $mount) {
551
+                if ($mount->getStorage()->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) {
552
+                    throw new \InvalidArgumentException('Path contains files shared with you');
553
+                }
554
+            }
555
+        }
556
+    }
557
+
558
+    /**
559
+     * Check if the user that is sharing can actually share
560
+     *
561
+     * @param \OCP\Share\IShare $share
562
+     * @throws \Exception
563
+     */
564
+    protected function canShare(\OCP\Share\IShare $share) {
565
+        if (!$this->shareApiEnabled()) {
566
+            throw new \Exception('Sharing is disabled');
567
+        }
568
+
569
+        if ($this->sharingDisabledForUser($share->getSharedBy())) {
570
+            throw new \Exception('Sharing is disabled for you');
571
+        }
572
+    }
573
+
574
+    /**
575
+     * Share a path
576
+     *
577
+     * @param \OCP\Share\IShare $share
578
+     * @return Share The share object
579
+     * @throws \Exception
580
+     *
581
+     * TODO: handle link share permissions or check them
582
+     */
583
+    public function createShare(\OCP\Share\IShare $share) {
584
+        $this->canShare($share);
585
+
586
+        $this->generalCreateChecks($share);
587
+
588
+        // Verify if there are any issues with the path
589
+        $this->pathCreateChecks($share->getNode());
590
+
591
+        /*
592 592
 		 * On creation of a share the owner is always the owner of the path
593 593
 		 * Except for mounted federated shares.
594 594
 		 */
595
-		$storage = $share->getNode()->getStorage();
596
-		if ($storage->instanceOfStorage('OCA\Files_Sharing\External\Storage')) {
597
-			$parent = $share->getNode()->getParent();
598
-			while($parent->getStorage()->instanceOfStorage('OCA\Files_Sharing\External\Storage')) {
599
-				$parent = $parent->getParent();
600
-			}
601
-			$share->setShareOwner($parent->getOwner()->getUID());
602
-		} else {
603
-			$share->setShareOwner($share->getNode()->getOwner()->getUID());
604
-		}
605
-
606
-		//Verify share type
607
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
608
-			$this->userCreateChecks($share);
609
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
610
-			$this->groupCreateChecks($share);
611
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
612
-			$this->linkCreateChecks($share);
613
-			$this->setLinkParent($share);
614
-
615
-			/*
595
+        $storage = $share->getNode()->getStorage();
596
+        if ($storage->instanceOfStorage('OCA\Files_Sharing\External\Storage')) {
597
+            $parent = $share->getNode()->getParent();
598
+            while($parent->getStorage()->instanceOfStorage('OCA\Files_Sharing\External\Storage')) {
599
+                $parent = $parent->getParent();
600
+            }
601
+            $share->setShareOwner($parent->getOwner()->getUID());
602
+        } else {
603
+            $share->setShareOwner($share->getNode()->getOwner()->getUID());
604
+        }
605
+
606
+        //Verify share type
607
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
608
+            $this->userCreateChecks($share);
609
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
610
+            $this->groupCreateChecks($share);
611
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
612
+            $this->linkCreateChecks($share);
613
+            $this->setLinkParent($share);
614
+
615
+            /*
616 616
 			 * For now ignore a set token.
617 617
 			 */
618
-			$share->setToken(
619
-				$this->secureRandom->generate(
620
-					\OC\Share\Constants::TOKEN_LENGTH,
621
-					\OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE
622
-				)
623
-			);
624
-
625
-			//Verify the expiration date
626
-			$this->validateExpirationDate($share);
627
-
628
-			//Verify the password
629
-			$this->verifyPassword($share->getPassword());
630
-
631
-			// If a password is set. Hash it!
632
-			if ($share->getPassword() !== null) {
633
-				$share->setPassword($this->hasher->hash($share->getPassword()));
634
-			}
635
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
636
-			$share->setToken(
637
-				$this->secureRandom->generate(
638
-					\OC\Share\Constants::TOKEN_LENGTH,
639
-					\OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE
640
-				)
641
-			);
642
-		}
643
-
644
-		// Cannot share with the owner
645
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
646
-			$share->getSharedWith() === $share->getShareOwner()) {
647
-			throw new \InvalidArgumentException('Can’t share with the share owner');
648
-		}
649
-
650
-		// Generate the target
651
-		$target = $this->config->getSystemValue('share_folder', '/') .'/'. $share->getNode()->getName();
652
-		$target = \OC\Files\Filesystem::normalizePath($target);
653
-		$share->setTarget($target);
654
-
655
-		// Pre share event
656
-		$event = new GenericEvent($share);
657
-		$a = $this->eventDispatcher->dispatch('OCP\Share::preShare', $event);
658
-		if ($event->isPropagationStopped() && $event->hasArgument('error')) {
659
-			throw new \Exception($event->getArgument('error'));
660
-		}
661
-
662
-		$oldShare = $share;
663
-		$provider = $this->factory->getProviderForType($share->getShareType());
664
-		$share = $provider->create($share);
665
-		//reuse the node we already have
666
-		$share->setNode($oldShare->getNode());
667
-
668
-		// Post share event
669
-		$event = new GenericEvent($share);
670
-		$this->eventDispatcher->dispatch('OCP\Share::postShare', $event);
671
-
672
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
673
-			$mailSend = $share->getMailSend();
674
-			if($mailSend === true) {
675
-				$user = $this->userManager->get($share->getSharedWith());
676
-				if ($user !== null) {
677
-					$emailAddress = $user->getEMailAddress();
678
-					if ($emailAddress !== null && $emailAddress !== '') {
679
-						$userLang = $this->config->getUserValue($share->getSharedWith(), 'core', 'lang', null);
680
-						$l = $this->l10nFactory->get('lib', $userLang);
681
-						$this->sendMailNotification(
682
-							$l,
683
-							$share->getNode()->getName(),
684
-							$this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $share->getNode()->getId()]),
685
-							$share->getSharedBy(),
686
-							$emailAddress,
687
-							$share->getExpirationDate()
688
-						);
689
-						$this->logger->debug('Send share notification to ' . $emailAddress . ' for share with ID ' . $share->getId(), ['app' => 'share']);
690
-					} else {
691
-						$this->logger->debug('Share notification not send to ' . $share->getSharedWith() . ' because email address is not set.', ['app' => 'share']);
692
-					}
693
-				} else {
694
-					$this->logger->debug('Share notification not send to ' . $share->getSharedWith() . ' because user could not be found.', ['app' => 'share']);
695
-				}
696
-			} else {
697
-				$this->logger->debug('Share notification not send because mailsend is false.', ['app' => 'share']);
698
-			}
699
-		}
700
-
701
-		return $share;
702
-	}
703
-
704
-	/**
705
-	 * @param IL10N $l Language of the recipient
706
-	 * @param string $filename file/folder name
707
-	 * @param string $link link to the file/folder
708
-	 * @param string $initiator user ID of share sender
709
-	 * @param string $shareWith email address of share receiver
710
-	 * @param \DateTime|null $expiration
711
-	 * @throws \Exception If mail couldn't be sent
712
-	 */
713
-	protected function sendMailNotification(IL10N $l,
714
-											$filename,
715
-											$link,
716
-											$initiator,
717
-											$shareWith,
718
-											\DateTime $expiration = null) {
719
-		$initiatorUser = $this->userManager->get($initiator);
720
-		$initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
721
-
722
-		$message = $this->mailer->createMessage();
723
-
724
-		$emailTemplate = $this->mailer->createEMailTemplate('files_sharing.RecipientNotification', [
725
-			'filename' => $filename,
726
-			'link' => $link,
727
-			'initiator' => $initiatorDisplayName,
728
-			'expiration' => $expiration,
729
-			'shareWith' => $shareWith,
730
-		]);
731
-
732
-		$emailTemplate->setSubject($l->t('%s shared »%s« with you', array($initiatorDisplayName, $filename)));
733
-		$emailTemplate->addHeader();
734
-		$emailTemplate->addHeading($l->t('%s shared »%s« with you', [$initiatorDisplayName, $filename]), false);
735
-		$text = $l->t('%s shared »%s« with you.', [$initiatorDisplayName, $filename]);
736
-
737
-		$emailTemplate->addBodyText(
738
-			$text . ' ' . $l->t('Click the button below to open it.'),
739
-			$text
740
-		);
741
-		$emailTemplate->addBodyButton(
742
-			$l->t('Open »%s«', [$filename]),
743
-			$link
744
-		);
745
-
746
-		$message->setTo([$shareWith]);
747
-
748
-		// The "From" contains the sharers name
749
-		$instanceName = $this->defaults->getName();
750
-		$senderName = $l->t(
751
-			'%s via %s',
752
-			[
753
-				$initiatorDisplayName,
754
-				$instanceName
755
-			]
756
-		);
757
-		$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
758
-
759
-		// The "Reply-To" is set to the sharer if an mail address is configured
760
-		// also the default footer contains a "Do not reply" which needs to be adjusted.
761
-		$initiatorEmail = $initiatorUser->getEMailAddress();
762
-		if($initiatorEmail !== null) {
763
-			$message->setReplyTo([$initiatorEmail => $initiatorDisplayName]);
764
-			$emailTemplate->addFooter($instanceName . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : ''));
765
-		} else {
766
-			$emailTemplate->addFooter();
767
-		}
768
-
769
-		$message->useTemplate($emailTemplate);
770
-		$this->mailer->send($message);
771
-	}
772
-
773
-	/**
774
-	 * Update a share
775
-	 *
776
-	 * @param \OCP\Share\IShare $share
777
-	 * @return \OCP\Share\IShare The share object
778
-	 * @throws \InvalidArgumentException
779
-	 */
780
-	public function updateShare(\OCP\Share\IShare $share) {
781
-		$expirationDateUpdated = false;
782
-
783
-		$this->canShare($share);
784
-
785
-		try {
786
-			$originalShare = $this->getShareById($share->getFullId());
787
-		} catch (\UnexpectedValueException $e) {
788
-			throw new \InvalidArgumentException('Share does not have a full id');
789
-		}
790
-
791
-		// We can't change the share type!
792
-		if ($share->getShareType() !== $originalShare->getShareType()) {
793
-			throw new \InvalidArgumentException('Can’t change share type');
794
-		}
795
-
796
-		// We can only change the recipient on user shares
797
-		if ($share->getSharedWith() !== $originalShare->getSharedWith() &&
798
-		    $share->getShareType() !== \OCP\Share::SHARE_TYPE_USER) {
799
-			throw new \InvalidArgumentException('Can only update recipient on user shares');
800
-		}
801
-
802
-		// Cannot share with the owner
803
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
804
-			$share->getSharedWith() === $share->getShareOwner()) {
805
-			throw new \InvalidArgumentException('Can’t share with the share owner');
806
-		}
807
-
808
-		$this->generalCreateChecks($share);
809
-
810
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
811
-			$this->userCreateChecks($share);
812
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
813
-			$this->groupCreateChecks($share);
814
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
815
-			$this->linkCreateChecks($share);
816
-
817
-			$this->updateSharePasswordIfNeeded($share, $originalShare);
818
-
819
-			if ($share->getExpirationDate() != $originalShare->getExpirationDate()) {
820
-				//Verify the expiration date
821
-				$this->validateExpirationDate($share);
822
-				$expirationDateUpdated = true;
823
-			}
824
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
825
-			$plainTextPassword = $share->getPassword();
826
-			if (!$this->updateSharePasswordIfNeeded($share, $originalShare)) {
827
-				$plainTextPassword = null;
828
-			}
829
-		}
830
-
831
-		$this->pathCreateChecks($share->getNode());
832
-
833
-		// Now update the share!
834
-		$provider = $this->factory->getProviderForType($share->getShareType());
835
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
836
-			$share = $provider->update($share, $plainTextPassword);
837
-		} else {
838
-			$share = $provider->update($share);
839
-		}
840
-
841
-		if ($expirationDateUpdated === true) {
842
-			\OC_Hook::emit(Share::class, 'post_set_expiration_date', [
843
-				'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder',
844
-				'itemSource' => $share->getNode()->getId(),
845
-				'date' => $share->getExpirationDate(),
846
-				'uidOwner' => $share->getSharedBy(),
847
-			]);
848
-		}
849
-
850
-		if ($share->getPassword() !== $originalShare->getPassword()) {
851
-			\OC_Hook::emit(Share::class, 'post_update_password', [
852
-				'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder',
853
-				'itemSource' => $share->getNode()->getId(),
854
-				'uidOwner' => $share->getSharedBy(),
855
-				'token' => $share->getToken(),
856
-				'disabled' => is_null($share->getPassword()),
857
-			]);
858
-		}
859
-
860
-		if ($share->getPermissions() !== $originalShare->getPermissions()) {
861
-			if ($this->userManager->userExists($share->getShareOwner())) {
862
-				$userFolder = $this->rootFolder->getUserFolder($share->getShareOwner());
863
-			} else {
864
-				$userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
865
-			}
866
-			\OC_Hook::emit(Share::class, 'post_update_permissions', array(
867
-				'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder',
868
-				'itemSource' => $share->getNode()->getId(),
869
-				'shareType' => $share->getShareType(),
870
-				'shareWith' => $share->getSharedWith(),
871
-				'uidOwner' => $share->getSharedBy(),
872
-				'permissions' => $share->getPermissions(),
873
-				'path' => $userFolder->getRelativePath($share->getNode()->getPath()),
874
-			));
875
-		}
876
-
877
-		return $share;
878
-	}
879
-
880
-	/**
881
-	 * Updates the password of the given share if it is not the same as the
882
-	 * password of the original share.
883
-	 *
884
-	 * @param \OCP\Share\IShare $share the share to update its password.
885
-	 * @param \OCP\Share\IShare $originalShare the original share to compare its
886
-	 *        password with.
887
-	 * @return boolean whether the password was updated or not.
888
-	 */
889
-	private function updateSharePasswordIfNeeded(\OCP\Share\IShare $share, \OCP\Share\IShare $originalShare) {
890
-		// Password updated.
891
-		if ($share->getPassword() !== $originalShare->getPassword()) {
892
-			//Verify the password
893
-			$this->verifyPassword($share->getPassword());
894
-
895
-			// If a password is set. Hash it!
896
-			if ($share->getPassword() !== null) {
897
-				$share->setPassword($this->hasher->hash($share->getPassword()));
898
-
899
-				return true;
900
-			}
901
-		}
902
-
903
-		return false;
904
-	}
905
-
906
-	/**
907
-	 * Delete all the children of this share
908
-	 * FIXME: remove once https://github.com/owncloud/core/pull/21660 is in
909
-	 *
910
-	 * @param \OCP\Share\IShare $share
911
-	 * @return \OCP\Share\IShare[] List of deleted shares
912
-	 */
913
-	protected function deleteChildren(\OCP\Share\IShare $share) {
914
-		$deletedShares = [];
915
-
916
-		$provider = $this->factory->getProviderForType($share->getShareType());
917
-
918
-		foreach ($provider->getChildren($share) as $child) {
919
-			$deletedChildren = $this->deleteChildren($child);
920
-			$deletedShares = array_merge($deletedShares, $deletedChildren);
921
-
922
-			$provider->delete($child);
923
-			$deletedShares[] = $child;
924
-		}
925
-
926
-		return $deletedShares;
927
-	}
928
-
929
-	/**
930
-	 * Delete a share
931
-	 *
932
-	 * @param \OCP\Share\IShare $share
933
-	 * @throws ShareNotFound
934
-	 * @throws \InvalidArgumentException
935
-	 */
936
-	public function deleteShare(\OCP\Share\IShare $share) {
937
-
938
-		try {
939
-			$share->getFullId();
940
-		} catch (\UnexpectedValueException $e) {
941
-			throw new \InvalidArgumentException('Share does not have a full id');
942
-		}
943
-
944
-		$event = new GenericEvent($share);
945
-		$this->eventDispatcher->dispatch('OCP\Share::preUnshare', $event);
946
-
947
-		// Get all children and delete them as well
948
-		$deletedShares = $this->deleteChildren($share);
949
-
950
-		// Do the actual delete
951
-		$provider = $this->factory->getProviderForType($share->getShareType());
952
-		$provider->delete($share);
953
-
954
-		// All the deleted shares caused by this delete
955
-		$deletedShares[] = $share;
956
-
957
-		// Emit post hook
958
-		$event->setArgument('deletedShares', $deletedShares);
959
-		$this->eventDispatcher->dispatch('OCP\Share::postUnshare', $event);
960
-	}
961
-
962
-
963
-	/**
964
-	 * Unshare a file as the recipient.
965
-	 * This can be different from a regular delete for example when one of
966
-	 * the users in a groups deletes that share. But the provider should
967
-	 * handle this.
968
-	 *
969
-	 * @param \OCP\Share\IShare $share
970
-	 * @param string $recipientId
971
-	 */
972
-	public function deleteFromSelf(\OCP\Share\IShare $share, $recipientId) {
973
-		list($providerId, ) = $this->splitFullId($share->getFullId());
974
-		$provider = $this->factory->getProvider($providerId);
975
-
976
-		$provider->deleteFromSelf($share, $recipientId);
977
-		$event = new GenericEvent($share);
978
-		$this->eventDispatcher->dispatch('OCP\Share::postUnshareFromSelf', $event);
979
-	}
980
-
981
-	/**
982
-	 * @inheritdoc
983
-	 */
984
-	public function moveShare(\OCP\Share\IShare $share, $recipientId) {
985
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
986
-			throw new \InvalidArgumentException('Can’t change target of link share');
987
-		}
988
-
989
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && $share->getSharedWith() !== $recipientId) {
990
-			throw new \InvalidArgumentException('Invalid recipient');
991
-		}
992
-
993
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
994
-			$sharedWith = $this->groupManager->get($share->getSharedWith());
995
-			if (is_null($sharedWith)) {
996
-				throw new \InvalidArgumentException('Group "' . $share->getSharedWith() . '" does not exist');
997
-			}
998
-			$recipient = $this->userManager->get($recipientId);
999
-			if (!$sharedWith->inGroup($recipient)) {
1000
-				throw new \InvalidArgumentException('Invalid recipient');
1001
-			}
1002
-		}
1003
-
1004
-		list($providerId, ) = $this->splitFullId($share->getFullId());
1005
-		$provider = $this->factory->getProvider($providerId);
1006
-
1007
-		$provider->move($share, $recipientId);
1008
-	}
1009
-
1010
-	public function getSharesInFolder($userId, Folder $node, $reshares = false) {
1011
-		$providers = $this->factory->getAllProviders();
1012
-
1013
-		return array_reduce($providers, function($shares, IShareProvider $provider) use ($userId, $node, $reshares) {
1014
-			$newShares = $provider->getSharesInFolder($userId, $node, $reshares);
1015
-			foreach ($newShares as $fid => $data) {
1016
-				if (!isset($shares[$fid])) {
1017
-					$shares[$fid] = [];
1018
-				}
1019
-
1020
-				$shares[$fid] = array_merge($shares[$fid], $data);
1021
-			}
1022
-			return $shares;
1023
-		}, []);
1024
-	}
1025
-
1026
-	/**
1027
-	 * @inheritdoc
1028
-	 */
1029
-	public function getSharesBy($userId, $shareType, $path = null, $reshares = false, $limit = 50, $offset = 0) {
1030
-		if ($path !== null &&
1031
-				!($path instanceof \OCP\Files\File) &&
1032
-				!($path instanceof \OCP\Files\Folder)) {
1033
-			throw new \InvalidArgumentException('invalid path');
1034
-		}
1035
-
1036
-		try {
1037
-			$provider = $this->factory->getProviderForType($shareType);
1038
-		} catch (ProviderException $e) {
1039
-			return [];
1040
-		}
1041
-
1042
-		$shares = $provider->getSharesBy($userId, $shareType, $path, $reshares, $limit, $offset);
1043
-
1044
-		/*
618
+            $share->setToken(
619
+                $this->secureRandom->generate(
620
+                    \OC\Share\Constants::TOKEN_LENGTH,
621
+                    \OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE
622
+                )
623
+            );
624
+
625
+            //Verify the expiration date
626
+            $this->validateExpirationDate($share);
627
+
628
+            //Verify the password
629
+            $this->verifyPassword($share->getPassword());
630
+
631
+            // If a password is set. Hash it!
632
+            if ($share->getPassword() !== null) {
633
+                $share->setPassword($this->hasher->hash($share->getPassword()));
634
+            }
635
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
636
+            $share->setToken(
637
+                $this->secureRandom->generate(
638
+                    \OC\Share\Constants::TOKEN_LENGTH,
639
+                    \OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE
640
+                )
641
+            );
642
+        }
643
+
644
+        // Cannot share with the owner
645
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
646
+            $share->getSharedWith() === $share->getShareOwner()) {
647
+            throw new \InvalidArgumentException('Can’t share with the share owner');
648
+        }
649
+
650
+        // Generate the target
651
+        $target = $this->config->getSystemValue('share_folder', '/') .'/'. $share->getNode()->getName();
652
+        $target = \OC\Files\Filesystem::normalizePath($target);
653
+        $share->setTarget($target);
654
+
655
+        // Pre share event
656
+        $event = new GenericEvent($share);
657
+        $a = $this->eventDispatcher->dispatch('OCP\Share::preShare', $event);
658
+        if ($event->isPropagationStopped() && $event->hasArgument('error')) {
659
+            throw new \Exception($event->getArgument('error'));
660
+        }
661
+
662
+        $oldShare = $share;
663
+        $provider = $this->factory->getProviderForType($share->getShareType());
664
+        $share = $provider->create($share);
665
+        //reuse the node we already have
666
+        $share->setNode($oldShare->getNode());
667
+
668
+        // Post share event
669
+        $event = new GenericEvent($share);
670
+        $this->eventDispatcher->dispatch('OCP\Share::postShare', $event);
671
+
672
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
673
+            $mailSend = $share->getMailSend();
674
+            if($mailSend === true) {
675
+                $user = $this->userManager->get($share->getSharedWith());
676
+                if ($user !== null) {
677
+                    $emailAddress = $user->getEMailAddress();
678
+                    if ($emailAddress !== null && $emailAddress !== '') {
679
+                        $userLang = $this->config->getUserValue($share->getSharedWith(), 'core', 'lang', null);
680
+                        $l = $this->l10nFactory->get('lib', $userLang);
681
+                        $this->sendMailNotification(
682
+                            $l,
683
+                            $share->getNode()->getName(),
684
+                            $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $share->getNode()->getId()]),
685
+                            $share->getSharedBy(),
686
+                            $emailAddress,
687
+                            $share->getExpirationDate()
688
+                        );
689
+                        $this->logger->debug('Send share notification to ' . $emailAddress . ' for share with ID ' . $share->getId(), ['app' => 'share']);
690
+                    } else {
691
+                        $this->logger->debug('Share notification not send to ' . $share->getSharedWith() . ' because email address is not set.', ['app' => 'share']);
692
+                    }
693
+                } else {
694
+                    $this->logger->debug('Share notification not send to ' . $share->getSharedWith() . ' because user could not be found.', ['app' => 'share']);
695
+                }
696
+            } else {
697
+                $this->logger->debug('Share notification not send because mailsend is false.', ['app' => 'share']);
698
+            }
699
+        }
700
+
701
+        return $share;
702
+    }
703
+
704
+    /**
705
+     * @param IL10N $l Language of the recipient
706
+     * @param string $filename file/folder name
707
+     * @param string $link link to the file/folder
708
+     * @param string $initiator user ID of share sender
709
+     * @param string $shareWith email address of share receiver
710
+     * @param \DateTime|null $expiration
711
+     * @throws \Exception If mail couldn't be sent
712
+     */
713
+    protected function sendMailNotification(IL10N $l,
714
+                                            $filename,
715
+                                            $link,
716
+                                            $initiator,
717
+                                            $shareWith,
718
+                                            \DateTime $expiration = null) {
719
+        $initiatorUser = $this->userManager->get($initiator);
720
+        $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
721
+
722
+        $message = $this->mailer->createMessage();
723
+
724
+        $emailTemplate = $this->mailer->createEMailTemplate('files_sharing.RecipientNotification', [
725
+            'filename' => $filename,
726
+            'link' => $link,
727
+            'initiator' => $initiatorDisplayName,
728
+            'expiration' => $expiration,
729
+            'shareWith' => $shareWith,
730
+        ]);
731
+
732
+        $emailTemplate->setSubject($l->t('%s shared »%s« with you', array($initiatorDisplayName, $filename)));
733
+        $emailTemplate->addHeader();
734
+        $emailTemplate->addHeading($l->t('%s shared »%s« with you', [$initiatorDisplayName, $filename]), false);
735
+        $text = $l->t('%s shared »%s« with you.', [$initiatorDisplayName, $filename]);
736
+
737
+        $emailTemplate->addBodyText(
738
+            $text . ' ' . $l->t('Click the button below to open it.'),
739
+            $text
740
+        );
741
+        $emailTemplate->addBodyButton(
742
+            $l->t('Open »%s«', [$filename]),
743
+            $link
744
+        );
745
+
746
+        $message->setTo([$shareWith]);
747
+
748
+        // The "From" contains the sharers name
749
+        $instanceName = $this->defaults->getName();
750
+        $senderName = $l->t(
751
+            '%s via %s',
752
+            [
753
+                $initiatorDisplayName,
754
+                $instanceName
755
+            ]
756
+        );
757
+        $message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
758
+
759
+        // The "Reply-To" is set to the sharer if an mail address is configured
760
+        // also the default footer contains a "Do not reply" which needs to be adjusted.
761
+        $initiatorEmail = $initiatorUser->getEMailAddress();
762
+        if($initiatorEmail !== null) {
763
+            $message->setReplyTo([$initiatorEmail => $initiatorDisplayName]);
764
+            $emailTemplate->addFooter($instanceName . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : ''));
765
+        } else {
766
+            $emailTemplate->addFooter();
767
+        }
768
+
769
+        $message->useTemplate($emailTemplate);
770
+        $this->mailer->send($message);
771
+    }
772
+
773
+    /**
774
+     * Update a share
775
+     *
776
+     * @param \OCP\Share\IShare $share
777
+     * @return \OCP\Share\IShare The share object
778
+     * @throws \InvalidArgumentException
779
+     */
780
+    public function updateShare(\OCP\Share\IShare $share) {
781
+        $expirationDateUpdated = false;
782
+
783
+        $this->canShare($share);
784
+
785
+        try {
786
+            $originalShare = $this->getShareById($share->getFullId());
787
+        } catch (\UnexpectedValueException $e) {
788
+            throw new \InvalidArgumentException('Share does not have a full id');
789
+        }
790
+
791
+        // We can't change the share type!
792
+        if ($share->getShareType() !== $originalShare->getShareType()) {
793
+            throw new \InvalidArgumentException('Can’t change share type');
794
+        }
795
+
796
+        // We can only change the recipient on user shares
797
+        if ($share->getSharedWith() !== $originalShare->getSharedWith() &&
798
+            $share->getShareType() !== \OCP\Share::SHARE_TYPE_USER) {
799
+            throw new \InvalidArgumentException('Can only update recipient on user shares');
800
+        }
801
+
802
+        // Cannot share with the owner
803
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
804
+            $share->getSharedWith() === $share->getShareOwner()) {
805
+            throw new \InvalidArgumentException('Can’t share with the share owner');
806
+        }
807
+
808
+        $this->generalCreateChecks($share);
809
+
810
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
811
+            $this->userCreateChecks($share);
812
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
813
+            $this->groupCreateChecks($share);
814
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
815
+            $this->linkCreateChecks($share);
816
+
817
+            $this->updateSharePasswordIfNeeded($share, $originalShare);
818
+
819
+            if ($share->getExpirationDate() != $originalShare->getExpirationDate()) {
820
+                //Verify the expiration date
821
+                $this->validateExpirationDate($share);
822
+                $expirationDateUpdated = true;
823
+            }
824
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
825
+            $plainTextPassword = $share->getPassword();
826
+            if (!$this->updateSharePasswordIfNeeded($share, $originalShare)) {
827
+                $plainTextPassword = null;
828
+            }
829
+        }
830
+
831
+        $this->pathCreateChecks($share->getNode());
832
+
833
+        // Now update the share!
834
+        $provider = $this->factory->getProviderForType($share->getShareType());
835
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
836
+            $share = $provider->update($share, $plainTextPassword);
837
+        } else {
838
+            $share = $provider->update($share);
839
+        }
840
+
841
+        if ($expirationDateUpdated === true) {
842
+            \OC_Hook::emit(Share::class, 'post_set_expiration_date', [
843
+                'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder',
844
+                'itemSource' => $share->getNode()->getId(),
845
+                'date' => $share->getExpirationDate(),
846
+                'uidOwner' => $share->getSharedBy(),
847
+            ]);
848
+        }
849
+
850
+        if ($share->getPassword() !== $originalShare->getPassword()) {
851
+            \OC_Hook::emit(Share::class, 'post_update_password', [
852
+                'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder',
853
+                'itemSource' => $share->getNode()->getId(),
854
+                'uidOwner' => $share->getSharedBy(),
855
+                'token' => $share->getToken(),
856
+                'disabled' => is_null($share->getPassword()),
857
+            ]);
858
+        }
859
+
860
+        if ($share->getPermissions() !== $originalShare->getPermissions()) {
861
+            if ($this->userManager->userExists($share->getShareOwner())) {
862
+                $userFolder = $this->rootFolder->getUserFolder($share->getShareOwner());
863
+            } else {
864
+                $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
865
+            }
866
+            \OC_Hook::emit(Share::class, 'post_update_permissions', array(
867
+                'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder',
868
+                'itemSource' => $share->getNode()->getId(),
869
+                'shareType' => $share->getShareType(),
870
+                'shareWith' => $share->getSharedWith(),
871
+                'uidOwner' => $share->getSharedBy(),
872
+                'permissions' => $share->getPermissions(),
873
+                'path' => $userFolder->getRelativePath($share->getNode()->getPath()),
874
+            ));
875
+        }
876
+
877
+        return $share;
878
+    }
879
+
880
+    /**
881
+     * Updates the password of the given share if it is not the same as the
882
+     * password of the original share.
883
+     *
884
+     * @param \OCP\Share\IShare $share the share to update its password.
885
+     * @param \OCP\Share\IShare $originalShare the original share to compare its
886
+     *        password with.
887
+     * @return boolean whether the password was updated or not.
888
+     */
889
+    private function updateSharePasswordIfNeeded(\OCP\Share\IShare $share, \OCP\Share\IShare $originalShare) {
890
+        // Password updated.
891
+        if ($share->getPassword() !== $originalShare->getPassword()) {
892
+            //Verify the password
893
+            $this->verifyPassword($share->getPassword());
894
+
895
+            // If a password is set. Hash it!
896
+            if ($share->getPassword() !== null) {
897
+                $share->setPassword($this->hasher->hash($share->getPassword()));
898
+
899
+                return true;
900
+            }
901
+        }
902
+
903
+        return false;
904
+    }
905
+
906
+    /**
907
+     * Delete all the children of this share
908
+     * FIXME: remove once https://github.com/owncloud/core/pull/21660 is in
909
+     *
910
+     * @param \OCP\Share\IShare $share
911
+     * @return \OCP\Share\IShare[] List of deleted shares
912
+     */
913
+    protected function deleteChildren(\OCP\Share\IShare $share) {
914
+        $deletedShares = [];
915
+
916
+        $provider = $this->factory->getProviderForType($share->getShareType());
917
+
918
+        foreach ($provider->getChildren($share) as $child) {
919
+            $deletedChildren = $this->deleteChildren($child);
920
+            $deletedShares = array_merge($deletedShares, $deletedChildren);
921
+
922
+            $provider->delete($child);
923
+            $deletedShares[] = $child;
924
+        }
925
+
926
+        return $deletedShares;
927
+    }
928
+
929
+    /**
930
+     * Delete a share
931
+     *
932
+     * @param \OCP\Share\IShare $share
933
+     * @throws ShareNotFound
934
+     * @throws \InvalidArgumentException
935
+     */
936
+    public function deleteShare(\OCP\Share\IShare $share) {
937
+
938
+        try {
939
+            $share->getFullId();
940
+        } catch (\UnexpectedValueException $e) {
941
+            throw new \InvalidArgumentException('Share does not have a full id');
942
+        }
943
+
944
+        $event = new GenericEvent($share);
945
+        $this->eventDispatcher->dispatch('OCP\Share::preUnshare', $event);
946
+
947
+        // Get all children and delete them as well
948
+        $deletedShares = $this->deleteChildren($share);
949
+
950
+        // Do the actual delete
951
+        $provider = $this->factory->getProviderForType($share->getShareType());
952
+        $provider->delete($share);
953
+
954
+        // All the deleted shares caused by this delete
955
+        $deletedShares[] = $share;
956
+
957
+        // Emit post hook
958
+        $event->setArgument('deletedShares', $deletedShares);
959
+        $this->eventDispatcher->dispatch('OCP\Share::postUnshare', $event);
960
+    }
961
+
962
+
963
+    /**
964
+     * Unshare a file as the recipient.
965
+     * This can be different from a regular delete for example when one of
966
+     * the users in a groups deletes that share. But the provider should
967
+     * handle this.
968
+     *
969
+     * @param \OCP\Share\IShare $share
970
+     * @param string $recipientId
971
+     */
972
+    public function deleteFromSelf(\OCP\Share\IShare $share, $recipientId) {
973
+        list($providerId, ) = $this->splitFullId($share->getFullId());
974
+        $provider = $this->factory->getProvider($providerId);
975
+
976
+        $provider->deleteFromSelf($share, $recipientId);
977
+        $event = new GenericEvent($share);
978
+        $this->eventDispatcher->dispatch('OCP\Share::postUnshareFromSelf', $event);
979
+    }
980
+
981
+    /**
982
+     * @inheritdoc
983
+     */
984
+    public function moveShare(\OCP\Share\IShare $share, $recipientId) {
985
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
986
+            throw new \InvalidArgumentException('Can’t change target of link share');
987
+        }
988
+
989
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && $share->getSharedWith() !== $recipientId) {
990
+            throw new \InvalidArgumentException('Invalid recipient');
991
+        }
992
+
993
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
994
+            $sharedWith = $this->groupManager->get($share->getSharedWith());
995
+            if (is_null($sharedWith)) {
996
+                throw new \InvalidArgumentException('Group "' . $share->getSharedWith() . '" does not exist');
997
+            }
998
+            $recipient = $this->userManager->get($recipientId);
999
+            if (!$sharedWith->inGroup($recipient)) {
1000
+                throw new \InvalidArgumentException('Invalid recipient');
1001
+            }
1002
+        }
1003
+
1004
+        list($providerId, ) = $this->splitFullId($share->getFullId());
1005
+        $provider = $this->factory->getProvider($providerId);
1006
+
1007
+        $provider->move($share, $recipientId);
1008
+    }
1009
+
1010
+    public function getSharesInFolder($userId, Folder $node, $reshares = false) {
1011
+        $providers = $this->factory->getAllProviders();
1012
+
1013
+        return array_reduce($providers, function($shares, IShareProvider $provider) use ($userId, $node, $reshares) {
1014
+            $newShares = $provider->getSharesInFolder($userId, $node, $reshares);
1015
+            foreach ($newShares as $fid => $data) {
1016
+                if (!isset($shares[$fid])) {
1017
+                    $shares[$fid] = [];
1018
+                }
1019
+
1020
+                $shares[$fid] = array_merge($shares[$fid], $data);
1021
+            }
1022
+            return $shares;
1023
+        }, []);
1024
+    }
1025
+
1026
+    /**
1027
+     * @inheritdoc
1028
+     */
1029
+    public function getSharesBy($userId, $shareType, $path = null, $reshares = false, $limit = 50, $offset = 0) {
1030
+        if ($path !== null &&
1031
+                !($path instanceof \OCP\Files\File) &&
1032
+                !($path instanceof \OCP\Files\Folder)) {
1033
+            throw new \InvalidArgumentException('invalid path');
1034
+        }
1035
+
1036
+        try {
1037
+            $provider = $this->factory->getProviderForType($shareType);
1038
+        } catch (ProviderException $e) {
1039
+            return [];
1040
+        }
1041
+
1042
+        $shares = $provider->getSharesBy($userId, $shareType, $path, $reshares, $limit, $offset);
1043
+
1044
+        /*
1045 1045
 		 * Work around so we don't return expired shares but still follow
1046 1046
 		 * proper pagination.
1047 1047
 		 */
1048 1048
 
1049
-		$shares2 = [];
1050
-
1051
-		while(true) {
1052
-			$added = 0;
1053
-			foreach ($shares as $share) {
1054
-
1055
-				try {
1056
-					$this->checkExpireDate($share);
1057
-				} catch (ShareNotFound $e) {
1058
-					//Ignore since this basically means the share is deleted
1059
-					continue;
1060
-				}
1061
-
1062
-				$added++;
1063
-				$shares2[] = $share;
1064
-
1065
-				if (count($shares2) === $limit) {
1066
-					break;
1067
-				}
1068
-			}
1069
-
1070
-			// If we did not fetch more shares than the limit then there are no more shares
1071
-			if (count($shares) < $limit) {
1072
-				break;
1073
-			}
1074
-
1075
-			if (count($shares2) === $limit) {
1076
-				break;
1077
-			}
1078
-
1079
-			// If there was no limit on the select we are done
1080
-			if ($limit === -1) {
1081
-				break;
1082
-			}
1083
-
1084
-			$offset += $added;
1085
-
1086
-			// Fetch again $limit shares
1087
-			$shares = $provider->getSharesBy($userId, $shareType, $path, $reshares, $limit, $offset);
1088
-
1089
-			// No more shares means we are done
1090
-			if (empty($shares)) {
1091
-				break;
1092
-			}
1093
-		}
1094
-
1095
-		$shares = $shares2;
1096
-
1097
-		return $shares;
1098
-	}
1099
-
1100
-	/**
1101
-	 * @inheritdoc
1102
-	 */
1103
-	public function getSharedWith($userId, $shareType, $node = null, $limit = 50, $offset = 0) {
1104
-		try {
1105
-			$provider = $this->factory->getProviderForType($shareType);
1106
-		} catch (ProviderException $e) {
1107
-			return [];
1108
-		}
1109
-
1110
-		$shares = $provider->getSharedWith($userId, $shareType, $node, $limit, $offset);
1111
-
1112
-		// remove all shares which are already expired
1113
-		foreach ($shares as $key => $share) {
1114
-			try {
1115
-				$this->checkExpireDate($share);
1116
-			} catch (ShareNotFound $e) {
1117
-				unset($shares[$key]);
1118
-			}
1119
-		}
1120
-
1121
-		return $shares;
1122
-	}
1123
-
1124
-	/**
1125
-	 * @inheritdoc
1126
-	 */
1127
-	public function getShareById($id, $recipient = null) {
1128
-		if ($id === null) {
1129
-			throw new ShareNotFound();
1130
-		}
1131
-
1132
-		list($providerId, $id) = $this->splitFullId($id);
1133
-
1134
-		try {
1135
-			$provider = $this->factory->getProvider($providerId);
1136
-		} catch (ProviderException $e) {
1137
-			throw new ShareNotFound();
1138
-		}
1139
-
1140
-		$share = $provider->getShareById($id, $recipient);
1141
-
1142
-		$this->checkExpireDate($share);
1143
-
1144
-		return $share;
1145
-	}
1146
-
1147
-	/**
1148
-	 * Get all the shares for a given path
1149
-	 *
1150
-	 * @param \OCP\Files\Node $path
1151
-	 * @param int $page
1152
-	 * @param int $perPage
1153
-	 *
1154
-	 * @return Share[]
1155
-	 */
1156
-	public function getSharesByPath(\OCP\Files\Node $path, $page=0, $perPage=50) {
1157
-		return [];
1158
-	}
1159
-
1160
-	/**
1161
-	 * Get the share by token possible with password
1162
-	 *
1163
-	 * @param string $token
1164
-	 * @return Share
1165
-	 *
1166
-	 * @throws ShareNotFound
1167
-	 */
1168
-	public function getShareByToken($token) {
1169
-		$share = null;
1170
-		try {
1171
-			if($this->shareApiAllowLinks()) {
1172
-				$provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_LINK);
1173
-				$share = $provider->getShareByToken($token);
1174
-			}
1175
-		} catch (ProviderException $e) {
1176
-		} catch (ShareNotFound $e) {
1177
-		}
1178
-
1179
-
1180
-		// If it is not a link share try to fetch a federated share by token
1181
-		if ($share === null) {
1182
-			try {
1183
-				$provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_REMOTE);
1184
-				$share = $provider->getShareByToken($token);
1185
-			} catch (ProviderException $e) {
1186
-			} catch (ShareNotFound $e) {
1187
-			}
1188
-		}
1189
-
1190
-		// If it is not a link share try to fetch a mail share by token
1191
-		if ($share === null && $this->shareProviderExists(\OCP\Share::SHARE_TYPE_EMAIL)) {
1192
-			try {
1193
-				$provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_EMAIL);
1194
-				$share = $provider->getShareByToken($token);
1195
-			} catch (ProviderException $e) {
1196
-			} catch (ShareNotFound $e) {
1197
-			}
1198
-		}
1199
-
1200
-		if ($share === null && $this->shareProviderExists(\OCP\Share::SHARE_TYPE_CIRCLE)) {
1201
-			try {
1202
-				$provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_CIRCLE);
1203
-				$share = $provider->getShareByToken($token);
1204
-			} catch (ProviderException $e) {
1205
-			} catch (ShareNotFound $e) {
1206
-			}
1207
-		}
1208
-
1209
-		if ($share === null) {
1210
-			throw new ShareNotFound($this->l->t('The requested share does not exist anymore'));
1211
-		}
1212
-
1213
-		$this->checkExpireDate($share);
1214
-
1215
-		/*
1049
+        $shares2 = [];
1050
+
1051
+        while(true) {
1052
+            $added = 0;
1053
+            foreach ($shares as $share) {
1054
+
1055
+                try {
1056
+                    $this->checkExpireDate($share);
1057
+                } catch (ShareNotFound $e) {
1058
+                    //Ignore since this basically means the share is deleted
1059
+                    continue;
1060
+                }
1061
+
1062
+                $added++;
1063
+                $shares2[] = $share;
1064
+
1065
+                if (count($shares2) === $limit) {
1066
+                    break;
1067
+                }
1068
+            }
1069
+
1070
+            // If we did not fetch more shares than the limit then there are no more shares
1071
+            if (count($shares) < $limit) {
1072
+                break;
1073
+            }
1074
+
1075
+            if (count($shares2) === $limit) {
1076
+                break;
1077
+            }
1078
+
1079
+            // If there was no limit on the select we are done
1080
+            if ($limit === -1) {
1081
+                break;
1082
+            }
1083
+
1084
+            $offset += $added;
1085
+
1086
+            // Fetch again $limit shares
1087
+            $shares = $provider->getSharesBy($userId, $shareType, $path, $reshares, $limit, $offset);
1088
+
1089
+            // No more shares means we are done
1090
+            if (empty($shares)) {
1091
+                break;
1092
+            }
1093
+        }
1094
+
1095
+        $shares = $shares2;
1096
+
1097
+        return $shares;
1098
+    }
1099
+
1100
+    /**
1101
+     * @inheritdoc
1102
+     */
1103
+    public function getSharedWith($userId, $shareType, $node = null, $limit = 50, $offset = 0) {
1104
+        try {
1105
+            $provider = $this->factory->getProviderForType($shareType);
1106
+        } catch (ProviderException $e) {
1107
+            return [];
1108
+        }
1109
+
1110
+        $shares = $provider->getSharedWith($userId, $shareType, $node, $limit, $offset);
1111
+
1112
+        // remove all shares which are already expired
1113
+        foreach ($shares as $key => $share) {
1114
+            try {
1115
+                $this->checkExpireDate($share);
1116
+            } catch (ShareNotFound $e) {
1117
+                unset($shares[$key]);
1118
+            }
1119
+        }
1120
+
1121
+        return $shares;
1122
+    }
1123
+
1124
+    /**
1125
+     * @inheritdoc
1126
+     */
1127
+    public function getShareById($id, $recipient = null) {
1128
+        if ($id === null) {
1129
+            throw new ShareNotFound();
1130
+        }
1131
+
1132
+        list($providerId, $id) = $this->splitFullId($id);
1133
+
1134
+        try {
1135
+            $provider = $this->factory->getProvider($providerId);
1136
+        } catch (ProviderException $e) {
1137
+            throw new ShareNotFound();
1138
+        }
1139
+
1140
+        $share = $provider->getShareById($id, $recipient);
1141
+
1142
+        $this->checkExpireDate($share);
1143
+
1144
+        return $share;
1145
+    }
1146
+
1147
+    /**
1148
+     * Get all the shares for a given path
1149
+     *
1150
+     * @param \OCP\Files\Node $path
1151
+     * @param int $page
1152
+     * @param int $perPage
1153
+     *
1154
+     * @return Share[]
1155
+     */
1156
+    public function getSharesByPath(\OCP\Files\Node $path, $page=0, $perPage=50) {
1157
+        return [];
1158
+    }
1159
+
1160
+    /**
1161
+     * Get the share by token possible with password
1162
+     *
1163
+     * @param string $token
1164
+     * @return Share
1165
+     *
1166
+     * @throws ShareNotFound
1167
+     */
1168
+    public function getShareByToken($token) {
1169
+        $share = null;
1170
+        try {
1171
+            if($this->shareApiAllowLinks()) {
1172
+                $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_LINK);
1173
+                $share = $provider->getShareByToken($token);
1174
+            }
1175
+        } catch (ProviderException $e) {
1176
+        } catch (ShareNotFound $e) {
1177
+        }
1178
+
1179
+
1180
+        // If it is not a link share try to fetch a federated share by token
1181
+        if ($share === null) {
1182
+            try {
1183
+                $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_REMOTE);
1184
+                $share = $provider->getShareByToken($token);
1185
+            } catch (ProviderException $e) {
1186
+            } catch (ShareNotFound $e) {
1187
+            }
1188
+        }
1189
+
1190
+        // If it is not a link share try to fetch a mail share by token
1191
+        if ($share === null && $this->shareProviderExists(\OCP\Share::SHARE_TYPE_EMAIL)) {
1192
+            try {
1193
+                $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_EMAIL);
1194
+                $share = $provider->getShareByToken($token);
1195
+            } catch (ProviderException $e) {
1196
+            } catch (ShareNotFound $e) {
1197
+            }
1198
+        }
1199
+
1200
+        if ($share === null && $this->shareProviderExists(\OCP\Share::SHARE_TYPE_CIRCLE)) {
1201
+            try {
1202
+                $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_CIRCLE);
1203
+                $share = $provider->getShareByToken($token);
1204
+            } catch (ProviderException $e) {
1205
+            } catch (ShareNotFound $e) {
1206
+            }
1207
+        }
1208
+
1209
+        if ($share === null) {
1210
+            throw new ShareNotFound($this->l->t('The requested share does not exist anymore'));
1211
+        }
1212
+
1213
+        $this->checkExpireDate($share);
1214
+
1215
+        /*
1216 1216
 		 * Reduce the permissions for link shares if public upload is not enabled
1217 1217
 		 */
1218
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK &&
1219
-			!$this->shareApiLinkAllowPublicUpload()) {
1220
-			$share->setPermissions($share->getPermissions() & ~(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE));
1221
-		}
1222
-
1223
-		return $share;
1224
-	}
1225
-
1226
-	protected function checkExpireDate($share) {
1227
-		if ($share->getExpirationDate() !== null &&
1228
-			$share->getExpirationDate() <= new \DateTime()) {
1229
-			$this->deleteShare($share);
1230
-			throw new ShareNotFound($this->l->t('The requested share does not exist anymore'));
1231
-		}
1232
-
1233
-	}
1234
-
1235
-	/**
1236
-	 * Verify the password of a public share
1237
-	 *
1238
-	 * @param \OCP\Share\IShare $share
1239
-	 * @param string $password
1240
-	 * @return bool
1241
-	 */
1242
-	public function checkPassword(\OCP\Share\IShare $share, $password) {
1243
-		$passwordProtected = $share->getShareType() !== \OCP\Share::SHARE_TYPE_LINK
1244
-			|| $share->getShareType() !== \OCP\Share::SHARE_TYPE_EMAIL;
1245
-		if (!$passwordProtected) {
1246
-			//TODO maybe exception?
1247
-			return false;
1248
-		}
1249
-
1250
-		if ($password === null || $share->getPassword() === null) {
1251
-			return false;
1252
-		}
1253
-
1254
-		$newHash = '';
1255
-		if (!$this->hasher->verify($password, $share->getPassword(), $newHash)) {
1256
-			return false;
1257
-		}
1258
-
1259
-		if (!empty($newHash)) {
1260
-			$share->setPassword($newHash);
1261
-			$provider = $this->factory->getProviderForType($share->getShareType());
1262
-			$provider->update($share);
1263
-		}
1264
-
1265
-		return true;
1266
-	}
1267
-
1268
-	/**
1269
-	 * @inheritdoc
1270
-	 */
1271
-	public function userDeleted($uid) {
1272
-		$types = [\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_LINK, \OCP\Share::SHARE_TYPE_REMOTE, \OCP\Share::SHARE_TYPE_EMAIL];
1273
-
1274
-		foreach ($types as $type) {
1275
-			try {
1276
-				$provider = $this->factory->getProviderForType($type);
1277
-			} catch (ProviderException $e) {
1278
-				continue;
1279
-			}
1280
-			$provider->userDeleted($uid, $type);
1281
-		}
1282
-	}
1283
-
1284
-	/**
1285
-	 * @inheritdoc
1286
-	 */
1287
-	public function groupDeleted($gid) {
1288
-		$provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP);
1289
-		$provider->groupDeleted($gid);
1290
-	}
1291
-
1292
-	/**
1293
-	 * @inheritdoc
1294
-	 */
1295
-	public function userDeletedFromGroup($uid, $gid) {
1296
-		$provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP);
1297
-		$provider->userDeletedFromGroup($uid, $gid);
1298
-	}
1299
-
1300
-	/**
1301
-	 * Get access list to a path. This means
1302
-	 * all the users that can access a given path.
1303
-	 *
1304
-	 * Consider:
1305
-	 * -root
1306
-	 * |-folder1 (23)
1307
-	 *  |-folder2 (32)
1308
-	 *   |-fileA (42)
1309
-	 *
1310
-	 * fileA is shared with user1 and user1@server1
1311
-	 * folder2 is shared with group2 (user4 is a member of group2)
1312
-	 * folder1 is shared with user2 (renamed to "folder (1)") and user2@server2
1313
-	 *
1314
-	 * Then the access list to '/folder1/folder2/fileA' with $currentAccess is:
1315
-	 * [
1316
-	 *  users  => [
1317
-	 *      'user1' => ['node_id' => 42, 'node_path' => '/fileA'],
1318
-	 *      'user4' => ['node_id' => 32, 'node_path' => '/folder2'],
1319
-	 *      'user2' => ['node_id' => 23, 'node_path' => '/folder (1)'],
1320
-	 *  ],
1321
-	 *  remote => [
1322
-	 *      'user1@server1' => ['node_id' => 42, 'token' => 'SeCr3t'],
1323
-	 *      'user2@server2' => ['node_id' => 23, 'token' => 'FooBaR'],
1324
-	 *  ],
1325
-	 *  public => bool
1326
-	 *  mail => bool
1327
-	 * ]
1328
-	 *
1329
-	 * The access list to '/folder1/folder2/fileA' **without** $currentAccess is:
1330
-	 * [
1331
-	 *  users  => ['user1', 'user2', 'user4'],
1332
-	 *  remote => bool,
1333
-	 *  public => bool
1334
-	 *  mail => bool
1335
-	 * ]
1336
-	 *
1337
-	 * This is required for encryption/activity
1338
-	 *
1339
-	 * @param \OCP\Files\Node $path
1340
-	 * @param bool $recursive Should we check all parent folders as well
1341
-	 * @param bool $currentAccess Ensure the recipient has access to the file (e.g. did not unshare it)
1342
-	 * @return array
1343
-	 */
1344
-	public function getAccessList(\OCP\Files\Node $path, $recursive = true, $currentAccess = false) {
1345
-		$owner = $path->getOwner()->getUID();
1346
-
1347
-		if ($currentAccess) {
1348
-			$al = ['users' => [], 'remote' => [], 'public' => false];
1349
-		} else {
1350
-			$al = ['users' => [], 'remote' => false, 'public' => false];
1351
-		}
1352
-		if (!$this->userManager->userExists($owner)) {
1353
-			return $al;
1354
-		}
1355
-
1356
-		//Get node for the owner
1357
-		$userFolder = $this->rootFolder->getUserFolder($owner);
1358
-		if ($path->getId() !== $userFolder->getId() && !$userFolder->isSubNode($path)) {
1359
-			$path = $userFolder->getById($path->getId())[0];
1360
-		}
1361
-
1362
-		$providers = $this->factory->getAllProviders();
1363
-
1364
-		/** @var Node[] $nodes */
1365
-		$nodes = [];
1366
-
1367
-
1368
-		if ($currentAccess) {
1369
-			$ownerPath = $path->getPath();
1370
-			$ownerPath = explode('/', $ownerPath, 4);
1371
-			if (count($ownerPath) < 4) {
1372
-				$ownerPath = '';
1373
-			} else {
1374
-				$ownerPath = $ownerPath[3];
1375
-			}
1376
-			$al['users'][$owner] = [
1377
-				'node_id' => $path->getId(),
1378
-				'node_path' => '/' . $ownerPath,
1379
-			];
1380
-		} else {
1381
-			$al['users'][] = $owner;
1382
-		}
1383
-
1384
-		// Collect all the shares
1385
-		while ($path->getPath() !== $userFolder->getPath()) {
1386
-			$nodes[] = $path;
1387
-			if (!$recursive) {
1388
-				break;
1389
-			}
1390
-			$path = $path->getParent();
1391
-		}
1392
-
1393
-		foreach ($providers as $provider) {
1394
-			$tmp = $provider->getAccessList($nodes, $currentAccess);
1395
-
1396
-			foreach ($tmp as $k => $v) {
1397
-				if (isset($al[$k])) {
1398
-					if (is_array($al[$k])) {
1399
-						if ($currentAccess) {
1400
-							$al[$k] += $v;
1401
-						} else {
1402
-							$al[$k] = array_merge($al[$k], $v);
1403
-							$al[$k] = array_unique($al[$k]);
1404
-							$al[$k] = array_values($al[$k]);
1405
-						}
1406
-					} else {
1407
-						$al[$k] = $al[$k] || $v;
1408
-					}
1409
-				} else {
1410
-					$al[$k] = $v;
1411
-				}
1412
-			}
1413
-		}
1414
-
1415
-		return $al;
1416
-	}
1417
-
1418
-	/**
1419
-	 * Create a new share
1420
-	 * @return \OCP\Share\IShare
1421
-	 */
1422
-	public function newShare() {
1423
-		return new \OC\Share20\Share($this->rootFolder, $this->userManager);
1424
-	}
1425
-
1426
-	/**
1427
-	 * Is the share API enabled
1428
-	 *
1429
-	 * @return bool
1430
-	 */
1431
-	public function shareApiEnabled() {
1432
-		return $this->config->getAppValue('core', 'shareapi_enabled', 'yes') === 'yes';
1433
-	}
1434
-
1435
-	/**
1436
-	 * Is public link sharing enabled
1437
-	 *
1438
-	 * @return bool
1439
-	 */
1440
-	public function shareApiAllowLinks() {
1441
-		return $this->config->getAppValue('core', 'shareapi_allow_links', 'yes') === 'yes';
1442
-	}
1443
-
1444
-	/**
1445
-	 * Is password on public link requires
1446
-	 *
1447
-	 * @return bool
1448
-	 */
1449
-	public function shareApiLinkEnforcePassword() {
1450
-		return $this->config->getAppValue('core', 'shareapi_enforce_links_password', 'no') === 'yes';
1451
-	}
1452
-
1453
-	/**
1454
-	 * Is default expire date enabled
1455
-	 *
1456
-	 * @return bool
1457
-	 */
1458
-	public function shareApiLinkDefaultExpireDate() {
1459
-		return $this->config->getAppValue('core', 'shareapi_default_expire_date', 'no') === 'yes';
1460
-	}
1461
-
1462
-	/**
1463
-	 * Is default expire date enforced
1464
-	 *`
1465
-	 * @return bool
1466
-	 */
1467
-	public function shareApiLinkDefaultExpireDateEnforced() {
1468
-		return $this->shareApiLinkDefaultExpireDate() &&
1469
-			$this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'no') === 'yes';
1470
-	}
1471
-
1472
-	/**
1473
-	 * Number of default expire days
1474
-	 *shareApiLinkAllowPublicUpload
1475
-	 * @return int
1476
-	 */
1477
-	public function shareApiLinkDefaultExpireDays() {
1478
-		return (int)$this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7');
1479
-	}
1480
-
1481
-	/**
1482
-	 * Allow public upload on link shares
1483
-	 *
1484
-	 * @return bool
1485
-	 */
1486
-	public function shareApiLinkAllowPublicUpload() {
1487
-		return $this->config->getAppValue('core', 'shareapi_allow_public_upload', 'yes') === 'yes';
1488
-	}
1489
-
1490
-	/**
1491
-	 * check if user can only share with group members
1492
-	 * @return bool
1493
-	 */
1494
-	public function shareWithGroupMembersOnly() {
1495
-		return $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
1496
-	}
1497
-
1498
-	/**
1499
-	 * Check if users can share with groups
1500
-	 * @return bool
1501
-	 */
1502
-	public function allowGroupSharing() {
1503
-		return $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes') === 'yes';
1504
-	}
1505
-
1506
-	/**
1507
-	 * Copied from \OC_Util::isSharingDisabledForUser
1508
-	 *
1509
-	 * TODO: Deprecate fuction from OC_Util
1510
-	 *
1511
-	 * @param string $userId
1512
-	 * @return bool
1513
-	 */
1514
-	public function sharingDisabledForUser($userId) {
1515
-		if ($userId === null) {
1516
-			return false;
1517
-		}
1518
-
1519
-		if (isset($this->sharingDisabledForUsersCache[$userId])) {
1520
-			return $this->sharingDisabledForUsersCache[$userId];
1521
-		}
1522
-
1523
-		if ($this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes') {
1524
-			$groupsList = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', '');
1525
-			$excludedGroups = json_decode($groupsList);
1526
-			if (is_null($excludedGroups)) {
1527
-				$excludedGroups = explode(',', $groupsList);
1528
-				$newValue = json_encode($excludedGroups);
1529
-				$this->config->setAppValue('core', 'shareapi_exclude_groups_list', $newValue);
1530
-			}
1531
-			$user = $this->userManager->get($userId);
1532
-			$usersGroups = $this->groupManager->getUserGroupIds($user);
1533
-			if (!empty($usersGroups)) {
1534
-				$remainingGroups = array_diff($usersGroups, $excludedGroups);
1535
-				// if the user is only in groups which are disabled for sharing then
1536
-				// sharing is also disabled for the user
1537
-				if (empty($remainingGroups)) {
1538
-					$this->sharingDisabledForUsersCache[$userId] = true;
1539
-					return true;
1540
-				}
1541
-			}
1542
-		}
1543
-
1544
-		$this->sharingDisabledForUsersCache[$userId] = false;
1545
-		return false;
1546
-	}
1547
-
1548
-	/**
1549
-	 * @inheritdoc
1550
-	 */
1551
-	public function outgoingServer2ServerSharesAllowed() {
1552
-		return $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') === 'yes';
1553
-	}
1554
-
1555
-	/**
1556
-	 * @inheritdoc
1557
-	 */
1558
-	public function shareProviderExists($shareType) {
1559
-		try {
1560
-			$this->factory->getProviderForType($shareType);
1561
-		} catch (ProviderException $e) {
1562
-			return false;
1563
-		}
1564
-
1565
-		return true;
1566
-	}
1218
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK &&
1219
+            !$this->shareApiLinkAllowPublicUpload()) {
1220
+            $share->setPermissions($share->getPermissions() & ~(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE));
1221
+        }
1222
+
1223
+        return $share;
1224
+    }
1225
+
1226
+    protected function checkExpireDate($share) {
1227
+        if ($share->getExpirationDate() !== null &&
1228
+            $share->getExpirationDate() <= new \DateTime()) {
1229
+            $this->deleteShare($share);
1230
+            throw new ShareNotFound($this->l->t('The requested share does not exist anymore'));
1231
+        }
1232
+
1233
+    }
1234
+
1235
+    /**
1236
+     * Verify the password of a public share
1237
+     *
1238
+     * @param \OCP\Share\IShare $share
1239
+     * @param string $password
1240
+     * @return bool
1241
+     */
1242
+    public function checkPassword(\OCP\Share\IShare $share, $password) {
1243
+        $passwordProtected = $share->getShareType() !== \OCP\Share::SHARE_TYPE_LINK
1244
+            || $share->getShareType() !== \OCP\Share::SHARE_TYPE_EMAIL;
1245
+        if (!$passwordProtected) {
1246
+            //TODO maybe exception?
1247
+            return false;
1248
+        }
1249
+
1250
+        if ($password === null || $share->getPassword() === null) {
1251
+            return false;
1252
+        }
1253
+
1254
+        $newHash = '';
1255
+        if (!$this->hasher->verify($password, $share->getPassword(), $newHash)) {
1256
+            return false;
1257
+        }
1258
+
1259
+        if (!empty($newHash)) {
1260
+            $share->setPassword($newHash);
1261
+            $provider = $this->factory->getProviderForType($share->getShareType());
1262
+            $provider->update($share);
1263
+        }
1264
+
1265
+        return true;
1266
+    }
1267
+
1268
+    /**
1269
+     * @inheritdoc
1270
+     */
1271
+    public function userDeleted($uid) {
1272
+        $types = [\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_LINK, \OCP\Share::SHARE_TYPE_REMOTE, \OCP\Share::SHARE_TYPE_EMAIL];
1273
+
1274
+        foreach ($types as $type) {
1275
+            try {
1276
+                $provider = $this->factory->getProviderForType($type);
1277
+            } catch (ProviderException $e) {
1278
+                continue;
1279
+            }
1280
+            $provider->userDeleted($uid, $type);
1281
+        }
1282
+    }
1283
+
1284
+    /**
1285
+     * @inheritdoc
1286
+     */
1287
+    public function groupDeleted($gid) {
1288
+        $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP);
1289
+        $provider->groupDeleted($gid);
1290
+    }
1291
+
1292
+    /**
1293
+     * @inheritdoc
1294
+     */
1295
+    public function userDeletedFromGroup($uid, $gid) {
1296
+        $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP);
1297
+        $provider->userDeletedFromGroup($uid, $gid);
1298
+    }
1299
+
1300
+    /**
1301
+     * Get access list to a path. This means
1302
+     * all the users that can access a given path.
1303
+     *
1304
+     * Consider:
1305
+     * -root
1306
+     * |-folder1 (23)
1307
+     *  |-folder2 (32)
1308
+     *   |-fileA (42)
1309
+     *
1310
+     * fileA is shared with user1 and user1@server1
1311
+     * folder2 is shared with group2 (user4 is a member of group2)
1312
+     * folder1 is shared with user2 (renamed to "folder (1)") and user2@server2
1313
+     *
1314
+     * Then the access list to '/folder1/folder2/fileA' with $currentAccess is:
1315
+     * [
1316
+     *  users  => [
1317
+     *      'user1' => ['node_id' => 42, 'node_path' => '/fileA'],
1318
+     *      'user4' => ['node_id' => 32, 'node_path' => '/folder2'],
1319
+     *      'user2' => ['node_id' => 23, 'node_path' => '/folder (1)'],
1320
+     *  ],
1321
+     *  remote => [
1322
+     *      'user1@server1' => ['node_id' => 42, 'token' => 'SeCr3t'],
1323
+     *      'user2@server2' => ['node_id' => 23, 'token' => 'FooBaR'],
1324
+     *  ],
1325
+     *  public => bool
1326
+     *  mail => bool
1327
+     * ]
1328
+     *
1329
+     * The access list to '/folder1/folder2/fileA' **without** $currentAccess is:
1330
+     * [
1331
+     *  users  => ['user1', 'user2', 'user4'],
1332
+     *  remote => bool,
1333
+     *  public => bool
1334
+     *  mail => bool
1335
+     * ]
1336
+     *
1337
+     * This is required for encryption/activity
1338
+     *
1339
+     * @param \OCP\Files\Node $path
1340
+     * @param bool $recursive Should we check all parent folders as well
1341
+     * @param bool $currentAccess Ensure the recipient has access to the file (e.g. did not unshare it)
1342
+     * @return array
1343
+     */
1344
+    public function getAccessList(\OCP\Files\Node $path, $recursive = true, $currentAccess = false) {
1345
+        $owner = $path->getOwner()->getUID();
1346
+
1347
+        if ($currentAccess) {
1348
+            $al = ['users' => [], 'remote' => [], 'public' => false];
1349
+        } else {
1350
+            $al = ['users' => [], 'remote' => false, 'public' => false];
1351
+        }
1352
+        if (!$this->userManager->userExists($owner)) {
1353
+            return $al;
1354
+        }
1355
+
1356
+        //Get node for the owner
1357
+        $userFolder = $this->rootFolder->getUserFolder($owner);
1358
+        if ($path->getId() !== $userFolder->getId() && !$userFolder->isSubNode($path)) {
1359
+            $path = $userFolder->getById($path->getId())[0];
1360
+        }
1361
+
1362
+        $providers = $this->factory->getAllProviders();
1363
+
1364
+        /** @var Node[] $nodes */
1365
+        $nodes = [];
1366
+
1367
+
1368
+        if ($currentAccess) {
1369
+            $ownerPath = $path->getPath();
1370
+            $ownerPath = explode('/', $ownerPath, 4);
1371
+            if (count($ownerPath) < 4) {
1372
+                $ownerPath = '';
1373
+            } else {
1374
+                $ownerPath = $ownerPath[3];
1375
+            }
1376
+            $al['users'][$owner] = [
1377
+                'node_id' => $path->getId(),
1378
+                'node_path' => '/' . $ownerPath,
1379
+            ];
1380
+        } else {
1381
+            $al['users'][] = $owner;
1382
+        }
1383
+
1384
+        // Collect all the shares
1385
+        while ($path->getPath() !== $userFolder->getPath()) {
1386
+            $nodes[] = $path;
1387
+            if (!$recursive) {
1388
+                break;
1389
+            }
1390
+            $path = $path->getParent();
1391
+        }
1392
+
1393
+        foreach ($providers as $provider) {
1394
+            $tmp = $provider->getAccessList($nodes, $currentAccess);
1395
+
1396
+            foreach ($tmp as $k => $v) {
1397
+                if (isset($al[$k])) {
1398
+                    if (is_array($al[$k])) {
1399
+                        if ($currentAccess) {
1400
+                            $al[$k] += $v;
1401
+                        } else {
1402
+                            $al[$k] = array_merge($al[$k], $v);
1403
+                            $al[$k] = array_unique($al[$k]);
1404
+                            $al[$k] = array_values($al[$k]);
1405
+                        }
1406
+                    } else {
1407
+                        $al[$k] = $al[$k] || $v;
1408
+                    }
1409
+                } else {
1410
+                    $al[$k] = $v;
1411
+                }
1412
+            }
1413
+        }
1414
+
1415
+        return $al;
1416
+    }
1417
+
1418
+    /**
1419
+     * Create a new share
1420
+     * @return \OCP\Share\IShare
1421
+     */
1422
+    public function newShare() {
1423
+        return new \OC\Share20\Share($this->rootFolder, $this->userManager);
1424
+    }
1425
+
1426
+    /**
1427
+     * Is the share API enabled
1428
+     *
1429
+     * @return bool
1430
+     */
1431
+    public function shareApiEnabled() {
1432
+        return $this->config->getAppValue('core', 'shareapi_enabled', 'yes') === 'yes';
1433
+    }
1434
+
1435
+    /**
1436
+     * Is public link sharing enabled
1437
+     *
1438
+     * @return bool
1439
+     */
1440
+    public function shareApiAllowLinks() {
1441
+        return $this->config->getAppValue('core', 'shareapi_allow_links', 'yes') === 'yes';
1442
+    }
1443
+
1444
+    /**
1445
+     * Is password on public link requires
1446
+     *
1447
+     * @return bool
1448
+     */
1449
+    public function shareApiLinkEnforcePassword() {
1450
+        return $this->config->getAppValue('core', 'shareapi_enforce_links_password', 'no') === 'yes';
1451
+    }
1452
+
1453
+    /**
1454
+     * Is default expire date enabled
1455
+     *
1456
+     * @return bool
1457
+     */
1458
+    public function shareApiLinkDefaultExpireDate() {
1459
+        return $this->config->getAppValue('core', 'shareapi_default_expire_date', 'no') === 'yes';
1460
+    }
1461
+
1462
+    /**
1463
+     * Is default expire date enforced
1464
+     *`
1465
+     * @return bool
1466
+     */
1467
+    public function shareApiLinkDefaultExpireDateEnforced() {
1468
+        return $this->shareApiLinkDefaultExpireDate() &&
1469
+            $this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'no') === 'yes';
1470
+    }
1471
+
1472
+    /**
1473
+     * Number of default expire days
1474
+     *shareApiLinkAllowPublicUpload
1475
+     * @return int
1476
+     */
1477
+    public function shareApiLinkDefaultExpireDays() {
1478
+        return (int)$this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7');
1479
+    }
1480
+
1481
+    /**
1482
+     * Allow public upload on link shares
1483
+     *
1484
+     * @return bool
1485
+     */
1486
+    public function shareApiLinkAllowPublicUpload() {
1487
+        return $this->config->getAppValue('core', 'shareapi_allow_public_upload', 'yes') === 'yes';
1488
+    }
1489
+
1490
+    /**
1491
+     * check if user can only share with group members
1492
+     * @return bool
1493
+     */
1494
+    public function shareWithGroupMembersOnly() {
1495
+        return $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
1496
+    }
1497
+
1498
+    /**
1499
+     * Check if users can share with groups
1500
+     * @return bool
1501
+     */
1502
+    public function allowGroupSharing() {
1503
+        return $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes') === 'yes';
1504
+    }
1505
+
1506
+    /**
1507
+     * Copied from \OC_Util::isSharingDisabledForUser
1508
+     *
1509
+     * TODO: Deprecate fuction from OC_Util
1510
+     *
1511
+     * @param string $userId
1512
+     * @return bool
1513
+     */
1514
+    public function sharingDisabledForUser($userId) {
1515
+        if ($userId === null) {
1516
+            return false;
1517
+        }
1518
+
1519
+        if (isset($this->sharingDisabledForUsersCache[$userId])) {
1520
+            return $this->sharingDisabledForUsersCache[$userId];
1521
+        }
1522
+
1523
+        if ($this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes') {
1524
+            $groupsList = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', '');
1525
+            $excludedGroups = json_decode($groupsList);
1526
+            if (is_null($excludedGroups)) {
1527
+                $excludedGroups = explode(',', $groupsList);
1528
+                $newValue = json_encode($excludedGroups);
1529
+                $this->config->setAppValue('core', 'shareapi_exclude_groups_list', $newValue);
1530
+            }
1531
+            $user = $this->userManager->get($userId);
1532
+            $usersGroups = $this->groupManager->getUserGroupIds($user);
1533
+            if (!empty($usersGroups)) {
1534
+                $remainingGroups = array_diff($usersGroups, $excludedGroups);
1535
+                // if the user is only in groups which are disabled for sharing then
1536
+                // sharing is also disabled for the user
1537
+                if (empty($remainingGroups)) {
1538
+                    $this->sharingDisabledForUsersCache[$userId] = true;
1539
+                    return true;
1540
+                }
1541
+            }
1542
+        }
1543
+
1544
+        $this->sharingDisabledForUsersCache[$userId] = false;
1545
+        return false;
1546
+    }
1547
+
1548
+    /**
1549
+     * @inheritdoc
1550
+     */
1551
+    public function outgoingServer2ServerSharesAllowed() {
1552
+        return $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') === 'yes';
1553
+    }
1554
+
1555
+    /**
1556
+     * @inheritdoc
1557
+     */
1558
+    public function shareProviderExists($shareType) {
1559
+        try {
1560
+            $this->factory->getProviderForType($shareType);
1561
+        } catch (ProviderException $e) {
1562
+            return false;
1563
+        }
1564
+
1565
+        return true;
1566
+    }
1567 1567
 
1568 1568
 }
Please login to merge, or discard this patch.
settings/Controller/UsersController.php 1 patch
Indentation   +983 added lines, -983 removed lines patch added patch discarded remove patch
@@ -70,988 +70,988 @@
 block discarded – undo
70 70
  * @package OC\Settings\Controller
71 71
  */
72 72
 class UsersController extends Controller {
73
-	/** @var IL10N */
74
-	private $l10n;
75
-	/** @var IUserSession */
76
-	private $userSession;
77
-	/** @var bool */
78
-	private $isAdmin;
79
-	/** @var IUserManager */
80
-	private $userManager;
81
-	/** @var IGroupManager */
82
-	private $groupManager;
83
-	/** @var IConfig */
84
-	private $config;
85
-	/** @var ILogger */
86
-	private $log;
87
-	/** @var IMailer */
88
-	private $mailer;
89
-	/** @var bool contains the state of the encryption app */
90
-	private $isEncryptionAppEnabled;
91
-	/** @var bool contains the state of the admin recovery setting */
92
-	private $isRestoreEnabled = false;
93
-	/** @var IAppManager */
94
-	private $appManager;
95
-	/** @var IAvatarManager */
96
-	private $avatarManager;
97
-	/** @var AccountManager */
98
-	private $accountManager;
99
-	/** @var ISecureRandom */
100
-	private $secureRandom;
101
-	/** @var NewUserMailHelper */
102
-	private $newUserMailHelper;
103
-	/** @var ITimeFactory */
104
-	private $timeFactory;
105
-	/** @var ICrypto */
106
-	private $crypto;
107
-	/** @var Manager */
108
-	private $keyManager;
109
-	/** @var IJobList */
110
-	private $jobList;
111
-
112
-	/** @var IUserMountCache */
113
-	private $userMountCache;
114
-
115
-	/** @var IManager */
116
-	private $encryptionManager;
117
-
118
-
119
-	/**
120
-	 * @param string $appName
121
-	 * @param IRequest $request
122
-	 * @param IUserManager $userManager
123
-	 * @param IGroupManager $groupManager
124
-	 * @param IUserSession $userSession
125
-	 * @param IConfig $config
126
-	 * @param bool $isAdmin
127
-	 * @param IL10N $l10n
128
-	 * @param ILogger $log
129
-	 * @param IMailer $mailer
130
-	 * @param IURLGenerator $urlGenerator
131
-	 * @param IAppManager $appManager
132
-	 * @param IAvatarManager $avatarManager
133
-	 * @param AccountManager $accountManager
134
-	 * @param ISecureRandom $secureRandom
135
-	 * @param NewUserMailHelper $newUserMailHelper
136
-	 * @param ITimeFactory $timeFactory
137
-	 * @param ICrypto $crypto
138
-	 * @param Manager $keyManager
139
-	 * @param IJobList $jobList
140
-	 * @param IUserMountCache $userMountCache
141
-	 * @param IManager $encryptionManager
142
-	 */
143
-	public function __construct($appName,
144
-								IRequest $request,
145
-								IUserManager $userManager,
146
-								IGroupManager $groupManager,
147
-								IUserSession $userSession,
148
-								IConfig $config,
149
-								$isAdmin,
150
-								IL10N $l10n,
151
-								ILogger $log,
152
-								IMailer $mailer,
153
-								IURLGenerator $urlGenerator,
154
-								IAppManager $appManager,
155
-								IAvatarManager $avatarManager,
156
-								AccountManager $accountManager,
157
-								ISecureRandom $secureRandom,
158
-								NewUserMailHelper $newUserMailHelper,
159
-								ITimeFactory $timeFactory,
160
-								ICrypto $crypto,
161
-								Manager $keyManager,
162
-								IJobList $jobList,
163
-								IUserMountCache $userMountCache,
164
-								IManager $encryptionManager) {
165
-		parent::__construct($appName, $request);
166
-		$this->userManager = $userManager;
167
-		$this->groupManager = $groupManager;
168
-		$this->userSession = $userSession;
169
-		$this->config = $config;
170
-		$this->isAdmin = $isAdmin;
171
-		$this->l10n = $l10n;
172
-		$this->log = $log;
173
-		$this->mailer = $mailer;
174
-		$this->appManager = $appManager;
175
-		$this->avatarManager = $avatarManager;
176
-		$this->accountManager = $accountManager;
177
-		$this->secureRandom = $secureRandom;
178
-		$this->newUserMailHelper = $newUserMailHelper;
179
-		$this->timeFactory = $timeFactory;
180
-		$this->crypto = $crypto;
181
-		$this->keyManager = $keyManager;
182
-		$this->jobList = $jobList;
183
-		$this->userMountCache = $userMountCache;
184
-		$this->encryptionManager = $encryptionManager;
185
-
186
-		// check for encryption state - TODO see formatUserForIndex
187
-		$this->isEncryptionAppEnabled = $appManager->isEnabledForUser('encryption');
188
-		if ($this->isEncryptionAppEnabled) {
189
-			// putting this directly in empty is possible in PHP 5.5+
190
-			$result = $config->getAppValue('encryption', 'recoveryAdminEnabled', '0');
191
-			$this->isRestoreEnabled = !empty($result);
192
-		}
193
-	}
194
-
195
-	/**
196
-	 * @param IUser $user
197
-	 * @param array|null $userGroups
198
-	 * @return array
199
-	 */
200
-	private function formatUserForIndex(IUser $user, array $userGroups = null) {
201
-
202
-		// TODO: eliminate this encryption specific code below and somehow
203
-		// hook in additional user info from other apps
204
-
205
-		// recovery isn't possible if admin or user has it disabled and encryption
206
-		// is enabled - so we eliminate the else paths in the conditional tree
207
-		// below
208
-		$restorePossible = false;
209
-
210
-		if ($this->isEncryptionAppEnabled) {
211
-			if ($this->isRestoreEnabled) {
212
-				// check for the users recovery setting
213
-				$recoveryMode = $this->config->getUserValue($user->getUID(), 'encryption', 'recoveryEnabled', '0');
214
-				// method call inside empty is possible with PHP 5.5+
215
-				$recoveryModeEnabled = !empty($recoveryMode);
216
-				if ($recoveryModeEnabled) {
217
-					// user also has recovery mode enabled
218
-					$restorePossible = true;
219
-				}
220
-			} else {
221
-				$modules = $this->encryptionManager->getEncryptionModules();
222
-				$restorePossible = true;
223
-				foreach ($modules as $id => $module) {
224
-					/* @var IEncryptionModule $instance */
225
-					$instance = call_user_func($module['callback']);
226
-					if ($instance->needDetailedAccessList()) {
227
-						$restorePossible = false;
228
-						break;
229
-					}
230
-				}
231
-			}
232
-		} else {
233
-			// recovery is possible if encryption is disabled (plain files are
234
-			// available)
235
-			$restorePossible = true;
236
-		}
237
-
238
-		$subAdminGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user);
239
-		foreach ($subAdminGroups as $key => $subAdminGroup) {
240
-			$subAdminGroups[$key] = $subAdminGroup->getGID();
241
-		}
242
-
243
-		$displayName = $user->getEMailAddress();
244
-		if (is_null($displayName)) {
245
-			$displayName = '';
246
-		}
247
-
248
-		$avatarAvailable = false;
249
-		try {
250
-			$avatarAvailable = $this->avatarManager->getAvatar($user->getUID())->exists();
251
-		} catch (\Exception $e) {
252
-			//No avatar yet
253
-		}
254
-
255
-		return [
256
-			'name' => $user->getUID(),
257
-			'displayname' => $user->getDisplayName(),
258
-			'groups' => (empty($userGroups)) ? $this->groupManager->getUserGroupIds($user) : $userGroups,
259
-			'subadmin' => $subAdminGroups,
260
-			'quota' => $user->getQuota(),
261
-			'quota_bytes' => Util::computerFileSize($user->getQuota()),
262
-			'storageLocation' => $user->getHome(),
263
-			'lastLogin' => $user->getLastLogin() * 1000,
264
-			'backend' => $user->getBackendClassName(),
265
-			'email' => $displayName,
266
-			'isRestoreDisabled' => !$restorePossible,
267
-			'isAvatarAvailable' => $avatarAvailable,
268
-			'isEnabled' => $user->isEnabled(),
269
-		];
270
-	}
271
-
272
-	/**
273
-	 * @param array $userIDs Array with schema [$uid => $displayName]
274
-	 * @return IUser[]
275
-	 */
276
-	private function getUsersForUID(array $userIDs) {
277
-		$users = [];
278
-		foreach ($userIDs as $uid => $displayName) {
279
-			$users[$uid] = $this->userManager->get($uid);
280
-		}
281
-		return $users;
282
-	}
283
-
284
-	/**
285
-	 * @NoAdminRequired
286
-	 *
287
-	 * @param int $offset
288
-	 * @param int $limit
289
-	 * @param string $gid GID to filter for
290
-	 * @param string $pattern Pattern to search for in the username
291
-	 * @param string $backend Backend to filter for (class-name)
292
-	 * @return DataResponse
293
-	 *
294
-	 * TODO: Tidy up and write unit tests - code is mainly static method calls
295
-	 */
296
-	public function index($offset = 0, $limit = 10, $gid = '', $pattern = '', $backend = '') {
297
-		// Remove backends
298
-		if (!empty($backend)) {
299
-			$activeBackends = $this->userManager->getBackends();
300
-			$this->userManager->clearBackends();
301
-			foreach ($activeBackends as $singleActiveBackend) {
302
-				if ($backend === get_class($singleActiveBackend)) {
303
-					$this->userManager->registerBackend($singleActiveBackend);
304
-					break;
305
-				}
306
-			}
307
-		}
308
-
309
-		$userObjects = [];
310
-		$users = [];
311
-		if ($this->isAdmin) {
312
-			if ($gid !== '' && $gid !== '_disabledUsers' && $gid !== '_everyone') {
313
-				$batch = $this->getUsersForUID($this->groupManager->displayNamesInGroup($gid, $pattern, $limit, $offset));
314
-			} else {
315
-				$batch = $this->userManager->search($pattern, $limit, $offset);
316
-			}
317
-
318
-			foreach ($batch as $user) {
319
-				if (($gid !== '_disabledUsers' && $user->isEnabled()) ||
320
-					($gid === '_disabledUsers' && !$user->isEnabled())
321
-				) {
322
-					$userObjects[] = $user;
323
-					$users[] = $this->formatUserForIndex($user);
324
-				}
325
-			}
326
-
327
-		} else {
328
-			$subAdminOfGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($this->userSession->getUser());
329
-			// New class returns IGroup[] so convert back
330
-			$gids = [];
331
-			foreach ($subAdminOfGroups as $group) {
332
-				$gids[] = $group->getGID();
333
-			}
334
-			$subAdminOfGroups = $gids;
335
-
336
-			// Set the $gid parameter to an empty value if the subadmin has no rights to access a specific group
337
-			if ($gid !== '' && $gid !== '_disabledUsers' && !in_array($gid, $subAdminOfGroups)) {
338
-				$gid = '';
339
-			}
340
-
341
-			// Batch all groups the user is subadmin of when a group is specified
342
-			$batch = [];
343
-			if ($gid !== '' && $gid !== '_disabledUsers' && $gid !== '_everyone') {
344
-				$batch = $this->groupManager->displayNamesInGroup($gid, $pattern, $limit, $offset);
345
-			} else {
346
-				foreach ($subAdminOfGroups as $group) {
347
-					$groupUsers = $this->groupManager->displayNamesInGroup($group, $pattern, $limit, $offset);
348
-
349
-					foreach ($groupUsers as $uid => $displayName) {
350
-						$batch[$uid] = $displayName;
351
-					}
352
-				}
353
-			}
354
-			$batch = $this->getUsersForUID($batch);
355
-
356
-			foreach ($batch as $user) {
357
-				// Only add the groups, this user is a subadmin of
358
-				$userGroups = array_values(array_intersect(
359
-					$this->groupManager->getUserGroupIds($user),
360
-					$subAdminOfGroups
361
-				));
362
-				if (($gid !== '_disabledUsers' && $user->isEnabled()) ||
363
-					($gid === '_disabledUsers' && !$user->isEnabled())
364
-				) {
365
-					$userObjects[] = $user;
366
-					$users[] = $this->formatUserForIndex($user, $userGroups);
367
-				}
368
-			}
369
-		}
370
-
371
-		$usedSpace = $this->userMountCache->getUsedSpaceForUsers($userObjects);
372
-
373
-		foreach ($users as &$userData) {
374
-			$userData['size'] = isset($usedSpace[$userData['name']]) ? $usedSpace[$userData['name']] : 0;
375
-		}
376
-
377
-		return new DataResponse($users);
378
-	}
379
-
380
-	/**
381
-	 * @NoAdminRequired
382
-	 * @PasswordConfirmationRequired
383
-	 *
384
-	 * @param string $username
385
-	 * @param string $password
386
-	 * @param array $groups
387
-	 * @param string $email
388
-	 * @return DataResponse
389
-	 */
390
-	public function create($username, $password, array $groups = [], $email = '') {
391
-		if ($email !== '' && !$this->mailer->validateMailAddress($email)) {
392
-			return new DataResponse(
393
-				[
394
-					'message' => (string)$this->l10n->t('Invalid mail address')
395
-				],
396
-				Http::STATUS_UNPROCESSABLE_ENTITY
397
-			);
398
-		}
399
-
400
-		$currentUser = $this->userSession->getUser();
401
-
402
-		if (!$this->isAdmin) {
403
-			if (!empty($groups)) {
404
-				foreach ($groups as $key => $group) {
405
-					$groupObject = $this->groupManager->get($group);
406
-					if ($groupObject === null) {
407
-						unset($groups[$key]);
408
-						continue;
409
-					}
410
-
411
-					if (!$this->groupManager->getSubAdmin()->isSubAdminofGroup($currentUser, $groupObject)) {
412
-						unset($groups[$key]);
413
-					}
414
-				}
415
-			}
416
-
417
-			if (empty($groups)) {
418
-				return new DataResponse(
419
-					[
420
-						'message' => $this->l10n->t('No valid group selected'),
421
-					],
422
-					Http::STATUS_FORBIDDEN
423
-				);
424
-			}
425
-		}
426
-
427
-		if ($this->userManager->userExists($username)) {
428
-			return new DataResponse(
429
-				[
430
-					'message' => (string)$this->l10n->t('A user with that name already exists.')
431
-				],
432
-				Http::STATUS_CONFLICT
433
-			);
434
-		}
435
-
436
-		$generatePasswordResetToken = false;
437
-		if ($password === '') {
438
-			if ($email === '') {
439
-				return new DataResponse(
440
-					[
441
-						'message' => (string)$this->l10n->t('To send a password link to the user an email address is required.')
442
-					],
443
-					Http::STATUS_UNPROCESSABLE_ENTITY
444
-				);
445
-			}
446
-
447
-			$password = $this->secureRandom->generate(30);
448
-			// Make sure we pass the password_policy
449
-			$password .= $this->secureRandom->generate(2, '$!.,;:-~+*[]{}()');
450
-			$generatePasswordResetToken = true;
451
-		}
452
-
453
-		try {
454
-			$user = $this->userManager->createUser($username, $password);
455
-		} catch (\Exception $exception) {
456
-			$message = $exception->getMessage();
457
-			if ($exception instanceof HintException && $exception->getHint()) {
458
-				$message = $exception->getHint();
459
-			}
460
-			if (!$message) {
461
-				$message = $this->l10n->t('Unable to create user.');
462
-			}
463
-			return new DataResponse(
464
-				[
465
-					'message' => (string)$message,
466
-				],
467
-				Http::STATUS_FORBIDDEN
468
-			);
469
-		}
470
-
471
-		if ($user instanceof IUser) {
472
-			if ($groups !== null) {
473
-				foreach ($groups as $groupName) {
474
-					$group = $this->groupManager->get($groupName);
475
-
476
-					if (empty($group)) {
477
-						$group = $this->groupManager->createGroup($groupName);
478
-					}
479
-					$group->addUser($user);
480
-				}
481
-			}
482
-			/**
483
-			 * Send new user mail only if a mail is set
484
-			 */
485
-			if ($email !== '') {
486
-				$user->setEMailAddress($email);
487
-				try {
488
-					$emailTemplate = $this->newUserMailHelper->generateTemplate($user, $generatePasswordResetToken);
489
-					$this->newUserMailHelper->sendMail($user, $emailTemplate);
490
-				} catch (\Exception $e) {
491
-					$this->log->logException($e, [
492
-						'message' => "Can't send new user mail to $email",
493
-						'level' => \OCP\Util::ERROR,
494
-						'app' => 'settings',
495
-					]);
496
-				}
497
-			}
498
-			// fetch users groups
499
-			$userGroups = $this->groupManager->getUserGroupIds($user);
500
-
501
-			return new DataResponse(
502
-				$this->formatUserForIndex($user, $userGroups),
503
-				Http::STATUS_CREATED
504
-			);
505
-		}
506
-
507
-		return new DataResponse(
508
-			[
509
-				'message' => (string)$this->l10n->t('Unable to create user.')
510
-			],
511
-			Http::STATUS_FORBIDDEN
512
-		);
513
-
514
-	}
515
-
516
-	/**
517
-	 * @NoAdminRequired
518
-	 * @PasswordConfirmationRequired
519
-	 *
520
-	 * @param string $id
521
-	 * @return DataResponse
522
-	 */
523
-	public function destroy($id) {
524
-		$userId = $this->userSession->getUser()->getUID();
525
-		$user = $this->userManager->get($id);
526
-
527
-		if ($userId === $id) {
528
-			return new DataResponse(
529
-				[
530
-					'status' => 'error',
531
-					'data' => [
532
-						'message' => (string)$this->l10n->t('Unable to delete user.')
533
-					]
534
-				],
535
-				Http::STATUS_FORBIDDEN
536
-			);
537
-		}
538
-
539
-		if (!$this->isAdmin && !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)) {
540
-			return new DataResponse(
541
-				[
542
-					'status' => 'error',
543
-					'data' => [
544
-						'message' => (string)$this->l10n->t('Authentication error')
545
-					]
546
-				],
547
-				Http::STATUS_FORBIDDEN
548
-			);
549
-		}
550
-
551
-		if ($user) {
552
-			if ($user->delete()) {
553
-				return new DataResponse(
554
-					[
555
-						'status' => 'success',
556
-						'data' => [
557
-							'username' => $id
558
-						]
559
-					],
560
-					Http::STATUS_NO_CONTENT
561
-				);
562
-			}
563
-		}
564
-
565
-		return new DataResponse(
566
-			[
567
-				'status' => 'error',
568
-				'data' => [
569
-					'message' => (string)$this->l10n->t('Unable to delete user.')
570
-				]
571
-			],
572
-			Http::STATUS_FORBIDDEN
573
-		);
574
-	}
575
-
576
-	/**
577
-	 * @NoAdminRequired
578
-	 *
579
-	 * @param string $id
580
-	 * @param int $enabled
581
-	 * @return DataResponse
582
-	 */
583
-	public function setEnabled($id, $enabled) {
584
-		$enabled = (bool)$enabled;
585
-		if ($enabled) {
586
-			$errorMsgGeneral = (string)$this->l10n->t('Error while enabling user.');
587
-		} else {
588
-			$errorMsgGeneral = (string)$this->l10n->t('Error while disabling user.');
589
-		}
590
-
591
-		$userId = $this->userSession->getUser()->getUID();
592
-		$user = $this->userManager->get($id);
593
-
594
-		if ($userId === $id) {
595
-			return new DataResponse(
596
-				[
597
-					'status' => 'error',
598
-					'data' => [
599
-						'message' => $errorMsgGeneral
600
-					]
601
-				], Http::STATUS_FORBIDDEN
602
-			);
603
-		}
604
-
605
-		if ($user) {
606
-			if (!$this->isAdmin && !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)) {
607
-				return new DataResponse(
608
-					[
609
-						'status' => 'error',
610
-						'data' => [
611
-							'message' => (string)$this->l10n->t('Authentication error')
612
-						]
613
-					],
614
-					Http::STATUS_FORBIDDEN
615
-				);
616
-			}
617
-
618
-			$user->setEnabled($enabled);
619
-			return new DataResponse(
620
-				[
621
-					'status' => 'success',
622
-					'data' => [
623
-						'username' => $id,
624
-						'enabled' => $enabled
625
-					]
626
-				]
627
-			);
628
-		} else {
629
-			return new DataResponse(
630
-				[
631
-					'status' => 'error',
632
-					'data' => [
633
-						'message' => $errorMsgGeneral
634
-					]
635
-				],
636
-				Http::STATUS_FORBIDDEN
637
-			);
638
-		}
639
-
640
-	}
641
-
642
-	/**
643
-	 * Set the mail address of a user
644
-	 *
645
-	 * @NoAdminRequired
646
-	 * @NoSubadminRequired
647
-	 * @PasswordConfirmationRequired
648
-	 *
649
-	 * @param string $account
650
-	 * @param bool $onlyVerificationCode only return verification code without updating the data
651
-	 * @return DataResponse
652
-	 */
653
-	public function getVerificationCode($account, $onlyVerificationCode) {
654
-
655
-		$user = $this->userSession->getUser();
656
-
657
-		if ($user === null) {
658
-			return new DataResponse([], Http::STATUS_BAD_REQUEST);
659
-		}
660
-
661
-		$accountData = $this->accountManager->getUser($user);
662
-		$cloudId = $user->getCloudId();
663
-		$message = "Use my Federated Cloud ID to share with me: " . $cloudId;
664
-		$signature = $this->signMessage($user, $message);
665
-
666
-		$code = $message . ' ' . $signature;
667
-		$codeMd5 = $message . ' ' . md5($signature);
668
-
669
-		switch ($account) {
670
-			case 'verify-twitter':
671
-				$accountData[AccountManager::PROPERTY_TWITTER]['verified'] = AccountManager::VERIFICATION_IN_PROGRESS;
672
-				$msg = $this->l10n->t('In order to verify your Twitter account, post the following tweet on Twitter (please make sure to post it without any line breaks):');
673
-				$code = $codeMd5;
674
-				$type = AccountManager::PROPERTY_TWITTER;
675
-				$data = $accountData[AccountManager::PROPERTY_TWITTER]['value'];
676
-				$accountData[AccountManager::PROPERTY_TWITTER]['signature'] = $signature;
677
-				break;
678
-			case 'verify-website':
679
-				$accountData[AccountManager::PROPERTY_WEBSITE]['verified'] = AccountManager::VERIFICATION_IN_PROGRESS;
680
-				$msg = $this->l10n->t('In order to verify your Website, store the following content in your web-root at \'.well-known/CloudIdVerificationCode.txt\' (please make sure that the complete text is in one line):');
681
-				$type = AccountManager::PROPERTY_WEBSITE;
682
-				$data = $accountData[AccountManager::PROPERTY_WEBSITE]['value'];
683
-				$accountData[AccountManager::PROPERTY_WEBSITE]['signature'] = $signature;
684
-				break;
685
-			default:
686
-				return new DataResponse([], Http::STATUS_BAD_REQUEST);
687
-		}
688
-
689
-		if ($onlyVerificationCode === false) {
690
-			$this->accountManager->updateUser($user, $accountData);
691
-
692
-			$this->jobList->add(VerifyUserData::class,
693
-				[
694
-					'verificationCode' => $code,
695
-					'data' => $data,
696
-					'type' => $type,
697
-					'uid' => $user->getUID(),
698
-					'try' => 0,
699
-					'lastRun' => $this->getCurrentTime()
700
-				]
701
-			);
702
-		}
703
-
704
-		return new DataResponse(['msg' => $msg, 'code' => $code]);
705
-	}
706
-
707
-	/**
708
-	 * get current timestamp
709
-	 *
710
-	 * @return int
711
-	 */
712
-	protected function getCurrentTime() {
713
-		return time();
714
-	}
715
-
716
-	/**
717
-	 * sign message with users private key
718
-	 *
719
-	 * @param IUser $user
720
-	 * @param string $message
721
-	 *
722
-	 * @return string base64 encoded signature
723
-	 */
724
-	protected function signMessage(IUser $user, $message) {
725
-		$privateKey = $this->keyManager->getKey($user)->getPrivate();
726
-		openssl_sign(json_encode($message), $signature, $privateKey, OPENSSL_ALGO_SHA512);
727
-		$signatureBase64 = base64_encode($signature);
728
-
729
-		return $signatureBase64;
730
-	}
731
-
732
-	/**
733
-	 * @NoAdminRequired
734
-	 * @NoSubadminRequired
735
-	 * @PasswordConfirmationRequired
736
-	 *
737
-	 * @param string $avatarScope
738
-	 * @param string $displayname
739
-	 * @param string $displaynameScope
740
-	 * @param string $phone
741
-	 * @param string $phoneScope
742
-	 * @param string $email
743
-	 * @param string $emailScope
744
-	 * @param string $website
745
-	 * @param string $websiteScope
746
-	 * @param string $address
747
-	 * @param string $addressScope
748
-	 * @param string $twitter
749
-	 * @param string $twitterScope
750
-	 * @return DataResponse
751
-	 */
752
-	public function setUserSettings($avatarScope,
753
-									$displayname,
754
-									$displaynameScope,
755
-									$phone,
756
-									$phoneScope,
757
-									$email,
758
-									$emailScope,
759
-									$website,
760
-									$websiteScope,
761
-									$address,
762
-									$addressScope,
763
-									$twitter,
764
-									$twitterScope
765
-	) {
766
-
767
-		if (!empty($email) && !$this->mailer->validateMailAddress($email)) {
768
-			return new DataResponse(
769
-				[
770
-					'status' => 'error',
771
-					'data' => [
772
-						'message' => (string)$this->l10n->t('Invalid mail address')
773
-					]
774
-				],
775
-				Http::STATUS_UNPROCESSABLE_ENTITY
776
-			);
777
-		}
778
-
779
-		$user = $this->userSession->getUser();
780
-
781
-		$data = $this->accountManager->getUser($user);
782
-
783
-		$data[AccountManager::PROPERTY_AVATAR] = ['scope' => $avatarScope];
784
-		if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) {
785
-			$data[AccountManager::PROPERTY_DISPLAYNAME] = ['value' => $displayname, 'scope' => $displaynameScope];
786
-			$data[AccountManager::PROPERTY_EMAIL] = ['value' => $email, 'scope' => $emailScope];
787
-		}
788
-
789
-		if ($this->appManager->isEnabledForUser('federatedfilesharing')) {
790
-			$federatedFileSharing = new \OCA\FederatedFileSharing\AppInfo\Application();
791
-			$shareProvider = $federatedFileSharing->getFederatedShareProvider();
792
-			if ($shareProvider->isLookupServerUploadEnabled()) {
793
-				$data[AccountManager::PROPERTY_WEBSITE] = ['value' => $website, 'scope' => $websiteScope];
794
-				$data[AccountManager::PROPERTY_ADDRESS] = ['value' => $address, 'scope' => $addressScope];
795
-				$data[AccountManager::PROPERTY_PHONE] = ['value' => $phone, 'scope' => $phoneScope];
796
-				$data[AccountManager::PROPERTY_TWITTER] = ['value' => $twitter, 'scope' => $twitterScope];
797
-			}
798
-		}
799
-
800
-		try {
801
-			$this->saveUserSettings($user, $data);
802
-			return new DataResponse(
803
-				[
804
-					'status' => 'success',
805
-					'data' => [
806
-						'userId' => $user->getUID(),
807
-						'avatarScope' => $data[AccountManager::PROPERTY_AVATAR]['scope'],
808
-						'displayname' => $data[AccountManager::PROPERTY_DISPLAYNAME]['value'],
809
-						'displaynameScope' => $data[AccountManager::PROPERTY_DISPLAYNAME]['scope'],
810
-						'email' => $data[AccountManager::PROPERTY_EMAIL]['value'],
811
-						'emailScope' => $data[AccountManager::PROPERTY_EMAIL]['scope'],
812
-						'website' => $data[AccountManager::PROPERTY_WEBSITE]['value'],
813
-						'websiteScope' => $data[AccountManager::PROPERTY_WEBSITE]['scope'],
814
-						'address' => $data[AccountManager::PROPERTY_ADDRESS]['value'],
815
-						'addressScope' => $data[AccountManager::PROPERTY_ADDRESS]['scope'],
816
-						'message' => (string)$this->l10n->t('Settings saved')
817
-					]
818
-				],
819
-				Http::STATUS_OK
820
-			);
821
-		} catch (ForbiddenException $e) {
822
-			return new DataResponse([
823
-				'status' => 'error',
824
-				'data' => [
825
-					'message' => $e->getMessage()
826
-				],
827
-			]);
828
-		}
829
-
830
-	}
831
-
832
-
833
-	/**
834
-	 * update account manager with new user data
835
-	 *
836
-	 * @param IUser $user
837
-	 * @param array $data
838
-	 * @throws ForbiddenException
839
-	 */
840
-	protected function saveUserSettings(IUser $user, $data) {
841
-
842
-		// keep the user back-end up-to-date with the latest display name and email
843
-		// address
844
-		$oldDisplayName = $user->getDisplayName();
845
-		$oldDisplayName = is_null($oldDisplayName) ? '' : $oldDisplayName;
846
-		if (isset($data[AccountManager::PROPERTY_DISPLAYNAME]['value'])
847
-			&& $oldDisplayName !== $data[AccountManager::PROPERTY_DISPLAYNAME]['value']
848
-		) {
849
-			$result = $user->setDisplayName($data[AccountManager::PROPERTY_DISPLAYNAME]['value']);
850
-			if ($result === false) {
851
-				throw new ForbiddenException($this->l10n->t('Unable to change full name'));
852
-			}
853
-		}
854
-
855
-		$oldEmailAddress = $user->getEMailAddress();
856
-		$oldEmailAddress = is_null($oldEmailAddress) ? '' : $oldEmailAddress;
857
-		if (isset($data[AccountManager::PROPERTY_EMAIL]['value'])
858
-			&& $oldEmailAddress !== $data[AccountManager::PROPERTY_EMAIL]['value']
859
-		) {
860
-			// this is the only permission a backend provides and is also used
861
-			// for the permission of setting a email address
862
-			if (!$user->canChangeDisplayName()) {
863
-				throw new ForbiddenException($this->l10n->t('Unable to change email address'));
864
-			}
865
-			$user->setEMailAddress($data[AccountManager::PROPERTY_EMAIL]['value']);
866
-		}
867
-
868
-		$this->accountManager->updateUser($user, $data);
869
-	}
870
-
871
-	/**
872
-	 * Count all unique users visible for the current admin/subadmin.
873
-	 *
874
-	 * @NoAdminRequired
875
-	 *
876
-	 * @return DataResponse
877
-	 */
878
-	public function stats() {
879
-		$userCount = 0;
880
-		if ($this->isAdmin) {
881
-			$countByBackend = $this->userManager->countUsers();
882
-
883
-			if (!empty($countByBackend)) {
884
-				foreach ($countByBackend as $count) {
885
-					$userCount += $count;
886
-				}
887
-			}
888
-		} else {
889
-			$groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($this->userSession->getUser());
890
-
891
-			$uniqueUsers = [];
892
-			foreach ($groups as $group) {
893
-				foreach ($group->getUsers() as $uid => $displayName) {
894
-					$uniqueUsers[$uid] = true;
895
-				}
896
-			}
897
-
898
-			$userCount = count($uniqueUsers);
899
-		}
900
-
901
-		return new DataResponse(
902
-			[
903
-				'totalUsers' => $userCount
904
-			]
905
-		);
906
-	}
907
-
908
-
909
-	/**
910
-	 * Set the displayName of a user
911
-	 *
912
-	 * @NoAdminRequired
913
-	 * @NoSubadminRequired
914
-	 * @PasswordConfirmationRequired
915
-	 * @todo merge into saveUserSettings
916
-	 *
917
-	 * @param string $username
918
-	 * @param string $displayName
919
-	 * @return DataResponse
920
-	 */
921
-	public function setDisplayName($username, $displayName) {
922
-		$currentUser = $this->userSession->getUser();
923
-		$user = $this->userManager->get($username);
924
-
925
-		if ($user === null ||
926
-			!$user->canChangeDisplayName() ||
927
-			(
928
-				!$this->groupManager->isAdmin($currentUser->getUID()) &&
929
-				!$this->groupManager->getSubAdmin()->isUserAccessible($currentUser, $user) &&
930
-				$currentUser->getUID() !== $username
931
-
932
-			)
933
-		) {
934
-			return new DataResponse([
935
-				'status' => 'error',
936
-				'data' => [
937
-					'message' => $this->l10n->t('Authentication error'),
938
-				],
939
-			]);
940
-		}
941
-
942
-		$userData = $this->accountManager->getUser($user);
943
-		$userData[AccountManager::PROPERTY_DISPLAYNAME]['value'] = $displayName;
944
-
945
-
946
-		try {
947
-			$this->saveUserSettings($user, $userData);
948
-			return new DataResponse([
949
-				'status' => 'success',
950
-				'data' => [
951
-					'message' => $this->l10n->t('Your full name has been changed.'),
952
-					'username' => $username,
953
-					'displayName' => $displayName,
954
-				],
955
-			]);
956
-		} catch (ForbiddenException $e) {
957
-			return new DataResponse([
958
-				'status' => 'error',
959
-				'data' => [
960
-					'message' => $e->getMessage(),
961
-					'displayName' => $user->getDisplayName(),
962
-				],
963
-			]);
964
-		}
965
-	}
966
-
967
-	/**
968
-	 * Set the mail address of a user
969
-	 *
970
-	 * @NoAdminRequired
971
-	 * @NoSubadminRequired
972
-	 * @PasswordConfirmationRequired
973
-	 *
974
-	 * @param string $id
975
-	 * @param string $mailAddress
976
-	 * @return DataResponse
977
-	 */
978
-	public function setEMailAddress($id, $mailAddress) {
979
-		$user = $this->userManager->get($id);
980
-		if (!$this->isAdmin
981
-			&& !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)
982
-		) {
983
-			return new DataResponse(
984
-				[
985
-					'status' => 'error',
986
-					'data' => [
987
-						'message' => (string)$this->l10n->t('Forbidden')
988
-					]
989
-				],
990
-				Http::STATUS_FORBIDDEN
991
-			);
992
-		}
993
-
994
-		if ($mailAddress !== '' && !$this->mailer->validateMailAddress($mailAddress)) {
995
-			return new DataResponse(
996
-				[
997
-					'status' => 'error',
998
-					'data' => [
999
-						'message' => (string)$this->l10n->t('Invalid mail address')
1000
-					]
1001
-				],
1002
-				Http::STATUS_UNPROCESSABLE_ENTITY
1003
-			);
1004
-		}
1005
-
1006
-		if (!$user) {
1007
-			return new DataResponse(
1008
-				[
1009
-					'status' => 'error',
1010
-					'data' => [
1011
-						'message' => (string)$this->l10n->t('Invalid user')
1012
-					]
1013
-				],
1014
-				Http::STATUS_UNPROCESSABLE_ENTITY
1015
-			);
1016
-		}
1017
-		// this is the only permission a backend provides and is also used
1018
-		// for the permission of setting a email address
1019
-		if (!$user->canChangeDisplayName()) {
1020
-			return new DataResponse(
1021
-				[
1022
-					'status' => 'error',
1023
-					'data' => [
1024
-						'message' => (string)$this->l10n->t('Unable to change mail address')
1025
-					]
1026
-				],
1027
-				Http::STATUS_FORBIDDEN
1028
-			);
1029
-		}
1030
-
1031
-		$userData = $this->accountManager->getUser($user);
1032
-		$userData[AccountManager::PROPERTY_EMAIL]['value'] = $mailAddress;
1033
-
1034
-		try {
1035
-			$this->saveUserSettings($user, $userData);
1036
-			return new DataResponse(
1037
-				[
1038
-					'status' => 'success',
1039
-					'data' => [
1040
-						'username' => $id,
1041
-						'mailAddress' => $mailAddress,
1042
-						'message' => (string)$this->l10n->t('Email saved')
1043
-					]
1044
-				],
1045
-				Http::STATUS_OK
1046
-			);
1047
-		} catch (ForbiddenException $e) {
1048
-			return new DataResponse([
1049
-				'status' => 'error',
1050
-				'data' => [
1051
-					'message' => $e->getMessage()
1052
-				],
1053
-			]);
1054
-		}
1055
-	}
73
+    /** @var IL10N */
74
+    private $l10n;
75
+    /** @var IUserSession */
76
+    private $userSession;
77
+    /** @var bool */
78
+    private $isAdmin;
79
+    /** @var IUserManager */
80
+    private $userManager;
81
+    /** @var IGroupManager */
82
+    private $groupManager;
83
+    /** @var IConfig */
84
+    private $config;
85
+    /** @var ILogger */
86
+    private $log;
87
+    /** @var IMailer */
88
+    private $mailer;
89
+    /** @var bool contains the state of the encryption app */
90
+    private $isEncryptionAppEnabled;
91
+    /** @var bool contains the state of the admin recovery setting */
92
+    private $isRestoreEnabled = false;
93
+    /** @var IAppManager */
94
+    private $appManager;
95
+    /** @var IAvatarManager */
96
+    private $avatarManager;
97
+    /** @var AccountManager */
98
+    private $accountManager;
99
+    /** @var ISecureRandom */
100
+    private $secureRandom;
101
+    /** @var NewUserMailHelper */
102
+    private $newUserMailHelper;
103
+    /** @var ITimeFactory */
104
+    private $timeFactory;
105
+    /** @var ICrypto */
106
+    private $crypto;
107
+    /** @var Manager */
108
+    private $keyManager;
109
+    /** @var IJobList */
110
+    private $jobList;
111
+
112
+    /** @var IUserMountCache */
113
+    private $userMountCache;
114
+
115
+    /** @var IManager */
116
+    private $encryptionManager;
117
+
118
+
119
+    /**
120
+     * @param string $appName
121
+     * @param IRequest $request
122
+     * @param IUserManager $userManager
123
+     * @param IGroupManager $groupManager
124
+     * @param IUserSession $userSession
125
+     * @param IConfig $config
126
+     * @param bool $isAdmin
127
+     * @param IL10N $l10n
128
+     * @param ILogger $log
129
+     * @param IMailer $mailer
130
+     * @param IURLGenerator $urlGenerator
131
+     * @param IAppManager $appManager
132
+     * @param IAvatarManager $avatarManager
133
+     * @param AccountManager $accountManager
134
+     * @param ISecureRandom $secureRandom
135
+     * @param NewUserMailHelper $newUserMailHelper
136
+     * @param ITimeFactory $timeFactory
137
+     * @param ICrypto $crypto
138
+     * @param Manager $keyManager
139
+     * @param IJobList $jobList
140
+     * @param IUserMountCache $userMountCache
141
+     * @param IManager $encryptionManager
142
+     */
143
+    public function __construct($appName,
144
+                                IRequest $request,
145
+                                IUserManager $userManager,
146
+                                IGroupManager $groupManager,
147
+                                IUserSession $userSession,
148
+                                IConfig $config,
149
+                                $isAdmin,
150
+                                IL10N $l10n,
151
+                                ILogger $log,
152
+                                IMailer $mailer,
153
+                                IURLGenerator $urlGenerator,
154
+                                IAppManager $appManager,
155
+                                IAvatarManager $avatarManager,
156
+                                AccountManager $accountManager,
157
+                                ISecureRandom $secureRandom,
158
+                                NewUserMailHelper $newUserMailHelper,
159
+                                ITimeFactory $timeFactory,
160
+                                ICrypto $crypto,
161
+                                Manager $keyManager,
162
+                                IJobList $jobList,
163
+                                IUserMountCache $userMountCache,
164
+                                IManager $encryptionManager) {
165
+        parent::__construct($appName, $request);
166
+        $this->userManager = $userManager;
167
+        $this->groupManager = $groupManager;
168
+        $this->userSession = $userSession;
169
+        $this->config = $config;
170
+        $this->isAdmin = $isAdmin;
171
+        $this->l10n = $l10n;
172
+        $this->log = $log;
173
+        $this->mailer = $mailer;
174
+        $this->appManager = $appManager;
175
+        $this->avatarManager = $avatarManager;
176
+        $this->accountManager = $accountManager;
177
+        $this->secureRandom = $secureRandom;
178
+        $this->newUserMailHelper = $newUserMailHelper;
179
+        $this->timeFactory = $timeFactory;
180
+        $this->crypto = $crypto;
181
+        $this->keyManager = $keyManager;
182
+        $this->jobList = $jobList;
183
+        $this->userMountCache = $userMountCache;
184
+        $this->encryptionManager = $encryptionManager;
185
+
186
+        // check for encryption state - TODO see formatUserForIndex
187
+        $this->isEncryptionAppEnabled = $appManager->isEnabledForUser('encryption');
188
+        if ($this->isEncryptionAppEnabled) {
189
+            // putting this directly in empty is possible in PHP 5.5+
190
+            $result = $config->getAppValue('encryption', 'recoveryAdminEnabled', '0');
191
+            $this->isRestoreEnabled = !empty($result);
192
+        }
193
+    }
194
+
195
+    /**
196
+     * @param IUser $user
197
+     * @param array|null $userGroups
198
+     * @return array
199
+     */
200
+    private function formatUserForIndex(IUser $user, array $userGroups = null) {
201
+
202
+        // TODO: eliminate this encryption specific code below and somehow
203
+        // hook in additional user info from other apps
204
+
205
+        // recovery isn't possible if admin or user has it disabled and encryption
206
+        // is enabled - so we eliminate the else paths in the conditional tree
207
+        // below
208
+        $restorePossible = false;
209
+
210
+        if ($this->isEncryptionAppEnabled) {
211
+            if ($this->isRestoreEnabled) {
212
+                // check for the users recovery setting
213
+                $recoveryMode = $this->config->getUserValue($user->getUID(), 'encryption', 'recoveryEnabled', '0');
214
+                // method call inside empty is possible with PHP 5.5+
215
+                $recoveryModeEnabled = !empty($recoveryMode);
216
+                if ($recoveryModeEnabled) {
217
+                    // user also has recovery mode enabled
218
+                    $restorePossible = true;
219
+                }
220
+            } else {
221
+                $modules = $this->encryptionManager->getEncryptionModules();
222
+                $restorePossible = true;
223
+                foreach ($modules as $id => $module) {
224
+                    /* @var IEncryptionModule $instance */
225
+                    $instance = call_user_func($module['callback']);
226
+                    if ($instance->needDetailedAccessList()) {
227
+                        $restorePossible = false;
228
+                        break;
229
+                    }
230
+                }
231
+            }
232
+        } else {
233
+            // recovery is possible if encryption is disabled (plain files are
234
+            // available)
235
+            $restorePossible = true;
236
+        }
237
+
238
+        $subAdminGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user);
239
+        foreach ($subAdminGroups as $key => $subAdminGroup) {
240
+            $subAdminGroups[$key] = $subAdminGroup->getGID();
241
+        }
242
+
243
+        $displayName = $user->getEMailAddress();
244
+        if (is_null($displayName)) {
245
+            $displayName = '';
246
+        }
247
+
248
+        $avatarAvailable = false;
249
+        try {
250
+            $avatarAvailable = $this->avatarManager->getAvatar($user->getUID())->exists();
251
+        } catch (\Exception $e) {
252
+            //No avatar yet
253
+        }
254
+
255
+        return [
256
+            'name' => $user->getUID(),
257
+            'displayname' => $user->getDisplayName(),
258
+            'groups' => (empty($userGroups)) ? $this->groupManager->getUserGroupIds($user) : $userGroups,
259
+            'subadmin' => $subAdminGroups,
260
+            'quota' => $user->getQuota(),
261
+            'quota_bytes' => Util::computerFileSize($user->getQuota()),
262
+            'storageLocation' => $user->getHome(),
263
+            'lastLogin' => $user->getLastLogin() * 1000,
264
+            'backend' => $user->getBackendClassName(),
265
+            'email' => $displayName,
266
+            'isRestoreDisabled' => !$restorePossible,
267
+            'isAvatarAvailable' => $avatarAvailable,
268
+            'isEnabled' => $user->isEnabled(),
269
+        ];
270
+    }
271
+
272
+    /**
273
+     * @param array $userIDs Array with schema [$uid => $displayName]
274
+     * @return IUser[]
275
+     */
276
+    private function getUsersForUID(array $userIDs) {
277
+        $users = [];
278
+        foreach ($userIDs as $uid => $displayName) {
279
+            $users[$uid] = $this->userManager->get($uid);
280
+        }
281
+        return $users;
282
+    }
283
+
284
+    /**
285
+     * @NoAdminRequired
286
+     *
287
+     * @param int $offset
288
+     * @param int $limit
289
+     * @param string $gid GID to filter for
290
+     * @param string $pattern Pattern to search for in the username
291
+     * @param string $backend Backend to filter for (class-name)
292
+     * @return DataResponse
293
+     *
294
+     * TODO: Tidy up and write unit tests - code is mainly static method calls
295
+     */
296
+    public function index($offset = 0, $limit = 10, $gid = '', $pattern = '', $backend = '') {
297
+        // Remove backends
298
+        if (!empty($backend)) {
299
+            $activeBackends = $this->userManager->getBackends();
300
+            $this->userManager->clearBackends();
301
+            foreach ($activeBackends as $singleActiveBackend) {
302
+                if ($backend === get_class($singleActiveBackend)) {
303
+                    $this->userManager->registerBackend($singleActiveBackend);
304
+                    break;
305
+                }
306
+            }
307
+        }
308
+
309
+        $userObjects = [];
310
+        $users = [];
311
+        if ($this->isAdmin) {
312
+            if ($gid !== '' && $gid !== '_disabledUsers' && $gid !== '_everyone') {
313
+                $batch = $this->getUsersForUID($this->groupManager->displayNamesInGroup($gid, $pattern, $limit, $offset));
314
+            } else {
315
+                $batch = $this->userManager->search($pattern, $limit, $offset);
316
+            }
317
+
318
+            foreach ($batch as $user) {
319
+                if (($gid !== '_disabledUsers' && $user->isEnabled()) ||
320
+                    ($gid === '_disabledUsers' && !$user->isEnabled())
321
+                ) {
322
+                    $userObjects[] = $user;
323
+                    $users[] = $this->formatUserForIndex($user);
324
+                }
325
+            }
326
+
327
+        } else {
328
+            $subAdminOfGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($this->userSession->getUser());
329
+            // New class returns IGroup[] so convert back
330
+            $gids = [];
331
+            foreach ($subAdminOfGroups as $group) {
332
+                $gids[] = $group->getGID();
333
+            }
334
+            $subAdminOfGroups = $gids;
335
+
336
+            // Set the $gid parameter to an empty value if the subadmin has no rights to access a specific group
337
+            if ($gid !== '' && $gid !== '_disabledUsers' && !in_array($gid, $subAdminOfGroups)) {
338
+                $gid = '';
339
+            }
340
+
341
+            // Batch all groups the user is subadmin of when a group is specified
342
+            $batch = [];
343
+            if ($gid !== '' && $gid !== '_disabledUsers' && $gid !== '_everyone') {
344
+                $batch = $this->groupManager->displayNamesInGroup($gid, $pattern, $limit, $offset);
345
+            } else {
346
+                foreach ($subAdminOfGroups as $group) {
347
+                    $groupUsers = $this->groupManager->displayNamesInGroup($group, $pattern, $limit, $offset);
348
+
349
+                    foreach ($groupUsers as $uid => $displayName) {
350
+                        $batch[$uid] = $displayName;
351
+                    }
352
+                }
353
+            }
354
+            $batch = $this->getUsersForUID($batch);
355
+
356
+            foreach ($batch as $user) {
357
+                // Only add the groups, this user is a subadmin of
358
+                $userGroups = array_values(array_intersect(
359
+                    $this->groupManager->getUserGroupIds($user),
360
+                    $subAdminOfGroups
361
+                ));
362
+                if (($gid !== '_disabledUsers' && $user->isEnabled()) ||
363
+                    ($gid === '_disabledUsers' && !$user->isEnabled())
364
+                ) {
365
+                    $userObjects[] = $user;
366
+                    $users[] = $this->formatUserForIndex($user, $userGroups);
367
+                }
368
+            }
369
+        }
370
+
371
+        $usedSpace = $this->userMountCache->getUsedSpaceForUsers($userObjects);
372
+
373
+        foreach ($users as &$userData) {
374
+            $userData['size'] = isset($usedSpace[$userData['name']]) ? $usedSpace[$userData['name']] : 0;
375
+        }
376
+
377
+        return new DataResponse($users);
378
+    }
379
+
380
+    /**
381
+     * @NoAdminRequired
382
+     * @PasswordConfirmationRequired
383
+     *
384
+     * @param string $username
385
+     * @param string $password
386
+     * @param array $groups
387
+     * @param string $email
388
+     * @return DataResponse
389
+     */
390
+    public function create($username, $password, array $groups = [], $email = '') {
391
+        if ($email !== '' && !$this->mailer->validateMailAddress($email)) {
392
+            return new DataResponse(
393
+                [
394
+                    'message' => (string)$this->l10n->t('Invalid mail address')
395
+                ],
396
+                Http::STATUS_UNPROCESSABLE_ENTITY
397
+            );
398
+        }
399
+
400
+        $currentUser = $this->userSession->getUser();
401
+
402
+        if (!$this->isAdmin) {
403
+            if (!empty($groups)) {
404
+                foreach ($groups as $key => $group) {
405
+                    $groupObject = $this->groupManager->get($group);
406
+                    if ($groupObject === null) {
407
+                        unset($groups[$key]);
408
+                        continue;
409
+                    }
410
+
411
+                    if (!$this->groupManager->getSubAdmin()->isSubAdminofGroup($currentUser, $groupObject)) {
412
+                        unset($groups[$key]);
413
+                    }
414
+                }
415
+            }
416
+
417
+            if (empty($groups)) {
418
+                return new DataResponse(
419
+                    [
420
+                        'message' => $this->l10n->t('No valid group selected'),
421
+                    ],
422
+                    Http::STATUS_FORBIDDEN
423
+                );
424
+            }
425
+        }
426
+
427
+        if ($this->userManager->userExists($username)) {
428
+            return new DataResponse(
429
+                [
430
+                    'message' => (string)$this->l10n->t('A user with that name already exists.')
431
+                ],
432
+                Http::STATUS_CONFLICT
433
+            );
434
+        }
435
+
436
+        $generatePasswordResetToken = false;
437
+        if ($password === '') {
438
+            if ($email === '') {
439
+                return new DataResponse(
440
+                    [
441
+                        'message' => (string)$this->l10n->t('To send a password link to the user an email address is required.')
442
+                    ],
443
+                    Http::STATUS_UNPROCESSABLE_ENTITY
444
+                );
445
+            }
446
+
447
+            $password = $this->secureRandom->generate(30);
448
+            // Make sure we pass the password_policy
449
+            $password .= $this->secureRandom->generate(2, '$!.,;:-~+*[]{}()');
450
+            $generatePasswordResetToken = true;
451
+        }
452
+
453
+        try {
454
+            $user = $this->userManager->createUser($username, $password);
455
+        } catch (\Exception $exception) {
456
+            $message = $exception->getMessage();
457
+            if ($exception instanceof HintException && $exception->getHint()) {
458
+                $message = $exception->getHint();
459
+            }
460
+            if (!$message) {
461
+                $message = $this->l10n->t('Unable to create user.');
462
+            }
463
+            return new DataResponse(
464
+                [
465
+                    'message' => (string)$message,
466
+                ],
467
+                Http::STATUS_FORBIDDEN
468
+            );
469
+        }
470
+
471
+        if ($user instanceof IUser) {
472
+            if ($groups !== null) {
473
+                foreach ($groups as $groupName) {
474
+                    $group = $this->groupManager->get($groupName);
475
+
476
+                    if (empty($group)) {
477
+                        $group = $this->groupManager->createGroup($groupName);
478
+                    }
479
+                    $group->addUser($user);
480
+                }
481
+            }
482
+            /**
483
+             * Send new user mail only if a mail is set
484
+             */
485
+            if ($email !== '') {
486
+                $user->setEMailAddress($email);
487
+                try {
488
+                    $emailTemplate = $this->newUserMailHelper->generateTemplate($user, $generatePasswordResetToken);
489
+                    $this->newUserMailHelper->sendMail($user, $emailTemplate);
490
+                } catch (\Exception $e) {
491
+                    $this->log->logException($e, [
492
+                        'message' => "Can't send new user mail to $email",
493
+                        'level' => \OCP\Util::ERROR,
494
+                        'app' => 'settings',
495
+                    ]);
496
+                }
497
+            }
498
+            // fetch users groups
499
+            $userGroups = $this->groupManager->getUserGroupIds($user);
500
+
501
+            return new DataResponse(
502
+                $this->formatUserForIndex($user, $userGroups),
503
+                Http::STATUS_CREATED
504
+            );
505
+        }
506
+
507
+        return new DataResponse(
508
+            [
509
+                'message' => (string)$this->l10n->t('Unable to create user.')
510
+            ],
511
+            Http::STATUS_FORBIDDEN
512
+        );
513
+
514
+    }
515
+
516
+    /**
517
+     * @NoAdminRequired
518
+     * @PasswordConfirmationRequired
519
+     *
520
+     * @param string $id
521
+     * @return DataResponse
522
+     */
523
+    public function destroy($id) {
524
+        $userId = $this->userSession->getUser()->getUID();
525
+        $user = $this->userManager->get($id);
526
+
527
+        if ($userId === $id) {
528
+            return new DataResponse(
529
+                [
530
+                    'status' => 'error',
531
+                    'data' => [
532
+                        'message' => (string)$this->l10n->t('Unable to delete user.')
533
+                    ]
534
+                ],
535
+                Http::STATUS_FORBIDDEN
536
+            );
537
+        }
538
+
539
+        if (!$this->isAdmin && !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)) {
540
+            return new DataResponse(
541
+                [
542
+                    'status' => 'error',
543
+                    'data' => [
544
+                        'message' => (string)$this->l10n->t('Authentication error')
545
+                    ]
546
+                ],
547
+                Http::STATUS_FORBIDDEN
548
+            );
549
+        }
550
+
551
+        if ($user) {
552
+            if ($user->delete()) {
553
+                return new DataResponse(
554
+                    [
555
+                        'status' => 'success',
556
+                        'data' => [
557
+                            'username' => $id
558
+                        ]
559
+                    ],
560
+                    Http::STATUS_NO_CONTENT
561
+                );
562
+            }
563
+        }
564
+
565
+        return new DataResponse(
566
+            [
567
+                'status' => 'error',
568
+                'data' => [
569
+                    'message' => (string)$this->l10n->t('Unable to delete user.')
570
+                ]
571
+            ],
572
+            Http::STATUS_FORBIDDEN
573
+        );
574
+    }
575
+
576
+    /**
577
+     * @NoAdminRequired
578
+     *
579
+     * @param string $id
580
+     * @param int $enabled
581
+     * @return DataResponse
582
+     */
583
+    public function setEnabled($id, $enabled) {
584
+        $enabled = (bool)$enabled;
585
+        if ($enabled) {
586
+            $errorMsgGeneral = (string)$this->l10n->t('Error while enabling user.');
587
+        } else {
588
+            $errorMsgGeneral = (string)$this->l10n->t('Error while disabling user.');
589
+        }
590
+
591
+        $userId = $this->userSession->getUser()->getUID();
592
+        $user = $this->userManager->get($id);
593
+
594
+        if ($userId === $id) {
595
+            return new DataResponse(
596
+                [
597
+                    'status' => 'error',
598
+                    'data' => [
599
+                        'message' => $errorMsgGeneral
600
+                    ]
601
+                ], Http::STATUS_FORBIDDEN
602
+            );
603
+        }
604
+
605
+        if ($user) {
606
+            if (!$this->isAdmin && !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)) {
607
+                return new DataResponse(
608
+                    [
609
+                        'status' => 'error',
610
+                        'data' => [
611
+                            'message' => (string)$this->l10n->t('Authentication error')
612
+                        ]
613
+                    ],
614
+                    Http::STATUS_FORBIDDEN
615
+                );
616
+            }
617
+
618
+            $user->setEnabled($enabled);
619
+            return new DataResponse(
620
+                [
621
+                    'status' => 'success',
622
+                    'data' => [
623
+                        'username' => $id,
624
+                        'enabled' => $enabled
625
+                    ]
626
+                ]
627
+            );
628
+        } else {
629
+            return new DataResponse(
630
+                [
631
+                    'status' => 'error',
632
+                    'data' => [
633
+                        'message' => $errorMsgGeneral
634
+                    ]
635
+                ],
636
+                Http::STATUS_FORBIDDEN
637
+            );
638
+        }
639
+
640
+    }
641
+
642
+    /**
643
+     * Set the mail address of a user
644
+     *
645
+     * @NoAdminRequired
646
+     * @NoSubadminRequired
647
+     * @PasswordConfirmationRequired
648
+     *
649
+     * @param string $account
650
+     * @param bool $onlyVerificationCode only return verification code without updating the data
651
+     * @return DataResponse
652
+     */
653
+    public function getVerificationCode($account, $onlyVerificationCode) {
654
+
655
+        $user = $this->userSession->getUser();
656
+
657
+        if ($user === null) {
658
+            return new DataResponse([], Http::STATUS_BAD_REQUEST);
659
+        }
660
+
661
+        $accountData = $this->accountManager->getUser($user);
662
+        $cloudId = $user->getCloudId();
663
+        $message = "Use my Federated Cloud ID to share with me: " . $cloudId;
664
+        $signature = $this->signMessage($user, $message);
665
+
666
+        $code = $message . ' ' . $signature;
667
+        $codeMd5 = $message . ' ' . md5($signature);
668
+
669
+        switch ($account) {
670
+            case 'verify-twitter':
671
+                $accountData[AccountManager::PROPERTY_TWITTER]['verified'] = AccountManager::VERIFICATION_IN_PROGRESS;
672
+                $msg = $this->l10n->t('In order to verify your Twitter account, post the following tweet on Twitter (please make sure to post it without any line breaks):');
673
+                $code = $codeMd5;
674
+                $type = AccountManager::PROPERTY_TWITTER;
675
+                $data = $accountData[AccountManager::PROPERTY_TWITTER]['value'];
676
+                $accountData[AccountManager::PROPERTY_TWITTER]['signature'] = $signature;
677
+                break;
678
+            case 'verify-website':
679
+                $accountData[AccountManager::PROPERTY_WEBSITE]['verified'] = AccountManager::VERIFICATION_IN_PROGRESS;
680
+                $msg = $this->l10n->t('In order to verify your Website, store the following content in your web-root at \'.well-known/CloudIdVerificationCode.txt\' (please make sure that the complete text is in one line):');
681
+                $type = AccountManager::PROPERTY_WEBSITE;
682
+                $data = $accountData[AccountManager::PROPERTY_WEBSITE]['value'];
683
+                $accountData[AccountManager::PROPERTY_WEBSITE]['signature'] = $signature;
684
+                break;
685
+            default:
686
+                return new DataResponse([], Http::STATUS_BAD_REQUEST);
687
+        }
688
+
689
+        if ($onlyVerificationCode === false) {
690
+            $this->accountManager->updateUser($user, $accountData);
691
+
692
+            $this->jobList->add(VerifyUserData::class,
693
+                [
694
+                    'verificationCode' => $code,
695
+                    'data' => $data,
696
+                    'type' => $type,
697
+                    'uid' => $user->getUID(),
698
+                    'try' => 0,
699
+                    'lastRun' => $this->getCurrentTime()
700
+                ]
701
+            );
702
+        }
703
+
704
+        return new DataResponse(['msg' => $msg, 'code' => $code]);
705
+    }
706
+
707
+    /**
708
+     * get current timestamp
709
+     *
710
+     * @return int
711
+     */
712
+    protected function getCurrentTime() {
713
+        return time();
714
+    }
715
+
716
+    /**
717
+     * sign message with users private key
718
+     *
719
+     * @param IUser $user
720
+     * @param string $message
721
+     *
722
+     * @return string base64 encoded signature
723
+     */
724
+    protected function signMessage(IUser $user, $message) {
725
+        $privateKey = $this->keyManager->getKey($user)->getPrivate();
726
+        openssl_sign(json_encode($message), $signature, $privateKey, OPENSSL_ALGO_SHA512);
727
+        $signatureBase64 = base64_encode($signature);
728
+
729
+        return $signatureBase64;
730
+    }
731
+
732
+    /**
733
+     * @NoAdminRequired
734
+     * @NoSubadminRequired
735
+     * @PasswordConfirmationRequired
736
+     *
737
+     * @param string $avatarScope
738
+     * @param string $displayname
739
+     * @param string $displaynameScope
740
+     * @param string $phone
741
+     * @param string $phoneScope
742
+     * @param string $email
743
+     * @param string $emailScope
744
+     * @param string $website
745
+     * @param string $websiteScope
746
+     * @param string $address
747
+     * @param string $addressScope
748
+     * @param string $twitter
749
+     * @param string $twitterScope
750
+     * @return DataResponse
751
+     */
752
+    public function setUserSettings($avatarScope,
753
+                                    $displayname,
754
+                                    $displaynameScope,
755
+                                    $phone,
756
+                                    $phoneScope,
757
+                                    $email,
758
+                                    $emailScope,
759
+                                    $website,
760
+                                    $websiteScope,
761
+                                    $address,
762
+                                    $addressScope,
763
+                                    $twitter,
764
+                                    $twitterScope
765
+    ) {
766
+
767
+        if (!empty($email) && !$this->mailer->validateMailAddress($email)) {
768
+            return new DataResponse(
769
+                [
770
+                    'status' => 'error',
771
+                    'data' => [
772
+                        'message' => (string)$this->l10n->t('Invalid mail address')
773
+                    ]
774
+                ],
775
+                Http::STATUS_UNPROCESSABLE_ENTITY
776
+            );
777
+        }
778
+
779
+        $user = $this->userSession->getUser();
780
+
781
+        $data = $this->accountManager->getUser($user);
782
+
783
+        $data[AccountManager::PROPERTY_AVATAR] = ['scope' => $avatarScope];
784
+        if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) {
785
+            $data[AccountManager::PROPERTY_DISPLAYNAME] = ['value' => $displayname, 'scope' => $displaynameScope];
786
+            $data[AccountManager::PROPERTY_EMAIL] = ['value' => $email, 'scope' => $emailScope];
787
+        }
788
+
789
+        if ($this->appManager->isEnabledForUser('federatedfilesharing')) {
790
+            $federatedFileSharing = new \OCA\FederatedFileSharing\AppInfo\Application();
791
+            $shareProvider = $federatedFileSharing->getFederatedShareProvider();
792
+            if ($shareProvider->isLookupServerUploadEnabled()) {
793
+                $data[AccountManager::PROPERTY_WEBSITE] = ['value' => $website, 'scope' => $websiteScope];
794
+                $data[AccountManager::PROPERTY_ADDRESS] = ['value' => $address, 'scope' => $addressScope];
795
+                $data[AccountManager::PROPERTY_PHONE] = ['value' => $phone, 'scope' => $phoneScope];
796
+                $data[AccountManager::PROPERTY_TWITTER] = ['value' => $twitter, 'scope' => $twitterScope];
797
+            }
798
+        }
799
+
800
+        try {
801
+            $this->saveUserSettings($user, $data);
802
+            return new DataResponse(
803
+                [
804
+                    'status' => 'success',
805
+                    'data' => [
806
+                        'userId' => $user->getUID(),
807
+                        'avatarScope' => $data[AccountManager::PROPERTY_AVATAR]['scope'],
808
+                        'displayname' => $data[AccountManager::PROPERTY_DISPLAYNAME]['value'],
809
+                        'displaynameScope' => $data[AccountManager::PROPERTY_DISPLAYNAME]['scope'],
810
+                        'email' => $data[AccountManager::PROPERTY_EMAIL]['value'],
811
+                        'emailScope' => $data[AccountManager::PROPERTY_EMAIL]['scope'],
812
+                        'website' => $data[AccountManager::PROPERTY_WEBSITE]['value'],
813
+                        'websiteScope' => $data[AccountManager::PROPERTY_WEBSITE]['scope'],
814
+                        'address' => $data[AccountManager::PROPERTY_ADDRESS]['value'],
815
+                        'addressScope' => $data[AccountManager::PROPERTY_ADDRESS]['scope'],
816
+                        'message' => (string)$this->l10n->t('Settings saved')
817
+                    ]
818
+                ],
819
+                Http::STATUS_OK
820
+            );
821
+        } catch (ForbiddenException $e) {
822
+            return new DataResponse([
823
+                'status' => 'error',
824
+                'data' => [
825
+                    'message' => $e->getMessage()
826
+                ],
827
+            ]);
828
+        }
829
+
830
+    }
831
+
832
+
833
+    /**
834
+     * update account manager with new user data
835
+     *
836
+     * @param IUser $user
837
+     * @param array $data
838
+     * @throws ForbiddenException
839
+     */
840
+    protected function saveUserSettings(IUser $user, $data) {
841
+
842
+        // keep the user back-end up-to-date with the latest display name and email
843
+        // address
844
+        $oldDisplayName = $user->getDisplayName();
845
+        $oldDisplayName = is_null($oldDisplayName) ? '' : $oldDisplayName;
846
+        if (isset($data[AccountManager::PROPERTY_DISPLAYNAME]['value'])
847
+            && $oldDisplayName !== $data[AccountManager::PROPERTY_DISPLAYNAME]['value']
848
+        ) {
849
+            $result = $user->setDisplayName($data[AccountManager::PROPERTY_DISPLAYNAME]['value']);
850
+            if ($result === false) {
851
+                throw new ForbiddenException($this->l10n->t('Unable to change full name'));
852
+            }
853
+        }
854
+
855
+        $oldEmailAddress = $user->getEMailAddress();
856
+        $oldEmailAddress = is_null($oldEmailAddress) ? '' : $oldEmailAddress;
857
+        if (isset($data[AccountManager::PROPERTY_EMAIL]['value'])
858
+            && $oldEmailAddress !== $data[AccountManager::PROPERTY_EMAIL]['value']
859
+        ) {
860
+            // this is the only permission a backend provides and is also used
861
+            // for the permission of setting a email address
862
+            if (!$user->canChangeDisplayName()) {
863
+                throw new ForbiddenException($this->l10n->t('Unable to change email address'));
864
+            }
865
+            $user->setEMailAddress($data[AccountManager::PROPERTY_EMAIL]['value']);
866
+        }
867
+
868
+        $this->accountManager->updateUser($user, $data);
869
+    }
870
+
871
+    /**
872
+     * Count all unique users visible for the current admin/subadmin.
873
+     *
874
+     * @NoAdminRequired
875
+     *
876
+     * @return DataResponse
877
+     */
878
+    public function stats() {
879
+        $userCount = 0;
880
+        if ($this->isAdmin) {
881
+            $countByBackend = $this->userManager->countUsers();
882
+
883
+            if (!empty($countByBackend)) {
884
+                foreach ($countByBackend as $count) {
885
+                    $userCount += $count;
886
+                }
887
+            }
888
+        } else {
889
+            $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($this->userSession->getUser());
890
+
891
+            $uniqueUsers = [];
892
+            foreach ($groups as $group) {
893
+                foreach ($group->getUsers() as $uid => $displayName) {
894
+                    $uniqueUsers[$uid] = true;
895
+                }
896
+            }
897
+
898
+            $userCount = count($uniqueUsers);
899
+        }
900
+
901
+        return new DataResponse(
902
+            [
903
+                'totalUsers' => $userCount
904
+            ]
905
+        );
906
+    }
907
+
908
+
909
+    /**
910
+     * Set the displayName of a user
911
+     *
912
+     * @NoAdminRequired
913
+     * @NoSubadminRequired
914
+     * @PasswordConfirmationRequired
915
+     * @todo merge into saveUserSettings
916
+     *
917
+     * @param string $username
918
+     * @param string $displayName
919
+     * @return DataResponse
920
+     */
921
+    public function setDisplayName($username, $displayName) {
922
+        $currentUser = $this->userSession->getUser();
923
+        $user = $this->userManager->get($username);
924
+
925
+        if ($user === null ||
926
+            !$user->canChangeDisplayName() ||
927
+            (
928
+                !$this->groupManager->isAdmin($currentUser->getUID()) &&
929
+                !$this->groupManager->getSubAdmin()->isUserAccessible($currentUser, $user) &&
930
+                $currentUser->getUID() !== $username
931
+
932
+            )
933
+        ) {
934
+            return new DataResponse([
935
+                'status' => 'error',
936
+                'data' => [
937
+                    'message' => $this->l10n->t('Authentication error'),
938
+                ],
939
+            ]);
940
+        }
941
+
942
+        $userData = $this->accountManager->getUser($user);
943
+        $userData[AccountManager::PROPERTY_DISPLAYNAME]['value'] = $displayName;
944
+
945
+
946
+        try {
947
+            $this->saveUserSettings($user, $userData);
948
+            return new DataResponse([
949
+                'status' => 'success',
950
+                'data' => [
951
+                    'message' => $this->l10n->t('Your full name has been changed.'),
952
+                    'username' => $username,
953
+                    'displayName' => $displayName,
954
+                ],
955
+            ]);
956
+        } catch (ForbiddenException $e) {
957
+            return new DataResponse([
958
+                'status' => 'error',
959
+                'data' => [
960
+                    'message' => $e->getMessage(),
961
+                    'displayName' => $user->getDisplayName(),
962
+                ],
963
+            ]);
964
+        }
965
+    }
966
+
967
+    /**
968
+     * Set the mail address of a user
969
+     *
970
+     * @NoAdminRequired
971
+     * @NoSubadminRequired
972
+     * @PasswordConfirmationRequired
973
+     *
974
+     * @param string $id
975
+     * @param string $mailAddress
976
+     * @return DataResponse
977
+     */
978
+    public function setEMailAddress($id, $mailAddress) {
979
+        $user = $this->userManager->get($id);
980
+        if (!$this->isAdmin
981
+            && !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)
982
+        ) {
983
+            return new DataResponse(
984
+                [
985
+                    'status' => 'error',
986
+                    'data' => [
987
+                        'message' => (string)$this->l10n->t('Forbidden')
988
+                    ]
989
+                ],
990
+                Http::STATUS_FORBIDDEN
991
+            );
992
+        }
993
+
994
+        if ($mailAddress !== '' && !$this->mailer->validateMailAddress($mailAddress)) {
995
+            return new DataResponse(
996
+                [
997
+                    'status' => 'error',
998
+                    'data' => [
999
+                        'message' => (string)$this->l10n->t('Invalid mail address')
1000
+                    ]
1001
+                ],
1002
+                Http::STATUS_UNPROCESSABLE_ENTITY
1003
+            );
1004
+        }
1005
+
1006
+        if (!$user) {
1007
+            return new DataResponse(
1008
+                [
1009
+                    'status' => 'error',
1010
+                    'data' => [
1011
+                        'message' => (string)$this->l10n->t('Invalid user')
1012
+                    ]
1013
+                ],
1014
+                Http::STATUS_UNPROCESSABLE_ENTITY
1015
+            );
1016
+        }
1017
+        // this is the only permission a backend provides and is also used
1018
+        // for the permission of setting a email address
1019
+        if (!$user->canChangeDisplayName()) {
1020
+            return new DataResponse(
1021
+                [
1022
+                    'status' => 'error',
1023
+                    'data' => [
1024
+                        'message' => (string)$this->l10n->t('Unable to change mail address')
1025
+                    ]
1026
+                ],
1027
+                Http::STATUS_FORBIDDEN
1028
+            );
1029
+        }
1030
+
1031
+        $userData = $this->accountManager->getUser($user);
1032
+        $userData[AccountManager::PROPERTY_EMAIL]['value'] = $mailAddress;
1033
+
1034
+        try {
1035
+            $this->saveUserSettings($user, $userData);
1036
+            return new DataResponse(
1037
+                [
1038
+                    'status' => 'success',
1039
+                    'data' => [
1040
+                        'username' => $id,
1041
+                        'mailAddress' => $mailAddress,
1042
+                        'message' => (string)$this->l10n->t('Email saved')
1043
+                    ]
1044
+                ],
1045
+                Http::STATUS_OK
1046
+            );
1047
+        } catch (ForbiddenException $e) {
1048
+            return new DataResponse([
1049
+                'status' => 'error',
1050
+                'data' => [
1051
+                    'message' => $e->getMessage()
1052
+                ],
1053
+            ]);
1054
+        }
1055
+    }
1056 1056
 
1057 1057
 }
Please login to merge, or discard this patch.
settings/BackgroundJobs/VerifyUserData.php 1 patch
Indentation   +249 added lines, -249 removed lines patch added patch discarded remove patch
@@ -39,254 +39,254 @@
 block discarded – undo
39 39
 
40 40
 class VerifyUserData extends Job {
41 41
 
42
-	/** @var  bool */
43
-	private $retainJob = true;
44
-
45
-	/** @var int max number of attempts to send the request */
46
-	private $maxTry = 24;
47
-
48
-	/** @var int how much time should be between two tries (1 hour) */
49
-	private $interval = 3600;
50
-
51
-	/** @var AccountManager */
52
-	private $accountManager;
53
-
54
-	/** @var IUserManager */
55
-	private $userManager;
56
-
57
-	/** @var IClientService */
58
-	private $httpClientService;
59
-
60
-	/** @var ILogger */
61
-	private $logger;
62
-
63
-	/** @var string */
64
-	private $lookupServerUrl;
65
-
66
-	/**
67
-	 * VerifyUserData constructor.
68
-	 *
69
-	 * @param AccountManager $accountManager
70
-	 * @param IUserManager $userManager
71
-	 * @param IClientService $clientService
72
-	 * @param ILogger $logger
73
-	 * @param IConfig $config
74
-	 */
75
-	public function __construct(AccountManager $accountManager,
76
-								IUserManager $userManager,
77
-								IClientService $clientService,
78
-								ILogger $logger,
79
-								IConfig $config
80
-	) {
81
-		$this->accountManager = $accountManager;
82
-		$this->userManager = $userManager;
83
-		$this->httpClientService = $clientService;
84
-		$this->logger = $logger;
85
-
86
-		$lookupServerUrl = $config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com');
87
-		$this->lookupServerUrl = rtrim($lookupServerUrl, '/');
88
-	}
89
-
90
-	/**
91
-	 * run the job, then remove it from the jobList
92
-	 *
93
-	 * @param JobList $jobList
94
-	 * @param ILogger|null $logger
95
-	 */
96
-	public function execute($jobList, ILogger $logger = null) {
97
-
98
-		if ($this->shouldRun($this->argument)) {
99
-			parent::execute($jobList, $logger);
100
-			$jobList->remove($this, $this->argument);
101
-			if ($this->retainJob) {
102
-				$this->reAddJob($jobList, $this->argument);
103
-			} else {
104
-				$this->resetVerificationState();
105
-			}
106
-		}
107
-
108
-	}
109
-
110
-	protected function run($argument) {
111
-
112
-		$try = (int)$argument['try'] + 1;
113
-
114
-		switch($argument['type']) {
115
-			case AccountManager::PROPERTY_WEBSITE:
116
-				$result = $this->verifyWebsite($argument);
117
-				break;
118
-			case AccountManager::PROPERTY_TWITTER:
119
-			case AccountManager::PROPERTY_EMAIL:
120
-				$result = $this->verifyViaLookupServer($argument, $argument['type']);
121
-				break;
122
-			default:
123
-				// no valid type given, no need to retry
124
-				$this->logger->error($argument['type'] . ' is no valid type for user account data.');
125
-				$result = true;
126
-		}
127
-
128
-		if ($result === true || $try > $this->maxTry) {
129
-			$this->retainJob = false;
130
-		}
131
-	}
132
-
133
-	/**
134
-	 * verify web page
135
-	 *
136
-	 * @param array $argument
137
-	 * @return bool true if we could check the verification code, otherwise false
138
-	 */
139
-	protected function verifyWebsite(array $argument) {
140
-
141
-		$result = false;
142
-
143
-		$url = rtrim($argument['data'], '/') . '/.well-known/' . 'CloudIdVerificationCode.txt';
144
-
145
-		$client = $this->httpClientService->newClient();
146
-		try {
147
-			$response = $client->get($url);
148
-		} catch (\Exception $e) {
149
-			return false;
150
-		}
151
-
152
-		if ($response->getStatusCode() === Http::STATUS_OK) {
153
-			$result = true;
154
-			$publishedCode = $response->getBody();
155
-			// remove new lines and spaces
156
-			$publishedCodeSanitized = trim(preg_replace('/\s\s+/', ' ', $publishedCode));
157
-			$user = $this->userManager->get($argument['uid']);
158
-			// we don't check a valid user -> give up
159
-			if ($user === null) {
160
-				$this->logger->error($argument['uid'] . ' doesn\'t exist, can\'t verify user data.');
161
-				return $result;
162
-			}
163
-			$userData = $this->accountManager->getUser($user);
164
-
165
-			if ($publishedCodeSanitized === $argument['verificationCode']) {
166
-				$userData[AccountManager::PROPERTY_WEBSITE]['verified'] = AccountManager::VERIFIED;
167
-			} else {
168
-				$userData[AccountManager::PROPERTY_WEBSITE]['verified'] = AccountManager::NOT_VERIFIED;
169
-			}
170
-
171
-			$this->accountManager->updateUser($user, $userData);
172
-		}
173
-
174
-		return $result;
175
-	}
176
-
177
-	/**
178
-	 * verify email address
179
-	 *
180
-	 * @param array $argument
181
-	 * @param string $dataType
182
-	 * @return bool true if we could check the verification code, otherwise false
183
-	 */
184
-	protected function verifyViaLookupServer(array $argument, $dataType) {
185
-
186
-		$user = $this->userManager->get($argument['uid']);
187
-
188
-		// we don't check a valid user -> give up
189
-		if ($user === null) {
190
-			$this->logger->error($argument['uid'] . ' doesn\'t exist, can\'t verify user data.');
191
-			return true;
192
-		}
193
-
194
-		$localUserData = $this->accountManager->getUser($user);
195
-		$cloudId = $user->getCloudId();
196
-
197
-		// ask lookup-server for user data
198
-		$lookupServerData = $this->queryLookupServer($cloudId);
199
-
200
-		// for some reasons we couldn't read any data from the lookup server, try again later
201
-		if (empty($lookupServerData)) {
202
-			return false;
203
-		}
204
-
205
-		// lookup server has verification data for wrong user data (e.g. email address), try again later
206
-		if ($lookupServerData[$dataType]['value'] !== $argument['data']) {
207
-			return false;
208
-		}
209
-
210
-		// lookup server hasn't verified the email address so far, try again later
211
-		if ($lookupServerData[$dataType]['verified'] === AccountManager::NOT_VERIFIED) {
212
-			return false;
213
-		}
214
-
215
-		$localUserData[$dataType]['verified'] = AccountManager::VERIFIED;
216
-		$this->accountManager->updateUser($user, $localUserData);
217
-
218
-		return true;
219
-	}
220
-
221
-	/**
222
-	 * @param string $cloudId
223
-	 * @return array
224
-	 */
225
-	protected function queryLookupServer($cloudId) {
226
-		try {
227
-			$client = $this->httpClientService->newClient();
228
-			$response = $client->get(
229
-				$this->lookupServerUrl . '/users?search=' . urlencode($cloudId) . '&exactCloudId=1',
230
-				[
231
-					'timeout' => 10,
232
-					'connect_timeout' => 3,
233
-				]
234
-			);
235
-
236
-			$body = json_decode($response->getBody(), true);
237
-
238
-			if (is_array($body) && isset($body['federationId']) && $body['federationId'] === $cloudId) {
239
-				return $body;
240
-			}
241
-
242
-		} catch (\Exception $e) {
243
-			// do nothing, we will just re-try later
244
-		}
245
-
246
-		return [];
247
-	}
248
-
249
-	/**
250
-	 * re-add background job with new arguments
251
-	 *
252
-	 * @param IJobList $jobList
253
-	 * @param array $argument
254
-	 */
255
-	protected function reAddJob(IJobList $jobList, array $argument) {
256
-		$jobList->add(VerifyUserData::class,
257
-			[
258
-				'verificationCode' => $argument['verificationCode'],
259
-				'data' => $argument['data'],
260
-				'type' => $argument['type'],
261
-				'uid' => $argument['uid'],
262
-				'try' => (int)$argument['try'] + 1,
263
-				'lastRun' => time()
264
-			]
265
-		);
266
-	}
267
-
268
-	/**
269
-	 * test if it is time for the next run
270
-	 *
271
-	 * @param array $argument
272
-	 * @return bool
273
-	 */
274
-	protected function shouldRun(array $argument) {
275
-		$lastRun = (int)$argument['lastRun'];
276
-		return ((time() - $lastRun) > $this->interval);
277
-	}
278
-
279
-
280
-	/**
281
-	 * reset verification state after max tries are reached
282
-	 */
283
-	protected function resetVerificationState() {
284
-		$user = $this->userManager->get($this->argument['uid']);
285
-		if ($user !== null) {
286
-			$accountData = $this->accountManager->getUser($user);
287
-			$accountData[$this->argument['type']]['verified'] = AccountManager::NOT_VERIFIED;
288
-			$this->accountManager->updateUser($user, $accountData);
289
-		}
290
-	}
42
+    /** @var  bool */
43
+    private $retainJob = true;
44
+
45
+    /** @var int max number of attempts to send the request */
46
+    private $maxTry = 24;
47
+
48
+    /** @var int how much time should be between two tries (1 hour) */
49
+    private $interval = 3600;
50
+
51
+    /** @var AccountManager */
52
+    private $accountManager;
53
+
54
+    /** @var IUserManager */
55
+    private $userManager;
56
+
57
+    /** @var IClientService */
58
+    private $httpClientService;
59
+
60
+    /** @var ILogger */
61
+    private $logger;
62
+
63
+    /** @var string */
64
+    private $lookupServerUrl;
65
+
66
+    /**
67
+     * VerifyUserData constructor.
68
+     *
69
+     * @param AccountManager $accountManager
70
+     * @param IUserManager $userManager
71
+     * @param IClientService $clientService
72
+     * @param ILogger $logger
73
+     * @param IConfig $config
74
+     */
75
+    public function __construct(AccountManager $accountManager,
76
+                                IUserManager $userManager,
77
+                                IClientService $clientService,
78
+                                ILogger $logger,
79
+                                IConfig $config
80
+    ) {
81
+        $this->accountManager = $accountManager;
82
+        $this->userManager = $userManager;
83
+        $this->httpClientService = $clientService;
84
+        $this->logger = $logger;
85
+
86
+        $lookupServerUrl = $config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com');
87
+        $this->lookupServerUrl = rtrim($lookupServerUrl, '/');
88
+    }
89
+
90
+    /**
91
+     * run the job, then remove it from the jobList
92
+     *
93
+     * @param JobList $jobList
94
+     * @param ILogger|null $logger
95
+     */
96
+    public function execute($jobList, ILogger $logger = null) {
97
+
98
+        if ($this->shouldRun($this->argument)) {
99
+            parent::execute($jobList, $logger);
100
+            $jobList->remove($this, $this->argument);
101
+            if ($this->retainJob) {
102
+                $this->reAddJob($jobList, $this->argument);
103
+            } else {
104
+                $this->resetVerificationState();
105
+            }
106
+        }
107
+
108
+    }
109
+
110
+    protected function run($argument) {
111
+
112
+        $try = (int)$argument['try'] + 1;
113
+
114
+        switch($argument['type']) {
115
+            case AccountManager::PROPERTY_WEBSITE:
116
+                $result = $this->verifyWebsite($argument);
117
+                break;
118
+            case AccountManager::PROPERTY_TWITTER:
119
+            case AccountManager::PROPERTY_EMAIL:
120
+                $result = $this->verifyViaLookupServer($argument, $argument['type']);
121
+                break;
122
+            default:
123
+                // no valid type given, no need to retry
124
+                $this->logger->error($argument['type'] . ' is no valid type for user account data.');
125
+                $result = true;
126
+        }
127
+
128
+        if ($result === true || $try > $this->maxTry) {
129
+            $this->retainJob = false;
130
+        }
131
+    }
132
+
133
+    /**
134
+     * verify web page
135
+     *
136
+     * @param array $argument
137
+     * @return bool true if we could check the verification code, otherwise false
138
+     */
139
+    protected function verifyWebsite(array $argument) {
140
+
141
+        $result = false;
142
+
143
+        $url = rtrim($argument['data'], '/') . '/.well-known/' . 'CloudIdVerificationCode.txt';
144
+
145
+        $client = $this->httpClientService->newClient();
146
+        try {
147
+            $response = $client->get($url);
148
+        } catch (\Exception $e) {
149
+            return false;
150
+        }
151
+
152
+        if ($response->getStatusCode() === Http::STATUS_OK) {
153
+            $result = true;
154
+            $publishedCode = $response->getBody();
155
+            // remove new lines and spaces
156
+            $publishedCodeSanitized = trim(preg_replace('/\s\s+/', ' ', $publishedCode));
157
+            $user = $this->userManager->get($argument['uid']);
158
+            // we don't check a valid user -> give up
159
+            if ($user === null) {
160
+                $this->logger->error($argument['uid'] . ' doesn\'t exist, can\'t verify user data.');
161
+                return $result;
162
+            }
163
+            $userData = $this->accountManager->getUser($user);
164
+
165
+            if ($publishedCodeSanitized === $argument['verificationCode']) {
166
+                $userData[AccountManager::PROPERTY_WEBSITE]['verified'] = AccountManager::VERIFIED;
167
+            } else {
168
+                $userData[AccountManager::PROPERTY_WEBSITE]['verified'] = AccountManager::NOT_VERIFIED;
169
+            }
170
+
171
+            $this->accountManager->updateUser($user, $userData);
172
+        }
173
+
174
+        return $result;
175
+    }
176
+
177
+    /**
178
+     * verify email address
179
+     *
180
+     * @param array $argument
181
+     * @param string $dataType
182
+     * @return bool true if we could check the verification code, otherwise false
183
+     */
184
+    protected function verifyViaLookupServer(array $argument, $dataType) {
185
+
186
+        $user = $this->userManager->get($argument['uid']);
187
+
188
+        // we don't check a valid user -> give up
189
+        if ($user === null) {
190
+            $this->logger->error($argument['uid'] . ' doesn\'t exist, can\'t verify user data.');
191
+            return true;
192
+        }
193
+
194
+        $localUserData = $this->accountManager->getUser($user);
195
+        $cloudId = $user->getCloudId();
196
+
197
+        // ask lookup-server for user data
198
+        $lookupServerData = $this->queryLookupServer($cloudId);
199
+
200
+        // for some reasons we couldn't read any data from the lookup server, try again later
201
+        if (empty($lookupServerData)) {
202
+            return false;
203
+        }
204
+
205
+        // lookup server has verification data for wrong user data (e.g. email address), try again later
206
+        if ($lookupServerData[$dataType]['value'] !== $argument['data']) {
207
+            return false;
208
+        }
209
+
210
+        // lookup server hasn't verified the email address so far, try again later
211
+        if ($lookupServerData[$dataType]['verified'] === AccountManager::NOT_VERIFIED) {
212
+            return false;
213
+        }
214
+
215
+        $localUserData[$dataType]['verified'] = AccountManager::VERIFIED;
216
+        $this->accountManager->updateUser($user, $localUserData);
217
+
218
+        return true;
219
+    }
220
+
221
+    /**
222
+     * @param string $cloudId
223
+     * @return array
224
+     */
225
+    protected function queryLookupServer($cloudId) {
226
+        try {
227
+            $client = $this->httpClientService->newClient();
228
+            $response = $client->get(
229
+                $this->lookupServerUrl . '/users?search=' . urlencode($cloudId) . '&exactCloudId=1',
230
+                [
231
+                    'timeout' => 10,
232
+                    'connect_timeout' => 3,
233
+                ]
234
+            );
235
+
236
+            $body = json_decode($response->getBody(), true);
237
+
238
+            if (is_array($body) && isset($body['federationId']) && $body['federationId'] === $cloudId) {
239
+                return $body;
240
+            }
241
+
242
+        } catch (\Exception $e) {
243
+            // do nothing, we will just re-try later
244
+        }
245
+
246
+        return [];
247
+    }
248
+
249
+    /**
250
+     * re-add background job with new arguments
251
+     *
252
+     * @param IJobList $jobList
253
+     * @param array $argument
254
+     */
255
+    protected function reAddJob(IJobList $jobList, array $argument) {
256
+        $jobList->add(VerifyUserData::class,
257
+            [
258
+                'verificationCode' => $argument['verificationCode'],
259
+                'data' => $argument['data'],
260
+                'type' => $argument['type'],
261
+                'uid' => $argument['uid'],
262
+                'try' => (int)$argument['try'] + 1,
263
+                'lastRun' => time()
264
+            ]
265
+        );
266
+    }
267
+
268
+    /**
269
+     * test if it is time for the next run
270
+     *
271
+     * @param array $argument
272
+     * @return bool
273
+     */
274
+    protected function shouldRun(array $argument) {
275
+        $lastRun = (int)$argument['lastRun'];
276
+        return ((time() - $lastRun) > $this->interval);
277
+    }
278
+
279
+
280
+    /**
281
+     * reset verification state after max tries are reached
282
+     */
283
+    protected function resetVerificationState() {
284
+        $user = $this->userManager->get($this->argument['uid']);
285
+        if ($user !== null) {
286
+            $accountData = $this->accountManager->getUser($user);
287
+            $accountData[$this->argument['type']]['verified'] = AccountManager::NOT_VERIFIED;
288
+            $this->accountManager->updateUser($user, $accountData);
289
+        }
290
+    }
291 291
 
292 292
 }
Please login to merge, or discard this patch.
apps/sharebymail/lib/AppInfo/Application.php 1 patch
Indentation   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -30,19 +30,19 @@
 block discarded – undo
30 30
 
31 31
 class Application extends App {
32 32
 
33
-	public function __construct(array $urlParams = array()) {
34
-		parent::__construct('sharebymail', $urlParams);
33
+    public function __construct(array $urlParams = array()) {
34
+        parent::__construct('sharebymail', $urlParams);
35 35
 
36
-		$settingsManager = \OC::$server->query(Settings\SettingsManager::class);
37
-		$settings = new Settings($settingsManager);
36
+        $settingsManager = \OC::$server->query(Settings\SettingsManager::class);
37
+        $settings = new Settings($settingsManager);
38 38
 
39
-		/** register capabilities */
40
-		$container = $this->getContainer();
41
-		$container->registerCapability(Capabilities::class);
39
+        /** register capabilities */
40
+        $container = $this->getContainer();
41
+        $container->registerCapability(Capabilities::class);
42 42
 
43
-		/** register hooks */
44
-		Util::connectHook('\OCP\Config', 'js', $settings, 'announceShareProvider');
45
-		Util::connectHook('\OCP\Config', 'js', $settings, 'announceShareByMailSettings');
46
-	}
43
+        /** register hooks */
44
+        Util::connectHook('\OCP\Config', 'js', $settings, 'announceShareProvider');
45
+        Util::connectHook('\OCP\Config', 'js', $settings, 'announceShareByMailSettings');
46
+    }
47 47
 
48 48
 }
Please login to merge, or discard this patch.
apps/files/ajax/list.php 1 patch
Indentation   +56 added lines, -56 removed lines patch added patch discarded remove patch
@@ -36,72 +36,72 @@
 block discarded – undo
36 36
 $dir = \OC\Files\Filesystem::normalizePath($dir);
37 37
 
38 38
 try {
39
-	$dirInfo = \OC\Files\Filesystem::getFileInfo($dir);
40
-	if (!$dirInfo || !$dirInfo->getType() === 'dir') {
41
-		header("HTTP/1.0 404 Not Found");
42
-		exit();
43
-	}
39
+    $dirInfo = \OC\Files\Filesystem::getFileInfo($dir);
40
+    if (!$dirInfo || !$dirInfo->getType() === 'dir') {
41
+        header("HTTP/1.0 404 Not Found");
42
+        exit();
43
+    }
44 44
 
45
-	$data = array();
46
-	$baseUrl = OCP\Util::linkTo('files', 'index.php') . '?dir=';
45
+    $data = array();
46
+    $baseUrl = OCP\Util::linkTo('files', 'index.php') . '?dir=';
47 47
 
48
-	$permissions = $dirInfo->getPermissions();
48
+    $permissions = $dirInfo->getPermissions();
49 49
 
50
-	$sortAttribute = isset($_GET['sort']) ? (string)$_GET['sort'] : 'name';
51
-	$sortDirection = isset($_GET['sortdirection']) ? ($_GET['sortdirection'] === 'desc') : false;
52
-	$mimetypeFilters = isset($_GET['mimetypes']) ? json_decode($_GET['mimetypes']) : '';
50
+    $sortAttribute = isset($_GET['sort']) ? (string)$_GET['sort'] : 'name';
51
+    $sortDirection = isset($_GET['sortdirection']) ? ($_GET['sortdirection'] === 'desc') : false;
52
+    $mimetypeFilters = isset($_GET['mimetypes']) ? json_decode($_GET['mimetypes']) : '';
53 53
 
54
-	$files = [];
55
-	// Clean up duplicates from array
56
-	if (is_array($mimetypeFilters) && count($mimetypeFilters)) {
57
-		$mimetypeFilters = array_unique($mimetypeFilters);
54
+    $files = [];
55
+    // Clean up duplicates from array
56
+    if (is_array($mimetypeFilters) && count($mimetypeFilters)) {
57
+        $mimetypeFilters = array_unique($mimetypeFilters);
58 58
 
59
-		if (!in_array('httpd/unix-directory', $mimetypeFilters)) {
60
-			// append folder filter to be able to browse folders
61
-			$mimetypeFilters[] = 'httpd/unix-directory';
62
-		}
59
+        if (!in_array('httpd/unix-directory', $mimetypeFilters)) {
60
+            // append folder filter to be able to browse folders
61
+            $mimetypeFilters[] = 'httpd/unix-directory';
62
+        }
63 63
 
64
-		// create filelist with mimetype filter - as getFiles only supports on
65
-		// mimetype filter at once we will filter this folder for each
66
-		// mimetypeFilter
67
-		foreach ($mimetypeFilters as $mimetypeFilter) {
68
-			$files = array_merge($files, \OCA\Files\Helper::getFiles($dir, $sortAttribute, $sortDirection, $mimetypeFilter));
69
-		}
64
+        // create filelist with mimetype filter - as getFiles only supports on
65
+        // mimetype filter at once we will filter this folder for each
66
+        // mimetypeFilter
67
+        foreach ($mimetypeFilters as $mimetypeFilter) {
68
+            $files = array_merge($files, \OCA\Files\Helper::getFiles($dir, $sortAttribute, $sortDirection, $mimetypeFilter));
69
+        }
70 70
 
71
-		// sort the files accordingly
72
-		$files = \OCA\Files\Helper::sortFiles($files, $sortAttribute, $sortDirection);
73
-	} else {
74
-		// create file list without mimetype filter
75
-		$files = \OCA\Files\Helper::getFiles($dir, $sortAttribute, $sortDirection);
76
-	}
71
+        // sort the files accordingly
72
+        $files = \OCA\Files\Helper::sortFiles($files, $sortAttribute, $sortDirection);
73
+    } else {
74
+        // create file list without mimetype filter
75
+        $files = \OCA\Files\Helper::getFiles($dir, $sortAttribute, $sortDirection);
76
+    }
77 77
 
78
-	$data['directory'] = $dir;
79
-	$data['files'] = \OCA\Files\Helper::formatFileInfos($files);
80
-	$data['permissions'] = $permissions;
78
+    $data['directory'] = $dir;
79
+    $data['files'] = \OCA\Files\Helper::formatFileInfos($files);
80
+    $data['permissions'] = $permissions;
81 81
 
82
-	OCP\JSON::success(array('data' => $data));
82
+    OCP\JSON::success(array('data' => $data));
83 83
 } catch (\OCP\Files\StorageNotAvailableException $e) {
84
-	\OCP\Util::logException('files', $e);
85
-	OCP\JSON::error([
86
-		'data' => [
87
-			'exception' => StorageNotAvailableException::class,
88
-			'message' => $l->t('Storage is temporarily not available')
89
-		]
90
-	]);
84
+    \OCP\Util::logException('files', $e);
85
+    OCP\JSON::error([
86
+        'data' => [
87
+            'exception' => StorageNotAvailableException::class,
88
+            'message' => $l->t('Storage is temporarily not available')
89
+        ]
90
+    ]);
91 91
 } catch (\OCP\Files\StorageInvalidException $e) {
92
-	\OCP\Util::logException('files', $e);
93
-	OCP\JSON::error(array(
94
-		'data' => array(
95
-			'exception' => StorageInvalidException::class,
96
-			'message' => $l->t('Storage invalid')
97
-		)
98
-	));
92
+    \OCP\Util::logException('files', $e);
93
+    OCP\JSON::error(array(
94
+        'data' => array(
95
+            'exception' => StorageInvalidException::class,
96
+            'message' => $l->t('Storage invalid')
97
+        )
98
+    ));
99 99
 } catch (\Exception $e) {
100
-	\OCP\Util::logException('files', $e);
101
-	OCP\JSON::error(array(
102
-		'data' => array(
103
-			'exception' => \Exception::class,
104
-			'message' => $l->t('Unknown error')
105
-		)
106
-	));
100
+    \OCP\Util::logException('files', $e);
101
+    OCP\JSON::error(array(
102
+        'data' => array(
103
+            'exception' => \Exception::class,
104
+            'message' => $l->t('Unknown error')
105
+        )
106
+    ));
107 107
 }
Please login to merge, or discard this patch.
apps/files/appinfo/app.php 1 patch
Indentation   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -41,23 +41,23 @@
 block discarded – undo
41 41
 $templateManager->registerTemplate('application/vnd.oasis.opendocument.spreadsheet', 'core/templates/filetemplates/template.ods');
42 42
 
43 43
 \OCA\Files\App::getNavigationManager()->add(function () use ($l) {
44
-	return [
45
-		'id' => 'files',
46
-		'appname' => 'files',
47
-		'script' => 'list.php',
48
-		'order' => 0,
49
-		'name' => $l->t('All files'),
50
-	];
44
+    return [
45
+        'id' => 'files',
46
+        'appname' => 'files',
47
+        'script' => 'list.php',
48
+        'order' => 0,
49
+        'name' => $l->t('All files'),
50
+    ];
51 51
 });
52 52
 
53 53
 \OCA\Files\App::getNavigationManager()->add(function () use ($l) {
54
-	return [
55
-		'id' => 'recent',
56
-		'appname' => 'files',
57
-		'script' => 'recentlist.php',
58
-		'order' => 2,
59
-		'name' => $l->t('Recent'),
60
-	];
54
+    return [
55
+        'id' => 'recent',
56
+        'appname' => 'files',
57
+        'script' => 'recentlist.php',
58
+        'order' => 2,
59
+        'name' => $l->t('Recent'),
60
+    ];
61 61
 });
62 62
 
63 63
 \OCP\Util::connectHook('\OCP\Config', 'js', App::class, 'extendJsConfig');
Please login to merge, or discard this patch.
apps/files/lib/Command/ScanAppData.php 2 patches
Indentation   +234 added lines, -234 removed lines patch added patch discarded remove patch
@@ -40,266 +40,266 @@
 block discarded – undo
40 40
 
41 41
 class ScanAppData extends Base {
42 42
 
43
-	/** @var IRootFolder */
44
-	protected $root;
45
-	/** @var IConfig */
46
-	protected $config;
47
-	/** @var float */
48
-	protected $execTime = 0;
49
-	/** @var int */
50
-	protected $foldersCounter = 0;
51
-	/** @var int */
52
-	protected $filesCounter = 0;
43
+    /** @var IRootFolder */
44
+    protected $root;
45
+    /** @var IConfig */
46
+    protected $config;
47
+    /** @var float */
48
+    protected $execTime = 0;
49
+    /** @var int */
50
+    protected $foldersCounter = 0;
51
+    /** @var int */
52
+    protected $filesCounter = 0;
53 53
 
54
-	public function __construct(IRootFolder $rootFolder, IConfig $config) {
55
-		parent::__construct();
54
+    public function __construct(IRootFolder $rootFolder, IConfig $config) {
55
+        parent::__construct();
56 56
 
57
-		$this->root = $rootFolder;
58
-		$this->config = $config;
59
-	}
57
+        $this->root = $rootFolder;
58
+        $this->config = $config;
59
+    }
60 60
 
61
-	protected function configure() {
62
-		parent::configure();
61
+    protected function configure() {
62
+        parent::configure();
63 63
 
64
-		$this
65
-			->setName('files:scan-app-data')
66
-			->setDescription('rescan the AppData folder')
67
-			->addOption(
68
-				'quiet',
69
-				'q',
70
-				InputOption::VALUE_NONE,
71
-				'suppress any output'
72
-			)
73
-			->addOption(
74
-				'verbose',
75
-				'-v|vv|vvv',
76
-				InputOption::VALUE_NONE,
77
-				'verbose the output'
78
-			);
79
-	}
64
+        $this
65
+            ->setName('files:scan-app-data')
66
+            ->setDescription('rescan the AppData folder')
67
+            ->addOption(
68
+                'quiet',
69
+                'q',
70
+                InputOption::VALUE_NONE,
71
+                'suppress any output'
72
+            )
73
+            ->addOption(
74
+                'verbose',
75
+                '-v|vv|vvv',
76
+                InputOption::VALUE_NONE,
77
+                'verbose the output'
78
+            );
79
+    }
80 80
 
81
-	public function checkScanWarning($fullPath, OutputInterface $output) {
82
-		$normalizedPath = basename(\OC\Files\Filesystem::normalizePath($fullPath));
83
-		$path = basename($fullPath);
81
+    public function checkScanWarning($fullPath, OutputInterface $output) {
82
+        $normalizedPath = basename(\OC\Files\Filesystem::normalizePath($fullPath));
83
+        $path = basename($fullPath);
84 84
 
85
-		if ($normalizedPath !== $path) {
86
-			$output->writeln("\t<error>Entry \"" . $fullPath . '" will not be accessible due to incompatible encoding</error>');
87
-		}
88
-	}
85
+        if ($normalizedPath !== $path) {
86
+            $output->writeln("\t<error>Entry \"" . $fullPath . '" will not be accessible due to incompatible encoding</error>');
87
+        }
88
+    }
89 89
 
90
-	protected function scanFiles($verbose, OutputInterface $output) {
91
-		try {
92
-			$appData = $this->getAppDataFolder();
93
-		} catch (NotFoundException $e) {
94
-			$output->writeln('NoAppData folder found');
95
-			return;
96
-		}
90
+    protected function scanFiles($verbose, OutputInterface $output) {
91
+        try {
92
+            $appData = $this->getAppDataFolder();
93
+        } catch (NotFoundException $e) {
94
+            $output->writeln('NoAppData folder found');
95
+            return;
96
+        }
97 97
 
98
-		$connection = $this->reconnectToDatabase($output);
99
-		$scanner = new \OC\Files\Utils\Scanner(null, $connection, \OC::$server->getLogger());
100
-		# check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception
101
-		# printout and count
102
-		if ($verbose) {
103
-			$scanner->listen(Scanner::class, 'scanFile', function ($path) use ($output) {
104
-				$output->writeln("\tFile   <info>$path</info>");
105
-				$this->filesCounter += 1;
106
-				if ($this->hasBeenInterrupted()) {
107
-					throw new InterruptedException();
108
-				}
109
-			});
110
-			$scanner->listen(Scanner::class, 'scanFolder', function ($path) use ($output) {
111
-				$output->writeln("\tFolder <info>$path</info>");
112
-				$this->foldersCounter += 1;
113
-				if ($this->hasBeenInterrupted()) {
114
-					throw new InterruptedException();
115
-				}
116
-			});
117
-			$scanner->listen(Scanner::class, 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) {
118
-				$output->writeln("Error while scanning, storage not available (" . $e->getMessage() . ")");
119
-			});
120
-			# count only
121
-		} else {
122
-			$scanner->listen(Scanner::class, 'scanFile', function () use ($output) {
123
-				$this->filesCounter += 1;
124
-				if ($this->hasBeenInterrupted()) {
125
-					throw new InterruptedException();
126
-				}
127
-			});
128
-			$scanner->listen(Scanner::class, 'scanFolder', function () use ($output) {
129
-				$this->foldersCounter += 1;
130
-				if ($this->hasBeenInterrupted()) {
131
-					throw new InterruptedException();
132
-				}
133
-			});
134
-		}
135
-		$scanner->listen(Scanner::class, 'scanFile', function($path) use ($output) {
136
-			$this->checkScanWarning($path, $output);
137
-		});
138
-		$scanner->listen(Scanner::class, 'scanFolder', function($path) use ($output) {
139
-			$this->checkScanWarning($path, $output);
140
-		});
98
+        $connection = $this->reconnectToDatabase($output);
99
+        $scanner = new \OC\Files\Utils\Scanner(null, $connection, \OC::$server->getLogger());
100
+        # check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception
101
+        # printout and count
102
+        if ($verbose) {
103
+            $scanner->listen(Scanner::class, 'scanFile', function ($path) use ($output) {
104
+                $output->writeln("\tFile   <info>$path</info>");
105
+                $this->filesCounter += 1;
106
+                if ($this->hasBeenInterrupted()) {
107
+                    throw new InterruptedException();
108
+                }
109
+            });
110
+            $scanner->listen(Scanner::class, 'scanFolder', function ($path) use ($output) {
111
+                $output->writeln("\tFolder <info>$path</info>");
112
+                $this->foldersCounter += 1;
113
+                if ($this->hasBeenInterrupted()) {
114
+                    throw new InterruptedException();
115
+                }
116
+            });
117
+            $scanner->listen(Scanner::class, 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) {
118
+                $output->writeln("Error while scanning, storage not available (" . $e->getMessage() . ")");
119
+            });
120
+            # count only
121
+        } else {
122
+            $scanner->listen(Scanner::class, 'scanFile', function () use ($output) {
123
+                $this->filesCounter += 1;
124
+                if ($this->hasBeenInterrupted()) {
125
+                    throw new InterruptedException();
126
+                }
127
+            });
128
+            $scanner->listen(Scanner::class, 'scanFolder', function () use ($output) {
129
+                $this->foldersCounter += 1;
130
+                if ($this->hasBeenInterrupted()) {
131
+                    throw new InterruptedException();
132
+                }
133
+            });
134
+        }
135
+        $scanner->listen(Scanner::class, 'scanFile', function($path) use ($output) {
136
+            $this->checkScanWarning($path, $output);
137
+        });
138
+        $scanner->listen(Scanner::class, 'scanFolder', function($path) use ($output) {
139
+            $this->checkScanWarning($path, $output);
140
+        });
141 141
 
142
-		try {
143
-			$scanner->scan($appData->getPath());
144
-		} catch (ForbiddenException $e) {
145
-			$output->writeln("<error>Storage not writable</error>");
146
-			$output->writeln("Make sure you're running the scan command only as the user the web server runs as");
147
-		} catch (InterruptedException $e) {
148
-			# exit the function if ctrl-c has been pressed
149
-			$output->writeln('Interrupted by user');
150
-		} catch (NotFoundException $e) {
151
-			$output->writeln('<error>Path not found: ' . $e->getMessage() . '</error>');
152
-		} catch (\Exception $e) {
153
-			$output->writeln('<error>Exception during scan: ' . $e->getMessage() . '</error>');
154
-			$output->writeln('<error>' . $e->getTraceAsString() . '</error>');
155
-		}
156
-	}
142
+        try {
143
+            $scanner->scan($appData->getPath());
144
+        } catch (ForbiddenException $e) {
145
+            $output->writeln("<error>Storage not writable</error>");
146
+            $output->writeln("Make sure you're running the scan command only as the user the web server runs as");
147
+        } catch (InterruptedException $e) {
148
+            # exit the function if ctrl-c has been pressed
149
+            $output->writeln('Interrupted by user');
150
+        } catch (NotFoundException $e) {
151
+            $output->writeln('<error>Path not found: ' . $e->getMessage() . '</error>');
152
+        } catch (\Exception $e) {
153
+            $output->writeln('<error>Exception during scan: ' . $e->getMessage() . '</error>');
154
+            $output->writeln('<error>' . $e->getTraceAsString() . '</error>');
155
+        }
156
+    }
157 157
 
158 158
 
159
-	protected function execute(InputInterface $input, OutputInterface $output) {
160
-		# no messaging level option means: no full printout but statistics
161
-		# $quiet   means no print at all
162
-		# $verbose means full printout including statistics
163
-		# -q	-v	full	stat
164
-		#  0	 0	no	yes
165
-		#  0	 1	yes	yes
166
-		#  1	--	no	no  (quiet overrules verbose)
167
-		$verbose = $input->getOption('verbose');
168
-		$quiet = $input->getOption('quiet');
169
-		# restrict the verbosity level to VERBOSITY_VERBOSE
170
-		if ($output->getVerbosity() > OutputInterface::VERBOSITY_VERBOSE) {
171
-			$output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
172
-		}
173
-		if ($quiet) {
174
-			$verbose = false;
175
-		}
159
+    protected function execute(InputInterface $input, OutputInterface $output) {
160
+        # no messaging level option means: no full printout but statistics
161
+        # $quiet   means no print at all
162
+        # $verbose means full printout including statistics
163
+        # -q	-v	full	stat
164
+        #  0	 0	no	yes
165
+        #  0	 1	yes	yes
166
+        #  1	--	no	no  (quiet overrules verbose)
167
+        $verbose = $input->getOption('verbose');
168
+        $quiet = $input->getOption('quiet');
169
+        # restrict the verbosity level to VERBOSITY_VERBOSE
170
+        if ($output->getVerbosity() > OutputInterface::VERBOSITY_VERBOSE) {
171
+            $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
172
+        }
173
+        if ($quiet) {
174
+            $verbose = false;
175
+        }
176 176
 
177
-		$output->writeln("\nScanning AppData for files");
177
+        $output->writeln("\nScanning AppData for files");
178 178
 
179
-		$this->initTools();
179
+        $this->initTools();
180 180
 
181
-		$this->scanFiles($verbose, $output);
181
+        $this->scanFiles($verbose, $output);
182 182
 
183
-		# stat: printout statistics if $quiet was not set
184
-		if (!$quiet) {
185
-			$this->presentStats($output);
186
-		}
187
-	}
183
+        # stat: printout statistics if $quiet was not set
184
+        if (!$quiet) {
185
+            $this->presentStats($output);
186
+        }
187
+    }
188 188
 
189
-	/**
190
-	 * Initialises some useful tools for the Command
191
-	 */
192
-	protected function initTools() {
193
-		// Start the timer
194
-		$this->execTime = -microtime(true);
195
-		// Convert PHP errors to exceptions
196
-		set_error_handler([$this, 'exceptionErrorHandler'], E_ALL);
197
-	}
189
+    /**
190
+     * Initialises some useful tools for the Command
191
+     */
192
+    protected function initTools() {
193
+        // Start the timer
194
+        $this->execTime = -microtime(true);
195
+        // Convert PHP errors to exceptions
196
+        set_error_handler([$this, 'exceptionErrorHandler'], E_ALL);
197
+    }
198 198
 
199
-	/**
200
-	 * Processes PHP errors as exceptions in order to be able to keep track of problems
201
-	 *
202
-	 * @see https://secure.php.net/manual/en/function.set-error-handler.php
203
-	 *
204
-	 * @param int $severity the level of the error raised
205
-	 * @param string $message
206
-	 * @param string $file the filename that the error was raised in
207
-	 * @param int $line the line number the error was raised
208
-	 *
209
-	 * @throws \ErrorException
210
-	 */
211
-	public function exceptionErrorHandler($severity, $message, $file, $line) {
212
-		if (!(error_reporting() & $severity)) {
213
-			// This error code is not included in error_reporting
214
-			return;
215
-		}
216
-		throw new \ErrorException($message, 0, $severity, $file, $line);
217
-	}
199
+    /**
200
+     * Processes PHP errors as exceptions in order to be able to keep track of problems
201
+     *
202
+     * @see https://secure.php.net/manual/en/function.set-error-handler.php
203
+     *
204
+     * @param int $severity the level of the error raised
205
+     * @param string $message
206
+     * @param string $file the filename that the error was raised in
207
+     * @param int $line the line number the error was raised
208
+     *
209
+     * @throws \ErrorException
210
+     */
211
+    public function exceptionErrorHandler($severity, $message, $file, $line) {
212
+        if (!(error_reporting() & $severity)) {
213
+            // This error code is not included in error_reporting
214
+            return;
215
+        }
216
+        throw new \ErrorException($message, 0, $severity, $file, $line);
217
+    }
218 218
 
219
-	/**
220
-	 * @param OutputInterface $output
221
-	 */
222
-	protected function presentStats(OutputInterface $output) {
223
-		// Stop the timer
224
-		$this->execTime += microtime(true);
225
-		$output->writeln("");
219
+    /**
220
+     * @param OutputInterface $output
221
+     */
222
+    protected function presentStats(OutputInterface $output) {
223
+        // Stop the timer
224
+        $this->execTime += microtime(true);
225
+        $output->writeln("");
226 226
 
227
-		$headers = [
228
-			'Folders', 'Files', 'Elapsed time'
229
-		];
227
+        $headers = [
228
+            'Folders', 'Files', 'Elapsed time'
229
+        ];
230 230
 
231
-		$this->showSummary($headers, null, $output);
232
-	}
231
+        $this->showSummary($headers, null, $output);
232
+    }
233 233
 
234
-	/**
235
-	 * Shows a summary of operations
236
-	 *
237
-	 * @param string[] $headers
238
-	 * @param string[] $rows
239
-	 * @param OutputInterface $output
240
-	 */
241
-	protected function showSummary($headers, $rows, OutputInterface $output) {
242
-		$niceDate = $this->formatExecTime();
243
-		if (!$rows) {
244
-			$rows = [
245
-				$this->foldersCounter,
246
-				$this->filesCounter,
247
-				$niceDate,
248
-			];
249
-		}
250
-		$table = new Table($output);
251
-		$table
252
-			->setHeaders($headers)
253
-			->setRows([$rows]);
254
-		$table->render();
255
-	}
234
+    /**
235
+     * Shows a summary of operations
236
+     *
237
+     * @param string[] $headers
238
+     * @param string[] $rows
239
+     * @param OutputInterface $output
240
+     */
241
+    protected function showSummary($headers, $rows, OutputInterface $output) {
242
+        $niceDate = $this->formatExecTime();
243
+        if (!$rows) {
244
+            $rows = [
245
+                $this->foldersCounter,
246
+                $this->filesCounter,
247
+                $niceDate,
248
+            ];
249
+        }
250
+        $table = new Table($output);
251
+        $table
252
+            ->setHeaders($headers)
253
+            ->setRows([$rows]);
254
+        $table->render();
255
+    }
256 256
 
257 257
 
258
-	/**
259
-	 * Formats microtime into a human readable format
260
-	 *
261
-	 * @return string
262
-	 */
263
-	protected function formatExecTime() {
264
-		list($secs, ) = explode('.', sprintf("%.1f", ($this->execTime)));
258
+    /**
259
+     * Formats microtime into a human readable format
260
+     *
261
+     * @return string
262
+     */
263
+    protected function formatExecTime() {
264
+        list($secs, ) = explode('.', sprintf("%.1f", ($this->execTime)));
265 265
 
266
-		# if you want to have microseconds add this:   . '.' . $tens;
267
-		return date('H:i:s', $secs);
268
-	}
266
+        # if you want to have microseconds add this:   . '.' . $tens;
267
+        return date('H:i:s', $secs);
268
+    }
269 269
 
270
-	/**
271
-	 * @return \OCP\IDBConnection
272
-	 */
273
-	protected function reconnectToDatabase(OutputInterface $output) {
274
-		/** @var Connection | IDBConnection $connection*/
275
-		$connection = \OC::$server->getDatabaseConnection();
276
-		try {
277
-			$connection->close();
278
-		} catch (\Exception $ex) {
279
-			$output->writeln("<info>Error while disconnecting from database: {$ex->getMessage()}</info>");
280
-		}
281
-		while (!$connection->isConnected()) {
282
-			try {
283
-				$connection->connect();
284
-			} catch (\Exception $ex) {
285
-				$output->writeln("<info>Error while re-connecting to database: {$ex->getMessage()}</info>");
286
-				sleep(60);
287
-			}
288
-		}
289
-		return $connection;
290
-	}
270
+    /**
271
+     * @return \OCP\IDBConnection
272
+     */
273
+    protected function reconnectToDatabase(OutputInterface $output) {
274
+        /** @var Connection | IDBConnection $connection*/
275
+        $connection = \OC::$server->getDatabaseConnection();
276
+        try {
277
+            $connection->close();
278
+        } catch (\Exception $ex) {
279
+            $output->writeln("<info>Error while disconnecting from database: {$ex->getMessage()}</info>");
280
+        }
281
+        while (!$connection->isConnected()) {
282
+            try {
283
+                $connection->connect();
284
+            } catch (\Exception $ex) {
285
+                $output->writeln("<info>Error while re-connecting to database: {$ex->getMessage()}</info>");
286
+                sleep(60);
287
+            }
288
+        }
289
+        return $connection;
290
+    }
291 291
 
292
-	/**
293
-	 * @return \OCP\Files\Folder
294
-	 * @throws NotFoundException
295
-	 */
296
-	private function getAppDataFolder() {
297
-		$instanceId = $this->config->getSystemValue('instanceid', null);
292
+    /**
293
+     * @return \OCP\Files\Folder
294
+     * @throws NotFoundException
295
+     */
296
+    private function getAppDataFolder() {
297
+        $instanceId = $this->config->getSystemValue('instanceid', null);
298 298
 
299
-		if ($instanceId === null) {
300
-			throw new NotFoundException();
301
-		}
299
+        if ($instanceId === null) {
300
+            throw new NotFoundException();
301
+        }
302 302
 
303
-		return $this->root->get('appdata_'.$instanceId);
304
-	}
303
+        return $this->root->get('appdata_'.$instanceId);
304
+    }
305 305
 }
Please login to merge, or discard this patch.
Spacing   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -83,7 +83,7 @@  discard block
 block discarded – undo
83 83
 		$path = basename($fullPath);
84 84
 
85 85
 		if ($normalizedPath !== $path) {
86
-			$output->writeln("\t<error>Entry \"" . $fullPath . '" will not be accessible due to incompatible encoding</error>');
86
+			$output->writeln("\t<error>Entry \"".$fullPath.'" will not be accessible due to incompatible encoding</error>');
87 87
 		}
88 88
 	}
89 89
 
@@ -100,32 +100,32 @@  discard block
 block discarded – undo
100 100
 		# check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception
101 101
 		# printout and count
102 102
 		if ($verbose) {
103
-			$scanner->listen(Scanner::class, 'scanFile', function ($path) use ($output) {
103
+			$scanner->listen(Scanner::class, 'scanFile', function($path) use ($output) {
104 104
 				$output->writeln("\tFile   <info>$path</info>");
105 105
 				$this->filesCounter += 1;
106 106
 				if ($this->hasBeenInterrupted()) {
107 107
 					throw new InterruptedException();
108 108
 				}
109 109
 			});
110
-			$scanner->listen(Scanner::class, 'scanFolder', function ($path) use ($output) {
110
+			$scanner->listen(Scanner::class, 'scanFolder', function($path) use ($output) {
111 111
 				$output->writeln("\tFolder <info>$path</info>");
112 112
 				$this->foldersCounter += 1;
113 113
 				if ($this->hasBeenInterrupted()) {
114 114
 					throw new InterruptedException();
115 115
 				}
116 116
 			});
117
-			$scanner->listen(Scanner::class, 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) {
118
-				$output->writeln("Error while scanning, storage not available (" . $e->getMessage() . ")");
117
+			$scanner->listen(Scanner::class, 'StorageNotAvailable', function(StorageNotAvailableException $e) use ($output) {
118
+				$output->writeln("Error while scanning, storage not available (".$e->getMessage().")");
119 119
 			});
120 120
 			# count only
121 121
 		} else {
122
-			$scanner->listen(Scanner::class, 'scanFile', function () use ($output) {
122
+			$scanner->listen(Scanner::class, 'scanFile', function() use ($output) {
123 123
 				$this->filesCounter += 1;
124 124
 				if ($this->hasBeenInterrupted()) {
125 125
 					throw new InterruptedException();
126 126
 				}
127 127
 			});
128
-			$scanner->listen(Scanner::class, 'scanFolder', function () use ($output) {
128
+			$scanner->listen(Scanner::class, 'scanFolder', function() use ($output) {
129 129
 				$this->foldersCounter += 1;
130 130
 				if ($this->hasBeenInterrupted()) {
131 131
 					throw new InterruptedException();
@@ -148,10 +148,10 @@  discard block
 block discarded – undo
148 148
 			# exit the function if ctrl-c has been pressed
149 149
 			$output->writeln('Interrupted by user');
150 150
 		} catch (NotFoundException $e) {
151
-			$output->writeln('<error>Path not found: ' . $e->getMessage() . '</error>');
151
+			$output->writeln('<error>Path not found: '.$e->getMessage().'</error>');
152 152
 		} catch (\Exception $e) {
153
-			$output->writeln('<error>Exception during scan: ' . $e->getMessage() . '</error>');
154
-			$output->writeln('<error>' . $e->getTraceAsString() . '</error>');
153
+			$output->writeln('<error>Exception during scan: '.$e->getMessage().'</error>');
154
+			$output->writeln('<error>'.$e->getTraceAsString().'</error>');
155 155
 		}
156 156
 	}
157 157
 
@@ -261,7 +261,7 @@  discard block
 block discarded – undo
261 261
 	 * @return string
262 262
 	 */
263 263
 	protected function formatExecTime() {
264
-		list($secs, ) = explode('.', sprintf("%.1f", ($this->execTime)));
264
+		list($secs,) = explode('.', sprintf("%.1f", ($this->execTime)));
265 265
 
266 266
 		# if you want to have microseconds add this:   . '.' . $tens;
267 267
 		return date('H:i:s', $secs);
Please login to merge, or discard this patch.
apps/files/lib/Command/Scan.php 2 patches
Indentation   +281 added lines, -281 removed lines patch added patch discarded remove patch
@@ -45,310 +45,310 @@
 block discarded – undo
45 45
 
46 46
 class Scan extends Base {
47 47
 
48
-	/** @var IUserManager $userManager */
49
-	private $userManager;
50
-	/** @var float */
51
-	protected $execTime = 0;
52
-	/** @var int */
53
-	protected $foldersCounter = 0;
54
-	/** @var int */
55
-	protected $filesCounter = 0;
48
+    /** @var IUserManager $userManager */
49
+    private $userManager;
50
+    /** @var float */
51
+    protected $execTime = 0;
52
+    /** @var int */
53
+    protected $foldersCounter = 0;
54
+    /** @var int */
55
+    protected $filesCounter = 0;
56 56
 
57
-	public function __construct(IUserManager $userManager) {
58
-		$this->userManager = $userManager;
59
-		parent::__construct();
60
-	}
57
+    public function __construct(IUserManager $userManager) {
58
+        $this->userManager = $userManager;
59
+        parent::__construct();
60
+    }
61 61
 
62
-	protected function configure() {
63
-		parent::configure();
62
+    protected function configure() {
63
+        parent::configure();
64 64
 
65
-		$this
66
-			->setName('files:scan')
67
-			->setDescription('rescan filesystem')
68
-			->addArgument(
69
-				'user_id',
70
-				InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
71
-				'will rescan all files of the given user(s)'
72
-			)
73
-			->addOption(
74
-				'path',
75
-				'p',
76
-				InputArgument::OPTIONAL,
77
-				'limit rescan to this path, eg. --path="/alice/files/Music", the user_id is determined by the path and the user_id parameter and --all are ignored'
78
-			)
79
-			->addOption(
80
-				'quiet',
81
-				'q',
82
-				InputOption::VALUE_NONE,
83
-				'suppress any output'
84
-			)
85
-			->addOption(
86
-				'verbose',
87
-				'-v|vv|vvv',
88
-				InputOption::VALUE_NONE,
89
-				'verbose the output'
90
-			)
91
-			->addOption(
92
-				'all',
93
-				null,
94
-				InputOption::VALUE_NONE,
95
-				'will rescan all files of all known users'
96
-			)->addOption(
97
-				'unscanned',
98
-				null,
99
-				InputOption::VALUE_NONE,
100
-				'only scan files which are marked as not fully scanned'
101
-			);
102
-	}
65
+        $this
66
+            ->setName('files:scan')
67
+            ->setDescription('rescan filesystem')
68
+            ->addArgument(
69
+                'user_id',
70
+                InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
71
+                'will rescan all files of the given user(s)'
72
+            )
73
+            ->addOption(
74
+                'path',
75
+                'p',
76
+                InputArgument::OPTIONAL,
77
+                'limit rescan to this path, eg. --path="/alice/files/Music", the user_id is determined by the path and the user_id parameter and --all are ignored'
78
+            )
79
+            ->addOption(
80
+                'quiet',
81
+                'q',
82
+                InputOption::VALUE_NONE,
83
+                'suppress any output'
84
+            )
85
+            ->addOption(
86
+                'verbose',
87
+                '-v|vv|vvv',
88
+                InputOption::VALUE_NONE,
89
+                'verbose the output'
90
+            )
91
+            ->addOption(
92
+                'all',
93
+                null,
94
+                InputOption::VALUE_NONE,
95
+                'will rescan all files of all known users'
96
+            )->addOption(
97
+                'unscanned',
98
+                null,
99
+                InputOption::VALUE_NONE,
100
+                'only scan files which are marked as not fully scanned'
101
+            );
102
+    }
103 103
 
104
-	public function checkScanWarning($fullPath, OutputInterface $output) {
105
-		$normalizedPath = basename(\OC\Files\Filesystem::normalizePath($fullPath));
106
-		$path = basename($fullPath);
104
+    public function checkScanWarning($fullPath, OutputInterface $output) {
105
+        $normalizedPath = basename(\OC\Files\Filesystem::normalizePath($fullPath));
106
+        $path = basename($fullPath);
107 107
 
108
-		if ($normalizedPath !== $path) {
109
-			$output->writeln("\t<error>Entry \"" . $fullPath . '" will not be accessible due to incompatible encoding</error>');
110
-		}
111
-	}
108
+        if ($normalizedPath !== $path) {
109
+            $output->writeln("\t<error>Entry \"" . $fullPath . '" will not be accessible due to incompatible encoding</error>');
110
+        }
111
+    }
112 112
 
113
-	protected function scanFiles($user, $path, $verbose, OutputInterface $output, $backgroundScan = false) {
114
-		$connection = $this->reconnectToDatabase($output);
115
-		$scanner = new \OC\Files\Utils\Scanner($user, $connection, \OC::$server->getLogger());
116
-		# check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception
117
-		# printout and count
118
-		if ($verbose) {
119
-			$scanner->listen(Scanner::class, 'scanFile', function ($path) use ($output) {
120
-				$output->writeln("\tFile   <info>$path</info>");
121
-				$this->filesCounter += 1;
122
-				if ($this->hasBeenInterrupted()) {
123
-					throw new InterruptedException();
124
-				}
125
-			});
126
-			$scanner->listen(Scanner::class, 'scanFolder', function ($path) use ($output) {
127
-				$output->writeln("\tFolder <info>$path</info>");
128
-				$this->foldersCounter += 1;
129
-				if ($this->hasBeenInterrupted()) {
130
-					throw new InterruptedException();
131
-				}
132
-			});
133
-			$scanner->listen(Scanner::class, 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) {
134
-				$output->writeln("Error while scanning, storage not available (" . $e->getMessage() . ")");
135
-			});
136
-			# count only
137
-		} else {
138
-			$scanner->listen(Scanner::class, 'scanFile', function () use ($output) {
139
-				$this->filesCounter += 1;
140
-				if ($this->hasBeenInterrupted()) {
141
-					throw new InterruptedException();
142
-				}
143
-			});
144
-			$scanner->listen(Scanner::class, 'scanFolder', function () use ($output) {
145
-				$this->foldersCounter += 1;
146
-				if ($this->hasBeenInterrupted()) {
147
-					throw new InterruptedException();
148
-				}
149
-			});
150
-		}
151
-		$scanner->listen(Scanner::class, 'scanFile', function ($path) use ($output) {
152
-			$this->checkScanWarning($path, $output);
153
-		});
154
-		$scanner->listen(Scanner::class, 'scanFolder', function ($path) use ($output) {
155
-			$this->checkScanWarning($path, $output);
156
-		});
113
+    protected function scanFiles($user, $path, $verbose, OutputInterface $output, $backgroundScan = false) {
114
+        $connection = $this->reconnectToDatabase($output);
115
+        $scanner = new \OC\Files\Utils\Scanner($user, $connection, \OC::$server->getLogger());
116
+        # check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception
117
+        # printout and count
118
+        if ($verbose) {
119
+            $scanner->listen(Scanner::class, 'scanFile', function ($path) use ($output) {
120
+                $output->writeln("\tFile   <info>$path</info>");
121
+                $this->filesCounter += 1;
122
+                if ($this->hasBeenInterrupted()) {
123
+                    throw new InterruptedException();
124
+                }
125
+            });
126
+            $scanner->listen(Scanner::class, 'scanFolder', function ($path) use ($output) {
127
+                $output->writeln("\tFolder <info>$path</info>");
128
+                $this->foldersCounter += 1;
129
+                if ($this->hasBeenInterrupted()) {
130
+                    throw new InterruptedException();
131
+                }
132
+            });
133
+            $scanner->listen(Scanner::class, 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) {
134
+                $output->writeln("Error while scanning, storage not available (" . $e->getMessage() . ")");
135
+            });
136
+            # count only
137
+        } else {
138
+            $scanner->listen(Scanner::class, 'scanFile', function () use ($output) {
139
+                $this->filesCounter += 1;
140
+                if ($this->hasBeenInterrupted()) {
141
+                    throw new InterruptedException();
142
+                }
143
+            });
144
+            $scanner->listen(Scanner::class, 'scanFolder', function () use ($output) {
145
+                $this->foldersCounter += 1;
146
+                if ($this->hasBeenInterrupted()) {
147
+                    throw new InterruptedException();
148
+                }
149
+            });
150
+        }
151
+        $scanner->listen(Scanner::class, 'scanFile', function ($path) use ($output) {
152
+            $this->checkScanWarning($path, $output);
153
+        });
154
+        $scanner->listen(Scanner::class, 'scanFolder', function ($path) use ($output) {
155
+            $this->checkScanWarning($path, $output);
156
+        });
157 157
 
158
-		try {
159
-			if ($backgroundScan) {
160
-				$scanner->backgroundScan($path);
161
-			} else {
162
-				$scanner->scan($path);
163
-			}
164
-		} catch (ForbiddenException $e) {
165
-			$output->writeln("<error>Home storage for user $user not writable</error>");
166
-			$output->writeln("Make sure you're running the scan command only as the user the web server runs as");
167
-		} catch (InterruptedException $e) {
168
-			# exit the function if ctrl-c has been pressed
169
-			$output->writeln('Interrupted by user');
170
-		} catch (NotFoundException $e) {
171
-			$output->writeln('<error>Path not found: ' . $e->getMessage() . '</error>');
172
-		} catch (\Exception $e) {
173
-			$output->writeln('<error>Exception during scan: ' . $e->getMessage() . '</error>');
174
-			$output->writeln('<error>' . $e->getTraceAsString() . '</error>');
175
-		}
176
-	}
158
+        try {
159
+            if ($backgroundScan) {
160
+                $scanner->backgroundScan($path);
161
+            } else {
162
+                $scanner->scan($path);
163
+            }
164
+        } catch (ForbiddenException $e) {
165
+            $output->writeln("<error>Home storage for user $user not writable</error>");
166
+            $output->writeln("Make sure you're running the scan command only as the user the web server runs as");
167
+        } catch (InterruptedException $e) {
168
+            # exit the function if ctrl-c has been pressed
169
+            $output->writeln('Interrupted by user');
170
+        } catch (NotFoundException $e) {
171
+            $output->writeln('<error>Path not found: ' . $e->getMessage() . '</error>');
172
+        } catch (\Exception $e) {
173
+            $output->writeln('<error>Exception during scan: ' . $e->getMessage() . '</error>');
174
+            $output->writeln('<error>' . $e->getTraceAsString() . '</error>');
175
+        }
176
+    }
177 177
 
178 178
 
179
-	protected function execute(InputInterface $input, OutputInterface $output) {
180
-		$inputPath = $input->getOption('path');
181
-		if ($inputPath) {
182
-			$inputPath = '/' . trim($inputPath, '/');
183
-			list (, $user,) = explode('/', $inputPath, 3);
184
-			$users = array($user);
185
-		} else if ($input->getOption('all')) {
186
-			$users = $this->userManager->search('');
187
-		} else {
188
-			$users = $input->getArgument('user_id');
189
-		}
179
+    protected function execute(InputInterface $input, OutputInterface $output) {
180
+        $inputPath = $input->getOption('path');
181
+        if ($inputPath) {
182
+            $inputPath = '/' . trim($inputPath, '/');
183
+            list (, $user,) = explode('/', $inputPath, 3);
184
+            $users = array($user);
185
+        } else if ($input->getOption('all')) {
186
+            $users = $this->userManager->search('');
187
+        } else {
188
+            $users = $input->getArgument('user_id');
189
+        }
190 190
 
191
-		# no messaging level option means: no full printout but statistics
192
-		# $quiet   means no print at all
193
-		# $verbose means full printout including statistics
194
-		# -q	-v	full	stat
195
-		#  0	 0	no	yes
196
-		#  0	 1	yes	yes
197
-		#  1	--	no	no  (quiet overrules verbose)
198
-		$verbose = $input->getOption('verbose');
199
-		$quiet = $input->getOption('quiet');
200
-		# restrict the verbosity level to VERBOSITY_VERBOSE
201
-		if ($output->getVerbosity() > OutputInterface::VERBOSITY_VERBOSE) {
202
-			$output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
203
-		}
204
-		if ($quiet) {
205
-			$verbose = false;
206
-		}
191
+        # no messaging level option means: no full printout but statistics
192
+        # $quiet   means no print at all
193
+        # $verbose means full printout including statistics
194
+        # -q	-v	full	stat
195
+        #  0	 0	no	yes
196
+        #  0	 1	yes	yes
197
+        #  1	--	no	no  (quiet overrules verbose)
198
+        $verbose = $input->getOption('verbose');
199
+        $quiet = $input->getOption('quiet');
200
+        # restrict the verbosity level to VERBOSITY_VERBOSE
201
+        if ($output->getVerbosity() > OutputInterface::VERBOSITY_VERBOSE) {
202
+            $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
203
+        }
204
+        if ($quiet) {
205
+            $verbose = false;
206
+        }
207 207
 
208
-		# check quantity of users to be process and show it on the command line
209
-		$users_total = count($users);
210
-		if ($users_total === 0) {
211
-			$output->writeln("<error>Please specify the user id to scan, \"--all\" to scan for all users or \"--path=...\"</error>");
212
-			return;
213
-		} else {
214
-			if ($users_total > 1) {
215
-				$output->writeln("\nScanning files for $users_total users");
216
-			}
217
-		}
208
+        # check quantity of users to be process and show it on the command line
209
+        $users_total = count($users);
210
+        if ($users_total === 0) {
211
+            $output->writeln("<error>Please specify the user id to scan, \"--all\" to scan for all users or \"--path=...\"</error>");
212
+            return;
213
+        } else {
214
+            if ($users_total > 1) {
215
+                $output->writeln("\nScanning files for $users_total users");
216
+            }
217
+        }
218 218
 
219
-		$this->initTools();
219
+        $this->initTools();
220 220
 
221
-		$user_count = 0;
222
-		foreach ($users as $user) {
223
-			if (is_object($user)) {
224
-				$user = $user->getUID();
225
-			}
226
-			$path = $inputPath ? $inputPath : '/' . $user;
227
-			$user_count += 1;
228
-			if ($this->userManager->userExists($user)) {
229
-				# add an extra line when verbose is set to optical separate users
230
-				if ($verbose) {
231
-					$output->writeln("");
232
-				}
233
-				$output->writeln("Starting scan for user $user_count out of $users_total ($user)");
234
-				# full: printout data if $verbose was set
235
-				$this->scanFiles($user, $path, $verbose, $output, $input->getOption('unscanned'));
236
-			} else {
237
-				$output->writeln("<error>Unknown user $user_count $user</error>");
238
-			}
239
-			# check on each user if there was a user interrupt (ctrl-c) and exit foreach
240
-			if ($this->hasBeenInterrupted()) {
241
-				break;
242
-			}
243
-		}
221
+        $user_count = 0;
222
+        foreach ($users as $user) {
223
+            if (is_object($user)) {
224
+                $user = $user->getUID();
225
+            }
226
+            $path = $inputPath ? $inputPath : '/' . $user;
227
+            $user_count += 1;
228
+            if ($this->userManager->userExists($user)) {
229
+                # add an extra line when verbose is set to optical separate users
230
+                if ($verbose) {
231
+                    $output->writeln("");
232
+                }
233
+                $output->writeln("Starting scan for user $user_count out of $users_total ($user)");
234
+                # full: printout data if $verbose was set
235
+                $this->scanFiles($user, $path, $verbose, $output, $input->getOption('unscanned'));
236
+            } else {
237
+                $output->writeln("<error>Unknown user $user_count $user</error>");
238
+            }
239
+            # check on each user if there was a user interrupt (ctrl-c) and exit foreach
240
+            if ($this->hasBeenInterrupted()) {
241
+                break;
242
+            }
243
+        }
244 244
 
245
-		# stat: printout statistics if $quiet was not set
246
-		if (!$quiet) {
247
-			$this->presentStats($output);
248
-		}
249
-	}
245
+        # stat: printout statistics if $quiet was not set
246
+        if (!$quiet) {
247
+            $this->presentStats($output);
248
+        }
249
+    }
250 250
 
251
-	/**
252
-	 * Initialises some useful tools for the Command
253
-	 */
254
-	protected function initTools() {
255
-		// Start the timer
256
-		$this->execTime = -microtime(true);
257
-		// Convert PHP errors to exceptions
258
-		set_error_handler([$this, 'exceptionErrorHandler'], E_ALL);
259
-	}
251
+    /**
252
+     * Initialises some useful tools for the Command
253
+     */
254
+    protected function initTools() {
255
+        // Start the timer
256
+        $this->execTime = -microtime(true);
257
+        // Convert PHP errors to exceptions
258
+        set_error_handler([$this, 'exceptionErrorHandler'], E_ALL);
259
+    }
260 260
 
261
-	/**
262
-	 * Processes PHP errors as exceptions in order to be able to keep track of problems
263
-	 *
264
-	 * @see https://secure.php.net/manual/en/function.set-error-handler.php
265
-	 *
266
-	 * @param int $severity the level of the error raised
267
-	 * @param string $message
268
-	 * @param string $file the filename that the error was raised in
269
-	 * @param int $line the line number the error was raised
270
-	 *
271
-	 * @throws \ErrorException
272
-	 */
273
-	public function exceptionErrorHandler($severity, $message, $file, $line) {
274
-		if (!(error_reporting() & $severity)) {
275
-			// This error code is not included in error_reporting
276
-			return;
277
-		}
278
-		throw new \ErrorException($message, 0, $severity, $file, $line);
279
-	}
261
+    /**
262
+     * Processes PHP errors as exceptions in order to be able to keep track of problems
263
+     *
264
+     * @see https://secure.php.net/manual/en/function.set-error-handler.php
265
+     *
266
+     * @param int $severity the level of the error raised
267
+     * @param string $message
268
+     * @param string $file the filename that the error was raised in
269
+     * @param int $line the line number the error was raised
270
+     *
271
+     * @throws \ErrorException
272
+     */
273
+    public function exceptionErrorHandler($severity, $message, $file, $line) {
274
+        if (!(error_reporting() & $severity)) {
275
+            // This error code is not included in error_reporting
276
+            return;
277
+        }
278
+        throw new \ErrorException($message, 0, $severity, $file, $line);
279
+    }
280 280
 
281
-	/**
282
-	 * @param OutputInterface $output
283
-	 */
284
-	protected function presentStats(OutputInterface $output) {
285
-		// Stop the timer
286
-		$this->execTime += microtime(true);
287
-		$output->writeln("");
281
+    /**
282
+     * @param OutputInterface $output
283
+     */
284
+    protected function presentStats(OutputInterface $output) {
285
+        // Stop the timer
286
+        $this->execTime += microtime(true);
287
+        $output->writeln("");
288 288
 
289
-		$headers = [
290
-			'Folders', 'Files', 'Elapsed time'
291
-		];
289
+        $headers = [
290
+            'Folders', 'Files', 'Elapsed time'
291
+        ];
292 292
 
293
-		$this->showSummary($headers, null, $output);
294
-	}
293
+        $this->showSummary($headers, null, $output);
294
+    }
295 295
 
296
-	/**
297
-	 * Shows a summary of operations
298
-	 *
299
-	 * @param string[] $headers
300
-	 * @param string[] $rows
301
-	 * @param OutputInterface $output
302
-	 */
303
-	protected function showSummary($headers, $rows, OutputInterface $output) {
304
-		$niceDate = $this->formatExecTime();
305
-		if (!$rows) {
306
-			$rows = [
307
-				$this->foldersCounter,
308
-				$this->filesCounter,
309
-				$niceDate,
310
-			];
311
-		}
312
-		$table = new Table($output);
313
-		$table
314
-			->setHeaders($headers)
315
-			->setRows([$rows]);
316
-		$table->render();
317
-	}
296
+    /**
297
+     * Shows a summary of operations
298
+     *
299
+     * @param string[] $headers
300
+     * @param string[] $rows
301
+     * @param OutputInterface $output
302
+     */
303
+    protected function showSummary($headers, $rows, OutputInterface $output) {
304
+        $niceDate = $this->formatExecTime();
305
+        if (!$rows) {
306
+            $rows = [
307
+                $this->foldersCounter,
308
+                $this->filesCounter,
309
+                $niceDate,
310
+            ];
311
+        }
312
+        $table = new Table($output);
313
+        $table
314
+            ->setHeaders($headers)
315
+            ->setRows([$rows]);
316
+        $table->render();
317
+    }
318 318
 
319 319
 
320
-	/**
321
-	 * Formats microtime into a human readable format
322
-	 *
323
-	 * @return string
324
-	 */
325
-	protected function formatExecTime() {
326
-		list($secs, ) = explode('.', sprintf("%.1f", ($this->execTime)));
320
+    /**
321
+     * Formats microtime into a human readable format
322
+     *
323
+     * @return string
324
+     */
325
+    protected function formatExecTime() {
326
+        list($secs, ) = explode('.', sprintf("%.1f", ($this->execTime)));
327 327
 
328
-		# if you want to have microseconds add this:   . '.' . $tens;
329
-		return date('H:i:s', $secs);
330
-	}
328
+        # if you want to have microseconds add this:   . '.' . $tens;
329
+        return date('H:i:s', $secs);
330
+    }
331 331
 
332
-	/**
333
-	 * @return \OCP\IDBConnection
334
-	 */
335
-	protected function reconnectToDatabase(OutputInterface $output) {
336
-		/** @var Connection | IDBConnection $connection */
337
-		$connection = \OC::$server->getDatabaseConnection();
338
-		try {
339
-			$connection->close();
340
-		} catch (\Exception $ex) {
341
-			$output->writeln("<info>Error while disconnecting from database: {$ex->getMessage()}</info>");
342
-		}
343
-		while (!$connection->isConnected()) {
344
-			try {
345
-				$connection->connect();
346
-			} catch (\Exception $ex) {
347
-				$output->writeln("<info>Error while re-connecting to database: {$ex->getMessage()}</info>");
348
-				sleep(60);
349
-			}
350
-		}
351
-		return $connection;
352
-	}
332
+    /**
333
+     * @return \OCP\IDBConnection
334
+     */
335
+    protected function reconnectToDatabase(OutputInterface $output) {
336
+        /** @var Connection | IDBConnection $connection */
337
+        $connection = \OC::$server->getDatabaseConnection();
338
+        try {
339
+            $connection->close();
340
+        } catch (\Exception $ex) {
341
+            $output->writeln("<info>Error while disconnecting from database: {$ex->getMessage()}</info>");
342
+        }
343
+        while (!$connection->isConnected()) {
344
+            try {
345
+                $connection->connect();
346
+            } catch (\Exception $ex) {
347
+                $output->writeln("<info>Error while re-connecting to database: {$ex->getMessage()}</info>");
348
+                sleep(60);
349
+            }
350
+        }
351
+        return $connection;
352
+    }
353 353
 
354 354
 }
Please login to merge, or discard this patch.
Spacing   +15 added lines, -15 removed lines patch added patch discarded remove patch
@@ -106,7 +106,7 @@  discard block
 block discarded – undo
106 106
 		$path = basename($fullPath);
107 107
 
108 108
 		if ($normalizedPath !== $path) {
109
-			$output->writeln("\t<error>Entry \"" . $fullPath . '" will not be accessible due to incompatible encoding</error>');
109
+			$output->writeln("\t<error>Entry \"".$fullPath.'" will not be accessible due to incompatible encoding</error>');
110 110
 		}
111 111
 	}
112 112
 
@@ -116,42 +116,42 @@  discard block
 block discarded – undo
116 116
 		# check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception
117 117
 		# printout and count
118 118
 		if ($verbose) {
119
-			$scanner->listen(Scanner::class, 'scanFile', function ($path) use ($output) {
119
+			$scanner->listen(Scanner::class, 'scanFile', function($path) use ($output) {
120 120
 				$output->writeln("\tFile   <info>$path</info>");
121 121
 				$this->filesCounter += 1;
122 122
 				if ($this->hasBeenInterrupted()) {
123 123
 					throw new InterruptedException();
124 124
 				}
125 125
 			});
126
-			$scanner->listen(Scanner::class, 'scanFolder', function ($path) use ($output) {
126
+			$scanner->listen(Scanner::class, 'scanFolder', function($path) use ($output) {
127 127
 				$output->writeln("\tFolder <info>$path</info>");
128 128
 				$this->foldersCounter += 1;
129 129
 				if ($this->hasBeenInterrupted()) {
130 130
 					throw new InterruptedException();
131 131
 				}
132 132
 			});
133
-			$scanner->listen(Scanner::class, 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) {
134
-				$output->writeln("Error while scanning, storage not available (" . $e->getMessage() . ")");
133
+			$scanner->listen(Scanner::class, 'StorageNotAvailable', function(StorageNotAvailableException $e) use ($output) {
134
+				$output->writeln("Error while scanning, storage not available (".$e->getMessage().")");
135 135
 			});
136 136
 			# count only
137 137
 		} else {
138
-			$scanner->listen(Scanner::class, 'scanFile', function () use ($output) {
138
+			$scanner->listen(Scanner::class, 'scanFile', function() use ($output) {
139 139
 				$this->filesCounter += 1;
140 140
 				if ($this->hasBeenInterrupted()) {
141 141
 					throw new InterruptedException();
142 142
 				}
143 143
 			});
144
-			$scanner->listen(Scanner::class, 'scanFolder', function () use ($output) {
144
+			$scanner->listen(Scanner::class, 'scanFolder', function() use ($output) {
145 145
 				$this->foldersCounter += 1;
146 146
 				if ($this->hasBeenInterrupted()) {
147 147
 					throw new InterruptedException();
148 148
 				}
149 149
 			});
150 150
 		}
151
-		$scanner->listen(Scanner::class, 'scanFile', function ($path) use ($output) {
151
+		$scanner->listen(Scanner::class, 'scanFile', function($path) use ($output) {
152 152
 			$this->checkScanWarning($path, $output);
153 153
 		});
154
-		$scanner->listen(Scanner::class, 'scanFolder', function ($path) use ($output) {
154
+		$scanner->listen(Scanner::class, 'scanFolder', function($path) use ($output) {
155 155
 			$this->checkScanWarning($path, $output);
156 156
 		});
157 157
 
@@ -168,10 +168,10 @@  discard block
 block discarded – undo
168 168
 			# exit the function if ctrl-c has been pressed
169 169
 			$output->writeln('Interrupted by user');
170 170
 		} catch (NotFoundException $e) {
171
-			$output->writeln('<error>Path not found: ' . $e->getMessage() . '</error>');
171
+			$output->writeln('<error>Path not found: '.$e->getMessage().'</error>');
172 172
 		} catch (\Exception $e) {
173
-			$output->writeln('<error>Exception during scan: ' . $e->getMessage() . '</error>');
174
-			$output->writeln('<error>' . $e->getTraceAsString() . '</error>');
173
+			$output->writeln('<error>Exception during scan: '.$e->getMessage().'</error>');
174
+			$output->writeln('<error>'.$e->getTraceAsString().'</error>');
175 175
 		}
176 176
 	}
177 177
 
@@ -179,7 +179,7 @@  discard block
 block discarded – undo
179 179
 	protected function execute(InputInterface $input, OutputInterface $output) {
180 180
 		$inputPath = $input->getOption('path');
181 181
 		if ($inputPath) {
182
-			$inputPath = '/' . trim($inputPath, '/');
182
+			$inputPath = '/'.trim($inputPath, '/');
183 183
 			list (, $user,) = explode('/', $inputPath, 3);
184 184
 			$users = array($user);
185 185
 		} else if ($input->getOption('all')) {
@@ -223,7 +223,7 @@  discard block
 block discarded – undo
223 223
 			if (is_object($user)) {
224 224
 				$user = $user->getUID();
225 225
 			}
226
-			$path = $inputPath ? $inputPath : '/' . $user;
226
+			$path = $inputPath ? $inputPath : '/'.$user;
227 227
 			$user_count += 1;
228 228
 			if ($this->userManager->userExists($user)) {
229 229
 				# add an extra line when verbose is set to optical separate users
@@ -323,7 +323,7 @@  discard block
 block discarded – undo
323 323
 	 * @return string
324 324
 	 */
325 325
 	protected function formatExecTime() {
326
-		list($secs, ) = explode('.', sprintf("%.1f", ($this->execTime)));
326
+		list($secs,) = explode('.', sprintf("%.1f", ($this->execTime)));
327 327
 
328 328
 		# if you want to have microseconds add this:   . '.' . $tens;
329 329
 		return date('H:i:s', $secs);
Please login to merge, or discard this patch.