‪TYPO3CMS  10.4
ScalableVectorGraphicsContentObject.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
20 
25 {
32  public function ‪render($conf = []): string
33  {
34  $renderMode = isset($conf['renderMode.'])
35  ? $this->cObj->stdWrap($conf['renderMode'], $conf['renderMode.'])
36  : $conf['renderMode'];
37 
38  if ($renderMode === 'inline') {
39  return $this->‪renderInline($conf);
40  }
41 
42  return $this->‪renderObject($conf);
43  }
44 
50  protected function ‪renderInline(array $conf): string
51  {
52  $src = $this->‪resolveAbsoluteSourcePath($conf);
53  [$width, $height, $isDefaultWidth, $isDefaultHeight] = $this->‪getDimensions($conf);
54 
55  $content = '';
56  if (file_exists($src)) {
57  $svgContent = (string)file_get_contents($src);
58  $svgContent = preg_replace('/<script[\s\S]*?>[\s\S]*?<\/script>/i', '', $svgContent) ?? '';
59  // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
60  $previousValueOfEntityLoader = null;
61  if (PHP_MAJOR_VERSION < 8) {
62  $previousValueOfEntityLoader = libxml_disable_entity_loader();
63  }
64  $svgElement = simplexml_load_string($svgContent);
65  if (PHP_MAJOR_VERSION < 8) {
66  libxml_disable_entity_loader($previousValueOfEntityLoader);
67  }
68 
69  $domXml = dom_import_simplexml($svgElement);
70  if (!$isDefaultWidth) {
71  $domXml->setAttribute('width', $width);
72  }
73  if (!$isDefaultHeight) {
74  $domXml->setAttribute('height', $height);
75  }
76  // remove xml version tag
77  $content = $domXml->ownerDocument->saveXML($domXml->ownerDocument->documentElement);
78  } else {
79  $value = isset($conf['value.']) ? $this->cObj->stdWrap($conf['value'], $conf['value.']) : $conf['value'];
80  if (!empty($value)) {
81  $content = [];
82  $content[] = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="' . (int)$width . '" height="' . (int)$height . '">';
83  $content[] = $value;
84  $content[] = '</svg>';
85  $content = implode(LF, $content);
86  }
87  }
88  if (isset($conf['stdWrap.'])) {
89  $content = $this->cObj->stdWrap($content, $conf['stdWrap.']);
90  }
91  return $content;
92  }
93 
100  protected function ‪renderObject(array $conf): string
101  {
102  $src = $this->‪resolveAbsoluteSourcePath($conf);
103  [$width, $height] = $this->‪getDimensions($conf);
104 
105  $src = $src === '' ? null : ‪PathUtility::getAbsoluteWebPath($src);
106 
107  $value = isset($conf['value.']) ? $this->cObj->stdWrap($conf['value'], $conf['value.']) : $conf['value'];
108  $noscript = isset($conf['noscript.']) ? $this->cObj->stdWrap($conf['noscript'], $conf['noscript.']) : $conf['noscript'];
109 
110  $content = [];
111  if ($src) {
112  $content[] = '<!--[if IE]>';
113  $content[] = ' <object src="' . htmlspecialchars($src) . '" classid="image/svg+xml" width="' . (int)$width . '" height="' . (int)$height . '">';
114  $content[] = '<![endif]-->';
115  $content[] = '<!--[if !IE]>-->';
116  $content[] = ' <object data="' . htmlspecialchars($src) . '" type="image/svg+xml" width="' . (int)$width . '" height="' . (int)$height . '">';
117  $content[] = '<!--<![endif]-->';
118  $content[] = $noscript;
119  $content[] = '</object>';
120  }
121  $content = implode(LF, $content);
122  if (isset($conf['stdWrap.'])) {
123  $content = $this->cObj->stdWrap($content, $conf['stdWrap.']);
124  }
125  return $content;
126  }
127 
133  protected function ‪resolveAbsoluteSourcePath(array $conf): string
134  {
135  $src = isset($conf['src.']) ? $this->cObj->stdWrap($conf['src'], $conf['src.']) : $conf['src'];
136  return GeneralUtility::getFileAbsFileName($src);
137  }
138 
144  protected function ‪getDimensions(array $conf): array
145  {
146  $isDefaultWidth = false;
147  $isDefaultHeight = false;
148  $width = isset($conf['width.']) ? $this->cObj->stdWrap($conf['width'], $conf['width.']) : $conf['width'];
149  $height = isset($conf['height.']) ? $this->cObj->stdWrap($conf['height'], $conf['height.']) : $conf['height'];
150 
151  if (empty($width)) {
152  $isDefaultWidth = true;
153  $width = 600;
154  }
155  if (empty($height)) {
156  $isDefaultHeight = true;
157  $height = 400;
158  }
159 
160  return [$width, $height, $isDefaultWidth, $isDefaultHeight];
161  }
162 }
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:24
‪TYPO3\CMS\Frontend\ContentObject
Definition: AbstractContentObject.php:16
‪TYPO3\CMS\Frontend\ContentObject\ScalableVectorGraphicsContentObject\render
‪string render($conf=[])
Definition: ScalableVectorGraphicsContentObject.php:32
‪TYPO3\CMS\Frontend\ContentObject\ScalableVectorGraphicsContentObject\resolveAbsoluteSourcePath
‪string resolveAbsoluteSourcePath(array $conf)
Definition: ScalableVectorGraphicsContentObject.php:133
‪TYPO3\CMS\Frontend\ContentObject\ScalableVectorGraphicsContentObject
Definition: ScalableVectorGraphicsContentObject.php:25
‪TYPO3\CMS\Frontend\ContentObject\ScalableVectorGraphicsContentObject\renderObject
‪string renderObject(array $conf)
Definition: ScalableVectorGraphicsContentObject.php:100
‪TYPO3\CMS\Frontend\ContentObject\AbstractContentObject
Definition: AbstractContentObject.php:25
‪TYPO3\CMS\Frontend\ContentObject\ScalableVectorGraphicsContentObject\getDimensions
‪array getDimensions(array $conf)
Definition: ScalableVectorGraphicsContentObject.php:144
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Utility\PathUtility\getAbsoluteWebPath
‪static string getAbsoluteWebPath($targetPath)
Definition: PathUtility.php:43
‪TYPO3\CMS\Frontend\ContentObject\ScalableVectorGraphicsContentObject\renderInline
‪string renderInline(array $conf)
Definition: ScalableVectorGraphicsContentObject.php:50