Completed
Push — master ( 86d952...cbfcfb )
by Morris
24:23
created
lib/private/Share20/DefaultShareProvider.php 1 patch
Indentation   +1142 added lines, -1142 removed lines patch added patch discarded remove patch
@@ -52,1179 +52,1179 @@
 block discarded – undo
52 52
  */
53 53
 class DefaultShareProvider implements IShareProvider {
54 54
 
55
-	// Special share type for user modified group shares
56
-	const SHARE_TYPE_USERGROUP = 2;
57
-
58
-	/** @var IDBConnection */
59
-	private $dbConn;
60
-
61
-	/** @var IUserManager */
62
-	private $userManager;
63
-
64
-	/** @var IGroupManager */
65
-	private $groupManager;
66
-
67
-	/** @var IRootFolder */
68
-	private $rootFolder;
69
-
70
-	/**
71
-	 * DefaultShareProvider constructor.
72
-	 *
73
-	 * @param IDBConnection $connection
74
-	 * @param IUserManager $userManager
75
-	 * @param IGroupManager $groupManager
76
-	 * @param IRootFolder $rootFolder
77
-	 */
78
-	public function __construct(
79
-			IDBConnection $connection,
80
-			IUserManager $userManager,
81
-			IGroupManager $groupManager,
82
-			IRootFolder $rootFolder) {
83
-		$this->dbConn = $connection;
84
-		$this->userManager = $userManager;
85
-		$this->groupManager = $groupManager;
86
-		$this->rootFolder = $rootFolder;
87
-	}
88
-
89
-	/**
90
-	 * Return the identifier of this provider.
91
-	 *
92
-	 * @return string Containing only [a-zA-Z0-9]
93
-	 */
94
-	public function identifier() {
95
-		return 'ocinternal';
96
-	}
97
-
98
-	/**
99
-	 * Share a path
100
-	 *
101
-	 * @param \OCP\Share\IShare $share
102
-	 * @return \OCP\Share\IShare The share object
103
-	 * @throws ShareNotFound
104
-	 * @throws \Exception
105
-	 */
106
-	public function create(\OCP\Share\IShare $share) {
107
-		$qb = $this->dbConn->getQueryBuilder();
108
-
109
-		$qb->insert('share');
110
-		$qb->setValue('share_type', $qb->createNamedParameter($share->getShareType()));
111
-
112
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
113
-			//Set the UID of the user we share with
114
-			$qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith()));
115
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
116
-			//Set the GID of the group we share with
117
-			$qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith()));
118
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
119
-			//Set the token of the share
120
-			$qb->setValue('token', $qb->createNamedParameter($share->getToken()));
121
-
122
-			//If a password is set store it
123
-			if ($share->getPassword() !== null) {
124
-				$qb->setValue('password', $qb->createNamedParameter($share->getPassword()));
125
-			}
126
-
127
-			//If an expiration date is set store it
128
-			if ($share->getExpirationDate() !== null) {
129
-				$qb->setValue('expiration', $qb->createNamedParameter($share->getExpirationDate(), 'datetime'));
130
-			}
131
-
132
-			if (method_exists($share, 'getParent')) {
133
-				$qb->setValue('parent', $qb->createNamedParameter($share->getParent()));
134
-			}
135
-		} else {
136
-			throw new \Exception('invalid share type!');
137
-		}
138
-
139
-		// Set what is shares
140
-		$qb->setValue('item_type', $qb->createParameter('itemType'));
141
-		if ($share->getNode() instanceof \OCP\Files\File) {
142
-			$qb->setParameter('itemType', 'file');
143
-		} else {
144
-			$qb->setParameter('itemType', 'folder');
145
-		}
146
-
147
-		// Set the file id
148
-		$qb->setValue('item_source', $qb->createNamedParameter($share->getNode()->getId()));
149
-		$qb->setValue('file_source', $qb->createNamedParameter($share->getNode()->getId()));
150
-
151
-		// set the permissions
152
-		$qb->setValue('permissions', $qb->createNamedParameter($share->getPermissions()));
153
-
154
-		// Set who created this share
155
-		$qb->setValue('uid_initiator', $qb->createNamedParameter($share->getSharedBy()));
156
-
157
-		// Set who is the owner of this file/folder (and this the owner of the share)
158
-		$qb->setValue('uid_owner', $qb->createNamedParameter($share->getShareOwner()));
159
-
160
-		// Set the file target
161
-		$qb->setValue('file_target', $qb->createNamedParameter($share->getTarget()));
162
-
163
-		// Set the time this share was created
164
-		$qb->setValue('stime', $qb->createNamedParameter(time()));
165
-
166
-		// insert the data and fetch the id of the share
167
-		$this->dbConn->beginTransaction();
168
-		$qb->execute();
169
-		$id = $this->dbConn->lastInsertId('*PREFIX*share');
170
-
171
-		// Now fetch the inserted share and create a complete share object
172
-		$qb = $this->dbConn->getQueryBuilder();
173
-		$qb->select('*')
174
-			->from('share')
175
-			->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
176
-
177
-		$cursor = $qb->execute();
178
-		$data = $cursor->fetch();
179
-		$this->dbConn->commit();
180
-		$cursor->closeCursor();
181
-
182
-		if ($data === false) {
183
-			throw new ShareNotFound();
184
-		}
185
-
186
-		$mailSendValue = $share->getMailSend();
187
-		$data['mail_send'] = ($mailSendValue === null) ? true : $mailSendValue;
188
-
189
-		$share = $this->createShare($data);
190
-		return $share;
191
-	}
192
-
193
-	/**
194
-	 * Update a share
195
-	 *
196
-	 * @param \OCP\Share\IShare $share
197
-	 * @return \OCP\Share\IShare The share object
198
-	 */
199
-	public function update(\OCP\Share\IShare $share) {
200
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
201
-			/*
55
+    // Special share type for user modified group shares
56
+    const SHARE_TYPE_USERGROUP = 2;
57
+
58
+    /** @var IDBConnection */
59
+    private $dbConn;
60
+
61
+    /** @var IUserManager */
62
+    private $userManager;
63
+
64
+    /** @var IGroupManager */
65
+    private $groupManager;
66
+
67
+    /** @var IRootFolder */
68
+    private $rootFolder;
69
+
70
+    /**
71
+     * DefaultShareProvider constructor.
72
+     *
73
+     * @param IDBConnection $connection
74
+     * @param IUserManager $userManager
75
+     * @param IGroupManager $groupManager
76
+     * @param IRootFolder $rootFolder
77
+     */
78
+    public function __construct(
79
+            IDBConnection $connection,
80
+            IUserManager $userManager,
81
+            IGroupManager $groupManager,
82
+            IRootFolder $rootFolder) {
83
+        $this->dbConn = $connection;
84
+        $this->userManager = $userManager;
85
+        $this->groupManager = $groupManager;
86
+        $this->rootFolder = $rootFolder;
87
+    }
88
+
89
+    /**
90
+     * Return the identifier of this provider.
91
+     *
92
+     * @return string Containing only [a-zA-Z0-9]
93
+     */
94
+    public function identifier() {
95
+        return 'ocinternal';
96
+    }
97
+
98
+    /**
99
+     * Share a path
100
+     *
101
+     * @param \OCP\Share\IShare $share
102
+     * @return \OCP\Share\IShare The share object
103
+     * @throws ShareNotFound
104
+     * @throws \Exception
105
+     */
106
+    public function create(\OCP\Share\IShare $share) {
107
+        $qb = $this->dbConn->getQueryBuilder();
108
+
109
+        $qb->insert('share');
110
+        $qb->setValue('share_type', $qb->createNamedParameter($share->getShareType()));
111
+
112
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
113
+            //Set the UID of the user we share with
114
+            $qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith()));
115
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
116
+            //Set the GID of the group we share with
117
+            $qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith()));
118
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
119
+            //Set the token of the share
120
+            $qb->setValue('token', $qb->createNamedParameter($share->getToken()));
121
+
122
+            //If a password is set store it
123
+            if ($share->getPassword() !== null) {
124
+                $qb->setValue('password', $qb->createNamedParameter($share->getPassword()));
125
+            }
126
+
127
+            //If an expiration date is set store it
128
+            if ($share->getExpirationDate() !== null) {
129
+                $qb->setValue('expiration', $qb->createNamedParameter($share->getExpirationDate(), 'datetime'));
130
+            }
131
+
132
+            if (method_exists($share, 'getParent')) {
133
+                $qb->setValue('parent', $qb->createNamedParameter($share->getParent()));
134
+            }
135
+        } else {
136
+            throw new \Exception('invalid share type!');
137
+        }
138
+
139
+        // Set what is shares
140
+        $qb->setValue('item_type', $qb->createParameter('itemType'));
141
+        if ($share->getNode() instanceof \OCP\Files\File) {
142
+            $qb->setParameter('itemType', 'file');
143
+        } else {
144
+            $qb->setParameter('itemType', 'folder');
145
+        }
146
+
147
+        // Set the file id
148
+        $qb->setValue('item_source', $qb->createNamedParameter($share->getNode()->getId()));
149
+        $qb->setValue('file_source', $qb->createNamedParameter($share->getNode()->getId()));
150
+
151
+        // set the permissions
152
+        $qb->setValue('permissions', $qb->createNamedParameter($share->getPermissions()));
153
+
154
+        // Set who created this share
155
+        $qb->setValue('uid_initiator', $qb->createNamedParameter($share->getSharedBy()));
156
+
157
+        // Set who is the owner of this file/folder (and this the owner of the share)
158
+        $qb->setValue('uid_owner', $qb->createNamedParameter($share->getShareOwner()));
159
+
160
+        // Set the file target
161
+        $qb->setValue('file_target', $qb->createNamedParameter($share->getTarget()));
162
+
163
+        // Set the time this share was created
164
+        $qb->setValue('stime', $qb->createNamedParameter(time()));
165
+
166
+        // insert the data and fetch the id of the share
167
+        $this->dbConn->beginTransaction();
168
+        $qb->execute();
169
+        $id = $this->dbConn->lastInsertId('*PREFIX*share');
170
+
171
+        // Now fetch the inserted share and create a complete share object
172
+        $qb = $this->dbConn->getQueryBuilder();
173
+        $qb->select('*')
174
+            ->from('share')
175
+            ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
176
+
177
+        $cursor = $qb->execute();
178
+        $data = $cursor->fetch();
179
+        $this->dbConn->commit();
180
+        $cursor->closeCursor();
181
+
182
+        if ($data === false) {
183
+            throw new ShareNotFound();
184
+        }
185
+
186
+        $mailSendValue = $share->getMailSend();
187
+        $data['mail_send'] = ($mailSendValue === null) ? true : $mailSendValue;
188
+
189
+        $share = $this->createShare($data);
190
+        return $share;
191
+    }
192
+
193
+    /**
194
+     * Update a share
195
+     *
196
+     * @param \OCP\Share\IShare $share
197
+     * @return \OCP\Share\IShare The share object
198
+     */
199
+    public function update(\OCP\Share\IShare $share) {
200
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
201
+            /*
202 202
 			 * We allow updating the recipient on user shares.
203 203
 			 */
204
-			$qb = $this->dbConn->getQueryBuilder();
205
-			$qb->update('share')
206
-				->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
207
-				->set('share_with', $qb->createNamedParameter($share->getSharedWith()))
208
-				->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
209
-				->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
210
-				->set('permissions', $qb->createNamedParameter($share->getPermissions()))
211
-				->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
212
-				->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
213
-				->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
214
-				->execute();
215
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
216
-			$qb = $this->dbConn->getQueryBuilder();
217
-			$qb->update('share')
218
-				->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
219
-				->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
220
-				->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
221
-				->set('permissions', $qb->createNamedParameter($share->getPermissions()))
222
-				->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
223
-				->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
224
-				->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
225
-				->execute();
226
-
227
-			/*
204
+            $qb = $this->dbConn->getQueryBuilder();
205
+            $qb->update('share')
206
+                ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
207
+                ->set('share_with', $qb->createNamedParameter($share->getSharedWith()))
208
+                ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
209
+                ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
210
+                ->set('permissions', $qb->createNamedParameter($share->getPermissions()))
211
+                ->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
212
+                ->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
213
+                ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
214
+                ->execute();
215
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
216
+            $qb = $this->dbConn->getQueryBuilder();
217
+            $qb->update('share')
218
+                ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
219
+                ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
220
+                ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
221
+                ->set('permissions', $qb->createNamedParameter($share->getPermissions()))
222
+                ->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
223
+                ->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
224
+                ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
225
+                ->execute();
226
+
227
+            /*
228 228
 			 * Update all user defined group shares
229 229
 			 */
230
-			$qb = $this->dbConn->getQueryBuilder();
231
-			$qb->update('share')
232
-				->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
233
-				->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
234
-				->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
235
-				->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
236
-				->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
237
-				->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
238
-				->execute();
239
-
240
-			/*
230
+            $qb = $this->dbConn->getQueryBuilder();
231
+            $qb->update('share')
232
+                ->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
233
+                ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
234
+                ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
235
+                ->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
236
+                ->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
237
+                ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
238
+                ->execute();
239
+
240
+            /*
241 241
 			 * Now update the permissions for all children that have not set it to 0
242 242
 			 */
243
-			$qb = $this->dbConn->getQueryBuilder();
244
-			$qb->update('share')
245
-				->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
246
-				->andWhere($qb->expr()->neq('permissions', $qb->createNamedParameter(0)))
247
-				->set('permissions', $qb->createNamedParameter($share->getPermissions()))
248
-				->execute();
249
-
250
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
251
-			$qb = $this->dbConn->getQueryBuilder();
252
-			$qb->update('share')
253
-				->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
254
-				->set('password', $qb->createNamedParameter($share->getPassword()))
255
-				->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
256
-				->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
257
-				->set('permissions', $qb->createNamedParameter($share->getPermissions()))
258
-				->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
259
-				->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
260
-				->set('token', $qb->createNamedParameter($share->getToken()))
261
-				->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
262
-				->execute();
263
-		}
264
-
265
-		return $share;
266
-	}
267
-
268
-	/**
269
-	 * Get all children of this share
270
-	 * FIXME: remove once https://github.com/owncloud/core/pull/21660 is in
271
-	 *
272
-	 * @param \OCP\Share\IShare $parent
273
-	 * @return \OCP\Share\IShare[]
274
-	 */
275
-	public function getChildren(\OCP\Share\IShare $parent) {
276
-		$children = [];
277
-
278
-		$qb = $this->dbConn->getQueryBuilder();
279
-		$qb->select('*')
280
-			->from('share')
281
-			->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId())))
282
-			->andWhere(
283
-				$qb->expr()->in(
284
-					'share_type',
285
-					$qb->createNamedParameter([
286
-						\OCP\Share::SHARE_TYPE_USER,
287
-						\OCP\Share::SHARE_TYPE_GROUP,
288
-						\OCP\Share::SHARE_TYPE_LINK,
289
-					], IQueryBuilder::PARAM_INT_ARRAY)
290
-				)
291
-			)
292
-			->andWhere($qb->expr()->orX(
293
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
294
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
295
-			))
296
-			->orderBy('id');
297
-
298
-		$cursor = $qb->execute();
299
-		while($data = $cursor->fetch()) {
300
-			$children[] = $this->createShare($data);
301
-		}
302
-		$cursor->closeCursor();
303
-
304
-		return $children;
305
-	}
306
-
307
-	/**
308
-	 * Delete a share
309
-	 *
310
-	 * @param \OCP\Share\IShare $share
311
-	 */
312
-	public function delete(\OCP\Share\IShare $share) {
313
-		$qb = $this->dbConn->getQueryBuilder();
314
-		$qb->delete('share')
315
-			->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())));
316
-
317
-		/*
243
+            $qb = $this->dbConn->getQueryBuilder();
244
+            $qb->update('share')
245
+                ->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
246
+                ->andWhere($qb->expr()->neq('permissions', $qb->createNamedParameter(0)))
247
+                ->set('permissions', $qb->createNamedParameter($share->getPermissions()))
248
+                ->execute();
249
+
250
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
251
+            $qb = $this->dbConn->getQueryBuilder();
252
+            $qb->update('share')
253
+                ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
254
+                ->set('password', $qb->createNamedParameter($share->getPassword()))
255
+                ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
256
+                ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
257
+                ->set('permissions', $qb->createNamedParameter($share->getPermissions()))
258
+                ->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
259
+                ->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
260
+                ->set('token', $qb->createNamedParameter($share->getToken()))
261
+                ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
262
+                ->execute();
263
+        }
264
+
265
+        return $share;
266
+    }
267
+
268
+    /**
269
+     * Get all children of this share
270
+     * FIXME: remove once https://github.com/owncloud/core/pull/21660 is in
271
+     *
272
+     * @param \OCP\Share\IShare $parent
273
+     * @return \OCP\Share\IShare[]
274
+     */
275
+    public function getChildren(\OCP\Share\IShare $parent) {
276
+        $children = [];
277
+
278
+        $qb = $this->dbConn->getQueryBuilder();
279
+        $qb->select('*')
280
+            ->from('share')
281
+            ->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId())))
282
+            ->andWhere(
283
+                $qb->expr()->in(
284
+                    'share_type',
285
+                    $qb->createNamedParameter([
286
+                        \OCP\Share::SHARE_TYPE_USER,
287
+                        \OCP\Share::SHARE_TYPE_GROUP,
288
+                        \OCP\Share::SHARE_TYPE_LINK,
289
+                    ], IQueryBuilder::PARAM_INT_ARRAY)
290
+                )
291
+            )
292
+            ->andWhere($qb->expr()->orX(
293
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
294
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
295
+            ))
296
+            ->orderBy('id');
297
+
298
+        $cursor = $qb->execute();
299
+        while($data = $cursor->fetch()) {
300
+            $children[] = $this->createShare($data);
301
+        }
302
+        $cursor->closeCursor();
303
+
304
+        return $children;
305
+    }
306
+
307
+    /**
308
+     * Delete a share
309
+     *
310
+     * @param \OCP\Share\IShare $share
311
+     */
312
+    public function delete(\OCP\Share\IShare $share) {
313
+        $qb = $this->dbConn->getQueryBuilder();
314
+        $qb->delete('share')
315
+            ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())));
316
+
317
+        /*
318 318
 		 * If the share is a group share delete all possible
319 319
 		 * user defined groups shares.
320 320
 		 */
321
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
322
-			$qb->orWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())));
323
-		}
324
-
325
-		$qb->execute();
326
-	}
327
-
328
-	/**
329
-	 * Unshare a share from the recipient. If this is a group share
330
-	 * this means we need a special entry in the share db.
331
-	 *
332
-	 * @param \OCP\Share\IShare $share
333
-	 * @param string $recipient UserId of recipient
334
-	 * @throws BackendError
335
-	 * @throws ProviderException
336
-	 */
337
-	public function deleteFromSelf(\OCP\Share\IShare $share, $recipient) {
338
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
339
-
340
-			$group = $this->groupManager->get($share->getSharedWith());
341
-			$user = $this->userManager->get($recipient);
342
-
343
-			if (is_null($group)) {
344
-				throw new ProviderException('Group "' . $share->getSharedWith() . '" does not exist');
345
-			}
346
-
347
-			if (!$group->inGroup($user)) {
348
-				throw new ProviderException('Recipient not in receiving group');
349
-			}
350
-
351
-			// Try to fetch user specific share
352
-			$qb = $this->dbConn->getQueryBuilder();
353
-			$stmt = $qb->select('*')
354
-				->from('share')
355
-				->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
356
-				->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient)))
357
-				->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
358
-				->andWhere($qb->expr()->orX(
359
-					$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
360
-					$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
361
-				))
362
-				->execute();
363
-
364
-			$data = $stmt->fetch();
365
-
366
-			/*
321
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
322
+            $qb->orWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())));
323
+        }
324
+
325
+        $qb->execute();
326
+    }
327
+
328
+    /**
329
+     * Unshare a share from the recipient. If this is a group share
330
+     * this means we need a special entry in the share db.
331
+     *
332
+     * @param \OCP\Share\IShare $share
333
+     * @param string $recipient UserId of recipient
334
+     * @throws BackendError
335
+     * @throws ProviderException
336
+     */
337
+    public function deleteFromSelf(\OCP\Share\IShare $share, $recipient) {
338
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
339
+
340
+            $group = $this->groupManager->get($share->getSharedWith());
341
+            $user = $this->userManager->get($recipient);
342
+
343
+            if (is_null($group)) {
344
+                throw new ProviderException('Group "' . $share->getSharedWith() . '" does not exist');
345
+            }
346
+
347
+            if (!$group->inGroup($user)) {
348
+                throw new ProviderException('Recipient not in receiving group');
349
+            }
350
+
351
+            // Try to fetch user specific share
352
+            $qb = $this->dbConn->getQueryBuilder();
353
+            $stmt = $qb->select('*')
354
+                ->from('share')
355
+                ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
356
+                ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient)))
357
+                ->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
358
+                ->andWhere($qb->expr()->orX(
359
+                    $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
360
+                    $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
361
+                ))
362
+                ->execute();
363
+
364
+            $data = $stmt->fetch();
365
+
366
+            /*
367 367
 			 * Check if there already is a user specific group share.
368 368
 			 * If there is update it (if required).
369 369
 			 */
370
-			if ($data === false) {
371
-				$qb = $this->dbConn->getQueryBuilder();
372
-
373
-				$type = $share->getNodeType();
374
-
375
-				//Insert new share
376
-				$qb->insert('share')
377
-					->values([
378
-						'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP),
379
-						'share_with' => $qb->createNamedParameter($recipient),
380
-						'uid_owner' => $qb->createNamedParameter($share->getShareOwner()),
381
-						'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()),
382
-						'parent' => $qb->createNamedParameter($share->getId()),
383
-						'item_type' => $qb->createNamedParameter($type),
384
-						'item_source' => $qb->createNamedParameter($share->getNodeId()),
385
-						'file_source' => $qb->createNamedParameter($share->getNodeId()),
386
-						'file_target' => $qb->createNamedParameter($share->getTarget()),
387
-						'permissions' => $qb->createNamedParameter(0),
388
-						'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()),
389
-					])->execute();
390
-
391
-			} else if ($data['permissions'] !== 0) {
392
-
393
-				// Update existing usergroup share
394
-				$qb = $this->dbConn->getQueryBuilder();
395
-				$qb->update('share')
396
-					->set('permissions', $qb->createNamedParameter(0))
397
-					->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id'])))
398
-					->execute();
399
-			}
400
-
401
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
402
-
403
-			if ($share->getSharedWith() !== $recipient) {
404
-				throw new ProviderException('Recipient does not match');
405
-			}
406
-
407
-			// We can just delete user and link shares
408
-			$this->delete($share);
409
-		} else {
410
-			throw new ProviderException('Invalid shareType');
411
-		}
412
-	}
413
-
414
-	/**
415
-	 * @inheritdoc
416
-	 *
417
-	 * For now this only works for group shares
418
-	 * If this gets implemented for normal shares we have to extend it
419
-	 */
420
-	public function restore(IShare $share, string $recipient): IShare {
421
-		$qb = $this->dbConn->getQueryBuilder();
422
-		$qb->select('permissions')
423
-			->from('share')
424
-			->where(
425
-				$qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))
426
-			);
427
-		$cursor = $qb->execute();
428
-		$data = $cursor->fetch();
429
-		$cursor->closeCursor();
430
-
431
-		$originalPermission = $data['permissions'];
432
-
433
-		$qb = $this->dbConn->getQueryBuilder();
434
-		$qb->update('share')
435
-			->set('permissions', $qb->createNamedParameter($originalPermission))
436
-			->where(
437
-				$qb->expr()->eq('parent', $qb->createNamedParameter($share->getParent()))
438
-			)->andWhere(
439
-				$qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))
440
-			)->andWhere(
441
-				$qb->expr()->eq('share_with', $qb->createNamedParameter($recipient))
442
-			);
443
-
444
-		$qb->execute();
445
-
446
-		return $this->getShareById($share->getId(), $recipient);
447
-	}
448
-
449
-	/**
450
-	 * @inheritdoc
451
-	 */
452
-	public function move(\OCP\Share\IShare $share, $recipient) {
453
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
454
-			// Just update the target
455
-			$qb = $this->dbConn->getQueryBuilder();
456
-			$qb->update('share')
457
-				->set('file_target', $qb->createNamedParameter($share->getTarget()))
458
-				->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
459
-				->execute();
460
-
461
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
462
-
463
-			// Check if there is a usergroup share
464
-			$qb = $this->dbConn->getQueryBuilder();
465
-			$stmt = $qb->select('id')
466
-				->from('share')
467
-				->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
468
-				->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient)))
469
-				->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
470
-				->andWhere($qb->expr()->orX(
471
-					$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
472
-					$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
473
-				))
474
-				->setMaxResults(1)
475
-				->execute();
476
-
477
-			$data = $stmt->fetch();
478
-			$stmt->closeCursor();
479
-
480
-			if ($data === false) {
481
-				// No usergroup share yet. Create one.
482
-				$qb = $this->dbConn->getQueryBuilder();
483
-				$qb->insert('share')
484
-					->values([
485
-						'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP),
486
-						'share_with' => $qb->createNamedParameter($recipient),
487
-						'uid_owner' => $qb->createNamedParameter($share->getShareOwner()),
488
-						'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()),
489
-						'parent' => $qb->createNamedParameter($share->getId()),
490
-						'item_type' => $qb->createNamedParameter($share->getNodeType()),
491
-						'item_source' => $qb->createNamedParameter($share->getNodeId()),
492
-						'file_source' => $qb->createNamedParameter($share->getNodeId()),
493
-						'file_target' => $qb->createNamedParameter($share->getTarget()),
494
-						'permissions' => $qb->createNamedParameter($share->getPermissions()),
495
-						'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()),
496
-					])->execute();
497
-			} else {
498
-				// Already a usergroup share. Update it.
499
-				$qb = $this->dbConn->getQueryBuilder();
500
-				$qb->update('share')
501
-					->set('file_target', $qb->createNamedParameter($share->getTarget()))
502
-					->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id'])))
503
-					->execute();
504
-			}
505
-		}
506
-
507
-		return $share;
508
-	}
509
-
510
-	public function getSharesInFolder($userId, Folder $node, $reshares) {
511
-		$qb = $this->dbConn->getQueryBuilder();
512
-		$qb->select('*')
513
-			->from('share', 's')
514
-			->andWhere($qb->expr()->orX(
515
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
516
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
517
-			));
518
-
519
-		$qb->andWhere($qb->expr()->orX(
520
-			$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)),
521
-			$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)),
522
-			$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK))
523
-		));
524
-
525
-		/**
526
-		 * Reshares for this user are shares where they are the owner.
527
-		 */
528
-		if ($reshares === false) {
529
-			$qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
530
-		} else {
531
-			$qb->andWhere(
532
-				$qb->expr()->orX(
533
-					$qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
534
-					$qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
535
-				)
536
-			);
537
-		}
538
-
539
-		$qb->innerJoin('s', 'filecache' ,'f', $qb->expr()->eq('s.file_source', 'f.fileid'));
540
-		$qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())));
541
-
542
-		$qb->orderBy('id');
543
-
544
-		$cursor = $qb->execute();
545
-		$shares = [];
546
-		while ($data = $cursor->fetch()) {
547
-			$shares[$data['fileid']][] = $this->createShare($data);
548
-		}
549
-		$cursor->closeCursor();
550
-
551
-		return $shares;
552
-	}
553
-
554
-	/**
555
-	 * @inheritdoc
556
-	 */
557
-	public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) {
558
-		$qb = $this->dbConn->getQueryBuilder();
559
-		$qb->select('*')
560
-			->from('share')
561
-			->andWhere($qb->expr()->orX(
562
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
563
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
564
-			));
565
-
566
-		$qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter($shareType)));
567
-
568
-		/**
569
-		 * Reshares for this user are shares where they are the owner.
570
-		 */
571
-		if ($reshares === false) {
572
-			$qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
573
-		} else {
574
-			$qb->andWhere(
575
-				$qb->expr()->orX(
576
-					$qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
577
-					$qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
578
-				)
579
-			);
580
-		}
581
-
582
-		if ($node !== null) {
583
-			$qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
584
-		}
585
-
586
-		if ($limit !== -1) {
587
-			$qb->setMaxResults($limit);
588
-		}
589
-
590
-		$qb->setFirstResult($offset);
591
-		$qb->orderBy('id');
592
-
593
-		$cursor = $qb->execute();
594
-		$shares = [];
595
-		while($data = $cursor->fetch()) {
596
-			$shares[] = $this->createShare($data);
597
-		}
598
-		$cursor->closeCursor();
599
-
600
-		return $shares;
601
-	}
602
-
603
-	/**
604
-	 * @inheritdoc
605
-	 */
606
-	public function getShareById($id, $recipientId = null) {
607
-		$qb = $this->dbConn->getQueryBuilder();
608
-
609
-		$qb->select('*')
610
-			->from('share')
611
-			->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
612
-			->andWhere(
613
-				$qb->expr()->in(
614
-					'share_type',
615
-					$qb->createNamedParameter([
616
-						\OCP\Share::SHARE_TYPE_USER,
617
-						\OCP\Share::SHARE_TYPE_GROUP,
618
-						\OCP\Share::SHARE_TYPE_LINK,
619
-					], IQueryBuilder::PARAM_INT_ARRAY)
620
-				)
621
-			)
622
-			->andWhere($qb->expr()->orX(
623
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
624
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
625
-			));
626
-
627
-		$cursor = $qb->execute();
628
-		$data = $cursor->fetch();
629
-		$cursor->closeCursor();
630
-
631
-		if ($data === false) {
632
-			throw new ShareNotFound();
633
-		}
634
-
635
-		try {
636
-			$share = $this->createShare($data);
637
-		} catch (InvalidShare $e) {
638
-			throw new ShareNotFound();
639
-		}
640
-
641
-		// If the recipient is set for a group share resolve to that user
642
-		if ($recipientId !== null && $share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
643
-			$share = $this->resolveGroupShares([$share], $recipientId)[0];
644
-		}
645
-
646
-		return $share;
647
-	}
648
-
649
-	/**
650
-	 * Get shares for a given path
651
-	 *
652
-	 * @param \OCP\Files\Node $path
653
-	 * @return \OCP\Share\IShare[]
654
-	 */
655
-	public function getSharesByPath(Node $path) {
656
-		$qb = $this->dbConn->getQueryBuilder();
657
-
658
-		$cursor = $qb->select('*')
659
-			->from('share')
660
-			->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId())))
661
-			->andWhere(
662
-				$qb->expr()->orX(
663
-					$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)),
664
-					$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))
665
-				)
666
-			)
667
-			->andWhere($qb->expr()->orX(
668
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
669
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
670
-			))
671
-			->execute();
672
-
673
-		$shares = [];
674
-		while($data = $cursor->fetch()) {
675
-			$shares[] = $this->createShare($data);
676
-		}
677
-		$cursor->closeCursor();
678
-
679
-		return $shares;
680
-	}
681
-
682
-	/**
683
-	 * Returns whether the given database result can be interpreted as
684
-	 * a share with accessible file (not trashed, not deleted)
685
-	 */
686
-	private function isAccessibleResult($data) {
687
-		// exclude shares leading to deleted file entries
688
-		if ($data['fileid'] === null) {
689
-			return false;
690
-		}
691
-
692
-		// exclude shares leading to trashbin on home storages
693
-		$pathSections = explode('/', $data['path'], 2);
694
-		// FIXME: would not detect rare md5'd home storage case properly
695
-		if ($pathSections[0] !== 'files'
696
-		    	&& in_array(explode(':', $data['storage_string_id'], 2)[0], array('home', 'object'))) {
697
-			return false;
698
-		}
699
-		return true;
700
-	}
701
-
702
-	/**
703
-	 * @inheritdoc
704
-	 */
705
-	public function getSharedWith($userId, $shareType, $node, $limit, $offset) {
706
-		/** @var Share[] $shares */
707
-		$shares = [];
708
-
709
-		if ($shareType === \OCP\Share::SHARE_TYPE_USER) {
710
-			//Get shares directly with this user
711
-			$qb = $this->dbConn->getQueryBuilder();
712
-			$qb->select('s.*',
713
-				'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash',
714
-				'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime',
715
-				'f.encrypted', 'f.unencrypted_size', 'f.etag', 'f.checksum'
716
-			)
717
-				->selectAlias('st.id', 'storage_string_id')
718
-				->from('share', 's')
719
-				->leftJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid'))
720
-				->leftJoin('f', 'storages', 'st', $qb->expr()->eq('f.storage', 'st.numeric_id'));
721
-
722
-			// Order by id
723
-			$qb->orderBy('s.id');
724
-
725
-			// Set limit and offset
726
-			if ($limit !== -1) {
727
-				$qb->setMaxResults($limit);
728
-			}
729
-			$qb->setFirstResult($offset);
730
-
731
-			$qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)))
732
-				->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)))
733
-				->andWhere($qb->expr()->orX(
734
-					$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
735
-					$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
736
-				));
737
-
738
-			// Filter by node if provided
739
-			if ($node !== null) {
740
-				$qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
741
-			}
742
-
743
-			$cursor = $qb->execute();
744
-
745
-			while($data = $cursor->fetch()) {
746
-				if ($this->isAccessibleResult($data)) {
747
-					$shares[] = $this->createShare($data);
748
-				}
749
-			}
750
-			$cursor->closeCursor();
751
-
752
-		} else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
753
-			$user = $this->userManager->get($userId);
754
-			$allGroups = $this->groupManager->getUserGroups($user);
755
-
756
-			/** @var Share[] $shares2 */
757
-			$shares2 = [];
758
-
759
-			$start = 0;
760
-			while(true) {
761
-				$groups = array_slice($allGroups, $start, 100);
762
-				$start += 100;
763
-
764
-				if ($groups === []) {
765
-					break;
766
-				}
767
-
768
-				$qb = $this->dbConn->getQueryBuilder();
769
-				$qb->select('s.*',
770
-					'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash',
771
-					'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime',
772
-					'f.encrypted', 'f.unencrypted_size', 'f.etag', 'f.checksum'
773
-				)
774
-					->selectAlias('st.id', 'storage_string_id')
775
-					->from('share', 's')
776
-					->leftJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid'))
777
-					->leftJoin('f', 'storages', 'st', $qb->expr()->eq('f.storage', 'st.numeric_id'))
778
-					->orderBy('s.id')
779
-					->setFirstResult(0);
780
-
781
-				if ($limit !== -1) {
782
-					$qb->setMaxResults($limit - count($shares));
783
-				}
784
-
785
-				// Filter by node if provided
786
-				if ($node !== null) {
787
-					$qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
788
-				}
789
-
790
-
791
-				$groups = array_filter($groups, function($group) { return $group instanceof IGroup; });
792
-				$groups = array_map(function(IGroup $group) { return $group->getGID(); }, $groups);
793
-
794
-				$qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)))
795
-					->andWhere($qb->expr()->in('share_with', $qb->createNamedParameter(
796
-						$groups,
797
-						IQueryBuilder::PARAM_STR_ARRAY
798
-					)))
799
-					->andWhere($qb->expr()->orX(
800
-						$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
801
-						$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
802
-					));
803
-
804
-				$cursor = $qb->execute();
805
-				while($data = $cursor->fetch()) {
806
-					if ($offset > 0) {
807
-						$offset--;
808
-						continue;
809
-					}
810
-
811
-					if ($this->isAccessibleResult($data)) {
812
-						$shares2[] = $this->createShare($data);
813
-					}
814
-				}
815
-				$cursor->closeCursor();
816
-			}
817
-
818
-			/*
370
+            if ($data === false) {
371
+                $qb = $this->dbConn->getQueryBuilder();
372
+
373
+                $type = $share->getNodeType();
374
+
375
+                //Insert new share
376
+                $qb->insert('share')
377
+                    ->values([
378
+                        'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP),
379
+                        'share_with' => $qb->createNamedParameter($recipient),
380
+                        'uid_owner' => $qb->createNamedParameter($share->getShareOwner()),
381
+                        'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()),
382
+                        'parent' => $qb->createNamedParameter($share->getId()),
383
+                        'item_type' => $qb->createNamedParameter($type),
384
+                        'item_source' => $qb->createNamedParameter($share->getNodeId()),
385
+                        'file_source' => $qb->createNamedParameter($share->getNodeId()),
386
+                        'file_target' => $qb->createNamedParameter($share->getTarget()),
387
+                        'permissions' => $qb->createNamedParameter(0),
388
+                        'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()),
389
+                    ])->execute();
390
+
391
+            } else if ($data['permissions'] !== 0) {
392
+
393
+                // Update existing usergroup share
394
+                $qb = $this->dbConn->getQueryBuilder();
395
+                $qb->update('share')
396
+                    ->set('permissions', $qb->createNamedParameter(0))
397
+                    ->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id'])))
398
+                    ->execute();
399
+            }
400
+
401
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
402
+
403
+            if ($share->getSharedWith() !== $recipient) {
404
+                throw new ProviderException('Recipient does not match');
405
+            }
406
+
407
+            // We can just delete user and link shares
408
+            $this->delete($share);
409
+        } else {
410
+            throw new ProviderException('Invalid shareType');
411
+        }
412
+    }
413
+
414
+    /**
415
+     * @inheritdoc
416
+     *
417
+     * For now this only works for group shares
418
+     * If this gets implemented for normal shares we have to extend it
419
+     */
420
+    public function restore(IShare $share, string $recipient): IShare {
421
+        $qb = $this->dbConn->getQueryBuilder();
422
+        $qb->select('permissions')
423
+            ->from('share')
424
+            ->where(
425
+                $qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))
426
+            );
427
+        $cursor = $qb->execute();
428
+        $data = $cursor->fetch();
429
+        $cursor->closeCursor();
430
+
431
+        $originalPermission = $data['permissions'];
432
+
433
+        $qb = $this->dbConn->getQueryBuilder();
434
+        $qb->update('share')
435
+            ->set('permissions', $qb->createNamedParameter($originalPermission))
436
+            ->where(
437
+                $qb->expr()->eq('parent', $qb->createNamedParameter($share->getParent()))
438
+            )->andWhere(
439
+                $qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))
440
+            )->andWhere(
441
+                $qb->expr()->eq('share_with', $qb->createNamedParameter($recipient))
442
+            );
443
+
444
+        $qb->execute();
445
+
446
+        return $this->getShareById($share->getId(), $recipient);
447
+    }
448
+
449
+    /**
450
+     * @inheritdoc
451
+     */
452
+    public function move(\OCP\Share\IShare $share, $recipient) {
453
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
454
+            // Just update the target
455
+            $qb = $this->dbConn->getQueryBuilder();
456
+            $qb->update('share')
457
+                ->set('file_target', $qb->createNamedParameter($share->getTarget()))
458
+                ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
459
+                ->execute();
460
+
461
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
462
+
463
+            // Check if there is a usergroup share
464
+            $qb = $this->dbConn->getQueryBuilder();
465
+            $stmt = $qb->select('id')
466
+                ->from('share')
467
+                ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
468
+                ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient)))
469
+                ->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
470
+                ->andWhere($qb->expr()->orX(
471
+                    $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
472
+                    $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
473
+                ))
474
+                ->setMaxResults(1)
475
+                ->execute();
476
+
477
+            $data = $stmt->fetch();
478
+            $stmt->closeCursor();
479
+
480
+            if ($data === false) {
481
+                // No usergroup share yet. Create one.
482
+                $qb = $this->dbConn->getQueryBuilder();
483
+                $qb->insert('share')
484
+                    ->values([
485
+                        'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP),
486
+                        'share_with' => $qb->createNamedParameter($recipient),
487
+                        'uid_owner' => $qb->createNamedParameter($share->getShareOwner()),
488
+                        'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()),
489
+                        'parent' => $qb->createNamedParameter($share->getId()),
490
+                        'item_type' => $qb->createNamedParameter($share->getNodeType()),
491
+                        'item_source' => $qb->createNamedParameter($share->getNodeId()),
492
+                        'file_source' => $qb->createNamedParameter($share->getNodeId()),
493
+                        'file_target' => $qb->createNamedParameter($share->getTarget()),
494
+                        'permissions' => $qb->createNamedParameter($share->getPermissions()),
495
+                        'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()),
496
+                    ])->execute();
497
+            } else {
498
+                // Already a usergroup share. Update it.
499
+                $qb = $this->dbConn->getQueryBuilder();
500
+                $qb->update('share')
501
+                    ->set('file_target', $qb->createNamedParameter($share->getTarget()))
502
+                    ->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id'])))
503
+                    ->execute();
504
+            }
505
+        }
506
+
507
+        return $share;
508
+    }
509
+
510
+    public function getSharesInFolder($userId, Folder $node, $reshares) {
511
+        $qb = $this->dbConn->getQueryBuilder();
512
+        $qb->select('*')
513
+            ->from('share', 's')
514
+            ->andWhere($qb->expr()->orX(
515
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
516
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
517
+            ));
518
+
519
+        $qb->andWhere($qb->expr()->orX(
520
+            $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)),
521
+            $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)),
522
+            $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK))
523
+        ));
524
+
525
+        /**
526
+         * Reshares for this user are shares where they are the owner.
527
+         */
528
+        if ($reshares === false) {
529
+            $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
530
+        } else {
531
+            $qb->andWhere(
532
+                $qb->expr()->orX(
533
+                    $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
534
+                    $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
535
+                )
536
+            );
537
+        }
538
+
539
+        $qb->innerJoin('s', 'filecache' ,'f', $qb->expr()->eq('s.file_source', 'f.fileid'));
540
+        $qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())));
541
+
542
+        $qb->orderBy('id');
543
+
544
+        $cursor = $qb->execute();
545
+        $shares = [];
546
+        while ($data = $cursor->fetch()) {
547
+            $shares[$data['fileid']][] = $this->createShare($data);
548
+        }
549
+        $cursor->closeCursor();
550
+
551
+        return $shares;
552
+    }
553
+
554
+    /**
555
+     * @inheritdoc
556
+     */
557
+    public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) {
558
+        $qb = $this->dbConn->getQueryBuilder();
559
+        $qb->select('*')
560
+            ->from('share')
561
+            ->andWhere($qb->expr()->orX(
562
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
563
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
564
+            ));
565
+
566
+        $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter($shareType)));
567
+
568
+        /**
569
+         * Reshares for this user are shares where they are the owner.
570
+         */
571
+        if ($reshares === false) {
572
+            $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
573
+        } else {
574
+            $qb->andWhere(
575
+                $qb->expr()->orX(
576
+                    $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
577
+                    $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
578
+                )
579
+            );
580
+        }
581
+
582
+        if ($node !== null) {
583
+            $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
584
+        }
585
+
586
+        if ($limit !== -1) {
587
+            $qb->setMaxResults($limit);
588
+        }
589
+
590
+        $qb->setFirstResult($offset);
591
+        $qb->orderBy('id');
592
+
593
+        $cursor = $qb->execute();
594
+        $shares = [];
595
+        while($data = $cursor->fetch()) {
596
+            $shares[] = $this->createShare($data);
597
+        }
598
+        $cursor->closeCursor();
599
+
600
+        return $shares;
601
+    }
602
+
603
+    /**
604
+     * @inheritdoc
605
+     */
606
+    public function getShareById($id, $recipientId = null) {
607
+        $qb = $this->dbConn->getQueryBuilder();
608
+
609
+        $qb->select('*')
610
+            ->from('share')
611
+            ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
612
+            ->andWhere(
613
+                $qb->expr()->in(
614
+                    'share_type',
615
+                    $qb->createNamedParameter([
616
+                        \OCP\Share::SHARE_TYPE_USER,
617
+                        \OCP\Share::SHARE_TYPE_GROUP,
618
+                        \OCP\Share::SHARE_TYPE_LINK,
619
+                    ], IQueryBuilder::PARAM_INT_ARRAY)
620
+                )
621
+            )
622
+            ->andWhere($qb->expr()->orX(
623
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
624
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
625
+            ));
626
+
627
+        $cursor = $qb->execute();
628
+        $data = $cursor->fetch();
629
+        $cursor->closeCursor();
630
+
631
+        if ($data === false) {
632
+            throw new ShareNotFound();
633
+        }
634
+
635
+        try {
636
+            $share = $this->createShare($data);
637
+        } catch (InvalidShare $e) {
638
+            throw new ShareNotFound();
639
+        }
640
+
641
+        // If the recipient is set for a group share resolve to that user
642
+        if ($recipientId !== null && $share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
643
+            $share = $this->resolveGroupShares([$share], $recipientId)[0];
644
+        }
645
+
646
+        return $share;
647
+    }
648
+
649
+    /**
650
+     * Get shares for a given path
651
+     *
652
+     * @param \OCP\Files\Node $path
653
+     * @return \OCP\Share\IShare[]
654
+     */
655
+    public function getSharesByPath(Node $path) {
656
+        $qb = $this->dbConn->getQueryBuilder();
657
+
658
+        $cursor = $qb->select('*')
659
+            ->from('share')
660
+            ->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId())))
661
+            ->andWhere(
662
+                $qb->expr()->orX(
663
+                    $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)),
664
+                    $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))
665
+                )
666
+            )
667
+            ->andWhere($qb->expr()->orX(
668
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
669
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
670
+            ))
671
+            ->execute();
672
+
673
+        $shares = [];
674
+        while($data = $cursor->fetch()) {
675
+            $shares[] = $this->createShare($data);
676
+        }
677
+        $cursor->closeCursor();
678
+
679
+        return $shares;
680
+    }
681
+
682
+    /**
683
+     * Returns whether the given database result can be interpreted as
684
+     * a share with accessible file (not trashed, not deleted)
685
+     */
686
+    private function isAccessibleResult($data) {
687
+        // exclude shares leading to deleted file entries
688
+        if ($data['fileid'] === null) {
689
+            return false;
690
+        }
691
+
692
+        // exclude shares leading to trashbin on home storages
693
+        $pathSections = explode('/', $data['path'], 2);
694
+        // FIXME: would not detect rare md5'd home storage case properly
695
+        if ($pathSections[0] !== 'files'
696
+                && in_array(explode(':', $data['storage_string_id'], 2)[0], array('home', 'object'))) {
697
+            return false;
698
+        }
699
+        return true;
700
+    }
701
+
702
+    /**
703
+     * @inheritdoc
704
+     */
705
+    public function getSharedWith($userId, $shareType, $node, $limit, $offset) {
706
+        /** @var Share[] $shares */
707
+        $shares = [];
708
+
709
+        if ($shareType === \OCP\Share::SHARE_TYPE_USER) {
710
+            //Get shares directly with this user
711
+            $qb = $this->dbConn->getQueryBuilder();
712
+            $qb->select('s.*',
713
+                'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash',
714
+                'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime',
715
+                'f.encrypted', 'f.unencrypted_size', 'f.etag', 'f.checksum'
716
+            )
717
+                ->selectAlias('st.id', 'storage_string_id')
718
+                ->from('share', 's')
719
+                ->leftJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid'))
720
+                ->leftJoin('f', 'storages', 'st', $qb->expr()->eq('f.storage', 'st.numeric_id'));
721
+
722
+            // Order by id
723
+            $qb->orderBy('s.id');
724
+
725
+            // Set limit and offset
726
+            if ($limit !== -1) {
727
+                $qb->setMaxResults($limit);
728
+            }
729
+            $qb->setFirstResult($offset);
730
+
731
+            $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)))
732
+                ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)))
733
+                ->andWhere($qb->expr()->orX(
734
+                    $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
735
+                    $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
736
+                ));
737
+
738
+            // Filter by node if provided
739
+            if ($node !== null) {
740
+                $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
741
+            }
742
+
743
+            $cursor = $qb->execute();
744
+
745
+            while($data = $cursor->fetch()) {
746
+                if ($this->isAccessibleResult($data)) {
747
+                    $shares[] = $this->createShare($data);
748
+                }
749
+            }
750
+            $cursor->closeCursor();
751
+
752
+        } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
753
+            $user = $this->userManager->get($userId);
754
+            $allGroups = $this->groupManager->getUserGroups($user);
755
+
756
+            /** @var Share[] $shares2 */
757
+            $shares2 = [];
758
+
759
+            $start = 0;
760
+            while(true) {
761
+                $groups = array_slice($allGroups, $start, 100);
762
+                $start += 100;
763
+
764
+                if ($groups === []) {
765
+                    break;
766
+                }
767
+
768
+                $qb = $this->dbConn->getQueryBuilder();
769
+                $qb->select('s.*',
770
+                    'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash',
771
+                    'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime',
772
+                    'f.encrypted', 'f.unencrypted_size', 'f.etag', 'f.checksum'
773
+                )
774
+                    ->selectAlias('st.id', 'storage_string_id')
775
+                    ->from('share', 's')
776
+                    ->leftJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid'))
777
+                    ->leftJoin('f', 'storages', 'st', $qb->expr()->eq('f.storage', 'st.numeric_id'))
778
+                    ->orderBy('s.id')
779
+                    ->setFirstResult(0);
780
+
781
+                if ($limit !== -1) {
782
+                    $qb->setMaxResults($limit - count($shares));
783
+                }
784
+
785
+                // Filter by node if provided
786
+                if ($node !== null) {
787
+                    $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
788
+                }
789
+
790
+
791
+                $groups = array_filter($groups, function($group) { return $group instanceof IGroup; });
792
+                $groups = array_map(function(IGroup $group) { return $group->getGID(); }, $groups);
793
+
794
+                $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)))
795
+                    ->andWhere($qb->expr()->in('share_with', $qb->createNamedParameter(
796
+                        $groups,
797
+                        IQueryBuilder::PARAM_STR_ARRAY
798
+                    )))
799
+                    ->andWhere($qb->expr()->orX(
800
+                        $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
801
+                        $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
802
+                    ));
803
+
804
+                $cursor = $qb->execute();
805
+                while($data = $cursor->fetch()) {
806
+                    if ($offset > 0) {
807
+                        $offset--;
808
+                        continue;
809
+                    }
810
+
811
+                    if ($this->isAccessibleResult($data)) {
812
+                        $shares2[] = $this->createShare($data);
813
+                    }
814
+                }
815
+                $cursor->closeCursor();
816
+            }
817
+
818
+            /*
819 819
  			 * Resolve all group shares to user specific shares
820 820
  			 */
821
-			$shares = $this->resolveGroupShares($shares2, $userId);
822
-		} else {
823
-			throw new BackendError('Invalid backend');
824
-		}
825
-
826
-
827
-		return $shares;
828
-	}
829
-
830
-	/**
831
-	 * Get a share by token
832
-	 *
833
-	 * @param string $token
834
-	 * @return \OCP\Share\IShare
835
-	 * @throws ShareNotFound
836
-	 */
837
-	public function getShareByToken($token) {
838
-		$qb = $this->dbConn->getQueryBuilder();
839
-
840
-		$cursor = $qb->select('*')
841
-			->from('share')
842
-			->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK)))
843
-			->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token)))
844
-			->andWhere($qb->expr()->orX(
845
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
846
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
847
-			))
848
-			->execute();
849
-
850
-		$data = $cursor->fetch();
851
-
852
-		if ($data === false) {
853
-			throw new ShareNotFound();
854
-		}
855
-
856
-		try {
857
-			$share = $this->createShare($data);
858
-		} catch (InvalidShare $e) {
859
-			throw new ShareNotFound();
860
-		}
861
-
862
-		return $share;
863
-	}
864
-
865
-	/**
866
-	 * Create a share object from an database row
867
-	 *
868
-	 * @param mixed[] $data
869
-	 * @return \OCP\Share\IShare
870
-	 * @throws InvalidShare
871
-	 */
872
-	private function createShare($data) {
873
-		$share = new Share($this->rootFolder, $this->userManager);
874
-		$share->setId((int)$data['id'])
875
-			->setShareType((int)$data['share_type'])
876
-			->setPermissions((int)$data['permissions'])
877
-			->setTarget($data['file_target'])
878
-			->setMailSend((bool)$data['mail_send']);
879
-
880
-		$shareTime = new \DateTime();
881
-		$shareTime->setTimestamp((int)$data['stime']);
882
-		$share->setShareTime($shareTime);
883
-
884
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
885
-			$share->setSharedWith($data['share_with']);
886
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
887
-			$share->setSharedWith($data['share_with']);
888
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
889
-			$share->setPassword($data['password']);
890
-			$share->setToken($data['token']);
891
-		}
892
-
893
-		$share->setSharedBy($data['uid_initiator']);
894
-		$share->setShareOwner($data['uid_owner']);
895
-
896
-		$share->setNodeId((int)$data['file_source']);
897
-		$share->setNodeType($data['item_type']);
898
-
899
-		if ($data['expiration'] !== null) {
900
-			$expiration = \DateTime::createFromFormat('Y-m-d H:i:s', $data['expiration']);
901
-			$share->setExpirationDate($expiration);
902
-		}
903
-
904
-		if (isset($data['f_permissions'])) {
905
-			$entryData = $data;
906
-			$entryData['permissions'] = $entryData['f_permissions'];
907
-			$entryData['parent'] = $entryData['f_parent'];
908
-			$share->setNodeCacheEntry(Cache::cacheEntryFromData($entryData,
909
-				\OC::$server->getMimeTypeLoader()));
910
-		}
911
-
912
-		$share->setProviderId($this->identifier());
913
-
914
-		return $share;
915
-	}
916
-
917
-	/**
918
-	 * @param Share[] $shares
919
-	 * @param $userId
920
-	 * @return Share[] The updates shares if no update is found for a share return the original
921
-	 */
922
-	private function resolveGroupShares($shares, $userId) {
923
-		$result = [];
924
-
925
-		$start = 0;
926
-		while(true) {
927
-			/** @var Share[] $shareSlice */
928
-			$shareSlice = array_slice($shares, $start, 100);
929
-			$start += 100;
930
-
931
-			if ($shareSlice === []) {
932
-				break;
933
-			}
934
-
935
-			/** @var int[] $ids */
936
-			$ids = [];
937
-			/** @var Share[] $shareMap */
938
-			$shareMap = [];
939
-
940
-			foreach ($shareSlice as $share) {
941
-				$ids[] = (int)$share->getId();
942
-				$shareMap[$share->getId()] = $share;
943
-			}
944
-
945
-			$qb = $this->dbConn->getQueryBuilder();
946
-
947
-			$query = $qb->select('*')
948
-				->from('share')
949
-				->where($qb->expr()->in('parent', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
950
-				->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)))
951
-				->andWhere($qb->expr()->orX(
952
-					$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
953
-					$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
954
-				));
955
-
956
-			$stmt = $query->execute();
957
-
958
-			while($data = $stmt->fetch()) {
959
-				$shareMap[$data['parent']]->setPermissions((int)$data['permissions']);
960
-				$shareMap[$data['parent']]->setTarget($data['file_target']);
961
-				$shareMap[$data['parent']]->setParent($data['parent']);
962
-			}
963
-
964
-			$stmt->closeCursor();
965
-
966
-			foreach ($shareMap as $share) {
967
-				$result[] = $share;
968
-			}
969
-		}
970
-
971
-		return $result;
972
-	}
973
-
974
-	/**
975
-	 * A user is deleted from the system
976
-	 * So clean up the relevant shares.
977
-	 *
978
-	 * @param string $uid
979
-	 * @param int $shareType
980
-	 */
981
-	public function userDeleted($uid, $shareType) {
982
-		$qb = $this->dbConn->getQueryBuilder();
983
-
984
-		$qb->delete('share');
985
-
986
-		if ($shareType === \OCP\Share::SHARE_TYPE_USER) {
987
-			/*
821
+            $shares = $this->resolveGroupShares($shares2, $userId);
822
+        } else {
823
+            throw new BackendError('Invalid backend');
824
+        }
825
+
826
+
827
+        return $shares;
828
+    }
829
+
830
+    /**
831
+     * Get a share by token
832
+     *
833
+     * @param string $token
834
+     * @return \OCP\Share\IShare
835
+     * @throws ShareNotFound
836
+     */
837
+    public function getShareByToken($token) {
838
+        $qb = $this->dbConn->getQueryBuilder();
839
+
840
+        $cursor = $qb->select('*')
841
+            ->from('share')
842
+            ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK)))
843
+            ->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token)))
844
+            ->andWhere($qb->expr()->orX(
845
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
846
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
847
+            ))
848
+            ->execute();
849
+
850
+        $data = $cursor->fetch();
851
+
852
+        if ($data === false) {
853
+            throw new ShareNotFound();
854
+        }
855
+
856
+        try {
857
+            $share = $this->createShare($data);
858
+        } catch (InvalidShare $e) {
859
+            throw new ShareNotFound();
860
+        }
861
+
862
+        return $share;
863
+    }
864
+
865
+    /**
866
+     * Create a share object from an database row
867
+     *
868
+     * @param mixed[] $data
869
+     * @return \OCP\Share\IShare
870
+     * @throws InvalidShare
871
+     */
872
+    private function createShare($data) {
873
+        $share = new Share($this->rootFolder, $this->userManager);
874
+        $share->setId((int)$data['id'])
875
+            ->setShareType((int)$data['share_type'])
876
+            ->setPermissions((int)$data['permissions'])
877
+            ->setTarget($data['file_target'])
878
+            ->setMailSend((bool)$data['mail_send']);
879
+
880
+        $shareTime = new \DateTime();
881
+        $shareTime->setTimestamp((int)$data['stime']);
882
+        $share->setShareTime($shareTime);
883
+
884
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
885
+            $share->setSharedWith($data['share_with']);
886
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
887
+            $share->setSharedWith($data['share_with']);
888
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
889
+            $share->setPassword($data['password']);
890
+            $share->setToken($data['token']);
891
+        }
892
+
893
+        $share->setSharedBy($data['uid_initiator']);
894
+        $share->setShareOwner($data['uid_owner']);
895
+
896
+        $share->setNodeId((int)$data['file_source']);
897
+        $share->setNodeType($data['item_type']);
898
+
899
+        if ($data['expiration'] !== null) {
900
+            $expiration = \DateTime::createFromFormat('Y-m-d H:i:s', $data['expiration']);
901
+            $share->setExpirationDate($expiration);
902
+        }
903
+
904
+        if (isset($data['f_permissions'])) {
905
+            $entryData = $data;
906
+            $entryData['permissions'] = $entryData['f_permissions'];
907
+            $entryData['parent'] = $entryData['f_parent'];
908
+            $share->setNodeCacheEntry(Cache::cacheEntryFromData($entryData,
909
+                \OC::$server->getMimeTypeLoader()));
910
+        }
911
+
912
+        $share->setProviderId($this->identifier());
913
+
914
+        return $share;
915
+    }
916
+
917
+    /**
918
+     * @param Share[] $shares
919
+     * @param $userId
920
+     * @return Share[] The updates shares if no update is found for a share return the original
921
+     */
922
+    private function resolveGroupShares($shares, $userId) {
923
+        $result = [];
924
+
925
+        $start = 0;
926
+        while(true) {
927
+            /** @var Share[] $shareSlice */
928
+            $shareSlice = array_slice($shares, $start, 100);
929
+            $start += 100;
930
+
931
+            if ($shareSlice === []) {
932
+                break;
933
+            }
934
+
935
+            /** @var int[] $ids */
936
+            $ids = [];
937
+            /** @var Share[] $shareMap */
938
+            $shareMap = [];
939
+
940
+            foreach ($shareSlice as $share) {
941
+                $ids[] = (int)$share->getId();
942
+                $shareMap[$share->getId()] = $share;
943
+            }
944
+
945
+            $qb = $this->dbConn->getQueryBuilder();
946
+
947
+            $query = $qb->select('*')
948
+                ->from('share')
949
+                ->where($qb->expr()->in('parent', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
950
+                ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)))
951
+                ->andWhere($qb->expr()->orX(
952
+                    $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
953
+                    $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
954
+                ));
955
+
956
+            $stmt = $query->execute();
957
+
958
+            while($data = $stmt->fetch()) {
959
+                $shareMap[$data['parent']]->setPermissions((int)$data['permissions']);
960
+                $shareMap[$data['parent']]->setTarget($data['file_target']);
961
+                $shareMap[$data['parent']]->setParent($data['parent']);
962
+            }
963
+
964
+            $stmt->closeCursor();
965
+
966
+            foreach ($shareMap as $share) {
967
+                $result[] = $share;
968
+            }
969
+        }
970
+
971
+        return $result;
972
+    }
973
+
974
+    /**
975
+     * A user is deleted from the system
976
+     * So clean up the relevant shares.
977
+     *
978
+     * @param string $uid
979
+     * @param int $shareType
980
+     */
981
+    public function userDeleted($uid, $shareType) {
982
+        $qb = $this->dbConn->getQueryBuilder();
983
+
984
+        $qb->delete('share');
985
+
986
+        if ($shareType === \OCP\Share::SHARE_TYPE_USER) {
987
+            /*
988 988
 			 * Delete all user shares that are owned by this user
989 989
 			 * or that are received by this user
990 990
 			 */
991 991
 
992
-			$qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)));
992
+            $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)));
993 993
 
994
-			$qb->andWhere(
995
-				$qb->expr()->orX(
996
-					$qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)),
997
-					$qb->expr()->eq('share_with', $qb->createNamedParameter($uid))
998
-				)
999
-			);
1000
-		} else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
1001
-			/*
994
+            $qb->andWhere(
995
+                $qb->expr()->orX(
996
+                    $qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)),
997
+                    $qb->expr()->eq('share_with', $qb->createNamedParameter($uid))
998
+                )
999
+            );
1000
+        } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
1001
+            /*
1002 1002
 			 * Delete all group shares that are owned by this user
1003 1003
 			 * Or special user group shares that are received by this user
1004 1004
 			 */
1005
-			$qb->where(
1006
-				$qb->expr()->andX(
1007
-					$qb->expr()->orX(
1008
-						$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)),
1009
-						$qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))
1010
-					),
1011
-					$qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid))
1012
-				)
1013
-			);
1014
-
1015
-			$qb->orWhere(
1016
-				$qb->expr()->andX(
1017
-					$qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)),
1018
-					$qb->expr()->eq('share_with', $qb->createNamedParameter($uid))
1019
-				)
1020
-			);
1021
-		} else if ($shareType === \OCP\Share::SHARE_TYPE_LINK) {
1022
-			/*
1005
+            $qb->where(
1006
+                $qb->expr()->andX(
1007
+                    $qb->expr()->orX(
1008
+                        $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)),
1009
+                        $qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))
1010
+                    ),
1011
+                    $qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid))
1012
+                )
1013
+            );
1014
+
1015
+            $qb->orWhere(
1016
+                $qb->expr()->andX(
1017
+                    $qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)),
1018
+                    $qb->expr()->eq('share_with', $qb->createNamedParameter($uid))
1019
+                )
1020
+            );
1021
+        } else if ($shareType === \OCP\Share::SHARE_TYPE_LINK) {
1022
+            /*
1023 1023
 			 * Delete all link shares owned by this user.
1024 1024
 			 * And all link shares initiated by this user (until #22327 is in)
1025 1025
 			 */
1026
-			$qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK)));
1027
-
1028
-			$qb->andWhere(
1029
-				$qb->expr()->orX(
1030
-					$qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)),
1031
-					$qb->expr()->eq('uid_initiator', $qb->createNamedParameter($uid))
1032
-				)
1033
-			);
1034
-		}
1035
-
1036
-		$qb->execute();
1037
-	}
1038
-
1039
-	/**
1040
-	 * Delete all shares received by this group. As well as any custom group
1041
-	 * shares for group members.
1042
-	 *
1043
-	 * @param string $gid
1044
-	 */
1045
-	public function groupDeleted($gid) {
1046
-		/*
1026
+            $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK)));
1027
+
1028
+            $qb->andWhere(
1029
+                $qb->expr()->orX(
1030
+                    $qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)),
1031
+                    $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($uid))
1032
+                )
1033
+            );
1034
+        }
1035
+
1036
+        $qb->execute();
1037
+    }
1038
+
1039
+    /**
1040
+     * Delete all shares received by this group. As well as any custom group
1041
+     * shares for group members.
1042
+     *
1043
+     * @param string $gid
1044
+     */
1045
+    public function groupDeleted($gid) {
1046
+        /*
1047 1047
 		 * First delete all custom group shares for group members
1048 1048
 		 */
1049
-		$qb = $this->dbConn->getQueryBuilder();
1050
-		$qb->select('id')
1051
-			->from('share')
1052
-			->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)))
1053
-			->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid)));
1054
-
1055
-		$cursor = $qb->execute();
1056
-		$ids = [];
1057
-		while($row = $cursor->fetch()) {
1058
-			$ids[] = (int)$row['id'];
1059
-		}
1060
-		$cursor->closeCursor();
1061
-
1062
-		if (!empty($ids)) {
1063
-			$chunks = array_chunk($ids, 100);
1064
-			foreach ($chunks as $chunk) {
1065
-				$qb->delete('share')
1066
-					->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
1067
-					->andWhere($qb->expr()->in('parent', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY)));
1068
-				$qb->execute();
1069
-			}
1070
-		}
1071
-
1072
-		/*
1049
+        $qb = $this->dbConn->getQueryBuilder();
1050
+        $qb->select('id')
1051
+            ->from('share')
1052
+            ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)))
1053
+            ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid)));
1054
+
1055
+        $cursor = $qb->execute();
1056
+        $ids = [];
1057
+        while($row = $cursor->fetch()) {
1058
+            $ids[] = (int)$row['id'];
1059
+        }
1060
+        $cursor->closeCursor();
1061
+
1062
+        if (!empty($ids)) {
1063
+            $chunks = array_chunk($ids, 100);
1064
+            foreach ($chunks as $chunk) {
1065
+                $qb->delete('share')
1066
+                    ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
1067
+                    ->andWhere($qb->expr()->in('parent', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY)));
1068
+                $qb->execute();
1069
+            }
1070
+        }
1071
+
1072
+        /*
1073 1073
 		 * Now delete all the group shares
1074 1074
 		 */
1075
-		$qb = $this->dbConn->getQueryBuilder();
1076
-		$qb->delete('share')
1077
-			->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)))
1078
-			->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid)));
1079
-		$qb->execute();
1080
-	}
1081
-
1082
-	/**
1083
-	 * Delete custom group shares to this group for this user
1084
-	 *
1085
-	 * @param string $uid
1086
-	 * @param string $gid
1087
-	 */
1088
-	public function userDeletedFromGroup($uid, $gid) {
1089
-		/*
1075
+        $qb = $this->dbConn->getQueryBuilder();
1076
+        $qb->delete('share')
1077
+            ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)))
1078
+            ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid)));
1079
+        $qb->execute();
1080
+    }
1081
+
1082
+    /**
1083
+     * Delete custom group shares to this group for this user
1084
+     *
1085
+     * @param string $uid
1086
+     * @param string $gid
1087
+     */
1088
+    public function userDeletedFromGroup($uid, $gid) {
1089
+        /*
1090 1090
 		 * Get all group shares
1091 1091
 		 */
1092
-		$qb = $this->dbConn->getQueryBuilder();
1093
-		$qb->select('id')
1094
-			->from('share')
1095
-			->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)))
1096
-			->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid)));
1097
-
1098
-		$cursor = $qb->execute();
1099
-		$ids = [];
1100
-		while($row = $cursor->fetch()) {
1101
-			$ids[] = (int)$row['id'];
1102
-		}
1103
-		$cursor->closeCursor();
1104
-
1105
-		if (!empty($ids)) {
1106
-			$chunks = array_chunk($ids, 100);
1107
-			foreach ($chunks as $chunk) {
1108
-				/*
1092
+        $qb = $this->dbConn->getQueryBuilder();
1093
+        $qb->select('id')
1094
+            ->from('share')
1095
+            ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)))
1096
+            ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid)));
1097
+
1098
+        $cursor = $qb->execute();
1099
+        $ids = [];
1100
+        while($row = $cursor->fetch()) {
1101
+            $ids[] = (int)$row['id'];
1102
+        }
1103
+        $cursor->closeCursor();
1104
+
1105
+        if (!empty($ids)) {
1106
+            $chunks = array_chunk($ids, 100);
1107
+            foreach ($chunks as $chunk) {
1108
+                /*
1109 1109
 				 * Delete all special shares wit this users for the found group shares
1110 1110
 				 */
1111
-				$qb->delete('share')
1112
-					->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
1113
-					->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($uid)))
1114
-					->andWhere($qb->expr()->in('parent', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY)));
1115
-				$qb->execute();
1116
-			}
1117
-		}
1118
-	}
1119
-
1120
-	/**
1121
-	 * @inheritdoc
1122
-	 */
1123
-	public function getAccessList($nodes, $currentAccess) {
1124
-		$ids = [];
1125
-		foreach ($nodes as $node) {
1126
-			$ids[] = $node->getId();
1127
-		}
1128
-
1129
-		$qb = $this->dbConn->getQueryBuilder();
1130
-
1131
-		$or = $qb->expr()->orX(
1132
-			$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)),
1133
-			$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)),
1134
-			$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK))
1135
-		);
1136
-
1137
-		if ($currentAccess) {
1138
-			$or->add($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)));
1139
-		}
1140
-
1141
-		$qb->select('id', 'parent', 'share_type', 'share_with', 'file_source', 'file_target', 'permissions')
1142
-			->from('share')
1143
-			->where(
1144
-				$or
1145
-			)
1146
-			->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
1147
-			->andWhere($qb->expr()->orX(
1148
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
1149
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
1150
-			));
1151
-		$cursor = $qb->execute();
1152
-
1153
-		$users = [];
1154
-		$link = false;
1155
-		while($row = $cursor->fetch()) {
1156
-			$type = (int)$row['share_type'];
1157
-			if ($type === \OCP\Share::SHARE_TYPE_USER) {
1158
-				$uid = $row['share_with'];
1159
-				$users[$uid] = isset($users[$uid]) ? $users[$uid] : [];
1160
-				$users[$uid][$row['id']] = $row;
1161
-			} else if ($type === \OCP\Share::SHARE_TYPE_GROUP) {
1162
-				$gid = $row['share_with'];
1163
-				$group = $this->groupManager->get($gid);
1164
-
1165
-				if ($group === null) {
1166
-					continue;
1167
-				}
1168
-
1169
-				$userList = $group->getUsers();
1170
-				foreach ($userList as $user) {
1171
-					$uid = $user->getUID();
1172
-					$users[$uid] = isset($users[$uid]) ? $users[$uid] : [];
1173
-					$users[$uid][$row['id']] = $row;
1174
-				}
1175
-			} else if ($type === \OCP\Share::SHARE_TYPE_LINK) {
1176
-				$link = true;
1177
-			} else if ($type === self::SHARE_TYPE_USERGROUP && $currentAccess === true) {
1178
-				$uid = $row['share_with'];
1179
-				$users[$uid] = isset($users[$uid]) ? $users[$uid] : [];
1180
-				$users[$uid][$row['id']] = $row;
1181
-			}
1182
-		}
1183
-		$cursor->closeCursor();
1184
-
1185
-		if ($currentAccess === true) {
1186
-			$users = array_map([$this, 'filterSharesOfUser'], $users);
1187
-			$users = array_filter($users);
1188
-		} else {
1189
-			$users = array_keys($users);
1190
-		}
1191
-
1192
-		return ['users' => $users, 'public' => $link];
1193
-	}
1194
-
1195
-	/**
1196
-	 * For each user the path with the fewest slashes is returned
1197
-	 * @param array $shares
1198
-	 * @return array
1199
-	 */
1200
-	protected function filterSharesOfUser(array $shares) {
1201
-		// Group shares when the user has a share exception
1202
-		foreach ($shares as $id => $share) {
1203
-			$type = (int) $share['share_type'];
1204
-			$permissions = (int) $share['permissions'];
1205
-
1206
-			if ($type === self::SHARE_TYPE_USERGROUP) {
1207
-				unset($shares[$share['parent']]);
1208
-
1209
-				if ($permissions === 0) {
1210
-					unset($shares[$id]);
1211
-				}
1212
-			}
1213
-		}
1214
-
1215
-		$best = [];
1216
-		$bestDepth = 0;
1217
-		foreach ($shares as $id => $share) {
1218
-			$depth = substr_count($share['file_target'], '/');
1219
-			if (empty($best) || $depth < $bestDepth) {
1220
-				$bestDepth = $depth;
1221
-				$best = [
1222
-					'node_id' => $share['file_source'],
1223
-					'node_path' => $share['file_target'],
1224
-				];
1225
-			}
1226
-		}
1227
-
1228
-		return $best;
1229
-	}
1111
+                $qb->delete('share')
1112
+                    ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
1113
+                    ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($uid)))
1114
+                    ->andWhere($qb->expr()->in('parent', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY)));
1115
+                $qb->execute();
1116
+            }
1117
+        }
1118
+    }
1119
+
1120
+    /**
1121
+     * @inheritdoc
1122
+     */
1123
+    public function getAccessList($nodes, $currentAccess) {
1124
+        $ids = [];
1125
+        foreach ($nodes as $node) {
1126
+            $ids[] = $node->getId();
1127
+        }
1128
+
1129
+        $qb = $this->dbConn->getQueryBuilder();
1130
+
1131
+        $or = $qb->expr()->orX(
1132
+            $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)),
1133
+            $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)),
1134
+            $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK))
1135
+        );
1136
+
1137
+        if ($currentAccess) {
1138
+            $or->add($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)));
1139
+        }
1140
+
1141
+        $qb->select('id', 'parent', 'share_type', 'share_with', 'file_source', 'file_target', 'permissions')
1142
+            ->from('share')
1143
+            ->where(
1144
+                $or
1145
+            )
1146
+            ->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
1147
+            ->andWhere($qb->expr()->orX(
1148
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
1149
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
1150
+            ));
1151
+        $cursor = $qb->execute();
1152
+
1153
+        $users = [];
1154
+        $link = false;
1155
+        while($row = $cursor->fetch()) {
1156
+            $type = (int)$row['share_type'];
1157
+            if ($type === \OCP\Share::SHARE_TYPE_USER) {
1158
+                $uid = $row['share_with'];
1159
+                $users[$uid] = isset($users[$uid]) ? $users[$uid] : [];
1160
+                $users[$uid][$row['id']] = $row;
1161
+            } else if ($type === \OCP\Share::SHARE_TYPE_GROUP) {
1162
+                $gid = $row['share_with'];
1163
+                $group = $this->groupManager->get($gid);
1164
+
1165
+                if ($group === null) {
1166
+                    continue;
1167
+                }
1168
+
1169
+                $userList = $group->getUsers();
1170
+                foreach ($userList as $user) {
1171
+                    $uid = $user->getUID();
1172
+                    $users[$uid] = isset($users[$uid]) ? $users[$uid] : [];
1173
+                    $users[$uid][$row['id']] = $row;
1174
+                }
1175
+            } else if ($type === \OCP\Share::SHARE_TYPE_LINK) {
1176
+                $link = true;
1177
+            } else if ($type === self::SHARE_TYPE_USERGROUP && $currentAccess === true) {
1178
+                $uid = $row['share_with'];
1179
+                $users[$uid] = isset($users[$uid]) ? $users[$uid] : [];
1180
+                $users[$uid][$row['id']] = $row;
1181
+            }
1182
+        }
1183
+        $cursor->closeCursor();
1184
+
1185
+        if ($currentAccess === true) {
1186
+            $users = array_map([$this, 'filterSharesOfUser'], $users);
1187
+            $users = array_filter($users);
1188
+        } else {
1189
+            $users = array_keys($users);
1190
+        }
1191
+
1192
+        return ['users' => $users, 'public' => $link];
1193
+    }
1194
+
1195
+    /**
1196
+     * For each user the path with the fewest slashes is returned
1197
+     * @param array $shares
1198
+     * @return array
1199
+     */
1200
+    protected function filterSharesOfUser(array $shares) {
1201
+        // Group shares when the user has a share exception
1202
+        foreach ($shares as $id => $share) {
1203
+            $type = (int) $share['share_type'];
1204
+            $permissions = (int) $share['permissions'];
1205
+
1206
+            if ($type === self::SHARE_TYPE_USERGROUP) {
1207
+                unset($shares[$share['parent']]);
1208
+
1209
+                if ($permissions === 0) {
1210
+                    unset($shares[$id]);
1211
+                }
1212
+            }
1213
+        }
1214
+
1215
+        $best = [];
1216
+        $bestDepth = 0;
1217
+        foreach ($shares as $id => $share) {
1218
+            $depth = substr_count($share['file_target'], '/');
1219
+            if (empty($best) || $depth < $bestDepth) {
1220
+                $bestDepth = $depth;
1221
+                $best = [
1222
+                    'node_id' => $share['file_source'],
1223
+                    'node_path' => $share['file_target'],
1224
+                ];
1225
+            }
1226
+        }
1227
+
1228
+        return $best;
1229
+    }
1230 1230
 }
Please login to merge, or discard this patch.
apps/sharebymail/lib/ShareByMailProvider.php 1 patch
Indentation   +1022 added lines, -1022 removed lines patch added patch discarded remove patch
@@ -54,1040 +54,1040 @@
 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 %s failed, this item is already shared with %s';
169
-			$message_t = $this->l->t('Sharing %s failed, this item is already shared with %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
-	 */
247
-	protected function createShareActivity(IShare $share) {
248
-
249
-		$userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
250
-
251
-		$this->publishActivity(
252
-			Activity::SUBJECT_SHARED_EMAIL_SELF,
253
-			[$userFolder->getRelativePath($share->getNode()->getPath()), $share->getSharedWith()],
254
-			$share->getSharedBy(),
255
-			$share->getNode()->getId(),
256
-			$userFolder->getRelativePath($share->getNode()->getPath())
257
-		);
258
-
259
-		if ($share->getShareOwner() !== $share->getSharedBy()) {
260
-			$ownerFolder = $this->rootFolder->getUserFolder($share->getShareOwner());
261
-			$fileId = $share->getNode()->getId();
262
-			$nodes = $ownerFolder->getById($fileId);
263
-			$ownerPath = $nodes[0]->getPath();
264
-			$this->publishActivity(
265
-				Activity::SUBJECT_SHARED_EMAIL_BY,
266
-				[$ownerFolder->getRelativePath($ownerPath), $share->getSharedWith(), $share->getSharedBy()],
267
-				$share->getShareOwner(),
268
-				$fileId,
269
-				$ownerFolder->getRelativePath($ownerPath)
270
-			);
271
-		}
272
-
273
-	}
274
-
275
-	/**
276
-	 * create activity if a file/folder was shared by mail
277
-	 *
278
-	 * @param IShare $share
279
-	 * @param string $sharedWith
280
-	 * @param bool $sendToSelf
281
-	 */
282
-	protected function createPasswordSendActivity(IShare $share, $sharedWith, $sendToSelf) {
283
-
284
-		$userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
285
-
286
-		if ($sendToSelf) {
287
-			$this->publishActivity(
288
-				Activity::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF,
289
-				[$userFolder->getRelativePath($share->getNode()->getPath())],
290
-				$share->getSharedBy(),
291
-				$share->getNode()->getId(),
292
-				$userFolder->getRelativePath($share->getNode()->getPath())
293
-			);
294
-		} else {
295
-			$this->publishActivity(
296
-				Activity::SUBJECT_SHARED_EMAIL_PASSWORD_SEND,
297
-				[$userFolder->getRelativePath($share->getNode()->getPath()), $sharedWith],
298
-				$share->getSharedBy(),
299
-				$share->getNode()->getId(),
300
-				$userFolder->getRelativePath($share->getNode()->getPath())
301
-			);
302
-		}
303
-	}
304
-
305
-
306
-	/**
307
-	 * publish activity if a file/folder was shared by mail
308
-	 *
309
-	 * @param $subject
310
-	 * @param $parameters
311
-	 * @param $affectedUser
312
-	 * @param $fileId
313
-	 * @param $filePath
314
-	 */
315
-	protected function publishActivity($subject, $parameters, $affectedUser, $fileId, $filePath) {
316
-		$event = $this->activityManager->generateEvent();
317
-		$event->setApp('sharebymail')
318
-			->setType('shared')
319
-			->setSubject($subject, $parameters)
320
-			->setAffectedUser($affectedUser)
321
-			->setObject('files', $fileId, $filePath);
322
-		$this->activityManager->publish($event);
323
-
324
-	}
325
-
326
-	/**
327
-	 * @param IShare $share
328
-	 * @return int
329
-	 * @throws \Exception
330
-	 */
331
-	protected function createMailShare(IShare $share) {
332
-		$share->setToken($this->generateToken());
333
-		$shareId = $this->addShareToDB(
334
-			$share->getNodeId(),
335
-			$share->getNodeType(),
336
-			$share->getSharedWith(),
337
-			$share->getSharedBy(),
338
-			$share->getShareOwner(),
339
-			$share->getPermissions(),
340
-			$share->getToken(),
341
-			$share->getPassword()
342
-		);
343
-
344
-		try {
345
-			$link = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare',
346
-				['token' => $share->getToken()]);
347
-			$this->sendMailNotification(
348
-				$share->getNode()->getName(),
349
-				$link,
350
-				$share->getSharedBy(),
351
-				$share->getSharedWith(),
352
-				$share->getExpirationDate()
353
-			);
354
-		} catch (HintException $hintException) {
355
-			$this->logger->logException($hintException, [
356
-				'message' => 'Failed to send share by mail.',
357
-				'level' => ILogger::ERROR,
358
-				'app' => 'sharebymail',
359
-			]);
360
-			$this->removeShareFromTable($shareId);
361
-			throw $hintException;
362
-		} catch (\Exception $e) {
363
-			$this->logger->logException($e, [
364
-				'message' => 'Failed to send share by mail.',
365
-				'level' => ILogger::ERROR,
366
-				'app' => 'sharebymail',
367
-			]);
368
-			$this->removeShareFromTable($shareId);
369
-			throw new HintException('Failed to send share by mail',
370
-				$this->l->t('Failed to send share by email'));
371
-		}
372
-
373
-		return $shareId;
374
-
375
-	}
376
-
377
-	/**
378
-	 * @param string $filename
379
-	 * @param string $link
380
-	 * @param string $initiator
381
-	 * @param string $shareWith
382
-	 * @param \DateTime|null $expiration
383
-	 * @throws \Exception If mail couldn't be sent
384
-	 */
385
-	protected function sendMailNotification($filename,
386
-											$link,
387
-											$initiator,
388
-											$shareWith,
389
-											\DateTime $expiration = null) {
390
-		$initiatorUser = $this->userManager->get($initiator);
391
-		$initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
392
-		$message = $this->mailer->createMessage();
393
-
394
-		$emailTemplate = $this->mailer->createEMailTemplate('sharebymail.RecipientNotification', [
395
-			'filename' => $filename,
396
-			'link' => $link,
397
-			'initiator' => $initiatorDisplayName,
398
-			'expiration' => $expiration,
399
-			'shareWith' => $shareWith,
400
-		]);
401
-
402
-		$emailTemplate->setSubject($this->l->t('%s shared »%s« with you', array($initiatorDisplayName, $filename)));
403
-		$emailTemplate->addHeader();
404
-		$emailTemplate->addHeading($this->l->t('%s shared »%s« with you', [$initiatorDisplayName, $filename]), false);
405
-		$text = $this->l->t('%s shared »%s« with you.', [$initiatorDisplayName, $filename]);
406
-
407
-		$emailTemplate->addBodyText(
408
-			htmlspecialchars($text . ' ' . $this->l->t('Click the button below to open it.')),
409
-			$text
410
-		);
411
-		$emailTemplate->addBodyButton(
412
-			$this->l->t('Open »%s«', [$filename]),
413
-			$link
414
-		);
415
-
416
-		$message->setTo([$shareWith]);
417
-
418
-		// The "From" contains the sharers name
419
-		$instanceName = $this->defaults->getName();
420
-		$senderName = $this->l->t(
421
-			'%s via %s',
422
-			[
423
-				$initiatorDisplayName,
424
-				$instanceName
425
-			]
426
-		);
427
-		$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
428
-
429
-		// The "Reply-To" is set to the sharer if an mail address is configured
430
-		// also the default footer contains a "Do not reply" which needs to be adjusted.
431
-		$initiatorEmail = $initiatorUser->getEMailAddress();
432
-		if($initiatorEmail !== null) {
433
-			$message->setReplyTo([$initiatorEmail => $initiatorDisplayName]);
434
-			$emailTemplate->addFooter($instanceName . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : ''));
435
-		} else {
436
-			$emailTemplate->addFooter();
437
-		}
438
-
439
-		$message->useTemplate($emailTemplate);
440
-		$this->mailer->send($message);
441
-	}
442
-
443
-	/**
444
-	 * send password to recipient of a mail share
445
-	 *
446
-	 * @param IShare $share
447
-	 * @param string $password
448
-	 * @return bool
449
-	 */
450
-	protected function sendPassword(IShare $share, $password) {
451
-
452
-		$filename = $share->getNode()->getName();
453
-		$initiator = $share->getSharedBy();
454
-		$shareWith = $share->getSharedWith();
455
-
456
-		if ($password === '' || $this->settingsManager->sendPasswordByMail() === false) {
457
-			return false;
458
-		}
459
-
460
-		$initiatorUser = $this->userManager->get($initiator);
461
-		$initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
462
-		$initiatorEmailAddress = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null;
463
-
464
-		$plainBodyPart = $this->l->t("%s shared »%s« with you.\nYou should have already received a separate mail with a link to access it.\n", [$initiatorDisplayName, $filename]);
465
-		$htmlBodyPart = $this->l->t('%s shared »%s« with you. You should have already received a separate mail with a link to access it.', [$initiatorDisplayName, $filename]);
466
-
467
-		$message = $this->mailer->createMessage();
468
-
469
-		$emailTemplate = $this->mailer->createEMailTemplate('sharebymail.RecipientPasswordNotification', [
470
-			'filename' => $filename,
471
-			'password' => $password,
472
-			'initiator' => $initiatorDisplayName,
473
-			'initiatorEmail' => $initiatorEmailAddress,
474
-			'shareWith' => $shareWith,
475
-		]);
476
-
477
-		$emailTemplate->setSubject($this->l->t('Password to access »%s« shared to you by %s', [$filename, $initiatorDisplayName]));
478
-		$emailTemplate->addHeader();
479
-		$emailTemplate->addHeading($this->l->t('Password to access »%s«', [$filename]), false);
480
-		$emailTemplate->addBodyText(htmlspecialchars($htmlBodyPart), $plainBodyPart);
481
-		$emailTemplate->addBodyText($this->l->t('It is protected with the following password: %s', [$password]));
482
-
483
-		// The "From" contains the sharers name
484
-		$instanceName = $this->defaults->getName();
485
-		$senderName = $this->l->t(
486
-			'%s via %s',
487
-			[
488
-				$initiatorDisplayName,
489
-				$instanceName
490
-			]
491
-		);
492
-		$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
493
-		if ($initiatorEmailAddress !== null) {
494
-			$message->setReplyTo([$initiatorEmailAddress => $initiatorDisplayName]);
495
-			$emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
496
-		} else {
497
-			$emailTemplate->addFooter();
498
-		}
499
-
500
-		$message->setTo([$shareWith]);
501
-		$message->useTemplate($emailTemplate);
502
-		$this->mailer->send($message);
503
-
504
-		$this->createPasswordSendActivity($share, $shareWith, false);
505
-
506
-		return true;
507
-	}
508
-
509
-	/**
510
-	 * send auto generated password to the owner. This happens if the admin enforces
511
-	 * a password for mail shares and forbid to send the password by mail to the recipient
512
-	 *
513
-	 * @param IShare $share
514
-	 * @param string $password
515
-	 * @return bool
516
-	 * @throws \Exception
517
-	 */
518
-	protected function sendPasswordToOwner(IShare $share, $password) {
519
-
520
-		$filename = $share->getNode()->getName();
521
-		$initiator = $this->userManager->get($share->getSharedBy());
522
-		$initiatorEMailAddress = ($initiator instanceof IUser) ? $initiator->getEMailAddress() : null;
523
-		$initiatorDisplayName = ($initiator instanceof IUser) ? $initiator->getDisplayName() : $share->getSharedBy();
524
-		$shareWith = $share->getSharedWith();
525
-
526
-		if ($initiatorEMailAddress === null) {
527
-			throw new \Exception(
528
-				$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.")
529
-			);
530
-		}
531
-
532
-		$bodyPart = $this->l->t("You just shared »%s« with %s. The share was already send to the recipient. Due to the security policies defined by the administrator of %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()]);
533
-
534
-		$message = $this->mailer->createMessage();
535
-		$emailTemplate = $this->mailer->createEMailTemplate('sharebymail.OwnerPasswordNotification', [
536
-			'filename' => $filename,
537
-			'password' => $password,
538
-			'initiator' => $initiatorDisplayName,
539
-			'initiatorEmail' => $initiatorEMailAddress,
540
-			'shareWith' => $shareWith,
541
-		]);
542
-
543
-		$emailTemplate->setSubject($this->l->t('Password to access »%s« shared with %s', [$filename, $shareWith]));
544
-		$emailTemplate->addHeader();
545
-		$emailTemplate->addHeading($this->l->t('Password to access »%s«', [$filename]), false);
546
-		$emailTemplate->addBodyText($bodyPart);
547
-		$emailTemplate->addBodyText($this->l->t('This is the password: %s', [$password]));
548
-		$emailTemplate->addBodyText($this->l->t('You can choose a different password at any time in the share dialog.'));
549
-		$emailTemplate->addFooter();
550
-
551
-		if ($initiatorEMailAddress) {
552
-			$message->setFrom([$initiatorEMailAddress => $initiatorDisplayName]);
553
-		}
554
-		$message->setTo([$initiatorEMailAddress => $initiatorDisplayName]);
555
-		$message->useTemplate($emailTemplate);
556
-		$this->mailer->send($message);
557
-
558
-		$this->createPasswordSendActivity($share, $shareWith, true);
559
-
560
-		return true;
561
-	}
562
-
563
-	/**
564
-	 * generate share token
565
-	 *
566
-	 * @return string
567
-	 */
568
-	protected function generateToken($size = 15) {
569
-		$token = $this->secureRandom->generate($size, ISecureRandom::CHAR_HUMAN_READABLE);
570
-		return $token;
571
-	}
572
-
573
-	/**
574
-	 * Get all children of this share
575
-	 *
576
-	 * @param IShare $parent
577
-	 * @return IShare[]
578
-	 */
579
-	public function getChildren(IShare $parent) {
580
-		$children = [];
581
-
582
-		$qb = $this->dbConnection->getQueryBuilder();
583
-		$qb->select('*')
584
-			->from('share')
585
-			->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId())))
586
-			->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)))
587
-			->orderBy('id');
588
-
589
-		$cursor = $qb->execute();
590
-		while($data = $cursor->fetch()) {
591
-			$children[] = $this->createShareObject($data);
592
-		}
593
-		$cursor->closeCursor();
594
-
595
-		return $children;
596
-	}
597
-
598
-	/**
599
-	 * add share to the database and return the ID
600
-	 *
601
-	 * @param int $itemSource
602
-	 * @param string $itemType
603
-	 * @param string $shareWith
604
-	 * @param string $sharedBy
605
-	 * @param string $uidOwner
606
-	 * @param int $permissions
607
-	 * @param string $token
608
-	 * @return int
609
-	 */
610
-	protected function addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, $token, $password) {
611
-		$qb = $this->dbConnection->getQueryBuilder();
612
-		$qb->insert('share')
613
-			->setValue('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL))
614
-			->setValue('item_type', $qb->createNamedParameter($itemType))
615
-			->setValue('item_source', $qb->createNamedParameter($itemSource))
616
-			->setValue('file_source', $qb->createNamedParameter($itemSource))
617
-			->setValue('share_with', $qb->createNamedParameter($shareWith))
618
-			->setValue('uid_owner', $qb->createNamedParameter($uidOwner))
619
-			->setValue('uid_initiator', $qb->createNamedParameter($sharedBy))
620
-			->setValue('permissions', $qb->createNamedParameter($permissions))
621
-			->setValue('token', $qb->createNamedParameter($token))
622
-			->setValue('password', $qb->createNamedParameter($password))
623
-			->setValue('stime', $qb->createNamedParameter(time()));
624
-
625
-		/*
166
+        $alreadyShared = $this->getSharedWith($shareWith, \OCP\Share::SHARE_TYPE_EMAIL, $share->getNode(), 1, 0);
167
+        if (!empty($alreadyShared)) {
168
+            $message = 'Sharing %s failed, this item is already shared with %s';
169
+            $message_t = $this->l->t('Sharing %s failed, this item is already shared with %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
+     */
247
+    protected function createShareActivity(IShare $share) {
248
+
249
+        $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
250
+
251
+        $this->publishActivity(
252
+            Activity::SUBJECT_SHARED_EMAIL_SELF,
253
+            [$userFolder->getRelativePath($share->getNode()->getPath()), $share->getSharedWith()],
254
+            $share->getSharedBy(),
255
+            $share->getNode()->getId(),
256
+            $userFolder->getRelativePath($share->getNode()->getPath())
257
+        );
258
+
259
+        if ($share->getShareOwner() !== $share->getSharedBy()) {
260
+            $ownerFolder = $this->rootFolder->getUserFolder($share->getShareOwner());
261
+            $fileId = $share->getNode()->getId();
262
+            $nodes = $ownerFolder->getById($fileId);
263
+            $ownerPath = $nodes[0]->getPath();
264
+            $this->publishActivity(
265
+                Activity::SUBJECT_SHARED_EMAIL_BY,
266
+                [$ownerFolder->getRelativePath($ownerPath), $share->getSharedWith(), $share->getSharedBy()],
267
+                $share->getShareOwner(),
268
+                $fileId,
269
+                $ownerFolder->getRelativePath($ownerPath)
270
+            );
271
+        }
272
+
273
+    }
274
+
275
+    /**
276
+     * create activity if a file/folder was shared by mail
277
+     *
278
+     * @param IShare $share
279
+     * @param string $sharedWith
280
+     * @param bool $sendToSelf
281
+     */
282
+    protected function createPasswordSendActivity(IShare $share, $sharedWith, $sendToSelf) {
283
+
284
+        $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
285
+
286
+        if ($sendToSelf) {
287
+            $this->publishActivity(
288
+                Activity::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF,
289
+                [$userFolder->getRelativePath($share->getNode()->getPath())],
290
+                $share->getSharedBy(),
291
+                $share->getNode()->getId(),
292
+                $userFolder->getRelativePath($share->getNode()->getPath())
293
+            );
294
+        } else {
295
+            $this->publishActivity(
296
+                Activity::SUBJECT_SHARED_EMAIL_PASSWORD_SEND,
297
+                [$userFolder->getRelativePath($share->getNode()->getPath()), $sharedWith],
298
+                $share->getSharedBy(),
299
+                $share->getNode()->getId(),
300
+                $userFolder->getRelativePath($share->getNode()->getPath())
301
+            );
302
+        }
303
+    }
304
+
305
+
306
+    /**
307
+     * publish activity if a file/folder was shared by mail
308
+     *
309
+     * @param $subject
310
+     * @param $parameters
311
+     * @param $affectedUser
312
+     * @param $fileId
313
+     * @param $filePath
314
+     */
315
+    protected function publishActivity($subject, $parameters, $affectedUser, $fileId, $filePath) {
316
+        $event = $this->activityManager->generateEvent();
317
+        $event->setApp('sharebymail')
318
+            ->setType('shared')
319
+            ->setSubject($subject, $parameters)
320
+            ->setAffectedUser($affectedUser)
321
+            ->setObject('files', $fileId, $filePath);
322
+        $this->activityManager->publish($event);
323
+
324
+    }
325
+
326
+    /**
327
+     * @param IShare $share
328
+     * @return int
329
+     * @throws \Exception
330
+     */
331
+    protected function createMailShare(IShare $share) {
332
+        $share->setToken($this->generateToken());
333
+        $shareId = $this->addShareToDB(
334
+            $share->getNodeId(),
335
+            $share->getNodeType(),
336
+            $share->getSharedWith(),
337
+            $share->getSharedBy(),
338
+            $share->getShareOwner(),
339
+            $share->getPermissions(),
340
+            $share->getToken(),
341
+            $share->getPassword()
342
+        );
343
+
344
+        try {
345
+            $link = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare',
346
+                ['token' => $share->getToken()]);
347
+            $this->sendMailNotification(
348
+                $share->getNode()->getName(),
349
+                $link,
350
+                $share->getSharedBy(),
351
+                $share->getSharedWith(),
352
+                $share->getExpirationDate()
353
+            );
354
+        } catch (HintException $hintException) {
355
+            $this->logger->logException($hintException, [
356
+                'message' => 'Failed to send share by mail.',
357
+                'level' => ILogger::ERROR,
358
+                'app' => 'sharebymail',
359
+            ]);
360
+            $this->removeShareFromTable($shareId);
361
+            throw $hintException;
362
+        } catch (\Exception $e) {
363
+            $this->logger->logException($e, [
364
+                'message' => 'Failed to send share by mail.',
365
+                'level' => ILogger::ERROR,
366
+                'app' => 'sharebymail',
367
+            ]);
368
+            $this->removeShareFromTable($shareId);
369
+            throw new HintException('Failed to send share by mail',
370
+                $this->l->t('Failed to send share by email'));
371
+        }
372
+
373
+        return $shareId;
374
+
375
+    }
376
+
377
+    /**
378
+     * @param string $filename
379
+     * @param string $link
380
+     * @param string $initiator
381
+     * @param string $shareWith
382
+     * @param \DateTime|null $expiration
383
+     * @throws \Exception If mail couldn't be sent
384
+     */
385
+    protected function sendMailNotification($filename,
386
+                                            $link,
387
+                                            $initiator,
388
+                                            $shareWith,
389
+                                            \DateTime $expiration = null) {
390
+        $initiatorUser = $this->userManager->get($initiator);
391
+        $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
392
+        $message = $this->mailer->createMessage();
393
+
394
+        $emailTemplate = $this->mailer->createEMailTemplate('sharebymail.RecipientNotification', [
395
+            'filename' => $filename,
396
+            'link' => $link,
397
+            'initiator' => $initiatorDisplayName,
398
+            'expiration' => $expiration,
399
+            'shareWith' => $shareWith,
400
+        ]);
401
+
402
+        $emailTemplate->setSubject($this->l->t('%s shared »%s« with you', array($initiatorDisplayName, $filename)));
403
+        $emailTemplate->addHeader();
404
+        $emailTemplate->addHeading($this->l->t('%s shared »%s« with you', [$initiatorDisplayName, $filename]), false);
405
+        $text = $this->l->t('%s shared »%s« with you.', [$initiatorDisplayName, $filename]);
406
+
407
+        $emailTemplate->addBodyText(
408
+            htmlspecialchars($text . ' ' . $this->l->t('Click the button below to open it.')),
409
+            $text
410
+        );
411
+        $emailTemplate->addBodyButton(
412
+            $this->l->t('Open »%s«', [$filename]),
413
+            $link
414
+        );
415
+
416
+        $message->setTo([$shareWith]);
417
+
418
+        // The "From" contains the sharers name
419
+        $instanceName = $this->defaults->getName();
420
+        $senderName = $this->l->t(
421
+            '%s via %s',
422
+            [
423
+                $initiatorDisplayName,
424
+                $instanceName
425
+            ]
426
+        );
427
+        $message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
428
+
429
+        // The "Reply-To" is set to the sharer if an mail address is configured
430
+        // also the default footer contains a "Do not reply" which needs to be adjusted.
431
+        $initiatorEmail = $initiatorUser->getEMailAddress();
432
+        if($initiatorEmail !== null) {
433
+            $message->setReplyTo([$initiatorEmail => $initiatorDisplayName]);
434
+            $emailTemplate->addFooter($instanceName . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : ''));
435
+        } else {
436
+            $emailTemplate->addFooter();
437
+        }
438
+
439
+        $message->useTemplate($emailTemplate);
440
+        $this->mailer->send($message);
441
+    }
442
+
443
+    /**
444
+     * send password to recipient of a mail share
445
+     *
446
+     * @param IShare $share
447
+     * @param string $password
448
+     * @return bool
449
+     */
450
+    protected function sendPassword(IShare $share, $password) {
451
+
452
+        $filename = $share->getNode()->getName();
453
+        $initiator = $share->getSharedBy();
454
+        $shareWith = $share->getSharedWith();
455
+
456
+        if ($password === '' || $this->settingsManager->sendPasswordByMail() === false) {
457
+            return false;
458
+        }
459
+
460
+        $initiatorUser = $this->userManager->get($initiator);
461
+        $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
462
+        $initiatorEmailAddress = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null;
463
+
464
+        $plainBodyPart = $this->l->t("%s shared »%s« with you.\nYou should have already received a separate mail with a link to access it.\n", [$initiatorDisplayName, $filename]);
465
+        $htmlBodyPart = $this->l->t('%s shared »%s« with you. You should have already received a separate mail with a link to access it.', [$initiatorDisplayName, $filename]);
466
+
467
+        $message = $this->mailer->createMessage();
468
+
469
+        $emailTemplate = $this->mailer->createEMailTemplate('sharebymail.RecipientPasswordNotification', [
470
+            'filename' => $filename,
471
+            'password' => $password,
472
+            'initiator' => $initiatorDisplayName,
473
+            'initiatorEmail' => $initiatorEmailAddress,
474
+            'shareWith' => $shareWith,
475
+        ]);
476
+
477
+        $emailTemplate->setSubject($this->l->t('Password to access »%s« shared to you by %s', [$filename, $initiatorDisplayName]));
478
+        $emailTemplate->addHeader();
479
+        $emailTemplate->addHeading($this->l->t('Password to access »%s«', [$filename]), false);
480
+        $emailTemplate->addBodyText(htmlspecialchars($htmlBodyPart), $plainBodyPart);
481
+        $emailTemplate->addBodyText($this->l->t('It is protected with the following password: %s', [$password]));
482
+
483
+        // The "From" contains the sharers name
484
+        $instanceName = $this->defaults->getName();
485
+        $senderName = $this->l->t(
486
+            '%s via %s',
487
+            [
488
+                $initiatorDisplayName,
489
+                $instanceName
490
+            ]
491
+        );
492
+        $message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
493
+        if ($initiatorEmailAddress !== null) {
494
+            $message->setReplyTo([$initiatorEmailAddress => $initiatorDisplayName]);
495
+            $emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
496
+        } else {
497
+            $emailTemplate->addFooter();
498
+        }
499
+
500
+        $message->setTo([$shareWith]);
501
+        $message->useTemplate($emailTemplate);
502
+        $this->mailer->send($message);
503
+
504
+        $this->createPasswordSendActivity($share, $shareWith, false);
505
+
506
+        return true;
507
+    }
508
+
509
+    /**
510
+     * send auto generated password to the owner. This happens if the admin enforces
511
+     * a password for mail shares and forbid to send the password by mail to the recipient
512
+     *
513
+     * @param IShare $share
514
+     * @param string $password
515
+     * @return bool
516
+     * @throws \Exception
517
+     */
518
+    protected function sendPasswordToOwner(IShare $share, $password) {
519
+
520
+        $filename = $share->getNode()->getName();
521
+        $initiator = $this->userManager->get($share->getSharedBy());
522
+        $initiatorEMailAddress = ($initiator instanceof IUser) ? $initiator->getEMailAddress() : null;
523
+        $initiatorDisplayName = ($initiator instanceof IUser) ? $initiator->getDisplayName() : $share->getSharedBy();
524
+        $shareWith = $share->getSharedWith();
525
+
526
+        if ($initiatorEMailAddress === null) {
527
+            throw new \Exception(
528
+                $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.")
529
+            );
530
+        }
531
+
532
+        $bodyPart = $this->l->t("You just shared »%s« with %s. The share was already send to the recipient. Due to the security policies defined by the administrator of %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()]);
533
+
534
+        $message = $this->mailer->createMessage();
535
+        $emailTemplate = $this->mailer->createEMailTemplate('sharebymail.OwnerPasswordNotification', [
536
+            'filename' => $filename,
537
+            'password' => $password,
538
+            'initiator' => $initiatorDisplayName,
539
+            'initiatorEmail' => $initiatorEMailAddress,
540
+            'shareWith' => $shareWith,
541
+        ]);
542
+
543
+        $emailTemplate->setSubject($this->l->t('Password to access »%s« shared with %s', [$filename, $shareWith]));
544
+        $emailTemplate->addHeader();
545
+        $emailTemplate->addHeading($this->l->t('Password to access »%s«', [$filename]), false);
546
+        $emailTemplate->addBodyText($bodyPart);
547
+        $emailTemplate->addBodyText($this->l->t('This is the password: %s', [$password]));
548
+        $emailTemplate->addBodyText($this->l->t('You can choose a different password at any time in the share dialog.'));
549
+        $emailTemplate->addFooter();
550
+
551
+        if ($initiatorEMailAddress) {
552
+            $message->setFrom([$initiatorEMailAddress => $initiatorDisplayName]);
553
+        }
554
+        $message->setTo([$initiatorEMailAddress => $initiatorDisplayName]);
555
+        $message->useTemplate($emailTemplate);
556
+        $this->mailer->send($message);
557
+
558
+        $this->createPasswordSendActivity($share, $shareWith, true);
559
+
560
+        return true;
561
+    }
562
+
563
+    /**
564
+     * generate share token
565
+     *
566
+     * @return string
567
+     */
568
+    protected function generateToken($size = 15) {
569
+        $token = $this->secureRandom->generate($size, ISecureRandom::CHAR_HUMAN_READABLE);
570
+        return $token;
571
+    }
572
+
573
+    /**
574
+     * Get all children of this share
575
+     *
576
+     * @param IShare $parent
577
+     * @return IShare[]
578
+     */
579
+    public function getChildren(IShare $parent) {
580
+        $children = [];
581
+
582
+        $qb = $this->dbConnection->getQueryBuilder();
583
+        $qb->select('*')
584
+            ->from('share')
585
+            ->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId())))
586
+            ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)))
587
+            ->orderBy('id');
588
+
589
+        $cursor = $qb->execute();
590
+        while($data = $cursor->fetch()) {
591
+            $children[] = $this->createShareObject($data);
592
+        }
593
+        $cursor->closeCursor();
594
+
595
+        return $children;
596
+    }
597
+
598
+    /**
599
+     * add share to the database and return the ID
600
+     *
601
+     * @param int $itemSource
602
+     * @param string $itemType
603
+     * @param string $shareWith
604
+     * @param string $sharedBy
605
+     * @param string $uidOwner
606
+     * @param int $permissions
607
+     * @param string $token
608
+     * @return int
609
+     */
610
+    protected function addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, $token, $password) {
611
+        $qb = $this->dbConnection->getQueryBuilder();
612
+        $qb->insert('share')
613
+            ->setValue('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL))
614
+            ->setValue('item_type', $qb->createNamedParameter($itemType))
615
+            ->setValue('item_source', $qb->createNamedParameter($itemSource))
616
+            ->setValue('file_source', $qb->createNamedParameter($itemSource))
617
+            ->setValue('share_with', $qb->createNamedParameter($shareWith))
618
+            ->setValue('uid_owner', $qb->createNamedParameter($uidOwner))
619
+            ->setValue('uid_initiator', $qb->createNamedParameter($sharedBy))
620
+            ->setValue('permissions', $qb->createNamedParameter($permissions))
621
+            ->setValue('token', $qb->createNamedParameter($token))
622
+            ->setValue('password', $qb->createNamedParameter($password))
623
+            ->setValue('stime', $qb->createNamedParameter(time()));
624
+
625
+        /*
626 626
 		 * Added to fix https://github.com/owncloud/core/issues/22215
627 627
 		 * Can be removed once we get rid of ajax/share.php
628 628
 		 */
629
-		$qb->setValue('file_target', $qb->createNamedParameter(''));
629
+        $qb->setValue('file_target', $qb->createNamedParameter(''));
630 630
 
631
-		$qb->execute();
632
-		$id = $qb->getLastInsertId();
631
+        $qb->execute();
632
+        $id = $qb->getLastInsertId();
633 633
 
634
-		return (int)$id;
635
-	}
634
+        return (int)$id;
635
+    }
636 636
 
637
-	/**
638
-	 * Update a share
639
-	 *
640
-	 * @param IShare $share
641
-	 * @param string|null $plainTextPassword
642
-	 * @return IShare The share object
643
-	 */
644
-	public function update(IShare $share, $plainTextPassword = null) {
637
+    /**
638
+     * Update a share
639
+     *
640
+     * @param IShare $share
641
+     * @param string|null $plainTextPassword
642
+     * @return IShare The share object
643
+     */
644
+    public function update(IShare $share, $plainTextPassword = null) {
645 645
 
646
-		$originalShare = $this->getShareById($share->getId());
646
+        $originalShare = $this->getShareById($share->getId());
647 647
 
648
-		// a real password was given
649
-		$validPassword = $plainTextPassword !== null && $plainTextPassword !== '';
648
+        // a real password was given
649
+        $validPassword = $plainTextPassword !== null && $plainTextPassword !== '';
650 650
 
651
-		if($validPassword && $originalShare->getPassword() !== $share->getPassword()) {
652
-			$this->sendPassword($share, $plainTextPassword);
653
-		}
654
-		/*
651
+        if($validPassword && $originalShare->getPassword() !== $share->getPassword()) {
652
+            $this->sendPassword($share, $plainTextPassword);
653
+        }
654
+        /*
655 655
 		 * We allow updating the permissions and password of mail shares
656 656
 		 */
657
-		$qb = $this->dbConnection->getQueryBuilder();
658
-		$qb->update('share')
659
-			->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
660
-			->set('permissions', $qb->createNamedParameter($share->getPermissions()))
661
-			->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
662
-			->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
663
-			->set('password', $qb->createNamedParameter($share->getPassword()))
664
-			->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
665
-			->execute();
666
-
667
-		return $share;
668
-	}
669
-
670
-	/**
671
-	 * @inheritdoc
672
-	 */
673
-	public function move(IShare $share, $recipient) {
674
-		/**
675
-		 * nothing to do here, mail shares are only outgoing shares
676
-		 */
677
-		return $share;
678
-	}
679
-
680
-	/**
681
-	 * Delete a share (owner unShares the file)
682
-	 *
683
-	 * @param IShare $share
684
-	 */
685
-	public function delete(IShare $share) {
686
-		$this->removeShareFromTable($share->getId());
687
-	}
688
-
689
-	/**
690
-	 * @inheritdoc
691
-	 */
692
-	public function deleteFromSelf(IShare $share, $recipient) {
693
-		// nothing to do here, mail shares are only outgoing shares
694
-	}
695
-
696
-	public function restore(IShare $share, string $recipient): IShare {
697
-		throw new GenericShareException('not implemented');
698
-	}
699
-
700
-	/**
701
-	 * @inheritdoc
702
-	 */
703
-	public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) {
704
-		$qb = $this->dbConnection->getQueryBuilder();
705
-		$qb->select('*')
706
-			->from('share');
707
-
708
-		$qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)));
709
-
710
-		/**
711
-		 * Reshares for this user are shares where they are the owner.
712
-		 */
713
-		if ($reshares === false) {
714
-			//Special case for old shares created via the web UI
715
-			$or1 = $qb->expr()->andX(
716
-				$qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
717
-				$qb->expr()->isNull('uid_initiator')
718
-			);
719
-
720
-			$qb->andWhere(
721
-				$qb->expr()->orX(
722
-					$qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)),
723
-					$or1
724
-				)
725
-			);
726
-		} else {
727
-			$qb->andWhere(
728
-				$qb->expr()->orX(
729
-					$qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
730
-					$qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
731
-				)
732
-			);
733
-		}
734
-
735
-		if ($node !== null) {
736
-			$qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
737
-		}
738
-
739
-		if ($limit !== -1) {
740
-			$qb->setMaxResults($limit);
741
-		}
742
-
743
-		$qb->setFirstResult($offset);
744
-		$qb->orderBy('id');
745
-
746
-		$cursor = $qb->execute();
747
-		$shares = [];
748
-		while($data = $cursor->fetch()) {
749
-			$shares[] = $this->createShareObject($data);
750
-		}
751
-		$cursor->closeCursor();
752
-
753
-		return $shares;
754
-	}
755
-
756
-	/**
757
-	 * @inheritdoc
758
-	 */
759
-	public function getShareById($id, $recipientId = null) {
760
-		$qb = $this->dbConnection->getQueryBuilder();
761
-
762
-		$qb->select('*')
763
-			->from('share')
764
-			->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
765
-			->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)));
766
-
767
-		$cursor = $qb->execute();
768
-		$data = $cursor->fetch();
769
-		$cursor->closeCursor();
770
-
771
-		if ($data === false) {
772
-			throw new ShareNotFound();
773
-		}
774
-
775
-		try {
776
-			$share = $this->createShareObject($data);
777
-		} catch (InvalidShare $e) {
778
-			throw new ShareNotFound();
779
-		}
780
-
781
-		return $share;
782
-	}
783
-
784
-	/**
785
-	 * Get shares for a given path
786
-	 *
787
-	 * @param \OCP\Files\Node $path
788
-	 * @return IShare[]
789
-	 */
790
-	public function getSharesByPath(Node $path) {
791
-		$qb = $this->dbConnection->getQueryBuilder();
792
-
793
-		$cursor = $qb->select('*')
794
-			->from('share')
795
-			->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId())))
796
-			->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)))
797
-			->execute();
798
-
799
-		$shares = [];
800
-		while($data = $cursor->fetch()) {
801
-			$shares[] = $this->createShareObject($data);
802
-		}
803
-		$cursor->closeCursor();
804
-
805
-		return $shares;
806
-	}
807
-
808
-	/**
809
-	 * @inheritdoc
810
-	 */
811
-	public function getSharedWith($userId, $shareType, $node, $limit, $offset) {
812
-		/** @var IShare[] $shares */
813
-		$shares = [];
814
-
815
-		//Get shares directly with this user
816
-		$qb = $this->dbConnection->getQueryBuilder();
817
-		$qb->select('*')
818
-			->from('share');
819
-
820
-		// Order by id
821
-		$qb->orderBy('id');
822
-
823
-		// Set limit and offset
824
-		if ($limit !== -1) {
825
-			$qb->setMaxResults($limit);
826
-		}
827
-		$qb->setFirstResult($offset);
828
-
829
-		$qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)));
830
-		$qb->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)));
831
-
832
-		// Filter by node if provided
833
-		if ($node !== null) {
834
-			$qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
835
-		}
836
-
837
-		$cursor = $qb->execute();
838
-
839
-		while($data = $cursor->fetch()) {
840
-			$shares[] = $this->createShareObject($data);
841
-		}
842
-		$cursor->closeCursor();
843
-
844
-
845
-		return $shares;
846
-	}
847
-
848
-	/**
849
-	 * Get a share by token
850
-	 *
851
-	 * @param string $token
852
-	 * @return IShare
853
-	 * @throws ShareNotFound
854
-	 */
855
-	public function getShareByToken($token) {
856
-		$qb = $this->dbConnection->getQueryBuilder();
857
-
858
-		$cursor = $qb->select('*')
859
-			->from('share')
860
-			->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)))
861
-			->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token)))
862
-			->execute();
863
-
864
-		$data = $cursor->fetch();
865
-
866
-		if ($data === false) {
867
-			throw new ShareNotFound('Share not found', $this->l->t('Could not find share'));
868
-		}
869
-
870
-		try {
871
-			$share = $this->createShareObject($data);
872
-		} catch (InvalidShare $e) {
873
-			throw new ShareNotFound('Share not found', $this->l->t('Could not find share'));
874
-		}
875
-
876
-		return $share;
877
-	}
878
-
879
-	/**
880
-	 * remove share from table
881
-	 *
882
-	 * @param string $shareId
883
-	 */
884
-	protected function removeShareFromTable($shareId) {
885
-		$qb = $this->dbConnection->getQueryBuilder();
886
-		$qb->delete('share')
887
-			->where($qb->expr()->eq('id', $qb->createNamedParameter($shareId)));
888
-		$qb->execute();
889
-	}
890
-
891
-	/**
892
-	 * Create a share object from an database row
893
-	 *
894
-	 * @param array $data
895
-	 * @return IShare
896
-	 * @throws InvalidShare
897
-	 * @throws ShareNotFound
898
-	 */
899
-	protected function createShareObject($data) {
900
-
901
-		$share = new Share($this->rootFolder, $this->userManager);
902
-		$share->setId((int)$data['id'])
903
-			->setShareType((int)$data['share_type'])
904
-			->setPermissions((int)$data['permissions'])
905
-			->setTarget($data['file_target'])
906
-			->setMailSend((bool)$data['mail_send'])
907
-			->setToken($data['token']);
908
-
909
-		$shareTime = new \DateTime();
910
-		$shareTime->setTimestamp((int)$data['stime']);
911
-		$share->setShareTime($shareTime);
912
-		$share->setSharedWith($data['share_with']);
913
-		$share->setPassword($data['password']);
914
-
915
-		if ($data['uid_initiator'] !== null) {
916
-			$share->setShareOwner($data['uid_owner']);
917
-			$share->setSharedBy($data['uid_initiator']);
918
-		} else {
919
-			//OLD SHARE
920
-			$share->setSharedBy($data['uid_owner']);
921
-			$path = $this->getNode($share->getSharedBy(), (int)$data['file_source']);
922
-
923
-			$owner = $path->getOwner();
924
-			$share->setShareOwner($owner->getUID());
925
-		}
926
-
927
-		if ($data['expiration'] !== null) {
928
-			$expiration = \DateTime::createFromFormat('Y-m-d H:i:s', $data['expiration']);
929
-			if ($expiration !== false) {
930
-				$share->setExpirationDate($expiration);
931
-			}
932
-		}
933
-
934
-		$share->setNodeId((int)$data['file_source']);
935
-		$share->setNodeType($data['item_type']);
936
-
937
-		$share->setProviderId($this->identifier());
938
-
939
-		return $share;
940
-	}
941
-
942
-	/**
943
-	 * Get the node with file $id for $user
944
-	 *
945
-	 * @param string $userId
946
-	 * @param int $id
947
-	 * @return \OCP\Files\File|\OCP\Files\Folder
948
-	 * @throws InvalidShare
949
-	 */
950
-	private function getNode($userId, $id) {
951
-		try {
952
-			$userFolder = $this->rootFolder->getUserFolder($userId);
953
-		} catch (NoUserException $e) {
954
-			throw new InvalidShare();
955
-		}
956
-
957
-		$nodes = $userFolder->getById($id);
958
-
959
-		if (empty($nodes)) {
960
-			throw new InvalidShare();
961
-		}
962
-
963
-		return $nodes[0];
964
-	}
965
-
966
-	/**
967
-	 * A user is deleted from the system
968
-	 * So clean up the relevant shares.
969
-	 *
970
-	 * @param string $uid
971
-	 * @param int $shareType
972
-	 */
973
-	public function userDeleted($uid, $shareType) {
974
-		$qb = $this->dbConnection->getQueryBuilder();
975
-
976
-		$qb->delete('share')
977
-			->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)))
978
-			->andWhere($qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)))
979
-			->execute();
980
-	}
981
-
982
-	/**
983
-	 * This provider does not support group shares
984
-	 *
985
-	 * @param string $gid
986
-	 */
987
-	public function groupDeleted($gid) {
988
-	}
989
-
990
-	/**
991
-	 * This provider does not support group shares
992
-	 *
993
-	 * @param string $uid
994
-	 * @param string $gid
995
-	 */
996
-	public function userDeletedFromGroup($uid, $gid) {
997
-	}
998
-
999
-	/**
1000
-	 * get database row of a give share
1001
-	 *
1002
-	 * @param $id
1003
-	 * @return array
1004
-	 * @throws ShareNotFound
1005
-	 */
1006
-	protected function getRawShare($id) {
1007
-
1008
-		// Now fetch the inserted share and create a complete share object
1009
-		$qb = $this->dbConnection->getQueryBuilder();
1010
-		$qb->select('*')
1011
-			->from('share')
1012
-			->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
1013
-
1014
-		$cursor = $qb->execute();
1015
-		$data = $cursor->fetch();
1016
-		$cursor->closeCursor();
1017
-
1018
-		if ($data === false) {
1019
-			throw new ShareNotFound;
1020
-		}
1021
-
1022
-		return $data;
1023
-	}
1024
-
1025
-	public function getSharesInFolder($userId, Folder $node, $reshares) {
1026
-		$qb = $this->dbConnection->getQueryBuilder();
1027
-		$qb->select('*')
1028
-			->from('share', 's')
1029
-			->andWhere($qb->expr()->orX(
1030
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
1031
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
1032
-			))
1033
-			->andWhere(
1034
-				$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL))
1035
-			);
1036
-
1037
-		/**
1038
-		 * Reshares for this user are shares where they are the owner.
1039
-		 */
1040
-		if ($reshares === false) {
1041
-			$qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
1042
-		} else {
1043
-			$qb->andWhere(
1044
-				$qb->expr()->orX(
1045
-					$qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
1046
-					$qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
1047
-				)
1048
-			);
1049
-		}
1050
-
1051
-		$qb->innerJoin('s', 'filecache' ,'f', $qb->expr()->eq('s.file_source', 'f.fileid'));
1052
-		$qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())));
1053
-
1054
-		$qb->orderBy('id');
1055
-
1056
-		$cursor = $qb->execute();
1057
-		$shares = [];
1058
-		while ($data = $cursor->fetch()) {
1059
-			$shares[$data['fileid']][] = $this->createShareObject($data);
1060
-		}
1061
-		$cursor->closeCursor();
1062
-
1063
-		return $shares;
1064
-	}
1065
-
1066
-	/**
1067
-	 * @inheritdoc
1068
-	 */
1069
-	public function getAccessList($nodes, $currentAccess) {
1070
-		$ids = [];
1071
-		foreach ($nodes as $node) {
1072
-			$ids[] = $node->getId();
1073
-		}
1074
-
1075
-		$qb = $this->dbConnection->getQueryBuilder();
1076
-		$qb->select('share_with')
1077
-			->from('share')
1078
-			->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)))
1079
-			->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
1080
-			->andWhere($qb->expr()->orX(
1081
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
1082
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
1083
-			))
1084
-			->setMaxResults(1);
1085
-		$cursor = $qb->execute();
1086
-
1087
-		$mail = $cursor->fetch() !== false;
1088
-		$cursor->closeCursor();
1089
-
1090
-		return ['public' => $mail];
1091
-	}
657
+        $qb = $this->dbConnection->getQueryBuilder();
658
+        $qb->update('share')
659
+            ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
660
+            ->set('permissions', $qb->createNamedParameter($share->getPermissions()))
661
+            ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
662
+            ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
663
+            ->set('password', $qb->createNamedParameter($share->getPassword()))
664
+            ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
665
+            ->execute();
666
+
667
+        return $share;
668
+    }
669
+
670
+    /**
671
+     * @inheritdoc
672
+     */
673
+    public function move(IShare $share, $recipient) {
674
+        /**
675
+         * nothing to do here, mail shares are only outgoing shares
676
+         */
677
+        return $share;
678
+    }
679
+
680
+    /**
681
+     * Delete a share (owner unShares the file)
682
+     *
683
+     * @param IShare $share
684
+     */
685
+    public function delete(IShare $share) {
686
+        $this->removeShareFromTable($share->getId());
687
+    }
688
+
689
+    /**
690
+     * @inheritdoc
691
+     */
692
+    public function deleteFromSelf(IShare $share, $recipient) {
693
+        // nothing to do here, mail shares are only outgoing shares
694
+    }
695
+
696
+    public function restore(IShare $share, string $recipient): IShare {
697
+        throw new GenericShareException('not implemented');
698
+    }
699
+
700
+    /**
701
+     * @inheritdoc
702
+     */
703
+    public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) {
704
+        $qb = $this->dbConnection->getQueryBuilder();
705
+        $qb->select('*')
706
+            ->from('share');
707
+
708
+        $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)));
709
+
710
+        /**
711
+         * Reshares for this user are shares where they are the owner.
712
+         */
713
+        if ($reshares === false) {
714
+            //Special case for old shares created via the web UI
715
+            $or1 = $qb->expr()->andX(
716
+                $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
717
+                $qb->expr()->isNull('uid_initiator')
718
+            );
719
+
720
+            $qb->andWhere(
721
+                $qb->expr()->orX(
722
+                    $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)),
723
+                    $or1
724
+                )
725
+            );
726
+        } else {
727
+            $qb->andWhere(
728
+                $qb->expr()->orX(
729
+                    $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
730
+                    $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
731
+                )
732
+            );
733
+        }
734
+
735
+        if ($node !== null) {
736
+            $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
737
+        }
738
+
739
+        if ($limit !== -1) {
740
+            $qb->setMaxResults($limit);
741
+        }
742
+
743
+        $qb->setFirstResult($offset);
744
+        $qb->orderBy('id');
745
+
746
+        $cursor = $qb->execute();
747
+        $shares = [];
748
+        while($data = $cursor->fetch()) {
749
+            $shares[] = $this->createShareObject($data);
750
+        }
751
+        $cursor->closeCursor();
752
+
753
+        return $shares;
754
+    }
755
+
756
+    /**
757
+     * @inheritdoc
758
+     */
759
+    public function getShareById($id, $recipientId = null) {
760
+        $qb = $this->dbConnection->getQueryBuilder();
761
+
762
+        $qb->select('*')
763
+            ->from('share')
764
+            ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
765
+            ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)));
766
+
767
+        $cursor = $qb->execute();
768
+        $data = $cursor->fetch();
769
+        $cursor->closeCursor();
770
+
771
+        if ($data === false) {
772
+            throw new ShareNotFound();
773
+        }
774
+
775
+        try {
776
+            $share = $this->createShareObject($data);
777
+        } catch (InvalidShare $e) {
778
+            throw new ShareNotFound();
779
+        }
780
+
781
+        return $share;
782
+    }
783
+
784
+    /**
785
+     * Get shares for a given path
786
+     *
787
+     * @param \OCP\Files\Node $path
788
+     * @return IShare[]
789
+     */
790
+    public function getSharesByPath(Node $path) {
791
+        $qb = $this->dbConnection->getQueryBuilder();
792
+
793
+        $cursor = $qb->select('*')
794
+            ->from('share')
795
+            ->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId())))
796
+            ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)))
797
+            ->execute();
798
+
799
+        $shares = [];
800
+        while($data = $cursor->fetch()) {
801
+            $shares[] = $this->createShareObject($data);
802
+        }
803
+        $cursor->closeCursor();
804
+
805
+        return $shares;
806
+    }
807
+
808
+    /**
809
+     * @inheritdoc
810
+     */
811
+    public function getSharedWith($userId, $shareType, $node, $limit, $offset) {
812
+        /** @var IShare[] $shares */
813
+        $shares = [];
814
+
815
+        //Get shares directly with this user
816
+        $qb = $this->dbConnection->getQueryBuilder();
817
+        $qb->select('*')
818
+            ->from('share');
819
+
820
+        // Order by id
821
+        $qb->orderBy('id');
822
+
823
+        // Set limit and offset
824
+        if ($limit !== -1) {
825
+            $qb->setMaxResults($limit);
826
+        }
827
+        $qb->setFirstResult($offset);
828
+
829
+        $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)));
830
+        $qb->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)));
831
+
832
+        // Filter by node if provided
833
+        if ($node !== null) {
834
+            $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
835
+        }
836
+
837
+        $cursor = $qb->execute();
838
+
839
+        while($data = $cursor->fetch()) {
840
+            $shares[] = $this->createShareObject($data);
841
+        }
842
+        $cursor->closeCursor();
843
+
844
+
845
+        return $shares;
846
+    }
847
+
848
+    /**
849
+     * Get a share by token
850
+     *
851
+     * @param string $token
852
+     * @return IShare
853
+     * @throws ShareNotFound
854
+     */
855
+    public function getShareByToken($token) {
856
+        $qb = $this->dbConnection->getQueryBuilder();
857
+
858
+        $cursor = $qb->select('*')
859
+            ->from('share')
860
+            ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)))
861
+            ->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token)))
862
+            ->execute();
863
+
864
+        $data = $cursor->fetch();
865
+
866
+        if ($data === false) {
867
+            throw new ShareNotFound('Share not found', $this->l->t('Could not find share'));
868
+        }
869
+
870
+        try {
871
+            $share = $this->createShareObject($data);
872
+        } catch (InvalidShare $e) {
873
+            throw new ShareNotFound('Share not found', $this->l->t('Could not find share'));
874
+        }
875
+
876
+        return $share;
877
+    }
878
+
879
+    /**
880
+     * remove share from table
881
+     *
882
+     * @param string $shareId
883
+     */
884
+    protected function removeShareFromTable($shareId) {
885
+        $qb = $this->dbConnection->getQueryBuilder();
886
+        $qb->delete('share')
887
+            ->where($qb->expr()->eq('id', $qb->createNamedParameter($shareId)));
888
+        $qb->execute();
889
+    }
890
+
891
+    /**
892
+     * Create a share object from an database row
893
+     *
894
+     * @param array $data
895
+     * @return IShare
896
+     * @throws InvalidShare
897
+     * @throws ShareNotFound
898
+     */
899
+    protected function createShareObject($data) {
900
+
901
+        $share = new Share($this->rootFolder, $this->userManager);
902
+        $share->setId((int)$data['id'])
903
+            ->setShareType((int)$data['share_type'])
904
+            ->setPermissions((int)$data['permissions'])
905
+            ->setTarget($data['file_target'])
906
+            ->setMailSend((bool)$data['mail_send'])
907
+            ->setToken($data['token']);
908
+
909
+        $shareTime = new \DateTime();
910
+        $shareTime->setTimestamp((int)$data['stime']);
911
+        $share->setShareTime($shareTime);
912
+        $share->setSharedWith($data['share_with']);
913
+        $share->setPassword($data['password']);
914
+
915
+        if ($data['uid_initiator'] !== null) {
916
+            $share->setShareOwner($data['uid_owner']);
917
+            $share->setSharedBy($data['uid_initiator']);
918
+        } else {
919
+            //OLD SHARE
920
+            $share->setSharedBy($data['uid_owner']);
921
+            $path = $this->getNode($share->getSharedBy(), (int)$data['file_source']);
922
+
923
+            $owner = $path->getOwner();
924
+            $share->setShareOwner($owner->getUID());
925
+        }
926
+
927
+        if ($data['expiration'] !== null) {
928
+            $expiration = \DateTime::createFromFormat('Y-m-d H:i:s', $data['expiration']);
929
+            if ($expiration !== false) {
930
+                $share->setExpirationDate($expiration);
931
+            }
932
+        }
933
+
934
+        $share->setNodeId((int)$data['file_source']);
935
+        $share->setNodeType($data['item_type']);
936
+
937
+        $share->setProviderId($this->identifier());
938
+
939
+        return $share;
940
+    }
941
+
942
+    /**
943
+     * Get the node with file $id for $user
944
+     *
945
+     * @param string $userId
946
+     * @param int $id
947
+     * @return \OCP\Files\File|\OCP\Files\Folder
948
+     * @throws InvalidShare
949
+     */
950
+    private function getNode($userId, $id) {
951
+        try {
952
+            $userFolder = $this->rootFolder->getUserFolder($userId);
953
+        } catch (NoUserException $e) {
954
+            throw new InvalidShare();
955
+        }
956
+
957
+        $nodes = $userFolder->getById($id);
958
+
959
+        if (empty($nodes)) {
960
+            throw new InvalidShare();
961
+        }
962
+
963
+        return $nodes[0];
964
+    }
965
+
966
+    /**
967
+     * A user is deleted from the system
968
+     * So clean up the relevant shares.
969
+     *
970
+     * @param string $uid
971
+     * @param int $shareType
972
+     */
973
+    public function userDeleted($uid, $shareType) {
974
+        $qb = $this->dbConnection->getQueryBuilder();
975
+
976
+        $qb->delete('share')
977
+            ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)))
978
+            ->andWhere($qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)))
979
+            ->execute();
980
+    }
981
+
982
+    /**
983
+     * This provider does not support group shares
984
+     *
985
+     * @param string $gid
986
+     */
987
+    public function groupDeleted($gid) {
988
+    }
989
+
990
+    /**
991
+     * This provider does not support group shares
992
+     *
993
+     * @param string $uid
994
+     * @param string $gid
995
+     */
996
+    public function userDeletedFromGroup($uid, $gid) {
997
+    }
998
+
999
+    /**
1000
+     * get database row of a give share
1001
+     *
1002
+     * @param $id
1003
+     * @return array
1004
+     * @throws ShareNotFound
1005
+     */
1006
+    protected function getRawShare($id) {
1007
+
1008
+        // Now fetch the inserted share and create a complete share object
1009
+        $qb = $this->dbConnection->getQueryBuilder();
1010
+        $qb->select('*')
1011
+            ->from('share')
1012
+            ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
1013
+
1014
+        $cursor = $qb->execute();
1015
+        $data = $cursor->fetch();
1016
+        $cursor->closeCursor();
1017
+
1018
+        if ($data === false) {
1019
+            throw new ShareNotFound;
1020
+        }
1021
+
1022
+        return $data;
1023
+    }
1024
+
1025
+    public function getSharesInFolder($userId, Folder $node, $reshares) {
1026
+        $qb = $this->dbConnection->getQueryBuilder();
1027
+        $qb->select('*')
1028
+            ->from('share', 's')
1029
+            ->andWhere($qb->expr()->orX(
1030
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
1031
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
1032
+            ))
1033
+            ->andWhere(
1034
+                $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL))
1035
+            );
1036
+
1037
+        /**
1038
+         * Reshares for this user are shares where they are the owner.
1039
+         */
1040
+        if ($reshares === false) {
1041
+            $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
1042
+        } else {
1043
+            $qb->andWhere(
1044
+                $qb->expr()->orX(
1045
+                    $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
1046
+                    $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
1047
+                )
1048
+            );
1049
+        }
1050
+
1051
+        $qb->innerJoin('s', 'filecache' ,'f', $qb->expr()->eq('s.file_source', 'f.fileid'));
1052
+        $qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())));
1053
+
1054
+        $qb->orderBy('id');
1055
+
1056
+        $cursor = $qb->execute();
1057
+        $shares = [];
1058
+        while ($data = $cursor->fetch()) {
1059
+            $shares[$data['fileid']][] = $this->createShareObject($data);
1060
+        }
1061
+        $cursor->closeCursor();
1062
+
1063
+        return $shares;
1064
+    }
1065
+
1066
+    /**
1067
+     * @inheritdoc
1068
+     */
1069
+    public function getAccessList($nodes, $currentAccess) {
1070
+        $ids = [];
1071
+        foreach ($nodes as $node) {
1072
+            $ids[] = $node->getId();
1073
+        }
1074
+
1075
+        $qb = $this->dbConnection->getQueryBuilder();
1076
+        $qb->select('share_with')
1077
+            ->from('share')
1078
+            ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)))
1079
+            ->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
1080
+            ->andWhere($qb->expr()->orX(
1081
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
1082
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
1083
+            ))
1084
+            ->setMaxResults(1);
1085
+        $cursor = $qb->execute();
1086
+
1087
+        $mail = $cursor->fetch() !== false;
1088
+        $cursor->closeCursor();
1089
+
1090
+        return ['public' => $mail];
1091
+    }
1092 1092
 
1093 1093
 }
Please login to merge, or discard this patch.
lib/private/Share20/Manager.php 1 patch
Indentation   +1496 added lines, -1496 removed lines patch added patch discarded remove patch
@@ -72,1524 +72,1524 @@
 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 EventDispatcher */
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 EventDispatcher $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
-			EventDispatcher $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_EMAIL) {
230
-			if ($share->getSharedWith() === null) {
231
-				throw new \InvalidArgumentException('SharedWith should not be empty');
232
-			}
233
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_CIRCLE) {
234
-			$circle = \OCA\Circles\Api\v1\Circles::detailsCircle($share->getSharedWith());
235
-			if ($circle === null) {
236
-				throw new \InvalidArgumentException('SharedWith is not a valid circle');
237
-			}
238
-		} else {
239
-			// We can't handle other types yet
240
-			throw new \InvalidArgumentException('unknown share type');
241
-		}
242
-
243
-		// Verify the initiator of the share is set
244
-		if ($share->getSharedBy() === null) {
245
-			throw new \InvalidArgumentException('SharedBy should be set');
246
-		}
247
-
248
-		// Cannot share with yourself
249
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
250
-			$share->getSharedWith() === $share->getSharedBy()) {
251
-			throw new \InvalidArgumentException('Can’t share with yourself');
252
-		}
253
-
254
-		// The path should be set
255
-		if ($share->getNode() === null) {
256
-			throw new \InvalidArgumentException('Path should be set');
257
-		}
258
-
259
-		// And it should be a file or a folder
260
-		if (!($share->getNode() instanceof \OCP\Files\File) &&
261
-				!($share->getNode() instanceof \OCP\Files\Folder)) {
262
-			throw new \InvalidArgumentException('Path should be either a file or a folder');
263
-		}
264
-
265
-		// And you can't share your rootfolder
266
-		if ($this->userManager->userExists($share->getSharedBy())) {
267
-			$sharedPath = $this->rootFolder->getUserFolder($share->getSharedBy())->getPath();
268
-		} else {
269
-			$sharedPath = $this->rootFolder->getUserFolder($share->getShareOwner())->getPath();
270
-		}
271
-		if ($sharedPath === $share->getNode()->getPath()) {
272
-			throw new \InvalidArgumentException('You can’t share your root folder');
273
-		}
274
-
275
-		// Check if we actually have share permissions
276
-		if (!$share->getNode()->isShareable()) {
277
-			$message_t = $this->l->t('You are not allowed to share %s', [$share->getNode()->getPath()]);
278
-			throw new GenericShareException($message_t, $message_t, 404);
279
-		}
280
-
281
-		// Permissions should be set
282
-		if ($share->getPermissions() === null) {
283
-			throw new \InvalidArgumentException('A share requires permissions');
284
-		}
285
-
286
-		/*
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 EventDispatcher */
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 EventDispatcher $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
+            EventDispatcher $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_EMAIL) {
230
+            if ($share->getSharedWith() === null) {
231
+                throw new \InvalidArgumentException('SharedWith should not be empty');
232
+            }
233
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_CIRCLE) {
234
+            $circle = \OCA\Circles\Api\v1\Circles::detailsCircle($share->getSharedWith());
235
+            if ($circle === null) {
236
+                throw new \InvalidArgumentException('SharedWith is not a valid circle');
237
+            }
238
+        } else {
239
+            // We can't handle other types yet
240
+            throw new \InvalidArgumentException('unknown share type');
241
+        }
242
+
243
+        // Verify the initiator of the share is set
244
+        if ($share->getSharedBy() === null) {
245
+            throw new \InvalidArgumentException('SharedBy should be set');
246
+        }
247
+
248
+        // Cannot share with yourself
249
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
250
+            $share->getSharedWith() === $share->getSharedBy()) {
251
+            throw new \InvalidArgumentException('Can’t share with yourself');
252
+        }
253
+
254
+        // The path should be set
255
+        if ($share->getNode() === null) {
256
+            throw new \InvalidArgumentException('Path should be set');
257
+        }
258
+
259
+        // And it should be a file or a folder
260
+        if (!($share->getNode() instanceof \OCP\Files\File) &&
261
+                !($share->getNode() instanceof \OCP\Files\Folder)) {
262
+            throw new \InvalidArgumentException('Path should be either a file or a folder');
263
+        }
264
+
265
+        // And you can't share your rootfolder
266
+        if ($this->userManager->userExists($share->getSharedBy())) {
267
+            $sharedPath = $this->rootFolder->getUserFolder($share->getSharedBy())->getPath();
268
+        } else {
269
+            $sharedPath = $this->rootFolder->getUserFolder($share->getShareOwner())->getPath();
270
+        }
271
+        if ($sharedPath === $share->getNode()->getPath()) {
272
+            throw new \InvalidArgumentException('You can’t share your root folder');
273
+        }
274
+
275
+        // Check if we actually have share permissions
276
+        if (!$share->getNode()->isShareable()) {
277
+            $message_t = $this->l->t('You are not allowed to share %s', [$share->getNode()->getPath()]);
278
+            throw new GenericShareException($message_t, $message_t, 404);
279
+        }
280
+
281
+        // Permissions should be set
282
+        if ($share->getPermissions() === null) {
283
+            throw new \InvalidArgumentException('A share requires permissions');
284
+        }
285
+
286
+        /*
287 287
 		 * Quick fix for #23536
288 288
 		 * Non moveable mount points do not have update and delete permissions
289 289
 		 * while we 'most likely' do have that on the storage.
290 290
 		 */
291
-		$permissions = $share->getNode()->getPermissions();
292
-		$mount = $share->getNode()->getMountPoint();
293
-		if (!($mount instanceof MoveableMount)) {
294
-			$permissions |= \OCP\Constants::PERMISSION_DELETE | \OCP\Constants::PERMISSION_UPDATE;
295
-		}
296
-
297
-		// Check that we do not share with more permissions than we have
298
-		if ($share->getPermissions() & ~$permissions) {
299
-			$message_t = $this->l->t('Can’t increase permissions of %s', [$share->getNode()->getPath()]);
300
-			throw new GenericShareException($message_t, $message_t, 404);
301
-		}
302
-
303
-
304
-		// Check that read permissions are always set
305
-		// Link shares are allowed to have no read permissions to allow upload to hidden folders
306
-		$noReadPermissionRequired = $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK
307
-			|| $share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL;
308
-		if (!$noReadPermissionRequired &&
309
-			($share->getPermissions() & \OCP\Constants::PERMISSION_READ) === 0) {
310
-			throw new \InvalidArgumentException('Shares need at least read permissions');
311
-		}
312
-
313
-		if ($share->getNode() instanceof \OCP\Files\File) {
314
-			if ($share->getPermissions() & \OCP\Constants::PERMISSION_DELETE) {
315
-				$message_t = $this->l->t('Files can’t be shared with delete permissions');
316
-				throw new GenericShareException($message_t);
317
-			}
318
-			if ($share->getPermissions() & \OCP\Constants::PERMISSION_CREATE) {
319
-				$message_t = $this->l->t('Files can’t be shared with create permissions');
320
-				throw new GenericShareException($message_t);
321
-			}
322
-		}
323
-	}
324
-
325
-	/**
326
-	 * Validate if the expiration date fits the system settings
327
-	 *
328
-	 * @param \OCP\Share\IShare $share The share to validate the expiration date of
329
-	 * @return \OCP\Share\IShare The modified share object
330
-	 * @throws GenericShareException
331
-	 * @throws \InvalidArgumentException
332
-	 * @throws \Exception
333
-	 */
334
-	protected function validateExpirationDate(\OCP\Share\IShare $share) {
335
-
336
-		$expirationDate = $share->getExpirationDate();
337
-
338
-		if ($expirationDate !== null) {
339
-			//Make sure the expiration date is a date
340
-			$expirationDate->setTime(0, 0, 0);
341
-
342
-			$date = new \DateTime();
343
-			$date->setTime(0, 0, 0);
344
-			if ($date >= $expirationDate) {
345
-				$message = $this->l->t('Expiration date is in the past');
346
-				throw new GenericShareException($message, $message, 404);
347
-			}
348
-		}
349
-
350
-		// If expiredate is empty set a default one if there is a default
351
-		$fullId = null;
352
-		try {
353
-			$fullId = $share->getFullId();
354
-		} catch (\UnexpectedValueException $e) {
355
-			// This is a new share
356
-		}
357
-
358
-		if ($fullId === null && $expirationDate === null && $this->shareApiLinkDefaultExpireDate()) {
359
-			$expirationDate = new \DateTime();
360
-			$expirationDate->setTime(0,0,0);
361
-			$expirationDate->add(new \DateInterval('P'.$this->shareApiLinkDefaultExpireDays().'D'));
362
-		}
363
-
364
-		// If we enforce the expiration date check that is does not exceed
365
-		if ($this->shareApiLinkDefaultExpireDateEnforced()) {
366
-			if ($expirationDate === null) {
367
-				throw new \InvalidArgumentException('Expiration date is enforced');
368
-			}
369
-
370
-			$date = new \DateTime();
371
-			$date->setTime(0, 0, 0);
372
-			$date->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D'));
373
-			if ($date < $expirationDate) {
374
-				$message = $this->l->t('Can’t set expiration date more than %s days in the future', [$this->shareApiLinkDefaultExpireDays()]);
375
-				throw new GenericShareException($message, $message, 404);
376
-			}
377
-		}
378
-
379
-		$accepted = true;
380
-		$message = '';
381
-		\OCP\Util::emitHook('\OC\Share', 'verifyExpirationDate', [
382
-			'expirationDate' => &$expirationDate,
383
-			'accepted' => &$accepted,
384
-			'message' => &$message,
385
-			'passwordSet' => $share->getPassword() !== null,
386
-		]);
387
-
388
-		if (!$accepted) {
389
-			throw new \Exception($message);
390
-		}
391
-
392
-		$share->setExpirationDate($expirationDate);
393
-
394
-		return $share;
395
-	}
396
-
397
-	/**
398
-	 * Check for pre share requirements for user shares
399
-	 *
400
-	 * @param \OCP\Share\IShare $share
401
-	 * @throws \Exception
402
-	 */
403
-	protected function userCreateChecks(\OCP\Share\IShare $share) {
404
-		// Check if we can share with group members only
405
-		if ($this->shareWithGroupMembersOnly()) {
406
-			$sharedBy = $this->userManager->get($share->getSharedBy());
407
-			$sharedWith = $this->userManager->get($share->getSharedWith());
408
-			// Verify we can share with this user
409
-			$groups = array_intersect(
410
-					$this->groupManager->getUserGroupIds($sharedBy),
411
-					$this->groupManager->getUserGroupIds($sharedWith)
412
-			);
413
-			if (empty($groups)) {
414
-				throw new \Exception('Sharing is only allowed with group members');
415
-			}
416
-		}
417
-
418
-		/*
291
+        $permissions = $share->getNode()->getPermissions();
292
+        $mount = $share->getNode()->getMountPoint();
293
+        if (!($mount instanceof MoveableMount)) {
294
+            $permissions |= \OCP\Constants::PERMISSION_DELETE | \OCP\Constants::PERMISSION_UPDATE;
295
+        }
296
+
297
+        // Check that we do not share with more permissions than we have
298
+        if ($share->getPermissions() & ~$permissions) {
299
+            $message_t = $this->l->t('Can’t increase permissions of %s', [$share->getNode()->getPath()]);
300
+            throw new GenericShareException($message_t, $message_t, 404);
301
+        }
302
+
303
+
304
+        // Check that read permissions are always set
305
+        // Link shares are allowed to have no read permissions to allow upload to hidden folders
306
+        $noReadPermissionRequired = $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK
307
+            || $share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL;
308
+        if (!$noReadPermissionRequired &&
309
+            ($share->getPermissions() & \OCP\Constants::PERMISSION_READ) === 0) {
310
+            throw new \InvalidArgumentException('Shares need at least read permissions');
311
+        }
312
+
313
+        if ($share->getNode() instanceof \OCP\Files\File) {
314
+            if ($share->getPermissions() & \OCP\Constants::PERMISSION_DELETE) {
315
+                $message_t = $this->l->t('Files can’t be shared with delete permissions');
316
+                throw new GenericShareException($message_t);
317
+            }
318
+            if ($share->getPermissions() & \OCP\Constants::PERMISSION_CREATE) {
319
+                $message_t = $this->l->t('Files can’t be shared with create permissions');
320
+                throw new GenericShareException($message_t);
321
+            }
322
+        }
323
+    }
324
+
325
+    /**
326
+     * Validate if the expiration date fits the system settings
327
+     *
328
+     * @param \OCP\Share\IShare $share The share to validate the expiration date of
329
+     * @return \OCP\Share\IShare The modified share object
330
+     * @throws GenericShareException
331
+     * @throws \InvalidArgumentException
332
+     * @throws \Exception
333
+     */
334
+    protected function validateExpirationDate(\OCP\Share\IShare $share) {
335
+
336
+        $expirationDate = $share->getExpirationDate();
337
+
338
+        if ($expirationDate !== null) {
339
+            //Make sure the expiration date is a date
340
+            $expirationDate->setTime(0, 0, 0);
341
+
342
+            $date = new \DateTime();
343
+            $date->setTime(0, 0, 0);
344
+            if ($date >= $expirationDate) {
345
+                $message = $this->l->t('Expiration date is in the past');
346
+                throw new GenericShareException($message, $message, 404);
347
+            }
348
+        }
349
+
350
+        // If expiredate is empty set a default one if there is a default
351
+        $fullId = null;
352
+        try {
353
+            $fullId = $share->getFullId();
354
+        } catch (\UnexpectedValueException $e) {
355
+            // This is a new share
356
+        }
357
+
358
+        if ($fullId === null && $expirationDate === null && $this->shareApiLinkDefaultExpireDate()) {
359
+            $expirationDate = new \DateTime();
360
+            $expirationDate->setTime(0,0,0);
361
+            $expirationDate->add(new \DateInterval('P'.$this->shareApiLinkDefaultExpireDays().'D'));
362
+        }
363
+
364
+        // If we enforce the expiration date check that is does not exceed
365
+        if ($this->shareApiLinkDefaultExpireDateEnforced()) {
366
+            if ($expirationDate === null) {
367
+                throw new \InvalidArgumentException('Expiration date is enforced');
368
+            }
369
+
370
+            $date = new \DateTime();
371
+            $date->setTime(0, 0, 0);
372
+            $date->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D'));
373
+            if ($date < $expirationDate) {
374
+                $message = $this->l->t('Can’t set expiration date more than %s days in the future', [$this->shareApiLinkDefaultExpireDays()]);
375
+                throw new GenericShareException($message, $message, 404);
376
+            }
377
+        }
378
+
379
+        $accepted = true;
380
+        $message = '';
381
+        \OCP\Util::emitHook('\OC\Share', 'verifyExpirationDate', [
382
+            'expirationDate' => &$expirationDate,
383
+            'accepted' => &$accepted,
384
+            'message' => &$message,
385
+            'passwordSet' => $share->getPassword() !== null,
386
+        ]);
387
+
388
+        if (!$accepted) {
389
+            throw new \Exception($message);
390
+        }
391
+
392
+        $share->setExpirationDate($expirationDate);
393
+
394
+        return $share;
395
+    }
396
+
397
+    /**
398
+     * Check for pre share requirements for user shares
399
+     *
400
+     * @param \OCP\Share\IShare $share
401
+     * @throws \Exception
402
+     */
403
+    protected function userCreateChecks(\OCP\Share\IShare $share) {
404
+        // Check if we can share with group members only
405
+        if ($this->shareWithGroupMembersOnly()) {
406
+            $sharedBy = $this->userManager->get($share->getSharedBy());
407
+            $sharedWith = $this->userManager->get($share->getSharedWith());
408
+            // Verify we can share with this user
409
+            $groups = array_intersect(
410
+                    $this->groupManager->getUserGroupIds($sharedBy),
411
+                    $this->groupManager->getUserGroupIds($sharedWith)
412
+            );
413
+            if (empty($groups)) {
414
+                throw new \Exception('Sharing is only allowed with group members');
415
+            }
416
+        }
417
+
418
+        /*
419 419
 		 * TODO: Could be costly, fix
420 420
 		 *
421 421
 		 * Also this is not what we want in the future.. then we want to squash identical shares.
422 422
 		 */
423
-		$provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_USER);
424
-		$existingShares = $provider->getSharesByPath($share->getNode());
425
-		foreach($existingShares as $existingShare) {
426
-			// Ignore if it is the same share
427
-			try {
428
-				if ($existingShare->getFullId() === $share->getFullId()) {
429
-					continue;
430
-				}
431
-			} catch (\UnexpectedValueException $e) {
432
-				//Shares are not identical
433
-			}
434
-
435
-			// Identical share already existst
436
-			if ($existingShare->getSharedWith() === $share->getSharedWith()) {
437
-				throw new \Exception('Path is already shared with this user');
438
-			}
439
-
440
-			// The share is already shared with this user via a group share
441
-			if ($existingShare->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
442
-				$group = $this->groupManager->get($existingShare->getSharedWith());
443
-				if (!is_null($group)) {
444
-					$user = $this->userManager->get($share->getSharedWith());
445
-
446
-					if ($group->inGroup($user) && $existingShare->getShareOwner() !== $share->getShareOwner()) {
447
-						throw new \Exception('Path is already shared with this user');
448
-					}
449
-				}
450
-			}
451
-		}
452
-	}
453
-
454
-	/**
455
-	 * Check for pre share requirements for group shares
456
-	 *
457
-	 * @param \OCP\Share\IShare $share
458
-	 * @throws \Exception
459
-	 */
460
-	protected function groupCreateChecks(\OCP\Share\IShare $share) {
461
-		// Verify group shares are allowed
462
-		if (!$this->allowGroupSharing()) {
463
-			throw new \Exception('Group sharing is now allowed');
464
-		}
465
-
466
-		// Verify if the user can share with this group
467
-		if ($this->shareWithGroupMembersOnly()) {
468
-			$sharedBy = $this->userManager->get($share->getSharedBy());
469
-			$sharedWith = $this->groupManager->get($share->getSharedWith());
470
-			if (is_null($sharedWith) || !$sharedWith->inGroup($sharedBy)) {
471
-				throw new \Exception('Sharing is only allowed within your own groups');
472
-			}
473
-		}
474
-
475
-		/*
423
+        $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_USER);
424
+        $existingShares = $provider->getSharesByPath($share->getNode());
425
+        foreach($existingShares as $existingShare) {
426
+            // Ignore if it is the same share
427
+            try {
428
+                if ($existingShare->getFullId() === $share->getFullId()) {
429
+                    continue;
430
+                }
431
+            } catch (\UnexpectedValueException $e) {
432
+                //Shares are not identical
433
+            }
434
+
435
+            // Identical share already existst
436
+            if ($existingShare->getSharedWith() === $share->getSharedWith()) {
437
+                throw new \Exception('Path is already shared with this user');
438
+            }
439
+
440
+            // The share is already shared with this user via a group share
441
+            if ($existingShare->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
442
+                $group = $this->groupManager->get($existingShare->getSharedWith());
443
+                if (!is_null($group)) {
444
+                    $user = $this->userManager->get($share->getSharedWith());
445
+
446
+                    if ($group->inGroup($user) && $existingShare->getShareOwner() !== $share->getShareOwner()) {
447
+                        throw new \Exception('Path is already shared with this user');
448
+                    }
449
+                }
450
+            }
451
+        }
452
+    }
453
+
454
+    /**
455
+     * Check for pre share requirements for group shares
456
+     *
457
+     * @param \OCP\Share\IShare $share
458
+     * @throws \Exception
459
+     */
460
+    protected function groupCreateChecks(\OCP\Share\IShare $share) {
461
+        // Verify group shares are allowed
462
+        if (!$this->allowGroupSharing()) {
463
+            throw new \Exception('Group sharing is now allowed');
464
+        }
465
+
466
+        // Verify if the user can share with this group
467
+        if ($this->shareWithGroupMembersOnly()) {
468
+            $sharedBy = $this->userManager->get($share->getSharedBy());
469
+            $sharedWith = $this->groupManager->get($share->getSharedWith());
470
+            if (is_null($sharedWith) || !$sharedWith->inGroup($sharedBy)) {
471
+                throw new \Exception('Sharing is only allowed within your own groups');
472
+            }
473
+        }
474
+
475
+        /*
476 476
 		 * TODO: Could be costly, fix
477 477
 		 *
478 478
 		 * Also this is not what we want in the future.. then we want to squash identical shares.
479 479
 		 */
480
-		$provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP);
481
-		$existingShares = $provider->getSharesByPath($share->getNode());
482
-		foreach($existingShares as $existingShare) {
483
-			try {
484
-				if ($existingShare->getFullId() === $share->getFullId()) {
485
-					continue;
486
-				}
487
-			} catch (\UnexpectedValueException $e) {
488
-				//It is a new share so just continue
489
-			}
490
-
491
-			if ($existingShare->getSharedWith() === $share->getSharedWith()) {
492
-				throw new \Exception('Path is already shared with this group');
493
-			}
494
-		}
495
-	}
496
-
497
-	/**
498
-	 * Check for pre share requirements for link shares
499
-	 *
500
-	 * @param \OCP\Share\IShare $share
501
-	 * @throws \Exception
502
-	 */
503
-	protected function linkCreateChecks(\OCP\Share\IShare $share) {
504
-		// Are link shares allowed?
505
-		if (!$this->shareApiAllowLinks()) {
506
-			throw new \Exception('Link sharing is not allowed');
507
-		}
508
-
509
-		// Link shares by definition can't have share permissions
510
-		if ($share->getPermissions() & \OCP\Constants::PERMISSION_SHARE) {
511
-			throw new \InvalidArgumentException('Link shares can’t have reshare permissions');
512
-		}
513
-
514
-		// Check if public upload is allowed
515
-		if (!$this->shareApiLinkAllowPublicUpload() &&
516
-			($share->getPermissions() & (\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE))) {
517
-			throw new \InvalidArgumentException('Public upload is not allowed');
518
-		}
519
-	}
520
-
521
-	/**
522
-	 * To make sure we don't get invisible link shares we set the parent
523
-	 * of a link if it is a reshare. This is a quick word around
524
-	 * until we can properly display multiple link shares in the UI
525
-	 *
526
-	 * See: https://github.com/owncloud/core/issues/22295
527
-	 *
528
-	 * FIXME: Remove once multiple link shares can be properly displayed
529
-	 *
530
-	 * @param \OCP\Share\IShare $share
531
-	 */
532
-	protected function setLinkParent(\OCP\Share\IShare $share) {
533
-
534
-		// No sense in checking if the method is not there.
535
-		if (method_exists($share, 'setParent')) {
536
-			$storage = $share->getNode()->getStorage();
537
-			if ($storage->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) {
538
-				/** @var \OCA\Files_Sharing\SharedStorage $storage */
539
-				$share->setParent($storage->getShareId());
540
-			}
541
-		}
542
-	}
543
-
544
-	/**
545
-	 * @param File|Folder $path
546
-	 */
547
-	protected function pathCreateChecks($path) {
548
-		// Make sure that we do not share a path that contains a shared mountpoint
549
-		if ($path instanceof \OCP\Files\Folder) {
550
-			$mounts = $this->mountManager->findIn($path->getPath());
551
-			foreach($mounts as $mount) {
552
-				if ($mount->getStorage()->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) {
553
-					throw new \InvalidArgumentException('Path contains files shared with you');
554
-				}
555
-			}
556
-		}
557
-	}
558
-
559
-	/**
560
-	 * Check if the user that is sharing can actually share
561
-	 *
562
-	 * @param \OCP\Share\IShare $share
563
-	 * @throws \Exception
564
-	 */
565
-	protected function canShare(\OCP\Share\IShare $share) {
566
-		if (!$this->shareApiEnabled()) {
567
-			throw new \Exception('Sharing is disabled');
568
-		}
569
-
570
-		if ($this->sharingDisabledForUser($share->getSharedBy())) {
571
-			throw new \Exception('Sharing is disabled for you');
572
-		}
573
-	}
574
-
575
-	/**
576
-	 * Share a path
577
-	 *
578
-	 * @param \OCP\Share\IShare $share
579
-	 * @return Share The share object
580
-	 * @throws \Exception
581
-	 *
582
-	 * TODO: handle link share permissions or check them
583
-	 */
584
-	public function createShare(\OCP\Share\IShare $share) {
585
-		$this->canShare($share);
586
-
587
-		$this->generalCreateChecks($share);
588
-
589
-		// Verify if there are any issues with the path
590
-		$this->pathCreateChecks($share->getNode());
591
-
592
-		/*
480
+        $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP);
481
+        $existingShares = $provider->getSharesByPath($share->getNode());
482
+        foreach($existingShares as $existingShare) {
483
+            try {
484
+                if ($existingShare->getFullId() === $share->getFullId()) {
485
+                    continue;
486
+                }
487
+            } catch (\UnexpectedValueException $e) {
488
+                //It is a new share so just continue
489
+            }
490
+
491
+            if ($existingShare->getSharedWith() === $share->getSharedWith()) {
492
+                throw new \Exception('Path is already shared with this group');
493
+            }
494
+        }
495
+    }
496
+
497
+    /**
498
+     * Check for pre share requirements for link shares
499
+     *
500
+     * @param \OCP\Share\IShare $share
501
+     * @throws \Exception
502
+     */
503
+    protected function linkCreateChecks(\OCP\Share\IShare $share) {
504
+        // Are link shares allowed?
505
+        if (!$this->shareApiAllowLinks()) {
506
+            throw new \Exception('Link sharing is not allowed');
507
+        }
508
+
509
+        // Link shares by definition can't have share permissions
510
+        if ($share->getPermissions() & \OCP\Constants::PERMISSION_SHARE) {
511
+            throw new \InvalidArgumentException('Link shares can’t have reshare permissions');
512
+        }
513
+
514
+        // Check if public upload is allowed
515
+        if (!$this->shareApiLinkAllowPublicUpload() &&
516
+            ($share->getPermissions() & (\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE))) {
517
+            throw new \InvalidArgumentException('Public upload is not allowed');
518
+        }
519
+    }
520
+
521
+    /**
522
+     * To make sure we don't get invisible link shares we set the parent
523
+     * of a link if it is a reshare. This is a quick word around
524
+     * until we can properly display multiple link shares in the UI
525
+     *
526
+     * See: https://github.com/owncloud/core/issues/22295
527
+     *
528
+     * FIXME: Remove once multiple link shares can be properly displayed
529
+     *
530
+     * @param \OCP\Share\IShare $share
531
+     */
532
+    protected function setLinkParent(\OCP\Share\IShare $share) {
533
+
534
+        // No sense in checking if the method is not there.
535
+        if (method_exists($share, 'setParent')) {
536
+            $storage = $share->getNode()->getStorage();
537
+            if ($storage->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) {
538
+                /** @var \OCA\Files_Sharing\SharedStorage $storage */
539
+                $share->setParent($storage->getShareId());
540
+            }
541
+        }
542
+    }
543
+
544
+    /**
545
+     * @param File|Folder $path
546
+     */
547
+    protected function pathCreateChecks($path) {
548
+        // Make sure that we do not share a path that contains a shared mountpoint
549
+        if ($path instanceof \OCP\Files\Folder) {
550
+            $mounts = $this->mountManager->findIn($path->getPath());
551
+            foreach($mounts as $mount) {
552
+                if ($mount->getStorage()->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) {
553
+                    throw new \InvalidArgumentException('Path contains files shared with you');
554
+                }
555
+            }
556
+        }
557
+    }
558
+
559
+    /**
560
+     * Check if the user that is sharing can actually share
561
+     *
562
+     * @param \OCP\Share\IShare $share
563
+     * @throws \Exception
564
+     */
565
+    protected function canShare(\OCP\Share\IShare $share) {
566
+        if (!$this->shareApiEnabled()) {
567
+            throw new \Exception('Sharing is disabled');
568
+        }
569
+
570
+        if ($this->sharingDisabledForUser($share->getSharedBy())) {
571
+            throw new \Exception('Sharing is disabled for you');
572
+        }
573
+    }
574
+
575
+    /**
576
+     * Share a path
577
+     *
578
+     * @param \OCP\Share\IShare $share
579
+     * @return Share The share object
580
+     * @throws \Exception
581
+     *
582
+     * TODO: handle link share permissions or check them
583
+     */
584
+    public function createShare(\OCP\Share\IShare $share) {
585
+        $this->canShare($share);
586
+
587
+        $this->generalCreateChecks($share);
588
+
589
+        // Verify if there are any issues with the path
590
+        $this->pathCreateChecks($share->getNode());
591
+
592
+        /*
593 593
 		 * On creation of a share the owner is always the owner of the path
594 594
 		 * Except for mounted federated shares.
595 595
 		 */
596
-		$storage = $share->getNode()->getStorage();
597
-		if ($storage->instanceOfStorage('OCA\Files_Sharing\External\Storage')) {
598
-			$parent = $share->getNode()->getParent();
599
-			while($parent->getStorage()->instanceOfStorage('OCA\Files_Sharing\External\Storage')) {
600
-				$parent = $parent->getParent();
601
-			}
602
-			$share->setShareOwner($parent->getOwner()->getUID());
603
-		} else {
604
-			$share->setShareOwner($share->getNode()->getOwner()->getUID());
605
-		}
606
-
607
-		//Verify share type
608
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
609
-			$this->userCreateChecks($share);
610
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
611
-			$this->groupCreateChecks($share);
612
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
613
-			$this->linkCreateChecks($share);
614
-			$this->setLinkParent($share);
615
-
616
-			/*
596
+        $storage = $share->getNode()->getStorage();
597
+        if ($storage->instanceOfStorage('OCA\Files_Sharing\External\Storage')) {
598
+            $parent = $share->getNode()->getParent();
599
+            while($parent->getStorage()->instanceOfStorage('OCA\Files_Sharing\External\Storage')) {
600
+                $parent = $parent->getParent();
601
+            }
602
+            $share->setShareOwner($parent->getOwner()->getUID());
603
+        } else {
604
+            $share->setShareOwner($share->getNode()->getOwner()->getUID());
605
+        }
606
+
607
+        //Verify share type
608
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
609
+            $this->userCreateChecks($share);
610
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
611
+            $this->groupCreateChecks($share);
612
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
613
+            $this->linkCreateChecks($share);
614
+            $this->setLinkParent($share);
615
+
616
+            /*
617 617
 			 * For now ignore a set token.
618 618
 			 */
619
-			$share->setToken(
620
-				$this->secureRandom->generate(
621
-					\OC\Share\Constants::TOKEN_LENGTH,
622
-					\OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE
623
-				)
624
-			);
625
-
626
-			//Verify the expiration date
627
-			$this->validateExpirationDate($share);
628
-
629
-			//Verify the password
630
-			$this->verifyPassword($share->getPassword());
631
-
632
-			// If a password is set. Hash it!
633
-			if ($share->getPassword() !== null) {
634
-				$share->setPassword($this->hasher->hash($share->getPassword()));
635
-			}
636
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
637
-			$share->setToken(
638
-				$this->secureRandom->generate(
639
-					\OC\Share\Constants::TOKEN_LENGTH,
640
-					\OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE
641
-				)
642
-			);
643
-		}
644
-
645
-		// Cannot share with the owner
646
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
647
-			$share->getSharedWith() === $share->getShareOwner()) {
648
-			throw new \InvalidArgumentException('Can’t share with the share owner');
649
-		}
650
-
651
-		// Generate the target
652
-		$target = $this->config->getSystemValue('share_folder', '/') .'/'. $share->getNode()->getName();
653
-		$target = \OC\Files\Filesystem::normalizePath($target);
654
-		$share->setTarget($target);
655
-
656
-		// Pre share event
657
-		$event = new GenericEvent($share);
658
-		$a = $this->eventDispatcher->dispatch('OCP\Share::preShare', $event);
659
-		if ($event->isPropagationStopped() && $event->hasArgument('error')) {
660
-			throw new \Exception($event->getArgument('error'));
661
-		}
662
-
663
-		$oldShare = $share;
664
-		$provider = $this->factory->getProviderForType($share->getShareType());
665
-		$share = $provider->create($share);
666
-		//reuse the node we already have
667
-		$share->setNode($oldShare->getNode());
668
-
669
-		// Post share event
670
-		$event = new GenericEvent($share);
671
-		$this->eventDispatcher->dispatch('OCP\Share::postShare', $event);
672
-
673
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
674
-			$mailSend = $share->getMailSend();
675
-			if($mailSend === true) {
676
-				$user = $this->userManager->get($share->getSharedWith());
677
-				if ($user !== null) {
678
-					$emailAddress = $user->getEMailAddress();
679
-					if ($emailAddress !== null && $emailAddress !== '') {
680
-						$userLang = $this->config->getUserValue($share->getSharedWith(), 'core', 'lang', null);
681
-						$l = $this->l10nFactory->get('lib', $userLang);
682
-						$this->sendMailNotification(
683
-							$l,
684
-							$share->getNode()->getName(),
685
-							$this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $share->getNode()->getId()]),
686
-							$share->getSharedBy(),
687
-							$emailAddress,
688
-							$share->getExpirationDate()
689
-						);
690
-						$this->logger->debug('Send share notification to ' . $emailAddress . ' for share with ID ' . $share->getId(), ['app' => 'share']);
691
-					} else {
692
-						$this->logger->debug('Share notification not send to ' . $share->getSharedWith() . ' because email address is not set.', ['app' => 'share']);
693
-					}
694
-				} else {
695
-					$this->logger->debug('Share notification not send to ' . $share->getSharedWith() . ' because user could not be found.', ['app' => 'share']);
696
-				}
697
-			} else {
698
-				$this->logger->debug('Share notification not send because mailsend is false.', ['app' => 'share']);
699
-			}
700
-		}
701
-
702
-		return $share;
703
-	}
704
-
705
-	/**
706
-	 * @param IL10N $l Language of the recipient
707
-	 * @param string $filename file/folder name
708
-	 * @param string $link link to the file/folder
709
-	 * @param string $initiator user ID of share sender
710
-	 * @param string $shareWith email address of share receiver
711
-	 * @param \DateTime|null $expiration
712
-	 * @throws \Exception If mail couldn't be sent
713
-	 */
714
-	protected function sendMailNotification(IL10N $l,
715
-											$filename,
716
-											$link,
717
-											$initiator,
718
-											$shareWith,
719
-											\DateTime $expiration = null) {
720
-		$initiatorUser = $this->userManager->get($initiator);
721
-		$initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
722
-
723
-		$message = $this->mailer->createMessage();
724
-
725
-		$emailTemplate = $this->mailer->createEMailTemplate('files_sharing.RecipientNotification', [
726
-			'filename' => $filename,
727
-			'link' => $link,
728
-			'initiator' => $initiatorDisplayName,
729
-			'expiration' => $expiration,
730
-			'shareWith' => $shareWith,
731
-		]);
732
-
733
-		$emailTemplate->setSubject($l->t('%s shared »%s« with you', array($initiatorDisplayName, $filename)));
734
-		$emailTemplate->addHeader();
735
-		$emailTemplate->addHeading($l->t('%s shared »%s« with you', [$initiatorDisplayName, $filename]), false);
736
-		$text = $l->t('%s shared »%s« with you.', [$initiatorDisplayName, $filename]);
737
-
738
-		$emailTemplate->addBodyText(
739
-			htmlspecialchars($text . ' ' . $l->t('Click the button below to open it.')),
740
-			$text
741
-		);
742
-		$emailTemplate->addBodyButton(
743
-			$l->t('Open »%s«', [$filename]),
744
-			$link
745
-		);
746
-
747
-		$message->setTo([$shareWith]);
748
-
749
-		// The "From" contains the sharers name
750
-		$instanceName = $this->defaults->getName();
751
-		$senderName = $l->t(
752
-			'%s via %s',
753
-			[
754
-				$initiatorDisplayName,
755
-				$instanceName
756
-			]
757
-		);
758
-		$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
759
-
760
-		// The "Reply-To" is set to the sharer if an mail address is configured
761
-		// also the default footer contains a "Do not reply" which needs to be adjusted.
762
-		$initiatorEmail = $initiatorUser->getEMailAddress();
763
-		if($initiatorEmail !== null) {
764
-			$message->setReplyTo([$initiatorEmail => $initiatorDisplayName]);
765
-			$emailTemplate->addFooter($instanceName . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : ''));
766
-		} else {
767
-			$emailTemplate->addFooter();
768
-		}
769
-
770
-		$message->useTemplate($emailTemplate);
771
-		$this->mailer->send($message);
772
-	}
773
-
774
-	/**
775
-	 * Update a share
776
-	 *
777
-	 * @param \OCP\Share\IShare $share
778
-	 * @return \OCP\Share\IShare The share object
779
-	 * @throws \InvalidArgumentException
780
-	 */
781
-	public function updateShare(\OCP\Share\IShare $share) {
782
-		$expirationDateUpdated = false;
783
-
784
-		$this->canShare($share);
785
-
786
-		try {
787
-			$originalShare = $this->getShareById($share->getFullId());
788
-		} catch (\UnexpectedValueException $e) {
789
-			throw new \InvalidArgumentException('Share does not have a full id');
790
-		}
791
-
792
-		// We can't change the share type!
793
-		if ($share->getShareType() !== $originalShare->getShareType()) {
794
-			throw new \InvalidArgumentException('Can’t change share type');
795
-		}
796
-
797
-		// We can only change the recipient on user shares
798
-		if ($share->getSharedWith() !== $originalShare->getSharedWith() &&
799
-		    $share->getShareType() !== \OCP\Share::SHARE_TYPE_USER) {
800
-			throw new \InvalidArgumentException('Can only update recipient on user shares');
801
-		}
802
-
803
-		// Cannot share with the owner
804
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
805
-			$share->getSharedWith() === $share->getShareOwner()) {
806
-			throw new \InvalidArgumentException('Can’t share with the share owner');
807
-		}
808
-
809
-		$this->generalCreateChecks($share);
810
-
811
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
812
-			$this->userCreateChecks($share);
813
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
814
-			$this->groupCreateChecks($share);
815
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
816
-			$this->linkCreateChecks($share);
817
-
818
-			$this->updateSharePasswordIfNeeded($share, $originalShare);
819
-
820
-			if ($share->getExpirationDate() != $originalShare->getExpirationDate()) {
821
-				//Verify the expiration date
822
-				$this->validateExpirationDate($share);
823
-				$expirationDateUpdated = true;
824
-			}
825
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
826
-			$plainTextPassword = $share->getPassword();
827
-			if (!$this->updateSharePasswordIfNeeded($share, $originalShare)) {
828
-				$plainTextPassword = null;
829
-			}
830
-		}
831
-
832
-		$this->pathCreateChecks($share->getNode());
833
-
834
-		// Now update the share!
835
-		$provider = $this->factory->getProviderForType($share->getShareType());
836
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
837
-			$share = $provider->update($share, $plainTextPassword);
838
-		} else {
839
-			$share = $provider->update($share);
840
-		}
841
-
842
-		if ($expirationDateUpdated === true) {
843
-			\OC_Hook::emit(Share::class, 'post_set_expiration_date', [
844
-				'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder',
845
-				'itemSource' => $share->getNode()->getId(),
846
-				'date' => $share->getExpirationDate(),
847
-				'uidOwner' => $share->getSharedBy(),
848
-			]);
849
-		}
850
-
851
-		if ($share->getPassword() !== $originalShare->getPassword()) {
852
-			\OC_Hook::emit(Share::class, 'post_update_password', [
853
-				'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder',
854
-				'itemSource' => $share->getNode()->getId(),
855
-				'uidOwner' => $share->getSharedBy(),
856
-				'token' => $share->getToken(),
857
-				'disabled' => is_null($share->getPassword()),
858
-			]);
859
-		}
860
-
861
-		if ($share->getPermissions() !== $originalShare->getPermissions()) {
862
-			if ($this->userManager->userExists($share->getShareOwner())) {
863
-				$userFolder = $this->rootFolder->getUserFolder($share->getShareOwner());
864
-			} else {
865
-				$userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
866
-			}
867
-			\OC_Hook::emit(Share::class, 'post_update_permissions', array(
868
-				'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder',
869
-				'itemSource' => $share->getNode()->getId(),
870
-				'shareType' => $share->getShareType(),
871
-				'shareWith' => $share->getSharedWith(),
872
-				'uidOwner' => $share->getSharedBy(),
873
-				'permissions' => $share->getPermissions(),
874
-				'path' => $userFolder->getRelativePath($share->getNode()->getPath()),
875
-			));
876
-		}
877
-
878
-		return $share;
879
-	}
880
-
881
-	/**
882
-	 * Updates the password of the given share if it is not the same as the
883
-	 * password of the original share.
884
-	 *
885
-	 * @param \OCP\Share\IShare $share the share to update its password.
886
-	 * @param \OCP\Share\IShare $originalShare the original share to compare its
887
-	 *        password with.
888
-	 * @return boolean whether the password was updated or not.
889
-	 */
890
-	private function updateSharePasswordIfNeeded(\OCP\Share\IShare $share, \OCP\Share\IShare $originalShare) {
891
-		// Password updated.
892
-		if ($share->getPassword() !== $originalShare->getPassword()) {
893
-			//Verify the password
894
-			$this->verifyPassword($share->getPassword());
895
-
896
-			// If a password is set. Hash it!
897
-			if ($share->getPassword() !== null) {
898
-				$share->setPassword($this->hasher->hash($share->getPassword()));
899
-
900
-				return true;
901
-			}
902
-		}
903
-
904
-		return false;
905
-	}
906
-
907
-	/**
908
-	 * Delete all the children of this share
909
-	 * FIXME: remove once https://github.com/owncloud/core/pull/21660 is in
910
-	 *
911
-	 * @param \OCP\Share\IShare $share
912
-	 * @return \OCP\Share\IShare[] List of deleted shares
913
-	 */
914
-	protected function deleteChildren(\OCP\Share\IShare $share) {
915
-		$deletedShares = [];
916
-
917
-		$provider = $this->factory->getProviderForType($share->getShareType());
918
-
919
-		foreach ($provider->getChildren($share) as $child) {
920
-			$deletedChildren = $this->deleteChildren($child);
921
-			$deletedShares = array_merge($deletedShares, $deletedChildren);
922
-
923
-			$provider->delete($child);
924
-			$deletedShares[] = $child;
925
-		}
926
-
927
-		return $deletedShares;
928
-	}
929
-
930
-	/**
931
-	 * Delete a share
932
-	 *
933
-	 * @param \OCP\Share\IShare $share
934
-	 * @throws ShareNotFound
935
-	 * @throws \InvalidArgumentException
936
-	 */
937
-	public function deleteShare(\OCP\Share\IShare $share) {
938
-
939
-		try {
940
-			$share->getFullId();
941
-		} catch (\UnexpectedValueException $e) {
942
-			throw new \InvalidArgumentException('Share does not have a full id');
943
-		}
944
-
945
-		$event = new GenericEvent($share);
946
-		$this->eventDispatcher->dispatch('OCP\Share::preUnshare', $event);
947
-
948
-		// Get all children and delete them as well
949
-		$deletedShares = $this->deleteChildren($share);
950
-
951
-		// Do the actual delete
952
-		$provider = $this->factory->getProviderForType($share->getShareType());
953
-		$provider->delete($share);
954
-
955
-		// All the deleted shares caused by this delete
956
-		$deletedShares[] = $share;
957
-
958
-		// Emit post hook
959
-		$event->setArgument('deletedShares', $deletedShares);
960
-		$this->eventDispatcher->dispatch('OCP\Share::postUnshare', $event);
961
-	}
962
-
963
-
964
-	/**
965
-	 * Unshare a file as the recipient.
966
-	 * This can be different from a regular delete for example when one of
967
-	 * the users in a groups deletes that share. But the provider should
968
-	 * handle this.
969
-	 *
970
-	 * @param \OCP\Share\IShare $share
971
-	 * @param string $recipientId
972
-	 */
973
-	public function deleteFromSelf(\OCP\Share\IShare $share, $recipientId) {
974
-		list($providerId, ) = $this->splitFullId($share->getFullId());
975
-		$provider = $this->factory->getProvider($providerId);
976
-
977
-		$provider->deleteFromSelf($share, $recipientId);
978
-		$event = new GenericEvent($share);
979
-		$this->eventDispatcher->dispatch('OCP\Share::postUnshareFromSelf', $event);
980
-	}
981
-
982
-	public function restoreShare(IShare $share, string $recipientId): IShare {
983
-		list($providerId, ) = $this->splitFullId($share->getFullId());
984
-		$provider = $this->factory->getProvider($providerId);
985
-
986
-		return $provider->restore($share, $recipientId);
987
-	}
988
-
989
-	/**
990
-	 * @inheritdoc
991
-	 */
992
-	public function moveShare(\OCP\Share\IShare $share, $recipientId) {
993
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
994
-			throw new \InvalidArgumentException('Can’t change target of link share');
995
-		}
996
-
997
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && $share->getSharedWith() !== $recipientId) {
998
-			throw new \InvalidArgumentException('Invalid recipient');
999
-		}
1000
-
1001
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
1002
-			$sharedWith = $this->groupManager->get($share->getSharedWith());
1003
-			if (is_null($sharedWith)) {
1004
-				throw new \InvalidArgumentException('Group "' . $share->getSharedWith() . '" does not exist');
1005
-			}
1006
-			$recipient = $this->userManager->get($recipientId);
1007
-			if (!$sharedWith->inGroup($recipient)) {
1008
-				throw new \InvalidArgumentException('Invalid recipient');
1009
-			}
1010
-		}
1011
-
1012
-		list($providerId, ) = $this->splitFullId($share->getFullId());
1013
-		$provider = $this->factory->getProvider($providerId);
1014
-
1015
-		$provider->move($share, $recipientId);
1016
-	}
1017
-
1018
-	public function getSharesInFolder($userId, Folder $node, $reshares = false) {
1019
-		$providers = $this->factory->getAllProviders();
1020
-
1021
-		return array_reduce($providers, function($shares, IShareProvider $provider) use ($userId, $node, $reshares) {
1022
-			$newShares = $provider->getSharesInFolder($userId, $node, $reshares);
1023
-			foreach ($newShares as $fid => $data) {
1024
-				if (!isset($shares[$fid])) {
1025
-					$shares[$fid] = [];
1026
-				}
1027
-
1028
-				$shares[$fid] = array_merge($shares[$fid], $data);
1029
-			}
1030
-			return $shares;
1031
-		}, []);
1032
-	}
1033
-
1034
-	/**
1035
-	 * @inheritdoc
1036
-	 */
1037
-	public function getSharesBy($userId, $shareType, $path = null, $reshares = false, $limit = 50, $offset = 0) {
1038
-		if ($path !== null &&
1039
-				!($path instanceof \OCP\Files\File) &&
1040
-				!($path instanceof \OCP\Files\Folder)) {
1041
-			throw new \InvalidArgumentException('invalid path');
1042
-		}
1043
-
1044
-		try {
1045
-			$provider = $this->factory->getProviderForType($shareType);
1046
-		} catch (ProviderException $e) {
1047
-			return [];
1048
-		}
1049
-
1050
-		$shares = $provider->getSharesBy($userId, $shareType, $path, $reshares, $limit, $offset);
1051
-
1052
-		/*
619
+            $share->setToken(
620
+                $this->secureRandom->generate(
621
+                    \OC\Share\Constants::TOKEN_LENGTH,
622
+                    \OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE
623
+                )
624
+            );
625
+
626
+            //Verify the expiration date
627
+            $this->validateExpirationDate($share);
628
+
629
+            //Verify the password
630
+            $this->verifyPassword($share->getPassword());
631
+
632
+            // If a password is set. Hash it!
633
+            if ($share->getPassword() !== null) {
634
+                $share->setPassword($this->hasher->hash($share->getPassword()));
635
+            }
636
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
637
+            $share->setToken(
638
+                $this->secureRandom->generate(
639
+                    \OC\Share\Constants::TOKEN_LENGTH,
640
+                    \OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE
641
+                )
642
+            );
643
+        }
644
+
645
+        // Cannot share with the owner
646
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
647
+            $share->getSharedWith() === $share->getShareOwner()) {
648
+            throw new \InvalidArgumentException('Can’t share with the share owner');
649
+        }
650
+
651
+        // Generate the target
652
+        $target = $this->config->getSystemValue('share_folder', '/') .'/'. $share->getNode()->getName();
653
+        $target = \OC\Files\Filesystem::normalizePath($target);
654
+        $share->setTarget($target);
655
+
656
+        // Pre share event
657
+        $event = new GenericEvent($share);
658
+        $a = $this->eventDispatcher->dispatch('OCP\Share::preShare', $event);
659
+        if ($event->isPropagationStopped() && $event->hasArgument('error')) {
660
+            throw new \Exception($event->getArgument('error'));
661
+        }
662
+
663
+        $oldShare = $share;
664
+        $provider = $this->factory->getProviderForType($share->getShareType());
665
+        $share = $provider->create($share);
666
+        //reuse the node we already have
667
+        $share->setNode($oldShare->getNode());
668
+
669
+        // Post share event
670
+        $event = new GenericEvent($share);
671
+        $this->eventDispatcher->dispatch('OCP\Share::postShare', $event);
672
+
673
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
674
+            $mailSend = $share->getMailSend();
675
+            if($mailSend === true) {
676
+                $user = $this->userManager->get($share->getSharedWith());
677
+                if ($user !== null) {
678
+                    $emailAddress = $user->getEMailAddress();
679
+                    if ($emailAddress !== null && $emailAddress !== '') {
680
+                        $userLang = $this->config->getUserValue($share->getSharedWith(), 'core', 'lang', null);
681
+                        $l = $this->l10nFactory->get('lib', $userLang);
682
+                        $this->sendMailNotification(
683
+                            $l,
684
+                            $share->getNode()->getName(),
685
+                            $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $share->getNode()->getId()]),
686
+                            $share->getSharedBy(),
687
+                            $emailAddress,
688
+                            $share->getExpirationDate()
689
+                        );
690
+                        $this->logger->debug('Send share notification to ' . $emailAddress . ' for share with ID ' . $share->getId(), ['app' => 'share']);
691
+                    } else {
692
+                        $this->logger->debug('Share notification not send to ' . $share->getSharedWith() . ' because email address is not set.', ['app' => 'share']);
693
+                    }
694
+                } else {
695
+                    $this->logger->debug('Share notification not send to ' . $share->getSharedWith() . ' because user could not be found.', ['app' => 'share']);
696
+                }
697
+            } else {
698
+                $this->logger->debug('Share notification not send because mailsend is false.', ['app' => 'share']);
699
+            }
700
+        }
701
+
702
+        return $share;
703
+    }
704
+
705
+    /**
706
+     * @param IL10N $l Language of the recipient
707
+     * @param string $filename file/folder name
708
+     * @param string $link link to the file/folder
709
+     * @param string $initiator user ID of share sender
710
+     * @param string $shareWith email address of share receiver
711
+     * @param \DateTime|null $expiration
712
+     * @throws \Exception If mail couldn't be sent
713
+     */
714
+    protected function sendMailNotification(IL10N $l,
715
+                                            $filename,
716
+                                            $link,
717
+                                            $initiator,
718
+                                            $shareWith,
719
+                                            \DateTime $expiration = null) {
720
+        $initiatorUser = $this->userManager->get($initiator);
721
+        $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
722
+
723
+        $message = $this->mailer->createMessage();
724
+
725
+        $emailTemplate = $this->mailer->createEMailTemplate('files_sharing.RecipientNotification', [
726
+            'filename' => $filename,
727
+            'link' => $link,
728
+            'initiator' => $initiatorDisplayName,
729
+            'expiration' => $expiration,
730
+            'shareWith' => $shareWith,
731
+        ]);
732
+
733
+        $emailTemplate->setSubject($l->t('%s shared »%s« with you', array($initiatorDisplayName, $filename)));
734
+        $emailTemplate->addHeader();
735
+        $emailTemplate->addHeading($l->t('%s shared »%s« with you', [$initiatorDisplayName, $filename]), false);
736
+        $text = $l->t('%s shared »%s« with you.', [$initiatorDisplayName, $filename]);
737
+
738
+        $emailTemplate->addBodyText(
739
+            htmlspecialchars($text . ' ' . $l->t('Click the button below to open it.')),
740
+            $text
741
+        );
742
+        $emailTemplate->addBodyButton(
743
+            $l->t('Open »%s«', [$filename]),
744
+            $link
745
+        );
746
+
747
+        $message->setTo([$shareWith]);
748
+
749
+        // The "From" contains the sharers name
750
+        $instanceName = $this->defaults->getName();
751
+        $senderName = $l->t(
752
+            '%s via %s',
753
+            [
754
+                $initiatorDisplayName,
755
+                $instanceName
756
+            ]
757
+        );
758
+        $message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
759
+
760
+        // The "Reply-To" is set to the sharer if an mail address is configured
761
+        // also the default footer contains a "Do not reply" which needs to be adjusted.
762
+        $initiatorEmail = $initiatorUser->getEMailAddress();
763
+        if($initiatorEmail !== null) {
764
+            $message->setReplyTo([$initiatorEmail => $initiatorDisplayName]);
765
+            $emailTemplate->addFooter($instanceName . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : ''));
766
+        } else {
767
+            $emailTemplate->addFooter();
768
+        }
769
+
770
+        $message->useTemplate($emailTemplate);
771
+        $this->mailer->send($message);
772
+    }
773
+
774
+    /**
775
+     * Update a share
776
+     *
777
+     * @param \OCP\Share\IShare $share
778
+     * @return \OCP\Share\IShare The share object
779
+     * @throws \InvalidArgumentException
780
+     */
781
+    public function updateShare(\OCP\Share\IShare $share) {
782
+        $expirationDateUpdated = false;
783
+
784
+        $this->canShare($share);
785
+
786
+        try {
787
+            $originalShare = $this->getShareById($share->getFullId());
788
+        } catch (\UnexpectedValueException $e) {
789
+            throw new \InvalidArgumentException('Share does not have a full id');
790
+        }
791
+
792
+        // We can't change the share type!
793
+        if ($share->getShareType() !== $originalShare->getShareType()) {
794
+            throw new \InvalidArgumentException('Can’t change share type');
795
+        }
796
+
797
+        // We can only change the recipient on user shares
798
+        if ($share->getSharedWith() !== $originalShare->getSharedWith() &&
799
+            $share->getShareType() !== \OCP\Share::SHARE_TYPE_USER) {
800
+            throw new \InvalidArgumentException('Can only update recipient on user shares');
801
+        }
802
+
803
+        // Cannot share with the owner
804
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
805
+            $share->getSharedWith() === $share->getShareOwner()) {
806
+            throw new \InvalidArgumentException('Can’t share with the share owner');
807
+        }
808
+
809
+        $this->generalCreateChecks($share);
810
+
811
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
812
+            $this->userCreateChecks($share);
813
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
814
+            $this->groupCreateChecks($share);
815
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
816
+            $this->linkCreateChecks($share);
817
+
818
+            $this->updateSharePasswordIfNeeded($share, $originalShare);
819
+
820
+            if ($share->getExpirationDate() != $originalShare->getExpirationDate()) {
821
+                //Verify the expiration date
822
+                $this->validateExpirationDate($share);
823
+                $expirationDateUpdated = true;
824
+            }
825
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
826
+            $plainTextPassword = $share->getPassword();
827
+            if (!$this->updateSharePasswordIfNeeded($share, $originalShare)) {
828
+                $plainTextPassword = null;
829
+            }
830
+        }
831
+
832
+        $this->pathCreateChecks($share->getNode());
833
+
834
+        // Now update the share!
835
+        $provider = $this->factory->getProviderForType($share->getShareType());
836
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
837
+            $share = $provider->update($share, $plainTextPassword);
838
+        } else {
839
+            $share = $provider->update($share);
840
+        }
841
+
842
+        if ($expirationDateUpdated === true) {
843
+            \OC_Hook::emit(Share::class, 'post_set_expiration_date', [
844
+                'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder',
845
+                'itemSource' => $share->getNode()->getId(),
846
+                'date' => $share->getExpirationDate(),
847
+                'uidOwner' => $share->getSharedBy(),
848
+            ]);
849
+        }
850
+
851
+        if ($share->getPassword() !== $originalShare->getPassword()) {
852
+            \OC_Hook::emit(Share::class, 'post_update_password', [
853
+                'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder',
854
+                'itemSource' => $share->getNode()->getId(),
855
+                'uidOwner' => $share->getSharedBy(),
856
+                'token' => $share->getToken(),
857
+                'disabled' => is_null($share->getPassword()),
858
+            ]);
859
+        }
860
+
861
+        if ($share->getPermissions() !== $originalShare->getPermissions()) {
862
+            if ($this->userManager->userExists($share->getShareOwner())) {
863
+                $userFolder = $this->rootFolder->getUserFolder($share->getShareOwner());
864
+            } else {
865
+                $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
866
+            }
867
+            \OC_Hook::emit(Share::class, 'post_update_permissions', array(
868
+                'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder',
869
+                'itemSource' => $share->getNode()->getId(),
870
+                'shareType' => $share->getShareType(),
871
+                'shareWith' => $share->getSharedWith(),
872
+                'uidOwner' => $share->getSharedBy(),
873
+                'permissions' => $share->getPermissions(),
874
+                'path' => $userFolder->getRelativePath($share->getNode()->getPath()),
875
+            ));
876
+        }
877
+
878
+        return $share;
879
+    }
880
+
881
+    /**
882
+     * Updates the password of the given share if it is not the same as the
883
+     * password of the original share.
884
+     *
885
+     * @param \OCP\Share\IShare $share the share to update its password.
886
+     * @param \OCP\Share\IShare $originalShare the original share to compare its
887
+     *        password with.
888
+     * @return boolean whether the password was updated or not.
889
+     */
890
+    private function updateSharePasswordIfNeeded(\OCP\Share\IShare $share, \OCP\Share\IShare $originalShare) {
891
+        // Password updated.
892
+        if ($share->getPassword() !== $originalShare->getPassword()) {
893
+            //Verify the password
894
+            $this->verifyPassword($share->getPassword());
895
+
896
+            // If a password is set. Hash it!
897
+            if ($share->getPassword() !== null) {
898
+                $share->setPassword($this->hasher->hash($share->getPassword()));
899
+
900
+                return true;
901
+            }
902
+        }
903
+
904
+        return false;
905
+    }
906
+
907
+    /**
908
+     * Delete all the children of this share
909
+     * FIXME: remove once https://github.com/owncloud/core/pull/21660 is in
910
+     *
911
+     * @param \OCP\Share\IShare $share
912
+     * @return \OCP\Share\IShare[] List of deleted shares
913
+     */
914
+    protected function deleteChildren(\OCP\Share\IShare $share) {
915
+        $deletedShares = [];
916
+
917
+        $provider = $this->factory->getProviderForType($share->getShareType());
918
+
919
+        foreach ($provider->getChildren($share) as $child) {
920
+            $deletedChildren = $this->deleteChildren($child);
921
+            $deletedShares = array_merge($deletedShares, $deletedChildren);
922
+
923
+            $provider->delete($child);
924
+            $deletedShares[] = $child;
925
+        }
926
+
927
+        return $deletedShares;
928
+    }
929
+
930
+    /**
931
+     * Delete a share
932
+     *
933
+     * @param \OCP\Share\IShare $share
934
+     * @throws ShareNotFound
935
+     * @throws \InvalidArgumentException
936
+     */
937
+    public function deleteShare(\OCP\Share\IShare $share) {
938
+
939
+        try {
940
+            $share->getFullId();
941
+        } catch (\UnexpectedValueException $e) {
942
+            throw new \InvalidArgumentException('Share does not have a full id');
943
+        }
944
+
945
+        $event = new GenericEvent($share);
946
+        $this->eventDispatcher->dispatch('OCP\Share::preUnshare', $event);
947
+
948
+        // Get all children and delete them as well
949
+        $deletedShares = $this->deleteChildren($share);
950
+
951
+        // Do the actual delete
952
+        $provider = $this->factory->getProviderForType($share->getShareType());
953
+        $provider->delete($share);
954
+
955
+        // All the deleted shares caused by this delete
956
+        $deletedShares[] = $share;
957
+
958
+        // Emit post hook
959
+        $event->setArgument('deletedShares', $deletedShares);
960
+        $this->eventDispatcher->dispatch('OCP\Share::postUnshare', $event);
961
+    }
962
+
963
+
964
+    /**
965
+     * Unshare a file as the recipient.
966
+     * This can be different from a regular delete for example when one of
967
+     * the users in a groups deletes that share. But the provider should
968
+     * handle this.
969
+     *
970
+     * @param \OCP\Share\IShare $share
971
+     * @param string $recipientId
972
+     */
973
+    public function deleteFromSelf(\OCP\Share\IShare $share, $recipientId) {
974
+        list($providerId, ) = $this->splitFullId($share->getFullId());
975
+        $provider = $this->factory->getProvider($providerId);
976
+
977
+        $provider->deleteFromSelf($share, $recipientId);
978
+        $event = new GenericEvent($share);
979
+        $this->eventDispatcher->dispatch('OCP\Share::postUnshareFromSelf', $event);
980
+    }
981
+
982
+    public function restoreShare(IShare $share, string $recipientId): IShare {
983
+        list($providerId, ) = $this->splitFullId($share->getFullId());
984
+        $provider = $this->factory->getProvider($providerId);
985
+
986
+        return $provider->restore($share, $recipientId);
987
+    }
988
+
989
+    /**
990
+     * @inheritdoc
991
+     */
992
+    public function moveShare(\OCP\Share\IShare $share, $recipientId) {
993
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
994
+            throw new \InvalidArgumentException('Can’t change target of link share');
995
+        }
996
+
997
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && $share->getSharedWith() !== $recipientId) {
998
+            throw new \InvalidArgumentException('Invalid recipient');
999
+        }
1000
+
1001
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
1002
+            $sharedWith = $this->groupManager->get($share->getSharedWith());
1003
+            if (is_null($sharedWith)) {
1004
+                throw new \InvalidArgumentException('Group "' . $share->getSharedWith() . '" does not exist');
1005
+            }
1006
+            $recipient = $this->userManager->get($recipientId);
1007
+            if (!$sharedWith->inGroup($recipient)) {
1008
+                throw new \InvalidArgumentException('Invalid recipient');
1009
+            }
1010
+        }
1011
+
1012
+        list($providerId, ) = $this->splitFullId($share->getFullId());
1013
+        $provider = $this->factory->getProvider($providerId);
1014
+
1015
+        $provider->move($share, $recipientId);
1016
+    }
1017
+
1018
+    public function getSharesInFolder($userId, Folder $node, $reshares = false) {
1019
+        $providers = $this->factory->getAllProviders();
1020
+
1021
+        return array_reduce($providers, function($shares, IShareProvider $provider) use ($userId, $node, $reshares) {
1022
+            $newShares = $provider->getSharesInFolder($userId, $node, $reshares);
1023
+            foreach ($newShares as $fid => $data) {
1024
+                if (!isset($shares[$fid])) {
1025
+                    $shares[$fid] = [];
1026
+                }
1027
+
1028
+                $shares[$fid] = array_merge($shares[$fid], $data);
1029
+            }
1030
+            return $shares;
1031
+        }, []);
1032
+    }
1033
+
1034
+    /**
1035
+     * @inheritdoc
1036
+     */
1037
+    public function getSharesBy($userId, $shareType, $path = null, $reshares = false, $limit = 50, $offset = 0) {
1038
+        if ($path !== null &&
1039
+                !($path instanceof \OCP\Files\File) &&
1040
+                !($path instanceof \OCP\Files\Folder)) {
1041
+            throw new \InvalidArgumentException('invalid path');
1042
+        }
1043
+
1044
+        try {
1045
+            $provider = $this->factory->getProviderForType($shareType);
1046
+        } catch (ProviderException $e) {
1047
+            return [];
1048
+        }
1049
+
1050
+        $shares = $provider->getSharesBy($userId, $shareType, $path, $reshares, $limit, $offset);
1051
+
1052
+        /*
1053 1053
 		 * Work around so we don't return expired shares but still follow
1054 1054
 		 * proper pagination.
1055 1055
 		 */
1056 1056
 
1057
-		$shares2 = [];
1058
-
1059
-		while(true) {
1060
-			$added = 0;
1061
-			foreach ($shares as $share) {
1062
-
1063
-				try {
1064
-					$this->checkExpireDate($share);
1065
-				} catch (ShareNotFound $e) {
1066
-					//Ignore since this basically means the share is deleted
1067
-					continue;
1068
-				}
1069
-
1070
-				$added++;
1071
-				$shares2[] = $share;
1072
-
1073
-				if (count($shares2) === $limit) {
1074
-					break;
1075
-				}
1076
-			}
1077
-
1078
-			// If we did not fetch more shares than the limit then there are no more shares
1079
-			if (count($shares) < $limit) {
1080
-				break;
1081
-			}
1082
-
1083
-			if (count($shares2) === $limit) {
1084
-				break;
1085
-			}
1086
-
1087
-			// If there was no limit on the select we are done
1088
-			if ($limit === -1) {
1089
-				break;
1090
-			}
1091
-
1092
-			$offset += $added;
1093
-
1094
-			// Fetch again $limit shares
1095
-			$shares = $provider->getSharesBy($userId, $shareType, $path, $reshares, $limit, $offset);
1096
-
1097
-			// No more shares means we are done
1098
-			if (empty($shares)) {
1099
-				break;
1100
-			}
1101
-		}
1102
-
1103
-		$shares = $shares2;
1104
-
1105
-		return $shares;
1106
-	}
1107
-
1108
-	/**
1109
-	 * @inheritdoc
1110
-	 */
1111
-	public function getSharedWith($userId, $shareType, $node = null, $limit = 50, $offset = 0) {
1112
-		try {
1113
-			$provider = $this->factory->getProviderForType($shareType);
1114
-		} catch (ProviderException $e) {
1115
-			return [];
1116
-		}
1117
-
1118
-		$shares = $provider->getSharedWith($userId, $shareType, $node, $limit, $offset);
1119
-
1120
-		// remove all shares which are already expired
1121
-		foreach ($shares as $key => $share) {
1122
-			try {
1123
-				$this->checkExpireDate($share);
1124
-			} catch (ShareNotFound $e) {
1125
-				unset($shares[$key]);
1126
-			}
1127
-		}
1128
-
1129
-		return $shares;
1130
-	}
1131
-
1132
-	/**
1133
-	 * @inheritdoc
1134
-	 */
1135
-	public function getDeletedSharedWith($userId, $shareType, $node = null, $limit = 50, $offset = 0) {
1136
-		$shares = $this->getSharedWith($userId, $shareType, $node, $limit, $offset);
1137
-
1138
-		// Only get deleted shares
1139
-		$shares = array_filter($shares, function(IShare $share) {
1140
-			return $share->getPermissions() === 0;
1141
-		});
1142
-
1143
-		// Only get shares where the owner still exists
1144
-		$shares = array_filter($shares, function (IShare $share) {
1145
-			return $this->userManager->userExists($share->getShareOwner());
1146
-		});
1147
-
1148
-		return $shares;
1149
-	}
1150
-
1151
-	/**
1152
-	 * @inheritdoc
1153
-	 */
1154
-	public function getShareById($id, $recipient = null) {
1155
-		if ($id === null) {
1156
-			throw new ShareNotFound();
1157
-		}
1158
-
1159
-		list($providerId, $id) = $this->splitFullId($id);
1160
-
1161
-		try {
1162
-			$provider = $this->factory->getProvider($providerId);
1163
-		} catch (ProviderException $e) {
1164
-			throw new ShareNotFound();
1165
-		}
1166
-
1167
-		$share = $provider->getShareById($id, $recipient);
1168
-
1169
-		$this->checkExpireDate($share);
1170
-
1171
-		return $share;
1172
-	}
1173
-
1174
-	/**
1175
-	 * Get all the shares for a given path
1176
-	 *
1177
-	 * @param \OCP\Files\Node $path
1178
-	 * @param int $page
1179
-	 * @param int $perPage
1180
-	 *
1181
-	 * @return Share[]
1182
-	 */
1183
-	public function getSharesByPath(\OCP\Files\Node $path, $page=0, $perPage=50) {
1184
-		return [];
1185
-	}
1186
-
1187
-	/**
1188
-	 * Get the share by token possible with password
1189
-	 *
1190
-	 * @param string $token
1191
-	 * @return Share
1192
-	 *
1193
-	 * @throws ShareNotFound
1194
-	 */
1195
-	public function getShareByToken($token) {
1196
-		$share = null;
1197
-		try {
1198
-			if($this->shareApiAllowLinks()) {
1199
-				$provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_LINK);
1200
-				$share = $provider->getShareByToken($token);
1201
-			}
1202
-		} catch (ProviderException $e) {
1203
-		} catch (ShareNotFound $e) {
1204
-		}
1205
-
1206
-
1207
-		// If it is not a link share try to fetch a federated share by token
1208
-		if ($share === null) {
1209
-			try {
1210
-				$provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_REMOTE);
1211
-				$share = $provider->getShareByToken($token);
1212
-			} catch (ProviderException $e) {
1213
-			} catch (ShareNotFound $e) {
1214
-			}
1215
-		}
1216
-
1217
-		// If it is not a link share try to fetch a mail share by token
1218
-		if ($share === null && $this->shareProviderExists(\OCP\Share::SHARE_TYPE_EMAIL)) {
1219
-			try {
1220
-				$provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_EMAIL);
1221
-				$share = $provider->getShareByToken($token);
1222
-			} catch (ProviderException $e) {
1223
-			} catch (ShareNotFound $e) {
1224
-			}
1225
-		}
1226
-
1227
-		if ($share === null && $this->shareProviderExists(\OCP\Share::SHARE_TYPE_CIRCLE)) {
1228
-			try {
1229
-				$provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_CIRCLE);
1230
-				$share = $provider->getShareByToken($token);
1231
-			} catch (ProviderException $e) {
1232
-			} catch (ShareNotFound $e) {
1233
-			}
1234
-		}
1235
-
1236
-		if ($share === null) {
1237
-			throw new ShareNotFound($this->l->t('The requested share does not exist anymore'));
1238
-		}
1239
-
1240
-		$this->checkExpireDate($share);
1241
-
1242
-		/*
1057
+        $shares2 = [];
1058
+
1059
+        while(true) {
1060
+            $added = 0;
1061
+            foreach ($shares as $share) {
1062
+
1063
+                try {
1064
+                    $this->checkExpireDate($share);
1065
+                } catch (ShareNotFound $e) {
1066
+                    //Ignore since this basically means the share is deleted
1067
+                    continue;
1068
+                }
1069
+
1070
+                $added++;
1071
+                $shares2[] = $share;
1072
+
1073
+                if (count($shares2) === $limit) {
1074
+                    break;
1075
+                }
1076
+            }
1077
+
1078
+            // If we did not fetch more shares than the limit then there are no more shares
1079
+            if (count($shares) < $limit) {
1080
+                break;
1081
+            }
1082
+
1083
+            if (count($shares2) === $limit) {
1084
+                break;
1085
+            }
1086
+
1087
+            // If there was no limit on the select we are done
1088
+            if ($limit === -1) {
1089
+                break;
1090
+            }
1091
+
1092
+            $offset += $added;
1093
+
1094
+            // Fetch again $limit shares
1095
+            $shares = $provider->getSharesBy($userId, $shareType, $path, $reshares, $limit, $offset);
1096
+
1097
+            // No more shares means we are done
1098
+            if (empty($shares)) {
1099
+                break;
1100
+            }
1101
+        }
1102
+
1103
+        $shares = $shares2;
1104
+
1105
+        return $shares;
1106
+    }
1107
+
1108
+    /**
1109
+     * @inheritdoc
1110
+     */
1111
+    public function getSharedWith($userId, $shareType, $node = null, $limit = 50, $offset = 0) {
1112
+        try {
1113
+            $provider = $this->factory->getProviderForType($shareType);
1114
+        } catch (ProviderException $e) {
1115
+            return [];
1116
+        }
1117
+
1118
+        $shares = $provider->getSharedWith($userId, $shareType, $node, $limit, $offset);
1119
+
1120
+        // remove all shares which are already expired
1121
+        foreach ($shares as $key => $share) {
1122
+            try {
1123
+                $this->checkExpireDate($share);
1124
+            } catch (ShareNotFound $e) {
1125
+                unset($shares[$key]);
1126
+            }
1127
+        }
1128
+
1129
+        return $shares;
1130
+    }
1131
+
1132
+    /**
1133
+     * @inheritdoc
1134
+     */
1135
+    public function getDeletedSharedWith($userId, $shareType, $node = null, $limit = 50, $offset = 0) {
1136
+        $shares = $this->getSharedWith($userId, $shareType, $node, $limit, $offset);
1137
+
1138
+        // Only get deleted shares
1139
+        $shares = array_filter($shares, function(IShare $share) {
1140
+            return $share->getPermissions() === 0;
1141
+        });
1142
+
1143
+        // Only get shares where the owner still exists
1144
+        $shares = array_filter($shares, function (IShare $share) {
1145
+            return $this->userManager->userExists($share->getShareOwner());
1146
+        });
1147
+
1148
+        return $shares;
1149
+    }
1150
+
1151
+    /**
1152
+     * @inheritdoc
1153
+     */
1154
+    public function getShareById($id, $recipient = null) {
1155
+        if ($id === null) {
1156
+            throw new ShareNotFound();
1157
+        }
1158
+
1159
+        list($providerId, $id) = $this->splitFullId($id);
1160
+
1161
+        try {
1162
+            $provider = $this->factory->getProvider($providerId);
1163
+        } catch (ProviderException $e) {
1164
+            throw new ShareNotFound();
1165
+        }
1166
+
1167
+        $share = $provider->getShareById($id, $recipient);
1168
+
1169
+        $this->checkExpireDate($share);
1170
+
1171
+        return $share;
1172
+    }
1173
+
1174
+    /**
1175
+     * Get all the shares for a given path
1176
+     *
1177
+     * @param \OCP\Files\Node $path
1178
+     * @param int $page
1179
+     * @param int $perPage
1180
+     *
1181
+     * @return Share[]
1182
+     */
1183
+    public function getSharesByPath(\OCP\Files\Node $path, $page=0, $perPage=50) {
1184
+        return [];
1185
+    }
1186
+
1187
+    /**
1188
+     * Get the share by token possible with password
1189
+     *
1190
+     * @param string $token
1191
+     * @return Share
1192
+     *
1193
+     * @throws ShareNotFound
1194
+     */
1195
+    public function getShareByToken($token) {
1196
+        $share = null;
1197
+        try {
1198
+            if($this->shareApiAllowLinks()) {
1199
+                $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_LINK);
1200
+                $share = $provider->getShareByToken($token);
1201
+            }
1202
+        } catch (ProviderException $e) {
1203
+        } catch (ShareNotFound $e) {
1204
+        }
1205
+
1206
+
1207
+        // If it is not a link share try to fetch a federated share by token
1208
+        if ($share === null) {
1209
+            try {
1210
+                $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_REMOTE);
1211
+                $share = $provider->getShareByToken($token);
1212
+            } catch (ProviderException $e) {
1213
+            } catch (ShareNotFound $e) {
1214
+            }
1215
+        }
1216
+
1217
+        // If it is not a link share try to fetch a mail share by token
1218
+        if ($share === null && $this->shareProviderExists(\OCP\Share::SHARE_TYPE_EMAIL)) {
1219
+            try {
1220
+                $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_EMAIL);
1221
+                $share = $provider->getShareByToken($token);
1222
+            } catch (ProviderException $e) {
1223
+            } catch (ShareNotFound $e) {
1224
+            }
1225
+        }
1226
+
1227
+        if ($share === null && $this->shareProviderExists(\OCP\Share::SHARE_TYPE_CIRCLE)) {
1228
+            try {
1229
+                $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_CIRCLE);
1230
+                $share = $provider->getShareByToken($token);
1231
+            } catch (ProviderException $e) {
1232
+            } catch (ShareNotFound $e) {
1233
+            }
1234
+        }
1235
+
1236
+        if ($share === null) {
1237
+            throw new ShareNotFound($this->l->t('The requested share does not exist anymore'));
1238
+        }
1239
+
1240
+        $this->checkExpireDate($share);
1241
+
1242
+        /*
1243 1243
 		 * Reduce the permissions for link shares if public upload is not enabled
1244 1244
 		 */
1245
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK &&
1246
-			!$this->shareApiLinkAllowPublicUpload()) {
1247
-			$share->setPermissions($share->getPermissions() & ~(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE));
1248
-		}
1249
-
1250
-		return $share;
1251
-	}
1252
-
1253
-	protected function checkExpireDate($share) {
1254
-		if ($share->getExpirationDate() !== null &&
1255
-			$share->getExpirationDate() <= new \DateTime()) {
1256
-			$this->deleteShare($share);
1257
-			throw new ShareNotFound($this->l->t('The requested share does not exist anymore'));
1258
-		}
1259
-
1260
-	}
1261
-
1262
-	/**
1263
-	 * Verify the password of a public share
1264
-	 *
1265
-	 * @param \OCP\Share\IShare $share
1266
-	 * @param string $password
1267
-	 * @return bool
1268
-	 */
1269
-	public function checkPassword(\OCP\Share\IShare $share, $password) {
1270
-		$passwordProtected = $share->getShareType() !== \OCP\Share::SHARE_TYPE_LINK
1271
-			|| $share->getShareType() !== \OCP\Share::SHARE_TYPE_EMAIL;
1272
-		if (!$passwordProtected) {
1273
-			//TODO maybe exception?
1274
-			return false;
1275
-		}
1276
-
1277
-		if ($password === null || $share->getPassword() === null) {
1278
-			return false;
1279
-		}
1280
-
1281
-		$newHash = '';
1282
-		if (!$this->hasher->verify($password, $share->getPassword(), $newHash)) {
1283
-			return false;
1284
-		}
1285
-
1286
-		if (!empty($newHash)) {
1287
-			$share->setPassword($newHash);
1288
-			$provider = $this->factory->getProviderForType($share->getShareType());
1289
-			$provider->update($share);
1290
-		}
1291
-
1292
-		return true;
1293
-	}
1294
-
1295
-	/**
1296
-	 * @inheritdoc
1297
-	 */
1298
-	public function userDeleted($uid) {
1299
-		$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];
1300
-
1301
-		foreach ($types as $type) {
1302
-			try {
1303
-				$provider = $this->factory->getProviderForType($type);
1304
-			} catch (ProviderException $e) {
1305
-				continue;
1306
-			}
1307
-			$provider->userDeleted($uid, $type);
1308
-		}
1309
-	}
1310
-
1311
-	/**
1312
-	 * @inheritdoc
1313
-	 */
1314
-	public function groupDeleted($gid) {
1315
-		$provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP);
1316
-		$provider->groupDeleted($gid);
1317
-	}
1318
-
1319
-	/**
1320
-	 * @inheritdoc
1321
-	 */
1322
-	public function userDeletedFromGroup($uid, $gid) {
1323
-		$provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP);
1324
-		$provider->userDeletedFromGroup($uid, $gid);
1325
-	}
1326
-
1327
-	/**
1328
-	 * Get access list to a path. This means
1329
-	 * all the users that can access a given path.
1330
-	 *
1331
-	 * Consider:
1332
-	 * -root
1333
-	 * |-folder1 (23)
1334
-	 *  |-folder2 (32)
1335
-	 *   |-fileA (42)
1336
-	 *
1337
-	 * fileA is shared with user1 and user1@server1
1338
-	 * folder2 is shared with group2 (user4 is a member of group2)
1339
-	 * folder1 is shared with user2 (renamed to "folder (1)") and user2@server2
1340
-	 *
1341
-	 * Then the access list to '/folder1/folder2/fileA' with $currentAccess is:
1342
-	 * [
1343
-	 *  users  => [
1344
-	 *      'user1' => ['node_id' => 42, 'node_path' => '/fileA'],
1345
-	 *      'user4' => ['node_id' => 32, 'node_path' => '/folder2'],
1346
-	 *      'user2' => ['node_id' => 23, 'node_path' => '/folder (1)'],
1347
-	 *  ],
1348
-	 *  remote => [
1349
-	 *      'user1@server1' => ['node_id' => 42, 'token' => 'SeCr3t'],
1350
-	 *      'user2@server2' => ['node_id' => 23, 'token' => 'FooBaR'],
1351
-	 *  ],
1352
-	 *  public => bool
1353
-	 *  mail => bool
1354
-	 * ]
1355
-	 *
1356
-	 * The access list to '/folder1/folder2/fileA' **without** $currentAccess is:
1357
-	 * [
1358
-	 *  users  => ['user1', 'user2', 'user4'],
1359
-	 *  remote => bool,
1360
-	 *  public => bool
1361
-	 *  mail => bool
1362
-	 * ]
1363
-	 *
1364
-	 * This is required for encryption/activity
1365
-	 *
1366
-	 * @param \OCP\Files\Node $path
1367
-	 * @param bool $recursive Should we check all parent folders as well
1368
-	 * @param bool $currentAccess Ensure the recipient has access to the file (e.g. did not unshare it)
1369
-	 * @return array
1370
-	 */
1371
-	public function getAccessList(\OCP\Files\Node $path, $recursive = true, $currentAccess = false) {
1372
-		$owner = $path->getOwner()->getUID();
1373
-
1374
-		if ($currentAccess) {
1375
-			$al = ['users' => [], 'remote' => [], 'public' => false];
1376
-		} else {
1377
-			$al = ['users' => [], 'remote' => false, 'public' => false];
1378
-		}
1379
-		if (!$this->userManager->userExists($owner)) {
1380
-			return $al;
1381
-		}
1382
-
1383
-		//Get node for the owner
1384
-		$userFolder = $this->rootFolder->getUserFolder($owner);
1385
-		if ($path->getId() !== $userFolder->getId() && !$userFolder->isSubNode($path)) {
1386
-			$path = $userFolder->getById($path->getId())[0];
1387
-		}
1388
-
1389
-		$providers = $this->factory->getAllProviders();
1390
-
1391
-		/** @var Node[] $nodes */
1392
-		$nodes = [];
1393
-
1394
-
1395
-		if ($currentAccess) {
1396
-			$ownerPath = $path->getPath();
1397
-			$ownerPath = explode('/', $ownerPath, 4);
1398
-			if (count($ownerPath) < 4) {
1399
-				$ownerPath = '';
1400
-			} else {
1401
-				$ownerPath = $ownerPath[3];
1402
-			}
1403
-			$al['users'][$owner] = [
1404
-				'node_id' => $path->getId(),
1405
-				'node_path' => '/' . $ownerPath,
1406
-			];
1407
-		} else {
1408
-			$al['users'][] = $owner;
1409
-		}
1410
-
1411
-		// Collect all the shares
1412
-		while ($path->getPath() !== $userFolder->getPath()) {
1413
-			$nodes[] = $path;
1414
-			if (!$recursive) {
1415
-				break;
1416
-			}
1417
-			$path = $path->getParent();
1418
-		}
1419
-
1420
-		foreach ($providers as $provider) {
1421
-			$tmp = $provider->getAccessList($nodes, $currentAccess);
1422
-
1423
-			foreach ($tmp as $k => $v) {
1424
-				if (isset($al[$k])) {
1425
-					if (is_array($al[$k])) {
1426
-						if ($currentAccess) {
1427
-							$al[$k] += $v;
1428
-						} else {
1429
-							$al[$k] = array_merge($al[$k], $v);
1430
-							$al[$k] = array_unique($al[$k]);
1431
-							$al[$k] = array_values($al[$k]);
1432
-						}
1433
-					} else {
1434
-						$al[$k] = $al[$k] || $v;
1435
-					}
1436
-				} else {
1437
-					$al[$k] = $v;
1438
-				}
1439
-			}
1440
-		}
1441
-
1442
-		return $al;
1443
-	}
1444
-
1445
-	/**
1446
-	 * Create a new share
1447
-	 * @return \OCP\Share\IShare
1448
-	 */
1449
-	public function newShare() {
1450
-		return new \OC\Share20\Share($this->rootFolder, $this->userManager);
1451
-	}
1452
-
1453
-	/**
1454
-	 * Is the share API enabled
1455
-	 *
1456
-	 * @return bool
1457
-	 */
1458
-	public function shareApiEnabled() {
1459
-		return $this->config->getAppValue('core', 'shareapi_enabled', 'yes') === 'yes';
1460
-	}
1461
-
1462
-	/**
1463
-	 * Is public link sharing enabled
1464
-	 *
1465
-	 * @return bool
1466
-	 */
1467
-	public function shareApiAllowLinks() {
1468
-		return $this->config->getAppValue('core', 'shareapi_allow_links', 'yes') === 'yes';
1469
-	}
1470
-
1471
-	/**
1472
-	 * Is password on public link requires
1473
-	 *
1474
-	 * @return bool
1475
-	 */
1476
-	public function shareApiLinkEnforcePassword() {
1477
-		return $this->config->getAppValue('core', 'shareapi_enforce_links_password', 'no') === 'yes';
1478
-	}
1479
-
1480
-	/**
1481
-	 * Is default expire date enabled
1482
-	 *
1483
-	 * @return bool
1484
-	 */
1485
-	public function shareApiLinkDefaultExpireDate() {
1486
-		return $this->config->getAppValue('core', 'shareapi_default_expire_date', 'no') === 'yes';
1487
-	}
1488
-
1489
-	/**
1490
-	 * Is default expire date enforced
1491
-	 *`
1492
-	 * @return bool
1493
-	 */
1494
-	public function shareApiLinkDefaultExpireDateEnforced() {
1495
-		return $this->shareApiLinkDefaultExpireDate() &&
1496
-			$this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'no') === 'yes';
1497
-	}
1498
-
1499
-	/**
1500
-	 * Number of default expire days
1501
-	 *shareApiLinkAllowPublicUpload
1502
-	 * @return int
1503
-	 */
1504
-	public function shareApiLinkDefaultExpireDays() {
1505
-		return (int)$this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7');
1506
-	}
1507
-
1508
-	/**
1509
-	 * Allow public upload on link shares
1510
-	 *
1511
-	 * @return bool
1512
-	 */
1513
-	public function shareApiLinkAllowPublicUpload() {
1514
-		return $this->config->getAppValue('core', 'shareapi_allow_public_upload', 'yes') === 'yes';
1515
-	}
1516
-
1517
-	/**
1518
-	 * check if user can only share with group members
1519
-	 * @return bool
1520
-	 */
1521
-	public function shareWithGroupMembersOnly() {
1522
-		return $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
1523
-	}
1524
-
1525
-	/**
1526
-	 * Check if users can share with groups
1527
-	 * @return bool
1528
-	 */
1529
-	public function allowGroupSharing() {
1530
-		return $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes') === 'yes';
1531
-	}
1532
-
1533
-	/**
1534
-	 * Copied from \OC_Util::isSharingDisabledForUser
1535
-	 *
1536
-	 * TODO: Deprecate fuction from OC_Util
1537
-	 *
1538
-	 * @param string $userId
1539
-	 * @return bool
1540
-	 */
1541
-	public function sharingDisabledForUser($userId) {
1542
-		if ($userId === null) {
1543
-			return false;
1544
-		}
1545
-
1546
-		if (isset($this->sharingDisabledForUsersCache[$userId])) {
1547
-			return $this->sharingDisabledForUsersCache[$userId];
1548
-		}
1549
-
1550
-		if ($this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes') {
1551
-			$groupsList = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', '');
1552
-			$excludedGroups = json_decode($groupsList);
1553
-			if (is_null($excludedGroups)) {
1554
-				$excludedGroups = explode(',', $groupsList);
1555
-				$newValue = json_encode($excludedGroups);
1556
-				$this->config->setAppValue('core', 'shareapi_exclude_groups_list', $newValue);
1557
-			}
1558
-			$user = $this->userManager->get($userId);
1559
-			$usersGroups = $this->groupManager->getUserGroupIds($user);
1560
-			if (!empty($usersGroups)) {
1561
-				$remainingGroups = array_diff($usersGroups, $excludedGroups);
1562
-				// if the user is only in groups which are disabled for sharing then
1563
-				// sharing is also disabled for the user
1564
-				if (empty($remainingGroups)) {
1565
-					$this->sharingDisabledForUsersCache[$userId] = true;
1566
-					return true;
1567
-				}
1568
-			}
1569
-		}
1570
-
1571
-		$this->sharingDisabledForUsersCache[$userId] = false;
1572
-		return false;
1573
-	}
1574
-
1575
-	/**
1576
-	 * @inheritdoc
1577
-	 */
1578
-	public function outgoingServer2ServerSharesAllowed() {
1579
-		return $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') === 'yes';
1580
-	}
1581
-
1582
-	/**
1583
-	 * @inheritdoc
1584
-	 */
1585
-	public function shareProviderExists($shareType) {
1586
-		try {
1587
-			$this->factory->getProviderForType($shareType);
1588
-		} catch (ProviderException $e) {
1589
-			return false;
1590
-		}
1591
-
1592
-		return true;
1593
-	}
1245
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK &&
1246
+            !$this->shareApiLinkAllowPublicUpload()) {
1247
+            $share->setPermissions($share->getPermissions() & ~(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE));
1248
+        }
1249
+
1250
+        return $share;
1251
+    }
1252
+
1253
+    protected function checkExpireDate($share) {
1254
+        if ($share->getExpirationDate() !== null &&
1255
+            $share->getExpirationDate() <= new \DateTime()) {
1256
+            $this->deleteShare($share);
1257
+            throw new ShareNotFound($this->l->t('The requested share does not exist anymore'));
1258
+        }
1259
+
1260
+    }
1261
+
1262
+    /**
1263
+     * Verify the password of a public share
1264
+     *
1265
+     * @param \OCP\Share\IShare $share
1266
+     * @param string $password
1267
+     * @return bool
1268
+     */
1269
+    public function checkPassword(\OCP\Share\IShare $share, $password) {
1270
+        $passwordProtected = $share->getShareType() !== \OCP\Share::SHARE_TYPE_LINK
1271
+            || $share->getShareType() !== \OCP\Share::SHARE_TYPE_EMAIL;
1272
+        if (!$passwordProtected) {
1273
+            //TODO maybe exception?
1274
+            return false;
1275
+        }
1276
+
1277
+        if ($password === null || $share->getPassword() === null) {
1278
+            return false;
1279
+        }
1280
+
1281
+        $newHash = '';
1282
+        if (!$this->hasher->verify($password, $share->getPassword(), $newHash)) {
1283
+            return false;
1284
+        }
1285
+
1286
+        if (!empty($newHash)) {
1287
+            $share->setPassword($newHash);
1288
+            $provider = $this->factory->getProviderForType($share->getShareType());
1289
+            $provider->update($share);
1290
+        }
1291
+
1292
+        return true;
1293
+    }
1294
+
1295
+    /**
1296
+     * @inheritdoc
1297
+     */
1298
+    public function userDeleted($uid) {
1299
+        $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];
1300
+
1301
+        foreach ($types as $type) {
1302
+            try {
1303
+                $provider = $this->factory->getProviderForType($type);
1304
+            } catch (ProviderException $e) {
1305
+                continue;
1306
+            }
1307
+            $provider->userDeleted($uid, $type);
1308
+        }
1309
+    }
1310
+
1311
+    /**
1312
+     * @inheritdoc
1313
+     */
1314
+    public function groupDeleted($gid) {
1315
+        $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP);
1316
+        $provider->groupDeleted($gid);
1317
+    }
1318
+
1319
+    /**
1320
+     * @inheritdoc
1321
+     */
1322
+    public function userDeletedFromGroup($uid, $gid) {
1323
+        $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP);
1324
+        $provider->userDeletedFromGroup($uid, $gid);
1325
+    }
1326
+
1327
+    /**
1328
+     * Get access list to a path. This means
1329
+     * all the users that can access a given path.
1330
+     *
1331
+     * Consider:
1332
+     * -root
1333
+     * |-folder1 (23)
1334
+     *  |-folder2 (32)
1335
+     *   |-fileA (42)
1336
+     *
1337
+     * fileA is shared with user1 and user1@server1
1338
+     * folder2 is shared with group2 (user4 is a member of group2)
1339
+     * folder1 is shared with user2 (renamed to "folder (1)") and user2@server2
1340
+     *
1341
+     * Then the access list to '/folder1/folder2/fileA' with $currentAccess is:
1342
+     * [
1343
+     *  users  => [
1344
+     *      'user1' => ['node_id' => 42, 'node_path' => '/fileA'],
1345
+     *      'user4' => ['node_id' => 32, 'node_path' => '/folder2'],
1346
+     *      'user2' => ['node_id' => 23, 'node_path' => '/folder (1)'],
1347
+     *  ],
1348
+     *  remote => [
1349
+     *      'user1@server1' => ['node_id' => 42, 'token' => 'SeCr3t'],
1350
+     *      'user2@server2' => ['node_id' => 23, 'token' => 'FooBaR'],
1351
+     *  ],
1352
+     *  public => bool
1353
+     *  mail => bool
1354
+     * ]
1355
+     *
1356
+     * The access list to '/folder1/folder2/fileA' **without** $currentAccess is:
1357
+     * [
1358
+     *  users  => ['user1', 'user2', 'user4'],
1359
+     *  remote => bool,
1360
+     *  public => bool
1361
+     *  mail => bool
1362
+     * ]
1363
+     *
1364
+     * This is required for encryption/activity
1365
+     *
1366
+     * @param \OCP\Files\Node $path
1367
+     * @param bool $recursive Should we check all parent folders as well
1368
+     * @param bool $currentAccess Ensure the recipient has access to the file (e.g. did not unshare it)
1369
+     * @return array
1370
+     */
1371
+    public function getAccessList(\OCP\Files\Node $path, $recursive = true, $currentAccess = false) {
1372
+        $owner = $path->getOwner()->getUID();
1373
+
1374
+        if ($currentAccess) {
1375
+            $al = ['users' => [], 'remote' => [], 'public' => false];
1376
+        } else {
1377
+            $al = ['users' => [], 'remote' => false, 'public' => false];
1378
+        }
1379
+        if (!$this->userManager->userExists($owner)) {
1380
+            return $al;
1381
+        }
1382
+
1383
+        //Get node for the owner
1384
+        $userFolder = $this->rootFolder->getUserFolder($owner);
1385
+        if ($path->getId() !== $userFolder->getId() && !$userFolder->isSubNode($path)) {
1386
+            $path = $userFolder->getById($path->getId())[0];
1387
+        }
1388
+
1389
+        $providers = $this->factory->getAllProviders();
1390
+
1391
+        /** @var Node[] $nodes */
1392
+        $nodes = [];
1393
+
1394
+
1395
+        if ($currentAccess) {
1396
+            $ownerPath = $path->getPath();
1397
+            $ownerPath = explode('/', $ownerPath, 4);
1398
+            if (count($ownerPath) < 4) {
1399
+                $ownerPath = '';
1400
+            } else {
1401
+                $ownerPath = $ownerPath[3];
1402
+            }
1403
+            $al['users'][$owner] = [
1404
+                'node_id' => $path->getId(),
1405
+                'node_path' => '/' . $ownerPath,
1406
+            ];
1407
+        } else {
1408
+            $al['users'][] = $owner;
1409
+        }
1410
+
1411
+        // Collect all the shares
1412
+        while ($path->getPath() !== $userFolder->getPath()) {
1413
+            $nodes[] = $path;
1414
+            if (!$recursive) {
1415
+                break;
1416
+            }
1417
+            $path = $path->getParent();
1418
+        }
1419
+
1420
+        foreach ($providers as $provider) {
1421
+            $tmp = $provider->getAccessList($nodes, $currentAccess);
1422
+
1423
+            foreach ($tmp as $k => $v) {
1424
+                if (isset($al[$k])) {
1425
+                    if (is_array($al[$k])) {
1426
+                        if ($currentAccess) {
1427
+                            $al[$k] += $v;
1428
+                        } else {
1429
+                            $al[$k] = array_merge($al[$k], $v);
1430
+                            $al[$k] = array_unique($al[$k]);
1431
+                            $al[$k] = array_values($al[$k]);
1432
+                        }
1433
+                    } else {
1434
+                        $al[$k] = $al[$k] || $v;
1435
+                    }
1436
+                } else {
1437
+                    $al[$k] = $v;
1438
+                }
1439
+            }
1440
+        }
1441
+
1442
+        return $al;
1443
+    }
1444
+
1445
+    /**
1446
+     * Create a new share
1447
+     * @return \OCP\Share\IShare
1448
+     */
1449
+    public function newShare() {
1450
+        return new \OC\Share20\Share($this->rootFolder, $this->userManager);
1451
+    }
1452
+
1453
+    /**
1454
+     * Is the share API enabled
1455
+     *
1456
+     * @return bool
1457
+     */
1458
+    public function shareApiEnabled() {
1459
+        return $this->config->getAppValue('core', 'shareapi_enabled', 'yes') === 'yes';
1460
+    }
1461
+
1462
+    /**
1463
+     * Is public link sharing enabled
1464
+     *
1465
+     * @return bool
1466
+     */
1467
+    public function shareApiAllowLinks() {
1468
+        return $this->config->getAppValue('core', 'shareapi_allow_links', 'yes') === 'yes';
1469
+    }
1470
+
1471
+    /**
1472
+     * Is password on public link requires
1473
+     *
1474
+     * @return bool
1475
+     */
1476
+    public function shareApiLinkEnforcePassword() {
1477
+        return $this->config->getAppValue('core', 'shareapi_enforce_links_password', 'no') === 'yes';
1478
+    }
1479
+
1480
+    /**
1481
+     * Is default expire date enabled
1482
+     *
1483
+     * @return bool
1484
+     */
1485
+    public function shareApiLinkDefaultExpireDate() {
1486
+        return $this->config->getAppValue('core', 'shareapi_default_expire_date', 'no') === 'yes';
1487
+    }
1488
+
1489
+    /**
1490
+     * Is default expire date enforced
1491
+     *`
1492
+     * @return bool
1493
+     */
1494
+    public function shareApiLinkDefaultExpireDateEnforced() {
1495
+        return $this->shareApiLinkDefaultExpireDate() &&
1496
+            $this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'no') === 'yes';
1497
+    }
1498
+
1499
+    /**
1500
+     * Number of default expire days
1501
+     *shareApiLinkAllowPublicUpload
1502
+     * @return int
1503
+     */
1504
+    public function shareApiLinkDefaultExpireDays() {
1505
+        return (int)$this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7');
1506
+    }
1507
+
1508
+    /**
1509
+     * Allow public upload on link shares
1510
+     *
1511
+     * @return bool
1512
+     */
1513
+    public function shareApiLinkAllowPublicUpload() {
1514
+        return $this->config->getAppValue('core', 'shareapi_allow_public_upload', 'yes') === 'yes';
1515
+    }
1516
+
1517
+    /**
1518
+     * check if user can only share with group members
1519
+     * @return bool
1520
+     */
1521
+    public function shareWithGroupMembersOnly() {
1522
+        return $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
1523
+    }
1524
+
1525
+    /**
1526
+     * Check if users can share with groups
1527
+     * @return bool
1528
+     */
1529
+    public function allowGroupSharing() {
1530
+        return $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes') === 'yes';
1531
+    }
1532
+
1533
+    /**
1534
+     * Copied from \OC_Util::isSharingDisabledForUser
1535
+     *
1536
+     * TODO: Deprecate fuction from OC_Util
1537
+     *
1538
+     * @param string $userId
1539
+     * @return bool
1540
+     */
1541
+    public function sharingDisabledForUser($userId) {
1542
+        if ($userId === null) {
1543
+            return false;
1544
+        }
1545
+
1546
+        if (isset($this->sharingDisabledForUsersCache[$userId])) {
1547
+            return $this->sharingDisabledForUsersCache[$userId];
1548
+        }
1549
+
1550
+        if ($this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes') {
1551
+            $groupsList = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', '');
1552
+            $excludedGroups = json_decode($groupsList);
1553
+            if (is_null($excludedGroups)) {
1554
+                $excludedGroups = explode(',', $groupsList);
1555
+                $newValue = json_encode($excludedGroups);
1556
+                $this->config->setAppValue('core', 'shareapi_exclude_groups_list', $newValue);
1557
+            }
1558
+            $user = $this->userManager->get($userId);
1559
+            $usersGroups = $this->groupManager->getUserGroupIds($user);
1560
+            if (!empty($usersGroups)) {
1561
+                $remainingGroups = array_diff($usersGroups, $excludedGroups);
1562
+                // if the user is only in groups which are disabled for sharing then
1563
+                // sharing is also disabled for the user
1564
+                if (empty($remainingGroups)) {
1565
+                    $this->sharingDisabledForUsersCache[$userId] = true;
1566
+                    return true;
1567
+                }
1568
+            }
1569
+        }
1570
+
1571
+        $this->sharingDisabledForUsersCache[$userId] = false;
1572
+        return false;
1573
+    }
1574
+
1575
+    /**
1576
+     * @inheritdoc
1577
+     */
1578
+    public function outgoingServer2ServerSharesAllowed() {
1579
+        return $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') === 'yes';
1580
+    }
1581
+
1582
+    /**
1583
+     * @inheritdoc
1584
+     */
1585
+    public function shareProviderExists($shareType) {
1586
+        try {
1587
+            $this->factory->getProviderForType($shareType);
1588
+        } catch (ProviderException $e) {
1589
+            return false;
1590
+        }
1591
+
1592
+        return true;
1593
+    }
1594 1594
 
1595 1595
 }
Please login to merge, or discard this patch.
apps/files_sharing/lib/Controller/DeletedShareAPIController.php 1 patch
Indentation   +123 added lines, -123 removed lines patch added patch discarded remove patch
@@ -17,127 +17,127 @@
 block discarded – undo
17 17
 
18 18
 class DeletedShareAPIController extends OCSController {
19 19
 
20
-	/** @var ShareManager */
21
-	private $shareManager;
22
-
23
-	/** @var string */
24
-	private $userId;
25
-
26
-	/** @var IUserManager */
27
-	private $userManager;
28
-
29
-	/** @var IGroupManager */
30
-	private $groupManager;
31
-
32
-	/** @var IRootFolder */
33
-	private $rootFolder;
34
-
35
-	public function __construct(string $appName,
36
-								IRequest $request,
37
-								ShareManager $shareManager,
38
-								string $UserId,
39
-								IUserManager $userManager,
40
-								IGroupManager $groupManager,
41
-								IRootFolder $rootFolder) {
42
-		parent::__construct($appName, $request);
43
-
44
-		$this->shareManager = $shareManager;
45
-		$this->userId = $UserId;
46
-		$this->userManager = $userManager;
47
-		$this->groupManager = $groupManager;
48
-		$this->rootFolder = $rootFolder;
49
-	}
50
-
51
-	private function formatShare(IShare $share): array {
52
-
53
-		$result = [
54
-			'id' => $share->getFullId(),
55
-			'share_type' => $share->getShareType(),
56
-			'uid_owner' => $share->getSharedBy(),
57
-			'displayname_owner' => $this->userManager->get($share->getSharedBy())->getDisplayName(),
58
-			'permissions' => 0,
59
-			'stime' => $share->getShareTime()->getTimestamp(),
60
-			'parent' => null,
61
-			'expiration' => null,
62
-			'token' => null,
63
-			'uid_file_owner' => $share->getShareOwner(),
64
-			'displayname_file_owner' => $this->userManager->get($share->getShareOwner())->getDisplayName(),
65
-			'path' => $share->getTarget(),
66
-		];
67
-		$userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
68
-		$nodes = $userFolder->getById($share->getNodeId());
69
-		if (empty($nodes)) {
70
-			// fallback to guessing the path
71
-			$node = $userFolder->get($share->getTarget());
72
-			if ($node === null || $share->getTarget() === '') {
73
-				throw new NotFoundException();
74
-			}
75
-		} else {
76
-			$node = $nodes[0];
77
-		}
78
-
79
-		$result['path'] = $userFolder->getRelativePath($node->getPath());
80
-		if ($node instanceOf \OCP\Files\Folder) {
81
-			$result['item_type'] = 'folder';
82
-		} else {
83
-			$result['item_type'] = 'file';
84
-		}
85
-		$result['mimetype'] = $node->getMimetype();
86
-		$result['storage_id'] = $node->getStorage()->getId();
87
-		$result['storage'] = $node->getStorage()->getCache()->getNumericStorageId();
88
-		$result['item_source'] = $node->getId();
89
-		$result['file_source'] = $node->getId();
90
-		$result['file_parent'] = $node->getParent()->getId();
91
-		$result['file_target'] = $share->getTarget();
92
-
93
-		$expiration = $share->getExpirationDate();
94
-		if ($expiration !== null) {
95
-			$result['expiration'] = $expiration->format('Y-m-d 00:00:00');
96
-		}
97
-
98
-		$group = $this->groupManager->get($share->getSharedWith());
99
-		$result['share_with'] = $share->getSharedWith();
100
-		$result['share_with_displayname'] = $group !== null ? $group->getDisplayName() : $share->getSharedWith();
101
-
102
-		return $result;
103
-
104
-	}
105
-
106
-	/**
107
-	 * @NoAdminRequired
108
-	 */
109
-	public function index(): DataResponse {
110
-		$shares = $this->shareManager->getDeletedSharedWith($this->userId, \OCP\Share::SHARE_TYPE_GROUP, null, -1, 0);
111
-
112
-		$shares = array_map(function (IShare $share) {
113
-			return $this->formatShare($share);
114
-		}, $shares);
115
-
116
-		return new DataResponse($shares);
117
-	}
118
-
119
-	/**
120
-	 * @NoAdminRequired
121
-	 *
122
-	 * @throws OCSException
123
-	 */
124
-	public function undelete(string $id): DataResponse {
125
-		try {
126
-			$share = $this->shareManager->getShareById($id, $this->userId);
127
-		} catch (ShareNotFound $e) {
128
-			throw new OCSNotFoundException('Share not found');
129
-		}
130
-
131
-		if ($share->getPermissions() !== 0) {
132
-			throw new OCSNotFoundException('No deleted share found');
133
-		}
134
-
135
-		try {
136
-			$this->shareManager->restoreShare($share, $this->userId);
137
-		} catch (GenericShareException $e) {
138
-			throw new OCSException('Something went wrong');
139
-		}
140
-
141
-		return new DataResponse([]);
142
-	}
20
+    /** @var ShareManager */
21
+    private $shareManager;
22
+
23
+    /** @var string */
24
+    private $userId;
25
+
26
+    /** @var IUserManager */
27
+    private $userManager;
28
+
29
+    /** @var IGroupManager */
30
+    private $groupManager;
31
+
32
+    /** @var IRootFolder */
33
+    private $rootFolder;
34
+
35
+    public function __construct(string $appName,
36
+                                IRequest $request,
37
+                                ShareManager $shareManager,
38
+                                string $UserId,
39
+                                IUserManager $userManager,
40
+                                IGroupManager $groupManager,
41
+                                IRootFolder $rootFolder) {
42
+        parent::__construct($appName, $request);
43
+
44
+        $this->shareManager = $shareManager;
45
+        $this->userId = $UserId;
46
+        $this->userManager = $userManager;
47
+        $this->groupManager = $groupManager;
48
+        $this->rootFolder = $rootFolder;
49
+    }
50
+
51
+    private function formatShare(IShare $share): array {
52
+
53
+        $result = [
54
+            'id' => $share->getFullId(),
55
+            'share_type' => $share->getShareType(),
56
+            'uid_owner' => $share->getSharedBy(),
57
+            'displayname_owner' => $this->userManager->get($share->getSharedBy())->getDisplayName(),
58
+            'permissions' => 0,
59
+            'stime' => $share->getShareTime()->getTimestamp(),
60
+            'parent' => null,
61
+            'expiration' => null,
62
+            'token' => null,
63
+            'uid_file_owner' => $share->getShareOwner(),
64
+            'displayname_file_owner' => $this->userManager->get($share->getShareOwner())->getDisplayName(),
65
+            'path' => $share->getTarget(),
66
+        ];
67
+        $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
68
+        $nodes = $userFolder->getById($share->getNodeId());
69
+        if (empty($nodes)) {
70
+            // fallback to guessing the path
71
+            $node = $userFolder->get($share->getTarget());
72
+            if ($node === null || $share->getTarget() === '') {
73
+                throw new NotFoundException();
74
+            }
75
+        } else {
76
+            $node = $nodes[0];
77
+        }
78
+
79
+        $result['path'] = $userFolder->getRelativePath($node->getPath());
80
+        if ($node instanceOf \OCP\Files\Folder) {
81
+            $result['item_type'] = 'folder';
82
+        } else {
83
+            $result['item_type'] = 'file';
84
+        }
85
+        $result['mimetype'] = $node->getMimetype();
86
+        $result['storage_id'] = $node->getStorage()->getId();
87
+        $result['storage'] = $node->getStorage()->getCache()->getNumericStorageId();
88
+        $result['item_source'] = $node->getId();
89
+        $result['file_source'] = $node->getId();
90
+        $result['file_parent'] = $node->getParent()->getId();
91
+        $result['file_target'] = $share->getTarget();
92
+
93
+        $expiration = $share->getExpirationDate();
94
+        if ($expiration !== null) {
95
+            $result['expiration'] = $expiration->format('Y-m-d 00:00:00');
96
+        }
97
+
98
+        $group = $this->groupManager->get($share->getSharedWith());
99
+        $result['share_with'] = $share->getSharedWith();
100
+        $result['share_with_displayname'] = $group !== null ? $group->getDisplayName() : $share->getSharedWith();
101
+
102
+        return $result;
103
+
104
+    }
105
+
106
+    /**
107
+     * @NoAdminRequired
108
+     */
109
+    public function index(): DataResponse {
110
+        $shares = $this->shareManager->getDeletedSharedWith($this->userId, \OCP\Share::SHARE_TYPE_GROUP, null, -1, 0);
111
+
112
+        $shares = array_map(function (IShare $share) {
113
+            return $this->formatShare($share);
114
+        }, $shares);
115
+
116
+        return new DataResponse($shares);
117
+    }
118
+
119
+    /**
120
+     * @NoAdminRequired
121
+     *
122
+     * @throws OCSException
123
+     */
124
+    public function undelete(string $id): DataResponse {
125
+        try {
126
+            $share = $this->shareManager->getShareById($id, $this->userId);
127
+        } catch (ShareNotFound $e) {
128
+            throw new OCSNotFoundException('Share not found');
129
+        }
130
+
131
+        if ($share->getPermissions() !== 0) {
132
+            throw new OCSNotFoundException('No deleted share found');
133
+        }
134
+
135
+        try {
136
+            $this->shareManager->restoreShare($share, $this->userId);
137
+        } catch (GenericShareException $e) {
138
+            throw new OCSException('Something went wrong');
139
+        }
140
+
141
+        return new DataResponse([]);
142
+    }
143 143
 }
Please login to merge, or discard this patch.
apps/files_sharing/appinfo/routes.php 1 patch
Indentation   +96 added lines, -96 removed lines patch added patch discarded remove patch
@@ -23,109 +23,109 @@
 block discarded – undo
23 23
  */
24 24
 
25 25
 return [
26
-	'resources' => [
27
-		'ExternalShares' => ['url' => '/api/externalShares'],
28
-	],
29
-	'routes' => [
30
-		[
31
-			'name' => 'externalShares#testRemote',
32
-			'url' => '/testremote',
33
-			'verb' => 'GET'
34
-		],
35
-		[
36
-			'name' => 'PublicPreview#getPreview',
37
-			'url' => '/publicpreview/{token}',
38
-			'verb' => 'GET',
39
-		],
26
+    'resources' => [
27
+        'ExternalShares' => ['url' => '/api/externalShares'],
28
+    ],
29
+    'routes' => [
30
+        [
31
+            'name' => 'externalShares#testRemote',
32
+            'url' => '/testremote',
33
+            'verb' => 'GET'
34
+        ],
35
+        [
36
+            'name' => 'PublicPreview#getPreview',
37
+            'url' => '/publicpreview/{token}',
38
+            'verb' => 'GET',
39
+        ],
40 40
 
41
-		[
42
-			'name' => 'ShareInfo#info',
43
-			'url' => '/shareinfo',
44
-			'verb' => 'POST',
45
-		],
46
-	],
47
-	'ocs' => [
48
-		/*
41
+        [
42
+            'name' => 'ShareInfo#info',
43
+            'url' => '/shareinfo',
44
+            'verb' => 'POST',
45
+        ],
46
+    ],
47
+    'ocs' => [
48
+        /*
49 49
 		 * OCS Share API
50 50
 		 */
51
-		[
52
-			'name' => 'ShareAPI#getShares',
53
-			'url'  => '/api/v1/shares',
54
-			'verb' => 'GET',
55
-		],
56
-		[
57
-			'name' => 'ShareAPI#createShare',
58
-			'url'  => '/api/v1/shares',
59
-			'verb' => 'POST',
60
-		],
61
-		[
62
-			'name' => 'ShareAPI#getShare',
63
-			'url'  => '/api/v1/shares/{id}',
64
-			'verb' => 'GET',
65
-		],
66
-		[
67
-			'name' => 'ShareAPI#updateShare',
68
-			'url'  => '/api/v1/shares/{id}',
69
-			'verb' => 'PUT',
70
-		],
71
-		[
72
-			'name' => 'ShareAPI#deleteShare',
73
-			'url'  => '/api/v1/shares/{id}',
74
-			'verb' => 'DELETE',
75
-		],
76
-		/*
51
+        [
52
+            'name' => 'ShareAPI#getShares',
53
+            'url'  => '/api/v1/shares',
54
+            'verb' => 'GET',
55
+        ],
56
+        [
57
+            'name' => 'ShareAPI#createShare',
58
+            'url'  => '/api/v1/shares',
59
+            'verb' => 'POST',
60
+        ],
61
+        [
62
+            'name' => 'ShareAPI#getShare',
63
+            'url'  => '/api/v1/shares/{id}',
64
+            'verb' => 'GET',
65
+        ],
66
+        [
67
+            'name' => 'ShareAPI#updateShare',
68
+            'url'  => '/api/v1/shares/{id}',
69
+            'verb' => 'PUT',
70
+        ],
71
+        [
72
+            'name' => 'ShareAPI#deleteShare',
73
+            'url'  => '/api/v1/shares/{id}',
74
+            'verb' => 'DELETE',
75
+        ],
76
+        /*
77 77
 		 * Deleted Shares
78 78
 		 */
79
-		[
80
-			'name' => 'DeletedShareAPI#index',
81
-			'url'  => '/api/v1/deletedshares',
82
-			'verb' => 'GET',
83
-		],
84
-		[
85
-			'name' => 'DeletedShareAPI#undelete',
86
-			'url'  => '/api/v1/deletedshares/{id}',
87
-			'verb' => 'POST',
88
-		],
89
-		/*
79
+        [
80
+            'name' => 'DeletedShareAPI#index',
81
+            'url'  => '/api/v1/deletedshares',
82
+            'verb' => 'GET',
83
+        ],
84
+        [
85
+            'name' => 'DeletedShareAPI#undelete',
86
+            'url'  => '/api/v1/deletedshares/{id}',
87
+            'verb' => 'POST',
88
+        ],
89
+        /*
90 90
 		 * OCS Sharee API
91 91
 		 */
92
-		[
93
-			'name' => 'ShareesAPI#search',
94
-			'url' => '/api/v1/sharees',
95
-			'verb' => 'GET',
96
-		],
97
-		/*
92
+        [
93
+            'name' => 'ShareesAPI#search',
94
+            'url' => '/api/v1/sharees',
95
+            'verb' => 'GET',
96
+        ],
97
+        /*
98 98
 		 * Remote Shares
99 99
 		 */
100
-		[
101
-			'name' => 'Remote#getShares',
102
-			'url' => '/api/v1/remote_shares',
103
-			'verb' => 'GET',
104
-		],
105
-		[
106
-			'name' => 'Remote#getOpenShares',
107
-			'url' => '/api/v1/remote_shares/pending',
108
-			'verb' => 'GET',
109
-		],
110
-		[
111
-			'name' => 'Remote#acceptShare',
112
-			'url' => '/api/v1/remote_shares/pending/{id}',
113
-			'verb' => 'POST',
114
-		],
115
-		[
116
-			'name' => 'Remote#declineShare',
117
-			'url' => '/api/v1/remote_shares/pending/{id}',
118
-			'verb' => 'DELETE',
119
-		],
120
-		[
121
-			'name' => 'Remote#getShare',
122
-			'url' => '/api/v1/remote_shares/{id}',
123
-			'verb' => 'GET',
124
-		],
125
-		[
126
-			'name' => 'Remote#unshare',
127
-			'url' => '/api/v1/remote_shares/{id}',
128
-			'verb' => 'DELETE',
129
-		],
130
-	],
100
+        [
101
+            'name' => 'Remote#getShares',
102
+            'url' => '/api/v1/remote_shares',
103
+            'verb' => 'GET',
104
+        ],
105
+        [
106
+            'name' => 'Remote#getOpenShares',
107
+            'url' => '/api/v1/remote_shares/pending',
108
+            'verb' => 'GET',
109
+        ],
110
+        [
111
+            'name' => 'Remote#acceptShare',
112
+            'url' => '/api/v1/remote_shares/pending/{id}',
113
+            'verb' => 'POST',
114
+        ],
115
+        [
116
+            'name' => 'Remote#declineShare',
117
+            'url' => '/api/v1/remote_shares/pending/{id}',
118
+            'verb' => 'DELETE',
119
+        ],
120
+        [
121
+            'name' => 'Remote#getShare',
122
+            'url' => '/api/v1/remote_shares/{id}',
123
+            'verb' => 'GET',
124
+        ],
125
+        [
126
+            'name' => 'Remote#unshare',
127
+            'url' => '/api/v1/remote_shares/{id}',
128
+            'verb' => 'DELETE',
129
+        ],
130
+    ],
131 131
 ];
Please login to merge, or discard this patch.
apps/files_sharing/appinfo/app.php 1 patch
Indentation   +50 added lines, -50 removed lines patch added patch discarded remove patch
@@ -40,11 +40,11 @@  discard block
 block discarded – undo
40 40
 
41 41
 $eventDispatcher = \OC::$server->getEventDispatcher();
42 42
 $eventDispatcher->addListener(
43
-	'OCA\Files::loadAdditionalScripts',
44
-	function() {
45
-		\OCP\Util::addStyle('files_sharing', 'mergedAdditionalStyles');
46
-		\OCP\Util::addScript('files_sharing', 'additionalScripts');
47
-	}
43
+    'OCA\Files::loadAdditionalScripts',
44
+    function() {
45
+        \OCP\Util::addStyle('files_sharing', 'mergedAdditionalStyles');
46
+        \OCP\Util::addScript('files_sharing', 'additionalScripts');
47
+    }
48 48
 );
49 49
 
50 50
 $config = \OC::$server->getConfig();
@@ -52,52 +52,52 @@  discard block
 block discarded – undo
52 52
 $userSession = \OC::$server->getUserSession();
53 53
 
54 54
 if ($config->getAppValue('core', 'shareapi_enabled', 'yes') === 'yes') {
55
-	\OCA\Files\App::getNavigationManager()->add(function () {
56
-		$l = \OC::$server->getL10N('files_sharing');
57
-		return [
58
-			'id' => 'sharingin',
59
-			'appname' => 'files_sharing',
60
-			'script' => 'list.php',
61
-			'order' => 15,
62
-			'name' => $l->t('Shared with you'),
63
-		];
64
-	});
55
+    \OCA\Files\App::getNavigationManager()->add(function () {
56
+        $l = \OC::$server->getL10N('files_sharing');
57
+        return [
58
+            'id' => 'sharingin',
59
+            'appname' => 'files_sharing',
60
+            'script' => 'list.php',
61
+            'order' => 15,
62
+            'name' => $l->t('Shared with you'),
63
+        ];
64
+    });
65 65
 	
66
-	\OCA\Files\App::getNavigationManager()->add(function () {
67
-		$l = \OC::$server->getL10N('files_sharing');
68
-		return [
69
-			'id' => 'deletedshares',
70
-			'appname' => 'files_sharing',
71
-			'script' => 'list.php',
72
-			'order' => 18,
73
-			'name' => $l->t('Deleted shares'),
74
-		];
75
-	});
66
+    \OCA\Files\App::getNavigationManager()->add(function () {
67
+        $l = \OC::$server->getL10N('files_sharing');
68
+        return [
69
+            'id' => 'deletedshares',
70
+            'appname' => 'files_sharing',
71
+            'script' => 'list.php',
72
+            'order' => 18,
73
+            'name' => $l->t('Deleted shares'),
74
+        ];
75
+    });
76 76
 
77
-	if (\OCP\Util::isSharingDisabledForUser() === false) {
78
-		\OCA\Files\App::getNavigationManager()->add(function () {
79
-			$l = \OC::$server->getL10N('files_sharing');
80
-			return [
81
-				'id' => 'sharingout',
82
-				'appname' => 'files_sharing',
83
-				'script' => 'list.php',
84
-				'order' => 16,
85
-				'name' => $l->t('Shared with others'),
86
-			];
87
-		});
77
+    if (\OCP\Util::isSharingDisabledForUser() === false) {
78
+        \OCA\Files\App::getNavigationManager()->add(function () {
79
+            $l = \OC::$server->getL10N('files_sharing');
80
+            return [
81
+                'id' => 'sharingout',
82
+                'appname' => 'files_sharing',
83
+                'script' => 'list.php',
84
+                'order' => 16,
85
+                'name' => $l->t('Shared with others'),
86
+            ];
87
+        });
88 88
 
89
-		// Check if sharing by link is enabled
90
-		if ($config->getAppValue('core', 'shareapi_allow_links', 'yes') === 'yes') {
91
-			\OCA\Files\App::getNavigationManager()->add(function () {
92
-				$l = \OC::$server->getL10N('files_sharing');
93
-				return [
94
-					'id' => 'sharinglinks',
95
-					'appname' => 'files_sharing',
96
-					'script' => 'list.php',
97
-					'order' => 17,
98
-					'name' => $l->t('Shared by link'),
99
-				];
100
-			});
101
-		}
102
-	}
89
+        // Check if sharing by link is enabled
90
+        if ($config->getAppValue('core', 'shareapi_allow_links', 'yes') === 'yes') {
91
+            \OCA\Files\App::getNavigationManager()->add(function () {
92
+                $l = \OC::$server->getL10N('files_sharing');
93
+                return [
94
+                    'id' => 'sharinglinks',
95
+                    'appname' => 'files_sharing',
96
+                    'script' => 'list.php',
97
+                    'order' => 17,
98
+                    'name' => $l->t('Shared by link'),
99
+                ];
100
+            });
101
+        }
102
+    }
103 103
 }
Please login to merge, or discard this patch.
lib/public/Share/IManager.php 1 patch
Indentation   +336 added lines, -336 removed lines patch added patch discarded remove patch
@@ -40,341 +40,341 @@
 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 a given share provider exists
374
-	 * @param int $shareType
375
-	 * @return bool
376
-	 * @since 11.0.0
377
-	 */
378
-	public function shareProviderExists($shareType);
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 a given share provider exists
374
+     * @param int $shareType
375
+     * @return bool
376
+     * @since 11.0.0
377
+     */
378
+    public function shareProviderExists($shareType);
379 379
 
380 380
 }
Please login to merge, or discard this patch.
lib/public/Share/IShareProvider.php 1 patch
Indentation   +180 added lines, -180 removed lines patch added patch discarded remove patch
@@ -37,184 +37,184 @@
 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);
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 220
 }
Please login to merge, or discard this patch.
apps/files_sharing/lib/Controller/ShareAPIController.php 1 patch
Indentation   +889 added lines, -889 removed lines patch added patch discarded remove patch
@@ -62,902 +62,902 @@
 block discarded – undo
62 62
  */
63 63
 class ShareAPIController extends OCSController {
64 64
 
65
-	/** @var IManager */
66
-	private $shareManager;
67
-	/** @var IGroupManager */
68
-	private $groupManager;
69
-	/** @var IUserManager */
70
-	private $userManager;
71
-	/** @var IRootFolder */
72
-	private $rootFolder;
73
-	/** @var IURLGenerator */
74
-	private $urlGenerator;
75
-	/** @var string */
76
-	private $currentUser;
77
-	/** @var IL10N */
78
-	private $l;
79
-	/** @var \OCP\Files\Node */
80
-	private $lockedNode;
81
-	/** @var IConfig */
82
-	private $config;
83
-
84
-	/**
85
-	 * Share20OCS constructor.
86
-	 *
87
-	 * @param string $appName
88
-	 * @param IRequest $request
89
-	 * @param IManager $shareManager
90
-	 * @param IGroupManager $groupManager
91
-	 * @param IUserManager $userManager
92
-	 * @param IRootFolder $rootFolder
93
-	 * @param IURLGenerator $urlGenerator
94
-	 * @param string $userId
95
-	 * @param IL10N $l10n
96
-	 * @param IConfig $config
97
-	 */
98
-	public function __construct(
99
-		string $appName,
100
-		IRequest $request,
101
-		IManager $shareManager,
102
-		IGroupManager $groupManager,
103
-		IUserManager $userManager,
104
-		IRootFolder $rootFolder,
105
-		IURLGenerator $urlGenerator,
106
-		string $userId,
107
-		IL10N $l10n,
108
-		IConfig $config
109
-	) {
110
-		parent::__construct($appName, $request);
111
-
112
-		$this->shareManager = $shareManager;
113
-		$this->userManager = $userManager;
114
-		$this->groupManager = $groupManager;
115
-		$this->request = $request;
116
-		$this->rootFolder = $rootFolder;
117
-		$this->urlGenerator = $urlGenerator;
118
-		$this->currentUser = $userId;
119
-		$this->l = $l10n;
120
-		$this->config = $config;
121
-	}
122
-
123
-	/**
124
-	 * Convert an IShare to an array for OCS output
125
-	 *
126
-	 * @param \OCP\Share\IShare $share
127
-	 * @param Node|null $recipientNode
128
-	 * @return array
129
-	 * @throws NotFoundException In case the node can't be resolved.
130
-	 */
131
-	protected function formatShare(\OCP\Share\IShare $share, Node $recipientNode = null): array {
132
-		$sharedBy = $this->userManager->get($share->getSharedBy());
133
-		$shareOwner = $this->userManager->get($share->getShareOwner());
134
-
135
-		$result = [
136
-			'id' => $share->getId(),
137
-			'share_type' => $share->getShareType(),
138
-			'uid_owner' => $share->getSharedBy(),
139
-			'displayname_owner' => $sharedBy !== null ? $sharedBy->getDisplayName() : $share->getSharedBy(),
140
-			'permissions' => $share->getPermissions(),
141
-			'stime' => $share->getShareTime()->getTimestamp(),
142
-			'parent' => null,
143
-			'expiration' => null,
144
-			'token' => null,
145
-			'uid_file_owner' => $share->getShareOwner(),
146
-			'displayname_file_owner' => $shareOwner !== null ? $shareOwner->getDisplayName() : $share->getShareOwner(),
147
-		];
148
-
149
-		$userFolder = $this->rootFolder->getUserFolder($this->currentUser);
150
-		if ($recipientNode) {
151
-			$node = $recipientNode;
152
-		} else {
153
-			$nodes = $userFolder->getById($share->getNodeId());
154
-			if (empty($nodes)) {
155
-				// fallback to guessing the path
156
-				$node = $userFolder->get($share->getTarget());
157
-				if ($node === null || $share->getTarget() === '') {
158
-					throw new NotFoundException();
159
-				}
160
-			} else {
161
-				$node = $nodes[0];
162
-			}
163
-		}
164
-
165
-		$result['path'] = $userFolder->getRelativePath($node->getPath());
166
-		if ($node instanceOf \OCP\Files\Folder) {
167
-			$result['item_type'] = 'folder';
168
-		} else {
169
-			$result['item_type'] = 'file';
170
-		}
171
-		$result['mimetype'] = $node->getMimetype();
172
-		$result['storage_id'] = $node->getStorage()->getId();
173
-		$result['storage'] = $node->getStorage()->getCache()->getNumericStorageId();
174
-		$result['item_source'] = $node->getId();
175
-		$result['file_source'] = $node->getId();
176
-		$result['file_parent'] = $node->getParent()->getId();
177
-		$result['file_target'] = $share->getTarget();
178
-
179
-		$expiration = $share->getExpirationDate();
180
-		if ($expiration !== null) {
181
-			$result['expiration'] = $expiration->format('Y-m-d 00:00:00');
182
-		}
183
-
184
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
185
-			$sharedWith = $this->userManager->get($share->getSharedWith());
186
-			$result['share_with'] = $share->getSharedWith();
187
-			$result['share_with_displayname'] = $sharedWith !== null ? $sharedWith->getDisplayName() : $share->getSharedWith();
188
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
189
-			$group = $this->groupManager->get($share->getSharedWith());
190
-			$result['share_with'] = $share->getSharedWith();
191
-			$result['share_with_displayname'] = $group !== null ? $group->getDisplayName() : $share->getSharedWith();
192
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
193
-
194
-			$result['share_with'] = $share->getPassword();
195
-			$result['share_with_displayname'] = $share->getPassword();
196
-
197
-			$result['token'] = $share->getToken();
198
-			$result['url'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $share->getToken()]);
199
-
200
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE) {
201
-			$result['share_with'] = $share->getSharedWith();
202
-			$result['share_with_displayname'] = $this->getDisplayNameFromAddressBook($share->getSharedWith(), 'CLOUD');
203
-			$result['token'] = $share->getToken();
204
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
205
-			$result['share_with'] = $share->getSharedWith();
206
-			$result['password'] = $share->getPassword();
207
-			$result['share_with_displayname'] = $this->getDisplayNameFromAddressBook($share->getSharedWith(), 'EMAIL');
208
-			$result['token'] = $share->getToken();
209
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_CIRCLE) {
210
-			// getSharedWith() returns either "name (type, owner)" or
211
-			// "name (type, owner) [id]", depending on the Circles app version.
212
-			$hasCircleId = (substr($share->getSharedWith(), -1) === ']');
213
-
214
-			$displayNameLength = ($hasCircleId? strrpos($share->getSharedWith(), ' '): strlen($share->getSharedWith()));
215
-			$result['share_with_displayname'] = substr($share->getSharedWith(), 0, $displayNameLength);
216
-
217
-			$shareWithStart = ($hasCircleId? strrpos($share->getSharedWith(), '[') + 1: 0);
218
-			$shareWithLength = ($hasCircleId? -1: strpos($share->getSharedWith(), ' '));
219
-			$result['share_with'] = substr($share->getSharedWith(), $shareWithStart, $shareWithLength);
220
-		}
221
-
222
-
223
-		$result['mail_send'] = $share->getMailSend() ? 1 : 0;
224
-
225
-		return $result;
226
-	}
227
-
228
-	/**
229
-	 * Check if one of the users address books knows the exact property, if
230
-	 * yes we return the full name.
231
-	 *
232
-	 * @param string $query
233
-	 * @param string $property
234
-	 * @return string
235
-	 */
236
-	private function getDisplayNameFromAddressBook(string $query, string $property): string {
237
-		// FIXME: If we inject the contacts manager it gets initialized bofore any address books are registered
238
-		$result = \OC::$server->getContactsManager()->search($query, [$property]);
239
-		foreach ($result as $r) {
240
-			foreach($r[$property] as $value) {
241
-				if ($value === $query) {
242
-					return $r['FN'];
243
-				}
244
-			}
245
-		}
246
-
247
-		return $query;
248
-	}
249
-
250
-	/**
251
-	 * Get a specific share by id
252
-	 *
253
-	 * @NoAdminRequired
254
-	 *
255
-	 * @param string $id
256
-	 * @return DataResponse
257
-	 * @throws OCSNotFoundException
258
-	 */
259
-	public function getShare(string $id): DataResponse {
260
-		try {
261
-			$share = $this->getShareById($id);
262
-		} catch (ShareNotFound $e) {
263
-			throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
264
-		}
265
-
266
-		if ($this->canAccessShare($share)) {
267
-			try {
268
-				$share = $this->formatShare($share);
269
-				return new DataResponse([$share]);
270
-			} catch (NotFoundException $e) {
271
-				//Fall trough
272
-			}
273
-		}
274
-
275
-		throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
276
-	}
277
-
278
-	/**
279
-	 * Delete a share
280
-	 *
281
-	 * @NoAdminRequired
282
-	 *
283
-	 * @param string $id
284
-	 * @return DataResponse
285
-	 * @throws OCSNotFoundException
286
-	 */
287
-	public function deleteShare(string $id): DataResponse {
288
-		try {
289
-			$share = $this->getShareById($id);
290
-		} catch (ShareNotFound $e) {
291
-			throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
292
-		}
293
-
294
-		try {
295
-			$this->lock($share->getNode());
296
-		} catch (LockedException $e) {
297
-			throw new OCSNotFoundException($this->l->t('could not delete share'));
298
-		}
299
-
300
-		if (!$this->canAccessShare($share)) {
301
-			throw new OCSNotFoundException($this->l->t('Could not delete share'));
302
-		}
303
-
304
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP &&
305
-			$share->getShareOwner() !== $this->currentUser &&
306
-			$share->getSharedBy() !== $this->currentUser) {
307
-			$this->shareManager->deleteFromSelf($share, $this->currentUser);
308
-		} else {
309
-			$this->shareManager->deleteShare($share);
310
-		}
311
-
312
-		return new DataResponse();
313
-	}
314
-
315
-	/**
316
-	 * @NoAdminRequired
317
-	 *
318
-	 * @param string $path
319
-	 * @param int $permissions
320
-	 * @param int $shareType
321
-	 * @param string $shareWith
322
-	 * @param string $publicUpload
323
-	 * @param string $password
324
-	 * @param string $expireDate
325
-	 *
326
-	 * @return DataResponse
327
-	 * @throws OCSNotFoundException
328
-	 * @throws OCSForbiddenException
329
-	 * @throws OCSBadRequestException
330
-	 * @throws OCSException
331
-	 *
332
-	 * @suppress PhanUndeclaredClassMethod
333
-	 */
334
-	public function createShare(
335
-		string $path = null,
336
-		int $permissions = null,
337
-		int $shareType = -1,
338
-		string $shareWith = null,
339
-		string $publicUpload = 'false',
340
-		string $password = '',
341
-		string $expireDate = ''
342
-	): DataResponse {
343
-		$share = $this->shareManager->newShare();
344
-
345
-		if ($permissions === null) {
346
-			$permissions = $this->config->getAppValue('core', 'shareapi_default_permissions', Constants::PERMISSION_ALL);
347
-		}
348
-
349
-		// Verify path
350
-		if ($path === null) {
351
-			throw new OCSNotFoundException($this->l->t('Please specify a file or folder path'));
352
-		}
353
-
354
-		$userFolder = $this->rootFolder->getUserFolder($this->currentUser);
355
-		try {
356
-			$path = $userFolder->get($path);
357
-		} catch (NotFoundException $e) {
358
-			throw new OCSNotFoundException($this->l->t('Wrong path, file/folder doesn\'t exist'));
359
-		}
360
-
361
-		$share->setNode($path);
362
-
363
-		try {
364
-			$this->lock($share->getNode());
365
-		} catch (LockedException $e) {
366
-			throw new OCSNotFoundException($this->l->t('Could not create share'));
367
-		}
368
-
369
-		if ($permissions < 0 || $permissions > Constants::PERMISSION_ALL) {
370
-			throw new OCSNotFoundException($this->l->t('invalid permissions'));
371
-		}
372
-
373
-		// Shares always require read permissions
374
-		$permissions |= Constants::PERMISSION_READ;
375
-
376
-		if ($path instanceof \OCP\Files\File) {
377
-			// Single file shares should never have delete or create permissions
378
-			$permissions &= ~Constants::PERMISSION_DELETE;
379
-			$permissions &= ~Constants::PERMISSION_CREATE;
380
-		}
381
-
382
-		/*
65
+    /** @var IManager */
66
+    private $shareManager;
67
+    /** @var IGroupManager */
68
+    private $groupManager;
69
+    /** @var IUserManager */
70
+    private $userManager;
71
+    /** @var IRootFolder */
72
+    private $rootFolder;
73
+    /** @var IURLGenerator */
74
+    private $urlGenerator;
75
+    /** @var string */
76
+    private $currentUser;
77
+    /** @var IL10N */
78
+    private $l;
79
+    /** @var \OCP\Files\Node */
80
+    private $lockedNode;
81
+    /** @var IConfig */
82
+    private $config;
83
+
84
+    /**
85
+     * Share20OCS constructor.
86
+     *
87
+     * @param string $appName
88
+     * @param IRequest $request
89
+     * @param IManager $shareManager
90
+     * @param IGroupManager $groupManager
91
+     * @param IUserManager $userManager
92
+     * @param IRootFolder $rootFolder
93
+     * @param IURLGenerator $urlGenerator
94
+     * @param string $userId
95
+     * @param IL10N $l10n
96
+     * @param IConfig $config
97
+     */
98
+    public function __construct(
99
+        string $appName,
100
+        IRequest $request,
101
+        IManager $shareManager,
102
+        IGroupManager $groupManager,
103
+        IUserManager $userManager,
104
+        IRootFolder $rootFolder,
105
+        IURLGenerator $urlGenerator,
106
+        string $userId,
107
+        IL10N $l10n,
108
+        IConfig $config
109
+    ) {
110
+        parent::__construct($appName, $request);
111
+
112
+        $this->shareManager = $shareManager;
113
+        $this->userManager = $userManager;
114
+        $this->groupManager = $groupManager;
115
+        $this->request = $request;
116
+        $this->rootFolder = $rootFolder;
117
+        $this->urlGenerator = $urlGenerator;
118
+        $this->currentUser = $userId;
119
+        $this->l = $l10n;
120
+        $this->config = $config;
121
+    }
122
+
123
+    /**
124
+     * Convert an IShare to an array for OCS output
125
+     *
126
+     * @param \OCP\Share\IShare $share
127
+     * @param Node|null $recipientNode
128
+     * @return array
129
+     * @throws NotFoundException In case the node can't be resolved.
130
+     */
131
+    protected function formatShare(\OCP\Share\IShare $share, Node $recipientNode = null): array {
132
+        $sharedBy = $this->userManager->get($share->getSharedBy());
133
+        $shareOwner = $this->userManager->get($share->getShareOwner());
134
+
135
+        $result = [
136
+            'id' => $share->getId(),
137
+            'share_type' => $share->getShareType(),
138
+            'uid_owner' => $share->getSharedBy(),
139
+            'displayname_owner' => $sharedBy !== null ? $sharedBy->getDisplayName() : $share->getSharedBy(),
140
+            'permissions' => $share->getPermissions(),
141
+            'stime' => $share->getShareTime()->getTimestamp(),
142
+            'parent' => null,
143
+            'expiration' => null,
144
+            'token' => null,
145
+            'uid_file_owner' => $share->getShareOwner(),
146
+            'displayname_file_owner' => $shareOwner !== null ? $shareOwner->getDisplayName() : $share->getShareOwner(),
147
+        ];
148
+
149
+        $userFolder = $this->rootFolder->getUserFolder($this->currentUser);
150
+        if ($recipientNode) {
151
+            $node = $recipientNode;
152
+        } else {
153
+            $nodes = $userFolder->getById($share->getNodeId());
154
+            if (empty($nodes)) {
155
+                // fallback to guessing the path
156
+                $node = $userFolder->get($share->getTarget());
157
+                if ($node === null || $share->getTarget() === '') {
158
+                    throw new NotFoundException();
159
+                }
160
+            } else {
161
+                $node = $nodes[0];
162
+            }
163
+        }
164
+
165
+        $result['path'] = $userFolder->getRelativePath($node->getPath());
166
+        if ($node instanceOf \OCP\Files\Folder) {
167
+            $result['item_type'] = 'folder';
168
+        } else {
169
+            $result['item_type'] = 'file';
170
+        }
171
+        $result['mimetype'] = $node->getMimetype();
172
+        $result['storage_id'] = $node->getStorage()->getId();
173
+        $result['storage'] = $node->getStorage()->getCache()->getNumericStorageId();
174
+        $result['item_source'] = $node->getId();
175
+        $result['file_source'] = $node->getId();
176
+        $result['file_parent'] = $node->getParent()->getId();
177
+        $result['file_target'] = $share->getTarget();
178
+
179
+        $expiration = $share->getExpirationDate();
180
+        if ($expiration !== null) {
181
+            $result['expiration'] = $expiration->format('Y-m-d 00:00:00');
182
+        }
183
+
184
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
185
+            $sharedWith = $this->userManager->get($share->getSharedWith());
186
+            $result['share_with'] = $share->getSharedWith();
187
+            $result['share_with_displayname'] = $sharedWith !== null ? $sharedWith->getDisplayName() : $share->getSharedWith();
188
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
189
+            $group = $this->groupManager->get($share->getSharedWith());
190
+            $result['share_with'] = $share->getSharedWith();
191
+            $result['share_with_displayname'] = $group !== null ? $group->getDisplayName() : $share->getSharedWith();
192
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
193
+
194
+            $result['share_with'] = $share->getPassword();
195
+            $result['share_with_displayname'] = $share->getPassword();
196
+
197
+            $result['token'] = $share->getToken();
198
+            $result['url'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $share->getToken()]);
199
+
200
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE) {
201
+            $result['share_with'] = $share->getSharedWith();
202
+            $result['share_with_displayname'] = $this->getDisplayNameFromAddressBook($share->getSharedWith(), 'CLOUD');
203
+            $result['token'] = $share->getToken();
204
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
205
+            $result['share_with'] = $share->getSharedWith();
206
+            $result['password'] = $share->getPassword();
207
+            $result['share_with_displayname'] = $this->getDisplayNameFromAddressBook($share->getSharedWith(), 'EMAIL');
208
+            $result['token'] = $share->getToken();
209
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_CIRCLE) {
210
+            // getSharedWith() returns either "name (type, owner)" or
211
+            // "name (type, owner) [id]", depending on the Circles app version.
212
+            $hasCircleId = (substr($share->getSharedWith(), -1) === ']');
213
+
214
+            $displayNameLength = ($hasCircleId? strrpos($share->getSharedWith(), ' '): strlen($share->getSharedWith()));
215
+            $result['share_with_displayname'] = substr($share->getSharedWith(), 0, $displayNameLength);
216
+
217
+            $shareWithStart = ($hasCircleId? strrpos($share->getSharedWith(), '[') + 1: 0);
218
+            $shareWithLength = ($hasCircleId? -1: strpos($share->getSharedWith(), ' '));
219
+            $result['share_with'] = substr($share->getSharedWith(), $shareWithStart, $shareWithLength);
220
+        }
221
+
222
+
223
+        $result['mail_send'] = $share->getMailSend() ? 1 : 0;
224
+
225
+        return $result;
226
+    }
227
+
228
+    /**
229
+     * Check if one of the users address books knows the exact property, if
230
+     * yes we return the full name.
231
+     *
232
+     * @param string $query
233
+     * @param string $property
234
+     * @return string
235
+     */
236
+    private function getDisplayNameFromAddressBook(string $query, string $property): string {
237
+        // FIXME: If we inject the contacts manager it gets initialized bofore any address books are registered
238
+        $result = \OC::$server->getContactsManager()->search($query, [$property]);
239
+        foreach ($result as $r) {
240
+            foreach($r[$property] as $value) {
241
+                if ($value === $query) {
242
+                    return $r['FN'];
243
+                }
244
+            }
245
+        }
246
+
247
+        return $query;
248
+    }
249
+
250
+    /**
251
+     * Get a specific share by id
252
+     *
253
+     * @NoAdminRequired
254
+     *
255
+     * @param string $id
256
+     * @return DataResponse
257
+     * @throws OCSNotFoundException
258
+     */
259
+    public function getShare(string $id): DataResponse {
260
+        try {
261
+            $share = $this->getShareById($id);
262
+        } catch (ShareNotFound $e) {
263
+            throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
264
+        }
265
+
266
+        if ($this->canAccessShare($share)) {
267
+            try {
268
+                $share = $this->formatShare($share);
269
+                return new DataResponse([$share]);
270
+            } catch (NotFoundException $e) {
271
+                //Fall trough
272
+            }
273
+        }
274
+
275
+        throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
276
+    }
277
+
278
+    /**
279
+     * Delete a share
280
+     *
281
+     * @NoAdminRequired
282
+     *
283
+     * @param string $id
284
+     * @return DataResponse
285
+     * @throws OCSNotFoundException
286
+     */
287
+    public function deleteShare(string $id): DataResponse {
288
+        try {
289
+            $share = $this->getShareById($id);
290
+        } catch (ShareNotFound $e) {
291
+            throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
292
+        }
293
+
294
+        try {
295
+            $this->lock($share->getNode());
296
+        } catch (LockedException $e) {
297
+            throw new OCSNotFoundException($this->l->t('could not delete share'));
298
+        }
299
+
300
+        if (!$this->canAccessShare($share)) {
301
+            throw new OCSNotFoundException($this->l->t('Could not delete share'));
302
+        }
303
+
304
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP &&
305
+            $share->getShareOwner() !== $this->currentUser &&
306
+            $share->getSharedBy() !== $this->currentUser) {
307
+            $this->shareManager->deleteFromSelf($share, $this->currentUser);
308
+        } else {
309
+            $this->shareManager->deleteShare($share);
310
+        }
311
+
312
+        return new DataResponse();
313
+    }
314
+
315
+    /**
316
+     * @NoAdminRequired
317
+     *
318
+     * @param string $path
319
+     * @param int $permissions
320
+     * @param int $shareType
321
+     * @param string $shareWith
322
+     * @param string $publicUpload
323
+     * @param string $password
324
+     * @param string $expireDate
325
+     *
326
+     * @return DataResponse
327
+     * @throws OCSNotFoundException
328
+     * @throws OCSForbiddenException
329
+     * @throws OCSBadRequestException
330
+     * @throws OCSException
331
+     *
332
+     * @suppress PhanUndeclaredClassMethod
333
+     */
334
+    public function createShare(
335
+        string $path = null,
336
+        int $permissions = null,
337
+        int $shareType = -1,
338
+        string $shareWith = null,
339
+        string $publicUpload = 'false',
340
+        string $password = '',
341
+        string $expireDate = ''
342
+    ): DataResponse {
343
+        $share = $this->shareManager->newShare();
344
+
345
+        if ($permissions === null) {
346
+            $permissions = $this->config->getAppValue('core', 'shareapi_default_permissions', Constants::PERMISSION_ALL);
347
+        }
348
+
349
+        // Verify path
350
+        if ($path === null) {
351
+            throw new OCSNotFoundException($this->l->t('Please specify a file or folder path'));
352
+        }
353
+
354
+        $userFolder = $this->rootFolder->getUserFolder($this->currentUser);
355
+        try {
356
+            $path = $userFolder->get($path);
357
+        } catch (NotFoundException $e) {
358
+            throw new OCSNotFoundException($this->l->t('Wrong path, file/folder doesn\'t exist'));
359
+        }
360
+
361
+        $share->setNode($path);
362
+
363
+        try {
364
+            $this->lock($share->getNode());
365
+        } catch (LockedException $e) {
366
+            throw new OCSNotFoundException($this->l->t('Could not create share'));
367
+        }
368
+
369
+        if ($permissions < 0 || $permissions > Constants::PERMISSION_ALL) {
370
+            throw new OCSNotFoundException($this->l->t('invalid permissions'));
371
+        }
372
+
373
+        // Shares always require read permissions
374
+        $permissions |= Constants::PERMISSION_READ;
375
+
376
+        if ($path instanceof \OCP\Files\File) {
377
+            // Single file shares should never have delete or create permissions
378
+            $permissions &= ~Constants::PERMISSION_DELETE;
379
+            $permissions &= ~Constants::PERMISSION_CREATE;
380
+        }
381
+
382
+        /*
383 383
 		 * Hack for https://github.com/owncloud/core/issues/22587
384 384
 		 * We check the permissions via webdav. But the permissions of the mount point
385 385
 		 * do not equal the share permissions. Here we fix that for federated mounts.
386 386
 		 */
387
-		if ($path->getStorage()->instanceOfStorage(Storage::class)) {
388
-			$permissions &= ~($permissions & ~$path->getPermissions());
389
-		}
390
-
391
-		if ($shareType === \OCP\Share::SHARE_TYPE_USER) {
392
-			// Valid user is required to share
393
-			if ($shareWith === null || !$this->userManager->userExists($shareWith)) {
394
-				throw new OCSNotFoundException($this->l->t('Please specify a valid user'));
395
-			}
396
-			$share->setSharedWith($shareWith);
397
-			$share->setPermissions($permissions);
398
-		} else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
399
-			if (!$this->shareManager->allowGroupSharing()) {
400
-				throw new OCSNotFoundException($this->l->t('Group sharing is disabled by the administrator'));
401
-			}
402
-
403
-			// Valid group is required to share
404
-			if ($shareWith === null || !$this->groupManager->groupExists($shareWith)) {
405
-				throw new OCSNotFoundException($this->l->t('Please specify a valid group'));
406
-			}
407
-			$share->setSharedWith($shareWith);
408
-			$share->setPermissions($permissions);
409
-		} else if ($shareType === \OCP\Share::SHARE_TYPE_LINK) {
410
-			//Can we even share links?
411
-			if (!$this->shareManager->shareApiAllowLinks()) {
412
-				throw new OCSNotFoundException($this->l->t('Public link sharing is disabled by the administrator'));
413
-			}
414
-
415
-			/*
387
+        if ($path->getStorage()->instanceOfStorage(Storage::class)) {
388
+            $permissions &= ~($permissions & ~$path->getPermissions());
389
+        }
390
+
391
+        if ($shareType === \OCP\Share::SHARE_TYPE_USER) {
392
+            // Valid user is required to share
393
+            if ($shareWith === null || !$this->userManager->userExists($shareWith)) {
394
+                throw new OCSNotFoundException($this->l->t('Please specify a valid user'));
395
+            }
396
+            $share->setSharedWith($shareWith);
397
+            $share->setPermissions($permissions);
398
+        } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
399
+            if (!$this->shareManager->allowGroupSharing()) {
400
+                throw new OCSNotFoundException($this->l->t('Group sharing is disabled by the administrator'));
401
+            }
402
+
403
+            // Valid group is required to share
404
+            if ($shareWith === null || !$this->groupManager->groupExists($shareWith)) {
405
+                throw new OCSNotFoundException($this->l->t('Please specify a valid group'));
406
+            }
407
+            $share->setSharedWith($shareWith);
408
+            $share->setPermissions($permissions);
409
+        } else if ($shareType === \OCP\Share::SHARE_TYPE_LINK) {
410
+            //Can we even share links?
411
+            if (!$this->shareManager->shareApiAllowLinks()) {
412
+                throw new OCSNotFoundException($this->l->t('Public link sharing is disabled by the administrator'));
413
+            }
414
+
415
+            /*
416 416
 			 * For now we only allow 1 link share.
417 417
 			 * Return the existing link share if this is a duplicate
418 418
 			 */
419
-			$existingShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_LINK, $path, false, 1, 0);
420
-			if (!empty($existingShares)) {
421
-				return new DataResponse($this->formatShare($existingShares[0]));
422
-			}
423
-
424
-			if ($publicUpload === 'true') {
425
-				// Check if public upload is allowed
426
-				if (!$this->shareManager->shareApiLinkAllowPublicUpload()) {
427
-					throw new OCSForbiddenException($this->l->t('Public upload disabled by the administrator'));
428
-				}
429
-
430
-				// Public upload can only be set for folders
431
-				if ($path instanceof \OCP\Files\File) {
432
-					throw new OCSNotFoundException($this->l->t('Public upload is only possible for publicly shared folders'));
433
-				}
434
-
435
-				$share->setPermissions(
436
-					Constants::PERMISSION_READ |
437
-					Constants::PERMISSION_CREATE |
438
-					Constants::PERMISSION_UPDATE |
439
-					Constants::PERMISSION_DELETE
440
-				);
441
-			} else {
442
-				$share->setPermissions(Constants::PERMISSION_READ);
443
-			}
444
-
445
-			// Set password
446
-			if ($password !== '') {
447
-				$share->setPassword($password);
448
-			}
449
-
450
-			//Expire date
451
-			if ($expireDate !== '') {
452
-				try {
453
-					$expireDate = $this->parseDate($expireDate);
454
-					$share->setExpirationDate($expireDate);
455
-				} catch (\Exception $e) {
456
-					throw new OCSNotFoundException($this->l->t('Invalid date, date format must be YYYY-MM-DD'));
457
-				}
458
-			}
459
-
460
-		} else if ($shareType === \OCP\Share::SHARE_TYPE_REMOTE) {
461
-			if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) {
462
-				throw new OCSForbiddenException($this->l->t('Sharing %s failed because the back end does not allow shares from type %s', [$path->getPath(), $shareType]));
463
-			}
464
-
465
-			$share->setSharedWith($shareWith);
466
-			$share->setPermissions($permissions);
467
-		} else if ($shareType === \OCP\Share::SHARE_TYPE_EMAIL) {
468
-			if ($share->getNodeType() === 'file') {
469
-				$share->setPermissions(Constants::PERMISSION_READ);
470
-			} else {
471
-				$share->setPermissions($permissions);
472
-			}
473
-			$share->setSharedWith($shareWith);
474
-		} else if ($shareType === \OCP\Share::SHARE_TYPE_CIRCLE) {
475
-			if (!\OC::$server->getAppManager()->isEnabledForUser('circles') || !class_exists('\OCA\Circles\ShareByCircleProvider')) {
476
-				throw new OCSNotFoundException($this->l->t('You cannot share to a Circle if the app is not enabled'));
477
-			}
478
-
479
-			$circle = \OCA\Circles\Api\v1\Circles::detailsCircle($shareWith);
480
-
481
-			// Valid circle is required to share
482
-			if ($circle === null) {
483
-				throw new OCSNotFoundException($this->l->t('Please specify a valid circle'));
484
-			}
485
-			$share->setSharedWith($shareWith);
486
-			$share->setPermissions($permissions);
487
-		} else {
488
-			throw new OCSBadRequestException($this->l->t('Unknown share type'));
489
-		}
490
-
491
-		$share->setShareType($shareType);
492
-		$share->setSharedBy($this->currentUser);
493
-
494
-		try {
495
-			$share = $this->shareManager->createShare($share);
496
-		} catch (GenericShareException $e) {
497
-			$code = $e->getCode() === 0 ? 403 : $e->getCode();
498
-			throw new OCSException($e->getHint(), $code);
499
-		} catch (\Exception $e) {
500
-			throw new OCSForbiddenException($e->getMessage(), $e);
501
-		}
502
-
503
-		$output = $this->formatShare($share);
504
-
505
-		return new DataResponse($output);
506
-	}
507
-
508
-	/**
509
-	 * @param \OCP\Files\File|\OCP\Files\Folder $node
510
-	 * @param boolean $includeTags
511
-	 * @return DataResponse
512
-	 */
513
-	private function getSharedWithMe($node = null, bool $includeTags): DataResponse {
514
-
515
-		$userShares = $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_USER, $node, -1, 0);
516
-		$groupShares = $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_GROUP, $node, -1, 0);
517
-		$circleShares = $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_CIRCLE, $node, -1, 0);
518
-
519
-		$shares = array_merge($userShares, $groupShares, $circleShares);
520
-
521
-		$shares = array_filter($shares, function (IShare $share) {
522
-			return $share->getShareOwner() !== $this->currentUser;
523
-		});
524
-
525
-		$formatted = [];
526
-		foreach ($shares as $share) {
527
-			if ($this->canAccessShare($share)) {
528
-				try {
529
-					$formatted[] = $this->formatShare($share);
530
-				} catch (NotFoundException $e) {
531
-					// Ignore this share
532
-				}
533
-			}
534
-		}
535
-
536
-		if ($includeTags) {
537
-			$formatted = Helper::populateTags($formatted, 'file_source', \OC::$server->getTagManager());
538
-		}
539
-
540
-		return new DataResponse($formatted);
541
-	}
542
-
543
-	/**
544
-	 * @param \OCP\Files\Folder $folder
545
-	 * @return DataResponse
546
-	 * @throws OCSBadRequestException
547
-	 */
548
-	private function getSharesInDir(Node $folder): DataResponse {
549
-		if (!($folder instanceof \OCP\Files\Folder)) {
550
-			throw new OCSBadRequestException($this->l->t('Not a directory'));
551
-		}
552
-
553
-		$nodes = $folder->getDirectoryListing();
554
-		/** @var \OCP\Share\IShare[] $shares */
555
-		$shares = [];
556
-		foreach ($nodes as $node) {
557
-			$shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_USER, $node, false, -1, 0));
558
-			$shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_GROUP, $node, false, -1, 0));
559
-			$shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_LINK, $node, false, -1, 0));
560
-			if($this->shareManager->shareProviderExists(\OCP\Share::SHARE_TYPE_EMAIL)) {
561
-				$shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_EMAIL, $node, false, -1, 0));
562
-			}
563
-			if ($this->shareManager->outgoingServer2ServerSharesAllowed()) {
564
-				$shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_REMOTE, $node, false, -1, 0));
565
-			}
566
-		}
567
-
568
-		$formatted = [];
569
-		foreach ($shares as $share) {
570
-			try {
571
-				$formatted[] = $this->formatShare($share);
572
-			} catch (NotFoundException $e) {
573
-				//Ignore this share
574
-			}
575
-		}
576
-
577
-		return new DataResponse($formatted);
578
-	}
579
-
580
-	/**
581
-	 * The getShares function.
582
-	 *
583
-	 * @NoAdminRequired
584
-	 *
585
-	 * @param string $shared_with_me
586
-	 * @param string $reshares
587
-	 * @param string $subfiles
588
-	 * @param string $path
589
-	 *
590
-	 * - Get shares by the current user
591
-	 * - Get shares by the current user and reshares (?reshares=true)
592
-	 * - Get shares with the current user (?shared_with_me=true)
593
-	 * - Get shares for a specific path (?path=...)
594
-	 * - Get all shares in a folder (?subfiles=true&path=..)
595
-	 *
596
-	 * @return DataResponse
597
-	 * @throws OCSNotFoundException
598
-	 */
599
-	public function getShares(
600
-		string $shared_with_me = 'false',
601
-		string $reshares = 'false',
602
-		string $subfiles = 'false',
603
-		string $path = null,
604
-		string $include_tags = 'false'
605
-	): DataResponse {
606
-
607
-		if ($path !== null) {
608
-			$userFolder = $this->rootFolder->getUserFolder($this->currentUser);
609
-			try {
610
-				$path = $userFolder->get($path);
611
-				$this->lock($path);
612
-			} catch (\OCP\Files\NotFoundException $e) {
613
-				throw new OCSNotFoundException($this->l->t('Wrong path, file/folder doesn\'t exist'));
614
-			} catch (LockedException $e) {
615
-				throw new OCSNotFoundException($this->l->t('Could not lock path'));
616
-			}
617
-		}
618
-
619
-		$include_tags = $include_tags === 'true';
620
-
621
-		if ($shared_with_me === 'true') {
622
-			$result = $this->getSharedWithMe($path, $include_tags);
623
-			return $result;
624
-		}
625
-
626
-		if ($subfiles === 'true') {
627
-			$result = $this->getSharesInDir($path);
628
-			return $result;
629
-		}
630
-
631
-		if ($reshares === 'true') {
632
-			$reshares = true;
633
-		} else {
634
-			$reshares = false;
635
-		}
636
-
637
-		// Get all shares
638
-		$userShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_USER, $path, $reshares, -1, 0);
639
-		$groupShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_GROUP, $path, $reshares, -1, 0);
640
-		$linkShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_LINK, $path, $reshares, -1, 0);
641
-		if ($this->shareManager->shareProviderExists(\OCP\Share::SHARE_TYPE_EMAIL)) {
642
-			$mailShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_EMAIL, $path, $reshares, -1, 0);
643
-		} else {
644
-			$mailShares = [];
645
-		}
646
-		if ($this->shareManager->shareProviderExists(\OCP\Share::SHARE_TYPE_CIRCLE)) {
647
-			$circleShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_CIRCLE, $path, $reshares, -1, 0);
648
-		} else {
649
-			$circleShares = [];
650
-		}
651
-
652
-		$shares = array_merge($userShares, $groupShares, $linkShares, $mailShares, $circleShares);
653
-
654
-		if ($this->shareManager->outgoingServer2ServerSharesAllowed()) {
655
-			$federatedShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_REMOTE, $path, $reshares, -1, 0);
656
-			$shares = array_merge($shares, $federatedShares);
657
-		}
658
-
659
-		$formatted = [];
660
-		foreach ($shares as $share) {
661
-			try {
662
-				$formatted[] = $this->formatShare($share, $path);
663
-			} catch (NotFoundException $e) {
664
-				//Ignore share
665
-			}
666
-		}
667
-
668
-		if ($include_tags) {
669
-			$formatted = Helper::populateTags($formatted, 'file_source', \OC::$server->getTagManager());
670
-		}
671
-
672
-		return new DataResponse($formatted);
673
-	}
674
-
675
-	/**
676
-	 * @NoAdminRequired
677
-	 *
678
-	 * @param string $id
679
-	 * @param int $permissions
680
-	 * @param string $password
681
-	 * @param string $publicUpload
682
-	 * @param string $expireDate
683
-	 * @return DataResponse
684
-	 * @throws OCSNotFoundException
685
-	 * @throws OCSBadRequestException
686
-	 * @throws OCSForbiddenException
687
-	 */
688
-	public function updateShare(
689
-		string $id,
690
-		int $permissions = null,
691
-		string $password = null,
692
-		string $publicUpload = null,
693
-		string $expireDate = null
694
-	): DataResponse {
695
-		try {
696
-			$share = $this->getShareById($id);
697
-		} catch (ShareNotFound $e) {
698
-			throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
699
-		}
700
-
701
-		$this->lock($share->getNode());
702
-
703
-		if (!$this->canAccessShare($share, false)) {
704
-			throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
705
-		}
706
-
707
-		if ($permissions === null && $password === null && $publicUpload === null && $expireDate === null) {
708
-			throw new OCSBadRequestException($this->l->t('Wrong or no update parameter given'));
709
-		}
710
-
711
-		/*
419
+            $existingShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_LINK, $path, false, 1, 0);
420
+            if (!empty($existingShares)) {
421
+                return new DataResponse($this->formatShare($existingShares[0]));
422
+            }
423
+
424
+            if ($publicUpload === 'true') {
425
+                // Check if public upload is allowed
426
+                if (!$this->shareManager->shareApiLinkAllowPublicUpload()) {
427
+                    throw new OCSForbiddenException($this->l->t('Public upload disabled by the administrator'));
428
+                }
429
+
430
+                // Public upload can only be set for folders
431
+                if ($path instanceof \OCP\Files\File) {
432
+                    throw new OCSNotFoundException($this->l->t('Public upload is only possible for publicly shared folders'));
433
+                }
434
+
435
+                $share->setPermissions(
436
+                    Constants::PERMISSION_READ |
437
+                    Constants::PERMISSION_CREATE |
438
+                    Constants::PERMISSION_UPDATE |
439
+                    Constants::PERMISSION_DELETE
440
+                );
441
+            } else {
442
+                $share->setPermissions(Constants::PERMISSION_READ);
443
+            }
444
+
445
+            // Set password
446
+            if ($password !== '') {
447
+                $share->setPassword($password);
448
+            }
449
+
450
+            //Expire date
451
+            if ($expireDate !== '') {
452
+                try {
453
+                    $expireDate = $this->parseDate($expireDate);
454
+                    $share->setExpirationDate($expireDate);
455
+                } catch (\Exception $e) {
456
+                    throw new OCSNotFoundException($this->l->t('Invalid date, date format must be YYYY-MM-DD'));
457
+                }
458
+            }
459
+
460
+        } else if ($shareType === \OCP\Share::SHARE_TYPE_REMOTE) {
461
+            if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) {
462
+                throw new OCSForbiddenException($this->l->t('Sharing %s failed because the back end does not allow shares from type %s', [$path->getPath(), $shareType]));
463
+            }
464
+
465
+            $share->setSharedWith($shareWith);
466
+            $share->setPermissions($permissions);
467
+        } else if ($shareType === \OCP\Share::SHARE_TYPE_EMAIL) {
468
+            if ($share->getNodeType() === 'file') {
469
+                $share->setPermissions(Constants::PERMISSION_READ);
470
+            } else {
471
+                $share->setPermissions($permissions);
472
+            }
473
+            $share->setSharedWith($shareWith);
474
+        } else if ($shareType === \OCP\Share::SHARE_TYPE_CIRCLE) {
475
+            if (!\OC::$server->getAppManager()->isEnabledForUser('circles') || !class_exists('\OCA\Circles\ShareByCircleProvider')) {
476
+                throw new OCSNotFoundException($this->l->t('You cannot share to a Circle if the app is not enabled'));
477
+            }
478
+
479
+            $circle = \OCA\Circles\Api\v1\Circles::detailsCircle($shareWith);
480
+
481
+            // Valid circle is required to share
482
+            if ($circle === null) {
483
+                throw new OCSNotFoundException($this->l->t('Please specify a valid circle'));
484
+            }
485
+            $share->setSharedWith($shareWith);
486
+            $share->setPermissions($permissions);
487
+        } else {
488
+            throw new OCSBadRequestException($this->l->t('Unknown share type'));
489
+        }
490
+
491
+        $share->setShareType($shareType);
492
+        $share->setSharedBy($this->currentUser);
493
+
494
+        try {
495
+            $share = $this->shareManager->createShare($share);
496
+        } catch (GenericShareException $e) {
497
+            $code = $e->getCode() === 0 ? 403 : $e->getCode();
498
+            throw new OCSException($e->getHint(), $code);
499
+        } catch (\Exception $e) {
500
+            throw new OCSForbiddenException($e->getMessage(), $e);
501
+        }
502
+
503
+        $output = $this->formatShare($share);
504
+
505
+        return new DataResponse($output);
506
+    }
507
+
508
+    /**
509
+     * @param \OCP\Files\File|\OCP\Files\Folder $node
510
+     * @param boolean $includeTags
511
+     * @return DataResponse
512
+     */
513
+    private function getSharedWithMe($node = null, bool $includeTags): DataResponse {
514
+
515
+        $userShares = $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_USER, $node, -1, 0);
516
+        $groupShares = $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_GROUP, $node, -1, 0);
517
+        $circleShares = $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_CIRCLE, $node, -1, 0);
518
+
519
+        $shares = array_merge($userShares, $groupShares, $circleShares);
520
+
521
+        $shares = array_filter($shares, function (IShare $share) {
522
+            return $share->getShareOwner() !== $this->currentUser;
523
+        });
524
+
525
+        $formatted = [];
526
+        foreach ($shares as $share) {
527
+            if ($this->canAccessShare($share)) {
528
+                try {
529
+                    $formatted[] = $this->formatShare($share);
530
+                } catch (NotFoundException $e) {
531
+                    // Ignore this share
532
+                }
533
+            }
534
+        }
535
+
536
+        if ($includeTags) {
537
+            $formatted = Helper::populateTags($formatted, 'file_source', \OC::$server->getTagManager());
538
+        }
539
+
540
+        return new DataResponse($formatted);
541
+    }
542
+
543
+    /**
544
+     * @param \OCP\Files\Folder $folder
545
+     * @return DataResponse
546
+     * @throws OCSBadRequestException
547
+     */
548
+    private function getSharesInDir(Node $folder): DataResponse {
549
+        if (!($folder instanceof \OCP\Files\Folder)) {
550
+            throw new OCSBadRequestException($this->l->t('Not a directory'));
551
+        }
552
+
553
+        $nodes = $folder->getDirectoryListing();
554
+        /** @var \OCP\Share\IShare[] $shares */
555
+        $shares = [];
556
+        foreach ($nodes as $node) {
557
+            $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_USER, $node, false, -1, 0));
558
+            $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_GROUP, $node, false, -1, 0));
559
+            $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_LINK, $node, false, -1, 0));
560
+            if($this->shareManager->shareProviderExists(\OCP\Share::SHARE_TYPE_EMAIL)) {
561
+                $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_EMAIL, $node, false, -1, 0));
562
+            }
563
+            if ($this->shareManager->outgoingServer2ServerSharesAllowed()) {
564
+                $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_REMOTE, $node, false, -1, 0));
565
+            }
566
+        }
567
+
568
+        $formatted = [];
569
+        foreach ($shares as $share) {
570
+            try {
571
+                $formatted[] = $this->formatShare($share);
572
+            } catch (NotFoundException $e) {
573
+                //Ignore this share
574
+            }
575
+        }
576
+
577
+        return new DataResponse($formatted);
578
+    }
579
+
580
+    /**
581
+     * The getShares function.
582
+     *
583
+     * @NoAdminRequired
584
+     *
585
+     * @param string $shared_with_me
586
+     * @param string $reshares
587
+     * @param string $subfiles
588
+     * @param string $path
589
+     *
590
+     * - Get shares by the current user
591
+     * - Get shares by the current user and reshares (?reshares=true)
592
+     * - Get shares with the current user (?shared_with_me=true)
593
+     * - Get shares for a specific path (?path=...)
594
+     * - Get all shares in a folder (?subfiles=true&path=..)
595
+     *
596
+     * @return DataResponse
597
+     * @throws OCSNotFoundException
598
+     */
599
+    public function getShares(
600
+        string $shared_with_me = 'false',
601
+        string $reshares = 'false',
602
+        string $subfiles = 'false',
603
+        string $path = null,
604
+        string $include_tags = 'false'
605
+    ): DataResponse {
606
+
607
+        if ($path !== null) {
608
+            $userFolder = $this->rootFolder->getUserFolder($this->currentUser);
609
+            try {
610
+                $path = $userFolder->get($path);
611
+                $this->lock($path);
612
+            } catch (\OCP\Files\NotFoundException $e) {
613
+                throw new OCSNotFoundException($this->l->t('Wrong path, file/folder doesn\'t exist'));
614
+            } catch (LockedException $e) {
615
+                throw new OCSNotFoundException($this->l->t('Could not lock path'));
616
+            }
617
+        }
618
+
619
+        $include_tags = $include_tags === 'true';
620
+
621
+        if ($shared_with_me === 'true') {
622
+            $result = $this->getSharedWithMe($path, $include_tags);
623
+            return $result;
624
+        }
625
+
626
+        if ($subfiles === 'true') {
627
+            $result = $this->getSharesInDir($path);
628
+            return $result;
629
+        }
630
+
631
+        if ($reshares === 'true') {
632
+            $reshares = true;
633
+        } else {
634
+            $reshares = false;
635
+        }
636
+
637
+        // Get all shares
638
+        $userShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_USER, $path, $reshares, -1, 0);
639
+        $groupShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_GROUP, $path, $reshares, -1, 0);
640
+        $linkShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_LINK, $path, $reshares, -1, 0);
641
+        if ($this->shareManager->shareProviderExists(\OCP\Share::SHARE_TYPE_EMAIL)) {
642
+            $mailShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_EMAIL, $path, $reshares, -1, 0);
643
+        } else {
644
+            $mailShares = [];
645
+        }
646
+        if ($this->shareManager->shareProviderExists(\OCP\Share::SHARE_TYPE_CIRCLE)) {
647
+            $circleShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_CIRCLE, $path, $reshares, -1, 0);
648
+        } else {
649
+            $circleShares = [];
650
+        }
651
+
652
+        $shares = array_merge($userShares, $groupShares, $linkShares, $mailShares, $circleShares);
653
+
654
+        if ($this->shareManager->outgoingServer2ServerSharesAllowed()) {
655
+            $federatedShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_REMOTE, $path, $reshares, -1, 0);
656
+            $shares = array_merge($shares, $federatedShares);
657
+        }
658
+
659
+        $formatted = [];
660
+        foreach ($shares as $share) {
661
+            try {
662
+                $formatted[] = $this->formatShare($share, $path);
663
+            } catch (NotFoundException $e) {
664
+                //Ignore share
665
+            }
666
+        }
667
+
668
+        if ($include_tags) {
669
+            $formatted = Helper::populateTags($formatted, 'file_source', \OC::$server->getTagManager());
670
+        }
671
+
672
+        return new DataResponse($formatted);
673
+    }
674
+
675
+    /**
676
+     * @NoAdminRequired
677
+     *
678
+     * @param string $id
679
+     * @param int $permissions
680
+     * @param string $password
681
+     * @param string $publicUpload
682
+     * @param string $expireDate
683
+     * @return DataResponse
684
+     * @throws OCSNotFoundException
685
+     * @throws OCSBadRequestException
686
+     * @throws OCSForbiddenException
687
+     */
688
+    public function updateShare(
689
+        string $id,
690
+        int $permissions = null,
691
+        string $password = null,
692
+        string $publicUpload = null,
693
+        string $expireDate = null
694
+    ): DataResponse {
695
+        try {
696
+            $share = $this->getShareById($id);
697
+        } catch (ShareNotFound $e) {
698
+            throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
699
+        }
700
+
701
+        $this->lock($share->getNode());
702
+
703
+        if (!$this->canAccessShare($share, false)) {
704
+            throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
705
+        }
706
+
707
+        if ($permissions === null && $password === null && $publicUpload === null && $expireDate === null) {
708
+            throw new OCSBadRequestException($this->l->t('Wrong or no update parameter given'));
709
+        }
710
+
711
+        /*
712 712
 		 * expirationdate, password and publicUpload only make sense for link shares
713 713
 		 */
714
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
715
-
716
-			$newPermissions = null;
717
-			if ($publicUpload === 'true') {
718
-				$newPermissions = Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE;
719
-			} else if ($publicUpload === 'false') {
720
-				$newPermissions = Constants::PERMISSION_READ;
721
-			}
722
-
723
-			if ($permissions !== null) {
724
-				$newPermissions = (int)$permissions;
725
-				$newPermissions = $newPermissions & ~Constants::PERMISSION_SHARE;
726
-			}
727
-
728
-			if ($newPermissions !== null &&
729
-				!in_array($newPermissions, [
730
-					Constants::PERMISSION_READ,
731
-					Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE, // legacy
732
-					Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE, // correct
733
-					Constants::PERMISSION_CREATE, // hidden file list
734
-					Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE, // allow to edit single files
735
-				], true)
736
-			) {
737
-				throw new OCSBadRequestException($this->l->t('Can\'t change permissions for public share links'));
738
-			}
739
-
740
-			if (
741
-				// legacy
742
-				$newPermissions === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE) ||
743
-				// correct
744
-				$newPermissions === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE)
745
-			) {
746
-				if (!$this->shareManager->shareApiLinkAllowPublicUpload()) {
747
-					throw new OCSForbiddenException($this->l->t('Public upload disabled by the administrator'));
748
-				}
749
-
750
-				if (!($share->getNode() instanceof \OCP\Files\Folder)) {
751
-					throw new OCSBadRequestException($this->l->t('Public upload is only possible for publicly shared folders'));
752
-				}
753
-
754
-				// normalize to correct public upload permissions
755
-				$newPermissions = Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE;
756
-			}
757
-
758
-			if ($newPermissions !== null) {
759
-				$share->setPermissions($newPermissions);
760
-				$permissions = $newPermissions;
761
-			}
762
-
763
-			if ($expireDate === '') {
764
-				$share->setExpirationDate(null);
765
-			} else if ($expireDate !== null) {
766
-				try {
767
-					$expireDate = $this->parseDate($expireDate);
768
-				} catch (\Exception $e) {
769
-					throw new OCSBadRequestException($e->getMessage(), $e);
770
-				}
771
-				$share->setExpirationDate($expireDate);
772
-			}
773
-
774
-			if ($password === '') {
775
-				$share->setPassword(null);
776
-			} else if ($password !== null) {
777
-				$share->setPassword($password);
778
-			}
779
-
780
-		} else {
781
-			if ($permissions !== null) {
782
-				$permissions = (int)$permissions;
783
-				$share->setPermissions($permissions);
784
-			}
785
-
786
-			if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
787
-				if ($password === '') {
788
-					$share->setPassword(null);
789
-				} else if ($password !== null) {
790
-					$share->setPassword($password);
791
-				}
792
-			}
793
-
794
-			if ($expireDate === '') {
795
-				$share->setExpirationDate(null);
796
-			} else if ($expireDate !== null) {
797
-				try {
798
-					$expireDate = $this->parseDate($expireDate);
799
-				} catch (\Exception $e) {
800
-					throw new OCSBadRequestException($e->getMessage(), $e);
801
-				}
802
-				$share->setExpirationDate($expireDate);
803
-			}
804
-
805
-		}
806
-
807
-		if ($permissions !== null && $share->getShareOwner() !== $this->currentUser) {
808
-			/* Check if this is an incomming share */
809
-			$incomingShares = $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_USER, $share->getNode(), -1, 0);
810
-			$incomingShares = array_merge($incomingShares, $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_GROUP, $share->getNode(), -1, 0));
811
-
812
-			/** @var \OCP\Share\IShare[] $incomingShares */
813
-			if (!empty($incomingShares)) {
814
-				$maxPermissions = 0;
815
-				foreach ($incomingShares as $incomingShare) {
816
-					$maxPermissions |= $incomingShare->getPermissions();
817
-				}
818
-
819
-				if ($share->getPermissions() & ~$maxPermissions) {
820
-					throw new OCSNotFoundException($this->l->t('Cannot increase permissions'));
821
-				}
822
-			}
823
-		}
824
-
825
-
826
-		try {
827
-			$share = $this->shareManager->updateShare($share);
828
-		} catch (\Exception $e) {
829
-			throw new OCSBadRequestException($e->getMessage(), $e);
830
-		}
831
-
832
-		return new DataResponse($this->formatShare($share));
833
-	}
834
-
835
-	protected function canAccessShare(\OCP\Share\IShare $share, bool $checkGroups = true): bool {
836
-		// A file with permissions 0 can't be accessed by us. So Don't show it
837
-		if ($share->getPermissions() === 0) {
838
-			return false;
839
-		}
840
-
841
-		// Owner of the file and the sharer of the file can always get share
842
-		if ($share->getShareOwner() === $this->currentUser ||
843
-			$share->getSharedBy() === $this->currentUser
844
-		) {
845
-			return true;
846
-		}
847
-
848
-		// If the share is shared with you (or a group you are a member of)
849
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
850
-			$share->getSharedWith() === $this->currentUser
851
-		) {
852
-			return true;
853
-		}
854
-
855
-		if ($checkGroups && $share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
856
-			$sharedWith = $this->groupManager->get($share->getSharedWith());
857
-			$user = $this->userManager->get($this->currentUser);
858
-			if ($user !== null && $sharedWith !== null && $sharedWith->inGroup($user)) {
859
-				return true;
860
-			}
861
-		}
862
-
863
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_CIRCLE) {
864
-			// TODO: have a sanity check like above?
865
-			return true;
866
-		}
867
-
868
-		return false;
869
-	}
870
-
871
-	/**
872
-	 * Make sure that the passed date is valid ISO 8601
873
-	 * So YYYY-MM-DD
874
-	 * If not throw an exception
875
-	 *
876
-	 * @param string $expireDate
877
-	 *
878
-	 * @throws \Exception
879
-	 * @return \DateTime
880
-	 */
881
-	private function parseDate(string $expireDate): \DateTime {
882
-		try {
883
-			$date = new \DateTime($expireDate);
884
-		} catch (\Exception $e) {
885
-			throw new \Exception('Invalid date. Format must be YYYY-MM-DD');
886
-		}
887
-
888
-		if ($date === false) {
889
-			throw new \Exception('Invalid date. Format must be YYYY-MM-DD');
890
-		}
891
-
892
-		$date->setTime(0, 0, 0);
893
-
894
-		return $date;
895
-	}
896
-
897
-	/**
898
-	 * Since we have multiple providers but the OCS Share API v1 does
899
-	 * not support this we need to check all backends.
900
-	 *
901
-	 * @param string $id
902
-	 * @return \OCP\Share\IShare
903
-	 * @throws ShareNotFound
904
-	 */
905
-	private function getShareById(string $id): IShare {
906
-		$share = null;
907
-
908
-		// First check if it is an internal share.
909
-		try {
910
-			$share = $this->shareManager->getShareById('ocinternal:' . $id, $this->currentUser);
911
-			return $share;
912
-		} catch (ShareNotFound $e) {
913
-			// Do nothing, just try the other share type
914
-		}
915
-
916
-
917
-		try {
918
-			if ($this->shareManager->shareProviderExists(\OCP\Share::SHARE_TYPE_CIRCLE)) {
919
-				$share = $this->shareManager->getShareById('ocCircleShare:' . $id, $this->currentUser);
920
-				return $share;
921
-			}
922
-		} catch (ShareNotFound $e) {
923
-			// Do nothing, just try the other share type
924
-		}
925
-
926
-		try {
927
-			if ($this->shareManager->shareProviderExists(\OCP\Share::SHARE_TYPE_EMAIL)) {
928
-				$share = $this->shareManager->getShareById('ocMailShare:' . $id, $this->currentUser);
929
-				return $share;
930
-			}
931
-		} catch (ShareNotFound $e) {
932
-			// Do nothing, just try the other share type
933
-		}
934
-
935
-		if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) {
936
-			throw new ShareNotFound();
937
-		}
938
-		$share = $this->shareManager->getShareById('ocFederatedSharing:' . $id, $this->currentUser);
939
-
940
-		return $share;
941
-	}
942
-
943
-	/**
944
-	 * Lock a Node
945
-	 *
946
-	 * @param \OCP\Files\Node $node
947
-	 * @throws LockedException
948
-	 */
949
-	private function lock(\OCP\Files\Node $node) {
950
-		$node->lock(ILockingProvider::LOCK_SHARED);
951
-		$this->lockedNode = $node;
952
-	}
953
-
954
-	/**
955
-	 * Cleanup the remaining locks
956
-	 * @throws @LockedException
957
-	 */
958
-	public function cleanup() {
959
-		if ($this->lockedNode !== null) {
960
-			$this->lockedNode->unlock(ILockingProvider::LOCK_SHARED);
961
-		}
962
-	}
714
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
715
+
716
+            $newPermissions = null;
717
+            if ($publicUpload === 'true') {
718
+                $newPermissions = Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE;
719
+            } else if ($publicUpload === 'false') {
720
+                $newPermissions = Constants::PERMISSION_READ;
721
+            }
722
+
723
+            if ($permissions !== null) {
724
+                $newPermissions = (int)$permissions;
725
+                $newPermissions = $newPermissions & ~Constants::PERMISSION_SHARE;
726
+            }
727
+
728
+            if ($newPermissions !== null &&
729
+                !in_array($newPermissions, [
730
+                    Constants::PERMISSION_READ,
731
+                    Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE, // legacy
732
+                    Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE, // correct
733
+                    Constants::PERMISSION_CREATE, // hidden file list
734
+                    Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE, // allow to edit single files
735
+                ], true)
736
+            ) {
737
+                throw new OCSBadRequestException($this->l->t('Can\'t change permissions for public share links'));
738
+            }
739
+
740
+            if (
741
+                // legacy
742
+                $newPermissions === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE) ||
743
+                // correct
744
+                $newPermissions === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE)
745
+            ) {
746
+                if (!$this->shareManager->shareApiLinkAllowPublicUpload()) {
747
+                    throw new OCSForbiddenException($this->l->t('Public upload disabled by the administrator'));
748
+                }
749
+
750
+                if (!($share->getNode() instanceof \OCP\Files\Folder)) {
751
+                    throw new OCSBadRequestException($this->l->t('Public upload is only possible for publicly shared folders'));
752
+                }
753
+
754
+                // normalize to correct public upload permissions
755
+                $newPermissions = Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE;
756
+            }
757
+
758
+            if ($newPermissions !== null) {
759
+                $share->setPermissions($newPermissions);
760
+                $permissions = $newPermissions;
761
+            }
762
+
763
+            if ($expireDate === '') {
764
+                $share->setExpirationDate(null);
765
+            } else if ($expireDate !== null) {
766
+                try {
767
+                    $expireDate = $this->parseDate($expireDate);
768
+                } catch (\Exception $e) {
769
+                    throw new OCSBadRequestException($e->getMessage(), $e);
770
+                }
771
+                $share->setExpirationDate($expireDate);
772
+            }
773
+
774
+            if ($password === '') {
775
+                $share->setPassword(null);
776
+            } else if ($password !== null) {
777
+                $share->setPassword($password);
778
+            }
779
+
780
+        } else {
781
+            if ($permissions !== null) {
782
+                $permissions = (int)$permissions;
783
+                $share->setPermissions($permissions);
784
+            }
785
+
786
+            if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
787
+                if ($password === '') {
788
+                    $share->setPassword(null);
789
+                } else if ($password !== null) {
790
+                    $share->setPassword($password);
791
+                }
792
+            }
793
+
794
+            if ($expireDate === '') {
795
+                $share->setExpirationDate(null);
796
+            } else if ($expireDate !== null) {
797
+                try {
798
+                    $expireDate = $this->parseDate($expireDate);
799
+                } catch (\Exception $e) {
800
+                    throw new OCSBadRequestException($e->getMessage(), $e);
801
+                }
802
+                $share->setExpirationDate($expireDate);
803
+            }
804
+
805
+        }
806
+
807
+        if ($permissions !== null && $share->getShareOwner() !== $this->currentUser) {
808
+            /* Check if this is an incomming share */
809
+            $incomingShares = $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_USER, $share->getNode(), -1, 0);
810
+            $incomingShares = array_merge($incomingShares, $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_GROUP, $share->getNode(), -1, 0));
811
+
812
+            /** @var \OCP\Share\IShare[] $incomingShares */
813
+            if (!empty($incomingShares)) {
814
+                $maxPermissions = 0;
815
+                foreach ($incomingShares as $incomingShare) {
816
+                    $maxPermissions |= $incomingShare->getPermissions();
817
+                }
818
+
819
+                if ($share->getPermissions() & ~$maxPermissions) {
820
+                    throw new OCSNotFoundException($this->l->t('Cannot increase permissions'));
821
+                }
822
+            }
823
+        }
824
+
825
+
826
+        try {
827
+            $share = $this->shareManager->updateShare($share);
828
+        } catch (\Exception $e) {
829
+            throw new OCSBadRequestException($e->getMessage(), $e);
830
+        }
831
+
832
+        return new DataResponse($this->formatShare($share));
833
+    }
834
+
835
+    protected function canAccessShare(\OCP\Share\IShare $share, bool $checkGroups = true): bool {
836
+        // A file with permissions 0 can't be accessed by us. So Don't show it
837
+        if ($share->getPermissions() === 0) {
838
+            return false;
839
+        }
840
+
841
+        // Owner of the file and the sharer of the file can always get share
842
+        if ($share->getShareOwner() === $this->currentUser ||
843
+            $share->getSharedBy() === $this->currentUser
844
+        ) {
845
+            return true;
846
+        }
847
+
848
+        // If the share is shared with you (or a group you are a member of)
849
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
850
+            $share->getSharedWith() === $this->currentUser
851
+        ) {
852
+            return true;
853
+        }
854
+
855
+        if ($checkGroups && $share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
856
+            $sharedWith = $this->groupManager->get($share->getSharedWith());
857
+            $user = $this->userManager->get($this->currentUser);
858
+            if ($user !== null && $sharedWith !== null && $sharedWith->inGroup($user)) {
859
+                return true;
860
+            }
861
+        }
862
+
863
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_CIRCLE) {
864
+            // TODO: have a sanity check like above?
865
+            return true;
866
+        }
867
+
868
+        return false;
869
+    }
870
+
871
+    /**
872
+     * Make sure that the passed date is valid ISO 8601
873
+     * So YYYY-MM-DD
874
+     * If not throw an exception
875
+     *
876
+     * @param string $expireDate
877
+     *
878
+     * @throws \Exception
879
+     * @return \DateTime
880
+     */
881
+    private function parseDate(string $expireDate): \DateTime {
882
+        try {
883
+            $date = new \DateTime($expireDate);
884
+        } catch (\Exception $e) {
885
+            throw new \Exception('Invalid date. Format must be YYYY-MM-DD');
886
+        }
887
+
888
+        if ($date === false) {
889
+            throw new \Exception('Invalid date. Format must be YYYY-MM-DD');
890
+        }
891
+
892
+        $date->setTime(0, 0, 0);
893
+
894
+        return $date;
895
+    }
896
+
897
+    /**
898
+     * Since we have multiple providers but the OCS Share API v1 does
899
+     * not support this we need to check all backends.
900
+     *
901
+     * @param string $id
902
+     * @return \OCP\Share\IShare
903
+     * @throws ShareNotFound
904
+     */
905
+    private function getShareById(string $id): IShare {
906
+        $share = null;
907
+
908
+        // First check if it is an internal share.
909
+        try {
910
+            $share = $this->shareManager->getShareById('ocinternal:' . $id, $this->currentUser);
911
+            return $share;
912
+        } catch (ShareNotFound $e) {
913
+            // Do nothing, just try the other share type
914
+        }
915
+
916
+
917
+        try {
918
+            if ($this->shareManager->shareProviderExists(\OCP\Share::SHARE_TYPE_CIRCLE)) {
919
+                $share = $this->shareManager->getShareById('ocCircleShare:' . $id, $this->currentUser);
920
+                return $share;
921
+            }
922
+        } catch (ShareNotFound $e) {
923
+            // Do nothing, just try the other share type
924
+        }
925
+
926
+        try {
927
+            if ($this->shareManager->shareProviderExists(\OCP\Share::SHARE_TYPE_EMAIL)) {
928
+                $share = $this->shareManager->getShareById('ocMailShare:' . $id, $this->currentUser);
929
+                return $share;
930
+            }
931
+        } catch (ShareNotFound $e) {
932
+            // Do nothing, just try the other share type
933
+        }
934
+
935
+        if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) {
936
+            throw new ShareNotFound();
937
+        }
938
+        $share = $this->shareManager->getShareById('ocFederatedSharing:' . $id, $this->currentUser);
939
+
940
+        return $share;
941
+    }
942
+
943
+    /**
944
+     * Lock a Node
945
+     *
946
+     * @param \OCP\Files\Node $node
947
+     * @throws LockedException
948
+     */
949
+    private function lock(\OCP\Files\Node $node) {
950
+        $node->lock(ILockingProvider::LOCK_SHARED);
951
+        $this->lockedNode = $node;
952
+    }
953
+
954
+    /**
955
+     * Cleanup the remaining locks
956
+     * @throws @LockedException
957
+     */
958
+    public function cleanup() {
959
+        if ($this->lockedNode !== null) {
960
+            $this->lockedNode->unlock(ILockingProvider::LOCK_SHARED);
961
+        }
962
+    }
963 963
 }
Please login to merge, or discard this patch.