Completed
Push — master ( 462815...ee89ff )
by Maxence
03:04
created

ConfigService::getAppValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
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\Util;
33
34
class ConfigService {
35
36
	const CIRCLES_ALLOW_CIRCLES = 'allow_circles';
37
	const CIRCLES_SWAP_TO_TEAMS = 'swap_to_teams';
38
	const CIRCLES_ALLOW_FEDERATED_CIRCLES = 'allow_federated';
39
	const CIRCLES_ALLOW_LINKED_GROUPS = 'allow_linked_groups';
40
	const CIRCLES_ALLOW_NON_SSL_LINKS = 'allow_non_ssl_links';
41
	const CIRCLES_NON_SSL_LOCAL = 'local_is_non_ssl';
42
43
	private $defaults = [
44
		self::CIRCLES_ALLOW_CIRCLES           => Circle::CIRCLES_ALL,
45
		self::CIRCLES_SWAP_TO_TEAMS           => '0',
46
		self::CIRCLES_ALLOW_LINKED_GROUPS     => '0',
47
		self::CIRCLES_ALLOW_FEDERATED_CIRCLES => '0',
48
		self::CIRCLES_ALLOW_NON_SSL_LINKS     => '0',
49
		self::CIRCLES_NON_SSL_LOCAL           => '0'
50
	];
51
52
	/** @var string */
53
	private $appName;
54
55
	/** @var IConfig */
56
	private $config;
57
58
	/** @var string */
59
	private $userId;
60
61
	/** @var IRequest */
62
	private $request;
63
64
	/** @var MiscService */
65
	private $miscService;
66
67
	/** @var int */
68
	private $allowedCircle = -1;
69
70
	/** @var int */
71
	private $allowedLinkedGroups = -1;
72
73
	/** @var int */
74
	private $allowedFederatedCircles = -1;
75
76
	/** @var int */
77
	private $allowedNonSSLLinks = -1;
78
79
	/** @var int */
80
	private $localNonSSL = -1;
81
82
	/**
83
	 * ConfigService constructor.
84
	 *
85
	 * @param string $appName
86
	 * @param IConfig $config
87
	 * @param IRequest $request
88
	 * @param string $UserId
89
	 * @param MiscService $miscService
90
	 */
91
	public function __construct(
0 ignored issues
show
Coding Style Naming introduced by
The parameter $UserId is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
92
		$appName, IConfig $config, IRequest $request, $UserId, MiscService $miscService
93
	) {
94
		$this->appName = $appName;
95
		$this->config = $config;
96
		$this->request = $request;
97
		$this->userId = $UserId;
98
		$this->miscService = $miscService;
99
	}
100
101
102
	public function getLocalAddress() {
103
		return (($this->isLocalNonSSL()) ? 'http://' : '')
104
			   . $this->request->getServerHost();
105
	}
106
107
108
	/**
109
	 * returns if this type of circle is allowed by the current configuration.
110
	 *
111
	 * @param $type
112
	 *
113
	 * @return int
114
	 */
115
	public function isCircleAllowed($type) {
116
		if ($this->allowedCircle === -1) {
117
			$this->allowedCircle = (int)$this->getAppValue(self::CIRCLES_ALLOW_CIRCLES);
118
		}
119
120
		return ((int)$type & (int)$this->allowedCircle);
121
	}
122
123
124
	/**
125
	 * @return bool
126
	 */
127
	public function isLinkedGroupsAllowed() {
128
		if ($this->allowedLinkedGroups === -1) {
129
			$this->allowedLinkedGroups =
130
				(int)$this->getAppValue(self::CIRCLES_ALLOW_LINKED_GROUPS);
131
		}
132
133
		return ($this->allowedLinkedGroups === 1);
134
	}
135
136
137
	/**
138
	 * @return bool
139
	 */
140
	public function isFederatedCirclesAllowed() {
141
		if ($this->allowedFederatedCircles === -1) {
142
			$this->allowedFederatedCircles =
143
				(int)$this->getAppValue(self::CIRCLES_ALLOW_FEDERATED_CIRCLES);
144
		}
145
146
		return ($this->allowedFederatedCircles === 1);
147
	}
148
149
150
	/**
151
	 * @return bool
152
	 */
153
	public function isLocalNonSSL() {
154
		if ($this->localNonSSL === -1) {
155
			$this->localNonSSL =
156
				(int)$this->getAppValue(self::CIRCLES_NON_SSL_LOCAL);
157
		}
158
159
		return ($this->localNonSSL === 1);
160
	}
161
162
163
	/**
164
	 * @return bool
165
	 */
166
	public function isNonSSLLinksAllowed() {
167
		if ($this->allowedNonSSLLinks === -1) {
168
			$this->allowedNonSSLLinks =
169
				(int)$this->getAppValue(self::CIRCLES_ALLOW_NON_SSL_LINKS);
170
		}
171
172
		return ($this->allowedNonSSLLinks === 1);
173
	}
174
175
176
177
178
	/**
179
	 * @param string $remote
180
	 *
181
	 * @return string
182
	 */
183
	public function generateRemoteHost($remote) {
184
		if ((!$this->isNonSSLLinksAllowed() || strpos($remote, 'http://') !== 0)
185
			&& strpos($remote, 'https://') !== 0
186
		) {
187
			$remote = 'https://' . $remote;
188
		}
189
190
		return rtrim($remote, '/');
191
	}
192
193
194
195
196
	/**
197
	 * Get a value by key
198
	 *
199
	 * @param string $key
200
	 *
201
	 * @return string
202
	 */
203
	public function getAppValue($key) {
204
		$defaultValue = null;
205
206
		if (array_key_exists($key, $this->defaults)) {
207
			$defaultValue = $this->defaults[$key];
208
		}
209
210
		return $this->config->getAppValue($this->appName, $key, $defaultValue);
211
	}
212
213
	/**
214
	 * Set a value by key
215
	 *
216
	 * @param string $key
217
	 * @param string $value
218
	 *
219
	 * @return void
220
	 */
221
	public function setAppValue($key, $value) {
222
		$this->config->setAppValue($this->appName, $key, $value);
223
	}
224
225
	/**
226
	 * remove a key
227
	 *
228
	 * @param string $key
229
	 *
230
	 * @return string
231
	 */
232
	public function deleteAppValue($key) {
233
		return $this->config->deleteAppValue($this->appName, $key);
234
	}
235
236
	/**
237
	 * Get a user value by key
238
	 *
239
	 * @param string $key
240
	 *
241
	 * @return string
242
	 */
243
	public function getUserValue($key) {
244
		return $this->config->getUserValue($this->userId, $this->appName, $key);
245
	}
246
247
	/**
248
	 * Set a user value by key
249
	 *
250
	 * @param string $key
251
	 * @param string $value
252
	 *
253
	 * @return string
254
	 */
255
	public function setUserValue($key, $value) {
256
		return $this->config->setUserValue($this->userId, $this->appName, $key, $value);
257
	}
258
259
	/**
260
	 * Get a user value by key and user
261
	 *
262
	 * @param string $userId
263
	 * @param string $key
264
	 *
265
	 * @return string
266
	 */
267
	public function getValueForUser($userId, $key) {
268
		return $this->config->getUserValue($userId, $this->appName, $key);
269
	}
270
271
	/**
272
	 * Set a user value by key
273
	 *
274
	 * @param string $userId
275
	 * @param string $key
276
	 * @param string $value
277
	 *
278
	 * @return string
279
	 */
280
	public function setValueForUser($userId, $key, $value) {
281
		return $this->config->setUserValue($userId, $this->appName, $key, $value);
282
	}
283
284
	/**
285
	 * return the cloud version.
286
	 * if $complete is true, return a string x.y.z
287
	 *
288
	 * @param boolean $complete
289
	 *
290
	 * @return string|integer
291
	 */
292
	public function getCloudVersion($complete = false) {
293
		$ver = Util::getVersion();
294
295
		if ($complete) {
296
			return implode('.', $ver);
297
		}
298
299
		return $ver[0];
300
	}
301
}
302