1<?php
2
3/*
4	[UCenter] (C)2001-2099 Comsenz Inc.
5	This is NOT a freeware, use is subject to license terms
6
7	$Id: xml.class.php 1059 2011-03-01 07:25:09Z monkey $
8*/
9
10function xml_unserialize(&$xml, $isnormal = FALSE) {
11	$xml_parser = new XML($isnormal);
12	$data = $xml_parser->parse($xml);
13	$xml_parser->destruct();
14	return $data;
15}
16
17function xml_serialize($arr, $htmlon = FALSE, $isnormal = FALSE, $level = 1) {
18	$s = $level == 1 ? "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n<root>\r\n" : '';
19	$space = str_repeat("\t", $level);
20	foreach($arr as $k => $v) {
21		if(!is_array($v)) {
22			$s .= $space."<item id=\"$k\">".($htmlon ? '<![CDATA[' : '').$v.($htmlon ? ']]>' : '')."</item>\r\n";
23		} else {
24			$s .= $space."<item id=\"$k\">\r\n".xml_serialize($v, $htmlon, $isnormal, $level + 1).$space."</item>\r\n";
25		}
26	}
27	$s = preg_replace("/([\x01-\x08\x0b-\x0c\x0e-\x1f])+/", ' ', $s);
28	return $level == 1 ? $s."</root>" : $s;
29}
30
31class XML {
32
33	var $parser;
34	var $document;
35	var $stack;
36	var $data;
37	var $last_opened_tag;
38	var $isnormal;
39	var $attrs = array();
40	var $failed = FALSE;
41
42	function __construct($isnormal) {
43		$this->XML($isnormal);
44	}
45
46	function XML($isnormal) {
47		$this->isnormal = $isnormal;
48		$this->parser = xml_parser_create('ISO-8859-1');
49		xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
50		xml_set_object($this->parser, $this);
51		xml_set_element_handler($this->parser, 'open','close');
52		xml_set_character_data_handler($this->parser, 'data');
53	}
54
55	function destruct() {
56		xml_parser_free($this->parser);
57	}
58
59	function parse(&$data) {
60		$this->document = array();
61		$this->stack	= array();
62		return xml_parse($this->parser, $data, true) && !$this->failed ? $this->document : '';
63	}
64
65	function open(&$parser, $tag, $attributes) {
66		$this->data = '';
67		$this->failed = FALSE;
68		if(!$this->isnormal) {
69			if(isset($attributes['id']) && !is_string($this->document[$attributes['id']])) {
70				$this->document  = &$this->document[$attributes['id']];
71			} else {
72				$this->failed = TRUE;
73			}
74		} else {
75			if(!isset($this->document[$tag]) || !is_string($this->document[$tag])) {
76				$this->document  = &$this->document[$tag];
77			} else {
78				$this->failed = TRUE;
79			}
80		}
81		$this->stack[] = &$this->document;
82		$this->last_opened_tag = $tag;
83		$this->attrs = $attributes;
84	}
85
86	function data(&$parser, $data) {
87		if($this->last_opened_tag != NULL) {
88			$this->data .= $data;
89		}
90	}
91
92	function close(&$parser, $tag) {
93		if($this->last_opened_tag == $tag) {
94			$this->document = $this->data;
95			$this->last_opened_tag = NULL;
96		}
97		array_pop($this->stack);
98		if($this->stack) {
99			$this->document = &$this->stack[count($this->stack)-1];
100		}
101	}
102
103}
104
105?>