2018-06-13 14:16:49 +03:00
|
|
|
<?php
|
2019-12-03 21:57:53 +03:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-06-13 14:16:49 +03:00
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2018 John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
|
2019-09-03 16:49:27 +03:00
|
|
|
* @copyright Copyright (c) 2019 Janis Köhr <janiskoehr@icloud.com>
|
2018-06-13 14:16:49 +03:00
|
|
|
*
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Janis Köhr <janis.koehr@novatec-gmbh.de>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
|
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Thomas Citharel <nextcloud@tcit.fr>
|
2019-12-03 21:57:53 +03:00
|
|
|
*
|
2018-06-13 14:16:49 +03:00
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-06-13 14:16:49 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Accessibility\Controller;
|
|
|
|
|
2019-11-22 22:52:10 +03:00
|
|
|
use OC\Template\IconsCacher;
|
|
|
|
use OCP\App\IAppManager;
|
2018-06-13 14:16:49 +03:00
|
|
|
use OCP\AppFramework\Controller;
|
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\DataDisplayResponse;
|
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\IUserSession;
|
2021-03-05 16:38:36 +03:00
|
|
|
use Psr\Log\LoggerInterface;
|
2020-01-23 16:52:16 +03:00
|
|
|
use ScssPhp\ScssPhp\Compiler;
|
|
|
|
use ScssPhp\ScssPhp\Exception\ParserException;
|
2021-03-09 22:39:40 +03:00
|
|
|
use ScssPhp\ScssPhp\OutputStyle;
|
2018-06-13 14:16:49 +03:00
|
|
|
|
|
|
|
class AccessibilityController extends Controller {
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
protected $appName;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
protected $serverRoot;
|
|
|
|
|
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
|
|
|
|
2021-03-05 16:38:36 +03:00
|
|
|
/** @var LoggerInterface */
|
2018-06-13 14:16:49 +03:00
|
|
|
private $logger;
|
|
|
|
|
|
|
|
/** @var ITimeFactory */
|
|
|
|
protected $timeFactory;
|
|
|
|
|
|
|
|
/** @var IUserSession */
|
|
|
|
private $userSession;
|
|
|
|
|
2018-07-04 11:04:38 +03:00
|
|
|
/** @var IconsCacher */
|
|
|
|
protected $iconsCacher;
|
|
|
|
|
2018-08-15 08:25:31 +03:00
|
|
|
/** @var \OC_Defaults */
|
|
|
|
private $defaults;
|
|
|
|
|
|
|
|
/** @var null|string */
|
|
|
|
private $injectedVariables;
|
|
|
|
|
2020-11-12 00:17:53 +03:00
|
|
|
/** @var string */
|
|
|
|
private $appRoot;
|
|
|
|
|
2018-06-13 14:16:49 +03:00
|
|
|
public function __construct(string $appName,
|
2018-06-14 16:27:57 +03:00
|
|
|
IRequest $request,
|
|
|
|
IConfig $config,
|
2021-03-05 16:38:36 +03:00
|
|
|
LoggerInterface $logger,
|
2018-06-14 16:27:57 +03:00
|
|
|
ITimeFactory $timeFactory,
|
|
|
|
IUserSession $userSession,
|
2018-07-04 11:04:38 +03:00
|
|
|
IAppManager $appManager,
|
2018-08-15 08:25:31 +03:00
|
|
|
IconsCacher $iconsCacher,
|
|
|
|
\OC_Defaults $defaults) {
|
2018-06-13 14:16:49 +03:00
|
|
|
parent::__construct($appName, $request);
|
2020-10-05 16:12:57 +03:00
|
|
|
$this->appName = $appName;
|
|
|
|
$this->config = $config;
|
|
|
|
$this->logger = $logger;
|
|
|
|
$this->timeFactory = $timeFactory;
|
|
|
|
$this->userSession = $userSession;
|
|
|
|
$this->iconsCacher = $iconsCacher;
|
|
|
|
$this->defaults = $defaults;
|
2018-06-13 14:16:49 +03:00
|
|
|
|
|
|
|
$this->serverRoot = \OC::$SERVERROOT;
|
2020-11-12 00:17:53 +03:00
|
|
|
$this->appRoot = $appManager->getAppPath($this->appName);
|
2018-06-13 14:16:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-11 00:47:01 +03:00
|
|
|
* @PublicPage
|
2018-06-13 14:16:49 +03:00
|
|
|
* @NoCSRFRequired
|
2018-11-26 18:22:48 +03:00
|
|
|
* @NoSameSiteCookieRequired
|
2018-06-13 14:16:49 +03:00
|
|
|
*
|
2018-06-14 16:27:57 +03:00
|
|
|
* @return DataDisplayResponse
|
2018-06-13 14:16:49 +03:00
|
|
|
*/
|
|
|
|
public function getCss(): DataDisplayResponse {
|
2020-10-05 16:12:57 +03:00
|
|
|
$css = '';
|
|
|
|
$imports = '';
|
2020-06-11 00:47:01 +03:00
|
|
|
if ($this->userSession->isLoggedIn()) {
|
|
|
|
$userValues = $this->getUserValues();
|
|
|
|
} else {
|
|
|
|
$userValues = ['dark'];
|
|
|
|
}
|
2018-06-13 14:16:49 +03:00
|
|
|
|
2018-06-22 09:25:23 +03:00
|
|
|
foreach ($userValues as $key => $scssFile) {
|
2018-06-13 14:16:49 +03:00
|
|
|
if ($scssFile !== false) {
|
2019-09-03 14:56:45 +03:00
|
|
|
if ($scssFile === 'highcontrast' && in_array('dark', $userValues)) {
|
|
|
|
$scssFile .= 'dark';
|
|
|
|
}
|
2018-06-13 14:16:49 +03:00
|
|
|
$imports .= '@import "' . $scssFile . '";';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($imports !== '') {
|
|
|
|
$scss = new Compiler();
|
|
|
|
$scss->setImportPaths([
|
|
|
|
$this->appRoot . '/css/',
|
|
|
|
$this->serverRoot . '/core/css/'
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Continue after throw
|
2021-03-09 22:39:40 +03:00
|
|
|
$scss->setOutputStyle(OutputStyle::COMPRESSED);
|
2018-06-13 14:16:49 +03:00
|
|
|
|
2018-06-14 16:27:57 +03:00
|
|
|
// Import theme, variables and compile css4 variables
|
2018-06-13 14:16:49 +03:00
|
|
|
try {
|
|
|
|
$css .= $scss->compile(
|
|
|
|
$imports .
|
2018-08-15 08:25:31 +03:00
|
|
|
$this->getInjectedVariables() .
|
2018-08-29 17:46:12 +03:00
|
|
|
'@import "variables.scss";' .
|
2018-06-13 14:16:49 +03:00
|
|
|
'@import "css-variables.scss";'
|
|
|
|
);
|
|
|
|
} catch (ParserException $e) {
|
2021-03-05 16:38:36 +03:00
|
|
|
$this->logger->error($e->getMessage(),
|
|
|
|
[
|
|
|
|
'app' => 'core',
|
|
|
|
'exception' => $e,
|
|
|
|
]
|
|
|
|
);
|
2018-06-13 14:16:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't want to override vars with url since path is different
|
|
|
|
$css = $this->filterOutRule('/--[a-z-:]+url\([^;]+\)/mi', $css);
|
|
|
|
|
2018-06-22 09:25:23 +03:00
|
|
|
// Rebase all urls
|
2018-06-22 11:32:16 +03:00
|
|
|
$appWebRoot = substr($this->appRoot, strlen($this->serverRoot) - strlen(\OC::$WEBROOT));
|
2020-10-05 16:12:57 +03:00
|
|
|
$css = $this->rebaseUrls($css, $appWebRoot . '/css');
|
2018-07-04 11:04:38 +03:00
|
|
|
|
2019-08-30 12:23:09 +03:00
|
|
|
if (in_array('dark', $userValues) && $this->iconsCacher->getCachedList() && $this->iconsCacher->getCachedList()->getSize() > 0) {
|
2018-10-25 12:57:27 +03:00
|
|
|
$iconsCss = $this->invertSvgIconsColor($this->iconsCacher->getCachedList()->getContent());
|
2018-07-04 11:04:38 +03:00
|
|
|
$css = $css . $iconsCss;
|
|
|
|
}
|
2018-06-22 09:25:23 +03:00
|
|
|
|
2018-06-13 14:16:49 +03:00
|
|
|
$response = new DataDisplayResponse($css, Http::STATUS_OK, ['Content-Type' => 'text/css']);
|
|
|
|
|
2018-06-14 16:27:57 +03:00
|
|
|
// Set cache control
|
2018-06-13 14:16:49 +03:00
|
|
|
$ttl = 31536000;
|
|
|
|
$response->addHeader('Cache-Control', 'max-age=' . $ttl . ', immutable');
|
|
|
|
$expires = new \DateTime();
|
|
|
|
$expires->setTimestamp($this->timeFactory->getTime());
|
|
|
|
$expires->add(new \DateInterval('PT' . $ttl . 'S'));
|
|
|
|
$response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
|
|
|
|
$response->addHeader('Pragma', 'cache');
|
|
|
|
|
2018-10-02 10:46:05 +03:00
|
|
|
// store current cache hash
|
2020-06-11 00:47:01 +03:00
|
|
|
if ($this->userSession->isLoggedIn()) {
|
|
|
|
$this->config->setUserValue($this->userSession->getUser()->getUID(), $this->appName, 'icons-css', md5($css));
|
|
|
|
}
|
2018-10-02 10:46:05 +03:00
|
|
|
|
2018-06-13 14:16:49 +03:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2018-06-22 09:25:23 +03:00
|
|
|
/**
|
|
|
|
* Return an array with the user theme & font settings
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2020-04-09 14:53:40 +03:00
|
|
|
private function getUserValues(): array {
|
2018-06-14 16:27:57 +03:00
|
|
|
$userTheme = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'theme', false);
|
2020-10-05 16:12:57 +03:00
|
|
|
$userFont = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'font', false);
|
2019-08-30 12:23:09 +03:00
|
|
|
$userHighContrast = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'highcontrast', false);
|
2018-06-13 14:16:49 +03:00
|
|
|
|
2019-08-30 12:23:09 +03:00
|
|
|
return [$userTheme, $userHighContrast, $userFont];
|
2018-06-13 14:16:49 +03:00
|
|
|
}
|
|
|
|
|
2018-06-22 09:25:23 +03:00
|
|
|
/**
|
|
|
|
* Remove all matches from the $rule regex
|
|
|
|
*
|
|
|
|
* @param string $rule regex to match
|
|
|
|
* @param string $css string to parse
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function filterOutRule(string $rule, string $css): string {
|
2018-06-13 14:16:49 +03:00
|
|
|
return preg_replace($rule, '', $css);
|
|
|
|
}
|
|
|
|
|
2018-06-22 09:25:23 +03:00
|
|
|
/**
|
|
|
|
* Add the correct uri prefix to make uri valid again
|
|
|
|
*
|
|
|
|
* @param string $css
|
|
|
|
* @param string $webDir
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function rebaseUrls(string $css, string $webDir): string {
|
2020-10-05 16:12:57 +03:00
|
|
|
$re = '/url\([\'"]([^\/][\.\w?=\/-]*)[\'"]\)/x';
|
2018-06-22 09:25:23 +03:00
|
|
|
$subst = 'url(\'' . $webDir . '/$1\')';
|
|
|
|
|
|
|
|
return preg_replace($re, $subst, $css);
|
|
|
|
}
|
2018-07-04 11:04:38 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all matches from the $rule regex
|
|
|
|
*
|
|
|
|
* @param string $css string to parse
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function invertSvgIconsColor(string $css) {
|
2019-03-04 13:21:57 +03:00
|
|
|
return str_replace(
|
|
|
|
['color=000&', 'color=fff&', 'color=***&'],
|
|
|
|
['color=***&', 'color=000&', 'color=fff&'],
|
|
|
|
str_replace(
|
|
|
|
['color=000000&', 'color=ffffff&', 'color=******&'],
|
|
|
|
['color=******&', 'color=000000&', 'color=ffffff&'],
|
|
|
|
$css
|
|
|
|
)
|
|
|
|
);
|
2018-07-04 11:04:38 +03:00
|
|
|
}
|
2018-08-15 08:25:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string SCSS code for variables from OC_Defaults
|
|
|
|
*/
|
|
|
|
private function getInjectedVariables(): string {
|
|
|
|
if ($this->injectedVariables !== null) {
|
|
|
|
return $this->injectedVariables;
|
|
|
|
}
|
|
|
|
$variables = '';
|
|
|
|
foreach ($this->defaults->getScssVariables() as $key => $value) {
|
|
|
|
$variables .= '$' . $key . ': ' . $value . ';';
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for valid variables / otherwise fall back to defaults
|
|
|
|
try {
|
|
|
|
$scss = new Compiler();
|
|
|
|
$scss->compile($variables);
|
|
|
|
$this->injectedVariables = $variables;
|
|
|
|
} catch (ParserException $e) {
|
2021-03-05 16:38:36 +03:00
|
|
|
$this->logger->error($e->getMessage(),
|
|
|
|
[
|
|
|
|
'app' => 'core',
|
|
|
|
'exception' => $e,
|
|
|
|
]
|
|
|
|
);
|
2018-08-15 08:25:31 +03:00
|
|
|
}
|
|
|
|
return $variables;
|
|
|
|
}
|
2018-06-13 14:16:49 +03:00
|
|
|
}
|