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