ThemingDefaults.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 $this->config->getAppValue('theming', 'imprintUrl', '');
  131. }
  132. public function getShortFooter() {
  133. $slogan = $this->getSlogan();
  134. $footer = '<a href="'. $this->getBaseUrl() . '" target="_blank"' .
  135. ' rel="noreferrer noopener">' .$this->getEntity() . '</a>'.
  136. ($slogan !== '' ? ' – ' . $slogan : '');
  137. $imprintUrl = (string)$this->getImprintUrl();
  138. if($imprintUrl !== ''
  139. && filter_var($imprintUrl, FILTER_VALIDATE_URL, [
  140. 'flags' => FILTER_FLAG_SCHEME_REQUIRED | FILTER_FLAG_HOST_REQUIRED
  141. ])
  142. ) {
  143. $footer .= '<br/><a href="' . $imprintUrl . '" class="legal" target="_blank"' .
  144. ' rel="noreferrer noopener">' . $this->l->t('Legal notice') . '</a>';
  145. }
  146. return $footer;
  147. }
  148. /**
  149. * Color that is used for the header as well as for mail headers
  150. *
  151. * @return string
  152. */
  153. public function getColorPrimary() {
  154. return $this->config->getAppValue('theming', 'color', $this->color);
  155. }
  156. /**
  157. * Themed logo url
  158. *
  159. * @param bool $useSvg Whether to point to the SVG image or a fallback
  160. * @return string
  161. */
  162. public function getLogo($useSvg = true): string {
  163. $logo = $this->config->getAppValue('theming', 'logoMime', false);
  164. $logoExists = true;
  165. try {
  166. $this->imageManager->getImage('logo');
  167. } catch (\Exception $e) {
  168. $logoExists = false;
  169. }
  170. $cacheBusterCounter = $this->config->getAppValue('theming', 'cachebuster', '0');
  171. if(!$logo || !$logoExists) {
  172. if($useSvg) {
  173. $logo = $this->urlGenerator->imagePath('core', 'logo.svg');
  174. } else {
  175. $logo = $this->urlGenerator->imagePath('core', 'logo.png');
  176. }
  177. return $logo . '?v=' . $cacheBusterCounter;
  178. }
  179. return $this->urlGenerator->linkToRoute('theming.Theming.getImage', [ 'key' => 'logo' ]) . '?v=' . $cacheBusterCounter;
  180. }
  181. /**
  182. * Themed background image url
  183. *
  184. * @return string
  185. */
  186. public function getBackground(): string {
  187. return $this->imageManager->getImageUrl('background');
  188. }
  189. /**
  190. * @return string
  191. */
  192. public function getiTunesAppId() {
  193. return $this->config->getAppValue('theming', 'iTunesAppId', $this->iTunesAppId);
  194. }
  195. /**
  196. * @return string
  197. */
  198. public function getiOSClientUrl() {
  199. return $this->config->getAppValue('theming', 'iOSClientUrl', $this->iOSClientUrl);
  200. }
  201. /**
  202. * @return string
  203. */
  204. public function getAndroidClientUrl() {
  205. return $this->config->getAppValue('theming', 'AndroidClientUrl', $this->AndroidClientUrl);
  206. }
  207. /**
  208. * @return array scss variables to overwrite
  209. */
  210. public function getScssVariables() {
  211. $cache = $this->cacheFactory->createDistributed('theming-' . $this->urlGenerator->getBaseUrl());
  212. if ($value = $cache->get('getScssVariables')) {
  213. return $value;
  214. }
  215. $variables = [
  216. 'theming-cachebuster' => "'" . $this->config->getAppValue('theming', 'cachebuster', '0') . "'",
  217. 'theming-logo-mime' => "'" . $this->config->getAppValue('theming', 'logoMime') . "'",
  218. 'theming-background-mime' => "'" . $this->config->getAppValue('theming', 'backgroundMime') . "'",
  219. 'theming-logoheader-mime' => "'" . $this->config->getAppValue('theming', 'logoheaderMime') . "'",
  220. 'theming-favicon-mime' => "'" . $this->config->getAppValue('theming', 'faviconMime') . "'"
  221. ];
  222. $variables['image-logo'] = "'".$this->imageManager->getImageUrl('logo')."'";
  223. $variables['image-logoheader'] = "'".$this->imageManager->getImageUrl('logoheader')."'";
  224. $variables['image-favicon'] = "'".$this->imageManager->getImageUrl('favicon')."'";
  225. $variables['image-login-background'] = "'".$this->imageManager->getImageUrl('background')."'";
  226. $variables['image-login-plain'] = 'false';
  227. if ($this->config->getAppValue('theming', 'color', null) !== null) {
  228. $variables['color-primary'] = $this->getColorPrimary();
  229. $variables['color-primary-text'] = $this->getTextColorPrimary();
  230. $variables['color-primary-element'] = $this->util->elementColor($this->getColorPrimary());
  231. }
  232. if ($this->config->getAppValue('theming', 'backgroundMime', null) === 'backgroundColor') {
  233. $variables['image-login-plain'] = 'true';
  234. }
  235. $cache->set('getScssVariables', $variables);
  236. return $variables;
  237. }
  238. /**
  239. * Check if the image should be replaced by the theming app
  240. * and return the new image location then
  241. *
  242. * @param string $app name of the app
  243. * @param string $image filename of the image
  244. * @return bool|string false if image should not replaced, otherwise the location of the image
  245. */
  246. public function replaceImagePath($app, $image) {
  247. if($app==='') {
  248. $app = 'core';
  249. }
  250. $cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0');
  251. try {
  252. $customFavicon = $this->imageManager->getImage('favicon');
  253. } catch (NotFoundException $e) {
  254. $customFavicon = null;
  255. }
  256. if ($image === 'favicon.ico' && ($customFavicon !== null || $this->shouldReplaceIcons())) {
  257. return $this->urlGenerator->linkToRoute('theming.Icon.getFavicon', ['app' => $app]) . '?v=' . $cacheBusterValue;
  258. }
  259. if ($image === 'favicon-touch.png' && ($customFavicon !== null || $this->shouldReplaceIcons())) {
  260. return $this->urlGenerator->linkToRoute('theming.Icon.getTouchIcon', ['app' => $app]) . '?v=' . $cacheBusterValue;
  261. }
  262. if ($image === 'manifest.json') {
  263. try {
  264. $appPath = $this->appManager->getAppPath($app);
  265. if (file_exists($appPath . '/img/manifest.json')) {
  266. return false;
  267. }
  268. } catch (AppPathNotFoundException $e) {}
  269. return $this->urlGenerator->linkToRoute('theming.Theming.getManifest') . '?v=' . $cacheBusterValue;
  270. }
  271. return false;
  272. }
  273. /**
  274. * Check if Imagemagick is enabled and if SVG is supported
  275. * otherwise we can't render custom icons
  276. *
  277. * @return bool
  278. */
  279. public function shouldReplaceIcons() {
  280. $cache = $this->cacheFactory->createDistributed('theming-' . $this->urlGenerator->getBaseUrl());
  281. if($value = $cache->get('shouldReplaceIcons')) {
  282. return (bool)$value;
  283. }
  284. $value = false;
  285. if(extension_loaded('imagick')) {
  286. $checkImagick = new \Imagick();
  287. if (count($checkImagick->queryFormats('SVG')) >= 1) {
  288. $value = true;
  289. }
  290. $checkImagick->clear();
  291. }
  292. $cache->set('shouldReplaceIcons', $value);
  293. return $value;
  294. }
  295. /**
  296. * Increases the cache buster key
  297. */
  298. private function increaseCacheBuster() {
  299. $cacheBusterKey = $this->config->getAppValue('theming', 'cachebuster', '0');
  300. $this->config->setAppValue('theming', 'cachebuster', (int)$cacheBusterKey+1);
  301. $this->cacheFactory->createDistributed('theming-')->clear();
  302. $this->cacheFactory->createDistributed('imagePath')->clear();
  303. }
  304. /**
  305. * Update setting in the database
  306. *
  307. * @param string $setting
  308. * @param string $value
  309. */
  310. public function set($setting, $value) {
  311. $this->config->setAppValue('theming', $setting, $value);
  312. $this->increaseCacheBuster();
  313. }
  314. /**
  315. * Revert settings to the default value
  316. *
  317. * @param string $setting setting which should be reverted
  318. * @return string default value
  319. */
  320. public function undo($setting) {
  321. $this->config->deleteAppValue('theming', $setting);
  322. $this->increaseCacheBuster();
  323. switch ($setting) {
  324. case 'name':
  325. $returnValue = $this->getEntity();
  326. break;
  327. case 'url':
  328. $returnValue = $this->getBaseUrl();
  329. break;
  330. case 'slogan':
  331. $returnValue = $this->getSlogan();
  332. break;
  333. case 'color':
  334. $returnValue = $this->getColorPrimary();
  335. break;
  336. default:
  337. $returnValue = '';
  338. break;
  339. }
  340. return $returnValue;
  341. }
  342. /**
  343. * Color of text in the header and primary buttons
  344. *
  345. * @return string
  346. */
  347. public function getTextColorPrimary() {
  348. return $this->util->invertTextColor($this->getColorPrimary()) ? '#000000' : '#ffffff';
  349. }
  350. }