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