Util.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Julius Haertl <jus@bitgrid.net>
  7. * @author Julius Härtl <jus@bitgrid.net>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\Theming;
  26. use OCP\App\AppPathNotFoundException;
  27. use OCP\App\IAppManager;
  28. use OCP\Files\IAppData;
  29. use OCP\Files\NotFoundException;
  30. use OCP\Files\SimpleFS\ISimpleFile;
  31. use OCP\IConfig;
  32. use Leafo\ScssPhp\Compiler;
  33. class Util {
  34. /** @var IConfig */
  35. private $config;
  36. /** @var IAppManager */
  37. private $appManager;
  38. /** @var IAppData */
  39. private $appData;
  40. /**
  41. * Util constructor.
  42. *
  43. * @param IConfig $config
  44. * @param IAppManager $appManager
  45. * @param IAppData $appData
  46. */
  47. public function __construct(IConfig $config, IAppManager $appManager, IAppData $appData) {
  48. $this->config = $config;
  49. $this->appManager = $appManager;
  50. $this->appData = $appData;
  51. }
  52. /**
  53. * @param string $color rgb color value
  54. * @return bool
  55. */
  56. public function invertTextColor($color) {
  57. $l = $this->calculateLuma($color);
  58. if($l>0.6) {
  59. return true;
  60. } else {
  61. return false;
  62. }
  63. }
  64. /**
  65. * get color for on-page elements:
  66. * theme color by default, grey if theme color is to bright
  67. * @param $color
  68. * @return string
  69. */
  70. public function elementColor($color) {
  71. $l = $this->calculateLuminance($color);
  72. if($l>0.8) {
  73. return '#dddddd';
  74. }
  75. return $color;
  76. }
  77. /**
  78. * @param string $color rgb color value
  79. * @return float
  80. */
  81. public function calculateLuminance($color) {
  82. list($red, $green, $blue) = $this->hexToRGB($color);
  83. $compiler = new Compiler();
  84. $hsl = $compiler->toHSL($red, $green, $blue);
  85. return $hsl[3]/100;
  86. }
  87. /**
  88. * @param string $color rgb color value
  89. * @return float
  90. */
  91. public function calculateLuma($color) {
  92. list($red, $green, $blue) = $this->hexToRGB($color);
  93. return (0.2126 * $red + 0.7152 * $green + 0.0722 * $blue) / 255;
  94. }
  95. /**
  96. * @param string $color rgb color value
  97. * @return int[]
  98. */
  99. public function hexToRGB($color) {
  100. $hex = preg_replace("/[^0-9A-Fa-f]/", '', $color);
  101. if (strlen($hex) === 3) {
  102. $hex = $hex{0} . $hex{0} . $hex{1} . $hex{1} . $hex{2} . $hex{2};
  103. }
  104. if (strlen($hex) !== 6) {
  105. return 0;
  106. }
  107. return [
  108. hexdec(substr($hex, 0, 2)),
  109. hexdec(substr($hex, 2, 2)),
  110. hexdec(substr($hex, 4, 2))
  111. ];
  112. }
  113. /**
  114. * @param $color
  115. * @return string base64 encoded radio button svg
  116. */
  117. public function generateRadioButton($color) {
  118. $radioButtonIcon = '<svg xmlns="http://www.w3.org/2000/svg" height="16" width="16">' .
  119. '<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>';
  120. return base64_encode($radioButtonIcon);
  121. }
  122. /**
  123. * @param $app string app name
  124. * @return string|ISimpleFile path to app icon / file of logo
  125. */
  126. public function getAppIcon($app) {
  127. $app = str_replace(array('\0', '/', '\\', '..'), '', $app);
  128. try {
  129. $appPath = $this->appManager->getAppPath($app);
  130. $icon = $appPath . '/img/' . $app . '.svg';
  131. if (file_exists($icon)) {
  132. return $icon;
  133. }
  134. $icon = $appPath . '/img/app.svg';
  135. if (file_exists($icon)) {
  136. return $icon;
  137. }
  138. } catch (AppPathNotFoundException $e) {}
  139. if ($this->config->getAppValue('theming', 'logoMime', '') !== '') {
  140. $logoFile = null;
  141. try {
  142. $folder = $this->appData->getFolder('images');
  143. if ($folder !== null) {
  144. return $folder->getFile('logo');
  145. }
  146. } catch (NotFoundException $e) {}
  147. }
  148. return \OC::$SERVERROOT . '/core/img/logo/logo.svg';
  149. }
  150. /**
  151. * @param $app string app name
  152. * @param $image string relative path to image in app folder
  153. * @return string|false absolute path to image
  154. */
  155. public function getAppImage($app, $image) {
  156. $app = str_replace(array('\0', '/', '\\', '..'), '', $app);
  157. $image = str_replace(array('\0', '\\', '..'), '', $image);
  158. if ($app === "core") {
  159. $icon = \OC::$SERVERROOT . '/core/img/' . $image;
  160. if (file_exists($icon)) {
  161. return $icon;
  162. }
  163. }
  164. try {
  165. $appPath = $this->appManager->getAppPath($app);
  166. } catch (AppPathNotFoundException $e) {
  167. return false;
  168. }
  169. $icon = $appPath . '/img/' . $image;
  170. if (file_exists($icon)) {
  171. return $icon;
  172. }
  173. $icon = $appPath . '/img/' . $image . '.svg';
  174. if (file_exists($icon)) {
  175. return $icon;
  176. }
  177. $icon = $appPath . '/img/' . $image . '.png';
  178. if (file_exists($icon)) {
  179. return $icon;
  180. }
  181. $icon = $appPath . '/img/' . $image . '.gif';
  182. if (file_exists($icon)) {
  183. return $icon;
  184. }
  185. $icon = $appPath . '/img/' . $image . '.jpg';
  186. if (file_exists($icon)) {
  187. return $icon;
  188. }
  189. return false;
  190. }
  191. /**
  192. * replace default color with a custom one
  193. *
  194. * @param $svg string content of a svg file
  195. * @param $color string color to match
  196. * @return string
  197. */
  198. public function colorizeSvg($svg, $color) {
  199. $svg = preg_replace('/#0082c9/i', $color, $svg);
  200. return $svg;
  201. }
  202. /**
  203. * Check if a custom theme is set in the server configuration
  204. *
  205. * @return bool
  206. */
  207. public function isAlreadyThemed() {
  208. $theme = $this->config->getSystemValue('theme', '');
  209. if ($theme !== '') {
  210. return true;
  211. }
  212. return false;
  213. }
  214. public function isBackgroundThemed() {
  215. $backgroundLogo = $this->config->getAppValue('theming', 'backgroundMime',false);
  216. $backgroundExists = true;
  217. try {
  218. $this->appData->getFolder('images')->getFile('background');
  219. } catch (\Exception $e) {
  220. $backgroundExists = false;
  221. }
  222. return $backgroundLogo && $backgroundLogo !== 'backgroundColor' && $backgroundExists;
  223. }
  224. }