You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

267 lines
7.5 KiB

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