1
0

ThemingDefaults.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. $themingBackground = '';
  205. if (!empty($user)) {
  206. $themingBackground = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'background', '');
  207. // If the user selected the default background
  208. if ($themingBackground === '') {
  209. return $defaultColor;
  210. }
  211. // If the user selected a valid custom colour
  212. if (preg_match('/^\#([0-9a-f]{3}|[0-9a-f]{6})$/i', $themingBackground)) {
  213. return $themingBackground;
  214. }
  215. // if the user-selected background is a background reference
  216. if (isset(BackgroundService::SHIPPED_BACKGROUNDS[$themingBackground]['primary_color'])) {
  217. return BackgroundService::SHIPPED_BACKGROUNDS[$themingBackground]['primary_color'];
  218. }
  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 the color is invalid, fallback to default
  229. if (!preg_match('/^\#([0-9a-f]{3}|[0-9a-f]{6})$/i', $color)) {
  230. $color = BackgroundService::DEFAULT_COLOR;
  231. }
  232. return $color;
  233. }
  234. /**
  235. * Themed logo url
  236. *
  237. * @param bool $useSvg Whether to point to the SVG image or a fallback
  238. * @return string
  239. */
  240. public function getLogo($useSvg = true): string {
  241. $logo = $this->config->getAppValue('theming', 'logoMime', '');
  242. // short cut to avoid setting up the filesystem just to check if the logo is there
  243. //
  244. // explanation: if an SVG is requested and the app config value for logoMime is set then the logo is there.
  245. // otherwise we need to check it and maybe also generate a PNG from the SVG (that's done in getImage() which
  246. // needs to be called then)
  247. if ($useSvg === true && $logo !== false) {
  248. $logoExists = true;
  249. } else {
  250. try {
  251. $this->imageManager->getImage('logo', $useSvg);
  252. $logoExists = true;
  253. } catch (\Exception $e) {
  254. $logoExists = false;
  255. }
  256. }
  257. $cacheBusterCounter = $this->config->getAppValue('theming', 'cachebuster', '0');
  258. if (!$logo || !$logoExists) {
  259. if ($useSvg) {
  260. $logo = $this->urlGenerator->imagePath('core', 'logo/logo.svg');
  261. } else {
  262. $logo = $this->urlGenerator->imagePath('core', 'logo/logo.png');
  263. }
  264. return $logo . '?v=' . $cacheBusterCounter;
  265. }
  266. return $this->urlGenerator->linkToRoute('theming.Theming.getImage', [ 'key' => 'logo', 'useSvg' => $useSvg, 'v' => $cacheBusterCounter ]);
  267. }
  268. /**
  269. * Themed background image url
  270. *
  271. * @return string
  272. */
  273. public function getBackground(): string {
  274. return $this->imageManager->getImageUrl('background');
  275. }
  276. /**
  277. * @return string
  278. */
  279. public function getiTunesAppId() {
  280. return $this->config->getAppValue('theming', 'iTunesAppId', $this->iTunesAppId);
  281. }
  282. /**
  283. * @return string
  284. */
  285. public function getiOSClientUrl() {
  286. return $this->config->getAppValue('theming', 'iOSClientUrl', $this->iOSClientUrl);
  287. }
  288. /**
  289. * @return string
  290. */
  291. public function getAndroidClientUrl() {
  292. return $this->config->getAppValue('theming', 'AndroidClientUrl', $this->AndroidClientUrl);
  293. }
  294. /**
  295. * @return string
  296. */
  297. public function getFDroidClientUrl() {
  298. return $this->config->getAppValue('theming', 'FDroidClientUrl', $this->FDroidClientUrl);
  299. }
  300. /**
  301. * @return array scss variables to overwrite
  302. */
  303. public function getScssVariables() {
  304. $cacheBuster = $this->config->getAppValue('theming', 'cachebuster', '0');
  305. $cache = $this->cacheFactory->createDistributed('theming-' . $cacheBuster . '-' . $this->urlGenerator->getBaseUrl());
  306. if ($value = $cache->get('getScssVariables')) {
  307. return $value;
  308. }
  309. $variables = [
  310. 'theming-cachebuster' => "'" . $cacheBuster . "'",
  311. 'theming-logo-mime' => "'" . $this->config->getAppValue('theming', 'logoMime') . "'",
  312. 'theming-background-mime' => "'" . $this->config->getAppValue('theming', 'backgroundMime') . "'",
  313. 'theming-logoheader-mime' => "'" . $this->config->getAppValue('theming', 'logoheaderMime') . "'",
  314. 'theming-favicon-mime' => "'" . $this->config->getAppValue('theming', 'faviconMime') . "'"
  315. ];
  316. $variables['image-logo'] = "url('".$this->imageManager->getImageUrl('logo')."')";
  317. $variables['image-logoheader'] = "url('".$this->imageManager->getImageUrl('logoheader')."')";
  318. $variables['image-favicon'] = "url('".$this->imageManager->getImageUrl('favicon')."')";
  319. $variables['image-login-background'] = "url('".$this->imageManager->getImageUrl('background')."')";
  320. $variables['image-login-plain'] = 'false';
  321. if ($this->config->getAppValue('theming', 'color', '') !== '') {
  322. $variables['color-primary'] = $this->getColorPrimary();
  323. $variables['color-primary-text'] = $this->getTextColorPrimary();
  324. $variables['color-primary-element'] = $this->util->elementColor($this->getColorPrimary());
  325. }
  326. if ($this->config->getAppValue('theming', 'backgroundMime', '') === 'backgroundColor') {
  327. $variables['image-login-plain'] = 'true';
  328. }
  329. $variables['has-legal-links'] = 'false';
  330. if ($this->getImprintUrl() !== '' || $this->getPrivacyUrl() !== '') {
  331. $variables['has-legal-links'] = 'true';
  332. }
  333. $cache->set('getScssVariables', $variables);
  334. return $variables;
  335. }
  336. /**
  337. * Check if the image should be replaced by the theming app
  338. * and return the new image location then
  339. *
  340. * @param string $app name of the app
  341. * @param string $image filename of the image
  342. * @return bool|string false if image should not replaced, otherwise the location of the image
  343. */
  344. public function replaceImagePath($app, $image) {
  345. if ($app === '' || $app === 'files_sharing') {
  346. $app = 'core';
  347. }
  348. $cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0');
  349. $route = false;
  350. if ($image === 'favicon.ico' && ($this->imageManager->shouldReplaceIcons() || $this->getCustomFavicon() !== null)) {
  351. $route = $this->urlGenerator->linkToRoute('theming.Icon.getFavicon', ['app' => $app]);
  352. }
  353. if (($image === 'favicon-touch.png' || $image === 'favicon-fb.png') && ($this->imageManager->shouldReplaceIcons() || $this->getCustomFavicon() !== null)) {
  354. $route = $this->urlGenerator->linkToRoute('theming.Icon.getTouchIcon', ['app' => $app]);
  355. }
  356. if ($image === 'manifest.json') {
  357. try {
  358. $appPath = $this->appManager->getAppPath($app);
  359. if (file_exists($appPath . '/img/manifest.json')) {
  360. return false;
  361. }
  362. } catch (AppPathNotFoundException $e) {
  363. }
  364. $route = $this->urlGenerator->linkToRoute('theming.Theming.getManifest', ['app' => $app ]);
  365. }
  366. if (strpos($image, 'filetypes/') === 0 && file_exists(\OC::$SERVERROOT . '/core/img/' . $image)) {
  367. $route = $this->urlGenerator->linkToRoute('theming.Icon.getThemedIcon', ['app' => $app, 'image' => $image]);
  368. }
  369. if ($route) {
  370. return $route . '?v=' . $this->util->getCacheBuster();
  371. }
  372. return false;
  373. }
  374. protected function getCustomFavicon(): ?ISimpleFile {
  375. try {
  376. return $this->imageManager->getImage('favicon');
  377. } catch (NotFoundException $e) {
  378. return null;
  379. }
  380. }
  381. /**
  382. * Increases the cache buster key
  383. */
  384. public function increaseCacheBuster(): void {
  385. $cacheBusterKey = (int)$this->config->getAppValue('theming', 'cachebuster', '0');
  386. $this->config->setAppValue('theming', 'cachebuster', (string)($cacheBusterKey + 1));
  387. $this->cacheFactory->createDistributed('theming-')->clear();
  388. $this->cacheFactory->createDistributed('imagePath')->clear();
  389. }
  390. /**
  391. * Update setting in the database
  392. *
  393. * @param string $setting
  394. * @param string $value
  395. */
  396. public function set($setting, $value) {
  397. $this->config->setAppValue('theming', $setting, $value);
  398. $this->increaseCacheBuster();
  399. }
  400. /**
  401. * Revert settings to the default value
  402. *
  403. * @param string $setting setting which should be reverted
  404. * @return string default value
  405. */
  406. public function undo($setting) {
  407. $this->config->deleteAppValue('theming', $setting);
  408. $this->increaseCacheBuster();
  409. $returnValue = '';
  410. switch ($setting) {
  411. case 'name':
  412. $returnValue = $this->getEntity();
  413. break;
  414. case 'url':
  415. $returnValue = $this->getBaseUrl();
  416. break;
  417. case 'slogan':
  418. $returnValue = $this->getSlogan();
  419. break;
  420. case 'color':
  421. $returnValue = $this->getDefaultColorPrimary();
  422. break;
  423. case 'logo':
  424. case 'logoheader':
  425. case 'background':
  426. case 'favicon':
  427. $this->imageManager->delete($setting);
  428. break;
  429. }
  430. return $returnValue;
  431. }
  432. /**
  433. * Color of text in the header and primary buttons
  434. *
  435. * @return string
  436. */
  437. public function getTextColorPrimary() {
  438. return $this->util->invertTextColor($this->getColorPrimary()) ? '#000000' : '#ffffff';
  439. }
  440. /**
  441. * Has the admin disabled user customization
  442. */
  443. public function isUserThemingDisabled(): bool {
  444. return $this->config->getAppValue('theming', 'disable-user-theming', 'no') === 'yes';
  445. }
  446. }