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.

ThemeInjectionService.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2022 John Molakvoæ <skjnldsv@protonmail.com>
  4. *
  5. * @author John Molakvoæ <skjnldsv@protonmail.com>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\Theming\Service;
  24. use OCA\Theming\ITheme;
  25. use OCA\Theming\Themes\DefaultTheme;
  26. use OCA\Theming\Util;
  27. use OCP\IConfig;
  28. use OCP\IURLGenerator;
  29. use OCP\IUserSession;
  30. class ThemeInjectionService {
  31. private IURLGenerator $urlGenerator;
  32. private ThemesService $themesService;
  33. private DefaultTheme $defaultTheme;
  34. private Util $util;
  35. private IConfig $config;
  36. private ?string $userId;
  37. public function __construct(IURLGenerator $urlGenerator,
  38. ThemesService $themesService,
  39. DefaultTheme $defaultTheme,
  40. Util $util,
  41. IConfig $config,
  42. IUserSession $userSession) {
  43. $this->urlGenerator = $urlGenerator;
  44. $this->themesService = $themesService;
  45. $this->defaultTheme = $defaultTheme;
  46. $this->util = $util;
  47. $this->config = $config;
  48. if ($userSession->getUser() !== null) {
  49. $this->userId = $userSession->getUser()->getUID();
  50. } else {
  51. $this->userId = null;
  52. }
  53. }
  54. public function injectHeaders(): void {
  55. $themes = $this->themesService->getThemes();
  56. $defaultTheme = $themes[$this->defaultTheme->getId()];
  57. $mediaThemes = array_filter($themes, function ($theme) {
  58. // Check if the theme provides a media query
  59. return (bool)$theme->getMediaQuery();
  60. });
  61. // Default theme fallback
  62. $this->addThemeHeaders($defaultTheme);
  63. // Themes applied by media queries
  64. foreach($mediaThemes as $theme) {
  65. $this->addThemeHeaders($theme, true, $theme->getMediaQuery());
  66. }
  67. // Themes
  68. foreach($this->themesService->getThemes() as $theme) {
  69. // Ignore default theme as already processed first
  70. if ($theme->getId() === $this->defaultTheme->getId()) {
  71. continue;
  72. }
  73. $this->addThemeHeaders($theme, false);
  74. }
  75. // Meta headers
  76. $this->addThemeMetaHeaders($themes);
  77. }
  78. /**
  79. * Inject theme header into rendered page
  80. *
  81. * @param ITheme $theme the theme
  82. * @param bool $plain request the :root syntax
  83. * @param string $media media query to use in the <link> element
  84. */
  85. private function addThemeHeaders(ITheme $theme, bool $plain = true, ?string $media = null): void {
  86. $linkToCSS = $this->urlGenerator->linkToRoute('theming.Theming.getThemeStylesheet', [
  87. 'themeId' => $theme->getId(),
  88. 'plain' => $plain,
  89. 'v' => $this->util->getCacheBuster(),
  90. ]);
  91. \OCP\Util::addHeader('link', [
  92. 'rel' => 'stylesheet',
  93. 'media' => $media,
  94. 'href' => $linkToCSS,
  95. 'class' => 'theme'
  96. ]);
  97. }
  98. /**
  99. * Inject meta headers into rendered page
  100. *
  101. * @param ITheme[] $themes the theme
  102. */
  103. private function addThemeMetaHeaders(array $themes): void {
  104. $metaHeaders = [];
  105. // Meta headers
  106. foreach($this->themesService->getThemes() as $theme) {
  107. if (!empty($theme->getMeta())) {
  108. foreach($theme->getMeta() as $meta) {
  109. if (!isset($meta['name']) || !isset($meta['content'])) {
  110. continue;
  111. }
  112. if (!isset($metaHeaders[$meta['name']])) {
  113. $metaHeaders[$meta['name']] = [];
  114. }
  115. $metaHeaders[$meta['name']][] = $meta['content'];
  116. }
  117. }
  118. }
  119. foreach($metaHeaders as $name => $content) {
  120. \OCP\Util::addHeader('meta', [
  121. 'name' => $name,
  122. 'content' => join(' ', array_unique($content)),
  123. ]);
  124. }
  125. }
  126. }