Passed
Push — master ( 268acd...1eb084 )
by Morris
15:42 queued 11s
created
apps/federatedfilesharing/lib/Notifications.php 1 patch
Indentation   +429 added lines, -429 removed lines patch added patch discarded remove patch
@@ -38,433 +38,433 @@
 block discarded – undo
38 38
 use OCP\ILogger;
39 39
 
40 40
 class Notifications {
41
-	public const RESPONSE_FORMAT = 'json'; // default response format for ocs calls
42
-
43
-	/** @var AddressHandler */
44
-	private $addressHandler;
45
-
46
-	/** @var IClientService */
47
-	private $httpClientService;
48
-
49
-	/** @var IDiscoveryService */
50
-	private $discoveryService;
51
-
52
-	/** @var IJobList  */
53
-	private $jobList;
54
-
55
-	/** @var ICloudFederationProviderManager */
56
-	private $federationProviderManager;
57
-
58
-	/** @var ICloudFederationFactory */
59
-	private $cloudFederationFactory;
60
-
61
-	/** @var IEventDispatcher */
62
-	private $eventDispatcher;
63
-
64
-	/** @var ILogger */
65
-	private $logger;
66
-
67
-	public function __construct(
68
-		AddressHandler $addressHandler,
69
-		IClientService $httpClientService,
70
-		IDiscoveryService $discoveryService,
71
-		ILogger $logger,
72
-		IJobList $jobList,
73
-		ICloudFederationProviderManager $federationProviderManager,
74
-		ICloudFederationFactory $cloudFederationFactory,
75
-		IEventDispatcher $eventDispatcher
76
-	) {
77
-		$this->addressHandler = $addressHandler;
78
-		$this->httpClientService = $httpClientService;
79
-		$this->discoveryService = $discoveryService;
80
-		$this->jobList = $jobList;
81
-		$this->logger = $logger;
82
-		$this->federationProviderManager = $federationProviderManager;
83
-		$this->cloudFederationFactory = $cloudFederationFactory;
84
-		$this->eventDispatcher = $eventDispatcher;
85
-	}
86
-
87
-	/**
88
-	 * send server-to-server share to remote server
89
-	 *
90
-	 * @param string $token
91
-	 * @param string $shareWith
92
-	 * @param string $name
93
-	 * @param string $remoteId
94
-	 * @param string $owner
95
-	 * @param string $ownerFederatedId
96
-	 * @param string $sharedBy
97
-	 * @param string $sharedByFederatedId
98
-	 * @param int $shareType (can be a remote user or group share)
99
-	 * @return bool
100
-	 * @throws \OC\HintException
101
-	 * @throws \OC\ServerNotAvailableException
102
-	 */
103
-	public function sendRemoteShare($token, $shareWith, $name, $remoteId, $owner, $ownerFederatedId, $sharedBy, $sharedByFederatedId, $shareType) {
104
-		[$user, $remote] = $this->addressHandler->splitUserRemote($shareWith);
105
-
106
-		if ($user && $remote) {
107
-			$local = $this->addressHandler->generateRemoteURL();
108
-
109
-			$fields = [
110
-				'shareWith' => $user,
111
-				'token' => $token,
112
-				'name' => $name,
113
-				'remoteId' => $remoteId,
114
-				'owner' => $owner,
115
-				'ownerFederatedId' => $ownerFederatedId,
116
-				'sharedBy' => $sharedBy,
117
-				'sharedByFederatedId' => $sharedByFederatedId,
118
-				'remote' => $local,
119
-				'shareType' => $shareType
120
-			];
121
-
122
-			$result = $this->tryHttpPostToShareEndpoint($remote, '', $fields);
123
-			$status = json_decode($result['result'], true);
124
-
125
-			$ocsStatus = isset($status['ocs']);
126
-			$ocsSuccess = $ocsStatus && ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200);
127
-
128
-			if ($result['success'] && (!$ocsStatus || $ocsSuccess)) {
129
-				$event = new FederatedShareAddedEvent($remote);
130
-				$this->eventDispatcher->dispatchTyped($event);
131
-				return true;
132
-			} else {
133
-				$this->logger->info(
134
-					"failed sharing $name with $shareWith",
135
-					['app' => 'federatedfilesharing']
136
-				);
137
-			}
138
-		} else {
139
-			$this->logger->info(
140
-				"could not share $name, invalid contact $shareWith",
141
-				['app' => 'federatedfilesharing']
142
-			);
143
-		}
144
-
145
-		return false;
146
-	}
147
-
148
-	/**
149
-	 * ask owner to re-share the file with the given user
150
-	 *
151
-	 * @param string $token
152
-	 * @param string $id remote Id
153
-	 * @param string $shareId internal share Id
154
-	 * @param string $remote remote address of the owner
155
-	 * @param string $shareWith
156
-	 * @param int $permission
157
-	 * @param string $filename
158
-	 * @return array|false
159
-	 * @throws \OC\HintException
160
-	 * @throws \OC\ServerNotAvailableException
161
-	 */
162
-	public function requestReShare($token, $id, $shareId, $remote, $shareWith, $permission, $filename) {
163
-		$fields = [
164
-			'shareWith' => $shareWith,
165
-			'token' => $token,
166
-			'permission' => $permission,
167
-			'remoteId' => $shareId,
168
-		];
169
-
170
-		$ocmFields = $fields;
171
-		$ocmFields['remoteId'] = (string)$id;
172
-		$ocmFields['localId'] = $shareId;
173
-		$ocmFields['name'] = $filename;
174
-
175
-		$ocmResult = $this->tryOCMEndPoint($remote, $ocmFields, 'reshare');
176
-		if (is_array($ocmResult) && isset($ocmResult['token']) && isset($ocmResult['providerId'])) {
177
-			return [$ocmResult['token'], $ocmResult['providerId']];
178
-		}
179
-
180
-		$result = $this->tryLegacyEndPoint(rtrim($remote, '/'), '/' . $id . '/reshare', $fields);
181
-		$status = json_decode($result['result'], true);
182
-
183
-		$httpRequestSuccessful = $result['success'];
184
-		$ocsCallSuccessful = $status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200;
185
-		$validToken = isset($status['ocs']['data']['token']) && is_string($status['ocs']['data']['token']);
186
-		$validRemoteId = isset($status['ocs']['data']['remoteId']);
187
-
188
-		if ($httpRequestSuccessful && $ocsCallSuccessful && $validToken && $validRemoteId) {
189
-			return [
190
-				$status['ocs']['data']['token'],
191
-				$status['ocs']['data']['remoteId']
192
-			];
193
-		} elseif (!$validToken) {
194
-			$this->logger->info(
195
-				"invalid or missing token requesting re-share for $filename to $remote",
196
-				['app' => 'federatedfilesharing']
197
-			);
198
-		} elseif (!$validRemoteId) {
199
-			$this->logger->info(
200
-				"missing remote id requesting re-share for $filename to $remote",
201
-				['app' => 'federatedfilesharing']
202
-			);
203
-		} else {
204
-			$this->logger->info(
205
-				"failed requesting re-share for $filename to $remote",
206
-				['app' => 'federatedfilesharing']
207
-			);
208
-		}
209
-
210
-		return false;
211
-	}
212
-
213
-	/**
214
-	 * send server-to-server unshare to remote server
215
-	 *
216
-	 * @param string $remote url
217
-	 * @param string $id share id
218
-	 * @param string $token
219
-	 * @return bool
220
-	 */
221
-	public function sendRemoteUnShare($remote, $id, $token) {
222
-		$this->sendUpdateToRemote($remote, $id, $token, 'unshare');
223
-	}
224
-
225
-	/**
226
-	 * send server-to-server unshare to remote server
227
-	 *
228
-	 * @param string $remote url
229
-	 * @param string $id share id
230
-	 * @param string $token
231
-	 * @return bool
232
-	 */
233
-	public function sendRevokeShare($remote, $id, $token) {
234
-		$this->sendUpdateToRemote($remote, $id, $token, 'reshare_undo');
235
-	}
236
-
237
-	/**
238
-	 * send notification to remote server if the permissions was changed
239
-	 *
240
-	 * @param string $remote
241
-	 * @param string $remoteId
242
-	 * @param string $token
243
-	 * @param int $permissions
244
-	 * @return bool
245
-	 */
246
-	public function sendPermissionChange($remote, $remoteId, $token, $permissions) {
247
-		$this->sendUpdateToRemote($remote, $remoteId, $token, 'permissions', ['permissions' => $permissions]);
248
-	}
249
-
250
-	/**
251
-	 * forward accept reShare to remote server
252
-	 *
253
-	 * @param string $remote
254
-	 * @param string $remoteId
255
-	 * @param string $token
256
-	 */
257
-	public function sendAcceptShare($remote, $remoteId, $token) {
258
-		$this->sendUpdateToRemote($remote, $remoteId, $token, 'accept');
259
-	}
260
-
261
-	/**
262
-	 * forward decline reShare to remote server
263
-	 *
264
-	 * @param string $remote
265
-	 * @param string $remoteId
266
-	 * @param string $token
267
-	 */
268
-	public function sendDeclineShare($remote, $remoteId, $token) {
269
-		$this->sendUpdateToRemote($remote, $remoteId, $token, 'decline');
270
-	}
271
-
272
-	/**
273
-	 * inform remote server whether server-to-server share was accepted/declined
274
-	 *
275
-	 * @param string $remote
276
-	 * @param string $token
277
-	 * @param string $remoteId Share id on the remote host
278
-	 * @param string $action possible actions: accept, decline, unshare, revoke, permissions
279
-	 * @param array $data
280
-	 * @param int $try
281
-	 * @return boolean
282
-	 */
283
-	public function sendUpdateToRemote($remote, $remoteId, $token, $action, $data = [], $try = 0) {
284
-		$fields = [
285
-			'token' => $token,
286
-			'remoteId' => $remoteId
287
-		];
288
-		foreach ($data as $key => $value) {
289
-			$fields[$key] = $value;
290
-		}
291
-
292
-		$result = $this->tryHttpPostToShareEndpoint(rtrim($remote, '/'), '/' . $remoteId . '/' . $action, $fields, $action);
293
-		$status = json_decode($result['result'], true);
294
-
295
-		if ($result['success'] &&
296
-			($status['ocs']['meta']['statuscode'] === 100 ||
297
-				$status['ocs']['meta']['statuscode'] === 200
298
-			)
299
-		) {
300
-			return true;
301
-		} elseif ($try === 0) {
302
-			// only add new job on first try
303
-			$this->jobList->add('OCA\FederatedFileSharing\BackgroundJob\RetryJob',
304
-				[
305
-					'remote' => $remote,
306
-					'remoteId' => $remoteId,
307
-					'token' => $token,
308
-					'action' => $action,
309
-					'data' => json_encode($data),
310
-					'try' => $try,
311
-					'lastRun' => $this->getTimestamp()
312
-				]
313
-			);
314
-		}
315
-
316
-		return false;
317
-	}
318
-
319
-
320
-	/**
321
-	 * return current timestamp
322
-	 *
323
-	 * @return int
324
-	 */
325
-	protected function getTimestamp() {
326
-		return time();
327
-	}
328
-
329
-	/**
330
-	 * try http post with the given protocol, if no protocol is given we pick
331
-	 * the secure one (https)
332
-	 *
333
-	 * @param string $remoteDomain
334
-	 * @param string $urlSuffix
335
-	 * @param array $fields post parameters
336
-	 * @param string $action define the action (possible values: share, reshare, accept, decline, unshare, revoke, permissions)
337
-	 * @return array
338
-	 * @throws \Exception
339
-	 */
340
-	protected function tryHttpPostToShareEndpoint($remoteDomain, $urlSuffix, array $fields, $action = "share") {
341
-		if ($this->addressHandler->urlContainProtocol($remoteDomain) === false) {
342
-			$remoteDomain = 'https://' . $remoteDomain;
343
-		}
344
-
345
-		$result = [
346
-			'success' => false,
347
-			'result' => '',
348
-		];
349
-
350
-		// if possible we use the new OCM API
351
-		$ocmResult = $this->tryOCMEndPoint($remoteDomain, $fields, $action);
352
-		if (is_array($ocmResult)) {
353
-			$result['success'] = true;
354
-			$result['result'] = json_encode([
355
-				'ocs' => ['meta' => ['statuscode' => 200]]]);
356
-			return $result;
357
-		}
358
-
359
-		return $this->tryLegacyEndPoint($remoteDomain, $urlSuffix, $fields);
360
-	}
361
-
362
-	/**
363
-	 * try old federated sharing API if the OCM api doesn't work
364
-	 *
365
-	 * @param $remoteDomain
366
-	 * @param $urlSuffix
367
-	 * @param array $fields
368
-	 * @return mixed
369
-	 * @throws \Exception
370
-	 */
371
-	protected function tryLegacyEndPoint($remoteDomain, $urlSuffix, array $fields) {
372
-		$result = [
373
-			'success' => false,
374
-			'result' => '',
375
-		];
376
-
377
-		// Fall back to old API
378
-		$client = $this->httpClientService->newClient();
379
-		$federationEndpoints = $this->discoveryService->discover($remoteDomain, 'FEDERATED_SHARING');
380
-		$endpoint = isset($federationEndpoints['share']) ? $federationEndpoints['share'] : '/ocs/v2.php/cloud/shares';
381
-		try {
382
-			$response = $client->post($remoteDomain . $endpoint . $urlSuffix . '?format=' . self::RESPONSE_FORMAT, [
383
-				'body' => $fields,
384
-				'timeout' => 10,
385
-				'connect_timeout' => 10,
386
-			]);
387
-			$result['result'] = $response->getBody();
388
-			$result['success'] = true;
389
-		} catch (\Exception $e) {
390
-			// if flat re-sharing is not supported by the remote server
391
-			// we re-throw the exception and fall back to the old behaviour.
392
-			// (flat re-shares has been introduced in Nextcloud 9.1)
393
-			if ($e->getCode() === Http::STATUS_INTERNAL_SERVER_ERROR) {
394
-				throw $e;
395
-			}
396
-		}
397
-
398
-		return $result;
399
-	}
400
-
401
-	/**
402
-	 * send action regarding federated sharing to the remote server using the OCM API
403
-	 *
404
-	 * @param $remoteDomain
405
-	 * @param $fields
406
-	 * @param $action
407
-	 *
408
-	 * @return bool
409
-	 */
410
-	protected function tryOCMEndPoint($remoteDomain, $fields, $action) {
411
-		switch ($action) {
412
-			case 'share':
413
-				$share = $this->cloudFederationFactory->getCloudFederationShare(
414
-					$fields['shareWith'] . '@' . $remoteDomain,
415
-					$fields['name'],
416
-					'',
417
-					$fields['remoteId'],
418
-					$fields['ownerFederatedId'],
419
-					$fields['owner'],
420
-					$fields['sharedByFederatedId'],
421
-					$fields['sharedBy'],
422
-					$fields['token'],
423
-					$fields['shareType'],
424
-					'file'
425
-				);
426
-				return $this->federationProviderManager->sendShare($share);
427
-			case 'reshare':
428
-				// ask owner to reshare a file
429
-				$notification = $this->cloudFederationFactory->getCloudFederationNotification();
430
-				$notification->setMessage('REQUEST_RESHARE',
431
-					'file',
432
-					$fields['remoteId'],
433
-					[
434
-						'sharedSecret' => $fields['token'],
435
-						'shareWith' => $fields['shareWith'],
436
-						'senderId' => $fields['localId'],
437
-						'shareType' => $fields['shareType'],
438
-						'message' => 'Ask owner to reshare the file'
439
-					]
440
-				);
441
-				return $this->federationProviderManager->sendNotification($remoteDomain, $notification);
442
-			case 'unshare':
443
-				//owner unshares the file from the recipient again
444
-				$notification = $this->cloudFederationFactory->getCloudFederationNotification();
445
-				$notification->setMessage('SHARE_UNSHARED',
446
-					'file',
447
-					$fields['remoteId'],
448
-					[
449
-						'sharedSecret' => $fields['token'],
450
-						'messgage' => 'file is no longer shared with you'
451
-					]
452
-				);
453
-				return $this->federationProviderManager->sendNotification($remoteDomain, $notification);
454
-			case 'reshare_undo':
455
-				// if a reshare was unshared we send the information to the initiator/owner
456
-				$notification = $this->cloudFederationFactory->getCloudFederationNotification();
457
-				$notification->setMessage('RESHARE_UNDO',
458
-					'file',
459
-					$fields['remoteId'],
460
-					[
461
-						'sharedSecret' => $fields['token'],
462
-						'message' => 'reshare was revoked'
463
-					]
464
-				);
465
-				return $this->federationProviderManager->sendNotification($remoteDomain, $notification);
466
-		}
467
-
468
-		return false;
469
-	}
41
+    public const RESPONSE_FORMAT = 'json'; // default response format for ocs calls
42
+
43
+    /** @var AddressHandler */
44
+    private $addressHandler;
45
+
46
+    /** @var IClientService */
47
+    private $httpClientService;
48
+
49
+    /** @var IDiscoveryService */
50
+    private $discoveryService;
51
+
52
+    /** @var IJobList  */
53
+    private $jobList;
54
+
55
+    /** @var ICloudFederationProviderManager */
56
+    private $federationProviderManager;
57
+
58
+    /** @var ICloudFederationFactory */
59
+    private $cloudFederationFactory;
60
+
61
+    /** @var IEventDispatcher */
62
+    private $eventDispatcher;
63
+
64
+    /** @var ILogger */
65
+    private $logger;
66
+
67
+    public function __construct(
68
+        AddressHandler $addressHandler,
69
+        IClientService $httpClientService,
70
+        IDiscoveryService $discoveryService,
71
+        ILogger $logger,
72
+        IJobList $jobList,
73
+        ICloudFederationProviderManager $federationProviderManager,
74
+        ICloudFederationFactory $cloudFederationFactory,
75
+        IEventDispatcher $eventDispatcher
76
+    ) {
77
+        $this->addressHandler = $addressHandler;
78
+        $this->httpClientService = $httpClientService;
79
+        $this->discoveryService = $discoveryService;
80
+        $this->jobList = $jobList;
81
+        $this->logger = $logger;
82
+        $this->federationProviderManager = $federationProviderManager;
83
+        $this->cloudFederationFactory = $cloudFederationFactory;
84
+        $this->eventDispatcher = $eventDispatcher;
85
+    }
86
+
87
+    /**
88
+     * send server-to-server share to remote server
89
+     *
90
+     * @param string $token
91
+     * @param string $shareWith
92
+     * @param string $name
93
+     * @param string $remoteId
94
+     * @param string $owner
95
+     * @param string $ownerFederatedId
96
+     * @param string $sharedBy
97
+     * @param string $sharedByFederatedId
98
+     * @param int $shareType (can be a remote user or group share)
99
+     * @return bool
100
+     * @throws \OC\HintException
101
+     * @throws \OC\ServerNotAvailableException
102
+     */
103
+    public function sendRemoteShare($token, $shareWith, $name, $remoteId, $owner, $ownerFederatedId, $sharedBy, $sharedByFederatedId, $shareType) {
104
+        [$user, $remote] = $this->addressHandler->splitUserRemote($shareWith);
105
+
106
+        if ($user && $remote) {
107
+            $local = $this->addressHandler->generateRemoteURL();
108
+
109
+            $fields = [
110
+                'shareWith' => $user,
111
+                'token' => $token,
112
+                'name' => $name,
113
+                'remoteId' => $remoteId,
114
+                'owner' => $owner,
115
+                'ownerFederatedId' => $ownerFederatedId,
116
+                'sharedBy' => $sharedBy,
117
+                'sharedByFederatedId' => $sharedByFederatedId,
118
+                'remote' => $local,
119
+                'shareType' => $shareType
120
+            ];
121
+
122
+            $result = $this->tryHttpPostToShareEndpoint($remote, '', $fields);
123
+            $status = json_decode($result['result'], true);
124
+
125
+            $ocsStatus = isset($status['ocs']);
126
+            $ocsSuccess = $ocsStatus && ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200);
127
+
128
+            if ($result['success'] && (!$ocsStatus || $ocsSuccess)) {
129
+                $event = new FederatedShareAddedEvent($remote);
130
+                $this->eventDispatcher->dispatchTyped($event);
131
+                return true;
132
+            } else {
133
+                $this->logger->info(
134
+                    "failed sharing $name with $shareWith",
135
+                    ['app' => 'federatedfilesharing']
136
+                );
137
+            }
138
+        } else {
139
+            $this->logger->info(
140
+                "could not share $name, invalid contact $shareWith",
141
+                ['app' => 'federatedfilesharing']
142
+            );
143
+        }
144
+
145
+        return false;
146
+    }
147
+
148
+    /**
149
+     * ask owner to re-share the file with the given user
150
+     *
151
+     * @param string $token
152
+     * @param string $id remote Id
153
+     * @param string $shareId internal share Id
154
+     * @param string $remote remote address of the owner
155
+     * @param string $shareWith
156
+     * @param int $permission
157
+     * @param string $filename
158
+     * @return array|false
159
+     * @throws \OC\HintException
160
+     * @throws \OC\ServerNotAvailableException
161
+     */
162
+    public function requestReShare($token, $id, $shareId, $remote, $shareWith, $permission, $filename) {
163
+        $fields = [
164
+            'shareWith' => $shareWith,
165
+            'token' => $token,
166
+            'permission' => $permission,
167
+            'remoteId' => $shareId,
168
+        ];
169
+
170
+        $ocmFields = $fields;
171
+        $ocmFields['remoteId'] = (string)$id;
172
+        $ocmFields['localId'] = $shareId;
173
+        $ocmFields['name'] = $filename;
174
+
175
+        $ocmResult = $this->tryOCMEndPoint($remote, $ocmFields, 'reshare');
176
+        if (is_array($ocmResult) && isset($ocmResult['token']) && isset($ocmResult['providerId'])) {
177
+            return [$ocmResult['token'], $ocmResult['providerId']];
178
+        }
179
+
180
+        $result = $this->tryLegacyEndPoint(rtrim($remote, '/'), '/' . $id . '/reshare', $fields);
181
+        $status = json_decode($result['result'], true);
182
+
183
+        $httpRequestSuccessful = $result['success'];
184
+        $ocsCallSuccessful = $status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200;
185
+        $validToken = isset($status['ocs']['data']['token']) && is_string($status['ocs']['data']['token']);
186
+        $validRemoteId = isset($status['ocs']['data']['remoteId']);
187
+
188
+        if ($httpRequestSuccessful && $ocsCallSuccessful && $validToken && $validRemoteId) {
189
+            return [
190
+                $status['ocs']['data']['token'],
191
+                $status['ocs']['data']['remoteId']
192
+            ];
193
+        } elseif (!$validToken) {
194
+            $this->logger->info(
195
+                "invalid or missing token requesting re-share for $filename to $remote",
196
+                ['app' => 'federatedfilesharing']
197
+            );
198
+        } elseif (!$validRemoteId) {
199
+            $this->logger->info(
200
+                "missing remote id requesting re-share for $filename to $remote",
201
+                ['app' => 'federatedfilesharing']
202
+            );
203
+        } else {
204
+            $this->logger->info(
205
+                "failed requesting re-share for $filename to $remote",
206
+                ['app' => 'federatedfilesharing']
207
+            );
208
+        }
209
+
210
+        return false;
211
+    }
212
+
213
+    /**
214
+     * send server-to-server unshare to remote server
215
+     *
216
+     * @param string $remote url
217
+     * @param string $id share id
218
+     * @param string $token
219
+     * @return bool
220
+     */
221
+    public function sendRemoteUnShare($remote, $id, $token) {
222
+        $this->sendUpdateToRemote($remote, $id, $token, 'unshare');
223
+    }
224
+
225
+    /**
226
+     * send server-to-server unshare to remote server
227
+     *
228
+     * @param string $remote url
229
+     * @param string $id share id
230
+     * @param string $token
231
+     * @return bool
232
+     */
233
+    public function sendRevokeShare($remote, $id, $token) {
234
+        $this->sendUpdateToRemote($remote, $id, $token, 'reshare_undo');
235
+    }
236
+
237
+    /**
238
+     * send notification to remote server if the permissions was changed
239
+     *
240
+     * @param string $remote
241
+     * @param string $remoteId
242
+     * @param string $token
243
+     * @param int $permissions
244
+     * @return bool
245
+     */
246
+    public function sendPermissionChange($remote, $remoteId, $token, $permissions) {
247
+        $this->sendUpdateToRemote($remote, $remoteId, $token, 'permissions', ['permissions' => $permissions]);
248
+    }
249
+
250
+    /**
251
+     * forward accept reShare to remote server
252
+     *
253
+     * @param string $remote
254
+     * @param string $remoteId
255
+     * @param string $token
256
+     */
257
+    public function sendAcceptShare($remote, $remoteId, $token) {
258
+        $this->sendUpdateToRemote($remote, $remoteId, $token, 'accept');
259
+    }
260
+
261
+    /**
262
+     * forward decline reShare to remote server
263
+     *
264
+     * @param string $remote
265
+     * @param string $remoteId
266
+     * @param string $token
267
+     */
268
+    public function sendDeclineShare($remote, $remoteId, $token) {
269
+        $this->sendUpdateToRemote($remote, $remoteId, $token, 'decline');
270
+    }
271
+
272
+    /**
273
+     * inform remote server whether server-to-server share was accepted/declined
274
+     *
275
+     * @param string $remote
276
+     * @param string $token
277
+     * @param string $remoteId Share id on the remote host
278
+     * @param string $action possible actions: accept, decline, unshare, revoke, permissions
279
+     * @param array $data
280
+     * @param int $try
281
+     * @return boolean
282
+     */
283
+    public function sendUpdateToRemote($remote, $remoteId, $token, $action, $data = [], $try = 0) {
284
+        $fields = [
285
+            'token' => $token,
286
+            'remoteId' => $remoteId
287
+        ];
288
+        foreach ($data as $key => $value) {
289
+            $fields[$key] = $value;
290
+        }
291
+
292
+        $result = $this->tryHttpPostToShareEndpoint(rtrim($remote, '/'), '/' . $remoteId . '/' . $action, $fields, $action);
293
+        $status = json_decode($result['result'], true);
294
+
295
+        if ($result['success'] &&
296
+            ($status['ocs']['meta']['statuscode'] === 100 ||
297
+                $status['ocs']['meta']['statuscode'] === 200
298
+            )
299
+        ) {
300
+            return true;
301
+        } elseif ($try === 0) {
302
+            // only add new job on first try
303
+            $this->jobList->add('OCA\FederatedFileSharing\BackgroundJob\RetryJob',
304
+                [
305
+                    'remote' => $remote,
306
+                    'remoteId' => $remoteId,
307
+                    'token' => $token,
308
+                    'action' => $action,
309
+                    'data' => json_encode($data),
310
+                    'try' => $try,
311
+                    'lastRun' => $this->getTimestamp()
312
+                ]
313
+            );
314
+        }
315
+
316
+        return false;
317
+    }
318
+
319
+
320
+    /**
321
+     * return current timestamp
322
+     *
323
+     * @return int
324
+     */
325
+    protected function getTimestamp() {
326
+        return time();
327
+    }
328
+
329
+    /**
330
+     * try http post with the given protocol, if no protocol is given we pick
331
+     * the secure one (https)
332
+     *
333
+     * @param string $remoteDomain
334
+     * @param string $urlSuffix
335
+     * @param array $fields post parameters
336
+     * @param string $action define the action (possible values: share, reshare, accept, decline, unshare, revoke, permissions)
337
+     * @return array
338
+     * @throws \Exception
339
+     */
340
+    protected function tryHttpPostToShareEndpoint($remoteDomain, $urlSuffix, array $fields, $action = "share") {
341
+        if ($this->addressHandler->urlContainProtocol($remoteDomain) === false) {
342
+            $remoteDomain = 'https://' . $remoteDomain;
343
+        }
344
+
345
+        $result = [
346
+            'success' => false,
347
+            'result' => '',
348
+        ];
349
+
350
+        // if possible we use the new OCM API
351
+        $ocmResult = $this->tryOCMEndPoint($remoteDomain, $fields, $action);
352
+        if (is_array($ocmResult)) {
353
+            $result['success'] = true;
354
+            $result['result'] = json_encode([
355
+                'ocs' => ['meta' => ['statuscode' => 200]]]);
356
+            return $result;
357
+        }
358
+
359
+        return $this->tryLegacyEndPoint($remoteDomain, $urlSuffix, $fields);
360
+    }
361
+
362
+    /**
363
+     * try old federated sharing API if the OCM api doesn't work
364
+     *
365
+     * @param $remoteDomain
366
+     * @param $urlSuffix
367
+     * @param array $fields
368
+     * @return mixed
369
+     * @throws \Exception
370
+     */
371
+    protected function tryLegacyEndPoint($remoteDomain, $urlSuffix, array $fields) {
372
+        $result = [
373
+            'success' => false,
374
+            'result' => '',
375
+        ];
376
+
377
+        // Fall back to old API
378
+        $client = $this->httpClientService->newClient();
379
+        $federationEndpoints = $this->discoveryService->discover($remoteDomain, 'FEDERATED_SHARING');
380
+        $endpoint = isset($federationEndpoints['share']) ? $federationEndpoints['share'] : '/ocs/v2.php/cloud/shares';
381
+        try {
382
+            $response = $client->post($remoteDomain . $endpoint . $urlSuffix . '?format=' . self::RESPONSE_FORMAT, [
383
+                'body' => $fields,
384
+                'timeout' => 10,
385
+                'connect_timeout' => 10,
386
+            ]);
387
+            $result['result'] = $response->getBody();
388
+            $result['success'] = true;
389
+        } catch (\Exception $e) {
390
+            // if flat re-sharing is not supported by the remote server
391
+            // we re-throw the exception and fall back to the old behaviour.
392
+            // (flat re-shares has been introduced in Nextcloud 9.1)
393
+            if ($e->getCode() === Http::STATUS_INTERNAL_SERVER_ERROR) {
394
+                throw $e;
395
+            }
396
+        }
397
+
398
+        return $result;
399
+    }
400
+
401
+    /**
402
+     * send action regarding federated sharing to the remote server using the OCM API
403
+     *
404
+     * @param $remoteDomain
405
+     * @param $fields
406
+     * @param $action
407
+     *
408
+     * @return bool
409
+     */
410
+    protected function tryOCMEndPoint($remoteDomain, $fields, $action) {
411
+        switch ($action) {
412
+            case 'share':
413
+                $share = $this->cloudFederationFactory->getCloudFederationShare(
414
+                    $fields['shareWith'] . '@' . $remoteDomain,
415
+                    $fields['name'],
416
+                    '',
417
+                    $fields['remoteId'],
418
+                    $fields['ownerFederatedId'],
419
+                    $fields['owner'],
420
+                    $fields['sharedByFederatedId'],
421
+                    $fields['sharedBy'],
422
+                    $fields['token'],
423
+                    $fields['shareType'],
424
+                    'file'
425
+                );
426
+                return $this->federationProviderManager->sendShare($share);
427
+            case 'reshare':
428
+                // ask owner to reshare a file
429
+                $notification = $this->cloudFederationFactory->getCloudFederationNotification();
430
+                $notification->setMessage('REQUEST_RESHARE',
431
+                    'file',
432
+                    $fields['remoteId'],
433
+                    [
434
+                        'sharedSecret' => $fields['token'],
435
+                        'shareWith' => $fields['shareWith'],
436
+                        'senderId' => $fields['localId'],
437
+                        'shareType' => $fields['shareType'],
438
+                        'message' => 'Ask owner to reshare the file'
439
+                    ]
440
+                );
441
+                return $this->federationProviderManager->sendNotification($remoteDomain, $notification);
442
+            case 'unshare':
443
+                //owner unshares the file from the recipient again
444
+                $notification = $this->cloudFederationFactory->getCloudFederationNotification();
445
+                $notification->setMessage('SHARE_UNSHARED',
446
+                    'file',
447
+                    $fields['remoteId'],
448
+                    [
449
+                        'sharedSecret' => $fields['token'],
450
+                        'messgage' => 'file is no longer shared with you'
451
+                    ]
452
+                );
453
+                return $this->federationProviderManager->sendNotification($remoteDomain, $notification);
454
+            case 'reshare_undo':
455
+                // if a reshare was unshared we send the information to the initiator/owner
456
+                $notification = $this->cloudFederationFactory->getCloudFederationNotification();
457
+                $notification->setMessage('RESHARE_UNDO',
458
+                    'file',
459
+                    $fields['remoteId'],
460
+                    [
461
+                        'sharedSecret' => $fields['token'],
462
+                        'message' => 'reshare was revoked'
463
+                    ]
464
+                );
465
+                return $this->federationProviderManager->sendNotification($remoteDomain, $notification);
466
+        }
467
+
468
+        return false;
469
+    }
470 470
 }
Please login to merge, or discard this patch.
lib/private/Share20/ProviderFactory.php 1 patch
Indentation   +298 added lines, -298 removed lines patch added patch discarded remove patch
@@ -55,311 +55,311 @@
 block discarded – undo
55 55
  */
56 56
 class ProviderFactory implements IProviderFactory {
57 57
 
58
-	/** @var IServerContainer */
59
-	private $serverContainer;
60
-	/** @var DefaultShareProvider */
61
-	private $defaultProvider = null;
62
-	/** @var FederatedShareProvider */
63
-	private $federatedProvider = null;
64
-	/** @var  ShareByMailProvider */
65
-	private $shareByMailProvider;
66
-	/** @var  \OCA\Circles\ShareByCircleProvider */
67
-	private $shareByCircleProvider = null;
68
-	/** @var bool */
69
-	private $circlesAreNotAvailable = false;
70
-	/** @var \OCA\Talk\Share\RoomShareProvider */
71
-	private $roomShareProvider = null;
72
-
73
-	private $registeredShareProviders = [];
74
-
75
-	private $shareProviders = [];
76
-
77
-	/**
78
-	 * IProviderFactory constructor.
79
-	 *
80
-	 * @param IServerContainer $serverContainer
81
-	 */
82
-	public function __construct(IServerContainer $serverContainer) {
83
-		$this->serverContainer = $serverContainer;
84
-	}
85
-
86
-	public function registerProvider(string $shareProviderClass): void {
87
-		$this->registeredShareProviders[] = $shareProviderClass;
88
-	}
89
-
90
-	/**
91
-	 * Create the default share provider.
92
-	 *
93
-	 * @return DefaultShareProvider
94
-	 */
95
-	protected function defaultShareProvider() {
96
-		if ($this->defaultProvider === null) {
97
-			$this->defaultProvider = new DefaultShareProvider(
98
-				$this->serverContainer->getDatabaseConnection(),
99
-				$this->serverContainer->getUserManager(),
100
-				$this->serverContainer->getGroupManager(),
101
-				$this->serverContainer->getLazyRootFolder(),
102
-				$this->serverContainer->getMailer(),
103
-				$this->serverContainer->query(Defaults::class),
104
-				$this->serverContainer->getL10NFactory(),
105
-				$this->serverContainer->getURLGenerator(),
106
-				$this->serverContainer->getConfig()
107
-			);
108
-		}
109
-
110
-		return $this->defaultProvider;
111
-	}
112
-
113
-	/**
114
-	 * Create the federated share provider
115
-	 *
116
-	 * @return FederatedShareProvider
117
-	 */
118
-	protected function federatedShareProvider() {
119
-		if ($this->federatedProvider === null) {
120
-			/*
58
+    /** @var IServerContainer */
59
+    private $serverContainer;
60
+    /** @var DefaultShareProvider */
61
+    private $defaultProvider = null;
62
+    /** @var FederatedShareProvider */
63
+    private $federatedProvider = null;
64
+    /** @var  ShareByMailProvider */
65
+    private $shareByMailProvider;
66
+    /** @var  \OCA\Circles\ShareByCircleProvider */
67
+    private $shareByCircleProvider = null;
68
+    /** @var bool */
69
+    private $circlesAreNotAvailable = false;
70
+    /** @var \OCA\Talk\Share\RoomShareProvider */
71
+    private $roomShareProvider = null;
72
+
73
+    private $registeredShareProviders = [];
74
+
75
+    private $shareProviders = [];
76
+
77
+    /**
78
+     * IProviderFactory constructor.
79
+     *
80
+     * @param IServerContainer $serverContainer
81
+     */
82
+    public function __construct(IServerContainer $serverContainer) {
83
+        $this->serverContainer = $serverContainer;
84
+    }
85
+
86
+    public function registerProvider(string $shareProviderClass): void {
87
+        $this->registeredShareProviders[] = $shareProviderClass;
88
+    }
89
+
90
+    /**
91
+     * Create the default share provider.
92
+     *
93
+     * @return DefaultShareProvider
94
+     */
95
+    protected function defaultShareProvider() {
96
+        if ($this->defaultProvider === null) {
97
+            $this->defaultProvider = new DefaultShareProvider(
98
+                $this->serverContainer->getDatabaseConnection(),
99
+                $this->serverContainer->getUserManager(),
100
+                $this->serverContainer->getGroupManager(),
101
+                $this->serverContainer->getLazyRootFolder(),
102
+                $this->serverContainer->getMailer(),
103
+                $this->serverContainer->query(Defaults::class),
104
+                $this->serverContainer->getL10NFactory(),
105
+                $this->serverContainer->getURLGenerator(),
106
+                $this->serverContainer->getConfig()
107
+            );
108
+        }
109
+
110
+        return $this->defaultProvider;
111
+    }
112
+
113
+    /**
114
+     * Create the federated share provider
115
+     *
116
+     * @return FederatedShareProvider
117
+     */
118
+    protected function federatedShareProvider() {
119
+        if ($this->federatedProvider === null) {
120
+            /*
121 121
 			 * Check if the app is enabled
122 122
 			 */
123
-			$appManager = $this->serverContainer->getAppManager();
124
-			if (!$appManager->isEnabledForUser('federatedfilesharing')) {
125
-				return null;
126
-			}
123
+            $appManager = $this->serverContainer->getAppManager();
124
+            if (!$appManager->isEnabledForUser('federatedfilesharing')) {
125
+                return null;
126
+            }
127 127
 
128
-			/*
128
+            /*
129 129
 			 * TODO: add factory to federated sharing app
130 130
 			 */
131
-			$l = $this->serverContainer->getL10N('federatedfilesharing');
132
-			$addressHandler = new AddressHandler(
133
-				$this->serverContainer->getURLGenerator(),
134
-				$l,
135
-				$this->serverContainer->getCloudIdManager()
136
-			);
137
-			$notifications = new Notifications(
138
-				$addressHandler,
139
-				$this->serverContainer->getHTTPClientService(),
140
-				$this->serverContainer->query(\OCP\OCS\IDiscoveryService::class),
141
-				$this->serverContainer->getLogger(),
142
-				$this->serverContainer->getJobList(),
143
-				\OC::$server->getCloudFederationProviderManager(),
144
-				\OC::$server->getCloudFederationFactory(),
145
-				$this->serverContainer->query(IEventDispatcher::class)
146
-			);
147
-			$tokenHandler = new TokenHandler(
148
-				$this->serverContainer->getSecureRandom()
149
-			);
150
-
151
-			$this->federatedProvider = new FederatedShareProvider(
152
-				$this->serverContainer->getDatabaseConnection(),
153
-				$addressHandler,
154
-				$notifications,
155
-				$tokenHandler,
156
-				$l,
157
-				$this->serverContainer->getLogger(),
158
-				$this->serverContainer->getLazyRootFolder(),
159
-				$this->serverContainer->getConfig(),
160
-				$this->serverContainer->getUserManager(),
161
-				$this->serverContainer->getCloudIdManager(),
162
-				$this->serverContainer->getGlobalScaleConfig(),
163
-				$this->serverContainer->getCloudFederationProviderManager()
164
-			);
165
-		}
166
-
167
-		return $this->federatedProvider;
168
-	}
169
-
170
-	/**
171
-	 * Create the federated share provider
172
-	 *
173
-	 * @return ShareByMailProvider
174
-	 */
175
-	protected function getShareByMailProvider() {
176
-		if ($this->shareByMailProvider === null) {
177
-			/*
131
+            $l = $this->serverContainer->getL10N('federatedfilesharing');
132
+            $addressHandler = new AddressHandler(
133
+                $this->serverContainer->getURLGenerator(),
134
+                $l,
135
+                $this->serverContainer->getCloudIdManager()
136
+            );
137
+            $notifications = new Notifications(
138
+                $addressHandler,
139
+                $this->serverContainer->getHTTPClientService(),
140
+                $this->serverContainer->query(\OCP\OCS\IDiscoveryService::class),
141
+                $this->serverContainer->getLogger(),
142
+                $this->serverContainer->getJobList(),
143
+                \OC::$server->getCloudFederationProviderManager(),
144
+                \OC::$server->getCloudFederationFactory(),
145
+                $this->serverContainer->query(IEventDispatcher::class)
146
+            );
147
+            $tokenHandler = new TokenHandler(
148
+                $this->serverContainer->getSecureRandom()
149
+            );
150
+
151
+            $this->federatedProvider = new FederatedShareProvider(
152
+                $this->serverContainer->getDatabaseConnection(),
153
+                $addressHandler,
154
+                $notifications,
155
+                $tokenHandler,
156
+                $l,
157
+                $this->serverContainer->getLogger(),
158
+                $this->serverContainer->getLazyRootFolder(),
159
+                $this->serverContainer->getConfig(),
160
+                $this->serverContainer->getUserManager(),
161
+                $this->serverContainer->getCloudIdManager(),
162
+                $this->serverContainer->getGlobalScaleConfig(),
163
+                $this->serverContainer->getCloudFederationProviderManager()
164
+            );
165
+        }
166
+
167
+        return $this->federatedProvider;
168
+    }
169
+
170
+    /**
171
+     * Create the federated share provider
172
+     *
173
+     * @return ShareByMailProvider
174
+     */
175
+    protected function getShareByMailProvider() {
176
+        if ($this->shareByMailProvider === null) {
177
+            /*
178 178
 			 * Check if the app is enabled
179 179
 			 */
180
-			$appManager = $this->serverContainer->getAppManager();
181
-			if (!$appManager->isEnabledForUser('sharebymail')) {
182
-				return null;
183
-			}
184
-
185
-			$settingsManager = new SettingsManager($this->serverContainer->getConfig());
186
-
187
-			$this->shareByMailProvider = new ShareByMailProvider(
188
-				$this->serverContainer->getDatabaseConnection(),
189
-				$this->serverContainer->getSecureRandom(),
190
-				$this->serverContainer->getUserManager(),
191
-				$this->serverContainer->getLazyRootFolder(),
192
-				$this->serverContainer->getL10N('sharebymail'),
193
-				$this->serverContainer->getLogger(),
194
-				$this->serverContainer->getMailer(),
195
-				$this->serverContainer->getURLGenerator(),
196
-				$this->serverContainer->getActivityManager(),
197
-				$settingsManager,
198
-				$this->serverContainer->query(Defaults::class),
199
-				$this->serverContainer->getHasher(),
200
-				$this->serverContainer->get(IEventDispatcher::class),
201
-				$this->serverContainer->get(IManager::class)
202
-			);
203
-		}
204
-
205
-		return $this->shareByMailProvider;
206
-	}
207
-
208
-
209
-	/**
210
-	 * Create the circle share provider
211
-	 *
212
-	 * @return FederatedShareProvider
213
-	 *
214
-	 * @suppress PhanUndeclaredClassMethod
215
-	 */
216
-	protected function getShareByCircleProvider() {
217
-		if ($this->circlesAreNotAvailable) {
218
-			return null;
219
-		}
220
-
221
-		if (!$this->serverContainer->getAppManager()->isEnabledForUser('circles') ||
222
-			!class_exists('\OCA\Circles\ShareByCircleProvider')
223
-		) {
224
-			$this->circlesAreNotAvailable = true;
225
-			return null;
226
-		}
227
-
228
-		if ($this->shareByCircleProvider === null) {
229
-			$this->shareByCircleProvider = new \OCA\Circles\ShareByCircleProvider(
230
-				$this->serverContainer->getDatabaseConnection(),
231
-				$this->serverContainer->getSecureRandom(),
232
-				$this->serverContainer->getUserManager(),
233
-				$this->serverContainer->getLazyRootFolder(),
234
-				$this->serverContainer->getL10N('circles'),
235
-				$this->serverContainer->getLogger(),
236
-				$this->serverContainer->getURLGenerator()
237
-			);
238
-		}
239
-
240
-		return $this->shareByCircleProvider;
241
-	}
242
-
243
-	/**
244
-	 * Create the room share provider
245
-	 *
246
-	 * @return RoomShareProvider
247
-	 */
248
-	protected function getRoomShareProvider() {
249
-		if ($this->roomShareProvider === null) {
250
-			/*
180
+            $appManager = $this->serverContainer->getAppManager();
181
+            if (!$appManager->isEnabledForUser('sharebymail')) {
182
+                return null;
183
+            }
184
+
185
+            $settingsManager = new SettingsManager($this->serverContainer->getConfig());
186
+
187
+            $this->shareByMailProvider = new ShareByMailProvider(
188
+                $this->serverContainer->getDatabaseConnection(),
189
+                $this->serverContainer->getSecureRandom(),
190
+                $this->serverContainer->getUserManager(),
191
+                $this->serverContainer->getLazyRootFolder(),
192
+                $this->serverContainer->getL10N('sharebymail'),
193
+                $this->serverContainer->getLogger(),
194
+                $this->serverContainer->getMailer(),
195
+                $this->serverContainer->getURLGenerator(),
196
+                $this->serverContainer->getActivityManager(),
197
+                $settingsManager,
198
+                $this->serverContainer->query(Defaults::class),
199
+                $this->serverContainer->getHasher(),
200
+                $this->serverContainer->get(IEventDispatcher::class),
201
+                $this->serverContainer->get(IManager::class)
202
+            );
203
+        }
204
+
205
+        return $this->shareByMailProvider;
206
+    }
207
+
208
+
209
+    /**
210
+     * Create the circle share provider
211
+     *
212
+     * @return FederatedShareProvider
213
+     *
214
+     * @suppress PhanUndeclaredClassMethod
215
+     */
216
+    protected function getShareByCircleProvider() {
217
+        if ($this->circlesAreNotAvailable) {
218
+            return null;
219
+        }
220
+
221
+        if (!$this->serverContainer->getAppManager()->isEnabledForUser('circles') ||
222
+            !class_exists('\OCA\Circles\ShareByCircleProvider')
223
+        ) {
224
+            $this->circlesAreNotAvailable = true;
225
+            return null;
226
+        }
227
+
228
+        if ($this->shareByCircleProvider === null) {
229
+            $this->shareByCircleProvider = new \OCA\Circles\ShareByCircleProvider(
230
+                $this->serverContainer->getDatabaseConnection(),
231
+                $this->serverContainer->getSecureRandom(),
232
+                $this->serverContainer->getUserManager(),
233
+                $this->serverContainer->getLazyRootFolder(),
234
+                $this->serverContainer->getL10N('circles'),
235
+                $this->serverContainer->getLogger(),
236
+                $this->serverContainer->getURLGenerator()
237
+            );
238
+        }
239
+
240
+        return $this->shareByCircleProvider;
241
+    }
242
+
243
+    /**
244
+     * Create the room share provider
245
+     *
246
+     * @return RoomShareProvider
247
+     */
248
+    protected function getRoomShareProvider() {
249
+        if ($this->roomShareProvider === null) {
250
+            /*
251 251
 			 * Check if the app is enabled
252 252
 			 */
253
-			$appManager = $this->serverContainer->getAppManager();
254
-			if (!$appManager->isEnabledForUser('spreed')) {
255
-				return null;
256
-			}
257
-
258
-			try {
259
-				$this->roomShareProvider = $this->serverContainer->query('\OCA\Talk\Share\RoomShareProvider');
260
-			} catch (\OCP\AppFramework\QueryException $e) {
261
-				return null;
262
-			}
263
-		}
264
-
265
-		return $this->roomShareProvider;
266
-	}
267
-
268
-	/**
269
-	 * @inheritdoc
270
-	 */
271
-	public function getProvider($id) {
272
-		$provider = null;
273
-		if (isset($this->shareProviders[$id])) {
274
-			return $this->shareProviders[$id];
275
-		}
276
-
277
-		if ($id === 'ocinternal') {
278
-			$provider = $this->defaultShareProvider();
279
-		} elseif ($id === 'ocFederatedSharing') {
280
-			$provider = $this->federatedShareProvider();
281
-		} elseif ($id === 'ocMailShare') {
282
-			$provider = $this->getShareByMailProvider();
283
-		} elseif ($id === 'ocCircleShare') {
284
-			$provider = $this->getShareByCircleProvider();
285
-		} elseif ($id === 'ocRoomShare') {
286
-			$provider = $this->getRoomShareProvider();
287
-		}
288
-
289
-		foreach ($this->registeredShareProviders as $shareProvider) {
290
-			/** @var IShareProvider $instance */
291
-			$instance = $this->serverContainer->get($shareProvider);
292
-			$this->shareProviders[$instance->identifier()] = $instance;
293
-		}
294
-
295
-		if (isset($this->shareProviders[$id])) {
296
-			$provider = $this->shareProviders[$id];
297
-		}
298
-
299
-		if ($provider === null) {
300
-			throw new ProviderException('No provider with id .' . $id . ' found.');
301
-		}
302
-
303
-		return $provider;
304
-	}
305
-
306
-	/**
307
-	 * @inheritdoc
308
-	 */
309
-	public function getProviderForType($shareType) {
310
-		$provider = null;
311
-
312
-		if ($shareType === IShare::TYPE_USER ||
313
-			$shareType === IShare::TYPE_GROUP ||
314
-			$shareType === IShare::TYPE_LINK
315
-		) {
316
-			$provider = $this->defaultShareProvider();
317
-		} elseif ($shareType === IShare::TYPE_REMOTE || $shareType === IShare::TYPE_REMOTE_GROUP) {
318
-			$provider = $this->federatedShareProvider();
319
-		} elseif ($shareType === IShare::TYPE_EMAIL) {
320
-			$provider = $this->getShareByMailProvider();
321
-		} elseif ($shareType === IShare::TYPE_CIRCLE) {
322
-			$provider = $this->getShareByCircleProvider();
323
-		} elseif ($shareType === IShare::TYPE_ROOM) {
324
-			$provider = $this->getRoomShareProvider();
325
-		} elseif ($shareType === IShare::TYPE_DECK) {
326
-			$provider = $this->getProvider('deck');
327
-		}
328
-
329
-
330
-		if ($provider === null) {
331
-			throw new ProviderException('No share provider for share type ' . $shareType);
332
-		}
333
-
334
-		return $provider;
335
-	}
336
-
337
-	public function getAllProviders() {
338
-		$shares = [$this->defaultShareProvider(), $this->federatedShareProvider()];
339
-		$shareByMail = $this->getShareByMailProvider();
340
-		if ($shareByMail !== null) {
341
-			$shares[] = $shareByMail;
342
-		}
343
-		$shareByCircle = $this->getShareByCircleProvider();
344
-		if ($shareByCircle !== null) {
345
-			$shares[] = $shareByCircle;
346
-		}
347
-		$roomShare = $this->getRoomShareProvider();
348
-		if ($roomShare !== null) {
349
-			$shares[] = $roomShare;
350
-		}
351
-
352
-		foreach ($this->registeredShareProviders as $shareProvider) {
353
-			/** @var IShareProvider $instance */
354
-			$instance = $this->serverContainer->get($shareProvider);
355
-			if (!isset($this->shareProviders[$instance->identifier()])) {
356
-				$this->shareProviders[$instance->identifier()] = $instance;
357
-			}
358
-			$shares[] = $this->shareProviders[$instance->identifier()];
359
-		}
360
-
361
-
362
-
363
-		return $shares;
364
-	}
253
+            $appManager = $this->serverContainer->getAppManager();
254
+            if (!$appManager->isEnabledForUser('spreed')) {
255
+                return null;
256
+            }
257
+
258
+            try {
259
+                $this->roomShareProvider = $this->serverContainer->query('\OCA\Talk\Share\RoomShareProvider');
260
+            } catch (\OCP\AppFramework\QueryException $e) {
261
+                return null;
262
+            }
263
+        }
264
+
265
+        return $this->roomShareProvider;
266
+    }
267
+
268
+    /**
269
+     * @inheritdoc
270
+     */
271
+    public function getProvider($id) {
272
+        $provider = null;
273
+        if (isset($this->shareProviders[$id])) {
274
+            return $this->shareProviders[$id];
275
+        }
276
+
277
+        if ($id === 'ocinternal') {
278
+            $provider = $this->defaultShareProvider();
279
+        } elseif ($id === 'ocFederatedSharing') {
280
+            $provider = $this->federatedShareProvider();
281
+        } elseif ($id === 'ocMailShare') {
282
+            $provider = $this->getShareByMailProvider();
283
+        } elseif ($id === 'ocCircleShare') {
284
+            $provider = $this->getShareByCircleProvider();
285
+        } elseif ($id === 'ocRoomShare') {
286
+            $provider = $this->getRoomShareProvider();
287
+        }
288
+
289
+        foreach ($this->registeredShareProviders as $shareProvider) {
290
+            /** @var IShareProvider $instance */
291
+            $instance = $this->serverContainer->get($shareProvider);
292
+            $this->shareProviders[$instance->identifier()] = $instance;
293
+        }
294
+
295
+        if (isset($this->shareProviders[$id])) {
296
+            $provider = $this->shareProviders[$id];
297
+        }
298
+
299
+        if ($provider === null) {
300
+            throw new ProviderException('No provider with id .' . $id . ' found.');
301
+        }
302
+
303
+        return $provider;
304
+    }
305
+
306
+    /**
307
+     * @inheritdoc
308
+     */
309
+    public function getProviderForType($shareType) {
310
+        $provider = null;
311
+
312
+        if ($shareType === IShare::TYPE_USER ||
313
+            $shareType === IShare::TYPE_GROUP ||
314
+            $shareType === IShare::TYPE_LINK
315
+        ) {
316
+            $provider = $this->defaultShareProvider();
317
+        } elseif ($shareType === IShare::TYPE_REMOTE || $shareType === IShare::TYPE_REMOTE_GROUP) {
318
+            $provider = $this->federatedShareProvider();
319
+        } elseif ($shareType === IShare::TYPE_EMAIL) {
320
+            $provider = $this->getShareByMailProvider();
321
+        } elseif ($shareType === IShare::TYPE_CIRCLE) {
322
+            $provider = $this->getShareByCircleProvider();
323
+        } elseif ($shareType === IShare::TYPE_ROOM) {
324
+            $provider = $this->getRoomShareProvider();
325
+        } elseif ($shareType === IShare::TYPE_DECK) {
326
+            $provider = $this->getProvider('deck');
327
+        }
328
+
329
+
330
+        if ($provider === null) {
331
+            throw new ProviderException('No share provider for share type ' . $shareType);
332
+        }
333
+
334
+        return $provider;
335
+    }
336
+
337
+    public function getAllProviders() {
338
+        $shares = [$this->defaultShareProvider(), $this->federatedShareProvider()];
339
+        $shareByMail = $this->getShareByMailProvider();
340
+        if ($shareByMail !== null) {
341
+            $shares[] = $shareByMail;
342
+        }
343
+        $shareByCircle = $this->getShareByCircleProvider();
344
+        if ($shareByCircle !== null) {
345
+            $shares[] = $shareByCircle;
346
+        }
347
+        $roomShare = $this->getRoomShareProvider();
348
+        if ($roomShare !== null) {
349
+            $shares[] = $roomShare;
350
+        }
351
+
352
+        foreach ($this->registeredShareProviders as $shareProvider) {
353
+            /** @var IShareProvider $instance */
354
+            $instance = $this->serverContainer->get($shareProvider);
355
+            if (!isset($this->shareProviders[$instance->identifier()])) {
356
+                $this->shareProviders[$instance->identifier()] = $instance;
357
+            }
358
+            $shares[] = $this->shareProviders[$instance->identifier()];
359
+        }
360
+
361
+
362
+
363
+        return $shares;
364
+    }
365 365
 }
Please login to merge, or discard this patch.