Passed
Push — master ( ec5d8c...8a3f8b )
by John
27:02 queued 11:27
created

TemplateLayout   C

Complexity

Total Complexity 53

Size/Duplication

Total Lines 339
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 194
c 6
b 0
f 0
dl 0
loc 339
rs 6.96
wmc 53

6 Methods

Rating   Name   Duplication   Size   Complexity  
A convertToRelativePath() 0 7 2
B getVersionHashSuffix() 0 33 10
A getAppNamefromPath() 0 10 4
F __construct() 0 208 34
A findJavascriptFiles() 0 13 1
A findStylesheetFiles() 0 19 2

How to fix   Complexity   

Complex Class

Complex classes like TemplateLayout often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use TemplateLayout, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Bart Visscher <[email protected]>
6
 * @author Christopher Schäpers <[email protected]>
7
 * @author Christoph Wurst <[email protected]>
8
 * @author Clark Tomlinson <[email protected]>
9
 * @author Daniel Calviño Sánchez <[email protected]>
10
 * @author Guillaume COMPAGNON <[email protected]>
11
 * @author Hendrik Leppelsack <[email protected]>
12
 * @author Joas Schilling <[email protected]>
13
 * @author John Molakvoæ <[email protected]>
14
 * @author Jörn Friedrich Dreyer <[email protected]>
15
 * @author Julius Haertl <[email protected]>
16
 * @author Julius Härtl <[email protected]>
17
 * @author Lukas Reschke <[email protected]>
18
 * @author Michael Gapczynski <[email protected]>
19
 * @author Morris Jobke <[email protected]>
20
 * @author Nils <[email protected]>
21
 * @author Remco Brenninkmeijer <[email protected]>
22
 * @author Robin Appelman <[email protected]>
23
 * @author Robin McCorkell <[email protected]>
24
 * @author Roeland Jago Douma <[email protected]>
25
 * @author Thomas Citharel <[email protected]>
26
 * @author Thomas Müller <[email protected]>
27
 *
28
 * @license AGPL-3.0
29
 *
30
 * This code is free software: you can redistribute it and/or modify
31
 * it under the terms of the GNU Affero General Public License, version 3,
32
 * as published by the Free Software Foundation.
33
 *
34
 * This program is distributed in the hope that it will be useful,
35
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37
 * GNU Affero General Public License for more details.
38
 *
39
 * You should have received a copy of the GNU Affero General Public License, version 3,
40
 * along with this program. If not, see <http://www.gnu.org/licenses/>
41
 *
42
 */
43
namespace OC;
44
45
use bantu\IniGetWrapper\IniGetWrapper;
46
use OC\Search\SearchQuery;
47
use OC\Template\JSCombiner;
48
use OC\Template\JSConfigHelper;
49
use OC\Template\SCSSCacher;
50
use OCP\AppFramework\Http\TemplateResponse;
51
use OCP\Defaults;
52
use OCP\IConfig;
53
use OCP\IInitialStateService;
54
use OCP\INavigationManager;
55
use OCP\IUserSession;
56
use OCP\Support\Subscription\IRegistry;
57
use OCP\Util;
58
use Psr\Log\LoggerInterface;
59
60
class TemplateLayout extends \OC_Template {
61
	private static $versionHash = '';
62
63
	/** @var IConfig */
64
	private $config;
65
66
	/** @var IInitialStateService */
67
	private $initialState;
68
69
	/** @var INavigationManager */
70
	private $navigationManager;
71
72
	/**
73
	 * @param string $renderAs
74
	 * @param string $appId application id
75
	 */
76
	public function __construct($renderAs, $appId = '') {
77
78
		/** @var IConfig */
79
		$this->config = \OC::$server->get(IConfig::class);
0 ignored issues
show
Bug introduced by
Accessing config on the interface OCP\IConfig suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
80
81
		/** @var IInitialStateService */
82
		$this->initialState = \OC::$server->get(IInitialStateService::class);
0 ignored issues
show
Bug introduced by
Accessing initialState on the interface OCP\IInitialStateService suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
83
84
		// Add fallback theming variables if theming is disabled
85
		if ($renderAs !== TemplateResponse::RENDER_AS_USER
86
			|| !\OC::$server->getAppManager()->isEnabledForUser('theming')) {
87
			// TODO cache generated default theme if enabled for fallback if server is erroring ?
88
			Util::addStyle('theming', 'default');
89
		}
90
91
		// Decide which page we show
92
		if ($renderAs === TemplateResponse::RENDER_AS_USER) {
93
			/** @var INavigationManager */
94
			$this->navigationManager = \OC::$server->get(INavigationManager::class);
0 ignored issues
show
Bug introduced by
Accessing navigationManager on the interface OCP\INavigationManager suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
95
96
			parent::__construct('core', 'layout.user');
97
			if (in_array(\OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) {
98
				$this->assign('bodyid', 'body-settings');
99
			} else {
100
				$this->assign('bodyid', 'body-user');
101
			}
102
103
			$this->initialState->provideInitialState('core', 'active-app', $this->navigationManager->getActiveEntry());
104
			$this->initialState->provideInitialState('unified-search', 'limit-default', (int)$this->config->getAppValue('core', 'unified-search.limit-default', (string)SearchQuery::LIMIT_DEFAULT));
105
			$this->initialState->provideInitialState('unified-search', 'min-search-length', (int)$this->config->getAppValue('core', 'unified-search.min-search-length', (string)2));
106
			$this->initialState->provideInitialState('unified-search', 'live-search', $this->config->getAppValue('core', 'unified-search.live-search', 'yes') === 'yes');
107
			Util::addScript('core', 'unified-search', 'core');
108
109
			// Set body data-theme
110
			if (\OC::$server->getAppManager()->isEnabledForUser('theming') && class_exists('\OCA\Theming\Service\ThemesService')) {
111
				/** @var \OCA\Theming\Service\ThemesService */
112
				$themesService = \OC::$server->get(\OCA\Theming\Service\ThemesService::class);
113
				$this->assign('enabledThemes', $themesService->getEnabledThemes());
114
			}
115
116
			// set logo link target
117
			$logoUrl = $this->config->getSystemValueString('logo_url', '');
118
			$this->assign('logoUrl', $logoUrl);
119
120
			// Add navigation entry
121
			$this->assign('application', '');
122
			$this->assign('appid', $appId);
123
124
			$navigation = $this->navigationManager->getAll();
125
			$this->assign('navigation', $navigation);
126
			$settingsNavigation = $this->navigationManager->getAll('settings');
127
			$this->assign('settingsnavigation', $settingsNavigation);
128
129
			foreach ($navigation as $entry) {
130
				if ($entry['active']) {
131
					$this->assign('application', $entry['name']);
132
					break;
133
				}
134
			}
135
136
			foreach ($settingsNavigation as $entry) {
137
				if ($entry['active']) {
138
					$this->assign('application', $entry['name']);
139
					break;
140
				}
141
			}
142
143
			$userDisplayName = false;
144
			$user = \OC::$server->get(IUserSession::class)->getUser();
145
			if ($user) {
146
				$userDisplayName = $user->getDisplayName();
147
			}
148
			$this->assign('user_displayname', $userDisplayName);
149
			$this->assign('user_uid', \OC_User::getUser());
150
151
			if ($user === null) {
152
				$this->assign('userAvatarSet', false);
153
				$this->assign('userStatus', false);
154
			} else {
155
				$this->assign('userAvatarSet', true);
156
				$this->assign('userAvatarVersion', $this->config->getUserValue(\OC_User::getUser(), 'avatar', 'version', 0));
157
			}
158
		} elseif ($renderAs === TemplateResponse::RENDER_AS_ERROR) {
159
			parent::__construct('core', 'layout.guest', '', false);
160
			$this->assign('bodyid', 'body-login');
161
			$this->assign('user_displayname', '');
162
			$this->assign('user_uid', '');
163
		} elseif ($renderAs === TemplateResponse::RENDER_AS_GUEST) {
164
			parent::__construct('core', 'layout.guest');
165
			\OC_Util::addStyle('guest');
166
			$this->assign('bodyid', 'body-login');
167
168
			$userDisplayName = false;
169
			$user = \OC::$server->get(IUserSession::class)->getUser();
170
			if ($user) {
171
				$userDisplayName = $user->getDisplayName();
172
			}
173
			$this->assign('user_displayname', $userDisplayName);
174
			$this->assign('user_uid', \OC_User::getUser());
175
		} elseif ($renderAs === TemplateResponse::RENDER_AS_PUBLIC) {
176
			parent::__construct('core', 'layout.public');
177
			$this->assign('appid', $appId);
178
			$this->assign('bodyid', 'body-public');
179
180
			/** @var IRegistry $subscription */
181
			$subscription = \OC::$server->query(IRegistry::class);
182
			$showSimpleSignup = $this->config->getSystemValueBool('simpleSignUpLink.shown', true);
183
			if ($showSimpleSignup && $subscription->delegateHasValidSubscription()) {
184
				$showSimpleSignup = false;
185
			}
186
			$this->assign('showSimpleSignUpLink', $showSimpleSignup);
187
		} else {
188
			parent::__construct('core', 'layout.base');
189
		}
190
		// Send the language and the locale to our layouts
191
		$lang = \OC::$server->getL10NFactory()->findLanguage();
192
		$locale = \OC::$server->getL10NFactory()->findLocale($lang);
193
194
		$lang = str_replace('_', '-', $lang);
195
		$this->assign('language', $lang);
196
		$this->assign('locale', $locale);
197
198
		if (\OC::$server->getSystemConfig()->getValue('installed', false)) {
199
			if (empty(self::$versionHash)) {
200
				$v = \OC_App::getAppVersions();
201
				$v['core'] = implode('.', \OCP\Util::getVersion());
202
				self::$versionHash = substr(md5(implode(',', $v)), 0, 8);
203
			}
204
		} else {
205
			self::$versionHash = md5('not installed');
206
		}
207
208
		// Add the js files
209
		// TODO: remove deprecated OC_Util injection
210
		$jsFiles = self::findJavascriptFiles(array_merge(\OC_Util::$scripts, Util::getScripts()));
211
		$this->assign('jsfiles', []);
212
		if ($this->config->getSystemValue('installed', false) && $renderAs != TemplateResponse::RENDER_AS_ERROR) {
213
			// this is on purpose outside of the if statement below so that the initial state is prefilled (done in the getConfig() call)
214
			// see https://github.com/nextcloud/server/pull/22636 for details
215
			$jsConfigHelper = new JSConfigHelper(
216
				\OC::$server->getL10N('lib'),
217
				\OC::$server->query(Defaults::class),
218
				\OC::$server->getAppManager(),
219
				\OC::$server->getSession(),
220
				\OC::$server->getUserSession()->getUser(),
221
				$this->config,
222
				\OC::$server->getGroupManager(),
223
				\OC::$server->get(IniGetWrapper::class),
224
				\OC::$server->getURLGenerator(),
225
				\OC::$server->getCapabilitiesManager(),
226
				\OC::$server->query(IInitialStateService::class)
227
			);
228
			$config = $jsConfigHelper->getConfig();
229
			if (\OC::$server->getContentSecurityPolicyNonceManager()->browserSupportsCspV3()) {
230
				$this->assign('inline_ocjs', $config);
231
			} else {
232
				$this->append('jsfiles', \OC::$server->getURLGenerator()->linkToRoute('core.OCJS.getConfig', ['v' => self::$versionHash]));
233
			}
234
		}
235
		foreach ($jsFiles as $info) {
236
			$web = $info[1];
237
			$file = $info[2];
238
			$this->append('jsfiles', $web.'/'.$file . $this->getVersionHashSuffix());
239
		}
240
241
		try {
242
			$pathInfo = \OC::$server->getRequest()->getPathInfo();
243
		} catch (\Exception $e) {
244
			$pathInfo = '';
245
		}
246
247
		// Do not initialise scss appdata until we have a fully installed instance
248
		// Do not load scss for update, errors, installation or login page
249
		if (\OC::$server->getSystemConfig()->getValue('installed', false)
250
			&& !\OCP\Util::needUpgrade()
251
			&& $pathInfo !== ''
252
			&& !preg_match('/^\/login/', $pathInfo)
253
			&& $renderAs !== TemplateResponse::RENDER_AS_ERROR
254
		) {
255
			$cssFiles = self::findStylesheetFiles(\OC_Util::$styles);
256
		} else {
257
			// If we ignore the scss compiler,
258
			// we need to load the guest css fallback
259
			\OC_Util::addStyle('guest');
260
			$cssFiles = self::findStylesheetFiles(\OC_Util::$styles, false);
261
		}
262
263
		$this->assign('cssfiles', []);
264
		$this->assign('printcssfiles', []);
265
		$this->assign('versionHash', self::$versionHash);
266
		foreach ($cssFiles as $info) {
267
			$web = $info[1];
268
			$file = $info[2];
269
270
			if (substr($file, -strlen('print.css')) === 'print.css') {
271
				$this->append('printcssfiles', $web.'/'.$file . $this->getVersionHashSuffix());
272
			} else {
273
				$suffix = $this->getVersionHashSuffix($web, $file);
274
275
				if (strpos($file, '?v=') == false) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strpos($file, '?v=') of type integer to the boolean false. If you are specifically checking for 0, consider using something more explicit like === 0 instead.
Loading history...
276
					$this->append('cssfiles', $web.'/'.$file . $suffix);
277
				} else {
278
					$this->append('cssfiles', $web.'/'.$file . '-' . substr($suffix, 3));
279
				}
280
			}
281
		}
282
283
		$this->assign('initialStates', $this->initialState->getInitialStates());
284
	}
285
286
	/**
287
	 * @param string $path
288
	 * @param string $file
289
	 * @return string
290
	 */
291
	protected function getVersionHashSuffix($path = false, $file = false) {
292
		if ($this->config->getSystemValue('debug', false)) {
293
			// allows chrome workspace mapping in debug mode
294
			return "";
295
		}
296
		$themingSuffix = '';
297
		$v = [];
298
299
		if ($this->config->getSystemValue('installed', false)) {
300
			if (\OC::$server->getAppManager()->isInstalled('theming')) {
301
				$themingSuffix = '-' . $this->config->getAppValue('theming', 'cachebuster', '0');
302
			}
303
			$v = \OC_App::getAppVersions();
304
		}
305
306
		// Try the webroot path for a match
307
		if ($path !== false && $path !== '') {
308
			$appName = $this->getAppNamefromPath($path);
309
			if (array_key_exists($appName, $v)) {
310
				$appVersion = $v[$appName];
311
				return '?v=' . substr(md5($appVersion), 0, 8) . $themingSuffix;
312
			}
313
		}
314
		// fallback to the file path instead
315
		if ($file !== false && $file !== '') {
316
			$appName = $this->getAppNamefromPath($file);
317
			if (array_key_exists($appName, $v)) {
318
				$appVersion = $v[$appName];
319
				return '?v=' . substr(md5($appVersion), 0, 8) . $themingSuffix;
320
			}
321
		}
322
323
		return '?v=' . self::$versionHash . $themingSuffix;
324
	}
325
326
	/**
327
	 * @param array $styles
328
	 * @return array
329
	 */
330
	public static function findStylesheetFiles($styles, $compileScss = true) {
331
		// Read the selected theme from the config file
332
		$theme = \OC_Util::getTheme();
333
334
		if ($compileScss) {
335
			$SCSSCacher = \OC::$server->query(SCSSCacher::class);
336
		} else {
337
			$SCSSCacher = null;
338
		}
339
340
		$locator = new \OC\Template\CSSResourceLocator(
341
			\OC::$server->get(LoggerInterface::class),
342
			$theme,
343
			[ \OC::$SERVERROOT => \OC::$WEBROOT ],
344
			[ \OC::$SERVERROOT => \OC::$WEBROOT ],
345
			$SCSSCacher
346
		);
347
		$locator->find($styles);
348
		return $locator->getResources();
349
	}
350
351
	/**
352
	 * @param string $path
353
	 * @return string|boolean
354
	 */
355
	public function getAppNamefromPath($path) {
356
		if ($path !== '' && is_string($path)) {
357
			$pathParts = explode('/', $path);
358
			if ($pathParts[0] === 'css') {
359
				// This is a scss request
360
				return $pathParts[1];
361
			}
362
			return end($pathParts);
363
		}
364
		return false;
365
	}
366
367
	/**
368
	 * @param array $scripts
369
	 * @return array
370
	 */
371
	public static function findJavascriptFiles($scripts) {
372
		// Read the selected theme from the config file
373
		$theme = \OC_Util::getTheme();
374
375
		$locator = new \OC\Template\JSResourceLocator(
376
			\OC::$server->get(LoggerInterface::class),
377
			$theme,
378
			[ \OC::$SERVERROOT => \OC::$WEBROOT ],
379
			[ \OC::$SERVERROOT => \OC::$WEBROOT ],
380
			\OC::$server->query(JSCombiner::class)
381
			);
382
		$locator->find($scripts);
383
		return $locator->getResources();
384
	}
385
386
	/**
387
	 * Converts the absolute file path to a relative path from \OC::$SERVERROOT
388
	 * @param string $filePath Absolute path
389
	 * @return string Relative path
390
	 * @throws \Exception If $filePath is not under \OC::$SERVERROOT
391
	 */
392
	public static function convertToRelativePath($filePath) {
393
		$relativePath = explode(\OC::$SERVERROOT, $filePath);
394
		if (count($relativePath) !== 2) {
395
			throw new \Exception('$filePath is not under the \OC::$SERVERROOT');
396
		}
397
398
		return $relativePath[1];
399
	}
400
}
401