ThemingDefaults.php 16 KB

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