1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
*
* @author Lukas Reschke <lukas@statuscode.ch>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Talk\Controller;
use OC\HintException;
use OCA\Talk\Exceptions\ParticipantNotFoundException;
use OCA\Talk\Exceptions\RoomNotFoundException;
use OCA\Talk\Config;
use OCA\Talk\Manager;
use OCA\Talk\Participant;
use OCA\Talk\Room;
use OCA\Talk\TalkSession;
use OCA\Talk\TInitialState;
use OCA\Viewer\Event\LoadViewer;
use OCP\App\IAppManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\Template\PublicTemplateResponse;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\IRootFolder;
use OCP\Files\NotPermittedException;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IInitialStateService;
use OCP\ILogger;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Notification\IManager as INotificationManager;
use OCP\Util;
class PageController extends Controller {
use TInitialState;
/** @var string|null */
private $userId;
/** @var IEventDispatcher */
private $eventDispatcher;
/** @var RoomController */
private $api;
/** @var TalkSession */
private $talkSession;
/** @var IUserSession */
private $userSession;
/** @var ILogger */
private $logger;
/** @var Manager */
private $manager;
/** @var IURLGenerator */
private $url;
/** @var INotificationManager */
private $notificationManager;
/** @var IAppManager */
private $appManager;
/** @var IRootFolder */
private $rootFolder;
public function __construct(string $appName,
IRequest $request,
IEventDispatcher $eventDispatcher,
RoomController $api,
TalkSession $session,
IUserSession $userSession,
?string $UserId,
ILogger $logger,
Manager $manager,
IURLGenerator $url,
INotificationManager $notificationManager,
IAppManager $appManager,
IInitialStateService $initialStateService,
ICacheFactory $memcacheFactory,
IRootFolder $rootFolder,
Config $talkConfig,
IConfig $serverConfig) {
parent::__construct($appName, $request);
$this->eventDispatcher = $eventDispatcher;
$this->api = $api;
$this->talkSession = $session;
$this->userSession = $userSession;
$this->userId = $UserId;
$this->logger = $logger;
$this->manager = $manager;
$this->url = $url;
$this->notificationManager = $notificationManager;
$this->appManager = $appManager;
$this->initialStateService = $initialStateService;
$this->memcacheFactory = $memcacheFactory;
$this->rootFolder = $rootFolder;
$this->talkConfig = $talkConfig;
$this->serverConfig = $serverConfig;
}
/**
* @PublicPage
* @NoCSRFRequired
* @UseSession
*
* @param string $token
* @return Response
* @throws HintException
*/
public function showCall(string $token): Response {
// This is the entry point from the `/call/{token}` URL which is hardcoded in the server.
return $this->index($token);
}
/**
* @PublicPage
* @NoCSRFRequired
* @UseSession
*
* @param string $token
* @param string $password
* @return Response
* @throws HintException
*/
public function authenticatePassword(string $token, string $password = ''): Response {
// This is the entry point from the `/call/{token}` URL which is hardcoded in the server.
return $this->index($token, '', $password);
}
/**
* @PublicPage
* @NoCSRFRequired
*
* @return Response
*/
public function notFound(): Response {
return new RedirectResponse($this->url->linkToRouteAbsolute('spreed.Page.index'));
}
/**
* @PublicPage
* @NoCSRFRequired
* @UseSession
*
* @param string $token
* @param string $callUser
* @param string $password
* @return TemplateResponse|RedirectResponse
* @throws HintException
*/
public function index(string $token = '', string $callUser = '', string $password = ''): Response {
$user = $this->userSession->getUser();
if (!$user instanceof IUser) {
return $this->guestEnterRoom($token, $password);
}
if ($token !== '') {
$room = null;
try {
$room = $this->manager->getRoomByToken($token);
$notification = $this->notificationManager->createNotification();
try {
$notification->setApp('spreed')
->setUser($this->userId)
->setObject('room', $room->getToken());
$this->notificationManager->markProcessed($notification);
$notification->setObject('call', $room->getToken());
$this->notificationManager->markProcessed($notification);
} catch (\InvalidArgumentException $e) {
$this->logger->logException($e, ['app' => 'spreed']);
}
// If the room is not a public room, check if the user is in the participants
if ($room->getType() !== Room::PUBLIC_CALL) {
$this->manager->getRoomForParticipant($room->getId(), $this->userId);
}
} catch (RoomNotFoundException $e) {
// Room not found, redirect to main page
$token = '';
}
if ($room instanceof Room && $room->hasPassword()) {
// If the user joined themselves or is not found, they need the password.
try {
$participant = $room->getParticipant($this->userId);
$requirePassword = $participant->getParticipantType() === Participant::USER_SELF_JOINED;
} catch (ParticipantNotFoundException $e) {
$requirePassword = true;
}
if ($requirePassword) {
$password = $password !== '' ? $password : (string) $this->talkSession->getPasswordForRoom($token);
$passwordVerification = $room->verifyPassword($password);
if ($passwordVerification['result']) {
$this->talkSession->setPasswordForRoom($token, $password);
} else {
$this->talkSession->removePasswordForRoom($token);
if ($passwordVerification['url'] === '') {
return new TemplateResponse($this->appName, 'authenticate', [
'wrongpw' => $password !== '',
], 'guest');
}
return new RedirectResponse($passwordVerification['url']);
}
}
}
} else {
$response = $this->api->createRoom(Room::ONE_TO_ONE_CALL, $callUser);
if ($response->getStatus() !== Http::STATUS_NOT_FOUND) {
$data = $response->getData();
return $this->redirectToConversation($data['token']);
}
}
$this->publishInitialStateForUser($user, $this->rootFolder, $this->appManager);
if (class_exists(LoadViewer::class)) {
$this->eventDispatcher->dispatchTyped(new LoadViewer());
}
$response = new TemplateResponse($this->appName, 'index');
$csp = new ContentSecurityPolicy();
$csp->addAllowedConnectDomain('*');
$csp->addAllowedMediaDomain('blob:');
$response->setContentSecurityPolicy($csp);
return $response;
}
/**
* @param string $token
* @param string $password
* @return TemplateResponse|RedirectResponse
* @throws HintException
*/
protected function guestEnterRoom(string $token, string $password): Response {
try {
$room = $this->manager->getRoomByToken($token);
if ($room->getType() !== Room::PUBLIC_CALL) {
throw new RoomNotFoundException();
}
} catch (RoomNotFoundException $e) {
$redirectUrl = $this->url->linkToRoute('spreed.Page.index');
if ($token) {
$redirectUrl = $this->url->linkToRoute('spreed.Page.showCall', ['token' => $token]);
}
return new RedirectResponse($this->url->linkToRoute('core.login.showLoginForm', [
'redirect_url' => $redirectUrl,
]));
}
if ($room->hasPassword()) {
$password = $password !== '' ? $password : (string) $this->talkSession->getPasswordForRoom($token);
$passwordVerification = $room->verifyPassword($password);
if ($passwordVerification['result']) {
$this->talkSession->setPasswordForRoom($token, $password);
} else {
$this->talkSession->removePasswordForRoom($token);
if ($passwordVerification['url'] === '') {
return new TemplateResponse($this->appName, 'authenticate', [
'wrongpw' => $password !== '',
], 'guest');
}
return new RedirectResponse($passwordVerification['url']);
}
}
$this->publishInitialStateForGuest();
$response = new PublicTemplateResponse($this->appName, 'index');
$response->setFooterVisible(false);
$csp = new ContentSecurityPolicy();
$csp->addAllowedConnectDomain('*');
$csp->addAllowedMediaDomain('blob:');
$response->setContentSecurityPolicy($csp);
return $response;
}
/**
* @PublicPage
* @NoCSRFRequired
*
* @param string $token
* @return RedirectResponse
*/
protected function redirectToConversation(string $token): RedirectResponse {
// These redirects are already done outside of this method
if ($this->userId === null) {
try {
$room = $this->manager->getRoomByToken($token);
if ($room->getType() !== Room::PUBLIC_CALL) {
throw new RoomNotFoundException();
}
return new RedirectResponse($this->url->linkToRoute('spreed.Page.showCall', ['token' => $token]));
} catch (RoomNotFoundException $e) {
return new RedirectResponse($this->url->linkToRoute('core.login.showLoginForm', [
'redirect_url' => $this->url->linkToRoute('spreed.Page.showCall', ['token' => $token]),
]));
}
}
return new RedirectResponse($this->url->linkToRoute('spreed.Page.showCall', ['token' => $token]));
}
}
|