Completed
Pull Request — master (#9895)
by Björn
43:43 queued 13:05
created
apps/cloud_federation_api/lib/Controller/RequestHandlerController.php 1 patch
Indentation   +245 added lines, -245 removed lines patch added patch discarded remove patch
@@ -52,250 +52,250 @@
 block discarded – undo
52 52
  */
53 53
 class RequestHandlerController extends Controller {
54 54
 
55
-	/** @var ILogger */
56
-	private $logger;
57
-
58
-	/** @var IUserManager */
59
-	private $userManager;
60
-
61
-	/** @var IGroupManager */
62
-	private $groupManager;
63
-
64
-	/** @var IURLGenerator */
65
-	private $urlGenerator;
66
-
67
-	/** @var ICloudFederationProviderManager */
68
-	private $cloudFederationProviderManager;
69
-
70
-	/** @var Config */
71
-	private $config;
72
-
73
-	/** @var ICloudFederationFactory */
74
-	private $factory;
75
-
76
-	/** @var ICloudIdManager */
77
-	private $cloudIdManager;
78
-
79
-	public function __construct($appName,
80
-								IRequest $request,
81
-								ILogger $logger,
82
-								IUserManager $userManager,
83
-								IGroupManager $groupManager,
84
-								IURLGenerator $urlGenerator,
85
-								ICloudFederationProviderManager $cloudFederationProviderManager,
86
-								Config $config,
87
-								ICloudFederationFactory $factory,
88
-								ICloudIdManager $cloudIdManager
89
-	) {
90
-		parent::__construct($appName, $request);
91
-
92
-		$this->logger = $logger;
93
-		$this->userManager = $userManager;
94
-		$this->groupManager = $groupManager;
95
-		$this->urlGenerator = $urlGenerator;
96
-		$this->cloudFederationProviderManager = $cloudFederationProviderManager;
97
-		$this->config = $config;
98
-		$this->factory = $factory;
99
-		$this->cloudIdManager = $cloudIdManager;
100
-	}
101
-
102
-	/**
103
-	 * add share
104
-	 *
105
-	 * @NoCSRFRequired
106
-	 * @PublicPage
107
-	 * @BruteForceProtection(action=receiveFederatedShare)
108
-	 *
109
-	 * @param string $shareWith
110
-	 * @param string $name resource name (e.g. document.odt)
111
-	 * @param string $description share description (optional)
112
-	 * @param string $providerId resource UID on the provider side
113
-	 * @param string $owner provider specific UID of the user who owns the resource
114
-	 * @param string $ownerDisplayName display name of the user who shared the item
115
-	 * @param string $sharedBy provider specific UID of the user who shared the resource
116
-	 * @param string $sharedByDisplayName display name of the user who shared the resource
117
-	 * @param array $protocol (e,.g. ['name' => 'webdav', 'options' => ['username' => 'john', 'permissions' => 31]])
118
-	 * @param string $shareType ('group' or 'user' share)
119
-	 * @param $resourceType ('file', 'calendar',...)
120
-	 * @return Http\DataResponse|JSONResponse
121
-	 *
122
-	 * Example: curl -H "Content-Type: application/json" -X POST -d '{"shareWith":"admin1@serve1","name":"welcome server2.txt","description":"desc","providerId":"2","owner":"admin2@http://localhost/server2","ownerDisplayName":"admin2 display","shareType":"user","resourceType":"file","protocol":{"name":"webdav","options":{"sharedSecret":"secret","permissions":"webdav-property"}}}' http://localhost/server/index.php/ocm/shares
123
-	 */
124
-	public function addShare($shareWith, $name, $description, $providerId, $owner, $ownerDisplayName, $sharedBy, $sharedByDisplayName, $protocol, $shareType, $resourceType) {
125
-
126
-		// check if all required parameters are set
127
-		if ($shareWith === null ||
128
-			$name === null ||
129
-			$providerId === null ||
130
-			$owner === null ||
131
-			$resourceType === null ||
132
-			$shareType === null ||
133
-			!is_array($protocol) ||
134
-			!isset($protocol['name']) ||
135
-			!isset ($protocol['options']) ||
136
-			!is_array($protocol['options']) ||
137
-			!isset($protocol['options']['sharedSecret'])
138
-		) {
139
-			return new JSONResponse(
140
-				['message' => 'Missing arguments'],
141
-				Http::STATUS_BAD_REQUEST
142
-			);
143
-		}
144
-
145
-		$supportedShareTypes = $this->config->getSupportedShareTypes($resourceType);
146
-		if (!in_array($shareType, $supportedShareTypes)) {
147
-			return new JSONResponse(
148
-				['message' => 'Share type "' . $shareType . '" not implemented'],
149
-				Http::STATUS_NOT_IMPLEMENTED
150
-			);
151
-		}
152
-
153
-		$cloudId = $this->cloudIdManager->resolveCloudId($shareWith);
154
-		$shareWithLocalId = $cloudId->getUser();
155
-
156
-		if ($shareType === 'user') {
157
-			$shareWith = $this->mapUid($shareWithLocalId);
158
-
159
-			if (!$this->userManager->userExists($shareWith)) {
160
-				return new JSONResponse(
161
-					['message' => 'User "' . $shareWith . '" does not exists at ' . $this->urlGenerator->getBaseUrl()],
162
-					Http::STATUS_BAD_REQUEST
163
-				);
164
-			}
165
-		}
166
-
167
-		if ($shareType === 'group') {
168
-			if(!$this->groupManager->groupExists($shareWithLocalId)) {
169
-				return new JSONResponse(
170
-					['message' => 'Group "' . $shareWith . '" does not exists at ' . $this->urlGenerator->getBaseUrl()],
171
-					Http::STATUS_BAD_REQUEST
172
-				);
173
-			}
174
-		}
175
-
176
-		// if no explicit display name is given, we use the uid as display name
177
-		$ownerDisplayName = $ownerDisplayName === null ? $owner : $ownerDisplayName;
178
-		$sharedByDisplayName = $sharedByDisplayName === null ? $sharedBy : $sharedByDisplayName;
179
-
180
-		// sharedBy* parameter is optional, if nothing is set we assume that it is the same user as the owner
181
-		if ($sharedBy === null) {
182
-			$sharedBy = $owner;
183
-			$sharedByDisplayName = $ownerDisplayName;
184
-		}
185
-
186
-		try {
187
-			$provider = $this->cloudFederationProviderManager->getCloudFederationProvider($resourceType);
188
-			$share = $this->factory->getCloudFederationShare($shareWith, $name, $description, $providerId, $owner, $ownerDisplayName, $sharedBy, $sharedByDisplayName, '', $shareType, $resourceType);
189
-			$share->setProtocol($protocol);
190
-			$provider->shareReceived($share);
191
-		} catch (ProviderDoesNotExistsException $e) {
192
-			return new JSONResponse(
193
-				['message' => $e->getMessage()],
194
-				Http::STATUS_NOT_IMPLEMENTED
195
-			);
196
-		} catch (ProviderCouldNotAddShareException $e) {
197
-			return new JSONResponse(
198
-				['message' => $e->getMessage()],
199
-				$e->getCode()
200
-			);
201
-		} catch (\Exception $e) {
202
-			return new JSONResponse(
203
-				['message' => 'Internal error at ' . $this->urlGenerator->getBaseUrl()],
204
-				Http::STATUS_BAD_REQUEST
205
-			);
206
-		}
207
-
208
-		$user = $this->userManager->get($shareWithLocalId);
209
-		$recipientDisplayName = '';
210
-		if($user) {
211
-			$recipientDisplayName = $user->getDisplayName();
212
-		}
213
-
214
-		return new JSONResponse(
215
-			['recipientDisplayName' => $recipientDisplayName],
216
-			Http::STATUS_CREATED);
217
-
218
-	}
219
-
220
-	/**
221
-	 * receive notification about existing share
222
-	 *
223
-	 * @NoCSRFRequired
224
-	 * @PublicPage
225
-	 * @BruteForceProtection(action=receiveFederatedShareNotification)
226
-	 *
227
-	 * @param string $notificationType (notification type, e.g. SHARE_ACCEPTED)
228
-	 * @param string $resourceType (calendar, file, contact,...)
229
-	 * @param string $providerId id of the share
230
-	 * @param array $notification the actual payload of the notification
231
-	 * @return JSONResponse
232
-	 */
233
-	public function receiveNotification($notificationType, $resourceType, $providerId, array $notification) {
234
-
235
-		// check if all required parameters are set
236
-		if ($notificationType === null ||
237
-			$resourceType === null ||
238
-			$providerId === null ||
239
-			!is_array($notification)
240
-		) {
241
-			return new JSONResponse(
242
-				['message' => 'Missing arguments'],
243
-				Http::STATUS_BAD_REQUEST
244
-			);
245
-		}
246
-
247
-		try {
248
-			$provider = $this->cloudFederationProviderManager->getCloudFederationProvider($resourceType);
249
-			$result = $provider->notificationReceived($notificationType, $providerId, $notification);
250
-		} catch (ProviderDoesNotExistsException $e) {
251
-			return new JSONResponse(
252
-				['message' => $e->getMessage()],
253
-				Http::STATUS_BAD_REQUEST
254
-			);
255
-		} catch (ShareNotFound $e) {
256
-			return new JSONResponse(
257
-				['message' => $e->getMessage()],
258
-				Http::STATUS_BAD_REQUEST
259
-			);
260
-		} catch (ActionNotSupportedException $e) {
261
-			return new JSONResponse(
262
-				['message' => $e->getMessage()],
263
-				Http::STATUS_NOT_IMPLEMENTED
264
-			);
265
-		} catch (BadRequestException $e) {
266
-			return new JSONResponse($e->getReturnMessage(), Http::STATUS_BAD_REQUEST);
267
-		} catch (AuthenticationFailedException $e) {
268
-			return new JSONResponse(["message" => "RESOURCE_NOT_FOUND"], Http::STATUS_FORBIDDEN);
269
-		}
270
-		catch (\Exception $e) {
271
-			return new JSONResponse(
272
-				['message' => 'Internal error at ' . $this->urlGenerator->getBaseUrl()],
273
-				Http::STATUS_BAD_REQUEST
274
-			);
275
-		}
276
-
277
-		return new JSONResponse($result,Http::STATUS_CREATED);
278
-
279
-	}
280
-
281
-	/**
282
-	 * map login name to internal LDAP UID if a LDAP backend is in use
283
-	 *
284
-	 * @param string $uid
285
-	 * @return string mixed
286
-	 */
287
-	private function mapUid($uid) {
288
-		\OC::$server->getURLGenerator()->linkToDocs('key');
289
-		// FIXME this should be a method in the user management instead
290
-		$this->logger->debug('shareWith before, ' . $uid, ['app' => $this->appName]);
291
-		\OCP\Util::emitHook(
292
-			'\OCA\Files_Sharing\API\Server2Server',
293
-			'preLoginNameUsedAsUserName',
294
-			array('uid' => &$uid)
295
-		);
296
-		$this->logger->debug('shareWith after, ' . $uid, ['app' => $this->appName]);
297
-
298
-		return $uid;
299
-	}
55
+    /** @var ILogger */
56
+    private $logger;
57
+
58
+    /** @var IUserManager */
59
+    private $userManager;
60
+
61
+    /** @var IGroupManager */
62
+    private $groupManager;
63
+
64
+    /** @var IURLGenerator */
65
+    private $urlGenerator;
66
+
67
+    /** @var ICloudFederationProviderManager */
68
+    private $cloudFederationProviderManager;
69
+
70
+    /** @var Config */
71
+    private $config;
72
+
73
+    /** @var ICloudFederationFactory */
74
+    private $factory;
75
+
76
+    /** @var ICloudIdManager */
77
+    private $cloudIdManager;
78
+
79
+    public function __construct($appName,
80
+                                IRequest $request,
81
+                                ILogger $logger,
82
+                                IUserManager $userManager,
83
+                                IGroupManager $groupManager,
84
+                                IURLGenerator $urlGenerator,
85
+                                ICloudFederationProviderManager $cloudFederationProviderManager,
86
+                                Config $config,
87
+                                ICloudFederationFactory $factory,
88
+                                ICloudIdManager $cloudIdManager
89
+    ) {
90
+        parent::__construct($appName, $request);
91
+
92
+        $this->logger = $logger;
93
+        $this->userManager = $userManager;
94
+        $this->groupManager = $groupManager;
95
+        $this->urlGenerator = $urlGenerator;
96
+        $this->cloudFederationProviderManager = $cloudFederationProviderManager;
97
+        $this->config = $config;
98
+        $this->factory = $factory;
99
+        $this->cloudIdManager = $cloudIdManager;
100
+    }
101
+
102
+    /**
103
+     * add share
104
+     *
105
+     * @NoCSRFRequired
106
+     * @PublicPage
107
+     * @BruteForceProtection(action=receiveFederatedShare)
108
+     *
109
+     * @param string $shareWith
110
+     * @param string $name resource name (e.g. document.odt)
111
+     * @param string $description share description (optional)
112
+     * @param string $providerId resource UID on the provider side
113
+     * @param string $owner provider specific UID of the user who owns the resource
114
+     * @param string $ownerDisplayName display name of the user who shared the item
115
+     * @param string $sharedBy provider specific UID of the user who shared the resource
116
+     * @param string $sharedByDisplayName display name of the user who shared the resource
117
+     * @param array $protocol (e,.g. ['name' => 'webdav', 'options' => ['username' => 'john', 'permissions' => 31]])
118
+     * @param string $shareType ('group' or 'user' share)
119
+     * @param $resourceType ('file', 'calendar',...)
120
+     * @return Http\DataResponse|JSONResponse
121
+     *
122
+     * Example: curl -H "Content-Type: application/json" -X POST -d '{"shareWith":"admin1@serve1","name":"welcome server2.txt","description":"desc","providerId":"2","owner":"admin2@http://localhost/server2","ownerDisplayName":"admin2 display","shareType":"user","resourceType":"file","protocol":{"name":"webdav","options":{"sharedSecret":"secret","permissions":"webdav-property"}}}' http://localhost/server/index.php/ocm/shares
123
+     */
124
+    public function addShare($shareWith, $name, $description, $providerId, $owner, $ownerDisplayName, $sharedBy, $sharedByDisplayName, $protocol, $shareType, $resourceType) {
125
+
126
+        // check if all required parameters are set
127
+        if ($shareWith === null ||
128
+            $name === null ||
129
+            $providerId === null ||
130
+            $owner === null ||
131
+            $resourceType === null ||
132
+            $shareType === null ||
133
+            !is_array($protocol) ||
134
+            !isset($protocol['name']) ||
135
+            !isset ($protocol['options']) ||
136
+            !is_array($protocol['options']) ||
137
+            !isset($protocol['options']['sharedSecret'])
138
+        ) {
139
+            return new JSONResponse(
140
+                ['message' => 'Missing arguments'],
141
+                Http::STATUS_BAD_REQUEST
142
+            );
143
+        }
144
+
145
+        $supportedShareTypes = $this->config->getSupportedShareTypes($resourceType);
146
+        if (!in_array($shareType, $supportedShareTypes)) {
147
+            return new JSONResponse(
148
+                ['message' => 'Share type "' . $shareType . '" not implemented'],
149
+                Http::STATUS_NOT_IMPLEMENTED
150
+            );
151
+        }
152
+
153
+        $cloudId = $this->cloudIdManager->resolveCloudId($shareWith);
154
+        $shareWithLocalId = $cloudId->getUser();
155
+
156
+        if ($shareType === 'user') {
157
+            $shareWith = $this->mapUid($shareWithLocalId);
158
+
159
+            if (!$this->userManager->userExists($shareWith)) {
160
+                return new JSONResponse(
161
+                    ['message' => 'User "' . $shareWith . '" does not exists at ' . $this->urlGenerator->getBaseUrl()],
162
+                    Http::STATUS_BAD_REQUEST
163
+                );
164
+            }
165
+        }
166
+
167
+        if ($shareType === 'group') {
168
+            if(!$this->groupManager->groupExists($shareWithLocalId)) {
169
+                return new JSONResponse(
170
+                    ['message' => 'Group "' . $shareWith . '" does not exists at ' . $this->urlGenerator->getBaseUrl()],
171
+                    Http::STATUS_BAD_REQUEST
172
+                );
173
+            }
174
+        }
175
+
176
+        // if no explicit display name is given, we use the uid as display name
177
+        $ownerDisplayName = $ownerDisplayName === null ? $owner : $ownerDisplayName;
178
+        $sharedByDisplayName = $sharedByDisplayName === null ? $sharedBy : $sharedByDisplayName;
179
+
180
+        // sharedBy* parameter is optional, if nothing is set we assume that it is the same user as the owner
181
+        if ($sharedBy === null) {
182
+            $sharedBy = $owner;
183
+            $sharedByDisplayName = $ownerDisplayName;
184
+        }
185
+
186
+        try {
187
+            $provider = $this->cloudFederationProviderManager->getCloudFederationProvider($resourceType);
188
+            $share = $this->factory->getCloudFederationShare($shareWith, $name, $description, $providerId, $owner, $ownerDisplayName, $sharedBy, $sharedByDisplayName, '', $shareType, $resourceType);
189
+            $share->setProtocol($protocol);
190
+            $provider->shareReceived($share);
191
+        } catch (ProviderDoesNotExistsException $e) {
192
+            return new JSONResponse(
193
+                ['message' => $e->getMessage()],
194
+                Http::STATUS_NOT_IMPLEMENTED
195
+            );
196
+        } catch (ProviderCouldNotAddShareException $e) {
197
+            return new JSONResponse(
198
+                ['message' => $e->getMessage()],
199
+                $e->getCode()
200
+            );
201
+        } catch (\Exception $e) {
202
+            return new JSONResponse(
203
+                ['message' => 'Internal error at ' . $this->urlGenerator->getBaseUrl()],
204
+                Http::STATUS_BAD_REQUEST
205
+            );
206
+        }
207
+
208
+        $user = $this->userManager->get($shareWithLocalId);
209
+        $recipientDisplayName = '';
210
+        if($user) {
211
+            $recipientDisplayName = $user->getDisplayName();
212
+        }
213
+
214
+        return new JSONResponse(
215
+            ['recipientDisplayName' => $recipientDisplayName],
216
+            Http::STATUS_CREATED);
217
+
218
+    }
219
+
220
+    /**
221
+     * receive notification about existing share
222
+     *
223
+     * @NoCSRFRequired
224
+     * @PublicPage
225
+     * @BruteForceProtection(action=receiveFederatedShareNotification)
226
+     *
227
+     * @param string $notificationType (notification type, e.g. SHARE_ACCEPTED)
228
+     * @param string $resourceType (calendar, file, contact,...)
229
+     * @param string $providerId id of the share
230
+     * @param array $notification the actual payload of the notification
231
+     * @return JSONResponse
232
+     */
233
+    public function receiveNotification($notificationType, $resourceType, $providerId, array $notification) {
234
+
235
+        // check if all required parameters are set
236
+        if ($notificationType === null ||
237
+            $resourceType === null ||
238
+            $providerId === null ||
239
+            !is_array($notification)
240
+        ) {
241
+            return new JSONResponse(
242
+                ['message' => 'Missing arguments'],
243
+                Http::STATUS_BAD_REQUEST
244
+            );
245
+        }
246
+
247
+        try {
248
+            $provider = $this->cloudFederationProviderManager->getCloudFederationProvider($resourceType);
249
+            $result = $provider->notificationReceived($notificationType, $providerId, $notification);
250
+        } catch (ProviderDoesNotExistsException $e) {
251
+            return new JSONResponse(
252
+                ['message' => $e->getMessage()],
253
+                Http::STATUS_BAD_REQUEST
254
+            );
255
+        } catch (ShareNotFound $e) {
256
+            return new JSONResponse(
257
+                ['message' => $e->getMessage()],
258
+                Http::STATUS_BAD_REQUEST
259
+            );
260
+        } catch (ActionNotSupportedException $e) {
261
+            return new JSONResponse(
262
+                ['message' => $e->getMessage()],
263
+                Http::STATUS_NOT_IMPLEMENTED
264
+            );
265
+        } catch (BadRequestException $e) {
266
+            return new JSONResponse($e->getReturnMessage(), Http::STATUS_BAD_REQUEST);
267
+        } catch (AuthenticationFailedException $e) {
268
+            return new JSONResponse(["message" => "RESOURCE_NOT_FOUND"], Http::STATUS_FORBIDDEN);
269
+        }
270
+        catch (\Exception $e) {
271
+            return new JSONResponse(
272
+                ['message' => 'Internal error at ' . $this->urlGenerator->getBaseUrl()],
273
+                Http::STATUS_BAD_REQUEST
274
+            );
275
+        }
276
+
277
+        return new JSONResponse($result,Http::STATUS_CREATED);
278
+
279
+    }
280
+
281
+    /**
282
+     * map login name to internal LDAP UID if a LDAP backend is in use
283
+     *
284
+     * @param string $uid
285
+     * @return string mixed
286
+     */
287
+    private function mapUid($uid) {
288
+        \OC::$server->getURLGenerator()->linkToDocs('key');
289
+        // FIXME this should be a method in the user management instead
290
+        $this->logger->debug('shareWith before, ' . $uid, ['app' => $this->appName]);
291
+        \OCP\Util::emitHook(
292
+            '\OCA\Files_Sharing\API\Server2Server',
293
+            'preLoginNameUsedAsUserName',
294
+            array('uid' => &$uid)
295
+        );
296
+        $this->logger->debug('shareWith after, ' . $uid, ['app' => $this->appName]);
297
+
298
+        return $uid;
299
+    }
300 300
 
301 301
 }
Please login to merge, or discard this patch.
apps/cloud_federation_api/lib/Config.php 1 patch
Indentation   +21 added lines, -21 removed lines patch added patch discarded remove patch
@@ -35,26 +35,26 @@
 block discarded – undo
35 35
  */
36 36
 class Config {
37 37
 
38
-	/** @var ICloudFederationProviderManager */
39
-	private $cloudFederationProviderManager;
40
-
41
-	public function __construct(ICloudFederationProviderManager $cloudFederationProviderManager) {
42
-		$this->cloudFederationProviderManager = $cloudFederationProviderManager;
43
-	}
44
-
45
-	/**
46
-	 * get a list of supported share types
47
-	 *
48
-	 * @param string $resourceType
49
-	 * @return array
50
-	 */
51
-	public function getSupportedShareTypes($resourceType) {
52
-		try {
53
-			$provider = $this->cloudFederationProviderManager->getCloudFederationProvider($resourceType);
54
-			return $provider->getSupportedShareTypes();
55
-		} catch (\Exception $e) {
56
-			return [];
57
-		}
58
-	}
38
+    /** @var ICloudFederationProviderManager */
39
+    private $cloudFederationProviderManager;
40
+
41
+    public function __construct(ICloudFederationProviderManager $cloudFederationProviderManager) {
42
+        $this->cloudFederationProviderManager = $cloudFederationProviderManager;
43
+    }
44
+
45
+    /**
46
+     * get a list of supported share types
47
+     *
48
+     * @param string $resourceType
49
+     * @return array
50
+     */
51
+    public function getSupportedShareTypes($resourceType) {
52
+        try {
53
+            $provider = $this->cloudFederationProviderManager->getCloudFederationProvider($resourceType);
54
+            return $provider->getSupportedShareTypes();
55
+        } catch (\Exception $e) {
56
+            return [];
57
+        }
58
+    }
59 59
 
60 60
 }
Please login to merge, or discard this patch.
apps/federatedfilesharing/lib/FederatedShareProvider.php 1 patch
Indentation   +967 added lines, -967 removed lines patch added patch discarded remove patch
@@ -53,981 +53,981 @@
 block discarded – undo
53 53
  */
54 54
 class FederatedShareProvider implements IShareProvider {
55 55
 
56
-	const SHARE_TYPE_REMOTE = 6;
57
-
58
-	/** @var IDBConnection */
59
-	private $dbConnection;
60
-
61
-	/** @var AddressHandler */
62
-	private $addressHandler;
63
-
64
-	/** @var Notifications */
65
-	private $notifications;
66
-
67
-	/** @var TokenHandler */
68
-	private $tokenHandler;
69
-
70
-	/** @var IL10N */
71
-	private $l;
72
-
73
-	/** @var ILogger */
74
-	private $logger;
75
-
76
-	/** @var IRootFolder */
77
-	private $rootFolder;
78
-
79
-	/** @var IConfig */
80
-	private $config;
81
-
82
-	/** @var string */
83
-	private $externalShareTable = 'share_external';
84
-
85
-	/** @var IUserManager */
86
-	private $userManager;
87
-
88
-	/** @var ICloudIdManager */
89
-	private $cloudIdManager;
90
-
91
-	/** @var \OCP\GlobalScale\IConfig */
92
-	private $gsConfig;
93
-
94
-	/**
95
-	 * DefaultShareProvider constructor.
96
-	 *
97
-	 * @param IDBConnection $connection
98
-	 * @param AddressHandler $addressHandler
99
-	 * @param Notifications $notifications
100
-	 * @param TokenHandler $tokenHandler
101
-	 * @param IL10N $l10n
102
-	 * @param ILogger $logger
103
-	 * @param IRootFolder $rootFolder
104
-	 * @param IConfig $config
105
-	 * @param IUserManager $userManager
106
-	 * @param ICloudIdManager $cloudIdManager
107
-	 * @param \OCP\GlobalScale\IConfig $globalScaleConfig
108
-	 */
109
-	public function __construct(
110
-			IDBConnection $connection,
111
-			AddressHandler $addressHandler,
112
-			Notifications $notifications,
113
-			TokenHandler $tokenHandler,
114
-			IL10N $l10n,
115
-			ILogger $logger,
116
-			IRootFolder $rootFolder,
117
-			IConfig $config,
118
-			IUserManager $userManager,
119
-			ICloudIdManager $cloudIdManager,
120
-			\OCP\GlobalScale\IConfig $globalScaleConfig
121
-	) {
122
-		$this->dbConnection = $connection;
123
-		$this->addressHandler = $addressHandler;
124
-		$this->notifications = $notifications;
125
-		$this->tokenHandler = $tokenHandler;
126
-		$this->l = $l10n;
127
-		$this->logger = $logger;
128
-		$this->rootFolder = $rootFolder;
129
-		$this->config = $config;
130
-		$this->userManager = $userManager;
131
-		$this->cloudIdManager = $cloudIdManager;
132
-		$this->gsConfig = $globalScaleConfig;
133
-	}
134
-
135
-	/**
136
-	 * Return the identifier of this provider.
137
-	 *
138
-	 * @return string Containing only [a-zA-Z0-9]
139
-	 */
140
-	public function identifier() {
141
-		return 'ocFederatedSharing';
142
-	}
143
-
144
-	/**
145
-	 * Share a path
146
-	 *
147
-	 * @param IShare $share
148
-	 * @return IShare The share object
149
-	 * @throws ShareNotFound
150
-	 * @throws \Exception
151
-	 */
152
-	public function create(IShare $share) {
153
-
154
-		$shareWith = $share->getSharedWith();
155
-		$itemSource = $share->getNodeId();
156
-		$itemType = $share->getNodeType();
157
-		$permissions = $share->getPermissions();
158
-		$sharedBy = $share->getSharedBy();
159
-
160
-		/*
56
+    const SHARE_TYPE_REMOTE = 6;
57
+
58
+    /** @var IDBConnection */
59
+    private $dbConnection;
60
+
61
+    /** @var AddressHandler */
62
+    private $addressHandler;
63
+
64
+    /** @var Notifications */
65
+    private $notifications;
66
+
67
+    /** @var TokenHandler */
68
+    private $tokenHandler;
69
+
70
+    /** @var IL10N */
71
+    private $l;
72
+
73
+    /** @var ILogger */
74
+    private $logger;
75
+
76
+    /** @var IRootFolder */
77
+    private $rootFolder;
78
+
79
+    /** @var IConfig */
80
+    private $config;
81
+
82
+    /** @var string */
83
+    private $externalShareTable = 'share_external';
84
+
85
+    /** @var IUserManager */
86
+    private $userManager;
87
+
88
+    /** @var ICloudIdManager */
89
+    private $cloudIdManager;
90
+
91
+    /** @var \OCP\GlobalScale\IConfig */
92
+    private $gsConfig;
93
+
94
+    /**
95
+     * DefaultShareProvider constructor.
96
+     *
97
+     * @param IDBConnection $connection
98
+     * @param AddressHandler $addressHandler
99
+     * @param Notifications $notifications
100
+     * @param TokenHandler $tokenHandler
101
+     * @param IL10N $l10n
102
+     * @param ILogger $logger
103
+     * @param IRootFolder $rootFolder
104
+     * @param IConfig $config
105
+     * @param IUserManager $userManager
106
+     * @param ICloudIdManager $cloudIdManager
107
+     * @param \OCP\GlobalScale\IConfig $globalScaleConfig
108
+     */
109
+    public function __construct(
110
+            IDBConnection $connection,
111
+            AddressHandler $addressHandler,
112
+            Notifications $notifications,
113
+            TokenHandler $tokenHandler,
114
+            IL10N $l10n,
115
+            ILogger $logger,
116
+            IRootFolder $rootFolder,
117
+            IConfig $config,
118
+            IUserManager $userManager,
119
+            ICloudIdManager $cloudIdManager,
120
+            \OCP\GlobalScale\IConfig $globalScaleConfig
121
+    ) {
122
+        $this->dbConnection = $connection;
123
+        $this->addressHandler = $addressHandler;
124
+        $this->notifications = $notifications;
125
+        $this->tokenHandler = $tokenHandler;
126
+        $this->l = $l10n;
127
+        $this->logger = $logger;
128
+        $this->rootFolder = $rootFolder;
129
+        $this->config = $config;
130
+        $this->userManager = $userManager;
131
+        $this->cloudIdManager = $cloudIdManager;
132
+        $this->gsConfig = $globalScaleConfig;
133
+    }
134
+
135
+    /**
136
+     * Return the identifier of this provider.
137
+     *
138
+     * @return string Containing only [a-zA-Z0-9]
139
+     */
140
+    public function identifier() {
141
+        return 'ocFederatedSharing';
142
+    }
143
+
144
+    /**
145
+     * Share a path
146
+     *
147
+     * @param IShare $share
148
+     * @return IShare The share object
149
+     * @throws ShareNotFound
150
+     * @throws \Exception
151
+     */
152
+    public function create(IShare $share) {
153
+
154
+        $shareWith = $share->getSharedWith();
155
+        $itemSource = $share->getNodeId();
156
+        $itemType = $share->getNodeType();
157
+        $permissions = $share->getPermissions();
158
+        $sharedBy = $share->getSharedBy();
159
+
160
+        /*
161 161
 		 * Check if file is not already shared with the remote user
162 162
 		 */
163
-		$alreadyShared = $this->getSharedWith($shareWith, self::SHARE_TYPE_REMOTE, $share->getNode(), 1, 0);
164
-		if (!empty($alreadyShared)) {
165
-			$message = 'Sharing %s failed, because this item is already shared with %s';
166
-			$message_t = $this->l->t('Sharing %s failed, because this item is already shared with %s', array($share->getNode()->getName(), $shareWith));
167
-			$this->logger->debug(sprintf($message, $share->getNode()->getName(), $shareWith), ['app' => 'Federated File Sharing']);
168
-			throw new \Exception($message_t);
169
-		}
170
-
171
-
172
-		// don't allow federated shares if source and target server are the same
173
-		$cloudId = $this->cloudIdManager->resolveCloudId($shareWith);
174
-		$currentServer = $this->addressHandler->generateRemoteURL();
175
-		$currentUser = $sharedBy;
176
-		if ($this->addressHandler->compareAddresses($cloudId->getUser(), $cloudId->getRemote(), $currentUser, $currentServer)) {
177
-			$message = 'Not allowed to create a federated share with the same user.';
178
-			$message_t = $this->l->t('Not allowed to create a federated share with the same user');
179
-			$this->logger->debug($message, ['app' => 'Federated File Sharing']);
180
-			throw new \Exception($message_t);
181
-		}
182
-
183
-
184
-		$share->setSharedWith($cloudId->getId());
185
-
186
-		try {
187
-			$remoteShare = $this->getShareFromExternalShareTable($share);
188
-		} catch (ShareNotFound $e) {
189
-			$remoteShare = null;
190
-		}
191
-
192
-		if ($remoteShare) {
193
-			try {
194
-				$ownerCloudId = $this->cloudIdManager->getCloudId($remoteShare['owner'], $remoteShare['remote']);
195
-				$shareId = $this->addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $ownerCloudId->getId(), $permissions, 'tmp_token_' . time());
196
-				$share->setId($shareId);
197
-				list($token, $remoteId) = $this->askOwnerToReShare($shareWith, $share, $shareId);
198
-				// remote share was create successfully if we get a valid token as return
199
-				$send = is_string($token) && $token !== '';
200
-			} catch (\Exception $e) {
201
-				// fall back to old re-share behavior if the remote server
202
-				// doesn't support flat re-shares (was introduced with Nextcloud 9.1)
203
-				$this->removeShareFromTable($share);
204
-				$shareId = $this->createFederatedShare($share);
205
-			}
206
-			if ($send) {
207
-				$this->updateSuccessfulReshare($shareId, $token);
208
-				$this->storeRemoteId($shareId, $remoteId);
209
-			} else {
210
-				$this->removeShareFromTable($share);
211
-				$message_t = $this->l->t('File is already shared with %s', [$shareWith]);
212
-				throw new \Exception($message_t);
213
-			}
214
-
215
-		} else {
216
-			$shareId = $this->createFederatedShare($share);
217
-		}
218
-
219
-		$data = $this->getRawShare($shareId);
220
-		return $this->createShareObject($data);
221
-	}
222
-
223
-	/**
224
-	 * create federated share and inform the recipient
225
-	 *
226
-	 * @param IShare $share
227
-	 * @return int
228
-	 * @throws ShareNotFound
229
-	 * @throws \Exception
230
-	 */
231
-	protected function createFederatedShare(IShare $share) {
232
-		$token = $this->tokenHandler->generateToken();
233
-		$shareId = $this->addShareToDB(
234
-			$share->getNodeId(),
235
-			$share->getNodeType(),
236
-			$share->getSharedWith(),
237
-			$share->getSharedBy(),
238
-			$share->getShareOwner(),
239
-			$share->getPermissions(),
240
-			$token
241
-		);
242
-
243
-		$failure = false;
244
-
245
-		try {
246
-			$sharedByFederatedId = $share->getSharedBy();
247
-			if ($this->userManager->userExists($sharedByFederatedId)) {
248
-				$cloudId = $this->cloudIdManager->getCloudId($sharedByFederatedId, $this->addressHandler->generateRemoteURL());
249
-				$sharedByFederatedId = $cloudId->getId();
250
-			}
251
-			$ownerCloudId = $this->cloudIdManager->getCloudId($share->getShareOwner(), $this->addressHandler->generateRemoteURL());
252
-			$send = $this->notifications->sendRemoteShare(
253
-				$token,
254
-				$share->getSharedWith(),
255
-				$share->getNode()->getName(),
256
-				$shareId,
257
-				$share->getShareOwner(),
258
-				$ownerCloudId->getId(),
259
-				$share->getSharedBy(),
260
-				$sharedByFederatedId,
261
-				$share->setShareType()
262
-			);
263
-
264
-			if ($send === false) {
265
-				$failure = true;
266
-			}
267
-		} catch (\Exception $e) {
268
-			$this->logger->logException($e, [
269
-				'message' => 'Failed to notify remote server of federated share, removing share.',
270
-				'level' => ILogger::ERROR,
271
-				'app' => 'federatedfilesharing',
272
-			]);
273
-			$failure = true;
274
-		}
275
-
276
-		if($failure) {
277
-			$this->removeShareFromTableById($shareId);
278
-			$message_t = $this->l->t('Sharing %s failed, could not find %s, maybe the server is currently unreachable or uses a self-signed certificate.',
279
-				[$share->getNode()->getName(), $share->getSharedWith()]);
280
-			throw new \Exception($message_t);
281
-		}
282
-
283
-		return $shareId;
284
-
285
-	}
286
-
287
-	/**
288
-	 * @param string $shareWith
289
-	 * @param IShare $share
290
-	 * @param string $shareId internal share Id
291
-	 * @return array
292
-	 * @throws \Exception
293
-	 */
294
-	protected function askOwnerToReShare($shareWith, IShare $share, $shareId) {
295
-
296
-		$remoteShare = $this->getShareFromExternalShareTable($share);
297
-		$token = $remoteShare['share_token'];
298
-		$remoteId = $remoteShare['remote_id'];
299
-		$remote = $remoteShare['remote'];
300
-
301
-		list($token, $remoteId) = $this->notifications->requestReShare(
302
-			$token,
303
-			$remoteId,
304
-			$shareId,
305
-			$remote,
306
-			$shareWith,
307
-			$share->getPermissions(),
308
-			$share->getNode()->getName()
309
-		);
310
-
311
-		return [$token, $remoteId];
312
-	}
313
-
314
-	/**
315
-	 * get federated share from the share_external table but exclude mounted link shares
316
-	 *
317
-	 * @param IShare $share
318
-	 * @return array
319
-	 * @throws ShareNotFound
320
-	 */
321
-	protected function getShareFromExternalShareTable(IShare $share) {
322
-		$query = $this->dbConnection->getQueryBuilder();
323
-		$query->select('*')->from($this->externalShareTable)
324
-			->where($query->expr()->eq('user', $query->createNamedParameter($share->getShareOwner())))
325
-			->andWhere($query->expr()->eq('mountpoint', $query->createNamedParameter($share->getTarget())));
326
-		$result = $query->execute()->fetchAll();
327
-
328
-		if (isset($result[0]) && (int)$result[0]['remote_id'] > 0) {
329
-			return $result[0];
330
-		}
331
-
332
-		throw new ShareNotFound('share not found in share_external table');
333
-	}
334
-
335
-	/**
336
-	 * add share to the database and return the ID
337
-	 *
338
-	 * @param int $itemSource
339
-	 * @param string $itemType
340
-	 * @param string $shareWith
341
-	 * @param string $sharedBy
342
-	 * @param string $uidOwner
343
-	 * @param int $permissions
344
-	 * @param string $token
345
-	 * @return int
346
-	 */
347
-	private function addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, $token) {
348
-		$qb = $this->dbConnection->getQueryBuilder();
349
-		$qb->insert('share')
350
-			->setValue('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE))
351
-			->setValue('item_type', $qb->createNamedParameter($itemType))
352
-			->setValue('item_source', $qb->createNamedParameter($itemSource))
353
-			->setValue('file_source', $qb->createNamedParameter($itemSource))
354
-			->setValue('share_with', $qb->createNamedParameter($shareWith))
355
-			->setValue('uid_owner', $qb->createNamedParameter($uidOwner))
356
-			->setValue('uid_initiator', $qb->createNamedParameter($sharedBy))
357
-			->setValue('permissions', $qb->createNamedParameter($permissions))
358
-			->setValue('token', $qb->createNamedParameter($token))
359
-			->setValue('stime', $qb->createNamedParameter(time()));
360
-
361
-		/*
163
+        $alreadyShared = $this->getSharedWith($shareWith, self::SHARE_TYPE_REMOTE, $share->getNode(), 1, 0);
164
+        if (!empty($alreadyShared)) {
165
+            $message = 'Sharing %s failed, because this item is already shared with %s';
166
+            $message_t = $this->l->t('Sharing %s failed, because this item is already shared with %s', array($share->getNode()->getName(), $shareWith));
167
+            $this->logger->debug(sprintf($message, $share->getNode()->getName(), $shareWith), ['app' => 'Federated File Sharing']);
168
+            throw new \Exception($message_t);
169
+        }
170
+
171
+
172
+        // don't allow federated shares if source and target server are the same
173
+        $cloudId = $this->cloudIdManager->resolveCloudId($shareWith);
174
+        $currentServer = $this->addressHandler->generateRemoteURL();
175
+        $currentUser = $sharedBy;
176
+        if ($this->addressHandler->compareAddresses($cloudId->getUser(), $cloudId->getRemote(), $currentUser, $currentServer)) {
177
+            $message = 'Not allowed to create a federated share with the same user.';
178
+            $message_t = $this->l->t('Not allowed to create a federated share with the same user');
179
+            $this->logger->debug($message, ['app' => 'Federated File Sharing']);
180
+            throw new \Exception($message_t);
181
+        }
182
+
183
+
184
+        $share->setSharedWith($cloudId->getId());
185
+
186
+        try {
187
+            $remoteShare = $this->getShareFromExternalShareTable($share);
188
+        } catch (ShareNotFound $e) {
189
+            $remoteShare = null;
190
+        }
191
+
192
+        if ($remoteShare) {
193
+            try {
194
+                $ownerCloudId = $this->cloudIdManager->getCloudId($remoteShare['owner'], $remoteShare['remote']);
195
+                $shareId = $this->addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $ownerCloudId->getId(), $permissions, 'tmp_token_' . time());
196
+                $share->setId($shareId);
197
+                list($token, $remoteId) = $this->askOwnerToReShare($shareWith, $share, $shareId);
198
+                // remote share was create successfully if we get a valid token as return
199
+                $send = is_string($token) && $token !== '';
200
+            } catch (\Exception $e) {
201
+                // fall back to old re-share behavior if the remote server
202
+                // doesn't support flat re-shares (was introduced with Nextcloud 9.1)
203
+                $this->removeShareFromTable($share);
204
+                $shareId = $this->createFederatedShare($share);
205
+            }
206
+            if ($send) {
207
+                $this->updateSuccessfulReshare($shareId, $token);
208
+                $this->storeRemoteId($shareId, $remoteId);
209
+            } else {
210
+                $this->removeShareFromTable($share);
211
+                $message_t = $this->l->t('File is already shared with %s', [$shareWith]);
212
+                throw new \Exception($message_t);
213
+            }
214
+
215
+        } else {
216
+            $shareId = $this->createFederatedShare($share);
217
+        }
218
+
219
+        $data = $this->getRawShare($shareId);
220
+        return $this->createShareObject($data);
221
+    }
222
+
223
+    /**
224
+     * create federated share and inform the recipient
225
+     *
226
+     * @param IShare $share
227
+     * @return int
228
+     * @throws ShareNotFound
229
+     * @throws \Exception
230
+     */
231
+    protected function createFederatedShare(IShare $share) {
232
+        $token = $this->tokenHandler->generateToken();
233
+        $shareId = $this->addShareToDB(
234
+            $share->getNodeId(),
235
+            $share->getNodeType(),
236
+            $share->getSharedWith(),
237
+            $share->getSharedBy(),
238
+            $share->getShareOwner(),
239
+            $share->getPermissions(),
240
+            $token
241
+        );
242
+
243
+        $failure = false;
244
+
245
+        try {
246
+            $sharedByFederatedId = $share->getSharedBy();
247
+            if ($this->userManager->userExists($sharedByFederatedId)) {
248
+                $cloudId = $this->cloudIdManager->getCloudId($sharedByFederatedId, $this->addressHandler->generateRemoteURL());
249
+                $sharedByFederatedId = $cloudId->getId();
250
+            }
251
+            $ownerCloudId = $this->cloudIdManager->getCloudId($share->getShareOwner(), $this->addressHandler->generateRemoteURL());
252
+            $send = $this->notifications->sendRemoteShare(
253
+                $token,
254
+                $share->getSharedWith(),
255
+                $share->getNode()->getName(),
256
+                $shareId,
257
+                $share->getShareOwner(),
258
+                $ownerCloudId->getId(),
259
+                $share->getSharedBy(),
260
+                $sharedByFederatedId,
261
+                $share->setShareType()
262
+            );
263
+
264
+            if ($send === false) {
265
+                $failure = true;
266
+            }
267
+        } catch (\Exception $e) {
268
+            $this->logger->logException($e, [
269
+                'message' => 'Failed to notify remote server of federated share, removing share.',
270
+                'level' => ILogger::ERROR,
271
+                'app' => 'federatedfilesharing',
272
+            ]);
273
+            $failure = true;
274
+        }
275
+
276
+        if($failure) {
277
+            $this->removeShareFromTableById($shareId);
278
+            $message_t = $this->l->t('Sharing %s failed, could not find %s, maybe the server is currently unreachable or uses a self-signed certificate.',
279
+                [$share->getNode()->getName(), $share->getSharedWith()]);
280
+            throw new \Exception($message_t);
281
+        }
282
+
283
+        return $shareId;
284
+
285
+    }
286
+
287
+    /**
288
+     * @param string $shareWith
289
+     * @param IShare $share
290
+     * @param string $shareId internal share Id
291
+     * @return array
292
+     * @throws \Exception
293
+     */
294
+    protected function askOwnerToReShare($shareWith, IShare $share, $shareId) {
295
+
296
+        $remoteShare = $this->getShareFromExternalShareTable($share);
297
+        $token = $remoteShare['share_token'];
298
+        $remoteId = $remoteShare['remote_id'];
299
+        $remote = $remoteShare['remote'];
300
+
301
+        list($token, $remoteId) = $this->notifications->requestReShare(
302
+            $token,
303
+            $remoteId,
304
+            $shareId,
305
+            $remote,
306
+            $shareWith,
307
+            $share->getPermissions(),
308
+            $share->getNode()->getName()
309
+        );
310
+
311
+        return [$token, $remoteId];
312
+    }
313
+
314
+    /**
315
+     * get federated share from the share_external table but exclude mounted link shares
316
+     *
317
+     * @param IShare $share
318
+     * @return array
319
+     * @throws ShareNotFound
320
+     */
321
+    protected function getShareFromExternalShareTable(IShare $share) {
322
+        $query = $this->dbConnection->getQueryBuilder();
323
+        $query->select('*')->from($this->externalShareTable)
324
+            ->where($query->expr()->eq('user', $query->createNamedParameter($share->getShareOwner())))
325
+            ->andWhere($query->expr()->eq('mountpoint', $query->createNamedParameter($share->getTarget())));
326
+        $result = $query->execute()->fetchAll();
327
+
328
+        if (isset($result[0]) && (int)$result[0]['remote_id'] > 0) {
329
+            return $result[0];
330
+        }
331
+
332
+        throw new ShareNotFound('share not found in share_external table');
333
+    }
334
+
335
+    /**
336
+     * add share to the database and return the ID
337
+     *
338
+     * @param int $itemSource
339
+     * @param string $itemType
340
+     * @param string $shareWith
341
+     * @param string $sharedBy
342
+     * @param string $uidOwner
343
+     * @param int $permissions
344
+     * @param string $token
345
+     * @return int
346
+     */
347
+    private function addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, $token) {
348
+        $qb = $this->dbConnection->getQueryBuilder();
349
+        $qb->insert('share')
350
+            ->setValue('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE))
351
+            ->setValue('item_type', $qb->createNamedParameter($itemType))
352
+            ->setValue('item_source', $qb->createNamedParameter($itemSource))
353
+            ->setValue('file_source', $qb->createNamedParameter($itemSource))
354
+            ->setValue('share_with', $qb->createNamedParameter($shareWith))
355
+            ->setValue('uid_owner', $qb->createNamedParameter($uidOwner))
356
+            ->setValue('uid_initiator', $qb->createNamedParameter($sharedBy))
357
+            ->setValue('permissions', $qb->createNamedParameter($permissions))
358
+            ->setValue('token', $qb->createNamedParameter($token))
359
+            ->setValue('stime', $qb->createNamedParameter(time()));
360
+
361
+        /*
362 362
 		 * Added to fix https://github.com/owncloud/core/issues/22215
363 363
 		 * Can be removed once we get rid of ajax/share.php
364 364
 		 */
365
-		$qb->setValue('file_target', $qb->createNamedParameter(''));
366
-
367
-		$qb->execute();
368
-		$id = $qb->getLastInsertId();
369
-
370
-		return (int)$id;
371
-	}
372
-
373
-	/**
374
-	 * Update a share
375
-	 *
376
-	 * @param IShare $share
377
-	 * @return IShare The share object
378
-	 */
379
-	public function update(IShare $share) {
380
-		/*
365
+        $qb->setValue('file_target', $qb->createNamedParameter(''));
366
+
367
+        $qb->execute();
368
+        $id = $qb->getLastInsertId();
369
+
370
+        return (int)$id;
371
+    }
372
+
373
+    /**
374
+     * Update a share
375
+     *
376
+     * @param IShare $share
377
+     * @return IShare The share object
378
+     */
379
+    public function update(IShare $share) {
380
+        /*
381 381
 		 * We allow updating the permissions of federated shares
382 382
 		 */
383
-		$qb = $this->dbConnection->getQueryBuilder();
384
-			$qb->update('share')
385
-				->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
386
-				->set('permissions', $qb->createNamedParameter($share->getPermissions()))
387
-				->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
388
-				->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
389
-				->execute();
390
-
391
-		// send the updated permission to the owner/initiator, if they are not the same
392
-		if ($share->getShareOwner() !== $share->getSharedBy()) {
393
-			$this->sendPermissionUpdate($share);
394
-		}
395
-
396
-		return $share;
397
-	}
398
-
399
-	/**
400
-	 * send the updated permission to the owner/initiator, if they are not the same
401
-	 *
402
-	 * @param IShare $share
403
-	 * @throws ShareNotFound
404
-	 * @throws \OC\HintException
405
-	 */
406
-	protected function sendPermissionUpdate(IShare $share) {
407
-		$remoteId = $this->getRemoteId($share);
408
-		// if the local user is the owner we send the permission change to the initiator
409
-		if ($this->userManager->userExists($share->getShareOwner())) {
410
-			list(, $remote) = $this->addressHandler->splitUserRemote($share->getSharedBy());
411
-		} else { // ... if not we send the permission change to the owner
412
-			list(, $remote) = $this->addressHandler->splitUserRemote($share->getShareOwner());
413
-		}
414
-		$this->notifications->sendPermissionChange($remote, $remoteId, $share->getToken(), $share->getPermissions());
415
-	}
416
-
417
-
418
-	/**
419
-	 * update successful reShare with the correct token
420
-	 *
421
-	 * @param int $shareId
422
-	 * @param string $token
423
-	 */
424
-	protected function updateSuccessfulReShare($shareId, $token) {
425
-		$query = $this->dbConnection->getQueryBuilder();
426
-		$query->update('share')
427
-			->where($query->expr()->eq('id', $query->createNamedParameter($shareId)))
428
-			->set('token', $query->createNamedParameter($token))
429
-			->execute();
430
-	}
431
-
432
-	/**
433
-	 * store remote ID in federated reShare table
434
-	 *
435
-	 * @param $shareId
436
-	 * @param $remoteId
437
-	 */
438
-	public function storeRemoteId($shareId, $remoteId) {
439
-		$query = $this->dbConnection->getQueryBuilder();
440
-		$query->insert('federated_reshares')
441
-			->values(
442
-				[
443
-					'share_id' =>  $query->createNamedParameter($shareId),
444
-					'remote_id' => $query->createNamedParameter($remoteId),
445
-				]
446
-			);
447
-		$query->execute();
448
-	}
449
-
450
-	/**
451
-	 * get share ID on remote server for federated re-shares
452
-	 *
453
-	 * @param IShare $share
454
-	 * @return int
455
-	 * @throws ShareNotFound
456
-	 */
457
-	public function getRemoteId(IShare $share) {
458
-		$query = $this->dbConnection->getQueryBuilder();
459
-		$query->select('remote_id')->from('federated_reshares')
460
-			->where($query->expr()->eq('share_id', $query->createNamedParameter((int)$share->getId())));
461
-		$data = $query->execute()->fetch();
462
-
463
-		if (!is_array($data) || !isset($data['remote_id'])) {
464
-			throw new ShareNotFound();
465
-		}
466
-
467
-		return (int)$data['remote_id'];
468
-	}
469
-
470
-	/**
471
-	 * @inheritdoc
472
-	 */
473
-	public function move(IShare $share, $recipient) {
474
-		/*
383
+        $qb = $this->dbConnection->getQueryBuilder();
384
+            $qb->update('share')
385
+                ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
386
+                ->set('permissions', $qb->createNamedParameter($share->getPermissions()))
387
+                ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
388
+                ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
389
+                ->execute();
390
+
391
+        // send the updated permission to the owner/initiator, if they are not the same
392
+        if ($share->getShareOwner() !== $share->getSharedBy()) {
393
+            $this->sendPermissionUpdate($share);
394
+        }
395
+
396
+        return $share;
397
+    }
398
+
399
+    /**
400
+     * send the updated permission to the owner/initiator, if they are not the same
401
+     *
402
+     * @param IShare $share
403
+     * @throws ShareNotFound
404
+     * @throws \OC\HintException
405
+     */
406
+    protected function sendPermissionUpdate(IShare $share) {
407
+        $remoteId = $this->getRemoteId($share);
408
+        // if the local user is the owner we send the permission change to the initiator
409
+        if ($this->userManager->userExists($share->getShareOwner())) {
410
+            list(, $remote) = $this->addressHandler->splitUserRemote($share->getSharedBy());
411
+        } else { // ... if not we send the permission change to the owner
412
+            list(, $remote) = $this->addressHandler->splitUserRemote($share->getShareOwner());
413
+        }
414
+        $this->notifications->sendPermissionChange($remote, $remoteId, $share->getToken(), $share->getPermissions());
415
+    }
416
+
417
+
418
+    /**
419
+     * update successful reShare with the correct token
420
+     *
421
+     * @param int $shareId
422
+     * @param string $token
423
+     */
424
+    protected function updateSuccessfulReShare($shareId, $token) {
425
+        $query = $this->dbConnection->getQueryBuilder();
426
+        $query->update('share')
427
+            ->where($query->expr()->eq('id', $query->createNamedParameter($shareId)))
428
+            ->set('token', $query->createNamedParameter($token))
429
+            ->execute();
430
+    }
431
+
432
+    /**
433
+     * store remote ID in federated reShare table
434
+     *
435
+     * @param $shareId
436
+     * @param $remoteId
437
+     */
438
+    public function storeRemoteId($shareId, $remoteId) {
439
+        $query = $this->dbConnection->getQueryBuilder();
440
+        $query->insert('federated_reshares')
441
+            ->values(
442
+                [
443
+                    'share_id' =>  $query->createNamedParameter($shareId),
444
+                    'remote_id' => $query->createNamedParameter($remoteId),
445
+                ]
446
+            );
447
+        $query->execute();
448
+    }
449
+
450
+    /**
451
+     * get share ID on remote server for federated re-shares
452
+     *
453
+     * @param IShare $share
454
+     * @return int
455
+     * @throws ShareNotFound
456
+     */
457
+    public function getRemoteId(IShare $share) {
458
+        $query = $this->dbConnection->getQueryBuilder();
459
+        $query->select('remote_id')->from('federated_reshares')
460
+            ->where($query->expr()->eq('share_id', $query->createNamedParameter((int)$share->getId())));
461
+        $data = $query->execute()->fetch();
462
+
463
+        if (!is_array($data) || !isset($data['remote_id'])) {
464
+            throw new ShareNotFound();
465
+        }
466
+
467
+        return (int)$data['remote_id'];
468
+    }
469
+
470
+    /**
471
+     * @inheritdoc
472
+     */
473
+    public function move(IShare $share, $recipient) {
474
+        /*
475 475
 		 * This function does nothing yet as it is just for outgoing
476 476
 		 * federated shares.
477 477
 		 */
478
-		return $share;
479
-	}
480
-
481
-	/**
482
-	 * Get all children of this share
483
-	 *
484
-	 * @param IShare $parent
485
-	 * @return IShare[]
486
-	 */
487
-	public function getChildren(IShare $parent) {
488
-		$children = [];
489
-
490
-		$qb = $this->dbConnection->getQueryBuilder();
491
-		$qb->select('*')
492
-			->from('share')
493
-			->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId())))
494
-			->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE)))
495
-			->orderBy('id');
496
-
497
-		$cursor = $qb->execute();
498
-		while($data = $cursor->fetch()) {
499
-			$children[] = $this->createShareObject($data);
500
-		}
501
-		$cursor->closeCursor();
502
-
503
-		return $children;
504
-	}
505
-
506
-	/**
507
-	 * Delete a share (owner unShares the file)
508
-	 *
509
-	 * @param IShare $share
510
-	 * @throws ShareNotFound
511
-	 * @throws \OC\HintException
512
-	 */
513
-	public function delete(IShare $share) {
514
-
515
-		list(, $remote) = $this->addressHandler->splitUserRemote($share->getSharedWith());
516
-
517
-		// if the local user is the owner we can send the unShare request directly...
518
-		if ($this->userManager->userExists($share->getShareOwner())) {
519
-			$this->notifications->sendRemoteUnShare($remote, $share->getId(), $share->getToken());
520
-			$this->revokeShare($share, true);
521
-		} else { // ... if not we need to correct ID for the unShare request
522
-			$remoteId = $this->getRemoteId($share);
523
-			$this->notifications->sendRemoteUnShare($remote, $remoteId, $share->getToken());
524
-			$this->revokeShare($share, false);
525
-		}
526
-
527
-		// only remove the share when all messages are send to not lose information
528
-		// about the share to early
529
-		$this->removeShareFromTable($share);
530
-	}
531
-
532
-	/**
533
-	 * in case of a re-share we need to send the other use (initiator or owner)
534
-	 * a message that the file was unshared
535
-	 *
536
-	 * @param IShare $share
537
-	 * @param bool $isOwner the user can either be the owner or the user who re-sahred it
538
-	 * @throws ShareNotFound
539
-	 * @throws \OC\HintException
540
-	 */
541
-	protected function revokeShare($share, $isOwner) {
542
-		// also send a unShare request to the initiator, if this is a different user than the owner
543
-		if ($share->getShareOwner() !== $share->getSharedBy()) {
544
-			if ($isOwner) {
545
-				list(, $remote) = $this->addressHandler->splitUserRemote($share->getSharedBy());
546
-			} else {
547
-				list(, $remote) = $this->addressHandler->splitUserRemote($share->getShareOwner());
548
-			}
549
-			$remoteId = $this->getRemoteId($share);
550
-			$this->notifications->sendRevokeShare($remote, $remoteId, $share->getToken());
551
-		}
552
-	}
553
-
554
-	/**
555
-	 * remove share from table
556
-	 *
557
-	 * @param IShare $share
558
-	 */
559
-	public function removeShareFromTable(IShare $share) {
560
-		$this->removeShareFromTableById($share->getId());
561
-	}
562
-
563
-	/**
564
-	 * remove share from table
565
-	 *
566
-	 * @param string $shareId
567
-	 */
568
-	private function removeShareFromTableById($shareId) {
569
-		$qb = $this->dbConnection->getQueryBuilder();
570
-		$qb->delete('share')
571
-			->where($qb->expr()->eq('id', $qb->createNamedParameter($shareId)));
572
-		$qb->execute();
573
-
574
-		$qb->delete('federated_reshares')
575
-			->where($qb->expr()->eq('share_id', $qb->createNamedParameter($shareId)));
576
-		$qb->execute();
577
-	}
578
-
579
-	/**
580
-	 * @inheritdoc
581
-	 */
582
-	public function deleteFromSelf(IShare $share, $recipient) {
583
-		// nothing to do here. Technically deleteFromSelf in the context of federated
584
-		// shares is a umount of a external storage. This is handled here
585
-		// apps/files_sharing/lib/external/manager.php
586
-		// TODO move this code over to this app
587
-	}
588
-
589
-
590
-	public function getSharesInFolder($userId, Folder $node, $reshares) {
591
-		$qb = $this->dbConnection->getQueryBuilder();
592
-		$qb->select('*')
593
-			->from('share', 's')
594
-			->andWhere($qb->expr()->orX(
595
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
596
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
597
-			))
598
-			->andWhere(
599
-				$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_REMOTE))
600
-			);
601
-
602
-		/**
603
-		 * Reshares for this user are shares where they are the owner.
604
-		 */
605
-		if ($reshares === false) {
606
-			$qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
607
-		} else {
608
-			$qb->andWhere(
609
-				$qb->expr()->orX(
610
-					$qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
611
-					$qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
612
-				)
613
-			);
614
-		}
615
-
616
-		$qb->innerJoin('s', 'filecache' ,'f', $qb->expr()->eq('s.file_source', 'f.fileid'));
617
-		$qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())));
618
-
619
-		$qb->orderBy('id');
620
-
621
-		$cursor = $qb->execute();
622
-		$shares = [];
623
-		while ($data = $cursor->fetch()) {
624
-			$shares[$data['fileid']][] = $this->createShareObject($data);
625
-		}
626
-		$cursor->closeCursor();
627
-
628
-		return $shares;
629
-	}
630
-
631
-	/**
632
-	 * @inheritdoc
633
-	 */
634
-	public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) {
635
-		$qb = $this->dbConnection->getQueryBuilder();
636
-		$qb->select('*')
637
-			->from('share');
638
-
639
-		$qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE)));
640
-
641
-		/**
642
-		 * Reshares for this user are shares where they are the owner.
643
-		 */
644
-		if ($reshares === false) {
645
-			//Special case for old shares created via the web UI
646
-			$or1 = $qb->expr()->andX(
647
-				$qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
648
-				$qb->expr()->isNull('uid_initiator')
649
-			);
650
-
651
-			$qb->andWhere(
652
-				$qb->expr()->orX(
653
-					$qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)),
654
-					$or1
655
-				)
656
-			);
657
-		} else {
658
-			$qb->andWhere(
659
-				$qb->expr()->orX(
660
-					$qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
661
-					$qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
662
-				)
663
-			);
664
-		}
665
-
666
-		if ($node !== null) {
667
-			$qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
668
-		}
669
-
670
-		if ($limit !== -1) {
671
-			$qb->setMaxResults($limit);
672
-		}
673
-
674
-		$qb->setFirstResult($offset);
675
-		$qb->orderBy('id');
676
-
677
-		$cursor = $qb->execute();
678
-		$shares = [];
679
-		while($data = $cursor->fetch()) {
680
-			$shares[] = $this->createShareObject($data);
681
-		}
682
-		$cursor->closeCursor();
683
-
684
-		return $shares;
685
-	}
686
-
687
-	/**
688
-	 * @inheritdoc
689
-	 */
690
-	public function getShareById($id, $recipientId = null) {
691
-		$qb = $this->dbConnection->getQueryBuilder();
692
-
693
-		$qb->select('*')
694
-			->from('share')
695
-			->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
696
-			->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE)));
697
-
698
-		$cursor = $qb->execute();
699
-		$data = $cursor->fetch();
700
-		$cursor->closeCursor();
701
-
702
-		if ($data === false) {
703
-			throw new ShareNotFound('Can not find share with ID: ' . $id);
704
-		}
705
-
706
-		try {
707
-			$share = $this->createShareObject($data);
708
-		} catch (InvalidShare $e) {
709
-			throw new ShareNotFound();
710
-		}
711
-
712
-		return $share;
713
-	}
714
-
715
-	/**
716
-	 * Get shares for a given path
717
-	 *
718
-	 * @param \OCP\Files\Node $path
719
-	 * @return IShare[]
720
-	 */
721
-	public function getSharesByPath(Node $path) {
722
-		$qb = $this->dbConnection->getQueryBuilder();
723
-
724
-		$cursor = $qb->select('*')
725
-			->from('share')
726
-			->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId())))
727
-			->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE)))
728
-			->execute();
729
-
730
-		$shares = [];
731
-		while($data = $cursor->fetch()) {
732
-			$shares[] = $this->createShareObject($data);
733
-		}
734
-		$cursor->closeCursor();
735
-
736
-		return $shares;
737
-	}
738
-
739
-	/**
740
-	 * @inheritdoc
741
-	 */
742
-	public function getSharedWith($userId, $shareType, $node, $limit, $offset) {
743
-		/** @var IShare[] $shares */
744
-		$shares = [];
745
-
746
-		//Get shares directly with this user
747
-		$qb = $this->dbConnection->getQueryBuilder();
748
-		$qb->select('*')
749
-			->from('share');
750
-
751
-		// Order by id
752
-		$qb->orderBy('id');
753
-
754
-		// Set limit and offset
755
-		if ($limit !== -1) {
756
-			$qb->setMaxResults($limit);
757
-		}
758
-		$qb->setFirstResult($offset);
759
-
760
-		$qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE)));
761
-		$qb->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)));
762
-
763
-		// Filter by node if provided
764
-		if ($node !== null) {
765
-			$qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
766
-		}
767
-
768
-		$cursor = $qb->execute();
769
-
770
-		while($data = $cursor->fetch()) {
771
-			$shares[] = $this->createShareObject($data);
772
-		}
773
-		$cursor->closeCursor();
774
-
775
-
776
-		return $shares;
777
-	}
778
-
779
-	/**
780
-	 * Get a share by token
781
-	 *
782
-	 * @param string $token
783
-	 * @return IShare
784
-	 * @throws ShareNotFound
785
-	 */
786
-	public function getShareByToken($token) {
787
-		$qb = $this->dbConnection->getQueryBuilder();
788
-
789
-		$cursor = $qb->select('*')
790
-			->from('share')
791
-			->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE)))
792
-			->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token)))
793
-			->execute();
794
-
795
-		$data = $cursor->fetch();
796
-
797
-		if ($data === false) {
798
-			throw new ShareNotFound('Share not found', $this->l->t('Could not find share'));
799
-		}
800
-
801
-		try {
802
-			$share = $this->createShareObject($data);
803
-		} catch (InvalidShare $e) {
804
-			throw new ShareNotFound('Share not found', $this->l->t('Could not find share'));
805
-		}
806
-
807
-		return $share;
808
-	}
809
-
810
-	/**
811
-	 * get database row of a give share
812
-	 *
813
-	 * @param $id
814
-	 * @return array
815
-	 * @throws ShareNotFound
816
-	 */
817
-	private function getRawShare($id) {
818
-
819
-		// Now fetch the inserted share and create a complete share object
820
-		$qb = $this->dbConnection->getQueryBuilder();
821
-		$qb->select('*')
822
-			->from('share')
823
-			->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
824
-
825
-		$cursor = $qb->execute();
826
-		$data = $cursor->fetch();
827
-		$cursor->closeCursor();
828
-
829
-		if ($data === false) {
830
-			throw new ShareNotFound;
831
-		}
832
-
833
-		return $data;
834
-	}
835
-
836
-	/**
837
-	 * Create a share object from an database row
838
-	 *
839
-	 * @param array $data
840
-	 * @return IShare
841
-	 * @throws InvalidShare
842
-	 * @throws ShareNotFound
843
-	 */
844
-	private function createShareObject($data) {
845
-
846
-		$share = new Share($this->rootFolder, $this->userManager);
847
-		$share->setId((int)$data['id'])
848
-			->setShareType((int)$data['share_type'])
849
-			->setPermissions((int)$data['permissions'])
850
-			->setTarget($data['file_target'])
851
-			->setMailSend((bool)$data['mail_send'])
852
-			->setToken($data['token']);
853
-
854
-		$shareTime = new \DateTime();
855
-		$shareTime->setTimestamp((int)$data['stime']);
856
-		$share->setShareTime($shareTime);
857
-		$share->setSharedWith($data['share_with']);
858
-
859
-		if ($data['uid_initiator'] !== null) {
860
-			$share->setShareOwner($data['uid_owner']);
861
-			$share->setSharedBy($data['uid_initiator']);
862
-		} else {
863
-			//OLD SHARE
864
-			$share->setSharedBy($data['uid_owner']);
865
-			$path = $this->getNode($share->getSharedBy(), (int)$data['file_source']);
866
-
867
-			$owner = $path->getOwner();
868
-			$share->setShareOwner($owner->getUID());
869
-		}
870
-
871
-		$share->setNodeId((int)$data['file_source']);
872
-		$share->setNodeType($data['item_type']);
873
-
874
-		$share->setProviderId($this->identifier());
875
-
876
-		return $share;
877
-	}
878
-
879
-	/**
880
-	 * Get the node with file $id for $user
881
-	 *
882
-	 * @param string $userId
883
-	 * @param int $id
884
-	 * @return \OCP\Files\File|\OCP\Files\Folder
885
-	 * @throws InvalidShare
886
-	 */
887
-	private function getNode($userId, $id) {
888
-		try {
889
-			$userFolder = $this->rootFolder->getUserFolder($userId);
890
-		} catch (NotFoundException $e) {
891
-			throw new InvalidShare();
892
-		}
893
-
894
-		$nodes = $userFolder->getById($id);
895
-
896
-		if (empty($nodes)) {
897
-			throw new InvalidShare();
898
-		}
899
-
900
-		return $nodes[0];
901
-	}
902
-
903
-	/**
904
-	 * A user is deleted from the system
905
-	 * So clean up the relevant shares.
906
-	 *
907
-	 * @param string $uid
908
-	 * @param int $shareType
909
-	 */
910
-	public function userDeleted($uid, $shareType) {
911
-		//TODO: probabaly a good idea to send unshare info to remote servers
912
-
913
-		$qb = $this->dbConnection->getQueryBuilder();
914
-
915
-		$qb->delete('share')
916
-			->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_REMOTE)))
917
-			->andWhere($qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)))
918
-			->execute();
919
-	}
920
-
921
-	/**
922
-	 * This provider does not handle groups
923
-	 *
924
-	 * @param string $gid
925
-	 */
926
-	public function groupDeleted($gid) {
927
-		// We don't handle groups here
928
-	}
929
-
930
-	/**
931
-	 * This provider does not handle groups
932
-	 *
933
-	 * @param string $uid
934
-	 * @param string $gid
935
-	 */
936
-	public function userDeletedFromGroup($uid, $gid) {
937
-		// We don't handle groups here
938
-	}
939
-
940
-	/**
941
-	 * check if users from other Nextcloud instances are allowed to mount public links share by this instance
942
-	 *
943
-	 * @return bool
944
-	 */
945
-	public function isOutgoingServer2serverShareEnabled() {
946
-		if ($this->gsConfig->onlyInternalFederation()) {
947
-			return false;
948
-		}
949
-		$result = $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes');
950
-		return ($result === 'yes');
951
-	}
952
-
953
-	/**
954
-	 * check if users are allowed to mount public links from other Nextclouds
955
-	 *
956
-	 * @return bool
957
-	 */
958
-	public function isIncomingServer2serverShareEnabled() {
959
-		if ($this->gsConfig->onlyInternalFederation()) {
960
-			return false;
961
-		}
962
-		$result = $this->config->getAppValue('files_sharing', 'incoming_server2server_share_enabled', 'yes');
963
-		return ($result === 'yes');
964
-	}
965
-
966
-	/**
967
-	 * Check if querying sharees on the lookup server is enabled
968
-	 *
969
-	 * @return bool
970
-	 */
971
-	public function isLookupServerQueriesEnabled() {
972
-		// in a global scale setup we should always query the lookup server
973
-		if ($this->gsConfig->isGlobalScaleEnabled()) {
974
-			return true;
975
-		}
976
-		$result = $this->config->getAppValue('files_sharing', 'lookupServerEnabled', 'no');
977
-		return ($result === 'yes');
978
-	}
979
-
980
-
981
-	/**
982
-	 * Check if it is allowed to publish user specific data to the lookup server
983
-	 *
984
-	 * @return bool
985
-	 */
986
-	public function isLookupServerUploadEnabled() {
987
-		// in a global scale setup the admin is responsible to keep the lookup server up-to-date
988
-		if ($this->gsConfig->isGlobalScaleEnabled()) {
989
-			return false;
990
-		}
991
-		$result = $this->config->getAppValue('files_sharing', 'lookupServerUploadEnabled', 'yes');
992
-		return ($result === 'yes');
993
-	}
994
-
995
-	/**
996
-	 * @inheritdoc
997
-	 */
998
-	public function getAccessList($nodes, $currentAccess) {
999
-		$ids = [];
1000
-		foreach ($nodes as $node) {
1001
-			$ids[] = $node->getId();
1002
-		}
1003
-
1004
-		$qb = $this->dbConnection->getQueryBuilder();
1005
-		$qb->select('share_with', 'token', 'file_source')
1006
-			->from('share')
1007
-			->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_REMOTE)))
1008
-			->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
1009
-			->andWhere($qb->expr()->orX(
1010
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
1011
-				$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
1012
-			));
1013
-		$cursor = $qb->execute();
1014
-
1015
-		if ($currentAccess === false) {
1016
-			$remote = $cursor->fetch() !== false;
1017
-			$cursor->closeCursor();
1018
-
1019
-			return ['remote' => $remote];
1020
-		}
1021
-
1022
-		$remote = [];
1023
-		while ($row = $cursor->fetch()) {
1024
-			$remote[$row['share_with']] = [
1025
-				'node_id' => $row['file_source'],
1026
-				'token' => $row['token'],
1027
-			];
1028
-		}
1029
-		$cursor->closeCursor();
1030
-
1031
-		return ['remote' => $remote];
1032
-	}
478
+        return $share;
479
+    }
480
+
481
+    /**
482
+     * Get all children of this share
483
+     *
484
+     * @param IShare $parent
485
+     * @return IShare[]
486
+     */
487
+    public function getChildren(IShare $parent) {
488
+        $children = [];
489
+
490
+        $qb = $this->dbConnection->getQueryBuilder();
491
+        $qb->select('*')
492
+            ->from('share')
493
+            ->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId())))
494
+            ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE)))
495
+            ->orderBy('id');
496
+
497
+        $cursor = $qb->execute();
498
+        while($data = $cursor->fetch()) {
499
+            $children[] = $this->createShareObject($data);
500
+        }
501
+        $cursor->closeCursor();
502
+
503
+        return $children;
504
+    }
505
+
506
+    /**
507
+     * Delete a share (owner unShares the file)
508
+     *
509
+     * @param IShare $share
510
+     * @throws ShareNotFound
511
+     * @throws \OC\HintException
512
+     */
513
+    public function delete(IShare $share) {
514
+
515
+        list(, $remote) = $this->addressHandler->splitUserRemote($share->getSharedWith());
516
+
517
+        // if the local user is the owner we can send the unShare request directly...
518
+        if ($this->userManager->userExists($share->getShareOwner())) {
519
+            $this->notifications->sendRemoteUnShare($remote, $share->getId(), $share->getToken());
520
+            $this->revokeShare($share, true);
521
+        } else { // ... if not we need to correct ID for the unShare request
522
+            $remoteId = $this->getRemoteId($share);
523
+            $this->notifications->sendRemoteUnShare($remote, $remoteId, $share->getToken());
524
+            $this->revokeShare($share, false);
525
+        }
526
+
527
+        // only remove the share when all messages are send to not lose information
528
+        // about the share to early
529
+        $this->removeShareFromTable($share);
530
+    }
531
+
532
+    /**
533
+     * in case of a re-share we need to send the other use (initiator or owner)
534
+     * a message that the file was unshared
535
+     *
536
+     * @param IShare $share
537
+     * @param bool $isOwner the user can either be the owner or the user who re-sahred it
538
+     * @throws ShareNotFound
539
+     * @throws \OC\HintException
540
+     */
541
+    protected function revokeShare($share, $isOwner) {
542
+        // also send a unShare request to the initiator, if this is a different user than the owner
543
+        if ($share->getShareOwner() !== $share->getSharedBy()) {
544
+            if ($isOwner) {
545
+                list(, $remote) = $this->addressHandler->splitUserRemote($share->getSharedBy());
546
+            } else {
547
+                list(, $remote) = $this->addressHandler->splitUserRemote($share->getShareOwner());
548
+            }
549
+            $remoteId = $this->getRemoteId($share);
550
+            $this->notifications->sendRevokeShare($remote, $remoteId, $share->getToken());
551
+        }
552
+    }
553
+
554
+    /**
555
+     * remove share from table
556
+     *
557
+     * @param IShare $share
558
+     */
559
+    public function removeShareFromTable(IShare $share) {
560
+        $this->removeShareFromTableById($share->getId());
561
+    }
562
+
563
+    /**
564
+     * remove share from table
565
+     *
566
+     * @param string $shareId
567
+     */
568
+    private function removeShareFromTableById($shareId) {
569
+        $qb = $this->dbConnection->getQueryBuilder();
570
+        $qb->delete('share')
571
+            ->where($qb->expr()->eq('id', $qb->createNamedParameter($shareId)));
572
+        $qb->execute();
573
+
574
+        $qb->delete('federated_reshares')
575
+            ->where($qb->expr()->eq('share_id', $qb->createNamedParameter($shareId)));
576
+        $qb->execute();
577
+    }
578
+
579
+    /**
580
+     * @inheritdoc
581
+     */
582
+    public function deleteFromSelf(IShare $share, $recipient) {
583
+        // nothing to do here. Technically deleteFromSelf in the context of federated
584
+        // shares is a umount of a external storage. This is handled here
585
+        // apps/files_sharing/lib/external/manager.php
586
+        // TODO move this code over to this app
587
+    }
588
+
589
+
590
+    public function getSharesInFolder($userId, Folder $node, $reshares) {
591
+        $qb = $this->dbConnection->getQueryBuilder();
592
+        $qb->select('*')
593
+            ->from('share', 's')
594
+            ->andWhere($qb->expr()->orX(
595
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
596
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
597
+            ))
598
+            ->andWhere(
599
+                $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_REMOTE))
600
+            );
601
+
602
+        /**
603
+         * Reshares for this user are shares where they are the owner.
604
+         */
605
+        if ($reshares === false) {
606
+            $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
607
+        } else {
608
+            $qb->andWhere(
609
+                $qb->expr()->orX(
610
+                    $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
611
+                    $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
612
+                )
613
+            );
614
+        }
615
+
616
+        $qb->innerJoin('s', 'filecache' ,'f', $qb->expr()->eq('s.file_source', 'f.fileid'));
617
+        $qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())));
618
+
619
+        $qb->orderBy('id');
620
+
621
+        $cursor = $qb->execute();
622
+        $shares = [];
623
+        while ($data = $cursor->fetch()) {
624
+            $shares[$data['fileid']][] = $this->createShareObject($data);
625
+        }
626
+        $cursor->closeCursor();
627
+
628
+        return $shares;
629
+    }
630
+
631
+    /**
632
+     * @inheritdoc
633
+     */
634
+    public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) {
635
+        $qb = $this->dbConnection->getQueryBuilder();
636
+        $qb->select('*')
637
+            ->from('share');
638
+
639
+        $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE)));
640
+
641
+        /**
642
+         * Reshares for this user are shares where they are the owner.
643
+         */
644
+        if ($reshares === false) {
645
+            //Special case for old shares created via the web UI
646
+            $or1 = $qb->expr()->andX(
647
+                $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
648
+                $qb->expr()->isNull('uid_initiator')
649
+            );
650
+
651
+            $qb->andWhere(
652
+                $qb->expr()->orX(
653
+                    $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)),
654
+                    $or1
655
+                )
656
+            );
657
+        } else {
658
+            $qb->andWhere(
659
+                $qb->expr()->orX(
660
+                    $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
661
+                    $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
662
+                )
663
+            );
664
+        }
665
+
666
+        if ($node !== null) {
667
+            $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
668
+        }
669
+
670
+        if ($limit !== -1) {
671
+            $qb->setMaxResults($limit);
672
+        }
673
+
674
+        $qb->setFirstResult($offset);
675
+        $qb->orderBy('id');
676
+
677
+        $cursor = $qb->execute();
678
+        $shares = [];
679
+        while($data = $cursor->fetch()) {
680
+            $shares[] = $this->createShareObject($data);
681
+        }
682
+        $cursor->closeCursor();
683
+
684
+        return $shares;
685
+    }
686
+
687
+    /**
688
+     * @inheritdoc
689
+     */
690
+    public function getShareById($id, $recipientId = null) {
691
+        $qb = $this->dbConnection->getQueryBuilder();
692
+
693
+        $qb->select('*')
694
+            ->from('share')
695
+            ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
696
+            ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE)));
697
+
698
+        $cursor = $qb->execute();
699
+        $data = $cursor->fetch();
700
+        $cursor->closeCursor();
701
+
702
+        if ($data === false) {
703
+            throw new ShareNotFound('Can not find share with ID: ' . $id);
704
+        }
705
+
706
+        try {
707
+            $share = $this->createShareObject($data);
708
+        } catch (InvalidShare $e) {
709
+            throw new ShareNotFound();
710
+        }
711
+
712
+        return $share;
713
+    }
714
+
715
+    /**
716
+     * Get shares for a given path
717
+     *
718
+     * @param \OCP\Files\Node $path
719
+     * @return IShare[]
720
+     */
721
+    public function getSharesByPath(Node $path) {
722
+        $qb = $this->dbConnection->getQueryBuilder();
723
+
724
+        $cursor = $qb->select('*')
725
+            ->from('share')
726
+            ->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId())))
727
+            ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE)))
728
+            ->execute();
729
+
730
+        $shares = [];
731
+        while($data = $cursor->fetch()) {
732
+            $shares[] = $this->createShareObject($data);
733
+        }
734
+        $cursor->closeCursor();
735
+
736
+        return $shares;
737
+    }
738
+
739
+    /**
740
+     * @inheritdoc
741
+     */
742
+    public function getSharedWith($userId, $shareType, $node, $limit, $offset) {
743
+        /** @var IShare[] $shares */
744
+        $shares = [];
745
+
746
+        //Get shares directly with this user
747
+        $qb = $this->dbConnection->getQueryBuilder();
748
+        $qb->select('*')
749
+            ->from('share');
750
+
751
+        // Order by id
752
+        $qb->orderBy('id');
753
+
754
+        // Set limit and offset
755
+        if ($limit !== -1) {
756
+            $qb->setMaxResults($limit);
757
+        }
758
+        $qb->setFirstResult($offset);
759
+
760
+        $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE)));
761
+        $qb->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)));
762
+
763
+        // Filter by node if provided
764
+        if ($node !== null) {
765
+            $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
766
+        }
767
+
768
+        $cursor = $qb->execute();
769
+
770
+        while($data = $cursor->fetch()) {
771
+            $shares[] = $this->createShareObject($data);
772
+        }
773
+        $cursor->closeCursor();
774
+
775
+
776
+        return $shares;
777
+    }
778
+
779
+    /**
780
+     * Get a share by token
781
+     *
782
+     * @param string $token
783
+     * @return IShare
784
+     * @throws ShareNotFound
785
+     */
786
+    public function getShareByToken($token) {
787
+        $qb = $this->dbConnection->getQueryBuilder();
788
+
789
+        $cursor = $qb->select('*')
790
+            ->from('share')
791
+            ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE)))
792
+            ->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token)))
793
+            ->execute();
794
+
795
+        $data = $cursor->fetch();
796
+
797
+        if ($data === false) {
798
+            throw new ShareNotFound('Share not found', $this->l->t('Could not find share'));
799
+        }
800
+
801
+        try {
802
+            $share = $this->createShareObject($data);
803
+        } catch (InvalidShare $e) {
804
+            throw new ShareNotFound('Share not found', $this->l->t('Could not find share'));
805
+        }
806
+
807
+        return $share;
808
+    }
809
+
810
+    /**
811
+     * get database row of a give share
812
+     *
813
+     * @param $id
814
+     * @return array
815
+     * @throws ShareNotFound
816
+     */
817
+    private function getRawShare($id) {
818
+
819
+        // Now fetch the inserted share and create a complete share object
820
+        $qb = $this->dbConnection->getQueryBuilder();
821
+        $qb->select('*')
822
+            ->from('share')
823
+            ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
824
+
825
+        $cursor = $qb->execute();
826
+        $data = $cursor->fetch();
827
+        $cursor->closeCursor();
828
+
829
+        if ($data === false) {
830
+            throw new ShareNotFound;
831
+        }
832
+
833
+        return $data;
834
+    }
835
+
836
+    /**
837
+     * Create a share object from an database row
838
+     *
839
+     * @param array $data
840
+     * @return IShare
841
+     * @throws InvalidShare
842
+     * @throws ShareNotFound
843
+     */
844
+    private function createShareObject($data) {
845
+
846
+        $share = new Share($this->rootFolder, $this->userManager);
847
+        $share->setId((int)$data['id'])
848
+            ->setShareType((int)$data['share_type'])
849
+            ->setPermissions((int)$data['permissions'])
850
+            ->setTarget($data['file_target'])
851
+            ->setMailSend((bool)$data['mail_send'])
852
+            ->setToken($data['token']);
853
+
854
+        $shareTime = new \DateTime();
855
+        $shareTime->setTimestamp((int)$data['stime']);
856
+        $share->setShareTime($shareTime);
857
+        $share->setSharedWith($data['share_with']);
858
+
859
+        if ($data['uid_initiator'] !== null) {
860
+            $share->setShareOwner($data['uid_owner']);
861
+            $share->setSharedBy($data['uid_initiator']);
862
+        } else {
863
+            //OLD SHARE
864
+            $share->setSharedBy($data['uid_owner']);
865
+            $path = $this->getNode($share->getSharedBy(), (int)$data['file_source']);
866
+
867
+            $owner = $path->getOwner();
868
+            $share->setShareOwner($owner->getUID());
869
+        }
870
+
871
+        $share->setNodeId((int)$data['file_source']);
872
+        $share->setNodeType($data['item_type']);
873
+
874
+        $share->setProviderId($this->identifier());
875
+
876
+        return $share;
877
+    }
878
+
879
+    /**
880
+     * Get the node with file $id for $user
881
+     *
882
+     * @param string $userId
883
+     * @param int $id
884
+     * @return \OCP\Files\File|\OCP\Files\Folder
885
+     * @throws InvalidShare
886
+     */
887
+    private function getNode($userId, $id) {
888
+        try {
889
+            $userFolder = $this->rootFolder->getUserFolder($userId);
890
+        } catch (NotFoundException $e) {
891
+            throw new InvalidShare();
892
+        }
893
+
894
+        $nodes = $userFolder->getById($id);
895
+
896
+        if (empty($nodes)) {
897
+            throw new InvalidShare();
898
+        }
899
+
900
+        return $nodes[0];
901
+    }
902
+
903
+    /**
904
+     * A user is deleted from the system
905
+     * So clean up the relevant shares.
906
+     *
907
+     * @param string $uid
908
+     * @param int $shareType
909
+     */
910
+    public function userDeleted($uid, $shareType) {
911
+        //TODO: probabaly a good idea to send unshare info to remote servers
912
+
913
+        $qb = $this->dbConnection->getQueryBuilder();
914
+
915
+        $qb->delete('share')
916
+            ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_REMOTE)))
917
+            ->andWhere($qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)))
918
+            ->execute();
919
+    }
920
+
921
+    /**
922
+     * This provider does not handle groups
923
+     *
924
+     * @param string $gid
925
+     */
926
+    public function groupDeleted($gid) {
927
+        // We don't handle groups here
928
+    }
929
+
930
+    /**
931
+     * This provider does not handle groups
932
+     *
933
+     * @param string $uid
934
+     * @param string $gid
935
+     */
936
+    public function userDeletedFromGroup($uid, $gid) {
937
+        // We don't handle groups here
938
+    }
939
+
940
+    /**
941
+     * check if users from other Nextcloud instances are allowed to mount public links share by this instance
942
+     *
943
+     * @return bool
944
+     */
945
+    public function isOutgoingServer2serverShareEnabled() {
946
+        if ($this->gsConfig->onlyInternalFederation()) {
947
+            return false;
948
+        }
949
+        $result = $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes');
950
+        return ($result === 'yes');
951
+    }
952
+
953
+    /**
954
+     * check if users are allowed to mount public links from other Nextclouds
955
+     *
956
+     * @return bool
957
+     */
958
+    public function isIncomingServer2serverShareEnabled() {
959
+        if ($this->gsConfig->onlyInternalFederation()) {
960
+            return false;
961
+        }
962
+        $result = $this->config->getAppValue('files_sharing', 'incoming_server2server_share_enabled', 'yes');
963
+        return ($result === 'yes');
964
+    }
965
+
966
+    /**
967
+     * Check if querying sharees on the lookup server is enabled
968
+     *
969
+     * @return bool
970
+     */
971
+    public function isLookupServerQueriesEnabled() {
972
+        // in a global scale setup we should always query the lookup server
973
+        if ($this->gsConfig->isGlobalScaleEnabled()) {
974
+            return true;
975
+        }
976
+        $result = $this->config->getAppValue('files_sharing', 'lookupServerEnabled', 'no');
977
+        return ($result === 'yes');
978
+    }
979
+
980
+
981
+    /**
982
+     * Check if it is allowed to publish user specific data to the lookup server
983
+     *
984
+     * @return bool
985
+     */
986
+    public function isLookupServerUploadEnabled() {
987
+        // in a global scale setup the admin is responsible to keep the lookup server up-to-date
988
+        if ($this->gsConfig->isGlobalScaleEnabled()) {
989
+            return false;
990
+        }
991
+        $result = $this->config->getAppValue('files_sharing', 'lookupServerUploadEnabled', 'yes');
992
+        return ($result === 'yes');
993
+    }
994
+
995
+    /**
996
+     * @inheritdoc
997
+     */
998
+    public function getAccessList($nodes, $currentAccess) {
999
+        $ids = [];
1000
+        foreach ($nodes as $node) {
1001
+            $ids[] = $node->getId();
1002
+        }
1003
+
1004
+        $qb = $this->dbConnection->getQueryBuilder();
1005
+        $qb->select('share_with', 'token', 'file_source')
1006
+            ->from('share')
1007
+            ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_REMOTE)))
1008
+            ->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
1009
+            ->andWhere($qb->expr()->orX(
1010
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
1011
+                $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
1012
+            ));
1013
+        $cursor = $qb->execute();
1014
+
1015
+        if ($currentAccess === false) {
1016
+            $remote = $cursor->fetch() !== false;
1017
+            $cursor->closeCursor();
1018
+
1019
+            return ['remote' => $remote];
1020
+        }
1021
+
1022
+        $remote = [];
1023
+        while ($row = $cursor->fetch()) {
1024
+            $remote[$row['share_with']] = [
1025
+                'node_id' => $row['file_source'],
1026
+                'token' => $row['token'],
1027
+            ];
1028
+        }
1029
+        $cursor->closeCursor();
1030
+
1031
+        return ['remote' => $remote];
1032
+    }
1033 1033
 }
Please login to merge, or discard this patch.
apps/federatedfilesharing/lib/ocm/CloudFederationProviderFiles.php 1 patch
Indentation   +763 added lines, -763 removed lines patch added patch discarded remove patch
@@ -52,767 +52,767 @@
 block discarded – undo
52 52
 
53 53
 class CloudFederationProviderFiles implements ICloudFederationProvider {
54 54
 
55
-	/** @var IAppManager */
56
-	private $appManager;
57
-
58
-	/** @var FederatedShareProvider */
59
-	private $federatedShareProvider;
60
-
61
-	/** @var AddressHandler */
62
-	private $addressHandler;
63
-
64
-	/** @var ILogger */
65
-	private $logger;
66
-
67
-	/** @var IUserManager */
68
-	private $userManager;
69
-
70
-	/** @var ICloudIdManager */
71
-	private $cloudIdManager;
72
-
73
-	/** @var IActivityManager */
74
-	private $activityManager;
75
-
76
-	/** @var INotificationManager */
77
-	private $notificationManager;
78
-
79
-	/** @var IURLGenerator */
80
-	private $urlGenerator;
81
-
82
-	/** @var ICloudFederationFactory */
83
-	private $cloudFederationFactory;
84
-
85
-	/** @var ICloudFederationProviderManager */
86
-	private $cloudFederationProviderManager;
87
-
88
-	/** @var IDBConnection */
89
-	private $connection;
90
-
91
-	/** @var IGroupManager */
92
-	private $groupManager;
93
-
94
-	/**
95
-	 * CloudFederationProvider constructor.
96
-	 *
97
-	 * @param IAppManager $appManager
98
-	 * @param FederatedShareProvider $federatedShareProvider
99
-	 * @param AddressHandler $addressHandler
100
-	 * @param ILogger $logger
101
-	 * @param IUserManager $userManager
102
-	 * @param ICloudIdManager $cloudIdManager
103
-	 * @param IActivityManager $activityManager
104
-	 * @param INotificationManager $notificationManager
105
-	 * @param IURLGenerator $urlGenerator
106
-	 * @param ICloudFederationFactory $cloudFederationFactory
107
-	 * @param ICloudFederationProviderManager $cloudFederationProviderManager
108
-	 * @param IDBConnection $connection
109
-	 * @param IGroupManager $groupManager
110
-	 */
111
-	public function __construct(IAppManager $appManager,
112
-								FederatedShareProvider $federatedShareProvider,
113
-								AddressHandler $addressHandler,
114
-								ILogger $logger,
115
-								IUserManager $userManager,
116
-								ICloudIdManager $cloudIdManager,
117
-								IActivityManager $activityManager,
118
-								INotificationManager $notificationManager,
119
-								IURLGenerator $urlGenerator,
120
-								ICloudFederationFactory $cloudFederationFactory,
121
-								ICloudFederationProviderManager $cloudFederationProviderManager,
122
-								IDBConnection $connection,
123
-								IGroupManager $groupManager
124
-	) {
125
-		$this->appManager = $appManager;
126
-		$this->federatedShareProvider = $federatedShareProvider;
127
-		$this->addressHandler = $addressHandler;
128
-		$this->logger = $logger;
129
-		$this->userManager = $userManager;
130
-		$this->cloudIdManager = $cloudIdManager;
131
-		$this->activityManager = $activityManager;
132
-		$this->notificationManager = $notificationManager;
133
-		$this->urlGenerator = $urlGenerator;
134
-		$this->cloudFederationFactory = $cloudFederationFactory;
135
-		$this->cloudFederationProviderManager = $cloudFederationProviderManager;
136
-		$this->connection = $connection;
137
-		$this->groupManager = $groupManager;
138
-	}
139
-
140
-
141
-
142
-	/**
143
-	 * @return string
144
-	 */
145
-	public function getShareType() {
146
-		return 'file';
147
-	}
148
-
149
-	/**
150
-	 * share received from another server
151
-	 *
152
-	 * @param ICloudFederationShare $share
153
-	 * @return string provider specific unique ID of the share
154
-	 *
155
-	 * @throws ProviderCouldNotAddShareException
156
-	 * @throws \OCP\AppFramework\QueryException
157
-	 * @throws \OC\HintException
158
-	 * @since 14.0.0
159
-	 */
160
-	public function shareReceived(ICloudFederationShare $share) {
161
-
162
-		if (!$this->isS2SEnabled(true)) {
163
-			throw new ProviderCouldNotAddShareException('Server does not support federated cloud sharing', '', Http::STATUS_SERVICE_UNAVAILABLE);
164
-		}
165
-
166
-		$protocol = $share->getProtocol();
167
-		if ($protocol['name'] !== 'webdav') {
168
-			throw new ProviderCouldNotAddShareException('Unsupported protocol for data exchange.', '', Http::STATUS_NOT_IMPLEMENTED);
169
-		}
170
-
171
-		list($ownerUid, $remote) = $this->addressHandler->splitUserRemote($share->getOwner());
172
-		// for backward compatibility make sure that the remote url stored in the
173
-		// database ends with a trailing slash
174
-		if (substr($remote, -1) !== '/') {
175
-			$remote = $remote . '/';
176
-		}
177
-
178
-		$token = $share->getShareSecret();
179
-		$name = $share->getResourceName();
180
-		$owner = $share->getOwnerDisplayName();
181
-		$sharedBy = $share->getSharedByDisplayName();
182
-		$shareWith = $share->getShareWith();
183
-		$remoteId = $share->getProviderId();
184
-		$sharedByFederatedId = $share->getSharedBy();
185
-		$ownerFederatedId = $share->getOwner();
186
-		$shareType = $this->mapShareTypeToNextcloud($share->getShareType());
187
-
188
-		// if no explicit information about the person who created the share was send
189
-		// we assume that the share comes from the owner
190
-		if ($sharedByFederatedId === null) {
191
-			$sharedBy = $owner;
192
-			$sharedByFederatedId = $ownerFederatedId;
193
-		}
194
-
195
-		if ($remote && $token && $name && $owner && $remoteId && $shareWith) {
196
-
197
-			if (!Util::isValidFileName($name)) {
198
-				throw new ProviderCouldNotAddShareException('The mountpoint name contains invalid characters.', '', Http::STATUS_BAD_REQUEST);
199
-			}
200
-
201
-			// FIXME this should be a method in the user management instead
202
-			$this->logger->debug('shareWith before, ' . $shareWith, ['app' => 'files_sharing']);
203
-			Util::emitHook(
204
-				'\OCA\Files_Sharing\API\Server2Server',
205
-				'preLoginNameUsedAsUserName',
206
-				array('uid' => &$shareWith)
207
-			);
208
-			$this->logger->debug('shareWith after, ' . $shareWith, ['app' => 'files_sharing']);
209
-
210
-			if (!$this->userManager->userExists($shareWith)) {
211
-				throw new ProviderCouldNotAddShareException('User does not exists', '',Http::STATUS_BAD_REQUEST);
212
-			}
213
-
214
-			\OC_Util::setupFS($shareWith);
215
-
216
-			$externalManager = new \OCA\Files_Sharing\External\Manager(
217
-				\OC::$server->getDatabaseConnection(),
218
-				Filesystem::getMountManager(),
219
-				Filesystem::getLoader(),
220
-				\OC::$server->getHTTPClientService(),
221
-				\OC::$server->getNotificationManager(),
222
-				\OC::$server->query(\OCP\OCS\IDiscoveryService::class),
223
-				\OC::$server->getCloudFederationProviderManager(),
224
-				\OC::$server->getCloudFederationFactory(),
225
-				$shareWith
226
-			);
227
-
228
-			try {
229
-				$externalManager->addShare($remote, $token, '', $name, $owner, $shareType,false, $shareWith, $remoteId);
230
-				$shareId = \OC::$server->getDatabaseConnection()->lastInsertId('*PREFIX*share_external');
231
-
232
-				$event = $this->activityManager->generateEvent();
233
-				$event->setApp('files_sharing')
234
-					->setType('remote_share')
235
-					->setSubject(RemoteShares::SUBJECT_REMOTE_SHARE_RECEIVED, [$ownerFederatedId, trim($name, '/')])
236
-					->setAffectedUser($shareWith)
237
-					->setObject('remote_share', (int)$shareId, $name);
238
-				\OC::$server->getActivityManager()->publish($event);
239
-
240
-				if ($shareType === Share::SHARE_TYPE_USER) {
241
-					$this->notifyAboutNewShare($shareWith, $shareId, $ownerFederatedId, $sharedByFederatedId, $name);
242
-				} else {
243
-					$groupMembers = $this->groupManager->get($shareWith)->getUsers();
244
-					foreach ($groupMembers as $user) {
245
-						$this->notifyAboutNewShare($user, $shareId, $ownerFederatedId, $sharedByFederatedId, $name);
246
-					}
247
-				}
248
-				return $shareId;
249
-			} catch (\Exception $e) {
250
-				$this->logger->logException($e, [
251
-					'message' => 'Server can not add remote share.',
252
-					'level' => ILogger::ERROR,
253
-					'app' => 'files_sharing'
254
-				]);
255
-				throw new ProviderCouldNotAddShareException('internal server error, was not able to add share from ' . $remote, '', HTTP::STATUS_INTERNAL_SERVER_ERROR);
256
-			}
257
-		}
258
-
259
-		throw new ProviderCouldNotAddShareException('server can not add remote share, missing parameter', '', HTTP::STATUS_BAD_REQUEST);
260
-
261
-	}
262
-
263
-	/**
264
-	 * notification received from another server
265
-	 *
266
-	 * @param string $notificationType (e.g. SHARE_ACCEPTED)
267
-	 * @param string $providerId id of the share
268
-	 * @param array $notification payload of the notification
269
-	 * @return array data send back to the sender
270
-	 *
271
-	 * @throws ActionNotSupportedException
272
-	 * @throws AuthenticationFailedException
273
-	 * @throws BadRequestException
274
-	 * @throws \OC\HintException
275
-	 * @since 14.0.0
276
-	 */
277
-	public function notificationReceived($notificationType, $providerId, array $notification) {
278
-
279
-		switch ($notificationType) {
280
-			case 'SHARE_ACCEPTED':
281
-				return $this->shareAccepted($providerId, $notification);
282
-			case 'SHARE_DECLINED':
283
-				return $this->shareDeclined($providerId, $notification);
284
-			case 'SHARE_UNSHARED':
285
-				return $this->unshare($providerId, $notification);
286
-			case 'REQUEST_RESHARE':
287
-				return $this->reshareRequested($providerId, $notification);
288
-			case 'RESHARE_UNDO':
289
-				return $this->undoReshare($providerId, $notification);
290
-			case 'RESHARE_CHANGE_PERMISSION':
291
-				return $this->updateResharePermissions($providerId, $notification);
292
-		}
293
-
294
-
295
-		throw new BadRequestException([$notificationType]);
296
-	}
297
-
298
-	/**
299
-	 * map OCM share type (strings) to Nextcloud internal share types (integer)
300
-	 *
301
-	 * @param string $shareType
302
-	 * @return int
303
-	 */
304
-	private function mapShareTypeToNextcloud($shareType) {
305
-		$result = Share::SHARE_TYPE_USER;
306
-		if ($shareType === 'group') {
307
-			$result = Share::SHARE_TYPE_GROUP;
308
-		}
309
-
310
-		return $result;
311
-	}
312
-
313
-	/**
314
-	 * notify user about new federated share
315
-	 *
316
-	 * @param $shareWith
317
-	 * @param $shareId
318
-	 * @param $ownerFederatedId
319
-	 * @param $sharedByFederatedId
320
-	 * @param $name
321
-	 */
322
-	private function notifyAboutNewShare($shareWith, $shareId, $ownerFederatedId, $sharedByFederatedId, $name) {
323
-		$notification = $this->notificationManager->createNotification();
324
-		$notification->setApp('files_sharing')
325
-			->setUser($shareWith)
326
-			->setDateTime(new \DateTime())
327
-			->setObject('remote_share', $shareId)
328
-			->setSubject('remote_share', [$ownerFederatedId, $sharedByFederatedId, trim($name, '/')]);
329
-
330
-		$declineAction = $notification->createAction();
331
-		$declineAction->setLabel('decline')
332
-			->setLink($this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkTo('', 'ocs/v2.php/apps/files_sharing/api/v1/remote_shares/pending/' . $shareId)), 'DELETE');
333
-		$notification->addAction($declineAction);
334
-
335
-		$acceptAction = $notification->createAction();
336
-		$acceptAction->setLabel('accept')
337
-			->setLink($this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkTo('', 'ocs/v2.php/apps/files_sharing/api/v1/remote_shares/pending/' . $shareId)), 'POST');
338
-		$notification->addAction($acceptAction);
339
-
340
-		$this->notificationManager->notify($notification);
341
-	}
342
-
343
-	/**
344
-	 * process notification that the recipient accepted a share
345
-	 *
346
-	 * @param string $id
347
-	 * @param array $notification
348
-	 * @return array
349
-	 * @throws ActionNotSupportedException
350
-	 * @throws AuthenticationFailedException
351
-	 * @throws BadRequestException
352
-	 * @throws \OC\HintException
353
-	 */
354
-	private function shareAccepted($id, array $notification) {
355
-
356
-		if (!$this->isS2SEnabled()) {
357
-			throw new ActionNotSupportedException('Server does not support federated cloud sharing');
358
-		}
359
-
360
-		if (!isset($notification['sharedSecret'])) {
361
-			throw new BadRequestException(['sharedSecret']);
362
-		}
363
-
364
-		$token = $notification['sharedSecret'];
365
-
366
-		$share = $this->federatedShareProvider->getShareById($id);
367
-
368
-		$this->verifyShare($share, $token);
369
-		$this->executeAcceptShare($share);
370
-		if ($share->getShareOwner() !== $share->getSharedBy()) {
371
-			list(, $remote) = $this->addressHandler->splitUserRemote($share->getSharedBy());
372
-			$remoteId = $this->federatedShareProvider->getRemoteId($share);
373
-			$notification = $this->cloudFederationFactory->getCloudFederationNotification();
374
-			$notification->setMessage(
375
-				'SHARE_ACCEPTED',
376
-				'file',
377
-				$remoteId,
378
-				[
379
-					'sharedSecret' => $token,
380
-					'message' => 'Recipient accepted the re-share'
381
-				]
382
-
383
-			);
384
-			$this->cloudFederationProviderManager->sendNotification($remote, $notification);
385
-
386
-		}
387
-
388
-		return [];
389
-	}
390
-
391
-	/**
392
-	 * @param IShare $share
393
-	 * @throws ShareNotFound
394
-	 */
395
-	protected function executeAcceptShare(IShare $share) {
396
-		try {
397
-			$fileId = (int)$share->getNode()->getId();
398
-			list($file, $link) = $this->getFile($this->getCorrectUid($share), $fileId);
399
-		} catch (\Exception $e) {
400
-			throw new ShareNotFound();
401
-		}
402
-
403
-		$event = $this->activityManager->generateEvent();
404
-		$event->setApp('files_sharing')
405
-			->setType('remote_share')
406
-			->setAffectedUser($this->getCorrectUid($share))
407
-			->setSubject(RemoteShares::SUBJECT_REMOTE_SHARE_ACCEPTED, [$share->getSharedWith(), [$fileId => $file]])
408
-			->setObject('files', $fileId, $file)
409
-			->setLink($link);
410
-		$this->activityManager->publish($event);
411
-	}
412
-
413
-	/**
414
-	 * process notification that the recipient declined a share
415
-	 *
416
-	 * @param string $id
417
-	 * @param array $notification
418
-	 * @return array
419
-	 * @throws ActionNotSupportedException
420
-	 * @throws AuthenticationFailedException
421
-	 * @throws BadRequestException
422
-	 * @throws ShareNotFound
423
-	 * @throws \OC\HintException
424
-	 *
425
-	 */
426
-	protected function shareDeclined($id, array $notification) {
427
-
428
-		if (!$this->isS2SEnabled()) {
429
-			throw new ActionNotSupportedException('Server does not support federated cloud sharing');
430
-		}
431
-
432
-		if (!isset($notification['sharedSecret'])) {
433
-			throw new BadRequestException(['sharedSecret']);
434
-		}
435
-
436
-		$token = $notification['sharedSecret'];
437
-
438
-		$share = $this->federatedShareProvider->getShareById($id);
439
-
440
-		$this->verifyShare($share, $token);
441
-
442
-		if ($share->getShareOwner() !== $share->getSharedBy()) {
443
-			list(, $remote) = $this->addressHandler->splitUserRemote($share->getSharedBy());
444
-			$remoteId = $this->federatedShareProvider->getRemoteId($share);
445
-			$notification = $this->cloudFederationFactory->getCloudFederationNotification();
446
-			$notification->setMessage(
447
-				'SHARE_DECLINED',
448
-				'file',
449
-				$remoteId,
450
-				[
451
-					'sharedSecret' => $token,
452
-					'message' => 'Recipient declined the re-share'
453
-				]
454
-
455
-			);
456
-			$this->cloudFederationProviderManager->sendNotification($remote, $notification);
457
-		}
458
-
459
-		$this->executeDeclineShare($share);
460
-
461
-		return [];
462
-
463
-	}
464
-
465
-	/**
466
-	 * delete declined share and create a activity
467
-	 *
468
-	 * @param IShare $share
469
-	 * @throws ShareNotFound
470
-	 */
471
-	protected function executeDeclineShare(IShare $share) {
472
-		$this->federatedShareProvider->removeShareFromTable($share);
473
-
474
-		try {
475
-			$fileId = (int)$share->getNode()->getId();
476
-			list($file, $link) = $this->getFile($this->getCorrectUid($share), $fileId);
477
-		} catch (\Exception $e) {
478
-			throw new ShareNotFound();
479
-		}
480
-
481
-		$event = $this->activityManager->generateEvent();
482
-		$event->setApp('files_sharing')
483
-			->setType('remote_share')
484
-			->setAffectedUser($this->getCorrectUid($share))
485
-			->setSubject(RemoteShares::SUBJECT_REMOTE_SHARE_DECLINED, [$share->getSharedWith(), [$fileId => $file]])
486
-			->setObject('files', $fileId, $file)
487
-			->setLink($link);
488
-		$this->activityManager->publish($event);
489
-
490
-	}
491
-
492
-	/**
493
-	 * received the notification that the owner unshared a file from you
494
-	 *
495
-	 * @param string $id
496
-	 * @param array $notification
497
-	 * @return array
498
-	 * @throws AuthenticationFailedException
499
-	 * @throws BadRequestException
500
-	 */
501
-	private function undoReshare($id, array $notification) {
502
-		if (!isset($notification['sharedSecret'])) {
503
-			throw new BadRequestException(['sharedSecret']);
504
-		}
505
-		$token = $notification['sharedSecret'];
506
-
507
-		$share = $this->federatedShareProvider->getShareById($id);
508
-
509
-		$this->verifyShare($share, $token);
510
-		$this->federatedShareProvider->removeShareFromTable($share);
511
-		return [];
512
-	}
513
-
514
-	/**
515
-	 * unshare file from self
516
-	 *
517
-	 * @param string $id
518
-	 * @param array $notification
519
-	 * @return array
520
-	 * @throws ActionNotSupportedException
521
-	 * @throws BadRequestException
522
-	 */
523
-	private function unshare($id, array $notification) {
524
-
525
-		if (!$this->isS2SEnabled(true)) {
526
-			throw new ActionNotSupportedException("incoming shares disabled!");
527
-		}
528
-
529
-		if (!isset($notification['sharedSecret'])) {
530
-			throw new BadRequestException(['sharedSecret']);
531
-		}
532
-		$token = $notification['sharedSecret'];
533
-
534
-		$qb = $this->connection->getQueryBuilder();
535
-		$qb->select('*')
536
-			->from('share_external')
537
-			->where(
538
-				$qb->expr()->andX(
539
-					$qb->expr()->eq('remote_id', $qb->createNamedParameter($id)),
540
-					$qb->expr()->eq('share_token', $qb->createNamedParameter($token))
541
-				)
542
-			);
543
-
544
-		$result = $qb->execute();
545
-		$share = $result->fetch();
546
-		$result->closeCursor();
547
-
548
-		if ($token && $id && !empty($share)) {
549
-
550
-			$remote = $this->cleanupRemote($share['remote']);
551
-
552
-			$owner = $this->cloudIdManager->getCloudId($share['owner'], $remote);
553
-			$mountpoint = $share['mountpoint'];
554
-			$user = $share['user'];
555
-
556
-			$qb = $this->connection->getQueryBuilder();
557
-			$qb->delete('share_external')
558
-				->where(
559
-					$qb->expr()->andX(
560
-						$qb->expr()->eq('remote_id', $qb->createNamedParameter($id)),
561
-						$qb->expr()->eq('share_token', $qb->createNamedParameter($token))
562
-					)
563
-				);
564
-
565
-			$qb->execute();
566
-
567
-			if ($share['accepted']) {
568
-				$path = trim($mountpoint, '/');
569
-			} else {
570
-				$path = trim($share['name'], '/');
571
-			}
572
-
573
-			$notification = $this->notificationManager->createNotification();
574
-			$notification->setApp('files_sharing')
575
-				->setUser($share['user'])
576
-				->setObject('remote_share', (int)$share['id']);
577
-			$this->notificationManager->markProcessed($notification);
578
-
579
-			$event = $this->activityManager->generateEvent();
580
-			$event->setApp('files_sharing')
581
-				->setType('remote_share')
582
-				->setSubject(RemoteShares::SUBJECT_REMOTE_SHARE_UNSHARED, [$owner->getId(), $path])
583
-				->setAffectedUser($user)
584
-				->setObject('remote_share', (int)$share['id'], $path);
585
-			\OC::$server->getActivityManager()->publish($event);
586
-		}
587
-
588
-		return [];
589
-	}
590
-
591
-	private function cleanupRemote($remote) {
592
-		$remote = substr($remote, strpos($remote, '://') + 3);
593
-
594
-		return rtrim($remote, '/');
595
-	}
596
-
597
-	/**
598
-	 * recipient of a share request to re-share the file with another user
599
-	 *
600
-	 * @param string $id
601
-	 * @param array $notification
602
-	 * @return array
603
-	 * @throws AuthenticationFailedException
604
-	 * @throws BadRequestException
605
-	 * @throws ProviderCouldNotAddShareException
606
-	 * @throws ShareNotFound
607
-	 */
608
-	protected function reshareRequested($id, array $notification) {
609
-
610
-		if (!isset($notification['sharedSecret'])) {
611
-			throw new BadRequestException(['sharedSecret']);
612
-		}
613
-		$token = $notification['sharedSecret'];
614
-
615
-		if (!isset($notification['shareWith'])) {
616
-			throw new BadRequestException(['shareWith']);
617
-		}
618
-		$shareWith = $notification['shareWith'];
619
-
620
-		if (!isset($notification['senderId'])) {
621
-			throw new BadRequestException(['senderId']);
622
-		}
623
-		$senderId = $notification['senderId'];
624
-
625
-		$share = $this->federatedShareProvider->getShareById($id);
626
-		// don't allow to share a file back to the owner
627
-		try {
628
-			list($user, $remote) = $this->addressHandler->splitUserRemote($shareWith);
629
-			$owner = $share->getShareOwner();
630
-			$currentServer = $this->addressHandler->generateRemoteURL();
631
-			if ($this->addressHandler->compareAddresses($user, $remote, $owner, $currentServer)) {
632
-				throw new ProviderCouldNotAddShareException('Resharing back to the owner is not allowed: ' . $id);
633
-			}
634
-		} catch (\Exception $e) {
635
-			throw new ProviderCouldNotAddShareException($e->getMessage());
636
-		}
637
-
638
-		$this->verifyShare($share, $token);
639
-
640
-		// check if re-sharing is allowed
641
-		if ($share->getPermissions() & Constants::PERMISSION_SHARE) {
642
-			// the recipient of the initial share is now the initiator for the re-share
643
-			$share->setSharedBy($share->getSharedWith());
644
-			$share->setSharedWith($shareWith);
645
-			$result = $this->federatedShareProvider->create($share);
646
-			$this->federatedShareProvider->storeRemoteId((int)$result->getId(), $senderId);
647
-			return ['token' => $result->getToken(), 'providerId' => $result->getId()];
648
-		} else {
649
-			throw new ProviderCouldNotAddShareException('resharing not allowed for share: ' . $id);
650
-		}
651
-
652
-	}
653
-
654
-	/**
655
-	 * update permission of a re-share so that the share dialog shows the right
656
-	 * permission if the owner or the sender changes the permission
657
-	 *
658
-	 * @param string $id
659
-	 * @param array $notification
660
-	 * @return array
661
-	 * @throws AuthenticationFailedException
662
-	 * @throws BadRequestException
663
-	 */
664
-	protected function updateResharePermissions($id, array $notification) {
665
-
666
-		if (!isset($notification['sharedSecret'])) {
667
-			throw new BadRequestException(['sharedSecret']);
668
-		}
669
-		$token = $notification['sharedSecret'];
670
-
671
-		if (!isset($notification['permission'])) {
672
-			throw new BadRequestException(['permission']);
673
-		}
674
-		$ocmPermissions = $notification['permission'];
675
-
676
-		$share = $this->federatedShareProvider->getShareById($id);
677
-
678
-		$ncPermission = $this->ocmPermissions2ncPermissions($ocmPermissions);
679
-
680
-		$this->verifyShare($share, $token);
681
-		$this->updatePermissionsInDatabase($share, $ncPermission);
682
-
683
-		return [];
684
-	}
685
-
686
-	/**
687
-	 * translate OCM Permissions to Nextcloud permissions
688
-	 *
689
-	 * @param array $ocmPermissions
690
-	 * @return int
691
-	 * @throws BadRequestException
692
-	 */
693
-	protected function ocmPermissions2ncPermissions(array $ocmPermissions) {
694
-		$ncPermissions = 0;
695
-		foreach($ocmPermissions as $permission) {
696
-			switch (strtolower($permission)) {
697
-				case 'read':
698
-					$ncPermissions += Constants::PERMISSION_READ;
699
-					break;
700
-				case 'write':
701
-					$ncPermissions += Constants::PERMISSION_CREATE + Constants::PERMISSION_UPDATE;
702
-					break;
703
-				case 'share':
704
-					$ncPermissions += Constants::PERMISSION_SHARE;
705
-					break;
706
-				default:
707
-					throw new BadRequestException(['permission']);
708
-			}
709
-
710
-			error_log("new permissions: " . $ncPermissions);
711
-		}
712
-
713
-		return $ncPermissions;
714
-	}
715
-
716
-	/**
717
-	 * update permissions in database
718
-	 *
719
-	 * @param IShare $share
720
-	 * @param int $permissions
721
-	 */
722
-	protected function updatePermissionsInDatabase(IShare $share, $permissions) {
723
-		$query = $this->connection->getQueryBuilder();
724
-		$query->update('share')
725
-			->where($query->expr()->eq('id', $query->createNamedParameter($share->getId())))
726
-			->set('permissions', $query->createNamedParameter($permissions))
727
-			->execute();
728
-	}
729
-
730
-
731
-	/**
732
-	 * get file
733
-	 *
734
-	 * @param string $user
735
-	 * @param int $fileSource
736
-	 * @return array with internal path of the file and a absolute link to it
737
-	 */
738
-	private function getFile($user, $fileSource) {
739
-		\OC_Util::setupFS($user);
740
-
741
-		try {
742
-			$file = Filesystem::getPath($fileSource);
743
-		} catch (NotFoundException $e) {
744
-			$file = null;
745
-		}
746
-		$args = Filesystem::is_dir($file) ? array('dir' => $file) : array('dir' => dirname($file), 'scrollto' => $file);
747
-		$link = Util::linkToAbsolute('files', 'index.php', $args);
748
-
749
-		return [$file, $link];
750
-
751
-	}
752
-
753
-	/**
754
-	 * check if we are the initiator or the owner of a re-share and return the correct UID
755
-	 *
756
-	 * @param IShare $share
757
-	 * @return string
758
-	 */
759
-	protected function getCorrectUid(IShare $share) {
760
-		if ($this->userManager->userExists($share->getShareOwner())) {
761
-			return $share->getShareOwner();
762
-		}
763
-
764
-		return $share->getSharedBy();
765
-	}
766
-
767
-
768
-
769
-	/**
770
-	 * check if we got the right share
771
-	 *
772
-	 * @param IShare $share
773
-	 * @param string $token
774
-	 * @return bool
775
-	 * @throws AuthenticationFailedException
776
-	 */
777
-	protected function verifyShare(IShare $share, $token) {
778
-		if (
779
-			$share->getShareType() === FederatedShareProvider::SHARE_TYPE_REMOTE &&
780
-			$share->getToken() === $token
781
-		) {
782
-			return true;
783
-		}
784
-
785
-		throw new AuthenticationFailedException();
786
-	}
787
-
788
-
789
-
790
-	/**
791
-	 * check if server-to-server sharing is enabled
792
-	 *
793
-	 * @param bool $incoming
794
-	 * @return bool
795
-	 */
796
-	private function isS2SEnabled($incoming = false) {
797
-
798
-		$result = $this->appManager->isEnabledForUser('files_sharing');
799
-
800
-		if ($incoming) {
801
-			$result = $result && $this->federatedShareProvider->isIncomingServer2serverShareEnabled();
802
-		} else {
803
-			$result = $result && $this->federatedShareProvider->isOutgoingServer2serverShareEnabled();
804
-		}
805
-
806
-		return $result;
807
-	}
808
-
809
-
810
-	/**
811
-	 * get the supported share types, e.g. "user", "group", etc.
812
-	 *
813
-	 * @return array
814
-	 */
815
-	public function getSupportedShareTypes() {
816
-		return ['user'];
817
-	}
55
+    /** @var IAppManager */
56
+    private $appManager;
57
+
58
+    /** @var FederatedShareProvider */
59
+    private $federatedShareProvider;
60
+
61
+    /** @var AddressHandler */
62
+    private $addressHandler;
63
+
64
+    /** @var ILogger */
65
+    private $logger;
66
+
67
+    /** @var IUserManager */
68
+    private $userManager;
69
+
70
+    /** @var ICloudIdManager */
71
+    private $cloudIdManager;
72
+
73
+    /** @var IActivityManager */
74
+    private $activityManager;
75
+
76
+    /** @var INotificationManager */
77
+    private $notificationManager;
78
+
79
+    /** @var IURLGenerator */
80
+    private $urlGenerator;
81
+
82
+    /** @var ICloudFederationFactory */
83
+    private $cloudFederationFactory;
84
+
85
+    /** @var ICloudFederationProviderManager */
86
+    private $cloudFederationProviderManager;
87
+
88
+    /** @var IDBConnection */
89
+    private $connection;
90
+
91
+    /** @var IGroupManager */
92
+    private $groupManager;
93
+
94
+    /**
95
+     * CloudFederationProvider constructor.
96
+     *
97
+     * @param IAppManager $appManager
98
+     * @param FederatedShareProvider $federatedShareProvider
99
+     * @param AddressHandler $addressHandler
100
+     * @param ILogger $logger
101
+     * @param IUserManager $userManager
102
+     * @param ICloudIdManager $cloudIdManager
103
+     * @param IActivityManager $activityManager
104
+     * @param INotificationManager $notificationManager
105
+     * @param IURLGenerator $urlGenerator
106
+     * @param ICloudFederationFactory $cloudFederationFactory
107
+     * @param ICloudFederationProviderManager $cloudFederationProviderManager
108
+     * @param IDBConnection $connection
109
+     * @param IGroupManager $groupManager
110
+     */
111
+    public function __construct(IAppManager $appManager,
112
+                                FederatedShareProvider $federatedShareProvider,
113
+                                AddressHandler $addressHandler,
114
+                                ILogger $logger,
115
+                                IUserManager $userManager,
116
+                                ICloudIdManager $cloudIdManager,
117
+                                IActivityManager $activityManager,
118
+                                INotificationManager $notificationManager,
119
+                                IURLGenerator $urlGenerator,
120
+                                ICloudFederationFactory $cloudFederationFactory,
121
+                                ICloudFederationProviderManager $cloudFederationProviderManager,
122
+                                IDBConnection $connection,
123
+                                IGroupManager $groupManager
124
+    ) {
125
+        $this->appManager = $appManager;
126
+        $this->federatedShareProvider = $federatedShareProvider;
127
+        $this->addressHandler = $addressHandler;
128
+        $this->logger = $logger;
129
+        $this->userManager = $userManager;
130
+        $this->cloudIdManager = $cloudIdManager;
131
+        $this->activityManager = $activityManager;
132
+        $this->notificationManager = $notificationManager;
133
+        $this->urlGenerator = $urlGenerator;
134
+        $this->cloudFederationFactory = $cloudFederationFactory;
135
+        $this->cloudFederationProviderManager = $cloudFederationProviderManager;
136
+        $this->connection = $connection;
137
+        $this->groupManager = $groupManager;
138
+    }
139
+
140
+
141
+
142
+    /**
143
+     * @return string
144
+     */
145
+    public function getShareType() {
146
+        return 'file';
147
+    }
148
+
149
+    /**
150
+     * share received from another server
151
+     *
152
+     * @param ICloudFederationShare $share
153
+     * @return string provider specific unique ID of the share
154
+     *
155
+     * @throws ProviderCouldNotAddShareException
156
+     * @throws \OCP\AppFramework\QueryException
157
+     * @throws \OC\HintException
158
+     * @since 14.0.0
159
+     */
160
+    public function shareReceived(ICloudFederationShare $share) {
161
+
162
+        if (!$this->isS2SEnabled(true)) {
163
+            throw new ProviderCouldNotAddShareException('Server does not support federated cloud sharing', '', Http::STATUS_SERVICE_UNAVAILABLE);
164
+        }
165
+
166
+        $protocol = $share->getProtocol();
167
+        if ($protocol['name'] !== 'webdav') {
168
+            throw new ProviderCouldNotAddShareException('Unsupported protocol for data exchange.', '', Http::STATUS_NOT_IMPLEMENTED);
169
+        }
170
+
171
+        list($ownerUid, $remote) = $this->addressHandler->splitUserRemote($share->getOwner());
172
+        // for backward compatibility make sure that the remote url stored in the
173
+        // database ends with a trailing slash
174
+        if (substr($remote, -1) !== '/') {
175
+            $remote = $remote . '/';
176
+        }
177
+
178
+        $token = $share->getShareSecret();
179
+        $name = $share->getResourceName();
180
+        $owner = $share->getOwnerDisplayName();
181
+        $sharedBy = $share->getSharedByDisplayName();
182
+        $shareWith = $share->getShareWith();
183
+        $remoteId = $share->getProviderId();
184
+        $sharedByFederatedId = $share->getSharedBy();
185
+        $ownerFederatedId = $share->getOwner();
186
+        $shareType = $this->mapShareTypeToNextcloud($share->getShareType());
187
+
188
+        // if no explicit information about the person who created the share was send
189
+        // we assume that the share comes from the owner
190
+        if ($sharedByFederatedId === null) {
191
+            $sharedBy = $owner;
192
+            $sharedByFederatedId = $ownerFederatedId;
193
+        }
194
+
195
+        if ($remote && $token && $name && $owner && $remoteId && $shareWith) {
196
+
197
+            if (!Util::isValidFileName($name)) {
198
+                throw new ProviderCouldNotAddShareException('The mountpoint name contains invalid characters.', '', Http::STATUS_BAD_REQUEST);
199
+            }
200
+
201
+            // FIXME this should be a method in the user management instead
202
+            $this->logger->debug('shareWith before, ' . $shareWith, ['app' => 'files_sharing']);
203
+            Util::emitHook(
204
+                '\OCA\Files_Sharing\API\Server2Server',
205
+                'preLoginNameUsedAsUserName',
206
+                array('uid' => &$shareWith)
207
+            );
208
+            $this->logger->debug('shareWith after, ' . $shareWith, ['app' => 'files_sharing']);
209
+
210
+            if (!$this->userManager->userExists($shareWith)) {
211
+                throw new ProviderCouldNotAddShareException('User does not exists', '',Http::STATUS_BAD_REQUEST);
212
+            }
213
+
214
+            \OC_Util::setupFS($shareWith);
215
+
216
+            $externalManager = new \OCA\Files_Sharing\External\Manager(
217
+                \OC::$server->getDatabaseConnection(),
218
+                Filesystem::getMountManager(),
219
+                Filesystem::getLoader(),
220
+                \OC::$server->getHTTPClientService(),
221
+                \OC::$server->getNotificationManager(),
222
+                \OC::$server->query(\OCP\OCS\IDiscoveryService::class),
223
+                \OC::$server->getCloudFederationProviderManager(),
224
+                \OC::$server->getCloudFederationFactory(),
225
+                $shareWith
226
+            );
227
+
228
+            try {
229
+                $externalManager->addShare($remote, $token, '', $name, $owner, $shareType,false, $shareWith, $remoteId);
230
+                $shareId = \OC::$server->getDatabaseConnection()->lastInsertId('*PREFIX*share_external');
231
+
232
+                $event = $this->activityManager->generateEvent();
233
+                $event->setApp('files_sharing')
234
+                    ->setType('remote_share')
235
+                    ->setSubject(RemoteShares::SUBJECT_REMOTE_SHARE_RECEIVED, [$ownerFederatedId, trim($name, '/')])
236
+                    ->setAffectedUser($shareWith)
237
+                    ->setObject('remote_share', (int)$shareId, $name);
238
+                \OC::$server->getActivityManager()->publish($event);
239
+
240
+                if ($shareType === Share::SHARE_TYPE_USER) {
241
+                    $this->notifyAboutNewShare($shareWith, $shareId, $ownerFederatedId, $sharedByFederatedId, $name);
242
+                } else {
243
+                    $groupMembers = $this->groupManager->get($shareWith)->getUsers();
244
+                    foreach ($groupMembers as $user) {
245
+                        $this->notifyAboutNewShare($user, $shareId, $ownerFederatedId, $sharedByFederatedId, $name);
246
+                    }
247
+                }
248
+                return $shareId;
249
+            } catch (\Exception $e) {
250
+                $this->logger->logException($e, [
251
+                    'message' => 'Server can not add remote share.',
252
+                    'level' => ILogger::ERROR,
253
+                    'app' => 'files_sharing'
254
+                ]);
255
+                throw new ProviderCouldNotAddShareException('internal server error, was not able to add share from ' . $remote, '', HTTP::STATUS_INTERNAL_SERVER_ERROR);
256
+            }
257
+        }
258
+
259
+        throw new ProviderCouldNotAddShareException('server can not add remote share, missing parameter', '', HTTP::STATUS_BAD_REQUEST);
260
+
261
+    }
262
+
263
+    /**
264
+     * notification received from another server
265
+     *
266
+     * @param string $notificationType (e.g. SHARE_ACCEPTED)
267
+     * @param string $providerId id of the share
268
+     * @param array $notification payload of the notification
269
+     * @return array data send back to the sender
270
+     *
271
+     * @throws ActionNotSupportedException
272
+     * @throws AuthenticationFailedException
273
+     * @throws BadRequestException
274
+     * @throws \OC\HintException
275
+     * @since 14.0.0
276
+     */
277
+    public function notificationReceived($notificationType, $providerId, array $notification) {
278
+
279
+        switch ($notificationType) {
280
+            case 'SHARE_ACCEPTED':
281
+                return $this->shareAccepted($providerId, $notification);
282
+            case 'SHARE_DECLINED':
283
+                return $this->shareDeclined($providerId, $notification);
284
+            case 'SHARE_UNSHARED':
285
+                return $this->unshare($providerId, $notification);
286
+            case 'REQUEST_RESHARE':
287
+                return $this->reshareRequested($providerId, $notification);
288
+            case 'RESHARE_UNDO':
289
+                return $this->undoReshare($providerId, $notification);
290
+            case 'RESHARE_CHANGE_PERMISSION':
291
+                return $this->updateResharePermissions($providerId, $notification);
292
+        }
293
+
294
+
295
+        throw new BadRequestException([$notificationType]);
296
+    }
297
+
298
+    /**
299
+     * map OCM share type (strings) to Nextcloud internal share types (integer)
300
+     *
301
+     * @param string $shareType
302
+     * @return int
303
+     */
304
+    private function mapShareTypeToNextcloud($shareType) {
305
+        $result = Share::SHARE_TYPE_USER;
306
+        if ($shareType === 'group') {
307
+            $result = Share::SHARE_TYPE_GROUP;
308
+        }
309
+
310
+        return $result;
311
+    }
312
+
313
+    /**
314
+     * notify user about new federated share
315
+     *
316
+     * @param $shareWith
317
+     * @param $shareId
318
+     * @param $ownerFederatedId
319
+     * @param $sharedByFederatedId
320
+     * @param $name
321
+     */
322
+    private function notifyAboutNewShare($shareWith, $shareId, $ownerFederatedId, $sharedByFederatedId, $name) {
323
+        $notification = $this->notificationManager->createNotification();
324
+        $notification->setApp('files_sharing')
325
+            ->setUser($shareWith)
326
+            ->setDateTime(new \DateTime())
327
+            ->setObject('remote_share', $shareId)
328
+            ->setSubject('remote_share', [$ownerFederatedId, $sharedByFederatedId, trim($name, '/')]);
329
+
330
+        $declineAction = $notification->createAction();
331
+        $declineAction->setLabel('decline')
332
+            ->setLink($this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkTo('', 'ocs/v2.php/apps/files_sharing/api/v1/remote_shares/pending/' . $shareId)), 'DELETE');
333
+        $notification->addAction($declineAction);
334
+
335
+        $acceptAction = $notification->createAction();
336
+        $acceptAction->setLabel('accept')
337
+            ->setLink($this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkTo('', 'ocs/v2.php/apps/files_sharing/api/v1/remote_shares/pending/' . $shareId)), 'POST');
338
+        $notification->addAction($acceptAction);
339
+
340
+        $this->notificationManager->notify($notification);
341
+    }
342
+
343
+    /**
344
+     * process notification that the recipient accepted a share
345
+     *
346
+     * @param string $id
347
+     * @param array $notification
348
+     * @return array
349
+     * @throws ActionNotSupportedException
350
+     * @throws AuthenticationFailedException
351
+     * @throws BadRequestException
352
+     * @throws \OC\HintException
353
+     */
354
+    private function shareAccepted($id, array $notification) {
355
+
356
+        if (!$this->isS2SEnabled()) {
357
+            throw new ActionNotSupportedException('Server does not support federated cloud sharing');
358
+        }
359
+
360
+        if (!isset($notification['sharedSecret'])) {
361
+            throw new BadRequestException(['sharedSecret']);
362
+        }
363
+
364
+        $token = $notification['sharedSecret'];
365
+
366
+        $share = $this->federatedShareProvider->getShareById($id);
367
+
368
+        $this->verifyShare($share, $token);
369
+        $this->executeAcceptShare($share);
370
+        if ($share->getShareOwner() !== $share->getSharedBy()) {
371
+            list(, $remote) = $this->addressHandler->splitUserRemote($share->getSharedBy());
372
+            $remoteId = $this->federatedShareProvider->getRemoteId($share);
373
+            $notification = $this->cloudFederationFactory->getCloudFederationNotification();
374
+            $notification->setMessage(
375
+                'SHARE_ACCEPTED',
376
+                'file',
377
+                $remoteId,
378
+                [
379
+                    'sharedSecret' => $token,
380
+                    'message' => 'Recipient accepted the re-share'
381
+                ]
382
+
383
+            );
384
+            $this->cloudFederationProviderManager->sendNotification($remote, $notification);
385
+
386
+        }
387
+
388
+        return [];
389
+    }
390
+
391
+    /**
392
+     * @param IShare $share
393
+     * @throws ShareNotFound
394
+     */
395
+    protected function executeAcceptShare(IShare $share) {
396
+        try {
397
+            $fileId = (int)$share->getNode()->getId();
398
+            list($file, $link) = $this->getFile($this->getCorrectUid($share), $fileId);
399
+        } catch (\Exception $e) {
400
+            throw new ShareNotFound();
401
+        }
402
+
403
+        $event = $this->activityManager->generateEvent();
404
+        $event->setApp('files_sharing')
405
+            ->setType('remote_share')
406
+            ->setAffectedUser($this->getCorrectUid($share))
407
+            ->setSubject(RemoteShares::SUBJECT_REMOTE_SHARE_ACCEPTED, [$share->getSharedWith(), [$fileId => $file]])
408
+            ->setObject('files', $fileId, $file)
409
+            ->setLink($link);
410
+        $this->activityManager->publish($event);
411
+    }
412
+
413
+    /**
414
+     * process notification that the recipient declined a share
415
+     *
416
+     * @param string $id
417
+     * @param array $notification
418
+     * @return array
419
+     * @throws ActionNotSupportedException
420
+     * @throws AuthenticationFailedException
421
+     * @throws BadRequestException
422
+     * @throws ShareNotFound
423
+     * @throws \OC\HintException
424
+     *
425
+     */
426
+    protected function shareDeclined($id, array $notification) {
427
+
428
+        if (!$this->isS2SEnabled()) {
429
+            throw new ActionNotSupportedException('Server does not support federated cloud sharing');
430
+        }
431
+
432
+        if (!isset($notification['sharedSecret'])) {
433
+            throw new BadRequestException(['sharedSecret']);
434
+        }
435
+
436
+        $token = $notification['sharedSecret'];
437
+
438
+        $share = $this->federatedShareProvider->getShareById($id);
439
+
440
+        $this->verifyShare($share, $token);
441
+
442
+        if ($share->getShareOwner() !== $share->getSharedBy()) {
443
+            list(, $remote) = $this->addressHandler->splitUserRemote($share->getSharedBy());
444
+            $remoteId = $this->federatedShareProvider->getRemoteId($share);
445
+            $notification = $this->cloudFederationFactory->getCloudFederationNotification();
446
+            $notification->setMessage(
447
+                'SHARE_DECLINED',
448
+                'file',
449
+                $remoteId,
450
+                [
451
+                    'sharedSecret' => $token,
452
+                    'message' => 'Recipient declined the re-share'
453
+                ]
454
+
455
+            );
456
+            $this->cloudFederationProviderManager->sendNotification($remote, $notification);
457
+        }
458
+
459
+        $this->executeDeclineShare($share);
460
+
461
+        return [];
462
+
463
+    }
464
+
465
+    /**
466
+     * delete declined share and create a activity
467
+     *
468
+     * @param IShare $share
469
+     * @throws ShareNotFound
470
+     */
471
+    protected function executeDeclineShare(IShare $share) {
472
+        $this->federatedShareProvider->removeShareFromTable($share);
473
+
474
+        try {
475
+            $fileId = (int)$share->getNode()->getId();
476
+            list($file, $link) = $this->getFile($this->getCorrectUid($share), $fileId);
477
+        } catch (\Exception $e) {
478
+            throw new ShareNotFound();
479
+        }
480
+
481
+        $event = $this->activityManager->generateEvent();
482
+        $event->setApp('files_sharing')
483
+            ->setType('remote_share')
484
+            ->setAffectedUser($this->getCorrectUid($share))
485
+            ->setSubject(RemoteShares::SUBJECT_REMOTE_SHARE_DECLINED, [$share->getSharedWith(), [$fileId => $file]])
486
+            ->setObject('files', $fileId, $file)
487
+            ->setLink($link);
488
+        $this->activityManager->publish($event);
489
+
490
+    }
491
+
492
+    /**
493
+     * received the notification that the owner unshared a file from you
494
+     *
495
+     * @param string $id
496
+     * @param array $notification
497
+     * @return array
498
+     * @throws AuthenticationFailedException
499
+     * @throws BadRequestException
500
+     */
501
+    private function undoReshare($id, array $notification) {
502
+        if (!isset($notification['sharedSecret'])) {
503
+            throw new BadRequestException(['sharedSecret']);
504
+        }
505
+        $token = $notification['sharedSecret'];
506
+
507
+        $share = $this->federatedShareProvider->getShareById($id);
508
+
509
+        $this->verifyShare($share, $token);
510
+        $this->federatedShareProvider->removeShareFromTable($share);
511
+        return [];
512
+    }
513
+
514
+    /**
515
+     * unshare file from self
516
+     *
517
+     * @param string $id
518
+     * @param array $notification
519
+     * @return array
520
+     * @throws ActionNotSupportedException
521
+     * @throws BadRequestException
522
+     */
523
+    private function unshare($id, array $notification) {
524
+
525
+        if (!$this->isS2SEnabled(true)) {
526
+            throw new ActionNotSupportedException("incoming shares disabled!");
527
+        }
528
+
529
+        if (!isset($notification['sharedSecret'])) {
530
+            throw new BadRequestException(['sharedSecret']);
531
+        }
532
+        $token = $notification['sharedSecret'];
533
+
534
+        $qb = $this->connection->getQueryBuilder();
535
+        $qb->select('*')
536
+            ->from('share_external')
537
+            ->where(
538
+                $qb->expr()->andX(
539
+                    $qb->expr()->eq('remote_id', $qb->createNamedParameter($id)),
540
+                    $qb->expr()->eq('share_token', $qb->createNamedParameter($token))
541
+                )
542
+            );
543
+
544
+        $result = $qb->execute();
545
+        $share = $result->fetch();
546
+        $result->closeCursor();
547
+
548
+        if ($token && $id && !empty($share)) {
549
+
550
+            $remote = $this->cleanupRemote($share['remote']);
551
+
552
+            $owner = $this->cloudIdManager->getCloudId($share['owner'], $remote);
553
+            $mountpoint = $share['mountpoint'];
554
+            $user = $share['user'];
555
+
556
+            $qb = $this->connection->getQueryBuilder();
557
+            $qb->delete('share_external')
558
+                ->where(
559
+                    $qb->expr()->andX(
560
+                        $qb->expr()->eq('remote_id', $qb->createNamedParameter($id)),
561
+                        $qb->expr()->eq('share_token', $qb->createNamedParameter($token))
562
+                    )
563
+                );
564
+
565
+            $qb->execute();
566
+
567
+            if ($share['accepted']) {
568
+                $path = trim($mountpoint, '/');
569
+            } else {
570
+                $path = trim($share['name'], '/');
571
+            }
572
+
573
+            $notification = $this->notificationManager->createNotification();
574
+            $notification->setApp('files_sharing')
575
+                ->setUser($share['user'])
576
+                ->setObject('remote_share', (int)$share['id']);
577
+            $this->notificationManager->markProcessed($notification);
578
+
579
+            $event = $this->activityManager->generateEvent();
580
+            $event->setApp('files_sharing')
581
+                ->setType('remote_share')
582
+                ->setSubject(RemoteShares::SUBJECT_REMOTE_SHARE_UNSHARED, [$owner->getId(), $path])
583
+                ->setAffectedUser($user)
584
+                ->setObject('remote_share', (int)$share['id'], $path);
585
+            \OC::$server->getActivityManager()->publish($event);
586
+        }
587
+
588
+        return [];
589
+    }
590
+
591
+    private function cleanupRemote($remote) {
592
+        $remote = substr($remote, strpos($remote, '://') + 3);
593
+
594
+        return rtrim($remote, '/');
595
+    }
596
+
597
+    /**
598
+     * recipient of a share request to re-share the file with another user
599
+     *
600
+     * @param string $id
601
+     * @param array $notification
602
+     * @return array
603
+     * @throws AuthenticationFailedException
604
+     * @throws BadRequestException
605
+     * @throws ProviderCouldNotAddShareException
606
+     * @throws ShareNotFound
607
+     */
608
+    protected function reshareRequested($id, array $notification) {
609
+
610
+        if (!isset($notification['sharedSecret'])) {
611
+            throw new BadRequestException(['sharedSecret']);
612
+        }
613
+        $token = $notification['sharedSecret'];
614
+
615
+        if (!isset($notification['shareWith'])) {
616
+            throw new BadRequestException(['shareWith']);
617
+        }
618
+        $shareWith = $notification['shareWith'];
619
+
620
+        if (!isset($notification['senderId'])) {
621
+            throw new BadRequestException(['senderId']);
622
+        }
623
+        $senderId = $notification['senderId'];
624
+
625
+        $share = $this->federatedShareProvider->getShareById($id);
626
+        // don't allow to share a file back to the owner
627
+        try {
628
+            list($user, $remote) = $this->addressHandler->splitUserRemote($shareWith);
629
+            $owner = $share->getShareOwner();
630
+            $currentServer = $this->addressHandler->generateRemoteURL();
631
+            if ($this->addressHandler->compareAddresses($user, $remote, $owner, $currentServer)) {
632
+                throw new ProviderCouldNotAddShareException('Resharing back to the owner is not allowed: ' . $id);
633
+            }
634
+        } catch (\Exception $e) {
635
+            throw new ProviderCouldNotAddShareException($e->getMessage());
636
+        }
637
+
638
+        $this->verifyShare($share, $token);
639
+
640
+        // check if re-sharing is allowed
641
+        if ($share->getPermissions() & Constants::PERMISSION_SHARE) {
642
+            // the recipient of the initial share is now the initiator for the re-share
643
+            $share->setSharedBy($share->getSharedWith());
644
+            $share->setSharedWith($shareWith);
645
+            $result = $this->federatedShareProvider->create($share);
646
+            $this->federatedShareProvider->storeRemoteId((int)$result->getId(), $senderId);
647
+            return ['token' => $result->getToken(), 'providerId' => $result->getId()];
648
+        } else {
649
+            throw new ProviderCouldNotAddShareException('resharing not allowed for share: ' . $id);
650
+        }
651
+
652
+    }
653
+
654
+    /**
655
+     * update permission of a re-share so that the share dialog shows the right
656
+     * permission if the owner or the sender changes the permission
657
+     *
658
+     * @param string $id
659
+     * @param array $notification
660
+     * @return array
661
+     * @throws AuthenticationFailedException
662
+     * @throws BadRequestException
663
+     */
664
+    protected function updateResharePermissions($id, array $notification) {
665
+
666
+        if (!isset($notification['sharedSecret'])) {
667
+            throw new BadRequestException(['sharedSecret']);
668
+        }
669
+        $token = $notification['sharedSecret'];
670
+
671
+        if (!isset($notification['permission'])) {
672
+            throw new BadRequestException(['permission']);
673
+        }
674
+        $ocmPermissions = $notification['permission'];
675
+
676
+        $share = $this->federatedShareProvider->getShareById($id);
677
+
678
+        $ncPermission = $this->ocmPermissions2ncPermissions($ocmPermissions);
679
+
680
+        $this->verifyShare($share, $token);
681
+        $this->updatePermissionsInDatabase($share, $ncPermission);
682
+
683
+        return [];
684
+    }
685
+
686
+    /**
687
+     * translate OCM Permissions to Nextcloud permissions
688
+     *
689
+     * @param array $ocmPermissions
690
+     * @return int
691
+     * @throws BadRequestException
692
+     */
693
+    protected function ocmPermissions2ncPermissions(array $ocmPermissions) {
694
+        $ncPermissions = 0;
695
+        foreach($ocmPermissions as $permission) {
696
+            switch (strtolower($permission)) {
697
+                case 'read':
698
+                    $ncPermissions += Constants::PERMISSION_READ;
699
+                    break;
700
+                case 'write':
701
+                    $ncPermissions += Constants::PERMISSION_CREATE + Constants::PERMISSION_UPDATE;
702
+                    break;
703
+                case 'share':
704
+                    $ncPermissions += Constants::PERMISSION_SHARE;
705
+                    break;
706
+                default:
707
+                    throw new BadRequestException(['permission']);
708
+            }
709
+
710
+            error_log("new permissions: " . $ncPermissions);
711
+        }
712
+
713
+        return $ncPermissions;
714
+    }
715
+
716
+    /**
717
+     * update permissions in database
718
+     *
719
+     * @param IShare $share
720
+     * @param int $permissions
721
+     */
722
+    protected function updatePermissionsInDatabase(IShare $share, $permissions) {
723
+        $query = $this->connection->getQueryBuilder();
724
+        $query->update('share')
725
+            ->where($query->expr()->eq('id', $query->createNamedParameter($share->getId())))
726
+            ->set('permissions', $query->createNamedParameter($permissions))
727
+            ->execute();
728
+    }
729
+
730
+
731
+    /**
732
+     * get file
733
+     *
734
+     * @param string $user
735
+     * @param int $fileSource
736
+     * @return array with internal path of the file and a absolute link to it
737
+     */
738
+    private function getFile($user, $fileSource) {
739
+        \OC_Util::setupFS($user);
740
+
741
+        try {
742
+            $file = Filesystem::getPath($fileSource);
743
+        } catch (NotFoundException $e) {
744
+            $file = null;
745
+        }
746
+        $args = Filesystem::is_dir($file) ? array('dir' => $file) : array('dir' => dirname($file), 'scrollto' => $file);
747
+        $link = Util::linkToAbsolute('files', 'index.php', $args);
748
+
749
+        return [$file, $link];
750
+
751
+    }
752
+
753
+    /**
754
+     * check if we are the initiator or the owner of a re-share and return the correct UID
755
+     *
756
+     * @param IShare $share
757
+     * @return string
758
+     */
759
+    protected function getCorrectUid(IShare $share) {
760
+        if ($this->userManager->userExists($share->getShareOwner())) {
761
+            return $share->getShareOwner();
762
+        }
763
+
764
+        return $share->getSharedBy();
765
+    }
766
+
767
+
768
+
769
+    /**
770
+     * check if we got the right share
771
+     *
772
+     * @param IShare $share
773
+     * @param string $token
774
+     * @return bool
775
+     * @throws AuthenticationFailedException
776
+     */
777
+    protected function verifyShare(IShare $share, $token) {
778
+        if (
779
+            $share->getShareType() === FederatedShareProvider::SHARE_TYPE_REMOTE &&
780
+            $share->getToken() === $token
781
+        ) {
782
+            return true;
783
+        }
784
+
785
+        throw new AuthenticationFailedException();
786
+    }
787
+
788
+
789
+
790
+    /**
791
+     * check if server-to-server sharing is enabled
792
+     *
793
+     * @param bool $incoming
794
+     * @return bool
795
+     */
796
+    private function isS2SEnabled($incoming = false) {
797
+
798
+        $result = $this->appManager->isEnabledForUser('files_sharing');
799
+
800
+        if ($incoming) {
801
+            $result = $result && $this->federatedShareProvider->isIncomingServer2serverShareEnabled();
802
+        } else {
803
+            $result = $result && $this->federatedShareProvider->isOutgoingServer2serverShareEnabled();
804
+        }
805
+
806
+        return $result;
807
+    }
808
+
809
+
810
+    /**
811
+     * get the supported share types, e.g. "user", "group", etc.
812
+     *
813
+     * @return array
814
+     */
815
+    public function getSupportedShareTypes() {
816
+        return ['user'];
817
+    }
818 818
 }
Please login to merge, or discard this patch.
apps/federatedfilesharing/lib/Notifications.php 1 patch
Indentation   +433 added lines, -433 removed lines patch added patch discarded remove patch
@@ -33,437 +33,437 @@
 block discarded – undo
33 33
 use OCP\OCS\IDiscoveryService;
34 34
 
35 35
 class Notifications {
36
-	const RESPONSE_FORMAT = 'json'; // default response format for ocs calls
37
-
38
-	/** @var AddressHandler */
39
-	private $addressHandler;
40
-
41
-	/** @var IClientService */
42
-	private $httpClientService;
43
-
44
-	/** @var IDiscoveryService */
45
-	private $discoveryService;
46
-
47
-	/** @var IJobList  */
48
-	private $jobList;
49
-
50
-	/** @var ICloudFederationProviderManager */
51
-	private $federationProviderManager;
52
-
53
-	/** @var ICloudFederationFactory */
54
-	private $cloudFederationFactory;
55
-
56
-	/**
57
-	 * @param AddressHandler $addressHandler
58
-	 * @param IClientService $httpClientService
59
-	 * @param IDiscoveryService $discoveryService
60
-	 * @param IJobList $jobList
61
-	 * @param ICloudFederationProviderManager $federationProviderManager
62
-	 * @param ICloudFederationFactory $cloudFederationFactory
63
-	 */
64
-	public function __construct(
65
-		AddressHandler $addressHandler,
66
-		IClientService $httpClientService,
67
-		IDiscoveryService $discoveryService,
68
-		IJobList $jobList,
69
-		ICloudFederationProviderManager $federationProviderManager,
70
-		ICloudFederationFactory $cloudFederationFactory
71
-	) {
72
-		$this->addressHandler = $addressHandler;
73
-		$this->httpClientService = $httpClientService;
74
-		$this->discoveryService = $discoveryService;
75
-		$this->jobList = $jobList;
76
-		$this->federationProviderManager = $federationProviderManager;
77
-		$this->cloudFederationFactory = $cloudFederationFactory;
78
-	}
79
-
80
-	/**
81
-	 * send server-to-server share to remote server
82
-	 *
83
-	 * @param string $token
84
-	 * @param string $shareWith
85
-	 * @param string $name
86
-	 * @param int $remote_id
87
-	 * @param string $owner
88
-	 * @param string $ownerFederatedId
89
-	 * @param string $sharedBy
90
-	 * @param string $sharedByFederatedId
91
-	 * @param int $shareType (can be a remote user or group share)
92
-	 * @return bool
93
-	 * @throws \OC\HintException
94
-	 * @throws \OC\ServerNotAvailableException
95
-	 */
96
-	public function sendRemoteShare($token, $shareWith, $name, $remote_id, $owner, $ownerFederatedId, $sharedBy, $sharedByFederatedId, $shareType) {
97
-
98
-		list($user, $remote) = $this->addressHandler->splitUserRemote($shareWith);
99
-
100
-		if ($user && $remote) {
101
-			$local = $this->addressHandler->generateRemoteURL();
102
-
103
-			$fields = array(
104
-				'shareWith' => $user,
105
-				'token' => $token,
106
-				'name' => $name,
107
-				'remoteId' => $remote_id,
108
-				'owner' => $owner,
109
-				'ownerFederatedId' => $ownerFederatedId,
110
-				'sharedBy' => $sharedBy,
111
-				'sharedByFederatedId' => $sharedByFederatedId,
112
-				'remote' => $local,
113
-				'shareType' => $shareType
114
-			);
115
-
116
-			$result = $this->tryHttpPostToShareEndpoint($remote, '', $fields);
117
-			$status = json_decode($result['result'], true);
118
-
119
-			$ocsStatus = isset($status['ocs']);
120
-			$ocsSuccess = $ocsStatus && ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200);
121
-
122
-			if ($result['success'] && (!$ocsStatus ||$ocsSuccess)) {
123
-				\OC_Hook::emit('OCP\Share', 'federated_share_added', ['server' => $remote]);
124
-				return true;
125
-			}
126
-
127
-		}
128
-
129
-		return false;
130
-	}
131
-
132
-	/**
133
-	 * ask owner to re-share the file with the given user
134
-	 *
135
-	 * @param string $token
136
-	 * @param int $id remote Id
137
-	 * @param int $shareId internal share Id
138
-	 * @param string $remote remote address of the owner
139
-	 * @param string $shareWith
140
-	 * @param int $permission
141
-	 * @param string $filename
142
-	 * @return bool
143
-	 * @throws \OC\HintException
144
-	 * @throws \OC\ServerNotAvailableException
145
-	 */
146
-	public function requestReShare($token, $id, $shareId, $remote, $shareWith, $permission, $filename) {
147
-
148
-		$fields = array(
149
-			'shareWith' => $shareWith,
150
-			'token' => $token,
151
-			'permission' => $permission,
152
-			'remoteId' => $shareId,
153
-		);
154
-
155
-		$ocmFields = $fields;
156
-		$ocmFields['remoteId'] = $id;
157
-		$ocmFields['localId'] = $shareId;
158
-		$ocmFields['name'] = $filename;
159
-
160
-		$ocmResult = $this->tryOCMEndPoint($remote, $ocmFields, 'reshare');
161
-		if (is_array($ocmResult) && isset($ocmResult['token']) && isset($ocmResult['providerId'])) {
162
-			return [$ocmResult['token'], $ocmResult['providerId']];
163
-		}
164
-
165
-		$result = $this->tryLegacyEndPoint(rtrim($remote, '/'), '/' . $id . '/reshare', $fields);
166
-		$status = json_decode($result['result'], true);
167
-
168
-		$httpRequestSuccessful = $result['success'];
169
-		$ocsCallSuccessful = $status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200;
170
-		$validToken = isset($status['ocs']['data']['token']) && is_string($status['ocs']['data']['token']);
171
-		$validRemoteId = isset($status['ocs']['data']['remoteId']);
172
-
173
-		if ($httpRequestSuccessful && $ocsCallSuccessful && $validToken && $validRemoteId) {
174
-			return [
175
-				$status['ocs']['data']['token'],
176
-				(int)$status['ocs']['data']['remoteId']
177
-			];
178
-		}
179
-
180
-		return false;
181
-	}
182
-
183
-	/**
184
-	 * send server-to-server unshare to remote server
185
-	 *
186
-	 * @param string $remote url
187
-	 * @param int $id share id
188
-	 * @param string $token
189
-	 * @return bool
190
-	 */
191
-	public function sendRemoteUnShare($remote, $id, $token) {
192
-		$this->sendUpdateToRemote($remote, $id, $token, 'unshare');
193
-	}
194
-
195
-	/**
196
-	 * send server-to-server unshare to remote server
197
-	 *
198
-	 * @param string $remote url
199
-	 * @param int $id share id
200
-	 * @param string $token
201
-	 * @return bool
202
-	 */
203
-	public function sendRevokeShare($remote, $id, $token) {
204
-		$this->sendUpdateToRemote($remote, $id, $token, 'reshare_undo');
205
-	}
206
-
207
-	/**
208
-	 * send notification to remote server if the permissions was changed
209
-	 *
210
-	 * @param string $remote
211
-	 * @param int $remoteId
212
-	 * @param string $token
213
-	 * @param int $permissions
214
-	 * @return bool
215
-	 */
216
-	public function sendPermissionChange($remote, $remoteId, $token, $permissions) {
217
-		$this->sendUpdateToRemote($remote, $remoteId, $token, 'permissions', ['permissions' => $permissions]);
218
-	}
219
-
220
-	/**
221
-	 * forward accept reShare to remote server
222
-	 *
223
-	 * @param string $remote
224
-	 * @param int $remoteId
225
-	 * @param string $token
226
-	 */
227
-	public function sendAcceptShare($remote, $remoteId, $token) {
228
-		$this->sendUpdateToRemote($remote, $remoteId, $token, 'accept');
229
-	}
230
-
231
-	/**
232
-	 * forward decline reShare to remote server
233
-	 *
234
-	 * @param string $remote
235
-	 * @param int $remoteId
236
-	 * @param string $token
237
-	 */
238
-	public function sendDeclineShare($remote, $remoteId, $token) {
239
-		$this->sendUpdateToRemote($remote, $remoteId, $token, 'decline');
240
-	}
241
-
242
-	/**
243
-	 * inform remote server whether server-to-server share was accepted/declined
244
-	 *
245
-	 * @param string $remote
246
-	 * @param string $token
247
-	 * @param int $remoteId Share id on the remote host
248
-	 * @param string $action possible actions: accept, decline, unshare, revoke, permissions
249
-	 * @param array $data
250
-	 * @param int $try
251
-	 * @return boolean
252
-	 */
253
-	public function sendUpdateToRemote($remote, $remoteId, $token, $action, $data = [], $try = 0) {
254
-
255
-		$fields = [
256
-			'token' => $token,
257
-			'remoteId' => $remoteId
258
-			];
259
-		foreach ($data as $key => $value) {
260
-			$fields[$key] = $value;
261
-		}
262
-
263
-		$result = $this->tryHttpPostToShareEndpoint(rtrim($remote, '/'), '/' . $remoteId . '/' . $action, $fields, $action);
264
-		$status = json_decode($result['result'], true);
265
-
266
-		if ($result['success'] &&
267
-			($status['ocs']['meta']['statuscode'] === 100 ||
268
-				$status['ocs']['meta']['statuscode'] === 200
269
-			)
270
-		) {
271
-			return true;
272
-		} elseif ($try === 0) {
273
-			// only add new job on first try
274
-			$this->jobList->add('OCA\FederatedFileSharing\BackgroundJob\RetryJob',
275
-				[
276
-					'remote' => $remote,
277
-					'remoteId' => $remoteId,
278
-					'token' => $token,
279
-					'action' => $action,
280
-					'data' => json_encode($data),
281
-					'try' => $try,
282
-					'lastRun' => $this->getTimestamp()
283
-				]
284
-			);
285
-		}
286
-
287
-		return false;
288
-	}
289
-
290
-
291
-	/**
292
-	 * return current timestamp
293
-	 *
294
-	 * @return int
295
-	 */
296
-	protected function getTimestamp() {
297
-		return time();
298
-	}
299
-
300
-	/**
301
-	 * try http post with the given protocol, if no protocol is given we pick
302
-	 * the secure one (https)
303
-	 *
304
-	 * @param string $remoteDomain
305
-	 * @param string $urlSuffix
306
-	 * @param array $fields post parameters
307
-	 * @param string $action define the action (possible values: share, reshare, accept, decline, unshare, revoke, permissions)
308
-	 * @return array
309
-	 * @throws \Exception
310
-	 */
311
-	protected function tryHttpPostToShareEndpoint($remoteDomain, $urlSuffix, array $fields, $action="share") {
312
-
313
-		if ($this->addressHandler->urlContainProtocol($remoteDomain) === false) {
314
-			$remoteDomain = 'https://' . $remoteDomain;
315
-		}
316
-
317
-		$result = [
318
-			'success' => false,
319
-			'result' => '',
320
-		];
321
-
322
-		// if possible we use the new OCM API
323
-		$ocmResult = $this->tryOCMEndPoint($remoteDomain, $fields, $action);
324
-		if (is_array($ocmResult)) {
325
-			$result['success'] = true;
326
-			$result['result'] = json_encode([
327
-				'ocs' => ['meta' => ['statuscode' => 200]]]);
328
-			return $result;
329
-		}
330
-
331
-		return $this->tryLegacyEndPoint($remoteDomain, $urlSuffix, $fields);
332
-	}
333
-
334
-	/**
335
-	 * try old federated sharing API if the OCM api doesn't work
336
-	 *
337
-	 * @param $remoteDomain
338
-	 * @param $urlSuffix
339
-	 * @param array $fields
340
-	 * @return mixed
341
-	 * @throws \Exception
342
-	 */
343
-	protected function tryLegacyEndPoint($remoteDomain, $urlSuffix, array $fields) {
344
-
345
-		$result = [
346
-			'success' => false,
347
-			'result' => '',
348
-		];
349
-
350
-		// Fall back to old API
351
-		$client = $this->httpClientService->newClient();
352
-		$federationEndpoints = $this->discoveryService->discover($remoteDomain, 'FEDERATED_SHARING');
353
-		$endpoint = isset($federationEndpoints['share']) ? $federationEndpoints['share'] : '/ocs/v2.php/cloud/shares';
354
-		try {
355
-			$response = $client->post($remoteDomain . $endpoint . $urlSuffix . '?format=' . self::RESPONSE_FORMAT, [
356
-				'body' => $fields,
357
-				'timeout' => 10,
358
-				'connect_timeout' => 10,
359
-			]);
360
-			$result['result'] = $response->getBody();
361
-			$result['success'] = true;
362
-		} catch (\Exception $e) {
363
-			// if flat re-sharing is not supported by the remote server
364
-			// we re-throw the exception and fall back to the old behaviour.
365
-			// (flat re-shares has been introduced in Nextcloud 9.1)
366
-			if ($e->getCode() === Http::STATUS_INTERNAL_SERVER_ERROR) {
367
-				throw $e;
368
-			}
369
-		}
370
-
371
-		return $result;
372
-
373
-	}
374
-
375
-	/**
376
-	 * check if server supports the new OCM api and ask for the correct end-point
377
-	 *
378
-	 * @param string $url
379
-	 * @return string
380
-	 */
381
-	protected function getOCMEndPoint($url) {
382
-		$client = $this->httpClientService->newClient();
383
-		try {
384
-			$response = $client->get($url, ['timeout' => 10, 'connect_timeout' => 10]);
385
-		} catch (\Exception $e) {
386
-			return '';
387
-		}
388
-
389
-		$result = $response->getBody();
390
-		$result = json_decode($result, true);
391
-
392
-		if (isset($result['end-point'])) {
393
-			return $result['end-point'];
394
-		}
395
-
396
-		return '';
397
-	}
398
-
399
-	/**
400
-	 * send action regarding federated sharing to the remote server using the OCM API
401
-	 *
402
-	 * @param $remoteDomain
403
-	 * @param $fields
404
-	 * @param $action
405
-	 *
406
-	 * @return bool
407
-	 */
408
-	protected function tryOCMEndPoint($remoteDomain, $fields, $action) {
409
-		switch ($action) {
410
-			case 'share':
411
-				$share = $this->cloudFederationFactory->getCloudFederationShare(
412
-					$fields['shareWith'] . '@' . $remoteDomain,
413
-					$fields['name'],
414
-					'',
415
-					$fields['remoteId'],
416
-					$fields['ownerFederatedId'],
417
-					$fields['owner'],
418
-					$fields['sharedByFederatedId'],
419
-					$fields['sharedBy'],
420
-					$fields['token'],
421
-					$fields['shareType'],
422
-					'file'
423
-				);
424
-				return $this->federationProviderManager->sendShare($share);
425
-			case 'reshare':
426
-				// ask owner to reshare a file
427
-				$notification = $this->cloudFederationFactory->getCloudFederationNotification();
428
-				$notification->setMessage('REQUEST_RESHARE',
429
-					'file',
430
-					$fields['remoteId'],
431
-					[
432
-						'sharedSecret' => $fields['token'],
433
-						'shareWith' => $fields['shareWith'],
434
-						'senderId' => $fields['localId'],
435
-						'shareType' => $fields['shareType'],
436
-						'message' => 'Ask owner to reshare the file'
437
-					]
438
-				);
439
-				return $this->federationProviderManager->sendNotification($remoteDomain, $notification);
440
-			case 'unshare':
441
-				//owner unshares the file from the recipient again
442
-				$notification = $this->cloudFederationFactory->getCloudFederationNotification();
443
-				$notification->setMessage('SHARE_UNSHARED',
444
-					'file',
445
-					$fields['remoteId'],
446
-					[
447
-						'sharedSecret' => $fields['token'],
448
-						'messgage' => 'file is no longer shared with you'
449
-					]
450
-				);
451
-				return $this->federationProviderManager->sendNotification($remoteDomain, $notification);
452
-			case 'reshare_undo':
453
-				// if a reshare was unshared we send the information to the initiator/owner
454
-				$notification = $this->cloudFederationFactory->getCloudFederationNotification();
455
-				$notification->setMessage('RESHARE_UNDO',
456
-					'file',
457
-					$fields['remoteId'],
458
-					[
459
-						'sharedSecret' => $fields['token'],
460
-						'message' => 'reshare was revoked'
461
-					]
462
-				);
463
-				return $this->federationProviderManager->sendNotification($remoteDomain, $notification);
464
-		}
465
-
466
-		return false;
467
-
468
-	}
36
+    const RESPONSE_FORMAT = 'json'; // default response format for ocs calls
37
+
38
+    /** @var AddressHandler */
39
+    private $addressHandler;
40
+
41
+    /** @var IClientService */
42
+    private $httpClientService;
43
+
44
+    /** @var IDiscoveryService */
45
+    private $discoveryService;
46
+
47
+    /** @var IJobList  */
48
+    private $jobList;
49
+
50
+    /** @var ICloudFederationProviderManager */
51
+    private $federationProviderManager;
52
+
53
+    /** @var ICloudFederationFactory */
54
+    private $cloudFederationFactory;
55
+
56
+    /**
57
+     * @param AddressHandler $addressHandler
58
+     * @param IClientService $httpClientService
59
+     * @param IDiscoveryService $discoveryService
60
+     * @param IJobList $jobList
61
+     * @param ICloudFederationProviderManager $federationProviderManager
62
+     * @param ICloudFederationFactory $cloudFederationFactory
63
+     */
64
+    public function __construct(
65
+        AddressHandler $addressHandler,
66
+        IClientService $httpClientService,
67
+        IDiscoveryService $discoveryService,
68
+        IJobList $jobList,
69
+        ICloudFederationProviderManager $federationProviderManager,
70
+        ICloudFederationFactory $cloudFederationFactory
71
+    ) {
72
+        $this->addressHandler = $addressHandler;
73
+        $this->httpClientService = $httpClientService;
74
+        $this->discoveryService = $discoveryService;
75
+        $this->jobList = $jobList;
76
+        $this->federationProviderManager = $federationProviderManager;
77
+        $this->cloudFederationFactory = $cloudFederationFactory;
78
+    }
79
+
80
+    /**
81
+     * send server-to-server share to remote server
82
+     *
83
+     * @param string $token
84
+     * @param string $shareWith
85
+     * @param string $name
86
+     * @param int $remote_id
87
+     * @param string $owner
88
+     * @param string $ownerFederatedId
89
+     * @param string $sharedBy
90
+     * @param string $sharedByFederatedId
91
+     * @param int $shareType (can be a remote user or group share)
92
+     * @return bool
93
+     * @throws \OC\HintException
94
+     * @throws \OC\ServerNotAvailableException
95
+     */
96
+    public function sendRemoteShare($token, $shareWith, $name, $remote_id, $owner, $ownerFederatedId, $sharedBy, $sharedByFederatedId, $shareType) {
97
+
98
+        list($user, $remote) = $this->addressHandler->splitUserRemote($shareWith);
99
+
100
+        if ($user && $remote) {
101
+            $local = $this->addressHandler->generateRemoteURL();
102
+
103
+            $fields = array(
104
+                'shareWith' => $user,
105
+                'token' => $token,
106
+                'name' => $name,
107
+                'remoteId' => $remote_id,
108
+                'owner' => $owner,
109
+                'ownerFederatedId' => $ownerFederatedId,
110
+                'sharedBy' => $sharedBy,
111
+                'sharedByFederatedId' => $sharedByFederatedId,
112
+                'remote' => $local,
113
+                'shareType' => $shareType
114
+            );
115
+
116
+            $result = $this->tryHttpPostToShareEndpoint($remote, '', $fields);
117
+            $status = json_decode($result['result'], true);
118
+
119
+            $ocsStatus = isset($status['ocs']);
120
+            $ocsSuccess = $ocsStatus && ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200);
121
+
122
+            if ($result['success'] && (!$ocsStatus ||$ocsSuccess)) {
123
+                \OC_Hook::emit('OCP\Share', 'federated_share_added', ['server' => $remote]);
124
+                return true;
125
+            }
126
+
127
+        }
128
+
129
+        return false;
130
+    }
131
+
132
+    /**
133
+     * ask owner to re-share the file with the given user
134
+     *
135
+     * @param string $token
136
+     * @param int $id remote Id
137
+     * @param int $shareId internal share Id
138
+     * @param string $remote remote address of the owner
139
+     * @param string $shareWith
140
+     * @param int $permission
141
+     * @param string $filename
142
+     * @return bool
143
+     * @throws \OC\HintException
144
+     * @throws \OC\ServerNotAvailableException
145
+     */
146
+    public function requestReShare($token, $id, $shareId, $remote, $shareWith, $permission, $filename) {
147
+
148
+        $fields = array(
149
+            'shareWith' => $shareWith,
150
+            'token' => $token,
151
+            'permission' => $permission,
152
+            'remoteId' => $shareId,
153
+        );
154
+
155
+        $ocmFields = $fields;
156
+        $ocmFields['remoteId'] = $id;
157
+        $ocmFields['localId'] = $shareId;
158
+        $ocmFields['name'] = $filename;
159
+
160
+        $ocmResult = $this->tryOCMEndPoint($remote, $ocmFields, 'reshare');
161
+        if (is_array($ocmResult) && isset($ocmResult['token']) && isset($ocmResult['providerId'])) {
162
+            return [$ocmResult['token'], $ocmResult['providerId']];
163
+        }
164
+
165
+        $result = $this->tryLegacyEndPoint(rtrim($remote, '/'), '/' . $id . '/reshare', $fields);
166
+        $status = json_decode($result['result'], true);
167
+
168
+        $httpRequestSuccessful = $result['success'];
169
+        $ocsCallSuccessful = $status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200;
170
+        $validToken = isset($status['ocs']['data']['token']) && is_string($status['ocs']['data']['token']);
171
+        $validRemoteId = isset($status['ocs']['data']['remoteId']);
172
+
173
+        if ($httpRequestSuccessful && $ocsCallSuccessful && $validToken && $validRemoteId) {
174
+            return [
175
+                $status['ocs']['data']['token'],
176
+                (int)$status['ocs']['data']['remoteId']
177
+            ];
178
+        }
179
+
180
+        return false;
181
+    }
182
+
183
+    /**
184
+     * send server-to-server unshare to remote server
185
+     *
186
+     * @param string $remote url
187
+     * @param int $id share id
188
+     * @param string $token
189
+     * @return bool
190
+     */
191
+    public function sendRemoteUnShare($remote, $id, $token) {
192
+        $this->sendUpdateToRemote($remote, $id, $token, 'unshare');
193
+    }
194
+
195
+    /**
196
+     * send server-to-server unshare to remote server
197
+     *
198
+     * @param string $remote url
199
+     * @param int $id share id
200
+     * @param string $token
201
+     * @return bool
202
+     */
203
+    public function sendRevokeShare($remote, $id, $token) {
204
+        $this->sendUpdateToRemote($remote, $id, $token, 'reshare_undo');
205
+    }
206
+
207
+    /**
208
+     * send notification to remote server if the permissions was changed
209
+     *
210
+     * @param string $remote
211
+     * @param int $remoteId
212
+     * @param string $token
213
+     * @param int $permissions
214
+     * @return bool
215
+     */
216
+    public function sendPermissionChange($remote, $remoteId, $token, $permissions) {
217
+        $this->sendUpdateToRemote($remote, $remoteId, $token, 'permissions', ['permissions' => $permissions]);
218
+    }
219
+
220
+    /**
221
+     * forward accept reShare to remote server
222
+     *
223
+     * @param string $remote
224
+     * @param int $remoteId
225
+     * @param string $token
226
+     */
227
+    public function sendAcceptShare($remote, $remoteId, $token) {
228
+        $this->sendUpdateToRemote($remote, $remoteId, $token, 'accept');
229
+    }
230
+
231
+    /**
232
+     * forward decline reShare to remote server
233
+     *
234
+     * @param string $remote
235
+     * @param int $remoteId
236
+     * @param string $token
237
+     */
238
+    public function sendDeclineShare($remote, $remoteId, $token) {
239
+        $this->sendUpdateToRemote($remote, $remoteId, $token, 'decline');
240
+    }
241
+
242
+    /**
243
+     * inform remote server whether server-to-server share was accepted/declined
244
+     *
245
+     * @param string $remote
246
+     * @param string $token
247
+     * @param int $remoteId Share id on the remote host
248
+     * @param string $action possible actions: accept, decline, unshare, revoke, permissions
249
+     * @param array $data
250
+     * @param int $try
251
+     * @return boolean
252
+     */
253
+    public function sendUpdateToRemote($remote, $remoteId, $token, $action, $data = [], $try = 0) {
254
+
255
+        $fields = [
256
+            'token' => $token,
257
+            'remoteId' => $remoteId
258
+            ];
259
+        foreach ($data as $key => $value) {
260
+            $fields[$key] = $value;
261
+        }
262
+
263
+        $result = $this->tryHttpPostToShareEndpoint(rtrim($remote, '/'), '/' . $remoteId . '/' . $action, $fields, $action);
264
+        $status = json_decode($result['result'], true);
265
+
266
+        if ($result['success'] &&
267
+            ($status['ocs']['meta']['statuscode'] === 100 ||
268
+                $status['ocs']['meta']['statuscode'] === 200
269
+            )
270
+        ) {
271
+            return true;
272
+        } elseif ($try === 0) {
273
+            // only add new job on first try
274
+            $this->jobList->add('OCA\FederatedFileSharing\BackgroundJob\RetryJob',
275
+                [
276
+                    'remote' => $remote,
277
+                    'remoteId' => $remoteId,
278
+                    'token' => $token,
279
+                    'action' => $action,
280
+                    'data' => json_encode($data),
281
+                    'try' => $try,
282
+                    'lastRun' => $this->getTimestamp()
283
+                ]
284
+            );
285
+        }
286
+
287
+        return false;
288
+    }
289
+
290
+
291
+    /**
292
+     * return current timestamp
293
+     *
294
+     * @return int
295
+     */
296
+    protected function getTimestamp() {
297
+        return time();
298
+    }
299
+
300
+    /**
301
+     * try http post with the given protocol, if no protocol is given we pick
302
+     * the secure one (https)
303
+     *
304
+     * @param string $remoteDomain
305
+     * @param string $urlSuffix
306
+     * @param array $fields post parameters
307
+     * @param string $action define the action (possible values: share, reshare, accept, decline, unshare, revoke, permissions)
308
+     * @return array
309
+     * @throws \Exception
310
+     */
311
+    protected function tryHttpPostToShareEndpoint($remoteDomain, $urlSuffix, array $fields, $action="share") {
312
+
313
+        if ($this->addressHandler->urlContainProtocol($remoteDomain) === false) {
314
+            $remoteDomain = 'https://' . $remoteDomain;
315
+        }
316
+
317
+        $result = [
318
+            'success' => false,
319
+            'result' => '',
320
+        ];
321
+
322
+        // if possible we use the new OCM API
323
+        $ocmResult = $this->tryOCMEndPoint($remoteDomain, $fields, $action);
324
+        if (is_array($ocmResult)) {
325
+            $result['success'] = true;
326
+            $result['result'] = json_encode([
327
+                'ocs' => ['meta' => ['statuscode' => 200]]]);
328
+            return $result;
329
+        }
330
+
331
+        return $this->tryLegacyEndPoint($remoteDomain, $urlSuffix, $fields);
332
+    }
333
+
334
+    /**
335
+     * try old federated sharing API if the OCM api doesn't work
336
+     *
337
+     * @param $remoteDomain
338
+     * @param $urlSuffix
339
+     * @param array $fields
340
+     * @return mixed
341
+     * @throws \Exception
342
+     */
343
+    protected function tryLegacyEndPoint($remoteDomain, $urlSuffix, array $fields) {
344
+
345
+        $result = [
346
+            'success' => false,
347
+            'result' => '',
348
+        ];
349
+
350
+        // Fall back to old API
351
+        $client = $this->httpClientService->newClient();
352
+        $federationEndpoints = $this->discoveryService->discover($remoteDomain, 'FEDERATED_SHARING');
353
+        $endpoint = isset($federationEndpoints['share']) ? $federationEndpoints['share'] : '/ocs/v2.php/cloud/shares';
354
+        try {
355
+            $response = $client->post($remoteDomain . $endpoint . $urlSuffix . '?format=' . self::RESPONSE_FORMAT, [
356
+                'body' => $fields,
357
+                'timeout' => 10,
358
+                'connect_timeout' => 10,
359
+            ]);
360
+            $result['result'] = $response->getBody();
361
+            $result['success'] = true;
362
+        } catch (\Exception $e) {
363
+            // if flat re-sharing is not supported by the remote server
364
+            // we re-throw the exception and fall back to the old behaviour.
365
+            // (flat re-shares has been introduced in Nextcloud 9.1)
366
+            if ($e->getCode() === Http::STATUS_INTERNAL_SERVER_ERROR) {
367
+                throw $e;
368
+            }
369
+        }
370
+
371
+        return $result;
372
+
373
+    }
374
+
375
+    /**
376
+     * check if server supports the new OCM api and ask for the correct end-point
377
+     *
378
+     * @param string $url
379
+     * @return string
380
+     */
381
+    protected function getOCMEndPoint($url) {
382
+        $client = $this->httpClientService->newClient();
383
+        try {
384
+            $response = $client->get($url, ['timeout' => 10, 'connect_timeout' => 10]);
385
+        } catch (\Exception $e) {
386
+            return '';
387
+        }
388
+
389
+        $result = $response->getBody();
390
+        $result = json_decode($result, true);
391
+
392
+        if (isset($result['end-point'])) {
393
+            return $result['end-point'];
394
+        }
395
+
396
+        return '';
397
+    }
398
+
399
+    /**
400
+     * send action regarding federated sharing to the remote server using the OCM API
401
+     *
402
+     * @param $remoteDomain
403
+     * @param $fields
404
+     * @param $action
405
+     *
406
+     * @return bool
407
+     */
408
+    protected function tryOCMEndPoint($remoteDomain, $fields, $action) {
409
+        switch ($action) {
410
+            case 'share':
411
+                $share = $this->cloudFederationFactory->getCloudFederationShare(
412
+                    $fields['shareWith'] . '@' . $remoteDomain,
413
+                    $fields['name'],
414
+                    '',
415
+                    $fields['remoteId'],
416
+                    $fields['ownerFederatedId'],
417
+                    $fields['owner'],
418
+                    $fields['sharedByFederatedId'],
419
+                    $fields['sharedBy'],
420
+                    $fields['token'],
421
+                    $fields['shareType'],
422
+                    'file'
423
+                );
424
+                return $this->federationProviderManager->sendShare($share);
425
+            case 'reshare':
426
+                // ask owner to reshare a file
427
+                $notification = $this->cloudFederationFactory->getCloudFederationNotification();
428
+                $notification->setMessage('REQUEST_RESHARE',
429
+                    'file',
430
+                    $fields['remoteId'],
431
+                    [
432
+                        'sharedSecret' => $fields['token'],
433
+                        'shareWith' => $fields['shareWith'],
434
+                        'senderId' => $fields['localId'],
435
+                        'shareType' => $fields['shareType'],
436
+                        'message' => 'Ask owner to reshare the file'
437
+                    ]
438
+                );
439
+                return $this->federationProviderManager->sendNotification($remoteDomain, $notification);
440
+            case 'unshare':
441
+                //owner unshares the file from the recipient again
442
+                $notification = $this->cloudFederationFactory->getCloudFederationNotification();
443
+                $notification->setMessage('SHARE_UNSHARED',
444
+                    'file',
445
+                    $fields['remoteId'],
446
+                    [
447
+                        'sharedSecret' => $fields['token'],
448
+                        'messgage' => 'file is no longer shared with you'
449
+                    ]
450
+                );
451
+                return $this->federationProviderManager->sendNotification($remoteDomain, $notification);
452
+            case 'reshare_undo':
453
+                // if a reshare was unshared we send the information to the initiator/owner
454
+                $notification = $this->cloudFederationFactory->getCloudFederationNotification();
455
+                $notification->setMessage('RESHARE_UNDO',
456
+                    'file',
457
+                    $fields['remoteId'],
458
+                    [
459
+                        'sharedSecret' => $fields['token'],
460
+                        'message' => 'reshare was revoked'
461
+                    ]
462
+                );
463
+                return $this->federationProviderManager->sendNotification($remoteDomain, $notification);
464
+        }
465
+
466
+        return false;
467
+
468
+    }
469 469
 }
Please login to merge, or discard this patch.