ThemingDefaults.php 12 KB

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