1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Björn Schießle <[email protected]> |
4
|
|
|
* @author Joas Schilling <[email protected]> |
5
|
|
|
* @author Lukas Reschke <[email protected]> |
6
|
|
|
* @author Thomas Müller <[email protected]> |
7
|
|
|
* @author Vincent Petry <[email protected]> |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) 2018, ownCloud GmbH |
10
|
|
|
* @license AGPL-3.0 |
11
|
|
|
* |
12
|
|
|
* This code is free software: you can redistribute it and/or modify |
13
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
14
|
|
|
* as published by the Free Software Foundation. |
15
|
|
|
* |
16
|
|
|
* This program is distributed in the hope that it will be useful, |
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19
|
|
|
* GNU Affero General Public License for more details. |
20
|
|
|
* |
21
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
22
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
23
|
|
|
* |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace OCA\FederatedFileSharing; |
27
|
|
|
|
28
|
|
|
use OCA\FederatedFileSharing\Ocm\NotificationManager; |
29
|
|
|
use OCP\AppFramework\Http; |
30
|
|
|
use OCP\BackgroundJob\IJobList; |
31
|
|
|
use OCP\Http\Client\IClientService; |
32
|
|
|
use OCP\IConfig; |
33
|
|
|
|
34
|
|
|
class Notifications { |
35
|
|
|
const RESPONSE_FORMAT = 'json'; // default response format for ocs calls |
36
|
|
|
|
37
|
|
|
/** @var AddressHandler */ |
38
|
|
|
private $addressHandler; |
39
|
|
|
|
40
|
|
|
/** @var IClientService */ |
41
|
|
|
private $httpClientService; |
42
|
|
|
|
43
|
|
|
/** @var DiscoveryManager */ |
44
|
|
|
private $discoveryManager; |
45
|
1 |
|
|
46
|
|
|
/** @var IJobList */ |
47
|
|
|
private $jobList; |
48
|
|
|
|
49
|
1 |
|
/** @var IConfig */ |
50
|
1 |
|
private $config; |
51
|
1 |
|
|
52
|
|
|
/** |
53
|
|
|
* @param AddressHandler $addressHandler |
54
|
|
|
* @param IClientService $httpClientService |
55
|
|
|
* @param DiscoveryManager $discoveryManager |
56
|
|
|
* @param IJobList $jobList |
57
|
|
|
* @param IConfig $config |
58
|
|
|
*/ |
59
|
|
|
public function __construct( |
60
|
|
|
AddressHandler $addressHandler, |
61
|
|
|
IClientService $httpClientService, |
62
|
|
|
DiscoveryManager $discoveryManager, |
63
|
|
|
IJobList $jobList, |
64
|
|
|
IConfig $config |
65
|
|
|
) { |
66
|
|
|
$this->addressHandler = $addressHandler; |
67
|
|
|
$this->httpClientService = $httpClientService; |
68
|
|
|
$this->discoveryManager = $discoveryManager; |
69
|
|
|
$this->jobList = $jobList; |
70
|
|
|
$this->config = $config; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* send server-to-server share to remote server |
75
|
|
|
* |
76
|
|
|
* @param Address $shareWithAddress |
77
|
|
|
* @param Address $ownerAddress |
78
|
|
|
* @param Address $sharedByAddress |
79
|
|
|
* @param string $token |
80
|
|
|
* @param string $shareWith |
|
|
|
|
81
|
|
|
* @param string $name |
82
|
|
|
* @param int $remote_id |
83
|
|
|
* |
84
|
|
|
* @return bool |
85
|
|
|
* |
86
|
|
|
* @throws \OC\HintException |
87
|
|
|
* @throws \OC\ServerNotAvailableException |
88
|
|
|
*/ |
89
|
|
|
public function sendRemoteShare(Address $shareWithAddress, |
90
|
|
|
Address $ownerAddress, |
91
|
|
|
Address $sharedByAddress, |
92
|
|
|
$token, |
93
|
|
|
$name, |
94
|
|
|
$remote_id |
95
|
|
|
) { |
96
|
|
|
$remoteShareSuccess = false; |
97
|
|
|
if ($shareWithAddress->getUserId() && $shareWithAddress->getCloudId()) { |
98
|
|
|
$remoteShareSuccess = $this->sendOcmRemoteShare( |
99
|
|
|
$shareWithAddress, $ownerAddress, $sharedByAddress, $token, $name, $remote_id |
100
|
|
|
); |
101
|
|
|
if (!$remoteShareSuccess) { |
102
|
|
|
$remoteShareSuccess = $this->sendPreOcmRemoteShare( |
103
|
|
|
$shareWithAddress, $ownerAddress, $sharedByAddress, $token, $name, $remote_id |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
if ($remoteShareSuccess) { |
108
|
|
|
\OC_Hook::emit( |
109
|
|
|
'OCP\Share', |
110
|
|
|
'federated_share_added', |
111
|
|
|
['server' => $shareWithAddress->getHostName()] |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
return $remoteShareSuccess; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* ask owner to re-share the file with the given user |
119
|
|
|
* |
120
|
|
|
* @param string $token |
121
|
|
|
* @param int $id remote Id |
122
|
|
|
* @param int $shareId internal share Id |
123
|
|
|
* @param string $remote remote address of the owner |
124
|
|
|
* @param string $shareWith |
125
|
|
|
* @param int $permission |
126
|
|
|
* @return bool |
127
|
|
|
* @throws \Exception |
128
|
|
|
*/ |
129
|
|
|
public function requestReShare($token, $id, $shareId, $remote, $shareWith, $permission) { |
130
|
|
|
$fields = [ |
131
|
|
|
'shareWith' => $shareWith, |
132
|
|
|
'token' => $token, |
133
|
|
|
'permission' => $permission, |
134
|
|
|
'remoteId' => $shareId |
135
|
|
|
]; |
136
|
|
|
|
137
|
|
|
$url = $this->addressHandler->removeProtocolFromUrl($remote); |
138
|
|
|
$result = $this->tryHttpPostToShareEndpoint(\rtrim($url, '/'), '/' . $id . '/reshare', $fields); |
139
|
|
|
$status = \json_decode($result['result'], true); |
140
|
|
|
|
141
|
|
|
$httpRequestSuccessful = $result['success']; |
142
|
|
|
$validToken = isset($status['ocs']['data']['token']) && \is_string($status['ocs']['data']['token']); |
143
|
|
|
$validRemoteId = isset($status['ocs']['data']['remoteId']); |
144
|
|
|
|
145
|
|
|
if ($httpRequestSuccessful && $this->isOcsStatusOk($status) && $validToken && $validRemoteId) { |
146
|
|
|
return [ |
147
|
|
|
$status['ocs']['data']['token'], |
148
|
|
|
(int)$status['ocs']['data']['remoteId'] |
149
|
|
|
]; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return false; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* send server-to-server unshare to remote server |
157
|
|
|
* |
158
|
|
|
* @param string $remote url |
159
|
|
|
* @param int $id share id |
160
|
|
|
* @param string $token |
161
|
|
|
* @return bool |
162
|
|
|
*/ |
163
|
|
|
public function sendRemoteUnShare($remote, $id, $token) { |
164
|
|
|
return $this->sendUpdateToRemote($remote, $id, $token, 'unshare'); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* send server-to-server unshare to remote server |
169
|
|
|
* |
170
|
|
|
* @param string $remote url |
171
|
|
|
* @param int $id share id |
172
|
|
|
* @param string $token |
173
|
|
|
* @return bool |
174
|
|
|
*/ |
175
|
|
|
public function sendRevokeShare($remote, $id, $token) { |
176
|
|
|
return $this->sendUpdateToRemote($remote, $id, $token, 'revoke'); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* send notification to remote server if the permissions was changed |
181
|
|
|
* |
182
|
|
|
* @param string $remote |
183
|
|
|
* @param int $remoteId |
184
|
|
|
* @param string $token |
185
|
|
|
* @param int $permissions |
186
|
|
|
* @return bool |
187
|
|
|
*/ |
188
|
|
|
public function sendPermissionChange($remote, $remoteId, $token, $permissions) { |
189
|
|
|
return $this->sendUpdateToRemote($remote, $remoteId, $token, 'permissions', ['permissions' => $permissions]); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* forward accept reShare to remote server |
194
|
|
|
* |
195
|
|
|
* @param string $remote |
196
|
|
|
* @param int $remoteId |
197
|
|
|
* @param string $token |
198
|
|
|
*/ |
199
|
|
|
public function sendAcceptShare($remote, $remoteId, $token) { |
200
|
|
|
$this->sendUpdateToRemote($remote, $remoteId, $token, 'accept'); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* forward decline reShare to remote server |
205
|
|
|
* |
206
|
|
|
* @param string $remote |
207
|
|
|
* @param int $remoteId |
208
|
|
|
* @param string $token |
209
|
|
|
*/ |
210
|
|
|
public function sendDeclineShare($remote, $remoteId, $token) { |
211
|
|
|
$this->sendUpdateToRemote($remote, $remoteId, $token, 'decline'); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* inform remote server whether server-to-server share was accepted/declined |
216
|
|
|
* |
217
|
|
|
* @param string $remote |
218
|
|
|
* @param int $remoteId Share id on the remote host |
219
|
|
|
* @param string $token |
220
|
|
|
* @param string $action possible actions: |
221
|
|
|
* accept, decline, unshare, revoke, permissions |
222
|
|
|
* @param array $data |
223
|
|
|
* @param int $try |
224
|
|
|
* |
225
|
|
|
* @return boolean |
226
|
|
|
* |
227
|
|
|
* @throws \Exception |
228
|
|
|
*/ |
229
|
|
|
public function sendUpdateToRemote($remote, $remoteId, $token, $action, $data = [], $try = 0) { |
230
|
|
|
$ocmNotificationManager = new NotificationManager(); |
231
|
|
|
$ocmNotification = $ocmNotificationManager->convertToOcmFileNotification($remoteId, $token, $action, $data); |
232
|
|
|
$ocmFields = $ocmNotification->toArray(); |
233
|
|
|
$url = \rtrim( |
234
|
|
|
$this->addressHandler->removeProtocolFromUrl($remote), |
235
|
|
|
'/' |
236
|
|
|
); |
237
|
|
|
if ($this->tryHttpPostToShareEndpoint($url, '/notifications', $ocmFields, true)) { |
238
|
|
|
return true; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
$fields = ['token' => $token]; |
242
|
|
|
foreach ($data as $key => $value) { |
243
|
|
|
$fields[$key] = $value; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
$url = \rtrim( |
247
|
|
|
$this->addressHandler->removeProtocolFromUrl($remote), |
248
|
|
|
'/' |
249
|
|
|
); |
250
|
|
|
$result = $this->tryHttpPostToShareEndpoint($url, '/' . $remoteId . '/' . $action, $fields); |
251
|
|
|
$status = \json_decode($result['result'], true); |
252
|
|
|
|
253
|
|
|
if ($result['success'] && $this->isOcsStatusOk($status)) { |
254
|
|
|
return true; |
255
|
|
|
} elseif ($try === 0) { |
256
|
|
|
// only add new job on first try |
257
|
|
|
$this->jobList->add('OCA\FederatedFileSharing\BackgroundJob\RetryJob', |
258
|
|
|
[ |
259
|
|
|
'remote' => $remote, |
260
|
|
|
'remoteId' => $remoteId, |
261
|
|
|
'token' => $token, |
262
|
|
|
'action' => $action, |
263
|
|
|
'data' => \json_encode($data), |
264
|
|
|
'try' => $try, |
265
|
|
|
'lastRun' => $this->getTimestamp() |
266
|
|
|
] |
267
|
|
|
); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
return false; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* return current timestamp |
275
|
|
|
* |
276
|
|
|
* @return int |
277
|
|
|
*/ |
278
|
|
|
protected function getTimestamp() { |
279
|
|
|
return \time(); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* try http post first with https and then with http as a fallback |
284
|
|
|
* |
285
|
|
|
* @param string $remoteDomain |
286
|
|
|
* @param string $urlSuffix |
287
|
|
|
* @param array $fields post parameters |
288
|
|
|
* @return array |
289
|
|
|
* @throws \Exception |
290
|
|
|
*/ |
291
|
|
|
protected function tryHttpPostToShareEndpoint($remoteDomain, $urlSuffix, array $fields, $useOcm = false) { |
292
|
|
|
$client = $this->httpClientService->newClient(); |
293
|
|
|
$protocol = 'https://'; |
294
|
|
|
$result = [ |
295
|
|
|
'success' => false, |
296
|
|
|
'result' => '', |
297
|
|
|
]; |
298
|
|
|
$try = 0; |
299
|
|
|
|
300
|
|
|
while ($result['success'] === false && $try < 2) { |
301
|
|
|
if ($useOcm) { |
302
|
|
|
$endpoint = $this->discoveryManager->getOcmShareEndpoint($protocol . $remoteDomain); |
303
|
|
|
$endpoint .= $urlSuffix; |
304
|
|
|
} else { |
305
|
|
|
$relativePath = $this->discoveryManager->getShareEndpoint($protocol . $remoteDomain); |
306
|
|
|
$endpoint = $protocol . $remoteDomain . $relativePath . $urlSuffix . '?format=' . self::RESPONSE_FORMAT; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
try { |
310
|
|
|
$response = $client->post($endpoint, [ |
311
|
|
|
'body' => $fields, |
312
|
|
|
'timeout' => 10, |
313
|
|
|
'connect_timeout' => 10, |
314
|
|
|
]); |
315
|
|
|
$result['result'] = $response->getBody(); |
316
|
|
|
$result['statusCode'] = $response->getStatusCode(); |
317
|
|
|
$result['success'] = true; |
318
|
|
|
break; |
319
|
|
|
} catch (\Exception $e) { |
320
|
|
|
// if flat re-sharing is not supported by the remote server |
321
|
|
|
// we re-throw the exception and fall back to the old behaviour. |
322
|
|
|
// (flat re-shares has been introduced in ownCloud 9.1) |
323
|
|
|
if ($e->getCode() === Http::STATUS_INTERNAL_SERVER_ERROR) { |
324
|
|
|
throw $e; |
325
|
|
|
} |
326
|
|
|
$allowHttpFallback = $this->config->getSystemValue('sharing.federation.allowHttpFallback', false) === true; |
327
|
|
|
if (!$allowHttpFallback) { |
328
|
|
|
break; |
329
|
|
|
} |
330
|
|
|
$try++; |
331
|
|
|
$protocol = 'http://'; |
332
|
|
|
} |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
return $result; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
protected function sendOcmRemoteShare(Address $shareWithAddress, Address $ownerAddress, Address $sharedByAddress, $token, $name, $remote_id) { |
339
|
|
|
$fields = [ |
340
|
|
|
'shareWith' => $shareWithAddress->getCloudId(), |
341
|
|
|
'name' => $name, |
342
|
|
|
'providerId' => $remote_id, |
343
|
|
|
'owner' => $ownerAddress->getCloudId(), |
344
|
|
|
'ownerDisplayName' => $ownerAddress->getDisplayName(), |
345
|
|
|
'sender' => $sharedByAddress->getCloudId(), |
346
|
|
|
'senderDisplayName' => $sharedByAddress->getDisplayName(), |
347
|
|
|
'shareType' => 'user', |
348
|
|
|
'resourceType' => 'file', |
349
|
|
|
'protocol' => [ |
350
|
|
|
'name' => 'webdav', |
351
|
|
|
'options' => [ |
352
|
|
|
'sharedSecret' => $token |
353
|
|
|
] |
354
|
|
|
] |
355
|
|
|
]; |
356
|
|
|
|
357
|
|
|
$url = $shareWithAddress->getCleanHostName(); |
358
|
|
|
$result = $this->tryHttpPostToShareEndpoint($url, '/shares', $fields, true); |
359
|
|
|
|
360
|
|
|
if (isset($result['statusCode']) && $result['statusCode'] === 201) { |
361
|
|
|
return true; |
362
|
|
|
} |
363
|
|
|
return false; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
protected function sendPreOcmRemoteShare(Address $shareWithAddress, Address $ownerAddress, Address $sharedByAddress, $token, $name, $remote_id) { |
367
|
|
|
$fields = [ |
368
|
|
|
'shareWith' => $shareWithAddress->getUserId(), |
369
|
|
|
'token' => $token, |
370
|
|
|
'name' => $name, |
371
|
|
|
'remoteId' => $remote_id, |
372
|
|
|
'owner' => $ownerAddress->getUserId(), |
373
|
|
|
'ownerFederatedId' => $ownerAddress->getCloudId(), |
374
|
|
|
'sharedBy' => $sharedByAddress->getUserId(), |
375
|
|
|
'sharedByFederatedId' => $sharedByAddress->getUserId(), |
376
|
|
|
'remote' => $this->addressHandler->generateRemoteURL(), |
377
|
|
|
]; |
378
|
|
|
$url = $shareWithAddress->getCleanHostName(); |
379
|
|
|
$result = $this->tryHttpPostToShareEndpoint($url, '', $fields); |
380
|
|
|
$status = \json_decode($result['result'], true); |
381
|
|
|
|
382
|
|
|
if ($result['success'] && $this->isOcsStatusOk($status)) { |
383
|
|
|
return true; |
384
|
|
|
} |
385
|
|
|
return false; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* Validate ocs response - 100 or 200 means success |
390
|
|
|
* |
391
|
|
|
* @param array $status |
392
|
|
|
* |
393
|
|
|
* @return bool |
394
|
|
|
*/ |
395
|
|
|
private function isOcsStatusOk($status) { |
396
|
|
|
return \in_array($status['ocs']['meta']['statuscode'], [100, 200]); |
397
|
|
|
} |
398
|
|
|
} |
399
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.