ThemingDefaults.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 Jan-Christoph Borchardt <hey@jancborchardt.net>
  9. * @author Joachim Bauch <bauch@struktur.de>
  10. * @author Joas Schilling <coding@schilljs.com>
  11. * @author Julius Haertl <jus@bitgrid.net>
  12. * @author Julius Härtl <jus@bitgrid.net>
  13. * @author Lukas Reschke <lukas@statuscode.ch>
  14. * @author Morris Jobke <hey@morrisjobke.de>
  15. * @author Roeland Jago Douma <roeland@famdouma.nl>
  16. *
  17. * @license GNU AGPL version 3 or any later version
  18. *
  19. * This program is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Affero General Public License as
  21. * published by the Free Software Foundation, either version 3 of the
  22. * License, or (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU Affero General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Affero General Public License
  30. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  31. *
  32. */
  33. namespace OCA\Theming;
  34. use OCP\App\AppPathNotFoundException;
  35. use OCP\App\IAppManager;
  36. use OCP\Files\NotFoundException;
  37. use OCP\ICacheFactory;
  38. use OCP\IConfig;
  39. use OCP\IL10N;
  40. use OCP\INavigationManager;
  41. use OCP\IURLGenerator;
  42. class ThemingDefaults extends \OC_Defaults {
  43. /** @var IConfig */
  44. private $config;
  45. /** @var IL10N */
  46. private $l;
  47. /** @var ImageManager */
  48. private $imageManager;
  49. /** @var IURLGenerator */
  50. private $urlGenerator;
  51. /** @var ICacheFactory */
  52. private $cacheFactory;
  53. /** @var Util */
  54. private $util;
  55. /** @var IAppManager */
  56. private $appManager;
  57. /** @var INavigationManager */
  58. private $navigationManager;
  59. /** @var string */
  60. private $name;
  61. /** @var string */
  62. private $title;
  63. /** @var string */
  64. private $entity;
  65. /** @var string */
  66. private $url;
  67. /** @var string */
  68. private $slogan;
  69. /** @var string */
  70. private $color;
  71. /** @var string */
  72. private $iTunesAppId;
  73. /** @var string */
  74. private $iOSClientUrl;
  75. /** @var string */
  76. private $AndroidClientUrl;
  77. /**
  78. * ThemingDefaults constructor.
  79. *
  80. * @param IConfig $config
  81. * @param IL10N $l
  82. * @param ImageManager $imageManager
  83. * @param IURLGenerator $urlGenerator
  84. * @param ICacheFactory $cacheFactory
  85. * @param Util $util
  86. * @param IAppManager $appManager
  87. */
  88. public function __construct(IConfig $config,
  89. IL10N $l,
  90. IURLGenerator $urlGenerator,
  91. ICacheFactory $cacheFactory,
  92. Util $util,
  93. ImageManager $imageManager,
  94. IAppManager $appManager,
  95. INavigationManager $navigationManager
  96. ) {
  97. parent::__construct();
  98. $this->config = $config;
  99. $this->l = $l;
  100. $this->imageManager = $imageManager;
  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->url = parent::getBaseUrl();
  110. $this->slogan = parent::getSlogan();
  111. $this->color = parent::getColorPrimary();
  112. $this->iTunesAppId = parent::getiTunesAppId();
  113. $this->iOSClientUrl = parent::getiOSClientUrl();
  114. $this->AndroidClientUrl = parent::getAndroidClientUrl();
  115. }
  116. public function getName() {
  117. return strip_tags($this->config->getAppValue('theming', 'name', $this->name));
  118. }
  119. public function getHTMLName() {
  120. return $this->config->getAppValue('theming', 'name', $this->name);
  121. }
  122. public function getTitle() {
  123. return strip_tags($this->config->getAppValue('theming', 'name', $this->title));
  124. }
  125. public function getEntity() {
  126. return strip_tags($this->config->getAppValue('theming', 'name', $this->entity));
  127. }
  128. public function getBaseUrl() {
  129. return $this->config->getAppValue('theming', 'url', $this->url);
  130. }
  131. public function getSlogan() {
  132. return \OCP\Util::sanitizeHTML($this->config->getAppValue('theming', 'slogan', $this->slogan));
  133. }
  134. public function getImprintUrl() {
  135. return (string)$this->config->getAppValue('theming', 'imprintUrl', '');
  136. }
  137. public function getPrivacyUrl() {
  138. return (string)$this->config->getAppValue('theming', 'privacyUrl', '');
  139. }
  140. public function getShortFooter() {
  141. $slogan = $this->getSlogan();
  142. $baseUrl = $this->getBaseUrl();
  143. if ($baseUrl !== '') {
  144. $footer = '<a href="' . $baseUrl . '" target="_blank"' .
  145. ' rel="noreferrer noopener" class="entity-name">' . $this->getEntity() . '</a>';
  146. } else {
  147. $footer = '<span class="entity-name">' .$this->getEntity() . '</span>';
  148. }
  149. $footer .= ($slogan !== '' ? ' – ' . $slogan : '');
  150. $links = [
  151. [
  152. 'text' => $this->l->t('Legal notice'),
  153. 'url' => (string)$this->getImprintUrl()
  154. ],
  155. [
  156. 'text' => $this->l->t('Privacy policy'),
  157. 'url' => (string)$this->getPrivacyUrl()
  158. ],
  159. ];
  160. $navigation = $this->navigationManager->getAll(INavigationManager::TYPE_GUEST);
  161. $guestNavigation = array_map(function($nav) {
  162. return [
  163. 'text' => $nav['name'],
  164. 'url' => $nav['href']
  165. ];
  166. }, $navigation);
  167. $links = array_merge($links, $guestNavigation);
  168. $legalLinks = ''; $divider = '';
  169. foreach($links as $link) {
  170. if($link['url'] !== ''
  171. && filter_var($link['url'], FILTER_VALIDATE_URL)
  172. ) {
  173. $legalLinks .= $divider . '<a href="' . $link['url'] . '" class="legal" target="_blank"' .
  174. ' rel="noreferrer noopener">' . $link['text'] . '</a>';
  175. $divider = ' · ';
  176. }
  177. }
  178. if($legalLinks !== '' ) {
  179. $footer .= '<br/>' . $legalLinks;
  180. }
  181. return $footer;
  182. }
  183. /**
  184. * Color that is used for the header as well as for mail headers
  185. *
  186. * @return string
  187. */
  188. public function getColorPrimary() {
  189. return $this->config->getAppValue('theming', 'color', $this->color);
  190. }
  191. /**
  192. * Themed logo url
  193. *
  194. * @param bool $useSvg Whether to point to the SVG image or a fallback
  195. * @return string
  196. */
  197. public function getLogo($useSvg = true): string {
  198. $logo = $this->config->getAppValue('theming', 'logoMime', false);
  199. $logoExists = true;
  200. try {
  201. $this->imageManager->getImage('logo', $useSvg);
  202. } catch (\Exception $e) {
  203. $logoExists = false;
  204. }
  205. $cacheBusterCounter = $this->config->getAppValue('theming', 'cachebuster', '0');
  206. if(!$logo || !$logoExists) {
  207. if($useSvg) {
  208. $logo = $this->urlGenerator->imagePath('core', 'logo/logo.svg');
  209. } else {
  210. $logo = $this->urlGenerator->imagePath('core', 'logo/logo.png');
  211. }
  212. return $logo . '?v=' . $cacheBusterCounter;
  213. }
  214. return $this->urlGenerator->linkToRoute('theming.Theming.getImage', [ 'key' => 'logo', 'useSvg' => $useSvg, 'v' => $cacheBusterCounter ]);
  215. }
  216. /**
  217. * Themed background image url
  218. *
  219. * @return string
  220. */
  221. public function getBackground(): string {
  222. return $this->imageManager->getImageUrl('background');
  223. }
  224. /**
  225. * @return string
  226. */
  227. public function getiTunesAppId() {
  228. return $this->config->getAppValue('theming', 'iTunesAppId', $this->iTunesAppId);
  229. }
  230. /**
  231. * @return string
  232. */
  233. public function getiOSClientUrl() {
  234. return $this->config->getAppValue('theming', 'iOSClientUrl', $this->iOSClientUrl);
  235. }
  236. /**
  237. * @return string
  238. */
  239. public function getAndroidClientUrl() {
  240. return $this->config->getAppValue('theming', 'AndroidClientUrl', $this->AndroidClientUrl);
  241. }
  242. /**
  243. * @return array scss variables to overwrite
  244. */
  245. public function getScssVariables() {
  246. $cache = $this->cacheFactory->createDistributed('theming-' . $this->urlGenerator->getBaseUrl());
  247. if ($value = $cache->get('getScssVariables')) {
  248. return $value;
  249. }
  250. $variables = [
  251. 'theming-cachebuster' => "'" . $this->config->getAppValue('theming', 'cachebuster', '0') . "'",
  252. 'theming-logo-mime' => "'" . $this->config->getAppValue('theming', 'logoMime') . "'",
  253. 'theming-background-mime' => "'" . $this->config->getAppValue('theming', 'backgroundMime') . "'",
  254. 'theming-logoheader-mime' => "'" . $this->config->getAppValue('theming', 'logoheaderMime') . "'",
  255. 'theming-favicon-mime' => "'" . $this->config->getAppValue('theming', 'faviconMime') . "'"
  256. ];
  257. $variables['image-logo'] = "url('".$this->imageManager->getImageUrl('logo')."')";
  258. $variables['image-logoheader'] = "'".$this->imageManager->getImageUrl('logoheader')."'";
  259. $variables['image-favicon'] = "'".$this->imageManager->getImageUrl('favicon')."'";
  260. $variables['image-login-background'] = "url('".$this->imageManager->getImageUrl('background')."')";
  261. $variables['image-login-plain'] = 'false';
  262. if ($this->config->getAppValue('theming', 'color', null) !== null) {
  263. $variables['color-primary'] = $this->getColorPrimary();
  264. $variables['color-primary-text'] = $this->getTextColorPrimary();
  265. $variables['color-primary-element'] = $this->util->elementColor($this->getColorPrimary());
  266. }
  267. if ($this->config->getAppValue('theming', 'backgroundMime', null) === 'backgroundColor') {
  268. $variables['image-login-plain'] = 'true';
  269. }
  270. $variables['has-legal-links'] = 'false';
  271. if($this->getImprintUrl() !== '' || $this->getPrivacyUrl() !== '') {
  272. $variables['has-legal-links'] = 'true';
  273. }
  274. $cache->set('getScssVariables', $variables);
  275. return $variables;
  276. }
  277. /**
  278. * Check if the image should be replaced by the theming app
  279. * and return the new image location then
  280. *
  281. * @param string $app name of the app
  282. * @param string $image filename of the image
  283. * @return bool|string false if image should not replaced, otherwise the location of the image
  284. */
  285. public function replaceImagePath($app, $image) {
  286. if ($app === '' || $app === 'files_sharing') {
  287. $app = 'core';
  288. }
  289. $cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0');
  290. try {
  291. $customFavicon = $this->imageManager->getImage('favicon');
  292. } catch (NotFoundException $e) {
  293. $customFavicon = null;
  294. }
  295. if ($image === 'favicon.ico' && ($customFavicon !== null || $this->imageManager->shouldReplaceIcons())) {
  296. return $this->urlGenerator->linkToRoute('theming.Icon.getFavicon', ['app' => $app]) . '?v=' . $cacheBusterValue;
  297. }
  298. if ($image === 'favicon-touch.png' && ($customFavicon !== null || $this->imageManager->shouldReplaceIcons())) {
  299. return $this->urlGenerator->linkToRoute('theming.Icon.getTouchIcon', ['app' => $app]) . '?v=' . $cacheBusterValue;
  300. }
  301. if ($image === 'manifest.json') {
  302. try {
  303. $appPath = $this->appManager->getAppPath($app);
  304. if (file_exists($appPath . '/img/manifest.json')) {
  305. return false;
  306. }
  307. } catch (AppPathNotFoundException $e) {}
  308. return $this->urlGenerator->linkToRoute('theming.Theming.getManifest') . '?v=' . $cacheBusterValue;
  309. }
  310. if (strpos($image, 'filetypes/') === 0 && file_exists(\OC::$SERVERROOT . '/core/img/' . $image )) {
  311. return $this->urlGenerator->linkToRoute('theming.Icon.getThemedIcon', ['app' => $app, 'image' => $image]);
  312. }
  313. return false;
  314. }
  315. /**
  316. * Increases the cache buster key
  317. */
  318. private function increaseCacheBuster() {
  319. $cacheBusterKey = $this->config->getAppValue('theming', 'cachebuster', '0');
  320. $this->config->setAppValue('theming', 'cachebuster', (int)$cacheBusterKey+1);
  321. $this->cacheFactory->createDistributed('theming-')->clear();
  322. $this->cacheFactory->createDistributed('imagePath')->clear();
  323. }
  324. /**
  325. * Update setting in the database
  326. *
  327. * @param string $setting
  328. * @param string $value
  329. */
  330. public function set($setting, $value) {
  331. $this->config->setAppValue('theming', $setting, $value);
  332. $this->increaseCacheBuster();
  333. }
  334. /**
  335. * Revert settings to the default value
  336. *
  337. * @param string $setting setting which should be reverted
  338. * @return string default value
  339. */
  340. public function undo($setting) {
  341. $this->config->deleteAppValue('theming', $setting);
  342. $this->increaseCacheBuster();
  343. switch ($setting) {
  344. case 'name':
  345. $returnValue = $this->getEntity();
  346. break;
  347. case 'url':
  348. $returnValue = $this->getBaseUrl();
  349. break;
  350. case 'slogan':
  351. $returnValue = $this->getSlogan();
  352. break;
  353. case 'color':
  354. $returnValue = $this->getColorPrimary();
  355. break;
  356. default:
  357. $returnValue = '';
  358. break;
  359. }
  360. return $returnValue;
  361. }
  362. /**
  363. * Color of text in the header and primary buttons
  364. *
  365. * @return string
  366. */
  367. public function getTextColorPrimary() {
  368. return $this->util->invertTextColor($this->getColorPrimary()) ? '#000000' : '#ffffff';
  369. }
  370. }