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
|
<?php
/**
* Classes and functions for manipulating XML templates.
*
* @author The phpLDAPadmin development team
* @package phpLDAPadmin
*/
/**
* XML Parser
*
* This will read our XML file and convert it into variables for us to parse.
*
* @package phpLDAPadmin
* @subpackage XML
*/
class xml2array {
var $stack = array();
var $stack_ref;
var $arrOutput = array();
var $resParser;
var $strXmlData;
private function push_pos(&$pos) {
$this->stack[count($this->stack)] = &$pos;
$this->stack_ref = &$pos;
}
private function pop_pos() {
unset($this->stack[count($this->stack) - 1]);
$this->stack_ref = &$this->stack[count($this->stack) - 1];
}
public function parseXML($strInputXML,$filename) {
$this->resParser = xml_parser_create();
xml_set_element_handler($this->resParser, [$this, 'tagOpen'], [$this, 'tagClosed']);
xml_set_character_data_handler($this->resParser, [$this, 'tagData']);
$this->push_pos($this->arrOutput);
$this->strXmlData = xml_parse($this->resParser,$strInputXML);
if (! $this->strXmlData)
die(sprintf('XML error: %s at line %d in file %s',
xml_error_string(xml_get_error_code($this->resParser)),
xml_get_current_line_number($this->resParser),
$filename));
xml_parser_free($this->resParser);
$output = array();
foreach ($this->arrOutput as $key => $values)
$output[$key] = $this->cleanXML($values);
#return $this->arrOutput;
return $output;
}
private function tagOpen($parser,$name,$attrs) {
$name = strtolower($name);
if (isset($this->stack_ref[$name])) {
if (! isset($this->stack_ref[$name][0])) {
$tmp = $this->stack_ref[$name];
unset($this->stack_ref[$name]);
$this->stack_ref[$name][0] = $tmp;
}
$cnt = count($this->stack_ref[$name]);
$this->stack_ref[$name][$cnt] = array();
if (isset($attrs))
$this->stack_ref[$name][$cnt] = $attrs;
$this->push_pos($this->stack_ref[$name][$cnt]);
} else {
$this->stack_ref[$name]=array();
if (isset($attrs))
$this->stack_ref[$name]=$attrs;
$this->push_pos($this->stack_ref[$name]);
}
}
private function tagData($parser,$tagData) {
if (trim($tagData) != '') {
if (isset($this->stack_ref['#text']))
$this->stack_ref['#text'] .= $tagData;
else
$this->stack_ref['#text'] = $tagData;
}
}
private function tagClosed($parser,$name) {
$this->pop_pos();
}
/**
* This function will parse an XML array and make a normal array.
*
* @return array - Clean XML data
*/
private function cleanXML($details) {
# Quick processing for the final branch of the XML array.
if (is_array($details) && isset($details['#text']))
return $details['#text'];
elseif (is_array($details) && isset($details['ID']) && count($details) == 1)
return $details['ID'];
$cleanXML = array();
# Quick processing for the final branch, when it holds the ID and values.
if (is_array($details) && isset($details['ID']) && count($details) > 1) {
$key = $details['ID'];
unset($details['ID']);
$cleanXML[$key] = $this->cleanXML($details);
$details = array();
}
# More detailed processing...
if (is_array($details))
foreach ($details as $key => $values)
if (is_numeric($key) && isset($values['ID']) && count($values) > 1) {
$key = $values['ID'];
unset($values['ID']);
$cleanXML[$key] = $this->cleanXML($values);
} elseif (isset($values['#text']))
$cleanXML[$key] = $this->cleanXML($values);
elseif (is_array($values))
$cleanXML[$key] = $this->cleanXML($values);
if (! $cleanXML)
return $details;
else
return $cleanXML;
}
}
|