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