-
-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathWebViewController.php
More file actions
217 lines (202 loc) Β· 8.23 KB
/
Copy pathWebViewController.php
File metadata and controls
217 lines (202 loc) Β· 8.23 KB
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
<?php
/*
* Copyright (c) 2020-2024. The Nextcloud Bookmarks contributors.
*
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
*/
namespace OCA\Bookmarks\Controller;
use OCA\Bookmarks\AugmentedTemplateResponse;
use OCA\Bookmarks\Db\BookmarkMapper;
use OCA\Bookmarks\Db\Folder;
use OCA\Bookmarks\Db\FolderMapper;
use OCA\Bookmarks\Db\PublicFolder;
use OCA\Bookmarks\Db\PublicFolderMapper;
use OCA\Bookmarks\Service\SettingsService;
use OCA\Bookmarks\Service\UserSettingsService;
use OCA\Viewer\Event\LoadViewer;
use OCP\App\IAppManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\AppFramework\Http\Attribute\BruteForceProtection;
use OCP\AppFramework\Http\Attribute\FrontpageRoute;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\Attribute\PublicPage;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\NotFoundResponse;
use OCP\AppFramework\Http\StreamResponse;
use OCP\AppFramework\Http\Template\PublicTemplateResponse;
use OCP\DB\Exception;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUserManager;
use Psr\Log\LoggerInterface;
class WebViewController extends Controller {
public function __construct(
$appName,
IRequest $request,
private ?string $userId,
private IL10N $l,
private PublicFolderMapper $publicFolderMapper,
private IUserManager $userManager,
private FolderMapper $folderMapper,
private IURLGenerator $urlGenerator,
private \OCP\IInitialStateService $initialState,
private \OCA\Bookmarks\Controller\InternalFoldersController $folderController,
private \OCA\Bookmarks\Controller\InternalBookmarkController $bookmarkController,
private \OCA\Bookmarks\Controller\InternalTagsController $tagsController,
private BookmarkMapper $bookmarkMapper,
private UserSettingsService $userSettingsService,
private SettingsService $settings,
private IAppManager $appManager,
private IConfig $config,
private IEventDispatcher $eventDispatcher,
private LoggerInterface $logger,
) {
parent::__construct($appName, $request);
}
#[NoAdminRequired]
#[NoCSRFRequired]
#[FrontpageRoute(verb: 'GET', url: '/')]
#[FrontpageRoute(verb: 'GET', url: '/recent', postfix: 'recent')]
#[FrontpageRoute(verb: 'GET', url: '/frequent', postfix: 'frequent')]
#[FrontpageRoute(verb: 'GET', url: '/folders/{folder}/search/{search}', postfix: 'search')]
#[FrontpageRoute(verb: 'GET', url: '/folders/{folder}', postfix: 'folder')]
#[FrontpageRoute(verb: 'GET', url: '/bookmarks/{bookmark}', postfix: 'bookmark')]
#[FrontpageRoute(verb: 'GET', url: '/tags/{tags}', postfix: 'tags')]
#[FrontpageRoute(verb: 'GET', url: '/untagged', postfix: 'untagged')]
#[FrontpageRoute(verb: 'GET', url: '/unavailable', postfix: 'unavailable')]
#[FrontpageRoute(verb: 'GET', url: '/archived', postfix: 'archived')]
#[FrontpageRoute(verb: 'GET', url: '/duplicated', postfix: 'duplicated')]
#[FrontpageRoute(verb: 'GET', url: '/bookmarklet', postfix: 'bookmarklet')]
#[FrontpageRoute(verb: 'GET', url: '/trashbin', postfix: 'trashbin')]
public function index(): AugmentedTemplateResponse {
if (class_exists(LoadViewer::class)) {
$this->eventDispatcher->dispatchTyped(new LoadViewer());
}
$res = new AugmentedTemplateResponse($this->appName, 'main', ['url' => $this->urlGenerator]);
$policy = new ContentSecurityPolicy();
$policy->addAllowedWorkerSrcDomain("'self'");
$policy->addAllowedScriptDomain("'self'");
$policy->addAllowedConnectDomain("'self'");
$policy->addAllowedFrameDomain("'self'");
$res->setContentSecurityPolicy($policy);
$this->initialState->provideInitialState($this->appName, 'folders', $this->folderController->getFolders()->getData()['data']);
$this->initialState->provideInitialState($this->appName, 'allCount', $this->bookmarkController->countBookmarks(-1)->getData()['item']);
try {
$this->initialState->provideInitialState($this->appName, 'archivedCount', $this->bookmarkMapper->countArchived($this->userId));
$this->initialState->provideInitialState($this->appName, 'deletedCount', $this->bookmarkMapper->countDeleted($this->userId));
$this->initialState->provideInitialState($this->appName, 'duplicatedCount', $this->bookmarkMapper->countDuplicated($this->userId));
$this->initialState->provideInitialState($this->appName, 'unavailableCount', $this->bookmarkMapper->countUnavailable($this->userId));
$this->initialState->provideInitialState($this->appName, 'allClicksCount', $this->bookmarkMapper->countAllClicks($this->userId));
$this->initialState->provideInitialState($this->appName, 'withClicksCount', $this->bookmarkMapper->countWithClicks($this->userId));
} catch (Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
}
$this->initialState->provideInitialState($this->appName, 'tags', $this->tagsController->fullTags(true)->getData());
$this->initialState->provideInitialState($this->appName, 'contextChatInstalled', $this->appManager->isEnabledForUser('context_chat'));
$this->initialState->provideInitialState($this->appName, 'appStoreEnabled', $this->config->getSystemValueBool('appstoreenabled', true));
$settings = $this->userSettingsService->toArray();
$settings['shareapi_allow_links'] = $this->settings->getLinkSharingAllowed();
$this->initialState->provideInitialState($this->appName, 'settings', $settings);
return $res;
}
/**
* @param string $token
*
* @return NotFoundResponse|PublicTemplateResponse
*
* @NoAdminRequired
* @NoCSRFRequired
* @BruteForceProtection
* @PublicPage
*/
#[PublicPage]
#[NoCSRFRequired]
#[BruteForceProtection('link')]
#[NoAdminRequired]
#[FrontpageRoute(verb: 'GET', url: '/public/{token}')]
public function link(string $token) {
$title = 'No title found';
$userName = 'Unknown';
try {
/**
* @var PublicFolder $publicFolder
*/
$publicFolder = $this->publicFolderMapper->find($token);
/**
* @var Folder $folder
*/
$folder = $this->folderMapper->find($publicFolder->getFolderId());
$title = $folder->getTitle();
$user = $this->userManager->get($folder->getUserId());
if ($user !== null) {
$userName = $user->getDisplayName();
}
} catch (DoesNotExistException|MultipleObjectsReturnedException $e) {
$res = new NotFoundResponse();
$res->throttle();
return $res;
}
$res = new PublicTemplateResponse($this->appName, 'main', []);
$res->setHeaderTitle($title);
$res->setHeaderDetails($this->l->t('Bookmarks shared by %s', [$userName]));
$res->setFooterVisible(false);
return $res;
}
/**
* @return StreamResponse
*/
#[FrontpageRoute(verb: 'GET', url: '/service-worker.js')]
#[NoCSRFRequired]
#[NoAdminRequired]
public function serviceWorker(): StreamResponse {
$response = new StreamResponse(__DIR__ . '/../../js/bookmarks-service-worker.js');
$response->setHeaders(['Content-Type' => 'application/javascript']);
$policy = new ContentSecurityPolicy();
$policy->addAllowedWorkerSrcDomain("'self'");
$policy->addAllowedScriptDomain("'self'");
$policy->addAllowedConnectDomain("'self'");
$response->setContentSecurityPolicy($policy);
return $response;
}
/**
* @return JSONResponse
*/
#[FrontpageRoute(verb: 'GET', url: '/manifest.webmanifest')]
#[NoCSRFRequired]
#[NoAdminRequired]
#[PublicPage]
public function manifest(): JSONResponse {
$responseJS = [
'name' => $this->l->t('Bookmarks'),
'short_name' => $this->l->t('Bookmarks'),
'start_url' => $this->urlGenerator->linkToRouteAbsolute('bookmarks.webview.index'),
'icons'
=> [
[
'src' => $this->urlGenerator->linkToRoute('theming.Icon.getTouchIcon',
['app' => 'bookmarks']),
'type' => 'image/png',
'sizes' => '512x512'
],
[
'src' => $this->urlGenerator->linkToRoute('theming.Icon.getFavicon',
['app' => 'bookmark']),
'type' => 'image/svg+xml',
'sizes' => '128x128'
]
],
'display' => 'standalone'
];
$response = new JSONResponse($responseJS);
$response->setHeaders(['Content-Type' => 'application/manifest+json']);
//$response->cacheFor(3600);
return $response;
}
}