Completed
Push — master ( 5c18a0...b18ba8 )
by Josh
16:03
created

TemplateLoader::replaceCDATA()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2019 The s9e Authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\TextFormatter\Configurator\Helpers;
9
10
use DOMDocument;
11
use DOMElement;
12
use DOMXPath;
13
use RuntimeException;
14
15
abstract class TemplateLoader
16
{
17
	/**
18
	* XSL namespace
19
	*/
20
	const XMLNS_XSL = 'http://www.w3.org/1999/XSL/Transform';
21
22
	/**
23
	* Get the XML content of an element
24
	*
25
	* @private
26
	*
27
	* @param  DOMElement $element
28
	* @return string
29
	*/
30 14
	public static function innerXML(DOMElement $element)
31
	{
32
		// Serialize the XML then remove the outer element
33 14
		$xml = $element->ownerDocument->saveXML($element);
34 14
		$pos = 1 + strpos($xml, '>');
35 14
		$len = strrpos($xml, '<') - $pos;
36
37
		// If the template is empty, return an empty string
38 14
		return ($len < 1) ? '' : substr($xml, $pos, $len);
39
	}
40
41
	/**
42
	* Load a template as an xsl:template node
43
	*
44
	* Will attempt to load it as XML first, then as HTML as a fallback. Either way, an xsl:template
45
	* node is returned
46
	*
47
	* @param  string      $template
48
	* @return DOMDocument
49
	*/
50 22
	public static function load($template)
51
	{
52 22
		$dom = self::loadAsXML($template) ?: self::loadAsXML(self::fixEntities($template));
53 22
		if ($dom)
54
		{
55 12
			return $dom;
56
		}
57
58
		// If the template contains an XSL element, abort now. Otherwise, try reparsing it as HTML
59 10
		if (strpos($template, '<xsl:') !== false)
60
		{
61 1
			$error = libxml_get_last_error();
62
63 1
			throw new RuntimeException('Invalid XSL: ' . $error->message);
64
		}
65
66 9
		return self::loadAsHTML($template);
67
	}
68
69
	/**
70
	* Serialize a loaded template back into a string
71
	*
72
	* NOTE: removes the root node created by load()
73
	*
74
	* @param  DOMDocument $dom
75
	* @return string
76
	*/
77 6
	public static function save(DOMDocument $dom)
78
	{
79 6
		$xml = self::innerXML($dom->documentElement);
80 6
		if (strpos($xml, 'xmlns:xsl') !== false)
81
		{
82
			$xml = preg_replace('((<[^>]+?) xmlns:xsl="' . self::XMLNS_XSL . '")', '$1', $xml);
83
		}
84
85 6
		return $xml;
86
	}
87
88
	/**
89
	* Replace HTML entities and unescaped ampersands in given template
90
	*
91
	* @param  string $template
92
	* @return string
93
	*/
94 13
	protected static function fixEntities($template)
95
	{
96 13
		return preg_replace_callback(
97 13
			'(&(?!quot;|amp;|apos;|lt;|gt;)\\w+;)',
98
			function ($m)
99
			{
100 2
				return html_entity_decode($m[0], ENT_NOQUOTES, 'UTF-8');
101 13
			},
102 13
			preg_replace('(&(?![A-Za-z0-9]+;|#\\d+;|#x[A-Fa-f0-9]+;))', '&amp;', $template)
103
		);
104
	}
105
106
	/**
107
	* Load given HTML template in a DOM document
108
	*
109
	* @param  string      $template Original template
110
	* @return DOMDocument
111
	*/
112 9
	protected static function loadAsHTML($template)
113
	{
114 9
		$template = self::replaceCDATA($template);
115
116 9
		$dom  = new DOMDocument;
117 9
		$html = '<?xml version="1.0" encoding="utf-8" ?><html><body><div>' . $template . '</div></body></html>';
118
119 9
		$useErrors = libxml_use_internal_errors(true);
120 9
		$dom->loadHTML($html, LIBXML_NSCLEAN);
121 9
		self::removeInvalidAttributes($dom);
122 9
		libxml_use_internal_errors($useErrors);
123
124
		// Now dump the thing as XML then reload it with the proper root element
125 9
		$xml = '<?xml version="1.0" encoding="utf-8" ?><xsl:template xmlns:xsl="' . self::XMLNS_XSL . '">' . self::innerXML($dom->documentElement->firstChild->firstChild) . '</xsl:template>';
126
127 9
		$useErrors = libxml_use_internal_errors(true);
128 9
		$dom->loadXML($xml, LIBXML_NSCLEAN);
129 9
		libxml_use_internal_errors($useErrors);
130
131 9
		return $dom;
132
	}
133
134
	/**
135
	* Load given XSL template in a DOM document
136
	*
137
	* @param  string           $template Original template
138
	* @return bool|DOMDocument           DOMDocument on success, FALSE otherwise
139
	*/
140 22
	protected static function loadAsXML($template)
141
	{
142 22
		$xml = '<?xml version="1.0" encoding="utf-8" ?><xsl:template xmlns:xsl="' . self::XMLNS_XSL . '">' . $template . '</xsl:template>';
143
144 22
		$useErrors = libxml_use_internal_errors(true);
145 22
		$dom       = new DOMDocument;
146 22
		$success   = $dom->loadXML($xml, LIBXML_NOCDATA | LIBXML_NSCLEAN);
147 22
		self::removeInvalidAttributes($dom);
148 22
		libxml_use_internal_errors($useErrors);
149
150 22
		return ($success) ? $dom : false;
151
	}
152
153
	/**
154
	* Remove attributes with an invalid name from given DOM document
155
	*
156
	* @param  DOMDocument $dom
157
	* @return void
158
	*/
159 22
	protected static function removeInvalidAttributes(DOMDocument $dom)
160
	{
161 22
		$xpath = new DOMXPath($dom);
162 22
		foreach ($xpath->query('//@*') as $attribute)
163
		{
164 10
			if (!preg_match('(^(?:[-\\w]+:)?(?!\\d)[-\\w]+$)D', $attribute->nodeName))
165
			{
166 3
				$attribute->parentNode->removeAttributeNode($attribute);
167
			}
168
		}
169 22
	}
170
171
	/**
172
	* Replace CDATA sections in given template
173
	*
174
	* @param  string $template Original template
175
	* @return string           Modified template
176
	*/
177 9
	protected static function replaceCDATA($template)
178
	{
179 9
		return preg_replace_callback(
180 9
			'(<!\\[CDATA\\[(.*?)\\]\\]>)',
181
			function ($m)
182
			{
183 1
				return htmlspecialchars($m[1]);
184 9
			},
185
			$template
186
		);
187
	}
188
}