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.

Application.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018 John Molakvoæ <skjnldsv@protonmail.com>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  9. * @author Julius Härtl <jus@bitgrid.net>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license GNU AGPL version 3 or any later version
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License as
  16. * published by the Free Software Foundation, either version 3 of the
  17. * License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. */
  28. namespace OCA\Accessibility\AppInfo;
  29. use OCA\Accessibility\Service\JSDataService;
  30. use OCP\AppFramework\App;
  31. use OCP\AppFramework\Bootstrap\IBootContext;
  32. use OCP\AppFramework\Bootstrap\IBootstrap;
  33. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  34. use OCP\IConfig;
  35. use OCP\IURLGenerator;
  36. use OCP\IUserSession;
  37. use function count;
  38. use function implode;
  39. use function md5;
  40. class Application extends App implements IBootstrap {
  41. /** @var string */
  42. public const APP_ID = 'accessibility';
  43. public function __construct() {
  44. parent::__construct(self::APP_ID);
  45. }
  46. public function register(IRegistrationContext $context): void {
  47. $context->registerInitialStateProvider(JSDataService::class);
  48. }
  49. public function boot(IBootContext $context): void {
  50. $context->injectFn([$this, 'injectCss']);
  51. }
  52. public function injectCss(IUserSession $userSession,
  53. IConfig $config,
  54. IURLGenerator $urlGenerator) {
  55. // Inject the fake css on all pages if enabled and user is logged
  56. $loggedUser = $userSession->getUser();
  57. if ($loggedUser !== null) {
  58. $userValues = $config->getUserKeys($loggedUser->getUID(), self::APP_ID);
  59. // we want to check if any theme or font is enabled.
  60. if (count($userValues) > 0) {
  61. $hash = $config->getUserValue($loggedUser->getUID(), self::APP_ID, 'icons-css', md5(implode('-', $userValues)));
  62. $linkToCSS = $urlGenerator->linkToRoute(self::APP_ID . '.accessibility.getCss', ['md5' => $hash]);
  63. \OCP\Util::addHeader('link', ['rel' => 'stylesheet', 'href' => $linkToCSS]);
  64. }
  65. \OCP\Util::addScript('accessibility', 'accessibilityoca');
  66. } else {
  67. $userValues = ['dark'];
  68. $hash = md5(implode('-', $userValues));
  69. $linkToCSS = $urlGenerator->linkToRoute(self::APP_ID . '.accessibility.getCss', ['md5' => $hash]);
  70. \OCP\Util::addHeader('link', ['rel' => 'stylesheet', 'media' => '(prefers-color-scheme: dark)', 'href' => $linkToCSS]);
  71. }
  72. }
  73. }