Passed
Push — master ( 399621...0c9e41 )
by Morris
10:44
created
lib/private/TemplateLayout.php 1 patch
Indentation   +284 added lines, -284 removed lines patch added patch discarded remove patch
@@ -46,288 +46,288 @@
 block discarded – undo
46 46
 
47 47
 class TemplateLayout extends \OC_Template {
48 48
 
49
-	private static $versionHash = '';
50
-
51
-	/**
52
-	 * @var \OCP\IConfig
53
-	 */
54
-	private $config;
55
-
56
-	/**
57
-	 * @param string $renderAs
58
-	 * @param string $appId application id
59
-	 */
60
-	public function __construct( $renderAs, $appId = '' ) {
61
-
62
-		// yes - should be injected ....
63
-		$this->config = \OC::$server->getConfig();
64
-
65
-		if(\OCP\Util::isIE()) {
66
-			\OC_Util::addStyle('ie');
67
-		}
68
-
69
-		// Decide which page we show
70
-		if($renderAs == 'user') {
71
-			parent::__construct( 'core', 'layout.user' );
72
-			if(in_array(\OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) {
73
-				$this->assign('bodyid', 'body-settings');
74
-			}else{
75
-				$this->assign('bodyid', 'body-user');
76
-			}
77
-
78
-			// Add navigation entry
79
-			$this->assign( 'application', '');
80
-			$this->assign( 'appid', $appId );
81
-			$navigation = \OC::$server->getNavigationManager()->getAll();
82
-			$this->assign( 'navigation', $navigation);
83
-			$settingsNavigation = \OC::$server->getNavigationManager()->getAll('settings');
84
-			$this->assign( 'settingsnavigation', $settingsNavigation);
85
-			foreach($navigation as $entry) {
86
-				if ($entry['active']) {
87
-					$this->assign( 'application', $entry['name'] );
88
-					break;
89
-				}
90
-			}
91
-
92
-			foreach($settingsNavigation as $entry) {
93
-				if ($entry['active']) {
94
-					$this->assign( 'application', $entry['name'] );
95
-					break;
96
-				}
97
-			}
98
-			$userDisplayName = \OC_User::getDisplayName();
99
-			$this->assign('user_displayname', $userDisplayName);
100
-			$this->assign('user_uid', \OC_User::getUser());
101
-
102
-			if (\OC_User::getUser() === false) {
103
-				$this->assign('userAvatarSet', false);
104
-			} else {
105
-				$this->assign('userAvatarSet', \OC::$server->getAvatarManager()->getAvatar(\OC_User::getUser())->exists());
106
-				$this->assign('userAvatarVersion', $this->config->getUserValue(\OC_User::getUser(), 'avatar', 'version', 0));
107
-			}
108
-
109
-			// check if app menu icons should be inverted
110
-			try {
111
-				/** @var \OCA\Theming\Util $util */
112
-				$util = \OC::$server->query(\OCA\Theming\Util::class);
113
-				$this->assign('themingInvertMenu', $util->invertTextColor(\OC::$server->getThemingDefaults()->getColorPrimary()));
114
-			} catch (\OCP\AppFramework\QueryException $e) {
115
-				$this->assign('themingInvertMenu', false);
116
-			}
117
-
118
-		} else if ($renderAs == 'error') {
119
-			parent::__construct('core', 'layout.guest', '', false);
120
-			$this->assign('bodyid', 'body-login');
121
-		} else if ($renderAs == 'guest') {
122
-			parent::__construct('core', 'layout.guest');
123
-			\OC_Util::addStyle('guest');
124
-			$this->assign('bodyid', 'body-login');
125
-		} else if ($renderAs == 'public') {
126
-			parent::__construct('core', 'layout.public');
127
-			$this->assign( 'appid', $appId );
128
-			$this->assign('bodyid', 'body-public');
129
-			$this->assign('showSimpleSignUpLink', $this->config->getSystemValue('simpleSignUpLink.shown', true) !== false);
130
-		} else {
131
-			parent::__construct('core', 'layout.base');
132
-
133
-		}
134
-		// Send the language and the locale to our layouts
135
-		$lang = \OC::$server->getL10NFactory()->findLanguage();
136
-		$locale = \OC::$server->getL10NFactory()->findLocale($lang);
137
-		$localeLang = \OC::$server->getL10NFactory()->findLanguageFromLocale('lib', $locale);
138
-
139
-		$lang = str_replace('_', '-', $lang);
140
-		$this->assign('language', $lang);
141
-		$this->assign('locale', $locale);
142
-
143
-		if(\OC::$server->getSystemConfig()->getValue('installed', false)) {
144
-			if (empty(self::$versionHash)) {
145
-				$v = \OC_App::getAppVersions();
146
-				$v['core'] = implode('.', \OCP\Util::getVersion());
147
-				self::$versionHash = substr(md5(implode(',', $v)), 0, 8);
148
-			}
149
-		} else {
150
-			self::$versionHash = md5('not installed');
151
-		}
152
-
153
-		// Add the js files
154
-		$jsFiles = self::findJavascriptFiles(\OC_Util::$scripts);
155
-		$this->assign('jsfiles', array());
156
-		if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') {
157
-			if (\OC::$server->getContentSecurityPolicyNonceManager()->browserSupportsCspV3()) {
158
-				$jsConfigHelper = new JSConfigHelper(
159
-					\OC::$server->getL10N('lib', $localeLang ?: $lang),
160
-					\OC::$server->query(Defaults::class),
161
-					\OC::$server->getAppManager(),
162
-					\OC::$server->getSession(),
163
-					\OC::$server->getUserSession()->getUser(),
164
-					$this->config,
165
-					\OC::$server->getGroupManager(),
166
-					\OC::$server->getIniWrapper(),
167
-					\OC::$server->getURLGenerator(),
168
-					\OC::$server->getCapabilitiesManager()
169
-				);
170
-				$this->assign('inline_ocjs', $jsConfigHelper->getConfig());
171
-			} else {
172
-				$this->append('jsfiles', \OC::$server->getURLGenerator()->linkToRoute('core.OCJS.getConfig', ['v' => self::$versionHash]));
173
-			}
174
-		}
175
-		foreach($jsFiles as $info) {
176
-			$web = $info[1];
177
-			$file = $info[2];
178
-			$this->append( 'jsfiles', $web.'/'.$file . $this->getVersionHashSuffix() );
179
-		}
180
-
181
-		try {
182
-			$pathInfo = \OC::$server->getRequest()->getPathInfo();
183
-		} catch (\Exception $e) {
184
-			$pathInfo = '';
185
-		}
186
-
187
-		// Do not initialise scss appdata until we have a fully installed instance
188
-		// Do not load scss for update, errors, installation or login page
189
-		if(\OC::$server->getSystemConfig()->getValue('installed', false)
190
-			&& !\OCP\Util::needUpgrade()
191
-			&& $pathInfo !== ''
192
-			&& !preg_match('/^\/login/', $pathInfo)
193
-			&& $renderAs !== 'error'
194
-		) {
195
-			$cssFiles = self::findStylesheetFiles(\OC_Util::$styles);
196
-		} else {
197
-			// If we ignore the scss compiler,
198
-			// we need to load the guest css fallback
199
-			\OC_Util::addStyle('guest');
200
-			$cssFiles = self::findStylesheetFiles(\OC_Util::$styles, false);
201
-		}
202
-
203
-		$this->assign('cssfiles', array());
204
-		$this->assign('printcssfiles', []);
205
-		$this->assign('versionHash', self::$versionHash);
206
-		foreach($cssFiles as $info) {
207
-			$web = $info[1];
208
-			$file = $info[2];
209
-
210
-			if (substr($file, -strlen('print.css')) === 'print.css') {
211
-				$this->append( 'printcssfiles', $web.'/'.$file . $this->getVersionHashSuffix() );
212
-			} else {
213
-				$this->append( 'cssfiles', $web.'/'.$file . $this->getVersionHashSuffix($web, $file)  );
214
-			}
215
-		}
216
-	}
217
-
218
-	/**
219
-	 * @param string $path
220
- 	 * @param string $file
221
-	 * @return string
222
-	 */
223
-	protected function getVersionHashSuffix($path = false, $file = false) {
224
-		if ($this->config->getSystemValue('debug', false)) {
225
-			// allows chrome workspace mapping in debug mode
226
-			return "";
227
-		}
228
-		$themingSuffix = '';
229
-		$v = [];
230
-
231
-		if ($this->config->getSystemValue('installed', false)) {
232
-			if (\OC::$server->getAppManager()->isInstalled('theming')) {
233
-				$themingSuffix = '-' . $this->config->getAppValue('theming', 'cachebuster', '0');
234
-			}
235
-			$v = \OC_App::getAppVersions();
236
-		}
237
-
238
-		// Try the webroot path for a match
239
-		if ($path !== false && $path !== '') {
240
-			$appName = $this->getAppNamefromPath($path);
241
-			if(array_key_exists($appName, $v)) {
242
-				$appVersion = $v[$appName];
243
-				return '?v=' . substr(md5($appVersion), 0, 8) . $themingSuffix;
244
-			}
245
-		}
246
-		// fallback to the file path instead
247
-		if ($file !== false && $file !== '') {
248
-			$appName = $this->getAppNamefromPath($file);
249
-			if(array_key_exists($appName, $v)) {
250
-				$appVersion = $v[$appName];
251
-				return '?v=' . substr(md5($appVersion), 0, 8) . $themingSuffix;
252
-			}
253
-		}
254
-
255
-		return '?v=' . self::$versionHash . $themingSuffix;
256
-	}
257
-
258
-	/**
259
-	 * @param array $styles
260
-	 * @return array
261
-	 */
262
-	static public function findStylesheetFiles($styles, $compileScss = true) {
263
-		// Read the selected theme from the config file
264
-		$theme = \OC_Util::getTheme();
265
-
266
-		if($compileScss) {
267
-			$SCSSCacher = \OC::$server->query(SCSSCacher::class);
268
-		} else {
269
-			$SCSSCacher = null;
270
-		}
271
-
272
-		$locator = new \OC\Template\CSSResourceLocator(
273
-			\OC::$server->getLogger(),
274
-			$theme,
275
-			array( \OC::$SERVERROOT => \OC::$WEBROOT ),
276
-			array( \OC::$SERVERROOT => \OC::$WEBROOT ),
277
-			$SCSSCacher
278
-		);
279
-		$locator->find($styles);
280
-		return $locator->getResources();
281
-	}
282
-
283
-	/**
284
-	 * @param string $path
285
-	 * @return string|boolean
286
-	 */
287
-	public function getAppNamefromPath($path) {
288
-		if ($path !== '' && is_string($path)) {
289
-			$pathParts = explode('/', $path);
290
-			if ($pathParts[0] === 'css') {
291
-				// This is a scss request
292
-				return $pathParts[1];
293
-			}
294
-			return end($pathParts);
295
-		}
296
-		return false;
297
-
298
-	}
299
-
300
-	/**
301
-	 * @param array $scripts
302
-	 * @return array
303
-	 */
304
-	static public function findJavascriptFiles($scripts) {
305
-		// Read the selected theme from the config file
306
-		$theme = \OC_Util::getTheme();
307
-
308
-		$locator = new \OC\Template\JSResourceLocator(
309
-			\OC::$server->getLogger(),
310
-			$theme,
311
-			array( \OC::$SERVERROOT => \OC::$WEBROOT ),
312
-			array( \OC::$SERVERROOT => \OC::$WEBROOT ),
313
-			\OC::$server->query(JSCombiner::class)
314
-			);
315
-		$locator->find($scripts);
316
-		return $locator->getResources();
317
-	}
318
-
319
-	/**
320
-	 * Converts the absolute file path to a relative path from \OC::$SERVERROOT
321
-	 * @param string $filePath Absolute path
322
-	 * @return string Relative path
323
-	 * @throws \Exception If $filePath is not under \OC::$SERVERROOT
324
-	 */
325
-	public static function convertToRelativePath($filePath) {
326
-		$relativePath = explode(\OC::$SERVERROOT, $filePath);
327
-		if(count($relativePath) !== 2) {
328
-			throw new \Exception('$filePath is not under the \OC::$SERVERROOT');
329
-		}
330
-
331
-		return $relativePath[1];
332
-	}
49
+    private static $versionHash = '';
50
+
51
+    /**
52
+     * @var \OCP\IConfig
53
+     */
54
+    private $config;
55
+
56
+    /**
57
+     * @param string $renderAs
58
+     * @param string $appId application id
59
+     */
60
+    public function __construct( $renderAs, $appId = '' ) {
61
+
62
+        // yes - should be injected ....
63
+        $this->config = \OC::$server->getConfig();
64
+
65
+        if(\OCP\Util::isIE()) {
66
+            \OC_Util::addStyle('ie');
67
+        }
68
+
69
+        // Decide which page we show
70
+        if($renderAs == 'user') {
71
+            parent::__construct( 'core', 'layout.user' );
72
+            if(in_array(\OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) {
73
+                $this->assign('bodyid', 'body-settings');
74
+            }else{
75
+                $this->assign('bodyid', 'body-user');
76
+            }
77
+
78
+            // Add navigation entry
79
+            $this->assign( 'application', '');
80
+            $this->assign( 'appid', $appId );
81
+            $navigation = \OC::$server->getNavigationManager()->getAll();
82
+            $this->assign( 'navigation', $navigation);
83
+            $settingsNavigation = \OC::$server->getNavigationManager()->getAll('settings');
84
+            $this->assign( 'settingsnavigation', $settingsNavigation);
85
+            foreach($navigation as $entry) {
86
+                if ($entry['active']) {
87
+                    $this->assign( 'application', $entry['name'] );
88
+                    break;
89
+                }
90
+            }
91
+
92
+            foreach($settingsNavigation as $entry) {
93
+                if ($entry['active']) {
94
+                    $this->assign( 'application', $entry['name'] );
95
+                    break;
96
+                }
97
+            }
98
+            $userDisplayName = \OC_User::getDisplayName();
99
+            $this->assign('user_displayname', $userDisplayName);
100
+            $this->assign('user_uid', \OC_User::getUser());
101
+
102
+            if (\OC_User::getUser() === false) {
103
+                $this->assign('userAvatarSet', false);
104
+            } else {
105
+                $this->assign('userAvatarSet', \OC::$server->getAvatarManager()->getAvatar(\OC_User::getUser())->exists());
106
+                $this->assign('userAvatarVersion', $this->config->getUserValue(\OC_User::getUser(), 'avatar', 'version', 0));
107
+            }
108
+
109
+            // check if app menu icons should be inverted
110
+            try {
111
+                /** @var \OCA\Theming\Util $util */
112
+                $util = \OC::$server->query(\OCA\Theming\Util::class);
113
+                $this->assign('themingInvertMenu', $util->invertTextColor(\OC::$server->getThemingDefaults()->getColorPrimary()));
114
+            } catch (\OCP\AppFramework\QueryException $e) {
115
+                $this->assign('themingInvertMenu', false);
116
+            }
117
+
118
+        } else if ($renderAs == 'error') {
119
+            parent::__construct('core', 'layout.guest', '', false);
120
+            $this->assign('bodyid', 'body-login');
121
+        } else if ($renderAs == 'guest') {
122
+            parent::__construct('core', 'layout.guest');
123
+            \OC_Util::addStyle('guest');
124
+            $this->assign('bodyid', 'body-login');
125
+        } else if ($renderAs == 'public') {
126
+            parent::__construct('core', 'layout.public');
127
+            $this->assign( 'appid', $appId );
128
+            $this->assign('bodyid', 'body-public');
129
+            $this->assign('showSimpleSignUpLink', $this->config->getSystemValue('simpleSignUpLink.shown', true) !== false);
130
+        } else {
131
+            parent::__construct('core', 'layout.base');
132
+
133
+        }
134
+        // Send the language and the locale to our layouts
135
+        $lang = \OC::$server->getL10NFactory()->findLanguage();
136
+        $locale = \OC::$server->getL10NFactory()->findLocale($lang);
137
+        $localeLang = \OC::$server->getL10NFactory()->findLanguageFromLocale('lib', $locale);
138
+
139
+        $lang = str_replace('_', '-', $lang);
140
+        $this->assign('language', $lang);
141
+        $this->assign('locale', $locale);
142
+
143
+        if(\OC::$server->getSystemConfig()->getValue('installed', false)) {
144
+            if (empty(self::$versionHash)) {
145
+                $v = \OC_App::getAppVersions();
146
+                $v['core'] = implode('.', \OCP\Util::getVersion());
147
+                self::$versionHash = substr(md5(implode(',', $v)), 0, 8);
148
+            }
149
+        } else {
150
+            self::$versionHash = md5('not installed');
151
+        }
152
+
153
+        // Add the js files
154
+        $jsFiles = self::findJavascriptFiles(\OC_Util::$scripts);
155
+        $this->assign('jsfiles', array());
156
+        if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') {
157
+            if (\OC::$server->getContentSecurityPolicyNonceManager()->browserSupportsCspV3()) {
158
+                $jsConfigHelper = new JSConfigHelper(
159
+                    \OC::$server->getL10N('lib', $localeLang ?: $lang),
160
+                    \OC::$server->query(Defaults::class),
161
+                    \OC::$server->getAppManager(),
162
+                    \OC::$server->getSession(),
163
+                    \OC::$server->getUserSession()->getUser(),
164
+                    $this->config,
165
+                    \OC::$server->getGroupManager(),
166
+                    \OC::$server->getIniWrapper(),
167
+                    \OC::$server->getURLGenerator(),
168
+                    \OC::$server->getCapabilitiesManager()
169
+                );
170
+                $this->assign('inline_ocjs', $jsConfigHelper->getConfig());
171
+            } else {
172
+                $this->append('jsfiles', \OC::$server->getURLGenerator()->linkToRoute('core.OCJS.getConfig', ['v' => self::$versionHash]));
173
+            }
174
+        }
175
+        foreach($jsFiles as $info) {
176
+            $web = $info[1];
177
+            $file = $info[2];
178
+            $this->append( 'jsfiles', $web.'/'.$file . $this->getVersionHashSuffix() );
179
+        }
180
+
181
+        try {
182
+            $pathInfo = \OC::$server->getRequest()->getPathInfo();
183
+        } catch (\Exception $e) {
184
+            $pathInfo = '';
185
+        }
186
+
187
+        // Do not initialise scss appdata until we have a fully installed instance
188
+        // Do not load scss for update, errors, installation or login page
189
+        if(\OC::$server->getSystemConfig()->getValue('installed', false)
190
+            && !\OCP\Util::needUpgrade()
191
+            && $pathInfo !== ''
192
+            && !preg_match('/^\/login/', $pathInfo)
193
+            && $renderAs !== 'error'
194
+        ) {
195
+            $cssFiles = self::findStylesheetFiles(\OC_Util::$styles);
196
+        } else {
197
+            // If we ignore the scss compiler,
198
+            // we need to load the guest css fallback
199
+            \OC_Util::addStyle('guest');
200
+            $cssFiles = self::findStylesheetFiles(\OC_Util::$styles, false);
201
+        }
202
+
203
+        $this->assign('cssfiles', array());
204
+        $this->assign('printcssfiles', []);
205
+        $this->assign('versionHash', self::$versionHash);
206
+        foreach($cssFiles as $info) {
207
+            $web = $info[1];
208
+            $file = $info[2];
209
+
210
+            if (substr($file, -strlen('print.css')) === 'print.css') {
211
+                $this->append( 'printcssfiles', $web.'/'.$file . $this->getVersionHashSuffix() );
212
+            } else {
213
+                $this->append( 'cssfiles', $web.'/'.$file . $this->getVersionHashSuffix($web, $file)  );
214
+            }
215
+        }
216
+    }
217
+
218
+    /**
219
+     * @param string $path
220
+     * @param string $file
221
+     * @return string
222
+     */
223
+    protected function getVersionHashSuffix($path = false, $file = false) {
224
+        if ($this->config->getSystemValue('debug', false)) {
225
+            // allows chrome workspace mapping in debug mode
226
+            return "";
227
+        }
228
+        $themingSuffix = '';
229
+        $v = [];
230
+
231
+        if ($this->config->getSystemValue('installed', false)) {
232
+            if (\OC::$server->getAppManager()->isInstalled('theming')) {
233
+                $themingSuffix = '-' . $this->config->getAppValue('theming', 'cachebuster', '0');
234
+            }
235
+            $v = \OC_App::getAppVersions();
236
+        }
237
+
238
+        // Try the webroot path for a match
239
+        if ($path !== false && $path !== '') {
240
+            $appName = $this->getAppNamefromPath($path);
241
+            if(array_key_exists($appName, $v)) {
242
+                $appVersion = $v[$appName];
243
+                return '?v=' . substr(md5($appVersion), 0, 8) . $themingSuffix;
244
+            }
245
+        }
246
+        // fallback to the file path instead
247
+        if ($file !== false && $file !== '') {
248
+            $appName = $this->getAppNamefromPath($file);
249
+            if(array_key_exists($appName, $v)) {
250
+                $appVersion = $v[$appName];
251
+                return '?v=' . substr(md5($appVersion), 0, 8) . $themingSuffix;
252
+            }
253
+        }
254
+
255
+        return '?v=' . self::$versionHash . $themingSuffix;
256
+    }
257
+
258
+    /**
259
+     * @param array $styles
260
+     * @return array
261
+     */
262
+    static public function findStylesheetFiles($styles, $compileScss = true) {
263
+        // Read the selected theme from the config file
264
+        $theme = \OC_Util::getTheme();
265
+
266
+        if($compileScss) {
267
+            $SCSSCacher = \OC::$server->query(SCSSCacher::class);
268
+        } else {
269
+            $SCSSCacher = null;
270
+        }
271
+
272
+        $locator = new \OC\Template\CSSResourceLocator(
273
+            \OC::$server->getLogger(),
274
+            $theme,
275
+            array( \OC::$SERVERROOT => \OC::$WEBROOT ),
276
+            array( \OC::$SERVERROOT => \OC::$WEBROOT ),
277
+            $SCSSCacher
278
+        );
279
+        $locator->find($styles);
280
+        return $locator->getResources();
281
+    }
282
+
283
+    /**
284
+     * @param string $path
285
+     * @return string|boolean
286
+     */
287
+    public function getAppNamefromPath($path) {
288
+        if ($path !== '' && is_string($path)) {
289
+            $pathParts = explode('/', $path);
290
+            if ($pathParts[0] === 'css') {
291
+                // This is a scss request
292
+                return $pathParts[1];
293
+            }
294
+            return end($pathParts);
295
+        }
296
+        return false;
297
+
298
+    }
299
+
300
+    /**
301
+     * @param array $scripts
302
+     * @return array
303
+     */
304
+    static public function findJavascriptFiles($scripts) {
305
+        // Read the selected theme from the config file
306
+        $theme = \OC_Util::getTheme();
307
+
308
+        $locator = new \OC\Template\JSResourceLocator(
309
+            \OC::$server->getLogger(),
310
+            $theme,
311
+            array( \OC::$SERVERROOT => \OC::$WEBROOT ),
312
+            array( \OC::$SERVERROOT => \OC::$WEBROOT ),
313
+            \OC::$server->query(JSCombiner::class)
314
+            );
315
+        $locator->find($scripts);
316
+        return $locator->getResources();
317
+    }
318
+
319
+    /**
320
+     * Converts the absolute file path to a relative path from \OC::$SERVERROOT
321
+     * @param string $filePath Absolute path
322
+     * @return string Relative path
323
+     * @throws \Exception If $filePath is not under \OC::$SERVERROOT
324
+     */
325
+    public static function convertToRelativePath($filePath) {
326
+        $relativePath = explode(\OC::$SERVERROOT, $filePath);
327
+        if(count($relativePath) !== 2) {
328
+            throw new \Exception('$filePath is not under the \OC::$SERVERROOT');
329
+        }
330
+
331
+        return $relativePath[1];
332
+    }
333 333
 }
Please login to merge, or discard this patch.