1
0

ThemingDefaults.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Theming;
  7. use OCA\Theming\AppInfo\Application;
  8. use OCA\Theming\Service\BackgroundService;
  9. use OCP\App\AppPathNotFoundException;
  10. use OCP\App\IAppManager;
  11. use OCP\Files\NotFoundException;
  12. use OCP\Files\SimpleFS\ISimpleFile;
  13. use OCP\IAppConfig;
  14. use OCP\ICacheFactory;
  15. use OCP\IConfig;
  16. use OCP\IL10N;
  17. use OCP\INavigationManager;
  18. use OCP\IURLGenerator;
  19. use OCP\IUserSession;
  20. class ThemingDefaults extends \OC_Defaults {
  21. private string $name;
  22. private string $title;
  23. private string $entity;
  24. private string $productName;
  25. private string $url;
  26. private string $backgroundColor;
  27. private string $primaryColor;
  28. private string $docBaseUrl;
  29. private string $iTunesAppId;
  30. private string $iOSClientUrl;
  31. private string $AndroidClientUrl;
  32. private string $FDroidClientUrl;
  33. /**
  34. * ThemingDefaults constructor.
  35. */
  36. public function __construct(
  37. private IConfig $config,
  38. private IAppConfig $appConfig,
  39. private IL10N $l,
  40. private IUserSession $userSession,
  41. private IURLGenerator $urlGenerator,
  42. private ICacheFactory $cacheFactory,
  43. private Util $util,
  44. private ImageManager $imageManager,
  45. private IAppManager $appManager,
  46. private INavigationManager $navigationManager,
  47. private BackgroundService $backgroundService,
  48. ) {
  49. parent::__construct();
  50. $this->name = parent::getName();
  51. $this->title = parent::getTitle();
  52. $this->entity = parent::getEntity();
  53. $this->productName = parent::getProductName();
  54. $this->url = parent::getBaseUrl();
  55. $this->primaryColor = parent::getColorPrimary();
  56. $this->backgroundColor = parent::getColorBackground();
  57. $this->iTunesAppId = parent::getiTunesAppId();
  58. $this->iOSClientUrl = parent::getiOSClientUrl();
  59. $this->AndroidClientUrl = parent::getAndroidClientUrl();
  60. $this->FDroidClientUrl = parent::getFDroidClientUrl();
  61. $this->docBaseUrl = parent::getDocBaseUrl();
  62. }
  63. public function getName() {
  64. return strip_tags($this->config->getAppValue('theming', 'name', $this->name));
  65. }
  66. public function getHTMLName() {
  67. return $this->config->getAppValue('theming', 'name', $this->name);
  68. }
  69. public function getTitle() {
  70. return strip_tags($this->config->getAppValue('theming', 'name', $this->title));
  71. }
  72. public function getEntity() {
  73. return strip_tags($this->config->getAppValue('theming', 'name', $this->entity));
  74. }
  75. public function getProductName() {
  76. return strip_tags($this->config->getAppValue('theming', 'productName', $this->productName));
  77. }
  78. public function getBaseUrl() {
  79. return $this->config->getAppValue('theming', 'url', $this->url);
  80. }
  81. /**
  82. * We pass a string and sanitizeHTML will return a string too in that case
  83. * @psalm-suppress InvalidReturnStatement
  84. * @psalm-suppress InvalidReturnType
  85. */
  86. public function getSlogan(?string $lang = null) {
  87. return \OCP\Util::sanitizeHTML($this->config->getAppValue('theming', 'slogan', parent::getSlogan($lang)));
  88. }
  89. public function getImprintUrl() {
  90. return (string)$this->config->getAppValue('theming', 'imprintUrl', '');
  91. }
  92. public function getPrivacyUrl() {
  93. return (string)$this->config->getAppValue('theming', 'privacyUrl', '');
  94. }
  95. public function getDocBaseUrl() {
  96. return (string)$this->config->getAppValue('theming', 'docBaseUrl', $this->docBaseUrl);
  97. }
  98. public function getShortFooter() {
  99. $slogan = $this->getSlogan();
  100. $baseUrl = $this->getBaseUrl();
  101. $entity = $this->getEntity();
  102. $footer = '';
  103. if ($entity !== '') {
  104. if ($baseUrl !== '') {
  105. $footer = '<a href="' . $baseUrl . '" target="_blank"' .
  106. ' rel="noreferrer noopener" class="entity-name">' . $entity . '</a>';
  107. } else {
  108. $footer = '<span class="entity-name">' . $entity . '</span>';
  109. }
  110. }
  111. $footer .= ($slogan !== '' ? ' – ' . $slogan : '');
  112. $links = [
  113. [
  114. 'text' => $this->l->t('Legal notice'),
  115. 'url' => (string)$this->getImprintUrl()
  116. ],
  117. [
  118. 'text' => $this->l->t('Privacy policy'),
  119. 'url' => (string)$this->getPrivacyUrl()
  120. ],
  121. ];
  122. $navigation = $this->navigationManager->getAll(INavigationManager::TYPE_GUEST);
  123. $guestNavigation = array_map(function ($nav) {
  124. return [
  125. 'text' => $nav['name'],
  126. 'url' => $nav['href']
  127. ];
  128. }, $navigation);
  129. $links = array_merge($links, $guestNavigation);
  130. $legalLinks = '';
  131. $divider = '';
  132. foreach ($links as $link) {
  133. if ($link['url'] !== ''
  134. && filter_var($link['url'], FILTER_VALIDATE_URL)
  135. ) {
  136. $legalLinks .= $divider . '<a href="' . $link['url'] . '" class="legal" target="_blank"' .
  137. ' rel="noreferrer noopener">' . $link['text'] . '</a>';
  138. $divider = ' · ';
  139. }
  140. }
  141. if ($legalLinks !== '') {
  142. $footer .= '<br/><span class="footer__legal-links">' . $legalLinks . '</span>';
  143. }
  144. return $footer;
  145. }
  146. /**
  147. * Color that is used for highlighting elements like important buttons
  148. * If user theming is enabled then the user defined value is returned
  149. */
  150. public function getColorPrimary(): string {
  151. $user = $this->userSession->getUser();
  152. // admin-defined primary color
  153. $defaultColor = $this->getDefaultColorPrimary();
  154. if ($this->isUserThemingDisabled()) {
  155. return $defaultColor;
  156. }
  157. // user-defined primary color
  158. if (!empty($user)) {
  159. $userPrimaryColor = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'primary_color', '');
  160. if (preg_match('/^\#([0-9a-f]{3}|[0-9a-f]{6})$/i', $userPrimaryColor)) {
  161. return $userPrimaryColor;
  162. }
  163. }
  164. // Finally, return the system global primary color
  165. return $defaultColor;
  166. }
  167. /**
  168. * Color that is used for the page background (e.g. the header)
  169. * If user theming is enabled then the user defined value is returned
  170. */
  171. public function getColorBackground(): string {
  172. $user = $this->userSession->getUser();
  173. // admin-defined background color
  174. $defaultColor = $this->getDefaultColorBackground();
  175. if ($this->isUserThemingDisabled()) {
  176. return $defaultColor;
  177. }
  178. // user-defined background color
  179. if (!empty($user)) {
  180. $userBackgroundColor = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'background_color', '');
  181. if (preg_match('/^\#([0-9a-f]{3}|[0-9a-f]{6})$/i', $userBackgroundColor)) {
  182. return $userBackgroundColor;
  183. }
  184. }
  185. // Finally, return the system global background color
  186. return $defaultColor;
  187. }
  188. /**
  189. * Return the default primary color - only taking admin setting into account
  190. */
  191. public function getDefaultColorPrimary(): string {
  192. // try admin color
  193. $defaultColor = $this->appConfig->getValueString(Application::APP_ID, 'primary_color', '');
  194. if (preg_match('/^\#([0-9a-f]{3}|[0-9a-f]{6})$/i', $defaultColor)) {
  195. return $defaultColor;
  196. }
  197. // fall back to default primary color
  198. return $this->primaryColor;
  199. }
  200. /**
  201. * Default background color only taking admin setting into account
  202. */
  203. public function getDefaultColorBackground(): string {
  204. $defaultColor = $this->appConfig->getValueString(Application::APP_ID, 'background_color');
  205. if (preg_match('/^\#([0-9a-f]{3}|[0-9a-f]{6})$/i', $defaultColor)) {
  206. return $defaultColor;
  207. }
  208. return $this->backgroundColor;
  209. }
  210. /**
  211. * Themed logo url
  212. *
  213. * @param bool $useSvg Whether to point to the SVG image or a fallback
  214. * @return string
  215. */
  216. public function getLogo($useSvg = true): string {
  217. $logo = $this->config->getAppValue('theming', 'logoMime', '');
  218. // short cut to avoid setting up the filesystem just to check if the logo is there
  219. //
  220. // explanation: if an SVG is requested and the app config value for logoMime is set then the logo is there.
  221. // otherwise we need to check it and maybe also generate a PNG from the SVG (that's done in getImage() which
  222. // needs to be called then)
  223. if ($useSvg === true && $logo !== false) {
  224. $logoExists = true;
  225. } else {
  226. try {
  227. $this->imageManager->getImage('logo', $useSvg);
  228. $logoExists = true;
  229. } catch (\Exception $e) {
  230. $logoExists = false;
  231. }
  232. }
  233. $cacheBusterCounter = $this->config->getAppValue('theming', 'cachebuster', '0');
  234. if (!$logo || !$logoExists) {
  235. if ($useSvg) {
  236. $logo = $this->urlGenerator->imagePath('core', 'logo/logo.svg');
  237. } else {
  238. $logo = $this->urlGenerator->imagePath('core', 'logo/logo.png');
  239. }
  240. return $logo . '?v=' . $cacheBusterCounter;
  241. }
  242. return $this->urlGenerator->linkToRoute('theming.Theming.getImage', [ 'key' => 'logo', 'useSvg' => $useSvg, 'v' => $cacheBusterCounter ]);
  243. }
  244. /**
  245. * Themed background image url
  246. *
  247. * @param bool $darkVariant if the dark variant (if available) of the background should be used
  248. * @return string
  249. */
  250. public function getBackground(bool $darkVariant = false): string {
  251. return $this->imageManager->getImageUrl('background' . ($darkVariant ? 'Dark' : ''));
  252. }
  253. /**
  254. * @return string
  255. */
  256. public function getiTunesAppId() {
  257. return $this->config->getAppValue('theming', 'iTunesAppId', $this->iTunesAppId);
  258. }
  259. /**
  260. * @return string
  261. */
  262. public function getiOSClientUrl() {
  263. return $this->config->getAppValue('theming', 'iOSClientUrl', $this->iOSClientUrl);
  264. }
  265. /**
  266. * @return string
  267. */
  268. public function getAndroidClientUrl() {
  269. return $this->config->getAppValue('theming', 'AndroidClientUrl', $this->AndroidClientUrl);
  270. }
  271. /**
  272. * @return string
  273. */
  274. public function getFDroidClientUrl() {
  275. return $this->config->getAppValue('theming', 'FDroidClientUrl', $this->FDroidClientUrl);
  276. }
  277. /**
  278. * @return array scss variables to overwrite
  279. * @deprecated since Nextcloud 22 - https://github.com/nextcloud/server/issues/9940
  280. */
  281. public function getScssVariables() {
  282. $cacheBuster = $this->config->getAppValue('theming', 'cachebuster', '0');
  283. $cache = $this->cacheFactory->createDistributed('theming-' . $cacheBuster . '-' . $this->urlGenerator->getBaseUrl());
  284. if ($value = $cache->get('getScssVariables')) {
  285. return $value;
  286. }
  287. $variables = [
  288. 'theming-cachebuster' => "'" . $cacheBuster . "'",
  289. 'theming-logo-mime' => "'" . $this->config->getAppValue('theming', 'logoMime') . "'",
  290. 'theming-background-mime' => "'" . $this->config->getAppValue('theming', 'backgroundMime') . "'",
  291. 'theming-logoheader-mime' => "'" . $this->config->getAppValue('theming', 'logoheaderMime') . "'",
  292. 'theming-favicon-mime' => "'" . $this->config->getAppValue('theming', 'faviconMime') . "'"
  293. ];
  294. $variables['image-logo'] = "url('" . $this->imageManager->getImageUrl('logo') . "')";
  295. $variables['image-logoheader'] = "url('" . $this->imageManager->getImageUrl('logoheader') . "')";
  296. $variables['image-favicon'] = "url('" . $this->imageManager->getImageUrl('favicon') . "')";
  297. $variables['image-login-background'] = "url('" . $this->imageManager->getImageUrl('background') . "')";
  298. $variables['image-login-plain'] = 'false';
  299. if ($this->appConfig->getValueString(Application::APP_ID, 'primary_color', '') !== '') {
  300. $variables['color-primary'] = $this->getColorPrimary();
  301. $variables['color-primary-text'] = $this->getTextColorPrimary();
  302. $variables['color-primary-element'] = $this->util->elementColor($this->getColorPrimary());
  303. }
  304. if ($this->config->getAppValue('theming', 'backgroundMime', '') === 'backgroundColor') {
  305. $variables['image-login-plain'] = 'true';
  306. }
  307. $variables['has-legal-links'] = 'false';
  308. if ($this->getImprintUrl() !== '' || $this->getPrivacyUrl() !== '') {
  309. $variables['has-legal-links'] = 'true';
  310. }
  311. $cache->set('getScssVariables', $variables);
  312. return $variables;
  313. }
  314. /**
  315. * Check if the image should be replaced by the theming app
  316. * and return the new image location then
  317. *
  318. * @param string $app name of the app
  319. * @param string $image filename of the image
  320. * @return bool|string false if image should not replaced, otherwise the location of the image
  321. */
  322. public function replaceImagePath($app, $image) {
  323. if ($app === '' || $app === 'files_sharing') {
  324. $app = 'core';
  325. }
  326. $cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0');
  327. $route = false;
  328. if ($image === 'favicon.ico' && ($this->imageManager->shouldReplaceIcons() || $this->getCustomFavicon() !== null)) {
  329. $route = $this->urlGenerator->linkToRoute('theming.Icon.getFavicon', ['app' => $app]);
  330. }
  331. if (($image === 'favicon-touch.png' || $image === 'favicon-fb.png') && ($this->imageManager->shouldReplaceIcons() || $this->getCustomFavicon() !== null)) {
  332. $route = $this->urlGenerator->linkToRoute('theming.Icon.getTouchIcon', ['app' => $app]);
  333. }
  334. if ($image === 'manifest.json') {
  335. try {
  336. $appPath = $this->appManager->getAppPath($app);
  337. if (file_exists($appPath . '/img/manifest.json')) {
  338. return false;
  339. }
  340. } catch (AppPathNotFoundException $e) {
  341. }
  342. $route = $this->urlGenerator->linkToRoute('theming.Theming.getManifest', ['app' => $app ]);
  343. }
  344. if (str_starts_with($image, 'filetypes/') && file_exists(\OC::$SERVERROOT . '/core/img/' . $image)) {
  345. $route = $this->urlGenerator->linkToRoute('theming.Icon.getThemedIcon', ['app' => $app, 'image' => $image]);
  346. }
  347. if ($route) {
  348. return $route . '?v=' . $this->util->getCacheBuster();
  349. }
  350. return false;
  351. }
  352. protected function getCustomFavicon(): ?ISimpleFile {
  353. try {
  354. return $this->imageManager->getImage('favicon');
  355. } catch (NotFoundException $e) {
  356. return null;
  357. }
  358. }
  359. /**
  360. * Increases the cache buster key
  361. */
  362. public function increaseCacheBuster(): void {
  363. $cacheBusterKey = (int)$this->config->getAppValue('theming', 'cachebuster', '0');
  364. $this->config->setAppValue('theming', 'cachebuster', (string)($cacheBusterKey + 1));
  365. $this->cacheFactory->createDistributed('theming-')->clear();
  366. $this->cacheFactory->createDistributed('imagePath')->clear();
  367. }
  368. /**
  369. * Update setting in the database
  370. *
  371. * @param string $setting
  372. * @param string $value
  373. */
  374. public function set($setting, $value): void {
  375. $this->config->setAppValue('theming', $setting, $value);
  376. $this->increaseCacheBuster();
  377. }
  378. /**
  379. * Revert all settings to the default value
  380. */
  381. public function undoAll(): void {
  382. // Remember the current cachebuster value, as we do not want to reset this value
  383. // Otherwise this can lead to caching issues as the value might be known to a browser already
  384. $cacheBusterKey = $this->config->getAppValue('theming', 'cachebuster', '0');
  385. $this->config->deleteAppValues('theming');
  386. $this->config->setAppValue('theming', 'cachebuster', $cacheBusterKey);
  387. $this->increaseCacheBuster();
  388. }
  389. /**
  390. * Revert admin settings to the default value
  391. *
  392. * @param string $setting setting which should be reverted
  393. * @return string default value
  394. */
  395. public function undo($setting): string {
  396. $this->config->deleteAppValue('theming', $setting);
  397. $this->increaseCacheBuster();
  398. $returnValue = '';
  399. switch ($setting) {
  400. case 'name':
  401. $returnValue = $this->getEntity();
  402. break;
  403. case 'url':
  404. $returnValue = $this->getBaseUrl();
  405. break;
  406. case 'slogan':
  407. $returnValue = $this->getSlogan();
  408. break;
  409. case 'primary_color':
  410. $returnValue = BackgroundService::DEFAULT_COLOR;
  411. break;
  412. case 'background_color':
  413. // If a background image is set we revert to the mean image color
  414. if ($this->imageManager->hasImage('background')) {
  415. $file = $this->imageManager->getImage('background');
  416. $returnValue = $this->backgroundService->setGlobalBackground($file->read()) ?? '';
  417. }
  418. break;
  419. case 'logo':
  420. case 'logoheader':
  421. case 'background':
  422. case 'favicon':
  423. $this->imageManager->delete($setting);
  424. $this->config->deleteAppValue('theming', $setting . 'Mime');
  425. break;
  426. }
  427. return $returnValue;
  428. }
  429. /**
  430. * Color of text in the header menu
  431. *
  432. * @return string
  433. */
  434. public function getTextColorBackground() {
  435. return $this->util->invertTextColor($this->getColorBackground()) ? '#000000' : '#ffffff';
  436. }
  437. /**
  438. * Color of text on primary buttons and other elements
  439. *
  440. * @return string
  441. */
  442. public function getTextColorPrimary() {
  443. return $this->util->invertTextColor($this->getColorPrimary()) ? '#000000' : '#ffffff';
  444. }
  445. /**
  446. * Color of text in the header and primary buttons
  447. *
  448. * @return string
  449. */
  450. public function getDefaultTextColorPrimary() {
  451. return $this->util->invertTextColor($this->getDefaultColorPrimary()) ? '#000000' : '#ffffff';
  452. }
  453. /**
  454. * Has the admin disabled user customization
  455. */
  456. public function isUserThemingDisabled(): bool {
  457. return $this->appConfig->getValueBool(Application::APP_ID, 'disable-user-theming');
  458. }
  459. }