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