Passed
Push — master ( 7ab34e...9af7ee )
by John
13:55
created
lib/private/TemplateLayout.php 1 patch
Indentation   +330 added lines, -330 removed lines patch added patch discarded remove patch
@@ -57,334 +57,334 @@
 block discarded – undo
57 57
 use OCP\Util;
58 58
 
59 59
 class TemplateLayout extends \OC_Template {
60
-	private static $versionHash = '';
61
-
62
-	/** @var CSSResourceLocator|null */
63
-	public static $cssLocator = null;
64
-
65
-	/** @var JSResourceLocator|null */
66
-	public static $jsLocator = null;
67
-
68
-	/** @var IConfig */
69
-	private $config;
70
-
71
-	/** @var IInitialStateService */
72
-	private $initialState;
73
-
74
-	/** @var INavigationManager */
75
-	private $navigationManager;
76
-
77
-	/**
78
-	 * @param string $renderAs
79
-	 * @param string $appId application id
80
-	 */
81
-	public function __construct($renderAs, $appId = '') {
82
-
83
-		/** @var IConfig */
84
-		$this->config = \OC::$server->get(IConfig::class);
85
-
86
-		/** @var IInitialStateService */
87
-		$this->initialState = \OC::$server->get(IInitialStateService::class);
88
-
89
-		// Add fallback theming variables if theming is disabled
90
-		if ($renderAs !== TemplateResponse::RENDER_AS_USER
91
-			|| !\OC::$server->getAppManager()->isEnabledForUser('theming')) {
92
-			// TODO cache generated default theme if enabled for fallback if server is erroring ?
93
-			Util::addStyle('theming', 'default');
94
-		}
95
-
96
-		// Decide which page we show
97
-		if ($renderAs === TemplateResponse::RENDER_AS_USER) {
98
-			/** @var INavigationManager */
99
-			$this->navigationManager = \OC::$server->get(INavigationManager::class);
100
-
101
-			parent::__construct('core', 'layout.user');
102
-			if (in_array(\OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) {
103
-				$this->assign('bodyid', 'body-settings');
104
-			} else {
105
-				$this->assign('bodyid', 'body-user');
106
-			}
107
-
108
-			$this->initialState->provideInitialState('core', 'active-app', $this->navigationManager->getActiveEntry());
109
-			$this->initialState->provideInitialState('core', 'apps', $this->navigationManager->getAll());
110
-			$this->initialState->provideInitialState('unified-search', 'limit-default', (int)$this->config->getAppValue('core', 'unified-search.limit-default', (string)SearchQuery::LIMIT_DEFAULT));
111
-			$this->initialState->provideInitialState('unified-search', 'min-search-length', (int)$this->config->getAppValue('core', 'unified-search.min-search-length', (string)1));
112
-			$this->initialState->provideInitialState('unified-search', 'live-search', $this->config->getAppValue('core', 'unified-search.live-search', 'yes') === 'yes');
113
-			Util::addScript('core', 'unified-search', 'core');
114
-
115
-			// Set body data-theme
116
-			$this->assign('enabledThemes', []);
117
-			if (\OC::$server->getAppManager()->isEnabledForUser('theming') && class_exists('\OCA\Theming\Service\ThemesService')) {
118
-				/** @var \OCA\Theming\Service\ThemesService */
119
-				$themesService = \OC::$server->get(\OCA\Theming\Service\ThemesService::class);
120
-				$this->assign('enabledThemes', $themesService->getEnabledThemes());
121
-			}
122
-
123
-			// set logo link target
124
-			$logoUrl = $this->config->getSystemValueString('logo_url', '');
125
-			$this->assign('logoUrl', $logoUrl);
126
-
127
-			// Add navigation entry
128
-			$this->assign('application', '');
129
-			$this->assign('appid', $appId);
130
-
131
-			$navigation = $this->navigationManager->getAll();
132
-			$this->assign('navigation', $navigation);
133
-			$settingsNavigation = $this->navigationManager->getAll('settings');
134
-			$this->assign('settingsnavigation', $settingsNavigation);
135
-
136
-			foreach ($navigation as $entry) {
137
-				if ($entry['active']) {
138
-					$this->assign('application', $entry['name']);
139
-					break;
140
-				}
141
-			}
142
-
143
-			foreach ($settingsNavigation as $entry) {
144
-				if ($entry['active']) {
145
-					$this->assign('application', $entry['name']);
146
-					break;
147
-				}
148
-			}
149
-
150
-			$userDisplayName = false;
151
-			$user = \OC::$server->get(IUserSession::class)->getUser();
152
-			if ($user) {
153
-				$userDisplayName = $user->getDisplayName();
154
-			}
155
-			$this->assign('user_displayname', $userDisplayName);
156
-			$this->assign('user_uid', \OC_User::getUser());
157
-
158
-			if ($user === null) {
159
-				$this->assign('userAvatarSet', false);
160
-				$this->assign('userStatus', false);
161
-			} else {
162
-				$this->assign('userAvatarSet', true);
163
-				$this->assign('userAvatarVersion', $this->config->getUserValue(\OC_User::getUser(), 'avatar', 'version', 0));
164
-			}
165
-		} elseif ($renderAs === TemplateResponse::RENDER_AS_ERROR) {
166
-			parent::__construct('core', 'layout.guest', '', false);
167
-			$this->assign('bodyid', 'body-login');
168
-			$this->assign('user_displayname', '');
169
-			$this->assign('user_uid', '');
170
-		} elseif ($renderAs === TemplateResponse::RENDER_AS_GUEST) {
171
-			parent::__construct('core', 'layout.guest');
172
-			\OC_Util::addStyle('guest');
173
-			$this->assign('bodyid', 'body-login');
174
-
175
-			$userDisplayName = false;
176
-			$user = \OC::$server->get(IUserSession::class)->getUser();
177
-			if ($user) {
178
-				$userDisplayName = $user->getDisplayName();
179
-			}
180
-			$this->assign('user_displayname', $userDisplayName);
181
-			$this->assign('user_uid', \OC_User::getUser());
182
-		} elseif ($renderAs === TemplateResponse::RENDER_AS_PUBLIC) {
183
-			parent::__construct('core', 'layout.public');
184
-			$this->assign('appid', $appId);
185
-			$this->assign('bodyid', 'body-public');
186
-
187
-			/** @var IRegistry $subscription */
188
-			$subscription = \OC::$server->query(IRegistry::class);
189
-			$showSimpleSignup = $this->config->getSystemValueBool('simpleSignUpLink.shown', true);
190
-			if ($showSimpleSignup && $subscription->delegateHasValidSubscription()) {
191
-				$showSimpleSignup = false;
192
-			}
193
-			$this->assign('showSimpleSignUpLink', $showSimpleSignup);
194
-		} else {
195
-			parent::__construct('core', 'layout.base');
196
-		}
197
-		// Send the language and the locale to our layouts
198
-		$lang = \OC::$server->getL10NFactory()->findLanguage();
199
-		$locale = \OC::$server->getL10NFactory()->findLocale($lang);
200
-
201
-		$lang = str_replace('_', '-', $lang);
202
-		$this->assign('language', $lang);
203
-		$this->assign('locale', $locale);
204
-
205
-		if (\OC::$server->getSystemConfig()->getValue('installed', false)) {
206
-			if (empty(self::$versionHash)) {
207
-				$v = \OC_App::getAppVersions();
208
-				$v['core'] = implode('.', \OCP\Util::getVersion());
209
-				self::$versionHash = substr(md5(implode(',', $v)), 0, 8);
210
-			}
211
-		} else {
212
-			self::$versionHash = md5('not installed');
213
-		}
214
-
215
-		// Add the js files
216
-		// TODO: remove deprecated OC_Util injection
217
-		$jsFiles = self::findJavascriptFiles(array_merge(\OC_Util::$scripts, Util::getScripts()));
218
-		$this->assign('jsfiles', []);
219
-		if ($this->config->getSystemValue('installed', false) && $renderAs != TemplateResponse::RENDER_AS_ERROR) {
220
-			// this is on purpose outside of the if statement below so that the initial state is prefilled (done in the getConfig() call)
221
-			// see https://github.com/nextcloud/server/pull/22636 for details
222
-			$jsConfigHelper = new JSConfigHelper(
223
-				\OC::$server->getL10N('lib'),
224
-				\OC::$server->query(Defaults::class),
225
-				\OC::$server->getAppManager(),
226
-				\OC::$server->getSession(),
227
-				\OC::$server->getUserSession()->getUser(),
228
-				$this->config,
229
-				\OC::$server->getGroupManager(),
230
-				\OC::$server->get(IniGetWrapper::class),
231
-				\OC::$server->getURLGenerator(),
232
-				\OC::$server->getCapabilitiesManager(),
233
-				\OC::$server->query(IInitialStateService::class)
234
-			);
235
-			$config = $jsConfigHelper->getConfig();
236
-			if (\OC::$server->getContentSecurityPolicyNonceManager()->browserSupportsCspV3()) {
237
-				$this->assign('inline_ocjs', $config);
238
-			} else {
239
-				$this->append('jsfiles', \OC::$server->getURLGenerator()->linkToRoute('core.OCJS.getConfig', ['v' => self::$versionHash]));
240
-			}
241
-		}
242
-		foreach ($jsFiles as $info) {
243
-			$web = $info[1];
244
-			$file = $info[2];
245
-			$this->append('jsfiles', $web.'/'.$file . $this->getVersionHashSuffix());
246
-		}
247
-
248
-		try {
249
-			$pathInfo = \OC::$server->getRequest()->getPathInfo();
250
-		} catch (\Exception $e) {
251
-			$pathInfo = '';
252
-		}
253
-
254
-		// Do not initialise scss appdata until we have a fully installed instance
255
-		// Do not load scss for update, errors, installation or login page
256
-		if (\OC::$server->getSystemConfig()->getValue('installed', false)
257
-			&& !\OCP\Util::needUpgrade()
258
-			&& $pathInfo !== ''
259
-			&& !preg_match('/^\/login/', $pathInfo)
260
-			&& $renderAs !== TemplateResponse::RENDER_AS_ERROR
261
-		) {
262
-			$cssFiles = self::findStylesheetFiles(\OC_Util::$styles);
263
-		} else {
264
-			// If we ignore the scss compiler,
265
-			// we need to load the guest css fallback
266
-			\OC_Util::addStyle('guest');
267
-			$cssFiles = self::findStylesheetFiles(\OC_Util::$styles, false);
268
-		}
269
-
270
-		$this->assign('cssfiles', []);
271
-		$this->assign('printcssfiles', []);
272
-		$this->assign('versionHash', self::$versionHash);
273
-		foreach ($cssFiles as $info) {
274
-			$web = $info[1];
275
-			$file = $info[2];
276
-
277
-			if (substr($file, -strlen('print.css')) === 'print.css') {
278
-				$this->append('printcssfiles', $web.'/'.$file . $this->getVersionHashSuffix());
279
-			} else {
280
-				$suffix = $this->getVersionHashSuffix($web, $file);
281
-
282
-				if (strpos($file, '?v=') == false) {
283
-					$this->append('cssfiles', $web.'/'.$file . $suffix);
284
-				} else {
285
-					$this->append('cssfiles', $web.'/'.$file . '-' . substr($suffix, 3));
286
-				}
287
-			}
288
-		}
289
-
290
-		$this->assign('initialStates', $this->initialState->getInitialStates());
291
-
292
-		$this->assign('id-app-content', $renderAs === TemplateResponse::RENDER_AS_USER ? '#app-content' : '#content');
293
-		$this->assign('id-app-navigation', $renderAs === TemplateResponse::RENDER_AS_USER ? '#app-navigation' : null);
294
-	}
295
-
296
-	/**
297
-	 * @param string $path
298
-	 * @param string $file
299
-	 * @return string
300
-	 */
301
-	protected function getVersionHashSuffix($path = false, $file = false) {
302
-		if ($this->config->getSystemValue('debug', false)) {
303
-			// allows chrome workspace mapping in debug mode
304
-			return "";
305
-		}
306
-		$themingSuffix = '';
307
-		$v = [];
308
-
309
-		if ($this->config->getSystemValue('installed', false)) {
310
-			if (\OC::$server->getAppManager()->isInstalled('theming')) {
311
-				$themingSuffix = '-' . $this->config->getAppValue('theming', 'cachebuster', '0');
312
-			}
313
-			$v = \OC_App::getAppVersions();
314
-		}
315
-
316
-		// Try the webroot path for a match
317
-		if ($path !== false && $path !== '') {
318
-			$appName = $this->getAppNamefromPath($path);
319
-			if (array_key_exists($appName, $v)) {
320
-				$appVersion = $v[$appName];
321
-				return '?v=' . substr(md5($appVersion), 0, 8) . $themingSuffix;
322
-			}
323
-		}
324
-		// fallback to the file path instead
325
-		if ($file !== false && $file !== '') {
326
-			$appName = $this->getAppNamefromPath($file);
327
-			if (array_key_exists($appName, $v)) {
328
-				$appVersion = $v[$appName];
329
-				return '?v=' . substr(md5($appVersion), 0, 8) . $themingSuffix;
330
-			}
331
-		}
332
-
333
-		return '?v=' . self::$versionHash . $themingSuffix;
334
-	}
335
-
336
-	/**
337
-	 * @param array $styles
338
-	 * @return array
339
-	 */
340
-	public static function findStylesheetFiles($styles, $compileScss = true) {
341
-		if (!self::$cssLocator) {
342
-			self::$cssLocator = \OCP\Server::get(CSSResourceLocator::class);
343
-		}
344
-		self::$cssLocator->find($styles);
345
-		return self::$cssLocator->getResources();
346
-	}
347
-
348
-	/**
349
-	 * @param string $path
350
-	 * @return string|boolean
351
-	 */
352
-	public function getAppNamefromPath($path) {
353
-		if ($path !== '' && is_string($path)) {
354
-			$pathParts = explode('/', $path);
355
-			if ($pathParts[0] === 'css') {
356
-				// This is a scss request
357
-				return $pathParts[1];
358
-			}
359
-			return end($pathParts);
360
-		}
361
-		return false;
362
-	}
363
-
364
-	/**
365
-	 * @param array $scripts
366
-	 * @return array
367
-	 */
368
-	public static function findJavascriptFiles($scripts) {
369
-		if (!self::$jsLocator) {
370
-			self::$jsLocator = \OCP\Server::get(JSResourceLocator::class);
371
-		}
372
-		self::$jsLocator->find($scripts);
373
-		return self::$jsLocator->getResources();
374
-	}
375
-
376
-	/**
377
-	 * Converts the absolute file path to a relative path from \OC::$SERVERROOT
378
-	 * @param string $filePath Absolute path
379
-	 * @return string Relative path
380
-	 * @throws \Exception If $filePath is not under \OC::$SERVERROOT
381
-	 */
382
-	public static function convertToRelativePath($filePath) {
383
-		$relativePath = explode(\OC::$SERVERROOT, $filePath);
384
-		if (count($relativePath) !== 2) {
385
-			throw new \Exception('$filePath is not under the \OC::$SERVERROOT');
386
-		}
387
-
388
-		return $relativePath[1];
389
-	}
60
+    private static $versionHash = '';
61
+
62
+    /** @var CSSResourceLocator|null */
63
+    public static $cssLocator = null;
64
+
65
+    /** @var JSResourceLocator|null */
66
+    public static $jsLocator = null;
67
+
68
+    /** @var IConfig */
69
+    private $config;
70
+
71
+    /** @var IInitialStateService */
72
+    private $initialState;
73
+
74
+    /** @var INavigationManager */
75
+    private $navigationManager;
76
+
77
+    /**
78
+     * @param string $renderAs
79
+     * @param string $appId application id
80
+     */
81
+    public function __construct($renderAs, $appId = '') {
82
+
83
+        /** @var IConfig */
84
+        $this->config = \OC::$server->get(IConfig::class);
85
+
86
+        /** @var IInitialStateService */
87
+        $this->initialState = \OC::$server->get(IInitialStateService::class);
88
+
89
+        // Add fallback theming variables if theming is disabled
90
+        if ($renderAs !== TemplateResponse::RENDER_AS_USER
91
+            || !\OC::$server->getAppManager()->isEnabledForUser('theming')) {
92
+            // TODO cache generated default theme if enabled for fallback if server is erroring ?
93
+            Util::addStyle('theming', 'default');
94
+        }
95
+
96
+        // Decide which page we show
97
+        if ($renderAs === TemplateResponse::RENDER_AS_USER) {
98
+            /** @var INavigationManager */
99
+            $this->navigationManager = \OC::$server->get(INavigationManager::class);
100
+
101
+            parent::__construct('core', 'layout.user');
102
+            if (in_array(\OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) {
103
+                $this->assign('bodyid', 'body-settings');
104
+            } else {
105
+                $this->assign('bodyid', 'body-user');
106
+            }
107
+
108
+            $this->initialState->provideInitialState('core', 'active-app', $this->navigationManager->getActiveEntry());
109
+            $this->initialState->provideInitialState('core', 'apps', $this->navigationManager->getAll());
110
+            $this->initialState->provideInitialState('unified-search', 'limit-default', (int)$this->config->getAppValue('core', 'unified-search.limit-default', (string)SearchQuery::LIMIT_DEFAULT));
111
+            $this->initialState->provideInitialState('unified-search', 'min-search-length', (int)$this->config->getAppValue('core', 'unified-search.min-search-length', (string)1));
112
+            $this->initialState->provideInitialState('unified-search', 'live-search', $this->config->getAppValue('core', 'unified-search.live-search', 'yes') === 'yes');
113
+            Util::addScript('core', 'unified-search', 'core');
114
+
115
+            // Set body data-theme
116
+            $this->assign('enabledThemes', []);
117
+            if (\OC::$server->getAppManager()->isEnabledForUser('theming') && class_exists('\OCA\Theming\Service\ThemesService')) {
118
+                /** @var \OCA\Theming\Service\ThemesService */
119
+                $themesService = \OC::$server->get(\OCA\Theming\Service\ThemesService::class);
120
+                $this->assign('enabledThemes', $themesService->getEnabledThemes());
121
+            }
122
+
123
+            // set logo link target
124
+            $logoUrl = $this->config->getSystemValueString('logo_url', '');
125
+            $this->assign('logoUrl', $logoUrl);
126
+
127
+            // Add navigation entry
128
+            $this->assign('application', '');
129
+            $this->assign('appid', $appId);
130
+
131
+            $navigation = $this->navigationManager->getAll();
132
+            $this->assign('navigation', $navigation);
133
+            $settingsNavigation = $this->navigationManager->getAll('settings');
134
+            $this->assign('settingsnavigation', $settingsNavigation);
135
+
136
+            foreach ($navigation as $entry) {
137
+                if ($entry['active']) {
138
+                    $this->assign('application', $entry['name']);
139
+                    break;
140
+                }
141
+            }
142
+
143
+            foreach ($settingsNavigation as $entry) {
144
+                if ($entry['active']) {
145
+                    $this->assign('application', $entry['name']);
146
+                    break;
147
+                }
148
+            }
149
+
150
+            $userDisplayName = false;
151
+            $user = \OC::$server->get(IUserSession::class)->getUser();
152
+            if ($user) {
153
+                $userDisplayName = $user->getDisplayName();
154
+            }
155
+            $this->assign('user_displayname', $userDisplayName);
156
+            $this->assign('user_uid', \OC_User::getUser());
157
+
158
+            if ($user === null) {
159
+                $this->assign('userAvatarSet', false);
160
+                $this->assign('userStatus', false);
161
+            } else {
162
+                $this->assign('userAvatarSet', true);
163
+                $this->assign('userAvatarVersion', $this->config->getUserValue(\OC_User::getUser(), 'avatar', 'version', 0));
164
+            }
165
+        } elseif ($renderAs === TemplateResponse::RENDER_AS_ERROR) {
166
+            parent::__construct('core', 'layout.guest', '', false);
167
+            $this->assign('bodyid', 'body-login');
168
+            $this->assign('user_displayname', '');
169
+            $this->assign('user_uid', '');
170
+        } elseif ($renderAs === TemplateResponse::RENDER_AS_GUEST) {
171
+            parent::__construct('core', 'layout.guest');
172
+            \OC_Util::addStyle('guest');
173
+            $this->assign('bodyid', 'body-login');
174
+
175
+            $userDisplayName = false;
176
+            $user = \OC::$server->get(IUserSession::class)->getUser();
177
+            if ($user) {
178
+                $userDisplayName = $user->getDisplayName();
179
+            }
180
+            $this->assign('user_displayname', $userDisplayName);
181
+            $this->assign('user_uid', \OC_User::getUser());
182
+        } elseif ($renderAs === TemplateResponse::RENDER_AS_PUBLIC) {
183
+            parent::__construct('core', 'layout.public');
184
+            $this->assign('appid', $appId);
185
+            $this->assign('bodyid', 'body-public');
186
+
187
+            /** @var IRegistry $subscription */
188
+            $subscription = \OC::$server->query(IRegistry::class);
189
+            $showSimpleSignup = $this->config->getSystemValueBool('simpleSignUpLink.shown', true);
190
+            if ($showSimpleSignup && $subscription->delegateHasValidSubscription()) {
191
+                $showSimpleSignup = false;
192
+            }
193
+            $this->assign('showSimpleSignUpLink', $showSimpleSignup);
194
+        } else {
195
+            parent::__construct('core', 'layout.base');
196
+        }
197
+        // Send the language and the locale to our layouts
198
+        $lang = \OC::$server->getL10NFactory()->findLanguage();
199
+        $locale = \OC::$server->getL10NFactory()->findLocale($lang);
200
+
201
+        $lang = str_replace('_', '-', $lang);
202
+        $this->assign('language', $lang);
203
+        $this->assign('locale', $locale);
204
+
205
+        if (\OC::$server->getSystemConfig()->getValue('installed', false)) {
206
+            if (empty(self::$versionHash)) {
207
+                $v = \OC_App::getAppVersions();
208
+                $v['core'] = implode('.', \OCP\Util::getVersion());
209
+                self::$versionHash = substr(md5(implode(',', $v)), 0, 8);
210
+            }
211
+        } else {
212
+            self::$versionHash = md5('not installed');
213
+        }
214
+
215
+        // Add the js files
216
+        // TODO: remove deprecated OC_Util injection
217
+        $jsFiles = self::findJavascriptFiles(array_merge(\OC_Util::$scripts, Util::getScripts()));
218
+        $this->assign('jsfiles', []);
219
+        if ($this->config->getSystemValue('installed', false) && $renderAs != TemplateResponse::RENDER_AS_ERROR) {
220
+            // this is on purpose outside of the if statement below so that the initial state is prefilled (done in the getConfig() call)
221
+            // see https://github.com/nextcloud/server/pull/22636 for details
222
+            $jsConfigHelper = new JSConfigHelper(
223
+                \OC::$server->getL10N('lib'),
224
+                \OC::$server->query(Defaults::class),
225
+                \OC::$server->getAppManager(),
226
+                \OC::$server->getSession(),
227
+                \OC::$server->getUserSession()->getUser(),
228
+                $this->config,
229
+                \OC::$server->getGroupManager(),
230
+                \OC::$server->get(IniGetWrapper::class),
231
+                \OC::$server->getURLGenerator(),
232
+                \OC::$server->getCapabilitiesManager(),
233
+                \OC::$server->query(IInitialStateService::class)
234
+            );
235
+            $config = $jsConfigHelper->getConfig();
236
+            if (\OC::$server->getContentSecurityPolicyNonceManager()->browserSupportsCspV3()) {
237
+                $this->assign('inline_ocjs', $config);
238
+            } else {
239
+                $this->append('jsfiles', \OC::$server->getURLGenerator()->linkToRoute('core.OCJS.getConfig', ['v' => self::$versionHash]));
240
+            }
241
+        }
242
+        foreach ($jsFiles as $info) {
243
+            $web = $info[1];
244
+            $file = $info[2];
245
+            $this->append('jsfiles', $web.'/'.$file . $this->getVersionHashSuffix());
246
+        }
247
+
248
+        try {
249
+            $pathInfo = \OC::$server->getRequest()->getPathInfo();
250
+        } catch (\Exception $e) {
251
+            $pathInfo = '';
252
+        }
253
+
254
+        // Do not initialise scss appdata until we have a fully installed instance
255
+        // Do not load scss for update, errors, installation or login page
256
+        if (\OC::$server->getSystemConfig()->getValue('installed', false)
257
+            && !\OCP\Util::needUpgrade()
258
+            && $pathInfo !== ''
259
+            && !preg_match('/^\/login/', $pathInfo)
260
+            && $renderAs !== TemplateResponse::RENDER_AS_ERROR
261
+        ) {
262
+            $cssFiles = self::findStylesheetFiles(\OC_Util::$styles);
263
+        } else {
264
+            // If we ignore the scss compiler,
265
+            // we need to load the guest css fallback
266
+            \OC_Util::addStyle('guest');
267
+            $cssFiles = self::findStylesheetFiles(\OC_Util::$styles, false);
268
+        }
269
+
270
+        $this->assign('cssfiles', []);
271
+        $this->assign('printcssfiles', []);
272
+        $this->assign('versionHash', self::$versionHash);
273
+        foreach ($cssFiles as $info) {
274
+            $web = $info[1];
275
+            $file = $info[2];
276
+
277
+            if (substr($file, -strlen('print.css')) === 'print.css') {
278
+                $this->append('printcssfiles', $web.'/'.$file . $this->getVersionHashSuffix());
279
+            } else {
280
+                $suffix = $this->getVersionHashSuffix($web, $file);
281
+
282
+                if (strpos($file, '?v=') == false) {
283
+                    $this->append('cssfiles', $web.'/'.$file . $suffix);
284
+                } else {
285
+                    $this->append('cssfiles', $web.'/'.$file . '-' . substr($suffix, 3));
286
+                }
287
+            }
288
+        }
289
+
290
+        $this->assign('initialStates', $this->initialState->getInitialStates());
291
+
292
+        $this->assign('id-app-content', $renderAs === TemplateResponse::RENDER_AS_USER ? '#app-content' : '#content');
293
+        $this->assign('id-app-navigation', $renderAs === TemplateResponse::RENDER_AS_USER ? '#app-navigation' : null);
294
+    }
295
+
296
+    /**
297
+     * @param string $path
298
+     * @param string $file
299
+     * @return string
300
+     */
301
+    protected function getVersionHashSuffix($path = false, $file = false) {
302
+        if ($this->config->getSystemValue('debug', false)) {
303
+            // allows chrome workspace mapping in debug mode
304
+            return "";
305
+        }
306
+        $themingSuffix = '';
307
+        $v = [];
308
+
309
+        if ($this->config->getSystemValue('installed', false)) {
310
+            if (\OC::$server->getAppManager()->isInstalled('theming')) {
311
+                $themingSuffix = '-' . $this->config->getAppValue('theming', 'cachebuster', '0');
312
+            }
313
+            $v = \OC_App::getAppVersions();
314
+        }
315
+
316
+        // Try the webroot path for a match
317
+        if ($path !== false && $path !== '') {
318
+            $appName = $this->getAppNamefromPath($path);
319
+            if (array_key_exists($appName, $v)) {
320
+                $appVersion = $v[$appName];
321
+                return '?v=' . substr(md5($appVersion), 0, 8) . $themingSuffix;
322
+            }
323
+        }
324
+        // fallback to the file path instead
325
+        if ($file !== false && $file !== '') {
326
+            $appName = $this->getAppNamefromPath($file);
327
+            if (array_key_exists($appName, $v)) {
328
+                $appVersion = $v[$appName];
329
+                return '?v=' . substr(md5($appVersion), 0, 8) . $themingSuffix;
330
+            }
331
+        }
332
+
333
+        return '?v=' . self::$versionHash . $themingSuffix;
334
+    }
335
+
336
+    /**
337
+     * @param array $styles
338
+     * @return array
339
+     */
340
+    public static function findStylesheetFiles($styles, $compileScss = true) {
341
+        if (!self::$cssLocator) {
342
+            self::$cssLocator = \OCP\Server::get(CSSResourceLocator::class);
343
+        }
344
+        self::$cssLocator->find($styles);
345
+        return self::$cssLocator->getResources();
346
+    }
347
+
348
+    /**
349
+     * @param string $path
350
+     * @return string|boolean
351
+     */
352
+    public function getAppNamefromPath($path) {
353
+        if ($path !== '' && is_string($path)) {
354
+            $pathParts = explode('/', $path);
355
+            if ($pathParts[0] === 'css') {
356
+                // This is a scss request
357
+                return $pathParts[1];
358
+            }
359
+            return end($pathParts);
360
+        }
361
+        return false;
362
+    }
363
+
364
+    /**
365
+     * @param array $scripts
366
+     * @return array
367
+     */
368
+    public static function findJavascriptFiles($scripts) {
369
+        if (!self::$jsLocator) {
370
+            self::$jsLocator = \OCP\Server::get(JSResourceLocator::class);
371
+        }
372
+        self::$jsLocator->find($scripts);
373
+        return self::$jsLocator->getResources();
374
+    }
375
+
376
+    /**
377
+     * Converts the absolute file path to a relative path from \OC::$SERVERROOT
378
+     * @param string $filePath Absolute path
379
+     * @return string Relative path
380
+     * @throws \Exception If $filePath is not under \OC::$SERVERROOT
381
+     */
382
+    public static function convertToRelativePath($filePath) {
383
+        $relativePath = explode(\OC::$SERVERROOT, $filePath);
384
+        if (count($relativePath) !== 2) {
385
+            throw new \Exception('$filePath is not under the \OC::$SERVERROOT');
386
+        }
387
+
388
+        return $relativePath[1];
389
+    }
390 390
 }
Please login to merge, or discard this patch.