Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
XSLT.php
001 <?php
002
003 /**
004 * @package s9e\TextFormatter
005 * @copyright Copyright (c) 2010-2022 The s9e authors
006 * @license http://www.opensource.org/licenses/mit-license.php The MIT License
007 */
008 namespace s9e\TextFormatter\Renderers;
009
010 use s9e\TextFormatter\Renderer;
011 use XSLTProcessor;
012
013 class XSLT extends Renderer
014 {
015 /**
016 * @var XSLTProcessor The lazy-loaded XSLTProcessor instance used by this renderer
017 */
018 protected $proc;
019
020 /**
021 * @var bool Whether parameters need to be reloaded
022 */
023 protected $reloadParams = false;
024
025 /**
026 * @var string The stylesheet used by this renderer
027 */
028 protected $stylesheet;
029
030 /**
031 * Constructor
032 *
033 * @param string $stylesheet The stylesheet used to render intermediate representations
034 */
035 public function __construct($stylesheet)
036 {
037 $this->stylesheet = $stylesheet;
038
039 // Capture the parameters' values from the stylesheet
040 preg_match_all('#<xsl:param name="([^"]+)"(?>/>|>([^<]+))#', $stylesheet, $matches);
041 foreach ($matches[1] as $k => $paramName)
042 {
043 $this->params[$paramName] = htmlspecialchars_decode($matches[2][$k]);
044 }
045 }
046
047 /**
048 * Serializer
049 *
050 * @return string[] List of properties to serialize
051 */
052 public function __sleep()
053 {
054 $props = get_object_vars($this);
055 unset($props['proc']);
056
057 if (empty($props['reloadParams']))
058 {
059 unset($props['reloadParams']);
060 }
061
062 return array_keys($props);
063 }
064
065 /**
066 * Unserialize helper
067 *
068 * Will reload parameters if they were changed between generation and serialization
069 *
070 * @return void
071 */
072 public function __wakeup()
073 {
074 if (!empty($this->reloadParams))
075 {
076 $this->setParameters($this->params);
077 $this->reloadParams = false;
078 }
079 }
080
081 /**
082 * {@inheritdoc}
083 */
084 public function setParameter($paramName, $paramValue)
085 {
086 /**
087 * @link https://bugs.php.net/64137
088 */
089 if (strpos($paramValue, '"') !== false && strpos($paramValue, "'") !== false)
090 {
091 $paramValue = str_replace('"', "\xEF\xBC\x82", $paramValue);
092 }
093 else
094 {
095 $paramValue = (string) $paramValue;
096 }
097
098 if (!isset($this->params[$paramName]) || $this->params[$paramName] !== $paramValue)
099 {
100 $this->load();
101 $this->proc->setParameter('', $paramName, $paramValue);
102 $this->params[$paramName] = $paramValue;
103 $this->reloadParams = true;
104 }
105 }
106
107 /**
108 * {@inheritdoc}
109 */
110 protected function renderRichText($xml)
111 {
112 // Load the intermediate representation
113 $dom = $this->loadXML($xml);
114
115 // Load the stylesheet
116 $this->load();
117
118 // Perform the transformation and cast it as a string because it may return NULL if the
119 // transformation didn't output anything
120 $this->setLocale();
121 $output = (string) $this->proc->transformToXml($dom);
122 $this->restoreLocale();
123
124 // XSLTProcessor does not correctly identify <embed> as a void element. We fix it by
125 // removing </embed> end tags
126 $output = str_replace('</embed>', '', $output);
127
128 // Remove the \n that XSL adds at the end of the output, if applicable
129 if (substr($output, -1) === "\n")
130 {
131 $output = substr($output, 0, -1);
132 }
133
134 // Force HTML attributes to use double quotes to be consistent with the PHP renderer
135 if (strpos($output, "='") !== false)
136 {
137 $output = $this->normalizeAttributes($output);
138 }
139
140 return $output;
141 }
142
143 /**
144 * Create an XSLTProcessor and load the stylesheet
145 *
146 * @return void
147 */
148 protected function load()
149 {
150 if (!isset($this->proc))
151 {
152 $xsl = $this->loadXML($this->stylesheet);
153
154 $this->proc = new XSLTProcessor;
155 $this->proc->importStylesheet($xsl);
156 }
157 }
158
159 /**
160 * Normalize given attribute's value to use double quotes
161 *
162 * @param string[] $m
163 * @return string
164 */
165 protected function normalizeAttribute(array $m)
166 {
167 if ($m[0][0] === '"')
168 {
169 return $m[0];
170 }
171
172 return '"' . str_replace('"', '"', substr($m[0], 1, -1)) . '"';
173 }
174
175 /**
176 * Normalize all attributes in given HTML to use double quotes
177 *
178 * @param string $html
179 * @return string
180 */
181 protected function normalizeAttributes($html)
182 {
183 return preg_replace_callback('(<\\S++ [^>]++>)', [$this, 'normalizeElement'], $html);
184 }
185
186 /**
187 * Normalize attributes in given element to use double quotes
188 *
189 * @param string[] $m
190 * @return string
191 */
192 protected function normalizeElement(array $m)
193 {
194 if (strpos($m[0], "='") === false)
195 {
196 return $m[0];
197 }
198
199 return preg_replace_callback('((?:"[^"]*"|\'[^\']*\'))S', [$this, 'normalizeAttribute'], $m[0]);
200 }
201 }