-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathAppConfig.php
More file actions
338 lines (290 loc) Β· 10.8 KB
/
Copy pathAppConfig.php
File metadata and controls
338 lines (290 loc) Β· 10.8 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
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
328
329
330
331
332
333
334
335
336
337
338
<?php
/**
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Richdocuments;
use OCA\Richdocuments\AppInfo\Application;
use OCA\Richdocuments\Service\FederationService;
use OCP\App\IAppManager;
use OCP\AppFramework\Services\IAppConfig;
use OCP\GlobalScale\IConfig as GlobalScaleConfig;
use OCP\IConfig;
use OCP\IURLGenerator;
class AppConfig {
public const SERVER_MODE = 'server_mode';
// URL that Nextcloud will use to connect to Collabora
public const WOPI_URL = 'wopi_url';
// URL that the browser will use to connect to Collabora (inherited from the discovery endpoint of Collabora,
// either wopi_url or what is configured as server_name)
public const PUBLIC_WOPI_URL = 'public_wopi_url';
// URL that should be used by Collabora to connect back to Nextcloud (defaults to the users browser host)
public const WOPI_CALLBACK_URL = 'wopi_callback_url';
public const FEDERATION_USE_TRUSTED_DOMAINS = 'federation_use_trusted_domains';
public const SYSTEM_GS_TRUSTED_HOSTS = 'gs.trustedHosts';
public const READ_ONLY_FEATURE_LOCK = 'read_only_feature_lock';
// Load additional mime types (like images) on public share links when hide download is enabled
// Default: 'no', set to 'yes' to enable
public const USE_SECURE_VIEW_ADDITIONAL_MIMES = 'use_secure_view_additional_mimes';
private array $defaults = [
'wopi_url' => '',
'timeout' => 15,
'watermark_text' => '{userId}',
'watermark_allGroupsList' => [],
'watermark_allTagsList' => [],
'watermark_linkTagsList' => [],
'token_ttl' => 36000, // 10 hours
'doc_format' => 'ooxml',
];
public const WATERMARK_APP_NAMESPACE = 'files';
public const APP_SETTING_TYPES = [
'watermark_allGroupsList' => 'array',
'watermark_allTagsList' => 'array',
'watermark_linkTagsList' => 'array'
];
private const INTEGER_LIST_KEYS = [
'watermark_allTagsList' => true,
'watermark_linkTagsList' => true,
];
public function __construct(
private IConfig $config,
private IAppConfig $appConfig,
private IAppManager $appManager,
private GlobalScaleConfig $globalScaleConfig,
private IURLGenerator $urlGenerator,
) {
}
public function getAppNamespace($key) {
if (str_starts_with($key, 'watermark_')) {
return self::WATERMARK_APP_NAMESPACE;
}
return Application::APPNAME;
}
/**
* Get a value by key
* @param string $key
* @param string|null $defaultValue The fallback value if no configuration and global fallback was found.
* @return string
*/
public function getAppValue($key, $defaultValue = null) {
if (array_key_exists($key, $this->defaults)) {
$defaultValue = $this->defaults[$key];
}
return $this->config->getAppValue($this->getAppNamespace($key), $key, $defaultValue);
}
/**
* @param $key
* @return list<string>|string
*/
public function getAppValueArray($key) {
$value = $this->config->getAppValue($this->getAppNamespace($key), $key, []);
if (array_key_exists($key, self::APP_SETTING_TYPES) && self::APP_SETTING_TYPES[$key] === 'array') {
$value = $value !== '' ? explode(',', $value) : [];
}
return $value;
}
/**
* Set a value by key
* @param string $key
* @param string $value
* @return void
*/
public function setAppValue($key, $value) {
$this->config->setAppValue($this->getAppNamespace($key), $key, $value);
}
/**
* Get all app settings
* @return array
*/
public function getAppSettings() {
$result = [];
$keys = $this->config->getAppKeys(Application::APPNAME);
foreach ($keys as $key) {
$value = $this->getAppValueArray($key);
$value = $value === 'yes' ? true : $value;
$result[$key] = $value === 'no' ? false : $value;
}
$keys = $this->config->getAppKeys(self::WATERMARK_APP_NAMESPACE);
foreach ($keys as $key) {
if (str_starts_with($key, 'watermark_')) {
$value = $this->getAppValueArray($key);
$value = $value === 'yes' ? true : $value;
$result[$key] = $value === 'no' ? false : $value;
}
if (!empty(self::INTEGER_LIST_KEYS[$key])) {
$result[$key] = array_map('intval', $result[$key] ?? []);
}
}
return $result;
}
/**
* Returns the configured server mode ('builtin', 'custom', 'demo', or '').
*/
public function getServerMode(): string {
$stored = $this->config->getAppValue(Application::APPNAME, self::SERVER_MODE, '');
if ($stored !== '') {
return $stored;
}
// Fallback for legacy builtin installs: if the migration step has not yet run (or was
// somehow skipped), detect builtin from the stored wopi_url.
$wopiUrl = $this->config->getAppValue(Application::APPNAME, self::WOPI_URL, '');
if ($wopiUrl !== '' && str_contains($wopiUrl, 'proxy.php?req=')) {
return 'builtin';
}
return '';
}
public function isBuiltinServer(): bool {
return $this->getServerMode() === 'builtin';
}
/**
* Returns the built-in CODE proxy URL derived at runtime from IURLGenerator.
* Never stored - always fresh, so it survives Nextcloud URL/domain changes.
* Returns null if CODE is not installed or not supported on this platform.
*/
public function getBuiltinServerUrl(): ?string {
$arch = php_uname('m');
$supportedArchs = ['x86_64', 'aarch64'];
if (PHP_OS_FAMILY !== 'Linux' || !in_array($arch, $supportedArchs)) {
return null;
}
$CODEAppID = ($arch === 'aarch64') ? 'richdocumentscode_arm64' : 'richdocumentscode';
if (!$this->appManager->isInstalled($CODEAppID)) {
return null;
}
$relativeUrl = $this->urlGenerator->linkTo($CODEAppID, '') . 'proxy.php';
return $this->urlGenerator->getAbsoluteURL($relativeUrl) . '?req=';
}
/**
* Returns a list of trusted domains from the gs.trustedHosts config
*/
public function getGlobalScaleTrustedHosts(): array {
return $this->config->getSystemValue(self::SYSTEM_GS_TRUSTED_HOSTS, []);
}
/**
* Returns if federation trusted domains should be always allowed for federated editing
*/
public function isTrustedDomainAllowedForFederation(): bool {
return $this->config->getAppValue(Application::APPNAME, self::FEDERATION_USE_TRUSTED_DOMAINS, 'no') === 'yes';
}
/**
* For builtin mode, public_wopi_url is always Nextcloud's own public origin β
* CODE has no separate hostname. Derived from IURLGenerator so it is correct
* in both HTTP and CLI contexts (the latter requires overwrite.cli.url to be set,
* which is a standard Nextcloud prerequisite).
*
* For custom/standalone servers, returns the stored public_wopi_url.
*/
public function getCollaboraUrlPublic(): string {
if ($this->isBuiltinServer()) {
return $this->deriveBuiltinPublicUrl();
}
return rtrim($this->config->getAppValue(Application::APPNAME, self::PUBLIC_WOPI_URL,
$this->getCollaboraUrlInternal()), '/');
}
public function getCollaboraUrlInternal(): string {
if ($this->isBuiltinServer()) {
// Derives the internal URL at runtime from IURLGenerator.
// This avoids the CLI/browser context mismatch that otherwise arise since built-in uses ProxyPrefix not server_name/etc
return $this->getBuiltinServerUrl() ?? '';
}
return rtrim($this->config->getAppValue(Application::APPNAME, self::WOPI_URL, ''), '/');
}
public function getNextcloudUrl(): string {
return rtrim($this->config->getAppValue(Application::APPNAME, self::WOPI_CALLBACK_URL, ''), '/');
}
public function getDisableCertificateValidation(): bool {
return $this->config->getAppValue(Application::APPNAME, 'disable_certificate_verification', 'no') === 'yes';
}
public function getUseGroups(): ?array {
$groups = $this->config->getAppValue(Application::APPNAME, 'use_groups', '');
if ($groups === '') {
return null;
}
return $this->splitGroups($groups);
}
public function getEditGroups(): ?array {
$groups = $this->config->getAppValue(Application::APPNAME, 'edit_groups', '');
if ($groups === '') {
return null;
}
return $this->splitGroups($groups);
}
public function isReadOnlyFeatureLocked(): bool {
return $this->config->getAppValue(Application::APPNAME, self::READ_ONLY_FEATURE_LOCK, 'no') === 'yes';
}
private function splitGroups(string $groupString): array {
return explode('|', $groupString);
}
/**
* Allow to override values from the WOPI checkFileInfo response through app config
*/
public function getWopiOverride(): array {
$wopiOverride = $this->config->getAppValue(Application::APPNAME, 'wopi_override', '');
if ($wopiOverride !== '') {
$wopiOverride = json_decode($wopiOverride, true);
return $wopiOverride ?: [];
}
return [];
}
public function useSecureViewAdditionalMimes(): bool {
return $this->config->getAppValue(Application::APPNAME, self::USE_SECURE_VIEW_ADDITIONAL_MIMES, 'no') === 'yes';
}
public function getMimeTypes(): array {
return array_merge(
Capabilities::MIMETYPES,
Capabilities::MIMETYPES_MSOFFICE,
Capabilities::MIMETYPES_OPTIONAL,
);
}
public function getDomainList(): array {
$urls = array_merge(
[ $this->domainOnly($this->getCollaboraUrlPublic()) ],
$this->getFederationDomains(),
$this->getGSDomains()
);
return array_map(fn ($url) => idn_to_ascii($url), array_filter($urls));
}
private function getFederationDomains(): array {
if (!$this->appManager->isEnabledForUser('federation')) {
return [];
}
$federationService = \OCP\Server::get(FederationService::class);
$trustedNextcloudDomains = array_filter(array_map(fn ($server) => $federationService->isTrustedRemote($server) ? $server : null, $federationService->getTrustedServers()));
$trustedCollaboraDomains = array_filter(array_map(function ($server) use ($federationService) {
try {
return $federationService->getRemoteCollaboraURL($server);
} catch (\Exception) {
// If there is no remote collabora server we can just skip that
return null;
}
}, $trustedNextcloudDomains));
return array_map(fn ($url) => $this->domainOnly($url), array_merge($trustedNextcloudDomains, $trustedCollaboraDomains));
}
public function isPreviewGenerationEnabled(): bool {
return $this->appConfig->getAppValueBool('preview_generation', true);
}
private function getGSDomains(): array {
if (!$this->globalScaleConfig->isGlobalScaleEnabled()) {
return [];
}
return $this->getGlobalScaleTrustedHosts();
}
/**
* Derives the public URL for the built-in CODE server directly from IURLGenerator,
* without requiring server_mode to already be set. Used during initial setup/CLI
* where we want to validate the URL before committing server_mode to config.
*/
public function deriveBuiltinPublicUrl(): string {
return rtrim($this->domainOnly($this->urlGenerator->getAbsoluteURL('/')), '/');
}
/**
* Strips the path and query parameters from the URL.
*/
public function domainOnly(string $url): string {
$parsedUrl = parse_url($url);
$scheme = isset($parsedUrl['scheme']) ? $parsedUrl['scheme'] . '://' : '';
$host = $parsedUrl['host'] ?? '';
$port = isset($parsedUrl['port']) ? ':' . $parsedUrl['port'] : '';
return "$scheme$host$port";
}
}