‪TYPO3CMS  9.5
SvgIconProvider.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
21 
26 {
27  const ‪MARKUP_IDENTIFIER_INLINE = 'inline';
28 
33  public function ‪prepareIconMarkup(‪Icon $icon, array $options = [])
34  {
35  $icon->‪setMarkup($this->‪generateMarkup($icon, $options));
36  $icon->‪setAlternativeMarkup(self::MARKUP_IDENTIFIER_INLINE, $this->‪generateInlineMarkup($icon, $options));
37  }
38 
45  protected function ‪generateMarkup(‪Icon $icon, array $options)
46  {
47  if (empty($options['source'])) {
48  throw new \InvalidArgumentException('[' . $icon->‪getIdentifier() . '] The option "source" is required and must not be empty', 1460976566);
49  }
50 
51  $source = $options['source'];
52 
53  if (strpos($source, 'EXT:') === 0 || strpos($source, '/') !== 0) {
54  $source = GeneralUtility::getFileAbsFileName($source);
55  }
56 
57  $source = ‪PathUtility::getAbsoluteWebPath($source);
58  return '<img src="' . htmlspecialchars($source) . '" width="' . $icon->‪getDimension()->‪getWidth() . '" height="' . $icon->‪getDimension()->‪getHeight() . '" />';
59  }
60 
67  protected function ‪generateInlineMarkup(‪Icon $icon, array $options)
68  {
69  if (empty($options['source'])) {
70  throw new \InvalidArgumentException('The option "source" is required and must not be empty', 1460976610);
71  }
72 
73  $source = $options['source'];
74 
75  if (strpos($source, 'EXT:') === 0 || strpos($source, '/') !== 0) {
76  $source = GeneralUtility::getFileAbsFileName($source);
77  }
78 
79  return $this->‪getInlineSvg($source);
80  }
81 
87  protected function ‪getInlineSvg($source)
88  {
89  if (!file_exists($source)) {
90  return '';
91  }
92 
93  $svgContent = file_get_contents($source);
94  $svgContent = preg_replace('/<script[\s\S]*?>[\s\S]*?<\/script>/i', '', $svgContent);
95  // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
96  $previousValueOfEntityLoader = libxml_disable_entity_loader(true);
97  $svgElement = simplexml_load_string($svgContent);
98  libxml_disable_entity_loader($previousValueOfEntityLoader);
99 
100  // remove xml version tag
101  $domXml = dom_import_simplexml($svgElement);
102  return $domXml->ownerDocument->saveXML($domXml->ownerDocument->documentElement);
103  }
104 }
‪TYPO3\CMS\Core\Imaging\IconProvider\SvgIconProvider\prepareIconMarkup
‪prepareIconMarkup(Icon $icon, array $options=[])
Definition: SvgIconProvider.php:33
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:23
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:25
‪TYPO3\CMS\Core\Imaging\IconProvider\SvgIconProvider\getInlineSvg
‪string getInlineSvg($source)
Definition: SvgIconProvider.php:87
‪TYPO3\CMS\Core\Imaging\IconProvider
Definition: BitmapIconProvider.php:2
‪TYPO3\CMS\Core\Imaging\IconProvider\SvgIconProvider\generateMarkup
‪string generateMarkup(Icon $icon, array $options)
Definition: SvgIconProvider.php:45
‪TYPO3\CMS\Core\Imaging\Dimension\getHeight
‪int getHeight()
Definition: Dimension.php:71
‪TYPO3\CMS\Core\Imaging\Icon\getIdentifier
‪string getIdentifier()
Definition: Icon.php:134
‪TYPO3\CMS\Core\Imaging\IconProviderInterface
Definition: IconProviderInterface.php:21
‪TYPO3\CMS\Core\Imaging\IconProvider\SvgIconProvider\MARKUP_IDENTIFIER_INLINE
‪const MARKUP_IDENTIFIER_INLINE
Definition: SvgIconProvider.php:27
‪TYPO3\CMS\Core\Imaging\IconProvider\SvgIconProvider\generateInlineMarkup
‪string generateInlineMarkup(Icon $icon, array $options)
Definition: SvgIconProvider.php:67
‪TYPO3\CMS\Core\Imaging\Icon\setMarkup
‪setMarkup($markup)
Definition: Icon.php:107
‪TYPO3\CMS\Core\Imaging\Dimension\getWidth
‪int getWidth()
Definition: Dimension.php:62
‪TYPO3\CMS\Core\Imaging\Icon\getDimension
‪Dimension getDimension()
Definition: Icon.php:219
‪TYPO3\CMS\Core\Imaging\IconProvider\SvgIconProvider
Definition: SvgIconProvider.php:26
‪TYPO3\CMS\Core\Imaging\Icon\setAlternativeMarkup
‪setAlternativeMarkup($markupIdentifier, $markup)
Definition: Icon.php:126
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Utility\PathUtility\getAbsoluteWebPath
‪static string getAbsoluteWebPath($targetPath)
Definition: PathUtility.php:42