Completed
Push — master ( bcf587...5a9224 )
by Morris
19:50 queued 04:43
created

TemplateLayout::findStylesheetFiles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 14
nc 2
nop 2
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
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 Clark Tomlinson <[email protected]>
8
 * @author Hendrik Leppelsack <[email protected]>
9
 * @author Joas Schilling <[email protected]>
10
 * @author Jörn Friedrich Dreyer <[email protected]>
11
 * @author Lukas Reschke <[email protected]>
12
 * @author Michael Gapczynski <[email protected]>
13
 * @author Morris Jobke <[email protected]>
14
 * @author Remco Brenninkmeijer <[email protected]>
15
 * @author Robin Appelman <[email protected]>
16
 * @author Robin McCorkell <[email protected]>
17
 * @author Roeland Jago Douma <[email protected]>
18
 * @author Thomas Müller <[email protected]>
19
 * @author Victor Dubiniuk <[email protected]>
20
 *
21
 * @license AGPL-3.0
22
 *
23
 * This code is free software: you can redistribute it and/or modify
24
 * it under the terms of the GNU Affero General Public License, version 3,
25
 * as published by the Free Software Foundation.
26
 *
27
 * This program is distributed in the hope that it will be useful,
28
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30
 * GNU Affero General Public License for more details.
31
 *
32
 * You should have received a copy of the GNU Affero General Public License, version 3,
33
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
34
 *
35
 */
36
namespace OC;
37
38
use OC\Template\JSCombiner;
39
use OC\Template\JSConfigHelper;
40
use OC\Template\SCSSCacher;
41
use OCP\Defaults;
42
43
class TemplateLayout extends \OC_Template {
44
45
	private static $versionHash = '';
46
47
	/**
48
	 * @var \OCP\IConfig
49
	 */
50
	private $config;
51
52
	/**
53
	 * @param string $renderAs
54
	 * @param string $appId application id
55
	 */
56
	public function __construct( $renderAs, $appId = '' ) {
57
58
		// yes - should be injected ....
59
		$this->config = \OC::$server->getConfig();
60
61
62
		// Decide which page we show
63
		if($renderAs == 'user') {
64
			parent::__construct( 'core', 'layout.user' );
65
			if(in_array(\OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) {
66
				$this->assign('bodyid', 'body-settings');
67
			}else{
68
				$this->assign('bodyid', 'body-user');
69
			}
70
71
			// Code integrity notification
72
			$integrityChecker = \OC::$server->getIntegrityCodeChecker();
73
			if(\OC_User::isAdminUser(\OC_User::getUser()) && $integrityChecker->isCodeCheckEnforced() && !$integrityChecker->hasPassedCheck()) {
74
				\OCP\Util::addScript('core', 'integritycheck-failed-notification');
75
			}
76
77
			// Add navigation entry
78
			$this->assign( 'application', '');
79
			$this->assign( 'appid', $appId );
80
			$navigation = \OC_App::getNavigation();
81
			$this->assign( 'navigation', $navigation);
82
			$navigation = \OC_App::getHeaderNavigation();
83
			$this->assign( 'headernavigation', $navigation);
84
			$settingsNavigation = \OC_App::getSettingsNavigation();
85
			$this->assign( 'settingsnavigation', $settingsNavigation);
86
			foreach($navigation as $entry) {
87
				if ($entry['active']) {
88
					$this->assign( 'application', $entry['name'] );
89
					break;
90
				}
91
			}
92
			
93
			foreach($settingsNavigation as $entry) {
94
				if ($entry['active']) {
95
					$this->assign( 'application', $entry['name'] );
96
					break;
97
				}
98
			}
99
			$userDisplayName = \OC_User::getDisplayName();
100
			$this->assign('user_displayname', $userDisplayName);
101
			$this->assign('user_uid', \OC_User::getUser());
102
103
			if (\OC_User::getUser() === false) {
104
				$this->assign('userAvatarSet', false);
105
			} else {
106
				$this->assign('userAvatarSet', \OC::$server->getAvatarManager()->getAvatar(\OC_User::getUser())->exists());
107
				$this->assign('userAvatarVersion', \OC::$server->getConfig()->getUserValue(\OC_User::getUser(), 'avatar', 'version', 0));
108
			}
109
110
		} else if ($renderAs == 'error') {
111
			parent::__construct('core', 'layout.guest', '', false);
112
			$this->assign('bodyid', 'body-login');
113
		} else if ($renderAs == 'guest') {
114
			parent::__construct('core', 'layout.guest');
115
			$this->assign('bodyid', 'body-login');
116
		} else {
117
			parent::__construct('core', 'layout.base');
118
119
		}
120
		// Send the language to our layouts
121
		$this->assign('language', \OC::$server->getL10NFactory()->findLanguage());
122
123
		if(\OC::$server->getSystemConfig()->getValue('installed', false)) {
124
			if (empty(self::$versionHash)) {
125
				$v = \OC_App::getAppVersions();
126
				$v['core'] = implode('.', \OCP\Util::getVersion());
127
				self::$versionHash = md5(implode(',', $v));
128
			}
129
		} else {
130
			self::$versionHash = md5('not installed');
131
		}
132
133
		// Add the js files
134
		$jsFiles = self::findJavascriptFiles(\OC_Util::$scripts);
135
		$this->assign('jsfiles', array());
136
		if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') {
137
			if (\OC::$server->getContentSecurityPolicyNonceManager()->browserSupportsCspV3()) {
138
				$jsConfigHelper = new JSConfigHelper(
139
					\OC::$server->getL10N('core'),
140
					\OC::$server->query(Defaults::class),
141
					\OC::$server->getAppManager(),
142
					\OC::$server->getSession(),
143
					\OC::$server->getUserSession()->getUser(),
144
					\OC::$server->getConfig(),
145
					\OC::$server->getGroupManager(),
146
					\OC::$server->getIniWrapper(),
147
					\OC::$server->getURLGenerator()
148
				);
149
				$this->assign('inline_ocjs', $jsConfigHelper->getConfig());
150
			} else {
151
				$this->append('jsfiles', \OC::$server->getURLGenerator()->linkToRoute('core.OCJS.getConfig', ['v' => self::$versionHash]));
152
			}
153
		}
154
		foreach($jsFiles as $info) {
155
			$web = $info[1];
156
			$file = $info[2];
157
			$this->append( 'jsfiles', $web.'/'.$file . $this->getVersionHashSuffix() );
158
		}
159
160
		try {
161
			$pathInfo = \OC::$server->getRequest()->getPathInfo();
162
		} catch (\Exception $e) {
163
			$pathInfo = '';
164
		}
165
166
		// Do not initialise scss appdata until we have a fully installed instance
167
		// Do not load scss for update, errors, installation or login page
168
		if(\OC::$server->getSystemConfig()->getValue('installed', false)
169
			&& !\OCP\Util::needUpgrade()
170
			&& $pathInfo !== ''
171
			&& !preg_match('/^\/login/', $pathInfo)) {
172
			$cssFiles = self::findStylesheetFiles(\OC_Util::$styles);
173
		} else {
174
			// If we ignore the scss compiler,
175
			// we need to load the guest css fallback
176
			\OC_Util::addStyle('guest');
177
			$cssFiles = self::findStylesheetFiles(\OC_Util::$styles, false);
178
		}
179
180
		$this->assign('cssfiles', array());
181
		$this->assign('printcssfiles', []);
182
		$this->assign('versionHash', self::$versionHash);
183
		foreach($cssFiles as $info) {
184
			$web = $info[1];
185
			$file = $info[2];
186
187
			if (substr($file, -strlen('print.css')) === 'print.css') {
188
				$this->append( 'printcssfiles', $web.'/'.$file . $this->getVersionHashSuffix() );
189
			} else {
190
				$this->append( 'cssfiles', $web.'/'.$file . $this->getVersionHashSuffix()  );
191
			}
192
		}
193
	}
194
195
	protected function getVersionHashSuffix() {
196
		if(\OC::$server->getConfig()->getSystemValue('debug', false)) {
197
			// allows chrome workspace mapping in debug mode
198
			return "";
199
		}
200
		if ($this->config->getSystemValue('installed', false) && \OC::$server->getAppManager()->isInstalled('theming')) {
201
			return '?v=' . self::$versionHash . '-' . $this->config->getAppValue('theming', 'cachebuster', '0');
202
		}
203
		return '?v=' . self::$versionHash;
204
	}
205
206
	/**
207
	 * @param array $styles
208
	 * @return array
209
	 */
210
	static public function findStylesheetFiles($styles, $compileScss = true) {
211
		// Read the selected theme from the config file
212
		$theme = \OC_Util::getTheme();
213
214
		if($compileScss) {
215
			$SCSSCacher = \OC::$server->query(SCSSCacher::class);
216
		} else {
217
			$SCSSCacher = null;
218
		}
219
220
		$locator = new \OC\Template\CSSResourceLocator(
221
			\OC::$server->getLogger(),
222
			$theme,
223
			array( \OC::$SERVERROOT => \OC::$WEBROOT ),
224
			array( \OC::$SERVERROOT => \OC::$WEBROOT ),
225
			$SCSSCacher
226
		);
227
		$locator->find($styles);
228
		return $locator->getResources();
229
	}
230
231
	/**
232
	 * @param array $scripts
233
	 * @return array
234
	 */
235
	static public function findJavascriptFiles($scripts) {
236
		// Read the selected theme from the config file
237
		$theme = \OC_Util::getTheme();
238
239
		$locator = new \OC\Template\JSResourceLocator(
240
			\OC::$server->getLogger(),
241
			$theme,
242
			array( \OC::$SERVERROOT => \OC::$WEBROOT ),
243
			array( \OC::$SERVERROOT => \OC::$WEBROOT ),
244
			new JSCombiner(
245
				\OC::$server->getAppDataDir('js'),
246
				\OC::$server->getURLGenerator(),
247
				\OC::$server->getMemCacheFactory()->create('JS'),
248
				\OC::$server->getSystemConfig()
249
			)
250
			);
251
		$locator->find($scripts);
252
		return $locator->getResources();
253
	}
254
255
	/**
256
	 * Converts the absolute file path to a relative path from \OC::$SERVERROOT
257
	 * @param string $filePath Absolute path
258
	 * @return string Relative path
259
	 * @throws \Exception If $filePath is not under \OC::$SERVERROOT
260
	 */
261
	public static function convertToRelativePath($filePath) {
262
		$relativePath = explode(\OC::$SERVERROOT, $filePath);
263
		if(count($relativePath) !== 2) {
264
			throw new \Exception('$filePath is not under the \OC::$SERVERROOT');
265
		}
266
267
		return $relativePath[1];
268
	}
269
}
270