ThemingDefaults.php 15 KB

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