Util.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 OCP\Files\IRootFolder;
  33. use Leafo\ScssPhp\Compiler;
  34. class Util {
  35. /** @var IConfig */
  36. private $config;
  37. /** @var IAppManager */
  38. private $appManager;
  39. /** @var IAppData */
  40. private $appData;
  41. /**
  42. * Util constructor.
  43. *
  44. * @param IConfig $config
  45. * @param IAppManager $appManager
  46. * @param IAppData $appData
  47. */
  48. public function __construct(IConfig $config, IAppManager $appManager, IAppData $appData) {
  49. $this->config = $config;
  50. $this->appManager = $appManager;
  51. $this->appData = $appData;
  52. }
  53. /**
  54. * @param string $color rgb color value
  55. * @return bool
  56. */
  57. public function invertTextColor($color) {
  58. $l = $this->calculateLuma($color);
  59. if($l>0.6) {
  60. return true;
  61. } else {
  62. return false;
  63. }
  64. }
  65. /**
  66. * get color for on-page elements:
  67. * theme color by default, grey if theme color is to bright
  68. * @param $color
  69. * @return string
  70. */
  71. public function elementColor($color) {
  72. $l = $this->calculateLuminance($color);
  73. if($l>0.8) {
  74. return '#555555';
  75. }
  76. return $color;
  77. }
  78. /**
  79. * @param string $color rgb color value
  80. * @return float
  81. */
  82. public function calculateLuminance($color) {
  83. list($red, $green, $blue) = $this->hexToRGB($color);
  84. $compiler = new Compiler();
  85. $hsl = $compiler->toHSL($red, $green, $blue);
  86. return $hsl[3]/100;
  87. }
  88. /**
  89. * @param string $color rgb color value
  90. * @return float
  91. */
  92. public function calculateLuma($color) {
  93. list($red, $green, $blue) = $this->hexToRGB($color);
  94. return (0.2126 * $red + 0.7152 * $green + 0.0722 * $blue) / 255;
  95. }
  96. /**
  97. * @param string $color rgb color value
  98. * @return int[]
  99. */
  100. public function hexToRGB($color) {
  101. $hex = preg_replace("/[^0-9A-Fa-f]/", '', $color);
  102. if (strlen($hex) === 3) {
  103. $hex = $hex{0} . $hex{0} . $hex{1} . $hex{1} . $hex{2} . $hex{2};
  104. }
  105. if (strlen($hex) !== 6) {
  106. return 0;
  107. }
  108. return [
  109. hexdec(substr($hex, 0, 2)),
  110. hexdec(substr($hex, 2, 2)),
  111. hexdec(substr($hex, 4, 2))
  112. ];
  113. }
  114. /**
  115. * @param $color
  116. * @return string base64 encoded radio button svg
  117. */
  118. public function generateRadioButton($color) {
  119. $radioButtonIcon = '<svg xmlns="http://www.w3.org/2000/svg" height="16" width="16">' .
  120. '<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>';
  121. return base64_encode($radioButtonIcon);
  122. }
  123. /**
  124. * @param $app string app name
  125. * @return string|ISimpleFile path to app icon / file of logo
  126. */
  127. public function getAppIcon($app) {
  128. $app = str_replace(array('\0', '/', '\\', '..'), '', $app);
  129. try {
  130. $appPath = $this->appManager->getAppPath($app);
  131. $icon = $appPath . '/img/' . $app . '.svg';
  132. if (file_exists($icon)) {
  133. return $icon;
  134. }
  135. $icon = $appPath . '/img/app.svg';
  136. if (file_exists($icon)) {
  137. return $icon;
  138. }
  139. } catch (AppPathNotFoundException $e) {}
  140. if ($this->config->getAppValue('theming', 'logoMime', '') !== '') {
  141. $logoFile = null;
  142. try {
  143. $folder = $this->appData->getFolder('images');
  144. if ($folder !== null) {
  145. return $folder->getFile('logo');
  146. }
  147. } catch (NotFoundException $e) {}
  148. }
  149. return \OC::$SERVERROOT . '/core/img/logo.svg';
  150. }
  151. /**
  152. * @param $app string app name
  153. * @param $image string relative path to image in app folder
  154. * @return string|false absolute path to image
  155. */
  156. public function getAppImage($app, $image) {
  157. $app = str_replace(array('\0', '/', '\\', '..'), '', $app);
  158. $image = str_replace(array('\0', '\\', '..'), '', $image);
  159. if ($app === "core") {
  160. $icon = \OC::$SERVERROOT . '/core/img/' . $image;
  161. if (file_exists($icon)) {
  162. return $icon;
  163. }
  164. }
  165. try {
  166. $appPath = $this->appManager->getAppPath($app);
  167. } catch (AppPathNotFoundException $e) {
  168. return false;
  169. }
  170. $icon = $appPath . '/img/' . $image;
  171. if (file_exists($icon)) {
  172. return $icon;
  173. }
  174. $icon = $appPath . '/img/' . $image . '.svg';
  175. if (file_exists($icon)) {
  176. return $icon;
  177. }
  178. $icon = $appPath . '/img/' . $image . '.png';
  179. if (file_exists($icon)) {
  180. return $icon;
  181. }
  182. $icon = $appPath . '/img/' . $image . '.gif';
  183. if (file_exists($icon)) {
  184. return $icon;
  185. }
  186. $icon = $appPath . '/img/' . $image . '.jpg';
  187. if (file_exists($icon)) {
  188. return $icon;
  189. }
  190. return false;
  191. }
  192. /**
  193. * replace default color with a custom one
  194. *
  195. * @param $svg string content of a svg file
  196. * @param $color string color to match
  197. * @return string
  198. */
  199. public function colorizeSvg($svg, $color) {
  200. $svg = preg_replace('/#0082c9/i', $color, $svg);
  201. return $svg;
  202. }
  203. /**
  204. * Check if a custom theme is set in the server configuration
  205. *
  206. * @return bool
  207. */
  208. public function isAlreadyThemed() {
  209. $theme = $this->config->getSystemValue('theme', '');
  210. if ($theme !== '') {
  211. return true;
  212. }
  213. return false;
  214. }
  215. public function isBackgroundThemed() {
  216. $backgroundLogo = $this->config->getAppValue('theming', 'backgroundMime',false);
  217. $backgroundExists = true;
  218. try {
  219. $this->appData->getFolder('images')->getFile('background');
  220. } catch (\Exception $e) {
  221. $backgroundExists = false;
  222. }
  223. return $backgroundLogo && $backgroundLogo !== 'backgroundColor' && $backgroundExists;
  224. }
  225. }