1
0

Util.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. * @param string $color rgb color value
  51. * @return bool
  52. */
  53. public function invertTextColor($color) {
  54. $l = $this->calculateLuma($color);
  55. if ($l > 0.6) {
  56. return true;
  57. } else {
  58. return false;
  59. }
  60. }
  61. /**
  62. * get color for on-page elements:
  63. * theme color by default, grey if theme color is to bright
  64. * @param string $color
  65. * @param bool $brightBackground
  66. * @return string
  67. */
  68. public function elementColor($color, bool $brightBackground = true) {
  69. $luminance = $this->calculateLuminance($color);
  70. if ($brightBackground && $luminance > 0.8) {
  71. // If the color is too bright in bright mode, we fall back to a darker gray
  72. return '#aaaaaa';
  73. }
  74. if (!$brightBackground && $luminance < 0.2) {
  75. // If the color is too dark in dark mode, we fall back to a brighter gray
  76. return '#555555';
  77. }
  78. return $color;
  79. }
  80. public function mix(string $color1, string $color2, int $factor): string {
  81. $color = new Color($color1);
  82. return '#' . $color->mix($color2, $factor);
  83. }
  84. public function lighten(string $color, int $factor): string {
  85. $color = new Color($color);
  86. return '#' . $color->lighten($factor);
  87. }
  88. public function darken(string $color, int $factor): string {
  89. $color = new Color($color);
  90. return '#' . $color->darken($factor);
  91. }
  92. /**
  93. * Convert RGB to HSL
  94. *
  95. * Copied from cssphp, copyright Leaf Corcoran, licensed under MIT
  96. *
  97. * @param int $red
  98. * @param int $green
  99. * @param int $blue
  100. *
  101. * @return float[]
  102. */
  103. public function toHSL(int $red, int $green, int $blue): array {
  104. $color = new Color(Color::rgbToHex(['R' => $red, 'G' => $green, 'B' => $blue]));
  105. return array_values($color->getHsl());
  106. }
  107. /**
  108. * @param string $color rgb color value
  109. * @return float
  110. */
  111. public function calculateLuminance(string $color): float {
  112. [$red, $green, $blue] = $this->hexToRGB($color);
  113. $hsl = $this->toHSL($red, $green, $blue);
  114. return $hsl[2];
  115. }
  116. /**
  117. * @param string $color rgb color value
  118. * @return float
  119. */
  120. public function calculateLuma(string $color): float {
  121. [$red, $green, $blue] = $this->hexToRGB($color);
  122. return (0.2126 * $red + 0.7152 * $green + 0.0722 * $blue) / 255;
  123. }
  124. /**
  125. * @param string $color rgb color value
  126. * @return int[]
  127. * @psalm-return array{0: int, 1: int, 2: int}
  128. */
  129. public function hexToRGB(string $color): array {
  130. $color = new Color($color);
  131. return array_values($color->getRgb());
  132. }
  133. /**
  134. * @param $color
  135. * @return string base64 encoded radio button svg
  136. */
  137. public function generateRadioButton($color) {
  138. $radioButtonIcon = '<svg xmlns="http://www.w3.org/2000/svg" height="16" width="16">' .
  139. '<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>';
  140. return base64_encode($radioButtonIcon);
  141. }
  142. /**
  143. * @param $app string app name
  144. * @return string|ISimpleFile path to app icon / file of logo
  145. */
  146. public function getAppIcon($app) {
  147. $app = str_replace(['\0', '/', '\\', '..'], '', $app);
  148. try {
  149. $appPath = $this->appManager->getAppPath($app);
  150. $icon = $appPath . '/img/' . $app . '.svg';
  151. if (file_exists($icon)) {
  152. return $icon;
  153. }
  154. $icon = $appPath . '/img/app.svg';
  155. if (file_exists($icon)) {
  156. return $icon;
  157. }
  158. } catch (AppPathNotFoundException $e) {
  159. }
  160. if ($this->config->getAppValue('theming', 'logoMime', '') !== '') {
  161. $logoFile = null;
  162. try {
  163. $folder = $this->appData->getFolder('global/images');
  164. return $folder->getFile('logo');
  165. } catch (NotFoundException $e) {
  166. }
  167. }
  168. return \OC::$SERVERROOT . '/core/img/logo/logo.svg';
  169. }
  170. /**
  171. * @param $app string app name
  172. * @param $image string relative path to image in app folder
  173. * @return string|false absolute path to image
  174. */
  175. public function getAppImage($app, $image) {
  176. $app = str_replace(['\0', '/', '\\', '..'], '', $app);
  177. $image = str_replace(['\0', '\\', '..'], '', $image);
  178. if ($app === "core") {
  179. $icon = \OC::$SERVERROOT . '/core/img/' . $image;
  180. if (file_exists($icon)) {
  181. return $icon;
  182. }
  183. }
  184. try {
  185. $appPath = $this->appManager->getAppPath($app);
  186. } catch (AppPathNotFoundException $e) {
  187. return false;
  188. }
  189. $icon = $appPath . '/img/' . $image;
  190. if (file_exists($icon)) {
  191. return $icon;
  192. }
  193. $icon = $appPath . '/img/' . $image . '.svg';
  194. if (file_exists($icon)) {
  195. return $icon;
  196. }
  197. $icon = $appPath . '/img/' . $image . '.png';
  198. if (file_exists($icon)) {
  199. return $icon;
  200. }
  201. $icon = $appPath . '/img/' . $image . '.gif';
  202. if (file_exists($icon)) {
  203. return $icon;
  204. }
  205. $icon = $appPath . '/img/' . $image . '.jpg';
  206. if (file_exists($icon)) {
  207. return $icon;
  208. }
  209. return false;
  210. }
  211. /**
  212. * replace default color with a custom one
  213. *
  214. * @param $svg string content of a svg file
  215. * @param $color string color to match
  216. * @return string
  217. */
  218. public function colorizeSvg($svg, $color) {
  219. $svg = preg_replace('/#0082c9/i', $color, $svg);
  220. return $svg;
  221. }
  222. /**
  223. * Check if a custom theme is set in the server configuration
  224. *
  225. * @return bool
  226. */
  227. public function isAlreadyThemed() {
  228. $theme = $this->config->getSystemValue('theme', '');
  229. if ($theme !== '') {
  230. return true;
  231. }
  232. return false;
  233. }
  234. public function isBackgroundThemed() {
  235. $backgroundLogo = $this->config->getAppValue('theming', 'backgroundMime', '');
  236. return $backgroundLogo !== '' && $backgroundLogo !== 'backgroundColor';
  237. }
  238. public function isLogoThemed() {
  239. return $this->imageManager->hasImage('logo')
  240. || $this->imageManager->hasImage('logoheader');
  241. }
  242. public function getCacheBuster(): string {
  243. $userSession = \OC::$server->get(IUserSession::class);
  244. $userId = '';
  245. $user = $userSession->getUser();
  246. if (!is_null($user)) {
  247. $userId = $user->getUID();
  248. }
  249. $userCacheBuster = '';
  250. if ($userId) {
  251. $userCacheBusterValue = (int)$this->config->getUserValue($userId, 'theming', 'userCacheBuster', '0');
  252. $userCacheBuster = $userId . '_' . $userCacheBusterValue;
  253. }
  254. $systemCacheBuster = $this->config->getAppValue('theming', 'cachebuster', '0');
  255. return substr(sha1($userCacheBuster . $systemCacheBuster), 0, 8);
  256. }
  257. }