Passed
Push — master ( 2d7d82...2f678e )
by Christoph
13:15
created
apps/accessibility/lib/Controller/AccessibilityController.php 1 patch
Indentation   +210 added lines, -210 removed lines patch added patch discarded remove patch
@@ -49,214 +49,214 @@
 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 ILogger */
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
-								ILogger $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(), ['app' => 'core']);
150
-			}
151
-		}
152
-
153
-		// We don't want to override vars with url since path is different
154
-		$css = $this->filterOutRule('/--[a-z-:]+url\([^;]+\)/mi', $css);
155
-
156
-		// Rebase all urls
157
-		$appWebRoot = substr($this->appRoot, strlen($this->serverRoot) - strlen(\OC::$WEBROOT));
158
-		$css = $this->rebaseUrls($css, $appWebRoot . '/css');
159
-
160
-		if (in_array('dark', $userValues) && $this->iconsCacher->getCachedList() && $this->iconsCacher->getCachedList()->getSize() > 0) {
161
-			$iconsCss = $this->invertSvgIconsColor($this->iconsCacher->getCachedList()->getContent());
162
-			$css = $css . $iconsCss;
163
-		}
164
-
165
-		$response = new DataDisplayResponse($css, Http::STATUS_OK, ['Content-Type' => 'text/css']);
166
-
167
-		// Set cache control
168
-		$ttl = 31536000;
169
-		$response->addHeader('Cache-Control', 'max-age=' . $ttl . ', immutable');
170
-		$expires = new \DateTime();
171
-		$expires->setTimestamp($this->timeFactory->getTime());
172
-		$expires->add(new \DateInterval('PT' . $ttl . 'S'));
173
-		$response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
174
-		$response->addHeader('Pragma', 'cache');
175
-
176
-		// store current cache hash
177
-		if ($this->userSession->isLoggedIn()) {
178
-			$this->config->setUserValue($this->userSession->getUser()->getUID(), $this->appName, 'icons-css', md5($css));
179
-		}
180
-
181
-		return $response;
182
-	}
183
-
184
-	/**
185
-	 * Return an array with the user theme & font settings
186
-	 *
187
-	 * @return array
188
-	 */
189
-	private function getUserValues(): array {
190
-		$userTheme = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'theme', false);
191
-		$userFont = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'font', false);
192
-		$userHighContrast = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'highcontrast', false);
193
-
194
-		return [$userTheme, $userHighContrast, $userFont];
195
-	}
196
-
197
-	/**
198
-	 * Remove all matches from the $rule regex
199
-	 *
200
-	 * @param string $rule regex to match
201
-	 * @param string $css string to parse
202
-	 * @return string
203
-	 */
204
-	private function filterOutRule(string $rule, string $css): string {
205
-		return preg_replace($rule, '', $css);
206
-	}
207
-
208
-	/**
209
-	 * Add the correct uri prefix to make uri valid again
210
-	 *
211
-	 * @param string $css
212
-	 * @param string $webDir
213
-	 * @return string
214
-	 */
215
-	private function rebaseUrls(string $css, string $webDir): string {
216
-		$re = '/url\([\'"]([^\/][\.\w?=\/-]*)[\'"]\)/x';
217
-		$subst = 'url(\'' . $webDir . '/$1\')';
218
-
219
-		return preg_replace($re, $subst, $css);
220
-	}
221
-
222
-	/**
223
-	 * Remove all matches from the $rule regex
224
-	 *
225
-	 * @param string $css string to parse
226
-	 * @return string
227
-	 */
228
-	private function invertSvgIconsColor(string $css) {
229
-		return str_replace(
230
-			['color=000&', 'color=fff&', 'color=***&'],
231
-			['color=***&', 'color=000&', 'color=fff&'],
232
-			str_replace(
233
-				['color=000000&', 'color=ffffff&', 'color=******&'],
234
-				['color=******&', 'color=000000&', 'color=ffffff&'],
235
-				$css
236
-			)
237
-		);
238
-	}
239
-
240
-	/**
241
-	 * @return string SCSS code for variables from OC_Defaults
242
-	 */
243
-	private function getInjectedVariables(): string {
244
-		if ($this->injectedVariables !== null) {
245
-			return $this->injectedVariables;
246
-		}
247
-		$variables = '';
248
-		foreach ($this->defaults->getScssVariables() as $key => $value) {
249
-			$variables .= '$' . $key . ': ' . $value . ';';
250
-		}
251
-
252
-		// check for valid variables / otherwise fall back to defaults
253
-		try {
254
-			$scss = new Compiler();
255
-			$scss->compile($variables);
256
-			$this->injectedVariables = $variables;
257
-		} catch (ParserException $e) {
258
-			$this->logger->logException($e, ['app' => 'core']);
259
-		}
260
-		return $variables;
261
-	}
52
+    /** @var string */
53
+    protected $appName;
54
+
55
+    /** @var string */
56
+    protected $serverRoot;
57
+
58
+    /** @var IConfig */
59
+    private $config;
60
+
61
+    /** @var ILogger */
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
+                                ILogger $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(), ['app' => 'core']);
150
+            }
151
+        }
152
+
153
+        // We don't want to override vars with url since path is different
154
+        $css = $this->filterOutRule('/--[a-z-:]+url\([^;]+\)/mi', $css);
155
+
156
+        // Rebase all urls
157
+        $appWebRoot = substr($this->appRoot, strlen($this->serverRoot) - strlen(\OC::$WEBROOT));
158
+        $css = $this->rebaseUrls($css, $appWebRoot . '/css');
159
+
160
+        if (in_array('dark', $userValues) && $this->iconsCacher->getCachedList() && $this->iconsCacher->getCachedList()->getSize() > 0) {
161
+            $iconsCss = $this->invertSvgIconsColor($this->iconsCacher->getCachedList()->getContent());
162
+            $css = $css . $iconsCss;
163
+        }
164
+
165
+        $response = new DataDisplayResponse($css, Http::STATUS_OK, ['Content-Type' => 'text/css']);
166
+
167
+        // Set cache control
168
+        $ttl = 31536000;
169
+        $response->addHeader('Cache-Control', 'max-age=' . $ttl . ', immutable');
170
+        $expires = new \DateTime();
171
+        $expires->setTimestamp($this->timeFactory->getTime());
172
+        $expires->add(new \DateInterval('PT' . $ttl . 'S'));
173
+        $response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
174
+        $response->addHeader('Pragma', 'cache');
175
+
176
+        // store current cache hash
177
+        if ($this->userSession->isLoggedIn()) {
178
+            $this->config->setUserValue($this->userSession->getUser()->getUID(), $this->appName, 'icons-css', md5($css));
179
+        }
180
+
181
+        return $response;
182
+    }
183
+
184
+    /**
185
+     * Return an array with the user theme & font settings
186
+     *
187
+     * @return array
188
+     */
189
+    private function getUserValues(): array {
190
+        $userTheme = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'theme', false);
191
+        $userFont = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'font', false);
192
+        $userHighContrast = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'highcontrast', false);
193
+
194
+        return [$userTheme, $userHighContrast, $userFont];
195
+    }
196
+
197
+    /**
198
+     * Remove all matches from the $rule regex
199
+     *
200
+     * @param string $rule regex to match
201
+     * @param string $css string to parse
202
+     * @return string
203
+     */
204
+    private function filterOutRule(string $rule, string $css): string {
205
+        return preg_replace($rule, '', $css);
206
+    }
207
+
208
+    /**
209
+     * Add the correct uri prefix to make uri valid again
210
+     *
211
+     * @param string $css
212
+     * @param string $webDir
213
+     * @return string
214
+     */
215
+    private function rebaseUrls(string $css, string $webDir): string {
216
+        $re = '/url\([\'"]([^\/][\.\w?=\/-]*)[\'"]\)/x';
217
+        $subst = 'url(\'' . $webDir . '/$1\')';
218
+
219
+        return preg_replace($re, $subst, $css);
220
+    }
221
+
222
+    /**
223
+     * Remove all matches from the $rule regex
224
+     *
225
+     * @param string $css string to parse
226
+     * @return string
227
+     */
228
+    private function invertSvgIconsColor(string $css) {
229
+        return str_replace(
230
+            ['color=000&', 'color=fff&', 'color=***&'],
231
+            ['color=***&', 'color=000&', 'color=fff&'],
232
+            str_replace(
233
+                ['color=000000&', 'color=ffffff&', 'color=******&'],
234
+                ['color=******&', 'color=000000&', 'color=ffffff&'],
235
+                $css
236
+            )
237
+        );
238
+    }
239
+
240
+    /**
241
+     * @return string SCSS code for variables from OC_Defaults
242
+     */
243
+    private function getInjectedVariables(): string {
244
+        if ($this->injectedVariables !== null) {
245
+            return $this->injectedVariables;
246
+        }
247
+        $variables = '';
248
+        foreach ($this->defaults->getScssVariables() as $key => $value) {
249
+            $variables .= '$' . $key . ': ' . $value . ';';
250
+        }
251
+
252
+        // check for valid variables / otherwise fall back to defaults
253
+        try {
254
+            $scss = new Compiler();
255
+            $scss->compile($variables);
256
+            $this->injectedVariables = $variables;
257
+        } catch (ParserException $e) {
258
+            $this->logger->logException($e, ['app' => 'core']);
259
+        }
260
+        return $variables;
261
+    }
262 262
 }
Please login to merge, or discard this patch.