ThemingDefaults.php 12 KB

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