Get rid of old vCalendar/vComponent code. Add commented alternative for XML escaping.
[awl.git] / inc / XMLElement.php
blob83269458ccd62a8e1def88d875a0ec41a412f127
1 <?php
2 /**
3 * A class to assist with construction of XML documents
5 * @package awl
6 * @subpackage XMLElement
7 * @author Andrew McMillan <andrew@mcmillan.net.nz>
8 * @copyright Catalyst .Net Ltd, Morphoss Ltd <http://www.morphoss.com/>
9 * @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU LGPL version 3 or later
12 require_once('AWLUtilities.php');
14 /**
15 * A class for XML elements which may have attributes, or contain
16 * other XML sub-elements
18 * @package awl
20 class XMLElement {
21 protected $tagname;
22 protected $xmlns;
23 protected $attributes;
24 protected $content;
25 protected $_parent;
27 /**
28 * Constructor - nothing fancy as yet.
30 * @param string $tagname The tag name of the new element
31 * @param mixed $content Either a string of content, or an array of sub-elements
32 * @param array $attributes An array of attribute name/value pairs
33 * @param string $xmlns An XML namespace specifier
35 function __construct( $tagname, $content=false, $attributes=false, $xmlns=null ) {
36 $this->tagname=$tagname;
37 if ( gettype($content) == "object" ) {
38 // Subtree to be parented here
39 $this->content = array(&$content);
41 else {
42 // Array or text
43 $this->content = $content;
45 $this->attributes = $attributes;
46 if ( isset($xmlns) ) {
47 $this->xmlns = $xmlns;
49 else {
50 if ( preg_match( '{^(.*):([^:]*)$}', $tagname, $matches) ) {
51 $prefix = $matches[1];
52 $tag = $matches[2];
53 if ( isset($this->attributes['xmlns:'.$prefix]) ) {
54 $this->xmlns = $this->attributes['xmlns:'.$prefix];
57 else if ( isset($this->attributes['xmlns']) ) {
58 $this->xmlns = $this->attributes['xmlns'];
64 /**
65 * Count the number of elements
66 * @return int The number of elements
68 function CountElements( ) {
69 if ( $this->content === false ) return 0;
70 if ( is_array($this->content) ) return count($this->content);
71 if ( $this->content == '' ) return 0;
72 return 1;
75 /**
76 * Set an element attribute to a value
78 * @param string The attribute name
79 * @param string The attribute value
81 function SetAttribute($k,$v) {
82 if ( gettype($this->attributes) != "array" ) $this->attributes = array();
83 $this->attributes[$k] = $v;
84 if ( strtolower($k) == 'xmlns' ) {
85 $this->xmlns = $v;
89 /**
90 * Set the whole content to a value
92 * @param mixed The element content, which may be text, or an array of sub-elements
94 function SetContent($v) {
95 $this->content = $v;
98 /**
99 * Accessor for the tag name
101 * @return string The tag name of the element
103 function GetTag() {
104 return $this->tagname;
108 * Accessor for the full-namespaced tag name
110 * @return string The tag name of the element, prefixed by the namespace
112 function GetNSTag() {
113 return (empty($this->xmlns) ? '' : $this->xmlns . ':') . $this->tagname;
117 * Accessor for a single attribute
118 * @param string $attr The name of the attribute.
119 * @return string The value of that attribute of the element
121 function GetAttribute( $attr ) {
122 if ( $attr == 'xmlns' ) return $this->xmlns;
123 if ( isset($this->attributes[$attr]) ) return $this->attributes[$attr];
124 return null;
128 * Accessor for the attributes
130 * @return array The attributes of this element
132 function GetAttributes() {
133 return $this->attributes;
137 * Accessor for the content
139 * @return array The content of this element
141 function GetContent() {
142 return $this->content;
146 * Return an array of elements matching the specified tag, or all elements if no tag is supplied.
147 * Unlike GetContent() this will always return an array.
149 * @return array The XMLElements within the tree which match this tag
151 function GetElements( $tag=null, $recursive=false ) {
152 $elements = array();
153 if ( gettype($this->content) == "array" ) {
154 foreach( $this->content AS $k => $v ) {
155 if ( empty($tag) || $v->GetNSTag() == $tag ) {
156 $elements[] = $v;
158 if ( $recursive ) {
159 $elements = $elements + $v->GetElements($tag,true);
163 else if ( empty($tag) || (isset($v->content->tagname) && $v->content->GetNSTag() == $tag) ) {
164 $elements[] = $this->content;
166 return $elements;
171 * Return an array of elements matching the specified path
173 * @return array The XMLElements within the tree which match this tag
175 function GetPath( $path ) {
176 $elements = array();
177 // printf( "Querying within '%s' for path '%s'\n", $this->tagname, $path );
178 if ( !preg_match( '#(/)?([^/]+)(/?.*)$#', $path, $matches ) ) return $elements;
179 // printf( "Matches: %s -- %s -- %s\n", $matches[1], $matches[2], $matches[3] );
180 if ( $matches[2] == '*' || $matches[2] == $this->GetNSTag()) {
181 if ( $matches[3] == '' ) {
183 * That is the full path
185 $elements[] = $this;
187 else if ( gettype($this->content) == "array" ) {
189 * There is more to the path, so we recurse into that sub-part
191 foreach( $this->content AS $k => $v ) {
192 $elements = array_merge( $elements, $v->GetPath($matches[3]) );
197 if ( $matches[1] != '/' && gettype($this->content) == "array" ) {
199 * If our input $path was not rooted, we recurse further
201 foreach( $this->content AS $k => $v ) {
202 $elements = array_merge( $elements, $v->GetPath($path) );
205 // printf( "Found %d within '%s' for path '%s'\n", count($elements), $this->tagname, $path );
206 return $elements;
211 * Add a sub-element
213 * @param object An XMLElement to be appended to the array of sub-elements
215 function AddSubTag(&$v) {
216 if ( gettype($this->content) != "array" ) $this->content = array();
217 $this->content[] =& $v;
218 return count($this->content);
222 * Add a new sub-element
224 * @param string The tag name of the new element
225 * @param mixed Either a string of content, or an array of sub-elements
226 * @param array An array of attribute name/value pairs
228 * @return objectref A reference to the new XMLElement
230 function &NewElement( $tagname, $content=false, $attributes=false, $xmlns=null ) {
231 if ( gettype($this->content) != "array" ) $this->content = array();
232 $element = new XMLElement($tagname,$content,$attributes,$xmlns);
233 $this->content[] =& $element;
234 return $element;
239 * Render just the internal content
241 * @return string The content of this element, as a string without this element wrapping it.
243 function RenderContent($indent=0, $nslist=null, $force_xmlns=false ) {
244 $r = "";
245 if ( is_array($this->content) ) {
247 * Render the sub-elements with a deeper indent level
249 $r .= "\n";
250 foreach( $this->content AS $k => $v ) {
251 if ( is_object($v) ) {
252 $r .= $v->Render($indent+1, "", $nslist, $force_xmlns);
255 $r .= substr(" ",0,$indent);
257 else {
259 * Render the content, with special characters escaped
262 if(strpos($this->content, '<![CDATA[')===0 && strrpos($this->content, ']]>')===strlen($this->content)-3)
263 $r .= '<![CDATA[' . str_replace(']]>', ']]]]><![CDATA[>', substr($this->content, 9, -3)) . ']]>';
264 // else if ( preg_match('{^[\u0009\u000A\u000D\u0020-\uD7FF\uE000-\uFFFD\u10000-\u10FFFF]+$}', $this->content) )
265 // $r .= '<![CDATA[' . $this->content . ']]>';
266 else
267 $r .= htmlspecialchars($this->content, ENT_NOQUOTES | ENT_XML1 | ENT_DISALLOWED );
269 return $r;
274 * Render the document tree into (nicely formatted) XML
276 * @param int The indenting level for the pretty formatting of the element
278 function Render($indent=0, $xmldef="", $nslist=null, $force_xmlns=false) {
279 $r = ( $xmldef == "" ? "" : $xmldef."\n");
281 $attr = "";
282 $tagname = $this->tagname;
283 $xmlns_done = false;
284 if ( gettype($this->attributes) == "array" ) {
286 * Render the element attribute values
288 foreach( $this->attributes AS $k => $v ) {
289 if ( preg_match('#^xmlns(:?(.+))?$#', $k, $matches ) ) {
290 // if ( $force_xmlns ) printf( "1: %s: %s\n", $this->tagname, $this->xmlns );
291 if ( !isset($nslist) ) $nslist = array();
292 $prefix = (isset($matches[2]) ? $matches[2] : '');
293 if ( isset($nslist[$v]) && $nslist[$v] == $prefix ) continue; // No need to include in list as it's in a wrapping element
294 $nslist[$v] = $prefix;
295 if ( !isset($this->xmlns) ) $this->xmlns = $v;
296 $xmlns_done = true;
298 $attr .= sprintf( ' %s="%s"', $k, htmlspecialchars($v) );
301 if ( isset($this->xmlns) && isset($nslist[$this->xmlns]) && $nslist[$this->xmlns] != '' ) {
302 // if ( $force_xmlns ) printf( "2: %s: %s\n", $this->tagname, $this->xmlns );
303 $tagname = $nslist[$this->xmlns] . ':' . $tagname;
304 if ( $force_xmlns ) $attr .= sprintf( ' xmlns="%s"', $this->xmlns);
306 else if ( isset($this->xmlns) && !isset($nslist[$this->xmlns]) && gettype($this->attributes) == 'array' && !isset($this->attributes[$this->xmlns]) ) {
307 // if ( $force_xmlns ) printf( "3: %s: %s\n", $this->tagname, $this->xmlns );
308 $attr .= sprintf( ' xmlns="%s"', $this->xmlns);
310 else if ( $force_xmlns && isset($this->xmlns) && ! $xmlns_done ) {
311 // printf( "4: %s: %s\n", $this->tagname, $this->xmlns );
312 $attr .= sprintf( ' xmlns="%s"', $this->xmlns);
315 $r .= substr(" ",0,$indent) . '<' . $tagname . $attr;
317 if ( (is_array($this->content) && count($this->content) > 0) || (!is_array($this->content) && strlen($this->content) > 0) ) {
318 $r .= ">";
319 $r .= $this->RenderContent($indent,$nslist,$force_xmlns);
320 $r .= '</' . $tagname.">\n";
322 else {
323 $r .= "/>\n";
325 return $r;
329 function __tostring() {
330 return $this->Render();
336 * Rebuild an XML tree in our own style from the parsed XML tags using
337 * a tail-recursive approach.
339 * @param array $xmltags An array of XML tags we get from using the PHP XML parser
340 * @param intref &$start_from A pointer to our current integer offset into $xmltags
341 * @return mixed Either a single XMLElement, or an array of XMLElement objects.
343 function BuildXMLTree( $xmltags, &$start_from ) {
344 $content = array();
346 if ( !isset($start_from) ) $start_from = 0;
348 for( $i=0; $i < 50000 && isset($xmltags[$start_from]); $i++) {
349 $tagdata = $xmltags[$start_from++];
350 if ( !isset($tagdata) || !isset($tagdata['tag']) || !isset($tagdata['type']) ) break;
351 if ( $tagdata['type'] == "close" ) break;
352 $xmlns = null;
353 $tag = $tagdata['tag'];
354 if ( preg_match( '{^(.*):([^:]*)$}', $tag, $matches) ) {
355 $xmlns = $matches[1];
356 $tag = $matches[2];
358 $attributes = ( isset($tagdata['attributes']) ? $tagdata['attributes'] : false );
359 if ( $tagdata['type'] == "open" ) {
360 $subtree = BuildXMLTree( $xmltags, $start_from );
361 $content[] = new XMLElement($tag, $subtree, $attributes, $xmlns );
363 else if ( $tagdata['type'] == "complete" ) {
364 $value = ( isset($tagdata['value']) ? $tagdata['value'] : false );
365 $content[] = new XMLElement($tag, $value, $attributes, $xmlns );
370 * If there is only one element, return it directly, otherwise return the
371 * array of them
373 if ( count($content) == 1 ) {
374 return $content[0];
376 return $content;