ThemingDefaults.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 OCP\App\AppPathNotFoundException;
  43. use OCP\App\IAppManager;
  44. use OCP\Files\NotFoundException;
  45. use OCP\Files\SimpleFS\ISimpleFile;
  46. use OCP\ICacheFactory;
  47. use OCP\IConfig;
  48. use OCP\IL10N;
  49. use OCP\INavigationManager;
  50. use OCP\IURLGenerator;
  51. class ThemingDefaults extends \OC_Defaults {
  52. /** @var IConfig */
  53. private $config;
  54. /** @var IL10N */
  55. private $l;
  56. /** @var ImageManager */
  57. private $imageManager;
  58. /** @var IURLGenerator */
  59. private $urlGenerator;
  60. /** @var ICacheFactory */
  61. private $cacheFactory;
  62. /** @var Util */
  63. private $util;
  64. /** @var IAppManager */
  65. private $appManager;
  66. /** @var INavigationManager */
  67. private $navigationManager;
  68. /** @var string */
  69. private $name;
  70. /** @var string */
  71. private $title;
  72. /** @var string */
  73. private $entity;
  74. /** @var string */
  75. private $productName;
  76. /** @var string */
  77. private $url;
  78. /** @var string */
  79. private $color;
  80. /** @var string */
  81. private $iTunesAppId;
  82. /** @var string */
  83. private $iOSClientUrl;
  84. /** @var string */
  85. private $AndroidClientUrl;
  86. /** @var string */
  87. private $FDroidClientUrl;
  88. /**
  89. * ThemingDefaults constructor.
  90. *
  91. * @param IConfig $config
  92. * @param IL10N $l
  93. * @param ImageManager $imageManager
  94. * @param IURLGenerator $urlGenerator
  95. * @param ICacheFactory $cacheFactory
  96. * @param Util $util
  97. * @param IAppManager $appManager
  98. */
  99. public function __construct(IConfig $config,
  100. IL10N $l,
  101. IURLGenerator $urlGenerator,
  102. ICacheFactory $cacheFactory,
  103. Util $util,
  104. ImageManager $imageManager,
  105. IAppManager $appManager,
  106. INavigationManager $navigationManager
  107. ) {
  108. parent::__construct();
  109. $this->config = $config;
  110. $this->l = $l;
  111. $this->imageManager = $imageManager;
  112. $this->urlGenerator = $urlGenerator;
  113. $this->cacheFactory = $cacheFactory;
  114. $this->util = $util;
  115. $this->appManager = $appManager;
  116. $this->navigationManager = $navigationManager;
  117. $this->name = parent::getName();
  118. $this->title = parent::getTitle();
  119. $this->entity = parent::getEntity();
  120. $this->productName = parent::getProductName();
  121. $this->url = parent::getBaseUrl();
  122. $this->color = parent::getColorPrimary();
  123. $this->iTunesAppId = parent::getiTunesAppId();
  124. $this->iOSClientUrl = parent::getiOSClientUrl();
  125. $this->AndroidClientUrl = parent::getAndroidClientUrl();
  126. $this->FDroidClientUrl = parent::getFDroidClientUrl();
  127. }
  128. public function getName() {
  129. return strip_tags($this->config->getAppValue('theming', 'name', $this->name));
  130. }
  131. public function getHTMLName() {
  132. return $this->config->getAppValue('theming', 'name', $this->name);
  133. }
  134. public function getTitle() {
  135. return strip_tags($this->config->getAppValue('theming', 'name', $this->title));
  136. }
  137. public function getEntity() {
  138. return strip_tags($this->config->getAppValue('theming', 'name', $this->entity));
  139. }
  140. public function getProductName() {
  141. return strip_tags($this->config->getAppValue('theming', 'productName', $this->productName));
  142. }
  143. public function getBaseUrl() {
  144. return $this->config->getAppValue('theming', 'url', $this->url);
  145. }
  146. /**
  147. * We pass a string and sanitizeHTML will return a string too in that case
  148. * @psalm-suppress InvalidReturnStatement
  149. * @psalm-suppress InvalidReturnType
  150. */
  151. public function getSlogan(?string $lang = null) {
  152. return \OCP\Util::sanitizeHTML($this->config->getAppValue('theming', 'slogan', parent::getSlogan($lang)));
  153. }
  154. public function getImprintUrl() {
  155. return (string)$this->config->getAppValue('theming', 'imprintUrl', '');
  156. }
  157. public function getPrivacyUrl() {
  158. return (string)$this->config->getAppValue('theming', 'privacyUrl', '');
  159. }
  160. public function getShortFooter() {
  161. $slogan = $this->getSlogan();
  162. $baseUrl = $this->getBaseUrl();
  163. if ($baseUrl !== '') {
  164. $footer = '<a href="' . $baseUrl . '" target="_blank"' .
  165. ' rel="noreferrer noopener" class="entity-name">' . $this->getEntity() . '</a>';
  166. } else {
  167. $footer = '<span class="entity-name">' .$this->getEntity() . '</span>';
  168. }
  169. $footer .= ($slogan !== '' ? ' – ' . $slogan : '');
  170. $links = [
  171. [
  172. 'text' => $this->l->t('Legal notice'),
  173. 'url' => (string)$this->getImprintUrl()
  174. ],
  175. [
  176. 'text' => $this->l->t('Privacy policy'),
  177. 'url' => (string)$this->getPrivacyUrl()
  178. ],
  179. ];
  180. $navigation = $this->navigationManager->getAll(INavigationManager::TYPE_GUEST);
  181. $guestNavigation = array_map(function ($nav) {
  182. return [
  183. 'text' => $nav['name'],
  184. 'url' => $nav['href']
  185. ];
  186. }, $navigation);
  187. $links = array_merge($links, $guestNavigation);
  188. $legalLinks = '';
  189. $divider = '';
  190. foreach ($links as $link) {
  191. if ($link['url'] !== ''
  192. && filter_var($link['url'], FILTER_VALIDATE_URL)
  193. ) {
  194. $legalLinks .= $divider . '<a href="' . $link['url'] . '" class="legal" target="_blank"' .
  195. ' rel="noreferrer noopener">' . $link['text'] . '</a>';
  196. $divider = ' · ';
  197. }
  198. }
  199. if ($legalLinks !== '') {
  200. $footer .= '<br/>' . $legalLinks;
  201. }
  202. return $footer;
  203. }
  204. /**
  205. * Color that is used for the header as well as for mail headers
  206. *
  207. * @return string
  208. */
  209. public function getColorPrimary() {
  210. $color = $this->config->getAppValue('theming', 'color', $this->color);
  211. if (!preg_match('/^\#([0-9a-f]{3}|[0-9a-f]{6})$/i', $color)) {
  212. $color = '#0082c9';
  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(bool $brightBackground = true) {
  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(), $brightBackground);
  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. }