ThemingDefaults.php 16 KB

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