Completed
Push — master ( 012e52...bde115 )
by Lukas
06:59
created

TemplateLayout::findJavascriptFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 12
rs 9.4285
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\JSConfigHelper;
39
use OC\Template\SCSSCacher;
40
41
class TemplateLayout extends \OC_Template {
42
43
	private static $versionHash = '';
44
45
	/**
46
	 * @var \OCP\IConfig
47
	 */
48
	private $config;
49
50
	/**
51
	 * @param string $renderAs
52
	 * @param string $appId application id
53
	 */
54
	public function __construct( $renderAs, $appId = '' ) {
55
56
		// yes - should be injected ....
57
		$this->config = \OC::$server->getConfig();
58
59
		// Decide which page we show
60
		if($renderAs == 'user') {
61
			parent::__construct( 'core', 'layout.user' );
62
			if(in_array(\OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) {
63
				$this->assign('bodyid', 'body-settings');
64
			}else{
65
				$this->assign('bodyid', 'body-user');
66
			}
67
68
			// Code integrity notification
69
			$integrityChecker = \OC::$server->getIntegrityCodeChecker();
70
			if(\OC_User::isAdminUser(\OC_User::getUser()) && $integrityChecker->isCodeCheckEnforced() && !$integrityChecker->hasPassedCheck()) {
71
				\OCP\Util::addScript('core', 'integritycheck-failed-notification');
72
			}
73
74
			// Add navigation entry
75
			$this->assign( 'application', '');
76
			$this->assign( 'appid', $appId );
77
			$navigation = \OC_App::getNavigation();
78
			$this->assign( 'navigation', $navigation);
79
			$settingsNavigation = \OC_App::getSettingsNavigation();
80
			$this->assign( 'settingsnavigation', $settingsNavigation);
81
			foreach($navigation as $entry) {
82
				if ($entry['active']) {
83
					$this->assign( 'application', $entry['name'] );
84
					break;
85
				}
86
			}
87
			
88
			foreach($settingsNavigation as $entry) {
89
				if ($entry['active']) {
90
					$this->assign( 'application', $entry['name'] );
91
					break;
92
				}
93
			}
94
			$userDisplayName = \OC_User::getDisplayName();
95
			$appsMgmtActive = strpos(\OC::$server->getRequest()->getRequestUri(), \OC::$server->getURLGenerator()->linkToRoute('settings.AppSettings.viewApps')) === 0;
96
			if ($appsMgmtActive) {
97
				$l = \OC::$server->getL10N('lib');
98
				$this->assign('application', $l->t('Apps'));
99
			}
100
			$this->assign('user_displayname', $userDisplayName);
101
			$this->assign('user_uid', \OC_User::getUser());
102
			$this->assign('appsmanagement_active', $appsMgmtActive);
103
			$this->assign('enableAvatars', $this->config->getSystemValue('enable_avatars', true) === true);
104
105
			if (\OC_User::getUser() === false) {
106
				$this->assign('userAvatarSet', false);
107
			} else {
108
				$this->assign('userAvatarSet', \OC::$server->getAvatarManager()->getAvatar(\OC_User::getUser())->exists());
109
				$this->assign('userAvatarVersion', \OC::$server->getConfig()->getUserValue(\OC_User::getUser(), 'avatar', 'version', 0));
110
			}
111
112
		} else if ($renderAs == 'error') {
113
			parent::__construct('core', 'layout.guest', '', false);
114
			$this->assign('bodyid', 'body-login');
115
		} else if ($renderAs == 'guest') {
116
			parent::__construct('core', 'layout.guest');
117
			$this->assign('bodyid', 'body-login');
118
		} else {
119
			parent::__construct('core', 'layout.base');
120
121
		}
122
		// Send the language to our layouts
123
		$this->assign('language', \OC::$server->getL10NFactory()->findLanguage());
124
125
		if(\OC::$server->getSystemConfig()->getValue('installed', false)) {
126
			if (empty(self::$versionHash)) {
127
				$v = \OC_App::getAppVersions();
128
				$v['core'] = implode('.', \OCP\Util::getVersion());
129
				self::$versionHash = md5(implode(',', $v));
130
			}
131
		} else {
132
			self::$versionHash = md5('not installed');
133
		}
134
135
		// Add the js files
136
		$jsFiles = self::findJavascriptFiles(\OC_Util::$scripts);
137
		$this->assign('jsfiles', array());
138
		if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') {
139
			if (\OC::$server->getContentSecurityPolicyNonceManager()->browserSupportsCspV3()) {
140
				$jsConfigHelper = new JSConfigHelper(
141
					\OC::$server->getL10N('core'),
142
					\OC::$server->getThemingDefaults(),
143
					\OC::$server->getAppManager(),
144
					\OC::$server->getSession(),
145
					\OC::$server->getUserSession()->getUser(),
146
					\OC::$server->getConfig(),
147
					\OC::$server->getGroupManager(),
148
					\OC::$server->getIniWrapper(),
149
					\OC::$server->getURLGenerator()
150
				);
151
				$this->assign('inline_ocjs', $jsConfigHelper->getConfig());
152
				$this->assign('foo', 'bar');
153
			} else {
154
				$this->append('jsfiles', \OC::$server->getURLGenerator()->linkToRoute('core.OCJS.getConfig', ['v' => self::$versionHash]));
155
			}
156
		}
157
		foreach($jsFiles as $info) {
158
			$web = $info[1];
159
			$file = $info[2];
160
			$this->append( 'jsfiles', $web.'/'.$file . $this->getVersionHashSuffix() );
161
		}
162
163
		// Do not initialise scss appdata until we have a fully installed instance
164
		// Do not load scss for update, errors, installation or login page
165
		if(\OC::$server->getSystemConfig()->getValue('installed', false)
166
			&& !\OCP\Util::needUpgrade()
167
			&& \OC_User::isLoggedIn()) {
0 ignored issues
show
Deprecated Code introduced by
The method OC_User::isLoggedIn() has been deprecated with message: use \OC::$server->getUserSession()->isLoggedIn()

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...
168
			$cssFiles = self::findStylesheetFiles(\OC_Util::$styles);
169
		} else {
170
			$cssFiles = self::findStylesheetFiles(\OC_Util::$styles, false);
171
		}
172
		$this->assign('cssfiles', array());
173
		$this->assign('printcssfiles', []);
174
		$this->assign('versionHash', self::$versionHash);
175
		foreach($cssFiles as $info) {
176
			$web = $info[1];
177
			$file = $info[2];
178
179
			if (substr($file, -strlen('print.css')) === 'print.css') {
180
				$this->append( 'printcssfiles', $web.'/'.$file . $this->getVersionHashSuffix() );
181
			} else {
182
				$this->append( 'cssfiles', $web.'/'.$file . $this->getVersionHashSuffix()  );
183
			}
184
		}
185
	}
186
187
	protected function getVersionHashSuffix() {
188
		if(\OC::$server->getConfig()->getSystemValue('debug', false)) {
189
			// allows chrome workspace mapping in debug mode
190
			return "";
191
		}
192
193
		return '?v=' . self::$versionHash;
194
	}
195
196
	/**
197
	 * @param array $styles
198
	 * @return array
199
	 */
200
	static public function findStylesheetFiles($styles, $compileScss = true) {
201
		// Read the selected theme from the config file
202
		$theme = \OC_Util::getTheme();
203
204
		if($compileScss) {
205
			$SCSSCacher = new SCSSCacher(
206
				\OC::$server->getLogger(),
207
				\OC::$server->getAppDataDir('css'),
208
				\OC::$server->getURLGenerator(),
209
				\OC::$server->getSystemConfig()
210
			);
211
		} else {
212
			$SCSSCacher = null;
213
		}
214
215
		$locator = new \OC\Template\CSSResourceLocator(
216
			\OC::$server->getLogger(),
217
			$theme,
218
			array( \OC::$SERVERROOT => \OC::$WEBROOT ),
219
			array( \OC::$SERVERROOT => \OC::$WEBROOT ),
220
			$SCSSCacher);
0 ignored issues
show
Bug introduced by
It seems like $SCSSCacher defined by null on line 212 can be null; however, OC\Template\CSSResourceLocator::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
221
		$locator->find($styles);
222
		return $locator->getResources();
223
	}
224
225
	/**
226
	 * @param array $scripts
227
	 * @return array
228
	 */
229
	static public function findJavascriptFiles($scripts) {
230
		// Read the selected theme from the config file
231
		$theme = \OC_Util::getTheme();
232
233
		$locator = new \OC\Template\JSResourceLocator(
234
			\OC::$server->getLogger(),
235
			$theme,
236
			array( \OC::$SERVERROOT => \OC::$WEBROOT ),
237
			array( \OC::$SERVERROOT => \OC::$WEBROOT ));
238
		$locator->find($scripts);
239
		return $locator->getResources();
240
	}
241
242
	/**
243
	 * Converts the absolute file path to a relative path from \OC::$SERVERROOT
244
	 * @param string $filePath Absolute path
245
	 * @return string Relative path
246
	 * @throws \Exception If $filePath is not under \OC::$SERVERROOT
247
	 */
248
	public static function convertToRelativePath($filePath) {
249
		$relativePath = explode(\OC::$SERVERROOT, $filePath);
250
		if(count($relativePath) !== 2) {
251
			throw new \Exception('$filePath is not under the \OC::$SERVERROOT');
252
		}
253
254
		return $relativePath[1];
255
	}
256
}
257