summaryrefslogtreecommitdiff
path: root/it_xml.class
blob: 7765054ca0af006e4ee1cb26c50adf4f259884a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php
/*
**	Copyright (C) 1995-2021 by the ITools Authors.
**	This file is part of ITools - the Internet Tools Library
**
**	ITools is free software; you can redistribute it and/or modify
**	it under the terms of the GNU General Public License as published by
**	the Free Software Foundation; either version 3 of the License, or
**	(at your option) any later version.
**
**	ITools is distributed in the hope that it will be useful,
**	but WITHOUT ANY WARRANTY; without even the implied warranty of
**	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**	GNU General Public License for more details.
**
**	You should have received a copy of the GNU General Public License
**	along with this program.  If not, see <http://www.gnu.org/licenses/>.
**
**	it_xml.class - XML parser / object factory
*/

#[AllowDynamicProperties]
class it_xml
{
/* 
 * Add xml snippet as attribute tree to $this
 * @param $xmldata XML string or filehandle (result from fopen) to parse
 * @param $p associative array
 * @param $p['forcearray'] xml tags to ALWAYS return as array
 * @param $p['safety'] OBSOLETE, see it_error. (0 = ignore errs, 1 = report errs, 2 = abort on errs)
 * @param $p['it_error'] Parameters for it_errors on parsing problems
 * @param $p['encoding'] Output character encoding (utf-8, iso-8859-1 or us-ascii, default: ini_get('default_charset')
 * @param $p['prefix'] Optional prefix for class names
 * @param $p['lowercase'] Lowercase all tag and attribute names
 * @return XML object tree or null on failure
 */

function __construct($xmldata = "", $p = array())
{
	if ($xmldata)
		$this->from_xml($xmldata, $p);
}

/**
 * Factory method to return XML object tree from XML string. Params like constructor
 * WARNING: Returned value will not contain the $p parameters in $this->_p so writing may cause problems
 * Example: $root = it_xml::create("<a><b id='2'/></a>", array("b"));
 */

static function create($xmldata, $p = array())
{
	$xml = new it_xml;
	return $xml->from_xml($xmldata, array('factory' => true) + $p) ? $xml->_root : null;
}

function from_xml($xmldata, $p)
{
	$p += ['encoding' => ini_get('default_charset'), 'it_error' => $p['safety'] === 0 ? false : ($p['safety'] == 2 ? ['fatal' => true] : [])];
	$this->_p = $p;
	$this->_arrayforce = array_flip((array)$this->_p['forcearray']);
	$this->_stack = array();
	unset($this->error);
	$parser = xml_parser_create();
	xml_set_object($parser, $this);
	xml_set_element_handler($parser, "start_element", "end_element");
	xml_set_character_data_handler($parser, "character_data");
	xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
	xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, $this->_p['encoding']);

	$result = true;

	if (is_resource($xmldata))
	{
		while ($result && !feof($xmldata))
		{
			$data = fread($xmldata, 1024 * 1024);
			while (!feof($xmldata) && preg_match('/[\x80-\xff]$/', $data))	# Make sure end of chunk is not in the middle of a UTF8 character
				$data .= fread($xmldata, 1);

			$xmlorig = $data;	# Do not append to keep memory footprint down!
			list($data, $isutf8) = $this->_sanitize($data, $isutf8);
			$result = xml_parse($parser, $data);
		}

		if ($result)
			$result = xml_parse($parser, "", true);
	}
	else
	{
		$xmlorig = $xmldata;
		list($xmldata, $isutf8) = $this->_sanitize($xmldata);
		$result = xml_parse($parser, $xmldata, true);
	}

	if (!$result)
		$this->error .= sprintf("it_xml error: %s at line %d", xml_error_string(xml_get_error_code($parser)), xml_get_current_line_number($parser));

	if ($this->error)
	{
		it::error((array)$p['it_error'] + ['title' => $this->error, 'body' => $xmlorig]);

		if ($this->_p['factory'])
			$GLOBALS['IT_XML_ERROR'] = $this->error;
	}

	unset($this->_arrayforce, $this->_p['safety'], $this->_p['it_error'], $this->_p['factory'], $this->_stack);
	xml_parser_free($parser);

	return empty($this->error);
}

# Use various heuristics to fix real-world XML files
function _sanitize($xmldata, $isutf8 = null)
{
	if (!isset($isutf8))	# Check if we already decided on charset yet
	{
		$xmldata = ltrim($xmldata);

		# Add header for charset detection (PHP5) if no header/BOM
		# See http://www.w3.org/TR/2006/REC-xml-20060816/#sec-guessing
		if (!preg_match('/^(<\?xml|\xEF\xBB\xBF|\xFE\xFF|\xFF\xFE|\x00\x00\xFE\xFF|\x00\x00\xFF\xFE)/', $xmldata))
			$xmldata = '<?xml version="1.0" encoding="' . $this->_p['encoding'] . '"?>' . $xmldata;

		$isutf8 = (!preg_match('/^<\?xml[^>]* encoding=/i', $xmldata) || preg_match('/^<\?xml[^>]* encoding=.utf-?8/i', $xmldata));
	}

	# Decode illegal entities but protect semantically important ones
	$xmldata = html_entity_decode(preg_replace('/&(amp|lt|gt|#38|#60|#62|#x26|#x3C|#x3E);/i', '&amp;$1;', $xmldata), ENT_NOQUOTES, $this->_p['encoding']);

	# If should be utf-8 and can't be decoded as such, fix it, even if mixed between both
	if ($isutf8 && preg_match('/[^\x80-\xff][\x80-\xff][^\x80-\xff]/', $xmldata))
		$xmldata = preg_replace_callback('/[\x80-\xff]{1,4}/', function($m) { return it_xml::_utf8_fix($m[0]); }, $xmldata);

	# If not utf-8, remove characters illegal for latin-1
	if (!$isutf8 && preg_match('/[\x00-\x08\x0b-\x0c\x0e-\x1f\x80-\x9f]/', $xmldata))
		$xmldata = it_html::_cleanup($xmldata, $isutf8 ? "utf-8" : "iso-8859-1");

	return array($xmldata, $isutf8);
}

# Encode non-utf8 characters in a string, leave utf8 alone
static function _utf8_fix($str)
{
	return preg_match('/^([\xc0-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf][\x80-\xbf]|[\xf0-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf])$/', $str) ? $str : it::utf8_encode($str);
}

function consume(/* $p */)
{
	return false;
}

/**
 * Utility function which recursively flattens container into array
 * @param with_attr if true, include attributes in result array
 * @return Array with key/values-pairs for vals and optionally attrs of sub-objects
 */
function flatten($with_attr = false)
{
	$result = $with_attr ? (array)$this->attr : array();

	foreach ($this->elements() as $key => $values)
	{
		if (is_array($values))
		{
			$result[$key] = array();

			foreach ($values as $value)
				$result[$key][] = isset($value->val) || !get_object_vars($value) ? $value->val : $value->flatten($with_attr);
		}
		else
			$result[$key] = isset($values->val) || !get_object_vars($values) ? $values->val : $values->flatten($with_attr);	# don' take recursion if val is set or object is empty
	}

	return $result;
}

# Strip namespace and convert extra characters to _
function _make_identifier($name)
{
	$id = preg_replace(array('/^.*:/', '/\W/'), array('', '_'), $name);
	return $this->_p['lowercase'] ? strtolower($id) : $id;
}

function start_element($dummy_parser, $name, $attrs)
{
	$name = $this->_p['prefix'] . $this->_make_identifier($name);

	if (!class_exists($name))
		eval("class $name extends it_xml {}");	# Extending the base class caused problems with tel_xmlentry

	if (!$this->_stack && !$this->_p['factory'])
		array_unshift($this->_stack, $this);
	else
		array_unshift($this->_stack, new $name);

	if (!is_a($this->_stack[0], "it_xml"))
		$this->error .= "Class $name used in it_xml for tag $name does not extend it_xml: Name clash?\n";

	if ($attrs)
	{
		foreach ($attrs as $key => $val)
			$this->_stack[0]->attr[$this->_make_identifier($key)] = $val;
	}
}

function end_element($dummy_parser, $name)
{
	$name = $this->_make_identifier($name);

	if (!$this->_stack[0]->consume($this->_p))
	{
		if (!isset($this->_stack[1]))
			$this->_stack[1] = (object)[];

		if (is_array($this->_stack[1]->$name))
			array_push($this->_stack[1]->$name, $this->_stack[0]);
		else if (isset($this->_stack[1]->$name))
		{
			if (is_a($this->_stack[1]->$name, "it_xml"))
				$this->_stack[1]->$name = array($this->_stack[1]->$name, &$this->_stack[0]);
			else
				$this->error .= "Variable $name used in it_xml for tag {$name} is already used in object: Name clash?\n";
		}
		else if (isset($this->_arrayforce[$name]))
			$this->_stack[1]->$name = array(&$this->_stack[0]);
		else
			$this->_stack[1]->$name =& $this->_stack[0];
	}

	$node = array_shift($this->_stack);

	if ($this->_p['factory'])
		$this->_root = $node;
}

function character_data($dummy_parser, $cdata)
{
	if (isset($this->_stack[0]->val) || (trim($cdata) !== ""))
		$this->_stack[0]->val .= $cdata;
}

/**
 * Convert XML object tree back to string representation
 * @param $p['tag'] Name of root XML tag (otherwise determined by get_class)
 * @param $p['indent'] String to indent with (default: "  ")
 * @param $p['lineseparator'] String to start new line with (default: "\n")
 * @param $p['prefix'] String to prefix class names with (default: "")
 * @param $p['sort'] value true causes to_xml() to output elements sorted
 * @return XML string representation of this object (only attributes with is_a == it_xml and no leading _)
 */
function to_xml($p = array())
{
	$p += array(
		'tag' => substr(get_class($this), strlen($p['prefix'])),
		'indent' => "  ",
		'lineseparator' => "\n",
		'prefix' => "",
		'sort' => false,
		'stripempty' => false, # remove xml tags without content
	);

	$currenttag = $p['tag'];
	$tag = $p['nextindentation'] . "<$currenttag";

	if (empty($p['nextindentation']))
		$p['nextindentation'] = $p['lineseparator'];

	$currentindentation = $p['nextindentation'];
	$p['nextindentation'] .= $p['indent'];

	foreach ((array)$this->attr as $key => $value)
		$tag .= " $key=\"" . htmlspecialchars($value, ENT_COMPAT, ini_get('default_charset')) . '"';

	$vars = get_object_vars($this);

	if ($p['sort'])
		ksort($vars);

	foreach ($vars as $key => $values)
	{
		$p['tag'] = $key;	# Manually set as PHP4 lowercases get_class()

		if (!preg_match('/^(_|attr$)/', $key))	# Skip implementation detail fields
		{
			if (is_array($values))
			{
				foreach ($values as $value)
				{
					if (is_a($value, "it_xml"))
					{
						$body .= $value->to_xml($p);
						$indentation = $currentindentation;
					}
				}
			}
			else if (is_object($values))
			{
				if (is_a($values, "it_xml"))
				{
					$body .= $values->to_xml($p);
					$indentation = $currentindentation;
				}
			}
			else if ($key == 'val')
				$body .= htmlspecialchars($values, ENT_COMPAT, ini_get('default_charset'));
		}
	}

	return isset($body) ? "$tag>$body$indentation</$currenttag>" : ($p['stripempty'] ? "" : "$tag/>");
}

/**
 * Get array containing child elements (key value pairs)
 * @return Array containing child elements
 */
function elements()
{
	$result = array();

	foreach (get_object_vars($this) as $key => $value)
	{
		if (is_a($value, "it_xml") || (is_array($value) && is_a($value[0], "it_xml")))
			$result[$key] =& $this->$key;
	}

	return $result;
}

function error()
{
	return is_a($this, "it_xml") ? $this->error : $GLOBALS['IT_XML_ERROR'];
}

/**
 * Set nodes as subnode of current node
 * @param values assoc array of key => value pairs. May contain associative or numeric subarrays. 
 */

function set($vals, $parentprefix = null)
{
	$prefix = $parentprefix ? $parentprefix : $this->_p['prefix'];

	if (is_array($vals))
	{
		foreach ($vals as $key => $val)
		{
			$classname = $prefix . $this->_make_identifier($key);
			if (!class_exists($classname))
				eval("class $classname extends it_xml {}");

			if (is_array($val) && $val === array_values($val))
			{
				$arr = (array)$this->$key;
				foreach ($val as $i => $v)
				{
					if (!is_a($arr[$i], "it_xml"))
						$arr[$i] = new $classname;
					$arr[$i]->set($v, $prefix);
				}
				$this->$key = $arr;
			}
			else
			{
				if (!is_a($this->$key, "it_xml"))
					$this->$key = new $classname;
				$this->$key->set($val, $prefix);
			}
		}
	}
	else
		$this->val = $vals;
}

function __toString()
{
	return (string)$this->val;
}

} 

?>