ThemingDefaults.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. * @return string
  246. */
  247. public function getBackground(): string {
  248. return $this->imageManager->getImageUrl('background');
  249. }
  250. /**
  251. * @return string
  252. */
  253. public function getiTunesAppId() {
  254. return $this->config->getAppValue('theming', 'iTunesAppId', $this->iTunesAppId);
  255. }
  256. /**
  257. * @return string
  258. */
  259. public function getiOSClientUrl() {
  260. return $this->config->getAppValue('theming', 'iOSClientUrl', $this->iOSClientUrl);
  261. }
  262. /**
  263. * @return string
  264. */
  265. public function getAndroidClientUrl() {
  266. return $this->config->getAppValue('theming', 'AndroidClientUrl', $this->AndroidClientUrl);
  267. }
  268. /**
  269. * @return string
  270. */
  271. public function getFDroidClientUrl() {
  272. return $this->config->getAppValue('theming', 'FDroidClientUrl', $this->FDroidClientUrl);
  273. }
  274. /**
  275. * @return array scss variables to overwrite
  276. * @deprecated since Nextcloud 22 - https://github.com/nextcloud/server/issues/9940
  277. */
  278. public function getScssVariables() {
  279. $cacheBuster = $this->config->getAppValue('theming', 'cachebuster', '0');
  280. $cache = $this->cacheFactory->createDistributed('theming-' . $cacheBuster . '-' . $this->urlGenerator->getBaseUrl());
  281. if ($value = $cache->get('getScssVariables')) {
  282. return $value;
  283. }
  284. $variables = [
  285. 'theming-cachebuster' => "'" . $cacheBuster . "'",
  286. 'theming-logo-mime' => "'" . $this->config->getAppValue('theming', 'logoMime') . "'",
  287. 'theming-background-mime' => "'" . $this->config->getAppValue('theming', 'backgroundMime') . "'",
  288. 'theming-logoheader-mime' => "'" . $this->config->getAppValue('theming', 'logoheaderMime') . "'",
  289. 'theming-favicon-mime' => "'" . $this->config->getAppValue('theming', 'faviconMime') . "'"
  290. ];
  291. $variables['image-logo'] = "url('".$this->imageManager->getImageUrl('logo')."')";
  292. $variables['image-logoheader'] = "url('".$this->imageManager->getImageUrl('logoheader')."')";
  293. $variables['image-favicon'] = "url('".$this->imageManager->getImageUrl('favicon')."')";
  294. $variables['image-login-background'] = "url('".$this->imageManager->getImageUrl('background')."')";
  295. $variables['image-login-plain'] = 'false';
  296. if ($this->config->getAppValue('theming', 'primary_color', '') !== '') {
  297. $variables['color-primary'] = $this->getColorPrimary();
  298. $variables['color-primary-text'] = $this->getTextColorPrimary();
  299. $variables['color-primary-element'] = $this->util->elementColor($this->getColorPrimary());
  300. }
  301. if ($this->config->getAppValue('theming', 'backgroundMime', '') === 'backgroundColor') {
  302. $variables['image-login-plain'] = 'true';
  303. }
  304. $variables['has-legal-links'] = 'false';
  305. if ($this->getImprintUrl() !== '' || $this->getPrivacyUrl() !== '') {
  306. $variables['has-legal-links'] = 'true';
  307. }
  308. $cache->set('getScssVariables', $variables);
  309. return $variables;
  310. }
  311. /**
  312. * Check if the image should be replaced by the theming app
  313. * and return the new image location then
  314. *
  315. * @param string $app name of the app
  316. * @param string $image filename of the image
  317. * @return bool|string false if image should not replaced, otherwise the location of the image
  318. */
  319. public function replaceImagePath($app, $image) {
  320. if ($app === '' || $app === 'files_sharing') {
  321. $app = 'core';
  322. }
  323. $cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0');
  324. $route = false;
  325. if ($image === 'favicon.ico' && ($this->imageManager->shouldReplaceIcons() || $this->getCustomFavicon() !== null)) {
  326. $route = $this->urlGenerator->linkToRoute('theming.Icon.getFavicon', ['app' => $app]);
  327. }
  328. if (($image === 'favicon-touch.png' || $image === 'favicon-fb.png') && ($this->imageManager->shouldReplaceIcons() || $this->getCustomFavicon() !== null)) {
  329. $route = $this->urlGenerator->linkToRoute('theming.Icon.getTouchIcon', ['app' => $app]);
  330. }
  331. if ($image === 'manifest.json') {
  332. try {
  333. $appPath = $this->appManager->getAppPath($app);
  334. if (file_exists($appPath . '/img/manifest.json')) {
  335. return false;
  336. }
  337. } catch (AppPathNotFoundException $e) {
  338. }
  339. $route = $this->urlGenerator->linkToRoute('theming.Theming.getManifest', ['app' => $app ]);
  340. }
  341. if (str_starts_with($image, 'filetypes/') && file_exists(\OC::$SERVERROOT . '/core/img/' . $image)) {
  342. $route = $this->urlGenerator->linkToRoute('theming.Icon.getThemedIcon', ['app' => $app, 'image' => $image]);
  343. }
  344. if ($route) {
  345. return $route . '?v=' . $this->util->getCacheBuster();
  346. }
  347. return false;
  348. }
  349. protected function getCustomFavicon(): ?ISimpleFile {
  350. try {
  351. return $this->imageManager->getImage('favicon');
  352. } catch (NotFoundException $e) {
  353. return null;
  354. }
  355. }
  356. /**
  357. * Increases the cache buster key
  358. */
  359. public function increaseCacheBuster(): void {
  360. $cacheBusterKey = (int)$this->config->getAppValue('theming', 'cachebuster', '0');
  361. $this->config->setAppValue('theming', 'cachebuster', (string)($cacheBusterKey + 1));
  362. $this->cacheFactory->createDistributed('theming-')->clear();
  363. $this->cacheFactory->createDistributed('imagePath')->clear();
  364. }
  365. /**
  366. * Update setting in the database
  367. *
  368. * @param string $setting
  369. * @param string $value
  370. */
  371. public function set($setting, $value): void {
  372. $this->config->setAppValue('theming', $setting, $value);
  373. $this->increaseCacheBuster();
  374. }
  375. /**
  376. * Revert all settings to the default value
  377. */
  378. public function undoAll(): void {
  379. $this->config->deleteAppValues('theming');
  380. $this->increaseCacheBuster();
  381. }
  382. /**
  383. * Revert admin settings to the default value
  384. *
  385. * @param string $setting setting which should be reverted
  386. * @return string default value
  387. */
  388. public function undo($setting): string {
  389. $this->config->deleteAppValue('theming', $setting);
  390. $this->increaseCacheBuster();
  391. $returnValue = '';
  392. switch ($setting) {
  393. case 'name':
  394. $returnValue = $this->getEntity();
  395. break;
  396. case 'url':
  397. $returnValue = $this->getBaseUrl();
  398. break;
  399. case 'slogan':
  400. $returnValue = $this->getSlogan();
  401. break;
  402. case 'primary_color':
  403. $returnValue = BackgroundService::DEFAULT_COLOR;
  404. break;
  405. case 'background_color':
  406. // If a background image is set we revert to the mean image color
  407. if ($this->imageManager->hasImage('background')) {
  408. $file = $this->imageManager->getImage('background');
  409. $returnValue = $this->backgroundService->setGlobalBackground($file->read()) ?? '';
  410. }
  411. break;
  412. case 'logo':
  413. case 'logoheader':
  414. case 'background':
  415. case 'favicon':
  416. $this->imageManager->delete($setting);
  417. $this->config->deleteAppValue('theming', $setting . 'Mime');
  418. break;
  419. }
  420. return $returnValue;
  421. }
  422. /**
  423. * Color of text in the header menu
  424. *
  425. * @return string
  426. */
  427. public function getTextColorBackground() {
  428. return $this->util->invertTextColor($this->getColorBackground()) ? '#000000' : '#ffffff';
  429. }
  430. /**
  431. * Color of text on primary buttons and other elements
  432. *
  433. * @return string
  434. */
  435. public function getTextColorPrimary() {
  436. return $this->util->invertTextColor($this->getColorPrimary()) ? '#000000' : '#ffffff';
  437. }
  438. /**
  439. * Color of text in the header and primary buttons
  440. *
  441. * @return string
  442. */
  443. public function getDefaultTextColorPrimary() {
  444. return $this->util->invertTextColor($this->getDefaultColorPrimary()) ? '#000000' : '#ffffff';
  445. }
  446. /**
  447. * Has the admin disabled user customization
  448. */
  449. public function isUserThemingDisabled(): bool {
  450. return $this->config->getAppValue('theming', 'disable-user-theming', 'no') === 'yes';
  451. }
  452. }