Capabilities.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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-plain: bool,
  80. * background-default: bool,
  81. * logoheader: string,
  82. * favicon: string,
  83. * },
  84. * }
  85. */
  86. public function getCapabilities() {
  87. $color = $this->theming->getDefaultColorPrimary();
  88. // Same as in DefaultTheme
  89. if ($color === BackgroundService::DEFAULT_COLOR) {
  90. $color = BackgroundService::DEFAULT_ACCESSIBLE_COLOR;
  91. }
  92. $colorText = $this->util->invertTextColor($color) ? '#000000' : '#ffffff';
  93. $backgroundLogo = $this->config->getAppValue('theming', 'backgroundMime', '');
  94. $backgroundPlain = $backgroundLogo === 'backgroundColor' || ($backgroundLogo === '' && $color !== '#0082c9');
  95. $background = $backgroundPlain ? $color : $this->url->getAbsoluteURL($this->theming->getBackground());
  96. $user = $this->userSession->getUser();
  97. if ($user instanceof IUser) {
  98. /**
  99. * Mimics the logic of generateUserBackgroundVariables() that generates the CSS variables.
  100. * Also needs to be updated if the logic changes.
  101. * @see \OCA\Theming\Themes\CommonThemeTrait::generateUserBackgroundVariables()
  102. */
  103. $color = $this->theming->getColorPrimary();
  104. if ($color === BackgroundService::DEFAULT_COLOR) {
  105. $color = BackgroundService::DEFAULT_ACCESSIBLE_COLOR;
  106. }
  107. $colorText = $this->util->invertTextColor($color) ? '#000000' : '#ffffff';
  108. $backgroundImage = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'background_image', BackgroundService::BACKGROUND_DEFAULT);
  109. if ($backgroundImage === BackgroundService::BACKGROUND_CUSTOM) {
  110. $backgroundPlain = false;
  111. $background = $this->url->linkToRouteAbsolute('theming.userTheme.getBackground');
  112. } elseif (isset(BackgroundService::SHIPPED_BACKGROUNDS[$backgroundImage])) {
  113. $backgroundPlain = false;
  114. $background = $this->url->linkTo(Application::APP_ID, "img/background/$backgroundImage");
  115. } elseif ($backgroundImage !== BackgroundService::BACKGROUND_DEFAULT) {
  116. $backgroundPlain = true;
  117. $background = $color;
  118. }
  119. }
  120. return [
  121. 'theming' => [
  122. 'name' => $this->theming->getName(),
  123. 'url' => $this->theming->getBaseUrl(),
  124. 'slogan' => $this->theming->getSlogan(),
  125. 'color' => $color,
  126. 'color-text' => $colorText,
  127. 'color-element' => $this->util->elementColor($color),
  128. 'color-element-bright' => $this->util->elementColor($color),
  129. 'color-element-dark' => $this->util->elementColor($color, false),
  130. 'logo' => $this->url->getAbsoluteURL($this->theming->getLogo()),
  131. 'background' => $background,
  132. 'background-plain' => $backgroundPlain,
  133. 'background-default' => !$this->util->isBackgroundThemed(),
  134. 'logoheader' => $this->url->getAbsoluteURL($this->theming->getLogo()),
  135. 'favicon' => $this->url->getAbsoluteURL($this->theming->getLogo()),
  136. ],
  137. ];
  138. }
  139. }