Passed
Push — master ( e324c9...3ac048 )
by Roeland
19:27 queued 12s
created
apps/accessibility/appinfo/routes.php 1 patch
Indentation   +20 added lines, -20 removed lines patch added patch discarded remove patch
@@ -25,24 +25,24 @@
 block discarded – undo
25 25
  */
26 26
 
27 27
 return [
28
-	'routes' => [
29
-		['name' => 'accessibility#getCss', 'url' => '/css/user-{md5}', 'verb' => 'GET'],
30
-	],
31
-	'ocs' => [
32
-		[
33
-			'name' => 'Config#getConfig',
34
-			'url'  => '/api/v1/config',
35
-			'verb' => 'GET',
36
-		],
37
-		[
38
-			'name' => 'Config#setConfig',
39
-			'url'  => '/api/v1/config/{key}',
40
-			'verb' => 'PUT',
41
-		],
42
-		[
43
-			'name' => 'Config#deleteConfig',
44
-			'url'  => '/api/v1/config/{key}',
45
-			'verb' => 'DELETE',
46
-		],
47
-	]
28
+    'routes' => [
29
+        ['name' => 'accessibility#getCss', 'url' => '/css/user-{md5}', 'verb' => 'GET'],
30
+    ],
31
+    'ocs' => [
32
+        [
33
+            'name' => 'Config#getConfig',
34
+            'url'  => '/api/v1/config',
35
+            'verb' => 'GET',
36
+        ],
37
+        [
38
+            'name' => 'Config#setConfig',
39
+            'url'  => '/api/v1/config/{key}',
40
+            'verb' => 'PUT',
41
+        ],
42
+        [
43
+            'name' => 'Config#deleteConfig',
44
+            'url'  => '/api/v1/config/{key}',
45
+            'verb' => 'DELETE',
46
+        ],
47
+    ]
48 48
 ];
Please login to merge, or discard this patch.
apps/accessibility/composer/composer/autoload_classmap.php 1 patch
Spacing   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -6,12 +6,12 @@
 block discarded – undo
6 6
 $baseDir = $vendorDir;
7 7
 
8 8
 return array(
9
-    'OCA\\Accessibility\\AccessibilityProvider' => $baseDir . '/../lib/AccessibilityProvider.php',
10
-    'OCA\\Accessibility\\AppInfo\\Application' => $baseDir . '/../lib/AppInfo/Application.php',
11
-    'OCA\\Accessibility\\Controller\\AccessibilityController' => $baseDir . '/../lib/Controller/AccessibilityController.php',
12
-    'OCA\\Accessibility\\Controller\\ConfigController' => $baseDir . '/../lib/Controller/ConfigController.php',
13
-    'OCA\\Accessibility\\Migration\\RepairUserConfig' => $baseDir . '/../lib/Migration/RepairUserConfig.php',
14
-    'OCA\\Accessibility\\Service\\JSDataService' => $baseDir . '/../lib/Service/JSDataService.php',
15
-    'OCA\\Accessibility\\Settings\\Personal' => $baseDir . '/../lib/Settings/Personal.php',
16
-    'OCA\\Accessibility\\Settings\\PersonalSection' => $baseDir . '/../lib/Settings/PersonalSection.php',
9
+    'OCA\\Accessibility\\AccessibilityProvider' => $baseDir.'/../lib/AccessibilityProvider.php',
10
+    'OCA\\Accessibility\\AppInfo\\Application' => $baseDir.'/../lib/AppInfo/Application.php',
11
+    'OCA\\Accessibility\\Controller\\AccessibilityController' => $baseDir.'/../lib/Controller/AccessibilityController.php',
12
+    'OCA\\Accessibility\\Controller\\ConfigController' => $baseDir.'/../lib/Controller/ConfigController.php',
13
+    'OCA\\Accessibility\\Migration\\RepairUserConfig' => $baseDir.'/../lib/Migration/RepairUserConfig.php',
14
+    'OCA\\Accessibility\\Service\\JSDataService' => $baseDir.'/../lib/Service/JSDataService.php',
15
+    'OCA\\Accessibility\\Settings\\Personal' => $baseDir.'/../lib/Settings/Personal.php',
16
+    'OCA\\Accessibility\\Settings\\PersonalSection' => $baseDir.'/../lib/Settings/PersonalSection.php',
17 17
 );
Please login to merge, or discard this patch.
apps/accessibility/composer/composer/autoload_static.php 1 patch
Spacing   +15 added lines, -15 removed lines patch added patch discarded remove patch
@@ -6,34 +6,34 @@
 block discarded – undo
6 6
 
7 7
 class ComposerStaticInitAccessibility
8 8
 {
9
-    public static $prefixLengthsPsr4 = array (
9
+    public static $prefixLengthsPsr4 = array(
10 10
         'O' => 
11
-        array (
11
+        array(
12 12
             'OCA\\Accessibility\\' => 18,
13 13
         ),
14 14
     );
15 15
 
16
-    public static $prefixDirsPsr4 = array (
16
+    public static $prefixDirsPsr4 = array(
17 17
         'OCA\\Accessibility\\' => 
18
-        array (
19
-            0 => __DIR__ . '/..' . '/../lib',
18
+        array(
19
+            0 => __DIR__.'/..'.'/../lib',
20 20
         ),
21 21
     );
22 22
 
23
-    public static $classMap = array (
24
-        'OCA\\Accessibility\\AccessibilityProvider' => __DIR__ . '/..' . '/../lib/AccessibilityProvider.php',
25
-        'OCA\\Accessibility\\AppInfo\\Application' => __DIR__ . '/..' . '/../lib/AppInfo/Application.php',
26
-        'OCA\\Accessibility\\Controller\\AccessibilityController' => __DIR__ . '/..' . '/../lib/Controller/AccessibilityController.php',
27
-        'OCA\\Accessibility\\Controller\\ConfigController' => __DIR__ . '/..' . '/../lib/Controller/ConfigController.php',
28
-        'OCA\\Accessibility\\Migration\\RepairUserConfig' => __DIR__ . '/..' . '/../lib/Migration/RepairUserConfig.php',
29
-        'OCA\\Accessibility\\Service\\JSDataService' => __DIR__ . '/..' . '/../lib/Service/JSDataService.php',
30
-        'OCA\\Accessibility\\Settings\\Personal' => __DIR__ . '/..' . '/../lib/Settings/Personal.php',
31
-        'OCA\\Accessibility\\Settings\\PersonalSection' => __DIR__ . '/..' . '/../lib/Settings/PersonalSection.php',
23
+    public static $classMap = array(
24
+        'OCA\\Accessibility\\AccessibilityProvider' => __DIR__.'/..'.'/../lib/AccessibilityProvider.php',
25
+        'OCA\\Accessibility\\AppInfo\\Application' => __DIR__.'/..'.'/../lib/AppInfo/Application.php',
26
+        'OCA\\Accessibility\\Controller\\AccessibilityController' => __DIR__.'/..'.'/../lib/Controller/AccessibilityController.php',
27
+        'OCA\\Accessibility\\Controller\\ConfigController' => __DIR__.'/..'.'/../lib/Controller/ConfigController.php',
28
+        'OCA\\Accessibility\\Migration\\RepairUserConfig' => __DIR__.'/..'.'/../lib/Migration/RepairUserConfig.php',
29
+        'OCA\\Accessibility\\Service\\JSDataService' => __DIR__.'/..'.'/../lib/Service/JSDataService.php',
30
+        'OCA\\Accessibility\\Settings\\Personal' => __DIR__.'/..'.'/../lib/Settings/Personal.php',
31
+        'OCA\\Accessibility\\Settings\\PersonalSection' => __DIR__.'/..'.'/../lib/Settings/PersonalSection.php',
32 32
     );
33 33
 
34 34
     public static function getInitializer(ClassLoader $loader)
35 35
     {
36
-        return \Closure::bind(function () use ($loader) {
36
+        return \Closure::bind(function() use ($loader) {
37 37
             $loader->prefixLengthsPsr4 = ComposerStaticInitAccessibility::$prefixLengthsPsr4;
38 38
             $loader->prefixDirsPsr4 = ComposerStaticInitAccessibility::$prefixDirsPsr4;
39 39
             $loader->classMap = ComposerStaticInitAccessibility::$classMap;
Please login to merge, or discard this patch.
apps/accessibility/lib/Service/JSDataService.php 1 patch
Indentation   +29 added lines, -29 removed lines patch added patch discarded remove patch
@@ -30,33 +30,33 @@
 block discarded – undo
30 30
 use OCP\IUserSession;
31 31
 
32 32
 class JSDataService implements \JsonSerializable {
33
-	/** @var IUserSession */
34
-	private $userSession;
35
-	/** @var IConfig */
36
-	private $config;
37
-
38
-	public function __construct(
39
-		IUserSession $userSession,
40
-		IConfig $config
41
-	) {
42
-		$this->userSession = $userSession;
43
-		$this->config = $config;
44
-	}
45
-
46
-	public function jsonSerialize() {
47
-		$user = $this->userSession->getUser();
48
-
49
-		if ($user === null) {
50
-			$theme = false;
51
-			$highcontrast = false;
52
-		} else {
53
-			$theme = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'theme', false);
54
-			$highcontrast = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'highcontrast', false) !== false;
55
-		}
56
-
57
-		return [
58
-			'theme' => $theme,
59
-			'highcontrast' => $highcontrast,
60
-		];
61
-	}
33
+    /** @var IUserSession */
34
+    private $userSession;
35
+    /** @var IConfig */
36
+    private $config;
37
+
38
+    public function __construct(
39
+        IUserSession $userSession,
40
+        IConfig $config
41
+    ) {
42
+        $this->userSession = $userSession;
43
+        $this->config = $config;
44
+    }
45
+
46
+    public function jsonSerialize() {
47
+        $user = $this->userSession->getUser();
48
+
49
+        if ($user === null) {
50
+            $theme = false;
51
+            $highcontrast = false;
52
+        } else {
53
+            $theme = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'theme', false);
54
+            $highcontrast = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'highcontrast', false) !== false;
55
+        }
56
+
57
+        return [
58
+            'theme' => $theme,
59
+            'highcontrast' => $highcontrast,
60
+        ];
61
+    }
62 62
 }
Please login to merge, or discard this patch.
apps/accessibility/lib/AppInfo/Application.php 2 patches
Indentation   +39 added lines, -39 removed lines patch added patch discarded remove patch
@@ -45,50 +45,50 @@
 block discarded – undo
45 45
 
46 46
 class Application extends App implements IBootstrap {
47 47
 
48
-	/** @var string */
49
-	public const APP_ID = 'accessibility';
48
+    /** @var string */
49
+    public const APP_ID = 'accessibility';
50 50
 
51
-	public function __construct() {
52
-		parent::__construct(self::APP_ID);
53
-	}
51
+    public function __construct() {
52
+        parent::__construct(self::APP_ID);
53
+    }
54 54
 
55
-	public function register(IRegistrationContext $context): void {
56
-	}
55
+    public function register(IRegistrationContext $context): void {
56
+    }
57 57
 
58
-	public function boot(IBootContext $context): void {
59
-		$this->injectCss(
60
-			$context->getAppContainer()->query(IUserSession::class),
61
-			$context->getAppContainer()->query(IConfig::class),
62
-			$context->getAppContainer()->query(IURLGenerator::class)
63
-		);
58
+    public function boot(IBootContext $context): void {
59
+        $this->injectCss(
60
+            $context->getAppContainer()->query(IUserSession::class),
61
+            $context->getAppContainer()->query(IConfig::class),
62
+            $context->getAppContainer()->query(IURLGenerator::class)
63
+        );
64 64
 
65
-		$this->registerInitialState($context->getAppContainer());
66
-	}
65
+        $this->registerInitialState($context->getAppContainer());
66
+    }
67 67
 
68
-	private function injectCss(IUserSession $userSession,
69
-							   IConfig $config,
70
-							   IURLGenerator $urlGenerator) {
71
-		// Inject the fake css on all pages if enabled and user is logged
72
-		$loggedUser = $userSession->getUser();
73
-		if ($loggedUser !== null) {
74
-			$userValues = $config->getUserKeys($loggedUser->getUID(), self::APP_ID);
75
-			// we want to check if any theme or font is enabled.
76
-			if (count($userValues) > 0) {
77
-				$hash = $config->getUserValue($loggedUser->getUID(), self::APP_ID, 'icons-css', md5(implode('-', $userValues)));
78
-				$linkToCSS = $urlGenerator->linkToRoute(self::APP_ID . '.accessibility.getCss', ['md5' => $hash]);
79
-				\OCP\Util::addHeader('link', ['rel' => 'stylesheet', 'href' => $linkToCSS]);
80
-			}
81
-		}
82
-	}
68
+    private function injectCss(IUserSession $userSession,
69
+                                IConfig $config,
70
+                                IURLGenerator $urlGenerator) {
71
+        // Inject the fake css on all pages if enabled and user is logged
72
+        $loggedUser = $userSession->getUser();
73
+        if ($loggedUser !== null) {
74
+            $userValues = $config->getUserKeys($loggedUser->getUID(), self::APP_ID);
75
+            // we want to check if any theme or font is enabled.
76
+            if (count($userValues) > 0) {
77
+                $hash = $config->getUserValue($loggedUser->getUID(), self::APP_ID, 'icons-css', md5(implode('-', $userValues)));
78
+                $linkToCSS = $urlGenerator->linkToRoute(self::APP_ID . '.accessibility.getCss', ['md5' => $hash]);
79
+                \OCP\Util::addHeader('link', ['rel' => 'stylesheet', 'href' => $linkToCSS]);
80
+            }
81
+        }
82
+    }
83 83
 
84
-	private function registerInitialState(IAppContainer $container) {
85
-		/** @var IInitialStateService $initialState */
86
-		$initialState = $container->query(IInitialStateService::class);
84
+    private function registerInitialState(IAppContainer $container) {
85
+        /** @var IInitialStateService $initialState */
86
+        $initialState = $container->query(IInitialStateService::class);
87 87
 
88
-		$initialState->provideLazyInitialState(self::APP_ID, 'data', function () use ($container) {
89
-			/** @var JSDataService $data */
90
-			$data = $container->query(JSDataService::class);
91
-			return $data;
92
-		});
93
-	}
88
+        $initialState->provideLazyInitialState(self::APP_ID, 'data', function () use ($container) {
89
+            /** @var JSDataService $data */
90
+            $data = $container->query(JSDataService::class);
91
+            return $data;
92
+        });
93
+    }
94 94
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -75,7 +75,7 @@  discard block
 block discarded – undo
75 75
 			// we want to check if any theme or font is enabled.
76 76
 			if (count($userValues) > 0) {
77 77
 				$hash = $config->getUserValue($loggedUser->getUID(), self::APP_ID, 'icons-css', md5(implode('-', $userValues)));
78
-				$linkToCSS = $urlGenerator->linkToRoute(self::APP_ID . '.accessibility.getCss', ['md5' => $hash]);
78
+				$linkToCSS = $urlGenerator->linkToRoute(self::APP_ID.'.accessibility.getCss', ['md5' => $hash]);
79 79
 				\OCP\Util::addHeader('link', ['rel' => 'stylesheet', 'href' => $linkToCSS]);
80 80
 			}
81 81
 		}
@@ -85,7 +85,7 @@  discard block
 block discarded – undo
85 85
 		/** @var IInitialStateService $initialState */
86 86
 		$initialState = $container->query(IInitialStateService::class);
87 87
 
88
-		$initialState->provideLazyInitialState(self::APP_ID, 'data', function () use ($container) {
88
+		$initialState->provideLazyInitialState(self::APP_ID, 'data', function() use ($container) {
89 89
 			/** @var JSDataService $data */
90 90
 			$data = $container->query(JSDataService::class);
91 91
 			return $data;
Please login to merge, or discard this patch.
apps/accessibility/lib/Controller/AccessibilityController.php 2 patches
Indentation   +229 added lines, -229 removed lines patch added patch discarded remove patch
@@ -52,233 +52,233 @@
 block discarded – undo
52 52
 
53 53
 class AccessibilityController extends Controller {
54 54
 
55
-	/** @var string */
56
-	protected $appName;
57
-
58
-	/** @var string */
59
-	protected $serverRoot;
60
-
61
-	/** @var IConfig */
62
-	private $config;
63
-
64
-	/** @var IUserManager */
65
-	private $userManager;
66
-
67
-	/** @var ILogger */
68
-	private $logger;
69
-
70
-	/** @var IURLGenerator */
71
-	private $urlGenerator;
72
-
73
-	/** @var ITimeFactory */
74
-	protected $timeFactory;
75
-
76
-	/** @var IUserSession */
77
-	private $userSession;
78
-
79
-	/** @var IAppManager */
80
-	private $appManager;
81
-
82
-	/** @var IconsCacher */
83
-	protected $iconsCacher;
84
-
85
-	/** @var \OC_Defaults */
86
-	private $defaults;
87
-
88
-	/** @var null|string */
89
-	private $injectedVariables;
90
-
91
-	/**
92
-	 * Account constructor.
93
-	 *
94
-	 * @param string $appName
95
-	 * @param IRequest $request
96
-	 * @param IConfig $config
97
-	 * @param IUserManager $userManager
98
-	 * @param ILogger $logger
99
-	 * @param IURLGenerator $urlGenerator
100
-	 * @param ITimeFactory $timeFactory
101
-	 * @param IUserSession $userSession
102
-	 * @param IAppManager $appManager
103
-	 * @param \OC_Defaults $defaults
104
-	 */
105
-	public function __construct(string $appName,
106
-								IRequest $request,
107
-								IConfig $config,
108
-								IUserManager $userManager,
109
-								ILogger $logger,
110
-								IURLGenerator $urlGenerator,
111
-								ITimeFactory $timeFactory,
112
-								IUserSession $userSession,
113
-								IAppManager $appManager,
114
-								IconsCacher $iconsCacher,
115
-								\OC_Defaults $defaults) {
116
-		parent::__construct($appName, $request);
117
-		$this->appName      = $appName;
118
-		$this->config       = $config;
119
-		$this->userManager  = $userManager;
120
-		$this->logger       = $logger;
121
-		$this->urlGenerator = $urlGenerator;
122
-		$this->timeFactory  = $timeFactory;
123
-		$this->userSession  = $userSession;
124
-		$this->appManager   = $appManager;
125
-		$this->iconsCacher  = $iconsCacher;
126
-		$this->defaults     = $defaults;
127
-
128
-		$this->serverRoot = \OC::$SERVERROOT;
129
-		$this->appRoot    = $this->appManager->getAppPath($this->appName);
130
-	}
131
-
132
-	/**
133
-	 * @NoAdminRequired
134
-	 * @NoCSRFRequired
135
-	 * @NoSameSiteCookieRequired
136
-	 *
137
-	 * @return DataDisplayResponse
138
-	 */
139
-	public function getCss(): DataDisplayResponse {
140
-		$css        = '';
141
-		$imports    = '';
142
-		$userValues = $this->getUserValues();
143
-
144
-		foreach ($userValues as $key => $scssFile) {
145
-			if ($scssFile !== false) {
146
-				if ($scssFile === 'highcontrast' && in_array('dark', $userValues)) {
147
-					$scssFile .= 'dark';
148
-				}
149
-				$imports .= '@import "' . $scssFile . '";';
150
-			}
151
-		}
152
-
153
-		if ($imports !== '') {
154
-			$scss = new Compiler();
155
-			$scss->setImportPaths([
156
-				$this->appRoot . '/css/',
157
-				$this->serverRoot . '/core/css/'
158
-			]);
159
-
160
-			// Continue after throw
161
-			$scss->setIgnoreErrors(true);
162
-			$scss->setFormatter(Crunched::class);
163
-
164
-			// Import theme, variables and compile css4 variables
165
-			try {
166
-				$css .= $scss->compile(
167
-					$imports .
168
-					$this->getInjectedVariables() .
169
-					'@import "variables.scss";' .
170
-					'@import "css-variables.scss";'
171
-				);
172
-			} catch (ParserException $e) {
173
-				$this->logger->error($e->getMessage(), ['app' => 'core']);
174
-			}
175
-		}
176
-
177
-		// We don't want to override vars with url since path is different
178
-		$css = $this->filterOutRule('/--[a-z-:]+url\([^;]+\)/mi', $css);
179
-
180
-		// Rebase all urls
181
-		$appWebRoot = substr($this->appRoot, strlen($this->serverRoot) - strlen(\OC::$WEBROOT));
182
-		$css        = $this->rebaseUrls($css, $appWebRoot . '/css');
183
-
184
-		if (in_array('dark', $userValues) && $this->iconsCacher->getCachedList() && $this->iconsCacher->getCachedList()->getSize() > 0) {
185
-			$iconsCss = $this->invertSvgIconsColor($this->iconsCacher->getCachedList()->getContent());
186
-			$css = $css . $iconsCss;
187
-		}
188
-
189
-		$response = new DataDisplayResponse($css, Http::STATUS_OK, ['Content-Type' => 'text/css']);
190
-
191
-		// Set cache control
192
-		$ttl = 31536000;
193
-		$response->addHeader('Cache-Control', 'max-age=' . $ttl . ', immutable');
194
-		$expires = new \DateTime();
195
-		$expires->setTimestamp($this->timeFactory->getTime());
196
-		$expires->add(new \DateInterval('PT' . $ttl . 'S'));
197
-		$response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
198
-		$response->addHeader('Pragma', 'cache');
199
-
200
-		// store current cache hash
201
-		$this->config->setUserValue($this->userSession->getUser()->getUID(), $this->appName, 'icons-css', md5($css));
202
-
203
-		return $response;
204
-	}
205
-
206
-	/**
207
-	 * Return an array with the user theme & font settings
208
-	 *
209
-	 * @return array
210
-	 */
211
-	private function getUserValues(): array {
212
-		$userTheme = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'theme', false);
213
-		$userFont  = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'font', false);
214
-		$userHighContrast = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'highcontrast', false);
215
-
216
-		return [$userTheme, $userHighContrast, $userFont];
217
-	}
218
-
219
-	/**
220
-	 * Remove all matches from the $rule regex
221
-	 *
222
-	 * @param string $rule regex to match
223
-	 * @param string $css string to parse
224
-	 * @return string
225
-	 */
226
-	private function filterOutRule(string $rule, string $css): string {
227
-		return preg_replace($rule, '', $css);
228
-	}
229
-
230
-	/**
231
-	 * Add the correct uri prefix to make uri valid again
232
-	 *
233
-	 * @param string $css
234
-	 * @param string $webDir
235
-	 * @return string
236
-	 */
237
-	private function rebaseUrls(string $css, string $webDir): string {
238
-		$re    = '/url\([\'"]([^\/][\.\w?=\/-]*)[\'"]\)/x';
239
-		$subst = 'url(\'' . $webDir . '/$1\')';
240
-
241
-		return preg_replace($re, $subst, $css);
242
-	}
243
-
244
-	/**
245
-	 * Remove all matches from the $rule regex
246
-	 *
247
-	 * @param string $css string to parse
248
-	 * @return string
249
-	 */
250
-	private function invertSvgIconsColor(string $css) {
251
-		return str_replace(
252
-			['color=000&', 'color=fff&', 'color=***&'],
253
-			['color=***&', 'color=000&', 'color=fff&'],
254
-			str_replace(
255
-				['color=000000&', 'color=ffffff&', 'color=******&'],
256
-				['color=******&', 'color=000000&', 'color=ffffff&'],
257
-				$css
258
-			)
259
-		);
260
-	}
261
-
262
-	/**
263
-	 * @return string SCSS code for variables from OC_Defaults
264
-	 */
265
-	private function getInjectedVariables(): string {
266
-		if ($this->injectedVariables !== null) {
267
-			return $this->injectedVariables;
268
-		}
269
-		$variables = '';
270
-		foreach ($this->defaults->getScssVariables() as $key => $value) {
271
-			$variables .= '$' . $key . ': ' . $value . ';';
272
-		}
273
-
274
-		// check for valid variables / otherwise fall back to defaults
275
-		try {
276
-			$scss = new Compiler();
277
-			$scss->compile($variables);
278
-			$this->injectedVariables = $variables;
279
-		} catch (ParserException $e) {
280
-			$this->logger->logException($e, ['app' => 'core']);
281
-		}
282
-		return $variables;
283
-	}
55
+    /** @var string */
56
+    protected $appName;
57
+
58
+    /** @var string */
59
+    protected $serverRoot;
60
+
61
+    /** @var IConfig */
62
+    private $config;
63
+
64
+    /** @var IUserManager */
65
+    private $userManager;
66
+
67
+    /** @var ILogger */
68
+    private $logger;
69
+
70
+    /** @var IURLGenerator */
71
+    private $urlGenerator;
72
+
73
+    /** @var ITimeFactory */
74
+    protected $timeFactory;
75
+
76
+    /** @var IUserSession */
77
+    private $userSession;
78
+
79
+    /** @var IAppManager */
80
+    private $appManager;
81
+
82
+    /** @var IconsCacher */
83
+    protected $iconsCacher;
84
+
85
+    /** @var \OC_Defaults */
86
+    private $defaults;
87
+
88
+    /** @var null|string */
89
+    private $injectedVariables;
90
+
91
+    /**
92
+     * Account constructor.
93
+     *
94
+     * @param string $appName
95
+     * @param IRequest $request
96
+     * @param IConfig $config
97
+     * @param IUserManager $userManager
98
+     * @param ILogger $logger
99
+     * @param IURLGenerator $urlGenerator
100
+     * @param ITimeFactory $timeFactory
101
+     * @param IUserSession $userSession
102
+     * @param IAppManager $appManager
103
+     * @param \OC_Defaults $defaults
104
+     */
105
+    public function __construct(string $appName,
106
+                                IRequest $request,
107
+                                IConfig $config,
108
+                                IUserManager $userManager,
109
+                                ILogger $logger,
110
+                                IURLGenerator $urlGenerator,
111
+                                ITimeFactory $timeFactory,
112
+                                IUserSession $userSession,
113
+                                IAppManager $appManager,
114
+                                IconsCacher $iconsCacher,
115
+                                \OC_Defaults $defaults) {
116
+        parent::__construct($appName, $request);
117
+        $this->appName      = $appName;
118
+        $this->config       = $config;
119
+        $this->userManager  = $userManager;
120
+        $this->logger       = $logger;
121
+        $this->urlGenerator = $urlGenerator;
122
+        $this->timeFactory  = $timeFactory;
123
+        $this->userSession  = $userSession;
124
+        $this->appManager   = $appManager;
125
+        $this->iconsCacher  = $iconsCacher;
126
+        $this->defaults     = $defaults;
127
+
128
+        $this->serverRoot = \OC::$SERVERROOT;
129
+        $this->appRoot    = $this->appManager->getAppPath($this->appName);
130
+    }
131
+
132
+    /**
133
+     * @NoAdminRequired
134
+     * @NoCSRFRequired
135
+     * @NoSameSiteCookieRequired
136
+     *
137
+     * @return DataDisplayResponse
138
+     */
139
+    public function getCss(): DataDisplayResponse {
140
+        $css        = '';
141
+        $imports    = '';
142
+        $userValues = $this->getUserValues();
143
+
144
+        foreach ($userValues as $key => $scssFile) {
145
+            if ($scssFile !== false) {
146
+                if ($scssFile === 'highcontrast' && in_array('dark', $userValues)) {
147
+                    $scssFile .= 'dark';
148
+                }
149
+                $imports .= '@import "' . $scssFile . '";';
150
+            }
151
+        }
152
+
153
+        if ($imports !== '') {
154
+            $scss = new Compiler();
155
+            $scss->setImportPaths([
156
+                $this->appRoot . '/css/',
157
+                $this->serverRoot . '/core/css/'
158
+            ]);
159
+
160
+            // Continue after throw
161
+            $scss->setIgnoreErrors(true);
162
+            $scss->setFormatter(Crunched::class);
163
+
164
+            // Import theme, variables and compile css4 variables
165
+            try {
166
+                $css .= $scss->compile(
167
+                    $imports .
168
+                    $this->getInjectedVariables() .
169
+                    '@import "variables.scss";' .
170
+                    '@import "css-variables.scss";'
171
+                );
172
+            } catch (ParserException $e) {
173
+                $this->logger->error($e->getMessage(), ['app' => 'core']);
174
+            }
175
+        }
176
+
177
+        // We don't want to override vars with url since path is different
178
+        $css = $this->filterOutRule('/--[a-z-:]+url\([^;]+\)/mi', $css);
179
+
180
+        // Rebase all urls
181
+        $appWebRoot = substr($this->appRoot, strlen($this->serverRoot) - strlen(\OC::$WEBROOT));
182
+        $css        = $this->rebaseUrls($css, $appWebRoot . '/css');
183
+
184
+        if (in_array('dark', $userValues) && $this->iconsCacher->getCachedList() && $this->iconsCacher->getCachedList()->getSize() > 0) {
185
+            $iconsCss = $this->invertSvgIconsColor($this->iconsCacher->getCachedList()->getContent());
186
+            $css = $css . $iconsCss;
187
+        }
188
+
189
+        $response = new DataDisplayResponse($css, Http::STATUS_OK, ['Content-Type' => 'text/css']);
190
+
191
+        // Set cache control
192
+        $ttl = 31536000;
193
+        $response->addHeader('Cache-Control', 'max-age=' . $ttl . ', immutable');
194
+        $expires = new \DateTime();
195
+        $expires->setTimestamp($this->timeFactory->getTime());
196
+        $expires->add(new \DateInterval('PT' . $ttl . 'S'));
197
+        $response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
198
+        $response->addHeader('Pragma', 'cache');
199
+
200
+        // store current cache hash
201
+        $this->config->setUserValue($this->userSession->getUser()->getUID(), $this->appName, 'icons-css', md5($css));
202
+
203
+        return $response;
204
+    }
205
+
206
+    /**
207
+     * Return an array with the user theme & font settings
208
+     *
209
+     * @return array
210
+     */
211
+    private function getUserValues(): array {
212
+        $userTheme = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'theme', false);
213
+        $userFont  = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'font', false);
214
+        $userHighContrast = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'highcontrast', false);
215
+
216
+        return [$userTheme, $userHighContrast, $userFont];
217
+    }
218
+
219
+    /**
220
+     * Remove all matches from the $rule regex
221
+     *
222
+     * @param string $rule regex to match
223
+     * @param string $css string to parse
224
+     * @return string
225
+     */
226
+    private function filterOutRule(string $rule, string $css): string {
227
+        return preg_replace($rule, '', $css);
228
+    }
229
+
230
+    /**
231
+     * Add the correct uri prefix to make uri valid again
232
+     *
233
+     * @param string $css
234
+     * @param string $webDir
235
+     * @return string
236
+     */
237
+    private function rebaseUrls(string $css, string $webDir): string {
238
+        $re    = '/url\([\'"]([^\/][\.\w?=\/-]*)[\'"]\)/x';
239
+        $subst = 'url(\'' . $webDir . '/$1\')';
240
+
241
+        return preg_replace($re, $subst, $css);
242
+    }
243
+
244
+    /**
245
+     * Remove all matches from the $rule regex
246
+     *
247
+     * @param string $css string to parse
248
+     * @return string
249
+     */
250
+    private function invertSvgIconsColor(string $css) {
251
+        return str_replace(
252
+            ['color=000&', 'color=fff&', 'color=***&'],
253
+            ['color=***&', 'color=000&', 'color=fff&'],
254
+            str_replace(
255
+                ['color=000000&', 'color=ffffff&', 'color=******&'],
256
+                ['color=******&', 'color=000000&', 'color=ffffff&'],
257
+                $css
258
+            )
259
+        );
260
+    }
261
+
262
+    /**
263
+     * @return string SCSS code for variables from OC_Defaults
264
+     */
265
+    private function getInjectedVariables(): string {
266
+        if ($this->injectedVariables !== null) {
267
+            return $this->injectedVariables;
268
+        }
269
+        $variables = '';
270
+        foreach ($this->defaults->getScssVariables() as $key => $value) {
271
+            $variables .= '$' . $key . ': ' . $value . ';';
272
+        }
273
+
274
+        // check for valid variables / otherwise fall back to defaults
275
+        try {
276
+            $scss = new Compiler();
277
+            $scss->compile($variables);
278
+            $this->injectedVariables = $variables;
279
+        } catch (ParserException $e) {
280
+            $this->logger->logException($e, ['app' => 'core']);
281
+        }
282
+        return $variables;
283
+    }
284 284
 }
Please login to merge, or discard this patch.
Spacing   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -146,15 +146,15 @@  discard block
 block discarded – undo
146 146
 				if ($scssFile === 'highcontrast' && in_array('dark', $userValues)) {
147 147
 					$scssFile .= 'dark';
148 148
 				}
149
-				$imports .= '@import "' . $scssFile . '";';
149
+				$imports .= '@import "'.$scssFile.'";';
150 150
 			}
151 151
 		}
152 152
 
153 153
 		if ($imports !== '') {
154 154
 			$scss = new Compiler();
155 155
 			$scss->setImportPaths([
156
-				$this->appRoot . '/css/',
157
-				$this->serverRoot . '/core/css/'
156
+				$this->appRoot.'/css/',
157
+				$this->serverRoot.'/core/css/'
158 158
 			]);
159 159
 
160 160
 			// Continue after throw
@@ -164,9 +164,9 @@  discard block
 block discarded – undo
164 164
 			// Import theme, variables and compile css4 variables
165 165
 			try {
166 166
 				$css .= $scss->compile(
167
-					$imports .
168
-					$this->getInjectedVariables() .
169
-					'@import "variables.scss";' .
167
+					$imports.
168
+					$this->getInjectedVariables().
169
+					'@import "variables.scss";'.
170 170
 					'@import "css-variables.scss";'
171 171
 				);
172 172
 			} catch (ParserException $e) {
@@ -179,21 +179,21 @@  discard block
 block discarded – undo
179 179
 
180 180
 		// Rebase all urls
181 181
 		$appWebRoot = substr($this->appRoot, strlen($this->serverRoot) - strlen(\OC::$WEBROOT));
182
-		$css        = $this->rebaseUrls($css, $appWebRoot . '/css');
182
+		$css        = $this->rebaseUrls($css, $appWebRoot.'/css');
183 183
 
184 184
 		if (in_array('dark', $userValues) && $this->iconsCacher->getCachedList() && $this->iconsCacher->getCachedList()->getSize() > 0) {
185 185
 			$iconsCss = $this->invertSvgIconsColor($this->iconsCacher->getCachedList()->getContent());
186
-			$css = $css . $iconsCss;
186
+			$css = $css.$iconsCss;
187 187
 		}
188 188
 
189 189
 		$response = new DataDisplayResponse($css, Http::STATUS_OK, ['Content-Type' => 'text/css']);
190 190
 
191 191
 		// Set cache control
192 192
 		$ttl = 31536000;
193
-		$response->addHeader('Cache-Control', 'max-age=' . $ttl . ', immutable');
193
+		$response->addHeader('Cache-Control', 'max-age='.$ttl.', immutable');
194 194
 		$expires = new \DateTime();
195 195
 		$expires->setTimestamp($this->timeFactory->getTime());
196
-		$expires->add(new \DateInterval('PT' . $ttl . 'S'));
196
+		$expires->add(new \DateInterval('PT'.$ttl.'S'));
197 197
 		$response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
198 198
 		$response->addHeader('Pragma', 'cache');
199 199
 
@@ -236,7 +236,7 @@  discard block
 block discarded – undo
236 236
 	 */
237 237
 	private function rebaseUrls(string $css, string $webDir): string {
238 238
 		$re    = '/url\([\'"]([^\/][\.\w?=\/-]*)[\'"]\)/x';
239
-		$subst = 'url(\'' . $webDir . '/$1\')';
239
+		$subst = 'url(\''.$webDir.'/$1\')';
240 240
 
241 241
 		return preg_replace($re, $subst, $css);
242 242
 	}
@@ -268,7 +268,7 @@  discard block
 block discarded – undo
268 268
 		}
269 269
 		$variables = '';
270 270
 		foreach ($this->defaults->getScssVariables() as $key => $value) {
271
-			$variables .= '$' . $key . ': ' . $value . ';';
271
+			$variables .= '$'.$key.': '.$value.';';
272 272
 		}
273 273
 
274 274
 		// check for valid variables / otherwise fall back to defaults
Please login to merge, or discard this patch.