2008-01-30 20:33:16 +00:00
|
|
|
<?php
|
2006-12-02 04:36:16 +00:00
|
|
|
/*
|
2009-08-27 13:58:28 +00:00
|
|
|
* e107 website system
|
|
|
|
*
|
|
|
|
* Copyright (C) 2001-2008 e107 Inc (e107.org)
|
|
|
|
* Released under the terms and conditions of the
|
|
|
|
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
|
|
|
|
*
|
|
|
|
* Simple XML Parser
|
|
|
|
*
|
|
|
|
* $Source: /cvs_backup/e107_0.8/e107_handlers/xml_class.php,v $
|
2009-08-27 21:50:59 +00:00
|
|
|
* $Revision: 1.16 $
|
|
|
|
* $Date: 2009-08-27 21:50:59 $
|
2009-08-27 13:58:28 +00:00
|
|
|
* $Author: secretr $
|
|
|
|
*/
|
2009-08-24 00:58:32 +00:00
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
if (!defined('e107_INIT')) { exit; }
|
2006-12-02 04:36:16 +00:00
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
/**
|
|
|
|
* Simple XML Parser
|
|
|
|
*
|
|
|
|
* @package e107
|
|
|
|
* @category e107_handlers
|
|
|
|
* @version 1.1
|
|
|
|
* @author McFly
|
|
|
|
* @copyright Copyright (c) 2009, e107 Inc.
|
|
|
|
*/
|
2008-01-20 04:46:35 +00:00
|
|
|
class xmlClass
|
|
|
|
{
|
2009-08-27 13:58:28 +00:00
|
|
|
/**
|
|
|
|
* Loaded XML string
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $xmlFileContents = '';
|
2009-08-24 00:58:32 +00:00
|
|
|
|
2008-08-25 10:46:46 +00:00
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
/**
|
|
|
|
* Set to FALSE if not enabled (default on initialisation)
|
|
|
|
* Otherwise mirrors the required subset of the loaded XML - set a field FALSE to accept all
|
|
|
|
* ...elements lower down the tree. e.g.:
|
|
|
|
* <code>
|
|
|
|
* $filter = array(
|
|
|
|
* 'name' => FALSE,
|
|
|
|
* 'administration' => FALSE,
|
|
|
|
* 'management' => array('install' => FALSE)
|
|
|
|
* );
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @see setOptFilter()
|
|
|
|
* @see parseXml()
|
|
|
|
* @see xml2array()
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
public $filter = false; // Optional filter for loaded XML
|
2008-08-25 10:46:46 +00:00
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
/**
|
|
|
|
* Set true to strip all mention of comments from the returned array (default);
|
|
|
|
* FALSE to return comment markers (object SimpleXMLElement)
|
|
|
|
*
|
|
|
|
* @see setOptStripComments()
|
|
|
|
* @see parseXml()
|
|
|
|
* @see xml2array()
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
public $stripComments = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add root element to the result array
|
|
|
|
* Exmple:
|
|
|
|
* <code>
|
|
|
|
* <root>
|
|
|
|
* <tag>some value</tag>
|
|
|
|
* </root>
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* if
|
|
|
|
* <code>$_optAddRoot = true;</code>
|
|
|
|
* xml2array() result is array('root' => array('tag' => 'some value'));
|
|
|
|
*
|
|
|
|
* if
|
|
|
|
* <code>$_optAddRoot = false;</code>
|
|
|
|
* xml2array() result is array('tag' => 'some value');
|
|
|
|
*
|
|
|
|
* @see xml2array()
|
|
|
|
* @see setOptAddRoot()
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $_optAddRoot = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Always return array, even for single first level tag => value pair
|
|
|
|
* Exmple:
|
|
|
|
* <code>
|
|
|
|
* <root>
|
|
|
|
* <tag>some value</tag>
|
|
|
|
* </root>
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* if
|
|
|
|
* <code>$_optForceArray = true;</code>
|
|
|
|
* xml2array() result is array('tag' => array('value' => 'some value'));
|
|
|
|
* where 'value' is the value of $_optValueKey
|
|
|
|
*
|
|
|
|
* If
|
|
|
|
* <code>$_optForceArray = false;</code>
|
|
|
|
* xml2array() result is array('tag' => 'some value');
|
|
|
|
*
|
|
|
|
* @see xml2array()
|
|
|
|
* @see setOptForceArray()
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $_optForceArray = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Key name for simple tag => value pairs
|
|
|
|
*
|
|
|
|
* @see xml2array()
|
|
|
|
* @see setOptValueKey()
|
|
|
|
* @var string
|
|
|
|
*/
|
2009-08-27 21:50:59 +00:00
|
|
|
protected $_optValueKey = '@value';
|
2009-08-24 00:58:32 +00:00
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
/**
|
|
|
|
* Constructor - set defaults
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function __constructor()
|
2008-08-25 10:46:46 +00:00
|
|
|
{
|
2009-08-27 13:58:28 +00:00
|
|
|
$this->reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset object
|
|
|
|
*
|
|
|
|
* @param boolean $xml_contents [optional]
|
|
|
|
* @return xmlClass
|
|
|
|
*/
|
|
|
|
function reset($xml_contents = true)
|
|
|
|
{
|
|
|
|
if($xml_contents)
|
|
|
|
{
|
|
|
|
$this->xmlFileContents = '';
|
|
|
|
}
|
|
|
|
$this->filter = false;
|
|
|
|
$this->stripComments = true;
|
|
|
|
$this->_optAddRoot = false;
|
2009-08-27 21:50:59 +00:00
|
|
|
$this->_optValueKey = '@value';
|
2009-08-27 13:58:28 +00:00
|
|
|
$this->_optForceArray = false;
|
|
|
|
return $this;
|
2008-08-25 10:46:46 +00:00
|
|
|
}
|
2006-12-02 04:36:16 +00:00
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
/**
|
|
|
|
* Set addRoot option
|
|
|
|
*
|
|
|
|
* @param boolean $flag
|
|
|
|
* @return xmlClass
|
|
|
|
*/
|
|
|
|
public function setOptAddRoot($flag)
|
|
|
|
{
|
|
|
|
$this->_optAddRoot = (boolean) $flag;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set forceArray option
|
|
|
|
*
|
|
|
|
* @param string $flag
|
|
|
|
* @return xmlClass
|
|
|
|
*/
|
|
|
|
public function setOptForceArray($flag)
|
|
|
|
{
|
|
|
|
$this->_optForceArray = (boolean) $flag;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set valueKey option
|
|
|
|
*
|
|
|
|
* @param string $str
|
|
|
|
* @return xmlClass
|
|
|
|
*/
|
|
|
|
public function setOptValueKey($str)
|
|
|
|
{
|
|
|
|
$this->_optValueKey = trim((string) $str);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set strpComments option
|
|
|
|
*
|
|
|
|
* @param boolean $flag
|
|
|
|
* @return xmlClass
|
|
|
|
*/
|
|
|
|
public function setOptStripComments($flag)
|
|
|
|
{
|
|
|
|
$this->stripComments = (boolean) $flag;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set strpComments option
|
|
|
|
*
|
|
|
|
* @param array $filter
|
|
|
|
* @return xmlClass
|
|
|
|
*/
|
|
|
|
public function setOptFilter($filter)
|
|
|
|
{
|
|
|
|
$this->filter = (array) $filter;
|
|
|
|
return $this;
|
|
|
|
}
|
2009-08-17 12:48:52 +00:00
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
/**
|
|
|
|
* Get Remote file contents
|
|
|
|
*
|
|
|
|
* @param string $address
|
|
|
|
* @param integer $timeout [optional] seconds
|
|
|
|
* @return string
|
|
|
|
*/
|
2009-08-24 00:58:32 +00:00
|
|
|
function getRemoteFile($address, $timeout = 10)
|
|
|
|
{
|
|
|
|
// Could do something like: if ($timeout <= 0) $timeout = $pref['get_remote_timeout']; here
|
|
|
|
$timeout = min($timeout, 120);
|
|
|
|
$timeout = max($timeout, 3);
|
|
|
|
$address = str_replace(array("\r", "\n", "\t"), '', $address); // May be paranoia, but streaky thought it might be a good idea
|
|
|
|
// ... and there shouldn't be unprintable characters in the URL anyway
|
|
|
|
if (function_exists('file_get_contents'))
|
2008-01-20 04:46:35 +00:00
|
|
|
{
|
2009-08-24 00:58:32 +00:00
|
|
|
$old_timeout = e107_ini_set('default_socket_timeout', $timeout);
|
|
|
|
$data = file_get_contents(urlencode($address));
|
|
|
|
echo "data=".$data;
|
|
|
|
// $data = file_get_contents(htmlspecialchars($address)); // buggy - sometimes fails.
|
|
|
|
if ($old_timeout !== FALSE)
|
|
|
|
{
|
|
|
|
e107_ini_set('default_socket_timeout', $old_timeout);
|
|
|
|
}
|
|
|
|
if ($data)
|
|
|
|
{
|
|
|
|
return $data;
|
|
|
|
}
|
2008-01-20 04:46:35 +00:00
|
|
|
}
|
2009-08-24 00:58:32 +00:00
|
|
|
if (function_exists("curl_init"))
|
2006-12-02 04:36:16 +00:00
|
|
|
{
|
2008-12-02 20:14:47 +00:00
|
|
|
$cu = curl_init();
|
2006-12-02 04:36:16 +00:00
|
|
|
curl_setopt($cu, CURLOPT_URL, $address);
|
|
|
|
curl_setopt($cu, CURLOPT_RETURNTRANSFER, 1);
|
2009-08-24 00:58:32 +00:00
|
|
|
curl_setopt($cu, CURLOPT_HEADER, 0);
|
|
|
|
curl_setopt($cu, CURLOPT_TIMEOUT, $timeout);
|
2008-01-20 04:46:35 +00:00
|
|
|
$this->xmlFileContents = curl_exec($cu);
|
2006-12-02 04:36:16 +00:00
|
|
|
if (curl_error($cu))
|
|
|
|
{
|
2009-08-24 00:58:32 +00:00
|
|
|
$this->error = "Curl error: ".curl_errno($cu).", ".curl_error($cu);
|
2006-12-02 04:36:16 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2008-12-02 20:14:47 +00:00
|
|
|
curl_close($cu);
|
2008-01-20 04:46:35 +00:00
|
|
|
return $this->xmlFileContents;
|
2006-12-02 04:36:16 +00:00
|
|
|
}
|
2009-08-24 00:58:32 +00:00
|
|
|
if (ini_get("allow_url_fopen"))
|
2006-12-02 04:36:16 +00:00
|
|
|
{
|
2008-12-02 20:14:47 +00:00
|
|
|
$old_timeout = e107_ini_set('default_socket_timeout', $timeout);
|
|
|
|
$remote = @fopen($address, "r");
|
2009-08-24 00:58:32 +00:00
|
|
|
if (!$remote)
|
2008-12-02 20:14:47 +00:00
|
|
|
{
|
2009-08-24 00:58:32 +00:00
|
|
|
$this->error = "fopen: Unable to open remote XML file: ".$address;
|
2008-12-02 20:14:47 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2006-12-02 04:36:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-08-24 00:58:32 +00:00
|
|
|
$old_timeout = $timeout;
|
|
|
|
$tmp = parse_url($address);
|
|
|
|
if (!$remote = fsockopen($tmp['host'], 80, $errno, $errstr, $timeout))
|
|
|
|
{
|
|
|
|
$this->error = "Sockets: Unable to open remote XML file: ".$address;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
socket_set_timeout($remote, $timeout);
|
|
|
|
fputs($remote, "GET ".urlencode($address)." HTTP/1.0\r\n\r\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->xmlFileContents = "";
|
2006-12-02 04:36:16 +00:00
|
|
|
while (!feof($remote))
|
|
|
|
{
|
2009-08-24 00:58:32 +00:00
|
|
|
$this->xmlFileContents .= fgets($remote, 4096);
|
2006-12-02 04:36:16 +00:00
|
|
|
}
|
2009-08-24 00:58:32 +00:00
|
|
|
fclose($remote);
|
2008-12-02 20:14:47 +00:00
|
|
|
if ($old_timeout != $timeout)
|
|
|
|
{
|
2009-08-24 00:58:32 +00:00
|
|
|
if ($old_timeout !== FALSE)
|
|
|
|
{
|
|
|
|
e107_ini_set('default_socket_timeout', $old_timeout);
|
|
|
|
}
|
2008-12-02 20:14:47 +00:00
|
|
|
}
|
2008-01-20 04:46:35 +00:00
|
|
|
return $this->xmlFileContents;
|
2006-12-02 04:36:16 +00:00
|
|
|
}
|
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
/**
|
|
|
|
* Parse $xmlFileContents XML string to array
|
|
|
|
*
|
|
|
|
* @param string $xml [optional]
|
|
|
|
* @param boolean $simple [optional] false - use xml2array(), true - use xml_convert_to_array()
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function parseXml($xml = '', $simple = true)
|
2006-12-02 04:36:16 +00:00
|
|
|
{
|
2009-08-27 14:34:20 +00:00
|
|
|
if ($xml)
|
|
|
|
{
|
|
|
|
$this->xmlFileContents = $xml;
|
|
|
|
}
|
|
|
|
elseif ($this->xmlFileContents)
|
2006-12-02 04:36:16 +00:00
|
|
|
{
|
2008-01-20 04:46:35 +00:00
|
|
|
$xml = $this->xmlFileContents;
|
2006-12-02 04:36:16 +00:00
|
|
|
}
|
2009-08-24 00:58:32 +00:00
|
|
|
if (!$xml)
|
2006-12-02 04:36:16 +00:00
|
|
|
{
|
2008-01-20 04:46:35 +00:00
|
|
|
return false;
|
2006-12-02 04:36:16 +00:00
|
|
|
}
|
2008-01-20 04:46:35 +00:00
|
|
|
$xml = simplexml_load_string($xml);
|
2009-08-27 13:58:28 +00:00
|
|
|
|
|
|
|
$xml = $simple ? $this->xml_convert_to_array($xml, $this->filter, $this->stripComments) : $this->xml2array($xml);
|
2008-01-20 04:46:35 +00:00
|
|
|
return $xml;
|
2006-12-02 04:36:16 +00:00
|
|
|
}
|
2009-08-27 13:58:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Advanced XML parser - handles tags with attributes and values
|
|
|
|
* proper.
|
|
|
|
* TODO - filter (see xml_convert_to_array)
|
|
|
|
*
|
|
|
|
* @param SimpleXMLElement $xml
|
|
|
|
* @param string $rec_parent used for recursive calls only
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
function xml2array($xml, $rec_parent = '')
|
|
|
|
{
|
|
|
|
$ret = array();
|
|
|
|
$tags = get_object_vars($xml);
|
|
|
|
|
|
|
|
//remove comments
|
|
|
|
if($this->stripComments && isset($tags['comment']))
|
|
|
|
{
|
|
|
|
unset($tags['comment']);
|
|
|
|
}
|
|
|
|
|
|
|
|
//first call
|
|
|
|
if(!$rec_parent)
|
|
|
|
{
|
|
|
|
//$ret = $this->xml2array($xml, true);
|
|
|
|
//repeating code because of the _optForceArray functionality
|
2009-08-27 21:50:59 +00:00
|
|
|
|
|
|
|
if(!is_object($xml))
|
|
|
|
{
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
$tags = array_keys($tags);
|
|
|
|
foreach ($tags as $tag)
|
|
|
|
{
|
|
|
|
if($tag == '@attributes')
|
|
|
|
{
|
|
|
|
$tmp = (array) $xml->attributes();
|
|
|
|
$ret['@attributes'] = $tmp['@attributes'];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$count = count($xml->{$tag});
|
|
|
|
if($count > 1)
|
|
|
|
{
|
|
|
|
for ($i = 0; $i < $count; $i++)
|
|
|
|
{
|
|
|
|
$ret[$tag][$i] = $this->xml2array($xml->{$tag}[$i], $tag);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$ret[$tag] = $this->xml2array($xml->{$tag}, $tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ($this->_optAddRoot ? array($xml->getName() => $ret) : $ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Recursive calls start here
|
|
|
|
if($tags)
|
|
|
|
{
|
|
|
|
$tags = array_keys($tags);
|
|
|
|
$count_tags = count($tags);
|
|
|
|
|
|
|
|
//loop through tags
|
|
|
|
foreach ($tags as $tag)
|
|
|
|
{
|
|
|
|
switch($tag)
|
|
|
|
{
|
|
|
|
case '@attributes':
|
|
|
|
$tmp = (array) $xml->attributes();
|
|
|
|
$ret['@attributes'] = $tmp['@attributes'];
|
|
|
|
|
|
|
|
if($count_tags == 1) //only attributes & possible value
|
|
|
|
{
|
|
|
|
$ret[$this->_optValueKey] = trim((string) $xml);
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'comment':
|
|
|
|
$ret[$this->_optValueKey] = trim((string) $xml);
|
|
|
|
$ret['comment'] = $xml->comment;
|
|
|
|
break;
|
|
|
|
|
|
|
|
//more cases?
|
|
|
|
default:
|
|
|
|
$count = count($xml->{$tag});
|
|
|
|
if($count >= 1) //array of elements - loop
|
|
|
|
{
|
|
|
|
for ($i = 0; $i < $count; $i++)
|
|
|
|
{
|
|
|
|
$ret[$tag][$i] = $this->xml2array($xml->{$tag}[$i], $tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else //single element
|
|
|
|
{
|
|
|
|
$ret[$tag] = $this->xml2array($xml->{$tag}, $tag);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
//parse value only
|
|
|
|
$ret = trim((string) $xml);
|
|
|
|
return ($this->_optForceArray ? array($this->_optValueKey => $ret) : $ret);
|
|
|
|
}
|
2006-12-02 04:36:16 +00:00
|
|
|
|
2009-08-24 00:58:32 +00:00
|
|
|
|
2008-12-02 20:14:47 +00:00
|
|
|
function xml_convert_to_array($xml, $localFilter = FALSE, $stripComments = TRUE)
|
2006-12-02 04:36:16 +00:00
|
|
|
{
|
2009-08-27 14:34:20 +00:00
|
|
|
if (is_object($xml))
|
|
|
|
{
|
|
|
|
$xml = (array) $xml;
|
|
|
|
}
|
2009-08-24 00:58:32 +00:00
|
|
|
if (is_array($xml))
|
2006-12-02 04:36:16 +00:00
|
|
|
{
|
2009-08-24 00:58:32 +00:00
|
|
|
foreach ($xml as $k=>$v)
|
2008-01-20 04:46:35 +00:00
|
|
|
{
|
2008-12-02 20:14:47 +00:00
|
|
|
if ($stripComments && ($k === 'comment'))
|
|
|
|
{
|
|
|
|
unset($xml[$k]);
|
|
|
|
continue;
|
|
|
|
}
|
2008-08-25 10:46:46 +00:00
|
|
|
$enabled = FALSE;
|
|
|
|
if ($localFilter === FALSE)
|
2008-01-20 04:46:35 +00:00
|
|
|
{
|
2008-08-25 10:46:46 +00:00
|
|
|
$enabled = TRUE;
|
|
|
|
$onFilter = FALSE;
|
|
|
|
}
|
|
|
|
elseif (isset($localFilter[$k]))
|
|
|
|
{
|
|
|
|
$enabled = TRUE;
|
|
|
|
$onFilter = $localFilter[$k];
|
|
|
|
}
|
|
|
|
if ($enabled)
|
|
|
|
{
|
2009-08-24 00:58:32 +00:00
|
|
|
if (is_object($v))
|
2008-08-25 10:46:46 +00:00
|
|
|
{
|
2009-08-24 00:58:32 +00:00
|
|
|
$v = (array) $v;
|
2008-08-25 10:46:46 +00:00
|
|
|
}
|
2008-12-02 20:14:47 +00:00
|
|
|
$xml[$k] = $this->xml_convert_to_array($v, $onFilter, $stripComments);
|
2008-08-25 10:46:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unset($xml[$k]);
|
2008-01-20 04:46:35 +00:00
|
|
|
}
|
|
|
|
}
|
2009-08-24 00:58:32 +00:00
|
|
|
if (count($xml) == 1 && isset($xml[0]))
|
2008-01-20 04:46:35 +00:00
|
|
|
{
|
|
|
|
$xml = $xml[0];
|
|
|
|
}
|
2006-12-02 04:36:16 +00:00
|
|
|
}
|
2008-01-20 04:46:35 +00:00
|
|
|
return $xml;
|
2006-12-02 04:36:16 +00:00
|
|
|
}
|
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
/**
|
|
|
|
* Load XML file and parse it (optional)
|
|
|
|
*
|
|
|
|
* @param string $fname local or remote XML source file path
|
|
|
|
* @param boolean|string $parse false - no parse;
|
|
|
|
* true - use xml_convert_to_array();
|
|
|
|
* in any other case - use xml2array()
|
|
|
|
*
|
|
|
|
* @param boolean $replace_constants [optional]
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
function loadXMLfile($fname, $parse = false, $replace_constants = false)
|
2006-12-02 04:36:16 +00:00
|
|
|
{
|
2009-08-27 13:58:28 +00:00
|
|
|
if (empty($fname))
|
2006-12-02 04:36:16 +00:00
|
|
|
{
|
2008-01-20 04:46:35 +00:00
|
|
|
return false;
|
2006-12-02 04:36:16 +00:00
|
|
|
}
|
2008-01-20 04:46:35 +00:00
|
|
|
$xml = false;
|
2009-08-24 00:58:32 +00:00
|
|
|
if (strpos($fname, '://') !== false)
|
2006-12-02 04:36:16 +00:00
|
|
|
{
|
2008-01-20 04:46:35 +00:00
|
|
|
$this->getRemoteFile($fname);
|
2006-12-02 04:36:16 +00:00
|
|
|
}
|
2008-01-20 04:46:35 +00:00
|
|
|
else
|
2006-12-02 04:36:16 +00:00
|
|
|
{
|
2009-08-24 00:58:32 +00:00
|
|
|
if ($xml = file_get_contents($fname))
|
2008-01-20 04:46:35 +00:00
|
|
|
{
|
|
|
|
$this->xmlFileContents = $xml;
|
|
|
|
}
|
2006-12-02 04:36:16 +00:00
|
|
|
}
|
2009-08-24 00:58:32 +00:00
|
|
|
if ($this->xmlFileContents)
|
2006-12-02 04:36:16 +00:00
|
|
|
{
|
2009-08-24 00:58:32 +00:00
|
|
|
if ($replace_constants == true)
|
2008-02-01 14:11:27 +00:00
|
|
|
{
|
2009-08-27 13:58:28 +00:00
|
|
|
$this->xmlFileContents = e107::getParser()->replaceConstants($this->xmlFileContents, '', true);
|
2008-02-01 14:11:27 +00:00
|
|
|
}
|
2009-08-27 13:58:28 +00:00
|
|
|
if ($parse)
|
2008-01-20 04:46:35 +00:00
|
|
|
{
|
2009-08-27 13:58:28 +00:00
|
|
|
return $this->parseXML('', ($parse === true));
|
2008-01-20 04:46:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return $this->xmlFileContents;
|
|
|
|
}
|
2006-12-02 04:36:16 +00:00
|
|
|
}
|
2008-01-20 04:46:35 +00:00
|
|
|
return false;
|
2006-12-02 04:36:16 +00:00
|
|
|
}
|
2009-08-27 13:58:28 +00:00
|
|
|
|
|
|
|
//FIXME - TEST - remove me
|
2009-08-24 00:58:32 +00:00
|
|
|
function xml2ary(&$string)
|
|
|
|
{
|
|
|
|
$parser = xml_parser_create();
|
|
|
|
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
|
|
|
|
xml_parse_into_struct($parser, $string, $vals, $index);
|
|
|
|
xml_parser_free($parser);
|
|
|
|
$mnary = array();
|
|
|
|
$ary = &$mnary;
|
|
|
|
foreach ($vals as $r)
|
|
|
|
{
|
|
|
|
$t = $r['tag'];
|
|
|
|
if ($r['type'] == 'open')
|
|
|
|
{
|
|
|
|
if (isset($ary[$t]))
|
|
|
|
{
|
|
|
|
if (isset($ary[$t][0]))
|
|
|
|
$ary[$t][] = array();
|
|
|
|
else
|
|
|
|
$ary[$t] = array($ary[$t], array());
|
|
|
|
$cv = &$ary[$t][count($ary[$t]) - 1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
$cv = &$ary[$t];
|
|
|
|
if (isset($r['attributes']))
|
|
|
|
{
|
|
|
|
foreach ($r['attributes'] as $k=>$v)
|
|
|
|
$cv['_a'][$k] = $v;
|
|
|
|
}
|
|
|
|
$cv['_c'] = array();
|
|
|
|
$cv['_c']['_p'] = &$ary;
|
|
|
|
$ary = &$cv['_c'];
|
|
|
|
}
|
|
|
|
elseif ($r['type'] == 'complete')
|
|
|
|
{
|
|
|
|
if (isset($ary[$t]))
|
|
|
|
{ // same as open
|
|
|
|
if (isset($ary[$t][0]))
|
|
|
|
$ary[$t][] = array();
|
|
|
|
else
|
|
|
|
$ary[$t] = array($ary[$t], array());
|
|
|
|
$cv = &$ary[$t][count($ary[$t]) - 1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
$cv = &$ary[$t];
|
|
|
|
if (isset($r['attributes']))
|
|
|
|
{
|
|
|
|
foreach ($r['attributes'] as $k=>$v)
|
|
|
|
$cv['_a'][$k] = $v;
|
|
|
|
}
|
|
|
|
$cv['_v'] = (isset($r['value']) ? $r['value'] : '');
|
|
|
|
}
|
|
|
|
elseif ($r['type'] == 'close')
|
|
|
|
{
|
|
|
|
$ary = &$ary['_p'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->_del_p($mnary);
|
|
|
|
return $mnary;
|
|
|
|
}
|
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
//FIXME - TEST - remove me
|
2009-08-24 00:58:32 +00:00
|
|
|
function _del_p(&$ary)
|
|
|
|
{
|
|
|
|
foreach ($ary as $k=>$v)
|
|
|
|
{
|
|
|
|
if ($k === '_p')
|
|
|
|
unset($ary[$k]);
|
|
|
|
elseif (is_array($ary[$k]))
|
|
|
|
$this->_del_p($ary[$k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
//FIXME - TEST - remove me
|
2009-08-24 00:58:32 +00:00
|
|
|
function ary2xml($cary, $d = 0, $forcetag = '')
|
|
|
|
{
|
|
|
|
$res = array();
|
|
|
|
foreach ($cary as $tag=>$r)
|
|
|
|
{
|
|
|
|
if (isset($r[0]))
|
|
|
|
{
|
|
|
|
$res[] = $this->ary2xml($r, $d, $tag);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ($forcetag)
|
|
|
|
$tag = $forcetag;
|
|
|
|
$sp = str_repeat("\t", $d);
|
|
|
|
$res[] = "$sp<$tag";
|
|
|
|
if (isset($r['_a']))
|
|
|
|
{
|
|
|
|
foreach ($r['_a'] as $at=>$av)
|
|
|
|
$res[] = " $at=\"$av\"";
|
|
|
|
}
|
|
|
|
$res[] = ">".((isset($r['_c'])) ? "\n" : '');
|
|
|
|
if (isset($r['_c']))
|
|
|
|
$res[] = $this->ary2xml($r['_c'], $d + 1);
|
|
|
|
elseif (isset($r['_v']))
|
|
|
|
$res[] = $r['_v'];
|
|
|
|
$res[] = (isset($r['_c']) ? $sp : '')."</$tag>\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return implode('', $res);
|
|
|
|
}
|
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
//FIXME - TEST - remove me
|
2009-08-24 00:58:32 +00:00
|
|
|
function ins2ary(&$ary, $element, $pos)
|
|
|
|
{
|
|
|
|
$ar1 = array_slice($ary, 0, $pos);
|
|
|
|
$ar1[] = $element;
|
|
|
|
$ary = array_merge($ar1, array_slice($ary, $pos));
|
|
|
|
}
|
2008-01-30 20:33:16 +00:00
|
|
|
}
|