Passed
Push — master ( ab7f6d...881216 )
by Roeland
11:26 queued 23s
created
apps/accessibility/lib/Controller/AccessibilityController.php 1 patch
Indentation   +256 added lines, -256 removed lines patch added patch discarded remove patch
@@ -51,271 +51,271 @@
 block discarded – undo
51 51
 
52 52
 class AccessibilityController extends Controller {
53 53
 
54
-	/** @var string */
55
-	protected $appName;
56
-
57
-	/** @var string */
58
-	protected $serverRoot;
59
-
60
-	/** @var IConfig */
61
-	private $config;
62
-
63
-	/** @var IUserManager */
64
-	private $userManager;
65
-
66
-	/** @var ILogger */
67
-	private $logger;
68
-
69
-	/** @var IURLGenerator */
70
-	private $urlGenerator;
71
-
72
-	/** @var ITimeFactory */
73
-	protected $timeFactory;
74
-
75
-	/** @var IUserSession */
76
-	private $userSession;
77
-
78
-	/** @var IAppManager */
79
-	private $appManager;
80
-
81
-	/** @var IconsCacher */
82
-	protected $iconsCacher;
83
-
84
-	/** @var \OC_Defaults */
85
-	private $defaults;
86
-
87
-	/** @var null|string */
88
-	private $injectedVariables;
89
-
90
-	/**
91
-	 * Account constructor.
92
-	 *
93
-	 * @param string $appName
94
-	 * @param IRequest $request
95
-	 * @param IConfig $config
96
-	 * @param IUserManager $userManager
97
-	 * @param ILogger $logger
98
-	 * @param IURLGenerator $urlGenerator
99
-	 * @param ITimeFactory $timeFactory
100
-	 * @param IUserSession $userSession
101
-	 * @param IAppManager $appManager
102
-	 * @param \OC_Defaults $defaults
103
-	 */
104
-	public function __construct(string $appName,
105
-								IRequest $request,
106
-								IConfig $config,
107
-								IUserManager $userManager,
108
-								ILogger $logger,
109
-								IURLGenerator $urlGenerator,
110
-								ITimeFactory $timeFactory,
111
-								IUserSession $userSession,
112
-								IAppManager $appManager,
113
-								IconsCacher $iconsCacher,
114
-								\OC_Defaults $defaults) {
115
-		parent::__construct($appName, $request);
116
-		$this->appName      = $appName;
117
-		$this->config       = $config;
118
-		$this->userManager  = $userManager;
119
-		$this->logger       = $logger;
120
-		$this->urlGenerator = $urlGenerator;
121
-		$this->timeFactory  = $timeFactory;
122
-		$this->userSession  = $userSession;
123
-		$this->appManager   = $appManager;
124
-		$this->iconsCacher  = $iconsCacher;
125
-		$this->defaults     = $defaults;
126
-
127
-		$this->serverRoot = \OC::$SERVERROOT;
128
-		$this->appRoot    = $this->appManager->getAppPath($this->appName);
129
-	}
130
-
131
-	/**
132
-	 * @NoAdminRequired
133
-	 * @NoCSRFRequired
134
-	 * @NoSameSiteCookieRequired
135
-	 *
136
-	 * @return DataDisplayResponse
137
-	 */
138
-	public function getCss(): DataDisplayResponse {
139
-		$css        = '';
140
-		$imports    = '';
141
-		$userValues = $this->getUserValues();
142
-
143
-		foreach ($userValues as $key => $scssFile) {
144
-			if ($scssFile !== false) {
145
-				if ($scssFile === 'highcontrast' && in_array('dark', $userValues)) {
146
-					$scssFile .= 'dark';
147
-				}
148
-				$imports .= '@import "' . $scssFile . '";';
149
-			}
150
-		}
151
-
152
-		if ($imports !== '') {
153
-			$scss = new Compiler();
154
-			$scss->setImportPaths([
155
-				$this->appRoot . '/css/',
156
-				$this->serverRoot . '/core/css/'
157
-			]);
158
-
159
-			// Continue after throw
160
-			$scss->setIgnoreErrors(true);
161
-			$scss->setFormatter(Crunched::class);
162
-
163
-			// Import theme, variables and compile css4 variables
164
-			try {
165
-				$css .= $scss->compile(
166
-					$imports .
167
-					$this->getInjectedVariables() .
168
-					'@import "variables.scss";' .
169
-					'@import "css-variables.scss";'
170
-				);
171
-			} catch (ParserException $e) {
172
-				$this->logger->error($e->getMessage(), ['app' => 'core']);
173
-			}
174
-		}
175
-
176
-		// We don't want to override vars with url since path is different
177
-		$css = $this->filterOutRule('/--[a-z-:]+url\([^;]+\)/mi', $css);
178
-
179
-		// Rebase all urls
180
-		$appWebRoot = substr($this->appRoot, strlen($this->serverRoot) - strlen(\OC::$WEBROOT));
181
-		$css        = $this->rebaseUrls($css, $appWebRoot . '/css');
182
-
183
-		if (in_array('dark', $userValues) && $this->iconsCacher->getCachedList() && $this->iconsCacher->getCachedList()->getSize() > 0) {
184
-			$iconsCss = $this->invertSvgIconsColor($this->iconsCacher->getCachedList()->getContent());
185
-			$css = $css . $iconsCss;
186
-		}
187
-
188
-		$response = new DataDisplayResponse($css, Http::STATUS_OK, ['Content-Type' => 'text/css']);
189
-
190
-		// Set cache control
191
-		$ttl = 31536000;
192
-		$response->addHeader('Cache-Control', 'max-age=' . $ttl . ', immutable');
193
-		$expires = new \DateTime();
194
-		$expires->setTimestamp($this->timeFactory->getTime());
195
-		$expires->add(new \DateInterval('PT' . $ttl . 'S'));
196
-		$response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
197
-		$response->addHeader('Pragma', 'cache');
198
-
199
-		// store current cache hash
200
-		$this->config->setUserValue($this->userSession->getUser()->getUID(), $this->appName, 'icons-css', md5($css));
201
-
202
-		return $response;
203
-	}
204
-
205
-	/**
206
-	 * @NoCSRFRequired
207
-	 * @PublicPage
208
-	 * @NoSameSiteCookieRequired
209
-	 *
210
-	 * @return DataDownloadResponse
211
-	 */
212
-	public function getJavascript(): DataDownloadResponse {
213
-		$user = $this->userSession->getUser();
214
-
215
-		if ($user === null) {
216
-			$theme = false;
217
-			$highcontrast = false;
218
-		} else {
219
-			$theme = $this->config->getUserValue($user->getUID(), $this->appName, 'theme', false);
220
-			$highcontrast = $this->config->getUserValue($user->getUID(), $this->appName, 'highcontrast', false) !== false;
221
-		}
222
-		if ($theme !== false) {
223
-			$responseJS = '(function() {
54
+    /** @var string */
55
+    protected $appName;
56
+
57
+    /** @var string */
58
+    protected $serverRoot;
59
+
60
+    /** @var IConfig */
61
+    private $config;
62
+
63
+    /** @var IUserManager */
64
+    private $userManager;
65
+
66
+    /** @var ILogger */
67
+    private $logger;
68
+
69
+    /** @var IURLGenerator */
70
+    private $urlGenerator;
71
+
72
+    /** @var ITimeFactory */
73
+    protected $timeFactory;
74
+
75
+    /** @var IUserSession */
76
+    private $userSession;
77
+
78
+    /** @var IAppManager */
79
+    private $appManager;
80
+
81
+    /** @var IconsCacher */
82
+    protected $iconsCacher;
83
+
84
+    /** @var \OC_Defaults */
85
+    private $defaults;
86
+
87
+    /** @var null|string */
88
+    private $injectedVariables;
89
+
90
+    /**
91
+     * Account constructor.
92
+     *
93
+     * @param string $appName
94
+     * @param IRequest $request
95
+     * @param IConfig $config
96
+     * @param IUserManager $userManager
97
+     * @param ILogger $logger
98
+     * @param IURLGenerator $urlGenerator
99
+     * @param ITimeFactory $timeFactory
100
+     * @param IUserSession $userSession
101
+     * @param IAppManager $appManager
102
+     * @param \OC_Defaults $defaults
103
+     */
104
+    public function __construct(string $appName,
105
+                                IRequest $request,
106
+                                IConfig $config,
107
+                                IUserManager $userManager,
108
+                                ILogger $logger,
109
+                                IURLGenerator $urlGenerator,
110
+                                ITimeFactory $timeFactory,
111
+                                IUserSession $userSession,
112
+                                IAppManager $appManager,
113
+                                IconsCacher $iconsCacher,
114
+                                \OC_Defaults $defaults) {
115
+        parent::__construct($appName, $request);
116
+        $this->appName      = $appName;
117
+        $this->config       = $config;
118
+        $this->userManager  = $userManager;
119
+        $this->logger       = $logger;
120
+        $this->urlGenerator = $urlGenerator;
121
+        $this->timeFactory  = $timeFactory;
122
+        $this->userSession  = $userSession;
123
+        $this->appManager   = $appManager;
124
+        $this->iconsCacher  = $iconsCacher;
125
+        $this->defaults     = $defaults;
126
+
127
+        $this->serverRoot = \OC::$SERVERROOT;
128
+        $this->appRoot    = $this->appManager->getAppPath($this->appName);
129
+    }
130
+
131
+    /**
132
+     * @NoAdminRequired
133
+     * @NoCSRFRequired
134
+     * @NoSameSiteCookieRequired
135
+     *
136
+     * @return DataDisplayResponse
137
+     */
138
+    public function getCss(): DataDisplayResponse {
139
+        $css        = '';
140
+        $imports    = '';
141
+        $userValues = $this->getUserValues();
142
+
143
+        foreach ($userValues as $key => $scssFile) {
144
+            if ($scssFile !== false) {
145
+                if ($scssFile === 'highcontrast' && in_array('dark', $userValues)) {
146
+                    $scssFile .= 'dark';
147
+                }
148
+                $imports .= '@import "' . $scssFile . '";';
149
+            }
150
+        }
151
+
152
+        if ($imports !== '') {
153
+            $scss = new Compiler();
154
+            $scss->setImportPaths([
155
+                $this->appRoot . '/css/',
156
+                $this->serverRoot . '/core/css/'
157
+            ]);
158
+
159
+            // Continue after throw
160
+            $scss->setIgnoreErrors(true);
161
+            $scss->setFormatter(Crunched::class);
162
+
163
+            // Import theme, variables and compile css4 variables
164
+            try {
165
+                $css .= $scss->compile(
166
+                    $imports .
167
+                    $this->getInjectedVariables() .
168
+                    '@import "variables.scss";' .
169
+                    '@import "css-variables.scss";'
170
+                );
171
+            } catch (ParserException $e) {
172
+                $this->logger->error($e->getMessage(), ['app' => 'core']);
173
+            }
174
+        }
175
+
176
+        // We don't want to override vars with url since path is different
177
+        $css = $this->filterOutRule('/--[a-z-:]+url\([^;]+\)/mi', $css);
178
+
179
+        // Rebase all urls
180
+        $appWebRoot = substr($this->appRoot, strlen($this->serverRoot) - strlen(\OC::$WEBROOT));
181
+        $css        = $this->rebaseUrls($css, $appWebRoot . '/css');
182
+
183
+        if (in_array('dark', $userValues) && $this->iconsCacher->getCachedList() && $this->iconsCacher->getCachedList()->getSize() > 0) {
184
+            $iconsCss = $this->invertSvgIconsColor($this->iconsCacher->getCachedList()->getContent());
185
+            $css = $css . $iconsCss;
186
+        }
187
+
188
+        $response = new DataDisplayResponse($css, Http::STATUS_OK, ['Content-Type' => 'text/css']);
189
+
190
+        // Set cache control
191
+        $ttl = 31536000;
192
+        $response->addHeader('Cache-Control', 'max-age=' . $ttl . ', immutable');
193
+        $expires = new \DateTime();
194
+        $expires->setTimestamp($this->timeFactory->getTime());
195
+        $expires->add(new \DateInterval('PT' . $ttl . 'S'));
196
+        $response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
197
+        $response->addHeader('Pragma', 'cache');
198
+
199
+        // store current cache hash
200
+        $this->config->setUserValue($this->userSession->getUser()->getUID(), $this->appName, 'icons-css', md5($css));
201
+
202
+        return $response;
203
+    }
204
+
205
+    /**
206
+     * @NoCSRFRequired
207
+     * @PublicPage
208
+     * @NoSameSiteCookieRequired
209
+     *
210
+     * @return DataDownloadResponse
211
+     */
212
+    public function getJavascript(): DataDownloadResponse {
213
+        $user = $this->userSession->getUser();
214
+
215
+        if ($user === null) {
216
+            $theme = false;
217
+            $highcontrast = false;
218
+        } else {
219
+            $theme = $this->config->getUserValue($user->getUID(), $this->appName, 'theme', false);
220
+            $highcontrast = $this->config->getUserValue($user->getUID(), $this->appName, 'highcontrast', false) !== false;
221
+        }
222
+        if ($theme !== false) {
223
+            $responseJS = '(function() {
224 224
 	OCA.Accessibility = {
225 225
 		highcontrast: ' . json_encode($highcontrast) . ',
226 226
 		theme: ' . json_encode($theme) . ',
227 227
 	};
228 228
 	document.body.classList.add(' . json_encode($theme) . ');
229 229
 })();';
230
-		} else {
231
-			$responseJS = '(function() {
230
+        } else {
231
+            $responseJS = '(function() {
232 232
 	OCA.Accessibility = {
233 233
 		highcontrast: ' . json_encode($highcontrast) . ',
234 234
 		theme: ' . json_encode($theme) . ',
235 235
 	};
236 236
 })();';
237
-		}
238
-		$response = new DataDownloadResponse($responseJS, 'javascript', 'text/javascript');
239
-		$response->cacheFor(3600);
240
-		return $response;
241
-	}
242
-
243
-	/**
244
-	 * Return an array with the user theme & font settings
245
-	 *
246
-	 * @return array
247
-	 */
248
-	private function getUserValues(): array{
249
-		$userTheme = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'theme', false);
250
-		$userFont  = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'font', false);
251
-		$userHighContrast = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'highcontrast', false);
252
-
253
-		return [$userTheme, $userHighContrast, $userFont];
254
-	}
255
-
256
-	/**
257
-	 * Remove all matches from the $rule regex
258
-	 *
259
-	 * @param string $rule regex to match
260
-	 * @param string $css string to parse
261
-	 * @return string
262
-	 */
263
-	private function filterOutRule(string $rule, string $css): string {
264
-		return preg_replace($rule, '', $css);
265
-	}
266
-
267
-	/**
268
-	 * Add the correct uri prefix to make uri valid again
269
-	 *
270
-	 * @param string $css
271
-	 * @param string $webDir
272
-	 * @return string
273
-	 */
274
-	private function rebaseUrls(string $css, string $webDir): string {
275
-		$re    = '/url\([\'"]([^\/][\.\w?=\/-]*)[\'"]\)/x';
276
-		$subst = 'url(\'' . $webDir . '/$1\')';
277
-
278
-		return preg_replace($re, $subst, $css);
279
-	}
280
-
281
-	/**
282
-	 * Remove all matches from the $rule regex
283
-	 *
284
-	 * @param string $css string to parse
285
-	 * @return string
286
-	 */
287
-	private function invertSvgIconsColor(string $css) {
288
-		return str_replace(
289
-			['color=000&', 'color=fff&', 'color=***&'],
290
-			['color=***&', 'color=000&', 'color=fff&'],
291
-			str_replace(
292
-				['color=000000&', 'color=ffffff&', 'color=******&'],
293
-				['color=******&', 'color=000000&', 'color=ffffff&'],
294
-				$css
295
-			)
296
-		);
297
-	}
298
-
299
-	/**
300
-	 * @return string SCSS code for variables from OC_Defaults
301
-	 */
302
-	private function getInjectedVariables(): string {
303
-		if ($this->injectedVariables !== null) {
304
-			return $this->injectedVariables;
305
-		}
306
-		$variables = '';
307
-		foreach ($this->defaults->getScssVariables() as $key => $value) {
308
-			$variables .= '$' . $key . ': ' . $value . ';';
309
-		}
310
-
311
-		// check for valid variables / otherwise fall back to defaults
312
-		try {
313
-			$scss = new Compiler();
314
-			$scss->compile($variables);
315
-			$this->injectedVariables = $variables;
316
-		} catch (ParserException $e) {
317
-			$this->logger->logException($e, ['app' => 'core']);
318
-		}
319
-		return $variables;
320
-	}
237
+        }
238
+        $response = new DataDownloadResponse($responseJS, 'javascript', 'text/javascript');
239
+        $response->cacheFor(3600);
240
+        return $response;
241
+    }
242
+
243
+    /**
244
+     * Return an array with the user theme & font settings
245
+     *
246
+     * @return array
247
+     */
248
+    private function getUserValues(): array{
249
+        $userTheme = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'theme', false);
250
+        $userFont  = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'font', false);
251
+        $userHighContrast = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'highcontrast', false);
252
+
253
+        return [$userTheme, $userHighContrast, $userFont];
254
+    }
255
+
256
+    /**
257
+     * Remove all matches from the $rule regex
258
+     *
259
+     * @param string $rule regex to match
260
+     * @param string $css string to parse
261
+     * @return string
262
+     */
263
+    private function filterOutRule(string $rule, string $css): string {
264
+        return preg_replace($rule, '', $css);
265
+    }
266
+
267
+    /**
268
+     * Add the correct uri prefix to make uri valid again
269
+     *
270
+     * @param string $css
271
+     * @param string $webDir
272
+     * @return string
273
+     */
274
+    private function rebaseUrls(string $css, string $webDir): string {
275
+        $re    = '/url\([\'"]([^\/][\.\w?=\/-]*)[\'"]\)/x';
276
+        $subst = 'url(\'' . $webDir . '/$1\')';
277
+
278
+        return preg_replace($re, $subst, $css);
279
+    }
280
+
281
+    /**
282
+     * Remove all matches from the $rule regex
283
+     *
284
+     * @param string $css string to parse
285
+     * @return string
286
+     */
287
+    private function invertSvgIconsColor(string $css) {
288
+        return str_replace(
289
+            ['color=000&', 'color=fff&', 'color=***&'],
290
+            ['color=***&', 'color=000&', 'color=fff&'],
291
+            str_replace(
292
+                ['color=000000&', 'color=ffffff&', 'color=******&'],
293
+                ['color=******&', 'color=000000&', 'color=ffffff&'],
294
+                $css
295
+            )
296
+        );
297
+    }
298
+
299
+    /**
300
+     * @return string SCSS code for variables from OC_Defaults
301
+     */
302
+    private function getInjectedVariables(): string {
303
+        if ($this->injectedVariables !== null) {
304
+            return $this->injectedVariables;
305
+        }
306
+        $variables = '';
307
+        foreach ($this->defaults->getScssVariables() as $key => $value) {
308
+            $variables .= '$' . $key . ': ' . $value . ';';
309
+        }
310
+
311
+        // check for valid variables / otherwise fall back to defaults
312
+        try {
313
+            $scss = new Compiler();
314
+            $scss->compile($variables);
315
+            $this->injectedVariables = $variables;
316
+        } catch (ParserException $e) {
317
+            $this->logger->logException($e, ['app' => 'core']);
318
+        }
319
+        return $variables;
320
+    }
321 321
 }
Please login to merge, or discard this patch.