ThemingDefaults.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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) <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\ICacheFactory;
  46. use OCP\IConfig;
  47. use OCP\IL10N;
  48. use OCP\INavigationManager;
  49. use OCP\IURLGenerator;
  50. class ThemingDefaults extends \OC_Defaults {
  51. /** @var IConfig */
  52. private $config;
  53. /** @var IL10N */
  54. private $l;
  55. /** @var ImageManager */
  56. private $imageManager;
  57. /** @var IURLGenerator */
  58. private $urlGenerator;
  59. /** @var ICacheFactory */
  60. private $cacheFactory;
  61. /** @var Util */
  62. private $util;
  63. /** @var IAppManager */
  64. private $appManager;
  65. /** @var INavigationManager */
  66. private $navigationManager;
  67. /** @var string */
  68. private $name;
  69. /** @var string */
  70. private $title;
  71. /** @var string */
  72. private $entity;
  73. /** @var string */
  74. private $url;
  75. /** @var string */
  76. private $color;
  77. /** @var string */
  78. private $iTunesAppId;
  79. /** @var string */
  80. private $iOSClientUrl;
  81. /** @var string */
  82. private $AndroidClientUrl;
  83. /**
  84. * ThemingDefaults constructor.
  85. *
  86. * @param IConfig $config
  87. * @param IL10N $l
  88. * @param ImageManager $imageManager
  89. * @param IURLGenerator $urlGenerator
  90. * @param ICacheFactory $cacheFactory
  91. * @param Util $util
  92. * @param IAppManager $appManager
  93. */
  94. public function __construct(IConfig $config,
  95. IL10N $l,
  96. IURLGenerator $urlGenerator,
  97. ICacheFactory $cacheFactory,
  98. Util $util,
  99. ImageManager $imageManager,
  100. IAppManager $appManager,
  101. INavigationManager $navigationManager
  102. ) {
  103. parent::__construct();
  104. $this->config = $config;
  105. $this->l = $l;
  106. $this->imageManager = $imageManager;
  107. $this->urlGenerator = $urlGenerator;
  108. $this->cacheFactory = $cacheFactory;
  109. $this->util = $util;
  110. $this->appManager = $appManager;
  111. $this->navigationManager = $navigationManager;
  112. $this->name = parent::getName();
  113. $this->title = parent::getTitle();
  114. $this->entity = parent::getEntity();
  115. $this->url = parent::getBaseUrl();
  116. $this->color = parent::getColorPrimary();
  117. $this->iTunesAppId = parent::getiTunesAppId();
  118. $this->iOSClientUrl = parent::getiOSClientUrl();
  119. $this->AndroidClientUrl = parent::getAndroidClientUrl();
  120. }
  121. public function getName() {
  122. return strip_tags($this->config->getAppValue('theming', 'name', $this->name));
  123. }
  124. public function getHTMLName() {
  125. return $this->config->getAppValue('theming', 'name', $this->name);
  126. }
  127. public function getTitle() {
  128. return strip_tags($this->config->getAppValue('theming', 'name', $this->title));
  129. }
  130. public function getEntity() {
  131. return strip_tags($this->config->getAppValue('theming', 'name', $this->entity));
  132. }
  133. public function getBaseUrl() {
  134. return $this->config->getAppValue('theming', 'url', $this->url);
  135. }
  136. public function getSlogan(?string $lang = null) {
  137. return \OCP\Util::sanitizeHTML($this->config->getAppValue('theming', 'slogan', parent::getSlogan($lang)));
  138. }
  139. public function getImprintUrl() {
  140. return (string)$this->config->getAppValue('theming', 'imprintUrl', '');
  141. }
  142. public function getPrivacyUrl() {
  143. return (string)$this->config->getAppValue('theming', 'privacyUrl', '');
  144. }
  145. public function getShortFooter() {
  146. $slogan = $this->getSlogan();
  147. $baseUrl = $this->getBaseUrl();
  148. if ($baseUrl !== '') {
  149. $footer = '<a href="' . $baseUrl . '" target="_blank"' .
  150. ' rel="noreferrer noopener" class="entity-name">' . $this->getEntity() . '</a>';
  151. } else {
  152. $footer = '<span class="entity-name">' .$this->getEntity() . '</span>';
  153. }
  154. $footer .= ($slogan !== '' ? ' – ' . $slogan : '');
  155. $links = [
  156. [
  157. 'text' => $this->l->t('Legal notice'),
  158. 'url' => (string)$this->getImprintUrl()
  159. ],
  160. [
  161. 'text' => $this->l->t('Privacy policy'),
  162. 'url' => (string)$this->getPrivacyUrl()
  163. ],
  164. ];
  165. $navigation = $this->navigationManager->getAll(INavigationManager::TYPE_GUEST);
  166. $guestNavigation = array_map(function ($nav) {
  167. return [
  168. 'text' => $nav['name'],
  169. 'url' => $nav['href']
  170. ];
  171. }, $navigation);
  172. $links = array_merge($links, $guestNavigation);
  173. $legalLinks = '';
  174. $divider = '';
  175. foreach ($links as $link) {
  176. if ($link['url'] !== ''
  177. && filter_var($link['url'], FILTER_VALIDATE_URL)
  178. ) {
  179. $legalLinks .= $divider . '<a href="' . $link['url'] . '" class="legal" target="_blank"' .
  180. ' rel="noreferrer noopener">' . $link['text'] . '</a>';
  181. $divider = ' · ';
  182. }
  183. }
  184. if ($legalLinks !== '') {
  185. $footer .= '<br/>' . $legalLinks;
  186. }
  187. return $footer;
  188. }
  189. /**
  190. * Color that is used for the header as well as for mail headers
  191. *
  192. * @return string
  193. */
  194. public function getColorPrimary() {
  195. return $this->config->getAppValue('theming', 'color', $this->color);
  196. }
  197. /**
  198. * Themed logo url
  199. *
  200. * @param bool $useSvg Whether to point to the SVG image or a fallback
  201. * @return string
  202. */
  203. public function getLogo($useSvg = true): string {
  204. $logo = $this->config->getAppValue('theming', 'logoMime', '');
  205. // short cut to avoid setting up the filesystem just to check if the logo is there
  206. //
  207. // explanation: if an SVG is requested and the app config value for logoMime is set then the logo is there.
  208. // otherwise we need to check it and maybe also generate a PNG from the SVG (that's done in getImage() which
  209. // needs to be called then)
  210. if ($useSvg === true && $logo !== false) {
  211. $logoExists = true;
  212. } else {
  213. try {
  214. $this->imageManager->getImage('logo', $useSvg);
  215. $logoExists = true;
  216. } catch (\Exception $e) {
  217. $logoExists = false;
  218. }
  219. }
  220. $cacheBusterCounter = $this->config->getAppValue('theming', 'cachebuster', '0');
  221. if (!$logo || !$logoExists) {
  222. if ($useSvg) {
  223. $logo = $this->urlGenerator->imagePath('core', 'logo/logo.svg');
  224. } else {
  225. $logo = $this->urlGenerator->imagePath('core', 'logo/logo.png');
  226. }
  227. return $logo . '?v=' . $cacheBusterCounter;
  228. }
  229. return $this->urlGenerator->linkToRoute('theming.Theming.getImage', [ 'key' => 'logo', 'useSvg' => $useSvg, 'v' => $cacheBusterCounter ]);
  230. }
  231. /**
  232. * Themed background image url
  233. *
  234. * @return string
  235. */
  236. public function getBackground(): string {
  237. return $this->imageManager->getImageUrl('background');
  238. }
  239. /**
  240. * @return string
  241. */
  242. public function getiTunesAppId() {
  243. return $this->config->getAppValue('theming', 'iTunesAppId', $this->iTunesAppId);
  244. }
  245. /**
  246. * @return string
  247. */
  248. public function getiOSClientUrl() {
  249. return $this->config->getAppValue('theming', 'iOSClientUrl', $this->iOSClientUrl);
  250. }
  251. /**
  252. * @return string
  253. */
  254. public function getAndroidClientUrl() {
  255. return $this->config->getAppValue('theming', 'AndroidClientUrl', $this->AndroidClientUrl);
  256. }
  257. /**
  258. * @return array scss variables to overwrite
  259. */
  260. public function getScssVariables() {
  261. $cacheBuster = $this->config->getAppValue('theming', 'cachebuster', '0');
  262. $cache = $this->cacheFactory->createDistributed('theming-' . $cacheBuster . '-' . $this->urlGenerator->getBaseUrl());
  263. if ($value = $cache->get('getScssVariables')) {
  264. return $value;
  265. }
  266. $variables = [
  267. 'theming-cachebuster' => "'" . $cacheBuster . "'",
  268. 'theming-logo-mime' => "'" . $this->config->getAppValue('theming', 'logoMime') . "'",
  269. 'theming-background-mime' => "'" . $this->config->getAppValue('theming', 'backgroundMime') . "'",
  270. 'theming-logoheader-mime' => "'" . $this->config->getAppValue('theming', 'logoheaderMime') . "'",
  271. 'theming-favicon-mime' => "'" . $this->config->getAppValue('theming', 'faviconMime') . "'"
  272. ];
  273. $variables['image-logo'] = "url('".$this->imageManager->getImageUrl('logo')."')";
  274. $variables['image-logoheader'] = "url('".$this->imageManager->getImageUrl('logoheader')."')";
  275. $variables['image-favicon'] = "url('".$this->imageManager->getImageUrl('favicon')."')";
  276. $variables['image-login-background'] = "url('".$this->imageManager->getImageUrl('background')."')";
  277. $variables['image-login-plain'] = 'false';
  278. if ($this->config->getAppValue('theming', 'color', '') !== '') {
  279. $variables['color-primary'] = $this->getColorPrimary();
  280. $variables['color-primary-text'] = $this->getTextColorPrimary();
  281. $variables['color-primary-element'] = $this->util->elementColor($this->getColorPrimary());
  282. }
  283. if ($this->config->getAppValue('theming', 'backgroundMime', '') === 'backgroundColor') {
  284. $variables['image-login-plain'] = 'true';
  285. }
  286. $variables['has-legal-links'] = 'false';
  287. if ($this->getImprintUrl() !== '' || $this->getPrivacyUrl() !== '') {
  288. $variables['has-legal-links'] = 'true';
  289. }
  290. $cache->set('getScssVariables', $variables);
  291. return $variables;
  292. }
  293. /**
  294. * Check if the image should be replaced by the theming app
  295. * and return the new image location then
  296. *
  297. * @param string $app name of the app
  298. * @param string $image filename of the image
  299. * @return bool|string false if image should not replaced, otherwise the location of the image
  300. */
  301. public function replaceImagePath($app, $image) {
  302. if ($app === '' || $app === 'files_sharing') {
  303. $app = 'core';
  304. }
  305. $cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0');
  306. try {
  307. $customFavicon = $this->imageManager->getImage('favicon');
  308. } catch (NotFoundException $e) {
  309. $customFavicon = null;
  310. }
  311. $route = false;
  312. if ($image === 'favicon.ico' && ($customFavicon !== null || $this->imageManager->shouldReplaceIcons())) {
  313. $route = $this->urlGenerator->linkToRoute('theming.Icon.getFavicon', ['app' => $app]);
  314. }
  315. if (($image === 'favicon-touch.png' || $image === 'favicon-fb.png') && ($customFavicon !== null || $this->imageManager->shouldReplaceIcons())) {
  316. $route = $this->urlGenerator->linkToRoute('theming.Icon.getTouchIcon', ['app' => $app]);
  317. }
  318. if ($image === 'manifest.json') {
  319. try {
  320. $appPath = $this->appManager->getAppPath($app);
  321. if (file_exists($appPath . '/img/manifest.json')) {
  322. return false;
  323. }
  324. } catch (AppPathNotFoundException $e) {
  325. }
  326. $route = $this->urlGenerator->linkToRoute('theming.Theming.getManifest');
  327. }
  328. if (strpos($image, 'filetypes/') === 0 && file_exists(\OC::$SERVERROOT . '/core/img/' . $image)) {
  329. $route = $this->urlGenerator->linkToRoute('theming.Icon.getThemedIcon', ['app' => $app, 'image' => $image]);
  330. }
  331. if ($route) {
  332. return $route . '?v=' . $cacheBusterValue;
  333. }
  334. return false;
  335. }
  336. /**
  337. * Increases the cache buster key
  338. */
  339. private function increaseCacheBuster() {
  340. $cacheBusterKey = $this->config->getAppValue('theming', 'cachebuster', '0');
  341. $this->config->setAppValue('theming', 'cachebuster', (int)$cacheBusterKey + 1);
  342. $this->cacheFactory->createDistributed('theming-')->clear();
  343. $this->cacheFactory->createDistributed('imagePath')->clear();
  344. }
  345. /**
  346. * Update setting in the database
  347. *
  348. * @param string $setting
  349. * @param string $value
  350. */
  351. public function set($setting, $value) {
  352. $this->config->setAppValue('theming', $setting, $value);
  353. $this->increaseCacheBuster();
  354. }
  355. /**
  356. * Revert settings to the default value
  357. *
  358. * @param string $setting setting which should be reverted
  359. * @return string default value
  360. */
  361. public function undo($setting) {
  362. $this->config->deleteAppValue('theming', $setting);
  363. $this->increaseCacheBuster();
  364. $returnValue = '';
  365. switch ($setting) {
  366. case 'name':
  367. $returnValue = $this->getEntity();
  368. break;
  369. case 'url':
  370. $returnValue = $this->getBaseUrl();
  371. break;
  372. case 'slogan':
  373. $returnValue = $this->getSlogan();
  374. break;
  375. case 'color':
  376. $returnValue = $this->getColorPrimary();
  377. break;
  378. case 'logo':
  379. case 'logoheader':
  380. case 'background':
  381. case 'favicon':
  382. $this->imageManager->delete($setting);
  383. break;
  384. }
  385. return $returnValue;
  386. }
  387. /**
  388. * Color of text in the header and primary buttons
  389. *
  390. * @return string
  391. */
  392. public function getTextColorPrimary() {
  393. return $this->util->invertTextColor($this->getColorPrimary()) ? '#000000' : '#ffffff';
  394. }
  395. }