[TASK] Remove duplicate exception numbers
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Imaging / IconProvider / SvgIconProvider.php
1 <?php
2 namespace TYPO3\CMS\Core\Imaging\IconProvider;
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
17 use TYPO3\CMS\Core\Imaging\Icon;
18 use TYPO3\CMS\Core\Imaging\IconProviderInterface;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Core\Utility\PathUtility;
21 use TYPO3\CMS\Core\Utility\StringUtility;
22
23 /**
24 * Class SvgIconProvider provides icons that are classic <img> tags using vectors as source
25 */
26 class SvgIconProvider implements IconProviderInterface
27 {
28 const MARKUP_IDENTIFIER_INLINE = 'inline';
29
30 /**
31 * @param Icon $icon
32 * @param array $options
33 */
34 public function prepareIconMarkup(Icon $icon, array $options = array())
35 {
36 $icon->setMarkup($this->generateMarkup($icon, $options));
37 $icon->setAlternativeMarkup(self::MARKUP_IDENTIFIER_INLINE, $this->generateInlineMarkup($icon, $options));
38 }
39
40 /**
41 * @param Icon $icon
42 * @param array $options
43 * @return string
44 * @throws \InvalidArgumentException
45 */
46 protected function generateMarkup(Icon $icon, array $options)
47 {
48 if (empty($options['source'])) {
49 throw new \InvalidArgumentException('[' . $icon->getIdentifier() . '] The option "source" is required and must not be empty', 1460976566);
50 }
51
52 $source = $options['source'];
53
54 if (StringUtility::beginsWith($source, 'EXT:') || !StringUtility::beginsWith($source, '/')) {
55 $source = GeneralUtility::getFileAbsFileName($source);
56 }
57
58 $source = PathUtility::getAbsoluteWebPath($source);
59 return '<img src="' . htmlspecialchars($source) . '" width="' . $icon->getDimension()->getWidth() . '" height="' . $icon->getDimension()->getHeight() . '" />';
60 }
61
62 /**
63 * @param Icon $icon
64 * @param array $options
65 * @return string
66 * @throws \InvalidArgumentException
67 */
68 protected function generateInlineMarkup(Icon $icon, array $options)
69 {
70 if (empty($options['source'])) {
71 throw new \InvalidArgumentException('The option "source" is required and must not be empty', 1460976610);
72 }
73
74 $source = $options['source'];
75
76 if (StringUtility::beginsWith($source, 'EXT:') || !StringUtility::beginsWith($source, '/')) {
77 $source = GeneralUtility::getFileAbsFileName($source);
78 }
79
80 return $this->getInlineSvg($source);
81 }
82
83 /**
84 * @param string $source
85 *
86 * @return string
87 */
88 protected function getInlineSvg($source)
89 {
90 if (!file_exists($source)) {
91 return '';
92 }
93
94 $svgContent = file_get_contents($source);
95 $svgContent = preg_replace('/<script[\s\S]*?>[\s\S]*?<\/script>/i', '', $svgContent);
96 // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
97 $previousValueOfEntityLoader = libxml_disable_entity_loader(true);
98 $svgElement = simplexml_load_string($svgContent);
99 libxml_disable_entity_loader($previousValueOfEntityLoader);
100
101 // remove xml version tag
102 $domXml = dom_import_simplexml($svgElement);
103 return $domXml->ownerDocument->saveXML($domXml->ownerDocument->documentElement);
104 }
105 }