2019-02-08 13:44:52 -05:00
|
|
|
<?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 {
|
2019-02-12 14:52:01 -05:00
|
|
|
public $stack = array();
|
|
|
|
public $stack_ref;
|
|
|
|
public $arrOutput = array();
|
|
|
|
public $resParser;
|
|
|
|
public $strXmlData;
|
2019-02-08 13:44:52 -05:00
|
|
|
|
|
|
|
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_object($this->resParser,$this);
|
|
|
|
xml_set_element_handler($this->resParser,'tagOpen','tagClosed');
|
|
|
|
|
|
|
|
xml_set_character_data_handler($this->resParser,'tagData');
|
|
|
|
|
|
|
|
$this->push_pos($this->arrOutput);
|
|
|
|
|
|
|
|
$this->strXmlData = xml_parse($this->resParser,$strInputXML);
|
|
|
|
|
|
|
|
if (! $this->strXmlData)
|
2019-02-12 14:57:50 -05:00
|
|
|
{
|
2019-02-08 13:44:52 -05:00
|
|
|
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));
|
2019-02-12 14:57:50 -05:00
|
|
|
}
|
2019-02-08 13:44:52 -05:00
|
|
|
|
|
|
|
xml_parser_free($this->resParser);
|
|
|
|
|
|
|
|
$output = array();
|
|
|
|
foreach ($this->arrOutput as $key => $values)
|
2019-02-12 14:57:50 -05:00
|
|
|
{
|
2019-02-08 13:44:52 -05:00
|
|
|
$output[$key] = $this->cleanXML($values);
|
2019-02-12 14:57:50 -05:00
|
|
|
}
|
2019-02-08 13:44:52 -05:00
|
|
|
|
|
|
|
#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))
|
2019-02-12 14:57:50 -05:00
|
|
|
{
|
2019-02-08 13:44:52 -05:00
|
|
|
$this->stack_ref[$name][$cnt] = $attrs;
|
2019-02-12 14:57:50 -05:00
|
|
|
}
|
2019-02-08 13:44:52 -05:00
|
|
|
|
|
|
|
$this->push_pos($this->stack_ref[$name][$cnt]);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$this->stack_ref[$name]=array();
|
|
|
|
|
|
|
|
if (isset($attrs))
|
2019-02-12 14:57:50 -05:00
|
|
|
{
|
|
|
|
$this->stack_ref[$name] = $attrs;
|
|
|
|
}
|
2019-02-08 13:44:52 -05:00
|
|
|
|
|
|
|
$this->push_pos($this->stack_ref[$name]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function tagData($parser,$tagData) {
|
|
|
|
if (trim($tagData) != '') {
|
|
|
|
|
|
|
|
if (isset($this->stack_ref['#text']))
|
2019-02-12 14:57:50 -05:00
|
|
|
{
|
2019-02-08 13:44:52 -05:00
|
|
|
$this->stack_ref['#text'] .= $tagData;
|
2019-02-12 14:57:50 -05:00
|
|
|
}
|
2019-02-08 13:44:52 -05:00
|
|
|
else
|
2019-02-12 14:57:50 -05:00
|
|
|
{
|
2019-02-08 13:44:52 -05:00
|
|
|
$this->stack_ref['#text'] = $tagData;
|
2019-02-12 14:57:50 -05:00
|
|
|
}
|
2019-02-08 13:44:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function tagClosed($parser,$name) {
|
|
|
|
$this->pop_pos();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will parse an XML array and make a normal array.
|
|
|
|
*
|
2019-02-12 14:52:01 -05:00
|
|
|
* @param $details
|
2019-02-08 13:44:52 -05:00
|
|
|
* @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']))
|
2019-02-12 14:57:50 -05:00
|
|
|
{
|
2019-02-08 13:44:52 -05:00
|
|
|
return $details['#text'];
|
2019-02-12 14:57:50 -05:00
|
|
|
}
|
2019-02-08 13:44:52 -05:00
|
|
|
|
|
|
|
elseif (is_array($details) && isset($details['ID']) && count($details) == 1)
|
2019-02-12 14:57:50 -05:00
|
|
|
{
|
2019-02-08 13:44:52 -05:00
|
|
|
return $details['ID'];
|
2019-02-12 14:57:50 -05:00
|
|
|
}
|
2019-02-08 13:44:52 -05:00
|
|
|
|
|
|
|
$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))
|
2019-02-12 14:57:50 -05:00
|
|
|
{
|
2019-02-08 13:44:52 -05:00
|
|
|
foreach ($details as $key => $values)
|
2019-02-12 14:57:50 -05:00
|
|
|
{
|
|
|
|
if (is_numeric($key) && isset($values['ID']) && count($values) > 1)
|
|
|
|
{
|
2019-02-08 13:44:52 -05:00
|
|
|
$key = $values['ID'];
|
|
|
|
unset($values['ID']);
|
|
|
|
$cleanXML[$key] = $this->cleanXML($values);
|
|
|
|
|
|
|
|
} elseif (isset($values['#text']))
|
2019-02-12 14:57:50 -05:00
|
|
|
{
|
2019-02-08 13:44:52 -05:00
|
|
|
$cleanXML[$key] = $this->cleanXML($values);
|
2019-02-12 14:57:50 -05:00
|
|
|
} elseif (is_array($values))
|
|
|
|
{
|
2019-02-08 13:44:52 -05:00
|
|
|
$cleanXML[$key] = $this->cleanXML($values);
|
2019-02-12 14:57:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-08 13:44:52 -05:00
|
|
|
|
|
|
|
if (! $cleanXML)
|
2019-02-12 14:57:50 -05:00
|
|
|
{
|
2019-02-08 13:44:52 -05:00
|
|
|
return $details;
|
2019-02-12 14:57:50 -05:00
|
|
|
}
|
2019-02-08 13:44:52 -05:00
|
|
|
else
|
2019-02-12 14:57:50 -05:00
|
|
|
{
|
2019-02-08 13:44:52 -05:00
|
|
|
return $cleanXML;
|
2019-02-12 14:57:50 -05:00
|
|
|
}
|
2019-02-08 13:44:52 -05:00
|
|
|
}
|
|
|
|
}
|