‪TYPO3CMS  11.5
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 = $this->cObj->stdWrapValue('renderMode', $conf ?? []);
35 
36  if ($renderMode === 'inline') {
37  return $this->‪renderInline($conf);
38  }
39 
40  return $this->‪renderObject($conf);
41  }
42 
48  protected function ‪renderInline(array $conf): string
49  {
50  $src = $this->‪resolveAbsoluteSourcePath($conf);
51  [$width, $height, $isDefaultWidth, $isDefaultHeight] = $this->‪getDimensions($conf);
52 
53  $content = '';
54  if (file_exists($src)) {
55  $svgContent = (string)file_get_contents($src);
56  $svgContent = preg_replace('/<script[\s\S]*?>[\s\S]*?<\/script>/i', '', $svgContent) ?? '';
57  // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
58  $previousValueOfEntityLoader = null;
59  if (PHP_MAJOR_VERSION < 8) {
60  $previousValueOfEntityLoader = libxml_disable_entity_loader();
61  }
62  $svgElement = simplexml_load_string($svgContent);
63  if (PHP_MAJOR_VERSION < 8) {
64  libxml_disable_entity_loader($previousValueOfEntityLoader);
65  }
66 
67  $domXml = dom_import_simplexml($svgElement);
68  if (!$isDefaultWidth) {
69  $domXml->setAttribute('width', $width);
70  }
71  if (!$isDefaultHeight) {
72  $domXml->setAttribute('height', $height);
73  }
74  // remove xml version tag
75  $content = $domXml->ownerDocument->saveXML($domXml->ownerDocument->documentElement);
76  } else {
77  $value = $this->cObj->stdWrapValue('value', $conf ?? []);
78  if (!empty($value)) {
79  $content = [];
80  $content[] = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="' . (int)$width . '" height="' . (int)$height . '">';
81  $content[] = $value;
82  $content[] = '</svg>';
83  $content = implode(LF, $content);
84  }
85  }
86  if (isset($conf['stdWrap.'])) {
87  $content = $this->cObj->stdWrap($content, $conf['stdWrap.']);
88  }
89  return $content;
90  }
91 
98  protected function ‪renderObject(array $conf): string
99  {
100  $src = $this->‪resolveAbsoluteSourcePath($conf);
101  [$width, $height] = $this->‪getDimensions($conf);
102 
103  $src = $src === '' ? null : ‪PathUtility::getAbsoluteWebPath($src);
104 
105  $content = [];
106  if ($src) {
107  $content[] = '<!--[if IE]>';
108  $content[] = ' <object src="' . htmlspecialchars($src) . '" classid="image/svg+xml" width="' . (int)$width . '" height="' . (int)$height . '">';
109  $content[] = '<![endif]-->';
110  $content[] = '<!--[if !IE]>-->';
111  $content[] = ' <object data="' . htmlspecialchars($src) . '" type="image/svg+xml" width="' . (int)$width . '" height="' . (int)$height . '">';
112  $content[] = '<!--<![endif]-->';
113  $content[] = '</object>';
114  }
115  $content = implode(LF, $content);
116  if (isset($conf['stdWrap.'])) {
117  $content = $this->cObj->stdWrap($content, $conf['stdWrap.']);
118  }
119  return $content;
120  }
121 
127  protected function ‪resolveAbsoluteSourcePath(array $conf): string
128  {
129  $src = (string)$this->cObj->stdWrapValue('src', $conf ?? []);
130  return GeneralUtility::getFileAbsFileName($src);
131  }
132 
138  protected function ‪getDimensions(array $conf): array
139  {
140  $isDefaultWidth = false;
141  $isDefaultHeight = false;
142  $width = $this->cObj->stdWrapValue('width', $conf ?? []);
143  $height = $this->cObj->stdWrapValue('height', $conf ?? []);
144 
145  if (empty($width)) {
146  $isDefaultWidth = true;
147  $width = 600;
148  }
149  if (empty($height)) {
150  $isDefaultHeight = true;
151  $height = 400;
152  }
153 
154  return [$width, $height, $isDefaultWidth, $isDefaultHeight];
155  }
156 }
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:25
‪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:127
‪TYPO3\CMS\Frontend\ContentObject\ScalableVectorGraphicsContentObject
Definition: ScalableVectorGraphicsContentObject.php:25
‪TYPO3\CMS\Frontend\ContentObject\ScalableVectorGraphicsContentObject\renderObject
‪string renderObject(array $conf)
Definition: ScalableVectorGraphicsContentObject.php:98
‪TYPO3\CMS\Frontend\ContentObject\AbstractContentObject
Definition: AbstractContentObject.php:29
‪TYPO3\CMS\Frontend\ContentObject\ScalableVectorGraphicsContentObject\getDimensions
‪array getDimensions(array $conf)
Definition: ScalableVectorGraphicsContentObject.php:138
‪TYPO3\CMS\Core\Utility\PathUtility\getAbsoluteWebPath
‪static string getAbsoluteWebPath($targetPath, bool $prefixWithSitePath=true)
Definition: PathUtility.php:51
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Frontend\ContentObject\ScalableVectorGraphicsContentObject\renderInline
‪string renderInline(array $conf)
Definition: ScalableVectorGraphicsContentObject.php:48