Completed
Pull Request — master (#401)
by Tortue
02:02
created

ConfigService::isInvitationSkipped()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
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 OC;
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_GROUP_BACKEND = 'group_backend';
42
	const CIRCLES_GROUP_BACKEND_NAME_PREFIX = 'group_backend_name_prefix';
43
	const CIRCLES_GROUP_BACKEND_NAME_SUFFIX = 'group_backend_name_suffix';
44
	const CIRCLES_SWAP_TO_TEAMS = 'swap_to_teams';
45
	const CIRCLES_ALLOW_FEDERATED_CIRCLES = 'allow_federated';
46
	const CIRCLES_MEMBERS_LIMIT = 'members_limit';
47
	const CIRCLES_ACCOUNTS_ONLY = 'accounts_only';
48
	const CIRCLES_ALLOW_LINKED_GROUPS = 'allow_linked_groups';
49
	const CIRCLES_ALLOW_NON_SSL_LINKS = 'allow_non_ssl_links';
50
	const CIRCLES_NON_SSL_LOCAL = 'local_is_non_ssl';
51
	const CIRCLES_ACTIVITY_ON_CREATION = 'creation_activity';
52
	const CIRCLES_SKIP_INVITATION_STEP = 'skip_invitation_to_closed_circles';
53
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
	private $defaults = [
60
		self::CIRCLES_ALLOW_CIRCLES              => Circle::CIRCLES_ALL,
61
		self::CIRCLES_CONTACT_BACKEND            => '0',
62
		self::CIRCLES_STILL_FRONTEND             => '0',
63
		self::CIRCLES_GROUP_BACKEND              => '0',
64
		self::CIRCLES_GROUP_BACKEND_NAME_PREFIX  => '',
65
		self::CIRCLES_GROUP_BACKEND_NAME_SUFFIX  => '',
66
		self::CIRCLES_TEST_ASYNC_INIT            => '0',
67
		self::CIRCLES_SWAP_TO_TEAMS              => '0',
68
		self::CIRCLES_ACCOUNTS_ONLY              => '0',
69
		self::CIRCLES_MEMBERS_LIMIT              => '50',
70
		self::CIRCLES_ALLOW_FILES_CIRCLES_FILTER => '1',
71
		self::CIRCLES_ALLOW_LISTED_CIRCLES       => '1',
72
		self::CIRCLES_ALLOW_ANY_GROUP_MEMBERS    => '1',
73
		self::CIRCLES_ALLOW_LINKED_GROUPS        => '0',
74
		self::CIRCLES_ALLOW_FEDERATED_CIRCLES    => '0',
75
		self::CIRCLES_ALLOW_NON_SSL_LINKS        => '0',
76
		self::CIRCLES_NON_SSL_LOCAL              => '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
	/** @var string */
112
	private $groupBackendNamePrefix = null;
113
114
	/** @var string */
115
	private $groupBackendNameSuffix = null;
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
	 */
162
	public function isLinkedGroupsAllowed() {
163
		if ($this->allowedLinkedGroups === -1) {
164
			$this->allowedLinkedGroups =
165
				(int)$this->getAppValue(self::CIRCLES_ALLOW_LINKED_GROUPS);
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
	/**
248
	 * Get available hosts
249
	 *
250
	 * @return array
251
	 */
252
	public function getAvailableHosts(): array {
253
		return $this->config->getSystemValue('trusted_domains', []);
254
	}
255
256
257
	/**
258
	 * Get a value by key
259
	 *
260
	 * @param string $key
261
	 *
262
	 * @return string
263
	 */
264
	public function getAppValue($key) {
265
		$defaultValue = null;
266
267
		if (array_key_exists($key, $this->defaults)) {
268
			$defaultValue = $this->defaults[$key];
269
		}
270
271
		return $this->config->getAppValue($this->appName, $key, $defaultValue);
272
	}
273
274
	/**
275
	 * Set a value by key
276
	 *
277
	 * @param string $key
278
	 * @param string $value
279
	 *
280
	 * @return void
281
	 */
282
	public function setAppValue($key, $value) {
283
		$this->config->setAppValue($this->appName, $key, $value);
284
	}
285
286
	/**
287
	 * remove a key
288
	 *
289
	 * @param string $key
290
	 *
291
	 * @return string
292
	 */
293
	public function deleteAppValue($key) {
294
		return $this->config->deleteAppValue($this->appName, $key);
295
	}
296
297
	/**
298
	 * Get a user value by key
299
	 *
300
	 * @param string $key
301
	 *
302
	 * @return string
303
	 */
304
	public function getUserValue($key) {
305
		return $this->config->getUserValue($this->userId, $this->appName, $key);
306
	}
307
308
	/**
309
	 * Set a user value by key
310
	 *
311
	 * @param string $key
312
	 * @param string $value
313
	 *
314
	 * @return string
315
	 * @throws PreConditionNotMetException
316
	 */
317
	public function setUserValue($key, $value) {
318
		return $this->config->setUserValue($this->userId, $this->appName, $key, $value);
319
	}
320
321
322
	/**
323
	 * Get a user value by key and user
324
	 *
325
	 * @param string $userId
326
	 * @param string $key
327
	 *
328
	 * @param string $default
329
	 *
330
	 * @return string
331
	 */
332
	public function getCoreValueForUser($userId, $key, $default = '') {
333
		return $this->config->getUserValue($userId, 'core', $key, $default);
334
	}
335
336
337
	/**
338
	 * Get a user value by key and user
339
	 *
340
	 * @param string $userId
341
	 * @param string $key
342
	 *
343
	 * @return string
344
	 */
345
	public function getValueForUser($userId, $key) {
346
		return $this->config->getUserValue($userId, $this->appName, $key);
347
	}
348
349
	/**
350
	 * Set a user value by key
351
	 *
352
	 * @param string $userId
353
	 * @param string $key
354
	 * @param string $value
355
	 *
356
	 * @return string
357
	 * @throws PreConditionNotMetException
358
	 */
359
	public function setValueForUser($userId, $key, $value) {
360
		return $this->config->setUserValue($userId, $this->appName, $key, $value);
361
	}
362
363
	/**
364
	 * return the cloud version.
365
	 * if $complete is true, return a string x.y.z
366
	 *
367
	 * @param boolean $complete
368
	 *
369
	 * @return string|integer
370
	 */
371
	public function getCloudVersion($complete = false) {
372
		$ver = Util::getVersion();
373
374
		if ($complete) {
375
			return implode('.', $ver);
376
		}
377
378
		return $ver[0];
379
	}
380
381
382
	/**
383
	 * @return bool
384
	 */
385
	public function isAccountOnly() {
386
		return ($this->getAppValue(self::CIRCLES_ACCOUNTS_ONLY) === '1');
387
	}
388
389
390
	/**
391
	 * @return bool
392
	 */
393
	public function isContactsBackend(): bool {
394
		return ($this->getAppValue(ConfigService::CIRCLES_CONTACT_BACKEND) !== '0');
395
	}
396
397
398
	public function contactsBackendType(): int {
399
		return (int)$this->getAppValue(ConfigService::CIRCLES_CONTACT_BACKEND);
400
	}
401
402
	/**
403
	 * @return bool
404
	 */
405
	public function isGroupsBackend(): bool {
406
		return ($this->getAppValue(ConfigService::CIRCLES_GROUP_BACKEND) !== '0');
407
	}
408
409
	/**
410
	 * returns the prefix of the group name
411
	 *
412
	 * @return string|null
413
	 */
414
	public function getGroupBackendNamePrefix() {
415
		if ($this->groupBackendNamePrefix === null && $this->isGroupsBackend()) {
416
			$this->groupBackendNamePrefix = ltrim((string) $this->getAppValue(self::CIRCLES_GROUP_BACKEND_NAME_PREFIX));
417
		}
418
419
		return $this->groupBackendNamePrefix;
420
	}
421
422
	/**
423
	 * returns the suffix of the group name
424
	 *
425
	 * @return string|null
426
	 */
427
	public function getGroupBackendNameSuffix() {
428
		if ($this->groupBackendNameSuffix === null && $this->isGroupsBackend()) {
429
			$l = OC::$server->getL10N('circles');
430
			$defaultSuffix = ' '.$l->t('Circle');
431
			$customSuffix = (string) $this->getAppValue(self::CIRCLES_GROUP_BACKEND_NAME_SUFFIX);
432
			$this->groupBackendNameSuffix = rtrim($customSuffix ?: $defaultSuffix);
433
		}
434
435
		return $this->groupBackendNameSuffix;
436
	}
437
438
	/**
439
	 * @return bool
440
	 */
441
	public function stillFrontEnd(): bool {
442
		if ($this->getAppValue(self::CIRCLES_CONTACT_BACKEND) !== '1') {
443
			return true;
444
		}
445
446
		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...
447
			return true;
448
		}
449
450
		return false;
451
	}
452
453
454
	/**
455
	 * should the password for a mail share be send to the recipient
456
	 *
457
	 * @return bool
458
	 */
459
	public function sendPasswordByMail() {
460
		if ($this->getAppValue(self::CIRCLES_CONTACT_BACKEND) === '1') {
461
			return false;
462
		}
463
464
		return ($this->config->getAppValue('sharebymail', 'sendpasswordmail', 'yes') === 'yes');
465
	}
466
467
	/**
468
	 * do we require a share by mail to be password protected
469
	 *
470
	 * @return bool
471
	 */
472
	public function enforcePasswordProtection() {
473
		if ($this->getAppValue(self::CIRCLES_CONTACT_BACKEND) === '1') {
474
			return false;
475
		}
476
477
		return ($this->config->getAppValue('sharebymail', 'enforcePasswordProtection', 'no') === 'yes');
478
	}
479
480
481
	public function getInstanceId() {
482
		return $this->config->getSystemValue('instanceid');
483
	}
484
485
}
486