Completed
Push — master ( 4f3689...17b456 )
by Maxence
02:12 queued 11s
created

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