Completed
Pull Request — master (#1917)
by Lukas
40:33 queued 30:26
created

JSConfigHelper   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 211
Duplicated Lines 8.06 %

Coupling/Cohesion

Components 1
Dependencies 15

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
lcom 1
cbo 15
dl 17
loc 211
rs 9.1666
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 17 17 1
F getConfig() 0 156 12

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, Roeland Jago Douma <[email protected]>
4
 *
5
 * @author Roeland Jago Douma <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
namespace OC\Template;
24
25
use bantu\IniGetWrapper\IniGetWrapper;
26
use OCP\App\IAppManager;
27
use OCP\IConfig;
28
use OCP\IGroupManager;
29
use OCP\IL10N;
30
use OCP\IURLGenerator;
31
use OCP\IUser;
32
33
class JSConfigHelper {
34
35
	/** @var IL10N */
36
	private $l;
37
38
	/** @var \OC_Defaults */
39
	private $defaults;
40
41
	/** @var IAppManager */
42
	private $appManager;
43
44
	/** @var IUser */
45
	private $currentUser;
46
47
	/** @var IConfig */
48
	private $config;
49
50
	/** @var IGroupManager */
51
	private $groupManager;
52
53
	/** @var IniGetWrapper */
54
	private $iniWrapper;
55
56
	/** @var IURLGenerator */
57
	private $urlGenerator;
58
59
	/**
60
	 * @param IL10N $l
61
	 * @param \OC_Defaults $defaults
62
	 * @param IAppManager $appManager
63
	 * @param IUser|null $currentUser
64
	 * @param IConfig $config
65
	 * @param IGroupManager $groupManager
66
	 * @param IniGetWrapper $iniWrapper
67
	 * @param IURLGenerator $urlGenerator
68
	 */
69 View Code Duplication
	public function __construct(IL10N $l,
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
								\OC_Defaults $defaults,
71
								IAppManager $appManager,
72
								$currentUser,
73
								IConfig $config,
74
								IGroupManager $groupManager,
75
								IniGetWrapper $iniWrapper,
76
								IURLGenerator $urlGenerator) {
77
		$this->l = $l;
78
		$this->defaults = $defaults;
79
		$this->appManager = $appManager;
80
		$this->currentUser = $currentUser;
81
		$this->config = $config;
82
		$this->groupManager = $groupManager;
83
		$this->iniWrapper = $iniWrapper;
84
		$this->urlGenerator = $urlGenerator;
85
	}
86
87
	public function getConfig() {
88
89
		if ($this->currentUser !== null) {
90
			$uid = $this->currentUser->getUID();
91
		} else {
92
			$uid = null;
93
		}
94
95
		// Get the config
96
		$apps_paths = [];
97
98
		if ($this->currentUser === null) {
99
			$apps = $this->appManager->getInstalledApps();
100
		} else {
101
			$apps = $this->appManager->getEnabledAppsForUser($this->currentUser);
102
		}
103
104
		foreach($apps as $app) {
105
			$apps_paths[$app] = \OC_App::getAppWebPath($app);
106
		}
107
108
		$defaultExpireDateEnabled = $this->config->getAppValue('core', 'shareapi_default_expire_date', 'no') === 'yes';
109
		$defaultExpireDate = $enforceDefaultExpireDate = null;
110
		if ($defaultExpireDateEnabled) {
111
			$defaultExpireDate = (int) $this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7');
112
			$enforceDefaultExpireDate = $this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'no') === 'yes';
113
		}
114
		$outgoingServer2serverShareEnabled = $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') === 'yes';
115
116
		$countOfDataLocation = 0;
117
		$dataLocation = str_replace(\OC::$SERVERROOT .'/', '', $this->config->getSystemValue('datadirectory', ''), $countOfDataLocation);
118
		if($countOfDataLocation !== 1 || !$this->groupManager->isAdmin($uid)) {
119
			$dataLocation = false;
120
		}
121
122
		$array = [
123
			"oc_debug" => $this->config->getSystemValue('debug', false) ? 'true' : 'false',
124
			"oc_isadmin" => $this->groupManager->isAdmin($uid) ? 'true' : 'false',
125
			"oc_dataURL" => is_string($dataLocation) ? "\"".$dataLocation."\"" : 'false',
126
			"oc_webroot" => "\"".\OC::$WEBROOT."\"",
127
			"oc_appswebroots" =>  str_replace('\\/', '/', json_encode($apps_paths)), // Ugly unescape slashes waiting for better solution
128
			"datepickerFormatDate" => json_encode($this->l->l('jsdate', null)),
129
			"dayNames" =>  json_encode([
130
				(string)$this->l->t('Sunday'),
131
				(string)$this->l->t('Monday'),
132
				(string)$this->l->t('Tuesday'),
133
				(string)$this->l->t('Wednesday'),
134
				(string)$this->l->t('Thursday'),
135
				(string)$this->l->t('Friday'),
136
				(string)$this->l->t('Saturday')
137
			]),
138
			"dayNamesShort" =>  json_encode([
139
				(string)$this->l->t('Sun.'),
140
				(string)$this->l->t('Mon.'),
141
				(string)$this->l->t('Tue.'),
142
				(string)$this->l->t('Wed.'),
143
				(string)$this->l->t('Thu.'),
144
				(string)$this->l->t('Fri.'),
145
				(string)$this->l->t('Sat.')
146
			]),
147
			"dayNamesMin" =>  json_encode([
148
				(string)$this->l->t('Su'),
149
				(string)$this->l->t('Mo'),
150
				(string)$this->l->t('Tu'),
151
				(string)$this->l->t('We'),
152
				(string)$this->l->t('Th'),
153
				(string)$this->l->t('Fr'),
154
				(string)$this->l->t('Sa')
155
			]),
156
			"monthNames" => json_encode([
157
				(string)$this->l->t('January'),
158
				(string)$this->l->t('February'),
159
				(string)$this->l->t('March'),
160
				(string)$this->l->t('April'),
161
				(string)$this->l->t('May'),
162
				(string)$this->l->t('June'),
163
				(string)$this->l->t('July'),
164
				(string)$this->l->t('August'),
165
				(string)$this->l->t('September'),
166
				(string)$this->l->t('October'),
167
				(string)$this->l->t('November'),
168
				(string)$this->l->t('December')
169
			]),
170
			"monthNamesShort" => json_encode([
171
				(string)$this->l->t('Jan.'),
172
				(string)$this->l->t('Feb.'),
173
				(string)$this->l->t('Mar.'),
174
				(string)$this->l->t('Apr.'),
175
				(string)$this->l->t('May.'),
176
				(string)$this->l->t('Jun.'),
177
				(string)$this->l->t('Jul.'),
178
				(string)$this->l->t('Aug.'),
179
				(string)$this->l->t('Sep.'),
180
				(string)$this->l->t('Oct.'),
181
				(string)$this->l->t('Nov.'),
182
				(string)$this->l->t('Dec.')
183
			]),
184
			"firstDay" => json_encode($this->l->l('firstday', null)) ,
185
			"oc_config" => json_encode([
186
				'session_lifetime'	=> min($this->config->getSystemValue('session_lifetime', $this->iniWrapper->getNumeric('session.gc_maxlifetime')), $this->iniWrapper->getNumeric('session.gc_maxlifetime')),
187
				'session_keepalive'	=> $this->config->getSystemValue('session_keepalive', true),
188
				'version'			=> implode('.', \OCP\Util::getVersion()),
189
				'versionstring'		=> \OC_Util::getVersionString(),
190
				'enable_avatars'	=> $this->config->getSystemValue('enable_avatars', true) === true,
191
				'lost_password_link'=> $this->config->getSystemValue('lost_password_link', null),
192
				'modRewriteWorking'	=> (getenv('front_controller_active') === 'true'),
193
			]),
194
			"oc_appconfig" => json_encode([
195
				'core' => [
196
					'defaultExpireDateEnabled' => $defaultExpireDateEnabled,
197
					'defaultExpireDate' => $defaultExpireDate,
198
					'defaultExpireDateEnforced' => $enforceDefaultExpireDate,
199
					'enforcePasswordForPublicLink' => \OCP\Util::isPublicLinkPasswordRequired(),
200
					'sharingDisabledForUser' => \OCP\Util::isSharingDisabledForUser(),
0 ignored issues
show
Deprecated Code introduced by
The method OCP\Util::isSharingDisabledForUser() has been deprecated with message: 9.1.0 Use \OC::$server->getShareManager()->sharingDisabledForUser

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
201
					'resharingAllowed' => \OCP\Share::isResharingAllowed(),
202
					'remoteShareAllowed' => $outgoingServer2serverShareEnabled,
203
					'federatedCloudShareDoc' => $this->urlGenerator->linkToDocs('user-sharing-federated'),
204
					'allowGroupSharing' => \OC::$server->getShareManager()->allowGroupSharing()
205
				]
206
			]),
207
			"oc_defaults" => json_encode([
208
				'entity' => $this->defaults->getEntity(),
209
				'name' => $this->defaults->getName(),
210
				'title' => $this->defaults->getTitle(),
211
				'baseUrl' => $this->defaults->getBaseUrl(),
212
				'syncClientUrl' => $this->defaults->getSyncClientUrl(),
213
				'docBaseUrl' => $this->defaults->getDocBaseUrl(),
214
				'docPlaceholderUrl' => $this->defaults->buildDocLinkToKey('PLACEHOLDER'),
215
				'slogan' => $this->defaults->getSlogan(),
216
				'logoClaim' => $this->defaults->getLogoClaim(),
217
				'shortFooter' => $this->defaults->getShortFooter(),
218
				'longFooter' => $this->defaults->getLongFooter(),
219
				'folder' => \OC_Util::getTheme(),
220
			]),
221
		];
222
223
		if ($this->currentUser !== null) {
224
			$array['oc_userconfig'] = json_encode([
225
				'avatar' => [
226
					'version' => (int)$this->config->getUserValue($uid, 'avatar', 'version', 0),
227
				]
228
			]);
229
		}
230
231
		// Allow hooks to modify the output values
232
		\OC_Hook::emit('\OCP\Config', 'js', array('array' => &$array));
233
234
		$result = '';
235
236
		// Echo it
237
		foreach ($array as  $setting => $value) {
238
			$result .= 'var '. $setting . '='. $value . ';' . PHP_EOL;
239
		}
240
241
		return $result;
242
	}
243
}
244