File: CXmlImportReader.php

package info (click to toggle)
zabbix 1%3A4.0.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 83,104 kB
  • sloc: php: 162,280; ansic: 154,778; sql: 96,479; sh: 5,281; makefile: 1,340; java: 1,068; cpp: 227; perl: 41; xml: 29
file content (155 lines) | stat: -rw-r--r-- 4,223 bytes parent folder | download
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
<?php
/*
** Zabbix
** Copyright (C) 2001-2019 Zabbix SIA
**
** This program 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 2 of the License, or
** (at your option) any later version.
**
** This program 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, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**/


class CXmlImportReader extends CImportReader {

	/**
	 * Convert string with xml data to php array.
	 *
	 * @throws Exception
	 *
	 * @param string $string
	 *
	 * @return array
	 */
	public function read($string) {
		if ($string === '') {
			throw new Exception(_s('Cannot read XML: %1$s.', _('XML is empty')));
		}

		libxml_use_internal_errors(true);
		libxml_disable_entity_loader(true);
		$result = simplexml_load_string($string, null, LIBXML_IMPORT_FLAGS);
		if (!$result) {
			$errors = libxml_get_errors();
			libxml_clear_errors();

			foreach ($errors as $error) {
				throw new Exception(_s('Cannot read XML: %1$s.', _s('%1$s [Line: %2$s | Column: %3$s]',
					'('.$error->code.') '.trim($error->message), $error->line, $error->column
				)));
			}
		}

		$xml = new XMLReader();
		$xml->xml($string);
		$data = $this->xml_to_array($xml);
		$xml->close();
		return $data;
	}

	/**
	 * Method for recursive processing of xml dom nodes.
	 *
	 * @param XMLReader $xml
	 * @param string    $path
	 *
	 * @return array|string
	 */
	protected function xml_to_array(XMLReader $xml, $path = '') {
		$data = null;

		while ($xml->read()) {
			switch ($xml->nodeType) {
				case XMLReader::ELEMENT:
					if ($data === null) {
						$data = [];
					}
					elseif (!is_array($data)) {
						throw new Exception(_s('Invalid tag "%1$s": %2$s.', $path,
							_s('unexpected text "%1$s"', trim($data))
						));
					}

					$node_name = $xml->name;
					$sub_path = $path.'/'.$node_name;
					if (array_key_exists($node_name, $data)) {
						$node_name .= count($data);
						$sub_path .= '('.count($data).')';
					}

					/*
					 * A special case for 1.8 import where attributes are still used attributes must be added to the
					 * array as if they where child elements.
					 */
					if ($xml->hasAttributes) {
						while ($xml->moveToNextAttribute()) {
							$data[$node_name][$xml->name] = $xml->value;
						}

						// it make $this->isEmptyElement valid after processing attributes
						$xml->moveToElement();

						/*
						 * We assume that an element with attributes always contains child elements, not a text node
						 * works for 1.8 XML.
						 */
						if (!$xml->isEmptyElement) {
							$child_data = $this->xml_to_array($xml, $sub_path);
							if (is_array($child_data)) {
								foreach ($child_data as $child_node_name => $child_node_value) {
									if (array_key_exists($child_node_name, $data[$node_name])) {
										$child_node_name .= count($data[$node_name]);
									}
									$data[$node_name][$child_node_name] = $child_node_value;
								}
							}
							elseif ($child_data !== '') {
								throw new Exception(_s('Invalid tag "%1$s": %2$s.', $sub_path,
									_s('unexpected text "%1$s"', trim($child_data))
								));
							}
						}
					}
					else {
						$data[$node_name] = $xml->isEmptyElement ? '' : $this->xml_to_array($xml, $sub_path);
					}
					break;

				case XMLReader::CDATA:
					// falls through
				case XMLReader::TEXT:
					if ($data === null) {
						$data = $xml->value;
					}
					elseif (is_array($data)) {
						throw new Exception(_s('Invalid tag "%1$s": %2$s.', $path,
							_s('unexpected text "%1$s"', trim($xml->value))
						));
					}

					break;

				case XMLReader::END_ELEMENT:
					/*
					 * For tags with empty value: <dns></dns>.
					 */
					if ($data === null) {
						$data = '';
					}

					return $data;
			}
		}

		return $data;
	}
}