Completed
Push — master ( c43ec7...508e31 )
by Maxence
01:53 queued 17s
created

ConfigService::generateRemoteHost()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.9666
cc 4
nc 2
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\Model\Circle;
30
use OCP\IConfig;
31
use OCP\IRequest;
32
use OCP\PreConditionNotMetException;
33
use OCP\Util;
34
35
class ConfigService {
36
37
	const CIRCLES_ALLOW_CIRCLES = 'allow_circles';
38
	const CIRCLES_SWAP_TO_TEAMS = 'swap_to_teams';
39
	const CIRCLES_ALLOW_FEDERATED_CIRCLES = 'allow_federated';
40
	const CIRCLES_MEMBERS_LIMIT = 'members_limit';
41
	const CIRCLES_ALLOW_LINKED_GROUPS = 'allow_linked_groups';
42
	const CIRCLES_ALLOW_NON_SSL_LINKS = 'allow_non_ssl_links';
43
	const CIRCLES_NON_SSL_LOCAL = 'local_is_non_ssl';
44
	const CIRCLES_ACTIVITY_ON_CREATION = 'creation_activity';
45
46
	const CIRCLES_TEST_ASYNC_LOCK = 'test_async_lock';
47
	const CIRCLES_TEST_ASYNC_INIT = 'test_async_init';
48
	const CIRCLES_TEST_ASYNC_HAND = 'test_async_hand';
49
	const CIRCLES_TEST_ASYNC_COUNT = 'test_async_count';
50
51
	private $defaults = [
52
		self::CIRCLES_ALLOW_CIRCLES           => Circle::CIRCLES_ALL,
53
		self::CIRCLES_TEST_ASYNC_INIT         => '0',
54
		self::CIRCLES_SWAP_TO_TEAMS           => '0',
55
		self::CIRCLES_MEMBERS_LIMIT           => '50',
56
		self::CIRCLES_ALLOW_LINKED_GROUPS     => '0',
57
		self::CIRCLES_ALLOW_FEDERATED_CIRCLES => '0',
58
		self::CIRCLES_ALLOW_NON_SSL_LINKS     => '0',
59
		self::CIRCLES_NON_SSL_LOCAL           => '0',
60
		self::CIRCLES_ACTIVITY_ON_CREATION    => '1'
61
	];
62
63
	/** @var string */
64
	private $appName;
65
66
	/** @var IConfig */
67
	private $config;
68
69
	/** @var string */
70
	private $userId;
71
72
	/** @var IRequest */
73
	private $request;
74
75
	/** @var MiscService */
76
	private $miscService;
77
78
	/** @var int */
79
	private $allowedCircle = -1;
80
81
	/** @var int */
82
	private $allowedLinkedGroups = -1;
83
84
	/** @var int */
85
	private $allowedFederatedCircles = -1;
86
87
	/** @var int */
88
	private $allowedNonSSLLinks = -1;
89
90
	/** @var int */
91
	private $localNonSSL = -1;
92
93
	/**
94
	 * ConfigService constructor.
95
	 *
96
	 * @param string $appName
97
	 * @param IConfig $config
98
	 * @param IRequest $request
99
	 * @param string $userId
100
	 * @param MiscService $miscService
101
	 */
102
	public function __construct(
103
		$appName, IConfig $config, IRequest $request, $userId, MiscService $miscService
104
	) {
105
		$this->appName = $appName;
106
		$this->config = $config;
107
		$this->request = $request;
108
		$this->userId = $userId;
109
		$this->miscService = $miscService;
110
	}
111
112
113
	public function getLocalAddress() {
114
		return (($this->isLocalNonSSL()) ? 'http://' : '')
115
			   . $this->request->getServerHost();
116
	}
117
118
119
	/**
120
	 * returns if this type of circle is allowed by the current configuration.
121
	 *
122
	 * @param $type
123
	 *
124
	 * @return int
125
	 */
126
	public function isCircleAllowed($type) {
127
		if ($this->allowedCircle === -1) {
128
			$this->allowedCircle = (int)$this->getAppValue(self::CIRCLES_ALLOW_CIRCLES);
129
		}
130
131
		return ((int)$type & (int)$this->allowedCircle);
132
	}
133
134
135
	/**
136
	 * @return bool
137
	 */
138
	public function isLinkedGroupsAllowed() {
139
		if ($this->allowedLinkedGroups === -1) {
140
			$this->allowedLinkedGroups =
141
				(int)$this->getAppValue(self::CIRCLES_ALLOW_LINKED_GROUPS);
142
		}
143
144
		return ($this->allowedLinkedGroups === 1);
145
	}
146
147
148
	/**
149
	 * @return bool
150
	 */
151
	public function isFederatedCirclesAllowed() {
152
		if ($this->allowedFederatedCircles === -1) {
153
			$this->allowedFederatedCircles =
154
				(int)$this->getAppValue(self::CIRCLES_ALLOW_FEDERATED_CIRCLES);
155
		}
156
157
		return ($this->allowedFederatedCircles === 1);
158
	}
159
160
161
	/**
162
	 * @return bool
163
	 */
164
	public function isLocalNonSSL() {
165
		if ($this->localNonSSL === -1) {
166
			$this->localNonSSL =
167
				(int)$this->getAppValue(self::CIRCLES_NON_SSL_LOCAL);
168
		}
169
170
		return ($this->localNonSSL === 1);
171
	}
172
173
174
	/**
175
	 * @return bool
176
	 */
177
	public function isNonSSLLinksAllowed() {
178
		if ($this->allowedNonSSLLinks === -1) {
179
			$this->allowedNonSSLLinks =
180
				(int)$this->getAppValue(self::CIRCLES_ALLOW_NON_SSL_LINKS);
181
		}
182
183
		return ($this->allowedNonSSLLinks === 1);
184
	}
185
186
187
	/**
188
	 * @param string $remote
189
	 *
190
	 * @return string
191
	 */
192
	public function generateRemoteHost($remote) {
193
		if ((!$this->isNonSSLLinksAllowed() || strpos($remote, 'http://') !== 0)
194
			&& strpos($remote, 'https://') !== 0
195
		) {
196
			$remote = 'https://' . $remote;
197
		}
198
199
		return rtrim($remote, '/');
200
	}
201
202
203
	/**
204
	 * Get a value by key
205
	 *
206
	 * @param string $key
207
	 *
208
	 * @return string
209
	 */
210
	public function getCoreValue($key) {
211
		$defaultValue = null;
212
213
		return $this->config->getAppValue('core', $key, $defaultValue);
214
	}
215
216
217
	/**
218
	 * Get a value by key
219
	 *
220
	 * @param string $key
221
	 *
222
	 * @return string
223
	 */
224
	public function getAppValue($key) {
225
		$defaultValue = null;
226
227
		if (array_key_exists($key, $this->defaults)) {
228
			$defaultValue = $this->defaults[$key];
229
		}
230
231
		return $this->config->getAppValue($this->appName, $key, $defaultValue);
232
	}
233
234
	/**
235
	 * Set a value by key
236
	 *
237
	 * @param string $key
238
	 * @param string $value
239
	 *
240
	 * @return void
241
	 */
242
	public function setAppValue($key, $value) {
243
		$this->config->setAppValue($this->appName, $key, $value);
244
	}
245
246
	/**
247
	 * remove a key
248
	 *
249
	 * @param string $key
250
	 *
251
	 * @return string
252
	 */
253
	public function deleteAppValue($key) {
254
		return $this->config->deleteAppValue($this->appName, $key);
255
	}
256
257
	/**
258
	 * Get a user value by key
259
	 *
260
	 * @param string $key
261
	 *
262
	 * @return string
263
	 */
264
	public function getUserValue($key) {
265
		return $this->config->getUserValue($this->userId, $this->appName, $key);
266
	}
267
268
	/**
269
	 * Set a user value by key
270
	 *
271
	 * @param string $key
272
	 * @param string $value
273
	 *
274
	 * @return string
275
	 * @throws PreConditionNotMetException
276
	 */
277
	public function setUserValue($key, $value) {
278
		return $this->config->setUserValue($this->userId, $this->appName, $key, $value);
279
	}
280
281
282
	/**
283
	 * Get a user value by key and user
284
	 *
285
	 * @param string $userId
286
	 * @param string $key
287
	 *
288
	 * @param string $default
289
	 *
290
	 * @return string
291
	 */
292
	public function getCoreValueForUser($userId, $key, $default = '') {
293
		return $this->config->getUserValue($userId, 'core', $key, $default);
294
	}
295
296
297
	/**
298
	 * Get a user value by key and user
299
	 *
300
	 * @param string $userId
301
	 * @param string $key
302
	 *
303
	 * @return string
304
	 */
305
	public function getValueForUser($userId, $key) {
306
		return $this->config->getUserValue($userId, $this->appName, $key);
307
	}
308
309
	/**
310
	 * Set a user value by key
311
	 *
312
	 * @param string $userId
313
	 * @param string $key
314
	 * @param string $value
315
	 *
316
	 * @return string
317
	 * @throws PreConditionNotMetException
318
	 */
319
	public function setValueForUser($userId, $key, $value) {
320
		return $this->config->setUserValue($userId, $this->appName, $key, $value);
321
	}
322
323
	/**
324
	 * return the cloud version.
325
	 * if $complete is true, return a string x.y.z
326
	 *
327
	 * @param boolean $complete
328
	 *
329
	 * @return string|integer
330
	 */
331
	public function getCloudVersion($complete = false) {
332
		$ver = Util::getVersion();
333
334
		if ($complete) {
335
			return implode('.', $ver);
336
		}
337
338
		return $ver[0];
339
	}
340
}
341