Application.php 3.4 KB

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