Util.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Julien Veyssier <eneiluj@posteo.net>
  9. * @author Julius Haertl <jus@bitgrid.net>
  10. * @author Julius Härtl <jus@bitgrid.net>
  11. * @author Michael Weimann <mail@michael-weimann.eu>
  12. *
  13. * @license GNU AGPL version 3 or any later version
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License as
  17. * published by the Free Software Foundation, either version 3 of the
  18. * License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *
  28. */
  29. namespace OCA\Theming;
  30. use OCP\App\AppPathNotFoundException;
  31. use OCP\App\IAppManager;
  32. use OCP\Files\IAppData;
  33. use OCP\Files\NotFoundException;
  34. use OCP\Files\SimpleFS\ISimpleFile;
  35. use OCP\IConfig;
  36. use OCP\IUserSession;
  37. use Mexitek\PHPColors\Color;
  38. class Util {
  39. private IConfig $config;
  40. private IAppManager $appManager;
  41. private IAppData $appData;
  42. private ImageManager $imageManager;
  43. public function __construct(IConfig $config, IAppManager $appManager, IAppData $appData, ImageManager $imageManager) {
  44. $this->config = $config;
  45. $this->appManager = $appManager;
  46. $this->appData = $appData;
  47. $this->imageManager = $imageManager;
  48. }
  49. /**
  50. * Should we invert the text on this background color?
  51. * @param string $color rgb color value
  52. * @return bool
  53. */
  54. public function invertTextColor(string $color): bool {
  55. return $this->isBrightColor($color);
  56. }
  57. /**
  58. * Is this color too bright ?
  59. * @param string $color rgb color value
  60. * @return bool
  61. */
  62. public function isBrightColor(string $color): bool {
  63. $l = $this->calculateLuma($color);
  64. if ($l > 0.6) {
  65. return true;
  66. } else {
  67. return false;
  68. }
  69. }
  70. /**
  71. * get color for on-page elements:
  72. * theme color by default, grey if theme color is to bright
  73. * @param string $color
  74. * @param bool $brightBackground
  75. * @return string
  76. */
  77. public function elementColor($color, bool $brightBackground = true) {
  78. $luminance = $this->calculateLuminance($color);
  79. if ($brightBackground && $luminance > 0.8) {
  80. // If the color is too bright in bright mode, we fall back to a darker gray
  81. return '#aaaaaa';
  82. }
  83. if (!$brightBackground && $luminance < 0.2) {
  84. // If the color is too dark in dark mode, we fall back to a brighter gray
  85. return '#8c8c8c';
  86. }
  87. return $color;
  88. }
  89. public function mix(string $color1, string $color2, int $factor): string {
  90. $color = new Color($color1);
  91. return '#' . $color->mix($color2, $factor);
  92. }
  93. public function lighten(string $color, int $factor): string {
  94. $color = new Color($color);
  95. return '#' . $color->lighten($factor);
  96. }
  97. public function darken(string $color, int $factor): string {
  98. $color = new Color($color);
  99. return '#' . $color->darken($factor);
  100. }
  101. /**
  102. * Convert RGB to HSL
  103. *
  104. * Copied from cssphp, copyright Leaf Corcoran, licensed under MIT
  105. *
  106. * @param int $red
  107. * @param int $green
  108. * @param int $blue
  109. *
  110. * @return float[]
  111. */
  112. public function toHSL(int $red, int $green, int $blue): array {
  113. $color = new Color(Color::rgbToHex(['R' => $red, 'G' => $green, 'B' => $blue]));
  114. return array_values($color->getHsl());
  115. }
  116. /**
  117. * @param string $color rgb color value
  118. * @return float
  119. */
  120. public function calculateLuminance(string $color): float {
  121. [$red, $green, $blue] = $this->hexToRGB($color);
  122. $hsl = $this->toHSL($red, $green, $blue);
  123. return $hsl[2];
  124. }
  125. /**
  126. * @param string $color rgb color value
  127. * @return float
  128. */
  129. public function calculateLuma(string $color): float {
  130. [$red, $green, $blue] = $this->hexToRGB($color);
  131. return (0.2126 * $red + 0.7152 * $green + 0.0722 * $blue) / 255;
  132. }
  133. /**
  134. * @param string $color rgb color value
  135. * @return int[]
  136. * @psalm-return array{0: int, 1: int, 2: int}
  137. */
  138. public function hexToRGB(string $color): array {
  139. $color = new Color($color);
  140. return array_values($color->getRgb());
  141. }
  142. /**
  143. * @param $color
  144. * @return string base64 encoded radio button svg
  145. */
  146. public function generateRadioButton($color) {
  147. $radioButtonIcon = '<svg xmlns="http://www.w3.org/2000/svg" height="16" width="16">' .
  148. '<path d="M8 1a7 7 0 0 0-7 7 7 7 0 0 0 7 7 7 7 0 0 0 7-7 7 7 0 0 0-7-7zm0 1a6 6 0 0 1 6 6 6 6 0 0 1-6 6 6 6 0 0 1-6-6 6 6 0 0 1 6-6zm0 2a4 4 0 1 0 0 8 4 4 0 0 0 0-8z" fill="'.$color.'"/></svg>';
  149. return base64_encode($radioButtonIcon);
  150. }
  151. /**
  152. * @param $app string app name
  153. * @return string|ISimpleFile path to app icon / file of logo
  154. */
  155. public function getAppIcon($app) {
  156. $app = str_replace(['\0', '/', '\\', '..'], '', $app);
  157. try {
  158. $appPath = $this->appManager->getAppPath($app);
  159. $icon = $appPath . '/img/' . $app . '.svg';
  160. if (file_exists($icon)) {
  161. return $icon;
  162. }
  163. $icon = $appPath . '/img/app.svg';
  164. if (file_exists($icon)) {
  165. return $icon;
  166. }
  167. } catch (AppPathNotFoundException $e) {
  168. }
  169. if ($this->config->getAppValue('theming', 'logoMime', '') !== '') {
  170. $logoFile = null;
  171. try {
  172. $folder = $this->appData->getFolder('global/images');
  173. return $folder->getFile('logo');
  174. } catch (NotFoundException $e) {
  175. }
  176. }
  177. return \OC::$SERVERROOT . '/core/img/logo/logo.svg';
  178. }
  179. /**
  180. * @param $app string app name
  181. * @param $image string relative path to image in app folder
  182. * @return string|false absolute path to image
  183. */
  184. public function getAppImage($app, $image) {
  185. $app = str_replace(['\0', '/', '\\', '..'], '', $app);
  186. $image = str_replace(['\0', '\\', '..'], '', $image);
  187. if ($app === "core") {
  188. $icon = \OC::$SERVERROOT . '/core/img/' . $image;
  189. if (file_exists($icon)) {
  190. return $icon;
  191. }
  192. }
  193. try {
  194. $appPath = $this->appManager->getAppPath($app);
  195. } catch (AppPathNotFoundException $e) {
  196. return false;
  197. }
  198. $icon = $appPath . '/img/' . $image;
  199. if (file_exists($icon)) {
  200. return $icon;
  201. }
  202. $icon = $appPath . '/img/' . $image . '.svg';
  203. if (file_exists($icon)) {
  204. return $icon;
  205. }
  206. $icon = $appPath . '/img/' . $image . '.png';
  207. if (file_exists($icon)) {
  208. return $icon;
  209. }
  210. $icon = $appPath . '/img/' . $image . '.gif';
  211. if (file_exists($icon)) {
  212. return $icon;
  213. }
  214. $icon = $appPath . '/img/' . $image . '.jpg';
  215. if (file_exists($icon)) {
  216. return $icon;
  217. }
  218. return false;
  219. }
  220. /**
  221. * replace default color with a custom one
  222. *
  223. * @param $svg string content of a svg file
  224. * @param $color string color to match
  225. * @return string
  226. */
  227. public function colorizeSvg($svg, $color) {
  228. $svg = preg_replace('/#0082c9/i', $color, $svg);
  229. return $svg;
  230. }
  231. /**
  232. * Check if a custom theme is set in the server configuration
  233. *
  234. * @return bool
  235. */
  236. public function isAlreadyThemed() {
  237. $theme = $this->config->getSystemValue('theme', '');
  238. if ($theme !== '') {
  239. return true;
  240. }
  241. return false;
  242. }
  243. public function isBackgroundThemed() {
  244. $backgroundLogo = $this->config->getAppValue('theming', 'backgroundMime', '');
  245. return $backgroundLogo !== '' && $backgroundLogo !== 'backgroundColor';
  246. }
  247. public function isLogoThemed() {
  248. return $this->imageManager->hasImage('logo')
  249. || $this->imageManager->hasImage('logoheader');
  250. }
  251. public function getCacheBuster(): string {
  252. $userSession = \OC::$server->get(IUserSession::class);
  253. $userId = '';
  254. $user = $userSession->getUser();
  255. if (!is_null($user)) {
  256. $userId = $user->getUID();
  257. }
  258. $userCacheBuster = '';
  259. if ($userId) {
  260. $userCacheBusterValue = (int)$this->config->getUserValue($userId, 'theming', 'userCacheBuster', '0');
  261. $userCacheBuster = $userId . '_' . $userCacheBusterValue;
  262. }
  263. $systemCacheBuster = $this->config->getAppValue('theming', 'cachebuster', '0');
  264. return substr(sha1($userCacheBuster . $systemCacheBuster), 0, 8);
  265. }
  266. }