1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2017 Joas Schilling <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @license GNU AGPL version 3 or any later version |
6
|
|
|
* |
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU Affero General Public License as |
9
|
|
|
* published by the Free Software Foundation, either version 3 of the |
10
|
|
|
* License, or (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace OCA\Activity\Controller; |
23
|
|
|
|
24
|
|
|
use OCA\Activity\Extension\Files; |
25
|
|
|
use OCP\App\IAppManager; |
26
|
|
|
use OCP\AppFramework\Http; |
27
|
|
|
use OCP\AppFramework\Http\DataResponse; |
28
|
|
|
use OCP\AppFramework\OCSController; |
29
|
|
|
use OCP\Files\InvalidPathException; |
30
|
|
|
use OCP\Files\IRootFolder; |
31
|
|
|
use OCP\Files\NotFoundException; |
32
|
|
|
use OCP\IDBConnection; |
33
|
|
|
use OCP\IRequest; |
34
|
|
|
use OCP\Activity\IManager as IActivityManager; |
35
|
|
|
use OCP\IUser; |
36
|
|
|
use OCP\IUserManager; |
37
|
|
|
|
38
|
|
|
class RemoteActivity extends OCSController { |
39
|
|
|
|
40
|
|
|
/** @var IDBConnection */ |
41
|
|
|
protected $db; |
42
|
|
|
|
43
|
|
|
/** @var IUserManager */ |
44
|
|
|
protected $userManager; |
45
|
|
|
|
46
|
|
|
/** @var IAppManager */ |
47
|
|
|
protected $appManager; |
48
|
|
|
|
49
|
|
|
/** @var IRootFolder */ |
50
|
|
|
protected $rootFolder; |
51
|
|
|
|
52
|
|
|
/** @var IActivityManager */ |
53
|
|
|
protected $activityManager; |
54
|
|
|
|
55
|
|
|
public function __construct($appName, |
56
|
|
|
IRequest $request, |
57
|
|
|
IDBConnection $db, |
58
|
|
|
IUserManager $userManager, |
59
|
|
|
IAppManager $appManager, |
60
|
|
|
IRootFolder $rootFolder, |
61
|
|
|
IActivityManager $activityManager) { |
62
|
|
|
parent::__construct($appName, $request); |
63
|
|
|
$this->db = $db; |
64
|
|
|
$this->userManager = $userManager; |
65
|
|
|
$this->appManager = $appManager; |
66
|
|
|
$this->rootFolder = $rootFolder; |
67
|
|
|
$this->activityManager = $activityManager; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @PublicPage |
72
|
|
|
* @NoCSRFRequired |
73
|
|
|
* |
74
|
|
|
* @param string $token |
75
|
|
|
* @param string[] $to |
76
|
|
|
* @param string[] $actor |
77
|
|
|
* @param string $type |
78
|
|
|
* @param string $updated |
79
|
|
|
* @param string[] $object |
80
|
|
|
* @param string[] $target |
81
|
|
|
* @param string[] $origin |
82
|
|
|
* @return DataResponse |
83
|
|
|
*/ |
84
|
|
|
public function receiveActivity($token, array $to, array $actor, $type, $updated, array $object = [], array $target = [], array $origin = []) { |
85
|
|
|
|
86
|
|
|
$date = \DateTime::createFromFormat(\DateTime::W3C, $updated); |
87
|
|
|
if ($date === false) { |
88
|
|
|
return new DataResponse([], Http::STATUS_BAD_REQUEST); |
89
|
|
|
} |
90
|
|
|
$time = $date->getTimestamp(); |
91
|
|
|
|
92
|
|
|
\OC::$server->getLogger()->warning(json_encode(func_get_args())); |
93
|
|
View Code Duplication |
if (!isset($to['type'], $to['name']) || $to['type'] !== 'Person') { |
|
|
|
|
94
|
|
|
return new DataResponse([], Http::STATUS_BAD_REQUEST); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$user = $this->userManager->get($to['name']); |
98
|
|
|
if (!$user instanceof IUser) { |
|
|
|
|
99
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
View Code Duplication |
if (!isset($actor['type'], $actor['name']) || $actor['type'] !== 'Person') { |
|
|
|
|
103
|
|
|
return new DataResponse([], Http::STATUS_BAD_REQUEST); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if ($user->getCloudId() === $actor['name']) { |
107
|
|
|
return new DataResponse([], Http::STATUS_BAD_REQUEST); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if (!$this->appManager->isInstalled('federatedfilesharing')) { |
111
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$query = $this->db->getQueryBuilder(); |
115
|
|
|
$query->select('*') |
116
|
|
|
->from('share_external') |
117
|
|
|
->where($query->expr()->eq('share_token', $query->createNamedParameter($token))) |
118
|
|
|
->andWhere($query->expr()->eq('user', $query->createNamedParameter($user->getUID()))); |
119
|
|
|
|
120
|
|
|
$result = $query->execute(); |
121
|
|
|
$share = $result->fetch(); |
122
|
|
|
$result->closeCursor(); |
123
|
|
|
|
124
|
|
|
if (!is_array($share) || strpos($share['mountpoint'], '{{TemporaryMountPointName#') === 0) { |
125
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$internalType = $this->translateType($type); |
129
|
|
|
if ($internalType === '') { |
130
|
|
|
return new DataResponse([], Http::STATUS_BAD_REQUEST); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$path2 = null; |
134
|
|
|
if ($type === 'Move') { |
135
|
|
View Code Duplication |
if (!isset($target['type'], $target['name']) || $target['type'] !== 'Document') { |
|
|
|
|
136
|
|
|
return new DataResponse([], Http::STATUS_BAD_REQUEST); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
View Code Duplication |
if (!isset($origin['type'], $origin['name']) || $origin['type'] !== 'Document') { |
|
|
|
|
140
|
|
|
return new DataResponse([], Http::STATUS_BAD_REQUEST); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$path = $share['mountpoint'] . $target['name']; |
144
|
|
|
$path2 = $share['mountpoint'] . $origin['name']; |
145
|
|
|
} else { |
146
|
|
View Code Duplication |
if (!isset($object['type'], $object['name']) || $object['type'] !== 'Document') { |
|
|
|
|
147
|
|
|
return new DataResponse([], Http::STATUS_BAD_REQUEST); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$path = $share['mountpoint'] . $object['name']; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$subject = $this->getSubject($type, $path, $path2); |
154
|
|
|
if ($subject === '') { |
155
|
|
|
return new DataResponse([], Http::STATUS_BAD_REQUEST); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$userFolder = $this->rootFolder->getUserFolder($user->getUID()); |
159
|
|
|
try { |
160
|
|
|
$node = $userFolder->get($path); |
161
|
|
|
$fileId = $node->getId(); |
162
|
|
|
} catch (NotFoundException $e) { |
|
|
|
|
163
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND); |
164
|
|
|
} catch (InvalidPathException $e) { |
|
|
|
|
165
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if ($path2 !== null) { |
169
|
|
|
$secondPath = [$fileId => $path2]; |
170
|
|
|
if ($subject === 'moved_by') { |
171
|
|
|
try { |
172
|
|
|
$parent = $node->getParent(); |
173
|
|
|
$secondPath = [$parent->getId() => dirname($path2)]; |
174
|
|
|
} catch (NotFoundException $e) { |
|
|
|
|
175
|
|
|
} catch (InvalidPathException $e) { |
|
|
|
|
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
$subjectParams = [$secondPath, $actor['name'], [$fileId => $path]]; |
179
|
|
|
} else { |
180
|
|
|
$subjectParams = [[$fileId => $path], $actor['name']]; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$event = $this->activityManager->generateEvent(); |
184
|
|
|
try { |
185
|
|
|
$event->setAffectedUser($user->getUID()) |
186
|
|
|
->setApp('files') |
187
|
|
|
->setType($internalType) |
188
|
|
|
->setAuthor($actor['name']) |
189
|
|
|
->setObject('files', $fileId, $path) |
190
|
|
|
->setSubject($subject, $subjectParams) |
191
|
|
|
->setTimestamp($time); |
192
|
|
|
$this->activityManager->publish($event); |
193
|
|
|
} catch (\InvalidArgumentException $e) { |
194
|
|
|
return new DataResponse(['activity'], Http::STATUS_BAD_REQUEST); |
195
|
|
|
} catch (\BadMethodCallException $e) { |
196
|
|
|
return new DataResponse(['sending'], Http::STATUS_BAD_REQUEST); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return new DataResponse(); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
protected function getSubject($type, $path, $path2) { |
203
|
|
|
switch ($type) { |
204
|
|
|
case 'Create': |
205
|
|
|
return 'created_by'; |
206
|
|
|
case 'Move': |
207
|
|
|
if ($path2 === null) { |
208
|
|
|
return ''; |
209
|
|
|
} |
210
|
|
|
if (basename($path) === basename($path2)) { |
211
|
|
|
return 'moved_by'; |
212
|
|
|
} |
213
|
|
|
return 'renamed_by'; |
214
|
|
|
case 'Update': |
215
|
|
|
return 'changed_by'; |
216
|
|
|
case 'Delete': |
217
|
|
|
return 'deleted_by'; |
218
|
|
|
} |
219
|
|
|
return ''; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param string $type |
224
|
|
|
* @return string |
225
|
|
|
*/ |
226
|
|
|
protected function translateType($type) { |
227
|
|
|
switch ($type) { |
228
|
|
|
case 'Create': |
229
|
|
|
return Files::TYPE_SHARE_CREATED; |
230
|
|
|
case 'Move': |
231
|
|
|
case 'Update': |
232
|
|
|
return Files::TYPE_SHARE_CHANGED; |
233
|
|
|
case 'Delete': |
234
|
|
|
return Files::TYPE_SHARE_DELETED; |
235
|
|
|
} |
236
|
|
|
return ''; |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.