File: xml.php

package info (click to toggle)
cacti 1.2.30%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 67,176 kB
  • sloc: php: 123,193; javascript: 29,825; sql: 2,595; xml: 1,823; sh: 1,228; perl: 194; makefile: 65; python: 51; ruby: 9
file content (198 lines) | stat: -rw-r--r-- 5,594 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
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
<?php
/*
 +-------------------------------------------------------------------------+
 | Copyright (C) 2004-2024 The Cacti Group                                 |
 |                                                                         |
 | 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.                            |
 +-------------------------------------------------------------------------+
 | Cacti: The Complete RRDtool-based Graphing Solution                     |
 +-------------------------------------------------------------------------+
 | This code is designed, written, and maintained by the Cacti Group. See  |
 | about.php and/or the AUTHORS file for specific developer information.   |
 +-------------------------------------------------------------------------+
 | http://www.cacti.net/                                                   |
 +-------------------------------------------------------------------------+
*/

function xml2array($data) {
	/* mvo voncken@mailandnews.com
	original ripped from  on the php-manual:gdemartini@bol.com.br
	to be used for data retrieval(result-structure is Data oriented) */
	$p = xml_parser_create();
	$vals = array();
	$index = array();
	xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
	xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
	xml_parse_into_struct($p, $data, $vals, $index);
	xml_parser_free($p);

	$tree = array();
	$i = 0;
	$tree = get_children($vals, $i);

	return $tree;
}

function get_children($vals, &$i) {
	$children = array();

	if (isset($vals[$i]['value'])) {
		if ($vals[$i]['value']) array_push($children, $vals[$i]['value']);
	}

	$prevtag = ''; $j = 0;

	while (++$i < cacti_count($vals)) {
		switch ($vals[$i]['type']) {
		case 'cdata':
			array_push($children, $vals[$i]['value']);
			break;
		case 'complete':
			/* if the value is an empty string, php doesn't include the 'value' key
			in its array, so we need to check for this first */
			if (isset($vals[$i]['value'])) {
				$children[$vals[$i]['tag']] = $vals[$i]['value'];
			} else {
				$children[$vals[$i]['tag']] = '';
			}

			break;
		case 'open':
			$j++;

			if ($prevtag <> $vals[$i]['tag']) {
				$j = 0;
				$prevtag = $vals[$i]['tag'];
			}

			$children[$vals[$i]['tag']] = get_children($vals,$i);
			break;
		case 'close':
			return $children;
		}
	}
}

function rrdxport2array($data) {
	// Bug force encoding to UTF-8
	$data = str_replace(array('US-ASCII', 'ISO-8859-1'), 'UTF-8', $data);

	/* bug #1436 */
	/* scan XML for bad data RRDtool 1.2.30 */
	$array = explode("\n", $data);

	if (cacti_sizeof($array)){
		if ((substr(trim($array[0]),0,1)) == '<') {
			/* continue */
		} else {
			$new_array = array();
			foreach($array as $element) {
				if ((substr(trim($element),0,1)) == '<') {
					$new_array[] = $element;
				}
			}

			$array = $new_array;

			$data = implode("\n", $array);
		}
	}

	/* mvo voncken@mailandnews.com
	original ripped from  on the php-manual:gdemartini@bol.com.br
	to be used for data retrieval(result-structure is Data oriented) */
	$p = xml_parser_create('UTF-8');
	$vals = array();
	$index = array();
	xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
	xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
	xml_parser_set_option($p, XML_OPTION_TARGET_ENCODING, 'UTF-8');
	xml_parse_into_struct($p, $data, $vals, $index);
	xml_parser_free($p);

	$tree = array();
	$i = 0;
	$column = 0;
	$row = 0;
	$tree = get_rrd_children($vals, $i, $column, $row);

	return $tree;
}

function get_rrd_children($vals, &$i, &$column, &$row) {
	$children = array();

	if (isset($vals[$i]['value'])) {
		if ($vals[$i]['value']) array_push($children, $vals[$i]['value']);
	}

	$prevtag = ''; $j = 0;

	while (++$i < cacti_count($vals)) {
		switch ($vals[$i]['type']) {
		case 'cdata':
			array_push($children, $vals[$i]['value']);
			break;
		case 'complete':
			/* if the value is an empty string, php doesn't include the 'value' key
			in its array, so we need to check for this first */
			if (isset($vals[$i]['value'])) {
				switch($vals[$i]['tag']) {
					case 'entry':
						$column++;
						$children['col' . $column] = $vals[$i]['value'];
						break;
					case 't':
						$children['timestamp'] = $vals[$i]['value'];
						break;
					case 'v':
						$column++;
						$children['col' . $column] = $vals[$i]['value'];
						break;
					default:
						$children[$vals[$i]['tag']] = $vals[$i]['value'];
				}
			} else {
				$children[$vals[$i]['tag']] = '';
			}

			break;
		case 'open':
			$j++;

			if ($prevtag <> $vals[$i]['tag']) {
				$j = 0;
				$prevtag = $vals[$i]['tag'];
			}

			switch($vals[$i]['tag']) {
				case 'meta':
				case 'xport':
				case 'legend':
					$children[$vals[$i]['tag']] = get_rrd_children($vals,$i,$column,$row);
					break;
				case 'data':
					break;
				case 'row':
					$row++;
					$column=0;
					$children['data'][$row] = get_rrd_children($vals,$i,$column,$row);

					break;
			}

			break;
		case 'close':
			return $children;
		}
	}
}