Completed
Push — master ( 4edf58...b5cbd9 )
by Maxence
19s queued 10s
created

ConfigService::isLocalNonSSL()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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