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