ThemingDefaults.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. if ($baseUrl !== '') {
  158. $footer = '<a href="' . $baseUrl . '" target="_blank"' .
  159. ' rel="noreferrer noopener" class="entity-name">' . $this->getEntity() . '</a>';
  160. } else {
  161. $footer = '<span class="entity-name">' .$this->getEntity() . '</span>';
  162. }
  163. $footer .= ($slogan !== '' ? ' – ' . $slogan : '');
  164. $links = [
  165. [
  166. 'text' => $this->l->t('Legal notice'),
  167. 'url' => (string)$this->getImprintUrl()
  168. ],
  169. [
  170. 'text' => $this->l->t('Privacy policy'),
  171. 'url' => (string)$this->getPrivacyUrl()
  172. ],
  173. ];
  174. $navigation = $this->navigationManager->getAll(INavigationManager::TYPE_GUEST);
  175. $guestNavigation = array_map(function ($nav) {
  176. return [
  177. 'text' => $nav['name'],
  178. 'url' => $nav['href']
  179. ];
  180. }, $navigation);
  181. $links = array_merge($links, $guestNavigation);
  182. $legalLinks = '';
  183. $divider = '';
  184. foreach ($links as $link) {
  185. if ($link['url'] !== ''
  186. && filter_var($link['url'], FILTER_VALIDATE_URL)
  187. ) {
  188. $legalLinks .= $divider . '<a href="' . $link['url'] . '" class="legal" target="_blank"' .
  189. ' rel="noreferrer noopener">' . $link['text'] . '</a>';
  190. $divider = ' · ';
  191. }
  192. }
  193. if ($legalLinks !== '') {
  194. $footer .= '<br/>' . $legalLinks;
  195. }
  196. return $footer;
  197. }
  198. /**
  199. * Color that is used for the header as well as for mail headers
  200. */
  201. public function getColorPrimary(): string {
  202. $user = $this->userSession->getUser();
  203. // admin-defined primary color
  204. $defaultColor = $this->getDefaultColorPrimary();
  205. if ($this->isUserThemingDisabled()) {
  206. return $defaultColor;
  207. }
  208. // user-defined primary color
  209. if (!empty($user)) {
  210. $themingBackgroundColor = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'background_color', '');
  211. // If the user selected a specific colour
  212. if (preg_match('/^\#([0-9a-f]{3}|[0-9a-f]{6})$/i', $themingBackgroundColor)) {
  213. return $themingBackgroundColor;
  214. }
  215. }
  216. // If the default color is not valid, return the default background one
  217. if (!preg_match('/^\#([0-9a-f]{3}|[0-9a-f]{6})$/i', $defaultColor)) {
  218. return BackgroundService::DEFAULT_COLOR;
  219. }
  220. // Finally, return the system global primary color
  221. return $defaultColor;
  222. }
  223. /**
  224. * Return the default color primary
  225. */
  226. public function getDefaultColorPrimary(): string {
  227. $color = $this->config->getAppValue(Application::APP_ID, 'color', '');
  228. if (!preg_match('/^\#([0-9a-f]{3}|[0-9a-f]{6})$/i', $color)) {
  229. return BackgroundService::DEFAULT_COLOR;
  230. }
  231. return $color;
  232. }
  233. /**
  234. * Themed logo url
  235. *
  236. * @param bool $useSvg Whether to point to the SVG image or a fallback
  237. * @return string
  238. */
  239. public function getLogo($useSvg = true): string {
  240. $logo = $this->config->getAppValue('theming', 'logoMime', '');
  241. // short cut to avoid setting up the filesystem just to check if the logo is there
  242. //
  243. // explanation: if an SVG is requested and the app config value for logoMime is set then the logo is there.
  244. // otherwise we need to check it and maybe also generate a PNG from the SVG (that's done in getImage() which
  245. // needs to be called then)
  246. if ($useSvg === true && $logo !== false) {
  247. $logoExists = true;
  248. } else {
  249. try {
  250. $this->imageManager->getImage('logo', $useSvg);
  251. $logoExists = true;
  252. } catch (\Exception $e) {
  253. $logoExists = false;
  254. }
  255. }
  256. $cacheBusterCounter = $this->config->getAppValue('theming', 'cachebuster', '0');
  257. if (!$logo || !$logoExists) {
  258. if ($useSvg) {
  259. $logo = $this->urlGenerator->imagePath('core', 'logo/logo.svg');
  260. } else {
  261. $logo = $this->urlGenerator->imagePath('core', 'logo/logo.png');
  262. }
  263. return $logo . '?v=' . $cacheBusterCounter;
  264. }
  265. return $this->urlGenerator->linkToRoute('theming.Theming.getImage', [ 'key' => 'logo', 'useSvg' => $useSvg, 'v' => $cacheBusterCounter ]);
  266. }
  267. /**
  268. * Themed background image url
  269. *
  270. * @return string
  271. */
  272. public function getBackground(): string {
  273. return $this->imageManager->getImageUrl('background');
  274. }
  275. /**
  276. * @return string
  277. */
  278. public function getiTunesAppId() {
  279. return $this->config->getAppValue('theming', 'iTunesAppId', $this->iTunesAppId);
  280. }
  281. /**
  282. * @return string
  283. */
  284. public function getiOSClientUrl() {
  285. return $this->config->getAppValue('theming', 'iOSClientUrl', $this->iOSClientUrl);
  286. }
  287. /**
  288. * @return string
  289. */
  290. public function getAndroidClientUrl() {
  291. return $this->config->getAppValue('theming', 'AndroidClientUrl', $this->AndroidClientUrl);
  292. }
  293. /**
  294. * @return string
  295. */
  296. public function getFDroidClientUrl() {
  297. return $this->config->getAppValue('theming', 'FDroidClientUrl', $this->FDroidClientUrl);
  298. }
  299. /**
  300. * @return array scss variables to overwrite
  301. */
  302. public function getScssVariables() {
  303. $cacheBuster = $this->config->getAppValue('theming', 'cachebuster', '0');
  304. $cache = $this->cacheFactory->createDistributed('theming-' . $cacheBuster . '-' . $this->urlGenerator->getBaseUrl());
  305. if ($value = $cache->get('getScssVariables')) {
  306. return $value;
  307. }
  308. $variables = [
  309. 'theming-cachebuster' => "'" . $cacheBuster . "'",
  310. 'theming-logo-mime' => "'" . $this->config->getAppValue('theming', 'logoMime') . "'",
  311. 'theming-background-mime' => "'" . $this->config->getAppValue('theming', 'backgroundMime') . "'",
  312. 'theming-logoheader-mime' => "'" . $this->config->getAppValue('theming', 'logoheaderMime') . "'",
  313. 'theming-favicon-mime' => "'" . $this->config->getAppValue('theming', 'faviconMime') . "'"
  314. ];
  315. $variables['image-logo'] = "url('".$this->imageManager->getImageUrl('logo')."')";
  316. $variables['image-logoheader'] = "url('".$this->imageManager->getImageUrl('logoheader')."')";
  317. $variables['image-favicon'] = "url('".$this->imageManager->getImageUrl('favicon')."')";
  318. $variables['image-login-background'] = "url('".$this->imageManager->getImageUrl('background')."')";
  319. $variables['image-login-plain'] = 'false';
  320. if ($this->config->getAppValue('theming', 'color', '') !== '') {
  321. $variables['color-primary'] = $this->getColorPrimary();
  322. $variables['color-primary-text'] = $this->getTextColorPrimary();
  323. $variables['color-primary-element'] = $this->util->elementColor($this->getColorPrimary());
  324. }
  325. if ($this->config->getAppValue('theming', 'backgroundMime', '') === 'backgroundColor') {
  326. $variables['image-login-plain'] = 'true';
  327. }
  328. $variables['has-legal-links'] = 'false';
  329. if ($this->getImprintUrl() !== '' || $this->getPrivacyUrl() !== '') {
  330. $variables['has-legal-links'] = 'true';
  331. }
  332. $cache->set('getScssVariables', $variables);
  333. return $variables;
  334. }
  335. /**
  336. * Check if the image should be replaced by the theming app
  337. * and return the new image location then
  338. *
  339. * @param string $app name of the app
  340. * @param string $image filename of the image
  341. * @return bool|string false if image should not replaced, otherwise the location of the image
  342. */
  343. public function replaceImagePath($app, $image) {
  344. if ($app === '' || $app === 'files_sharing') {
  345. $app = 'core';
  346. }
  347. $cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0');
  348. $route = false;
  349. if ($image === 'favicon.ico' && ($this->imageManager->shouldReplaceIcons() || $this->getCustomFavicon() !== null)) {
  350. $route = $this->urlGenerator->linkToRoute('theming.Icon.getFavicon', ['app' => $app]);
  351. }
  352. if (($image === 'favicon-touch.png' || $image === 'favicon-fb.png') && ($this->imageManager->shouldReplaceIcons() || $this->getCustomFavicon() !== null)) {
  353. $route = $this->urlGenerator->linkToRoute('theming.Icon.getTouchIcon', ['app' => $app]);
  354. }
  355. if ($image === 'manifest.json') {
  356. try {
  357. $appPath = $this->appManager->getAppPath($app);
  358. if (file_exists($appPath . '/img/manifest.json')) {
  359. return false;
  360. }
  361. } catch (AppPathNotFoundException $e) {
  362. }
  363. $route = $this->urlGenerator->linkToRoute('theming.Theming.getManifest', ['app' => $app ]);
  364. }
  365. if (str_starts_with($image, 'filetypes/') && file_exists(\OC::$SERVERROOT . '/core/img/' . $image)) {
  366. $route = $this->urlGenerator->linkToRoute('theming.Icon.getThemedIcon', ['app' => $app, 'image' => $image]);
  367. }
  368. if ($route) {
  369. return $route . '?v=' . $this->util->getCacheBuster();
  370. }
  371. return false;
  372. }
  373. protected function getCustomFavicon(): ?ISimpleFile {
  374. try {
  375. return $this->imageManager->getImage('favicon');
  376. } catch (NotFoundException $e) {
  377. return null;
  378. }
  379. }
  380. /**
  381. * Increases the cache buster key
  382. */
  383. public function increaseCacheBuster(): void {
  384. $cacheBusterKey = (int)$this->config->getAppValue('theming', 'cachebuster', '0');
  385. $this->config->setAppValue('theming', 'cachebuster', (string)($cacheBusterKey + 1));
  386. $this->cacheFactory->createDistributed('theming-')->clear();
  387. $this->cacheFactory->createDistributed('imagePath')->clear();
  388. }
  389. /**
  390. * Update setting in the database
  391. *
  392. * @param string $setting
  393. * @param string $value
  394. */
  395. public function set($setting, $value): void {
  396. $this->config->setAppValue('theming', $setting, $value);
  397. $this->increaseCacheBuster();
  398. }
  399. /**
  400. * Revert all settings to the default value
  401. */
  402. public function undoAll(): void {
  403. $this->config->deleteAppValues('theming');
  404. $this->increaseCacheBuster();
  405. }
  406. /**
  407. * Revert settings to the default value
  408. *
  409. * @param string $setting setting which should be reverted
  410. * @return string default value
  411. */
  412. public function undo($setting): string {
  413. $this->config->deleteAppValue('theming', $setting);
  414. $this->increaseCacheBuster();
  415. $returnValue = '';
  416. switch ($setting) {
  417. case 'name':
  418. $returnValue = $this->getEntity();
  419. break;
  420. case 'url':
  421. $returnValue = $this->getBaseUrl();
  422. break;
  423. case 'slogan':
  424. $returnValue = $this->getSlogan();
  425. break;
  426. case 'color':
  427. $returnValue = $this->getDefaultColorPrimary();
  428. break;
  429. case 'logo':
  430. case 'logoheader':
  431. case 'background':
  432. case 'favicon':
  433. $this->imageManager->delete($setting);
  434. $this->config->deleteAppValue('theming', $setting . 'Mime');
  435. break;
  436. }
  437. return $returnValue;
  438. }
  439. /**
  440. * Color of text in the header and primary buttons
  441. *
  442. * @return string
  443. */
  444. public function getTextColorPrimary() {
  445. return $this->util->invertTextColor($this->getColorPrimary()) ? '#000000' : '#ffffff';
  446. }
  447. /**
  448. * Color of text in the header and primary buttons
  449. *
  450. * @return string
  451. */
  452. public function getDefaultTextColorPrimary() {
  453. return $this->util->invertTextColor($this->getDefaultColorPrimary()) ? '#000000' : '#ffffff';
  454. }
  455. /**
  456. * Has the admin disabled user customization
  457. */
  458. public function isUserThemingDisabled(): bool {
  459. return $this->config->getAppValue('theming', 'disable-user-theming', 'no') === 'yes';
  460. }
  461. }