diff options
Diffstat (limited to 'apps/accessibility/lib/Controller')
-rw-r--r-- | apps/accessibility/lib/Controller/AccessibilityController.php | 262 | ||||
-rw-r--r-- | apps/accessibility/lib/Controller/ConfigController.php | 159 |
2 files changed, 0 insertions, 421 deletions
diff --git a/apps/accessibility/lib/Controller/AccessibilityController.php b/apps/accessibility/lib/Controller/AccessibilityController.php deleted file mode 100644 index d455283dbaf..00000000000 --- a/apps/accessibility/lib/Controller/AccessibilityController.php +++ /dev/null @@ -1,262 +0,0 @@ -<?php - -declare(strict_types=1); - -/** - * @copyright Copyright (c) 2018 John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com> - * @copyright Copyright (c) 2019 Janis Köhr <janiskoehr@icloud.com> - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Janis Köhr <janis.koehr@novatec-gmbh.de> - * @author Joas Schilling <coding@schilljs.com> - * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com> - * @author Julius Härtl <jus@bitgrid.net> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Citharel <nextcloud@tcit.fr> - * - * @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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * - */ - -namespace OCA\Accessibility\Controller; - -use OC\Template\IconsCacher; -use OCP\App\IAppManager; -use OCP\AppFramework\Controller; -use OCP\AppFramework\Http; -use OCP\AppFramework\Http\DataDisplayResponse; -use OCP\AppFramework\Utility\ITimeFactory; -use OCP\IConfig; -use OCP\ILogger; -use OCP\IRequest; -use OCP\IUserSession; -use ScssPhp\ScssPhp\Compiler; -use ScssPhp\ScssPhp\Exception\ParserException; -use ScssPhp\ScssPhp\Formatter\Crunched; - -class AccessibilityController extends Controller { - - /** @var string */ - protected $appName; - - /** @var string */ - protected $serverRoot; - - /** @var IConfig */ - private $config; - - /** @var ILogger */ - private $logger; - - /** @var ITimeFactory */ - protected $timeFactory; - - /** @var IUserSession */ - private $userSession; - - /** @var IconsCacher */ - protected $iconsCacher; - - /** @var \OC_Defaults */ - private $defaults; - - /** @var null|string */ - private $injectedVariables; - - /** @var string */ - private $appRoot; - - public function __construct(string $appName, - IRequest $request, - IConfig $config, - ILogger $logger, - ITimeFactory $timeFactory, - IUserSession $userSession, - IAppManager $appManager, - IconsCacher $iconsCacher, - \OC_Defaults $defaults) { - parent::__construct($appName, $request); - $this->appName = $appName; - $this->config = $config; - $this->logger = $logger; - $this->timeFactory = $timeFactory; - $this->userSession = $userSession; - $this->iconsCacher = $iconsCacher; - $this->defaults = $defaults; - - $this->serverRoot = \OC::$SERVERROOT; - $this->appRoot = $appManager->getAppPath($this->appName); - } - - /** - * @PublicPage - * @NoCSRFRequired - * @NoSameSiteCookieRequired - * - * @return DataDisplayResponse - */ - public function getCss(): DataDisplayResponse { - $css = ''; - $imports = ''; - if ($this->userSession->isLoggedIn()) { - $userValues = $this->getUserValues(); - } else { - $userValues = ['dark']; - } - - foreach ($userValues as $key => $scssFile) { - if ($scssFile !== false) { - if ($scssFile === 'highcontrast' && in_array('dark', $userValues)) { - $scssFile .= 'dark'; - } - $imports .= '@import "' . $scssFile . '";'; - } - } - - if ($imports !== '') { - $scss = new Compiler(); - $scss->setImportPaths([ - $this->appRoot . '/css/', - $this->serverRoot . '/core/css/' - ]); - - // Continue after throw - $scss->setIgnoreErrors(true); - $scss->setFormatter(Crunched::class); - - // Import theme, variables and compile css4 variables - try { - $css .= $scss->compile( - $imports . - $this->getInjectedVariables() . - '@import "variables.scss";' . - '@import "css-variables.scss";' - ); - } catch (ParserException $e) { - $this->logger->error($e->getMessage(), ['app' => 'core']); - } - } - - // We don't want to override vars with url since path is different - $css = $this->filterOutRule('/--[a-z-:]+url\([^;]+\)/mi', $css); - - // Rebase all urls - $appWebRoot = substr($this->appRoot, strlen($this->serverRoot) - strlen(\OC::$WEBROOT)); - $css = $this->rebaseUrls($css, $appWebRoot . '/css'); - - if (in_array('dark', $userValues) && $this->iconsCacher->getCachedList() && $this->iconsCacher->getCachedList()->getSize() > 0) { - $iconsCss = $this->invertSvgIconsColor($this->iconsCacher->getCachedList()->getContent()); - $css = $css . $iconsCss; - } - - $response = new DataDisplayResponse($css, Http::STATUS_OK, ['Content-Type' => 'text/css']); - - // Set cache control - $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'); - - // store current cache hash - if ($this->userSession->isLoggedIn()) { - $this->config->setUserValue($this->userSession->getUser()->getUID(), $this->appName, 'icons-css', md5($css)); - } - - return $response; - } - - /** - * Return an array with the user theme & font settings - * - * @return array - */ - private function getUserValues(): array { - $userTheme = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'theme', false); - $userFont = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'font', false); - $userHighContrast = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'highcontrast', false); - - return [$userTheme, $userHighContrast, $userFont]; - } - - /** - * 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 { - return preg_replace($rule, '', $css); - } - - /** - * 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 { - $re = '/url\([\'"]([^\/][\.\w?=\/-]*)[\'"]\)/x'; - $subst = 'url(\'' . $webDir . '/$1\')'; - - return preg_replace($re, $subst, $css); - } - - /** - * Remove all matches from the $rule regex - * - * @param string $css string to parse - * @return string - */ - private function invertSvgIconsColor(string $css) { - 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 - ) - ); - } - - /** - * @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) { - $this->logger->logException($e, ['app' => 'core']); - } - return $variables; - } -} diff --git a/apps/accessibility/lib/Controller/ConfigController.php b/apps/accessibility/lib/Controller/ConfigController.php deleted file mode 100644 index b918b33f31b..00000000000 --- a/apps/accessibility/lib/Controller/ConfigController.php +++ /dev/null @@ -1,159 +0,0 @@ -<?php - -declare(strict_types=1); - -/** - * @copyright Copyright (c) 2018 John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com> - * @copyright Copyright (c) 2019 Janis Köhr <janiskoehr@icloud.com> - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Daniel Kesselberg <mail@danielkesselberg.de> - * @author Janis Köhr <janis.koehr@novatec-gmbh.de> - * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * - */ - -namespace OCA\Accessibility\Controller; - -use OCA\Accessibility\AccessibilityProvider; -use OCP\AppFramework\Http\DataResponse; -use OCP\AppFramework\OCS\OCSBadRequestException; -use OCP\AppFramework\OCSController; -use OCP\IConfig; -use OCP\IRequest; -use OCP\IUserSession; -use OCP\PreConditionNotMetException; - -class ConfigController extends OCSController { - - /** @var string */ - protected $appName; - - /** @var string */ - protected $userId; - - /** @var string */ - protected $serverRoot; - - /** @var IConfig */ - private $config; - - /** @var IUserSession */ - private $userSession; - - /** @var AccessibilityProvider */ - private $accessibilityProvider; - - /** - * Config constructor. - * - * @param string $appName - * @param IRequest $request - * @param IConfig $config - * @param IUserSession $userSession - * @param AccessibilityProvider $accessibilityProvider - */ - public function __construct(string $appName, - IRequest $request, - IConfig $config, - IUserSession $userSession, - AccessibilityProvider $accessibilityProvider) { - parent::__construct($appName, $request); - $this->appName = $appName; - $this->config = $config; - $this->userSession = $userSession; - $this->accessibilityProvider = $accessibilityProvider; - $this->userId = $userSession->getUser()->getUID(); - } - - /** - * @NoAdminRequired - * - * Get user accessibility config - * - * @param string $key theme or font - * @return DataResponse - */ - public function getConfig(): DataResponse { - return new DataResponse([ - 'highcontrast' => $this->config->getUserValue($this->userId, $this->appName, 'highcontrast', false), - 'theme' => $this->config->getUserValue($this->userId, $this->appName, 'theme', false), - 'font' => $this->config->getUserValue($this->userId, $this->appName, 'font', false) - ]); - } - - /** - * @NoAdminRequired - * - * Set theme or font config - * - * @param string $key theme or font - * @return DataResponse - * @throws OCSBadRequestException|PreConditionNotMetException - */ - public function setConfig(string $key, $value): DataResponse { - if ($key === 'theme' || $key === 'font' || $key === 'highcontrast') { - if ($value === false || $value === '') { - throw new OCSBadRequestException('Invalid value: ' . $value); - } - - $themes = $this->accessibilityProvider->getThemes(); - $highcontrast = [$this->accessibilityProvider->getHighContrast()]; - $fonts = $this->accessibilityProvider->getFonts(); - - $availableOptions = array_map(function ($option): string { - return $option['id']; - }, array_merge($themes, $highcontrast, $fonts)); - - if (in_array($value, $availableOptions)) { - $this->config->setUserValue($this->userId, $this->appName, $key, $value); - return new DataResponse(); - } - - throw new OCSBadRequestException('Invalid value: ' . $value); - } - - throw new OCSBadRequestException('Invalid key: ' . $key); - } - - /** - * @NoAdminRequired - * - * Unset theme or font config - * - * @param string $key theme or font - * @return DataResponse - * @throws OCSBadRequestException - */ - public function deleteConfig(string $key): DataResponse { - if ($key === 'theme' || $key === 'font' || $key === 'highcontrast') { - $this->config->deleteUserValue($this->userId, $this->appName, $key); - $userValues = $this->config->getUserKeys($this->userId, $this->appName); - - // remove hash if no settings selected - if (count($userValues) === 1 && $userValues[0] === 'icons-css') { - $this->config->deleteUserValue($this->userId, $this->appName, 'icons-css'); - } - - return new DataResponse(); - } - - throw new OCSBadRequestException('Invalid key: ' . $key); - } -} |