Passed
Push — master ( ef6806...fd475d )
by Roeland
09:13 queued 10s
created
lib/public/Share/IManager.php 1 patch
Indentation   +356 added lines, -356 removed lines patch added patch discarded remove patch
@@ -40,361 +40,361 @@
 block discarded – undo
40 40
  */
41 41
 interface IManager {
42 42
 
43
-	/**
44
-	 * Create a Share
45
-	 *
46
-	 * @param IShare $share
47
-	 * @return IShare The share object
48
-	 * @throws \Exception
49
-	 * @since 9.0.0
50
-	 */
51
-	public function createShare(IShare $share);
52
-
53
-	/**
54
-	 * Update a share.
55
-	 * The target of the share can't be changed this way: use moveShare
56
-	 * The share can't be removed this way (permission 0): use deleteShare
57
-	 *
58
-	 * @param IShare $share
59
-	 * @return IShare The share object
60
-	 * @throws \InvalidArgumentException
61
-	 * @since 9.0.0
62
-	 */
63
-	public function updateShare(IShare $share);
64
-
65
-	/**
66
-	 * Delete a share
67
-	 *
68
-	 * @param IShare $share
69
-	 * @throws ShareNotFound
70
-	 * @throws \InvalidArgumentException
71
-	 * @since 9.0.0
72
-	 */
73
-	public function deleteShare(IShare $share);
74
-
75
-	/**
76
-	 * Unshare a file as the recipient.
77
-	 * This can be different from a regular delete for example when one of
78
-	 * the users in a groups deletes that share. But the provider should
79
-	 * handle this.
80
-	 *
81
-	 * @param IShare $share
82
-	 * @param string $recipientId
83
-	 * @since 9.0.0
84
-	 */
85
-	public function deleteFromSelf(IShare $share, $recipientId);
86
-
87
-	/**
88
-	 * Restore the share when it has been deleted
89
-	 * Certain share types can be restored when they have been deleted
90
-	 * but the provider should properly handle this\
91
-	 *
92
-	 * @param IShare $share The share to restore
93
-	 * @param string $recipientId The user to restore the share for
94
-	 * @return IShare The restored share object
95
-	 * @throws GenericShareException In case restoring the share failed
96
-	 *
97
-	 * @since 14.0.0
98
-	 */
99
-	public function restoreShare(IShare $share, string $recipientId): IShare;
100
-
101
-	/**
102
-	 * Move the share as a recipient of the share.
103
-	 * This is updating the share target. So where the recipient has the share mounted.
104
-	 *
105
-	 * @param IShare $share
106
-	 * @param string $recipientId
107
-	 * @return IShare
108
-	 * @throws \InvalidArgumentException If $share is a link share or the $recipient does not match
109
-	 * @since 9.0.0
110
-	 */
111
-	public function moveShare(IShare $share, $recipientId);
112
-
113
-	/**
114
-	 * Get all shares shared by (initiated) by the provided user in a folder.
115
-	 *
116
-	 * @param string $userId
117
-	 * @param Folder $node
118
-	 * @param bool $reshares
119
-	 * @return IShare[][] [$fileId => IShare[], ...]
120
-	 * @since 11.0.0
121
-	 */
122
-	public function getSharesInFolder($userId, Folder $node, $reshares = false);
123
-
124
-	/**
125
-	 * Get shares shared by (initiated) by the provided user.
126
-	 *
127
-	 * @param string $userId
128
-	 * @param int $shareType
129
-	 * @param Node|null $path
130
-	 * @param bool $reshares
131
-	 * @param int $limit The maximum number of returned results, -1 for all results
132
-	 * @param int $offset
133
-	 * @return IShare[]
134
-	 * @since 9.0.0
135
-	 */
136
-	public function getSharesBy($userId, $shareType, $path = null, $reshares = false, $limit = 50, $offset = 0);
137
-
138
-	/**
139
-	 * Get shares shared with $user.
140
-	 * Filter by $node if provided
141
-	 *
142
-	 * @param string $userId
143
-	 * @param int $shareType
144
-	 * @param Node|null $node
145
-	 * @param int $limit The maximum number of shares returned, -1 for all
146
-	 * @param int $offset
147
-	 * @return IShare[]
148
-	 * @since 9.0.0
149
-	 */
150
-	public function getSharedWith($userId, $shareType, $node = null, $limit = 50, $offset = 0);
151
-
152
-	/**
153
-	 * Get deleted shares shared with $user.
154
-	 * Filter by $node if provided
155
-	 *
156
-	 * @param string $userId
157
-	 * @param int $shareType
158
-	 * @param Node|null $node
159
-	 * @param int $limit The maximum number of shares returned, -1 for all
160
-	 * @param int $offset
161
-	 * @return IShare[]
162
-	 * @since 14.0.0
163
-	 */
164
-	public function getDeletedSharedWith($userId, $shareType, $node = null, $limit = 50, $offset = 0);
165
-
166
-	/**
167
-	 * Retrieve a share by the share id.
168
-	 * If the recipient is set make sure to retrieve the file for that user.
169
-	 * This makes sure that if a user has moved/deleted a group share this
170
-	 * is reflected.
171
-	 *
172
-	 * @param string $id
173
-	 * @param string|null $recipient userID of the recipient
174
-	 * @return IShare
175
-	 * @throws ShareNotFound
176
-	 * @since 9.0.0
177
-	 */
178
-	public function getShareById($id, $recipient = null);
179
-
180
-	/**
181
-	 * Get the share by token possible with password
182
-	 *
183
-	 * @param string $token
184
-	 * @return IShare
185
-	 * @throws ShareNotFound
186
-	 * @since 9.0.0
187
-	 */
188
-	public function getShareByToken($token);
189
-
190
-	/**
191
-	 * Verify the password of a public share
192
-	 *
193
-	 * @param IShare $share
194
-	 * @param string $password
195
-	 * @return bool
196
-	 * @since 9.0.0
197
-	 */
198
-	public function checkPassword(IShare $share, $password);
199
-
200
-	/**
201
-	 * The user with UID is deleted.
202
-	 * All share providers have to cleanup the shares with this user as well
203
-	 * as shares owned by this user.
204
-	 * Shares only initiated by this user are fine.
205
-	 *
206
-	 * @param string $uid
207
-	 * @since 9.1.0
208
-	 */
209
-	public function userDeleted($uid);
210
-
211
-	/**
212
-	 * The group with $gid is deleted
213
-	 * We need to clear up all shares to this group
214
-	 *
215
-	 * @param string $gid
216
-	 * @since 9.1.0
217
-	 */
218
-	public function groupDeleted($gid);
219
-
220
-	/**
221
-	 * The user $uid is deleted from the group $gid
222
-	 * All user specific group shares have to be removed
223
-	 *
224
-	 * @param string $uid
225
-	 * @param string $gid
226
-	 * @since 9.1.0
227
-	 */
228
-	public function userDeletedFromGroup($uid, $gid);
229
-
230
-	/**
231
-	 * Get access list to a path. This means
232
-	 * all the users that can access a given path.
233
-	 *
234
-	 * Consider:
235
-	 * -root
236
-	 * |-folder1 (23)
237
-	 *  |-folder2 (32)
238
-	 *   |-fileA (42)
239
-	 *
240
-	 * fileA is shared with user1 and user1@server1
241
-	 * folder2 is shared with group2 (user4 is a member of group2)
242
-	 * folder1 is shared with user2 (renamed to "folder (1)") and user2@server2
243
-	 *
244
-	 * Then the access list to '/folder1/folder2/fileA' with $currentAccess is:
245
-	 * [
246
-	 *  users  => [
247
-	 *      'user1' => ['node_id' => 42, 'node_path' => '/fileA'],
248
-	 *      'user4' => ['node_id' => 32, 'node_path' => '/folder2'],
249
-	 *      'user2' => ['node_id' => 23, 'node_path' => '/folder (1)'],
250
-	 *  ],
251
-	 *  remote => [
252
-	 *      'user1@server1' => ['node_id' => 42, 'token' => 'SeCr3t'],
253
-	 *      'user2@server2' => ['node_id' => 23, 'token' => 'FooBaR'],
254
-	 *  ],
255
-	 *  public => bool
256
-	 *  mail => bool
257
-	 * ]
258
-	 *
259
-	 * The access list to '/folder1/folder2/fileA' **without** $currentAccess is:
260
-	 * [
261
-	 *  users  => ['user1', 'user2', 'user4'],
262
-	 *  remote => bool,
263
-	 *  public => bool
264
-	 *  mail => bool
265
-	 * ]
266
-	 *
267
-	 * This is required for encryption/activity
268
-	 *
269
-	 * @param \OCP\Files\Node $path
270
-	 * @param bool $recursive Should we check all parent folders as well
271
-	 * @param bool $currentAccess Should the user have currently access to the file
272
-	 * @return array
273
-	 * @since 12
274
-	 */
275
-	public function getAccessList(\OCP\Files\Node $path, $recursive = true, $currentAccess = false);
276
-
277
-	/**
278
-	 * Instantiates a new share object. This is to be passed to
279
-	 * createShare.
280
-	 *
281
-	 * @return IShare
282
-	 * @since 9.0.0
283
-	 */
284
-	public function newShare();
285
-
286
-	/**
287
-	 * Is the share API enabled
288
-	 *
289
-	 * @return bool
290
-	 * @since 9.0.0
291
-	 */
292
-	public function shareApiEnabled();
293
-
294
-	/**
295
-	 * Is public link sharing enabled
296
-	 *
297
-	 * @return bool
298
-	 * @since 9.0.0
299
-	 */
300
-	public function shareApiAllowLinks();
301
-
302
-	/**
303
-	 * Is password on public link requires
304
-	 *
305
-	 * @return bool
306
-	 * @since 9.0.0
307
-	 */
308
-	public function shareApiLinkEnforcePassword();
309
-
310
-	/**
311
-	 * Is default expire date enabled
312
-	 *
313
-	 * @return bool
314
-	 * @since 9.0.0
315
-	 */
316
-	public function shareApiLinkDefaultExpireDate();
317
-
318
-	/**
319
-	 * Is default expire date enforced
320
-	 *`
321
-	 * @return bool
322
-	 * @since 9.0.0
323
-	 */
324
-	public function shareApiLinkDefaultExpireDateEnforced();
325
-
326
-	/**
327
-	 * Number of default expire days
328
-	 *
329
-	 * @return int
330
-	 * @since 9.0.0
331
-	 */
332
-	public function shareApiLinkDefaultExpireDays();
333
-
334
-	/**
335
-	 * Allow public upload on link shares
336
-	 *
337
-	 * @return bool
338
-	 * @since 9.0.0
339
-	 */
340
-	public function shareApiLinkAllowPublicUpload();
341
-
342
-	/**
343
-	 * check if user can only share with group members
344
-	 * @return bool
345
-	 * @since 9.0.0
346
-	 */
347
-	public function shareWithGroupMembersOnly();
348
-
349
-	/**
350
-	 * Check if users can share with groups
351
-	 * @return bool
352
-	 * @since 9.0.1
353
-	 */
354
-	public function allowGroupSharing();
355
-
356
-	/**
357
-	 * Check if sharing is disabled for the given user
358
-	 *
359
-	 * @param string $userId
360
-	 * @return bool
361
-	 * @since 9.0.0
362
-	 */
363
-	public function sharingDisabledForUser($userId);
364
-
365
-	/**
366
-	 * Check if outgoing server2server shares are allowed
367
-	 * @return bool
368
-	 * @since 9.0.0
369
-	 */
370
-	public function outgoingServer2ServerSharesAllowed();
371
-
372
-	/**
373
-	 * Check if outgoing server2server shares are allowed
374
-	 * @return bool
375
-	 * @since 14.0.0
376
-	 */
377
-	public function outgoingServer2ServerGroupSharesAllowed();
378
-
379
-
380
-	/**
381
-	 * Check if a given share provider exists
382
-	 * @param int $shareType
383
-	 * @return bool
384
-	 * @since 11.0.0
385
-	 */
386
-	public function shareProviderExists($shareType);
387
-
388
-	/**
389
-	 * @Internal
390
-	 *
391
-	 * Get all the shares as iterable to reduce memory overhead
392
-	 * Note, since this opens up database cursors the iterable should
393
-	 * be fully itterated.
394
-	 *
395
-	 * @return iterable
396
-	 * @since 18.0.0
397
-	 */
398
-	public function getAllShares(): iterable;
43
+    /**
44
+     * Create a Share
45
+     *
46
+     * @param IShare $share
47
+     * @return IShare The share object
48
+     * @throws \Exception
49
+     * @since 9.0.0
50
+     */
51
+    public function createShare(IShare $share);
52
+
53
+    /**
54
+     * Update a share.
55
+     * The target of the share can't be changed this way: use moveShare
56
+     * The share can't be removed this way (permission 0): use deleteShare
57
+     *
58
+     * @param IShare $share
59
+     * @return IShare The share object
60
+     * @throws \InvalidArgumentException
61
+     * @since 9.0.0
62
+     */
63
+    public function updateShare(IShare $share);
64
+
65
+    /**
66
+     * Delete a share
67
+     *
68
+     * @param IShare $share
69
+     * @throws ShareNotFound
70
+     * @throws \InvalidArgumentException
71
+     * @since 9.0.0
72
+     */
73
+    public function deleteShare(IShare $share);
74
+
75
+    /**
76
+     * Unshare a file as the recipient.
77
+     * This can be different from a regular delete for example when one of
78
+     * the users in a groups deletes that share. But the provider should
79
+     * handle this.
80
+     *
81
+     * @param IShare $share
82
+     * @param string $recipientId
83
+     * @since 9.0.0
84
+     */
85
+    public function deleteFromSelf(IShare $share, $recipientId);
86
+
87
+    /**
88
+     * Restore the share when it has been deleted
89
+     * Certain share types can be restored when they have been deleted
90
+     * but the provider should properly handle this\
91
+     *
92
+     * @param IShare $share The share to restore
93
+     * @param string $recipientId The user to restore the share for
94
+     * @return IShare The restored share object
95
+     * @throws GenericShareException In case restoring the share failed
96
+     *
97
+     * @since 14.0.0
98
+     */
99
+    public function restoreShare(IShare $share, string $recipientId): IShare;
100
+
101
+    /**
102
+     * Move the share as a recipient of the share.
103
+     * This is updating the share target. So where the recipient has the share mounted.
104
+     *
105
+     * @param IShare $share
106
+     * @param string $recipientId
107
+     * @return IShare
108
+     * @throws \InvalidArgumentException If $share is a link share or the $recipient does not match
109
+     * @since 9.0.0
110
+     */
111
+    public function moveShare(IShare $share, $recipientId);
112
+
113
+    /**
114
+     * Get all shares shared by (initiated) by the provided user in a folder.
115
+     *
116
+     * @param string $userId
117
+     * @param Folder $node
118
+     * @param bool $reshares
119
+     * @return IShare[][] [$fileId => IShare[], ...]
120
+     * @since 11.0.0
121
+     */
122
+    public function getSharesInFolder($userId, Folder $node, $reshares = false);
123
+
124
+    /**
125
+     * Get shares shared by (initiated) by the provided user.
126
+     *
127
+     * @param string $userId
128
+     * @param int $shareType
129
+     * @param Node|null $path
130
+     * @param bool $reshares
131
+     * @param int $limit The maximum number of returned results, -1 for all results
132
+     * @param int $offset
133
+     * @return IShare[]
134
+     * @since 9.0.0
135
+     */
136
+    public function getSharesBy($userId, $shareType, $path = null, $reshares = false, $limit = 50, $offset = 0);
137
+
138
+    /**
139
+     * Get shares shared with $user.
140
+     * Filter by $node if provided
141
+     *
142
+     * @param string $userId
143
+     * @param int $shareType
144
+     * @param Node|null $node
145
+     * @param int $limit The maximum number of shares returned, -1 for all
146
+     * @param int $offset
147
+     * @return IShare[]
148
+     * @since 9.0.0
149
+     */
150
+    public function getSharedWith($userId, $shareType, $node = null, $limit = 50, $offset = 0);
151
+
152
+    /**
153
+     * Get deleted shares shared with $user.
154
+     * Filter by $node if provided
155
+     *
156
+     * @param string $userId
157
+     * @param int $shareType
158
+     * @param Node|null $node
159
+     * @param int $limit The maximum number of shares returned, -1 for all
160
+     * @param int $offset
161
+     * @return IShare[]
162
+     * @since 14.0.0
163
+     */
164
+    public function getDeletedSharedWith($userId, $shareType, $node = null, $limit = 50, $offset = 0);
165
+
166
+    /**
167
+     * Retrieve a share by the share id.
168
+     * If the recipient is set make sure to retrieve the file for that user.
169
+     * This makes sure that if a user has moved/deleted a group share this
170
+     * is reflected.
171
+     *
172
+     * @param string $id
173
+     * @param string|null $recipient userID of the recipient
174
+     * @return IShare
175
+     * @throws ShareNotFound
176
+     * @since 9.0.0
177
+     */
178
+    public function getShareById($id, $recipient = null);
179
+
180
+    /**
181
+     * Get the share by token possible with password
182
+     *
183
+     * @param string $token
184
+     * @return IShare
185
+     * @throws ShareNotFound
186
+     * @since 9.0.0
187
+     */
188
+    public function getShareByToken($token);
189
+
190
+    /**
191
+     * Verify the password of a public share
192
+     *
193
+     * @param IShare $share
194
+     * @param string $password
195
+     * @return bool
196
+     * @since 9.0.0
197
+     */
198
+    public function checkPassword(IShare $share, $password);
199
+
200
+    /**
201
+     * The user with UID is deleted.
202
+     * All share providers have to cleanup the shares with this user as well
203
+     * as shares owned by this user.
204
+     * Shares only initiated by this user are fine.
205
+     *
206
+     * @param string $uid
207
+     * @since 9.1.0
208
+     */
209
+    public function userDeleted($uid);
210
+
211
+    /**
212
+     * The group with $gid is deleted
213
+     * We need to clear up all shares to this group
214
+     *
215
+     * @param string $gid
216
+     * @since 9.1.0
217
+     */
218
+    public function groupDeleted($gid);
219
+
220
+    /**
221
+     * The user $uid is deleted from the group $gid
222
+     * All user specific group shares have to be removed
223
+     *
224
+     * @param string $uid
225
+     * @param string $gid
226
+     * @since 9.1.0
227
+     */
228
+    public function userDeletedFromGroup($uid, $gid);
229
+
230
+    /**
231
+     * Get access list to a path. This means
232
+     * all the users that can access a given path.
233
+     *
234
+     * Consider:
235
+     * -root
236
+     * |-folder1 (23)
237
+     *  |-folder2 (32)
238
+     *   |-fileA (42)
239
+     *
240
+     * fileA is shared with user1 and user1@server1
241
+     * folder2 is shared with group2 (user4 is a member of group2)
242
+     * folder1 is shared with user2 (renamed to "folder (1)") and user2@server2
243
+     *
244
+     * Then the access list to '/folder1/folder2/fileA' with $currentAccess is:
245
+     * [
246
+     *  users  => [
247
+     *      'user1' => ['node_id' => 42, 'node_path' => '/fileA'],
248
+     *      'user4' => ['node_id' => 32, 'node_path' => '/folder2'],
249
+     *      'user2' => ['node_id' => 23, 'node_path' => '/folder (1)'],
250
+     *  ],
251
+     *  remote => [
252
+     *      'user1@server1' => ['node_id' => 42, 'token' => 'SeCr3t'],
253
+     *      'user2@server2' => ['node_id' => 23, 'token' => 'FooBaR'],
254
+     *  ],
255
+     *  public => bool
256
+     *  mail => bool
257
+     * ]
258
+     *
259
+     * The access list to '/folder1/folder2/fileA' **without** $currentAccess is:
260
+     * [
261
+     *  users  => ['user1', 'user2', 'user4'],
262
+     *  remote => bool,
263
+     *  public => bool
264
+     *  mail => bool
265
+     * ]
266
+     *
267
+     * This is required for encryption/activity
268
+     *
269
+     * @param \OCP\Files\Node $path
270
+     * @param bool $recursive Should we check all parent folders as well
271
+     * @param bool $currentAccess Should the user have currently access to the file
272
+     * @return array
273
+     * @since 12
274
+     */
275
+    public function getAccessList(\OCP\Files\Node $path, $recursive = true, $currentAccess = false);
276
+
277
+    /**
278
+     * Instantiates a new share object. This is to be passed to
279
+     * createShare.
280
+     *
281
+     * @return IShare
282
+     * @since 9.0.0
283
+     */
284
+    public function newShare();
285
+
286
+    /**
287
+     * Is the share API enabled
288
+     *
289
+     * @return bool
290
+     * @since 9.0.0
291
+     */
292
+    public function shareApiEnabled();
293
+
294
+    /**
295
+     * Is public link sharing enabled
296
+     *
297
+     * @return bool
298
+     * @since 9.0.0
299
+     */
300
+    public function shareApiAllowLinks();
301
+
302
+    /**
303
+     * Is password on public link requires
304
+     *
305
+     * @return bool
306
+     * @since 9.0.0
307
+     */
308
+    public function shareApiLinkEnforcePassword();
309
+
310
+    /**
311
+     * Is default expire date enabled
312
+     *
313
+     * @return bool
314
+     * @since 9.0.0
315
+     */
316
+    public function shareApiLinkDefaultExpireDate();
317
+
318
+    /**
319
+     * Is default expire date enforced
320
+     *`
321
+     * @return bool
322
+     * @since 9.0.0
323
+     */
324
+    public function shareApiLinkDefaultExpireDateEnforced();
325
+
326
+    /**
327
+     * Number of default expire days
328
+     *
329
+     * @return int
330
+     * @since 9.0.0
331
+     */
332
+    public function shareApiLinkDefaultExpireDays();
333
+
334
+    /**
335
+     * Allow public upload on link shares
336
+     *
337
+     * @return bool
338
+     * @since 9.0.0
339
+     */
340
+    public function shareApiLinkAllowPublicUpload();
341
+
342
+    /**
343
+     * check if user can only share with group members
344
+     * @return bool
345
+     * @since 9.0.0
346
+     */
347
+    public function shareWithGroupMembersOnly();
348
+
349
+    /**
350
+     * Check if users can share with groups
351
+     * @return bool
352
+     * @since 9.0.1
353
+     */
354
+    public function allowGroupSharing();
355
+
356
+    /**
357
+     * Check if sharing is disabled for the given user
358
+     *
359
+     * @param string $userId
360
+     * @return bool
361
+     * @since 9.0.0
362
+     */
363
+    public function sharingDisabledForUser($userId);
364
+
365
+    /**
366
+     * Check if outgoing server2server shares are allowed
367
+     * @return bool
368
+     * @since 9.0.0
369
+     */
370
+    public function outgoingServer2ServerSharesAllowed();
371
+
372
+    /**
373
+     * Check if outgoing server2server shares are allowed
374
+     * @return bool
375
+     * @since 14.0.0
376
+     */
377
+    public function outgoingServer2ServerGroupSharesAllowed();
378
+
379
+
380
+    /**
381
+     * Check if a given share provider exists
382
+     * @param int $shareType
383
+     * @return bool
384
+     * @since 11.0.0
385
+     */
386
+    public function shareProviderExists($shareType);
387
+
388
+    /**
389
+     * @Internal
390
+     *
391
+     * Get all the shares as iterable to reduce memory overhead
392
+     * Note, since this opens up database cursors the iterable should
393
+     * be fully itterated.
394
+     *
395
+     * @return iterable
396
+     * @since 18.0.0
397
+     */
398
+    public function getAllShares(): iterable;
399 399
 
400 400
 }
Please login to merge, or discard this patch.
lib/public/Share/IShareProvider.php 1 patch
Indentation   +189 added lines, -189 removed lines patch added patch discarded remove patch
@@ -37,193 +37,193 @@
 block discarded – undo
37 37
  */
38 38
 interface IShareProvider {
39 39
 
40
-	/**
41
-	 * Return the identifier of this provider.
42
-	 *
43
-	 * @return string Containing only [a-zA-Z0-9]
44
-	 * @since 9.0.0
45
-	 */
46
-	public function identifier();
47
-
48
-	/**
49
-	 * Create a share
50
-	 *
51
-	 * @param \OCP\Share\IShare $share
52
-	 * @return \OCP\Share\IShare The share object
53
-	 * @since 9.0.0
54
-	 */
55
-	public function create(\OCP\Share\IShare $share);
56
-
57
-	/**
58
-	 * Update a share
59
-	 *
60
-	 * @param \OCP\Share\IShare $share
61
-	 * @return \OCP\Share\IShare The share object
62
-	 * @since 9.0.0
63
-	 */
64
-	public function update(\OCP\Share\IShare $share);
65
-
66
-	/**
67
-	 * Delete a share
68
-	 *
69
-	 * @param \OCP\Share\IShare $share
70
-	 * @since 9.0.0
71
-	 */
72
-	public function delete(\OCP\Share\IShare $share);
73
-
74
-	/**
75
-	 * Unshare a file from self as recipient.
76
-	 * This may require special handling. If a user unshares a group
77
-	 * share from their self then the original group share should still exist.
78
-	 *
79
-	 * @param \OCP\Share\IShare $share
80
-	 * @param string $recipient UserId of the recipient
81
-	 * @since 9.0.0
82
-	 */
83
-	public function deleteFromSelf(\OCP\Share\IShare $share, $recipient);
84
-
85
-	/**
86
-	 * Restore a share for a given recipient. The implementation could be provider independant.
87
-	 *
88
-	 * @param IShare $share
89
-	 * @param string $recipient
90
-	 * @return IShare The restored share object
91
-	 *
92
-	 * @since 14.0.0
93
-	 * @throws GenericShareException In case the share could not be restored
94
-	 */
95
-	public function restore(IShare $share, string $recipient): IShare;
96
-
97
-	/**
98
-	 * Move a share as a recipient.
99
-	 * This is updating the share target. Thus the mount point of the recipient.
100
-	 * This may require special handling. If a user moves a group share
101
-	 * the target should only be changed for them.
102
-	 *
103
-	 * @param \OCP\Share\IShare $share
104
-	 * @param string $recipient userId of recipient
105
-	 * @return \OCP\Share\IShare
106
-	 * @since 9.0.0
107
-	 */
108
-	public function move(\OCP\Share\IShare $share, $recipient);
109
-
110
-	/**
111
-	 * Get all shares by the given user in a folder
112
-	 *
113
-	 * @param string $userId
114
-	 * @param Folder $node
115
-	 * @param bool $reshares Also get the shares where $user is the owner instead of just the shares where $user is the initiator
116
-	 * @return \OCP\Share\IShare[]
117
-	 * @since 11.0.0
118
-	 */
119
-	public function getSharesInFolder($userId, Folder $node, $reshares);
120
-
121
-	/**
122
-	 * Get all shares by the given user
123
-	 *
124
-	 * @param string $userId
125
-	 * @param int $shareType
126
-	 * @param Node|null $node
127
-	 * @param bool $reshares Also get the shares where $user is the owner instead of just the shares where $user is the initiator
128
-	 * @param int $limit The maximum number of shares to be returned, -1 for all shares
129
-	 * @param int $offset
130
-	 * @return \OCP\Share\IShare[]
131
-	 * @since 9.0.0
132
-	 */
133
-	public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset);
134
-
135
-	/**
136
-	 * Get share by id
137
-	 *
138
-	 * @param int $id
139
-	 * @param string|null $recipientId
140
-	 * @return \OCP\Share\IShare
141
-	 * @throws ShareNotFound
142
-	 * @since 9.0.0
143
-	 */
144
-	public function getShareById($id, $recipientId = null);
145
-
146
-	/**
147
-	 * Get shares for a given path
148
-	 *
149
-	 * @param Node $path
150
-	 * @return \OCP\Share\IShare[]
151
-	 * @since 9.0.0
152
-	 */
153
-	public function getSharesByPath(Node $path);
154
-
155
-	/**
156
-	 * Get shared with the given user
157
-	 *
158
-	 * @param string $userId get shares where this user is the recipient
159
-	 * @param int $shareType
160
-	 * @param Node|null $node
161
-	 * @param int $limit The max number of entries returned, -1 for all
162
-	 * @param int $offset
163
-	 * @return \OCP\Share\IShare[]
164
-	 * @since 9.0.0
165
-	 */
166
-	public function getSharedWith($userId, $shareType, $node, $limit, $offset);
167
-
168
-	/**
169
-	 * Get a share by token
170
-	 *
171
-	 * @param string $token
172
-	 * @return \OCP\Share\IShare
173
-	 * @throws ShareNotFound
174
-	 * @since 9.0.0
175
-	 */
176
-	public function getShareByToken($token);
177
-
178
-	/**
179
-	 * A user is deleted from the system
180
-	 * So clean up the relevant shares.
181
-	 *
182
-	 * @param string $uid
183
-	 * @param int $shareType
184
-	 * @since 9.1.0
185
-	 */
186
-	public function userDeleted($uid, $shareType);
187
-
188
-	/**
189
-	 * A group is deleted from the system.
190
-	 * We have to clean up all shares to this group.
191
-	 * Providers not handling group shares should just return
192
-	 *
193
-	 * @param string $gid
194
-	 * @since 9.1.0
195
-	 */
196
-	public function groupDeleted($gid);
197
-
198
-	/**
199
-	 * A user is deleted from a group
200
-	 * We have to clean up all the related user specific group shares
201
-	 * Providers not handling group shares should just return
202
-	 *
203
-	 * @param string $uid
204
-	 * @param string $gid
205
-	 * @since 9.1.0
206
-	 */
207
-	public function userDeletedFromGroup($uid, $gid);
208
-
209
-	/**
210
-	 * Get the access list to the array of provided nodes.
211
-	 *
212
-	 * @see IManager::getAccessList() for sample docs
213
-	 *
214
-	 * @param Node[] $nodes The list of nodes to get access for
215
-	 * @param bool $currentAccess If current access is required (like for removed shares that might get revived later)
216
-	 * @return array
217
-	 * @since 12
218
-	 */
219
-	public function getAccessList($nodes, $currentAccess);
220
-
221
-	/**
222
-	 * Get all the shares in this provider returned as iterable to reduce memory
223
-	 * overhead
224
-	 *
225
-	 * @return iterable
226
-	 * @since 18.0.0
227
-	 */
228
-	public function getAllShares(): iterable;
40
+    /**
41
+     * Return the identifier of this provider.
42
+     *
43
+     * @return string Containing only [a-zA-Z0-9]
44
+     * @since 9.0.0
45
+     */
46
+    public function identifier();
47
+
48
+    /**
49
+     * Create a share
50
+     *
51
+     * @param \OCP\Share\IShare $share
52
+     * @return \OCP\Share\IShare The share object
53
+     * @since 9.0.0
54
+     */
55
+    public function create(\OCP\Share\IShare $share);
56
+
57
+    /**
58
+     * Update a share
59
+     *
60
+     * @param \OCP\Share\IShare $share
61
+     * @return \OCP\Share\IShare The share object
62
+     * @since 9.0.0
63
+     */
64
+    public function update(\OCP\Share\IShare $share);
65
+
66
+    /**
67
+     * Delete a share
68
+     *
69
+     * @param \OCP\Share\IShare $share
70
+     * @since 9.0.0
71
+     */
72
+    public function delete(\OCP\Share\IShare $share);
73
+
74
+    /**
75
+     * Unshare a file from self as recipient.
76
+     * This may require special handling. If a user unshares a group
77
+     * share from their self then the original group share should still exist.
78
+     *
79
+     * @param \OCP\Share\IShare $share
80
+     * @param string $recipient UserId of the recipient
81
+     * @since 9.0.0
82
+     */
83
+    public function deleteFromSelf(\OCP\Share\IShare $share, $recipient);
84
+
85
+    /**
86
+     * Restore a share for a given recipient. The implementation could be provider independant.
87
+     *
88
+     * @param IShare $share
89
+     * @param string $recipient
90
+     * @return IShare The restored share object
91
+     *
92
+     * @since 14.0.0
93
+     * @throws GenericShareException In case the share could not be restored
94
+     */
95
+    public function restore(IShare $share, string $recipient): IShare;
96
+
97
+    /**
98
+     * Move a share as a recipient.
99
+     * This is updating the share target. Thus the mount point of the recipient.
100
+     * This may require special handling. If a user moves a group share
101
+     * the target should only be changed for them.
102
+     *
103
+     * @param \OCP\Share\IShare $share
104
+     * @param string $recipient userId of recipient
105
+     * @return \OCP\Share\IShare
106
+     * @since 9.0.0
107
+     */
108
+    public function move(\OCP\Share\IShare $share, $recipient);
109
+
110
+    /**
111
+     * Get all shares by the given user in a folder
112
+     *
113
+     * @param string $userId
114
+     * @param Folder $node
115
+     * @param bool $reshares Also get the shares where $user is the owner instead of just the shares where $user is the initiator
116
+     * @return \OCP\Share\IShare[]
117
+     * @since 11.0.0
118
+     */
119
+    public function getSharesInFolder($userId, Folder $node, $reshares);
120
+
121
+    /**
122
+     * Get all shares by the given user
123
+     *
124
+     * @param string $userId
125
+     * @param int $shareType
126
+     * @param Node|null $node
127
+     * @param bool $reshares Also get the shares where $user is the owner instead of just the shares where $user is the initiator
128
+     * @param int $limit The maximum number of shares to be returned, -1 for all shares
129
+     * @param int $offset
130
+     * @return \OCP\Share\IShare[]
131
+     * @since 9.0.0
132
+     */
133
+    public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset);
134
+
135
+    /**
136
+     * Get share by id
137
+     *
138
+     * @param int $id
139
+     * @param string|null $recipientId
140
+     * @return \OCP\Share\IShare
141
+     * @throws ShareNotFound
142
+     * @since 9.0.0
143
+     */
144
+    public function getShareById($id, $recipientId = null);
145
+
146
+    /**
147
+     * Get shares for a given path
148
+     *
149
+     * @param Node $path
150
+     * @return \OCP\Share\IShare[]
151
+     * @since 9.0.0
152
+     */
153
+    public function getSharesByPath(Node $path);
154
+
155
+    /**
156
+     * Get shared with the given user
157
+     *
158
+     * @param string $userId get shares where this user is the recipient
159
+     * @param int $shareType
160
+     * @param Node|null $node
161
+     * @param int $limit The max number of entries returned, -1 for all
162
+     * @param int $offset
163
+     * @return \OCP\Share\IShare[]
164
+     * @since 9.0.0
165
+     */
166
+    public function getSharedWith($userId, $shareType, $node, $limit, $offset);
167
+
168
+    /**
169
+     * Get a share by token
170
+     *
171
+     * @param string $token
172
+     * @return \OCP\Share\IShare
173
+     * @throws ShareNotFound
174
+     * @since 9.0.0
175
+     */
176
+    public function getShareByToken($token);
177
+
178
+    /**
179
+     * A user is deleted from the system
180
+     * So clean up the relevant shares.
181
+     *
182
+     * @param string $uid
183
+     * @param int $shareType
184
+     * @since 9.1.0
185
+     */
186
+    public function userDeleted($uid, $shareType);
187
+
188
+    /**
189
+     * A group is deleted from the system.
190
+     * We have to clean up all shares to this group.
191
+     * Providers not handling group shares should just return
192
+     *
193
+     * @param string $gid
194
+     * @since 9.1.0
195
+     */
196
+    public function groupDeleted($gid);
197
+
198
+    /**
199
+     * A user is deleted from a group
200
+     * We have to clean up all the related user specific group shares
201
+     * Providers not handling group shares should just return
202
+     *
203
+     * @param string $uid
204
+     * @param string $gid
205
+     * @since 9.1.0
206
+     */
207
+    public function userDeletedFromGroup($uid, $gid);
208
+
209
+    /**
210
+     * Get the access list to the array of provided nodes.
211
+     *
212
+     * @see IManager::getAccessList() for sample docs
213
+     *
214
+     * @param Node[] $nodes The list of nodes to get access for
215
+     * @param bool $currentAccess If current access is required (like for removed shares that might get revived later)
216
+     * @return array
217
+     * @since 12
218
+     */
219
+    public function getAccessList($nodes, $currentAccess);
220
+
221
+    /**
222
+     * Get all the shares in this provider returned as iterable to reduce memory
223
+     * overhead
224
+     *
225
+     * @return iterable
226
+     * @since 18.0.0
227
+     */
228
+    public function getAllShares(): iterable;
229 229
 }
Please login to merge, or discard this patch.
lib/private/Share20/DefaultShareProvider.php 2 patches
Indentation   +1317 added lines, -1317 removed lines patch added patch discarded remove patch
@@ -58,1354 +58,1354 @@
 block discarded – undo
58 58
  */
59 59
 class DefaultShareProvider implements IShareProvider {
60 60
 
61
-	// Special share type for user modified group shares
62
-	const SHARE_TYPE_USERGROUP = 2;
63
-
64
-	/** @var IDBConnection */
65
-	private $dbConn;
66
-
67
-	/** @var IUserManager */
68
-	private $userManager;
69
-
70
-	/** @var IGroupManager */
71
-	private $groupManager;
72
-
73
-	/** @var IRootFolder */
74
-	private $rootFolder;
75
-
76
-	/** @var IMailer */
77
-	private $mailer;
78
-
79
-	/** @var Defaults */
80
-	private $defaults;
81
-
82
-	/** @var IL10N */
83
-	private $l;
84
-
85
-	/** @var IURLGenerator */
86
-	private $urlGenerator;
87
-
88
-	/**
89
-	 * DefaultShareProvider constructor.
90
-	 *
91
-	 * @param IDBConnection $connection
92
-	 * @param IUserManager $userManager
93
-	 * @param IGroupManager $groupManager
94
-	 * @param IRootFolder $rootFolder
95
-	 * @param IMailer $mailer ;
96
-	 * @param Defaults $defaults
97
-	 * @param IL10N $l
98
-	 * @param IURLGenerator $urlGenerator
99
-	 */
100
-	public function __construct(
101
-			IDBConnection $connection,
102
-			IUserManager $userManager,
103
-			IGroupManager $groupManager,
104
-			IRootFolder $rootFolder,
105
-			IMailer $mailer,
106
-			Defaults $defaults,
107
-			IL10N $l,
108
-			IURLGenerator $urlGenerator) {
109
-		$this->dbConn = $connection;
110
-		$this->userManager = $userManager;
111
-		$this->groupManager = $groupManager;
112
-		$this->rootFolder = $rootFolder;
113
-		$this->mailer = $mailer;
114
-		$this->defaults = $defaults;
115
-		$this->l = $l;
116
-		$this->urlGenerator = $urlGenerator;
117
-	}
118
-
119
-	/**
120
-	 * Return the identifier of this provider.
121
-	 *
122
-	 * @return string Containing only [a-zA-Z0-9]
123
-	 */
124
-	public function identifier() {
125
-		return 'ocinternal';
126
-	}
127
-
128
-	/**
129
-	 * Share a path
130
-	 *
131
-	 * @param \OCP\Share\IShare $share
132
-	 * @return \OCP\Share\IShare The share object
133
-	 * @throws ShareNotFound
134
-	 * @throws \Exception
135
-	 */
136
-	public function create(\OCP\Share\IShare $share) {
137
-		$qb = $this->dbConn->getQueryBuilder();
138
-
139
-		$qb->insert('share');
140
-		$qb->setValue('share_type', $qb->createNamedParameter($share->getShareType()));
141
-
142
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
143
-			//Set the UID of the user we share with
144
-			$qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith()));
145
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
146
-			//Set the GID of the group we share with
147
-			$qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith()));
148
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
149
-			//set label for public link
150
-			$qb->setValue('label', $qb->createNamedParameter($share->getLabel()));
151
-			//Set the token of the share
152
-			$qb->setValue('token', $qb->createNamedParameter($share->getToken()));
153
-
154
-			//If a password is set store it
155
-			if ($share->getPassword() !== null) {
156
-				$qb->setValue('password', $qb->createNamedParameter($share->getPassword()));
157
-			}
158
-
159
-			$qb->setValue('password_by_talk', $qb->createNamedParameter($share->getSendPasswordByTalk(), IQueryBuilder::PARAM_BOOL));
160
-
161
-			//If an expiration date is set store it
162
-			if ($share->getExpirationDate() !== null) {
163
-				$qb->setValue('expiration', $qb->createNamedParameter($share->getExpirationDate(), 'datetime'));
164
-			}
165
-
166
-			if (method_exists($share, 'getParent')) {
167
-				$qb->setValue('parent', $qb->createNamedParameter($share->getParent()));
168
-			}
169
-		} else {
170
-			throw new \Exception('invalid share type!');
171
-		}
172
-
173
-		// Set what is shares
174
-		$qb->setValue('item_type', $qb->createParameter('itemType'));
175
-		if ($share->getNode() instanceof \OCP\Files\File) {
176
-			$qb->setParameter('itemType', 'file');
177
-		} else {
178
-			$qb->setParameter('itemType', 'folder');
179
-		}
180
-
181
-		// Set the file id
182
-		$qb->setValue('item_source', $qb->createNamedParameter($share->getNode()->getId()));
183
-		$qb->setValue('file_source', $qb->createNamedParameter($share->getNode()->getId()));
184
-
185
-		// set the permissions
186
-		$qb->setValue('permissions', $qb->createNamedParameter($share->getPermissions()));
187
-
188
-		// Set who created this share
189
-		$qb->setValue('uid_initiator', $qb->createNamedParameter($share->getSharedBy()));
190
-
191
-		// Set who is the owner of this file/folder (and this the owner of the share)
192
-		$qb->setValue('uid_owner', $qb->createNamedParameter($share->getShareOwner()));
193
-
194
-		// Set the file target
195
-		$qb->setValue('file_target', $qb->createNamedParameter($share->getTarget()));
196
-
197
-		// Set the time this share was created
198
-		$qb->setValue('stime', $qb->createNamedParameter(time()));
199
-
200
-		// insert the data and fetch the id of the share
201
-		$this->dbConn->beginTransaction();
202
-		$qb->execute();
203
-		$id = $this->dbConn->lastInsertId('*PREFIX*share');
204
-
205
-		// Now fetch the inserted share and create a complete share object
206
-		$qb = $this->dbConn->getQueryBuilder();
207
-		$qb->select('*')
208
-			->from('share')
209
-			->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
210
-
211
-		$cursor = $qb->execute();
212
-		$data = $cursor->fetch();
213
-		$this->dbConn->commit();
214
-		$cursor->closeCursor();
215
-
216
-		if ($data === false) {
217
-			throw new ShareNotFound();
218
-		}
219
-
220
-		$mailSendValue = $share->getMailSend();
221
-		$data['mail_send'] = ($mailSendValue === null) ? true : $mailSendValue;
222
-
223
-		$share = $this->createShare($data);
224
-		return $share;
225
-	}
226
-
227
-	/**
228
-	 * Update a share
229
-	 *
230
-	 * @param \OCP\Share\IShare $share
231
-	 * @return \OCP\Share\IShare The share object
232
-	 * @throws ShareNotFound
233
-	 * @throws \OCP\Files\InvalidPathException
234
-	 * @throws \OCP\Files\NotFoundException
235
-	 */
236
-	public function update(\OCP\Share\IShare $share) {
237
-
238
-		$originalShare = $this->getShareById($share->getId());
239
-
240
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
241
-			/*
61
+    // Special share type for user modified group shares
62
+    const SHARE_TYPE_USERGROUP = 2;
63
+
64
+    /** @var IDBConnection */
65
+    private $dbConn;
66
+
67
+    /** @var IUserManager */
68
+    private $userManager;
69
+
70
+    /** @var IGroupManager */
71
+    private $groupManager;
72
+
73
+    /** @var IRootFolder */
74
+    private $rootFolder;
75
+
76
+    /** @var IMailer */
77
+    private $mailer;
78
+
79
+    /** @var Defaults */
80
+    private $defaults;
81
+
82
+    /** @var IL10N */
83
+    private $l;
84
+
85
+    /** @var IURLGenerator */
86
+    private $urlGenerator;
87
+
88
+    /**
89
+     * DefaultShareProvider constructor.
90
+     *
91
+     * @param IDBConnection $connection
92
+     * @param IUserManager $userManager
93
+     * @param IGroupManager $groupManager
94
+     * @param IRootFolder $rootFolder
95
+     * @param IMailer $mailer ;
96
+     * @param Defaults $defaults
97
+     * @param IL10N $l
98
+     * @param IURLGenerator $urlGenerator
99
+     */
100
+    public function __construct(
101
+            IDBConnection $connection,
102
+            IUserManager $userManager,
103
+            IGroupManager $groupManager,
104
+            IRootFolder $rootFolder,
105
+            IMailer $mailer,
106
+            Defaults $defaults,
107
+            IL10N $l,
108
+            IURLGenerator $urlGenerator) {
109
+        $this->dbConn = $connection;
110
+        $this->userManager = $userManager;
111
+        $this->groupManager = $groupManager;
112
+        $this->rootFolder = $rootFolder;
113
+        $this->mailer = $mailer;
114
+        $this->defaults = $defaults;
115
+        $this->l = $l;
116
+        $this->urlGenerator = $urlGenerator;
117
+    }
118
+
119
+    /**
120
+     * Return the identifier of this provider.
121
+     *
122
+     * @return string Containing only [a-zA-Z0-9]
123
+     */
124
+    public function identifier() {
125
+        return 'ocinternal';
126
+    }
127
+
128
+    /**
129
+     * Share a path
130
+     *
131
+     * @param \OCP\Share\IShare $share
132
+     * @return \OCP\Share\IShare The share object
133
+     * @throws ShareNotFound
134
+     * @throws \Exception
135
+     */
136
+    public function create(\OCP\Share\IShare $share) {
137
+        $qb = $this->dbConn->getQueryBuilder();
138
+
139
+        $qb->insert('share');
140
+        $qb->setValue('share_type', $qb->createNamedParameter($share->getShareType()));
141
+
142
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
143
+            //Set the UID of the user we share with
144
+            $qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith()));
145
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
146
+            //Set the GID of the group we share with
147
+            $qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith()));
148
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
149
+            //set label for public link
150
+            $qb->setValue('label', $qb->createNamedParameter($share->getLabel()));
151
+            //Set the token of the share
152
+            $qb->setValue('token', $qb->createNamedParameter($share->getToken()));
153
+
154
+            //If a password is set store it
155
+            if ($share->getPassword() !== null) {
156
+                $qb->setValue('password', $qb->createNamedParameter($share->getPassword()));
157
+            }
158
+
159
+            $qb->setValue('password_by_talk', $qb->createNamedParameter($share->getSendPasswordByTalk(), IQueryBuilder::PARAM_BOOL));
160
+
161
+            //If an expiration date is set store it
162
+            if ($share->getExpirationDate() !== null) {
163
+                $qb->setValue('expiration', $qb->createNamedParameter($share->getExpirationDate(), 'datetime'));
164
+            }
165
+
166
+            if (method_exists($share, 'getParent')) {
167
+                $qb->setValue('parent', $qb->createNamedParameter($share->getParent()));
168
+            }
169
+        } else {
170
+            throw new \Exception('invalid share type!');
171
+        }
172
+
173
+        // Set what is shares
174
+        $qb->setValue('item_type', $qb->createParameter('itemType'));
175
+        if ($share->getNode() instanceof \OCP\Files\File) {
176
+            $qb->setParameter('itemType', 'file');
177
+        } else {
178
+            $qb->setParameter('itemType', 'folder');
179
+        }
180
+
181
+        // Set the file id
182
+        $qb->setValue('item_source', $qb->createNamedParameter($share->getNode()->getId()));
183
+        $qb->setValue('file_source', $qb->createNamedParameter($share->getNode()->getId()));
184
+
185
+        // set the permissions
186
+        $qb->setValue('permissions', $qb->createNamedParameter($share->getPermissions()));
187
+
188
+        // Set who created this share
189
+        $qb->setValue('uid_initiator', $qb->createNamedParameter($share->getSharedBy()));
190
+
191
+        // Set who is the owner of this file/folder (and this the owner of the share)
192
+        $qb->setValue('uid_owner', $qb->createNamedParameter($share->getShareOwner()));
193
+
194
+        // Set the file target
195
+        $qb->setValue('file_target', $qb->createNamedParameter($share->getTarget()));
196
+
197
+        // Set the time this share was created
198
+        $qb->setValue('stime', $qb->createNamedParameter(time()));
199
+
200
+        // insert the data and fetch the id of the share
201
+        $this->dbConn->beginTransaction();
202
+        $qb->execute();
203
+        $id = $this->dbConn->lastInsertId('*PREFIX*share');
204
+
205
+        // Now fetch the inserted share and create a complete share object
206
+        $qb = $this->dbConn->getQueryBuilder();
207
+        $qb->select('*')
208
+            ->from('share')
209
+            ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
210
+
211
+        $cursor = $qb->execute();
212
+        $data = $cursor->fetch();
213
+        $this->dbConn->commit();
214
+        $cursor->closeCursor();
215
+
216
+        if ($data === false) {
217
+            throw new ShareNotFound();
218
+        }
219
+
220
+        $mailSendValue = $share->getMailSend();
221
+        $data['mail_send'] = ($mailSendValue === null) ? true : $mailSendValue;
222
+
223
+        $share = $this->createShare($data);
224
+        return $share;
225
+    }
226
+
227
+    /**
228
+     * Update a share
229
+     *
230
+     * @param \OCP\Share\IShare $share
231
+     * @return \OCP\Share\IShare The share object
232
+     * @throws ShareNotFound
233
+     * @throws \OCP\Files\InvalidPathException
234
+     * @throws \OCP\Files\NotFoundException
235
+     */
236
+    public function update(\OCP\Share\IShare $share) {
237
+
238
+        $originalShare = $this->getShareById($share->getId());
239
+
240
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
241
+            /*
242 242
 			 * We allow updating the recipient on user shares.
243 243
 			 */
244
-			$qb = $this->dbConn->getQueryBuilder();
245
-			$qb->update('share')
246
-				->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
247
-				->set('share_with', $qb->createNamedParameter($share->getSharedWith()))
248
-				->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
249
-				->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
250
-				->set('permissions', $qb->createNamedParameter($share->getPermissions()))
251
-				->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
252
-				->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
253
-				->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
254
-				->set('note', $qb->createNamedParameter($share->getNote()))
255
-				->execute();
256
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
257
-			$qb = $this->dbConn->getQueryBuilder();
258
-			$qb->update('share')
259
-				->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
260
-				->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
261
-				->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
262
-				->set('permissions', $qb->createNamedParameter($share->getPermissions()))
263
-				->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
264
-				->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
265
-				->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
266
-				->set('note', $qb->createNamedParameter($share->getNote()))
267
-				->execute();
268
-
269
-			/*
244
+            $qb = $this->dbConn->getQueryBuilder();
245
+            $qb->update('share')
246
+                ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
247
+                ->set('share_with', $qb->createNamedParameter($share->getSharedWith()))
248
+                ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
249
+                ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
250
+                ->set('permissions', $qb->createNamedParameter($share->getPermissions()))
251
+                ->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
252
+                ->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
253
+                ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
254
+                ->set('note', $qb->createNamedParameter($share->getNote()))
255
+                ->execute();
256
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
257
+            $qb = $this->dbConn->getQueryBuilder();
258
+            $qb->update('share')
259
+                ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
260
+                ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
261
+                ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
262
+                ->set('permissions', $qb->createNamedParameter($share->getPermissions()))
263
+                ->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
264
+                ->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
265
+                ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
266
+                ->set('note', $qb->createNamedParameter($share->getNote()))
267
+                ->execute();
268
+
269
+            /*
270 270
 			 * Update all user defined group shares
271 271
 			 */
272
-			$qb = $this->dbConn->getQueryBuilder();
273
-			$qb->update('share')
274
-				->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
275
-				->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
276
-				->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
277
-				->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
278
-				->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
279
-				->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
280
-				->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
281
-				->set('note', $qb->createNamedParameter($share->getNote()))
282
-				->execute();
283
-
284
-			/*
272
+            $qb = $this->dbConn->getQueryBuilder();
273
+            $qb->update('share')
274
+                ->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
275
+                ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
276
+                ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
277
+                ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
278
+                ->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
279
+                ->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
280
+                ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
281
+                ->set('note', $qb->createNamedParameter($share->getNote()))
282
+                ->execute();
283
+
284
+            /*
285 285
 			 * Now update the permissions for all children that have not set it to 0
286 286
 			 */
287
-			$qb = $this->dbConn->getQueryBuilder();
288
-			$qb->update('share')
289
-				->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
290
-				->andWhere($qb->expr()->neq('permissions', $qb->createNamedParameter(0)))
291
-				->set('permissions', $qb->createNamedParameter($share->getPermissions()))
292
-				->execute();
293
-
294
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
295
-			$qb = $this->dbConn->getQueryBuilder();
296
-			$qb->update('share')
297
-				->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
298
-				->set('password', $qb->createNamedParameter($share->getPassword()))
299
-				->set('password_by_talk', $qb->createNamedParameter($share->getSendPasswordByTalk(), IQueryBuilder::PARAM_BOOL))
300
-				->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
301
-				->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
302
-				->set('permissions', $qb->createNamedParameter($share->getPermissions()))
303
-				->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
304
-				->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
305
-				->set('token', $qb->createNamedParameter($share->getToken()))
306
-				->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
307
-				->set('note', $qb->createNamedParameter($share->getNote()))
308
-				->set('label', $qb->createNamedParameter($share->getLabel()))
309
-				->set('hide_download', $qb->createNamedParameter($share->getHideDownload() ? 1 : 0), IQueryBuilder::PARAM_INT)
310
-				->execute();
311
-		}
312
-
313
-		if ($originalShare->getNote() !== $share->getNote() && $share->getNote() !== '') {
314
-			$this->propagateNote($share);
315
-		}
316
-
317
-
318
-		return $share;
319
-	}
320
-
321
-	/**
322
-	 * Get all children of this share
323
-	 * FIXME: remove once https://github.com/owncloud/core/pull/21660 is in
324
-	 *
325
-	 * @param \OCP\Share\IShare $parent
326
-	 * @return \OCP\Share\IShare[]
327
-	 */
328
-	public function getChildren(\OCP\Share\IShare $parent) {
329
-		$children = [];
330
-
331
-		$qb = $this->dbConn->getQueryBuilder();
332
-		$qb->select('*')
333
-			->from('share')
334
-			->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId())))
335
-			->andWhere(
336
-				$qb->expr()->in(
337
-					'share_type',
338
-					$qb->createNamedParameter([
339
-						\OCP\Share::SHARE_TYPE_USER,
340
-						\OCP\Share::SHARE_TYPE_GROUP,
341
-						\OCP\Share::SHARE_TYPE_LINK,
342
-					], IQueryBuilder::PARAM_INT_ARRAY)
343
-				)
344
-			)
345
-			->andWhere($qb->expr()->orX(
346
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
347
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
348
-			))
349
-			->orderBy('id');
350
-
351
-		$cursor = $qb->execute();
352
-		while($data = $cursor->fetch()) {
353
-			$children[] = $this->createShare($data);
354
-		}
355
-		$cursor->closeCursor();
356
-
357
-		return $children;
358
-	}
359
-
360
-	/**
361
-	 * Delete a share
362
-	 *
363
-	 * @param \OCP\Share\IShare $share
364
-	 */
365
-	public function delete(\OCP\Share\IShare $share) {
366
-		$qb = $this->dbConn->getQueryBuilder();
367
-		$qb->delete('share')
368
-			->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())));
369
-
370
-		/*
287
+            $qb = $this->dbConn->getQueryBuilder();
288
+            $qb->update('share')
289
+                ->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
290
+                ->andWhere($qb->expr()->neq('permissions', $qb->createNamedParameter(0)))
291
+                ->set('permissions', $qb->createNamedParameter($share->getPermissions()))
292
+                ->execute();
293
+
294
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
295
+            $qb = $this->dbConn->getQueryBuilder();
296
+            $qb->update('share')
297
+                ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
298
+                ->set('password', $qb->createNamedParameter($share->getPassword()))
299
+                ->set('password_by_talk', $qb->createNamedParameter($share->getSendPasswordByTalk(), IQueryBuilder::PARAM_BOOL))
300
+                ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
301
+                ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
302
+                ->set('permissions', $qb->createNamedParameter($share->getPermissions()))
303
+                ->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
304
+                ->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
305
+                ->set('token', $qb->createNamedParameter($share->getToken()))
306
+                ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
307
+                ->set('note', $qb->createNamedParameter($share->getNote()))
308
+                ->set('label', $qb->createNamedParameter($share->getLabel()))
309
+                ->set('hide_download', $qb->createNamedParameter($share->getHideDownload() ? 1 : 0), IQueryBuilder::PARAM_INT)
310
+                ->execute();
311
+        }
312
+
313
+        if ($originalShare->getNote() !== $share->getNote() && $share->getNote() !== '') {
314
+            $this->propagateNote($share);
315
+        }
316
+
317
+
318
+        return $share;
319
+    }
320
+
321
+    /**
322
+     * Get all children of this share
323
+     * FIXME: remove once https://github.com/owncloud/core/pull/21660 is in
324
+     *
325
+     * @param \OCP\Share\IShare $parent
326
+     * @return \OCP\Share\IShare[]
327
+     */
328
+    public function getChildren(\OCP\Share\IShare $parent) {
329
+        $children = [];
330
+
331
+        $qb = $this->dbConn->getQueryBuilder();
332
+        $qb->select('*')
333
+            ->from('share')
334
+            ->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId())))
335
+            ->andWhere(
336
+                $qb->expr()->in(
337
+                    'share_type',
338
+                    $qb->createNamedParameter([
339
+                        \OCP\Share::SHARE_TYPE_USER,
340
+                        \OCP\Share::SHARE_TYPE_GROUP,
341
+                        \OCP\Share::SHARE_TYPE_LINK,
342
+                    ], IQueryBuilder::PARAM_INT_ARRAY)
343
+                )
344
+            )
345
+            ->andWhere($qb->expr()->orX(
346
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
347
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
348
+            ))
349
+            ->orderBy('id');
350
+
351
+        $cursor = $qb->execute();
352
+        while($data = $cursor->fetch()) {
353
+            $children[] = $this->createShare($data);
354
+        }
355
+        $cursor->closeCursor();
356
+
357
+        return $children;
358
+    }
359
+
360
+    /**
361
+     * Delete a share
362
+     *
363
+     * @param \OCP\Share\IShare $share
364
+     */
365
+    public function delete(\OCP\Share\IShare $share) {
366
+        $qb = $this->dbConn->getQueryBuilder();
367
+        $qb->delete('share')
368
+            ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())));
369
+
370
+        /*
371 371
 		 * If the share is a group share delete all possible
372 372
 		 * user defined groups shares.
373 373
 		 */
374
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
375
-			$qb->orWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())));
376
-		}
377
-
378
-		$qb->execute();
379
-	}
380
-
381
-	/**
382
-	 * Unshare a share from the recipient. If this is a group share
383
-	 * this means we need a special entry in the share db.
384
-	 *
385
-	 * @param \OCP\Share\IShare $share
386
-	 * @param string $recipient UserId of recipient
387
-	 * @throws BackendError
388
-	 * @throws ProviderException
389
-	 */
390
-	public function deleteFromSelf(\OCP\Share\IShare $share, $recipient) {
391
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
392
-
393
-			$group = $this->groupManager->get($share->getSharedWith());
394
-			$user = $this->userManager->get($recipient);
395
-
396
-			if (is_null($group)) {
397
-				throw new ProviderException('Group "' . $share->getSharedWith() . '" does not exist');
398
-			}
399
-
400
-			if (!$group->inGroup($user)) {
401
-				throw new ProviderException('Recipient not in receiving group');
402
-			}
403
-
404
-			// Try to fetch user specific share
405
-			$qb = $this->dbConn->getQueryBuilder();
406
-			$stmt = $qb->select('*')
407
-				->from('share')
408
-				->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
409
-				->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient)))
410
-				->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
411
-				->andWhere($qb->expr()->orX(
412
-					$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
413
-					$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
414
-				))
415
-				->execute();
416
-
417
-			$data = $stmt->fetch();
418
-
419
-			/*
374
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
375
+            $qb->orWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())));
376
+        }
377
+
378
+        $qb->execute();
379
+    }
380
+
381
+    /**
382
+     * Unshare a share from the recipient. If this is a group share
383
+     * this means we need a special entry in the share db.
384
+     *
385
+     * @param \OCP\Share\IShare $share
386
+     * @param string $recipient UserId of recipient
387
+     * @throws BackendError
388
+     * @throws ProviderException
389
+     */
390
+    public function deleteFromSelf(\OCP\Share\IShare $share, $recipient) {
391
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
392
+
393
+            $group = $this->groupManager->get($share->getSharedWith());
394
+            $user = $this->userManager->get($recipient);
395
+
396
+            if (is_null($group)) {
397
+                throw new ProviderException('Group "' . $share->getSharedWith() . '" does not exist');
398
+            }
399
+
400
+            if (!$group->inGroup($user)) {
401
+                throw new ProviderException('Recipient not in receiving group');
402
+            }
403
+
404
+            // Try to fetch user specific share
405
+            $qb = $this->dbConn->getQueryBuilder();
406
+            $stmt = $qb->select('*')
407
+                ->from('share')
408
+                ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
409
+                ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient)))
410
+                ->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
411
+                ->andWhere($qb->expr()->orX(
412
+                    $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
413
+                    $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
414
+                ))
415
+                ->execute();
416
+
417
+            $data = $stmt->fetch();
418
+
419
+            /*
420 420
 			 * Check if there already is a user specific group share.
421 421
 			 * If there is update it (if required).
422 422
 			 */
423
-			if ($data === false) {
424
-				$qb = $this->dbConn->getQueryBuilder();
425
-
426
-				$type = $share->getNodeType();
427
-
428
-				//Insert new share
429
-				$qb->insert('share')
430
-					->values([
431
-						'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP),
432
-						'share_with' => $qb->createNamedParameter($recipient),
433
-						'uid_owner' => $qb->createNamedParameter($share->getShareOwner()),
434
-						'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()),
435
-						'parent' => $qb->createNamedParameter($share->getId()),
436
-						'item_type' => $qb->createNamedParameter($type),
437
-						'item_source' => $qb->createNamedParameter($share->getNodeId()),
438
-						'file_source' => $qb->createNamedParameter($share->getNodeId()),
439
-						'file_target' => $qb->createNamedParameter($share->getTarget()),
440
-						'permissions' => $qb->createNamedParameter(0),
441
-						'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()),
442
-					])->execute();
443
-
444
-			} else if ($data['permissions'] !== 0) {
445
-
446
-				// Update existing usergroup share
447
-				$qb = $this->dbConn->getQueryBuilder();
448
-				$qb->update('share')
449
-					->set('permissions', $qb->createNamedParameter(0))
450
-					->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id'])))
451
-					->execute();
452
-			}
453
-
454
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
455
-
456
-			if ($share->getSharedWith() !== $recipient) {
457
-				throw new ProviderException('Recipient does not match');
458
-			}
459
-
460
-			// We can just delete user and link shares
461
-			$this->delete($share);
462
-		} else {
463
-			throw new ProviderException('Invalid shareType');
464
-		}
465
-	}
466
-
467
-	/**
468
-	 * @inheritdoc
469
-	 *
470
-	 * For now this only works for group shares
471
-	 * If this gets implemented for normal shares we have to extend it
472
-	 */
473
-	public function restore(IShare $share, string $recipient): IShare {
474
-		$qb = $this->dbConn->getQueryBuilder();
475
-		$qb->select('permissions')
476
-			->from('share')
477
-			->where(
478
-				$qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))
479
-			);
480
-		$cursor = $qb->execute();
481
-		$data = $cursor->fetch();
482
-		$cursor->closeCursor();
483
-
484
-		$originalPermission = $data['permissions'];
485
-
486
-		$qb = $this->dbConn->getQueryBuilder();
487
-		$qb->update('share')
488
-			->set('permissions', $qb->createNamedParameter($originalPermission))
489
-			->where(
490
-				$qb->expr()->eq('parent', $qb->createNamedParameter($share->getParent()))
491
-			)->andWhere(
492
-				$qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))
493
-			)->andWhere(
494
-				$qb->expr()->eq('share_with', $qb->createNamedParameter($recipient))
495
-			);
496
-
497
-		$qb->execute();
498
-
499
-		return $this->getShareById($share->getId(), $recipient);
500
-	}
501
-
502
-	/**
503
-	 * @inheritdoc
504
-	 */
505
-	public function move(\OCP\Share\IShare $share, $recipient) {
506
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
507
-			// Just update the target
508
-			$qb = $this->dbConn->getQueryBuilder();
509
-			$qb->update('share')
510
-				->set('file_target', $qb->createNamedParameter($share->getTarget()))
511
-				->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
512
-				->execute();
513
-
514
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
515
-
516
-			// Check if there is a usergroup share
517
-			$qb = $this->dbConn->getQueryBuilder();
518
-			$stmt = $qb->select('id')
519
-				->from('share')
520
-				->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
521
-				->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient)))
522
-				->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
523
-				->andWhere($qb->expr()->orX(
524
-					$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
525
-					$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
526
-				))
527
-				->setMaxResults(1)
528
-				->execute();
529
-
530
-			$data = $stmt->fetch();
531
-			$stmt->closeCursor();
532
-
533
-			if ($data === false) {
534
-				// No usergroup share yet. Create one.
535
-				$qb = $this->dbConn->getQueryBuilder();
536
-				$qb->insert('share')
537
-					->values([
538
-						'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP),
539
-						'share_with' => $qb->createNamedParameter($recipient),
540
-						'uid_owner' => $qb->createNamedParameter($share->getShareOwner()),
541
-						'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()),
542
-						'parent' => $qb->createNamedParameter($share->getId()),
543
-						'item_type' => $qb->createNamedParameter($share->getNodeType()),
544
-						'item_source' => $qb->createNamedParameter($share->getNodeId()),
545
-						'file_source' => $qb->createNamedParameter($share->getNodeId()),
546
-						'file_target' => $qb->createNamedParameter($share->getTarget()),
547
-						'permissions' => $qb->createNamedParameter($share->getPermissions()),
548
-						'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()),
549
-					])->execute();
550
-			} else {
551
-				// Already a usergroup share. Update it.
552
-				$qb = $this->dbConn->getQueryBuilder();
553
-				$qb->update('share')
554
-					->set('file_target', $qb->createNamedParameter($share->getTarget()))
555
-					->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id'])))
556
-					->execute();
557
-			}
558
-		}
559
-
560
-		return $share;
561
-	}
562
-
563
-	public function getSharesInFolder($userId, Folder $node, $reshares) {
564
-		$qb = $this->dbConn->getQueryBuilder();
565
-		$qb->select('*')
566
-			->from('share', 's')
567
-			->andWhere($qb->expr()->orX(
568
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
569
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
570
-			));
571
-
572
-		$qb->andWhere($qb->expr()->orX(
573
-			$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)),
574
-			$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)),
575
-			$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK))
576
-		));
577
-
578
-		/**
579
-		 * Reshares for this user are shares where they are the owner.
580
-		 */
581
-		if ($reshares === false) {
582
-			$qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
583
-		} else {
584
-			$qb->andWhere(
585
-				$qb->expr()->orX(
586
-					$qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
587
-					$qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
588
-				)
589
-			);
590
-		}
591
-
592
-		$qb->innerJoin('s', 'filecache' ,'f', $qb->expr()->eq('s.file_source', 'f.fileid'));
593
-		$qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())));
594
-
595
-		$qb->orderBy('id');
596
-
597
-		$cursor = $qb->execute();
598
-		$shares = [];
599
-		while ($data = $cursor->fetch()) {
600
-			$shares[$data['fileid']][] = $this->createShare($data);
601
-		}
602
-		$cursor->closeCursor();
603
-
604
-		return $shares;
605
-	}
606
-
607
-	/**
608
-	 * @inheritdoc
609
-	 */
610
-	public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) {
611
-		$qb = $this->dbConn->getQueryBuilder();
612
-		$qb->select('*')
613
-			->from('share')
614
-			->andWhere($qb->expr()->orX(
615
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
616
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
617
-			));
618
-
619
-		$qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter($shareType)));
620
-
621
-		/**
622
-		 * Reshares for this user are shares where they are the owner.
623
-		 */
624
-		if ($reshares === false) {
625
-			$qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
626
-		} else {
627
-			if ($node === null) {
628
-				$qb->andWhere(
629
-					$qb->expr()->orX(
630
-						$qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
631
-						$qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
632
-					)
633
-				);
634
-			}
635
-		}
636
-
637
-		if ($node !== null) {
638
-			$qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
639
-		}
640
-
641
-		if ($limit !== -1) {
642
-			$qb->setMaxResults($limit);
643
-		}
644
-
645
-		$qb->setFirstResult($offset);
646
-		$qb->orderBy('id');
647
-
648
-		$cursor = $qb->execute();
649
-		$shares = [];
650
-		while($data = $cursor->fetch()) {
651
-			$shares[] = $this->createShare($data);
652
-		}
653
-		$cursor->closeCursor();
654
-
655
-		return $shares;
656
-	}
657
-
658
-	/**
659
-	 * @inheritdoc
660
-	 */
661
-	public function getShareById($id, $recipientId = null) {
662
-		$qb = $this->dbConn->getQueryBuilder();
663
-
664
-		$qb->select('*')
665
-			->from('share')
666
-			->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
667
-			->andWhere(
668
-				$qb->expr()->in(
669
-					'share_type',
670
-					$qb->createNamedParameter([
671
-						\OCP\Share::SHARE_TYPE_USER,
672
-						\OCP\Share::SHARE_TYPE_GROUP,
673
-						\OCP\Share::SHARE_TYPE_LINK,
674
-					], IQueryBuilder::PARAM_INT_ARRAY)
675
-				)
676
-			)
677
-			->andWhere($qb->expr()->orX(
678
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
679
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
680
-			));
681
-
682
-		$cursor = $qb->execute();
683
-		$data = $cursor->fetch();
684
-		$cursor->closeCursor();
685
-
686
-		if ($data === false) {
687
-			throw new ShareNotFound();
688
-		}
689
-
690
-		try {
691
-			$share = $this->createShare($data);
692
-		} catch (InvalidShare $e) {
693
-			throw new ShareNotFound();
694
-		}
695
-
696
-		// If the recipient is set for a group share resolve to that user
697
-		if ($recipientId !== null && $share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
698
-			$share = $this->resolveGroupShares([$share], $recipientId)[0];
699
-		}
700
-
701
-		return $share;
702
-	}
703
-
704
-	/**
705
-	 * Get shares for a given path
706
-	 *
707
-	 * @param \OCP\Files\Node $path
708
-	 * @return \OCP\Share\IShare[]
709
-	 */
710
-	public function getSharesByPath(Node $path) {
711
-		$qb = $this->dbConn->getQueryBuilder();
712
-
713
-		$cursor = $qb->select('*')
714
-			->from('share')
715
-			->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId())))
716
-			->andWhere(
717
-				$qb->expr()->orX(
718
-					$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)),
719
-					$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))
720
-				)
721
-			)
722
-			->andWhere($qb->expr()->orX(
723
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
724
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
725
-			))
726
-			->execute();
727
-
728
-		$shares = [];
729
-		while($data = $cursor->fetch()) {
730
-			$shares[] = $this->createShare($data);
731
-		}
732
-		$cursor->closeCursor();
733
-
734
-		return $shares;
735
-	}
736
-
737
-	/**
738
-	 * Returns whether the given database result can be interpreted as
739
-	 * a share with accessible file (not trashed, not deleted)
740
-	 */
741
-	private function isAccessibleResult($data) {
742
-		// exclude shares leading to deleted file entries
743
-		if ($data['fileid'] === null) {
744
-			return false;
745
-		}
746
-
747
-		// exclude shares leading to trashbin on home storages
748
-		$pathSections = explode('/', $data['path'], 2);
749
-		// FIXME: would not detect rare md5'd home storage case properly
750
-		if ($pathSections[0] !== 'files'
751
-		    	&& in_array(explode(':', $data['storage_string_id'], 2)[0], array('home', 'object'))) {
752
-			return false;
753
-		}
754
-		return true;
755
-	}
756
-
757
-	/**
758
-	 * @inheritdoc
759
-	 */
760
-	public function getSharedWith($userId, $shareType, $node, $limit, $offset) {
761
-		/** @var Share[] $shares */
762
-		$shares = [];
763
-
764
-		if ($shareType === \OCP\Share::SHARE_TYPE_USER) {
765
-			//Get shares directly with this user
766
-			$qb = $this->dbConn->getQueryBuilder();
767
-			$qb->select('s.*',
768
-				'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash',
769
-				'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime',
770
-				'f.encrypted', 'f.unencrypted_size', 'f.etag', 'f.checksum'
771
-			)
772
-				->selectAlias('st.id', 'storage_string_id')
773
-				->from('share', 's')
774
-				->leftJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid'))
775
-				->leftJoin('f', 'storages', 'st', $qb->expr()->eq('f.storage', 'st.numeric_id'));
776
-
777
-			// Order by id
778
-			$qb->orderBy('s.id');
779
-
780
-			// Set limit and offset
781
-			if ($limit !== -1) {
782
-				$qb->setMaxResults($limit);
783
-			}
784
-			$qb->setFirstResult($offset);
785
-
786
-			$qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)))
787
-				->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)))
788
-				->andWhere($qb->expr()->orX(
789
-					$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
790
-					$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
791
-				));
792
-
793
-			// Filter by node if provided
794
-			if ($node !== null) {
795
-				$qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
796
-			}
797
-
798
-			$cursor = $qb->execute();
799
-
800
-			while($data = $cursor->fetch()) {
801
-				if ($this->isAccessibleResult($data)) {
802
-					$shares[] = $this->createShare($data);
803
-				}
804
-			}
805
-			$cursor->closeCursor();
806
-
807
-		} else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
808
-			$user = $this->userManager->get($userId);
809
-			$allGroups = $this->groupManager->getUserGroups($user);
810
-
811
-			/** @var Share[] $shares2 */
812
-			$shares2 = [];
813
-
814
-			$start = 0;
815
-			while(true) {
816
-				$groups = array_slice($allGroups, $start, 100);
817
-				$start += 100;
818
-
819
-				if ($groups === []) {
820
-					break;
821
-				}
822
-
823
-				$qb = $this->dbConn->getQueryBuilder();
824
-				$qb->select('s.*',
825
-					'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash',
826
-					'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime',
827
-					'f.encrypted', 'f.unencrypted_size', 'f.etag', 'f.checksum'
828
-				)
829
-					->selectAlias('st.id', 'storage_string_id')
830
-					->from('share', 's')
831
-					->leftJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid'))
832
-					->leftJoin('f', 'storages', 'st', $qb->expr()->eq('f.storage', 'st.numeric_id'))
833
-					->orderBy('s.id')
834
-					->setFirstResult(0);
835
-
836
-				if ($limit !== -1) {
837
-					$qb->setMaxResults($limit - count($shares));
838
-				}
839
-
840
-				// Filter by node if provided
841
-				if ($node !== null) {
842
-					$qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
843
-				}
844
-
845
-
846
-				$groups = array_filter($groups, function($group) { return $group instanceof IGroup; });
847
-				$groups = array_map(function(IGroup $group) { return $group->getGID(); }, $groups);
848
-
849
-				$qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)))
850
-					->andWhere($qb->expr()->in('share_with', $qb->createNamedParameter(
851
-						$groups,
852
-						IQueryBuilder::PARAM_STR_ARRAY
853
-					)))
854
-					->andWhere($qb->expr()->orX(
855
-						$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
856
-						$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
857
-					));
858
-
859
-				$cursor = $qb->execute();
860
-				while($data = $cursor->fetch()) {
861
-					if ($offset > 0) {
862
-						$offset--;
863
-						continue;
864
-					}
865
-
866
-					if ($this->isAccessibleResult($data)) {
867
-						$shares2[] = $this->createShare($data);
868
-					}
869
-				}
870
-				$cursor->closeCursor();
871
-			}
872
-
873
-			/*
423
+            if ($data === false) {
424
+                $qb = $this->dbConn->getQueryBuilder();
425
+
426
+                $type = $share->getNodeType();
427
+
428
+                //Insert new share
429
+                $qb->insert('share')
430
+                    ->values([
431
+                        'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP),
432
+                        'share_with' => $qb->createNamedParameter($recipient),
433
+                        'uid_owner' => $qb->createNamedParameter($share->getShareOwner()),
434
+                        'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()),
435
+                        'parent' => $qb->createNamedParameter($share->getId()),
436
+                        'item_type' => $qb->createNamedParameter($type),
437
+                        'item_source' => $qb->createNamedParameter($share->getNodeId()),
438
+                        'file_source' => $qb->createNamedParameter($share->getNodeId()),
439
+                        'file_target' => $qb->createNamedParameter($share->getTarget()),
440
+                        'permissions' => $qb->createNamedParameter(0),
441
+                        'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()),
442
+                    ])->execute();
443
+
444
+            } else if ($data['permissions'] !== 0) {
445
+
446
+                // Update existing usergroup share
447
+                $qb = $this->dbConn->getQueryBuilder();
448
+                $qb->update('share')
449
+                    ->set('permissions', $qb->createNamedParameter(0))
450
+                    ->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id'])))
451
+                    ->execute();
452
+            }
453
+
454
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
455
+
456
+            if ($share->getSharedWith() !== $recipient) {
457
+                throw new ProviderException('Recipient does not match');
458
+            }
459
+
460
+            // We can just delete user and link shares
461
+            $this->delete($share);
462
+        } else {
463
+            throw new ProviderException('Invalid shareType');
464
+        }
465
+    }
466
+
467
+    /**
468
+     * @inheritdoc
469
+     *
470
+     * For now this only works for group shares
471
+     * If this gets implemented for normal shares we have to extend it
472
+     */
473
+    public function restore(IShare $share, string $recipient): IShare {
474
+        $qb = $this->dbConn->getQueryBuilder();
475
+        $qb->select('permissions')
476
+            ->from('share')
477
+            ->where(
478
+                $qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))
479
+            );
480
+        $cursor = $qb->execute();
481
+        $data = $cursor->fetch();
482
+        $cursor->closeCursor();
483
+
484
+        $originalPermission = $data['permissions'];
485
+
486
+        $qb = $this->dbConn->getQueryBuilder();
487
+        $qb->update('share')
488
+            ->set('permissions', $qb->createNamedParameter($originalPermission))
489
+            ->where(
490
+                $qb->expr()->eq('parent', $qb->createNamedParameter($share->getParent()))
491
+            )->andWhere(
492
+                $qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))
493
+            )->andWhere(
494
+                $qb->expr()->eq('share_with', $qb->createNamedParameter($recipient))
495
+            );
496
+
497
+        $qb->execute();
498
+
499
+        return $this->getShareById($share->getId(), $recipient);
500
+    }
501
+
502
+    /**
503
+     * @inheritdoc
504
+     */
505
+    public function move(\OCP\Share\IShare $share, $recipient) {
506
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
507
+            // Just update the target
508
+            $qb = $this->dbConn->getQueryBuilder();
509
+            $qb->update('share')
510
+                ->set('file_target', $qb->createNamedParameter($share->getTarget()))
511
+                ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
512
+                ->execute();
513
+
514
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
515
+
516
+            // Check if there is a usergroup share
517
+            $qb = $this->dbConn->getQueryBuilder();
518
+            $stmt = $qb->select('id')
519
+                ->from('share')
520
+                ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
521
+                ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient)))
522
+                ->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
523
+                ->andWhere($qb->expr()->orX(
524
+                    $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
525
+                    $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
526
+                ))
527
+                ->setMaxResults(1)
528
+                ->execute();
529
+
530
+            $data = $stmt->fetch();
531
+            $stmt->closeCursor();
532
+
533
+            if ($data === false) {
534
+                // No usergroup share yet. Create one.
535
+                $qb = $this->dbConn->getQueryBuilder();
536
+                $qb->insert('share')
537
+                    ->values([
538
+                        'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP),
539
+                        'share_with' => $qb->createNamedParameter($recipient),
540
+                        'uid_owner' => $qb->createNamedParameter($share->getShareOwner()),
541
+                        'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()),
542
+                        'parent' => $qb->createNamedParameter($share->getId()),
543
+                        'item_type' => $qb->createNamedParameter($share->getNodeType()),
544
+                        'item_source' => $qb->createNamedParameter($share->getNodeId()),
545
+                        'file_source' => $qb->createNamedParameter($share->getNodeId()),
546
+                        'file_target' => $qb->createNamedParameter($share->getTarget()),
547
+                        'permissions' => $qb->createNamedParameter($share->getPermissions()),
548
+                        'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()),
549
+                    ])->execute();
550
+            } else {
551
+                // Already a usergroup share. Update it.
552
+                $qb = $this->dbConn->getQueryBuilder();
553
+                $qb->update('share')
554
+                    ->set('file_target', $qb->createNamedParameter($share->getTarget()))
555
+                    ->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id'])))
556
+                    ->execute();
557
+            }
558
+        }
559
+
560
+        return $share;
561
+    }
562
+
563
+    public function getSharesInFolder($userId, Folder $node, $reshares) {
564
+        $qb = $this->dbConn->getQueryBuilder();
565
+        $qb->select('*')
566
+            ->from('share', 's')
567
+            ->andWhere($qb->expr()->orX(
568
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
569
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
570
+            ));
571
+
572
+        $qb->andWhere($qb->expr()->orX(
573
+            $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)),
574
+            $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)),
575
+            $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK))
576
+        ));
577
+
578
+        /**
579
+         * Reshares for this user are shares where they are the owner.
580
+         */
581
+        if ($reshares === false) {
582
+            $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
583
+        } else {
584
+            $qb->andWhere(
585
+                $qb->expr()->orX(
586
+                    $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
587
+                    $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
588
+                )
589
+            );
590
+        }
591
+
592
+        $qb->innerJoin('s', 'filecache' ,'f', $qb->expr()->eq('s.file_source', 'f.fileid'));
593
+        $qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())));
594
+
595
+        $qb->orderBy('id');
596
+
597
+        $cursor = $qb->execute();
598
+        $shares = [];
599
+        while ($data = $cursor->fetch()) {
600
+            $shares[$data['fileid']][] = $this->createShare($data);
601
+        }
602
+        $cursor->closeCursor();
603
+
604
+        return $shares;
605
+    }
606
+
607
+    /**
608
+     * @inheritdoc
609
+     */
610
+    public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) {
611
+        $qb = $this->dbConn->getQueryBuilder();
612
+        $qb->select('*')
613
+            ->from('share')
614
+            ->andWhere($qb->expr()->orX(
615
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
616
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
617
+            ));
618
+
619
+        $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter($shareType)));
620
+
621
+        /**
622
+         * Reshares for this user are shares where they are the owner.
623
+         */
624
+        if ($reshares === false) {
625
+            $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
626
+        } else {
627
+            if ($node === null) {
628
+                $qb->andWhere(
629
+                    $qb->expr()->orX(
630
+                        $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
631
+                        $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
632
+                    )
633
+                );
634
+            }
635
+        }
636
+
637
+        if ($node !== null) {
638
+            $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
639
+        }
640
+
641
+        if ($limit !== -1) {
642
+            $qb->setMaxResults($limit);
643
+        }
644
+
645
+        $qb->setFirstResult($offset);
646
+        $qb->orderBy('id');
647
+
648
+        $cursor = $qb->execute();
649
+        $shares = [];
650
+        while($data = $cursor->fetch()) {
651
+            $shares[] = $this->createShare($data);
652
+        }
653
+        $cursor->closeCursor();
654
+
655
+        return $shares;
656
+    }
657
+
658
+    /**
659
+     * @inheritdoc
660
+     */
661
+    public function getShareById($id, $recipientId = null) {
662
+        $qb = $this->dbConn->getQueryBuilder();
663
+
664
+        $qb->select('*')
665
+            ->from('share')
666
+            ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
667
+            ->andWhere(
668
+                $qb->expr()->in(
669
+                    'share_type',
670
+                    $qb->createNamedParameter([
671
+                        \OCP\Share::SHARE_TYPE_USER,
672
+                        \OCP\Share::SHARE_TYPE_GROUP,
673
+                        \OCP\Share::SHARE_TYPE_LINK,
674
+                    ], IQueryBuilder::PARAM_INT_ARRAY)
675
+                )
676
+            )
677
+            ->andWhere($qb->expr()->orX(
678
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
679
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
680
+            ));
681
+
682
+        $cursor = $qb->execute();
683
+        $data = $cursor->fetch();
684
+        $cursor->closeCursor();
685
+
686
+        if ($data === false) {
687
+            throw new ShareNotFound();
688
+        }
689
+
690
+        try {
691
+            $share = $this->createShare($data);
692
+        } catch (InvalidShare $e) {
693
+            throw new ShareNotFound();
694
+        }
695
+
696
+        // If the recipient is set for a group share resolve to that user
697
+        if ($recipientId !== null && $share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
698
+            $share = $this->resolveGroupShares([$share], $recipientId)[0];
699
+        }
700
+
701
+        return $share;
702
+    }
703
+
704
+    /**
705
+     * Get shares for a given path
706
+     *
707
+     * @param \OCP\Files\Node $path
708
+     * @return \OCP\Share\IShare[]
709
+     */
710
+    public function getSharesByPath(Node $path) {
711
+        $qb = $this->dbConn->getQueryBuilder();
712
+
713
+        $cursor = $qb->select('*')
714
+            ->from('share')
715
+            ->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId())))
716
+            ->andWhere(
717
+                $qb->expr()->orX(
718
+                    $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)),
719
+                    $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))
720
+                )
721
+            )
722
+            ->andWhere($qb->expr()->orX(
723
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
724
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
725
+            ))
726
+            ->execute();
727
+
728
+        $shares = [];
729
+        while($data = $cursor->fetch()) {
730
+            $shares[] = $this->createShare($data);
731
+        }
732
+        $cursor->closeCursor();
733
+
734
+        return $shares;
735
+    }
736
+
737
+    /**
738
+     * Returns whether the given database result can be interpreted as
739
+     * a share with accessible file (not trashed, not deleted)
740
+     */
741
+    private function isAccessibleResult($data) {
742
+        // exclude shares leading to deleted file entries
743
+        if ($data['fileid'] === null) {
744
+            return false;
745
+        }
746
+
747
+        // exclude shares leading to trashbin on home storages
748
+        $pathSections = explode('/', $data['path'], 2);
749
+        // FIXME: would not detect rare md5'd home storage case properly
750
+        if ($pathSections[0] !== 'files'
751
+                && in_array(explode(':', $data['storage_string_id'], 2)[0], array('home', 'object'))) {
752
+            return false;
753
+        }
754
+        return true;
755
+    }
756
+
757
+    /**
758
+     * @inheritdoc
759
+     */
760
+    public function getSharedWith($userId, $shareType, $node, $limit, $offset) {
761
+        /** @var Share[] $shares */
762
+        $shares = [];
763
+
764
+        if ($shareType === \OCP\Share::SHARE_TYPE_USER) {
765
+            //Get shares directly with this user
766
+            $qb = $this->dbConn->getQueryBuilder();
767
+            $qb->select('s.*',
768
+                'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash',
769
+                'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime',
770
+                'f.encrypted', 'f.unencrypted_size', 'f.etag', 'f.checksum'
771
+            )
772
+                ->selectAlias('st.id', 'storage_string_id')
773
+                ->from('share', 's')
774
+                ->leftJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid'))
775
+                ->leftJoin('f', 'storages', 'st', $qb->expr()->eq('f.storage', 'st.numeric_id'));
776
+
777
+            // Order by id
778
+            $qb->orderBy('s.id');
779
+
780
+            // Set limit and offset
781
+            if ($limit !== -1) {
782
+                $qb->setMaxResults($limit);
783
+            }
784
+            $qb->setFirstResult($offset);
785
+
786
+            $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)))
787
+                ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)))
788
+                ->andWhere($qb->expr()->orX(
789
+                    $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
790
+                    $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
791
+                ));
792
+
793
+            // Filter by node if provided
794
+            if ($node !== null) {
795
+                $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
796
+            }
797
+
798
+            $cursor = $qb->execute();
799
+
800
+            while($data = $cursor->fetch()) {
801
+                if ($this->isAccessibleResult($data)) {
802
+                    $shares[] = $this->createShare($data);
803
+                }
804
+            }
805
+            $cursor->closeCursor();
806
+
807
+        } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
808
+            $user = $this->userManager->get($userId);
809
+            $allGroups = $this->groupManager->getUserGroups($user);
810
+
811
+            /** @var Share[] $shares2 */
812
+            $shares2 = [];
813
+
814
+            $start = 0;
815
+            while(true) {
816
+                $groups = array_slice($allGroups, $start, 100);
817
+                $start += 100;
818
+
819
+                if ($groups === []) {
820
+                    break;
821
+                }
822
+
823
+                $qb = $this->dbConn->getQueryBuilder();
824
+                $qb->select('s.*',
825
+                    'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash',
826
+                    'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime',
827
+                    'f.encrypted', 'f.unencrypted_size', 'f.etag', 'f.checksum'
828
+                )
829
+                    ->selectAlias('st.id', 'storage_string_id')
830
+                    ->from('share', 's')
831
+                    ->leftJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid'))
832
+                    ->leftJoin('f', 'storages', 'st', $qb->expr()->eq('f.storage', 'st.numeric_id'))
833
+                    ->orderBy('s.id')
834
+                    ->setFirstResult(0);
835
+
836
+                if ($limit !== -1) {
837
+                    $qb->setMaxResults($limit - count($shares));
838
+                }
839
+
840
+                // Filter by node if provided
841
+                if ($node !== null) {
842
+                    $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
843
+                }
844
+
845
+
846
+                $groups = array_filter($groups, function($group) { return $group instanceof IGroup; });
847
+                $groups = array_map(function(IGroup $group) { return $group->getGID(); }, $groups);
848
+
849
+                $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)))
850
+                    ->andWhere($qb->expr()->in('share_with', $qb->createNamedParameter(
851
+                        $groups,
852
+                        IQueryBuilder::PARAM_STR_ARRAY
853
+                    )))
854
+                    ->andWhere($qb->expr()->orX(
855
+                        $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
856
+                        $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
857
+                    ));
858
+
859
+                $cursor = $qb->execute();
860
+                while($data = $cursor->fetch()) {
861
+                    if ($offset > 0) {
862
+                        $offset--;
863
+                        continue;
864
+                    }
865
+
866
+                    if ($this->isAccessibleResult($data)) {
867
+                        $shares2[] = $this->createShare($data);
868
+                    }
869
+                }
870
+                $cursor->closeCursor();
871
+            }
872
+
873
+            /*
874 874
  			 * Resolve all group shares to user specific shares
875 875
  			 */
876
-			$shares = $this->resolveGroupShares($shares2, $userId);
877
-		} else {
878
-			throw new BackendError('Invalid backend');
879
-		}
880
-
881
-
882
-		return $shares;
883
-	}
884
-
885
-	/**
886
-	 * Get a share by token
887
-	 *
888
-	 * @param string $token
889
-	 * @return \OCP\Share\IShare
890
-	 * @throws ShareNotFound
891
-	 */
892
-	public function getShareByToken($token) {
893
-		$qb = $this->dbConn->getQueryBuilder();
894
-
895
-		$cursor = $qb->select('*')
896
-			->from('share')
897
-			->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK)))
898
-			->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token)))
899
-			->andWhere($qb->expr()->orX(
900
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
901
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
902
-			))
903
-			->execute();
904
-
905
-		$data = $cursor->fetch();
906
-
907
-		if ($data === false) {
908
-			throw new ShareNotFound();
909
-		}
910
-
911
-		try {
912
-			$share = $this->createShare($data);
913
-		} catch (InvalidShare $e) {
914
-			throw new ShareNotFound();
915
-		}
916
-
917
-		return $share;
918
-	}
919
-
920
-	/**
921
-	 * Create a share object from an database row
922
-	 *
923
-	 * @param mixed[] $data
924
-	 * @return \OCP\Share\IShare
925
-	 * @throws InvalidShare
926
-	 */
927
-	private function createShare($data) {
928
-		$share = new Share($this->rootFolder, $this->userManager);
929
-		$share->setId((int)$data['id'])
930
-			->setShareType((int)$data['share_type'])
931
-			->setPermissions((int)$data['permissions'])
932
-			->setTarget($data['file_target'])
933
-			->setNote($data['note'])
934
-			->setMailSend((bool)$data['mail_send'])
935
-			->setLabel($data['label']);
936
-
937
-		$shareTime = new \DateTime();
938
-		$shareTime->setTimestamp((int)$data['stime']);
939
-		$share->setShareTime($shareTime);
940
-
941
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
942
-			$share->setSharedWith($data['share_with']);
943
-			$user = $this->userManager->get($data['share_with']);
944
-			if ($user !== null) {
945
-				$share->setSharedWithDisplayName($user->getDisplayName());
946
-			}
947
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
948
-			$share->setSharedWith($data['share_with']);
949
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
950
-			$share->setPassword($data['password']);
951
-			$share->setSendPasswordByTalk((bool)$data['password_by_talk']);
952
-			$share->setToken($data['token']);
953
-		}
954
-
955
-		$share->setSharedBy($data['uid_initiator']);
956
-		$share->setShareOwner($data['uid_owner']);
957
-
958
-		$share->setNodeId((int)$data['file_source']);
959
-		$share->setNodeType($data['item_type']);
960
-
961
-		if ($data['expiration'] !== null) {
962
-			$expiration = \DateTime::createFromFormat('Y-m-d H:i:s', $data['expiration']);
963
-			$share->setExpirationDate($expiration);
964
-		}
965
-
966
-		if (isset($data['f_permissions'])) {
967
-			$entryData = $data;
968
-			$entryData['permissions'] = $entryData['f_permissions'];
969
-			$entryData['parent'] = $entryData['f_parent'];
970
-			$share->setNodeCacheEntry(Cache::cacheEntryFromData($entryData,
971
-				\OC::$server->getMimeTypeLoader()));
972
-		}
973
-
974
-		$share->setProviderId($this->identifier());
975
-		$share->setHideDownload((int)$data['hide_download'] === 1);
976
-
977
-		return $share;
978
-	}
979
-
980
-	/**
981
-	 * @param Share[] $shares
982
-	 * @param $userId
983
-	 * @return Share[] The updates shares if no update is found for a share return the original
984
-	 */
985
-	private function resolveGroupShares($shares, $userId) {
986
-		$result = [];
987
-
988
-		$start = 0;
989
-		while(true) {
990
-			/** @var Share[] $shareSlice */
991
-			$shareSlice = array_slice($shares, $start, 100);
992
-			$start += 100;
993
-
994
-			if ($shareSlice === []) {
995
-				break;
996
-			}
997
-
998
-			/** @var int[] $ids */
999
-			$ids = [];
1000
-			/** @var Share[] $shareMap */
1001
-			$shareMap = [];
1002
-
1003
-			foreach ($shareSlice as $share) {
1004
-				$ids[] = (int)$share->getId();
1005
-				$shareMap[$share->getId()] = $share;
1006
-			}
1007
-
1008
-			$qb = $this->dbConn->getQueryBuilder();
1009
-
1010
-			$query = $qb->select('*')
1011
-				->from('share')
1012
-				->where($qb->expr()->in('parent', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
1013
-				->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)))
1014
-				->andWhere($qb->expr()->orX(
1015
-					$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
1016
-					$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
1017
-				));
1018
-
1019
-			$stmt = $query->execute();
1020
-
1021
-			while($data = $stmt->fetch()) {
1022
-				$shareMap[$data['parent']]->setPermissions((int)$data['permissions']);
1023
-				$shareMap[$data['parent']]->setTarget($data['file_target']);
1024
-				$shareMap[$data['parent']]->setParent($data['parent']);
1025
-			}
1026
-
1027
-			$stmt->closeCursor();
1028
-
1029
-			foreach ($shareMap as $share) {
1030
-				$result[] = $share;
1031
-			}
1032
-		}
1033
-
1034
-		return $result;
1035
-	}
1036
-
1037
-	/**
1038
-	 * A user is deleted from the system
1039
-	 * So clean up the relevant shares.
1040
-	 *
1041
-	 * @param string $uid
1042
-	 * @param int $shareType
1043
-	 */
1044
-	public function userDeleted($uid, $shareType) {
1045
-		$qb = $this->dbConn->getQueryBuilder();
1046
-
1047
-		$qb->delete('share');
1048
-
1049
-		if ($shareType === \OCP\Share::SHARE_TYPE_USER) {
1050
-			/*
876
+            $shares = $this->resolveGroupShares($shares2, $userId);
877
+        } else {
878
+            throw new BackendError('Invalid backend');
879
+        }
880
+
881
+
882
+        return $shares;
883
+    }
884
+
885
+    /**
886
+     * Get a share by token
887
+     *
888
+     * @param string $token
889
+     * @return \OCP\Share\IShare
890
+     * @throws ShareNotFound
891
+     */
892
+    public function getShareByToken($token) {
893
+        $qb = $this->dbConn->getQueryBuilder();
894
+
895
+        $cursor = $qb->select('*')
896
+            ->from('share')
897
+            ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK)))
898
+            ->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token)))
899
+            ->andWhere($qb->expr()->orX(
900
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
901
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
902
+            ))
903
+            ->execute();
904
+
905
+        $data = $cursor->fetch();
906
+
907
+        if ($data === false) {
908
+            throw new ShareNotFound();
909
+        }
910
+
911
+        try {
912
+            $share = $this->createShare($data);
913
+        } catch (InvalidShare $e) {
914
+            throw new ShareNotFound();
915
+        }
916
+
917
+        return $share;
918
+    }
919
+
920
+    /**
921
+     * Create a share object from an database row
922
+     *
923
+     * @param mixed[] $data
924
+     * @return \OCP\Share\IShare
925
+     * @throws InvalidShare
926
+     */
927
+    private function createShare($data) {
928
+        $share = new Share($this->rootFolder, $this->userManager);
929
+        $share->setId((int)$data['id'])
930
+            ->setShareType((int)$data['share_type'])
931
+            ->setPermissions((int)$data['permissions'])
932
+            ->setTarget($data['file_target'])
933
+            ->setNote($data['note'])
934
+            ->setMailSend((bool)$data['mail_send'])
935
+            ->setLabel($data['label']);
936
+
937
+        $shareTime = new \DateTime();
938
+        $shareTime->setTimestamp((int)$data['stime']);
939
+        $share->setShareTime($shareTime);
940
+
941
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
942
+            $share->setSharedWith($data['share_with']);
943
+            $user = $this->userManager->get($data['share_with']);
944
+            if ($user !== null) {
945
+                $share->setSharedWithDisplayName($user->getDisplayName());
946
+            }
947
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
948
+            $share->setSharedWith($data['share_with']);
949
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
950
+            $share->setPassword($data['password']);
951
+            $share->setSendPasswordByTalk((bool)$data['password_by_talk']);
952
+            $share->setToken($data['token']);
953
+        }
954
+
955
+        $share->setSharedBy($data['uid_initiator']);
956
+        $share->setShareOwner($data['uid_owner']);
957
+
958
+        $share->setNodeId((int)$data['file_source']);
959
+        $share->setNodeType($data['item_type']);
960
+
961
+        if ($data['expiration'] !== null) {
962
+            $expiration = \DateTime::createFromFormat('Y-m-d H:i:s', $data['expiration']);
963
+            $share->setExpirationDate($expiration);
964
+        }
965
+
966
+        if (isset($data['f_permissions'])) {
967
+            $entryData = $data;
968
+            $entryData['permissions'] = $entryData['f_permissions'];
969
+            $entryData['parent'] = $entryData['f_parent'];
970
+            $share->setNodeCacheEntry(Cache::cacheEntryFromData($entryData,
971
+                \OC::$server->getMimeTypeLoader()));
972
+        }
973
+
974
+        $share->setProviderId($this->identifier());
975
+        $share->setHideDownload((int)$data['hide_download'] === 1);
976
+
977
+        return $share;
978
+    }
979
+
980
+    /**
981
+     * @param Share[] $shares
982
+     * @param $userId
983
+     * @return Share[] The updates shares if no update is found for a share return the original
984
+     */
985
+    private function resolveGroupShares($shares, $userId) {
986
+        $result = [];
987
+
988
+        $start = 0;
989
+        while(true) {
990
+            /** @var Share[] $shareSlice */
991
+            $shareSlice = array_slice($shares, $start, 100);
992
+            $start += 100;
993
+
994
+            if ($shareSlice === []) {
995
+                break;
996
+            }
997
+
998
+            /** @var int[] $ids */
999
+            $ids = [];
1000
+            /** @var Share[] $shareMap */
1001
+            $shareMap = [];
1002
+
1003
+            foreach ($shareSlice as $share) {
1004
+                $ids[] = (int)$share->getId();
1005
+                $shareMap[$share->getId()] = $share;
1006
+            }
1007
+
1008
+            $qb = $this->dbConn->getQueryBuilder();
1009
+
1010
+            $query = $qb->select('*')
1011
+                ->from('share')
1012
+                ->where($qb->expr()->in('parent', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
1013
+                ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)))
1014
+                ->andWhere($qb->expr()->orX(
1015
+                    $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
1016
+                    $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
1017
+                ));
1018
+
1019
+            $stmt = $query->execute();
1020
+
1021
+            while($data = $stmt->fetch()) {
1022
+                $shareMap[$data['parent']]->setPermissions((int)$data['permissions']);
1023
+                $shareMap[$data['parent']]->setTarget($data['file_target']);
1024
+                $shareMap[$data['parent']]->setParent($data['parent']);
1025
+            }
1026
+
1027
+            $stmt->closeCursor();
1028
+
1029
+            foreach ($shareMap as $share) {
1030
+                $result[] = $share;
1031
+            }
1032
+        }
1033
+
1034
+        return $result;
1035
+    }
1036
+
1037
+    /**
1038
+     * A user is deleted from the system
1039
+     * So clean up the relevant shares.
1040
+     *
1041
+     * @param string $uid
1042
+     * @param int $shareType
1043
+     */
1044
+    public function userDeleted($uid, $shareType) {
1045
+        $qb = $this->dbConn->getQueryBuilder();
1046
+
1047
+        $qb->delete('share');
1048
+
1049
+        if ($shareType === \OCP\Share::SHARE_TYPE_USER) {
1050
+            /*
1051 1051
 			 * Delete all user shares that are owned by this user
1052 1052
 			 * or that are received by this user
1053 1053
 			 */
1054 1054
 
1055
-			$qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)));
1055
+            $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)));
1056 1056
 
1057
-			$qb->andWhere(
1058
-				$qb->expr()->orX(
1059
-					$qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)),
1060
-					$qb->expr()->eq('share_with', $qb->createNamedParameter($uid))
1061
-				)
1062
-			);
1063
-		} else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
1064
-			/*
1057
+            $qb->andWhere(
1058
+                $qb->expr()->orX(
1059
+                    $qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)),
1060
+                    $qb->expr()->eq('share_with', $qb->createNamedParameter($uid))
1061
+                )
1062
+            );
1063
+        } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
1064
+            /*
1065 1065
 			 * Delete all group shares that are owned by this user
1066 1066
 			 * Or special user group shares that are received by this user
1067 1067
 			 */
1068
-			$qb->where(
1069
-				$qb->expr()->andX(
1070
-					$qb->expr()->orX(
1071
-						$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)),
1072
-						$qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))
1073
-					),
1074
-					$qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid))
1075
-				)
1076
-			);
1077
-
1078
-			$qb->orWhere(
1079
-				$qb->expr()->andX(
1080
-					$qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)),
1081
-					$qb->expr()->eq('share_with', $qb->createNamedParameter($uid))
1082
-				)
1083
-			);
1084
-		} else if ($shareType === \OCP\Share::SHARE_TYPE_LINK) {
1085
-			/*
1068
+            $qb->where(
1069
+                $qb->expr()->andX(
1070
+                    $qb->expr()->orX(
1071
+                        $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)),
1072
+                        $qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))
1073
+                    ),
1074
+                    $qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid))
1075
+                )
1076
+            );
1077
+
1078
+            $qb->orWhere(
1079
+                $qb->expr()->andX(
1080
+                    $qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)),
1081
+                    $qb->expr()->eq('share_with', $qb->createNamedParameter($uid))
1082
+                )
1083
+            );
1084
+        } else if ($shareType === \OCP\Share::SHARE_TYPE_LINK) {
1085
+            /*
1086 1086
 			 * Delete all link shares owned by this user.
1087 1087
 			 * And all link shares initiated by this user (until #22327 is in)
1088 1088
 			 */
1089
-			$qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK)));
1090
-
1091
-			$qb->andWhere(
1092
-				$qb->expr()->orX(
1093
-					$qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)),
1094
-					$qb->expr()->eq('uid_initiator', $qb->createNamedParameter($uid))
1095
-				)
1096
-			);
1097
-		}
1098
-
1099
-		$qb->execute();
1100
-	}
1101
-
1102
-	/**
1103
-	 * Delete all shares received by this group. As well as any custom group
1104
-	 * shares for group members.
1105
-	 *
1106
-	 * @param string $gid
1107
-	 */
1108
-	public function groupDeleted($gid) {
1109
-		/*
1089
+            $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK)));
1090
+
1091
+            $qb->andWhere(
1092
+                $qb->expr()->orX(
1093
+                    $qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)),
1094
+                    $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($uid))
1095
+                )
1096
+            );
1097
+        }
1098
+
1099
+        $qb->execute();
1100
+    }
1101
+
1102
+    /**
1103
+     * Delete all shares received by this group. As well as any custom group
1104
+     * shares for group members.
1105
+     *
1106
+     * @param string $gid
1107
+     */
1108
+    public function groupDeleted($gid) {
1109
+        /*
1110 1110
 		 * First delete all custom group shares for group members
1111 1111
 		 */
1112
-		$qb = $this->dbConn->getQueryBuilder();
1113
-		$qb->select('id')
1114
-			->from('share')
1115
-			->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)))
1116
-			->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid)));
1117
-
1118
-		$cursor = $qb->execute();
1119
-		$ids = [];
1120
-		while($row = $cursor->fetch()) {
1121
-			$ids[] = (int)$row['id'];
1122
-		}
1123
-		$cursor->closeCursor();
1124
-
1125
-		if (!empty($ids)) {
1126
-			$chunks = array_chunk($ids, 100);
1127
-			foreach ($chunks as $chunk) {
1128
-				$qb->delete('share')
1129
-					->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
1130
-					->andWhere($qb->expr()->in('parent', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY)));
1131
-				$qb->execute();
1132
-			}
1133
-		}
1134
-
1135
-		/*
1112
+        $qb = $this->dbConn->getQueryBuilder();
1113
+        $qb->select('id')
1114
+            ->from('share')
1115
+            ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)))
1116
+            ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid)));
1117
+
1118
+        $cursor = $qb->execute();
1119
+        $ids = [];
1120
+        while($row = $cursor->fetch()) {
1121
+            $ids[] = (int)$row['id'];
1122
+        }
1123
+        $cursor->closeCursor();
1124
+
1125
+        if (!empty($ids)) {
1126
+            $chunks = array_chunk($ids, 100);
1127
+            foreach ($chunks as $chunk) {
1128
+                $qb->delete('share')
1129
+                    ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
1130
+                    ->andWhere($qb->expr()->in('parent', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY)));
1131
+                $qb->execute();
1132
+            }
1133
+        }
1134
+
1135
+        /*
1136 1136
 		 * Now delete all the group shares
1137 1137
 		 */
1138
-		$qb = $this->dbConn->getQueryBuilder();
1139
-		$qb->delete('share')
1140
-			->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)))
1141
-			->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid)));
1142
-		$qb->execute();
1143
-	}
1144
-
1145
-	/**
1146
-	 * Delete custom group shares to this group for this user
1147
-	 *
1148
-	 * @param string $uid
1149
-	 * @param string $gid
1150
-	 */
1151
-	public function userDeletedFromGroup($uid, $gid) {
1152
-		/*
1138
+        $qb = $this->dbConn->getQueryBuilder();
1139
+        $qb->delete('share')
1140
+            ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)))
1141
+            ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid)));
1142
+        $qb->execute();
1143
+    }
1144
+
1145
+    /**
1146
+     * Delete custom group shares to this group for this user
1147
+     *
1148
+     * @param string $uid
1149
+     * @param string $gid
1150
+     */
1151
+    public function userDeletedFromGroup($uid, $gid) {
1152
+        /*
1153 1153
 		 * Get all group shares
1154 1154
 		 */
1155
-		$qb = $this->dbConn->getQueryBuilder();
1156
-		$qb->select('id')
1157
-			->from('share')
1158
-			->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)))
1159
-			->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid)));
1160
-
1161
-		$cursor = $qb->execute();
1162
-		$ids = [];
1163
-		while($row = $cursor->fetch()) {
1164
-			$ids[] = (int)$row['id'];
1165
-		}
1166
-		$cursor->closeCursor();
1167
-
1168
-		if (!empty($ids)) {
1169
-			$chunks = array_chunk($ids, 100);
1170
-			foreach ($chunks as $chunk) {
1171
-				/*
1155
+        $qb = $this->dbConn->getQueryBuilder();
1156
+        $qb->select('id')
1157
+            ->from('share')
1158
+            ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)))
1159
+            ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid)));
1160
+
1161
+        $cursor = $qb->execute();
1162
+        $ids = [];
1163
+        while($row = $cursor->fetch()) {
1164
+            $ids[] = (int)$row['id'];
1165
+        }
1166
+        $cursor->closeCursor();
1167
+
1168
+        if (!empty($ids)) {
1169
+            $chunks = array_chunk($ids, 100);
1170
+            foreach ($chunks as $chunk) {
1171
+                /*
1172 1172
 				 * Delete all special shares wit this users for the found group shares
1173 1173
 				 */
1174
-				$qb->delete('share')
1175
-					->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
1176
-					->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($uid)))
1177
-					->andWhere($qb->expr()->in('parent', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY)));
1178
-				$qb->execute();
1179
-			}
1180
-		}
1181
-	}
1182
-
1183
-	/**
1184
-	 * @inheritdoc
1185
-	 */
1186
-	public function getAccessList($nodes, $currentAccess) {
1187
-		$ids = [];
1188
-		foreach ($nodes as $node) {
1189
-			$ids[] = $node->getId();
1190
-		}
1191
-
1192
-		$qb = $this->dbConn->getQueryBuilder();
1193
-
1194
-		$or = $qb->expr()->orX(
1195
-			$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)),
1196
-			$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)),
1197
-			$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK))
1198
-		);
1199
-
1200
-		if ($currentAccess) {
1201
-			$or->add($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)));
1202
-		}
1203
-
1204
-		$qb->select('id', 'parent', 'share_type', 'share_with', 'file_source', 'file_target', 'permissions')
1205
-			->from('share')
1206
-			->where(
1207
-				$or
1208
-			)
1209
-			->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
1210
-			->andWhere($qb->expr()->orX(
1211
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
1212
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
1213
-			));
1214
-		$cursor = $qb->execute();
1215
-
1216
-		$users = [];
1217
-		$link = false;
1218
-		while($row = $cursor->fetch()) {
1219
-			$type = (int)$row['share_type'];
1220
-			if ($type === \OCP\Share::SHARE_TYPE_USER) {
1221
-				$uid = $row['share_with'];
1222
-				$users[$uid] = isset($users[$uid]) ? $users[$uid] : [];
1223
-				$users[$uid][$row['id']] = $row;
1224
-			} else if ($type === \OCP\Share::SHARE_TYPE_GROUP) {
1225
-				$gid = $row['share_with'];
1226
-				$group = $this->groupManager->get($gid);
1227
-
1228
-				if ($group === null) {
1229
-					continue;
1230
-				}
1231
-
1232
-				$userList = $group->getUsers();
1233
-				foreach ($userList as $user) {
1234
-					$uid = $user->getUID();
1235
-					$users[$uid] = isset($users[$uid]) ? $users[$uid] : [];
1236
-					$users[$uid][$row['id']] = $row;
1237
-				}
1238
-			} else if ($type === \OCP\Share::SHARE_TYPE_LINK) {
1239
-				$link = true;
1240
-			} else if ($type === self::SHARE_TYPE_USERGROUP && $currentAccess === true) {
1241
-				$uid = $row['share_with'];
1242
-				$users[$uid] = isset($users[$uid]) ? $users[$uid] : [];
1243
-				$users[$uid][$row['id']] = $row;
1244
-			}
1245
-		}
1246
-		$cursor->closeCursor();
1247
-
1248
-		if ($currentAccess === true) {
1249
-			$users = array_map([$this, 'filterSharesOfUser'], $users);
1250
-			$users = array_filter($users);
1251
-		} else {
1252
-			$users = array_keys($users);
1253
-		}
1254
-
1255
-		return ['users' => $users, 'public' => $link];
1256
-	}
1257
-
1258
-	/**
1259
-	 * For each user the path with the fewest slashes is returned
1260
-	 * @param array $shares
1261
-	 * @return array
1262
-	 */
1263
-	protected function filterSharesOfUser(array $shares) {
1264
-		// Group shares when the user has a share exception
1265
-		foreach ($shares as $id => $share) {
1266
-			$type = (int) $share['share_type'];
1267
-			$permissions = (int) $share['permissions'];
1268
-
1269
-			if ($type === self::SHARE_TYPE_USERGROUP) {
1270
-				unset($shares[$share['parent']]);
1271
-
1272
-				if ($permissions === 0) {
1273
-					unset($shares[$id]);
1274
-				}
1275
-			}
1276
-		}
1277
-
1278
-		$best = [];
1279
-		$bestDepth = 0;
1280
-		foreach ($shares as $id => $share) {
1281
-			$depth = substr_count($share['file_target'], '/');
1282
-			if (empty($best) || $depth < $bestDepth) {
1283
-				$bestDepth = $depth;
1284
-				$best = [
1285
-					'node_id' => $share['file_source'],
1286
-					'node_path' => $share['file_target'],
1287
-				];
1288
-			}
1289
-		}
1290
-
1291
-		return $best;
1292
-	}
1293
-
1294
-	/**
1295
-	 * propagate notes to the recipients
1296
-	 *
1297
-	 * @param IShare $share
1298
-	 * @throws \OCP\Files\NotFoundException
1299
-	 */
1300
-	private function propagateNote(IShare $share) {
1301
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
1302
-			$user = $this->userManager->get($share->getSharedWith());
1303
-			$this->sendNote([$user], $share);
1304
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
1305
-			$group = $this->groupManager->get($share->getSharedWith());
1306
-			$groupMembers = $group->getUsers();
1307
-			$this->sendNote($groupMembers, $share);
1308
-		}
1309
-	}
1310
-
1311
-	/**
1312
-	 * send note by mail
1313
-	 *
1314
-	 * @param array $recipients
1315
-	 * @param IShare $share
1316
-	 * @throws \OCP\Files\NotFoundException
1317
-	 */
1318
-	private function sendNote(array $recipients, IShare $share) {
1319
-
1320
-		$toList = [];
1321
-
1322
-		foreach ($recipients as $recipient) {
1323
-			/** @var IUser $recipient */
1324
-			$email = $recipient->getEMailAddress();
1325
-			if ($email) {
1326
-				$toList[$email] = $recipient->getDisplayName();
1327
-			}
1328
-		}
1329
-
1330
-		if (!empty($toList)) {
1331
-
1332
-			$filename = $share->getNode()->getName();
1333
-			$initiator = $share->getSharedBy();
1334
-			$note = $share->getNote();
1335
-
1336
-			$initiatorUser = $this->userManager->get($initiator);
1337
-			$initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
1338
-			$initiatorEmailAddress = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null;
1339
-			$plainHeading = $this->l->t('%1$s shared »%2$s« with you and wants to add:', [$initiatorDisplayName, $filename]);
1340
-			$htmlHeading = $this->l->t('%1$s shared »%2$s« with you and wants to add', [$initiatorDisplayName, $filename]);
1341
-			$message = $this->mailer->createMessage();
1342
-
1343
-			$emailTemplate = $this->mailer->createEMailTemplate('defaultShareProvider.sendNote');
1344
-
1345
-			$emailTemplate->setSubject($this->l->t('»%s« added a note to a file shared with you', [$initiatorDisplayName]));
1346
-			$emailTemplate->addHeader();
1347
-			$emailTemplate->addHeading($htmlHeading, $plainHeading);
1348
-			$emailTemplate->addBodyText(htmlspecialchars($note), $note);
1349
-
1350
-			$link = $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $share->getNode()->getId()]);
1351
-			$emailTemplate->addBodyButton(
1352
-				$this->l->t('Open »%s«', [$filename]),
1353
-				$link
1354
-			);
1355
-
1356
-
1357
-			// The "From" contains the sharers name
1358
-			$instanceName = $this->defaults->getName();
1359
-			$senderName = $this->l->t(
1360
-				'%1$s via %2$s',
1361
-				[
1362
-					$initiatorDisplayName,
1363
-					$instanceName
1364
-				]
1365
-			);
1366
-			$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
1367
-			if ($initiatorEmailAddress !== null) {
1368
-				$message->setReplyTo([$initiatorEmailAddress => $initiatorDisplayName]);
1369
-				$emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
1370
-			} else {
1371
-				$emailTemplate->addFooter();
1372
-			}
1373
-
1374
-			if (count($toList) === 1) {
1375
-				$message->setTo($toList);
1376
-			} else {
1377
-				$message->setTo([]);
1378
-				$message->setBcc($toList);
1379
-			}
1380
-			$message->useTemplate($emailTemplate);
1381
-			$this->mailer->send($message);
1382
-		}
1383
-
1384
-	}
1385
-
1386
-	public function getAllShares(): iterable {
1387
-		$qb = $this->dbConn->getQueryBuilder();
1388
-
1389
-		$qb->select('*')
1390
-			->from('share')
1391
-			->where(
1392
-				$qb->expr()->orX(
1393
-					$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_USER)),
1394
-					$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_GROUP)),
1395
-					$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_LINK))
1396
-				)
1397
-			);
1398
-
1399
-		$cursor = $qb->execute();
1400
-		while($data = $cursor->fetch()) {
1401
-			try {
1402
-				$share = $this->createShare($data);
1403
-			} catch (InvalidShare $e) {
1404
-				continue;
1405
-			}
1406
-
1407
-			yield $share;
1408
-		}
1409
-		$cursor->closeCursor();
1410
-	}
1174
+                $qb->delete('share')
1175
+                    ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
1176
+                    ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($uid)))
1177
+                    ->andWhere($qb->expr()->in('parent', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY)));
1178
+                $qb->execute();
1179
+            }
1180
+        }
1181
+    }
1182
+
1183
+    /**
1184
+     * @inheritdoc
1185
+     */
1186
+    public function getAccessList($nodes, $currentAccess) {
1187
+        $ids = [];
1188
+        foreach ($nodes as $node) {
1189
+            $ids[] = $node->getId();
1190
+        }
1191
+
1192
+        $qb = $this->dbConn->getQueryBuilder();
1193
+
1194
+        $or = $qb->expr()->orX(
1195
+            $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)),
1196
+            $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)),
1197
+            $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK))
1198
+        );
1199
+
1200
+        if ($currentAccess) {
1201
+            $or->add($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)));
1202
+        }
1203
+
1204
+        $qb->select('id', 'parent', 'share_type', 'share_with', 'file_source', 'file_target', 'permissions')
1205
+            ->from('share')
1206
+            ->where(
1207
+                $or
1208
+            )
1209
+            ->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
1210
+            ->andWhere($qb->expr()->orX(
1211
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
1212
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
1213
+            ));
1214
+        $cursor = $qb->execute();
1215
+
1216
+        $users = [];
1217
+        $link = false;
1218
+        while($row = $cursor->fetch()) {
1219
+            $type = (int)$row['share_type'];
1220
+            if ($type === \OCP\Share::SHARE_TYPE_USER) {
1221
+                $uid = $row['share_with'];
1222
+                $users[$uid] = isset($users[$uid]) ? $users[$uid] : [];
1223
+                $users[$uid][$row['id']] = $row;
1224
+            } else if ($type === \OCP\Share::SHARE_TYPE_GROUP) {
1225
+                $gid = $row['share_with'];
1226
+                $group = $this->groupManager->get($gid);
1227
+
1228
+                if ($group === null) {
1229
+                    continue;
1230
+                }
1231
+
1232
+                $userList = $group->getUsers();
1233
+                foreach ($userList as $user) {
1234
+                    $uid = $user->getUID();
1235
+                    $users[$uid] = isset($users[$uid]) ? $users[$uid] : [];
1236
+                    $users[$uid][$row['id']] = $row;
1237
+                }
1238
+            } else if ($type === \OCP\Share::SHARE_TYPE_LINK) {
1239
+                $link = true;
1240
+            } else if ($type === self::SHARE_TYPE_USERGROUP && $currentAccess === true) {
1241
+                $uid = $row['share_with'];
1242
+                $users[$uid] = isset($users[$uid]) ? $users[$uid] : [];
1243
+                $users[$uid][$row['id']] = $row;
1244
+            }
1245
+        }
1246
+        $cursor->closeCursor();
1247
+
1248
+        if ($currentAccess === true) {
1249
+            $users = array_map([$this, 'filterSharesOfUser'], $users);
1250
+            $users = array_filter($users);
1251
+        } else {
1252
+            $users = array_keys($users);
1253
+        }
1254
+
1255
+        return ['users' => $users, 'public' => $link];
1256
+    }
1257
+
1258
+    /**
1259
+     * For each user the path with the fewest slashes is returned
1260
+     * @param array $shares
1261
+     * @return array
1262
+     */
1263
+    protected function filterSharesOfUser(array $shares) {
1264
+        // Group shares when the user has a share exception
1265
+        foreach ($shares as $id => $share) {
1266
+            $type = (int) $share['share_type'];
1267
+            $permissions = (int) $share['permissions'];
1268
+
1269
+            if ($type === self::SHARE_TYPE_USERGROUP) {
1270
+                unset($shares[$share['parent']]);
1271
+
1272
+                if ($permissions === 0) {
1273
+                    unset($shares[$id]);
1274
+                }
1275
+            }
1276
+        }
1277
+
1278
+        $best = [];
1279
+        $bestDepth = 0;
1280
+        foreach ($shares as $id => $share) {
1281
+            $depth = substr_count($share['file_target'], '/');
1282
+            if (empty($best) || $depth < $bestDepth) {
1283
+                $bestDepth = $depth;
1284
+                $best = [
1285
+                    'node_id' => $share['file_source'],
1286
+                    'node_path' => $share['file_target'],
1287
+                ];
1288
+            }
1289
+        }
1290
+
1291
+        return $best;
1292
+    }
1293
+
1294
+    /**
1295
+     * propagate notes to the recipients
1296
+     *
1297
+     * @param IShare $share
1298
+     * @throws \OCP\Files\NotFoundException
1299
+     */
1300
+    private function propagateNote(IShare $share) {
1301
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
1302
+            $user = $this->userManager->get($share->getSharedWith());
1303
+            $this->sendNote([$user], $share);
1304
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
1305
+            $group = $this->groupManager->get($share->getSharedWith());
1306
+            $groupMembers = $group->getUsers();
1307
+            $this->sendNote($groupMembers, $share);
1308
+        }
1309
+    }
1310
+
1311
+    /**
1312
+     * send note by mail
1313
+     *
1314
+     * @param array $recipients
1315
+     * @param IShare $share
1316
+     * @throws \OCP\Files\NotFoundException
1317
+     */
1318
+    private function sendNote(array $recipients, IShare $share) {
1319
+
1320
+        $toList = [];
1321
+
1322
+        foreach ($recipients as $recipient) {
1323
+            /** @var IUser $recipient */
1324
+            $email = $recipient->getEMailAddress();
1325
+            if ($email) {
1326
+                $toList[$email] = $recipient->getDisplayName();
1327
+            }
1328
+        }
1329
+
1330
+        if (!empty($toList)) {
1331
+
1332
+            $filename = $share->getNode()->getName();
1333
+            $initiator = $share->getSharedBy();
1334
+            $note = $share->getNote();
1335
+
1336
+            $initiatorUser = $this->userManager->get($initiator);
1337
+            $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
1338
+            $initiatorEmailAddress = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null;
1339
+            $plainHeading = $this->l->t('%1$s shared »%2$s« with you and wants to add:', [$initiatorDisplayName, $filename]);
1340
+            $htmlHeading = $this->l->t('%1$s shared »%2$s« with you and wants to add', [$initiatorDisplayName, $filename]);
1341
+            $message = $this->mailer->createMessage();
1342
+
1343
+            $emailTemplate = $this->mailer->createEMailTemplate('defaultShareProvider.sendNote');
1344
+
1345
+            $emailTemplate->setSubject($this->l->t('»%s« added a note to a file shared with you', [$initiatorDisplayName]));
1346
+            $emailTemplate->addHeader();
1347
+            $emailTemplate->addHeading($htmlHeading, $plainHeading);
1348
+            $emailTemplate->addBodyText(htmlspecialchars($note), $note);
1349
+
1350
+            $link = $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $share->getNode()->getId()]);
1351
+            $emailTemplate->addBodyButton(
1352
+                $this->l->t('Open »%s«', [$filename]),
1353
+                $link
1354
+            );
1355
+
1356
+
1357
+            // The "From" contains the sharers name
1358
+            $instanceName = $this->defaults->getName();
1359
+            $senderName = $this->l->t(
1360
+                '%1$s via %2$s',
1361
+                [
1362
+                    $initiatorDisplayName,
1363
+                    $instanceName
1364
+                ]
1365
+            );
1366
+            $message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
1367
+            if ($initiatorEmailAddress !== null) {
1368
+                $message->setReplyTo([$initiatorEmailAddress => $initiatorDisplayName]);
1369
+                $emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
1370
+            } else {
1371
+                $emailTemplate->addFooter();
1372
+            }
1373
+
1374
+            if (count($toList) === 1) {
1375
+                $message->setTo($toList);
1376
+            } else {
1377
+                $message->setTo([]);
1378
+                $message->setBcc($toList);
1379
+            }
1380
+            $message->useTemplate($emailTemplate);
1381
+            $this->mailer->send($message);
1382
+        }
1383
+
1384
+    }
1385
+
1386
+    public function getAllShares(): iterable {
1387
+        $qb = $this->dbConn->getQueryBuilder();
1388
+
1389
+        $qb->select('*')
1390
+            ->from('share')
1391
+            ->where(
1392
+                $qb->expr()->orX(
1393
+                    $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_USER)),
1394
+                    $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_GROUP)),
1395
+                    $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_LINK))
1396
+                )
1397
+            );
1398
+
1399
+        $cursor = $qb->execute();
1400
+        while($data = $cursor->fetch()) {
1401
+            try {
1402
+                $share = $this->createShare($data);
1403
+            } catch (InvalidShare $e) {
1404
+                continue;
1405
+            }
1406
+
1407
+            yield $share;
1408
+        }
1409
+        $cursor->closeCursor();
1410
+    }
1411 1411
 }
Please login to merge, or discard this patch.
Spacing   +28 added lines, -28 removed lines patch added patch discarded remove patch
@@ -349,7 +349,7 @@  discard block
 block discarded – undo
349 349
 			->orderBy('id');
350 350
 
351 351
 		$cursor = $qb->execute();
352
-		while($data = $cursor->fetch()) {
352
+		while ($data = $cursor->fetch()) {
353 353
 			$children[] = $this->createShare($data);
354 354
 		}
355 355
 		$cursor->closeCursor();
@@ -394,7 +394,7 @@  discard block
 block discarded – undo
394 394
 			$user = $this->userManager->get($recipient);
395 395
 
396 396
 			if (is_null($group)) {
397
-				throw new ProviderException('Group "' . $share->getSharedWith() . '" does not exist');
397
+				throw new ProviderException('Group "'.$share->getSharedWith().'" does not exist');
398 398
 			}
399 399
 
400 400
 			if (!$group->inGroup($user)) {
@@ -589,7 +589,7 @@  discard block
 block discarded – undo
589 589
 			);
590 590
 		}
591 591
 
592
-		$qb->innerJoin('s', 'filecache' ,'f', $qb->expr()->eq('s.file_source', 'f.fileid'));
592
+		$qb->innerJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid'));
593 593
 		$qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())));
594 594
 
595 595
 		$qb->orderBy('id');
@@ -647,7 +647,7 @@  discard block
 block discarded – undo
647 647
 
648 648
 		$cursor = $qb->execute();
649 649
 		$shares = [];
650
-		while($data = $cursor->fetch()) {
650
+		while ($data = $cursor->fetch()) {
651 651
 			$shares[] = $this->createShare($data);
652 652
 		}
653 653
 		$cursor->closeCursor();
@@ -726,7 +726,7 @@  discard block
 block discarded – undo
726 726
 			->execute();
727 727
 
728 728
 		$shares = [];
729
-		while($data = $cursor->fetch()) {
729
+		while ($data = $cursor->fetch()) {
730 730
 			$shares[] = $this->createShare($data);
731 731
 		}
732 732
 		$cursor->closeCursor();
@@ -797,7 +797,7 @@  discard block
 block discarded – undo
797 797
 
798 798
 			$cursor = $qb->execute();
799 799
 
800
-			while($data = $cursor->fetch()) {
800
+			while ($data = $cursor->fetch()) {
801 801
 				if ($this->isAccessibleResult($data)) {
802 802
 					$shares[] = $this->createShare($data);
803 803
 				}
@@ -812,7 +812,7 @@  discard block
 block discarded – undo
812 812
 			$shares2 = [];
813 813
 
814 814
 			$start = 0;
815
-			while(true) {
815
+			while (true) {
816 816
 				$groups = array_slice($allGroups, $start, 100);
817 817
 				$start += 100;
818 818
 
@@ -857,7 +857,7 @@  discard block
 block discarded – undo
857 857
 					));
858 858
 
859 859
 				$cursor = $qb->execute();
860
-				while($data = $cursor->fetch()) {
860
+				while ($data = $cursor->fetch()) {
861 861
 					if ($offset > 0) {
862 862
 						$offset--;
863 863
 						continue;
@@ -926,16 +926,16 @@  discard block
 block discarded – undo
926 926
 	 */
927 927
 	private function createShare($data) {
928 928
 		$share = new Share($this->rootFolder, $this->userManager);
929
-		$share->setId((int)$data['id'])
930
-			->setShareType((int)$data['share_type'])
931
-			->setPermissions((int)$data['permissions'])
929
+		$share->setId((int) $data['id'])
930
+			->setShareType((int) $data['share_type'])
931
+			->setPermissions((int) $data['permissions'])
932 932
 			->setTarget($data['file_target'])
933 933
 			->setNote($data['note'])
934
-			->setMailSend((bool)$data['mail_send'])
934
+			->setMailSend((bool) $data['mail_send'])
935 935
 			->setLabel($data['label']);
936 936
 
937 937
 		$shareTime = new \DateTime();
938
-		$shareTime->setTimestamp((int)$data['stime']);
938
+		$shareTime->setTimestamp((int) $data['stime']);
939 939
 		$share->setShareTime($shareTime);
940 940
 
941 941
 		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
@@ -948,14 +948,14 @@  discard block
 block discarded – undo
948 948
 			$share->setSharedWith($data['share_with']);
949 949
 		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
950 950
 			$share->setPassword($data['password']);
951
-			$share->setSendPasswordByTalk((bool)$data['password_by_talk']);
951
+			$share->setSendPasswordByTalk((bool) $data['password_by_talk']);
952 952
 			$share->setToken($data['token']);
953 953
 		}
954 954
 
955 955
 		$share->setSharedBy($data['uid_initiator']);
956 956
 		$share->setShareOwner($data['uid_owner']);
957 957
 
958
-		$share->setNodeId((int)$data['file_source']);
958
+		$share->setNodeId((int) $data['file_source']);
959 959
 		$share->setNodeType($data['item_type']);
960 960
 
961 961
 		if ($data['expiration'] !== null) {
@@ -972,7 +972,7 @@  discard block
 block discarded – undo
972 972
 		}
973 973
 
974 974
 		$share->setProviderId($this->identifier());
975
-		$share->setHideDownload((int)$data['hide_download'] === 1);
975
+		$share->setHideDownload((int) $data['hide_download'] === 1);
976 976
 
977 977
 		return $share;
978 978
 	}
@@ -986,7 +986,7 @@  discard block
 block discarded – undo
986 986
 		$result = [];
987 987
 
988 988
 		$start = 0;
989
-		while(true) {
989
+		while (true) {
990 990
 			/** @var Share[] $shareSlice */
991 991
 			$shareSlice = array_slice($shares, $start, 100);
992 992
 			$start += 100;
@@ -1001,7 +1001,7 @@  discard block
 block discarded – undo
1001 1001
 			$shareMap = [];
1002 1002
 
1003 1003
 			foreach ($shareSlice as $share) {
1004
-				$ids[] = (int)$share->getId();
1004
+				$ids[] = (int) $share->getId();
1005 1005
 				$shareMap[$share->getId()] = $share;
1006 1006
 			}
1007 1007
 
@@ -1018,8 +1018,8 @@  discard block
 block discarded – undo
1018 1018
 
1019 1019
 			$stmt = $query->execute();
1020 1020
 
1021
-			while($data = $stmt->fetch()) {
1022
-				$shareMap[$data['parent']]->setPermissions((int)$data['permissions']);
1021
+			while ($data = $stmt->fetch()) {
1022
+				$shareMap[$data['parent']]->setPermissions((int) $data['permissions']);
1023 1023
 				$shareMap[$data['parent']]->setTarget($data['file_target']);
1024 1024
 				$shareMap[$data['parent']]->setParent($data['parent']);
1025 1025
 			}
@@ -1117,8 +1117,8 @@  discard block
 block discarded – undo
1117 1117
 
1118 1118
 		$cursor = $qb->execute();
1119 1119
 		$ids = [];
1120
-		while($row = $cursor->fetch()) {
1121
-			$ids[] = (int)$row['id'];
1120
+		while ($row = $cursor->fetch()) {
1121
+			$ids[] = (int) $row['id'];
1122 1122
 		}
1123 1123
 		$cursor->closeCursor();
1124 1124
 
@@ -1160,8 +1160,8 @@  discard block
 block discarded – undo
1160 1160
 
1161 1161
 		$cursor = $qb->execute();
1162 1162
 		$ids = [];
1163
-		while($row = $cursor->fetch()) {
1164
-			$ids[] = (int)$row['id'];
1163
+		while ($row = $cursor->fetch()) {
1164
+			$ids[] = (int) $row['id'];
1165 1165
 		}
1166 1166
 		$cursor->closeCursor();
1167 1167
 
@@ -1215,8 +1215,8 @@  discard block
 block discarded – undo
1215 1215
 
1216 1216
 		$users = [];
1217 1217
 		$link = false;
1218
-		while($row = $cursor->fetch()) {
1219
-			$type = (int)$row['share_type'];
1218
+		while ($row = $cursor->fetch()) {
1219
+			$type = (int) $row['share_type'];
1220 1220
 			if ($type === \OCP\Share::SHARE_TYPE_USER) {
1221 1221
 				$uid = $row['share_with'];
1222 1222
 				$users[$uid] = isset($users[$uid]) ? $users[$uid] : [];
@@ -1366,7 +1366,7 @@  discard block
 block discarded – undo
1366 1366
 			$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
1367 1367
 			if ($initiatorEmailAddress !== null) {
1368 1368
 				$message->setReplyTo([$initiatorEmailAddress => $initiatorDisplayName]);
1369
-				$emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
1369
+				$emailTemplate->addFooter($instanceName.' - '.$this->defaults->getSlogan());
1370 1370
 			} else {
1371 1371
 				$emailTemplate->addFooter();
1372 1372
 			}
@@ -1397,7 +1397,7 @@  discard block
 block discarded – undo
1397 1397
 			);
1398 1398
 
1399 1399
 		$cursor = $qb->execute();
1400
-		while($data = $cursor->fetch()) {
1400
+		while ($data = $cursor->fetch()) {
1401 1401
 			try {
1402 1402
 				$share = $this->createShare($data);
1403 1403
 			} catch (InvalidShare $e) {
Please login to merge, or discard this patch.
lib/private/Share20/Manager.php 1 patch
Indentation   +1583 added lines, -1583 removed lines patch added patch discarded remove patch
@@ -72,1610 +72,1610 @@
 block discarded – undo
72 72
  */
73 73
 class Manager implements IManager {
74 74
 
75
-	/** @var IProviderFactory */
76
-	private $factory;
77
-	/** @var ILogger */
78
-	private $logger;
79
-	/** @var IConfig */
80
-	private $config;
81
-	/** @var ISecureRandom */
82
-	private $secureRandom;
83
-	/** @var IHasher */
84
-	private $hasher;
85
-	/** @var IMountManager */
86
-	private $mountManager;
87
-	/** @var IGroupManager */
88
-	private $groupManager;
89
-	/** @var IL10N */
90
-	private $l;
91
-	/** @var IFactory */
92
-	private $l10nFactory;
93
-	/** @var IUserManager */
94
-	private $userManager;
95
-	/** @var IRootFolder */
96
-	private $rootFolder;
97
-	/** @var CappedMemoryCache */
98
-	private $sharingDisabledForUsersCache;
99
-	/** @var EventDispatcherInterface */
100
-	private $eventDispatcher;
101
-	/** @var LegacyHooks */
102
-	private $legacyHooks;
103
-	/** @var IMailer */
104
-	private $mailer;
105
-	/** @var IURLGenerator */
106
-	private $urlGenerator;
107
-	/** @var \OC_Defaults */
108
-	private $defaults;
109
-
110
-
111
-	/**
112
-	 * Manager constructor.
113
-	 *
114
-	 * @param ILogger $logger
115
-	 * @param IConfig $config
116
-	 * @param ISecureRandom $secureRandom
117
-	 * @param IHasher $hasher
118
-	 * @param IMountManager $mountManager
119
-	 * @param IGroupManager $groupManager
120
-	 * @param IL10N $l
121
-	 * @param IFactory $l10nFactory
122
-	 * @param IProviderFactory $factory
123
-	 * @param IUserManager $userManager
124
-	 * @param IRootFolder $rootFolder
125
-	 * @param EventDispatcherInterface $eventDispatcher
126
-	 * @param IMailer $mailer
127
-	 * @param IURLGenerator $urlGenerator
128
-	 * @param \OC_Defaults $defaults
129
-	 */
130
-	public function __construct(
131
-			ILogger $logger,
132
-			IConfig $config,
133
-			ISecureRandom $secureRandom,
134
-			IHasher $hasher,
135
-			IMountManager $mountManager,
136
-			IGroupManager $groupManager,
137
-			IL10N $l,
138
-			IFactory $l10nFactory,
139
-			IProviderFactory $factory,
140
-			IUserManager $userManager,
141
-			IRootFolder $rootFolder,
142
-			EventDispatcherInterface $eventDispatcher,
143
-			IMailer $mailer,
144
-			IURLGenerator $urlGenerator,
145
-			\OC_Defaults $defaults
146
-	) {
147
-		$this->logger = $logger;
148
-		$this->config = $config;
149
-		$this->secureRandom = $secureRandom;
150
-		$this->hasher = $hasher;
151
-		$this->mountManager = $mountManager;
152
-		$this->groupManager = $groupManager;
153
-		$this->l = $l;
154
-		$this->l10nFactory = $l10nFactory;
155
-		$this->factory = $factory;
156
-		$this->userManager = $userManager;
157
-		$this->rootFolder = $rootFolder;
158
-		$this->eventDispatcher = $eventDispatcher;
159
-		$this->sharingDisabledForUsersCache = new CappedMemoryCache();
160
-		$this->legacyHooks = new LegacyHooks($this->eventDispatcher);
161
-		$this->mailer = $mailer;
162
-		$this->urlGenerator = $urlGenerator;
163
-		$this->defaults = $defaults;
164
-	}
165
-
166
-	/**
167
-	 * Convert from a full share id to a tuple (providerId, shareId)
168
-	 *
169
-	 * @param string $id
170
-	 * @return string[]
171
-	 */
172
-	private function splitFullId($id) {
173
-		return explode(':', $id, 2);
174
-	}
175
-
176
-	/**
177
-	 * Verify if a password meets all requirements
178
-	 *
179
-	 * @param string $password
180
-	 * @throws \Exception
181
-	 */
182
-	protected function verifyPassword($password) {
183
-		if ($password === null) {
184
-			// No password is set, check if this is allowed.
185
-			if ($this->shareApiLinkEnforcePassword()) {
186
-				throw new \InvalidArgumentException('Passwords are enforced for link shares');
187
-			}
188
-
189
-			return;
190
-		}
191
-
192
-		// Let others verify the password
193
-		try {
194
-			$event = new GenericEvent($password);
195
-			$this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event);
196
-		} catch (HintException $e) {
197
-			throw new \Exception($e->getHint());
198
-		}
199
-	}
200
-
201
-	/**
202
-	 * Check for generic requirements before creating a share
203
-	 *
204
-	 * @param \OCP\Share\IShare $share
205
-	 * @throws \InvalidArgumentException
206
-	 * @throws GenericShareException
207
-	 *
208
-	 * @suppress PhanUndeclaredClassMethod
209
-	 */
210
-	protected function generalCreateChecks(\OCP\Share\IShare $share) {
211
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
212
-			// We expect a valid user as sharedWith for user shares
213
-			if (!$this->userManager->userExists($share->getSharedWith())) {
214
-				throw new \InvalidArgumentException('SharedWith is not a valid user');
215
-			}
216
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
217
-			// We expect a valid group as sharedWith for group shares
218
-			if (!$this->groupManager->groupExists($share->getSharedWith())) {
219
-				throw new \InvalidArgumentException('SharedWith is not a valid group');
220
-			}
221
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
222
-			if ($share->getSharedWith() !== null) {
223
-				throw new \InvalidArgumentException('SharedWith should be empty');
224
-			}
225
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE) {
226
-			if ($share->getSharedWith() === null) {
227
-				throw new \InvalidArgumentException('SharedWith should not be empty');
228
-			}
229
-		}  else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE_GROUP) {
230
-			if ($share->getSharedWith() === null) {
231
-				throw new \InvalidArgumentException('SharedWith should not be empty');
232
-			}
233
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
234
-			if ($share->getSharedWith() === null) {
235
-				throw new \InvalidArgumentException('SharedWith should not be empty');
236
-			}
237
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_CIRCLE) {
238
-			$circle = \OCA\Circles\Api\v1\Circles::detailsCircle($share->getSharedWith());
239
-			if ($circle === null) {
240
-				throw new \InvalidArgumentException('SharedWith is not a valid circle');
241
-			}
242
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_ROOM) {
243
-		} else {
244
-			// We can't handle other types yet
245
-			throw new \InvalidArgumentException('unknown share type');
246
-		}
247
-
248
-		// Verify the initiator of the share is set
249
-		if ($share->getSharedBy() === null) {
250
-			throw new \InvalidArgumentException('SharedBy should be set');
251
-		}
252
-
253
-		// Cannot share with yourself
254
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
255
-			$share->getSharedWith() === $share->getSharedBy()) {
256
-			throw new \InvalidArgumentException('Can’t share with yourself');
257
-		}
258
-
259
-		// The path should be set
260
-		if ($share->getNode() === null) {
261
-			throw new \InvalidArgumentException('Path should be set');
262
-		}
263
-
264
-		// And it should be a file or a folder
265
-		if (!($share->getNode() instanceof \OCP\Files\File) &&
266
-				!($share->getNode() instanceof \OCP\Files\Folder)) {
267
-			throw new \InvalidArgumentException('Path should be either a file or a folder');
268
-		}
269
-
270
-		// And you can't share your rootfolder
271
-		if ($this->userManager->userExists($share->getSharedBy())) {
272
-			$userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
273
-			$userFolderPath = $userFolder->getPath();
274
-		} else {
275
-			$userFolder = $this->rootFolder->getUserFolder($share->getShareOwner());
276
-			$userFolderPath = $userFolder->getPath();
277
-		}
278
-		if ($userFolderPath === $share->getNode()->getPath()) {
279
-			throw new \InvalidArgumentException('You can’t share your root folder');
280
-		}
281
-
282
-		// Check if we actually have share permissions
283
-		if (!$share->getNode()->isShareable()) {
284
-			$message_t = $this->l->t('You are not allowed to share %s', [$share->getNode()->getPath()]);
285
-			throw new GenericShareException($message_t, $message_t, 404);
286
-		}
287
-
288
-		// Permissions should be set
289
-		if ($share->getPermissions() === null) {
290
-			throw new \InvalidArgumentException('A share requires permissions');
291
-		}
292
-
293
-		$isFederatedShare = $share->getNode()->getStorage()->instanceOfStorage('\OCA\Files_Sharing\External\Storage');
294
-		$permissions = 0;
295
-		$mount = $share->getNode()->getMountPoint();
296
-		if (!$isFederatedShare && $share->getNode()->getOwner()->getUID() !== $share->getSharedBy()) {
297
-			// When it's a reshare use the parent share permissions as maximum
298
-			$userMountPointId = $mount->getStorageRootId();
299
-			$userMountPoints = $userFolder->getById($userMountPointId);
300
-			$userMountPoint = array_shift($userMountPoints);
301
-
302
-			/* Check if this is an incoming share */
303
-			$incomingShares = $this->getSharedWith($share->getSharedBy(), Share::SHARE_TYPE_USER, $userMountPoint, -1, 0);
304
-			$incomingShares = array_merge($incomingShares, $this->getSharedWith($share->getSharedBy(), Share::SHARE_TYPE_GROUP, $userMountPoint, -1, 0));
305
-			$incomingShares = array_merge($incomingShares, $this->getSharedWith($share->getSharedBy(), Share::SHARE_TYPE_ROOM, $userMountPoint, -1, 0));
306
-
307
-			/** @var \OCP\Share\IShare[] $incomingShares */
308
-			if (!empty($incomingShares)) {
309
-				foreach ($incomingShares as $incomingShare) {
310
-					$permissions |= $incomingShare->getPermissions();
311
-				}
312
-			}
313
-		} else {
314
-			/*
75
+    /** @var IProviderFactory */
76
+    private $factory;
77
+    /** @var ILogger */
78
+    private $logger;
79
+    /** @var IConfig */
80
+    private $config;
81
+    /** @var ISecureRandom */
82
+    private $secureRandom;
83
+    /** @var IHasher */
84
+    private $hasher;
85
+    /** @var IMountManager */
86
+    private $mountManager;
87
+    /** @var IGroupManager */
88
+    private $groupManager;
89
+    /** @var IL10N */
90
+    private $l;
91
+    /** @var IFactory */
92
+    private $l10nFactory;
93
+    /** @var IUserManager */
94
+    private $userManager;
95
+    /** @var IRootFolder */
96
+    private $rootFolder;
97
+    /** @var CappedMemoryCache */
98
+    private $sharingDisabledForUsersCache;
99
+    /** @var EventDispatcherInterface */
100
+    private $eventDispatcher;
101
+    /** @var LegacyHooks */
102
+    private $legacyHooks;
103
+    /** @var IMailer */
104
+    private $mailer;
105
+    /** @var IURLGenerator */
106
+    private $urlGenerator;
107
+    /** @var \OC_Defaults */
108
+    private $defaults;
109
+
110
+
111
+    /**
112
+     * Manager constructor.
113
+     *
114
+     * @param ILogger $logger
115
+     * @param IConfig $config
116
+     * @param ISecureRandom $secureRandom
117
+     * @param IHasher $hasher
118
+     * @param IMountManager $mountManager
119
+     * @param IGroupManager $groupManager
120
+     * @param IL10N $l
121
+     * @param IFactory $l10nFactory
122
+     * @param IProviderFactory $factory
123
+     * @param IUserManager $userManager
124
+     * @param IRootFolder $rootFolder
125
+     * @param EventDispatcherInterface $eventDispatcher
126
+     * @param IMailer $mailer
127
+     * @param IURLGenerator $urlGenerator
128
+     * @param \OC_Defaults $defaults
129
+     */
130
+    public function __construct(
131
+            ILogger $logger,
132
+            IConfig $config,
133
+            ISecureRandom $secureRandom,
134
+            IHasher $hasher,
135
+            IMountManager $mountManager,
136
+            IGroupManager $groupManager,
137
+            IL10N $l,
138
+            IFactory $l10nFactory,
139
+            IProviderFactory $factory,
140
+            IUserManager $userManager,
141
+            IRootFolder $rootFolder,
142
+            EventDispatcherInterface $eventDispatcher,
143
+            IMailer $mailer,
144
+            IURLGenerator $urlGenerator,
145
+            \OC_Defaults $defaults
146
+    ) {
147
+        $this->logger = $logger;
148
+        $this->config = $config;
149
+        $this->secureRandom = $secureRandom;
150
+        $this->hasher = $hasher;
151
+        $this->mountManager = $mountManager;
152
+        $this->groupManager = $groupManager;
153
+        $this->l = $l;
154
+        $this->l10nFactory = $l10nFactory;
155
+        $this->factory = $factory;
156
+        $this->userManager = $userManager;
157
+        $this->rootFolder = $rootFolder;
158
+        $this->eventDispatcher = $eventDispatcher;
159
+        $this->sharingDisabledForUsersCache = new CappedMemoryCache();
160
+        $this->legacyHooks = new LegacyHooks($this->eventDispatcher);
161
+        $this->mailer = $mailer;
162
+        $this->urlGenerator = $urlGenerator;
163
+        $this->defaults = $defaults;
164
+    }
165
+
166
+    /**
167
+     * Convert from a full share id to a tuple (providerId, shareId)
168
+     *
169
+     * @param string $id
170
+     * @return string[]
171
+     */
172
+    private function splitFullId($id) {
173
+        return explode(':', $id, 2);
174
+    }
175
+
176
+    /**
177
+     * Verify if a password meets all requirements
178
+     *
179
+     * @param string $password
180
+     * @throws \Exception
181
+     */
182
+    protected function verifyPassword($password) {
183
+        if ($password === null) {
184
+            // No password is set, check if this is allowed.
185
+            if ($this->shareApiLinkEnforcePassword()) {
186
+                throw new \InvalidArgumentException('Passwords are enforced for link shares');
187
+            }
188
+
189
+            return;
190
+        }
191
+
192
+        // Let others verify the password
193
+        try {
194
+            $event = new GenericEvent($password);
195
+            $this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event);
196
+        } catch (HintException $e) {
197
+            throw new \Exception($e->getHint());
198
+        }
199
+    }
200
+
201
+    /**
202
+     * Check for generic requirements before creating a share
203
+     *
204
+     * @param \OCP\Share\IShare $share
205
+     * @throws \InvalidArgumentException
206
+     * @throws GenericShareException
207
+     *
208
+     * @suppress PhanUndeclaredClassMethod
209
+     */
210
+    protected function generalCreateChecks(\OCP\Share\IShare $share) {
211
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
212
+            // We expect a valid user as sharedWith for user shares
213
+            if (!$this->userManager->userExists($share->getSharedWith())) {
214
+                throw new \InvalidArgumentException('SharedWith is not a valid user');
215
+            }
216
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
217
+            // We expect a valid group as sharedWith for group shares
218
+            if (!$this->groupManager->groupExists($share->getSharedWith())) {
219
+                throw new \InvalidArgumentException('SharedWith is not a valid group');
220
+            }
221
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
222
+            if ($share->getSharedWith() !== null) {
223
+                throw new \InvalidArgumentException('SharedWith should be empty');
224
+            }
225
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE) {
226
+            if ($share->getSharedWith() === null) {
227
+                throw new \InvalidArgumentException('SharedWith should not be empty');
228
+            }
229
+        }  else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE_GROUP) {
230
+            if ($share->getSharedWith() === null) {
231
+                throw new \InvalidArgumentException('SharedWith should not be empty');
232
+            }
233
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
234
+            if ($share->getSharedWith() === null) {
235
+                throw new \InvalidArgumentException('SharedWith should not be empty');
236
+            }
237
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_CIRCLE) {
238
+            $circle = \OCA\Circles\Api\v1\Circles::detailsCircle($share->getSharedWith());
239
+            if ($circle === null) {
240
+                throw new \InvalidArgumentException('SharedWith is not a valid circle');
241
+            }
242
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_ROOM) {
243
+        } else {
244
+            // We can't handle other types yet
245
+            throw new \InvalidArgumentException('unknown share type');
246
+        }
247
+
248
+        // Verify the initiator of the share is set
249
+        if ($share->getSharedBy() === null) {
250
+            throw new \InvalidArgumentException('SharedBy should be set');
251
+        }
252
+
253
+        // Cannot share with yourself
254
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
255
+            $share->getSharedWith() === $share->getSharedBy()) {
256
+            throw new \InvalidArgumentException('Can’t share with yourself');
257
+        }
258
+
259
+        // The path should be set
260
+        if ($share->getNode() === null) {
261
+            throw new \InvalidArgumentException('Path should be set');
262
+        }
263
+
264
+        // And it should be a file or a folder
265
+        if (!($share->getNode() instanceof \OCP\Files\File) &&
266
+                !($share->getNode() instanceof \OCP\Files\Folder)) {
267
+            throw new \InvalidArgumentException('Path should be either a file or a folder');
268
+        }
269
+
270
+        // And you can't share your rootfolder
271
+        if ($this->userManager->userExists($share->getSharedBy())) {
272
+            $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
273
+            $userFolderPath = $userFolder->getPath();
274
+        } else {
275
+            $userFolder = $this->rootFolder->getUserFolder($share->getShareOwner());
276
+            $userFolderPath = $userFolder->getPath();
277
+        }
278
+        if ($userFolderPath === $share->getNode()->getPath()) {
279
+            throw new \InvalidArgumentException('You can’t share your root folder');
280
+        }
281
+
282
+        // Check if we actually have share permissions
283
+        if (!$share->getNode()->isShareable()) {
284
+            $message_t = $this->l->t('You are not allowed to share %s', [$share->getNode()->getPath()]);
285
+            throw new GenericShareException($message_t, $message_t, 404);
286
+        }
287
+
288
+        // Permissions should be set
289
+        if ($share->getPermissions() === null) {
290
+            throw new \InvalidArgumentException('A share requires permissions');
291
+        }
292
+
293
+        $isFederatedShare = $share->getNode()->getStorage()->instanceOfStorage('\OCA\Files_Sharing\External\Storage');
294
+        $permissions = 0;
295
+        $mount = $share->getNode()->getMountPoint();
296
+        if (!$isFederatedShare && $share->getNode()->getOwner()->getUID() !== $share->getSharedBy()) {
297
+            // When it's a reshare use the parent share permissions as maximum
298
+            $userMountPointId = $mount->getStorageRootId();
299
+            $userMountPoints = $userFolder->getById($userMountPointId);
300
+            $userMountPoint = array_shift($userMountPoints);
301
+
302
+            /* Check if this is an incoming share */
303
+            $incomingShares = $this->getSharedWith($share->getSharedBy(), Share::SHARE_TYPE_USER, $userMountPoint, -1, 0);
304
+            $incomingShares = array_merge($incomingShares, $this->getSharedWith($share->getSharedBy(), Share::SHARE_TYPE_GROUP, $userMountPoint, -1, 0));
305
+            $incomingShares = array_merge($incomingShares, $this->getSharedWith($share->getSharedBy(), Share::SHARE_TYPE_ROOM, $userMountPoint, -1, 0));
306
+
307
+            /** @var \OCP\Share\IShare[] $incomingShares */
308
+            if (!empty($incomingShares)) {
309
+                foreach ($incomingShares as $incomingShare) {
310
+                    $permissions |= $incomingShare->getPermissions();
311
+                }
312
+            }
313
+        } else {
314
+            /*
315 315
 			 * Quick fix for #23536
316 316
 			 * Non moveable mount points do not have update and delete permissions
317 317
 			 * while we 'most likely' do have that on the storage.
318 318
 			 */
319
-			$permissions = $share->getNode()->getPermissions();
320
-			if (!($mount instanceof MoveableMount)) {
321
-				$permissions |= \OCP\Constants::PERMISSION_DELETE | \OCP\Constants::PERMISSION_UPDATE;
322
-			}
323
-		}
324
-
325
-		// Check that we do not share with more permissions than we have
326
-		if ($share->getPermissions() & ~$permissions) {
327
-			$message_t = $this->l->t('Can’t increase permissions of %s', [$share->getNode()->getPath()]);
328
-			throw new GenericShareException($message_t, $message_t, 404);
329
-		}
330
-
331
-
332
-		// Check that read permissions are always set
333
-		// Link shares are allowed to have no read permissions to allow upload to hidden folders
334
-		$noReadPermissionRequired = $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK
335
-			|| $share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL;
336
-		if (!$noReadPermissionRequired &&
337
-			($share->getPermissions() & \OCP\Constants::PERMISSION_READ) === 0) {
338
-			throw new \InvalidArgumentException('Shares need at least read permissions');
339
-		}
340
-
341
-		if ($share->getNode() instanceof \OCP\Files\File) {
342
-			if ($share->getPermissions() & \OCP\Constants::PERMISSION_DELETE) {
343
-				$message_t = $this->l->t('Files can’t be shared with delete permissions');
344
-				throw new GenericShareException($message_t);
345
-			}
346
-			if ($share->getPermissions() & \OCP\Constants::PERMISSION_CREATE) {
347
-				$message_t = $this->l->t('Files can’t be shared with create permissions');
348
-				throw new GenericShareException($message_t);
349
-			}
350
-		}
351
-	}
352
-
353
-	/**
354
-	 * Validate if the expiration date fits the system settings
355
-	 *
356
-	 * @param \OCP\Share\IShare $share The share to validate the expiration date of
357
-	 * @return \OCP\Share\IShare The modified share object
358
-	 * @throws GenericShareException
359
-	 * @throws \InvalidArgumentException
360
-	 * @throws \Exception
361
-	 */
362
-	protected function validateExpirationDate(\OCP\Share\IShare $share) {
363
-
364
-		$expirationDate = $share->getExpirationDate();
365
-
366
-		if ($expirationDate !== null) {
367
-			//Make sure the expiration date is a date
368
-			$expirationDate->setTime(0, 0, 0);
369
-
370
-			$date = new \DateTime();
371
-			$date->setTime(0, 0, 0);
372
-			if ($date >= $expirationDate) {
373
-				$message = $this->l->t('Expiration date is in the past');
374
-				throw new GenericShareException($message, $message, 404);
375
-			}
376
-		}
377
-
378
-		// If expiredate is empty set a default one if there is a default
379
-		$fullId = null;
380
-		try {
381
-			$fullId = $share->getFullId();
382
-		} catch (\UnexpectedValueException $e) {
383
-			// This is a new share
384
-		}
385
-
386
-		if ($fullId === null && $expirationDate === null && $this->shareApiLinkDefaultExpireDate()) {
387
-			$expirationDate = new \DateTime();
388
-			$expirationDate->setTime(0,0,0);
389
-			$expirationDate->add(new \DateInterval('P'.$this->shareApiLinkDefaultExpireDays().'D'));
390
-		}
391
-
392
-		// If we enforce the expiration date check that is does not exceed
393
-		if ($this->shareApiLinkDefaultExpireDateEnforced()) {
394
-			if ($expirationDate === null) {
395
-				throw new \InvalidArgumentException('Expiration date is enforced');
396
-			}
397
-
398
-			$date = new \DateTime();
399
-			$date->setTime(0, 0, 0);
400
-			$date->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D'));
401
-			if ($date < $expirationDate) {
402
-				$message = $this->l->t('Can’t set expiration date more than %s days in the future', [$this->shareApiLinkDefaultExpireDays()]);
403
-				throw new GenericShareException($message, $message, 404);
404
-			}
405
-		}
406
-
407
-		$accepted = true;
408
-		$message = '';
409
-		\OCP\Util::emitHook('\OC\Share', 'verifyExpirationDate', [
410
-			'expirationDate' => &$expirationDate,
411
-			'accepted' => &$accepted,
412
-			'message' => &$message,
413
-			'passwordSet' => $share->getPassword() !== null,
414
-		]);
415
-
416
-		if (!$accepted) {
417
-			throw new \Exception($message);
418
-		}
419
-
420
-		$share->setExpirationDate($expirationDate);
421
-
422
-		return $share;
423
-	}
424
-
425
-	/**
426
-	 * Check for pre share requirements for user shares
427
-	 *
428
-	 * @param \OCP\Share\IShare $share
429
-	 * @throws \Exception
430
-	 */
431
-	protected function userCreateChecks(\OCP\Share\IShare $share) {
432
-		// Check if we can share with group members only
433
-		if ($this->shareWithGroupMembersOnly()) {
434
-			$sharedBy = $this->userManager->get($share->getSharedBy());
435
-			$sharedWith = $this->userManager->get($share->getSharedWith());
436
-			// Verify we can share with this user
437
-			$groups = array_intersect(
438
-					$this->groupManager->getUserGroupIds($sharedBy),
439
-					$this->groupManager->getUserGroupIds($sharedWith)
440
-			);
441
-			if (empty($groups)) {
442
-				throw new \Exception('Sharing is only allowed with group members');
443
-			}
444
-		}
445
-
446
-		/*
319
+            $permissions = $share->getNode()->getPermissions();
320
+            if (!($mount instanceof MoveableMount)) {
321
+                $permissions |= \OCP\Constants::PERMISSION_DELETE | \OCP\Constants::PERMISSION_UPDATE;
322
+            }
323
+        }
324
+
325
+        // Check that we do not share with more permissions than we have
326
+        if ($share->getPermissions() & ~$permissions) {
327
+            $message_t = $this->l->t('Can’t increase permissions of %s', [$share->getNode()->getPath()]);
328
+            throw new GenericShareException($message_t, $message_t, 404);
329
+        }
330
+
331
+
332
+        // Check that read permissions are always set
333
+        // Link shares are allowed to have no read permissions to allow upload to hidden folders
334
+        $noReadPermissionRequired = $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK
335
+            || $share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL;
336
+        if (!$noReadPermissionRequired &&
337
+            ($share->getPermissions() & \OCP\Constants::PERMISSION_READ) === 0) {
338
+            throw new \InvalidArgumentException('Shares need at least read permissions');
339
+        }
340
+
341
+        if ($share->getNode() instanceof \OCP\Files\File) {
342
+            if ($share->getPermissions() & \OCP\Constants::PERMISSION_DELETE) {
343
+                $message_t = $this->l->t('Files can’t be shared with delete permissions');
344
+                throw new GenericShareException($message_t);
345
+            }
346
+            if ($share->getPermissions() & \OCP\Constants::PERMISSION_CREATE) {
347
+                $message_t = $this->l->t('Files can’t be shared with create permissions');
348
+                throw new GenericShareException($message_t);
349
+            }
350
+        }
351
+    }
352
+
353
+    /**
354
+     * Validate if the expiration date fits the system settings
355
+     *
356
+     * @param \OCP\Share\IShare $share The share to validate the expiration date of
357
+     * @return \OCP\Share\IShare The modified share object
358
+     * @throws GenericShareException
359
+     * @throws \InvalidArgumentException
360
+     * @throws \Exception
361
+     */
362
+    protected function validateExpirationDate(\OCP\Share\IShare $share) {
363
+
364
+        $expirationDate = $share->getExpirationDate();
365
+
366
+        if ($expirationDate !== null) {
367
+            //Make sure the expiration date is a date
368
+            $expirationDate->setTime(0, 0, 0);
369
+
370
+            $date = new \DateTime();
371
+            $date->setTime(0, 0, 0);
372
+            if ($date >= $expirationDate) {
373
+                $message = $this->l->t('Expiration date is in the past');
374
+                throw new GenericShareException($message, $message, 404);
375
+            }
376
+        }
377
+
378
+        // If expiredate is empty set a default one if there is a default
379
+        $fullId = null;
380
+        try {
381
+            $fullId = $share->getFullId();
382
+        } catch (\UnexpectedValueException $e) {
383
+            // This is a new share
384
+        }
385
+
386
+        if ($fullId === null && $expirationDate === null && $this->shareApiLinkDefaultExpireDate()) {
387
+            $expirationDate = new \DateTime();
388
+            $expirationDate->setTime(0,0,0);
389
+            $expirationDate->add(new \DateInterval('P'.$this->shareApiLinkDefaultExpireDays().'D'));
390
+        }
391
+
392
+        // If we enforce the expiration date check that is does not exceed
393
+        if ($this->shareApiLinkDefaultExpireDateEnforced()) {
394
+            if ($expirationDate === null) {
395
+                throw new \InvalidArgumentException('Expiration date is enforced');
396
+            }
397
+
398
+            $date = new \DateTime();
399
+            $date->setTime(0, 0, 0);
400
+            $date->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D'));
401
+            if ($date < $expirationDate) {
402
+                $message = $this->l->t('Can’t set expiration date more than %s days in the future', [$this->shareApiLinkDefaultExpireDays()]);
403
+                throw new GenericShareException($message, $message, 404);
404
+            }
405
+        }
406
+
407
+        $accepted = true;
408
+        $message = '';
409
+        \OCP\Util::emitHook('\OC\Share', 'verifyExpirationDate', [
410
+            'expirationDate' => &$expirationDate,
411
+            'accepted' => &$accepted,
412
+            'message' => &$message,
413
+            'passwordSet' => $share->getPassword() !== null,
414
+        ]);
415
+
416
+        if (!$accepted) {
417
+            throw new \Exception($message);
418
+        }
419
+
420
+        $share->setExpirationDate($expirationDate);
421
+
422
+        return $share;
423
+    }
424
+
425
+    /**
426
+     * Check for pre share requirements for user shares
427
+     *
428
+     * @param \OCP\Share\IShare $share
429
+     * @throws \Exception
430
+     */
431
+    protected function userCreateChecks(\OCP\Share\IShare $share) {
432
+        // Check if we can share with group members only
433
+        if ($this->shareWithGroupMembersOnly()) {
434
+            $sharedBy = $this->userManager->get($share->getSharedBy());
435
+            $sharedWith = $this->userManager->get($share->getSharedWith());
436
+            // Verify we can share with this user
437
+            $groups = array_intersect(
438
+                    $this->groupManager->getUserGroupIds($sharedBy),
439
+                    $this->groupManager->getUserGroupIds($sharedWith)
440
+            );
441
+            if (empty($groups)) {
442
+                throw new \Exception('Sharing is only allowed with group members');
443
+            }
444
+        }
445
+
446
+        /*
447 447
 		 * TODO: Could be costly, fix
448 448
 		 *
449 449
 		 * Also this is not what we want in the future.. then we want to squash identical shares.
450 450
 		 */
451
-		$provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_USER);
452
-		$existingShares = $provider->getSharesByPath($share->getNode());
453
-		foreach($existingShares as $existingShare) {
454
-			// Ignore if it is the same share
455
-			try {
456
-				if ($existingShare->getFullId() === $share->getFullId()) {
457
-					continue;
458
-				}
459
-			} catch (\UnexpectedValueException $e) {
460
-				//Shares are not identical
461
-			}
462
-
463
-			// Identical share already existst
464
-			if ($existingShare->getSharedWith() === $share->getSharedWith() && $existingShare->getShareType() === $share->getShareType()) {
465
-				throw new \Exception('Path is already shared with this user');
466
-			}
467
-
468
-			// The share is already shared with this user via a group share
469
-			if ($existingShare->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
470
-				$group = $this->groupManager->get($existingShare->getSharedWith());
471
-				if (!is_null($group)) {
472
-					$user = $this->userManager->get($share->getSharedWith());
473
-
474
-					if ($group->inGroup($user) && $existingShare->getShareOwner() !== $share->getShareOwner()) {
475
-						throw new \Exception('Path is already shared with this user');
476
-					}
477
-				}
478
-			}
479
-		}
480
-	}
481
-
482
-	/**
483
-	 * Check for pre share requirements for group shares
484
-	 *
485
-	 * @param \OCP\Share\IShare $share
486
-	 * @throws \Exception
487
-	 */
488
-	protected function groupCreateChecks(\OCP\Share\IShare $share) {
489
-		// Verify group shares are allowed
490
-		if (!$this->allowGroupSharing()) {
491
-			throw new \Exception('Group sharing is now allowed');
492
-		}
493
-
494
-		// Verify if the user can share with this group
495
-		if ($this->shareWithGroupMembersOnly()) {
496
-			$sharedBy = $this->userManager->get($share->getSharedBy());
497
-			$sharedWith = $this->groupManager->get($share->getSharedWith());
498
-			if (is_null($sharedWith) || !$sharedWith->inGroup($sharedBy)) {
499
-				throw new \Exception('Sharing is only allowed within your own groups');
500
-			}
501
-		}
502
-
503
-		/*
451
+        $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_USER);
452
+        $existingShares = $provider->getSharesByPath($share->getNode());
453
+        foreach($existingShares as $existingShare) {
454
+            // Ignore if it is the same share
455
+            try {
456
+                if ($existingShare->getFullId() === $share->getFullId()) {
457
+                    continue;
458
+                }
459
+            } catch (\UnexpectedValueException $e) {
460
+                //Shares are not identical
461
+            }
462
+
463
+            // Identical share already existst
464
+            if ($existingShare->getSharedWith() === $share->getSharedWith() && $existingShare->getShareType() === $share->getShareType()) {
465
+                throw new \Exception('Path is already shared with this user');
466
+            }
467
+
468
+            // The share is already shared with this user via a group share
469
+            if ($existingShare->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
470
+                $group = $this->groupManager->get($existingShare->getSharedWith());
471
+                if (!is_null($group)) {
472
+                    $user = $this->userManager->get($share->getSharedWith());
473
+
474
+                    if ($group->inGroup($user) && $existingShare->getShareOwner() !== $share->getShareOwner()) {
475
+                        throw new \Exception('Path is already shared with this user');
476
+                    }
477
+                }
478
+            }
479
+        }
480
+    }
481
+
482
+    /**
483
+     * Check for pre share requirements for group shares
484
+     *
485
+     * @param \OCP\Share\IShare $share
486
+     * @throws \Exception
487
+     */
488
+    protected function groupCreateChecks(\OCP\Share\IShare $share) {
489
+        // Verify group shares are allowed
490
+        if (!$this->allowGroupSharing()) {
491
+            throw new \Exception('Group sharing is now allowed');
492
+        }
493
+
494
+        // Verify if the user can share with this group
495
+        if ($this->shareWithGroupMembersOnly()) {
496
+            $sharedBy = $this->userManager->get($share->getSharedBy());
497
+            $sharedWith = $this->groupManager->get($share->getSharedWith());
498
+            if (is_null($sharedWith) || !$sharedWith->inGroup($sharedBy)) {
499
+                throw new \Exception('Sharing is only allowed within your own groups');
500
+            }
501
+        }
502
+
503
+        /*
504 504
 		 * TODO: Could be costly, fix
505 505
 		 *
506 506
 		 * Also this is not what we want in the future.. then we want to squash identical shares.
507 507
 		 */
508
-		$provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP);
509
-		$existingShares = $provider->getSharesByPath($share->getNode());
510
-		foreach($existingShares as $existingShare) {
511
-			try {
512
-				if ($existingShare->getFullId() === $share->getFullId()) {
513
-					continue;
514
-				}
515
-			} catch (\UnexpectedValueException $e) {
516
-				//It is a new share so just continue
517
-			}
518
-
519
-			if ($existingShare->getSharedWith() === $share->getSharedWith() && $existingShare->getShareType() === $share->getShareType()) {
520
-				throw new \Exception('Path is already shared with this group');
521
-			}
522
-		}
523
-	}
524
-
525
-	/**
526
-	 * Check for pre share requirements for link shares
527
-	 *
528
-	 * @param \OCP\Share\IShare $share
529
-	 * @throws \Exception
530
-	 */
531
-	protected function linkCreateChecks(\OCP\Share\IShare $share) {
532
-		// Are link shares allowed?
533
-		if (!$this->shareApiAllowLinks()) {
534
-			throw new \Exception('Link sharing is not allowed');
535
-		}
536
-
537
-		// Link shares by definition can't have share permissions
538
-		if ($share->getPermissions() & \OCP\Constants::PERMISSION_SHARE) {
539
-			throw new \InvalidArgumentException('Link shares can’t have reshare permissions');
540
-		}
541
-
542
-		// Check if public upload is allowed
543
-		if (!$this->shareApiLinkAllowPublicUpload() &&
544
-			($share->getPermissions() & (\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE))) {
545
-			throw new \InvalidArgumentException('Public upload is not allowed');
546
-		}
547
-	}
548
-
549
-	/**
550
-	 * To make sure we don't get invisible link shares we set the parent
551
-	 * of a link if it is a reshare. This is a quick word around
552
-	 * until we can properly display multiple link shares in the UI
553
-	 *
554
-	 * See: https://github.com/owncloud/core/issues/22295
555
-	 *
556
-	 * FIXME: Remove once multiple link shares can be properly displayed
557
-	 *
558
-	 * @param \OCP\Share\IShare $share
559
-	 */
560
-	protected function setLinkParent(\OCP\Share\IShare $share) {
561
-
562
-		// No sense in checking if the method is not there.
563
-		if (method_exists($share, 'setParent')) {
564
-			$storage = $share->getNode()->getStorage();
565
-			if ($storage->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) {
566
-				/** @var \OCA\Files_Sharing\SharedStorage $storage */
567
-				$share->setParent($storage->getShareId());
568
-			}
569
-		}
570
-	}
571
-
572
-	/**
573
-	 * @param File|Folder $path
574
-	 */
575
-	protected function pathCreateChecks($path) {
576
-		// Make sure that we do not share a path that contains a shared mountpoint
577
-		if ($path instanceof \OCP\Files\Folder) {
578
-			$mounts = $this->mountManager->findIn($path->getPath());
579
-			foreach($mounts as $mount) {
580
-				if ($mount->getStorage()->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) {
581
-					throw new \InvalidArgumentException('Path contains files shared with you');
582
-				}
583
-			}
584
-		}
585
-	}
586
-
587
-	/**
588
-	 * Check if the user that is sharing can actually share
589
-	 *
590
-	 * @param \OCP\Share\IShare $share
591
-	 * @throws \Exception
592
-	 */
593
-	protected function canShare(\OCP\Share\IShare $share) {
594
-		if (!$this->shareApiEnabled()) {
595
-			throw new \Exception('Sharing is disabled');
596
-		}
597
-
598
-		if ($this->sharingDisabledForUser($share->getSharedBy())) {
599
-			throw new \Exception('Sharing is disabled for you');
600
-		}
601
-	}
602
-
603
-	/**
604
-	 * Share a path
605
-	 *
606
-	 * @param \OCP\Share\IShare $share
607
-	 * @return Share The share object
608
-	 * @throws \Exception
609
-	 *
610
-	 * TODO: handle link share permissions or check them
611
-	 */
612
-	public function createShare(\OCP\Share\IShare $share) {
613
-		$this->canShare($share);
614
-
615
-		$this->generalCreateChecks($share);
616
-
617
-		// Verify if there are any issues with the path
618
-		$this->pathCreateChecks($share->getNode());
619
-
620
-		/*
508
+        $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP);
509
+        $existingShares = $provider->getSharesByPath($share->getNode());
510
+        foreach($existingShares as $existingShare) {
511
+            try {
512
+                if ($existingShare->getFullId() === $share->getFullId()) {
513
+                    continue;
514
+                }
515
+            } catch (\UnexpectedValueException $e) {
516
+                //It is a new share so just continue
517
+            }
518
+
519
+            if ($existingShare->getSharedWith() === $share->getSharedWith() && $existingShare->getShareType() === $share->getShareType()) {
520
+                throw new \Exception('Path is already shared with this group');
521
+            }
522
+        }
523
+    }
524
+
525
+    /**
526
+     * Check for pre share requirements for link shares
527
+     *
528
+     * @param \OCP\Share\IShare $share
529
+     * @throws \Exception
530
+     */
531
+    protected function linkCreateChecks(\OCP\Share\IShare $share) {
532
+        // Are link shares allowed?
533
+        if (!$this->shareApiAllowLinks()) {
534
+            throw new \Exception('Link sharing is not allowed');
535
+        }
536
+
537
+        // Link shares by definition can't have share permissions
538
+        if ($share->getPermissions() & \OCP\Constants::PERMISSION_SHARE) {
539
+            throw new \InvalidArgumentException('Link shares can’t have reshare permissions');
540
+        }
541
+
542
+        // Check if public upload is allowed
543
+        if (!$this->shareApiLinkAllowPublicUpload() &&
544
+            ($share->getPermissions() & (\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE))) {
545
+            throw new \InvalidArgumentException('Public upload is not allowed');
546
+        }
547
+    }
548
+
549
+    /**
550
+     * To make sure we don't get invisible link shares we set the parent
551
+     * of a link if it is a reshare. This is a quick word around
552
+     * until we can properly display multiple link shares in the UI
553
+     *
554
+     * See: https://github.com/owncloud/core/issues/22295
555
+     *
556
+     * FIXME: Remove once multiple link shares can be properly displayed
557
+     *
558
+     * @param \OCP\Share\IShare $share
559
+     */
560
+    protected function setLinkParent(\OCP\Share\IShare $share) {
561
+
562
+        // No sense in checking if the method is not there.
563
+        if (method_exists($share, 'setParent')) {
564
+            $storage = $share->getNode()->getStorage();
565
+            if ($storage->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) {
566
+                /** @var \OCA\Files_Sharing\SharedStorage $storage */
567
+                $share->setParent($storage->getShareId());
568
+            }
569
+        }
570
+    }
571
+
572
+    /**
573
+     * @param File|Folder $path
574
+     */
575
+    protected function pathCreateChecks($path) {
576
+        // Make sure that we do not share a path that contains a shared mountpoint
577
+        if ($path instanceof \OCP\Files\Folder) {
578
+            $mounts = $this->mountManager->findIn($path->getPath());
579
+            foreach($mounts as $mount) {
580
+                if ($mount->getStorage()->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) {
581
+                    throw new \InvalidArgumentException('Path contains files shared with you');
582
+                }
583
+            }
584
+        }
585
+    }
586
+
587
+    /**
588
+     * Check if the user that is sharing can actually share
589
+     *
590
+     * @param \OCP\Share\IShare $share
591
+     * @throws \Exception
592
+     */
593
+    protected function canShare(\OCP\Share\IShare $share) {
594
+        if (!$this->shareApiEnabled()) {
595
+            throw new \Exception('Sharing is disabled');
596
+        }
597
+
598
+        if ($this->sharingDisabledForUser($share->getSharedBy())) {
599
+            throw new \Exception('Sharing is disabled for you');
600
+        }
601
+    }
602
+
603
+    /**
604
+     * Share a path
605
+     *
606
+     * @param \OCP\Share\IShare $share
607
+     * @return Share The share object
608
+     * @throws \Exception
609
+     *
610
+     * TODO: handle link share permissions or check them
611
+     */
612
+    public function createShare(\OCP\Share\IShare $share) {
613
+        $this->canShare($share);
614
+
615
+        $this->generalCreateChecks($share);
616
+
617
+        // Verify if there are any issues with the path
618
+        $this->pathCreateChecks($share->getNode());
619
+
620
+        /*
621 621
 		 * On creation of a share the owner is always the owner of the path
622 622
 		 * Except for mounted federated shares.
623 623
 		 */
624
-		$storage = $share->getNode()->getStorage();
625
-		if ($storage->instanceOfStorage('OCA\Files_Sharing\External\Storage')) {
626
-			$parent = $share->getNode()->getParent();
627
-			while($parent->getStorage()->instanceOfStorage('OCA\Files_Sharing\External\Storage')) {
628
-				$parent = $parent->getParent();
629
-			}
630
-			$share->setShareOwner($parent->getOwner()->getUID());
631
-		} else {
632
-			$share->setShareOwner($share->getNode()->getOwner()->getUID());
633
-		}
634
-
635
-		//Verify share type
636
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
637
-			$this->userCreateChecks($share);
638
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
639
-			$this->groupCreateChecks($share);
640
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
641
-			$this->linkCreateChecks($share);
642
-			$this->setLinkParent($share);
643
-
644
-			/*
624
+        $storage = $share->getNode()->getStorage();
625
+        if ($storage->instanceOfStorage('OCA\Files_Sharing\External\Storage')) {
626
+            $parent = $share->getNode()->getParent();
627
+            while($parent->getStorage()->instanceOfStorage('OCA\Files_Sharing\External\Storage')) {
628
+                $parent = $parent->getParent();
629
+            }
630
+            $share->setShareOwner($parent->getOwner()->getUID());
631
+        } else {
632
+            $share->setShareOwner($share->getNode()->getOwner()->getUID());
633
+        }
634
+
635
+        //Verify share type
636
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
637
+            $this->userCreateChecks($share);
638
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
639
+            $this->groupCreateChecks($share);
640
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
641
+            $this->linkCreateChecks($share);
642
+            $this->setLinkParent($share);
643
+
644
+            /*
645 645
 			 * For now ignore a set token.
646 646
 			 */
647
-			$share->setToken(
648
-				$this->secureRandom->generate(
649
-					\OC\Share\Constants::TOKEN_LENGTH,
650
-					\OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE
651
-				)
652
-			);
653
-
654
-			//Verify the expiration date
655
-			$this->validateExpirationDate($share);
656
-
657
-			//Verify the password
658
-			$this->verifyPassword($share->getPassword());
659
-
660
-			// If a password is set. Hash it!
661
-			if ($share->getPassword() !== null) {
662
-				$share->setPassword($this->hasher->hash($share->getPassword()));
663
-			}
664
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
665
-			$share->setToken(
666
-				$this->secureRandom->generate(
667
-					\OC\Share\Constants::TOKEN_LENGTH,
668
-					\OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE
669
-				)
670
-			);
671
-		}
672
-
673
-		// Cannot share with the owner
674
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
675
-			$share->getSharedWith() === $share->getShareOwner()) {
676
-			throw new \InvalidArgumentException('Can’t share with the share owner');
677
-		}
678
-
679
-		// Generate the target
680
-		$target = $this->config->getSystemValue('share_folder', '/') .'/'. $share->getNode()->getName();
681
-		$target = \OC\Files\Filesystem::normalizePath($target);
682
-		$share->setTarget($target);
683
-
684
-		// Pre share event
685
-		$event = new GenericEvent($share);
686
-		$this->eventDispatcher->dispatch('OCP\Share::preShare', $event);
687
-		if ($event->isPropagationStopped() && $event->hasArgument('error')) {
688
-			throw new \Exception($event->getArgument('error'));
689
-		}
690
-
691
-		$oldShare = $share;
692
-		$provider = $this->factory->getProviderForType($share->getShareType());
693
-		$share = $provider->create($share);
694
-		//reuse the node we already have
695
-		$share->setNode($oldShare->getNode());
696
-
697
-		// Post share event
698
-		$event = new GenericEvent($share);
699
-		$this->eventDispatcher->dispatch('OCP\Share::postShare', $event);
700
-
701
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
702
-			$mailSend = $share->getMailSend();
703
-			if($mailSend === true) {
704
-				$user = $this->userManager->get($share->getSharedWith());
705
-				if ($user !== null) {
706
-					$emailAddress = $user->getEMailAddress();
707
-					if ($emailAddress !== null && $emailAddress !== '') {
708
-						$userLang = $this->config->getUserValue($share->getSharedWith(), 'core', 'lang', null);
709
-						$l = $this->l10nFactory->get('lib', $userLang);
710
-						$this->sendMailNotification(
711
-							$l,
712
-							$share->getNode()->getName(),
713
-							$this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $share->getNode()->getId()]),
714
-							$share->getSharedBy(),
715
-							$emailAddress,
716
-							$share->getExpirationDate()
717
-						);
718
-						$this->logger->debug('Sent share notification to ' . $emailAddress . ' for share with ID ' . $share->getId(), ['app' => 'share']);
719
-					} else {
720
-						$this->logger->debug('Share notification not sent to ' . $share->getSharedWith() . ' because email address is not set.', ['app' => 'share']);
721
-					}
722
-				} else {
723
-					$this->logger->debug('Share notification not sent to ' . $share->getSharedWith() . ' because user could not be found.', ['app' => 'share']);
724
-				}
725
-			} else {
726
-				$this->logger->debug('Share notification not sent because mailsend is false.', ['app' => 'share']);
727
-			}
728
-		}
729
-
730
-		return $share;
731
-	}
732
-
733
-	/**
734
-	 * Send mail notifications
735
-	 *
736
-	 * This method will catch and log mail transmission errors
737
-	 *
738
-	 * @param IL10N $l Language of the recipient
739
-	 * @param string $filename file/folder name
740
-	 * @param string $link link to the file/folder
741
-	 * @param string $initiator user ID of share sender
742
-	 * @param string $shareWith email address of share receiver
743
-	 * @param \DateTime|null $expiration
744
-	 */
745
-	protected function sendMailNotification(IL10N $l,
746
-											$filename,
747
-											$link,
748
-											$initiator,
749
-											$shareWith,
750
-											\DateTime $expiration = null) {
751
-		$initiatorUser = $this->userManager->get($initiator);
752
-		$initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
753
-
754
-		$message = $this->mailer->createMessage();
755
-
756
-		$emailTemplate = $this->mailer->createEMailTemplate('files_sharing.RecipientNotification', [
757
-			'filename' => $filename,
758
-			'link' => $link,
759
-			'initiator' => $initiatorDisplayName,
760
-			'expiration' => $expiration,
761
-			'shareWith' => $shareWith,
762
-		]);
763
-
764
-		$emailTemplate->setSubject($l->t('%1$s shared »%2$s« with you', array($initiatorDisplayName, $filename)));
765
-		$emailTemplate->addHeader();
766
-		$emailTemplate->addHeading($l->t('%1$s shared »%2$s« with you', [$initiatorDisplayName, $filename]), false);
767
-		$text = $l->t('%1$s shared »%2$s« with you.', [$initiatorDisplayName, $filename]);
768
-
769
-		$emailTemplate->addBodyText(
770
-			htmlspecialchars($text . ' ' . $l->t('Click the button below to open it.')),
771
-			$text
772
-		);
773
-		$emailTemplate->addBodyButton(
774
-			$l->t('Open »%s«', [$filename]),
775
-			$link
776
-		);
777
-
778
-		$message->setTo([$shareWith]);
779
-
780
-		// The "From" contains the sharers name
781
-		$instanceName = $this->defaults->getName();
782
-		$senderName = $l->t(
783
-			'%1$s via %2$s',
784
-			[
785
-				$initiatorDisplayName,
786
-				$instanceName
787
-			]
788
-		);
789
-		$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
790
-
791
-		// The "Reply-To" is set to the sharer if an mail address is configured
792
-		// also the default footer contains a "Do not reply" which needs to be adjusted.
793
-		$initiatorEmail = $initiatorUser->getEMailAddress();
794
-		if($initiatorEmail !== null) {
795
-			$message->setReplyTo([$initiatorEmail => $initiatorDisplayName]);
796
-			$emailTemplate->addFooter($instanceName . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : ''));
797
-		} else {
798
-			$emailTemplate->addFooter();
799
-		}
800
-
801
-		$message->useTemplate($emailTemplate);
802
-		try {
803
-			$failedRecipients = $this->mailer->send($message);
804
-			if(!empty($failedRecipients)) {
805
-				$this->logger->error('Share notification mail could not be sent to: ' . implode(', ', $failedRecipients));
806
-				return;
807
-			}
808
-		} catch (\Exception $e) {
809
-			$this->logger->logException($e, ['message' => 'Share notification mail could not be sent']);
810
-		}
811
-	}
812
-
813
-	/**
814
-	 * Update a share
815
-	 *
816
-	 * @param \OCP\Share\IShare $share
817
-	 * @return \OCP\Share\IShare The share object
818
-	 * @throws \InvalidArgumentException
819
-	 */
820
-	public function updateShare(\OCP\Share\IShare $share) {
821
-		$expirationDateUpdated = false;
822
-
823
-		$this->canShare($share);
824
-
825
-		try {
826
-			$originalShare = $this->getShareById($share->getFullId());
827
-		} catch (\UnexpectedValueException $e) {
828
-			throw new \InvalidArgumentException('Share does not have a full id');
829
-		}
830
-
831
-		// We can't change the share type!
832
-		if ($share->getShareType() !== $originalShare->getShareType()) {
833
-			throw new \InvalidArgumentException('Can’t change share type');
834
-		}
835
-
836
-		// We can only change the recipient on user shares
837
-		if ($share->getSharedWith() !== $originalShare->getSharedWith() &&
838
-		    $share->getShareType() !== \OCP\Share::SHARE_TYPE_USER) {
839
-			throw new \InvalidArgumentException('Can only update recipient on user shares');
840
-		}
841
-
842
-		// Cannot share with the owner
843
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
844
-			$share->getSharedWith() === $share->getShareOwner()) {
845
-			throw new \InvalidArgumentException('Can’t share with the share owner');
846
-		}
847
-
848
-		$this->generalCreateChecks($share);
849
-
850
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
851
-			$this->userCreateChecks($share);
852
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
853
-			$this->groupCreateChecks($share);
854
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
855
-			$this->linkCreateChecks($share);
856
-
857
-			$this->updateSharePasswordIfNeeded($share, $originalShare);
858
-
859
-			if ($share->getExpirationDate() != $originalShare->getExpirationDate()) {
860
-				//Verify the expiration date
861
-				$this->validateExpirationDate($share);
862
-				$expirationDateUpdated = true;
863
-			}
864
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
865
-			// The new password is not set again if it is the same as the old
866
-			// one, unless when switching from sending by Talk to sending by
867
-			// mail.
868
-			$plainTextPassword = $share->getPassword();
869
-			if (!empty($plainTextPassword) && !$this->updateSharePasswordIfNeeded($share, $originalShare) &&
870
-					!($originalShare->getSendPasswordByTalk() && !$share->getSendPasswordByTalk())) {
871
-				$plainTextPassword = null;
872
-			}
873
-			if (empty($plainTextPassword) && !$originalShare->getSendPasswordByTalk() && $share->getSendPasswordByTalk()) {
874
-				// If the same password was already sent by mail the recipient
875
-				// would already have access to the share without having to call
876
-				// the sharer to verify her identity
877
-				throw new \InvalidArgumentException('Can’t enable sending the password by Talk without setting a new password');
878
-			}
879
-		}
880
-
881
-		$this->pathCreateChecks($share->getNode());
882
-
883
-		// Now update the share!
884
-		$provider = $this->factory->getProviderForType($share->getShareType());
885
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
886
-			$share = $provider->update($share, $plainTextPassword);
887
-		} else {
888
-			$share = $provider->update($share);
889
-		}
890
-
891
-		if ($expirationDateUpdated === true) {
892
-			\OC_Hook::emit(Share::class, 'post_set_expiration_date', [
893
-				'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder',
894
-				'itemSource' => $share->getNode()->getId(),
895
-				'date' => $share->getExpirationDate(),
896
-				'uidOwner' => $share->getSharedBy(),
897
-			]);
898
-		}
899
-
900
-		if ($share->getPassword() !== $originalShare->getPassword()) {
901
-			\OC_Hook::emit(Share::class, 'post_update_password', [
902
-				'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder',
903
-				'itemSource' => $share->getNode()->getId(),
904
-				'uidOwner' => $share->getSharedBy(),
905
-				'token' => $share->getToken(),
906
-				'disabled' => is_null($share->getPassword()),
907
-			]);
908
-		}
909
-
910
-		if ($share->getPermissions() !== $originalShare->getPermissions()) {
911
-			if ($this->userManager->userExists($share->getShareOwner())) {
912
-				$userFolder = $this->rootFolder->getUserFolder($share->getShareOwner());
913
-			} else {
914
-				$userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
915
-			}
916
-			\OC_Hook::emit(Share::class, 'post_update_permissions', array(
917
-				'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder',
918
-				'itemSource' => $share->getNode()->getId(),
919
-				'shareType' => $share->getShareType(),
920
-				'shareWith' => $share->getSharedWith(),
921
-				'uidOwner' => $share->getSharedBy(),
922
-				'permissions' => $share->getPermissions(),
923
-				'path' => $userFolder->getRelativePath($share->getNode()->getPath()),
924
-			));
925
-		}
926
-
927
-		return $share;
928
-	}
929
-
930
-	/**
931
-	 * Updates the password of the given share if it is not the same as the
932
-	 * password of the original share.
933
-	 *
934
-	 * @param \OCP\Share\IShare $share the share to update its password.
935
-	 * @param \OCP\Share\IShare $originalShare the original share to compare its
936
-	 *        password with.
937
-	 * @return boolean whether the password was updated or not.
938
-	 */
939
-	private function updateSharePasswordIfNeeded(\OCP\Share\IShare $share, \OCP\Share\IShare $originalShare) {
940
-		// Password updated.
941
-		if ($share->getPassword() !== $originalShare->getPassword()) {
942
-			//Verify the password
943
-			$this->verifyPassword($share->getPassword());
944
-
945
-			// If a password is set. Hash it!
946
-			if ($share->getPassword() !== null) {
947
-				$share->setPassword($this->hasher->hash($share->getPassword()));
948
-
949
-				return true;
950
-			}
951
-		}
952
-
953
-		return false;
954
-	}
955
-
956
-	/**
957
-	 * Delete all the children of this share
958
-	 * FIXME: remove once https://github.com/owncloud/core/pull/21660 is in
959
-	 *
960
-	 * @param \OCP\Share\IShare $share
961
-	 * @return \OCP\Share\IShare[] List of deleted shares
962
-	 */
963
-	protected function deleteChildren(\OCP\Share\IShare $share) {
964
-		$deletedShares = [];
965
-
966
-		$provider = $this->factory->getProviderForType($share->getShareType());
967
-
968
-		foreach ($provider->getChildren($share) as $child) {
969
-			$deletedChildren = $this->deleteChildren($child);
970
-			$deletedShares = array_merge($deletedShares, $deletedChildren);
971
-
972
-			$provider->delete($child);
973
-			$deletedShares[] = $child;
974
-		}
975
-
976
-		return $deletedShares;
977
-	}
978
-
979
-	/**
980
-	 * Delete a share
981
-	 *
982
-	 * @param \OCP\Share\IShare $share
983
-	 * @throws ShareNotFound
984
-	 * @throws \InvalidArgumentException
985
-	 */
986
-	public function deleteShare(\OCP\Share\IShare $share) {
987
-
988
-		try {
989
-			$share->getFullId();
990
-		} catch (\UnexpectedValueException $e) {
991
-			throw new \InvalidArgumentException('Share does not have a full id');
992
-		}
993
-
994
-		$event = new GenericEvent($share);
995
-		$this->eventDispatcher->dispatch('OCP\Share::preUnshare', $event);
996
-
997
-		// Get all children and delete them as well
998
-		$deletedShares = $this->deleteChildren($share);
999
-
1000
-		// Do the actual delete
1001
-		$provider = $this->factory->getProviderForType($share->getShareType());
1002
-		$provider->delete($share);
1003
-
1004
-		// All the deleted shares caused by this delete
1005
-		$deletedShares[] = $share;
1006
-
1007
-		// Emit post hook
1008
-		$event->setArgument('deletedShares', $deletedShares);
1009
-		$this->eventDispatcher->dispatch('OCP\Share::postUnshare', $event);
1010
-	}
1011
-
1012
-
1013
-	/**
1014
-	 * Unshare a file as the recipient.
1015
-	 * This can be different from a regular delete for example when one of
1016
-	 * the users in a groups deletes that share. But the provider should
1017
-	 * handle this.
1018
-	 *
1019
-	 * @param \OCP\Share\IShare $share
1020
-	 * @param string $recipientId
1021
-	 */
1022
-	public function deleteFromSelf(\OCP\Share\IShare $share, $recipientId) {
1023
-		list($providerId, ) = $this->splitFullId($share->getFullId());
1024
-		$provider = $this->factory->getProvider($providerId);
1025
-
1026
-		$provider->deleteFromSelf($share, $recipientId);
1027
-		$event = new GenericEvent($share);
1028
-		$this->eventDispatcher->dispatch('OCP\Share::postUnshareFromSelf', $event);
1029
-	}
1030
-
1031
-	public function restoreShare(IShare $share, string $recipientId): IShare {
1032
-		list($providerId, ) = $this->splitFullId($share->getFullId());
1033
-		$provider = $this->factory->getProvider($providerId);
1034
-
1035
-		return $provider->restore($share, $recipientId);
1036
-	}
1037
-
1038
-	/**
1039
-	 * @inheritdoc
1040
-	 */
1041
-	public function moveShare(\OCP\Share\IShare $share, $recipientId) {
1042
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
1043
-			throw new \InvalidArgumentException('Can’t change target of link share');
1044
-		}
1045
-
1046
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && $share->getSharedWith() !== $recipientId) {
1047
-			throw new \InvalidArgumentException('Invalid recipient');
1048
-		}
1049
-
1050
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
1051
-			$sharedWith = $this->groupManager->get($share->getSharedWith());
1052
-			if (is_null($sharedWith)) {
1053
-				throw new \InvalidArgumentException('Group "' . $share->getSharedWith() . '" does not exist');
1054
-			}
1055
-			$recipient = $this->userManager->get($recipientId);
1056
-			if (!$sharedWith->inGroup($recipient)) {
1057
-				throw new \InvalidArgumentException('Invalid recipient');
1058
-			}
1059
-		}
1060
-
1061
-		list($providerId, ) = $this->splitFullId($share->getFullId());
1062
-		$provider = $this->factory->getProvider($providerId);
1063
-
1064
-		$provider->move($share, $recipientId);
1065
-	}
1066
-
1067
-	public function getSharesInFolder($userId, Folder $node, $reshares = false) {
1068
-		$providers = $this->factory->getAllProviders();
1069
-
1070
-		return array_reduce($providers, function($shares, IShareProvider $provider) use ($userId, $node, $reshares) {
1071
-			$newShares = $provider->getSharesInFolder($userId, $node, $reshares);
1072
-			foreach ($newShares as $fid => $data) {
1073
-				if (!isset($shares[$fid])) {
1074
-					$shares[$fid] = [];
1075
-				}
1076
-
1077
-				$shares[$fid] = array_merge($shares[$fid], $data);
1078
-			}
1079
-			return $shares;
1080
-		}, []);
1081
-	}
1082
-
1083
-	/**
1084
-	 * @inheritdoc
1085
-	 */
1086
-	public function getSharesBy($userId, $shareType, $path = null, $reshares = false, $limit = 50, $offset = 0) {
1087
-		if ($path !== null &&
1088
-				!($path instanceof \OCP\Files\File) &&
1089
-				!($path instanceof \OCP\Files\Folder)) {
1090
-			throw new \InvalidArgumentException('invalid path');
1091
-		}
1092
-
1093
-		try {
1094
-			$provider = $this->factory->getProviderForType($shareType);
1095
-		} catch (ProviderException $e) {
1096
-			return [];
1097
-		}
1098
-
1099
-		$shares = $provider->getSharesBy($userId, $shareType, $path, $reshares, $limit, $offset);
1100
-
1101
-		/*
647
+            $share->setToken(
648
+                $this->secureRandom->generate(
649
+                    \OC\Share\Constants::TOKEN_LENGTH,
650
+                    \OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE
651
+                )
652
+            );
653
+
654
+            //Verify the expiration date
655
+            $this->validateExpirationDate($share);
656
+
657
+            //Verify the password
658
+            $this->verifyPassword($share->getPassword());
659
+
660
+            // If a password is set. Hash it!
661
+            if ($share->getPassword() !== null) {
662
+                $share->setPassword($this->hasher->hash($share->getPassword()));
663
+            }
664
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
665
+            $share->setToken(
666
+                $this->secureRandom->generate(
667
+                    \OC\Share\Constants::TOKEN_LENGTH,
668
+                    \OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE
669
+                )
670
+            );
671
+        }
672
+
673
+        // Cannot share with the owner
674
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
675
+            $share->getSharedWith() === $share->getShareOwner()) {
676
+            throw new \InvalidArgumentException('Can’t share with the share owner');
677
+        }
678
+
679
+        // Generate the target
680
+        $target = $this->config->getSystemValue('share_folder', '/') .'/'. $share->getNode()->getName();
681
+        $target = \OC\Files\Filesystem::normalizePath($target);
682
+        $share->setTarget($target);
683
+
684
+        // Pre share event
685
+        $event = new GenericEvent($share);
686
+        $this->eventDispatcher->dispatch('OCP\Share::preShare', $event);
687
+        if ($event->isPropagationStopped() && $event->hasArgument('error')) {
688
+            throw new \Exception($event->getArgument('error'));
689
+        }
690
+
691
+        $oldShare = $share;
692
+        $provider = $this->factory->getProviderForType($share->getShareType());
693
+        $share = $provider->create($share);
694
+        //reuse the node we already have
695
+        $share->setNode($oldShare->getNode());
696
+
697
+        // Post share event
698
+        $event = new GenericEvent($share);
699
+        $this->eventDispatcher->dispatch('OCP\Share::postShare', $event);
700
+
701
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
702
+            $mailSend = $share->getMailSend();
703
+            if($mailSend === true) {
704
+                $user = $this->userManager->get($share->getSharedWith());
705
+                if ($user !== null) {
706
+                    $emailAddress = $user->getEMailAddress();
707
+                    if ($emailAddress !== null && $emailAddress !== '') {
708
+                        $userLang = $this->config->getUserValue($share->getSharedWith(), 'core', 'lang', null);
709
+                        $l = $this->l10nFactory->get('lib', $userLang);
710
+                        $this->sendMailNotification(
711
+                            $l,
712
+                            $share->getNode()->getName(),
713
+                            $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $share->getNode()->getId()]),
714
+                            $share->getSharedBy(),
715
+                            $emailAddress,
716
+                            $share->getExpirationDate()
717
+                        );
718
+                        $this->logger->debug('Sent share notification to ' . $emailAddress . ' for share with ID ' . $share->getId(), ['app' => 'share']);
719
+                    } else {
720
+                        $this->logger->debug('Share notification not sent to ' . $share->getSharedWith() . ' because email address is not set.', ['app' => 'share']);
721
+                    }
722
+                } else {
723
+                    $this->logger->debug('Share notification not sent to ' . $share->getSharedWith() . ' because user could not be found.', ['app' => 'share']);
724
+                }
725
+            } else {
726
+                $this->logger->debug('Share notification not sent because mailsend is false.', ['app' => 'share']);
727
+            }
728
+        }
729
+
730
+        return $share;
731
+    }
732
+
733
+    /**
734
+     * Send mail notifications
735
+     *
736
+     * This method will catch and log mail transmission errors
737
+     *
738
+     * @param IL10N $l Language of the recipient
739
+     * @param string $filename file/folder name
740
+     * @param string $link link to the file/folder
741
+     * @param string $initiator user ID of share sender
742
+     * @param string $shareWith email address of share receiver
743
+     * @param \DateTime|null $expiration
744
+     */
745
+    protected function sendMailNotification(IL10N $l,
746
+                                            $filename,
747
+                                            $link,
748
+                                            $initiator,
749
+                                            $shareWith,
750
+                                            \DateTime $expiration = null) {
751
+        $initiatorUser = $this->userManager->get($initiator);
752
+        $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
753
+
754
+        $message = $this->mailer->createMessage();
755
+
756
+        $emailTemplate = $this->mailer->createEMailTemplate('files_sharing.RecipientNotification', [
757
+            'filename' => $filename,
758
+            'link' => $link,
759
+            'initiator' => $initiatorDisplayName,
760
+            'expiration' => $expiration,
761
+            'shareWith' => $shareWith,
762
+        ]);
763
+
764
+        $emailTemplate->setSubject($l->t('%1$s shared »%2$s« with you', array($initiatorDisplayName, $filename)));
765
+        $emailTemplate->addHeader();
766
+        $emailTemplate->addHeading($l->t('%1$s shared »%2$s« with you', [$initiatorDisplayName, $filename]), false);
767
+        $text = $l->t('%1$s shared »%2$s« with you.', [$initiatorDisplayName, $filename]);
768
+
769
+        $emailTemplate->addBodyText(
770
+            htmlspecialchars($text . ' ' . $l->t('Click the button below to open it.')),
771
+            $text
772
+        );
773
+        $emailTemplate->addBodyButton(
774
+            $l->t('Open »%s«', [$filename]),
775
+            $link
776
+        );
777
+
778
+        $message->setTo([$shareWith]);
779
+
780
+        // The "From" contains the sharers name
781
+        $instanceName = $this->defaults->getName();
782
+        $senderName = $l->t(
783
+            '%1$s via %2$s',
784
+            [
785
+                $initiatorDisplayName,
786
+                $instanceName
787
+            ]
788
+        );
789
+        $message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
790
+
791
+        // The "Reply-To" is set to the sharer if an mail address is configured
792
+        // also the default footer contains a "Do not reply" which needs to be adjusted.
793
+        $initiatorEmail = $initiatorUser->getEMailAddress();
794
+        if($initiatorEmail !== null) {
795
+            $message->setReplyTo([$initiatorEmail => $initiatorDisplayName]);
796
+            $emailTemplate->addFooter($instanceName . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : ''));
797
+        } else {
798
+            $emailTemplate->addFooter();
799
+        }
800
+
801
+        $message->useTemplate($emailTemplate);
802
+        try {
803
+            $failedRecipients = $this->mailer->send($message);
804
+            if(!empty($failedRecipients)) {
805
+                $this->logger->error('Share notification mail could not be sent to: ' . implode(', ', $failedRecipients));
806
+                return;
807
+            }
808
+        } catch (\Exception $e) {
809
+            $this->logger->logException($e, ['message' => 'Share notification mail could not be sent']);
810
+        }
811
+    }
812
+
813
+    /**
814
+     * Update a share
815
+     *
816
+     * @param \OCP\Share\IShare $share
817
+     * @return \OCP\Share\IShare The share object
818
+     * @throws \InvalidArgumentException
819
+     */
820
+    public function updateShare(\OCP\Share\IShare $share) {
821
+        $expirationDateUpdated = false;
822
+
823
+        $this->canShare($share);
824
+
825
+        try {
826
+            $originalShare = $this->getShareById($share->getFullId());
827
+        } catch (\UnexpectedValueException $e) {
828
+            throw new \InvalidArgumentException('Share does not have a full id');
829
+        }
830
+
831
+        // We can't change the share type!
832
+        if ($share->getShareType() !== $originalShare->getShareType()) {
833
+            throw new \InvalidArgumentException('Can’t change share type');
834
+        }
835
+
836
+        // We can only change the recipient on user shares
837
+        if ($share->getSharedWith() !== $originalShare->getSharedWith() &&
838
+            $share->getShareType() !== \OCP\Share::SHARE_TYPE_USER) {
839
+            throw new \InvalidArgumentException('Can only update recipient on user shares');
840
+        }
841
+
842
+        // Cannot share with the owner
843
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
844
+            $share->getSharedWith() === $share->getShareOwner()) {
845
+            throw new \InvalidArgumentException('Can’t share with the share owner');
846
+        }
847
+
848
+        $this->generalCreateChecks($share);
849
+
850
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
851
+            $this->userCreateChecks($share);
852
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
853
+            $this->groupCreateChecks($share);
854
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
855
+            $this->linkCreateChecks($share);
856
+
857
+            $this->updateSharePasswordIfNeeded($share, $originalShare);
858
+
859
+            if ($share->getExpirationDate() != $originalShare->getExpirationDate()) {
860
+                //Verify the expiration date
861
+                $this->validateExpirationDate($share);
862
+                $expirationDateUpdated = true;
863
+            }
864
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
865
+            // The new password is not set again if it is the same as the old
866
+            // one, unless when switching from sending by Talk to sending by
867
+            // mail.
868
+            $plainTextPassword = $share->getPassword();
869
+            if (!empty($plainTextPassword) && !$this->updateSharePasswordIfNeeded($share, $originalShare) &&
870
+                    !($originalShare->getSendPasswordByTalk() && !$share->getSendPasswordByTalk())) {
871
+                $plainTextPassword = null;
872
+            }
873
+            if (empty($plainTextPassword) && !$originalShare->getSendPasswordByTalk() && $share->getSendPasswordByTalk()) {
874
+                // If the same password was already sent by mail the recipient
875
+                // would already have access to the share without having to call
876
+                // the sharer to verify her identity
877
+                throw new \InvalidArgumentException('Can’t enable sending the password by Talk without setting a new password');
878
+            }
879
+        }
880
+
881
+        $this->pathCreateChecks($share->getNode());
882
+
883
+        // Now update the share!
884
+        $provider = $this->factory->getProviderForType($share->getShareType());
885
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
886
+            $share = $provider->update($share, $plainTextPassword);
887
+        } else {
888
+            $share = $provider->update($share);
889
+        }
890
+
891
+        if ($expirationDateUpdated === true) {
892
+            \OC_Hook::emit(Share::class, 'post_set_expiration_date', [
893
+                'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder',
894
+                'itemSource' => $share->getNode()->getId(),
895
+                'date' => $share->getExpirationDate(),
896
+                'uidOwner' => $share->getSharedBy(),
897
+            ]);
898
+        }
899
+
900
+        if ($share->getPassword() !== $originalShare->getPassword()) {
901
+            \OC_Hook::emit(Share::class, 'post_update_password', [
902
+                'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder',
903
+                'itemSource' => $share->getNode()->getId(),
904
+                'uidOwner' => $share->getSharedBy(),
905
+                'token' => $share->getToken(),
906
+                'disabled' => is_null($share->getPassword()),
907
+            ]);
908
+        }
909
+
910
+        if ($share->getPermissions() !== $originalShare->getPermissions()) {
911
+            if ($this->userManager->userExists($share->getShareOwner())) {
912
+                $userFolder = $this->rootFolder->getUserFolder($share->getShareOwner());
913
+            } else {
914
+                $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
915
+            }
916
+            \OC_Hook::emit(Share::class, 'post_update_permissions', array(
917
+                'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder',
918
+                'itemSource' => $share->getNode()->getId(),
919
+                'shareType' => $share->getShareType(),
920
+                'shareWith' => $share->getSharedWith(),
921
+                'uidOwner' => $share->getSharedBy(),
922
+                'permissions' => $share->getPermissions(),
923
+                'path' => $userFolder->getRelativePath($share->getNode()->getPath()),
924
+            ));
925
+        }
926
+
927
+        return $share;
928
+    }
929
+
930
+    /**
931
+     * Updates the password of the given share if it is not the same as the
932
+     * password of the original share.
933
+     *
934
+     * @param \OCP\Share\IShare $share the share to update its password.
935
+     * @param \OCP\Share\IShare $originalShare the original share to compare its
936
+     *        password with.
937
+     * @return boolean whether the password was updated or not.
938
+     */
939
+    private function updateSharePasswordIfNeeded(\OCP\Share\IShare $share, \OCP\Share\IShare $originalShare) {
940
+        // Password updated.
941
+        if ($share->getPassword() !== $originalShare->getPassword()) {
942
+            //Verify the password
943
+            $this->verifyPassword($share->getPassword());
944
+
945
+            // If a password is set. Hash it!
946
+            if ($share->getPassword() !== null) {
947
+                $share->setPassword($this->hasher->hash($share->getPassword()));
948
+
949
+                return true;
950
+            }
951
+        }
952
+
953
+        return false;
954
+    }
955
+
956
+    /**
957
+     * Delete all the children of this share
958
+     * FIXME: remove once https://github.com/owncloud/core/pull/21660 is in
959
+     *
960
+     * @param \OCP\Share\IShare $share
961
+     * @return \OCP\Share\IShare[] List of deleted shares
962
+     */
963
+    protected function deleteChildren(\OCP\Share\IShare $share) {
964
+        $deletedShares = [];
965
+
966
+        $provider = $this->factory->getProviderForType($share->getShareType());
967
+
968
+        foreach ($provider->getChildren($share) as $child) {
969
+            $deletedChildren = $this->deleteChildren($child);
970
+            $deletedShares = array_merge($deletedShares, $deletedChildren);
971
+
972
+            $provider->delete($child);
973
+            $deletedShares[] = $child;
974
+        }
975
+
976
+        return $deletedShares;
977
+    }
978
+
979
+    /**
980
+     * Delete a share
981
+     *
982
+     * @param \OCP\Share\IShare $share
983
+     * @throws ShareNotFound
984
+     * @throws \InvalidArgumentException
985
+     */
986
+    public function deleteShare(\OCP\Share\IShare $share) {
987
+
988
+        try {
989
+            $share->getFullId();
990
+        } catch (\UnexpectedValueException $e) {
991
+            throw new \InvalidArgumentException('Share does not have a full id');
992
+        }
993
+
994
+        $event = new GenericEvent($share);
995
+        $this->eventDispatcher->dispatch('OCP\Share::preUnshare', $event);
996
+
997
+        // Get all children and delete them as well
998
+        $deletedShares = $this->deleteChildren($share);
999
+
1000
+        // Do the actual delete
1001
+        $provider = $this->factory->getProviderForType($share->getShareType());
1002
+        $provider->delete($share);
1003
+
1004
+        // All the deleted shares caused by this delete
1005
+        $deletedShares[] = $share;
1006
+
1007
+        // Emit post hook
1008
+        $event->setArgument('deletedShares', $deletedShares);
1009
+        $this->eventDispatcher->dispatch('OCP\Share::postUnshare', $event);
1010
+    }
1011
+
1012
+
1013
+    /**
1014
+     * Unshare a file as the recipient.
1015
+     * This can be different from a regular delete for example when one of
1016
+     * the users in a groups deletes that share. But the provider should
1017
+     * handle this.
1018
+     *
1019
+     * @param \OCP\Share\IShare $share
1020
+     * @param string $recipientId
1021
+     */
1022
+    public function deleteFromSelf(\OCP\Share\IShare $share, $recipientId) {
1023
+        list($providerId, ) = $this->splitFullId($share->getFullId());
1024
+        $provider = $this->factory->getProvider($providerId);
1025
+
1026
+        $provider->deleteFromSelf($share, $recipientId);
1027
+        $event = new GenericEvent($share);
1028
+        $this->eventDispatcher->dispatch('OCP\Share::postUnshareFromSelf', $event);
1029
+    }
1030
+
1031
+    public function restoreShare(IShare $share, string $recipientId): IShare {
1032
+        list($providerId, ) = $this->splitFullId($share->getFullId());
1033
+        $provider = $this->factory->getProvider($providerId);
1034
+
1035
+        return $provider->restore($share, $recipientId);
1036
+    }
1037
+
1038
+    /**
1039
+     * @inheritdoc
1040
+     */
1041
+    public function moveShare(\OCP\Share\IShare $share, $recipientId) {
1042
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
1043
+            throw new \InvalidArgumentException('Can’t change target of link share');
1044
+        }
1045
+
1046
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && $share->getSharedWith() !== $recipientId) {
1047
+            throw new \InvalidArgumentException('Invalid recipient');
1048
+        }
1049
+
1050
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
1051
+            $sharedWith = $this->groupManager->get($share->getSharedWith());
1052
+            if (is_null($sharedWith)) {
1053
+                throw new \InvalidArgumentException('Group "' . $share->getSharedWith() . '" does not exist');
1054
+            }
1055
+            $recipient = $this->userManager->get($recipientId);
1056
+            if (!$sharedWith->inGroup($recipient)) {
1057
+                throw new \InvalidArgumentException('Invalid recipient');
1058
+            }
1059
+        }
1060
+
1061
+        list($providerId, ) = $this->splitFullId($share->getFullId());
1062
+        $provider = $this->factory->getProvider($providerId);
1063
+
1064
+        $provider->move($share, $recipientId);
1065
+    }
1066
+
1067
+    public function getSharesInFolder($userId, Folder $node, $reshares = false) {
1068
+        $providers = $this->factory->getAllProviders();
1069
+
1070
+        return array_reduce($providers, function($shares, IShareProvider $provider) use ($userId, $node, $reshares) {
1071
+            $newShares = $provider->getSharesInFolder($userId, $node, $reshares);
1072
+            foreach ($newShares as $fid => $data) {
1073
+                if (!isset($shares[$fid])) {
1074
+                    $shares[$fid] = [];
1075
+                }
1076
+
1077
+                $shares[$fid] = array_merge($shares[$fid], $data);
1078
+            }
1079
+            return $shares;
1080
+        }, []);
1081
+    }
1082
+
1083
+    /**
1084
+     * @inheritdoc
1085
+     */
1086
+    public function getSharesBy($userId, $shareType, $path = null, $reshares = false, $limit = 50, $offset = 0) {
1087
+        if ($path !== null &&
1088
+                !($path instanceof \OCP\Files\File) &&
1089
+                !($path instanceof \OCP\Files\Folder)) {
1090
+            throw new \InvalidArgumentException('invalid path');
1091
+        }
1092
+
1093
+        try {
1094
+            $provider = $this->factory->getProviderForType($shareType);
1095
+        } catch (ProviderException $e) {
1096
+            return [];
1097
+        }
1098
+
1099
+        $shares = $provider->getSharesBy($userId, $shareType, $path, $reshares, $limit, $offset);
1100
+
1101
+        /*
1102 1102
 		 * Work around so we don't return expired shares but still follow
1103 1103
 		 * proper pagination.
1104 1104
 		 */
1105 1105
 
1106
-		$shares2 = [];
1107
-
1108
-		while(true) {
1109
-			$added = 0;
1110
-			foreach ($shares as $share) {
1111
-
1112
-				try {
1113
-					$this->checkExpireDate($share);
1114
-				} catch (ShareNotFound $e) {
1115
-					//Ignore since this basically means the share is deleted
1116
-					continue;
1117
-				}
1118
-
1119
-				$added++;
1120
-				$shares2[] = $share;
1121
-
1122
-				if (count($shares2) === $limit) {
1123
-					break;
1124
-				}
1125
-			}
1126
-
1127
-			// If we did not fetch more shares than the limit then there are no more shares
1128
-			if (count($shares) < $limit) {
1129
-				break;
1130
-			}
1131
-
1132
-			if (count($shares2) === $limit) {
1133
-				break;
1134
-			}
1135
-
1136
-			// If there was no limit on the select we are done
1137
-			if ($limit === -1) {
1138
-				break;
1139
-			}
1140
-
1141
-			$offset += $added;
1142
-
1143
-			// Fetch again $limit shares
1144
-			$shares = $provider->getSharesBy($userId, $shareType, $path, $reshares, $limit, $offset);
1145
-
1146
-			// No more shares means we are done
1147
-			if (empty($shares)) {
1148
-				break;
1149
-			}
1150
-		}
1151
-
1152
-		$shares = $shares2;
1153
-
1154
-		return $shares;
1155
-	}
1156
-
1157
-	/**
1158
-	 * @inheritdoc
1159
-	 */
1160
-	public function getSharedWith($userId, $shareType, $node = null, $limit = 50, $offset = 0) {
1161
-		try {
1162
-			$provider = $this->factory->getProviderForType($shareType);
1163
-		} catch (ProviderException $e) {
1164
-			return [];
1165
-		}
1166
-
1167
-		$shares = $provider->getSharedWith($userId, $shareType, $node, $limit, $offset);
1168
-
1169
-		// remove all shares which are already expired
1170
-		foreach ($shares as $key => $share) {
1171
-			try {
1172
-				$this->checkExpireDate($share);
1173
-			} catch (ShareNotFound $e) {
1174
-				unset($shares[$key]);
1175
-			}
1176
-		}
1177
-
1178
-		return $shares;
1179
-	}
1180
-
1181
-	/**
1182
-	 * @inheritdoc
1183
-	 */
1184
-	public function getDeletedSharedWith($userId, $shareType, $node = null, $limit = 50, $offset = 0) {
1185
-		$shares = $this->getSharedWith($userId, $shareType, $node, $limit, $offset);
1186
-
1187
-		// Only get deleted shares
1188
-		$shares = array_filter($shares, function(IShare $share) {
1189
-			return $share->getPermissions() === 0;
1190
-		});
1191
-
1192
-		// Only get shares where the owner still exists
1193
-		$shares = array_filter($shares, function (IShare $share) {
1194
-			return $this->userManager->userExists($share->getShareOwner());
1195
-		});
1196
-
1197
-		return $shares;
1198
-	}
1199
-
1200
-	/**
1201
-	 * @inheritdoc
1202
-	 */
1203
-	public function getShareById($id, $recipient = null) {
1204
-		if ($id === null) {
1205
-			throw new ShareNotFound();
1206
-		}
1207
-
1208
-		list($providerId, $id) = $this->splitFullId($id);
1209
-
1210
-		try {
1211
-			$provider = $this->factory->getProvider($providerId);
1212
-		} catch (ProviderException $e) {
1213
-			throw new ShareNotFound();
1214
-		}
1215
-
1216
-		$share = $provider->getShareById($id, $recipient);
1217
-
1218
-		$this->checkExpireDate($share);
1219
-
1220
-		return $share;
1221
-	}
1222
-
1223
-	/**
1224
-	 * Get all the shares for a given path
1225
-	 *
1226
-	 * @param \OCP\Files\Node $path
1227
-	 * @param int $page
1228
-	 * @param int $perPage
1229
-	 *
1230
-	 * @return Share[]
1231
-	 */
1232
-	public function getSharesByPath(\OCP\Files\Node $path, $page=0, $perPage=50) {
1233
-		return [];
1234
-	}
1235
-
1236
-	/**
1237
-	 * Get the share by token possible with password
1238
-	 *
1239
-	 * @param string $token
1240
-	 * @return Share
1241
-	 *
1242
-	 * @throws ShareNotFound
1243
-	 */
1244
-	public function getShareByToken($token) {
1245
-		// tokens can't be valid local user names
1246
-		if ($this->userManager->userExists($token)) {
1247
-			throw new ShareNotFound();
1248
-		}
1249
-		$share = null;
1250
-		try {
1251
-			if($this->shareApiAllowLinks()) {
1252
-				$provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_LINK);
1253
-				$share = $provider->getShareByToken($token);
1254
-			}
1255
-		} catch (ProviderException $e) {
1256
-		} catch (ShareNotFound $e) {
1257
-		}
1258
-
1259
-
1260
-		// If it is not a link share try to fetch a federated share by token
1261
-		if ($share === null) {
1262
-			try {
1263
-				$provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_REMOTE);
1264
-				$share = $provider->getShareByToken($token);
1265
-			} catch (ProviderException $e) {
1266
-			} catch (ShareNotFound $e) {
1267
-			}
1268
-		}
1269
-
1270
-		// If it is not a link share try to fetch a mail share by token
1271
-		if ($share === null && $this->shareProviderExists(\OCP\Share::SHARE_TYPE_EMAIL)) {
1272
-			try {
1273
-				$provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_EMAIL);
1274
-				$share = $provider->getShareByToken($token);
1275
-			} catch (ProviderException $e) {
1276
-			} catch (ShareNotFound $e) {
1277
-			}
1278
-		}
1279
-
1280
-		if ($share === null && $this->shareProviderExists(\OCP\Share::SHARE_TYPE_CIRCLE)) {
1281
-			try {
1282
-				$provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_CIRCLE);
1283
-				$share = $provider->getShareByToken($token);
1284
-			} catch (ProviderException $e) {
1285
-			} catch (ShareNotFound $e) {
1286
-			}
1287
-		}
1288
-
1289
-		if ($share === null && $this->shareProviderExists(\OCP\Share::SHARE_TYPE_ROOM)) {
1290
-			try {
1291
-				$provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_ROOM);
1292
-				$share = $provider->getShareByToken($token);
1293
-			} catch (ProviderException $e) {
1294
-			} catch (ShareNotFound $e) {
1295
-			}
1296
-		}
1297
-
1298
-		if ($share === null) {
1299
-			throw new ShareNotFound($this->l->t('The requested share does not exist anymore'));
1300
-		}
1301
-
1302
-		$this->checkExpireDate($share);
1303
-
1304
-		/*
1106
+        $shares2 = [];
1107
+
1108
+        while(true) {
1109
+            $added = 0;
1110
+            foreach ($shares as $share) {
1111
+
1112
+                try {
1113
+                    $this->checkExpireDate($share);
1114
+                } catch (ShareNotFound $e) {
1115
+                    //Ignore since this basically means the share is deleted
1116
+                    continue;
1117
+                }
1118
+
1119
+                $added++;
1120
+                $shares2[] = $share;
1121
+
1122
+                if (count($shares2) === $limit) {
1123
+                    break;
1124
+                }
1125
+            }
1126
+
1127
+            // If we did not fetch more shares than the limit then there are no more shares
1128
+            if (count($shares) < $limit) {
1129
+                break;
1130
+            }
1131
+
1132
+            if (count($shares2) === $limit) {
1133
+                break;
1134
+            }
1135
+
1136
+            // If there was no limit on the select we are done
1137
+            if ($limit === -1) {
1138
+                break;
1139
+            }
1140
+
1141
+            $offset += $added;
1142
+
1143
+            // Fetch again $limit shares
1144
+            $shares = $provider->getSharesBy($userId, $shareType, $path, $reshares, $limit, $offset);
1145
+
1146
+            // No more shares means we are done
1147
+            if (empty($shares)) {
1148
+                break;
1149
+            }
1150
+        }
1151
+
1152
+        $shares = $shares2;
1153
+
1154
+        return $shares;
1155
+    }
1156
+
1157
+    /**
1158
+     * @inheritdoc
1159
+     */
1160
+    public function getSharedWith($userId, $shareType, $node = null, $limit = 50, $offset = 0) {
1161
+        try {
1162
+            $provider = $this->factory->getProviderForType($shareType);
1163
+        } catch (ProviderException $e) {
1164
+            return [];
1165
+        }
1166
+
1167
+        $shares = $provider->getSharedWith($userId, $shareType, $node, $limit, $offset);
1168
+
1169
+        // remove all shares which are already expired
1170
+        foreach ($shares as $key => $share) {
1171
+            try {
1172
+                $this->checkExpireDate($share);
1173
+            } catch (ShareNotFound $e) {
1174
+                unset($shares[$key]);
1175
+            }
1176
+        }
1177
+
1178
+        return $shares;
1179
+    }
1180
+
1181
+    /**
1182
+     * @inheritdoc
1183
+     */
1184
+    public function getDeletedSharedWith($userId, $shareType, $node = null, $limit = 50, $offset = 0) {
1185
+        $shares = $this->getSharedWith($userId, $shareType, $node, $limit, $offset);
1186
+
1187
+        // Only get deleted shares
1188
+        $shares = array_filter($shares, function(IShare $share) {
1189
+            return $share->getPermissions() === 0;
1190
+        });
1191
+
1192
+        // Only get shares where the owner still exists
1193
+        $shares = array_filter($shares, function (IShare $share) {
1194
+            return $this->userManager->userExists($share->getShareOwner());
1195
+        });
1196
+
1197
+        return $shares;
1198
+    }
1199
+
1200
+    /**
1201
+     * @inheritdoc
1202
+     */
1203
+    public function getShareById($id, $recipient = null) {
1204
+        if ($id === null) {
1205
+            throw new ShareNotFound();
1206
+        }
1207
+
1208
+        list($providerId, $id) = $this->splitFullId($id);
1209
+
1210
+        try {
1211
+            $provider = $this->factory->getProvider($providerId);
1212
+        } catch (ProviderException $e) {
1213
+            throw new ShareNotFound();
1214
+        }
1215
+
1216
+        $share = $provider->getShareById($id, $recipient);
1217
+
1218
+        $this->checkExpireDate($share);
1219
+
1220
+        return $share;
1221
+    }
1222
+
1223
+    /**
1224
+     * Get all the shares for a given path
1225
+     *
1226
+     * @param \OCP\Files\Node $path
1227
+     * @param int $page
1228
+     * @param int $perPage
1229
+     *
1230
+     * @return Share[]
1231
+     */
1232
+    public function getSharesByPath(\OCP\Files\Node $path, $page=0, $perPage=50) {
1233
+        return [];
1234
+    }
1235
+
1236
+    /**
1237
+     * Get the share by token possible with password
1238
+     *
1239
+     * @param string $token
1240
+     * @return Share
1241
+     *
1242
+     * @throws ShareNotFound
1243
+     */
1244
+    public function getShareByToken($token) {
1245
+        // tokens can't be valid local user names
1246
+        if ($this->userManager->userExists($token)) {
1247
+            throw new ShareNotFound();
1248
+        }
1249
+        $share = null;
1250
+        try {
1251
+            if($this->shareApiAllowLinks()) {
1252
+                $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_LINK);
1253
+                $share = $provider->getShareByToken($token);
1254
+            }
1255
+        } catch (ProviderException $e) {
1256
+        } catch (ShareNotFound $e) {
1257
+        }
1258
+
1259
+
1260
+        // If it is not a link share try to fetch a federated share by token
1261
+        if ($share === null) {
1262
+            try {
1263
+                $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_REMOTE);
1264
+                $share = $provider->getShareByToken($token);
1265
+            } catch (ProviderException $e) {
1266
+            } catch (ShareNotFound $e) {
1267
+            }
1268
+        }
1269
+
1270
+        // If it is not a link share try to fetch a mail share by token
1271
+        if ($share === null && $this->shareProviderExists(\OCP\Share::SHARE_TYPE_EMAIL)) {
1272
+            try {
1273
+                $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_EMAIL);
1274
+                $share = $provider->getShareByToken($token);
1275
+            } catch (ProviderException $e) {
1276
+            } catch (ShareNotFound $e) {
1277
+            }
1278
+        }
1279
+
1280
+        if ($share === null && $this->shareProviderExists(\OCP\Share::SHARE_TYPE_CIRCLE)) {
1281
+            try {
1282
+                $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_CIRCLE);
1283
+                $share = $provider->getShareByToken($token);
1284
+            } catch (ProviderException $e) {
1285
+            } catch (ShareNotFound $e) {
1286
+            }
1287
+        }
1288
+
1289
+        if ($share === null && $this->shareProviderExists(\OCP\Share::SHARE_TYPE_ROOM)) {
1290
+            try {
1291
+                $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_ROOM);
1292
+                $share = $provider->getShareByToken($token);
1293
+            } catch (ProviderException $e) {
1294
+            } catch (ShareNotFound $e) {
1295
+            }
1296
+        }
1297
+
1298
+        if ($share === null) {
1299
+            throw new ShareNotFound($this->l->t('The requested share does not exist anymore'));
1300
+        }
1301
+
1302
+        $this->checkExpireDate($share);
1303
+
1304
+        /*
1305 1305
 		 * Reduce the permissions for link shares if public upload is not enabled
1306 1306
 		 */
1307
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK &&
1308
-			!$this->shareApiLinkAllowPublicUpload()) {
1309
-			$share->setPermissions($share->getPermissions() & ~(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE));
1310
-		}
1311
-
1312
-		return $share;
1313
-	}
1314
-
1315
-	protected function checkExpireDate($share) {
1316
-		if ($share->isExpired()) {
1317
-			$this->deleteShare($share);
1318
-			throw new ShareNotFound($this->l->t('The requested share does not exist anymore'));
1319
-		}
1320
-
1321
-	}
1322
-
1323
-	/**
1324
-	 * Verify the password of a public share
1325
-	 *
1326
-	 * @param \OCP\Share\IShare $share
1327
-	 * @param string $password
1328
-	 * @return bool
1329
-	 */
1330
-	public function checkPassword(\OCP\Share\IShare $share, $password) {
1331
-		$passwordProtected = $share->getShareType() !== \OCP\Share::SHARE_TYPE_LINK
1332
-			|| $share->getShareType() !== \OCP\Share::SHARE_TYPE_EMAIL;
1333
-		if (!$passwordProtected) {
1334
-			//TODO maybe exception?
1335
-			return false;
1336
-		}
1337
-
1338
-		if ($password === null || $share->getPassword() === null) {
1339
-			return false;
1340
-		}
1341
-
1342
-		$newHash = '';
1343
-		if (!$this->hasher->verify($password, $share->getPassword(), $newHash)) {
1344
-			return false;
1345
-		}
1346
-
1347
-		if (!empty($newHash)) {
1348
-			$share->setPassword($newHash);
1349
-			$provider = $this->factory->getProviderForType($share->getShareType());
1350
-			$provider->update($share);
1351
-		}
1352
-
1353
-		return true;
1354
-	}
1355
-
1356
-	/**
1357
-	 * @inheritdoc
1358
-	 */
1359
-	public function userDeleted($uid) {
1360
-		$types = [\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_LINK, \OCP\Share::SHARE_TYPE_REMOTE, \OCP\Share::SHARE_TYPE_EMAIL];
1361
-
1362
-		foreach ($types as $type) {
1363
-			try {
1364
-				$provider = $this->factory->getProviderForType($type);
1365
-			} catch (ProviderException $e) {
1366
-				continue;
1367
-			}
1368
-			$provider->userDeleted($uid, $type);
1369
-		}
1370
-	}
1371
-
1372
-	/**
1373
-	 * @inheritdoc
1374
-	 */
1375
-	public function groupDeleted($gid) {
1376
-		$provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP);
1377
-		$provider->groupDeleted($gid);
1378
-	}
1379
-
1380
-	/**
1381
-	 * @inheritdoc
1382
-	 */
1383
-	public function userDeletedFromGroup($uid, $gid) {
1384
-		$provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP);
1385
-		$provider->userDeletedFromGroup($uid, $gid);
1386
-	}
1387
-
1388
-	/**
1389
-	 * Get access list to a path. This means
1390
-	 * all the users that can access a given path.
1391
-	 *
1392
-	 * Consider:
1393
-	 * -root
1394
-	 * |-folder1 (23)
1395
-	 *  |-folder2 (32)
1396
-	 *   |-fileA (42)
1397
-	 *
1398
-	 * fileA is shared with user1 and user1@server1
1399
-	 * folder2 is shared with group2 (user4 is a member of group2)
1400
-	 * folder1 is shared with user2 (renamed to "folder (1)") and user2@server2
1401
-	 *
1402
-	 * Then the access list to '/folder1/folder2/fileA' with $currentAccess is:
1403
-	 * [
1404
-	 *  users  => [
1405
-	 *      'user1' => ['node_id' => 42, 'node_path' => '/fileA'],
1406
-	 *      'user4' => ['node_id' => 32, 'node_path' => '/folder2'],
1407
-	 *      'user2' => ['node_id' => 23, 'node_path' => '/folder (1)'],
1408
-	 *  ],
1409
-	 *  remote => [
1410
-	 *      'user1@server1' => ['node_id' => 42, 'token' => 'SeCr3t'],
1411
-	 *      'user2@server2' => ['node_id' => 23, 'token' => 'FooBaR'],
1412
-	 *  ],
1413
-	 *  public => bool
1414
-	 *  mail => bool
1415
-	 * ]
1416
-	 *
1417
-	 * The access list to '/folder1/folder2/fileA' **without** $currentAccess is:
1418
-	 * [
1419
-	 *  users  => ['user1', 'user2', 'user4'],
1420
-	 *  remote => bool,
1421
-	 *  public => bool
1422
-	 *  mail => bool
1423
-	 * ]
1424
-	 *
1425
-	 * This is required for encryption/activity
1426
-	 *
1427
-	 * @param \OCP\Files\Node $path
1428
-	 * @param bool $recursive Should we check all parent folders as well
1429
-	 * @param bool $currentAccess Ensure the recipient has access to the file (e.g. did not unshare it)
1430
-	 * @return array
1431
-	 */
1432
-	public function getAccessList(\OCP\Files\Node $path, $recursive = true, $currentAccess = false) {
1433
-		$owner = $path->getOwner();
1434
-
1435
-		if ($owner === null) {
1436
-			return [];
1437
-		}
1438
-
1439
-		$owner = $owner->getUID();
1440
-
1441
-		if ($currentAccess) {
1442
-			$al = ['users' => [], 'remote' => [], 'public' => false];
1443
-		} else {
1444
-			$al = ['users' => [], 'remote' => false, 'public' => false];
1445
-		}
1446
-		if (!$this->userManager->userExists($owner)) {
1447
-			return $al;
1448
-		}
1449
-
1450
-		//Get node for the owner and correct the owner in case of external storages
1451
-		$userFolder = $this->rootFolder->getUserFolder($owner);
1452
-		if ($path->getId() !== $userFolder->getId() && !$userFolder->isSubNode($path)) {
1453
-			$nodes = $userFolder->getById($path->getId());
1454
-			$path = array_shift($nodes);
1455
-			if ($path->getOwner() === null) {
1456
-				return [];
1457
-			}
1458
-			$owner = $path->getOwner()->getUID();
1459
-		}
1460
-
1461
-		$providers = $this->factory->getAllProviders();
1462
-
1463
-		/** @var Node[] $nodes */
1464
-		$nodes = [];
1465
-
1466
-
1467
-		if ($currentAccess) {
1468
-			$ownerPath = $path->getPath();
1469
-			$ownerPath = explode('/', $ownerPath, 4);
1470
-			if (count($ownerPath) < 4) {
1471
-				$ownerPath = '';
1472
-			} else {
1473
-				$ownerPath = $ownerPath[3];
1474
-			}
1475
-			$al['users'][$owner] = [
1476
-				'node_id' => $path->getId(),
1477
-				'node_path' => '/' . $ownerPath,
1478
-			];
1479
-		} else {
1480
-			$al['users'][] = $owner;
1481
-		}
1482
-
1483
-		// Collect all the shares
1484
-		while ($path->getPath() !== $userFolder->getPath()) {
1485
-			$nodes[] = $path;
1486
-			if (!$recursive) {
1487
-				break;
1488
-			}
1489
-			$path = $path->getParent();
1490
-		}
1491
-
1492
-		foreach ($providers as $provider) {
1493
-			$tmp = $provider->getAccessList($nodes, $currentAccess);
1494
-
1495
-			foreach ($tmp as $k => $v) {
1496
-				if (isset($al[$k])) {
1497
-					if (is_array($al[$k])) {
1498
-						if ($currentAccess) {
1499
-							$al[$k] += $v;
1500
-						} else {
1501
-							$al[$k] = array_merge($al[$k], $v);
1502
-							$al[$k] = array_unique($al[$k]);
1503
-							$al[$k] = array_values($al[$k]);
1504
-						}
1505
-					} else {
1506
-						$al[$k] = $al[$k] || $v;
1507
-					}
1508
-				} else {
1509
-					$al[$k] = $v;
1510
-				}
1511
-			}
1512
-		}
1513
-
1514
-		return $al;
1515
-	}
1516
-
1517
-	/**
1518
-	 * Create a new share
1519
-	 * @return \OCP\Share\IShare
1520
-	 */
1521
-	public function newShare() {
1522
-		return new \OC\Share20\Share($this->rootFolder, $this->userManager);
1523
-	}
1524
-
1525
-	/**
1526
-	 * Is the share API enabled
1527
-	 *
1528
-	 * @return bool
1529
-	 */
1530
-	public function shareApiEnabled() {
1531
-		return $this->config->getAppValue('core', 'shareapi_enabled', 'yes') === 'yes';
1532
-	}
1533
-
1534
-	/**
1535
-	 * Is public link sharing enabled
1536
-	 *
1537
-	 * @return bool
1538
-	 */
1539
-	public function shareApiAllowLinks() {
1540
-		return $this->config->getAppValue('core', 'shareapi_allow_links', 'yes') === 'yes';
1541
-	}
1542
-
1543
-	/**
1544
-	 * Is password on public link requires
1545
-	 *
1546
-	 * @return bool
1547
-	 */
1548
-	public function shareApiLinkEnforcePassword() {
1549
-		return $this->config->getAppValue('core', 'shareapi_enforce_links_password', 'no') === 'yes';
1550
-	}
1551
-
1552
-	/**
1553
-	 * Is default expire date enabled
1554
-	 *
1555
-	 * @return bool
1556
-	 */
1557
-	public function shareApiLinkDefaultExpireDate() {
1558
-		return $this->config->getAppValue('core', 'shareapi_default_expire_date', 'no') === 'yes';
1559
-	}
1560
-
1561
-	/**
1562
-	 * Is default expire date enforced
1563
-	 *`
1564
-	 * @return bool
1565
-	 */
1566
-	public function shareApiLinkDefaultExpireDateEnforced() {
1567
-		return $this->shareApiLinkDefaultExpireDate() &&
1568
-			$this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'no') === 'yes';
1569
-	}
1570
-
1571
-	/**
1572
-	 * Number of default expire days
1573
-	 *shareApiLinkAllowPublicUpload
1574
-	 * @return int
1575
-	 */
1576
-	public function shareApiLinkDefaultExpireDays() {
1577
-		return (int)$this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7');
1578
-	}
1579
-
1580
-	/**
1581
-	 * Allow public upload on link shares
1582
-	 *
1583
-	 * @return bool
1584
-	 */
1585
-	public function shareApiLinkAllowPublicUpload() {
1586
-		return $this->config->getAppValue('core', 'shareapi_allow_public_upload', 'yes') === 'yes';
1587
-	}
1588
-
1589
-	/**
1590
-	 * check if user can only share with group members
1591
-	 * @return bool
1592
-	 */
1593
-	public function shareWithGroupMembersOnly() {
1594
-		return $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
1595
-	}
1596
-
1597
-	/**
1598
-	 * Check if users can share with groups
1599
-	 * @return bool
1600
-	 */
1601
-	public function allowGroupSharing() {
1602
-		return $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes') === 'yes';
1603
-	}
1604
-
1605
-	/**
1606
-	 * Copied from \OC_Util::isSharingDisabledForUser
1607
-	 *
1608
-	 * TODO: Deprecate fuction from OC_Util
1609
-	 *
1610
-	 * @param string $userId
1611
-	 * @return bool
1612
-	 */
1613
-	public function sharingDisabledForUser($userId) {
1614
-		if ($userId === null) {
1615
-			return false;
1616
-		}
1617
-
1618
-		if (isset($this->sharingDisabledForUsersCache[$userId])) {
1619
-			return $this->sharingDisabledForUsersCache[$userId];
1620
-		}
1621
-
1622
-		if ($this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes') {
1623
-			$groupsList = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', '');
1624
-			$excludedGroups = json_decode($groupsList);
1625
-			if (is_null($excludedGroups)) {
1626
-				$excludedGroups = explode(',', $groupsList);
1627
-				$newValue = json_encode($excludedGroups);
1628
-				$this->config->setAppValue('core', 'shareapi_exclude_groups_list', $newValue);
1629
-			}
1630
-			$user = $this->userManager->get($userId);
1631
-			$usersGroups = $this->groupManager->getUserGroupIds($user);
1632
-			if (!empty($usersGroups)) {
1633
-				$remainingGroups = array_diff($usersGroups, $excludedGroups);
1634
-				// if the user is only in groups which are disabled for sharing then
1635
-				// sharing is also disabled for the user
1636
-				if (empty($remainingGroups)) {
1637
-					$this->sharingDisabledForUsersCache[$userId] = true;
1638
-					return true;
1639
-				}
1640
-			}
1641
-		}
1642
-
1643
-		$this->sharingDisabledForUsersCache[$userId] = false;
1644
-		return false;
1645
-	}
1646
-
1647
-	/**
1648
-	 * @inheritdoc
1649
-	 */
1650
-	public function outgoingServer2ServerSharesAllowed() {
1651
-		return $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') === 'yes';
1652
-	}
1653
-
1654
-	/**
1655
-	 * @inheritdoc
1656
-	 */
1657
-	public function outgoingServer2ServerGroupSharesAllowed() {
1658
-		return $this->config->getAppValue('files_sharing', 'outgoing_server2server_group_share_enabled', 'no') === 'yes';
1659
-	}
1660
-
1661
-	/**
1662
-	 * @inheritdoc
1663
-	 */
1664
-	public function shareProviderExists($shareType) {
1665
-		try {
1666
-			$this->factory->getProviderForType($shareType);
1667
-		} catch (ProviderException $e) {
1668
-			return false;
1669
-		}
1670
-
1671
-		return true;
1672
-	}
1673
-
1674
-	public function getAllShares(): iterable {
1675
-		$providers = $this->factory->getAllProviders();
1676
-
1677
-		foreach ($providers as $provider) {
1678
-			yield from $provider->getAllShares();
1679
-		}
1680
-	}
1307
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK &&
1308
+            !$this->shareApiLinkAllowPublicUpload()) {
1309
+            $share->setPermissions($share->getPermissions() & ~(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE));
1310
+        }
1311
+
1312
+        return $share;
1313
+    }
1314
+
1315
+    protected function checkExpireDate($share) {
1316
+        if ($share->isExpired()) {
1317
+            $this->deleteShare($share);
1318
+            throw new ShareNotFound($this->l->t('The requested share does not exist anymore'));
1319
+        }
1320
+
1321
+    }
1322
+
1323
+    /**
1324
+     * Verify the password of a public share
1325
+     *
1326
+     * @param \OCP\Share\IShare $share
1327
+     * @param string $password
1328
+     * @return bool
1329
+     */
1330
+    public function checkPassword(\OCP\Share\IShare $share, $password) {
1331
+        $passwordProtected = $share->getShareType() !== \OCP\Share::SHARE_TYPE_LINK
1332
+            || $share->getShareType() !== \OCP\Share::SHARE_TYPE_EMAIL;
1333
+        if (!$passwordProtected) {
1334
+            //TODO maybe exception?
1335
+            return false;
1336
+        }
1337
+
1338
+        if ($password === null || $share->getPassword() === null) {
1339
+            return false;
1340
+        }
1341
+
1342
+        $newHash = '';
1343
+        if (!$this->hasher->verify($password, $share->getPassword(), $newHash)) {
1344
+            return false;
1345
+        }
1346
+
1347
+        if (!empty($newHash)) {
1348
+            $share->setPassword($newHash);
1349
+            $provider = $this->factory->getProviderForType($share->getShareType());
1350
+            $provider->update($share);
1351
+        }
1352
+
1353
+        return true;
1354
+    }
1355
+
1356
+    /**
1357
+     * @inheritdoc
1358
+     */
1359
+    public function userDeleted($uid) {
1360
+        $types = [\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_LINK, \OCP\Share::SHARE_TYPE_REMOTE, \OCP\Share::SHARE_TYPE_EMAIL];
1361
+
1362
+        foreach ($types as $type) {
1363
+            try {
1364
+                $provider = $this->factory->getProviderForType($type);
1365
+            } catch (ProviderException $e) {
1366
+                continue;
1367
+            }
1368
+            $provider->userDeleted($uid, $type);
1369
+        }
1370
+    }
1371
+
1372
+    /**
1373
+     * @inheritdoc
1374
+     */
1375
+    public function groupDeleted($gid) {
1376
+        $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP);
1377
+        $provider->groupDeleted($gid);
1378
+    }
1379
+
1380
+    /**
1381
+     * @inheritdoc
1382
+     */
1383
+    public function userDeletedFromGroup($uid, $gid) {
1384
+        $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP);
1385
+        $provider->userDeletedFromGroup($uid, $gid);
1386
+    }
1387
+
1388
+    /**
1389
+     * Get access list to a path. This means
1390
+     * all the users that can access a given path.
1391
+     *
1392
+     * Consider:
1393
+     * -root
1394
+     * |-folder1 (23)
1395
+     *  |-folder2 (32)
1396
+     *   |-fileA (42)
1397
+     *
1398
+     * fileA is shared with user1 and user1@server1
1399
+     * folder2 is shared with group2 (user4 is a member of group2)
1400
+     * folder1 is shared with user2 (renamed to "folder (1)") and user2@server2
1401
+     *
1402
+     * Then the access list to '/folder1/folder2/fileA' with $currentAccess is:
1403
+     * [
1404
+     *  users  => [
1405
+     *      'user1' => ['node_id' => 42, 'node_path' => '/fileA'],
1406
+     *      'user4' => ['node_id' => 32, 'node_path' => '/folder2'],
1407
+     *      'user2' => ['node_id' => 23, 'node_path' => '/folder (1)'],
1408
+     *  ],
1409
+     *  remote => [
1410
+     *      'user1@server1' => ['node_id' => 42, 'token' => 'SeCr3t'],
1411
+     *      'user2@server2' => ['node_id' => 23, 'token' => 'FooBaR'],
1412
+     *  ],
1413
+     *  public => bool
1414
+     *  mail => bool
1415
+     * ]
1416
+     *
1417
+     * The access list to '/folder1/folder2/fileA' **without** $currentAccess is:
1418
+     * [
1419
+     *  users  => ['user1', 'user2', 'user4'],
1420
+     *  remote => bool,
1421
+     *  public => bool
1422
+     *  mail => bool
1423
+     * ]
1424
+     *
1425
+     * This is required for encryption/activity
1426
+     *
1427
+     * @param \OCP\Files\Node $path
1428
+     * @param bool $recursive Should we check all parent folders as well
1429
+     * @param bool $currentAccess Ensure the recipient has access to the file (e.g. did not unshare it)
1430
+     * @return array
1431
+     */
1432
+    public function getAccessList(\OCP\Files\Node $path, $recursive = true, $currentAccess = false) {
1433
+        $owner = $path->getOwner();
1434
+
1435
+        if ($owner === null) {
1436
+            return [];
1437
+        }
1438
+
1439
+        $owner = $owner->getUID();
1440
+
1441
+        if ($currentAccess) {
1442
+            $al = ['users' => [], 'remote' => [], 'public' => false];
1443
+        } else {
1444
+            $al = ['users' => [], 'remote' => false, 'public' => false];
1445
+        }
1446
+        if (!$this->userManager->userExists($owner)) {
1447
+            return $al;
1448
+        }
1449
+
1450
+        //Get node for the owner and correct the owner in case of external storages
1451
+        $userFolder = $this->rootFolder->getUserFolder($owner);
1452
+        if ($path->getId() !== $userFolder->getId() && !$userFolder->isSubNode($path)) {
1453
+            $nodes = $userFolder->getById($path->getId());
1454
+            $path = array_shift($nodes);
1455
+            if ($path->getOwner() === null) {
1456
+                return [];
1457
+            }
1458
+            $owner = $path->getOwner()->getUID();
1459
+        }
1460
+
1461
+        $providers = $this->factory->getAllProviders();
1462
+
1463
+        /** @var Node[] $nodes */
1464
+        $nodes = [];
1465
+
1466
+
1467
+        if ($currentAccess) {
1468
+            $ownerPath = $path->getPath();
1469
+            $ownerPath = explode('/', $ownerPath, 4);
1470
+            if (count($ownerPath) < 4) {
1471
+                $ownerPath = '';
1472
+            } else {
1473
+                $ownerPath = $ownerPath[3];
1474
+            }
1475
+            $al['users'][$owner] = [
1476
+                'node_id' => $path->getId(),
1477
+                'node_path' => '/' . $ownerPath,
1478
+            ];
1479
+        } else {
1480
+            $al['users'][] = $owner;
1481
+        }
1482
+
1483
+        // Collect all the shares
1484
+        while ($path->getPath() !== $userFolder->getPath()) {
1485
+            $nodes[] = $path;
1486
+            if (!$recursive) {
1487
+                break;
1488
+            }
1489
+            $path = $path->getParent();
1490
+        }
1491
+
1492
+        foreach ($providers as $provider) {
1493
+            $tmp = $provider->getAccessList($nodes, $currentAccess);
1494
+
1495
+            foreach ($tmp as $k => $v) {
1496
+                if (isset($al[$k])) {
1497
+                    if (is_array($al[$k])) {
1498
+                        if ($currentAccess) {
1499
+                            $al[$k] += $v;
1500
+                        } else {
1501
+                            $al[$k] = array_merge($al[$k], $v);
1502
+                            $al[$k] = array_unique($al[$k]);
1503
+                            $al[$k] = array_values($al[$k]);
1504
+                        }
1505
+                    } else {
1506
+                        $al[$k] = $al[$k] || $v;
1507
+                    }
1508
+                } else {
1509
+                    $al[$k] = $v;
1510
+                }
1511
+            }
1512
+        }
1513
+
1514
+        return $al;
1515
+    }
1516
+
1517
+    /**
1518
+     * Create a new share
1519
+     * @return \OCP\Share\IShare
1520
+     */
1521
+    public function newShare() {
1522
+        return new \OC\Share20\Share($this->rootFolder, $this->userManager);
1523
+    }
1524
+
1525
+    /**
1526
+     * Is the share API enabled
1527
+     *
1528
+     * @return bool
1529
+     */
1530
+    public function shareApiEnabled() {
1531
+        return $this->config->getAppValue('core', 'shareapi_enabled', 'yes') === 'yes';
1532
+    }
1533
+
1534
+    /**
1535
+     * Is public link sharing enabled
1536
+     *
1537
+     * @return bool
1538
+     */
1539
+    public function shareApiAllowLinks() {
1540
+        return $this->config->getAppValue('core', 'shareapi_allow_links', 'yes') === 'yes';
1541
+    }
1542
+
1543
+    /**
1544
+     * Is password on public link requires
1545
+     *
1546
+     * @return bool
1547
+     */
1548
+    public function shareApiLinkEnforcePassword() {
1549
+        return $this->config->getAppValue('core', 'shareapi_enforce_links_password', 'no') === 'yes';
1550
+    }
1551
+
1552
+    /**
1553
+     * Is default expire date enabled
1554
+     *
1555
+     * @return bool
1556
+     */
1557
+    public function shareApiLinkDefaultExpireDate() {
1558
+        return $this->config->getAppValue('core', 'shareapi_default_expire_date', 'no') === 'yes';
1559
+    }
1560
+
1561
+    /**
1562
+     * Is default expire date enforced
1563
+     *`
1564
+     * @return bool
1565
+     */
1566
+    public function shareApiLinkDefaultExpireDateEnforced() {
1567
+        return $this->shareApiLinkDefaultExpireDate() &&
1568
+            $this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'no') === 'yes';
1569
+    }
1570
+
1571
+    /**
1572
+     * Number of default expire days
1573
+     *shareApiLinkAllowPublicUpload
1574
+     * @return int
1575
+     */
1576
+    public function shareApiLinkDefaultExpireDays() {
1577
+        return (int)$this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7');
1578
+    }
1579
+
1580
+    /**
1581
+     * Allow public upload on link shares
1582
+     *
1583
+     * @return bool
1584
+     */
1585
+    public function shareApiLinkAllowPublicUpload() {
1586
+        return $this->config->getAppValue('core', 'shareapi_allow_public_upload', 'yes') === 'yes';
1587
+    }
1588
+
1589
+    /**
1590
+     * check if user can only share with group members
1591
+     * @return bool
1592
+     */
1593
+    public function shareWithGroupMembersOnly() {
1594
+        return $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
1595
+    }
1596
+
1597
+    /**
1598
+     * Check if users can share with groups
1599
+     * @return bool
1600
+     */
1601
+    public function allowGroupSharing() {
1602
+        return $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes') === 'yes';
1603
+    }
1604
+
1605
+    /**
1606
+     * Copied from \OC_Util::isSharingDisabledForUser
1607
+     *
1608
+     * TODO: Deprecate fuction from OC_Util
1609
+     *
1610
+     * @param string $userId
1611
+     * @return bool
1612
+     */
1613
+    public function sharingDisabledForUser($userId) {
1614
+        if ($userId === null) {
1615
+            return false;
1616
+        }
1617
+
1618
+        if (isset($this->sharingDisabledForUsersCache[$userId])) {
1619
+            return $this->sharingDisabledForUsersCache[$userId];
1620
+        }
1621
+
1622
+        if ($this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes') {
1623
+            $groupsList = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', '');
1624
+            $excludedGroups = json_decode($groupsList);
1625
+            if (is_null($excludedGroups)) {
1626
+                $excludedGroups = explode(',', $groupsList);
1627
+                $newValue = json_encode($excludedGroups);
1628
+                $this->config->setAppValue('core', 'shareapi_exclude_groups_list', $newValue);
1629
+            }
1630
+            $user = $this->userManager->get($userId);
1631
+            $usersGroups = $this->groupManager->getUserGroupIds($user);
1632
+            if (!empty($usersGroups)) {
1633
+                $remainingGroups = array_diff($usersGroups, $excludedGroups);
1634
+                // if the user is only in groups which are disabled for sharing then
1635
+                // sharing is also disabled for the user
1636
+                if (empty($remainingGroups)) {
1637
+                    $this->sharingDisabledForUsersCache[$userId] = true;
1638
+                    return true;
1639
+                }
1640
+            }
1641
+        }
1642
+
1643
+        $this->sharingDisabledForUsersCache[$userId] = false;
1644
+        return false;
1645
+    }
1646
+
1647
+    /**
1648
+     * @inheritdoc
1649
+     */
1650
+    public function outgoingServer2ServerSharesAllowed() {
1651
+        return $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') === 'yes';
1652
+    }
1653
+
1654
+    /**
1655
+     * @inheritdoc
1656
+     */
1657
+    public function outgoingServer2ServerGroupSharesAllowed() {
1658
+        return $this->config->getAppValue('files_sharing', 'outgoing_server2server_group_share_enabled', 'no') === 'yes';
1659
+    }
1660
+
1661
+    /**
1662
+     * @inheritdoc
1663
+     */
1664
+    public function shareProviderExists($shareType) {
1665
+        try {
1666
+            $this->factory->getProviderForType($shareType);
1667
+        } catch (ProviderException $e) {
1668
+            return false;
1669
+        }
1670
+
1671
+        return true;
1672
+    }
1673
+
1674
+    public function getAllShares(): iterable {
1675
+        $providers = $this->factory->getAllProviders();
1676
+
1677
+        foreach ($providers as $provider) {
1678
+            yield from $provider->getAllShares();
1679
+        }
1680
+    }
1681 1681
 }
Please login to merge, or discard this patch.
apps/sharebymail/lib/ShareByMailProvider.php 2 patches
Indentation   +1124 added lines, -1124 removed lines patch added patch discarded remove patch
@@ -54,1141 +54,1141 @@
 block discarded – undo
54 54
  */
55 55
 class ShareByMailProvider implements IShareProvider {
56 56
 
57
-	/** @var  IDBConnection */
58
-	private $dbConnection;
59
-
60
-	/** @var ILogger */
61
-	private $logger;
62
-
63
-	/** @var ISecureRandom */
64
-	private $secureRandom;
65
-
66
-	/** @var IUserManager */
67
-	private $userManager;
68
-
69
-	/** @var IRootFolder */
70
-	private $rootFolder;
71
-
72
-	/** @var IL10N */
73
-	private $l;
74
-
75
-	/** @var IMailer */
76
-	private $mailer;
77
-
78
-	/** @var IURLGenerator */
79
-	private $urlGenerator;
80
-
81
-	/** @var IManager  */
82
-	private $activityManager;
83
-
84
-	/** @var SettingsManager */
85
-	private $settingsManager;
86
-
87
-	/** @var Defaults */
88
-	private $defaults;
89
-
90
-	/** @var IHasher */
91
-	private $hasher;
92
-
93
-	/** @var  CapabilitiesManager */
94
-	private $capabilitiesManager;
95
-
96
-	/**
97
-	 * Return the identifier of this provider.
98
-	 *
99
-	 * @return string Containing only [a-zA-Z0-9]
100
-	 */
101
-	public function identifier() {
102
-		return 'ocMailShare';
103
-	}
104
-
105
-	/**
106
-	 * DefaultShareProvider constructor.
107
-	 *
108
-	 * @param IDBConnection $connection
109
-	 * @param ISecureRandom $secureRandom
110
-	 * @param IUserManager $userManager
111
-	 * @param IRootFolder $rootFolder
112
-	 * @param IL10N $l
113
-	 * @param ILogger $logger
114
-	 * @param IMailer $mailer
115
-	 * @param IURLGenerator $urlGenerator
116
-	 * @param IManager $activityManager
117
-	 * @param SettingsManager $settingsManager
118
-	 * @param Defaults $defaults
119
-	 * @param IHasher $hasher
120
-	 * @param CapabilitiesManager $capabilitiesManager
121
-	 */
122
-	public function __construct(
123
-		IDBConnection $connection,
124
-		ISecureRandom $secureRandom,
125
-		IUserManager $userManager,
126
-		IRootFolder $rootFolder,
127
-		IL10N $l,
128
-		ILogger $logger,
129
-		IMailer $mailer,
130
-		IURLGenerator $urlGenerator,
131
-		IManager $activityManager,
132
-		SettingsManager $settingsManager,
133
-		Defaults $defaults,
134
-		IHasher $hasher,
135
-		CapabilitiesManager $capabilitiesManager
136
-	) {
137
-		$this->dbConnection = $connection;
138
-		$this->secureRandom = $secureRandom;
139
-		$this->userManager = $userManager;
140
-		$this->rootFolder = $rootFolder;
141
-		$this->l = $l;
142
-		$this->logger = $logger;
143
-		$this->mailer = $mailer;
144
-		$this->urlGenerator = $urlGenerator;
145
-		$this->activityManager = $activityManager;
146
-		$this->settingsManager = $settingsManager;
147
-		$this->defaults = $defaults;
148
-		$this->hasher = $hasher;
149
-		$this->capabilitiesManager = $capabilitiesManager;
150
-	}
151
-
152
-	/**
153
-	 * Share a path
154
-	 *
155
-	 * @param IShare $share
156
-	 * @return IShare The share object
157
-	 * @throws ShareNotFound
158
-	 * @throws \Exception
159
-	 */
160
-	public function create(IShare $share) {
161
-
162
-		$shareWith = $share->getSharedWith();
163
-		/*
57
+    /** @var  IDBConnection */
58
+    private $dbConnection;
59
+
60
+    /** @var ILogger */
61
+    private $logger;
62
+
63
+    /** @var ISecureRandom */
64
+    private $secureRandom;
65
+
66
+    /** @var IUserManager */
67
+    private $userManager;
68
+
69
+    /** @var IRootFolder */
70
+    private $rootFolder;
71
+
72
+    /** @var IL10N */
73
+    private $l;
74
+
75
+    /** @var IMailer */
76
+    private $mailer;
77
+
78
+    /** @var IURLGenerator */
79
+    private $urlGenerator;
80
+
81
+    /** @var IManager  */
82
+    private $activityManager;
83
+
84
+    /** @var SettingsManager */
85
+    private $settingsManager;
86
+
87
+    /** @var Defaults */
88
+    private $defaults;
89
+
90
+    /** @var IHasher */
91
+    private $hasher;
92
+
93
+    /** @var  CapabilitiesManager */
94
+    private $capabilitiesManager;
95
+
96
+    /**
97
+     * Return the identifier of this provider.
98
+     *
99
+     * @return string Containing only [a-zA-Z0-9]
100
+     */
101
+    public function identifier() {
102
+        return 'ocMailShare';
103
+    }
104
+
105
+    /**
106
+     * DefaultShareProvider constructor.
107
+     *
108
+     * @param IDBConnection $connection
109
+     * @param ISecureRandom $secureRandom
110
+     * @param IUserManager $userManager
111
+     * @param IRootFolder $rootFolder
112
+     * @param IL10N $l
113
+     * @param ILogger $logger
114
+     * @param IMailer $mailer
115
+     * @param IURLGenerator $urlGenerator
116
+     * @param IManager $activityManager
117
+     * @param SettingsManager $settingsManager
118
+     * @param Defaults $defaults
119
+     * @param IHasher $hasher
120
+     * @param CapabilitiesManager $capabilitiesManager
121
+     */
122
+    public function __construct(
123
+        IDBConnection $connection,
124
+        ISecureRandom $secureRandom,
125
+        IUserManager $userManager,
126
+        IRootFolder $rootFolder,
127
+        IL10N $l,
128
+        ILogger $logger,
129
+        IMailer $mailer,
130
+        IURLGenerator $urlGenerator,
131
+        IManager $activityManager,
132
+        SettingsManager $settingsManager,
133
+        Defaults $defaults,
134
+        IHasher $hasher,
135
+        CapabilitiesManager $capabilitiesManager
136
+    ) {
137
+        $this->dbConnection = $connection;
138
+        $this->secureRandom = $secureRandom;
139
+        $this->userManager = $userManager;
140
+        $this->rootFolder = $rootFolder;
141
+        $this->l = $l;
142
+        $this->logger = $logger;
143
+        $this->mailer = $mailer;
144
+        $this->urlGenerator = $urlGenerator;
145
+        $this->activityManager = $activityManager;
146
+        $this->settingsManager = $settingsManager;
147
+        $this->defaults = $defaults;
148
+        $this->hasher = $hasher;
149
+        $this->capabilitiesManager = $capabilitiesManager;
150
+    }
151
+
152
+    /**
153
+     * Share a path
154
+     *
155
+     * @param IShare $share
156
+     * @return IShare The share object
157
+     * @throws ShareNotFound
158
+     * @throws \Exception
159
+     */
160
+    public function create(IShare $share) {
161
+
162
+        $shareWith = $share->getSharedWith();
163
+        /*
164 164
 		 * Check if file is not already shared with the remote user
165 165
 		 */
166
-		$alreadyShared = $this->getSharedWith($shareWith, \OCP\Share::SHARE_TYPE_EMAIL, $share->getNode(), 1, 0);
167
-		if (!empty($alreadyShared)) {
168
-			$message = 'Sharing %1$s failed, this item is already shared with %2$s';
169
-			$message_t = $this->l->t('Sharing %1$s failed, this item is already shared with %2$s', array($share->getNode()->getName(), $shareWith));
170
-			$this->logger->debug(sprintf($message, $share->getNode()->getName(), $shareWith), ['app' => 'Federated File Sharing']);
171
-			throw new \Exception($message_t);
172
-		}
173
-
174
-		// if the admin enforces a password for all mail shares we create a
175
-		// random password and send it to the recipient
176
-		$password = '';
177
-		$passwordEnforced = $this->settingsManager->enforcePasswordProtection();
178
-		if ($passwordEnforced) {
179
-			$password = $this->autoGeneratePassword($share);
180
-		}
181
-
182
-		$shareId = $this->createMailShare($share);
183
-		$send = $this->sendPassword($share, $password);
184
-		if ($passwordEnforced && $send === false) {
185
-			$this->sendPasswordToOwner($share, $password);
186
-		}
187
-
188
-		$this->createShareActivity($share);
189
-		$data = $this->getRawShare($shareId);
190
-
191
-		return $this->createShareObject($data);
192
-
193
-	}
194
-
195
-	/**
196
-	 * auto generate password in case of password enforcement on mail shares
197
-	 *
198
-	 * @param IShare $share
199
-	 * @return string
200
-	 * @throws \Exception
201
-	 */
202
-	protected function autoGeneratePassword($share) {
203
-		$initiatorUser = $this->userManager->get($share->getSharedBy());
204
-		$initiatorEMailAddress = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null;
205
-		$allowPasswordByMail = $this->settingsManager->sendPasswordByMail();
206
-
207
-		if ($initiatorEMailAddress === null && !$allowPasswordByMail) {
208
-			throw new \Exception(
209
-				$this->l->t("We can't send you the auto-generated password. Please set a valid email address in your personal settings and try again.")
210
-			);
211
-		}
212
-
213
-		$passwordPolicy = $this->getPasswordPolicy();
214
-		$passwordCharset = ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS;
215
-		$passwordLength = 8;
216
-		if (!empty($passwordPolicy)) {
217
-			$passwordLength = (int)$passwordPolicy['minLength'] > 0 ? (int)$passwordPolicy['minLength'] : $passwordLength;
218
-			$passwordCharset .= $passwordPolicy['enforceSpecialCharacters'] ? ISecureRandom::CHAR_SYMBOLS : '';
219
-		}
220
-
221
-		$password = $this->secureRandom->generate($passwordLength, $passwordCharset);
222
-
223
-		$share->setPassword($this->hasher->hash($password));
224
-
225
-		return $password;
226
-	}
227
-
228
-	/**
229
-	 * get password policy
230
-	 *
231
-	 * @return array
232
-	 */
233
-	protected function getPasswordPolicy() {
234
-		$capabilities = $this->capabilitiesManager->getCapabilities();
235
-		if (isset($capabilities['password_policy'])) {
236
-			return $capabilities['password_policy'];
237
-		}
238
-
239
-		return [];
240
-	}
241
-
242
-	/**
243
-	 * create activity if a file/folder was shared by mail
244
-	 *
245
-	 * @param IShare $share
246
-	 * @param string $type
247
-	 */
248
-	protected function createShareActivity(IShare $share, string $type = 'share') {
249
-
250
-		$userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
251
-
252
-		$this->publishActivity(
253
-			$type === 'share' ? Activity::SUBJECT_SHARED_EMAIL_SELF : Activity::SUBJECT_UNSHARED_EMAIL_SELF,
254
-			[$userFolder->getRelativePath($share->getNode()->getPath()), $share->getSharedWith()],
255
-			$share->getSharedBy(),
256
-			$share->getNode()->getId(),
257
-			(string) $userFolder->getRelativePath($share->getNode()->getPath())
258
-		);
259
-
260
-		if ($share->getShareOwner() !== $share->getSharedBy()) {
261
-			$ownerFolder = $this->rootFolder->getUserFolder($share->getShareOwner());
262
-			$fileId = $share->getNode()->getId();
263
-			$nodes = $ownerFolder->getById($fileId);
264
-			$ownerPath = $nodes[0]->getPath();
265
-			$this->publishActivity(
266
-				$type === 'share' ? Activity::SUBJECT_SHARED_EMAIL_BY : Activity::SUBJECT_UNSHARED_EMAIL_BY,
267
-				[$ownerFolder->getRelativePath($ownerPath), $share->getSharedWith(), $share->getSharedBy()],
268
-				$share->getShareOwner(),
269
-				$fileId,
270
-				(string) $ownerFolder->getRelativePath($ownerPath)
271
-			);
272
-		}
273
-
274
-	}
275
-
276
-	/**
277
-	 * create activity if a file/folder was shared by mail
278
-	 *
279
-	 * @param IShare $share
280
-	 * @param string $sharedWith
281
-	 * @param bool $sendToSelf
282
-	 */
283
-	protected function createPasswordSendActivity(IShare $share, $sharedWith, $sendToSelf) {
284
-
285
-		$userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
286
-
287
-		if ($sendToSelf) {
288
-			$this->publishActivity(
289
-				Activity::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF,
290
-				[$userFolder->getRelativePath($share->getNode()->getPath())],
291
-				$share->getSharedBy(),
292
-				$share->getNode()->getId(),
293
-				(string) $userFolder->getRelativePath($share->getNode()->getPath())
294
-			);
295
-		} else {
296
-			$this->publishActivity(
297
-				Activity::SUBJECT_SHARED_EMAIL_PASSWORD_SEND,
298
-				[$userFolder->getRelativePath($share->getNode()->getPath()), $sharedWith],
299
-				$share->getSharedBy(),
300
-				$share->getNode()->getId(),
301
-				(string) $userFolder->getRelativePath($share->getNode()->getPath())
302
-			);
303
-		}
304
-	}
305
-
306
-
307
-	/**
308
-	 * publish activity if a file/folder was shared by mail
309
-	 *
310
-	 * @param string $subject
311
-	 * @param array $parameters
312
-	 * @param string $affectedUser
313
-	 * @param int $fileId
314
-	 * @param string $filePath
315
-	 */
316
-	protected function publishActivity(string $subject, array $parameters, string $affectedUser, int $fileId, string $filePath) {
317
-		$event = $this->activityManager->generateEvent();
318
-		$event->setApp('sharebymail')
319
-			->setType('shared')
320
-			->setSubject($subject, $parameters)
321
-			->setAffectedUser($affectedUser)
322
-			->setObject('files', $fileId, $filePath);
323
-		$this->activityManager->publish($event);
324
-
325
-	}
326
-
327
-	/**
328
-	 * @param IShare $share
329
-	 * @return int
330
-	 * @throws \Exception
331
-	 */
332
-	protected function createMailShare(IShare $share) {
333
-		$share->setToken($this->generateToken());
334
-		$shareId = $this->addShareToDB(
335
-			$share->getNodeId(),
336
-			$share->getNodeType(),
337
-			$share->getSharedWith(),
338
-			$share->getSharedBy(),
339
-			$share->getShareOwner(),
340
-			$share->getPermissions(),
341
-			$share->getToken(),
342
-			$share->getPassword(),
343
-			$share->getSendPasswordByTalk()
344
-		);
345
-
346
-		try {
347
-			$link = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare',
348
-				['token' => $share->getToken()]);
349
-			$this->sendMailNotification(
350
-				$share->getNode()->getName(),
351
-				$link,
352
-				$share->getSharedBy(),
353
-				$share->getSharedWith(),
354
-				$share->getExpirationDate()
355
-			);
356
-		} catch (HintException $hintException) {
357
-			$this->logger->logException($hintException, [
358
-				'message' => 'Failed to send share by mail.',
359
-				'level' => ILogger::ERROR,
360
-				'app' => 'sharebymail',
361
-			]);
362
-			$this->removeShareFromTable($shareId);
363
-			throw $hintException;
364
-		} catch (\Exception $e) {
365
-			$this->logger->logException($e, [
366
-				'message' => 'Failed to send share by mail.',
367
-				'level' => ILogger::ERROR,
368
-				'app' => 'sharebymail',
369
-			]);
370
-			$this->removeShareFromTable($shareId);
371
-			throw new HintException('Failed to send share by mail',
372
-				$this->l->t('Failed to send share by email'));
373
-		}
374
-
375
-		return $shareId;
376
-
377
-	}
378
-
379
-	/**
380
-	 * @param string $filename
381
-	 * @param string $link
382
-	 * @param string $initiator
383
-	 * @param string $shareWith
384
-	 * @param \DateTime|null $expiration
385
-	 * @throws \Exception If mail couldn't be sent
386
-	 */
387
-	protected function sendMailNotification($filename,
388
-											$link,
389
-											$initiator,
390
-											$shareWith,
391
-											\DateTime $expiration = null) {
392
-		$initiatorUser = $this->userManager->get($initiator);
393
-		$initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
394
-		$message = $this->mailer->createMessage();
395
-
396
-		$emailTemplate = $this->mailer->createEMailTemplate('sharebymail.RecipientNotification', [
397
-			'filename' => $filename,
398
-			'link' => $link,
399
-			'initiator' => $initiatorDisplayName,
400
-			'expiration' => $expiration,
401
-			'shareWith' => $shareWith,
402
-		]);
403
-
404
-		$emailTemplate->setSubject($this->l->t('%1$s shared »%2$s« with you', array($initiatorDisplayName, $filename)));
405
-		$emailTemplate->addHeader();
406
-		$emailTemplate->addHeading($this->l->t('%1$s shared »%2$s« with you', [$initiatorDisplayName, $filename]), false);
407
-		$text = $this->l->t('%1$s shared »%2$s« with you.', [$initiatorDisplayName, $filename]);
408
-
409
-		$emailTemplate->addBodyText(
410
-			htmlspecialchars($text . ' ' . $this->l->t('Click the button below to open it.')),
411
-			$text
412
-		);
413
-		$emailTemplate->addBodyButton(
414
-			$this->l->t('Open »%s«', [$filename]),
415
-			$link
416
-		);
417
-
418
-		$message->setTo([$shareWith]);
419
-
420
-		// The "From" contains the sharers name
421
-		$instanceName = $this->defaults->getName();
422
-		$senderName = $this->l->t(
423
-			'%1$s via %2$s',
424
-			[
425
-				$initiatorDisplayName,
426
-				$instanceName
427
-			]
428
-		);
429
-		$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
430
-
431
-		// The "Reply-To" is set to the sharer if an mail address is configured
432
-		// also the default footer contains a "Do not reply" which needs to be adjusted.
433
-		$initiatorEmail = $initiatorUser->getEMailAddress();
434
-		if($initiatorEmail !== null) {
435
-			$message->setReplyTo([$initiatorEmail => $initiatorDisplayName]);
436
-			$emailTemplate->addFooter($instanceName . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : ''));
437
-		} else {
438
-			$emailTemplate->addFooter();
439
-		}
440
-
441
-		$message->useTemplate($emailTemplate);
442
-		$this->mailer->send($message);
443
-	}
444
-
445
-	/**
446
-	 * send password to recipient of a mail share
447
-	 *
448
-	 * @param IShare $share
449
-	 * @param string $password
450
-	 * @return bool
451
-	 */
452
-	protected function sendPassword(IShare $share, $password) {
453
-
454
-		$filename = $share->getNode()->getName();
455
-		$initiator = $share->getSharedBy();
456
-		$shareWith = $share->getSharedWith();
457
-
458
-		if ($password === '' || $this->settingsManager->sendPasswordByMail() === false || $share->getSendPasswordByTalk()) {
459
-			return false;
460
-		}
461
-
462
-		$initiatorUser = $this->userManager->get($initiator);
463
-		$initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
464
-		$initiatorEmailAddress = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null;
465
-
466
-		$plainBodyPart = $this->l->t("%1\$s shared »%2\$s« with you.\nYou should have already received a separate mail with a link to access it.\n", [$initiatorDisplayName, $filename]);
467
-		$htmlBodyPart = $this->l->t('%1$s shared »%2$s« with you. You should have already received a separate mail with a link to access it.', [$initiatorDisplayName, $filename]);
468
-
469
-		$message = $this->mailer->createMessage();
470
-
471
-		$emailTemplate = $this->mailer->createEMailTemplate('sharebymail.RecipientPasswordNotification', [
472
-			'filename' => $filename,
473
-			'password' => $password,
474
-			'initiator' => $initiatorDisplayName,
475
-			'initiatorEmail' => $initiatorEmailAddress,
476
-			'shareWith' => $shareWith,
477
-		]);
478
-
479
-		$emailTemplate->setSubject($this->l->t('Password to access »%1$s« shared to you by %2$s', [$filename, $initiatorDisplayName]));
480
-		$emailTemplate->addHeader();
481
-		$emailTemplate->addHeading($this->l->t('Password to access »%s«', [$filename]), false);
482
-		$emailTemplate->addBodyText(htmlspecialchars($htmlBodyPart), $plainBodyPart);
483
-		$emailTemplate->addBodyText($this->l->t('It is protected with the following password:'));
484
-		$emailTemplate->addBodyText($password);
485
-
486
-		// The "From" contains the sharers name
487
-		$instanceName = $this->defaults->getName();
488
-		$senderName = $this->l->t(
489
-			'%1$s via %2$s',
490
-			[
491
-				$initiatorDisplayName,
492
-				$instanceName
493
-			]
494
-		);
495
-		$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
496
-		if ($initiatorEmailAddress !== null) {
497
-			$message->setReplyTo([$initiatorEmailAddress => $initiatorDisplayName]);
498
-			$emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
499
-		} else {
500
-			$emailTemplate->addFooter();
501
-		}
502
-
503
-		$message->setTo([$shareWith]);
504
-		$message->useTemplate($emailTemplate);
505
-		$this->mailer->send($message);
506
-
507
-		$this->createPasswordSendActivity($share, $shareWith, false);
508
-
509
-		return true;
510
-	}
511
-
512
-	protected function sendNote(IShare $share) {
513
-
514
-		$recipient = $share->getSharedWith();
515
-
516
-
517
-		$filename = $share->getNode()->getName();
518
-		$initiator = $share->getSharedBy();
519
-		$note = $share->getNote();
520
-
521
-		$initiatorUser = $this->userManager->get($initiator);
522
-		$initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
523
-		$initiatorEmailAddress = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null;
524
-
525
-		$plainHeading = $this->l->t('%1$s shared »%2$s« with you and wants to add:', [$initiatorDisplayName, $filename]);
526
-		$htmlHeading = $this->l->t('%1$s shared »%2$s« with you and wants to add', [$initiatorDisplayName, $filename]);
527
-
528
-		$message = $this->mailer->createMessage();
529
-
530
-		$emailTemplate = $this->mailer->createEMailTemplate('shareByMail.sendNote');
531
-
532
-		$emailTemplate->setSubject($this->l->t('»%s« added a note to a file shared with you', [$initiatorDisplayName]));
533
-		$emailTemplate->addHeader();
534
-		$emailTemplate->addHeading(htmlspecialchars($htmlHeading), $plainHeading);
535
-		$emailTemplate->addBodyText(htmlspecialchars($note), $note);
536
-
537
-		$link = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare',
538
-			['token' => $share->getToken()]);
539
-		$emailTemplate->addBodyButton(
540
-			$this->l->t('Open »%s«', [$filename]),
541
-			$link
542
-		);
543
-
544
-		// The "From" contains the sharers name
545
-		$instanceName = $this->defaults->getName();
546
-		$senderName = $this->l->t(
547
-			'%1$s via %2$s',
548
-			[
549
-				$initiatorDisplayName,
550
-				$instanceName
551
-			]
552
-		);
553
-		$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
554
-		if ($initiatorEmailAddress !== null) {
555
-			$message->setReplyTo([$initiatorEmailAddress => $initiatorDisplayName]);
556
-			$emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
557
-		} else {
558
-			$emailTemplate->addFooter();
559
-		}
560
-
561
-		$message->setTo([$recipient]);
562
-		$message->useTemplate($emailTemplate);
563
-		$this->mailer->send($message);
564
-
565
-	}
566
-
567
-	/**
568
-	 * send auto generated password to the owner. This happens if the admin enforces
569
-	 * a password for mail shares and forbid to send the password by mail to the recipient
570
-	 *
571
-	 * @param IShare $share
572
-	 * @param string $password
573
-	 * @return bool
574
-	 * @throws \Exception
575
-	 */
576
-	protected function sendPasswordToOwner(IShare $share, $password) {
577
-
578
-		$filename = $share->getNode()->getName();
579
-		$initiator = $this->userManager->get($share->getSharedBy());
580
-		$initiatorEMailAddress = ($initiator instanceof IUser) ? $initiator->getEMailAddress() : null;
581
-		$initiatorDisplayName = ($initiator instanceof IUser) ? $initiator->getDisplayName() : $share->getSharedBy();
582
-		$shareWith = $share->getSharedWith();
583
-
584
-		if ($initiatorEMailAddress === null) {
585
-			throw new \Exception(
586
-				$this->l->t("We can't send you the auto-generated password. Please set a valid email address in your personal settings and try again.")
587
-			);
588
-		}
589
-
590
-		$bodyPart = $this->l->t('You just shared »%1$s« with %2$s. The share was already sent to the recipient. Due to the security policies defined by the administrator of %3$s each share needs to be protected by password and it is not allowed to send the password directly to the recipient. Therefore you need to forward the password manually to the recipient.', [$filename, $shareWith, $this->defaults->getName()]);
591
-
592
-		$message = $this->mailer->createMessage();
593
-		$emailTemplate = $this->mailer->createEMailTemplate('sharebymail.OwnerPasswordNotification', [
594
-			'filename' => $filename,
595
-			'password' => $password,
596
-			'initiator' => $initiatorDisplayName,
597
-			'initiatorEmail' => $initiatorEMailAddress,
598
-			'shareWith' => $shareWith,
599
-		]);
600
-
601
-		$emailTemplate->setSubject($this->l->t('Password to access »%1$s« shared by you with %2$s', [$filename, $shareWith]));
602
-		$emailTemplate->addHeader();
603
-		$emailTemplate->addHeading($this->l->t('Password to access »%s«', [$filename]), false);
604
-		$emailTemplate->addBodyText($bodyPart);
605
-		$emailTemplate->addBodyText($this->l->t('This is the password:'));
606
-		$emailTemplate->addBodyText($password);
607
-		$emailTemplate->addBodyText($this->l->t('You can choose a different password at any time in the share dialog.'));
608
-		$emailTemplate->addFooter();
609
-
610
-		if ($initiatorEMailAddress) {
611
-			$message->setFrom([$initiatorEMailAddress => $initiatorDisplayName]);
612
-		}
613
-		$message->setTo([$initiatorEMailAddress => $initiatorDisplayName]);
614
-		$message->useTemplate($emailTemplate);
615
-		$this->mailer->send($message);
616
-
617
-		$this->createPasswordSendActivity($share, $shareWith, true);
618
-
619
-		return true;
620
-	}
621
-
622
-	/**
623
-	 * generate share token
624
-	 *
625
-	 * @return string
626
-	 */
627
-	protected function generateToken($size = 15) {
628
-		$token = $this->secureRandom->generate($size, ISecureRandom::CHAR_HUMAN_READABLE);
629
-		return $token;
630
-	}
631
-
632
-	/**
633
-	 * Get all children of this share
634
-	 *
635
-	 * @param IShare $parent
636
-	 * @return IShare[]
637
-	 */
638
-	public function getChildren(IShare $parent) {
639
-		$children = [];
640
-
641
-		$qb = $this->dbConnection->getQueryBuilder();
642
-		$qb->select('*')
643
-			->from('share')
644
-			->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId())))
645
-			->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)))
646
-			->orderBy('id');
647
-
648
-		$cursor = $qb->execute();
649
-		while($data = $cursor->fetch()) {
650
-			$children[] = $this->createShareObject($data);
651
-		}
652
-		$cursor->closeCursor();
653
-
654
-		return $children;
655
-	}
656
-
657
-	/**
658
-	 * add share to the database and return the ID
659
-	 *
660
-	 * @param int $itemSource
661
-	 * @param string $itemType
662
-	 * @param string $shareWith
663
-	 * @param string $sharedBy
664
-	 * @param string $uidOwner
665
-	 * @param int $permissions
666
-	 * @param string $token
667
-	 * @param string $password
668
-	 * @param bool $sendPasswordByTalk
669
-	 * @return int
670
-	 */
671
-	protected function addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, $token, $password, $sendPasswordByTalk) {
672
-		$qb = $this->dbConnection->getQueryBuilder();
673
-		$qb->insert('share')
674
-			->setValue('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL))
675
-			->setValue('item_type', $qb->createNamedParameter($itemType))
676
-			->setValue('item_source', $qb->createNamedParameter($itemSource))
677
-			->setValue('file_source', $qb->createNamedParameter($itemSource))
678
-			->setValue('share_with', $qb->createNamedParameter($shareWith))
679
-			->setValue('uid_owner', $qb->createNamedParameter($uidOwner))
680
-			->setValue('uid_initiator', $qb->createNamedParameter($sharedBy))
681
-			->setValue('permissions', $qb->createNamedParameter($permissions))
682
-			->setValue('token', $qb->createNamedParameter($token))
683
-			->setValue('password', $qb->createNamedParameter($password))
684
-			->setValue('password_by_talk', $qb->createNamedParameter($sendPasswordByTalk, IQueryBuilder::PARAM_BOOL))
685
-			->setValue('stime', $qb->createNamedParameter(time()));
686
-
687
-		/*
166
+        $alreadyShared = $this->getSharedWith($shareWith, \OCP\Share::SHARE_TYPE_EMAIL, $share->getNode(), 1, 0);
167
+        if (!empty($alreadyShared)) {
168
+            $message = 'Sharing %1$s failed, this item is already shared with %2$s';
169
+            $message_t = $this->l->t('Sharing %1$s failed, this item is already shared with %2$s', array($share->getNode()->getName(), $shareWith));
170
+            $this->logger->debug(sprintf($message, $share->getNode()->getName(), $shareWith), ['app' => 'Federated File Sharing']);
171
+            throw new \Exception($message_t);
172
+        }
173
+
174
+        // if the admin enforces a password for all mail shares we create a
175
+        // random password and send it to the recipient
176
+        $password = '';
177
+        $passwordEnforced = $this->settingsManager->enforcePasswordProtection();
178
+        if ($passwordEnforced) {
179
+            $password = $this->autoGeneratePassword($share);
180
+        }
181
+
182
+        $shareId = $this->createMailShare($share);
183
+        $send = $this->sendPassword($share, $password);
184
+        if ($passwordEnforced && $send === false) {
185
+            $this->sendPasswordToOwner($share, $password);
186
+        }
187
+
188
+        $this->createShareActivity($share);
189
+        $data = $this->getRawShare($shareId);
190
+
191
+        return $this->createShareObject($data);
192
+
193
+    }
194
+
195
+    /**
196
+     * auto generate password in case of password enforcement on mail shares
197
+     *
198
+     * @param IShare $share
199
+     * @return string
200
+     * @throws \Exception
201
+     */
202
+    protected function autoGeneratePassword($share) {
203
+        $initiatorUser = $this->userManager->get($share->getSharedBy());
204
+        $initiatorEMailAddress = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null;
205
+        $allowPasswordByMail = $this->settingsManager->sendPasswordByMail();
206
+
207
+        if ($initiatorEMailAddress === null && !$allowPasswordByMail) {
208
+            throw new \Exception(
209
+                $this->l->t("We can't send you the auto-generated password. Please set a valid email address in your personal settings and try again.")
210
+            );
211
+        }
212
+
213
+        $passwordPolicy = $this->getPasswordPolicy();
214
+        $passwordCharset = ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS;
215
+        $passwordLength = 8;
216
+        if (!empty($passwordPolicy)) {
217
+            $passwordLength = (int)$passwordPolicy['minLength'] > 0 ? (int)$passwordPolicy['minLength'] : $passwordLength;
218
+            $passwordCharset .= $passwordPolicy['enforceSpecialCharacters'] ? ISecureRandom::CHAR_SYMBOLS : '';
219
+        }
220
+
221
+        $password = $this->secureRandom->generate($passwordLength, $passwordCharset);
222
+
223
+        $share->setPassword($this->hasher->hash($password));
224
+
225
+        return $password;
226
+    }
227
+
228
+    /**
229
+     * get password policy
230
+     *
231
+     * @return array
232
+     */
233
+    protected function getPasswordPolicy() {
234
+        $capabilities = $this->capabilitiesManager->getCapabilities();
235
+        if (isset($capabilities['password_policy'])) {
236
+            return $capabilities['password_policy'];
237
+        }
238
+
239
+        return [];
240
+    }
241
+
242
+    /**
243
+     * create activity if a file/folder was shared by mail
244
+     *
245
+     * @param IShare $share
246
+     * @param string $type
247
+     */
248
+    protected function createShareActivity(IShare $share, string $type = 'share') {
249
+
250
+        $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
251
+
252
+        $this->publishActivity(
253
+            $type === 'share' ? Activity::SUBJECT_SHARED_EMAIL_SELF : Activity::SUBJECT_UNSHARED_EMAIL_SELF,
254
+            [$userFolder->getRelativePath($share->getNode()->getPath()), $share->getSharedWith()],
255
+            $share->getSharedBy(),
256
+            $share->getNode()->getId(),
257
+            (string) $userFolder->getRelativePath($share->getNode()->getPath())
258
+        );
259
+
260
+        if ($share->getShareOwner() !== $share->getSharedBy()) {
261
+            $ownerFolder = $this->rootFolder->getUserFolder($share->getShareOwner());
262
+            $fileId = $share->getNode()->getId();
263
+            $nodes = $ownerFolder->getById($fileId);
264
+            $ownerPath = $nodes[0]->getPath();
265
+            $this->publishActivity(
266
+                $type === 'share' ? Activity::SUBJECT_SHARED_EMAIL_BY : Activity::SUBJECT_UNSHARED_EMAIL_BY,
267
+                [$ownerFolder->getRelativePath($ownerPath), $share->getSharedWith(), $share->getSharedBy()],
268
+                $share->getShareOwner(),
269
+                $fileId,
270
+                (string) $ownerFolder->getRelativePath($ownerPath)
271
+            );
272
+        }
273
+
274
+    }
275
+
276
+    /**
277
+     * create activity if a file/folder was shared by mail
278
+     *
279
+     * @param IShare $share
280
+     * @param string $sharedWith
281
+     * @param bool $sendToSelf
282
+     */
283
+    protected function createPasswordSendActivity(IShare $share, $sharedWith, $sendToSelf) {
284
+
285
+        $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
286
+
287
+        if ($sendToSelf) {
288
+            $this->publishActivity(
289
+                Activity::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF,
290
+                [$userFolder->getRelativePath($share->getNode()->getPath())],
291
+                $share->getSharedBy(),
292
+                $share->getNode()->getId(),
293
+                (string) $userFolder->getRelativePath($share->getNode()->getPath())
294
+            );
295
+        } else {
296
+            $this->publishActivity(
297
+                Activity::SUBJECT_SHARED_EMAIL_PASSWORD_SEND,
298
+                [$userFolder->getRelativePath($share->getNode()->getPath()), $sharedWith],
299
+                $share->getSharedBy(),
300
+                $share->getNode()->getId(),
301
+                (string) $userFolder->getRelativePath($share->getNode()->getPath())
302
+            );
303
+        }
304
+    }
305
+
306
+
307
+    /**
308
+     * publish activity if a file/folder was shared by mail
309
+     *
310
+     * @param string $subject
311
+     * @param array $parameters
312
+     * @param string $affectedUser
313
+     * @param int $fileId
314
+     * @param string $filePath
315
+     */
316
+    protected function publishActivity(string $subject, array $parameters, string $affectedUser, int $fileId, string $filePath) {
317
+        $event = $this->activityManager->generateEvent();
318
+        $event->setApp('sharebymail')
319
+            ->setType('shared')
320
+            ->setSubject($subject, $parameters)
321
+            ->setAffectedUser($affectedUser)
322
+            ->setObject('files', $fileId, $filePath);
323
+        $this->activityManager->publish($event);
324
+
325
+    }
326
+
327
+    /**
328
+     * @param IShare $share
329
+     * @return int
330
+     * @throws \Exception
331
+     */
332
+    protected function createMailShare(IShare $share) {
333
+        $share->setToken($this->generateToken());
334
+        $shareId = $this->addShareToDB(
335
+            $share->getNodeId(),
336
+            $share->getNodeType(),
337
+            $share->getSharedWith(),
338
+            $share->getSharedBy(),
339
+            $share->getShareOwner(),
340
+            $share->getPermissions(),
341
+            $share->getToken(),
342
+            $share->getPassword(),
343
+            $share->getSendPasswordByTalk()
344
+        );
345
+
346
+        try {
347
+            $link = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare',
348
+                ['token' => $share->getToken()]);
349
+            $this->sendMailNotification(
350
+                $share->getNode()->getName(),
351
+                $link,
352
+                $share->getSharedBy(),
353
+                $share->getSharedWith(),
354
+                $share->getExpirationDate()
355
+            );
356
+        } catch (HintException $hintException) {
357
+            $this->logger->logException($hintException, [
358
+                'message' => 'Failed to send share by mail.',
359
+                'level' => ILogger::ERROR,
360
+                'app' => 'sharebymail',
361
+            ]);
362
+            $this->removeShareFromTable($shareId);
363
+            throw $hintException;
364
+        } catch (\Exception $e) {
365
+            $this->logger->logException($e, [
366
+                'message' => 'Failed to send share by mail.',
367
+                'level' => ILogger::ERROR,
368
+                'app' => 'sharebymail',
369
+            ]);
370
+            $this->removeShareFromTable($shareId);
371
+            throw new HintException('Failed to send share by mail',
372
+                $this->l->t('Failed to send share by email'));
373
+        }
374
+
375
+        return $shareId;
376
+
377
+    }
378
+
379
+    /**
380
+     * @param string $filename
381
+     * @param string $link
382
+     * @param string $initiator
383
+     * @param string $shareWith
384
+     * @param \DateTime|null $expiration
385
+     * @throws \Exception If mail couldn't be sent
386
+     */
387
+    protected function sendMailNotification($filename,
388
+                                            $link,
389
+                                            $initiator,
390
+                                            $shareWith,
391
+                                            \DateTime $expiration = null) {
392
+        $initiatorUser = $this->userManager->get($initiator);
393
+        $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
394
+        $message = $this->mailer->createMessage();
395
+
396
+        $emailTemplate = $this->mailer->createEMailTemplate('sharebymail.RecipientNotification', [
397
+            'filename' => $filename,
398
+            'link' => $link,
399
+            'initiator' => $initiatorDisplayName,
400
+            'expiration' => $expiration,
401
+            'shareWith' => $shareWith,
402
+        ]);
403
+
404
+        $emailTemplate->setSubject($this->l->t('%1$s shared »%2$s« with you', array($initiatorDisplayName, $filename)));
405
+        $emailTemplate->addHeader();
406
+        $emailTemplate->addHeading($this->l->t('%1$s shared »%2$s« with you', [$initiatorDisplayName, $filename]), false);
407
+        $text = $this->l->t('%1$s shared »%2$s« with you.', [$initiatorDisplayName, $filename]);
408
+
409
+        $emailTemplate->addBodyText(
410
+            htmlspecialchars($text . ' ' . $this->l->t('Click the button below to open it.')),
411
+            $text
412
+        );
413
+        $emailTemplate->addBodyButton(
414
+            $this->l->t('Open »%s«', [$filename]),
415
+            $link
416
+        );
417
+
418
+        $message->setTo([$shareWith]);
419
+
420
+        // The "From" contains the sharers name
421
+        $instanceName = $this->defaults->getName();
422
+        $senderName = $this->l->t(
423
+            '%1$s via %2$s',
424
+            [
425
+                $initiatorDisplayName,
426
+                $instanceName
427
+            ]
428
+        );
429
+        $message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
430
+
431
+        // The "Reply-To" is set to the sharer if an mail address is configured
432
+        // also the default footer contains a "Do not reply" which needs to be adjusted.
433
+        $initiatorEmail = $initiatorUser->getEMailAddress();
434
+        if($initiatorEmail !== null) {
435
+            $message->setReplyTo([$initiatorEmail => $initiatorDisplayName]);
436
+            $emailTemplate->addFooter($instanceName . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : ''));
437
+        } else {
438
+            $emailTemplate->addFooter();
439
+        }
440
+
441
+        $message->useTemplate($emailTemplate);
442
+        $this->mailer->send($message);
443
+    }
444
+
445
+    /**
446
+     * send password to recipient of a mail share
447
+     *
448
+     * @param IShare $share
449
+     * @param string $password
450
+     * @return bool
451
+     */
452
+    protected function sendPassword(IShare $share, $password) {
453
+
454
+        $filename = $share->getNode()->getName();
455
+        $initiator = $share->getSharedBy();
456
+        $shareWith = $share->getSharedWith();
457
+
458
+        if ($password === '' || $this->settingsManager->sendPasswordByMail() === false || $share->getSendPasswordByTalk()) {
459
+            return false;
460
+        }
461
+
462
+        $initiatorUser = $this->userManager->get($initiator);
463
+        $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
464
+        $initiatorEmailAddress = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null;
465
+
466
+        $plainBodyPart = $this->l->t("%1\$s shared »%2\$s« with you.\nYou should have already received a separate mail with a link to access it.\n", [$initiatorDisplayName, $filename]);
467
+        $htmlBodyPart = $this->l->t('%1$s shared »%2$s« with you. You should have already received a separate mail with a link to access it.', [$initiatorDisplayName, $filename]);
468
+
469
+        $message = $this->mailer->createMessage();
470
+
471
+        $emailTemplate = $this->mailer->createEMailTemplate('sharebymail.RecipientPasswordNotification', [
472
+            'filename' => $filename,
473
+            'password' => $password,
474
+            'initiator' => $initiatorDisplayName,
475
+            'initiatorEmail' => $initiatorEmailAddress,
476
+            'shareWith' => $shareWith,
477
+        ]);
478
+
479
+        $emailTemplate->setSubject($this->l->t('Password to access »%1$s« shared to you by %2$s', [$filename, $initiatorDisplayName]));
480
+        $emailTemplate->addHeader();
481
+        $emailTemplate->addHeading($this->l->t('Password to access »%s«', [$filename]), false);
482
+        $emailTemplate->addBodyText(htmlspecialchars($htmlBodyPart), $plainBodyPart);
483
+        $emailTemplate->addBodyText($this->l->t('It is protected with the following password:'));
484
+        $emailTemplate->addBodyText($password);
485
+
486
+        // The "From" contains the sharers name
487
+        $instanceName = $this->defaults->getName();
488
+        $senderName = $this->l->t(
489
+            '%1$s via %2$s',
490
+            [
491
+                $initiatorDisplayName,
492
+                $instanceName
493
+            ]
494
+        );
495
+        $message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
496
+        if ($initiatorEmailAddress !== null) {
497
+            $message->setReplyTo([$initiatorEmailAddress => $initiatorDisplayName]);
498
+            $emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
499
+        } else {
500
+            $emailTemplate->addFooter();
501
+        }
502
+
503
+        $message->setTo([$shareWith]);
504
+        $message->useTemplate($emailTemplate);
505
+        $this->mailer->send($message);
506
+
507
+        $this->createPasswordSendActivity($share, $shareWith, false);
508
+
509
+        return true;
510
+    }
511
+
512
+    protected function sendNote(IShare $share) {
513
+
514
+        $recipient = $share->getSharedWith();
515
+
516
+
517
+        $filename = $share->getNode()->getName();
518
+        $initiator = $share->getSharedBy();
519
+        $note = $share->getNote();
520
+
521
+        $initiatorUser = $this->userManager->get($initiator);
522
+        $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
523
+        $initiatorEmailAddress = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null;
524
+
525
+        $plainHeading = $this->l->t('%1$s shared »%2$s« with you and wants to add:', [$initiatorDisplayName, $filename]);
526
+        $htmlHeading = $this->l->t('%1$s shared »%2$s« with you and wants to add', [$initiatorDisplayName, $filename]);
527
+
528
+        $message = $this->mailer->createMessage();
529
+
530
+        $emailTemplate = $this->mailer->createEMailTemplate('shareByMail.sendNote');
531
+
532
+        $emailTemplate->setSubject($this->l->t('»%s« added a note to a file shared with you', [$initiatorDisplayName]));
533
+        $emailTemplate->addHeader();
534
+        $emailTemplate->addHeading(htmlspecialchars($htmlHeading), $plainHeading);
535
+        $emailTemplate->addBodyText(htmlspecialchars($note), $note);
536
+
537
+        $link = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare',
538
+            ['token' => $share->getToken()]);
539
+        $emailTemplate->addBodyButton(
540
+            $this->l->t('Open »%s«', [$filename]),
541
+            $link
542
+        );
543
+
544
+        // The "From" contains the sharers name
545
+        $instanceName = $this->defaults->getName();
546
+        $senderName = $this->l->t(
547
+            '%1$s via %2$s',
548
+            [
549
+                $initiatorDisplayName,
550
+                $instanceName
551
+            ]
552
+        );
553
+        $message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
554
+        if ($initiatorEmailAddress !== null) {
555
+            $message->setReplyTo([$initiatorEmailAddress => $initiatorDisplayName]);
556
+            $emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
557
+        } else {
558
+            $emailTemplate->addFooter();
559
+        }
560
+
561
+        $message->setTo([$recipient]);
562
+        $message->useTemplate($emailTemplate);
563
+        $this->mailer->send($message);
564
+
565
+    }
566
+
567
+    /**
568
+     * send auto generated password to the owner. This happens if the admin enforces
569
+     * a password for mail shares and forbid to send the password by mail to the recipient
570
+     *
571
+     * @param IShare $share
572
+     * @param string $password
573
+     * @return bool
574
+     * @throws \Exception
575
+     */
576
+    protected function sendPasswordToOwner(IShare $share, $password) {
577
+
578
+        $filename = $share->getNode()->getName();
579
+        $initiator = $this->userManager->get($share->getSharedBy());
580
+        $initiatorEMailAddress = ($initiator instanceof IUser) ? $initiator->getEMailAddress() : null;
581
+        $initiatorDisplayName = ($initiator instanceof IUser) ? $initiator->getDisplayName() : $share->getSharedBy();
582
+        $shareWith = $share->getSharedWith();
583
+
584
+        if ($initiatorEMailAddress === null) {
585
+            throw new \Exception(
586
+                $this->l->t("We can't send you the auto-generated password. Please set a valid email address in your personal settings and try again.")
587
+            );
588
+        }
589
+
590
+        $bodyPart = $this->l->t('You just shared »%1$s« with %2$s. The share was already sent to the recipient. Due to the security policies defined by the administrator of %3$s each share needs to be protected by password and it is not allowed to send the password directly to the recipient. Therefore you need to forward the password manually to the recipient.', [$filename, $shareWith, $this->defaults->getName()]);
591
+
592
+        $message = $this->mailer->createMessage();
593
+        $emailTemplate = $this->mailer->createEMailTemplate('sharebymail.OwnerPasswordNotification', [
594
+            'filename' => $filename,
595
+            'password' => $password,
596
+            'initiator' => $initiatorDisplayName,
597
+            'initiatorEmail' => $initiatorEMailAddress,
598
+            'shareWith' => $shareWith,
599
+        ]);
600
+
601
+        $emailTemplate->setSubject($this->l->t('Password to access »%1$s« shared by you with %2$s', [$filename, $shareWith]));
602
+        $emailTemplate->addHeader();
603
+        $emailTemplate->addHeading($this->l->t('Password to access »%s«', [$filename]), false);
604
+        $emailTemplate->addBodyText($bodyPart);
605
+        $emailTemplate->addBodyText($this->l->t('This is the password:'));
606
+        $emailTemplate->addBodyText($password);
607
+        $emailTemplate->addBodyText($this->l->t('You can choose a different password at any time in the share dialog.'));
608
+        $emailTemplate->addFooter();
609
+
610
+        if ($initiatorEMailAddress) {
611
+            $message->setFrom([$initiatorEMailAddress => $initiatorDisplayName]);
612
+        }
613
+        $message->setTo([$initiatorEMailAddress => $initiatorDisplayName]);
614
+        $message->useTemplate($emailTemplate);
615
+        $this->mailer->send($message);
616
+
617
+        $this->createPasswordSendActivity($share, $shareWith, true);
618
+
619
+        return true;
620
+    }
621
+
622
+    /**
623
+     * generate share token
624
+     *
625
+     * @return string
626
+     */
627
+    protected function generateToken($size = 15) {
628
+        $token = $this->secureRandom->generate($size, ISecureRandom::CHAR_HUMAN_READABLE);
629
+        return $token;
630
+    }
631
+
632
+    /**
633
+     * Get all children of this share
634
+     *
635
+     * @param IShare $parent
636
+     * @return IShare[]
637
+     */
638
+    public function getChildren(IShare $parent) {
639
+        $children = [];
640
+
641
+        $qb = $this->dbConnection->getQueryBuilder();
642
+        $qb->select('*')
643
+            ->from('share')
644
+            ->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId())))
645
+            ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)))
646
+            ->orderBy('id');
647
+
648
+        $cursor = $qb->execute();
649
+        while($data = $cursor->fetch()) {
650
+            $children[] = $this->createShareObject($data);
651
+        }
652
+        $cursor->closeCursor();
653
+
654
+        return $children;
655
+    }
656
+
657
+    /**
658
+     * add share to the database and return the ID
659
+     *
660
+     * @param int $itemSource
661
+     * @param string $itemType
662
+     * @param string $shareWith
663
+     * @param string $sharedBy
664
+     * @param string $uidOwner
665
+     * @param int $permissions
666
+     * @param string $token
667
+     * @param string $password
668
+     * @param bool $sendPasswordByTalk
669
+     * @return int
670
+     */
671
+    protected function addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, $token, $password, $sendPasswordByTalk) {
672
+        $qb = $this->dbConnection->getQueryBuilder();
673
+        $qb->insert('share')
674
+            ->setValue('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL))
675
+            ->setValue('item_type', $qb->createNamedParameter($itemType))
676
+            ->setValue('item_source', $qb->createNamedParameter($itemSource))
677
+            ->setValue('file_source', $qb->createNamedParameter($itemSource))
678
+            ->setValue('share_with', $qb->createNamedParameter($shareWith))
679
+            ->setValue('uid_owner', $qb->createNamedParameter($uidOwner))
680
+            ->setValue('uid_initiator', $qb->createNamedParameter($sharedBy))
681
+            ->setValue('permissions', $qb->createNamedParameter($permissions))
682
+            ->setValue('token', $qb->createNamedParameter($token))
683
+            ->setValue('password', $qb->createNamedParameter($password))
684
+            ->setValue('password_by_talk', $qb->createNamedParameter($sendPasswordByTalk, IQueryBuilder::PARAM_BOOL))
685
+            ->setValue('stime', $qb->createNamedParameter(time()));
686
+
687
+        /*
688 688
 		 * Added to fix https://github.com/owncloud/core/issues/22215
689 689
 		 * Can be removed once we get rid of ajax/share.php
690 690
 		 */
691
-		$qb->setValue('file_target', $qb->createNamedParameter(''));
691
+        $qb->setValue('file_target', $qb->createNamedParameter(''));
692 692
 
693
-		$qb->execute();
694
-		$id = $qb->getLastInsertId();
693
+        $qb->execute();
694
+        $id = $qb->getLastInsertId();
695 695
 
696
-		return (int)$id;
697
-	}
696
+        return (int)$id;
697
+    }
698 698
 
699
-	/**
700
-	 * Update a share
701
-	 *
702
-	 * @param IShare $share
703
-	 * @param string|null $plainTextPassword
704
-	 * @return IShare The share object
705
-	 */
706
-	public function update(IShare $share, $plainTextPassword = null) {
699
+    /**
700
+     * Update a share
701
+     *
702
+     * @param IShare $share
703
+     * @param string|null $plainTextPassword
704
+     * @return IShare The share object
705
+     */
706
+    public function update(IShare $share, $plainTextPassword = null) {
707 707
 
708
-		$originalShare = $this->getShareById($share->getId());
708
+        $originalShare = $this->getShareById($share->getId());
709 709
 
710
-		// a real password was given
711
-		$validPassword = $plainTextPassword !== null && $plainTextPassword !== '';
710
+        // a real password was given
711
+        $validPassword = $plainTextPassword !== null && $plainTextPassword !== '';
712 712
 
713
-		if($validPassword && ($originalShare->getPassword() !== $share->getPassword() ||
714
-								($originalShare->getSendPasswordByTalk() && !$share->getSendPasswordByTalk()))) {
715
-			$this->sendPassword($share, $plainTextPassword);
716
-		}
717
-		/*
713
+        if($validPassword && ($originalShare->getPassword() !== $share->getPassword() ||
714
+                                ($originalShare->getSendPasswordByTalk() && !$share->getSendPasswordByTalk()))) {
715
+            $this->sendPassword($share, $plainTextPassword);
716
+        }
717
+        /*
718 718
 		 * We allow updating the permissions and password of mail shares
719 719
 		 */
720
-		$qb = $this->dbConnection->getQueryBuilder();
721
-		$qb->update('share')
722
-			->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
723
-			->set('permissions', $qb->createNamedParameter($share->getPermissions()))
724
-			->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
725
-			->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
726
-			->set('password', $qb->createNamedParameter($share->getPassword()))
727
-			->set('password_by_talk', $qb->createNamedParameter($share->getSendPasswordByTalk(), IQueryBuilder::PARAM_BOOL))
728
-			->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
729
-			->set('note', $qb->createNamedParameter($share->getNote()))
730
-			->execute();
731
-
732
-		if ($originalShare->getNote() !== $share->getNote() && $share->getNote() !== '') {
733
-			$this->sendNote($share);
734
-		}
735
-
736
-		return $share;
737
-	}
738
-
739
-	/**
740
-	 * @inheritdoc
741
-	 */
742
-	public function move(IShare $share, $recipient) {
743
-		/**
744
-		 * nothing to do here, mail shares are only outgoing shares
745
-		 */
746
-		return $share;
747
-	}
748
-
749
-	/**
750
-	 * Delete a share (owner unShares the file)
751
-	 *
752
-	 * @param IShare $share
753
-	 */
754
-	public function delete(IShare $share) {
755
-		try {
756
-			$this->createShareActivity($share, 'unshare');
757
-		} catch (\Exception $e) {
758
-		}
759
-
760
-		$this->removeShareFromTable($share->getId());
761
-	}
762
-
763
-	/**
764
-	 * @inheritdoc
765
-	 */
766
-	public function deleteFromSelf(IShare $share, $recipient) {
767
-		// nothing to do here, mail shares are only outgoing shares
768
-	}
769
-
770
-	public function restore(IShare $share, string $recipient): IShare {
771
-		throw new GenericShareException('not implemented');
772
-	}
773
-
774
-	/**
775
-	 * @inheritdoc
776
-	 */
777
-	public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) {
778
-		$qb = $this->dbConnection->getQueryBuilder();
779
-		$qb->select('*')
780
-			->from('share');
781
-
782
-		$qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)));
783
-
784
-		/**
785
-		 * Reshares for this user are shares where they are the owner.
786
-		 */
787
-		if ($reshares === false) {
788
-			//Special case for old shares created via the web UI
789
-			$or1 = $qb->expr()->andX(
790
-				$qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
791
-				$qb->expr()->isNull('uid_initiator')
792
-			);
793
-
794
-			$qb->andWhere(
795
-				$qb->expr()->orX(
796
-					$qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)),
797
-					$or1
798
-				)
799
-			);
800
-		} else {
801
-			$qb->andWhere(
802
-				$qb->expr()->orX(
803
-					$qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
804
-					$qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
805
-				)
806
-			);
807
-		}
808
-
809
-		if ($node !== null) {
810
-			$qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
811
-		}
812
-
813
-		if ($limit !== -1) {
814
-			$qb->setMaxResults($limit);
815
-		}
816
-
817
-		$qb->setFirstResult($offset);
818
-		$qb->orderBy('id');
819
-
820
-		$cursor = $qb->execute();
821
-		$shares = [];
822
-		while($data = $cursor->fetch()) {
823
-			$shares[] = $this->createShareObject($data);
824
-		}
825
-		$cursor->closeCursor();
826
-
827
-		return $shares;
828
-	}
829
-
830
-	/**
831
-	 * @inheritdoc
832
-	 */
833
-	public function getShareById($id, $recipientId = null) {
834
-		$qb = $this->dbConnection->getQueryBuilder();
835
-
836
-		$qb->select('*')
837
-			->from('share')
838
-			->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
839
-			->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)));
840
-
841
-		$cursor = $qb->execute();
842
-		$data = $cursor->fetch();
843
-		$cursor->closeCursor();
844
-
845
-		if ($data === false) {
846
-			throw new ShareNotFound();
847
-		}
848
-
849
-		try {
850
-			$share = $this->createShareObject($data);
851
-		} catch (InvalidShare $e) {
852
-			throw new ShareNotFound();
853
-		}
854
-
855
-		return $share;
856
-	}
857
-
858
-	/**
859
-	 * Get shares for a given path
860
-	 *
861
-	 * @param \OCP\Files\Node $path
862
-	 * @return IShare[]
863
-	 */
864
-	public function getSharesByPath(Node $path) {
865
-		$qb = $this->dbConnection->getQueryBuilder();
866
-
867
-		$cursor = $qb->select('*')
868
-			->from('share')
869
-			->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId())))
870
-			->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)))
871
-			->execute();
872
-
873
-		$shares = [];
874
-		while($data = $cursor->fetch()) {
875
-			$shares[] = $this->createShareObject($data);
876
-		}
877
-		$cursor->closeCursor();
878
-
879
-		return $shares;
880
-	}
881
-
882
-	/**
883
-	 * @inheritdoc
884
-	 */
885
-	public function getSharedWith($userId, $shareType, $node, $limit, $offset) {
886
-		/** @var IShare[] $shares */
887
-		$shares = [];
888
-
889
-		//Get shares directly with this user
890
-		$qb = $this->dbConnection->getQueryBuilder();
891
-		$qb->select('*')
892
-			->from('share');
893
-
894
-		// Order by id
895
-		$qb->orderBy('id');
896
-
897
-		// Set limit and offset
898
-		if ($limit !== -1) {
899
-			$qb->setMaxResults($limit);
900
-		}
901
-		$qb->setFirstResult($offset);
902
-
903
-		$qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)));
904
-		$qb->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)));
905
-
906
-		// Filter by node if provided
907
-		if ($node !== null) {
908
-			$qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
909
-		}
910
-
911
-		$cursor = $qb->execute();
912
-
913
-		while($data = $cursor->fetch()) {
914
-			$shares[] = $this->createShareObject($data);
915
-		}
916
-		$cursor->closeCursor();
917
-
918
-
919
-		return $shares;
920
-	}
921
-
922
-	/**
923
-	 * Get a share by token
924
-	 *
925
-	 * @param string $token
926
-	 * @return IShare
927
-	 * @throws ShareNotFound
928
-	 */
929
-	public function getShareByToken($token) {
930
-		$qb = $this->dbConnection->getQueryBuilder();
931
-
932
-		$cursor = $qb->select('*')
933
-			->from('share')
934
-			->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)))
935
-			->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token)))
936
-			->execute();
937
-
938
-		$data = $cursor->fetch();
939
-
940
-		if ($data === false) {
941
-			throw new ShareNotFound('Share not found', $this->l->t('Could not find share'));
942
-		}
943
-
944
-		try {
945
-			$share = $this->createShareObject($data);
946
-		} catch (InvalidShare $e) {
947
-			throw new ShareNotFound('Share not found', $this->l->t('Could not find share'));
948
-		}
949
-
950
-		return $share;
951
-	}
952
-
953
-	/**
954
-	 * remove share from table
955
-	 *
956
-	 * @param string $shareId
957
-	 */
958
-	protected function removeShareFromTable($shareId) {
959
-		$qb = $this->dbConnection->getQueryBuilder();
960
-		$qb->delete('share')
961
-			->where($qb->expr()->eq('id', $qb->createNamedParameter($shareId)));
962
-		$qb->execute();
963
-	}
964
-
965
-	/**
966
-	 * Create a share object from an database row
967
-	 *
968
-	 * @param array $data
969
-	 * @return IShare
970
-	 * @throws InvalidShare
971
-	 * @throws ShareNotFound
972
-	 */
973
-	protected function createShareObject($data) {
974
-
975
-		$share = new Share($this->rootFolder, $this->userManager);
976
-		$share->setId((int)$data['id'])
977
-			->setShareType((int)$data['share_type'])
978
-			->setPermissions((int)$data['permissions'])
979
-			->setTarget($data['file_target'])
980
-			->setMailSend((bool)$data['mail_send'])
981
-			->setNote($data['note'])
982
-			->setToken($data['token']);
983
-
984
-		$shareTime = new \DateTime();
985
-		$shareTime->setTimestamp((int)$data['stime']);
986
-		$share->setShareTime($shareTime);
987
-		$share->setSharedWith($data['share_with']);
988
-		$share->setPassword($data['password']);
989
-		$share->setSendPasswordByTalk((bool)$data['password_by_talk']);
990
-
991
-		if ($data['uid_initiator'] !== null) {
992
-			$share->setShareOwner($data['uid_owner']);
993
-			$share->setSharedBy($data['uid_initiator']);
994
-		} else {
995
-			//OLD SHARE
996
-			$share->setSharedBy($data['uid_owner']);
997
-			$path = $this->getNode($share->getSharedBy(), (int)$data['file_source']);
998
-
999
-			$owner = $path->getOwner();
1000
-			$share->setShareOwner($owner->getUID());
1001
-		}
1002
-
1003
-		if ($data['expiration'] !== null) {
1004
-			$expiration = \DateTime::createFromFormat('Y-m-d H:i:s', $data['expiration']);
1005
-			if ($expiration !== false) {
1006
-				$share->setExpirationDate($expiration);
1007
-			}
1008
-		}
1009
-
1010
-		$share->setNodeId((int)$data['file_source']);
1011
-		$share->setNodeType($data['item_type']);
1012
-
1013
-		$share->setProviderId($this->identifier());
1014
-
1015
-		return $share;
1016
-	}
1017
-
1018
-	/**
1019
-	 * Get the node with file $id for $user
1020
-	 *
1021
-	 * @param string $userId
1022
-	 * @param int $id
1023
-	 * @return \OCP\Files\File|\OCP\Files\Folder
1024
-	 * @throws InvalidShare
1025
-	 */
1026
-	private function getNode($userId, $id) {
1027
-		try {
1028
-			$userFolder = $this->rootFolder->getUserFolder($userId);
1029
-		} catch (NoUserException $e) {
1030
-			throw new InvalidShare();
1031
-		}
1032
-
1033
-		$nodes = $userFolder->getById($id);
1034
-
1035
-		if (empty($nodes)) {
1036
-			throw new InvalidShare();
1037
-		}
1038
-
1039
-		return $nodes[0];
1040
-	}
1041
-
1042
-	/**
1043
-	 * A user is deleted from the system
1044
-	 * So clean up the relevant shares.
1045
-	 *
1046
-	 * @param string $uid
1047
-	 * @param int $shareType
1048
-	 */
1049
-	public function userDeleted($uid, $shareType) {
1050
-		$qb = $this->dbConnection->getQueryBuilder();
1051
-
1052
-		$qb->delete('share')
1053
-			->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)))
1054
-			->andWhere($qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)))
1055
-			->execute();
1056
-	}
1057
-
1058
-	/**
1059
-	 * This provider does not support group shares
1060
-	 *
1061
-	 * @param string $gid
1062
-	 */
1063
-	public function groupDeleted($gid) {
1064
-	}
1065
-
1066
-	/**
1067
-	 * This provider does not support group shares
1068
-	 *
1069
-	 * @param string $uid
1070
-	 * @param string $gid
1071
-	 */
1072
-	public function userDeletedFromGroup($uid, $gid) {
1073
-	}
1074
-
1075
-	/**
1076
-	 * get database row of a give share
1077
-	 *
1078
-	 * @param $id
1079
-	 * @return array
1080
-	 * @throws ShareNotFound
1081
-	 */
1082
-	protected function getRawShare($id) {
1083
-
1084
-		// Now fetch the inserted share and create a complete share object
1085
-		$qb = $this->dbConnection->getQueryBuilder();
1086
-		$qb->select('*')
1087
-			->from('share')
1088
-			->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
1089
-
1090
-		$cursor = $qb->execute();
1091
-		$data = $cursor->fetch();
1092
-		$cursor->closeCursor();
1093
-
1094
-		if ($data === false) {
1095
-			throw new ShareNotFound;
1096
-		}
1097
-
1098
-		return $data;
1099
-	}
1100
-
1101
-	public function getSharesInFolder($userId, Folder $node, $reshares) {
1102
-		$qb = $this->dbConnection->getQueryBuilder();
1103
-		$qb->select('*')
1104
-			->from('share', 's')
1105
-			->andWhere($qb->expr()->orX(
1106
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
1107
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
1108
-			))
1109
-			->andWhere(
1110
-				$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL))
1111
-			);
1112
-
1113
-		/**
1114
-		 * Reshares for this user are shares where they are the owner.
1115
-		 */
1116
-		if ($reshares === false) {
1117
-			$qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
1118
-		} else {
1119
-			$qb->andWhere(
1120
-				$qb->expr()->orX(
1121
-					$qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
1122
-					$qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
1123
-				)
1124
-			);
1125
-		}
1126
-
1127
-		$qb->innerJoin('s', 'filecache' ,'f', $qb->expr()->eq('s.file_source', 'f.fileid'));
1128
-		$qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())));
1129
-
1130
-		$qb->orderBy('id');
1131
-
1132
-		$cursor = $qb->execute();
1133
-		$shares = [];
1134
-		while ($data = $cursor->fetch()) {
1135
-			$shares[$data['fileid']][] = $this->createShareObject($data);
1136
-		}
1137
-		$cursor->closeCursor();
1138
-
1139
-		return $shares;
1140
-	}
1141
-
1142
-	/**
1143
-	 * @inheritdoc
1144
-	 */
1145
-	public function getAccessList($nodes, $currentAccess) {
1146
-		$ids = [];
1147
-		foreach ($nodes as $node) {
1148
-			$ids[] = $node->getId();
1149
-		}
1150
-
1151
-		$qb = $this->dbConnection->getQueryBuilder();
1152
-		$qb->select('share_with')
1153
-			->from('share')
1154
-			->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)))
1155
-			->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
1156
-			->andWhere($qb->expr()->orX(
1157
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
1158
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
1159
-			))
1160
-			->setMaxResults(1);
1161
-		$cursor = $qb->execute();
1162
-
1163
-		$mail = $cursor->fetch() !== false;
1164
-		$cursor->closeCursor();
1165
-
1166
-		return ['public' => $mail];
1167
-	}
1168
-
1169
-	public function getAllShares(): iterable {
1170
-		$qb = $this->dbConnection->getQueryBuilder();
1171
-
1172
-		$qb->select('*')
1173
-			->from('share')
1174
-			->where(
1175
-				$qb->expr()->orX(
1176
-					$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_EMAIL))
1177
-				)
1178
-			);
1179
-
1180
-		$cursor = $qb->execute();
1181
-		while($data = $cursor->fetch()) {
1182
-			try {
1183
-				$share = $this->createShareObject($data);
1184
-			} catch (InvalidShare $e) {
1185
-				continue;
1186
-			} catch (ShareNotFound $e) {
1187
-				continue;
1188
-			}
1189
-
1190
-			yield $share;
1191
-		}
1192
-		$cursor->closeCursor();
1193
-	}
720
+        $qb = $this->dbConnection->getQueryBuilder();
721
+        $qb->update('share')
722
+            ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
723
+            ->set('permissions', $qb->createNamedParameter($share->getPermissions()))
724
+            ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
725
+            ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
726
+            ->set('password', $qb->createNamedParameter($share->getPassword()))
727
+            ->set('password_by_talk', $qb->createNamedParameter($share->getSendPasswordByTalk(), IQueryBuilder::PARAM_BOOL))
728
+            ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
729
+            ->set('note', $qb->createNamedParameter($share->getNote()))
730
+            ->execute();
731
+
732
+        if ($originalShare->getNote() !== $share->getNote() && $share->getNote() !== '') {
733
+            $this->sendNote($share);
734
+        }
735
+
736
+        return $share;
737
+    }
738
+
739
+    /**
740
+     * @inheritdoc
741
+     */
742
+    public function move(IShare $share, $recipient) {
743
+        /**
744
+         * nothing to do here, mail shares are only outgoing shares
745
+         */
746
+        return $share;
747
+    }
748
+
749
+    /**
750
+     * Delete a share (owner unShares the file)
751
+     *
752
+     * @param IShare $share
753
+     */
754
+    public function delete(IShare $share) {
755
+        try {
756
+            $this->createShareActivity($share, 'unshare');
757
+        } catch (\Exception $e) {
758
+        }
759
+
760
+        $this->removeShareFromTable($share->getId());
761
+    }
762
+
763
+    /**
764
+     * @inheritdoc
765
+     */
766
+    public function deleteFromSelf(IShare $share, $recipient) {
767
+        // nothing to do here, mail shares are only outgoing shares
768
+    }
769
+
770
+    public function restore(IShare $share, string $recipient): IShare {
771
+        throw new GenericShareException('not implemented');
772
+    }
773
+
774
+    /**
775
+     * @inheritdoc
776
+     */
777
+    public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) {
778
+        $qb = $this->dbConnection->getQueryBuilder();
779
+        $qb->select('*')
780
+            ->from('share');
781
+
782
+        $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)));
783
+
784
+        /**
785
+         * Reshares for this user are shares where they are the owner.
786
+         */
787
+        if ($reshares === false) {
788
+            //Special case for old shares created via the web UI
789
+            $or1 = $qb->expr()->andX(
790
+                $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
791
+                $qb->expr()->isNull('uid_initiator')
792
+            );
793
+
794
+            $qb->andWhere(
795
+                $qb->expr()->orX(
796
+                    $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)),
797
+                    $or1
798
+                )
799
+            );
800
+        } else {
801
+            $qb->andWhere(
802
+                $qb->expr()->orX(
803
+                    $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
804
+                    $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
805
+                )
806
+            );
807
+        }
808
+
809
+        if ($node !== null) {
810
+            $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
811
+        }
812
+
813
+        if ($limit !== -1) {
814
+            $qb->setMaxResults($limit);
815
+        }
816
+
817
+        $qb->setFirstResult($offset);
818
+        $qb->orderBy('id');
819
+
820
+        $cursor = $qb->execute();
821
+        $shares = [];
822
+        while($data = $cursor->fetch()) {
823
+            $shares[] = $this->createShareObject($data);
824
+        }
825
+        $cursor->closeCursor();
826
+
827
+        return $shares;
828
+    }
829
+
830
+    /**
831
+     * @inheritdoc
832
+     */
833
+    public function getShareById($id, $recipientId = null) {
834
+        $qb = $this->dbConnection->getQueryBuilder();
835
+
836
+        $qb->select('*')
837
+            ->from('share')
838
+            ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
839
+            ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)));
840
+
841
+        $cursor = $qb->execute();
842
+        $data = $cursor->fetch();
843
+        $cursor->closeCursor();
844
+
845
+        if ($data === false) {
846
+            throw new ShareNotFound();
847
+        }
848
+
849
+        try {
850
+            $share = $this->createShareObject($data);
851
+        } catch (InvalidShare $e) {
852
+            throw new ShareNotFound();
853
+        }
854
+
855
+        return $share;
856
+    }
857
+
858
+    /**
859
+     * Get shares for a given path
860
+     *
861
+     * @param \OCP\Files\Node $path
862
+     * @return IShare[]
863
+     */
864
+    public function getSharesByPath(Node $path) {
865
+        $qb = $this->dbConnection->getQueryBuilder();
866
+
867
+        $cursor = $qb->select('*')
868
+            ->from('share')
869
+            ->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId())))
870
+            ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)))
871
+            ->execute();
872
+
873
+        $shares = [];
874
+        while($data = $cursor->fetch()) {
875
+            $shares[] = $this->createShareObject($data);
876
+        }
877
+        $cursor->closeCursor();
878
+
879
+        return $shares;
880
+    }
881
+
882
+    /**
883
+     * @inheritdoc
884
+     */
885
+    public function getSharedWith($userId, $shareType, $node, $limit, $offset) {
886
+        /** @var IShare[] $shares */
887
+        $shares = [];
888
+
889
+        //Get shares directly with this user
890
+        $qb = $this->dbConnection->getQueryBuilder();
891
+        $qb->select('*')
892
+            ->from('share');
893
+
894
+        // Order by id
895
+        $qb->orderBy('id');
896
+
897
+        // Set limit and offset
898
+        if ($limit !== -1) {
899
+            $qb->setMaxResults($limit);
900
+        }
901
+        $qb->setFirstResult($offset);
902
+
903
+        $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)));
904
+        $qb->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)));
905
+
906
+        // Filter by node if provided
907
+        if ($node !== null) {
908
+            $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
909
+        }
910
+
911
+        $cursor = $qb->execute();
912
+
913
+        while($data = $cursor->fetch()) {
914
+            $shares[] = $this->createShareObject($data);
915
+        }
916
+        $cursor->closeCursor();
917
+
918
+
919
+        return $shares;
920
+    }
921
+
922
+    /**
923
+     * Get a share by token
924
+     *
925
+     * @param string $token
926
+     * @return IShare
927
+     * @throws ShareNotFound
928
+     */
929
+    public function getShareByToken($token) {
930
+        $qb = $this->dbConnection->getQueryBuilder();
931
+
932
+        $cursor = $qb->select('*')
933
+            ->from('share')
934
+            ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)))
935
+            ->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token)))
936
+            ->execute();
937
+
938
+        $data = $cursor->fetch();
939
+
940
+        if ($data === false) {
941
+            throw new ShareNotFound('Share not found', $this->l->t('Could not find share'));
942
+        }
943
+
944
+        try {
945
+            $share = $this->createShareObject($data);
946
+        } catch (InvalidShare $e) {
947
+            throw new ShareNotFound('Share not found', $this->l->t('Could not find share'));
948
+        }
949
+
950
+        return $share;
951
+    }
952
+
953
+    /**
954
+     * remove share from table
955
+     *
956
+     * @param string $shareId
957
+     */
958
+    protected function removeShareFromTable($shareId) {
959
+        $qb = $this->dbConnection->getQueryBuilder();
960
+        $qb->delete('share')
961
+            ->where($qb->expr()->eq('id', $qb->createNamedParameter($shareId)));
962
+        $qb->execute();
963
+    }
964
+
965
+    /**
966
+     * Create a share object from an database row
967
+     *
968
+     * @param array $data
969
+     * @return IShare
970
+     * @throws InvalidShare
971
+     * @throws ShareNotFound
972
+     */
973
+    protected function createShareObject($data) {
974
+
975
+        $share = new Share($this->rootFolder, $this->userManager);
976
+        $share->setId((int)$data['id'])
977
+            ->setShareType((int)$data['share_type'])
978
+            ->setPermissions((int)$data['permissions'])
979
+            ->setTarget($data['file_target'])
980
+            ->setMailSend((bool)$data['mail_send'])
981
+            ->setNote($data['note'])
982
+            ->setToken($data['token']);
983
+
984
+        $shareTime = new \DateTime();
985
+        $shareTime->setTimestamp((int)$data['stime']);
986
+        $share->setShareTime($shareTime);
987
+        $share->setSharedWith($data['share_with']);
988
+        $share->setPassword($data['password']);
989
+        $share->setSendPasswordByTalk((bool)$data['password_by_talk']);
990
+
991
+        if ($data['uid_initiator'] !== null) {
992
+            $share->setShareOwner($data['uid_owner']);
993
+            $share->setSharedBy($data['uid_initiator']);
994
+        } else {
995
+            //OLD SHARE
996
+            $share->setSharedBy($data['uid_owner']);
997
+            $path = $this->getNode($share->getSharedBy(), (int)$data['file_source']);
998
+
999
+            $owner = $path->getOwner();
1000
+            $share->setShareOwner($owner->getUID());
1001
+        }
1002
+
1003
+        if ($data['expiration'] !== null) {
1004
+            $expiration = \DateTime::createFromFormat('Y-m-d H:i:s', $data['expiration']);
1005
+            if ($expiration !== false) {
1006
+                $share->setExpirationDate($expiration);
1007
+            }
1008
+        }
1009
+
1010
+        $share->setNodeId((int)$data['file_source']);
1011
+        $share->setNodeType($data['item_type']);
1012
+
1013
+        $share->setProviderId($this->identifier());
1014
+
1015
+        return $share;
1016
+    }
1017
+
1018
+    /**
1019
+     * Get the node with file $id for $user
1020
+     *
1021
+     * @param string $userId
1022
+     * @param int $id
1023
+     * @return \OCP\Files\File|\OCP\Files\Folder
1024
+     * @throws InvalidShare
1025
+     */
1026
+    private function getNode($userId, $id) {
1027
+        try {
1028
+            $userFolder = $this->rootFolder->getUserFolder($userId);
1029
+        } catch (NoUserException $e) {
1030
+            throw new InvalidShare();
1031
+        }
1032
+
1033
+        $nodes = $userFolder->getById($id);
1034
+
1035
+        if (empty($nodes)) {
1036
+            throw new InvalidShare();
1037
+        }
1038
+
1039
+        return $nodes[0];
1040
+    }
1041
+
1042
+    /**
1043
+     * A user is deleted from the system
1044
+     * So clean up the relevant shares.
1045
+     *
1046
+     * @param string $uid
1047
+     * @param int $shareType
1048
+     */
1049
+    public function userDeleted($uid, $shareType) {
1050
+        $qb = $this->dbConnection->getQueryBuilder();
1051
+
1052
+        $qb->delete('share')
1053
+            ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)))
1054
+            ->andWhere($qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)))
1055
+            ->execute();
1056
+    }
1057
+
1058
+    /**
1059
+     * This provider does not support group shares
1060
+     *
1061
+     * @param string $gid
1062
+     */
1063
+    public function groupDeleted($gid) {
1064
+    }
1065
+
1066
+    /**
1067
+     * This provider does not support group shares
1068
+     *
1069
+     * @param string $uid
1070
+     * @param string $gid
1071
+     */
1072
+    public function userDeletedFromGroup($uid, $gid) {
1073
+    }
1074
+
1075
+    /**
1076
+     * get database row of a give share
1077
+     *
1078
+     * @param $id
1079
+     * @return array
1080
+     * @throws ShareNotFound
1081
+     */
1082
+    protected function getRawShare($id) {
1083
+
1084
+        // Now fetch the inserted share and create a complete share object
1085
+        $qb = $this->dbConnection->getQueryBuilder();
1086
+        $qb->select('*')
1087
+            ->from('share')
1088
+            ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
1089
+
1090
+        $cursor = $qb->execute();
1091
+        $data = $cursor->fetch();
1092
+        $cursor->closeCursor();
1093
+
1094
+        if ($data === false) {
1095
+            throw new ShareNotFound;
1096
+        }
1097
+
1098
+        return $data;
1099
+    }
1100
+
1101
+    public function getSharesInFolder($userId, Folder $node, $reshares) {
1102
+        $qb = $this->dbConnection->getQueryBuilder();
1103
+        $qb->select('*')
1104
+            ->from('share', 's')
1105
+            ->andWhere($qb->expr()->orX(
1106
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
1107
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
1108
+            ))
1109
+            ->andWhere(
1110
+                $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL))
1111
+            );
1112
+
1113
+        /**
1114
+         * Reshares for this user are shares where they are the owner.
1115
+         */
1116
+        if ($reshares === false) {
1117
+            $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
1118
+        } else {
1119
+            $qb->andWhere(
1120
+                $qb->expr()->orX(
1121
+                    $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
1122
+                    $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
1123
+                )
1124
+            );
1125
+        }
1126
+
1127
+        $qb->innerJoin('s', 'filecache' ,'f', $qb->expr()->eq('s.file_source', 'f.fileid'));
1128
+        $qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())));
1129
+
1130
+        $qb->orderBy('id');
1131
+
1132
+        $cursor = $qb->execute();
1133
+        $shares = [];
1134
+        while ($data = $cursor->fetch()) {
1135
+            $shares[$data['fileid']][] = $this->createShareObject($data);
1136
+        }
1137
+        $cursor->closeCursor();
1138
+
1139
+        return $shares;
1140
+    }
1141
+
1142
+    /**
1143
+     * @inheritdoc
1144
+     */
1145
+    public function getAccessList($nodes, $currentAccess) {
1146
+        $ids = [];
1147
+        foreach ($nodes as $node) {
1148
+            $ids[] = $node->getId();
1149
+        }
1150
+
1151
+        $qb = $this->dbConnection->getQueryBuilder();
1152
+        $qb->select('share_with')
1153
+            ->from('share')
1154
+            ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)))
1155
+            ->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
1156
+            ->andWhere($qb->expr()->orX(
1157
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
1158
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
1159
+            ))
1160
+            ->setMaxResults(1);
1161
+        $cursor = $qb->execute();
1162
+
1163
+        $mail = $cursor->fetch() !== false;
1164
+        $cursor->closeCursor();
1165
+
1166
+        return ['public' => $mail];
1167
+    }
1168
+
1169
+    public function getAllShares(): iterable {
1170
+        $qb = $this->dbConnection->getQueryBuilder();
1171
+
1172
+        $qb->select('*')
1173
+            ->from('share')
1174
+            ->where(
1175
+                $qb->expr()->orX(
1176
+                    $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_EMAIL))
1177
+                )
1178
+            );
1179
+
1180
+        $cursor = $qb->execute();
1181
+        while($data = $cursor->fetch()) {
1182
+            try {
1183
+                $share = $this->createShareObject($data);
1184
+            } catch (InvalidShare $e) {
1185
+                continue;
1186
+            } catch (ShareNotFound $e) {
1187
+                continue;
1188
+            }
1189
+
1190
+            yield $share;
1191
+        }
1192
+        $cursor->closeCursor();
1193
+    }
1194 1194
 }
Please login to merge, or discard this patch.
Spacing   +23 added lines, -23 removed lines patch added patch discarded remove patch
@@ -211,10 +211,10 @@  discard block
 block discarded – undo
211 211
 		}
212 212
 
213 213
 		$passwordPolicy = $this->getPasswordPolicy();
214
-		$passwordCharset = ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS;
214
+		$passwordCharset = ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_DIGITS;
215 215
 		$passwordLength = 8;
216 216
 		if (!empty($passwordPolicy)) {
217
-			$passwordLength = (int)$passwordPolicy['minLength'] > 0 ? (int)$passwordPolicy['minLength'] : $passwordLength;
217
+			$passwordLength = (int) $passwordPolicy['minLength'] > 0 ? (int) $passwordPolicy['minLength'] : $passwordLength;
218 218
 			$passwordCharset .= $passwordPolicy['enforceSpecialCharacters'] ? ISecureRandom::CHAR_SYMBOLS : '';
219 219
 		}
220 220
 
@@ -407,7 +407,7 @@  discard block
 block discarded – undo
407 407
 		$text = $this->l->t('%1$s shared »%2$s« with you.', [$initiatorDisplayName, $filename]);
408 408
 
409 409
 		$emailTemplate->addBodyText(
410
-			htmlspecialchars($text . ' ' . $this->l->t('Click the button below to open it.')),
410
+			htmlspecialchars($text.' '.$this->l->t('Click the button below to open it.')),
411 411
 			$text
412 412
 		);
413 413
 		$emailTemplate->addBodyButton(
@@ -431,9 +431,9 @@  discard block
 block discarded – undo
431 431
 		// The "Reply-To" is set to the sharer if an mail address is configured
432 432
 		// also the default footer contains a "Do not reply" which needs to be adjusted.
433 433
 		$initiatorEmail = $initiatorUser->getEMailAddress();
434
-		if($initiatorEmail !== null) {
434
+		if ($initiatorEmail !== null) {
435 435
 			$message->setReplyTo([$initiatorEmail => $initiatorDisplayName]);
436
-			$emailTemplate->addFooter($instanceName . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : ''));
436
+			$emailTemplate->addFooter($instanceName.($this->defaults->getSlogan() !== '' ? ' - '.$this->defaults->getSlogan() : ''));
437 437
 		} else {
438 438
 			$emailTemplate->addFooter();
439 439
 		}
@@ -495,7 +495,7 @@  discard block
 block discarded – undo
495 495
 		$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
496 496
 		if ($initiatorEmailAddress !== null) {
497 497
 			$message->setReplyTo([$initiatorEmailAddress => $initiatorDisplayName]);
498
-			$emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
498
+			$emailTemplate->addFooter($instanceName.' - '.$this->defaults->getSlogan());
499 499
 		} else {
500 500
 			$emailTemplate->addFooter();
501 501
 		}
@@ -553,7 +553,7 @@  discard block
 block discarded – undo
553 553
 		$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
554 554
 		if ($initiatorEmailAddress !== null) {
555 555
 			$message->setReplyTo([$initiatorEmailAddress => $initiatorDisplayName]);
556
-			$emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
556
+			$emailTemplate->addFooter($instanceName.' - '.$this->defaults->getSlogan());
557 557
 		} else {
558 558
 			$emailTemplate->addFooter();
559 559
 		}
@@ -646,7 +646,7 @@  discard block
 block discarded – undo
646 646
 			->orderBy('id');
647 647
 
648 648
 		$cursor = $qb->execute();
649
-		while($data = $cursor->fetch()) {
649
+		while ($data = $cursor->fetch()) {
650 650
 			$children[] = $this->createShareObject($data);
651 651
 		}
652 652
 		$cursor->closeCursor();
@@ -693,7 +693,7 @@  discard block
 block discarded – undo
693 693
 		$qb->execute();
694 694
 		$id = $qb->getLastInsertId();
695 695
 
696
-		return (int)$id;
696
+		return (int) $id;
697 697
 	}
698 698
 
699 699
 	/**
@@ -710,7 +710,7 @@  discard block
 block discarded – undo
710 710
 		// a real password was given
711 711
 		$validPassword = $plainTextPassword !== null && $plainTextPassword !== '';
712 712
 
713
-		if($validPassword && ($originalShare->getPassword() !== $share->getPassword() ||
713
+		if ($validPassword && ($originalShare->getPassword() !== $share->getPassword() ||
714 714
 								($originalShare->getSendPasswordByTalk() && !$share->getSendPasswordByTalk()))) {
715 715
 			$this->sendPassword($share, $plainTextPassword);
716 716
 		}
@@ -819,7 +819,7 @@  discard block
 block discarded – undo
819 819
 
820 820
 		$cursor = $qb->execute();
821 821
 		$shares = [];
822
-		while($data = $cursor->fetch()) {
822
+		while ($data = $cursor->fetch()) {
823 823
 			$shares[] = $this->createShareObject($data);
824 824
 		}
825 825
 		$cursor->closeCursor();
@@ -871,7 +871,7 @@  discard block
 block discarded – undo
871 871
 			->execute();
872 872
 
873 873
 		$shares = [];
874
-		while($data = $cursor->fetch()) {
874
+		while ($data = $cursor->fetch()) {
875 875
 			$shares[] = $this->createShareObject($data);
876 876
 		}
877 877
 		$cursor->closeCursor();
@@ -910,7 +910,7 @@  discard block
 block discarded – undo
910 910
 
911 911
 		$cursor = $qb->execute();
912 912
 
913
-		while($data = $cursor->fetch()) {
913
+		while ($data = $cursor->fetch()) {
914 914
 			$shares[] = $this->createShareObject($data);
915 915
 		}
916 916
 		$cursor->closeCursor();
@@ -973,20 +973,20 @@  discard block
 block discarded – undo
973 973
 	protected function createShareObject($data) {
974 974
 
975 975
 		$share = new Share($this->rootFolder, $this->userManager);
976
-		$share->setId((int)$data['id'])
977
-			->setShareType((int)$data['share_type'])
978
-			->setPermissions((int)$data['permissions'])
976
+		$share->setId((int) $data['id'])
977
+			->setShareType((int) $data['share_type'])
978
+			->setPermissions((int) $data['permissions'])
979 979
 			->setTarget($data['file_target'])
980
-			->setMailSend((bool)$data['mail_send'])
980
+			->setMailSend((bool) $data['mail_send'])
981 981
 			->setNote($data['note'])
982 982
 			->setToken($data['token']);
983 983
 
984 984
 		$shareTime = new \DateTime();
985
-		$shareTime->setTimestamp((int)$data['stime']);
985
+		$shareTime->setTimestamp((int) $data['stime']);
986 986
 		$share->setShareTime($shareTime);
987 987
 		$share->setSharedWith($data['share_with']);
988 988
 		$share->setPassword($data['password']);
989
-		$share->setSendPasswordByTalk((bool)$data['password_by_talk']);
989
+		$share->setSendPasswordByTalk((bool) $data['password_by_talk']);
990 990
 
991 991
 		if ($data['uid_initiator'] !== null) {
992 992
 			$share->setShareOwner($data['uid_owner']);
@@ -994,7 +994,7 @@  discard block
 block discarded – undo
994 994
 		} else {
995 995
 			//OLD SHARE
996 996
 			$share->setSharedBy($data['uid_owner']);
997
-			$path = $this->getNode($share->getSharedBy(), (int)$data['file_source']);
997
+			$path = $this->getNode($share->getSharedBy(), (int) $data['file_source']);
998 998
 
999 999
 			$owner = $path->getOwner();
1000 1000
 			$share->setShareOwner($owner->getUID());
@@ -1007,7 +1007,7 @@  discard block
 block discarded – undo
1007 1007
 			}
1008 1008
 		}
1009 1009
 
1010
-		$share->setNodeId((int)$data['file_source']);
1010
+		$share->setNodeId((int) $data['file_source']);
1011 1011
 		$share->setNodeType($data['item_type']);
1012 1012
 
1013 1013
 		$share->setProviderId($this->identifier());
@@ -1124,7 +1124,7 @@  discard block
 block discarded – undo
1124 1124
 			);
1125 1125
 		}
1126 1126
 
1127
-		$qb->innerJoin('s', 'filecache' ,'f', $qb->expr()->eq('s.file_source', 'f.fileid'));
1127
+		$qb->innerJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid'));
1128 1128
 		$qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())));
1129 1129
 
1130 1130
 		$qb->orderBy('id');
@@ -1178,7 +1178,7 @@  discard block
 block discarded – undo
1178 1178
 			);
1179 1179
 
1180 1180
 		$cursor = $qb->execute();
1181
-		while($data = $cursor->fetch()) {
1181
+		while ($data = $cursor->fetch()) {
1182 1182
 			try {
1183 1183
 				$share = $this->createShareObject($data);
1184 1184
 			} catch (InvalidShare $e) {
Please login to merge, or discard this patch.
apps/federatedfilesharing/lib/FederatedShareProvider.php 2 patches
Indentation   +1062 added lines, -1062 removed lines patch added patch discarded remove patch
@@ -55,1076 +55,1076 @@
 block discarded – undo
55 55
  */
56 56
 class FederatedShareProvider implements IShareProvider {
57 57
 
58
-	const SHARE_TYPE_REMOTE = 6;
59
-
60
-	/** @var IDBConnection */
61
-	private $dbConnection;
62
-
63
-	/** @var AddressHandler */
64
-	private $addressHandler;
65
-
66
-	/** @var Notifications */
67
-	private $notifications;
68
-
69
-	/** @var TokenHandler */
70
-	private $tokenHandler;
71
-
72
-	/** @var IL10N */
73
-	private $l;
74
-
75
-	/** @var ILogger */
76
-	private $logger;
77
-
78
-	/** @var IRootFolder */
79
-	private $rootFolder;
80
-
81
-	/** @var IConfig */
82
-	private $config;
83
-
84
-	/** @var string */
85
-	private $externalShareTable = 'share_external';
86
-
87
-	/** @var IUserManager */
88
-	private $userManager;
89
-
90
-	/** @var ICloudIdManager */
91
-	private $cloudIdManager;
92
-
93
-	/** @var \OCP\GlobalScale\IConfig */
94
-	private $gsConfig;
95
-
96
-	/** @var ICloudFederationProviderManager */
97
-	private $cloudFederationProviderManager;
98
-
99
-	/** @var array list of supported share types */
100
-	private $supportedShareType = [\OCP\Share::SHARE_TYPE_REMOTE_GROUP, \OCP\Share::SHARE_TYPE_REMOTE];
101
-
102
-	/**
103
-	 * DefaultShareProvider constructor.
104
-	 *
105
-	 * @param IDBConnection $connection
106
-	 * @param AddressHandler $addressHandler
107
-	 * @param Notifications $notifications
108
-	 * @param TokenHandler $tokenHandler
109
-	 * @param IL10N $l10n
110
-	 * @param ILogger $logger
111
-	 * @param IRootFolder $rootFolder
112
-	 * @param IConfig $config
113
-	 * @param IUserManager $userManager
114
-	 * @param ICloudIdManager $cloudIdManager
115
-	 * @param \OCP\GlobalScale\IConfig $globalScaleConfig
116
-	 * @param ICloudFederationProviderManager $cloudFederationProviderManager
117
-	 */
118
-	public function __construct(
119
-			IDBConnection $connection,
120
-			AddressHandler $addressHandler,
121
-			Notifications $notifications,
122
-			TokenHandler $tokenHandler,
123
-			IL10N $l10n,
124
-			ILogger $logger,
125
-			IRootFolder $rootFolder,
126
-			IConfig $config,
127
-			IUserManager $userManager,
128
-			ICloudIdManager $cloudIdManager,
129
-			\OCP\GlobalScale\IConfig $globalScaleConfig,
130
-			ICloudFederationProviderManager $cloudFederationProviderManager
131
-	) {
132
-		$this->dbConnection = $connection;
133
-		$this->addressHandler = $addressHandler;
134
-		$this->notifications = $notifications;
135
-		$this->tokenHandler = $tokenHandler;
136
-		$this->l = $l10n;
137
-		$this->logger = $logger;
138
-		$this->rootFolder = $rootFolder;
139
-		$this->config = $config;
140
-		$this->userManager = $userManager;
141
-		$this->cloudIdManager = $cloudIdManager;
142
-		$this->gsConfig = $globalScaleConfig;
143
-		$this->cloudFederationProviderManager = $cloudFederationProviderManager;
144
-	}
145
-
146
-	/**
147
-	 * Return the identifier of this provider.
148
-	 *
149
-	 * @return string Containing only [a-zA-Z0-9]
150
-	 */
151
-	public function identifier() {
152
-		return 'ocFederatedSharing';
153
-	}
154
-
155
-	/**
156
-	 * Share a path
157
-	 *
158
-	 * @param IShare $share
159
-	 * @return IShare The share object
160
-	 * @throws ShareNotFound
161
-	 * @throws \Exception
162
-	 */
163
-	public function create(IShare $share) {
164
-
165
-		$shareWith = $share->getSharedWith();
166
-		$itemSource = $share->getNodeId();
167
-		$itemType = $share->getNodeType();
168
-		$permissions = $share->getPermissions();
169
-		$sharedBy = $share->getSharedBy();
170
-		$shareType = $share->getShareType();
171
-
172
-		if ($shareType === \OCP\Share::SHARE_TYPE_REMOTE_GROUP &&
173
-			!$this->isOutgoingServer2serverGroupShareEnabled()
174
-		) {
175
-			$message = 'It is not allowed to send federated group shares from this server.';
176
-			$message_t = $this->l->t('It is not allowed to send federated group shares from this server.');
177
-			$this->logger->debug($message, ['app' => 'Federated File Sharing']);
178
-			throw new \Exception($message_t);
179
-		}
180
-
181
-		/*
58
+    const SHARE_TYPE_REMOTE = 6;
59
+
60
+    /** @var IDBConnection */
61
+    private $dbConnection;
62
+
63
+    /** @var AddressHandler */
64
+    private $addressHandler;
65
+
66
+    /** @var Notifications */
67
+    private $notifications;
68
+
69
+    /** @var TokenHandler */
70
+    private $tokenHandler;
71
+
72
+    /** @var IL10N */
73
+    private $l;
74
+
75
+    /** @var ILogger */
76
+    private $logger;
77
+
78
+    /** @var IRootFolder */
79
+    private $rootFolder;
80
+
81
+    /** @var IConfig */
82
+    private $config;
83
+
84
+    /** @var string */
85
+    private $externalShareTable = 'share_external';
86
+
87
+    /** @var IUserManager */
88
+    private $userManager;
89
+
90
+    /** @var ICloudIdManager */
91
+    private $cloudIdManager;
92
+
93
+    /** @var \OCP\GlobalScale\IConfig */
94
+    private $gsConfig;
95
+
96
+    /** @var ICloudFederationProviderManager */
97
+    private $cloudFederationProviderManager;
98
+
99
+    /** @var array list of supported share types */
100
+    private $supportedShareType = [\OCP\Share::SHARE_TYPE_REMOTE_GROUP, \OCP\Share::SHARE_TYPE_REMOTE];
101
+
102
+    /**
103
+     * DefaultShareProvider constructor.
104
+     *
105
+     * @param IDBConnection $connection
106
+     * @param AddressHandler $addressHandler
107
+     * @param Notifications $notifications
108
+     * @param TokenHandler $tokenHandler
109
+     * @param IL10N $l10n
110
+     * @param ILogger $logger
111
+     * @param IRootFolder $rootFolder
112
+     * @param IConfig $config
113
+     * @param IUserManager $userManager
114
+     * @param ICloudIdManager $cloudIdManager
115
+     * @param \OCP\GlobalScale\IConfig $globalScaleConfig
116
+     * @param ICloudFederationProviderManager $cloudFederationProviderManager
117
+     */
118
+    public function __construct(
119
+            IDBConnection $connection,
120
+            AddressHandler $addressHandler,
121
+            Notifications $notifications,
122
+            TokenHandler $tokenHandler,
123
+            IL10N $l10n,
124
+            ILogger $logger,
125
+            IRootFolder $rootFolder,
126
+            IConfig $config,
127
+            IUserManager $userManager,
128
+            ICloudIdManager $cloudIdManager,
129
+            \OCP\GlobalScale\IConfig $globalScaleConfig,
130
+            ICloudFederationProviderManager $cloudFederationProviderManager
131
+    ) {
132
+        $this->dbConnection = $connection;
133
+        $this->addressHandler = $addressHandler;
134
+        $this->notifications = $notifications;
135
+        $this->tokenHandler = $tokenHandler;
136
+        $this->l = $l10n;
137
+        $this->logger = $logger;
138
+        $this->rootFolder = $rootFolder;
139
+        $this->config = $config;
140
+        $this->userManager = $userManager;
141
+        $this->cloudIdManager = $cloudIdManager;
142
+        $this->gsConfig = $globalScaleConfig;
143
+        $this->cloudFederationProviderManager = $cloudFederationProviderManager;
144
+    }
145
+
146
+    /**
147
+     * Return the identifier of this provider.
148
+     *
149
+     * @return string Containing only [a-zA-Z0-9]
150
+     */
151
+    public function identifier() {
152
+        return 'ocFederatedSharing';
153
+    }
154
+
155
+    /**
156
+     * Share a path
157
+     *
158
+     * @param IShare $share
159
+     * @return IShare The share object
160
+     * @throws ShareNotFound
161
+     * @throws \Exception
162
+     */
163
+    public function create(IShare $share) {
164
+
165
+        $shareWith = $share->getSharedWith();
166
+        $itemSource = $share->getNodeId();
167
+        $itemType = $share->getNodeType();
168
+        $permissions = $share->getPermissions();
169
+        $sharedBy = $share->getSharedBy();
170
+        $shareType = $share->getShareType();
171
+
172
+        if ($shareType === \OCP\Share::SHARE_TYPE_REMOTE_GROUP &&
173
+            !$this->isOutgoingServer2serverGroupShareEnabled()
174
+        ) {
175
+            $message = 'It is not allowed to send federated group shares from this server.';
176
+            $message_t = $this->l->t('It is not allowed to send federated group shares from this server.');
177
+            $this->logger->debug($message, ['app' => 'Federated File Sharing']);
178
+            throw new \Exception($message_t);
179
+        }
180
+
181
+        /*
182 182
 		 * Check if file is not already shared with the remote user
183 183
 		 */
184
-		$alreadyShared = $this->getSharedWith($shareWith, \OCP\Share::SHARE_TYPE_REMOTE, $share->getNode(), 1, 0);
185
-		$alreadySharedGroup = $this->getSharedWith($shareWith, \OCP\Share::SHARE_TYPE_REMOTE_GROUP, $share->getNode(), 1, 0);
186
-		if (!empty($alreadyShared) || !empty($alreadySharedGroup)) {
187
-			$message = 'Sharing %1$s failed, because this item is already shared with %2$s';
188
-			$message_t = $this->l->t('Sharing %1$s failed, because this item is already shared with %2$s', array($share->getNode()->getName(), $shareWith));
189
-			$this->logger->debug(sprintf($message, $share->getNode()->getName(), $shareWith), ['app' => 'Federated File Sharing']);
190
-			throw new \Exception($message_t);
191
-		}
192
-
193
-
194
-		// don't allow federated shares if source and target server are the same
195
-		$cloudId = $this->cloudIdManager->resolveCloudId($shareWith);
196
-		$currentServer = $this->addressHandler->generateRemoteURL();
197
-		$currentUser = $sharedBy;
198
-		if ($this->addressHandler->compareAddresses($cloudId->getUser(), $cloudId->getRemote(), $currentUser, $currentServer)) {
199
-			$message = 'Not allowed to create a federated share with the same user.';
200
-			$message_t = $this->l->t('Not allowed to create a federated share with the same user');
201
-			$this->logger->debug($message, ['app' => 'Federated File Sharing']);
202
-			throw new \Exception($message_t);
203
-		}
204
-
205
-
206
-		$share->setSharedWith($cloudId->getId());
207
-
208
-		try {
209
-			$remoteShare = $this->getShareFromExternalShareTable($share);
210
-		} catch (ShareNotFound $e) {
211
-			$remoteShare = null;
212
-		}
213
-
214
-		if ($remoteShare) {
215
-			try {
216
-				$ownerCloudId = $this->cloudIdManager->getCloudId($remoteShare['owner'], $remoteShare['remote']);
217
-				$shareId = $this->addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $ownerCloudId->getId(), $permissions, 'tmp_token_' . time(), $shareType);
218
-				$share->setId($shareId);
219
-				list($token, $remoteId) = $this->askOwnerToReShare($shareWith, $share, $shareId);
220
-				// remote share was create successfully if we get a valid token as return
221
-				$send = is_string($token) && $token !== '';
222
-			} catch (\Exception $e) {
223
-				// fall back to old re-share behavior if the remote server
224
-				// doesn't support flat re-shares (was introduced with Nextcloud 9.1)
225
-				$this->removeShareFromTable($share);
226
-				$shareId = $this->createFederatedShare($share);
227
-			}
228
-			if ($send) {
229
-				$this->updateSuccessfulReshare($shareId, $token);
230
-				$this->storeRemoteId($shareId, $remoteId);
231
-			} else {
232
-				$this->removeShareFromTable($share);
233
-				$message_t = $this->l->t('File is already shared with %s', [$shareWith]);
234
-				throw new \Exception($message_t);
235
-			}
236
-
237
-		} else {
238
-			$shareId = $this->createFederatedShare($share);
239
-		}
240
-
241
-		$data = $this->getRawShare($shareId);
242
-		return $this->createShareObject($data);
243
-	}
244
-
245
-	/**
246
-	 * create federated share and inform the recipient
247
-	 *
248
-	 * @param IShare $share
249
-	 * @return int
250
-	 * @throws ShareNotFound
251
-	 * @throws \Exception
252
-	 */
253
-	protected function createFederatedShare(IShare $share) {
254
-		$token = $this->tokenHandler->generateToken();
255
-		$shareId = $this->addShareToDB(
256
-			$share->getNodeId(),
257
-			$share->getNodeType(),
258
-			$share->getSharedWith(),
259
-			$share->getSharedBy(),
260
-			$share->getShareOwner(),
261
-			$share->getPermissions(),
262
-			$token,
263
-			$share->getShareType()
264
-		);
265
-
266
-		$failure = false;
267
-
268
-		try {
269
-			$sharedByFederatedId = $share->getSharedBy();
270
-			if ($this->userManager->userExists($sharedByFederatedId)) {
271
-				$cloudId = $this->cloudIdManager->getCloudId($sharedByFederatedId, $this->addressHandler->generateRemoteURL());
272
-				$sharedByFederatedId = $cloudId->getId();
273
-			}
274
-			$ownerCloudId = $this->cloudIdManager->getCloudId($share->getShareOwner(), $this->addressHandler->generateRemoteURL());
275
-			$send = $this->notifications->sendRemoteShare(
276
-				$token,
277
-				$share->getSharedWith(),
278
-				$share->getNode()->getName(),
279
-				$shareId,
280
-				$share->getShareOwner(),
281
-				$ownerCloudId->getId(),
282
-				$share->getSharedBy(),
283
-				$sharedByFederatedId,
284
-				$share->getShareType()
285
-			);
286
-
287
-			if ($send === false) {
288
-				$failure = true;
289
-			}
290
-		} catch (\Exception $e) {
291
-			$this->logger->logException($e, [
292
-				'message' => 'Failed to notify remote server of federated share, removing share.',
293
-				'level' => ILogger::ERROR,
294
-				'app' => 'federatedfilesharing',
295
-			]);
296
-			$failure = true;
297
-		}
298
-
299
-		if($failure) {
300
-			$this->removeShareFromTableById($shareId);
301
-			$message_t = $this->l->t('Sharing %1$s failed, could not find %2$s, maybe the server is currently unreachable or uses a self-signed certificate.',
302
-				[$share->getNode()->getName(), $share->getSharedWith()]);
303
-			throw new \Exception($message_t);
304
-		}
305
-
306
-		return $shareId;
307
-
308
-	}
309
-
310
-	/**
311
-	 * @param string $shareWith
312
-	 * @param IShare $share
313
-	 * @param string $shareId internal share Id
314
-	 * @return array
315
-	 * @throws \Exception
316
-	 */
317
-	protected function askOwnerToReShare($shareWith, IShare $share, $shareId) {
318
-
319
-		$remoteShare = $this->getShareFromExternalShareTable($share);
320
-		$token = $remoteShare['share_token'];
321
-		$remoteId = $remoteShare['remote_id'];
322
-		$remote = $remoteShare['remote'];
323
-
324
-		list($token, $remoteId) = $this->notifications->requestReShare(
325
-			$token,
326
-			$remoteId,
327
-			$shareId,
328
-			$remote,
329
-			$shareWith,
330
-			$share->getPermissions(),
331
-			$share->getNode()->getName()
332
-		);
333
-
334
-		return [$token, $remoteId];
335
-	}
336
-
337
-	/**
338
-	 * get federated share from the share_external table but exclude mounted link shares
339
-	 *
340
-	 * @param IShare $share
341
-	 * @return array
342
-	 * @throws ShareNotFound
343
-	 */
344
-	protected function getShareFromExternalShareTable(IShare $share) {
345
-		$query = $this->dbConnection->getQueryBuilder();
346
-		$query->select('*')->from($this->externalShareTable)
347
-			->where($query->expr()->eq('user', $query->createNamedParameter($share->getShareOwner())))
348
-			->andWhere($query->expr()->eq('mountpoint', $query->createNamedParameter($share->getTarget())));
349
-		$result = $query->execute()->fetchAll();
350
-
351
-		if (isset($result[0]) && (int)$result[0]['remote_id'] > 0) {
352
-			return $result[0];
353
-		}
354
-
355
-		throw new ShareNotFound('share not found in share_external table');
356
-	}
357
-
358
-	/**
359
-	 * add share to the database and return the ID
360
-	 *
361
-	 * @param int $itemSource
362
-	 * @param string $itemType
363
-	 * @param string $shareWith
364
-	 * @param string $sharedBy
365
-	 * @param string $uidOwner
366
-	 * @param int $permissions
367
-	 * @param string $token
368
-	 * @param int $shareType
369
-	 * @return int
370
-	 */
371
-	private function addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, $token, $shareType) {
372
-		$qb = $this->dbConnection->getQueryBuilder();
373
-		$qb->insert('share')
374
-			->setValue('share_type', $qb->createNamedParameter($shareType))
375
-			->setValue('item_type', $qb->createNamedParameter($itemType))
376
-			->setValue('item_source', $qb->createNamedParameter($itemSource))
377
-			->setValue('file_source', $qb->createNamedParameter($itemSource))
378
-			->setValue('share_with', $qb->createNamedParameter($shareWith))
379
-			->setValue('uid_owner', $qb->createNamedParameter($uidOwner))
380
-			->setValue('uid_initiator', $qb->createNamedParameter($sharedBy))
381
-			->setValue('permissions', $qb->createNamedParameter($permissions))
382
-			->setValue('token', $qb->createNamedParameter($token))
383
-			->setValue('stime', $qb->createNamedParameter(time()));
384
-
385
-		/*
184
+        $alreadyShared = $this->getSharedWith($shareWith, \OCP\Share::SHARE_TYPE_REMOTE, $share->getNode(), 1, 0);
185
+        $alreadySharedGroup = $this->getSharedWith($shareWith, \OCP\Share::SHARE_TYPE_REMOTE_GROUP, $share->getNode(), 1, 0);
186
+        if (!empty($alreadyShared) || !empty($alreadySharedGroup)) {
187
+            $message = 'Sharing %1$s failed, because this item is already shared with %2$s';
188
+            $message_t = $this->l->t('Sharing %1$s failed, because this item is already shared with %2$s', array($share->getNode()->getName(), $shareWith));
189
+            $this->logger->debug(sprintf($message, $share->getNode()->getName(), $shareWith), ['app' => 'Federated File Sharing']);
190
+            throw new \Exception($message_t);
191
+        }
192
+
193
+
194
+        // don't allow federated shares if source and target server are the same
195
+        $cloudId = $this->cloudIdManager->resolveCloudId($shareWith);
196
+        $currentServer = $this->addressHandler->generateRemoteURL();
197
+        $currentUser = $sharedBy;
198
+        if ($this->addressHandler->compareAddresses($cloudId->getUser(), $cloudId->getRemote(), $currentUser, $currentServer)) {
199
+            $message = 'Not allowed to create a federated share with the same user.';
200
+            $message_t = $this->l->t('Not allowed to create a federated share with the same user');
201
+            $this->logger->debug($message, ['app' => 'Federated File Sharing']);
202
+            throw new \Exception($message_t);
203
+        }
204
+
205
+
206
+        $share->setSharedWith($cloudId->getId());
207
+
208
+        try {
209
+            $remoteShare = $this->getShareFromExternalShareTable($share);
210
+        } catch (ShareNotFound $e) {
211
+            $remoteShare = null;
212
+        }
213
+
214
+        if ($remoteShare) {
215
+            try {
216
+                $ownerCloudId = $this->cloudIdManager->getCloudId($remoteShare['owner'], $remoteShare['remote']);
217
+                $shareId = $this->addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $ownerCloudId->getId(), $permissions, 'tmp_token_' . time(), $shareType);
218
+                $share->setId($shareId);
219
+                list($token, $remoteId) = $this->askOwnerToReShare($shareWith, $share, $shareId);
220
+                // remote share was create successfully if we get a valid token as return
221
+                $send = is_string($token) && $token !== '';
222
+            } catch (\Exception $e) {
223
+                // fall back to old re-share behavior if the remote server
224
+                // doesn't support flat re-shares (was introduced with Nextcloud 9.1)
225
+                $this->removeShareFromTable($share);
226
+                $shareId = $this->createFederatedShare($share);
227
+            }
228
+            if ($send) {
229
+                $this->updateSuccessfulReshare($shareId, $token);
230
+                $this->storeRemoteId($shareId, $remoteId);
231
+            } else {
232
+                $this->removeShareFromTable($share);
233
+                $message_t = $this->l->t('File is already shared with %s', [$shareWith]);
234
+                throw new \Exception($message_t);
235
+            }
236
+
237
+        } else {
238
+            $shareId = $this->createFederatedShare($share);
239
+        }
240
+
241
+        $data = $this->getRawShare($shareId);
242
+        return $this->createShareObject($data);
243
+    }
244
+
245
+    /**
246
+     * create federated share and inform the recipient
247
+     *
248
+     * @param IShare $share
249
+     * @return int
250
+     * @throws ShareNotFound
251
+     * @throws \Exception
252
+     */
253
+    protected function createFederatedShare(IShare $share) {
254
+        $token = $this->tokenHandler->generateToken();
255
+        $shareId = $this->addShareToDB(
256
+            $share->getNodeId(),
257
+            $share->getNodeType(),
258
+            $share->getSharedWith(),
259
+            $share->getSharedBy(),
260
+            $share->getShareOwner(),
261
+            $share->getPermissions(),
262
+            $token,
263
+            $share->getShareType()
264
+        );
265
+
266
+        $failure = false;
267
+
268
+        try {
269
+            $sharedByFederatedId = $share->getSharedBy();
270
+            if ($this->userManager->userExists($sharedByFederatedId)) {
271
+                $cloudId = $this->cloudIdManager->getCloudId($sharedByFederatedId, $this->addressHandler->generateRemoteURL());
272
+                $sharedByFederatedId = $cloudId->getId();
273
+            }
274
+            $ownerCloudId = $this->cloudIdManager->getCloudId($share->getShareOwner(), $this->addressHandler->generateRemoteURL());
275
+            $send = $this->notifications->sendRemoteShare(
276
+                $token,
277
+                $share->getSharedWith(),
278
+                $share->getNode()->getName(),
279
+                $shareId,
280
+                $share->getShareOwner(),
281
+                $ownerCloudId->getId(),
282
+                $share->getSharedBy(),
283
+                $sharedByFederatedId,
284
+                $share->getShareType()
285
+            );
286
+
287
+            if ($send === false) {
288
+                $failure = true;
289
+            }
290
+        } catch (\Exception $e) {
291
+            $this->logger->logException($e, [
292
+                'message' => 'Failed to notify remote server of federated share, removing share.',
293
+                'level' => ILogger::ERROR,
294
+                'app' => 'federatedfilesharing',
295
+            ]);
296
+            $failure = true;
297
+        }
298
+
299
+        if($failure) {
300
+            $this->removeShareFromTableById($shareId);
301
+            $message_t = $this->l->t('Sharing %1$s failed, could not find %2$s, maybe the server is currently unreachable or uses a self-signed certificate.',
302
+                [$share->getNode()->getName(), $share->getSharedWith()]);
303
+            throw new \Exception($message_t);
304
+        }
305
+
306
+        return $shareId;
307
+
308
+    }
309
+
310
+    /**
311
+     * @param string $shareWith
312
+     * @param IShare $share
313
+     * @param string $shareId internal share Id
314
+     * @return array
315
+     * @throws \Exception
316
+     */
317
+    protected function askOwnerToReShare($shareWith, IShare $share, $shareId) {
318
+
319
+        $remoteShare = $this->getShareFromExternalShareTable($share);
320
+        $token = $remoteShare['share_token'];
321
+        $remoteId = $remoteShare['remote_id'];
322
+        $remote = $remoteShare['remote'];
323
+
324
+        list($token, $remoteId) = $this->notifications->requestReShare(
325
+            $token,
326
+            $remoteId,
327
+            $shareId,
328
+            $remote,
329
+            $shareWith,
330
+            $share->getPermissions(),
331
+            $share->getNode()->getName()
332
+        );
333
+
334
+        return [$token, $remoteId];
335
+    }
336
+
337
+    /**
338
+     * get federated share from the share_external table but exclude mounted link shares
339
+     *
340
+     * @param IShare $share
341
+     * @return array
342
+     * @throws ShareNotFound
343
+     */
344
+    protected function getShareFromExternalShareTable(IShare $share) {
345
+        $query = $this->dbConnection->getQueryBuilder();
346
+        $query->select('*')->from($this->externalShareTable)
347
+            ->where($query->expr()->eq('user', $query->createNamedParameter($share->getShareOwner())))
348
+            ->andWhere($query->expr()->eq('mountpoint', $query->createNamedParameter($share->getTarget())));
349
+        $result = $query->execute()->fetchAll();
350
+
351
+        if (isset($result[0]) && (int)$result[0]['remote_id'] > 0) {
352
+            return $result[0];
353
+        }
354
+
355
+        throw new ShareNotFound('share not found in share_external table');
356
+    }
357
+
358
+    /**
359
+     * add share to the database and return the ID
360
+     *
361
+     * @param int $itemSource
362
+     * @param string $itemType
363
+     * @param string $shareWith
364
+     * @param string $sharedBy
365
+     * @param string $uidOwner
366
+     * @param int $permissions
367
+     * @param string $token
368
+     * @param int $shareType
369
+     * @return int
370
+     */
371
+    private function addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, $token, $shareType) {
372
+        $qb = $this->dbConnection->getQueryBuilder();
373
+        $qb->insert('share')
374
+            ->setValue('share_type', $qb->createNamedParameter($shareType))
375
+            ->setValue('item_type', $qb->createNamedParameter($itemType))
376
+            ->setValue('item_source', $qb->createNamedParameter($itemSource))
377
+            ->setValue('file_source', $qb->createNamedParameter($itemSource))
378
+            ->setValue('share_with', $qb->createNamedParameter($shareWith))
379
+            ->setValue('uid_owner', $qb->createNamedParameter($uidOwner))
380
+            ->setValue('uid_initiator', $qb->createNamedParameter($sharedBy))
381
+            ->setValue('permissions', $qb->createNamedParameter($permissions))
382
+            ->setValue('token', $qb->createNamedParameter($token))
383
+            ->setValue('stime', $qb->createNamedParameter(time()));
384
+
385
+        /*
386 386
 		 * Added to fix https://github.com/owncloud/core/issues/22215
387 387
 		 * Can be removed once we get rid of ajax/share.php
388 388
 		 */
389
-		$qb->setValue('file_target', $qb->createNamedParameter(''));
390
-
391
-		$qb->execute();
392
-		$id = $qb->getLastInsertId();
393
-
394
-		return (int)$id;
395
-	}
396
-
397
-	/**
398
-	 * Update a share
399
-	 *
400
-	 * @param IShare $share
401
-	 * @return IShare The share object
402
-	 */
403
-	public function update(IShare $share) {
404
-		/*
389
+        $qb->setValue('file_target', $qb->createNamedParameter(''));
390
+
391
+        $qb->execute();
392
+        $id = $qb->getLastInsertId();
393
+
394
+        return (int)$id;
395
+    }
396
+
397
+    /**
398
+     * Update a share
399
+     *
400
+     * @param IShare $share
401
+     * @return IShare The share object
402
+     */
403
+    public function update(IShare $share) {
404
+        /*
405 405
 		 * We allow updating the permissions of federated shares
406 406
 		 */
407
-		$qb = $this->dbConnection->getQueryBuilder();
408
-			$qb->update('share')
409
-				->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
410
-				->set('permissions', $qb->createNamedParameter($share->getPermissions()))
411
-				->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
412
-				->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
413
-				->execute();
414
-
415
-		// send the updated permission to the owner/initiator, if they are not the same
416
-		if ($share->getShareOwner() !== $share->getSharedBy()) {
417
-			$this->sendPermissionUpdate($share);
418
-		}
419
-
420
-		return $share;
421
-	}
422
-
423
-	/**
424
-	 * send the updated permission to the owner/initiator, if they are not the same
425
-	 *
426
-	 * @param IShare $share
427
-	 * @throws ShareNotFound
428
-	 * @throws \OC\HintException
429
-	 */
430
-	protected function sendPermissionUpdate(IShare $share) {
431
-		$remoteId = $this->getRemoteId($share);
432
-		// if the local user is the owner we send the permission change to the initiator
433
-		if ($this->userManager->userExists($share->getShareOwner())) {
434
-			list(, $remote) = $this->addressHandler->splitUserRemote($share->getSharedBy());
435
-		} else { // ... if not we send the permission change to the owner
436
-			list(, $remote) = $this->addressHandler->splitUserRemote($share->getShareOwner());
437
-		}
438
-		$this->notifications->sendPermissionChange($remote, $remoteId, $share->getToken(), $share->getPermissions());
439
-	}
440
-
441
-
442
-	/**
443
-	 * update successful reShare with the correct token
444
-	 *
445
-	 * @param int $shareId
446
-	 * @param string $token
447
-	 */
448
-	protected function updateSuccessfulReShare($shareId, $token) {
449
-		$query = $this->dbConnection->getQueryBuilder();
450
-		$query->update('share')
451
-			->where($query->expr()->eq('id', $query->createNamedParameter($shareId)))
452
-			->set('token', $query->createNamedParameter($token))
453
-			->execute();
454
-	}
455
-
456
-	/**
457
-	 * store remote ID in federated reShare table
458
-	 *
459
-	 * @param $shareId
460
-	 * @param $remoteId
461
-	 */
462
-	public function storeRemoteId($shareId, $remoteId) {
463
-		$query = $this->dbConnection->getQueryBuilder();
464
-		$query->insert('federated_reshares')
465
-			->values(
466
-				[
467
-					'share_id' =>  $query->createNamedParameter($shareId),
468
-					'remote_id' => $query->createNamedParameter($remoteId),
469
-				]
470
-			);
471
-		$query->execute();
472
-	}
473
-
474
-	/**
475
-	 * get share ID on remote server for federated re-shares
476
-	 *
477
-	 * @param IShare $share
478
-	 * @return int
479
-	 * @throws ShareNotFound
480
-	 */
481
-	public function getRemoteId(IShare $share) {
482
-		$query = $this->dbConnection->getQueryBuilder();
483
-		$query->select('remote_id')->from('federated_reshares')
484
-			->where($query->expr()->eq('share_id', $query->createNamedParameter((int)$share->getId())));
485
-		$data = $query->execute()->fetch();
486
-
487
-		if (!is_array($data) || !isset($data['remote_id'])) {
488
-			throw new ShareNotFound();
489
-		}
490
-
491
-		return (int)$data['remote_id'];
492
-	}
493
-
494
-	/**
495
-	 * @inheritdoc
496
-	 */
497
-	public function move(IShare $share, $recipient) {
498
-		/*
407
+        $qb = $this->dbConnection->getQueryBuilder();
408
+            $qb->update('share')
409
+                ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
410
+                ->set('permissions', $qb->createNamedParameter($share->getPermissions()))
411
+                ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
412
+                ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
413
+                ->execute();
414
+
415
+        // send the updated permission to the owner/initiator, if they are not the same
416
+        if ($share->getShareOwner() !== $share->getSharedBy()) {
417
+            $this->sendPermissionUpdate($share);
418
+        }
419
+
420
+        return $share;
421
+    }
422
+
423
+    /**
424
+     * send the updated permission to the owner/initiator, if they are not the same
425
+     *
426
+     * @param IShare $share
427
+     * @throws ShareNotFound
428
+     * @throws \OC\HintException
429
+     */
430
+    protected function sendPermissionUpdate(IShare $share) {
431
+        $remoteId = $this->getRemoteId($share);
432
+        // if the local user is the owner we send the permission change to the initiator
433
+        if ($this->userManager->userExists($share->getShareOwner())) {
434
+            list(, $remote) = $this->addressHandler->splitUserRemote($share->getSharedBy());
435
+        } else { // ... if not we send the permission change to the owner
436
+            list(, $remote) = $this->addressHandler->splitUserRemote($share->getShareOwner());
437
+        }
438
+        $this->notifications->sendPermissionChange($remote, $remoteId, $share->getToken(), $share->getPermissions());
439
+    }
440
+
441
+
442
+    /**
443
+     * update successful reShare with the correct token
444
+     *
445
+     * @param int $shareId
446
+     * @param string $token
447
+     */
448
+    protected function updateSuccessfulReShare($shareId, $token) {
449
+        $query = $this->dbConnection->getQueryBuilder();
450
+        $query->update('share')
451
+            ->where($query->expr()->eq('id', $query->createNamedParameter($shareId)))
452
+            ->set('token', $query->createNamedParameter($token))
453
+            ->execute();
454
+    }
455
+
456
+    /**
457
+     * store remote ID in federated reShare table
458
+     *
459
+     * @param $shareId
460
+     * @param $remoteId
461
+     */
462
+    public function storeRemoteId($shareId, $remoteId) {
463
+        $query = $this->dbConnection->getQueryBuilder();
464
+        $query->insert('federated_reshares')
465
+            ->values(
466
+                [
467
+                    'share_id' =>  $query->createNamedParameter($shareId),
468
+                    'remote_id' => $query->createNamedParameter($remoteId),
469
+                ]
470
+            );
471
+        $query->execute();
472
+    }
473
+
474
+    /**
475
+     * get share ID on remote server for federated re-shares
476
+     *
477
+     * @param IShare $share
478
+     * @return int
479
+     * @throws ShareNotFound
480
+     */
481
+    public function getRemoteId(IShare $share) {
482
+        $query = $this->dbConnection->getQueryBuilder();
483
+        $query->select('remote_id')->from('federated_reshares')
484
+            ->where($query->expr()->eq('share_id', $query->createNamedParameter((int)$share->getId())));
485
+        $data = $query->execute()->fetch();
486
+
487
+        if (!is_array($data) || !isset($data['remote_id'])) {
488
+            throw new ShareNotFound();
489
+        }
490
+
491
+        return (int)$data['remote_id'];
492
+    }
493
+
494
+    /**
495
+     * @inheritdoc
496
+     */
497
+    public function move(IShare $share, $recipient) {
498
+        /*
499 499
 		 * This function does nothing yet as it is just for outgoing
500 500
 		 * federated shares.
501 501
 		 */
502
-		return $share;
503
-	}
504
-
505
-	/**
506
-	 * Get all children of this share
507
-	 *
508
-	 * @param IShare $parent
509
-	 * @return IShare[]
510
-	 */
511
-	public function getChildren(IShare $parent) {
512
-		$children = [];
513
-
514
-		$qb = $this->dbConnection->getQueryBuilder();
515
-		$qb->select('*')
516
-			->from('share')
517
-			->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId())))
518
-			->andWhere($qb->expr()->in('share_type', $qb->createNamedParameter($this->supportedShareType, IQueryBuilder::PARAM_INT_ARRAY)))
519
-			->orderBy('id');
520
-
521
-		$cursor = $qb->execute();
522
-		while($data = $cursor->fetch()) {
523
-			$children[] = $this->createShareObject($data);
524
-		}
525
-		$cursor->closeCursor();
526
-
527
-		return $children;
528
-	}
529
-
530
-	/**
531
-	 * Delete a share (owner unShares the file)
532
-	 *
533
-	 * @param IShare $share
534
-	 * @throws ShareNotFound
535
-	 * @throws \OC\HintException
536
-	 */
537
-	public function delete(IShare $share) {
538
-
539
-		list(, $remote) = $this->addressHandler->splitUserRemote($share->getSharedWith());
540
-
541
-		// if the local user is the owner we can send the unShare request directly...
542
-		if ($this->userManager->userExists($share->getShareOwner())) {
543
-			$this->notifications->sendRemoteUnShare($remote, $share->getId(), $share->getToken());
544
-			$this->revokeShare($share, true);
545
-		} else { // ... if not we need to correct ID for the unShare request
546
-			$remoteId = $this->getRemoteId($share);
547
-			$this->notifications->sendRemoteUnShare($remote, $remoteId, $share->getToken());
548
-			$this->revokeShare($share, false);
549
-		}
550
-
551
-		// only remove the share when all messages are send to not lose information
552
-		// about the share to early
553
-		$this->removeShareFromTable($share);
554
-	}
555
-
556
-	/**
557
-	 * in case of a re-share we need to send the other use (initiator or owner)
558
-	 * a message that the file was unshared
559
-	 *
560
-	 * @param IShare $share
561
-	 * @param bool $isOwner the user can either be the owner or the user who re-sahred it
562
-	 * @throws ShareNotFound
563
-	 * @throws \OC\HintException
564
-	 */
565
-	protected function revokeShare($share, $isOwner) {
566
-		if ($this->userManager->userExists($share->getShareOwner() && $this->userManager->userExists($share->getSharedBy()))) {
567
-			// If both the owner and the initiator of the share are local users we don't have to notify anybody else
568
-			return;
569
-		}
570
-
571
-		// also send a unShare request to the initiator, if this is a different user than the owner
572
-		if ($share->getShareOwner() !== $share->getSharedBy()) {
573
-			if ($isOwner) {
574
-				list(, $remote) = $this->addressHandler->splitUserRemote($share->getSharedBy());
575
-			} else {
576
-				list(, $remote) = $this->addressHandler->splitUserRemote($share->getShareOwner());
577
-			}
578
-			$remoteId = $this->getRemoteId($share);
579
-			$this->notifications->sendRevokeShare($remote, $remoteId, $share->getToken());
580
-		}
581
-	}
582
-
583
-	/**
584
-	 * remove share from table
585
-	 *
586
-	 * @param IShare $share
587
-	 */
588
-	public function removeShareFromTable(IShare $share) {
589
-		$this->removeShareFromTableById($share->getId());
590
-	}
591
-
592
-	/**
593
-	 * remove share from table
594
-	 *
595
-	 * @param string $shareId
596
-	 */
597
-	private function removeShareFromTableById($shareId) {
598
-		$qb = $this->dbConnection->getQueryBuilder();
599
-		$qb->delete('share')
600
-			->where($qb->expr()->eq('id', $qb->createNamedParameter($shareId)));
601
-		$qb->execute();
602
-
603
-		$qb->delete('federated_reshares')
604
-			->where($qb->expr()->eq('share_id', $qb->createNamedParameter($shareId)));
605
-		$qb->execute();
606
-	}
607
-
608
-	/**
609
-	 * @inheritdoc
610
-	 */
611
-	public function deleteFromSelf(IShare $share, $recipient) {
612
-		// nothing to do here. Technically deleteFromSelf in the context of federated
613
-		// shares is a umount of a external storage. This is handled here
614
-		// apps/files_sharing/lib/external/manager.php
615
-		// TODO move this code over to this app
616
-	}
617
-
618
-	public function restore(IShare $share, string $recipient): IShare {
619
-		throw new GenericShareException('not implemented');
620
-	}
621
-
622
-
623
-	public function getSharesInFolder($userId, Folder $node, $reshares) {
624
-		$qb = $this->dbConnection->getQueryBuilder();
625
-		$qb->select('*')
626
-			->from('share', 's')
627
-			->andWhere($qb->expr()->orX(
628
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
629
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
630
-			))
631
-			->andWhere(
632
-				$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_REMOTE))
633
-			);
634
-
635
-		/**
636
-		 * Reshares for this user are shares where they are the owner.
637
-		 */
638
-		if ($reshares === false) {
639
-			$qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
640
-		} else {
641
-			$qb->andWhere(
642
-				$qb->expr()->orX(
643
-					$qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
644
-					$qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
645
-				)
646
-			);
647
-		}
648
-
649
-		$qb->innerJoin('s', 'filecache' ,'f', $qb->expr()->eq('s.file_source', 'f.fileid'));
650
-		$qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())));
651
-
652
-		$qb->orderBy('id');
653
-
654
-		$cursor = $qb->execute();
655
-		$shares = [];
656
-		while ($data = $cursor->fetch()) {
657
-			$shares[$data['fileid']][] = $this->createShareObject($data);
658
-		}
659
-		$cursor->closeCursor();
660
-
661
-		return $shares;
662
-	}
663
-
664
-	/**
665
-	 * @inheritdoc
666
-	 */
667
-	public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) {
668
-		$qb = $this->dbConnection->getQueryBuilder();
669
-		$qb->select('*')
670
-			->from('share');
671
-
672
-		$qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter($shareType)));
673
-
674
-		/**
675
-		 * Reshares for this user are shares where they are the owner.
676
-		 */
677
-		if ($reshares === false) {
678
-			//Special case for old shares created via the web UI
679
-			$or1 = $qb->expr()->andX(
680
-				$qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
681
-				$qb->expr()->isNull('uid_initiator')
682
-			);
683
-
684
-			$qb->andWhere(
685
-				$qb->expr()->orX(
686
-					$qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)),
687
-					$or1
688
-				)
689
-			);
690
-		} else {
691
-			$qb->andWhere(
692
-				$qb->expr()->orX(
693
-					$qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
694
-					$qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
695
-				)
696
-			);
697
-		}
698
-
699
-		if ($node !== null) {
700
-			$qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
701
-		}
702
-
703
-		if ($limit !== -1) {
704
-			$qb->setMaxResults($limit);
705
-		}
706
-
707
-		$qb->setFirstResult($offset);
708
-		$qb->orderBy('id');
709
-
710
-		$cursor = $qb->execute();
711
-		$shares = [];
712
-		while($data = $cursor->fetch()) {
713
-			$shares[] = $this->createShareObject($data);
714
-		}
715
-		$cursor->closeCursor();
716
-
717
-		return $shares;
718
-	}
719
-
720
-	/**
721
-	 * @inheritdoc
722
-	 */
723
-	public function getShareById($id, $recipientId = null) {
724
-		$qb = $this->dbConnection->getQueryBuilder();
725
-
726
-		$qb->select('*')
727
-			->from('share')
728
-			->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
729
-			->andWhere($qb->expr()->in('share_type', $qb->createNamedParameter($this->supportedShareType, IQueryBuilder::PARAM_INT_ARRAY)));
730
-
731
-		$cursor = $qb->execute();
732
-		$data = $cursor->fetch();
733
-		$cursor->closeCursor();
734
-
735
-		if ($data === false) {
736
-			throw new ShareNotFound('Can not find share with ID: ' . $id);
737
-		}
738
-
739
-		try {
740
-			$share = $this->createShareObject($data);
741
-		} catch (InvalidShare $e) {
742
-			throw new ShareNotFound();
743
-		}
744
-
745
-		return $share;
746
-	}
747
-
748
-	/**
749
-	 * Get shares for a given path
750
-	 *
751
-	 * @param \OCP\Files\Node $path
752
-	 * @return IShare[]
753
-	 */
754
-	public function getSharesByPath(Node $path) {
755
-		$qb = $this->dbConnection->getQueryBuilder();
756
-
757
-		// get federated user shares
758
-		$cursor = $qb->select('*')
759
-			->from('share')
760
-			->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId())))
761
-			->andWhere($qb->expr()->in('share_type', $qb->createNamedParameter($this->supportedShareType, IQueryBuilder::PARAM_INT_ARRAY)))
762
-			->execute();
763
-
764
-		$shares = [];
765
-		while($data = $cursor->fetch()) {
766
-			$shares[] = $this->createShareObject($data);
767
-		}
768
-		$cursor->closeCursor();
769
-
770
-		return $shares;
771
-	}
772
-
773
-	/**
774
-	 * @inheritdoc
775
-	 */
776
-	public function getSharedWith($userId, $shareType, $node, $limit, $offset) {
777
-		/** @var IShare[] $shares */
778
-		$shares = [];
779
-
780
-		//Get shares directly with this user
781
-		$qb = $this->dbConnection->getQueryBuilder();
782
-		$qb->select('*')
783
-			->from('share');
784
-
785
-		// Order by id
786
-		$qb->orderBy('id');
787
-
788
-		// Set limit and offset
789
-		if ($limit !== -1) {
790
-			$qb->setMaxResults($limit);
791
-		}
792
-		$qb->setFirstResult($offset);
793
-
794
-		$qb->where($qb->expr()->in('share_type', $qb->createNamedParameter($this->supportedShareType, IQueryBuilder::PARAM_INT_ARRAY)));
795
-		$qb->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)));
796
-
797
-		// Filter by node if provided
798
-		if ($node !== null) {
799
-			$qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
800
-		}
801
-
802
-		$cursor = $qb->execute();
803
-
804
-		while($data = $cursor->fetch()) {
805
-			$shares[] = $this->createShareObject($data);
806
-		}
807
-		$cursor->closeCursor();
808
-
809
-
810
-		return $shares;
811
-	}
812
-
813
-	/**
814
-	 * Get a share by token
815
-	 *
816
-	 * @param string $token
817
-	 * @return IShare
818
-	 * @throws ShareNotFound
819
-	 */
820
-	public function getShareByToken($token) {
821
-		$qb = $this->dbConnection->getQueryBuilder();
822
-
823
-		$cursor = $qb->select('*')
824
-			->from('share')
825
-			->where($qb->expr()->in('share_type', $qb->createNamedParameter($this->supportedShareType, IQueryBuilder::PARAM_INT_ARRAY)))
826
-			->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token)))
827
-			->execute();
828
-
829
-		$data = $cursor->fetch();
830
-
831
-		if ($data === false) {
832
-			throw new ShareNotFound('Share not found', $this->l->t('Could not find share'));
833
-		}
834
-
835
-		try {
836
-			$share = $this->createShareObject($data);
837
-		} catch (InvalidShare $e) {
838
-			throw new ShareNotFound('Share not found', $this->l->t('Could not find share'));
839
-		}
840
-
841
-		return $share;
842
-	}
843
-
844
-	/**
845
-	 * get database row of a give share
846
-	 *
847
-	 * @param $id
848
-	 * @return array
849
-	 * @throws ShareNotFound
850
-	 */
851
-	private function getRawShare($id) {
852
-
853
-		// Now fetch the inserted share and create a complete share object
854
-		$qb = $this->dbConnection->getQueryBuilder();
855
-		$qb->select('*')
856
-			->from('share')
857
-			->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
858
-
859
-		$cursor = $qb->execute();
860
-		$data = $cursor->fetch();
861
-		$cursor->closeCursor();
862
-
863
-		if ($data === false) {
864
-			throw new ShareNotFound;
865
-		}
866
-
867
-		return $data;
868
-	}
869
-
870
-	/**
871
-	 * Create a share object from an database row
872
-	 *
873
-	 * @param array $data
874
-	 * @return IShare
875
-	 * @throws InvalidShare
876
-	 * @throws ShareNotFound
877
-	 */
878
-	private function createShareObject($data) {
879
-
880
-		$share = new Share($this->rootFolder, $this->userManager);
881
-		$share->setId((int)$data['id'])
882
-			->setShareType((int)$data['share_type'])
883
-			->setPermissions((int)$data['permissions'])
884
-			->setTarget($data['file_target'])
885
-			->setMailSend((bool)$data['mail_send'])
886
-			->setToken($data['token']);
887
-
888
-		$shareTime = new \DateTime();
889
-		$shareTime->setTimestamp((int)$data['stime']);
890
-		$share->setShareTime($shareTime);
891
-		$share->setSharedWith($data['share_with']);
892
-
893
-		if ($data['uid_initiator'] !== null) {
894
-			$share->setShareOwner($data['uid_owner']);
895
-			$share->setSharedBy($data['uid_initiator']);
896
-		} else {
897
-			//OLD SHARE
898
-			$share->setSharedBy($data['uid_owner']);
899
-			$path = $this->getNode($share->getSharedBy(), (int)$data['file_source']);
900
-
901
-			$owner = $path->getOwner();
902
-			$share->setShareOwner($owner->getUID());
903
-		}
904
-
905
-		$share->setNodeId((int)$data['file_source']);
906
-		$share->setNodeType($data['item_type']);
907
-
908
-		$share->setProviderId($this->identifier());
909
-
910
-		return $share;
911
-	}
912
-
913
-	/**
914
-	 * Get the node with file $id for $user
915
-	 *
916
-	 * @param string $userId
917
-	 * @param int $id
918
-	 * @return \OCP\Files\File|\OCP\Files\Folder
919
-	 * @throws InvalidShare
920
-	 */
921
-	private function getNode($userId, $id) {
922
-		try {
923
-			$userFolder = $this->rootFolder->getUserFolder($userId);
924
-		} catch (NotFoundException $e) {
925
-			throw new InvalidShare();
926
-		}
927
-
928
-		$nodes = $userFolder->getById($id);
929
-
930
-		if (empty($nodes)) {
931
-			throw new InvalidShare();
932
-		}
933
-
934
-		return $nodes[0];
935
-	}
936
-
937
-	/**
938
-	 * A user is deleted from the system
939
-	 * So clean up the relevant shares.
940
-	 *
941
-	 * @param string $uid
942
-	 * @param int $shareType
943
-	 */
944
-	public function userDeleted($uid, $shareType) {
945
-		//TODO: probabaly a good idea to send unshare info to remote servers
946
-
947
-		$qb = $this->dbConnection->getQueryBuilder();
948
-
949
-		$qb->delete('share')
950
-			->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_REMOTE)))
951
-			->andWhere($qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)))
952
-			->execute();
953
-	}
954
-
955
-	/**
956
-	 * This provider does not handle groups
957
-	 *
958
-	 * @param string $gid
959
-	 */
960
-	public function groupDeleted($gid) {
961
-		// We don't handle groups here
962
-	}
963
-
964
-	/**
965
-	 * This provider does not handle groups
966
-	 *
967
-	 * @param string $uid
968
-	 * @param string $gid
969
-	 */
970
-	public function userDeletedFromGroup($uid, $gid) {
971
-		// We don't handle groups here
972
-	}
973
-
974
-	/**
975
-	 * check if users from other Nextcloud instances are allowed to mount public links share by this instance
976
-	 *
977
-	 * @return bool
978
-	 */
979
-	public function isOutgoingServer2serverShareEnabled() {
980
-		if ($this->gsConfig->onlyInternalFederation()) {
981
-			return false;
982
-		}
983
-		$result = $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes');
984
-		return ($result === 'yes');
985
-	}
986
-
987
-	/**
988
-	 * check if users are allowed to mount public links from other Nextclouds
989
-	 *
990
-	 * @return bool
991
-	 */
992
-	public function isIncomingServer2serverShareEnabled() {
993
-		if ($this->gsConfig->onlyInternalFederation()) {
994
-			return false;
995
-		}
996
-		$result = $this->config->getAppValue('files_sharing', 'incoming_server2server_share_enabled', 'yes');
997
-		return ($result === 'yes');
998
-	}
999
-
1000
-
1001
-	/**
1002
-	 * check if users from other Nextcloud instances are allowed to send federated group shares
1003
-	 *
1004
-	 * @return bool
1005
-	 */
1006
-	public function isOutgoingServer2serverGroupShareEnabled() {
1007
-		if ($this->gsConfig->onlyInternalFederation()) {
1008
-			return false;
1009
-		}
1010
-		$result = $this->config->getAppValue('files_sharing', 'outgoing_server2server_group_share_enabled', 'no');
1011
-		return ($result === 'yes');
1012
-	}
1013
-
1014
-	/**
1015
-	 * check if users are allowed to receive federated group shares
1016
-	 *
1017
-	 * @return bool
1018
-	 */
1019
-	public function isIncomingServer2serverGroupShareEnabled() {
1020
-		if ($this->gsConfig->onlyInternalFederation()) {
1021
-			return false;
1022
-		}
1023
-		$result = $this->config->getAppValue('files_sharing', 'incoming_server2server_group_share_enabled', 'no');
1024
-		return ($result === 'yes');
1025
-	}
1026
-
1027
-	/**
1028
-	 * check if federated group sharing is supported, therefore the OCM API need to be enabled
1029
-	 *
1030
-	 * @return bool
1031
-	 */
1032
-	public function isFederatedGroupSharingSupported() {
1033
-		return $this->cloudFederationProviderManager->isReady();
1034
-	}
1035
-
1036
-	/**
1037
-	 * Check if querying sharees on the lookup server is enabled
1038
-	 *
1039
-	 * @return bool
1040
-	 */
1041
-	public function isLookupServerQueriesEnabled() {
1042
-		// in a global scale setup we should always query the lookup server
1043
-		if ($this->gsConfig->isGlobalScaleEnabled()) {
1044
-			return true;
1045
-		}
1046
-		$result = $this->config->getAppValue('files_sharing', 'lookupServerEnabled', 'no');
1047
-		return ($result === 'yes');
1048
-	}
1049
-
1050
-
1051
-	/**
1052
-	 * Check if it is allowed to publish user specific data to the lookup server
1053
-	 *
1054
-	 * @return bool
1055
-	 */
1056
-	public function isLookupServerUploadEnabled() {
1057
-		// in a global scale setup the admin is responsible to keep the lookup server up-to-date
1058
-		if ($this->gsConfig->isGlobalScaleEnabled()) {
1059
-			return false;
1060
-		}
1061
-		$result = $this->config->getAppValue('files_sharing', 'lookupServerUploadEnabled', 'yes');
1062
-		return ($result === 'yes');
1063
-	}
1064
-
1065
-	/**
1066
-	 * @inheritdoc
1067
-	 */
1068
-	public function getAccessList($nodes, $currentAccess) {
1069
-		$ids = [];
1070
-		foreach ($nodes as $node) {
1071
-			$ids[] = $node->getId();
1072
-		}
1073
-
1074
-		$qb = $this->dbConnection->getQueryBuilder();
1075
-		$qb->select('share_with', 'token', 'file_source')
1076
-			->from('share')
1077
-			->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_REMOTE)))
1078
-			->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
1079
-			->andWhere($qb->expr()->orX(
1080
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
1081
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
1082
-			));
1083
-		$cursor = $qb->execute();
1084
-
1085
-		if ($currentAccess === false) {
1086
-			$remote = $cursor->fetch() !== false;
1087
-			$cursor->closeCursor();
1088
-
1089
-			return ['remote' => $remote];
1090
-		}
1091
-
1092
-		$remote = [];
1093
-		while ($row = $cursor->fetch()) {
1094
-			$remote[$row['share_with']] = [
1095
-				'node_id' => $row['file_source'],
1096
-				'token' => $row['token'],
1097
-			];
1098
-		}
1099
-		$cursor->closeCursor();
1100
-
1101
-		return ['remote' => $remote];
1102
-	}
1103
-
1104
-	public function getAllShares(): iterable {
1105
-		$qb = $this->dbConnection->getQueryBuilder();
1106
-
1107
-		$qb->select('*')
1108
-			->from('share')
1109
-			->where(
1110
-				$qb->expr()->orX(
1111
-					$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_REMOTE)),
1112
-					$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_REMOTE_GROUP))
1113
-				)
1114
-			);
1115
-
1116
-		$cursor = $qb->execute();
1117
-		while($data = $cursor->fetch()) {
1118
-			try {
1119
-				$share = $this->createShareObject($data);
1120
-			} catch (InvalidShare $e) {
1121
-				continue;
1122
-			} catch (ShareNotFound $e) {
1123
-				continue;
1124
-			}
1125
-
1126
-			yield $share;
1127
-		}
1128
-		$cursor->closeCursor();
1129
-	}
502
+        return $share;
503
+    }
504
+
505
+    /**
506
+     * Get all children of this share
507
+     *
508
+     * @param IShare $parent
509
+     * @return IShare[]
510
+     */
511
+    public function getChildren(IShare $parent) {
512
+        $children = [];
513
+
514
+        $qb = $this->dbConnection->getQueryBuilder();
515
+        $qb->select('*')
516
+            ->from('share')
517
+            ->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId())))
518
+            ->andWhere($qb->expr()->in('share_type', $qb->createNamedParameter($this->supportedShareType, IQueryBuilder::PARAM_INT_ARRAY)))
519
+            ->orderBy('id');
520
+
521
+        $cursor = $qb->execute();
522
+        while($data = $cursor->fetch()) {
523
+            $children[] = $this->createShareObject($data);
524
+        }
525
+        $cursor->closeCursor();
526
+
527
+        return $children;
528
+    }
529
+
530
+    /**
531
+     * Delete a share (owner unShares the file)
532
+     *
533
+     * @param IShare $share
534
+     * @throws ShareNotFound
535
+     * @throws \OC\HintException
536
+     */
537
+    public function delete(IShare $share) {
538
+
539
+        list(, $remote) = $this->addressHandler->splitUserRemote($share->getSharedWith());
540
+
541
+        // if the local user is the owner we can send the unShare request directly...
542
+        if ($this->userManager->userExists($share->getShareOwner())) {
543
+            $this->notifications->sendRemoteUnShare($remote, $share->getId(), $share->getToken());
544
+            $this->revokeShare($share, true);
545
+        } else { // ... if not we need to correct ID for the unShare request
546
+            $remoteId = $this->getRemoteId($share);
547
+            $this->notifications->sendRemoteUnShare($remote, $remoteId, $share->getToken());
548
+            $this->revokeShare($share, false);
549
+        }
550
+
551
+        // only remove the share when all messages are send to not lose information
552
+        // about the share to early
553
+        $this->removeShareFromTable($share);
554
+    }
555
+
556
+    /**
557
+     * in case of a re-share we need to send the other use (initiator or owner)
558
+     * a message that the file was unshared
559
+     *
560
+     * @param IShare $share
561
+     * @param bool $isOwner the user can either be the owner or the user who re-sahred it
562
+     * @throws ShareNotFound
563
+     * @throws \OC\HintException
564
+     */
565
+    protected function revokeShare($share, $isOwner) {
566
+        if ($this->userManager->userExists($share->getShareOwner() && $this->userManager->userExists($share->getSharedBy()))) {
567
+            // If both the owner and the initiator of the share are local users we don't have to notify anybody else
568
+            return;
569
+        }
570
+
571
+        // also send a unShare request to the initiator, if this is a different user than the owner
572
+        if ($share->getShareOwner() !== $share->getSharedBy()) {
573
+            if ($isOwner) {
574
+                list(, $remote) = $this->addressHandler->splitUserRemote($share->getSharedBy());
575
+            } else {
576
+                list(, $remote) = $this->addressHandler->splitUserRemote($share->getShareOwner());
577
+            }
578
+            $remoteId = $this->getRemoteId($share);
579
+            $this->notifications->sendRevokeShare($remote, $remoteId, $share->getToken());
580
+        }
581
+    }
582
+
583
+    /**
584
+     * remove share from table
585
+     *
586
+     * @param IShare $share
587
+     */
588
+    public function removeShareFromTable(IShare $share) {
589
+        $this->removeShareFromTableById($share->getId());
590
+    }
591
+
592
+    /**
593
+     * remove share from table
594
+     *
595
+     * @param string $shareId
596
+     */
597
+    private function removeShareFromTableById($shareId) {
598
+        $qb = $this->dbConnection->getQueryBuilder();
599
+        $qb->delete('share')
600
+            ->where($qb->expr()->eq('id', $qb->createNamedParameter($shareId)));
601
+        $qb->execute();
602
+
603
+        $qb->delete('federated_reshares')
604
+            ->where($qb->expr()->eq('share_id', $qb->createNamedParameter($shareId)));
605
+        $qb->execute();
606
+    }
607
+
608
+    /**
609
+     * @inheritdoc
610
+     */
611
+    public function deleteFromSelf(IShare $share, $recipient) {
612
+        // nothing to do here. Technically deleteFromSelf in the context of federated
613
+        // shares is a umount of a external storage. This is handled here
614
+        // apps/files_sharing/lib/external/manager.php
615
+        // TODO move this code over to this app
616
+    }
617
+
618
+    public function restore(IShare $share, string $recipient): IShare {
619
+        throw new GenericShareException('not implemented');
620
+    }
621
+
622
+
623
+    public function getSharesInFolder($userId, Folder $node, $reshares) {
624
+        $qb = $this->dbConnection->getQueryBuilder();
625
+        $qb->select('*')
626
+            ->from('share', 's')
627
+            ->andWhere($qb->expr()->orX(
628
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
629
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
630
+            ))
631
+            ->andWhere(
632
+                $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_REMOTE))
633
+            );
634
+
635
+        /**
636
+         * Reshares for this user are shares where they are the owner.
637
+         */
638
+        if ($reshares === false) {
639
+            $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
640
+        } else {
641
+            $qb->andWhere(
642
+                $qb->expr()->orX(
643
+                    $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
644
+                    $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
645
+                )
646
+            );
647
+        }
648
+
649
+        $qb->innerJoin('s', 'filecache' ,'f', $qb->expr()->eq('s.file_source', 'f.fileid'));
650
+        $qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())));
651
+
652
+        $qb->orderBy('id');
653
+
654
+        $cursor = $qb->execute();
655
+        $shares = [];
656
+        while ($data = $cursor->fetch()) {
657
+            $shares[$data['fileid']][] = $this->createShareObject($data);
658
+        }
659
+        $cursor->closeCursor();
660
+
661
+        return $shares;
662
+    }
663
+
664
+    /**
665
+     * @inheritdoc
666
+     */
667
+    public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) {
668
+        $qb = $this->dbConnection->getQueryBuilder();
669
+        $qb->select('*')
670
+            ->from('share');
671
+
672
+        $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter($shareType)));
673
+
674
+        /**
675
+         * Reshares for this user are shares where they are the owner.
676
+         */
677
+        if ($reshares === false) {
678
+            //Special case for old shares created via the web UI
679
+            $or1 = $qb->expr()->andX(
680
+                $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
681
+                $qb->expr()->isNull('uid_initiator')
682
+            );
683
+
684
+            $qb->andWhere(
685
+                $qb->expr()->orX(
686
+                    $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)),
687
+                    $or1
688
+                )
689
+            );
690
+        } else {
691
+            $qb->andWhere(
692
+                $qb->expr()->orX(
693
+                    $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
694
+                    $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
695
+                )
696
+            );
697
+        }
698
+
699
+        if ($node !== null) {
700
+            $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
701
+        }
702
+
703
+        if ($limit !== -1) {
704
+            $qb->setMaxResults($limit);
705
+        }
706
+
707
+        $qb->setFirstResult($offset);
708
+        $qb->orderBy('id');
709
+
710
+        $cursor = $qb->execute();
711
+        $shares = [];
712
+        while($data = $cursor->fetch()) {
713
+            $shares[] = $this->createShareObject($data);
714
+        }
715
+        $cursor->closeCursor();
716
+
717
+        return $shares;
718
+    }
719
+
720
+    /**
721
+     * @inheritdoc
722
+     */
723
+    public function getShareById($id, $recipientId = null) {
724
+        $qb = $this->dbConnection->getQueryBuilder();
725
+
726
+        $qb->select('*')
727
+            ->from('share')
728
+            ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
729
+            ->andWhere($qb->expr()->in('share_type', $qb->createNamedParameter($this->supportedShareType, IQueryBuilder::PARAM_INT_ARRAY)));
730
+
731
+        $cursor = $qb->execute();
732
+        $data = $cursor->fetch();
733
+        $cursor->closeCursor();
734
+
735
+        if ($data === false) {
736
+            throw new ShareNotFound('Can not find share with ID: ' . $id);
737
+        }
738
+
739
+        try {
740
+            $share = $this->createShareObject($data);
741
+        } catch (InvalidShare $e) {
742
+            throw new ShareNotFound();
743
+        }
744
+
745
+        return $share;
746
+    }
747
+
748
+    /**
749
+     * Get shares for a given path
750
+     *
751
+     * @param \OCP\Files\Node $path
752
+     * @return IShare[]
753
+     */
754
+    public function getSharesByPath(Node $path) {
755
+        $qb = $this->dbConnection->getQueryBuilder();
756
+
757
+        // get federated user shares
758
+        $cursor = $qb->select('*')
759
+            ->from('share')
760
+            ->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId())))
761
+            ->andWhere($qb->expr()->in('share_type', $qb->createNamedParameter($this->supportedShareType, IQueryBuilder::PARAM_INT_ARRAY)))
762
+            ->execute();
763
+
764
+        $shares = [];
765
+        while($data = $cursor->fetch()) {
766
+            $shares[] = $this->createShareObject($data);
767
+        }
768
+        $cursor->closeCursor();
769
+
770
+        return $shares;
771
+    }
772
+
773
+    /**
774
+     * @inheritdoc
775
+     */
776
+    public function getSharedWith($userId, $shareType, $node, $limit, $offset) {
777
+        /** @var IShare[] $shares */
778
+        $shares = [];
779
+
780
+        //Get shares directly with this user
781
+        $qb = $this->dbConnection->getQueryBuilder();
782
+        $qb->select('*')
783
+            ->from('share');
784
+
785
+        // Order by id
786
+        $qb->orderBy('id');
787
+
788
+        // Set limit and offset
789
+        if ($limit !== -1) {
790
+            $qb->setMaxResults($limit);
791
+        }
792
+        $qb->setFirstResult($offset);
793
+
794
+        $qb->where($qb->expr()->in('share_type', $qb->createNamedParameter($this->supportedShareType, IQueryBuilder::PARAM_INT_ARRAY)));
795
+        $qb->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)));
796
+
797
+        // Filter by node if provided
798
+        if ($node !== null) {
799
+            $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
800
+        }
801
+
802
+        $cursor = $qb->execute();
803
+
804
+        while($data = $cursor->fetch()) {
805
+            $shares[] = $this->createShareObject($data);
806
+        }
807
+        $cursor->closeCursor();
808
+
809
+
810
+        return $shares;
811
+    }
812
+
813
+    /**
814
+     * Get a share by token
815
+     *
816
+     * @param string $token
817
+     * @return IShare
818
+     * @throws ShareNotFound
819
+     */
820
+    public function getShareByToken($token) {
821
+        $qb = $this->dbConnection->getQueryBuilder();
822
+
823
+        $cursor = $qb->select('*')
824
+            ->from('share')
825
+            ->where($qb->expr()->in('share_type', $qb->createNamedParameter($this->supportedShareType, IQueryBuilder::PARAM_INT_ARRAY)))
826
+            ->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token)))
827
+            ->execute();
828
+
829
+        $data = $cursor->fetch();
830
+
831
+        if ($data === false) {
832
+            throw new ShareNotFound('Share not found', $this->l->t('Could not find share'));
833
+        }
834
+
835
+        try {
836
+            $share = $this->createShareObject($data);
837
+        } catch (InvalidShare $e) {
838
+            throw new ShareNotFound('Share not found', $this->l->t('Could not find share'));
839
+        }
840
+
841
+        return $share;
842
+    }
843
+
844
+    /**
845
+     * get database row of a give share
846
+     *
847
+     * @param $id
848
+     * @return array
849
+     * @throws ShareNotFound
850
+     */
851
+    private function getRawShare($id) {
852
+
853
+        // Now fetch the inserted share and create a complete share object
854
+        $qb = $this->dbConnection->getQueryBuilder();
855
+        $qb->select('*')
856
+            ->from('share')
857
+            ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
858
+
859
+        $cursor = $qb->execute();
860
+        $data = $cursor->fetch();
861
+        $cursor->closeCursor();
862
+
863
+        if ($data === false) {
864
+            throw new ShareNotFound;
865
+        }
866
+
867
+        return $data;
868
+    }
869
+
870
+    /**
871
+     * Create a share object from an database row
872
+     *
873
+     * @param array $data
874
+     * @return IShare
875
+     * @throws InvalidShare
876
+     * @throws ShareNotFound
877
+     */
878
+    private function createShareObject($data) {
879
+
880
+        $share = new Share($this->rootFolder, $this->userManager);
881
+        $share->setId((int)$data['id'])
882
+            ->setShareType((int)$data['share_type'])
883
+            ->setPermissions((int)$data['permissions'])
884
+            ->setTarget($data['file_target'])
885
+            ->setMailSend((bool)$data['mail_send'])
886
+            ->setToken($data['token']);
887
+
888
+        $shareTime = new \DateTime();
889
+        $shareTime->setTimestamp((int)$data['stime']);
890
+        $share->setShareTime($shareTime);
891
+        $share->setSharedWith($data['share_with']);
892
+
893
+        if ($data['uid_initiator'] !== null) {
894
+            $share->setShareOwner($data['uid_owner']);
895
+            $share->setSharedBy($data['uid_initiator']);
896
+        } else {
897
+            //OLD SHARE
898
+            $share->setSharedBy($data['uid_owner']);
899
+            $path = $this->getNode($share->getSharedBy(), (int)$data['file_source']);
900
+
901
+            $owner = $path->getOwner();
902
+            $share->setShareOwner($owner->getUID());
903
+        }
904
+
905
+        $share->setNodeId((int)$data['file_source']);
906
+        $share->setNodeType($data['item_type']);
907
+
908
+        $share->setProviderId($this->identifier());
909
+
910
+        return $share;
911
+    }
912
+
913
+    /**
914
+     * Get the node with file $id for $user
915
+     *
916
+     * @param string $userId
917
+     * @param int $id
918
+     * @return \OCP\Files\File|\OCP\Files\Folder
919
+     * @throws InvalidShare
920
+     */
921
+    private function getNode($userId, $id) {
922
+        try {
923
+            $userFolder = $this->rootFolder->getUserFolder($userId);
924
+        } catch (NotFoundException $e) {
925
+            throw new InvalidShare();
926
+        }
927
+
928
+        $nodes = $userFolder->getById($id);
929
+
930
+        if (empty($nodes)) {
931
+            throw new InvalidShare();
932
+        }
933
+
934
+        return $nodes[0];
935
+    }
936
+
937
+    /**
938
+     * A user is deleted from the system
939
+     * So clean up the relevant shares.
940
+     *
941
+     * @param string $uid
942
+     * @param int $shareType
943
+     */
944
+    public function userDeleted($uid, $shareType) {
945
+        //TODO: probabaly a good idea to send unshare info to remote servers
946
+
947
+        $qb = $this->dbConnection->getQueryBuilder();
948
+
949
+        $qb->delete('share')
950
+            ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_REMOTE)))
951
+            ->andWhere($qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)))
952
+            ->execute();
953
+    }
954
+
955
+    /**
956
+     * This provider does not handle groups
957
+     *
958
+     * @param string $gid
959
+     */
960
+    public function groupDeleted($gid) {
961
+        // We don't handle groups here
962
+    }
963
+
964
+    /**
965
+     * This provider does not handle groups
966
+     *
967
+     * @param string $uid
968
+     * @param string $gid
969
+     */
970
+    public function userDeletedFromGroup($uid, $gid) {
971
+        // We don't handle groups here
972
+    }
973
+
974
+    /**
975
+     * check if users from other Nextcloud instances are allowed to mount public links share by this instance
976
+     *
977
+     * @return bool
978
+     */
979
+    public function isOutgoingServer2serverShareEnabled() {
980
+        if ($this->gsConfig->onlyInternalFederation()) {
981
+            return false;
982
+        }
983
+        $result = $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes');
984
+        return ($result === 'yes');
985
+    }
986
+
987
+    /**
988
+     * check if users are allowed to mount public links from other Nextclouds
989
+     *
990
+     * @return bool
991
+     */
992
+    public function isIncomingServer2serverShareEnabled() {
993
+        if ($this->gsConfig->onlyInternalFederation()) {
994
+            return false;
995
+        }
996
+        $result = $this->config->getAppValue('files_sharing', 'incoming_server2server_share_enabled', 'yes');
997
+        return ($result === 'yes');
998
+    }
999
+
1000
+
1001
+    /**
1002
+     * check if users from other Nextcloud instances are allowed to send federated group shares
1003
+     *
1004
+     * @return bool
1005
+     */
1006
+    public function isOutgoingServer2serverGroupShareEnabled() {
1007
+        if ($this->gsConfig->onlyInternalFederation()) {
1008
+            return false;
1009
+        }
1010
+        $result = $this->config->getAppValue('files_sharing', 'outgoing_server2server_group_share_enabled', 'no');
1011
+        return ($result === 'yes');
1012
+    }
1013
+
1014
+    /**
1015
+     * check if users are allowed to receive federated group shares
1016
+     *
1017
+     * @return bool
1018
+     */
1019
+    public function isIncomingServer2serverGroupShareEnabled() {
1020
+        if ($this->gsConfig->onlyInternalFederation()) {
1021
+            return false;
1022
+        }
1023
+        $result = $this->config->getAppValue('files_sharing', 'incoming_server2server_group_share_enabled', 'no');
1024
+        return ($result === 'yes');
1025
+    }
1026
+
1027
+    /**
1028
+     * check if federated group sharing is supported, therefore the OCM API need to be enabled
1029
+     *
1030
+     * @return bool
1031
+     */
1032
+    public function isFederatedGroupSharingSupported() {
1033
+        return $this->cloudFederationProviderManager->isReady();
1034
+    }
1035
+
1036
+    /**
1037
+     * Check if querying sharees on the lookup server is enabled
1038
+     *
1039
+     * @return bool
1040
+     */
1041
+    public function isLookupServerQueriesEnabled() {
1042
+        // in a global scale setup we should always query the lookup server
1043
+        if ($this->gsConfig->isGlobalScaleEnabled()) {
1044
+            return true;
1045
+        }
1046
+        $result = $this->config->getAppValue('files_sharing', 'lookupServerEnabled', 'no');
1047
+        return ($result === 'yes');
1048
+    }
1049
+
1050
+
1051
+    /**
1052
+     * Check if it is allowed to publish user specific data to the lookup server
1053
+     *
1054
+     * @return bool
1055
+     */
1056
+    public function isLookupServerUploadEnabled() {
1057
+        // in a global scale setup the admin is responsible to keep the lookup server up-to-date
1058
+        if ($this->gsConfig->isGlobalScaleEnabled()) {
1059
+            return false;
1060
+        }
1061
+        $result = $this->config->getAppValue('files_sharing', 'lookupServerUploadEnabled', 'yes');
1062
+        return ($result === 'yes');
1063
+    }
1064
+
1065
+    /**
1066
+     * @inheritdoc
1067
+     */
1068
+    public function getAccessList($nodes, $currentAccess) {
1069
+        $ids = [];
1070
+        foreach ($nodes as $node) {
1071
+            $ids[] = $node->getId();
1072
+        }
1073
+
1074
+        $qb = $this->dbConnection->getQueryBuilder();
1075
+        $qb->select('share_with', 'token', 'file_source')
1076
+            ->from('share')
1077
+            ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_REMOTE)))
1078
+            ->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
1079
+            ->andWhere($qb->expr()->orX(
1080
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
1081
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
1082
+            ));
1083
+        $cursor = $qb->execute();
1084
+
1085
+        if ($currentAccess === false) {
1086
+            $remote = $cursor->fetch() !== false;
1087
+            $cursor->closeCursor();
1088
+
1089
+            return ['remote' => $remote];
1090
+        }
1091
+
1092
+        $remote = [];
1093
+        while ($row = $cursor->fetch()) {
1094
+            $remote[$row['share_with']] = [
1095
+                'node_id' => $row['file_source'],
1096
+                'token' => $row['token'],
1097
+            ];
1098
+        }
1099
+        $cursor->closeCursor();
1100
+
1101
+        return ['remote' => $remote];
1102
+    }
1103
+
1104
+    public function getAllShares(): iterable {
1105
+        $qb = $this->dbConnection->getQueryBuilder();
1106
+
1107
+        $qb->select('*')
1108
+            ->from('share')
1109
+            ->where(
1110
+                $qb->expr()->orX(
1111
+                    $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_REMOTE)),
1112
+                    $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_REMOTE_GROUP))
1113
+                )
1114
+            );
1115
+
1116
+        $cursor = $qb->execute();
1117
+        while($data = $cursor->fetch()) {
1118
+            try {
1119
+                $share = $this->createShareObject($data);
1120
+            } catch (InvalidShare $e) {
1121
+                continue;
1122
+            } catch (ShareNotFound $e) {
1123
+                continue;
1124
+            }
1125
+
1126
+            yield $share;
1127
+        }
1128
+        $cursor->closeCursor();
1129
+    }
1130 1130
 }
Please login to merge, or discard this patch.
Spacing   +20 added lines, -20 removed lines patch added patch discarded remove patch
@@ -214,7 +214,7 @@  discard block
 block discarded – undo
214 214
 		if ($remoteShare) {
215 215
 			try {
216 216
 				$ownerCloudId = $this->cloudIdManager->getCloudId($remoteShare['owner'], $remoteShare['remote']);
217
-				$shareId = $this->addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $ownerCloudId->getId(), $permissions, 'tmp_token_' . time(), $shareType);
217
+				$shareId = $this->addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $ownerCloudId->getId(), $permissions, 'tmp_token_'.time(), $shareType);
218 218
 				$share->setId($shareId);
219 219
 				list($token, $remoteId) = $this->askOwnerToReShare($shareWith, $share, $shareId);
220 220
 				// remote share was create successfully if we get a valid token as return
@@ -296,7 +296,7 @@  discard block
 block discarded – undo
296 296
 			$failure = true;
297 297
 		}
298 298
 
299
-		if($failure) {
299
+		if ($failure) {
300 300
 			$this->removeShareFromTableById($shareId);
301 301
 			$message_t = $this->l->t('Sharing %1$s failed, could not find %2$s, maybe the server is currently unreachable or uses a self-signed certificate.',
302 302
 				[$share->getNode()->getName(), $share->getSharedWith()]);
@@ -348,7 +348,7 @@  discard block
 block discarded – undo
348 348
 			->andWhere($query->expr()->eq('mountpoint', $query->createNamedParameter($share->getTarget())));
349 349
 		$result = $query->execute()->fetchAll();
350 350
 
351
-		if (isset($result[0]) && (int)$result[0]['remote_id'] > 0) {
351
+		if (isset($result[0]) && (int) $result[0]['remote_id'] > 0) {
352 352
 			return $result[0];
353 353
 		}
354 354
 
@@ -391,7 +391,7 @@  discard block
 block discarded – undo
391 391
 		$qb->execute();
392 392
 		$id = $qb->getLastInsertId();
393 393
 
394
-		return (int)$id;
394
+		return (int) $id;
395 395
 	}
396 396
 
397 397
 	/**
@@ -481,14 +481,14 @@  discard block
 block discarded – undo
481 481
 	public function getRemoteId(IShare $share) {
482 482
 		$query = $this->dbConnection->getQueryBuilder();
483 483
 		$query->select('remote_id')->from('federated_reshares')
484
-			->where($query->expr()->eq('share_id', $query->createNamedParameter((int)$share->getId())));
484
+			->where($query->expr()->eq('share_id', $query->createNamedParameter((int) $share->getId())));
485 485
 		$data = $query->execute()->fetch();
486 486
 
487 487
 		if (!is_array($data) || !isset($data['remote_id'])) {
488 488
 			throw new ShareNotFound();
489 489
 		}
490 490
 
491
-		return (int)$data['remote_id'];
491
+		return (int) $data['remote_id'];
492 492
 	}
493 493
 
494 494
 	/**
@@ -519,7 +519,7 @@  discard block
 block discarded – undo
519 519
 			->orderBy('id');
520 520
 
521 521
 		$cursor = $qb->execute();
522
-		while($data = $cursor->fetch()) {
522
+		while ($data = $cursor->fetch()) {
523 523
 			$children[] = $this->createShareObject($data);
524 524
 		}
525 525
 		$cursor->closeCursor();
@@ -646,7 +646,7 @@  discard block
 block discarded – undo
646 646
 			);
647 647
 		}
648 648
 
649
-		$qb->innerJoin('s', 'filecache' ,'f', $qb->expr()->eq('s.file_source', 'f.fileid'));
649
+		$qb->innerJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid'));
650 650
 		$qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())));
651 651
 
652 652
 		$qb->orderBy('id');
@@ -709,7 +709,7 @@  discard block
 block discarded – undo
709 709
 
710 710
 		$cursor = $qb->execute();
711 711
 		$shares = [];
712
-		while($data = $cursor->fetch()) {
712
+		while ($data = $cursor->fetch()) {
713 713
 			$shares[] = $this->createShareObject($data);
714 714
 		}
715 715
 		$cursor->closeCursor();
@@ -733,7 +733,7 @@  discard block
 block discarded – undo
733 733
 		$cursor->closeCursor();
734 734
 
735 735
 		if ($data === false) {
736
-			throw new ShareNotFound('Can not find share with ID: ' . $id);
736
+			throw new ShareNotFound('Can not find share with ID: '.$id);
737 737
 		}
738 738
 
739 739
 		try {
@@ -762,7 +762,7 @@  discard block
 block discarded – undo
762 762
 			->execute();
763 763
 
764 764
 		$shares = [];
765
-		while($data = $cursor->fetch()) {
765
+		while ($data = $cursor->fetch()) {
766 766
 			$shares[] = $this->createShareObject($data);
767 767
 		}
768 768
 		$cursor->closeCursor();
@@ -801,7 +801,7 @@  discard block
 block discarded – undo
801 801
 
802 802
 		$cursor = $qb->execute();
803 803
 
804
-		while($data = $cursor->fetch()) {
804
+		while ($data = $cursor->fetch()) {
805 805
 			$shares[] = $this->createShareObject($data);
806 806
 		}
807 807
 		$cursor->closeCursor();
@@ -878,15 +878,15 @@  discard block
 block discarded – undo
878 878
 	private function createShareObject($data) {
879 879
 
880 880
 		$share = new Share($this->rootFolder, $this->userManager);
881
-		$share->setId((int)$data['id'])
882
-			->setShareType((int)$data['share_type'])
883
-			->setPermissions((int)$data['permissions'])
881
+		$share->setId((int) $data['id'])
882
+			->setShareType((int) $data['share_type'])
883
+			->setPermissions((int) $data['permissions'])
884 884
 			->setTarget($data['file_target'])
885
-			->setMailSend((bool)$data['mail_send'])
885
+			->setMailSend((bool) $data['mail_send'])
886 886
 			->setToken($data['token']);
887 887
 
888 888
 		$shareTime = new \DateTime();
889
-		$shareTime->setTimestamp((int)$data['stime']);
889
+		$shareTime->setTimestamp((int) $data['stime']);
890 890
 		$share->setShareTime($shareTime);
891 891
 		$share->setSharedWith($data['share_with']);
892 892
 
@@ -896,13 +896,13 @@  discard block
 block discarded – undo
896 896
 		} else {
897 897
 			//OLD SHARE
898 898
 			$share->setSharedBy($data['uid_owner']);
899
-			$path = $this->getNode($share->getSharedBy(), (int)$data['file_source']);
899
+			$path = $this->getNode($share->getSharedBy(), (int) $data['file_source']);
900 900
 
901 901
 			$owner = $path->getOwner();
902 902
 			$share->setShareOwner($owner->getUID());
903 903
 		}
904 904
 
905
-		$share->setNodeId((int)$data['file_source']);
905
+		$share->setNodeId((int) $data['file_source']);
906 906
 		$share->setNodeType($data['item_type']);
907 907
 
908 908
 		$share->setProviderId($this->identifier());
@@ -1114,7 +1114,7 @@  discard block
 block discarded – undo
1114 1114
 			);
1115 1115
 
1116 1116
 		$cursor = $qb->execute();
1117
-		while($data = $cursor->fetch()) {
1117
+		while ($data = $cursor->fetch()) {
1118 1118
 			try {
1119 1119
 				$share = $this->createShareObject($data);
1120 1120
 			} catch (InvalidShare $e) {
Please login to merge, or discard this patch.