You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AccessibilityController.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. declare (strict_types = 1);
  3. /**
  4. * @copyright Copyright (c) 2018 John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OCA\Accessibility\Controller;
  23. use Leafo\ScssPhp\Compiler;
  24. use Leafo\ScssPhp\Exception\ParserException;
  25. use Leafo\ScssPhp\Formatter\Crunched;
  26. use OCP\AppFramework\Controller;
  27. use OCP\AppFramework\Http;
  28. use OCP\AppFramework\Http\DataDisplayResponse;
  29. use OCP\AppFramework\Utility\ITimeFactory;
  30. use OCP\IConfig;
  31. use OCP\ILogger;
  32. use OCP\IRequest;
  33. use OCP\IURLGenerator;
  34. use OCP\IUserManager;
  35. use OCP\IUserSession;
  36. class AccessibilityController extends Controller {
  37. /** @var string */
  38. protected $appName;
  39. /** @var string */
  40. protected $serverRoot;
  41. /** @var IConfig */
  42. private $config;
  43. /** @var IUserManager */
  44. private $userManager;
  45. /** @var ILogger */
  46. private $logger;
  47. /** @var IURLGenerator */
  48. private $urlGenerator;
  49. /** @var ITimeFactory */
  50. protected $timeFactory;
  51. /** @var IUserSession */
  52. private $userSession;
  53. /**
  54. * Account constructor.
  55. *
  56. * @param string $appName
  57. * @param IRequest $request
  58. * @param IConfig $config
  59. * @param IUserManager $userManager
  60. * @param ILogger $logger
  61. * @param IURLGenerator $urlGenerator
  62. * @param ITimeFactory $timeFactory
  63. * @param IUserSession $userSession
  64. */
  65. public function __construct(string $appName,
  66. IRequest $request,
  67. IConfig $config,
  68. IUserManager $userManager,
  69. ILogger $logger,
  70. IURLGenerator $urlGenerator,
  71. ITimeFactory $timeFactory,
  72. IUserSession $userSession) {
  73. parent::__construct($appName, $request);
  74. $this->config = $config;
  75. $this->userManager = $userManager;
  76. $this->logger = $logger;
  77. $this->urlGenerator = $urlGenerator;
  78. $this->timeFactory = $timeFactory;
  79. $this->userSession = $userSession;
  80. $this->serverRoot = \OC::$SERVERROOT;
  81. $this->appRoot = \OC_App::getAppPath($this->appName);
  82. }
  83. /**
  84. * @NoAdminRequired
  85. * @NoCSRFRequired
  86. *
  87. * @return DataResponse
  88. */
  89. public function getCss(): DataDisplayResponse {
  90. $css = '';
  91. $imports = '';
  92. foreach ($this->getUserValues() as $scssFile) {
  93. if ($scssFile !== false) {
  94. $imports .= '@import "' . $scssFile . '";';
  95. }
  96. }
  97. if ($imports !== '') {
  98. $scss = new Compiler();
  99. $scss->setImportPaths([
  100. $this->appRoot . '/css/',
  101. $this->serverRoot . '/core/css/'
  102. ]);
  103. // Continue after throw
  104. $scss->setIgnoreErrors(true);
  105. $scss->setFormatter(Crunched::class);
  106. // Compile
  107. try {
  108. $css .= $scss->compile(
  109. $imports .
  110. '@import "variables.scss";' .
  111. '@import "css-variables.scss";'
  112. );
  113. } catch (ParserException $e) {
  114. $this->logger->error($e->getMessage(), ['app' => 'core']);
  115. }
  116. }
  117. // We don't want to override vars with url since path is different
  118. $css = $this->filterOutRule('/--[a-z-:]+url\([^;]+\)/mi', $css);
  119. $response = new DataDisplayResponse($css, Http::STATUS_OK, ['Content-Type' => 'text/css']);
  120. $ttl = 31536000;
  121. $response->addHeader('Cache-Control', 'max-age=' . $ttl . ', immutable');
  122. $expires = new \DateTime();
  123. $expires->setTimestamp($this->timeFactory->getTime());
  124. $expires->add(new \DateInterval('PT' . $ttl . 'S'));
  125. $response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
  126. $response->addHeader('Pragma', 'cache');
  127. return $response;
  128. }
  129. private function getUserValues() {
  130. $userTheme = $this->config->getUserValue($this->userSession->getUser()->getUID(), 'accessibility', 'theme', false);
  131. $userFont = $this->config->getUserValue($this->userSession->getUser()->getUID(), 'accessibility', 'font', false);
  132. return [
  133. 'theme' => $userTheme,
  134. 'font' => $userFont
  135. ];
  136. }
  137. private function filterOutRule(string $rule, string $css) {
  138. return preg_replace($rule, '', $css);
  139. }
  140. }