Completed
Push — master ( 66b941...dc964f )
by Maxence
02:29 queued 50s
created

ConfigService::getSystemValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\Circles\Service;
28
29
use OCA\Circles\Exceptions\GSStatusException;
30
use OCA\Circles\Model\Circle;
31
use OCP\IConfig;
32
use OCP\IRequest;
33
use OCP\PreConditionNotMetException;
34
use OCP\Util;
35
36
class ConfigService {
37
38
	const CIRCLES_ALLOW_CIRCLES = 'allow_circles';
39
	const CIRCLES_CONTACT_BACKEND = 'contact_backend';
40
	const CIRCLES_STILL_FRONTEND = 'still_frontend';
41
	const CIRCLES_SWAP_TO_TEAMS = 'swap_to_teams';
42
	const CIRCLES_ALLOW_FEDERATED_CIRCLES = 'allow_federated';
43
	const CIRCLES_GS_ENABLED = 'gs_enabled';
44
	const CIRCLES_MEMBERS_LIMIT = 'members_limit';
45
	const CIRCLES_ACCOUNTS_ONLY = 'accounts_only';
46
	const CIRCLES_ALLOW_LINKED_GROUPS = 'allow_linked_groups';
47
	const CIRCLES_ALLOW_NON_SSL_LINKS = 'allow_non_ssl_links';
48
	const CIRCLES_NON_SSL_LOCAL = 'local_is_non_ssl';
49
	const CIRCLES_SELF_SIGNED = 'self_signed_cert';
50
	const LOCAL_CLOUD_ID = 'local_cloud_id';
51
	const CIRCLES_ACTIVITY_ON_CREATION = 'creation_activity';
52
	const CIRCLES_SKIP_INVITATION_STEP = 'skip_invitation_to_closed_circles';
53
	const CIRCLES_SEARCH_FROM_COLLABORATOR = 'search_from_collaborator';
54
	const CIRCLES_TEST_ASYNC_LOCK = 'test_async_lock';
55
	const CIRCLES_TEST_ASYNC_INIT = 'test_async_init';
56
	const CIRCLES_TEST_ASYNC_HAND = 'test_async_hand';
57
	const CIRCLES_TEST_ASYNC_COUNT = 'test_async_count';
58
59
	const GS_ENABLED = 'enabled';
60
	const GS_MODE = 'mode';
61
	const GS_KEY = 'key';
62
	const GS_LOOKUP = 'lookup';
63
64
	const GS_LOOKUP_INSTANCES = '/instances';
65
66
67
	private $defaults = [
68
		self::CIRCLES_ALLOW_CIRCLES            => Circle::CIRCLES_ALL,
69
		self::CIRCLES_CONTACT_BACKEND          => '0',
70
		self::CIRCLES_STILL_FRONTEND           => '0',
71
		self::CIRCLES_TEST_ASYNC_INIT          => '0',
72
		self::CIRCLES_SWAP_TO_TEAMS            => '0',
73
		self::CIRCLES_ACCOUNTS_ONLY            => '0',
74
		self::CIRCLES_MEMBERS_LIMIT            => '50',
75
		self::CIRCLES_ALLOW_LINKED_GROUPS      => '0',
76
		self::CIRCLES_ALLOW_FEDERATED_CIRCLES  => '0',
77
		self::CIRCLES_GS_ENABLED               => '0',
78
		self::CIRCLES_ALLOW_NON_SSL_LINKS      => '0',
79
		self::CIRCLES_NON_SSL_LOCAL            => '0',
80
		self::CIRCLES_SELF_SIGNED              => '0',
81
		self::LOCAL_CLOUD_ID                   => '',
82
		self::CIRCLES_ACTIVITY_ON_CREATION     => '1',
83
		self::CIRCLES_SKIP_INVITATION_STEP     => '0',
84
		self::CIRCLES_SEARCH_FROM_COLLABORATOR => '0'
85
	];
86
87
	/** @var string */
88
	private $appName;
89
90
	/** @var IConfig */
91
	private $config;
92
93
	/** @var string */
94
	private $userId;
95
96
	/** @var IRequest */
97
	private $request;
98
99
	/** @var MiscService */
100
	private $miscService;
101
102
	/** @var int */
103
	private $allowedCircle = -1;
104
105
	/** @var int */
106
	private $allowedLinkedGroups = -1;
107
108
	/** @var int */
109
	private $allowedFederatedCircles = -1;
110
111
	/** @var int */
112
	private $allowedNonSSLLinks = -1;
113
114
	/** @var int */
115
	private $localNonSSL = -1;
116
117
	/**
118
	 * ConfigService constructor.
119
	 *
120
	 * @param string $appName
121
	 * @param IConfig $config
122
	 * @param IRequest $request
123
	 * @param string $userId
124
	 * @param MiscService $miscService
125
	 */
126
	public function __construct(
127
		$appName, IConfig $config, IRequest $request, $userId, MiscService $miscService
128
	) {
129
		$this->appName = $appName;
130
		$this->config = $config;
131
		$this->request = $request;
132
		$this->userId = $userId;
133
		$this->miscService = $miscService;
134
	}
135
136
137
	public function getLocalAddress() {
138
		return (($this->isLocalNonSSL()) ? 'http://' : '')
139
			   . $this->request->getServerHost();
140
	}
141
142
143
	/**
144
	 * returns if this type of circle is allowed by the current configuration.
145
	 *
146
	 * @param $type
147
	 *
148
	 * @return int
149
	 */
150
	public function isCircleAllowed($type) {
151
		if ($this->allowedCircle === -1) {
152
			$this->allowedCircle = (int)$this->getAppValue(self::CIRCLES_ALLOW_CIRCLES);
153
		}
154
155
		return ((int)$type & (int)$this->allowedCircle);
156
	}
157
158
159
	/**
160
	 * @return bool
161
	 * @throws GSStatusException
162
	 */
163
	public function isLinkedGroupsAllowed() {
164
		if ($this->allowedLinkedGroups === -1) {
165
			$allowed = ($this->getAppValue(self::CIRCLES_ALLOW_LINKED_GROUPS) === '1'
166
						&& !$this->getGSStatus(self::GS_ENABLED));
167
			$this->allowedLinkedGroups = ($allowed) ? 1 : 0;
168
		}
169
170
		return ($this->allowedLinkedGroups === 1);
171
	}
172
173
174
	/**
175
	 * @return bool
176
	 */
177
	public function isFederatedCirclesAllowed() {
178
		if ($this->allowedFederatedCircles === -1) {
179
			$this->allowedFederatedCircles =
180
				(int)$this->getAppValue(self::CIRCLES_ALLOW_FEDERATED_CIRCLES);
181
		}
182
183
		return ($this->allowedFederatedCircles === 1);
184
	}
185
186
	/**
187
	 * @return bool
188
	 */
189
	public function isInvitationSkipped() {
190
		return (int)$this->getAppValue(self::CIRCLES_SKIP_INVITATION_STEP) === 1;
191
	}
192
193
	/**
194
	 * @return bool
195
	 */
196
	public function isLocalNonSSL() {
197
		if ($this->localNonSSL === -1) {
198
			$this->localNonSSL =
199
				(int)$this->getAppValue(self::CIRCLES_NON_SSL_LOCAL);
200
		}
201
202
		return ($this->localNonSSL === 1);
203
	}
204
205
206
	/**
207
	 * @return bool
208
	 */
209
	public function isNonSSLLinksAllowed() {
210
		if ($this->allowedNonSSLLinks === -1) {
211
			$this->allowedNonSSLLinks =
212
				(int)$this->getAppValue(self::CIRCLES_ALLOW_NON_SSL_LINKS);
213
		}
214
215
		return ($this->allowedNonSSLLinks === 1);
216
	}
217
218
219
	/**
220
	 * @param string $remote
221
	 *
222
	 * @return string
223
	 */
224
	public function generateRemoteHost($remote) {
225
		if ((!$this->isNonSSLLinksAllowed() || strpos($remote, 'http://') !== 0)
226
			&& strpos($remote, 'https://') !== 0
227
		) {
228
			$remote = 'https://' . $remote;
229
		}
230
231
		return rtrim($remote, '/');
232
	}
233
234
235
	/**
236
	 * Get a value by key
237
	 *
238
	 * @param string $key
239
	 *
240
	 * @return string
241
	 */
242
	public function getCoreValue($key) {
243
		$defaultValue = null;
244
245
		return $this->config->getAppValue('core', $key, $defaultValue);
246
	}
247
248
	/**
249
	 * Get a value by key
250
	 *
251
	 * @param string $key
252
	 *
253
	 * @return string
254
	 */
255
	public function getSystemValue($key) {
256
		$defaultValue = null;
257
258
		return $this->config->getSystemValue($key, $defaultValue);
259
	}
260
261
262
	/**
263
	 * Get available hosts
264
	 *
265
	 * @return array
266
	 */
267
	public function getAvailableHosts(): array {
268
		return $this->config->getSystemValue('trusted_domains', []);
269
	}
270
271
272
	/**
273
	 * Get a value by key
274
	 *
275
	 * @param string $key
276
	 *
277
	 * @return string
278
	 */
279
	public function getAppValue($key) {
280
		$defaultValue = null;
281
282
		if (array_key_exists($key, $this->defaults)) {
283
			$defaultValue = $this->defaults[$key];
284
		}
285
286
		return $this->config->getAppValue($this->appName, $key, $defaultValue);
287
	}
288
289
	/**
290
	 * Set a value by key
291
	 *
292
	 * @param string $key
293
	 * @param string $value
294
	 *
295
	 * @return void
296
	 */
297
	public function setAppValue($key, $value) {
298
		$this->config->setAppValue($this->appName, $key, $value);
299
	}
300
301
	/**
302
	 * remove a key
303
	 *
304
	 * @param string $key
305
	 *
306
	 * @return string
307
	 */
308
	public function deleteAppValue($key) {
309
		return $this->config->deleteAppValue($this->appName, $key);
310
	}
311
312
	/**
313
	 * Get a user value by key
314
	 *
315
	 * @param string $key
316
	 *
317
	 * @return string
318
	 */
319
	public function getUserValue($key) {
320
		return $this->config->getUserValue($this->userId, $this->appName, $key);
321
	}
322
323
	/**
324
	 * Set a user value by key
325
	 *
326
	 * @param string $key
327
	 * @param string $value
328
	 *
329
	 * @return string
330
	 * @throws PreConditionNotMetException
331
	 */
332
	public function setUserValue($key, $value) {
333
		return $this->config->setUserValue($this->userId, $this->appName, $key, $value);
334
	}
335
336
337
	/**
338
	 * Get a user value by key and user
339
	 *
340
	 * @param string $userId
341
	 * @param string $key
342
	 *
343
	 * @param string $default
344
	 *
345
	 * @return string
346
	 */
347
	public function getCoreValueForUser($userId, $key, $default = '') {
348
		return $this->config->getUserValue($userId, 'core', $key, $default);
349
	}
350
351
352
	/**
353
	 * Get a user value by key and user
354
	 *
355
	 * @param string $userId
356
	 * @param string $key
357
	 *
358
	 * @return string
359
	 */
360
	public function getValueForUser($userId, $key) {
361
		return $this->config->getUserValue($userId, $this->appName, $key);
362
	}
363
364
	/**
365
	 * Set a user value by key
366
	 *
367
	 * @param string $userId
368
	 * @param string $key
369
	 * @param string $value
370
	 *
371
	 * @return string
372
	 * @throws PreConditionNotMetException
373
	 */
374
	public function setValueForUser($userId, $key, $value) {
375
		return $this->config->setUserValue($userId, $this->appName, $key, $value);
376
	}
377
378
	/**
379
	 * return the cloud version.
380
	 * if $complete is true, return a string x.y.z
381
	 *
382
	 * @param boolean $complete
383
	 *
384
	 * @return string|integer
385
	 */
386
	public function getCloudVersion($complete = false) {
387
		$ver = Util::getVersion();
388
389
		if ($complete) {
390
			return implode('.', $ver);
391
		}
392
393
		return $ver[0];
394
	}
395
396
397
	/**
398
	 * @return bool
399
	 */
400
	public function isAccountOnly() {
401
		return ($this->getAppValue(self::CIRCLES_ACCOUNTS_ONLY) === '1');
402
	}
403
404
405
	/**
406
	 * @return bool
407
	 */
408
	public function isContactsBackend(): bool {
409
		return ($this->getAppValue(ConfigService::CIRCLES_CONTACT_BACKEND) !== '0');
410
	}
411
412
413
	public function contactsBackendType(): int {
414
		return (int)$this->getAppValue(ConfigService::CIRCLES_CONTACT_BACKEND);
415
	}
416
417
418
	/**
419
	 * @return bool
420
	 */
421
	public function stillFrontEnd(): bool {
422
		if ($this->getAppValue(self::CIRCLES_CONTACT_BACKEND) !== '1') {
423
			return true;
424
		}
425
426
		if ($this->getAppValue(self::CIRCLES_STILL_FRONTEND) === '1') {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return $this->getAppValu...TILL_FRONTEND) === '1';.
Loading history...
427
			return true;
428
		}
429
430
		return false;
431
	}
432
433
434
	/**
435
	 * should the password for a mail share be send to the recipient
436
	 *
437
	 * @return bool
438
	 */
439
	public function sendPasswordByMail() {
440
		if ($this->getAppValue(self::CIRCLES_CONTACT_BACKEND) === '1') {
441
			return false;
442
		}
443
444
		return ($this->config->getAppValue('sharebymail', 'sendpasswordmail', 'yes') === 'yes');
445
	}
446
447
	/**
448
	 * do we require a share by mail to be password protected
449
	 *
450
	 * @param Circle $circle
451
	 *
452
	 * @return bool
453
	 */
454
	public function enforcePasswordProtection(Circle $circle) {
455
		if ($this->getAppValue(self::CIRCLES_CONTACT_BACKEND) === '1') {
456
			return false;
457
		}
458
459
		if ($circle->getSetting('password_enforcement') === 'true') {
460
			return true;
461
		}
462
463
		return ($this->config->getAppValue('sharebymail', 'enforcePasswordProtection', 'no') === 'yes');
464
	}
465
466
467
	/**
468
	 * @param string $type
469
	 *
470
	 * @throws GSStatusException
471
	 */
472
	public function getGSStatus(string $type = '') {
473
		$enabled = $this->config->getSystemValueBool('gs.enabled', false);
474
		$lookup = $this->config->getSystemValue('lookup_server', '');
475
476
		if ($lookup === '' || !$enabled) {
477
			if ($type === self::GS_ENABLED) {
478
				return false;
479
			}
480
481
			throw new GSStatusException('GS and lookup are not configured : ' . $lookup . ', ' . $enabled);
482
		}
483
484
		$clef = $this->config->getSystemValue('gss.jwt.key', '');
485
		$mode = $this->config->getSystemValue('gss.mode', '');
486
487
		switch ($type) {
488
			case self::GS_ENABLED:
489
				return $enabled;
490
491
			case self::GS_MODE:
492
				return $mode;
493
494
			case self::GS_KEY:
495
				return $clef;
496
497
			case self::GS_LOOKUP:
498
				return $lookup;
499
		}
500
501
		return [
502
			self::GS_ENABLED => $enabled,
503
			self::GS_LOOKUP  => $lookup,
504
			self::GS_MODE    => $clef,
505
			self::GS_KEY     => $mode,
506
		];
507
	}
508
509
510
	/**
511
	 * @return array
512
	 */
513
	public function getTrustedDomains(): array {
514
		$domains = [];
515
		foreach ($this->config->getSystemValue('trusted_domains', []) as $v) {
516
			$domains[] = $v;
517
		}
518
519
		return $domains;
520
	}
521
522
523
	/**
524
	 * @return string
525
	 */
526
	public function getLocalCloudId(): string {
527
		$localCloudId = $this->getAppValue(self::LOCAL_CLOUD_ID);
528
		if ($localCloudId === '') {
529
			return $this->getTrustedDomains()[0];
530
		}
531
532
		return $localCloudId;
533
	}
534
535
536
	/**
537
	 * @return mixed
538
	 */
539
	public function getInstanceId() {
540
		return $this->config->getSystemValue('instanceid');
541
	}
542
543
}
544
545