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