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