-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathAppConfig.php
More file actions
260 lines (218 loc) Β· 7.87 KB
/
AppConfig.php
File metadata and controls
260 lines (218 loc) Β· 7.87 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
<?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\GlobalScale\IConfig as GlobalScaleConfig;
use OCP\IConfig;
class AppConfig {
// 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
];
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 IAppManager $appManager,
private GlobalScaleConfig $globalScaleConfig,
) {
}
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 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';
}
public function getCollaboraUrlPublic(): string {
return rtrim($this->config->getAppValue(Application::APPNAME, self::PUBLIC_WOPI_URL, $this->getCollaboraUrlInternal()), '/');
}
public function getCollaboraUrlInternal(): string {
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,
);
}
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));
}
private function getGSDomains(): array {
if (!$this->globalScaleConfig->isGlobalScaleEnabled()) {
return [];
}
return $this->getGlobalScaleTrustedHosts();
}
/**
* 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";
}
}