Capabilities.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Guillaume COMPAGNON <gcompagnon@outlook.com>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Julien Veyssier <eneiluj@posteo.net>
  8. * @author Julius Härtl <jus@bitgrid.net>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Kate Döen <kate.doeen@nextcloud.com>
  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\Theming;
  29. use OCA\Theming\AppInfo\Application;
  30. use OCA\Theming\Service\BackgroundService;
  31. use OCP\Capabilities\IPublicCapability;
  32. use OCP\IConfig;
  33. use OCP\IURLGenerator;
  34. use OCP\IUser;
  35. use OCP\IUserSession;
  36. /**
  37. * Class Capabilities
  38. *
  39. * @package OCA\Theming
  40. */
  41. class Capabilities implements IPublicCapability {
  42. /** @var ThemingDefaults */
  43. protected $theming;
  44. /** @var Util */
  45. protected $util;
  46. /** @var IURLGenerator */
  47. protected $url;
  48. /** @var IConfig */
  49. protected $config;
  50. protected IUserSession $userSession;
  51. /**
  52. * @param ThemingDefaults $theming
  53. * @param Util $util
  54. * @param IURLGenerator $url
  55. * @param IConfig $config
  56. */
  57. public function __construct(ThemingDefaults $theming, Util $util, IURLGenerator $url, IConfig $config, IUserSession $userSession) {
  58. $this->theming = $theming;
  59. $this->util = $util;
  60. $this->url = $url;
  61. $this->config = $config;
  62. $this->userSession = $userSession;
  63. }
  64. /**
  65. * Return this classes capabilities
  66. *
  67. * @return array{
  68. * theming: array{
  69. * name: string,
  70. * url: string,
  71. * slogan: string,
  72. * color: string,
  73. * color-text: string,
  74. * color-element: string,
  75. * color-element-bright: string,
  76. * color-element-dark: string,
  77. * logo: string,
  78. * background: string,
  79. * background-text: string,
  80. * background-plain: bool,
  81. * background-default: bool,
  82. * logoheader: string,
  83. * favicon: string,
  84. * },
  85. * }
  86. */
  87. public function getCapabilities() {
  88. $color = $this->theming->getDefaultColorPrimary();
  89. $colorText = $this->util->invertTextColor($color) ? '#000000' : '#ffffff';
  90. $backgroundLogo = $this->config->getAppValue('theming', 'backgroundMime', '');
  91. $backgroundColor = $this->theming->getColorBackground();
  92. $backgroundText = $this->theming->getTextColorBackground();
  93. $backgroundPlain = $backgroundLogo === 'backgroundColor' || ($backgroundLogo === '' && $backgroundColor !== BackgroundService::DEFAULT_COLOR);
  94. $background = $backgroundPlain ? $backgroundColor : $this->url->getAbsoluteURL($this->theming->getBackground());
  95. $user = $this->userSession->getUser();
  96. if ($user instanceof IUser) {
  97. /**
  98. * Mimics the logic of generateUserBackgroundVariables() that generates the CSS variables.
  99. * Also needs to be updated if the logic changes.
  100. * @see \OCA\Theming\Themes\CommonThemeTrait::generateUserBackgroundVariables()
  101. */
  102. $color = $this->theming->getColorPrimary();
  103. $colorText = $this->theming->getTextColorPrimary();
  104. $backgroundImage = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'background_image', BackgroundService::BACKGROUND_DEFAULT);
  105. if ($backgroundImage === BackgroundService::BACKGROUND_CUSTOM) {
  106. $backgroundPlain = false;
  107. $background = $this->url->linkToRouteAbsolute('theming.userTheme.getBackground');
  108. } elseif (isset(BackgroundService::SHIPPED_BACKGROUNDS[$backgroundImage])) {
  109. $backgroundPlain = false;
  110. $background = $this->url->linkTo(Application::APP_ID, "img/background/$backgroundImage");
  111. } elseif ($backgroundImage !== BackgroundService::BACKGROUND_DEFAULT) {
  112. $backgroundPlain = true;
  113. $background = $backgroundColor;
  114. }
  115. }
  116. return [
  117. 'theming' => [
  118. 'name' => $this->theming->getName(),
  119. 'url' => $this->theming->getBaseUrl(),
  120. 'slogan' => $this->theming->getSlogan(),
  121. 'color' => $color,
  122. 'color-text' => $colorText,
  123. 'color-element' => $this->util->elementColor($color),
  124. 'color-element-bright' => $this->util->elementColor($color),
  125. 'color-element-dark' => $this->util->elementColor($color, false),
  126. 'logo' => $this->url->getAbsoluteURL($this->theming->getLogo()),
  127. 'background' => $background,
  128. 'background-text' => $backgroundText,
  129. 'background-plain' => $backgroundPlain,
  130. 'background-default' => !$this->util->isBackgroundThemed(),
  131. 'logoheader' => $this->url->getAbsoluteURL($this->theming->getLogo()),
  132. 'favicon' => $this->url->getAbsoluteURL($this->theming->getLogo()),
  133. ],
  134. ];
  135. }
  136. }