ThemingDefaults.php 16 KB

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