You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ThemingDefaults.php 14KB

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