Completed
Push — stable13 ( f62775...bea70d )
by Roeland
12:09
created
apps/files_sharing/lib/Controller/ShareAPIController.php 1 patch
Indentation   +891 added lines, -891 removed lines patch added patch discarded remove patch
@@ -57,904 +57,904 @@
 block discarded – undo
57 57
  */
58 58
 class ShareAPIController extends OCSController {
59 59
 
60
-	/** @var IManager */
61
-	private $shareManager;
62
-	/** @var IGroupManager */
63
-	private $groupManager;
64
-	/** @var IUserManager */
65
-	private $userManager;
66
-	/** @var IRequest */
67
-	protected $request;
68
-	/** @var IRootFolder */
69
-	private $rootFolder;
70
-	/** @var IURLGenerator */
71
-	private $urlGenerator;
72
-	/** @var string */
73
-	private $currentUser;
74
-	/** @var IL10N */
75
-	private $l;
76
-	/** @var \OCP\Files\Node */
77
-	private $lockedNode;
78
-
79
-	/**
80
-	 * Share20OCS constructor.
81
-	 *
82
-	 * @param string $appName
83
-	 * @param IRequest $request
84
-	 * @param IManager $shareManager
85
-	 * @param IGroupManager $groupManager
86
-	 * @param IUserManager $userManager
87
-	 * @param IRootFolder $rootFolder
88
-	 * @param IURLGenerator $urlGenerator
89
-	 * @param string $userId
90
-	 * @param IL10N $l10n
91
-	 */
92
-	public function __construct(
93
-		$appName,
94
-		IRequest $request,
95
-		IManager $shareManager,
96
-		IGroupManager $groupManager,
97
-		IUserManager $userManager,
98
-		IRootFolder $rootFolder,
99
-		IURLGenerator $urlGenerator,
100
-		$userId,
101
-		IL10N $l10n
102
-	) {
103
-		parent::__construct($appName, $request);
104
-
105
-		$this->shareManager = $shareManager;
106
-		$this->userManager = $userManager;
107
-		$this->groupManager = $groupManager;
108
-		$this->request = $request;
109
-		$this->rootFolder = $rootFolder;
110
-		$this->urlGenerator = $urlGenerator;
111
-		$this->currentUser = $userId;
112
-		$this->l = $l10n;
113
-	}
114
-
115
-	/**
116
-	 * Convert an IShare to an array for OCS output
117
-	 *
118
-	 * @param \OCP\Share\IShare $share
119
-	 * @param Node|null $recipientNode
120
-	 * @return array
121
-	 * @throws NotFoundException In case the node can't be resolved.
122
-	 */
123
-	protected function formatShare(\OCP\Share\IShare $share, Node $recipientNode = null) {
124
-		$sharedBy = $this->userManager->get($share->getSharedBy());
125
-		$shareOwner = $this->userManager->get($share->getShareOwner());
126
-
127
-		$result = [
128
-			'id' => $share->getId(),
129
-			'share_type' => $share->getShareType(),
130
-			'uid_owner' => $share->getSharedBy(),
131
-			'displayname_owner' => $sharedBy !== null ? $sharedBy->getDisplayName() : $share->getSharedBy(),
132
-			'permissions' => $share->getPermissions(),
133
-			'stime' => $share->getShareTime()->getTimestamp(),
134
-			'parent' => null,
135
-			'expiration' => null,
136
-			'token' => null,
137
-			'uid_file_owner' => $share->getShareOwner(),
138
-			'displayname_file_owner' => $shareOwner !== null ? $shareOwner->getDisplayName() : $share->getShareOwner(),
139
-		];
140
-
141
-		$userFolder = $this->rootFolder->getUserFolder($this->currentUser);
142
-		if ($recipientNode) {
143
-			$node = $recipientNode;
144
-		} else {
145
-			$nodes = $userFolder->getById($share->getNodeId());
146
-
147
-			if (empty($nodes)) {
148
-				// fallback to guessing the path
149
-				$node = $userFolder->get($share->getTarget());
150
-				if ($node === null || $share->getTarget() === '') {
151
-					throw new NotFoundException();
152
-				}
153
-			} else {
154
-				$node = $nodes[0];
155
-			}
156
-		}
157
-
158
-		$result['path'] = $userFolder->getRelativePath($node->getPath());
159
-		if ($node instanceOf \OCP\Files\Folder) {
160
-			$result['item_type'] = 'folder';
161
-		} else {
162
-			$result['item_type'] = 'file';
163
-		}
164
-		$result['mimetype'] = $node->getMimetype();
165
-		$result['storage_id'] = $node->getStorage()->getId();
166
-		$result['storage'] = $node->getStorage()->getCache()->getNumericStorageId();
167
-		$result['item_source'] = $node->getId();
168
-		$result['file_source'] = $node->getId();
169
-		$result['file_parent'] = $node->getParent()->getId();
170
-		$result['file_target'] = $share->getTarget();
171
-
172
-		$expiration = $share->getExpirationDate();
173
-		if ($expiration !== null) {
174
-			$result['expiration'] = $expiration->format('Y-m-d 00:00:00');
175
-		}
176
-
177
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
178
-			$sharedWith = $this->userManager->get($share->getSharedWith());
179
-			$result['share_with'] = $share->getSharedWith();
180
-			$result['share_with_displayname'] = $sharedWith !== null ? $sharedWith->getDisplayName() : $share->getSharedWith();
181
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
182
-			$group = $this->groupManager->get($share->getSharedWith());
183
-			$result['share_with'] = $share->getSharedWith();
184
-			$result['share_with_displayname'] = $group !== null ? $group->getDisplayName() : $share->getSharedWith();
185
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
186
-
187
-			$result['share_with'] = $share->getPassword();
188
-			$result['share_with_displayname'] = $share->getPassword();
189
-
190
-			$result['token'] = $share->getToken();
191
-			$result['url'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $share->getToken()]);
192
-
193
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE) {
194
-			$result['share_with'] = $share->getSharedWith();
195
-			$result['share_with_displayname'] = $this->getDisplayNameFromAddressBook($share->getSharedWith(), 'CLOUD');
196
-			$result['token'] = $share->getToken();
197
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
198
-			$result['share_with'] = $share->getSharedWith();
199
-			$result['password'] = $share->getPassword();
200
-			$result['share_with_displayname'] = $this->getDisplayNameFromAddressBook($share->getSharedWith(), 'EMAIL');
201
-			$result['token'] = $share->getToken();
202
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_CIRCLE) {
203
-			// getSharedWith() returns either "name (type, owner)" or
204
-			// "name (type, owner) [id]", depending on the Circles app version.
205
-			$hasCircleId = (substr($share->getSharedWith(), -1) === ']');
206
-
207
-			$displayNameLength = ($hasCircleId? strrpos($share->getSharedWith(), ' '): strlen($share->getSharedWith()));
208
-			$result['share_with_displayname'] = substr($share->getSharedWith(), 0, $displayNameLength);
209
-
210
-			$shareWithStart = ($hasCircleId? strrpos($share->getSharedWith(), '[') + 1: 0);
211
-			$shareWithLength = ($hasCircleId? -1: strpos($share->getSharedWith(), ' '));
212
-			$result['share_with'] = substr($share->getSharedWith(), $shareWithStart, $shareWithLength);
213
-		}
214
-
215
-
216
-		$result['mail_send'] = $share->getMailSend() ? 1 : 0;
217
-
218
-		return $result;
219
-	}
220
-
221
-	/**
222
-	 * Check if one of the users address books knows the exact property, if
223
-	 * yes we return the full name.
224
-	 *
225
-	 * @param string $query
226
-	 * @param string $property
227
-	 * @return string
228
-	 */
229
-	private function getDisplayNameFromAddressBook($query, $property) {
230
-		// FIXME: If we inject the contacts manager it gets initialized bofore any address books are registered
231
-		$result = \OC::$server->getContactsManager()->search($query, [$property]);
232
-		foreach ($result as $r) {
233
-			foreach($r[$property] as $value) {
234
-				if ($value === $query) {
235
-					return $r['FN'];
236
-				}
237
-			}
238
-		}
239
-
240
-		return $query;
241
-	}
242
-
243
-	/**
244
-	 * Get a specific share by id
245
-	 *
246
-	 * @NoAdminRequired
247
-	 *
248
-	 * @param string $id
249
-	 * @return DataResponse
250
-	 * @throws OCSNotFoundException
251
-	 */
252
-	public function getShare($id) {
253
-		try {
254
-			$share = $this->getShareById($id);
255
-		} catch (ShareNotFound $e) {
256
-			throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
257
-		}
258
-
259
-		if ($this->canAccessShare($share)) {
260
-			try {
261
-				$share = $this->formatShare($share);
262
-				return new DataResponse([$share]);
263
-			} catch (NotFoundException $e) {
264
-				//Fall trough
265
-			}
266
-		}
267
-
268
-		throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
269
-	}
270
-
271
-	/**
272
-	 * Delete a share
273
-	 *
274
-	 * @NoAdminRequired
275
-	 *
276
-	 * @param string $id
277
-	 * @return DataResponse
278
-	 * @throws OCSNotFoundException
279
-	 */
280
-	public function deleteShare($id) {
281
-		try {
282
-			$share = $this->getShareById($id);
283
-		} catch (ShareNotFound $e) {
284
-			throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
285
-		}
286
-
287
-		try {
288
-			$this->lock($share->getNode());
289
-		} catch (LockedException $e) {
290
-			throw new OCSNotFoundException($this->l->t('could not delete share'));
291
-		}
292
-
293
-		if (!$this->canAccessShare($share)) {
294
-			throw new OCSNotFoundException($this->l->t('Could not delete share'));
295
-		}
296
-
297
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP &&
298
-			$share->getShareOwner() !== $this->currentUser &&
299
-			$share->getSharedBy() !== $this->currentUser) {
300
-			$this->shareManager->deleteFromSelf($share, $this->currentUser);
301
-		} else {
302
-			$this->shareManager->deleteShare($share);
303
-		}
304
-
305
-		return new DataResponse();
306
-	}
307
-
308
-	/**
309
-	 * @NoAdminRequired
310
-	 *
311
-	 * @param string $path
312
-	 * @param int $permissions
313
-	 * @param int $shareType
314
-	 * @param string $shareWith
315
-	 * @param string $publicUpload
316
-	 * @param string $password
317
-	 * @param string $expireDate
318
-	 *
319
-	 * @return DataResponse
320
-	 * @throws OCSNotFoundException
321
-	 * @throws OCSForbiddenException
322
-	 * @throws OCSBadRequestException
323
-	 * @throws OCSException
324
-	 *
325
-	 * @suppress PhanUndeclaredClassMethod
326
-	 */
327
-	public function createShare(
328
-		$path = null,
329
-		$permissions = \OCP\Constants::PERMISSION_ALL,
330
-		$shareType = -1,
331
-		$shareWith = null,
332
-		$publicUpload = 'false',
333
-		$password = '',
334
-		$expireDate = ''
335
-	) {
336
-		$share = $this->shareManager->newShare();
337
-
338
-		// Verify path
339
-		if ($path === null) {
340
-			throw new OCSNotFoundException($this->l->t('Please specify a file or folder path'));
341
-		}
342
-
343
-		$userFolder = $this->rootFolder->getUserFolder($this->currentUser);
344
-		try {
345
-			$path = $userFolder->get($path);
346
-		} catch (NotFoundException $e) {
347
-			throw new OCSNotFoundException($this->l->t('Wrong path, file/folder doesn\'t exist'));
348
-		}
349
-
350
-		$share->setNode($path);
351
-
352
-		try {
353
-			$this->lock($share->getNode());
354
-		} catch (LockedException $e) {
355
-			throw new OCSNotFoundException($this->l->t('Could not create share'));
356
-		}
357
-
358
-		if ($permissions < 0 || $permissions > \OCP\Constants::PERMISSION_ALL) {
359
-			throw new OCSNotFoundException($this->l->t('invalid permissions'));
360
-		}
361
-
362
-		// Shares always require read permissions
363
-		$permissions |= \OCP\Constants::PERMISSION_READ;
364
-
365
-		if ($path instanceof \OCP\Files\File) {
366
-			// Single file shares should never have delete or create permissions
367
-			$permissions &= ~\OCP\Constants::PERMISSION_DELETE;
368
-			$permissions &= ~\OCP\Constants::PERMISSION_CREATE;
369
-		}
370
-
371
-		/*
60
+    /** @var IManager */
61
+    private $shareManager;
62
+    /** @var IGroupManager */
63
+    private $groupManager;
64
+    /** @var IUserManager */
65
+    private $userManager;
66
+    /** @var IRequest */
67
+    protected $request;
68
+    /** @var IRootFolder */
69
+    private $rootFolder;
70
+    /** @var IURLGenerator */
71
+    private $urlGenerator;
72
+    /** @var string */
73
+    private $currentUser;
74
+    /** @var IL10N */
75
+    private $l;
76
+    /** @var \OCP\Files\Node */
77
+    private $lockedNode;
78
+
79
+    /**
80
+     * Share20OCS constructor.
81
+     *
82
+     * @param string $appName
83
+     * @param IRequest $request
84
+     * @param IManager $shareManager
85
+     * @param IGroupManager $groupManager
86
+     * @param IUserManager $userManager
87
+     * @param IRootFolder $rootFolder
88
+     * @param IURLGenerator $urlGenerator
89
+     * @param string $userId
90
+     * @param IL10N $l10n
91
+     */
92
+    public function __construct(
93
+        $appName,
94
+        IRequest $request,
95
+        IManager $shareManager,
96
+        IGroupManager $groupManager,
97
+        IUserManager $userManager,
98
+        IRootFolder $rootFolder,
99
+        IURLGenerator $urlGenerator,
100
+        $userId,
101
+        IL10N $l10n
102
+    ) {
103
+        parent::__construct($appName, $request);
104
+
105
+        $this->shareManager = $shareManager;
106
+        $this->userManager = $userManager;
107
+        $this->groupManager = $groupManager;
108
+        $this->request = $request;
109
+        $this->rootFolder = $rootFolder;
110
+        $this->urlGenerator = $urlGenerator;
111
+        $this->currentUser = $userId;
112
+        $this->l = $l10n;
113
+    }
114
+
115
+    /**
116
+     * Convert an IShare to an array for OCS output
117
+     *
118
+     * @param \OCP\Share\IShare $share
119
+     * @param Node|null $recipientNode
120
+     * @return array
121
+     * @throws NotFoundException In case the node can't be resolved.
122
+     */
123
+    protected function formatShare(\OCP\Share\IShare $share, Node $recipientNode = null) {
124
+        $sharedBy = $this->userManager->get($share->getSharedBy());
125
+        $shareOwner = $this->userManager->get($share->getShareOwner());
126
+
127
+        $result = [
128
+            'id' => $share->getId(),
129
+            'share_type' => $share->getShareType(),
130
+            'uid_owner' => $share->getSharedBy(),
131
+            'displayname_owner' => $sharedBy !== null ? $sharedBy->getDisplayName() : $share->getSharedBy(),
132
+            'permissions' => $share->getPermissions(),
133
+            'stime' => $share->getShareTime()->getTimestamp(),
134
+            'parent' => null,
135
+            'expiration' => null,
136
+            'token' => null,
137
+            'uid_file_owner' => $share->getShareOwner(),
138
+            'displayname_file_owner' => $shareOwner !== null ? $shareOwner->getDisplayName() : $share->getShareOwner(),
139
+        ];
140
+
141
+        $userFolder = $this->rootFolder->getUserFolder($this->currentUser);
142
+        if ($recipientNode) {
143
+            $node = $recipientNode;
144
+        } else {
145
+            $nodes = $userFolder->getById($share->getNodeId());
146
+
147
+            if (empty($nodes)) {
148
+                // fallback to guessing the path
149
+                $node = $userFolder->get($share->getTarget());
150
+                if ($node === null || $share->getTarget() === '') {
151
+                    throw new NotFoundException();
152
+                }
153
+            } else {
154
+                $node = $nodes[0];
155
+            }
156
+        }
157
+
158
+        $result['path'] = $userFolder->getRelativePath($node->getPath());
159
+        if ($node instanceOf \OCP\Files\Folder) {
160
+            $result['item_type'] = 'folder';
161
+        } else {
162
+            $result['item_type'] = 'file';
163
+        }
164
+        $result['mimetype'] = $node->getMimetype();
165
+        $result['storage_id'] = $node->getStorage()->getId();
166
+        $result['storage'] = $node->getStorage()->getCache()->getNumericStorageId();
167
+        $result['item_source'] = $node->getId();
168
+        $result['file_source'] = $node->getId();
169
+        $result['file_parent'] = $node->getParent()->getId();
170
+        $result['file_target'] = $share->getTarget();
171
+
172
+        $expiration = $share->getExpirationDate();
173
+        if ($expiration !== null) {
174
+            $result['expiration'] = $expiration->format('Y-m-d 00:00:00');
175
+        }
176
+
177
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
178
+            $sharedWith = $this->userManager->get($share->getSharedWith());
179
+            $result['share_with'] = $share->getSharedWith();
180
+            $result['share_with_displayname'] = $sharedWith !== null ? $sharedWith->getDisplayName() : $share->getSharedWith();
181
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
182
+            $group = $this->groupManager->get($share->getSharedWith());
183
+            $result['share_with'] = $share->getSharedWith();
184
+            $result['share_with_displayname'] = $group !== null ? $group->getDisplayName() : $share->getSharedWith();
185
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
186
+
187
+            $result['share_with'] = $share->getPassword();
188
+            $result['share_with_displayname'] = $share->getPassword();
189
+
190
+            $result['token'] = $share->getToken();
191
+            $result['url'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $share->getToken()]);
192
+
193
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE) {
194
+            $result['share_with'] = $share->getSharedWith();
195
+            $result['share_with_displayname'] = $this->getDisplayNameFromAddressBook($share->getSharedWith(), 'CLOUD');
196
+            $result['token'] = $share->getToken();
197
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
198
+            $result['share_with'] = $share->getSharedWith();
199
+            $result['password'] = $share->getPassword();
200
+            $result['share_with_displayname'] = $this->getDisplayNameFromAddressBook($share->getSharedWith(), 'EMAIL');
201
+            $result['token'] = $share->getToken();
202
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_CIRCLE) {
203
+            // getSharedWith() returns either "name (type, owner)" or
204
+            // "name (type, owner) [id]", depending on the Circles app version.
205
+            $hasCircleId = (substr($share->getSharedWith(), -1) === ']');
206
+
207
+            $displayNameLength = ($hasCircleId? strrpos($share->getSharedWith(), ' '): strlen($share->getSharedWith()));
208
+            $result['share_with_displayname'] = substr($share->getSharedWith(), 0, $displayNameLength);
209
+
210
+            $shareWithStart = ($hasCircleId? strrpos($share->getSharedWith(), '[') + 1: 0);
211
+            $shareWithLength = ($hasCircleId? -1: strpos($share->getSharedWith(), ' '));
212
+            $result['share_with'] = substr($share->getSharedWith(), $shareWithStart, $shareWithLength);
213
+        }
214
+
215
+
216
+        $result['mail_send'] = $share->getMailSend() ? 1 : 0;
217
+
218
+        return $result;
219
+    }
220
+
221
+    /**
222
+     * Check if one of the users address books knows the exact property, if
223
+     * yes we return the full name.
224
+     *
225
+     * @param string $query
226
+     * @param string $property
227
+     * @return string
228
+     */
229
+    private function getDisplayNameFromAddressBook($query, $property) {
230
+        // FIXME: If we inject the contacts manager it gets initialized bofore any address books are registered
231
+        $result = \OC::$server->getContactsManager()->search($query, [$property]);
232
+        foreach ($result as $r) {
233
+            foreach($r[$property] as $value) {
234
+                if ($value === $query) {
235
+                    return $r['FN'];
236
+                }
237
+            }
238
+        }
239
+
240
+        return $query;
241
+    }
242
+
243
+    /**
244
+     * Get a specific share by id
245
+     *
246
+     * @NoAdminRequired
247
+     *
248
+     * @param string $id
249
+     * @return DataResponse
250
+     * @throws OCSNotFoundException
251
+     */
252
+    public function getShare($id) {
253
+        try {
254
+            $share = $this->getShareById($id);
255
+        } catch (ShareNotFound $e) {
256
+            throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
257
+        }
258
+
259
+        if ($this->canAccessShare($share)) {
260
+            try {
261
+                $share = $this->formatShare($share);
262
+                return new DataResponse([$share]);
263
+            } catch (NotFoundException $e) {
264
+                //Fall trough
265
+            }
266
+        }
267
+
268
+        throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
269
+    }
270
+
271
+    /**
272
+     * Delete a share
273
+     *
274
+     * @NoAdminRequired
275
+     *
276
+     * @param string $id
277
+     * @return DataResponse
278
+     * @throws OCSNotFoundException
279
+     */
280
+    public function deleteShare($id) {
281
+        try {
282
+            $share = $this->getShareById($id);
283
+        } catch (ShareNotFound $e) {
284
+            throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
285
+        }
286
+
287
+        try {
288
+            $this->lock($share->getNode());
289
+        } catch (LockedException $e) {
290
+            throw new OCSNotFoundException($this->l->t('could not delete share'));
291
+        }
292
+
293
+        if (!$this->canAccessShare($share)) {
294
+            throw new OCSNotFoundException($this->l->t('Could not delete share'));
295
+        }
296
+
297
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP &&
298
+            $share->getShareOwner() !== $this->currentUser &&
299
+            $share->getSharedBy() !== $this->currentUser) {
300
+            $this->shareManager->deleteFromSelf($share, $this->currentUser);
301
+        } else {
302
+            $this->shareManager->deleteShare($share);
303
+        }
304
+
305
+        return new DataResponse();
306
+    }
307
+
308
+    /**
309
+     * @NoAdminRequired
310
+     *
311
+     * @param string $path
312
+     * @param int $permissions
313
+     * @param int $shareType
314
+     * @param string $shareWith
315
+     * @param string $publicUpload
316
+     * @param string $password
317
+     * @param string $expireDate
318
+     *
319
+     * @return DataResponse
320
+     * @throws OCSNotFoundException
321
+     * @throws OCSForbiddenException
322
+     * @throws OCSBadRequestException
323
+     * @throws OCSException
324
+     *
325
+     * @suppress PhanUndeclaredClassMethod
326
+     */
327
+    public function createShare(
328
+        $path = null,
329
+        $permissions = \OCP\Constants::PERMISSION_ALL,
330
+        $shareType = -1,
331
+        $shareWith = null,
332
+        $publicUpload = 'false',
333
+        $password = '',
334
+        $expireDate = ''
335
+    ) {
336
+        $share = $this->shareManager->newShare();
337
+
338
+        // Verify path
339
+        if ($path === null) {
340
+            throw new OCSNotFoundException($this->l->t('Please specify a file or folder path'));
341
+        }
342
+
343
+        $userFolder = $this->rootFolder->getUserFolder($this->currentUser);
344
+        try {
345
+            $path = $userFolder->get($path);
346
+        } catch (NotFoundException $e) {
347
+            throw new OCSNotFoundException($this->l->t('Wrong path, file/folder doesn\'t exist'));
348
+        }
349
+
350
+        $share->setNode($path);
351
+
352
+        try {
353
+            $this->lock($share->getNode());
354
+        } catch (LockedException $e) {
355
+            throw new OCSNotFoundException($this->l->t('Could not create share'));
356
+        }
357
+
358
+        if ($permissions < 0 || $permissions > \OCP\Constants::PERMISSION_ALL) {
359
+            throw new OCSNotFoundException($this->l->t('invalid permissions'));
360
+        }
361
+
362
+        // Shares always require read permissions
363
+        $permissions |= \OCP\Constants::PERMISSION_READ;
364
+
365
+        if ($path instanceof \OCP\Files\File) {
366
+            // Single file shares should never have delete or create permissions
367
+            $permissions &= ~\OCP\Constants::PERMISSION_DELETE;
368
+            $permissions &= ~\OCP\Constants::PERMISSION_CREATE;
369
+        }
370
+
371
+        /*
372 372
 		 * Hack for https://github.com/owncloud/core/issues/22587
373 373
 		 * We check the permissions via webdav. But the permissions of the mount point
374 374
 		 * do not equal the share permissions. Here we fix that for federated mounts.
375 375
 		 */
376
-		if ($path->getStorage()->instanceOfStorage('OCA\Files_Sharing\External\Storage')) {
377
-			$permissions &= ~($permissions & ~$path->getPermissions());
378
-		}
379
-
380
-		if ($shareType === \OCP\Share::SHARE_TYPE_USER) {
381
-			// Valid user is required to share
382
-			if ($shareWith === null || !$this->userManager->userExists($shareWith)) {
383
-				throw new OCSNotFoundException($this->l->t('Please specify a valid user'));
384
-			}
385
-			$share->setSharedWith($shareWith);
386
-			$share->setPermissions($permissions);
387
-		} else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
388
-			if (!$this->shareManager->allowGroupSharing()) {
389
-				throw new OCSNotFoundException($this->l->t('Group sharing is disabled by the administrator'));
390
-			}
391
-
392
-			// Valid group is required to share
393
-			if ($shareWith === null || !$this->groupManager->groupExists($shareWith)) {
394
-				throw new OCSNotFoundException($this->l->t('Please specify a valid group'));
395
-			}
396
-			$share->setSharedWith($shareWith);
397
-			$share->setPermissions($permissions);
398
-		} else if ($shareType === \OCP\Share::SHARE_TYPE_LINK) {
399
-			//Can we even share links?
400
-			if (!$this->shareManager->shareApiAllowLinks()) {
401
-				throw new OCSNotFoundException($this->l->t('Public link sharing is disabled by the administrator'));
402
-			}
403
-
404
-			/*
376
+        if ($path->getStorage()->instanceOfStorage('OCA\Files_Sharing\External\Storage')) {
377
+            $permissions &= ~($permissions & ~$path->getPermissions());
378
+        }
379
+
380
+        if ($shareType === \OCP\Share::SHARE_TYPE_USER) {
381
+            // Valid user is required to share
382
+            if ($shareWith === null || !$this->userManager->userExists($shareWith)) {
383
+                throw new OCSNotFoundException($this->l->t('Please specify a valid user'));
384
+            }
385
+            $share->setSharedWith($shareWith);
386
+            $share->setPermissions($permissions);
387
+        } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
388
+            if (!$this->shareManager->allowGroupSharing()) {
389
+                throw new OCSNotFoundException($this->l->t('Group sharing is disabled by the administrator'));
390
+            }
391
+
392
+            // Valid group is required to share
393
+            if ($shareWith === null || !$this->groupManager->groupExists($shareWith)) {
394
+                throw new OCSNotFoundException($this->l->t('Please specify a valid group'));
395
+            }
396
+            $share->setSharedWith($shareWith);
397
+            $share->setPermissions($permissions);
398
+        } else if ($shareType === \OCP\Share::SHARE_TYPE_LINK) {
399
+            //Can we even share links?
400
+            if (!$this->shareManager->shareApiAllowLinks()) {
401
+                throw new OCSNotFoundException($this->l->t('Public link sharing is disabled by the administrator'));
402
+            }
403
+
404
+            /*
405 405
 			 * For now we only allow 1 link share.
406 406
 			 * Return the existing link share if this is a duplicate
407 407
 			 */
408
-			$existingShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_LINK, $path, false, 1, 0);
409
-			if (!empty($existingShares)) {
410
-				return new DataResponse($this->formatShare($existingShares[0]));
411
-			}
412
-
413
-			if ($publicUpload === 'true') {
414
-				// Check if public upload is allowed
415
-				if (!$this->shareManager->shareApiLinkAllowPublicUpload()) {
416
-					throw new OCSForbiddenException($this->l->t('Public upload disabled by the administrator'));
417
-				}
418
-
419
-				// Public upload can only be set for folders
420
-				if ($path instanceof \OCP\Files\File) {
421
-					throw new OCSNotFoundException($this->l->t('Public upload is only possible for publicly shared folders'));
422
-				}
423
-
424
-				$share->setPermissions(
425
-					\OCP\Constants::PERMISSION_READ |
426
-					\OCP\Constants::PERMISSION_CREATE |
427
-					\OCP\Constants::PERMISSION_UPDATE |
428
-					\OCP\Constants::PERMISSION_DELETE
429
-				);
430
-			} else {
431
-				$share->setPermissions(\OCP\Constants::PERMISSION_READ);
432
-			}
433
-
434
-			// Set password
435
-			if ($password !== '') {
436
-				$share->setPassword($password);
437
-			}
438
-
439
-			//Expire date
440
-			if ($expireDate !== '') {
441
-				try {
442
-					$expireDate = $this->parseDate($expireDate);
443
-					$share->setExpirationDate($expireDate);
444
-				} catch (\Exception $e) {
445
-					throw new OCSNotFoundException($this->l->t('Invalid date, date format must be YYYY-MM-DD'));
446
-				}
447
-			}
448
-
449
-		} else if ($shareType === \OCP\Share::SHARE_TYPE_REMOTE) {
450
-			if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) {
451
-				throw new OCSForbiddenException($this->l->t('Sharing %s failed because the back end does not allow shares from type %s', [$path->getPath(), $shareType]));
452
-			}
453
-
454
-			$share->setSharedWith($shareWith);
455
-			$share->setPermissions($permissions);
456
-		} else if ($shareType === \OCP\Share::SHARE_TYPE_EMAIL) {
457
-			if ($share->getNodeType() === 'file') {
458
-				$share->setPermissions(\OCP\Constants::PERMISSION_READ);
459
-			} else {
460
-				$share->setPermissions(
461
-					\OCP\Constants::PERMISSION_READ |
462
-					\OCP\Constants::PERMISSION_CREATE |
463
-					\OCP\Constants::PERMISSION_UPDATE |
464
-					\OCP\Constants::PERMISSION_DELETE);
465
-			}
466
-			$share->setSharedWith($shareWith);
467
-		} else if ($shareType === \OCP\Share::SHARE_TYPE_CIRCLE) {
468
-			if (!\OC::$server->getAppManager()->isEnabledForUser('circles') || !class_exists('\OCA\Circles\ShareByCircleProvider')) {
469
-				throw new OCSNotFoundException($this->l->t('You cannot share to a Circle if the app is not enabled'));
470
-			}
471
-
472
-			$circle = \OCA\Circles\Api\v1\Circles::detailsCircle($shareWith);
473
-
474
-			// Valid circle is required to share
475
-			if ($circle === null) {
476
-				throw new OCSNotFoundException($this->l->t('Please specify a valid circle'));
477
-			}
478
-			$share->setSharedWith($shareWith);
479
-			$share->setPermissions($permissions);
480
-		} else {
481
-			throw new OCSBadRequestException($this->l->t('Unknown share type'));
482
-		}
483
-
484
-		$share->setShareType($shareType);
485
-		$share->setSharedBy($this->currentUser);
486
-
487
-		try {
488
-			$share = $this->shareManager->createShare($share);
489
-		} catch (GenericShareException $e) {
490
-			$code = $e->getCode() === 0 ? 403 : $e->getCode();
491
-			throw new OCSException($e->getHint(), $code);
492
-		} catch (\Exception $e) {
493
-			throw new OCSForbiddenException($e->getMessage());
494
-		}
495
-
496
-		$output = $this->formatShare($share);
497
-
498
-		return new DataResponse($output);
499
-	}
500
-
501
-	/**
502
-	 * @param \OCP\Files\File|\OCP\Files\Folder $node
503
-	 * @param boolean $includeTags
504
-	 * @return DataResponse
505
-	 */
506
-	private function getSharedWithMe($node = null, $includeTags) {
507
-
508
-		$userShares = $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_USER, $node, -1, 0);
509
-		$groupShares = $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_GROUP, $node, -1, 0);
510
-		$circleShares = $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_CIRCLE, $node, -1, 0);
511
-
512
-		$shares = array_merge($userShares, $groupShares, $circleShares);
513
-
514
-		$shares = array_filter($shares, function (IShare $share) {
515
-			return $share->getShareOwner() !== $this->currentUser;
516
-		});
517
-
518
-		$formatted = [];
519
-		foreach ($shares as $share) {
520
-			if ($this->canAccessShare($share)) {
521
-				try {
522
-					$formatted[] = $this->formatShare($share);
523
-				} catch (NotFoundException $e) {
524
-					// Ignore this share
525
-				}
526
-			}
527
-		}
528
-
529
-		if ($includeTags) {
530
-			$formatted = Helper::populateTags($formatted, 'file_source', \OC::$server->getTagManager());
531
-		}
532
-
533
-		return new DataResponse($formatted);
534
-	}
535
-
536
-	/**
537
-	 * @param \OCP\Files\Folder $folder
538
-	 * @return DataResponse
539
-	 * @throws OCSBadRequestException
540
-	 */
541
-	private function getSharesInDir($folder) {
542
-		if (!($folder instanceof \OCP\Files\Folder)) {
543
-			throw new OCSBadRequestException($this->l->t('Not a directory'));
544
-		}
545
-
546
-		$nodes = $folder->getDirectoryListing();
547
-		/** @var \OCP\Share\IShare[] $shares */
548
-		$shares = [];
549
-		foreach ($nodes as $node) {
550
-			$shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_USER, $node, false, -1, 0));
551
-			$shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_GROUP, $node, false, -1, 0));
552
-			$shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_LINK, $node, false, -1, 0));
553
-			if($this->shareManager->shareProviderExists(\OCP\Share::SHARE_TYPE_EMAIL)) {
554
-				$shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_EMAIL, $node, false, -1, 0));
555
-			}
556
-			if ($this->shareManager->outgoingServer2ServerSharesAllowed()) {
557
-				$shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_REMOTE, $node, false, -1, 0));
558
-			}
559
-		}
560
-
561
-		$formatted = [];
562
-		foreach ($shares as $share) {
563
-			try {
564
-				$formatted[] = $this->formatShare($share);
565
-			} catch (NotFoundException $e) {
566
-				//Ignore this share
567
-			}
568
-		}
569
-
570
-		return new DataResponse($formatted);
571
-	}
572
-
573
-	/**
574
-	 * The getShares function.
575
-	 *
576
-	 * @NoAdminRequired
577
-	 *
578
-	 * @param string $shared_with_me
579
-	 * @param string $reshares
580
-	 * @param string $subfiles
581
-	 * @param string $path
582
-	 *
583
-	 * - Get shares by the current user
584
-	 * - Get shares by the current user and reshares (?reshares=true)
585
-	 * - Get shares with the current user (?shared_with_me=true)
586
-	 * - Get shares for a specific path (?path=...)
587
-	 * - Get all shares in a folder (?subfiles=true&path=..)
588
-	 *
589
-	 * @return DataResponse
590
-	 * @throws OCSNotFoundException
591
-	 */
592
-	public function getShares(
593
-		$shared_with_me = 'false',
594
-		$reshares = 'false',
595
-		$subfiles = 'false',
596
-		$path = null,
597
-		$include_tags = 'false'
598
-	) {
599
-
600
-		if ($path !== null) {
601
-			$userFolder = $this->rootFolder->getUserFolder($this->currentUser);
602
-			try {
603
-				$path = $userFolder->get($path);
604
-				$this->lock($path);
605
-			} catch (\OCP\Files\NotFoundException $e) {
606
-				throw new OCSNotFoundException($this->l->t('Wrong path, file/folder doesn\'t exist'));
607
-			} catch (LockedException $e) {
608
-				throw new OCSNotFoundException($this->l->t('Could not lock path'));
609
-			}
610
-		}
611
-
612
-		if ($shared_with_me === 'true') {
613
-			$result = $this->getSharedWithMe($path, $include_tags);
614
-			return $result;
615
-		}
616
-
617
-		if ($subfiles === 'true') {
618
-			$result = $this->getSharesInDir($path);
619
-			return $result;
620
-		}
621
-
622
-		if ($reshares === 'true') {
623
-			$reshares = true;
624
-		} else {
625
-			$reshares = false;
626
-		}
627
-
628
-		// Get all shares
629
-		$userShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_USER, $path, $reshares, -1, 0);
630
-		$groupShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_GROUP, $path, $reshares, -1, 0);
631
-		$linkShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_LINK, $path, $reshares, -1, 0);
632
-		if ($this->shareManager->shareProviderExists(\OCP\Share::SHARE_TYPE_EMAIL)) {
633
-			$mailShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_EMAIL, $path, $reshares, -1, 0);
634
-		} else {
635
-			$mailShares = [];
636
-		}
637
-		if ($this->shareManager->shareProviderExists(\OCP\Share::SHARE_TYPE_CIRCLE)) {
638
-			$circleShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_CIRCLE, $path, $reshares, -1, 0);
639
-		} else {
640
-			$circleShares = [];
641
-		}
642
-
643
-		$shares = array_merge($userShares, $groupShares, $linkShares, $mailShares, $circleShares);
644
-
645
-		if ($this->shareManager->outgoingServer2ServerSharesAllowed()) {
646
-			$federatedShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_REMOTE, $path, $reshares, -1, 0);
647
-			$shares = array_merge($shares, $federatedShares);
648
-		}
649
-
650
-		$formatted = [];
651
-		foreach ($shares as $share) {
652
-			try {
653
-				$formatted[] = $this->formatShare($share, $path);
654
-			} catch (NotFoundException $e) {
655
-				//Ignore share
656
-			}
657
-		}
658
-
659
-		if ($include_tags) {
660
-			$formatted = Helper::populateTags($formatted, 'file_source', \OC::$server->getTagManager());
661
-		}
662
-
663
-		return new DataResponse($formatted);
664
-	}
665
-
666
-	/**
667
-	 * @NoAdminRequired
668
-	 *
669
-	 * @param int $id
670
-	 * @param int $permissions
671
-	 * @param string $password
672
-	 * @param string $publicUpload
673
-	 * @param string $expireDate
674
-	 * @return DataResponse
675
-	 * @throws OCSNotFoundException
676
-	 * @throws OCSBadRequestException
677
-	 * @throws OCSForbiddenException
678
-	 */
679
-	public function updateShare(
680
-		$id,
681
-		$permissions = null,
682
-		$password = null,
683
-		$publicUpload = null,
684
-		$expireDate = null
685
-	) {
686
-		try {
687
-			$share = $this->getShareById($id);
688
-		} catch (ShareNotFound $e) {
689
-			throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
690
-		}
691
-
692
-		$this->lock($share->getNode());
693
-
694
-		if (!$this->canAccessShare($share, false)) {
695
-			throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
696
-		}
697
-
698
-		if ($share->getShareOwner() !== $this->currentUser && $share->getSharedBy() !== $this->currentUser) {
699
-			throw new OCSForbiddenException('You are not allowed to edit incomming shares');
700
-		}
701
-
702
-		if ($permissions === null && $password === null && $publicUpload === null && $expireDate === null) {
703
-			throw new OCSBadRequestException($this->l->t('Wrong or no update parameter given'));
704
-		}
705
-
706
-		/*
408
+            $existingShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_LINK, $path, false, 1, 0);
409
+            if (!empty($existingShares)) {
410
+                return new DataResponse($this->formatShare($existingShares[0]));
411
+            }
412
+
413
+            if ($publicUpload === 'true') {
414
+                // Check if public upload is allowed
415
+                if (!$this->shareManager->shareApiLinkAllowPublicUpload()) {
416
+                    throw new OCSForbiddenException($this->l->t('Public upload disabled by the administrator'));
417
+                }
418
+
419
+                // Public upload can only be set for folders
420
+                if ($path instanceof \OCP\Files\File) {
421
+                    throw new OCSNotFoundException($this->l->t('Public upload is only possible for publicly shared folders'));
422
+                }
423
+
424
+                $share->setPermissions(
425
+                    \OCP\Constants::PERMISSION_READ |
426
+                    \OCP\Constants::PERMISSION_CREATE |
427
+                    \OCP\Constants::PERMISSION_UPDATE |
428
+                    \OCP\Constants::PERMISSION_DELETE
429
+                );
430
+            } else {
431
+                $share->setPermissions(\OCP\Constants::PERMISSION_READ);
432
+            }
433
+
434
+            // Set password
435
+            if ($password !== '') {
436
+                $share->setPassword($password);
437
+            }
438
+
439
+            //Expire date
440
+            if ($expireDate !== '') {
441
+                try {
442
+                    $expireDate = $this->parseDate($expireDate);
443
+                    $share->setExpirationDate($expireDate);
444
+                } catch (\Exception $e) {
445
+                    throw new OCSNotFoundException($this->l->t('Invalid date, date format must be YYYY-MM-DD'));
446
+                }
447
+            }
448
+
449
+        } else if ($shareType === \OCP\Share::SHARE_TYPE_REMOTE) {
450
+            if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) {
451
+                throw new OCSForbiddenException($this->l->t('Sharing %s failed because the back end does not allow shares from type %s', [$path->getPath(), $shareType]));
452
+            }
453
+
454
+            $share->setSharedWith($shareWith);
455
+            $share->setPermissions($permissions);
456
+        } else if ($shareType === \OCP\Share::SHARE_TYPE_EMAIL) {
457
+            if ($share->getNodeType() === 'file') {
458
+                $share->setPermissions(\OCP\Constants::PERMISSION_READ);
459
+            } else {
460
+                $share->setPermissions(
461
+                    \OCP\Constants::PERMISSION_READ |
462
+                    \OCP\Constants::PERMISSION_CREATE |
463
+                    \OCP\Constants::PERMISSION_UPDATE |
464
+                    \OCP\Constants::PERMISSION_DELETE);
465
+            }
466
+            $share->setSharedWith($shareWith);
467
+        } else if ($shareType === \OCP\Share::SHARE_TYPE_CIRCLE) {
468
+            if (!\OC::$server->getAppManager()->isEnabledForUser('circles') || !class_exists('\OCA\Circles\ShareByCircleProvider')) {
469
+                throw new OCSNotFoundException($this->l->t('You cannot share to a Circle if the app is not enabled'));
470
+            }
471
+
472
+            $circle = \OCA\Circles\Api\v1\Circles::detailsCircle($shareWith);
473
+
474
+            // Valid circle is required to share
475
+            if ($circle === null) {
476
+                throw new OCSNotFoundException($this->l->t('Please specify a valid circle'));
477
+            }
478
+            $share->setSharedWith($shareWith);
479
+            $share->setPermissions($permissions);
480
+        } else {
481
+            throw new OCSBadRequestException($this->l->t('Unknown share type'));
482
+        }
483
+
484
+        $share->setShareType($shareType);
485
+        $share->setSharedBy($this->currentUser);
486
+
487
+        try {
488
+            $share = $this->shareManager->createShare($share);
489
+        } catch (GenericShareException $e) {
490
+            $code = $e->getCode() === 0 ? 403 : $e->getCode();
491
+            throw new OCSException($e->getHint(), $code);
492
+        } catch (\Exception $e) {
493
+            throw new OCSForbiddenException($e->getMessage());
494
+        }
495
+
496
+        $output = $this->formatShare($share);
497
+
498
+        return new DataResponse($output);
499
+    }
500
+
501
+    /**
502
+     * @param \OCP\Files\File|\OCP\Files\Folder $node
503
+     * @param boolean $includeTags
504
+     * @return DataResponse
505
+     */
506
+    private function getSharedWithMe($node = null, $includeTags) {
507
+
508
+        $userShares = $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_USER, $node, -1, 0);
509
+        $groupShares = $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_GROUP, $node, -1, 0);
510
+        $circleShares = $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_CIRCLE, $node, -1, 0);
511
+
512
+        $shares = array_merge($userShares, $groupShares, $circleShares);
513
+
514
+        $shares = array_filter($shares, function (IShare $share) {
515
+            return $share->getShareOwner() !== $this->currentUser;
516
+        });
517
+
518
+        $formatted = [];
519
+        foreach ($shares as $share) {
520
+            if ($this->canAccessShare($share)) {
521
+                try {
522
+                    $formatted[] = $this->formatShare($share);
523
+                } catch (NotFoundException $e) {
524
+                    // Ignore this share
525
+                }
526
+            }
527
+        }
528
+
529
+        if ($includeTags) {
530
+            $formatted = Helper::populateTags($formatted, 'file_source', \OC::$server->getTagManager());
531
+        }
532
+
533
+        return new DataResponse($formatted);
534
+    }
535
+
536
+    /**
537
+     * @param \OCP\Files\Folder $folder
538
+     * @return DataResponse
539
+     * @throws OCSBadRequestException
540
+     */
541
+    private function getSharesInDir($folder) {
542
+        if (!($folder instanceof \OCP\Files\Folder)) {
543
+            throw new OCSBadRequestException($this->l->t('Not a directory'));
544
+        }
545
+
546
+        $nodes = $folder->getDirectoryListing();
547
+        /** @var \OCP\Share\IShare[] $shares */
548
+        $shares = [];
549
+        foreach ($nodes as $node) {
550
+            $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_USER, $node, false, -1, 0));
551
+            $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_GROUP, $node, false, -1, 0));
552
+            $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_LINK, $node, false, -1, 0));
553
+            if($this->shareManager->shareProviderExists(\OCP\Share::SHARE_TYPE_EMAIL)) {
554
+                $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_EMAIL, $node, false, -1, 0));
555
+            }
556
+            if ($this->shareManager->outgoingServer2ServerSharesAllowed()) {
557
+                $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_REMOTE, $node, false, -1, 0));
558
+            }
559
+        }
560
+
561
+        $formatted = [];
562
+        foreach ($shares as $share) {
563
+            try {
564
+                $formatted[] = $this->formatShare($share);
565
+            } catch (NotFoundException $e) {
566
+                //Ignore this share
567
+            }
568
+        }
569
+
570
+        return new DataResponse($formatted);
571
+    }
572
+
573
+    /**
574
+     * The getShares function.
575
+     *
576
+     * @NoAdminRequired
577
+     *
578
+     * @param string $shared_with_me
579
+     * @param string $reshares
580
+     * @param string $subfiles
581
+     * @param string $path
582
+     *
583
+     * - Get shares by the current user
584
+     * - Get shares by the current user and reshares (?reshares=true)
585
+     * - Get shares with the current user (?shared_with_me=true)
586
+     * - Get shares for a specific path (?path=...)
587
+     * - Get all shares in a folder (?subfiles=true&path=..)
588
+     *
589
+     * @return DataResponse
590
+     * @throws OCSNotFoundException
591
+     */
592
+    public function getShares(
593
+        $shared_with_me = 'false',
594
+        $reshares = 'false',
595
+        $subfiles = 'false',
596
+        $path = null,
597
+        $include_tags = 'false'
598
+    ) {
599
+
600
+        if ($path !== null) {
601
+            $userFolder = $this->rootFolder->getUserFolder($this->currentUser);
602
+            try {
603
+                $path = $userFolder->get($path);
604
+                $this->lock($path);
605
+            } catch (\OCP\Files\NotFoundException $e) {
606
+                throw new OCSNotFoundException($this->l->t('Wrong path, file/folder doesn\'t exist'));
607
+            } catch (LockedException $e) {
608
+                throw new OCSNotFoundException($this->l->t('Could not lock path'));
609
+            }
610
+        }
611
+
612
+        if ($shared_with_me === 'true') {
613
+            $result = $this->getSharedWithMe($path, $include_tags);
614
+            return $result;
615
+        }
616
+
617
+        if ($subfiles === 'true') {
618
+            $result = $this->getSharesInDir($path);
619
+            return $result;
620
+        }
621
+
622
+        if ($reshares === 'true') {
623
+            $reshares = true;
624
+        } else {
625
+            $reshares = false;
626
+        }
627
+
628
+        // Get all shares
629
+        $userShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_USER, $path, $reshares, -1, 0);
630
+        $groupShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_GROUP, $path, $reshares, -1, 0);
631
+        $linkShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_LINK, $path, $reshares, -1, 0);
632
+        if ($this->shareManager->shareProviderExists(\OCP\Share::SHARE_TYPE_EMAIL)) {
633
+            $mailShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_EMAIL, $path, $reshares, -1, 0);
634
+        } else {
635
+            $mailShares = [];
636
+        }
637
+        if ($this->shareManager->shareProviderExists(\OCP\Share::SHARE_TYPE_CIRCLE)) {
638
+            $circleShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_CIRCLE, $path, $reshares, -1, 0);
639
+        } else {
640
+            $circleShares = [];
641
+        }
642
+
643
+        $shares = array_merge($userShares, $groupShares, $linkShares, $mailShares, $circleShares);
644
+
645
+        if ($this->shareManager->outgoingServer2ServerSharesAllowed()) {
646
+            $federatedShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_REMOTE, $path, $reshares, -1, 0);
647
+            $shares = array_merge($shares, $federatedShares);
648
+        }
649
+
650
+        $formatted = [];
651
+        foreach ($shares as $share) {
652
+            try {
653
+                $formatted[] = $this->formatShare($share, $path);
654
+            } catch (NotFoundException $e) {
655
+                //Ignore share
656
+            }
657
+        }
658
+
659
+        if ($include_tags) {
660
+            $formatted = Helper::populateTags($formatted, 'file_source', \OC::$server->getTagManager());
661
+        }
662
+
663
+        return new DataResponse($formatted);
664
+    }
665
+
666
+    /**
667
+     * @NoAdminRequired
668
+     *
669
+     * @param int $id
670
+     * @param int $permissions
671
+     * @param string $password
672
+     * @param string $publicUpload
673
+     * @param string $expireDate
674
+     * @return DataResponse
675
+     * @throws OCSNotFoundException
676
+     * @throws OCSBadRequestException
677
+     * @throws OCSForbiddenException
678
+     */
679
+    public function updateShare(
680
+        $id,
681
+        $permissions = null,
682
+        $password = null,
683
+        $publicUpload = null,
684
+        $expireDate = null
685
+    ) {
686
+        try {
687
+            $share = $this->getShareById($id);
688
+        } catch (ShareNotFound $e) {
689
+            throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
690
+        }
691
+
692
+        $this->lock($share->getNode());
693
+
694
+        if (!$this->canAccessShare($share, false)) {
695
+            throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
696
+        }
697
+
698
+        if ($share->getShareOwner() !== $this->currentUser && $share->getSharedBy() !== $this->currentUser) {
699
+            throw new OCSForbiddenException('You are not allowed to edit incomming shares');
700
+        }
701
+
702
+        if ($permissions === null && $password === null && $publicUpload === null && $expireDate === null) {
703
+            throw new OCSBadRequestException($this->l->t('Wrong or no update parameter given'));
704
+        }
705
+
706
+        /*
707 707
 		 * expirationdate, password and publicUpload only make sense for link shares
708 708
 		 */
709
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
710
-
711
-			$newPermissions = null;
712
-			if ($publicUpload === 'true') {
713
-				$newPermissions = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE;
714
-			} else if ($publicUpload === 'false') {
715
-				$newPermissions = \OCP\Constants::PERMISSION_READ;
716
-			}
717
-
718
-			if ($permissions !== null) {
719
-				$newPermissions = (int)$permissions;
720
-				$newPermissions = $newPermissions & ~\OCP\Constants::PERMISSION_SHARE;
721
-			}
722
-
723
-			if ($newPermissions !== null &&
724
-				!in_array($newPermissions, [
725
-					\OCP\Constants::PERMISSION_READ,
726
-					\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE, // legacy
727
-					\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE, // correct
728
-					\OCP\Constants::PERMISSION_CREATE, // hidden file list
729
-					\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE, // allow to edit single files
730
-				])
731
-			) {
732
-				throw new OCSBadRequestException($this->l->t('Can\'t change permissions for public share links'));
733
-			}
734
-
735
-			if (
736
-				// legacy
737
-				$newPermissions === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE) ||
738
-				// correct
739
-				$newPermissions === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE)
740
-			) {
741
-				if (!$this->shareManager->shareApiLinkAllowPublicUpload()) {
742
-					throw new OCSForbiddenException($this->l->t('Public upload disabled by the administrator'));
743
-				}
744
-
745
-				if (!($share->getNode() instanceof \OCP\Files\Folder)) {
746
-					throw new OCSBadRequestException($this->l->t('Public upload is only possible for publicly shared folders'));
747
-				}
748
-
749
-				// normalize to correct public upload permissions
750
-				$newPermissions = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE;
751
-			}
752
-
753
-			if ($newPermissions !== null) {
754
-				$share->setPermissions($newPermissions);
755
-				$permissions = $newPermissions;
756
-			}
757
-
758
-			if ($expireDate === '') {
759
-				$share->setExpirationDate(null);
760
-			} else if ($expireDate !== null) {
761
-				try {
762
-					$expireDate = $this->parseDate($expireDate);
763
-				} catch (\Exception $e) {
764
-					throw new OCSBadRequestException($e->getMessage());
765
-				}
766
-				$share->setExpirationDate($expireDate);
767
-			}
768
-
769
-			if ($password === '') {
770
-				$share->setPassword(null);
771
-			} else if ($password !== null) {
772
-				$share->setPassword($password);
773
-			}
774
-
775
-		} else {
776
-			if ($permissions !== null) {
777
-				$permissions = (int)$permissions;
778
-				$share->setPermissions($permissions);
779
-			}
780
-
781
-			if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
782
-				if ($password === '') {
783
-					$share->setPassword(null);
784
-				} else if ($password !== null) {
785
-					$share->setPassword($password);
786
-				}
787
-			}
788
-
789
-			if ($expireDate === '') {
790
-				$share->setExpirationDate(null);
791
-			} else if ($expireDate !== null) {
792
-				try {
793
-					$expireDate = $this->parseDate($expireDate);
794
-				} catch (\Exception $e) {
795
-					throw new OCSBadRequestException($e->getMessage());
796
-				}
797
-				$share->setExpirationDate($expireDate);
798
-			}
799
-
800
-		}
801
-
802
-		if ($permissions !== null && $share->getShareOwner() !== $this->currentUser) {
803
-			/* Check if this is an incomming share */
804
-			$incomingShares = $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_USER, $share->getNode(), -1, 0);
805
-			$incomingShares = array_merge($incomingShares, $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_GROUP, $share->getNode(), -1, 0));
806
-
807
-			/** @var \OCP\Share\IShare[] $incomingShares */
808
-			if (!empty($incomingShares)) {
809
-				$maxPermissions = 0;
810
-				foreach ($incomingShares as $incomingShare) {
811
-					$maxPermissions |= $incomingShare->getPermissions();
812
-				}
813
-
814
-				if ($share->getPermissions() & ~$maxPermissions) {
815
-					throw new OCSNotFoundException($this->l->t('Cannot increase permissions'));
816
-				}
817
-			}
818
-		}
819
-
820
-
821
-		try {
822
-			$share = $this->shareManager->updateShare($share);
823
-		} catch (\Exception $e) {
824
-			throw new OCSBadRequestException($e->getMessage());
825
-		}
826
-
827
-		return new DataResponse($this->formatShare($share));
828
-	}
829
-
830
-	/**
831
-	 * @param \OCP\Share\IShare $share
832
-	 * @return bool
833
-	 */
834
-	protected function canAccessShare(\OCP\Share\IShare $share, $checkGroups = true) {
835
-		// A file with permissions 0 can't be accessed by us. So Don't show it
836
-		if ($share->getPermissions() === 0) {
837
-			return false;
838
-		}
839
-
840
-		// Owner of the file and the sharer of the file can always get share
841
-		if ($share->getShareOwner() === $this->currentUser ||
842
-			$share->getSharedBy() === $this->currentUser
843
-		) {
844
-			return true;
845
-		}
846
-
847
-		// If the share is shared with you (or a group you are a member of)
848
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
849
-			$share->getSharedWith() === $this->currentUser
850
-		) {
851
-			return true;
852
-		}
853
-
854
-		if ($checkGroups && $share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
855
-			$sharedWith = $this->groupManager->get($share->getSharedWith());
856
-			$user = $this->userManager->get($this->currentUser);
857
-			if ($user !== null && $sharedWith !== null && $sharedWith->inGroup($user)) {
858
-				return true;
859
-			}
860
-		}
861
-
862
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_CIRCLE) {
863
-			// TODO: have a sanity check like above?
864
-			return true;
865
-		}
866
-
867
-		return false;
868
-	}
869
-
870
-	/**
871
-	 * Make sure that the passed date is valid ISO 8601
872
-	 * So YYYY-MM-DD
873
-	 * If not throw an exception
874
-	 *
875
-	 * @param string $expireDate
876
-	 *
877
-	 * @throws \Exception
878
-	 * @return \DateTime
879
-	 */
880
-	private function parseDate($expireDate) {
881
-		try {
882
-			$date = new \DateTime($expireDate);
883
-		} catch (\Exception $e) {
884
-			throw new \Exception('Invalid date. Format must be YYYY-MM-DD');
885
-		}
886
-
887
-		if ($date === false) {
888
-			throw new \Exception('Invalid date. Format must be YYYY-MM-DD');
889
-		}
890
-
891
-		$date->setTime(0, 0, 0);
892
-
893
-		return $date;
894
-	}
895
-
896
-	/**
897
-	 * Since we have multiple providers but the OCS Share API v1 does
898
-	 * not support this we need to check all backends.
899
-	 *
900
-	 * @param string $id
901
-	 * @return \OCP\Share\IShare
902
-	 * @throws ShareNotFound
903
-	 */
904
-	private function getShareById($id) {
905
-		$share = null;
906
-
907
-		// First check if it is an internal share.
908
-		try {
909
-			$share = $this->shareManager->getShareById('ocinternal:' . $id);
910
-			return $share;
911
-		} catch (ShareNotFound $e) {
912
-			// Do nothing, just try the other share type
913
-		}
914
-
915
-
916
-		try {
917
-			if ($this->shareManager->shareProviderExists(\OCP\Share::SHARE_TYPE_CIRCLE)) {
918
-				$share = $this->shareManager->getShareById('ocCircleShare:' . $id);
919
-				return $share;
920
-			}
921
-		} catch (ShareNotFound $e) {
922
-			// Do nothing, just try the other share type
923
-		}
924
-
925
-		try {
926
-			if ($this->shareManager->shareProviderExists(\OCP\Share::SHARE_TYPE_EMAIL)) {
927
-				$share = $this->shareManager->getShareById('ocMailShare:' . $id);
928
-				return $share;
929
-			}
930
-		} catch (ShareNotFound $e) {
931
-			// Do nothing, just try the other share type
932
-		}
933
-
934
-		if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) {
935
-			throw new ShareNotFound();
936
-		}
937
-		$share = $this->shareManager->getShareById('ocFederatedSharing:' . $id);
938
-
939
-		return $share;
940
-	}
941
-
942
-	/**
943
-	 * Lock a Node
944
-	 *
945
-	 * @param \OCP\Files\Node $node
946
-	 */
947
-	private function lock(\OCP\Files\Node $node) {
948
-		$node->lock(ILockingProvider::LOCK_SHARED);
949
-		$this->lockedNode = $node;
950
-	}
951
-
952
-	/**
953
-	 * Cleanup the remaining locks
954
-	 */
955
-	public function cleanup() {
956
-		if ($this->lockedNode !== null) {
957
-			$this->lockedNode->unlock(ILockingProvider::LOCK_SHARED);
958
-		}
959
-	}
709
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
710
+
711
+            $newPermissions = null;
712
+            if ($publicUpload === 'true') {
713
+                $newPermissions = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE;
714
+            } else if ($publicUpload === 'false') {
715
+                $newPermissions = \OCP\Constants::PERMISSION_READ;
716
+            }
717
+
718
+            if ($permissions !== null) {
719
+                $newPermissions = (int)$permissions;
720
+                $newPermissions = $newPermissions & ~\OCP\Constants::PERMISSION_SHARE;
721
+            }
722
+
723
+            if ($newPermissions !== null &&
724
+                !in_array($newPermissions, [
725
+                    \OCP\Constants::PERMISSION_READ,
726
+                    \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE, // legacy
727
+                    \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE, // correct
728
+                    \OCP\Constants::PERMISSION_CREATE, // hidden file list
729
+                    \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE, // allow to edit single files
730
+                ])
731
+            ) {
732
+                throw new OCSBadRequestException($this->l->t('Can\'t change permissions for public share links'));
733
+            }
734
+
735
+            if (
736
+                // legacy
737
+                $newPermissions === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE) ||
738
+                // correct
739
+                $newPermissions === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE)
740
+            ) {
741
+                if (!$this->shareManager->shareApiLinkAllowPublicUpload()) {
742
+                    throw new OCSForbiddenException($this->l->t('Public upload disabled by the administrator'));
743
+                }
744
+
745
+                if (!($share->getNode() instanceof \OCP\Files\Folder)) {
746
+                    throw new OCSBadRequestException($this->l->t('Public upload is only possible for publicly shared folders'));
747
+                }
748
+
749
+                // normalize to correct public upload permissions
750
+                $newPermissions = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE;
751
+            }
752
+
753
+            if ($newPermissions !== null) {
754
+                $share->setPermissions($newPermissions);
755
+                $permissions = $newPermissions;
756
+            }
757
+
758
+            if ($expireDate === '') {
759
+                $share->setExpirationDate(null);
760
+            } else if ($expireDate !== null) {
761
+                try {
762
+                    $expireDate = $this->parseDate($expireDate);
763
+                } catch (\Exception $e) {
764
+                    throw new OCSBadRequestException($e->getMessage());
765
+                }
766
+                $share->setExpirationDate($expireDate);
767
+            }
768
+
769
+            if ($password === '') {
770
+                $share->setPassword(null);
771
+            } else if ($password !== null) {
772
+                $share->setPassword($password);
773
+            }
774
+
775
+        } else {
776
+            if ($permissions !== null) {
777
+                $permissions = (int)$permissions;
778
+                $share->setPermissions($permissions);
779
+            }
780
+
781
+            if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
782
+                if ($password === '') {
783
+                    $share->setPassword(null);
784
+                } else if ($password !== null) {
785
+                    $share->setPassword($password);
786
+                }
787
+            }
788
+
789
+            if ($expireDate === '') {
790
+                $share->setExpirationDate(null);
791
+            } else if ($expireDate !== null) {
792
+                try {
793
+                    $expireDate = $this->parseDate($expireDate);
794
+                } catch (\Exception $e) {
795
+                    throw new OCSBadRequestException($e->getMessage());
796
+                }
797
+                $share->setExpirationDate($expireDate);
798
+            }
799
+
800
+        }
801
+
802
+        if ($permissions !== null && $share->getShareOwner() !== $this->currentUser) {
803
+            /* Check if this is an incomming share */
804
+            $incomingShares = $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_USER, $share->getNode(), -1, 0);
805
+            $incomingShares = array_merge($incomingShares, $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_GROUP, $share->getNode(), -1, 0));
806
+
807
+            /** @var \OCP\Share\IShare[] $incomingShares */
808
+            if (!empty($incomingShares)) {
809
+                $maxPermissions = 0;
810
+                foreach ($incomingShares as $incomingShare) {
811
+                    $maxPermissions |= $incomingShare->getPermissions();
812
+                }
813
+
814
+                if ($share->getPermissions() & ~$maxPermissions) {
815
+                    throw new OCSNotFoundException($this->l->t('Cannot increase permissions'));
816
+                }
817
+            }
818
+        }
819
+
820
+
821
+        try {
822
+            $share = $this->shareManager->updateShare($share);
823
+        } catch (\Exception $e) {
824
+            throw new OCSBadRequestException($e->getMessage());
825
+        }
826
+
827
+        return new DataResponse($this->formatShare($share));
828
+    }
829
+
830
+    /**
831
+     * @param \OCP\Share\IShare $share
832
+     * @return bool
833
+     */
834
+    protected function canAccessShare(\OCP\Share\IShare $share, $checkGroups = true) {
835
+        // A file with permissions 0 can't be accessed by us. So Don't show it
836
+        if ($share->getPermissions() === 0) {
837
+            return false;
838
+        }
839
+
840
+        // Owner of the file and the sharer of the file can always get share
841
+        if ($share->getShareOwner() === $this->currentUser ||
842
+            $share->getSharedBy() === $this->currentUser
843
+        ) {
844
+            return true;
845
+        }
846
+
847
+        // If the share is shared with you (or a group you are a member of)
848
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
849
+            $share->getSharedWith() === $this->currentUser
850
+        ) {
851
+            return true;
852
+        }
853
+
854
+        if ($checkGroups && $share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
855
+            $sharedWith = $this->groupManager->get($share->getSharedWith());
856
+            $user = $this->userManager->get($this->currentUser);
857
+            if ($user !== null && $sharedWith !== null && $sharedWith->inGroup($user)) {
858
+                return true;
859
+            }
860
+        }
861
+
862
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_CIRCLE) {
863
+            // TODO: have a sanity check like above?
864
+            return true;
865
+        }
866
+
867
+        return false;
868
+    }
869
+
870
+    /**
871
+     * Make sure that the passed date is valid ISO 8601
872
+     * So YYYY-MM-DD
873
+     * If not throw an exception
874
+     *
875
+     * @param string $expireDate
876
+     *
877
+     * @throws \Exception
878
+     * @return \DateTime
879
+     */
880
+    private function parseDate($expireDate) {
881
+        try {
882
+            $date = new \DateTime($expireDate);
883
+        } catch (\Exception $e) {
884
+            throw new \Exception('Invalid date. Format must be YYYY-MM-DD');
885
+        }
886
+
887
+        if ($date === false) {
888
+            throw new \Exception('Invalid date. Format must be YYYY-MM-DD');
889
+        }
890
+
891
+        $date->setTime(0, 0, 0);
892
+
893
+        return $date;
894
+    }
895
+
896
+    /**
897
+     * Since we have multiple providers but the OCS Share API v1 does
898
+     * not support this we need to check all backends.
899
+     *
900
+     * @param string $id
901
+     * @return \OCP\Share\IShare
902
+     * @throws ShareNotFound
903
+     */
904
+    private function getShareById($id) {
905
+        $share = null;
906
+
907
+        // First check if it is an internal share.
908
+        try {
909
+            $share = $this->shareManager->getShareById('ocinternal:' . $id);
910
+            return $share;
911
+        } catch (ShareNotFound $e) {
912
+            // Do nothing, just try the other share type
913
+        }
914
+
915
+
916
+        try {
917
+            if ($this->shareManager->shareProviderExists(\OCP\Share::SHARE_TYPE_CIRCLE)) {
918
+                $share = $this->shareManager->getShareById('ocCircleShare:' . $id);
919
+                return $share;
920
+            }
921
+        } catch (ShareNotFound $e) {
922
+            // Do nothing, just try the other share type
923
+        }
924
+
925
+        try {
926
+            if ($this->shareManager->shareProviderExists(\OCP\Share::SHARE_TYPE_EMAIL)) {
927
+                $share = $this->shareManager->getShareById('ocMailShare:' . $id);
928
+                return $share;
929
+            }
930
+        } catch (ShareNotFound $e) {
931
+            // Do nothing, just try the other share type
932
+        }
933
+
934
+        if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) {
935
+            throw new ShareNotFound();
936
+        }
937
+        $share = $this->shareManager->getShareById('ocFederatedSharing:' . $id);
938
+
939
+        return $share;
940
+    }
941
+
942
+    /**
943
+     * Lock a Node
944
+     *
945
+     * @param \OCP\Files\Node $node
946
+     */
947
+    private function lock(\OCP\Files\Node $node) {
948
+        $node->lock(ILockingProvider::LOCK_SHARED);
949
+        $this->lockedNode = $node;
950
+    }
951
+
952
+    /**
953
+     * Cleanup the remaining locks
954
+     */
955
+    public function cleanup() {
956
+        if ($this->lockedNode !== null) {
957
+            $this->lockedNode->unlock(ILockingProvider::LOCK_SHARED);
958
+        }
959
+    }
960 960
 }
Please login to merge, or discard this patch.