1
0

Util.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
  4. *
  5. * @author Julius Haertl <jus@bitgrid.net>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\Theming;
  24. use OCP\App\AppPathNotFoundException;
  25. use OCP\App\IAppManager;
  26. use OCP\IConfig;
  27. use OCP\Files\IRootFolder;
  28. class Util {
  29. /** @var IConfig */
  30. private $config;
  31. /** @var IRootFolder */
  32. private $rootFolder;
  33. /** @var IAppManager */
  34. private $appManager;
  35. /**
  36. * Util constructor.
  37. *
  38. * @param IConfig $config
  39. * @param IRootFolder $rootFolder
  40. * @param IAppManager $appManager
  41. */
  42. public function __construct(IConfig $config, IRootFolder $rootFolder, IAppManager $appManager) {
  43. $this->config = $config;
  44. $this->rootFolder = $rootFolder;
  45. $this->appManager = $appManager;
  46. }
  47. /**
  48. * @param string $color rgb color value
  49. * @return bool
  50. */
  51. public function invertTextColor($color) {
  52. $l = $this->calculateLuminance($color);
  53. if($l>0.5) {
  54. return true;
  55. } else {
  56. return false;
  57. }
  58. }
  59. /**
  60. * get color for on-page elements:
  61. * theme color by default, grey if theme color is to bright
  62. * @param $color
  63. * @return string
  64. */
  65. public function elementColor($color) {
  66. $l = $this->calculateLuminance($color);
  67. if($l>0.8) {
  68. return '#555555';
  69. } else {
  70. return $color;
  71. }
  72. }
  73. /**
  74. * @param string $color rgb color value
  75. * @return float
  76. */
  77. public function calculateLuminance($color) {
  78. $hex = preg_replace("/[^0-9A-Fa-f]/", '', $color);
  79. if (strlen($hex) === 3) {
  80. $hex = $hex{0} . $hex{0} . $hex{1} . $hex{1} . $hex{2} . $hex{2};
  81. }
  82. if (strlen($hex) !== 6) {
  83. return 0;
  84. }
  85. $r = hexdec(substr($hex, 0, 2));
  86. $g = hexdec(substr($hex, 2, 2));
  87. $b = hexdec(substr($hex, 4, 2));
  88. return (0.299 * $r + 0.587 * $g + 0.114 * $b)/255;
  89. }
  90. /**
  91. * @param $color
  92. * @return string base64 encoded radio button svg
  93. */
  94. public function generateRadioButton($color) {
  95. $radioButtonIcon = '<svg xmlns="http://www.w3.org/2000/svg" height="16" width="16">' .
  96. '<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>';
  97. return base64_encode($radioButtonIcon);
  98. }
  99. /**
  100. * @param $app string app name
  101. * @return string path to app icon / logo
  102. */
  103. public function getAppIcon($app) {
  104. $app = str_replace(array('\0', '/', '\\', '..'), '', $app);
  105. try {
  106. $appPath = $this->appManager->getAppPath($app);
  107. $icon = $appPath . '/img/' . $app . '.svg';
  108. if (file_exists($icon)) {
  109. return $icon;
  110. }
  111. $icon = $appPath . '/img/app.svg';
  112. if (file_exists($icon)) {
  113. return $icon;
  114. }
  115. } catch (AppPathNotFoundException $e) {}
  116. if($this->config->getAppValue('theming', 'logoMime', '') !== '' && $this->rootFolder->nodeExists('/themedinstancelogo')) {
  117. return $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/themedinstancelogo';
  118. }
  119. return \OC::$SERVERROOT . '/core/img/logo.svg';
  120. }
  121. /**
  122. * @param $app string app name
  123. * @param $image string relative path to image in app folder
  124. * @return string|false absolute path to image
  125. */
  126. public function getAppImage($app, $image) {
  127. $app = str_replace(array('\0', '/', '\\', '..'), '', $app);
  128. $image = str_replace(array('\0', '\\', '..'), '', $image);
  129. if ($app === "core") {
  130. $icon = \OC::$SERVERROOT . '/core/img/' . $image;
  131. if (file_exists($icon)) {
  132. return $icon;
  133. }
  134. }
  135. try {
  136. $appPath = $this->appManager->getAppPath($app);
  137. } catch (AppPathNotFoundException $e) {
  138. return false;
  139. }
  140. $icon = $appPath . '/img/' . $image;
  141. if (file_exists($icon)) {
  142. return $icon;
  143. }
  144. $icon = $appPath . '/img/' . $image . '.svg';
  145. if (file_exists($icon)) {
  146. return $icon;
  147. }
  148. $icon = $appPath . '/img/' . $image . '.png';
  149. if (file_exists($icon)) {
  150. return $icon;
  151. }
  152. $icon = $appPath . '/img/' . $image . '.gif';
  153. if (file_exists($icon)) {
  154. return $icon;
  155. }
  156. $icon = $appPath . '/img/' . $image . '.jpg';
  157. if (file_exists($icon)) {
  158. return $icon;
  159. }
  160. return false;
  161. }
  162. /**
  163. * replace default color with a custom one
  164. *
  165. * @param $svg string content of a svg file
  166. * @param $color string color to match
  167. * @return string
  168. */
  169. public function colorizeSvg($svg, $color) {
  170. $svg = preg_replace('/#0082c9/i', $color, $svg);
  171. return $svg;
  172. }
  173. }