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