IconsCacher.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. declare (strict_types = 1);
  3. /**
  4. * @copyright Copyright (c) 2018, John Molakvoæ (skjnldsv@protonmail.com)
  5. *
  6. * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Template;
  25. use OCP\AppFramework\Utility\ITimeFactory;
  26. use OCP\Files\IAppData;
  27. use OCP\Files\NotFoundException;
  28. use OCP\Files\SimpleFS\ISimpleFolder;
  29. use OCP\Files\SimpleFS\ISimpleFile;
  30. use OCP\ILogger;
  31. use OCP\IURLGenerator;
  32. use OC\Files\AppData\Factory;
  33. class IconsCacher {
  34. /** @var ILogger */
  35. protected $logger;
  36. /** @var IAppData */
  37. protected $appData;
  38. /** @var ISimpleFolder */
  39. private $folder;
  40. /** @var IURLGenerator */
  41. protected $urlGenerator;
  42. /** @var ITimeFactory */
  43. protected $timeFactory;
  44. /** @var string */
  45. private $iconVarRE = '/--(icon-[a-zA-Z0-9-]+):\s?url\(["\']?([a-zA-Z0-9-_\~\/\.\?\&\=\:\;\+\,]+)[^;]+;/m';
  46. /** @var string */
  47. private $fileName = 'icons-vars.css';
  48. private $iconList = 'icons-list.template';
  49. /**
  50. * @param ILogger $logger
  51. * @param Factory $appDataFactory
  52. * @param IURLGenerator $urlGenerator
  53. * @param ITimeFactory $timeFactory
  54. * @throws \OCP\Files\NotPermittedException
  55. */
  56. public function __construct(ILogger $logger,
  57. Factory $appDataFactory,
  58. IURLGenerator $urlGenerator,
  59. ITimeFactory $timeFactory) {
  60. $this->logger = $logger;
  61. $this->appData = $appDataFactory->get('css');
  62. $this->urlGenerator = $urlGenerator;
  63. $this->timeFactory = $timeFactory;
  64. try {
  65. $this->folder = $this->appData->getFolder('icons');
  66. } catch (NotFoundException $e) {
  67. $this->folder = $this->appData->newFolder('icons');
  68. }
  69. }
  70. private function getIconsFromCss(string $css): array {
  71. preg_match_all($this->iconVarRE, $css, $matches, PREG_SET_ORDER);
  72. $icons = [];
  73. foreach ($matches as $icon) {
  74. $icons[$icon[1]] = $icon[2];
  75. }
  76. return $icons;
  77. }
  78. /**
  79. * @param string $css
  80. * @return string
  81. * @throws NotFoundException
  82. * @throws \OCP\Files\NotPermittedException
  83. */
  84. public function setIconsCss(string $css): string {
  85. $cachedFile = $this->getCachedList();
  86. if (!$cachedFile) {
  87. $currentData = '';
  88. $cachedFile = $this->folder->newFile($this->iconList);
  89. } else {
  90. $currentData = $cachedFile->getContent();
  91. }
  92. $cachedVarsCssFile = $this->getCachedCSS();
  93. if (!$cachedVarsCssFile) {
  94. $cachedVarsCssFile = $this->folder->newFile($this->fileName);
  95. }
  96. $icons = $this->getIconsFromCss($currentData . $css);
  97. $data = '';
  98. $list = '';
  99. foreach ($icons as $icon => $url) {
  100. $list .= "--$icon: url('$url');";
  101. list($location,$color) = $this->parseUrl($url);
  102. $svg = false;
  103. if ($location !== '' && \file_exists($location)) {
  104. $svg = \file_get_contents($location);
  105. }
  106. if ($svg === false) {
  107. $this->logger->debug('Failed to get icon file ' . $location);
  108. $data .= "--$icon: url('$url');";
  109. continue;
  110. }
  111. $encode = base64_encode($this->colorizeSvg($svg, $color));
  112. $data .= '--' . $icon . ': url(data:image/svg+xml;base64,' . $encode . ');';
  113. }
  114. if (\strlen($data) > 0 && \strlen($list) > 0) {
  115. $data = ":root {\n$data\n}";
  116. $cachedVarsCssFile->putContent($data);
  117. $list = ":root {\n$list\n}";
  118. $cachedFile->putContent($list);
  119. }
  120. return preg_replace($this->iconVarRE, '', $css);
  121. }
  122. /**
  123. * @param $url
  124. * @return array
  125. */
  126. private function parseUrl($url): array {
  127. $location = '';
  128. $color = '';
  129. $base = $this->getRoutePrefix() . '/svg/';
  130. $cleanUrl = \substr($url, \strlen($base));
  131. if (\strpos($url, $base . 'core') === 0) {
  132. $cleanUrl = \substr($cleanUrl, \strlen('core'));
  133. if (\preg_match('/\/([a-zA-Z0-9-_\~\/\.\=\:\;\+\,]+)\?color=([0-9a-fA-F]{3,6})/', $cleanUrl, $matches)) {
  134. list(,$cleanUrl,$color) = $matches;
  135. $location = \OC::$SERVERROOT . '/core/img/' . $cleanUrl . '.svg';
  136. }
  137. } elseif (\strpos($url, $base) === 0) {
  138. if(\preg_match('/([A-z0-9\_\-]+)\/([a-zA-Z0-9-_\~\/\.\=\:\;\+\,]+)\?color=([0-9a-fA-F]{3,6})/', $cleanUrl, $matches)) {
  139. list(,$app,$cleanUrl, $color) = $matches;
  140. $location = \OC_App::getAppPath($app) . '/img/' . $cleanUrl . '.svg';
  141. if ($app === 'settings') {
  142. $location = \OC::$SERVERROOT . '/settings/img/' . $cleanUrl . '.svg';
  143. }
  144. }
  145. }
  146. return [
  147. $location,
  148. $color
  149. ];
  150. }
  151. /**
  152. * @param $svg
  153. * @param $color
  154. * @return string
  155. */
  156. public function colorizeSvg($svg, $color): string {
  157. // add fill (fill is not present on black elements)
  158. $fillRe = '/<((circle|rect|path)((?!fill)[a-z0-9 =".\-#():;])+)\/>/mi';
  159. $svg = preg_replace($fillRe, '<$1 fill="#' . $color . '"/>', $svg);
  160. // replace any fill or stroke colors
  161. $svg = preg_replace('/stroke="#([a-z0-9]{3,6})"/mi', 'stroke="#' . $color . '"', $svg);
  162. $svg = preg_replace('/fill="#([a-z0-9]{3,6})"/mi', 'fill="#' . $color . '"', $svg);
  163. return $svg;
  164. }
  165. private function getRoutePrefix() {
  166. $frontControllerActive = (\OC::$server->getConfig()->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true');
  167. $prefix = \OC::$WEBROOT . '/index.php';
  168. if ($frontControllerActive) {
  169. $prefix = \OC::$WEBROOT;
  170. }
  171. return $prefix;
  172. }
  173. /**
  174. * Get icons css file
  175. * @return ISimpleFile|boolean
  176. */
  177. public function getCachedCSS() {
  178. try {
  179. return $this->folder->getFile($this->fileName);
  180. } catch (NotFoundException $e) {
  181. return false;
  182. }
  183. }
  184. /**
  185. * Get icon-vars list template
  186. * @return ISimpleFile|boolean
  187. */
  188. public function getCachedList() {
  189. try {
  190. return $this->folder->getFile($this->iconList);
  191. } catch (NotFoundException $e) {
  192. return false;
  193. }
  194. }
  195. public function injectCss() {
  196. $mtime = $this->timeFactory->getTime();
  197. $file = $this->getCachedList();
  198. if ($file) {
  199. $mtime = $file->getMTime();
  200. }
  201. // Only inject once
  202. foreach (\OC_Util::$headers as $header) {
  203. if (
  204. array_key_exists('attributes', $header) &&
  205. array_key_exists('href', $header['attributes']) &&
  206. strpos($header['attributes']['href'], $this->fileName) !== false) {
  207. return;
  208. }
  209. }
  210. $linkToCSS = $this->urlGenerator->linkToRoute('core.Css.getCss', ['appName' => 'icons', 'fileName' => $this->fileName, 'v' => $mtime]);
  211. \OC_Util::addHeader('link', ['rel' => 'stylesheet', 'href' => $linkToCSS], null, true);
  212. }
  213. }