1
0

IconsCacher.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. private $cachedCss;
  50. private $cachedList;
  51. /**
  52. * @param ILogger $logger
  53. * @param Factory $appDataFactory
  54. * @param IURLGenerator $urlGenerator
  55. * @param ITimeFactory $timeFactory
  56. * @throws \OCP\Files\NotPermittedException
  57. */
  58. public function __construct(ILogger $logger,
  59. Factory $appDataFactory,
  60. IURLGenerator $urlGenerator,
  61. ITimeFactory $timeFactory) {
  62. $this->logger = $logger;
  63. $this->appData = $appDataFactory->get('css');
  64. $this->urlGenerator = $urlGenerator;
  65. $this->timeFactory = $timeFactory;
  66. try {
  67. $this->folder = $this->appData->getFolder('icons');
  68. } catch (NotFoundException $e) {
  69. $this->folder = $this->appData->newFolder('icons');
  70. }
  71. }
  72. private function getIconsFromCss(string $css): array {
  73. preg_match_all($this->iconVarRE, $css, $matches, PREG_SET_ORDER);
  74. $icons = [];
  75. foreach ($matches as $icon) {
  76. $icons[$icon[1]] = $icon[2];
  77. }
  78. return $icons;
  79. }
  80. /**
  81. * @param string $css
  82. * @return string
  83. * @throws NotFoundException
  84. * @throws \OCP\Files\NotPermittedException
  85. */
  86. public function setIconsCss(string $css): string {
  87. $cachedFile = $this->getCachedList();
  88. if (!$cachedFile) {
  89. $currentData = '';
  90. $cachedFile = $this->folder->newFile($this->iconList);
  91. } else {
  92. $currentData = $cachedFile->getContent();
  93. }
  94. $cachedVarsCssFile = $this->getCachedCSS();
  95. if (!$cachedVarsCssFile) {
  96. $cachedVarsCssFile = $this->folder->newFile($this->fileName);
  97. }
  98. $icons = $this->getIconsFromCss($currentData . $css);
  99. $data = '';
  100. $list = '';
  101. foreach ($icons as $icon => $url) {
  102. $list .= "--$icon: url('$url');";
  103. list($location,$color) = $this->parseUrl($url);
  104. $svg = false;
  105. if ($location !== '' && \file_exists($location)) {
  106. $svg = \file_get_contents($location);
  107. }
  108. if ($svg === false) {
  109. $this->logger->debug('Failed to get icon file ' . $location);
  110. $data .= "--$icon: url('$url');";
  111. continue;
  112. }
  113. $encode = base64_encode($this->colorizeSvg($svg, $color));
  114. $data .= '--' . $icon . ': url(data:image/svg+xml;base64,' . $encode . ');';
  115. }
  116. if (\strlen($data) > 0 && \strlen($list) > 0) {
  117. $data = ":root {\n$data\n}";
  118. $cachedVarsCssFile->putContent($data);
  119. $list = ":root {\n$list\n}";
  120. $cachedFile->putContent($list);
  121. $this->cachedList = null;
  122. $this->cachedCss = null;
  123. }
  124. return preg_replace($this->iconVarRE, '', $css);
  125. }
  126. /**
  127. * @param $url
  128. * @return array
  129. */
  130. private function parseUrl($url): array {
  131. $location = '';
  132. $color = '';
  133. $base = $this->getRoutePrefix() . '/svg/';
  134. $cleanUrl = \substr($url, \strlen($base));
  135. if (\strpos($url, $base . 'core') === 0) {
  136. $cleanUrl = \substr($cleanUrl, \strlen('core'));
  137. if (\preg_match('/\/([a-zA-Z0-9-_\~\/\.\=\:\;\+\,]+)\?color=([0-9a-fA-F]{3,6})/', $cleanUrl, $matches)) {
  138. list(,$cleanUrl,$color) = $matches;
  139. $location = \OC::$SERVERROOT . '/core/img/' . $cleanUrl . '.svg';
  140. }
  141. } elseif (\strpos($url, $base) === 0) {
  142. if(\preg_match('/([A-z0-9\_\-]+)\/([a-zA-Z0-9-_\~\/\.\=\:\;\+\,]+)\?color=([0-9a-fA-F]{3,6})/', $cleanUrl, $matches)) {
  143. list(,$app,$cleanUrl, $color) = $matches;
  144. $location = \OC_App::getAppPath($app) . '/img/' . $cleanUrl . '.svg';
  145. if ($app === 'settings') {
  146. $location = \OC::$SERVERROOT . '/settings/img/' . $cleanUrl . '.svg';
  147. }
  148. }
  149. }
  150. return [
  151. $location,
  152. $color
  153. ];
  154. }
  155. /**
  156. * @param $svg
  157. * @param $color
  158. * @return string
  159. */
  160. public function colorizeSvg($svg, $color): string {
  161. // add fill (fill is not present on black elements)
  162. $fillRe = '/<((circle|rect|path)((?!fill)[a-z0-9 =".\-#():;,])+)\/>/mi';
  163. $svg = preg_replace($fillRe, '<$1 fill="#' . $color . '"/>', $svg);
  164. // replace any fill or stroke colors
  165. $svg = preg_replace('/stroke="#([a-z0-9]{3,6})"/mi', 'stroke="#' . $color . '"', $svg);
  166. $svg = preg_replace('/fill="#([a-z0-9]{3,6})"/mi', 'fill="#' . $color . '"', $svg);
  167. return $svg;
  168. }
  169. private function getRoutePrefix() {
  170. $frontControllerActive = (\OC::$server->getConfig()->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true');
  171. $prefix = \OC::$WEBROOT . '/index.php';
  172. if ($frontControllerActive) {
  173. $prefix = \OC::$WEBROOT;
  174. }
  175. return $prefix;
  176. }
  177. /**
  178. * Get icons css file
  179. * @return ISimpleFile|boolean
  180. */
  181. public function getCachedCSS() {
  182. try {
  183. if (!$this->cachedCss) {
  184. $this->cachedCss = $this->folder->getFile($this->fileName);
  185. }
  186. return $this->cachedCss;
  187. } catch (NotFoundException $e) {
  188. return false;
  189. }
  190. }
  191. /**
  192. * Get icon-vars list template
  193. * @return ISimpleFile|boolean
  194. */
  195. public function getCachedList() {
  196. try {
  197. if (!$this->cachedList) {
  198. $this->cachedList = $this->folder->getFile($this->iconList);
  199. }
  200. return $this->cachedList;
  201. } catch (NotFoundException $e) {
  202. return false;
  203. }
  204. }
  205. public function injectCss() {
  206. $mtime = $this->timeFactory->getTime();
  207. $file = $this->getCachedList();
  208. if ($file) {
  209. $mtime = $file->getMTime();
  210. }
  211. // Only inject once
  212. foreach (\OC_Util::$headers as $header) {
  213. if (
  214. array_key_exists('attributes', $header) &&
  215. array_key_exists('href', $header['attributes']) &&
  216. strpos($header['attributes']['href'], $this->fileName) !== false) {
  217. return;
  218. }
  219. }
  220. $linkToCSS = $this->urlGenerator->linkToRoute('core.Css.getCss', ['appName' => 'icons', 'fileName' => $this->fileName, 'v' => $mtime]);
  221. \OC_Util::addHeader('link', ['rel' => 'stylesheet', 'href' => $linkToCSS], null, true);
  222. }
  223. }