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
|
|
|
|
*
|
2011-10-02 17:16:16 +00:00
|
|
|
* Copyright (C) 2008-2011 e107 Inc (e107.org)
|
2009-08-27 13:58:28 +00:00
|
|
|
* Released under the terms and conditions of the
|
|
|
|
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
|
|
|
|
*
|
|
|
|
* Simple XML Parser
|
|
|
|
*
|
2010-05-16 11:14:19 +00:00
|
|
|
* $URL $
|
|
|
|
* $Id$
|
2009-08-27 13:58:28 +00:00
|
|
|
*/
|
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
|
|
|
|
{
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
/**
|
|
|
|
* Loaded XML string
|
2010-02-07 00:44:08 +00:00
|
|
|
*
|
2009-08-27 13:58:28 +00:00
|
|
|
* @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>
|
2010-02-07 00:44:08 +00:00
|
|
|
*
|
2009-08-27 13:58:28 +00:00
|
|
|
* @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
|
|
|
/**
|
2010-02-07 00:44:08 +00:00
|
|
|
* Set true to strip all mention of comments from the returned array (default);
|
2009-08-27 13:58:28 +00:00
|
|
|
* FALSE to return comment markers (object SimpleXMLElement)
|
2010-02-07 00:44:08 +00:00
|
|
|
*
|
2009-08-27 13:58:28 +00:00
|
|
|
* @see setOptStripComments()
|
|
|
|
* @see parseXml()
|
|
|
|
* @see xml2array()
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2010-02-07 00:44:08 +00:00
|
|
|
public $stripComments = true;
|
|
|
|
|
2009-09-05 23:02:23 +00:00
|
|
|
/**
|
|
|
|
* Log of all paths replaced.
|
2009-11-17 10:31:05 +00:00
|
|
|
*
|
|
|
|
* @var array
|
2010-02-07 00:44:08 +00:00
|
|
|
*/
|
2009-09-05 23:02:23 +00:00
|
|
|
public $fileConvertLog = array();
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-09-05 23:02:23 +00:00
|
|
|
public $convertFilePaths = FALSE;
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-09-05 23:02:23 +00:00
|
|
|
public $filePathDestination = FALSE;
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-11-17 10:31:05 +00:00
|
|
|
public $convertFileTypes = array("jpg", "gif", "png", "jpeg");
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-09-10 09:49:01 +00:00
|
|
|
public $filePathPrepend = array();
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-09-10 09:49:01 +00:00
|
|
|
public $filePathConvKeys = array();
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-09-17 04:30:25 +00:00
|
|
|
public $errors;
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-09-16 13:01:19 +00:00
|
|
|
private $arrayTags = false;
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-09-21 21:53:38 +00:00
|
|
|
private $stringTags = false;
|
2010-02-07 00:44:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
/**
|
|
|
|
* Add root element to the result array
|
|
|
|
* Exmple:
|
|
|
|
* <code>
|
|
|
|
* <root>
|
2010-02-07 00:44:08 +00:00
|
|
|
* <tag>some value</tag>
|
2009-08-27 13:58:28 +00:00
|
|
|
* </root>
|
|
|
|
* </code>
|
2010-02-07 00:44:08 +00:00
|
|
|
*
|
2009-08-27 13:58:28 +00:00
|
|
|
* if
|
|
|
|
* <code>$_optAddRoot = true;</code>
|
|
|
|
* xml2array() result is array('root' => array('tag' => 'some value'));
|
2010-02-07 00:44:08 +00:00
|
|
|
*
|
2009-08-27 13:58:28 +00:00
|
|
|
* if
|
|
|
|
* <code>$_optAddRoot = false;</code>
|
|
|
|
* xml2array() result is array('tag' => 'some value');
|
2010-02-07 00:44:08 +00:00
|
|
|
*
|
2009-08-27 13:58:28 +00:00
|
|
|
* @see xml2array()
|
|
|
|
* @see setOptAddRoot()
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $_optAddRoot = false;
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
/**
|
|
|
|
* Always return array, even for single first level tag => value pair
|
|
|
|
* Exmple:
|
|
|
|
* <code>
|
|
|
|
* <root>
|
2010-02-07 00:44:08 +00:00
|
|
|
* <tag>some value</tag>
|
2009-08-27 13:58:28 +00:00
|
|
|
* </root>
|
|
|
|
* </code>
|
2010-02-07 00:44:08 +00:00
|
|
|
*
|
2009-08-27 13:58:28 +00:00
|
|
|
* if
|
|
|
|
* <code>$_optForceArray = true;</code>
|
|
|
|
* xml2array() result is array('tag' => array('value' => 'some value'));
|
|
|
|
* where 'value' is the value of $_optValueKey
|
2010-02-07 00:44:08 +00:00
|
|
|
*
|
2009-08-27 13:58:28 +00:00
|
|
|
* If
|
|
|
|
* <code>$_optForceArray = false;</code>
|
|
|
|
* xml2array() result is array('tag' => 'some value');
|
2010-02-07 00:44:08 +00:00
|
|
|
*
|
2009-08-27 13:58:28 +00:00
|
|
|
* @see xml2array()
|
|
|
|
* @see setOptForceArray()
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $_optForceArray = false;
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
/**
|
|
|
|
* Key name for simple tag => value pairs
|
2010-02-07 00:44:08 +00:00
|
|
|
*
|
2009-08-27 13:58:28 +00:00
|
|
|
* @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
|
|
|
|
2012-08-18 00:53:04 +00:00
|
|
|
|
|
|
|
protected $_feedUrl = FALSE;
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
/**
|
|
|
|
* Constructor - set defaults
|
2010-02-07 00:44:08 +00:00
|
|
|
*
|
2009-08-27 13:58:28 +00:00
|
|
|
*/
|
|
|
|
function __constructor()
|
2010-02-07 00:44:08 +00:00
|
|
|
{
|
2009-08-27 13:58:28 +00:00
|
|
|
$this->reset();
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-09-10 09:49:01 +00:00
|
|
|
if(count($this->filePathConversions))
|
|
|
|
{
|
2010-02-07 00:44:08 +00:00
|
|
|
$this->filePathConvKeys = array_keys($this->filePathConversions);
|
2009-09-10 09:49:01 +00:00
|
|
|
}
|
2009-08-27 13:58:28 +00:00
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
/**
|
|
|
|
* Reset object
|
2010-02-07 00:44:08 +00:00
|
|
|
*
|
2009-08-27 13:58:28 +00:00
|
|
|
* @param boolean $xml_contents [optional]
|
|
|
|
* @return xmlClass
|
|
|
|
*/
|
|
|
|
function reset($xml_contents = true)
|
|
|
|
{
|
2010-02-07 00:44:08 +00:00
|
|
|
if($xml_contents)
|
|
|
|
{
|
|
|
|
$this->xmlFileContents = '';
|
2009-08-27 13:58:28 +00:00
|
|
|
}
|
|
|
|
$this->filter = false;
|
2010-02-07 00:44:08 +00:00
|
|
|
$this->stripComments = true;
|
2009-08-27 13:58:28 +00:00
|
|
|
$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
|
2010-02-07 00:44:08 +00:00
|
|
|
*
|
2009-08-27 13:58:28 +00:00
|
|
|
* @param boolean $flag
|
|
|
|
* @return xmlClass
|
|
|
|
*/
|
|
|
|
public function setOptAddRoot($flag)
|
|
|
|
{
|
|
|
|
$this->_optAddRoot = (boolean) $flag;
|
|
|
|
return $this;
|
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-09-16 13:01:19 +00:00
|
|
|
/**
|
|
|
|
* Set Xml tags that should always return arrays.
|
2009-11-17 10:31:05 +00:00
|
|
|
*
|
2012-07-20 07:31:16 +00:00
|
|
|
* @param object $string (comma separated)
|
2009-11-17 10:31:05 +00:00
|
|
|
* @return xmlClass
|
2009-09-16 13:01:19 +00:00
|
|
|
*/
|
|
|
|
public function setOptArrayTags($string)
|
|
|
|
{
|
2009-11-17 10:31:05 +00:00
|
|
|
$this->arrayTags = (array) explode(",", $string);
|
2009-09-16 13:01:19 +00:00
|
|
|
return $this;
|
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-09-21 21:53:38 +00:00
|
|
|
public function setOptStringTags($string)
|
|
|
|
{
|
2009-11-17 10:31:05 +00:00
|
|
|
$this->stringTags = (array) explode(",", $string);
|
2009-09-21 21:53:38 +00:00
|
|
|
return $this;
|
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
/**
|
|
|
|
* Set forceArray option
|
2010-02-07 00:44:08 +00:00
|
|
|
*
|
2009-11-17 10:31:05 +00:00
|
|
|
* @param boolean $flag
|
2009-08-27 13:58:28 +00:00
|
|
|
* @return xmlClass
|
|
|
|
*/
|
|
|
|
public function setOptForceArray($flag)
|
|
|
|
{
|
|
|
|
$this->_optForceArray = (boolean) $flag;
|
|
|
|
return $this;
|
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
/**
|
|
|
|
* Set valueKey option
|
2010-02-07 00:44:08 +00:00
|
|
|
*
|
2009-08-27 13:58:28 +00:00
|
|
|
* @param string $str
|
|
|
|
* @return xmlClass
|
|
|
|
*/
|
|
|
|
public function setOptValueKey($str)
|
|
|
|
{
|
|
|
|
$this->_optValueKey = trim((string) $str);
|
|
|
|
return $this;
|
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
/**
|
|
|
|
* Set strpComments option
|
2010-02-07 00:44:08 +00:00
|
|
|
*
|
2009-08-27 13:58:28 +00:00
|
|
|
* @param boolean $flag
|
|
|
|
* @return xmlClass
|
|
|
|
*/
|
|
|
|
public function setOptStripComments($flag)
|
|
|
|
{
|
|
|
|
$this->stripComments = (boolean) $flag;
|
|
|
|
return $this;
|
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
/**
|
|
|
|
* Set strpComments option
|
2010-02-07 00:44:08 +00:00
|
|
|
*
|
2009-08-27 13:58:28 +00:00
|
|
|
* @param array $filter
|
|
|
|
* @return xmlClass
|
|
|
|
*/
|
|
|
|
public function setOptFilter($filter)
|
|
|
|
{
|
|
|
|
$this->filter = (array) $filter;
|
|
|
|
return $this;
|
|
|
|
}
|
2012-08-18 00:53:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function setFeedUrl($feed)
|
|
|
|
{
|
|
|
|
if($feed)
|
|
|
|
{
|
|
|
|
$this->_feedUrl = $feed;
|
|
|
|
}
|
|
|
|
}
|
2009-08-17 12:48:52 +00:00
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
/**
|
|
|
|
* Get Remote file contents
|
2012-07-20 07:31:16 +00:00
|
|
|
* use setOptArrayTags above if you require a consistent array result by in 1 item or many.
|
2009-08-27 13:58:28 +00:00
|
|
|
* @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);
|
2012-08-18 00:53:04 +00:00
|
|
|
|
|
|
|
if($this->_feedUrl) // override option for use when part of the address needs to be encoded.
|
|
|
|
{
|
|
|
|
$address = $this->_feedUrl;
|
|
|
|
echo "address=".$address;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$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
|
|
|
|
}
|
|
|
|
|
2009-08-24 00:58:32 +00:00
|
|
|
// ... and there shouldn't be unprintable characters in the URL anyway
|
2011-07-06 03:17:00 +00:00
|
|
|
if (function_exists('file_get_contents') && ini_get('allow_url_fopen'))
|
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);
|
2012-08-18 00:53:04 +00:00
|
|
|
$address = ($this->_feedUrl) ? $this->_feedUrl : urldecode($address);
|
|
|
|
|
|
|
|
$data = file_get_contents($address);
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-08-24 00:58:32 +00:00
|
|
|
// $data = file_get_contents(htmlspecialchars($address)); // buggy - sometimes fails.
|
|
|
|
if ($old_timeout !== FALSE)
|
|
|
|
{
|
|
|
|
e107_ini_set('default_socket_timeout', $old_timeout);
|
|
|
|
}
|
2011-10-02 17:16:16 +00:00
|
|
|
if ($data !== FALSE)
|
2009-08-24 00:58:32 +00:00
|
|
|
{
|
2010-02-07 00:44:08 +00:00
|
|
|
$this->xmlFileContents = $data;
|
2009-08-24 00:58:32 +00:00
|
|
|
return $data;
|
|
|
|
}
|
2011-10-02 17:16:16 +00:00
|
|
|
$this->error = "File_get_contents(XML) error"; // Fill in more info later
|
|
|
|
return FALSE;
|
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
|
2010-02-07 00:44:08 +00:00
|
|
|
*
|
|
|
|
* @param string $xml [optional]
|
2009-08-27 13:58:28 +00:00
|
|
|
* @param boolean $simple [optional] false - use xml2array(), true - use xml_convert_to_array()
|
|
|
|
* @return string
|
|
|
|
*/
|
2009-09-17 04:30:25 +00:00
|
|
|
function parseXml($xmlData = '', $simple = true)
|
2006-12-02 04:36:16 +00:00
|
|
|
{
|
2009-09-17 04:30:25 +00:00
|
|
|
if ($xmlData)
|
2009-08-27 14:34:20 +00:00
|
|
|
{
|
2009-09-17 04:30:25 +00:00
|
|
|
$this->xmlFileContents = $xmlData;
|
2009-08-27 14:34:20 +00:00
|
|
|
}
|
|
|
|
elseif ($this->xmlFileContents)
|
2006-12-02 04:36:16 +00:00
|
|
|
{
|
2009-09-17 04:30:25 +00:00
|
|
|
$xmlData = $this->xmlFileContents;
|
2006-12-02 04:36:16 +00:00
|
|
|
}
|
2009-09-17 04:30:25 +00:00
|
|
|
if (!$xmlData)
|
2006-12-02 04:36:16 +00:00
|
|
|
{
|
2009-09-17 04:30:25 +00:00
|
|
|
return FALSE;
|
2006-12-02 04:36:16 +00:00
|
|
|
}
|
2012-07-23 02:25:17 +00:00
|
|
|
|
|
|
|
$xmlData = str_replace('content:encoded', 'content_encoded', $xmlData);
|
|
|
|
|
2009-09-17 04:30:25 +00:00
|
|
|
if(!$xml = simplexml_load_string($xmlData))
|
|
|
|
{
|
|
|
|
$this->errors = $this->getErrors($xmlData);
|
2010-02-07 00:44:08 +00:00
|
|
|
return FALSE;
|
2009-09-17 04:30:25 +00:00
|
|
|
};
|
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
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
/**
|
|
|
|
* Advanced XML parser - handles tags with attributes and values
|
2009-11-17 14:50:37 +00:00
|
|
|
* properly.
|
2009-08-27 13:58:28 +00:00
|
|
|
* TODO - filter (see xml_convert_to_array)
|
2010-02-07 00:44:08 +00:00
|
|
|
*
|
2009-08-27 13:58:28 +00:00
|
|
|
* @param SimpleXMLElement $xml
|
|
|
|
* @param string $rec_parent used for recursive calls only
|
2009-11-17 14:50:37 +00:00
|
|
|
* @return array
|
2009-08-27 13:58:28 +00:00
|
|
|
*/
|
|
|
|
function xml2array($xml, $rec_parent = '')
|
|
|
|
{
|
2012-07-23 02:25:17 +00:00
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
$ret = array();
|
2012-07-23 02:25:17 +00:00
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
$tags = get_object_vars($xml);
|
2012-07-23 02:25:17 +00:00
|
|
|
|
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
//remove comments
|
|
|
|
if($this->stripComments && isset($tags['comment']))
|
|
|
|
{
|
|
|
|
unset($tags['comment']);
|
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
//first call
|
|
|
|
if(!$rec_parent)
|
|
|
|
{
|
|
|
|
//$ret = $this->xml2array($xml, true);
|
2010-02-07 00:44:08 +00:00
|
|
|
//repeating code because of the _optForceArray functionality
|
|
|
|
|
2009-08-27 21:50:59 +00:00
|
|
|
if(!is_object($xml))
|
|
|
|
{
|
|
|
|
return array();
|
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
|
|
|
|
$tags = array_keys($tags);
|
2009-08-27 13:58:28 +00:00
|
|
|
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);
|
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
|
|
|
|
$ret = $this->parseArrayTags($ret);
|
2009-09-21 21:53:38 +00:00
|
|
|
$ret = $this->parseStringTags($ret);
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
return ($this->_optAddRoot ? array($xml->getName() => $ret) : $ret);
|
|
|
|
}
|
|
|
|
|
2010-02-07 00:44:08 +00:00
|
|
|
//Recursive calls start here
|
2009-08-27 13:58:28 +00:00
|
|
|
if($tags)
|
|
|
|
{
|
|
|
|
$tags = array_keys($tags);
|
|
|
|
$count_tags = count($tags);
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
//loop through tags
|
|
|
|
foreach ($tags as $tag)
|
|
|
|
{
|
|
|
|
switch($tag)
|
|
|
|
{
|
|
|
|
case '@attributes':
|
|
|
|
$tmp = (array) $xml->attributes();
|
|
|
|
$ret['@attributes'] = $tmp['@attributes'];
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
if($count_tags == 1) //only attributes & possible value
|
|
|
|
{
|
|
|
|
$ret[$this->_optValueKey] = trim((string) $xml);
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
break;
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
case 'comment':
|
|
|
|
$ret[$this->_optValueKey] = trim((string) $xml);
|
|
|
|
$ret['comment'] = $xml->comment;
|
|
|
|
break;
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
//more cases?
|
|
|
|
default:
|
2012-07-23 02:25:17 +00:00
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
$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);
|
2009-09-21 21:53:38 +00:00
|
|
|
$ret[$tag][$i] = $this->parseStringTags($ret[$tag][$i]);
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else //single element
|
|
|
|
{
|
|
|
|
$ret[$tag] = $this->xml2array($xml->{$tag}, $tag);
|
2009-09-21 21:53:38 +00:00
|
|
|
$ret[$tag] = $this->parseStringTags($ret[$tag]);
|
2009-08-27 13:58:28 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-09-21 21:53:38 +00:00
|
|
|
$ret = $this->parseStringTags($ret);
|
2009-08-27 13:58:28 +00:00
|
|
|
return $ret;
|
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
//parse value only
|
|
|
|
$ret = trim((string) $xml);
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
return ($this->_optForceArray ? array($this->_optValueKey => $ret) : $ret);
|
|
|
|
}
|
2006-12-02 04:36:16 +00:00
|
|
|
|
2009-11-26 17:14:07 +00:00
|
|
|
// OLD
|
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];
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2008-01-20 04:46:35 +00:00
|
|
|
}
|
2006-12-02 04:36:16 +00:00
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
|
|
|
|
$xml = $this->parseArrayTags($xml);
|
2009-09-21 21:53:38 +00:00
|
|
|
// $xml = $this->parseStringTags($xml);
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2008-01-20 04:46:35 +00:00
|
|
|
return $xml;
|
2006-12-02 04:36:16 +00:00
|
|
|
}
|
|
|
|
|
2009-09-16 13:01:19 +00:00
|
|
|
|
2009-09-21 21:53:38 +00:00
|
|
|
/**
|
2010-02-07 00:44:08 +00:00
|
|
|
* Convert Array(0) to String based on specified Tags.
|
2009-11-17 10:31:05 +00:00
|
|
|
*
|
|
|
|
* @param array|string $vars
|
2009-11-17 14:50:37 +00:00
|
|
|
* @return string
|
2009-09-21 21:53:38 +00:00
|
|
|
*/
|
|
|
|
function parseStringTags($vars)
|
|
|
|
{
|
2009-09-21 22:22:14 +00:00
|
|
|
if(!$this->stringTags || !is_array($vars))
|
2009-09-21 21:53:38 +00:00
|
|
|
{
|
|
|
|
return $vars;
|
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-09-21 21:53:38 +00:00
|
|
|
foreach($this->stringTags as $vl)
|
2010-02-07 00:44:08 +00:00
|
|
|
{
|
2010-04-15 15:33:20 +00:00
|
|
|
if(isset($vars[$vl]) && isset($vars[$vl][0]))
|
2009-09-21 21:53:38 +00:00
|
|
|
{
|
2010-02-07 00:44:08 +00:00
|
|
|
$vars[$vl] = $vars[$vl][0];
|
|
|
|
}
|
2009-09-21 21:53:38 +00:00
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
|
|
|
|
return $vars;
|
2009-09-21 21:53:38 +00:00
|
|
|
}
|
|
|
|
|
2009-09-16 13:01:19 +00:00
|
|
|
/**
|
|
|
|
* Return as an array, even when a single xml tag value is found
|
2010-02-07 00:44:08 +00:00
|
|
|
* Use setArrayTags() to set which tags are affected.
|
2009-11-17 10:31:05 +00:00
|
|
|
*
|
|
|
|
* @param array $vars
|
2009-09-16 13:01:19 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function parseArrayTags($vars)
|
|
|
|
{
|
|
|
|
|
|
|
|
if(!$this->arrayTags)
|
|
|
|
{
|
|
|
|
return $vars;
|
|
|
|
}
|
|
|
|
|
2009-09-21 21:53:38 +00:00
|
|
|
|
2009-09-16 13:01:19 +00:00
|
|
|
foreach($this->arrayTags as $vl)
|
|
|
|
{
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-09-17 00:13:40 +00:00
|
|
|
if(isset($vars[$vl]) && is_array($vars[$vl]) && !varset($vars[$vl][0]))
|
2009-09-16 13:01:19 +00:00
|
|
|
{
|
|
|
|
|
2010-02-07 00:44:08 +00:00
|
|
|
$vars[$vl] = array($vars[$vl]);
|
|
|
|
}
|
2009-09-16 13:01:19 +00:00
|
|
|
}
|
2012-07-23 02:25:17 +00:00
|
|
|
|
2009-09-16 13:01:19 +00:00
|
|
|
return $vars;
|
|
|
|
}
|
|
|
|
|
2009-08-27 13:58:28 +00:00
|
|
|
/**
|
|
|
|
* Load XML file and parse it (optional)
|
2010-02-07 00:44:08 +00:00
|
|
|
*
|
2009-08-27 13:58:28 +00:00
|
|
|
* @param string $fname local or remote XML source file path
|
2010-02-07 00:44:08 +00:00
|
|
|
* @param boolean|string $parse false - no parse;
|
|
|
|
* true - use xml_convert_to_array();
|
2009-08-27 13:58:28 +00:00
|
|
|
* in any other case - use xml2array()
|
2010-02-07 00:44:08 +00:00
|
|
|
*
|
2009-08-27 13:58:28 +00:00
|
|
|
* @param boolean $replace_constants [optional]
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
function loadXMLfile($fname, $parse = false, $replace_constants = false)
|
2006-12-02 04:36:16 +00:00
|
|
|
{
|
2010-02-16 21:35:09 +00:00
|
|
|
$tp = e107::getParser();
|
2012-08-18 00:53:04 +00:00
|
|
|
|
|
|
|
if($this->_feedUrl !== false)
|
|
|
|
{
|
|
|
|
$fname = $this->_feedUrl;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2012-08-18 00:53:04 +00:00
|
|
|
|
2008-01-20 04:46:35 +00:00
|
|
|
$xml = false;
|
2012-08-18 00:53:04 +00:00
|
|
|
|
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
|
|
|
{
|
2010-02-07 00:44:08 +00:00
|
|
|
$this->xmlFileContents = $tp->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
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-09-06 01:02:01 +00:00
|
|
|
/**
|
2010-02-07 00:44:08 +00:00
|
|
|
* Convert file path for inclusion in XML file.
|
2009-11-17 10:31:05 +00:00
|
|
|
|
|
|
|
* @see e107ExportValue()
|
|
|
|
* @param string $text - callback function
|
|
|
|
* @return string converted file path
|
2009-09-06 01:02:01 +00:00
|
|
|
*/
|
|
|
|
private function replaceFilePaths($text)
|
2009-09-05 23:02:23 +00:00
|
|
|
{
|
|
|
|
$fullpath = e107::getParser()->replaceConstants($text[1]);
|
|
|
|
$this->fileConvertLog[] = $fullpath;
|
|
|
|
$file = basename($fullpath);
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-09-05 23:02:23 +00:00
|
|
|
return $this->filePathDestination.$file;
|
|
|
|
|
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
|
|
|
|
|
2009-09-06 01:02:01 +00:00
|
|
|
/**
|
2009-11-17 10:31:05 +00:00
|
|
|
* Process data values for XML file. If $this->convertFilePaths is TRUE, convert paths
|
|
|
|
*
|
|
|
|
* @see replaceFilePaths()
|
2009-09-06 01:02:01 +00:00
|
|
|
* @param mixed $val
|
2010-02-07 00:44:08 +00:00
|
|
|
* @param string $key key for the current value. Used for exception processing.
|
2009-09-06 01:02:01 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2009-11-17 10:31:05 +00:00
|
|
|
private function e107ExportValue($val, $key = '')
|
2009-08-24 00:58:32 +00:00
|
|
|
{
|
2009-09-10 09:49:01 +00:00
|
|
|
if($key && isset($this->filePathPrepend[$key]))
|
|
|
|
{
|
2010-02-07 00:44:08 +00:00
|
|
|
$val = $this->filePathPrepend[$key].$val;
|
2009-09-10 09:49:01 +00:00
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-09-05 23:02:23 +00:00
|
|
|
if($this->convertFilePaths)
|
|
|
|
{
|
|
|
|
$types = implode("|",$this->convertFileTypes);
|
|
|
|
$val = preg_replace_callback("#({e_.*?\.(".$types."))#i", array($this,'replaceFilePaths'), $val);
|
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-08-31 02:00:51 +00:00
|
|
|
if(is_array($val))
|
2009-08-24 00:58:32 +00:00
|
|
|
{
|
2010-02-07 00:44:08 +00:00
|
|
|
return "<![CDATA[".e107::getArrayStorage()->WriteArray($val,FALSE)."]]>";
|
2009-08-31 02:00:51 +00:00
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-08-31 02:00:51 +00:00
|
|
|
if((strpos($val,"<")!==FALSE) || (strpos($val,">")!==FALSE) || (strpos($val,"&")!==FALSE))
|
|
|
|
{
|
2010-02-07 00:44:08 +00:00
|
|
|
return "<![CDATA[". $val."]]>";
|
2009-08-31 02:00:51 +00:00
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
|
|
|
|
return $val;
|
2009-08-31 02:00:51 +00:00
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-08-31 02:00:51 +00:00
|
|
|
/**
|
|
|
|
* Create an e107 Export File in XML format
|
2010-02-07 00:44:08 +00:00
|
|
|
* Note: If $this->filePathDestination has a value, then the file will be saved there.
|
2009-11-17 10:31:05 +00:00
|
|
|
*
|
2009-08-31 02:00:51 +00:00
|
|
|
* @param array $prefs - see e_core_pref $aliases (eg. core, ipool etc)
|
2009-09-06 01:02:01 +00:00
|
|
|
* @param array $tables - table names without the prefix
|
2009-08-31 02:00:51 +00:00
|
|
|
* @param boolean $debug [optional]
|
2009-11-17 10:31:05 +00:00
|
|
|
* @return string text / file for download
|
2009-08-31 02:00:51 +00:00
|
|
|
*/
|
2009-11-17 10:31:05 +00:00
|
|
|
public function e107Export($xmlprefs, $tables, $debug = FALSE)
|
2009-08-31 02:00:51 +00:00
|
|
|
{
|
|
|
|
require_once(e_ADMIN."ver.php");
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-11-17 10:31:05 +00:00
|
|
|
$text = "<?xml version='1.0' encoding='utf-8' ?".">\n";
|
2009-08-31 02:00:51 +00:00
|
|
|
$text .= "<e107Export version='".$e107info['e107_version']."' timestamp='".time()."' >\n";
|
2010-02-07 00:44:08 +00:00
|
|
|
|
|
|
|
if(varset($xmlprefs)) // Export Core Preferences.
|
|
|
|
{
|
2009-08-31 02:00:51 +00:00
|
|
|
$text .= "\t<prefs>\n";
|
|
|
|
foreach($xmlprefs as $type)
|
2009-08-24 00:58:32 +00:00
|
|
|
{
|
2009-08-31 02:00:51 +00:00
|
|
|
$theprefs = e107::getConfig($type)->getPref();
|
|
|
|
$prefsorted = ksort($theprefs);
|
|
|
|
foreach($theprefs as $key=>$val)
|
2009-08-24 00:58:32 +00:00
|
|
|
{
|
2009-08-31 02:00:51 +00:00
|
|
|
if(isset($val))
|
|
|
|
{
|
|
|
|
$text .= "\t\t<".$type." name='$key'>".$this->e107ExportValue($val)."</".$type.">\n";
|
|
|
|
}
|
2009-08-24 00:58:32 +00:00
|
|
|
}
|
|
|
|
}
|
2009-08-31 02:00:51 +00:00
|
|
|
$text .= "\t</prefs>\n";
|
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-08-31 02:00:51 +00:00
|
|
|
if(varset($tables))
|
|
|
|
{
|
|
|
|
$text .= "\t<database>\n";
|
|
|
|
foreach($tables as $tbl)
|
2009-08-24 00:58:32 +00:00
|
|
|
{
|
2009-08-31 02:00:51 +00:00
|
|
|
$eTable= str_replace(MPREFIX,"",$tbl);
|
|
|
|
e107::getDB()->db_Select($eTable, "*");
|
|
|
|
$text .= "\t<dbTable name='$eTable'>\n";
|
|
|
|
while($row = e107::getDB()-> db_Fetch())
|
2009-08-24 00:58:32 +00:00
|
|
|
{
|
2009-08-31 02:00:51 +00:00
|
|
|
$text .= "\t\t<item>\n";
|
|
|
|
foreach($row as $key=>$val)
|
|
|
|
{
|
2009-09-10 09:49:01 +00:00
|
|
|
$text .= "\t\t\t<field name='".$key."'>".$this->e107ExportValue($val,$key)."</field>\n";
|
2009-08-31 02:00:51 +00:00
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-08-31 02:00:51 +00:00
|
|
|
$text .= "\t\t</item>\n";
|
2009-08-24 00:58:32 +00:00
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
$text .= "\t</dbTable>\n";
|
|
|
|
|
2009-08-24 00:58:32 +00:00
|
|
|
}
|
2009-08-31 02:00:51 +00:00
|
|
|
$text .= "\t</database>\n";
|
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-08-31 02:00:51 +00:00
|
|
|
$text .= "</e107Export>";
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-08-31 02:00:51 +00:00
|
|
|
if($debug==TRUE)
|
2010-02-07 00:44:08 +00:00
|
|
|
{
|
2009-09-05 23:02:23 +00:00
|
|
|
echo "<pre>".htmlentities($text)."</pre>";
|
2010-02-07 00:44:08 +00:00
|
|
|
return TRUE;
|
2009-08-31 02:00:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-05 23:02:23 +00:00
|
|
|
if(!$text)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-09-05 23:02:23 +00:00
|
|
|
$path = e107::getParser()->replaceConstants($this->filePathDestination);
|
|
|
|
if($path)
|
|
|
|
{
|
|
|
|
file_put_contents($path."install.xml",$text,FILE_TEXT);
|
2010-02-07 00:44:08 +00:00
|
|
|
return true;
|
2009-09-05 23:02:23 +00:00
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-08-31 02:00:51 +00:00
|
|
|
header('Content-type: application/xml', TRUE);
|
|
|
|
header("Content-disposition: attachment; filename= e107Export_" . date("Y-m-d").".xml");
|
|
|
|
header("Cache-Control: max-age=30");
|
|
|
|
header("Pragma: public");
|
|
|
|
echo $text;
|
2009-09-05 23:02:23 +00:00
|
|
|
exit;
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-08-24 00:58:32 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-09-01 02:00:56 +00:00
|
|
|
/**
|
|
|
|
* Return an Array of core preferences from e107 XML Dump data
|
2009-11-17 10:31:05 +00:00
|
|
|
*
|
|
|
|
* @param array $XMLData Raw XML e107 Export Data
|
|
|
|
* @param string $prefType [optional] the type of core pref: core|emote|ipool|menu etc.
|
|
|
|
* @return array preference array equivalent to the old $pref global;
|
2009-09-01 02:00:56 +00:00
|
|
|
*/
|
2009-11-17 10:31:05 +00:00
|
|
|
public function e107ImportPrefs($XMLData, $prefType='core')
|
2009-09-01 02:00:56 +00:00
|
|
|
{
|
|
|
|
if(!vartrue($XMLData['prefs'][$prefType]))
|
|
|
|
{
|
2010-04-15 15:33:20 +00:00
|
|
|
return array();
|
2010-02-07 00:44:08 +00:00
|
|
|
}
|
|
|
|
|
2012-01-06 09:49:08 +00:00
|
|
|
//$mes = eMessage::getInstance();
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-09-01 02:00:56 +00:00
|
|
|
$pref = array();
|
|
|
|
foreach($XMLData['prefs'][$prefType] as $val)
|
2010-02-07 00:44:08 +00:00
|
|
|
{
|
2009-09-01 02:00:56 +00:00
|
|
|
$name = $val['@attributes']['name'];
|
2012-01-06 09:49:08 +00:00
|
|
|
// if(strpos($val['@value'], 'array (') === 0)
|
|
|
|
// {
|
|
|
|
// echo '<pre>'.$val['@value'];
|
|
|
|
// echo "\n";
|
|
|
|
// var_dump(e107::getArrayStorage()->ReadArray($val['@value']));
|
|
|
|
// echo $val['@value'].'</pre>';
|
|
|
|
// }
|
|
|
|
$value = strpos($val['@value'], 'array (') === 0 ? e107::getArrayStorage()->ReadArray($val['@value']) : $val['@value'];
|
2009-10-21 11:43:30 +00:00
|
|
|
$pref[$name] = $value;
|
|
|
|
|
2010-02-07 00:44:08 +00:00
|
|
|
// $mes->add("Setting up ".$prefType." Pref [".$name."] => ".$value, E_MESSAGE_DEBUG);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return $pref;
|
2009-09-01 02:00:56 +00:00
|
|
|
}
|
2009-11-26 17:14:07 +00:00
|
|
|
|
2009-08-31 02:00:51 +00:00
|
|
|
/**
|
|
|
|
* Import an e107 XML file into site preferences and DB tables
|
2009-11-17 10:31:05 +00:00
|
|
|
*
|
2009-08-31 02:00:51 +00:00
|
|
|
* @param path $file - e107 XML file path
|
2009-09-03 22:27:32 +00:00
|
|
|
* @param string $mode[optional] - add|replace
|
2010-05-16 11:14:19 +00:00
|
|
|
* @param boolean $noLogs [optional] tells pref handler to disable admin logs when true (install issues)
|
2009-08-31 02:00:51 +00:00
|
|
|
* @param boolean $debug [optional]
|
2010-02-07 00:44:08 +00:00
|
|
|
* @return array with keys 'success' and 'failed' - DB table entry status.
|
2009-08-31 02:00:51 +00:00
|
|
|
*/
|
2010-05-16 11:14:19 +00:00
|
|
|
public function e107Import($file, $mode='replace', $noLogs = false, $debug=FALSE)
|
2009-08-24 00:58:32 +00:00
|
|
|
{
|
2009-08-31 02:00:51 +00:00
|
|
|
|
2010-04-15 15:33:20 +00:00
|
|
|
$xmlArray = $this->loadXMLfile($file, 'advanced');
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-08-31 02:00:51 +00:00
|
|
|
if($debug)
|
2009-08-24 00:58:32 +00:00
|
|
|
{
|
2012-01-06 09:49:08 +00:00
|
|
|
//$message = print_r($xmlArray);
|
|
|
|
echo "<pre>".var_export($xmlArray,TRUE)."</pre>";
|
2009-08-31 02:00:51 +00:00
|
|
|
return;
|
2009-08-24 00:58:32 +00:00
|
|
|
}
|
|
|
|
|
2009-08-31 02:00:51 +00:00
|
|
|
$ret = array();
|
2012-01-06 09:49:08 +00:00
|
|
|
|
2009-09-03 22:27:32 +00:00
|
|
|
if(vartrue($xmlArray['prefs'])) // Save Core Prefs
|
2009-08-24 00:58:32 +00:00
|
|
|
{
|
2009-08-31 02:00:51 +00:00
|
|
|
foreach($xmlArray['prefs'] as $type=>$array)
|
2009-08-24 00:58:32 +00:00
|
|
|
{
|
2012-01-06 09:49:08 +00:00
|
|
|
|
2009-09-01 02:00:56 +00:00
|
|
|
$pArray = $this->e107ImportPrefs($xmlArray,$type);
|
2012-01-06 09:49:08 +00:00
|
|
|
|
|
|
|
if($mode == 'replace') // merge with existing, add new
|
2009-09-03 22:27:32 +00:00
|
|
|
{
|
|
|
|
e107::getConfig($type)->setPref($pArray);
|
|
|
|
}
|
2012-01-06 09:49:08 +00:00
|
|
|
else // 'add' only new prefs
|
2009-09-03 22:27:32 +00:00
|
|
|
{
|
2010-05-16 11:14:19 +00:00
|
|
|
foreach ($pArray as $pname => $pval)
|
2010-04-15 15:33:20 +00:00
|
|
|
{
|
|
|
|
e107::getConfig($type)->add($pname, $pval); // don't parse x/y/z
|
|
|
|
}
|
2009-09-03 22:27:32 +00:00
|
|
|
}
|
2009-09-01 02:00:56 +00:00
|
|
|
|
|
|
|
if($debug == FALSE)
|
2009-08-24 00:58:32 +00:00
|
|
|
{
|
2010-05-16 11:14:19 +00:00
|
|
|
e107::getConfig($type)
|
|
|
|
->setParam('nologs', $noLogs)
|
|
|
|
->save(FALSE,TRUE);
|
2010-02-07 00:44:08 +00:00
|
|
|
}
|
2009-08-24 00:58:32 +00:00
|
|
|
}
|
|
|
|
}
|
2010-05-16 11:14:19 +00:00
|
|
|
|
2009-09-03 22:27:32 +00:00
|
|
|
if(vartrue($xmlArray['database']))
|
2009-08-31 02:00:51 +00:00
|
|
|
{
|
|
|
|
foreach($xmlArray['database']['dbTable'] as $val)
|
|
|
|
{
|
|
|
|
$table = $val['@attributes']['name'];
|
2012-07-18 02:24:34 +00:00
|
|
|
|
|
|
|
if(!isset($val['item']))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
|
2009-08-31 02:00:51 +00:00
|
|
|
foreach($val['item'] as $item)
|
|
|
|
{
|
|
|
|
$insert_array = array();
|
|
|
|
foreach($item['field'] as $f)
|
|
|
|
{
|
|
|
|
$fieldkey = $f['@attributes']['name'];
|
|
|
|
$fieldval = (isset($f['@value'])) ? $f['@value'] : "";
|
2010-02-07 00:44:08 +00:00
|
|
|
|
|
|
|
$insert_array[$fieldkey] = $fieldval;
|
2009-08-31 02:00:51 +00:00
|
|
|
}
|
2009-09-03 22:27:32 +00:00
|
|
|
if(($mode == "replace") && e107::getDB()->db_Replace($table, $insert_array)!==FALSE)
|
2010-02-07 00:44:08 +00:00
|
|
|
{
|
|
|
|
$ret['success'][] = $table;
|
2009-08-31 02:00:51 +00:00
|
|
|
}
|
2009-09-03 22:27:32 +00:00
|
|
|
elseif(($mode == "add") && e107::getDB()->db_Insert($table, $insert_array)!==FALSE)
|
|
|
|
{
|
2010-02-07 00:44:08 +00:00
|
|
|
$ret['success'][] = $table;
|
2009-09-03 22:27:32 +00:00
|
|
|
}
|
2009-08-31 02:00:51 +00:00
|
|
|
else
|
|
|
|
{
|
2010-02-07 00:44:08 +00:00
|
|
|
$ret['failed'][] = $table;
|
2009-08-31 02:00:51 +00:00
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-08-31 02:00:51 +00:00
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
|
|
|
|
return $ret;
|
2009-08-24 00:58:32 +00:00
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
|
|
|
|
|
2009-09-17 04:30:25 +00:00
|
|
|
function getErrors($xml)
|
|
|
|
{
|
|
|
|
libxml_use_internal_errors(true);
|
|
|
|
$sxe = simplexml_load_string($xml);
|
|
|
|
$errors = array();
|
|
|
|
if (!$sxe)
|
2010-02-07 00:44:08 +00:00
|
|
|
{
|
2009-09-17 04:30:25 +00:00
|
|
|
foreach(libxml_get_errors() as $error)
|
|
|
|
{
|
|
|
|
$errors[] = $error->message. "Line:".$error->line." Column:".$error->column;
|
|
|
|
}
|
|
|
|
return $errors;
|
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
return FALSE;
|
2009-09-17 04:30:25 +00:00
|
|
|
}
|
2010-02-07 00:44:08 +00:00
|
|
|
|
|
|
|
|
2009-08-24 00:58:32 +00:00
|
|
|
|
2009-08-31 02:00:51 +00:00
|
|
|
|
2008-01-30 20:33:16 +00:00
|
|
|
}
|