ThemingDefaults.php 17 KB

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