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
2013-03-13 21:54:57 -07:00
class parseXml extends xmlClass // BC with v1.x
{
2014-01-20 04:15:59 -08:00
private $xmlData = array ();
private $counterArray = array ();
2020-12-14 16:21:48 -08:00
2014-01-20 04:15:59 -08:00
2014-01-20 03:26:15 -08:00
function __construct ()
{
$data = debug_backtrace ( true );
2020-12-22 09:36:02 -08:00
$log = e107 :: getLog ();
2014-01-20 03:26:15 -08:00
$log -> addDebug ( 'Deprecated XML Parser Used' );
$log -> addArray ( $data );
2015-03-08 11:12:42 -07:00
$log -> save ( 'DEPRECATED' , E_LOG_NOTICE , '' , false , LOG_TO_ROLLING );
2014-01-20 03:26:15 -08:00
}
2013-03-13 21:54:57 -07:00
function setUrl ( $feed )
{
$this -> setFeedUrl ( $feed );
}
function getRemoteXmlFile ( $address , $timeout = 10 )
{
2014-01-20 03:26:15 -08:00
// $data = $this->getRemoteFile($address, $timeout);
$fl = e107 :: getFile ();
2020-12-08 12:21:12 -08:00
$data = $fl -> getRemoteContent ( $address , [ 'timeout' => $timeout ]);
2014-01-20 03:26:15 -08:00
$this -> xmlLegacyContents = $data ;
return $data ;
}
function parseXmlContents ()
{
2020-12-22 09:36:02 -08:00
$log = e107 :: getLog ();
2014-01-20 03:26:15 -08:00
foreach ( $this -> xmlData as $key => $value )
{
unset ( $this -> xmlData [ $key ]);
}
foreach ( $this -> counterArray as $key => $value )
{
unset ( $this -> counterArray [ $key ]);
}
if ( ! function_exists ( 'xml_parser_create' ))
{
$log -> addDebug ( " No XML source specified " ) -> save ( 'XML' , E_LOG_WARNING );
return FALSE ;
}
if ( ! $this -> xmlLegacyContents )
{
$log -> addDebug ( " No XML source specified " ) -> save ( 'XML' );
return FALSE ;
}
$this -> parser = xml_parser_create ( '' );
xml_set_object ( $this -> parser , $this );
xml_set_element_handler ( $this -> parser , 'startElement' , 'endElement' );
xml_set_character_data_handler ( $this -> parser , 'characterData' );
$array = explode ( " \n " , $this -> xmlLegacyContents );
foreach ( $array as $data )
{
2020-12-14 16:21:48 -08:00
if ( strlen ( $data ) == 4096 )
2014-01-20 03:26:15 -08:00
{
$log -> addDebug ( " The XML cannot be parsed as it is badly formed. " ) -> save ( 'XML' );
return FALSE ;
}
if ( ! xml_parse ( $this -> parser , $data ))
{
$error = sprintf ( 'XML error: %s at line %d, column %d' , xml_error_string ( xml_get_error_code ( $this -> parser )), xml_get_current_line_number ( $this -> parser ), xml_get_current_column_number ( $this -> parser ));
$log -> addDebug ( $error ) -> save ( 'XML' );
2016-10-28 09:09:47 -07:00
if ( e_DEBUG === true )
{
$error .= " \n " . $data ;
$error .= " \n -------------------------------------------- \n \n " ;
$log -> addDebug ( $error ) -> toFile ( 'xmlErrors' , " XML Error Log " , true );
}
2014-01-20 03:26:15 -08:00
return FALSE ;
}
}
xml_parser_free ( $this -> parser );
return $this -> xmlData ;
2013-03-13 21:54:57 -07:00
}
2014-01-20 03:26:15 -08:00
function startElement ( $p , $element , & $attrs )
{
$this -> start_tag = $element ;
$this -> current_tag = strtolower ( $element );
if ( ! array_key_exists ( $this -> current_tag , $this -> counterArray ))
{
$this -> counterArray [ $this -> current_tag ] = 0 ;
$this -> xmlData [ $this -> current_tag ][ $this -> counterArray [ $this -> current_tag ]] = " " ;
}
}
function endElement ( $p , $element )
{
if ( $this -> start_tag == $element )
{
$this -> counterArray [ $this -> current_tag ] ++ ;
}
}
function characterData ( $p , $data )
{
2020-12-10 15:52:48 -08:00
$data = trim ( rtrim ( $data ));
2014-01-20 03:26:15 -08:00
$data = preg_replace ( '/&(?!amp;)/' , '&' , $data );
if ( ! array_key_exists ( $this -> current_tag , $this -> xmlData ))
{
$this -> xmlData [ $this -> current_tag ] = array ();
}
if ( array_key_exists ( $this -> counterArray [ $this -> current_tag ], $this -> xmlData [ $this -> current_tag ]))
{
$this -> xmlData [ $this -> current_tag ] [ $this -> counterArray [ $this -> current_tag ]] .= $data ;
}
else
{
$this -> xmlData [ $this -> current_tag ] [ $this -> counterArray [ $this -> current_tag ]] = $data ;
}
}
2013-03-13 21:54:57 -07: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
2016-05-30 15:19:19 -07:00
public $modifiedPrefsOnly = false ;
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
2020-12-08 12:21:12 -08:00
private $arrayTags ;
2010-02-07 00:44:08 +00:00
2020-12-08 12:21:12 -08:00
private $stringTags ;
2010-02-07 00:44:08 +00:00
2013-05-02 14:20:19 -07:00
private $urlPrefix = 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
*
2020-12-14 16:21:48 -08:00
*//*
function __construct ()
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
}
2020-12-14 16:21:48 -08: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
*
2014-02-25 11:43:28 +02:00
* @ param string $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 )
{
2013-05-16 10:53:53 +03: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
2013-05-02 14:20:19 -07:00
/**
* Set urlPrefix
*
* @ param array $filter
* @ return xmlClass
*/
public function setUrlPrefix ( $url )
{
$this -> urlPrefix = $url ;
return $this ;
}
2012-08-18 00:53:04 +00:00
public function setFeedUrl ( $feed )
{
if ( $feed )
{
$this -> _feedUrl = $feed ;
2014-01-20 03:26:15 -08:00
}
return $this ;
2012-08-18 00:53:04 +00:00
}
2009-08-17 12:48:52 +00:00
2009-08-27 13:58:28 +00:00
/**
2014-01-21 05:12:58 -08:00
* Get Remote XML 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
*/
2013-05-03 21:34:59 -07:00
function getRemoteFile ( $address , $timeout = 10 , $postData = null )
2014-01-21 05:12:58 -08:00
{
2013-06-06 15:25:43 +03:00
$_file = e107 :: getFile ();
$this -> xmlFileContents = $_file -> getRemoteContent ( $address , array ( 'timeout' => $timeout , 'post' => $postData ));
2017-09-14 12:07:30 -07:00
$this -> errors = $_file -> getErrorMessage ();
2013-06-06 15:25:43 +03:00
return $this -> xmlFileContents ;
2013-05-03 21:34:59 -07:00
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 ()
2019-01-18 14:07:59 -08:00
* @ return array | string
2009-08-27 13:58:28 +00:00
*/
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
}
2015-06-16 01:09:27 -07:00
$extendedTypes = array (
'content:encoded' => 'content_encoded' ,
'<media:' => '<media_' ,
'</media:' => '</media_' ,
'<opensearch:' => '<opensearch_' ,
'</opensearch:' => '</opensearch_'
);
$xmlData = str_replace ( array_keys ( $extendedTypes ), array_values ( $extendedTypes ), $xmlData );
2019-01-18 14:07:59 -08:00
if ( strpos ( $xmlData , '<html lang=' ) !== false )
{
$this -> errors = " HTML cannot be parsed as XML " ;
return false ;
}
2016-02-16 13:08:15 -08:00
if ( ! $xml = simplexml_load_string ( $xmlData , 'SimpleXMLElement' , LIBXML_NOCDATA ))
2009-09-17 04:30:25 +00:00
{
$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 )
2013-05-16 19:48:44 +03:00
* FIXME can ' t handle multi - dimensional associative arrays ( e . g . < screnshots >< image >...</ image >< image >...</ image ></ screenshots > to screenshots [ image ] = array ( ... ))
* XXX New parser in testing phase - see e_marketplace :: parse ()
2009-08-27 13:58:28 +00:00
* @ param SimpleXMLElement $xml
* @ param string $rec_parent used for recursive calls only
2020-12-14 16:21:48 -08:00
* @ return array | string
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 )
{
2020-12-14 16:21:48 -08:00
if ( $tag === '@attributes' )
2009-08-27 13:58:28 +00:00
{
$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
2018-02-04 05:57:16 -06:00
if ( self :: is_assoc ( $tags ))
2009-08-27 13:58:28 +00:00
{
$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 )
{
2018-02-05 09:28:57 -06:00
if ( is_int ( $tag )) continue ;
2009-08-27 13:58:28 +00:00
switch ( $tag )
{
case '@attributes' :
$tmp = ( array ) $xml -> attributes ();
$ret [ '@attributes' ] = $tmp [ '@attributes' ];
2010-02-07 00:44:08 +00:00
2018-02-05 09:28:57 -06:00
if ( $count_tags == 1 || [ '@attributes' , 0 ] === $tags ) //only attributes & possible value
2009-08-27 13:58:28 +00:00
{
$ret [ $this -> _optValueKey ] = trim (( string ) $xml );
2018-02-05 09:28:57 -06:00
//return $ret;
2009-08-27 13:58:28 +00:00
}
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 :
2013-06-03 03:05:59 -07:00
//FIXME - commented code breaks parsing of plugin.xml extended and userclass tags and possibly other xml files.
/*
2013-05-16 13:19:17 +03:00
// fix - empty SimpleXMLElement
if ( empty ( $xml -> { $tag }))
{
if ( $this -> arrayTags && in_array ( $tag , $this -> arrayTags ))
{
$ret [ $tag ] = array ();
}
else $ret [ $tag ] = '' ;
break ;
}
2013-06-03 03:05:59 -07:00
*/
2013-06-12 17:20:28 +03:00
$count = count ( $xml -> { $tag });
2009-08-27 13:58:28 +00:00
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
{
2013-05-16 10:53:53 +03:00
if ( isset ( $vars [ $vl ]) && is_array ( $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 ;
}
2012-12-15 16:31:57 -08:00
2012-12-11 00:34:08 -08:00
foreach ( $this -> arrayTags as $p )
2009-09-16 13:01:19 +00:00
{
2012-12-15 16:31:57 -08:00
if ( strpos ( $p , " / " ) !== false )
{
list ( $vl , $sub ) = explode ( " / " , $p );
}
else
{
$vl = $p ;
$sub = false ;
}
2012-12-11 00:34:08 -08:00
if ( $sub )
{
if ( isset ( $vars [ $vl ][ $sub ]) && is_string ( $vars [ $vl ][ $sub ]))
{
$vars [ $vl ][ $sub ] = array ( $vars [ $vl ][ $sub ]);
}
continue ;
}
2013-05-16 13:19:17 +03: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
{
2012-12-11 00:34:08 -08:00
$vars [ $vl ] = array ( $vars [ $vl ]);
2010-02-07 00:44:08 +00:00
}
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 ;
}
2018-02-04 05:57:16 -06:00
/**
* Determine if the provided variable is an associative array
*
* This method is necessary because since PHP 7.2 , get_object_vars () on
* a SimpleXMLElement object began returning sequential arrays , and
* xmlClass :: xml2array () interpreted the sequence as XML tags .
*
* See https :// github . com / e107inc / e107 / issues / 3018 for details .
*
* @ param array $array The variable to check
* @ return boolean true if the provided variable is an associative array ,
* false if it ' s a sequential array or anything else
*/
private static function is_assoc ( $array )
{
if ( ! is_array ( $array ) || array () === $array ) return false ;
return array_keys ( $array ) !== range ( 0 , count ( $array ) - 1 );
}
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 );
2013-11-03 21:46:56 -08:00
$this -> _feedUrl = false ; // clear it to avoid conflicts.
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
{
2013-11-03 21:46:56 -08: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
{
2019-07-09 12:08:52 -07: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
*/
2017-12-17 08:49:17 -08:00
public 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
2016-05-30 15:19:19 -07:00
if ( is_array ( $val ))
{
2020-12-14 16:21:48 -08:00
$val = e107 :: serialize ( $val );
2018-11-15 14:20:08 -08:00
if ( $val === null )
{
return '<![CDATA[array ()]]>' ;
}
2016-05-30 15:19:19 -07: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 (( 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
2017-12-17 08:49:17 -08:00
$val = str_replace ( chr ( 1 ), '{\u0001}' , $val );
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
2017-01-27 18:02:57 -08:00
* @ param array $options [ optional ] debug , return , query
2009-11-17 10:31:05 +00:00
* @ return string text / file for download
2009-08-31 02:00:51 +00:00
*/
2019-07-09 12:08:52 -07:00
public function e107Export ( $xmlprefs , $tables , $plugPrefs = null , $themePrefs = null , $options = array ())
2009-08-31 02:00:51 +00:00
{
2016-12-24 09:34:00 -08:00
// error_reporting(0);
2017-04-27 15:21:20 -07:00
// $e107info = array();
// 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 " ;
2017-04-27 15:21:20 -07:00
$text .= " <e107Export version= \" " . e_VERSION . " \" timestamp= \" " . time () . " \" > \n " ;
2010-02-07 00:44:08 +00:00
2016-05-30 15:19:19 -07:00
$default = array ();
$excludes = array ();
if ( $this -> modifiedPrefsOnly == true )
{
$xmlArray = e107 :: getSingleton ( 'xmlClass' ) -> loadXMLfile ( e_CORE . " xml/default_install.xml " , 'advanced' );
$default = e107 :: getSingleton ( 'xmlClass' ) -> e107ImportPrefs ( $xmlArray , 'core' );
2017-12-17 16:11:09 -08:00
$excludes = array ( 'social_login' , 'replyto_email' , 'replyto_name' , 'siteadminemail' , 'lan_global_list' , 'menuconfig_list' , 'plug_installed' , 'shortcode_legacy_list' , 'siteurl' , 'cookie_name' , 'install_date' , 'wysiwyg' );
2016-05-30 15:19:19 -07:00
}
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 ();
2016-05-30 15:19:19 -07:00
ksort ( $theprefs );
2009-08-31 02:00:51 +00:00
foreach ( $theprefs as $key => $val )
2009-08-24 00:58:32 +00:00
{
2020-12-14 16:21:48 -08:00
if ( $type === 'core' && $this -> modifiedPrefsOnly == true && (( $val == $default [ $key ]) || in_array ( $key , $excludes ) || strpos ( $key , 'e_' ) === 0 ))
2016-05-30 15:19:19 -07:00
{
continue ;
}
2017-01-27 18:02:57 -08:00
elseif ( ! empty ( $options [ 'debug' ]))
2016-05-30 15:19:19 -07:00
{
echo " <div>Original/Modiied <b> " . $key . " </b> " ;
var_dump ( $default [ $key ], $val );
echo " </div> " ;
}
2009-08-31 02:00:51 +00:00
if ( isset ( $val ))
{
2012-12-16 16:02:13 -08:00
$text .= " \t \t < " . $type . " name= \" " . $key . " \" > " . $this -> e107ExportValue ( $val ) . " </ " . $type . " > \n " ;
2009-08-31 02:00:51 +00:00
}
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
2016-12-24 12:41:06 -08:00
if ( ! empty ( $plugPrefs ))
{
$text .= " \t <pluginPrefs> \n " ;
foreach ( $plugPrefs as $plug )
{
$prefs = e107 :: getPlugConfig ( $plug ) -> getPref ();
foreach ( $prefs as $key => $val )
{
if ( isset ( $val ))
{
$text .= " \t \t < " . $plug . " name= \" " . $key . " \" > " . $this -> e107ExportValue ( $val ) . " </ " . $plug . " > \n " ;
}
}
}
$text .= " \t </pluginPrefs> \n " ;
}
2019-07-09 12:08:52 -07:00
if ( ! empty ( $themePrefs ))
{
$text .= " \t <themePrefs> \n " ;
foreach ( $themePrefs as $plug )
{
$prefs = e107 :: getThemeConfig ( $plug ) -> getPref ();
foreach ( $prefs as $key => $val )
{
if ( isset ( $val ))
{
$text .= " \t \t < " . $plug . " name= \" " . $key . " \" > " . $this -> e107ExportValue ( $val ) . " </ " . $plug . " > \n " ;
}
}
}
$text .= " \t </themePrefs> \n " ;
}
2016-12-24 12:41:06 -08:00
2017-01-27 18:02:57 -08:00
if ( ! empty ( $tables ))
2009-08-31 02:00:51 +00:00
{
$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 );
2017-01-27 18:02:57 -08:00
$eQry = ( ! empty ( $options [ 'query' ])) ? $options [ 'query' ] : null ;
2019-06-03 15:27:36 -07:00
e107 :: getDb () -> select ( $eTable , " * " , $eQry );
2012-12-16 16:02:13 -08:00
$text .= " \t <dbTable name= \" " . $eTable . " \" > \n " ;
2020-12-14 16:21:48 -08:00
// $count = 1;
2019-06-03 15:27:36 -07:00
while ( $row = e107 :: getDb () -> fetch ())
2009-08-24 00:58:32 +00:00
{
2016-05-30 15:19:19 -07:00
2020-12-14 16:21:48 -08:00
if ( $this -> convertFilePaths == true && $eTable === 'core_media' && strpos ( $row [ 'media_url' ], '{e_MEDIA' ) !== 0 )
2016-05-30 15:19:19 -07:00
{
continue ;
}
2009-08-31 02:00:51 +00:00
$text .= " \t \t <item> \n " ;
foreach ( $row as $key => $val )
{
2012-12-16 16:02:13 -08: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 " ;
2020-12-14 16:21:48 -08:00
// $count++;
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
2016-12-24 09:34:00 -08:00
2017-01-27 18:02:57 -08:00
if ( ! empty ( $options [ 'return' ]))
2016-12-24 09:34:00 -08:00
{
return $text ;
}
2017-01-27 18:02:57 -08:00
if ( ! empty ( $options [ 'debug' ]))
2010-02-07 00:44:08 +00:00
{
2009-09-05 23:02:23 +00:00
echo " <pre> " . htmlentities ( $text ) . " </pre> " ;
2016-12-24 12:41:06 -08:00
return null ;
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 )
{
2017-01-26 11:10:44 -08:00
$fileName = " install.xml " ;
if ( file_exists ( $path . $fileName ))
{
$fileName = " install_ " . date ( 'Y-m-d' ) . " .xml " ;
}
file_put_contents ( $path . $fileName , $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
2017-01-27 18:02:57 -08:00
$fileName = ( ! empty ( $options [ 'file' ])) ? $options [ 'file' ] : " e107Export_ " . date ( " Y-m-d " ) . " .xml " ;
2009-08-31 02:00:51 +00:00
header ( 'Content-type: application/xml' , TRUE );
2017-01-27 18:02:57 -08:00
header ( " Content-disposition: attachment; filename= " . $fileName );
2009-08-31 02:00:51 +00:00
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
2016-12-25 08:47:48 -08:00
* @ param string $prefType [ optional ] the type of core pref : core | emote | ipool | menu etc or plugin - folder name
* @ param string $mode core | plugin
2009-11-17 10:31:05 +00:00
* @ return array preference array equivalent to the old $pref global ;
2009-09-01 02:00:56 +00:00
*/
2016-12-25 08:47:48 -08:00
public function e107ImportPrefs ( $XMLData , $prefType = 'core' , $mode = 'core' )
2009-09-01 02:00:56 +00:00
{
2016-12-25 08:47:48 -08:00
2019-07-09 12:08:52 -07:00
switch ( $mode )
{
case " plugin " :
$key = 'pluginPrefs' ;
break ;
case " theme " :
$key = 'themePrefs' ;
break ;
case " core " :
default :
2019-09-03 05:09:19 -07:00
$key = 'prefs' ;
2019-07-09 12:08:52 -07:00
}
// $key = ($mode === 'core') ? 'prefs' : 'pluginPrefs';
2016-12-25 08:47:48 -08:00
if ( ! vartrue ( $XMLData [ $key ][ $prefType ]))
2009-09-01 02:00:56 +00:00
{
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 ();
2016-12-25 08:47:48 -08:00
foreach ( $XMLData [ $key ][ $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>';
// }
2015-02-14 23:34:15 -08:00
$value = strpos ( $val [ '@value' ], 'array (' ) === 0 ? e107 :: unserialize ( $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
*
2016-12-16 12:13:52 -08:00
* @ param string $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
*/
2013-07-14 09:45:01 -07:00
public function e107Import ( $file , $mode = 'replace' , $noLogs = false , $debug = FALSE , $sql = null )
2009-08-24 00:58:32 +00:00
{
2009-08-31 02:00:51 +00:00
2013-07-14 09:45:01 -07:00
if ( $sql == null )
{
2019-06-03 15:27:36 -07:00
$sql = e107 :: getDb ();
2013-07-14 09:45:01 -07:00
}
2019-06-03 15:27:36 -07: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> " ;
2017-12-17 08:49:17 -08:00
return null ;
2009-08-24 00:58:32 +00:00
}
2009-08-31 02:00:51 +00:00
$ret = array ();
2016-12-25 08:47:48 -08:00
// ----------------- Save Core Prefs ---------------------
if ( ! empty ( $xmlArray [ '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
2016-12-25 08:47:48 -08:00
// --------------- Save Plugin Prefs ---------------------
if ( ! empty ( $xmlArray [ 'pluginPrefs' ]))
{
foreach ( $xmlArray [ 'pluginPrefs' ] as $type => $array )
{
$pArray = $this -> e107ImportPrefs ( $xmlArray , $type , 'plugin' );
if ( $mode == 'replace' ) // merge with existing, add new
{
e107 :: getPlugConfig ( $type ) -> setPref ( $pArray );
}
else // 'add' only new prefs
{
foreach ( $pArray as $pname => $pval )
{
e107 :: getPlugConfig ( $type ) -> add ( $pname , $pval ); // don't parse x/y/z
}
}
if ( $debug == false )
{
e107 :: getPlugConfig ( $type )
-> setParam ( 'nologs' , $noLogs )
-> save ( FALSE , TRUE );
}
}
}
2019-07-09 12:08:52 -07:00
// --------------- Save Theme Prefs ---------------------
if ( ! empty ( $xmlArray [ 'themePrefs' ]))
{
foreach ( $xmlArray [ 'themePrefs' ] as $type => $array )
{
$pArray = $this -> e107ImportPrefs ( $xmlArray , $type , 'theme' );
if ( $mode == 'replace' ) // merge with existing, add new
{
e107 :: getThemeConfig ( $type ) -> setPref ( $pArray );
}
else // 'add' only new prefs
{
foreach ( $pArray as $pname => $pval )
{
e107 :: getThemeConfig ( $type ) -> add ( $pname , $pval ); // don't parse x/y/z
}
}
if ( $debug == false )
{
e107 :: getThemeConfig ( $type )
-> setParam ( 'nologs' , $noLogs )
-> save ( FALSE , TRUE );
}
}
}
2016-12-25 08:47:48 -08:00
2020-12-17 09:55:51 -08:00
if ( ! empty ( $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' ];
2017-12-17 08:49:17 -08:00
$fieldval = ( isset ( $f [ '@value' ])) ? $this -> e107ImportValue ( $f [ '@value' ]) : " " ;
2010-02-07 00:44:08 +00:00
$insert_array [ $fieldkey ] = $fieldval ;
2016-02-14 12:15:55 -08:00
2009-08-31 02:00:51 +00:00
}
2020-12-14 16:21:48 -08:00
if (( $mode === " replace " ) && $sql -> replace ( $table , $insert_array ) !== FALSE )
2010-02-07 00:44:08 +00:00
{
$ret [ 'success' ][] = $table ;
2009-08-31 02:00:51 +00:00
}
2013-07-14 09:45:01 -07:00
elseif (( $mode == " add " ) && $sql -> insert ( $table , $insert_array ) !== FALSE )
2009-09-03 22:27:32 +00:00
{
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
{
2016-02-11 13:07:59 -08:00
$error = $sql -> getLastErrorText ();
$lastQry = $sql -> getLastQuery ();
2016-12-16 12:13:52 -08:00
if ( is_array ( $lastQry ))
{
$lastQry = $lastQry [ 'PREPARE' ];
}
2016-02-11 13:07:59 -08:00
$ret [ 'failed' ][] = $table . " \n [ " . $error . " ] \n " . $lastQry . " \n \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
}
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
2017-12-17 08:49:17 -08:00
function e107ImportValue ( $val )
{
$val = str_replace ( '{\u0001}' , chr ( 1 ), $val );
return $val ;
}
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
2017-09-14 12:07:30 -07:00
public function getLastErrorMessage ()
{
return $this -> errors ;
}
2013-03-13 21:54:57 -07:00
}
2009-08-31 02:00:51 +00:00
2013-03-13 21:54:57 -07:00
/**
* DEPRECATED XML Class from v1 . x
*/
class XMLParse
{
var $rawXML ;
var $valueArray = array ();
var $keyArray = array ();
var $parsed = array ();
var $index = 0 ;
var $attribKey = 'attributes' ;
var $valueKey = 'value' ;
var $cdataKey = 'cdata' ;
var $isError = false ;
var $error = '' ;
2016-02-14 19:00:12 -08:00
function __construct ( $xml = NULL )
2013-03-13 21:54:57 -07:00
{
$this -> rawXML = $xml ;
$mes = e107 :: getMessage ();
$mes -> addDebug ( " Deprecated class XMLParse used. Please use 'xmlClass' instead " );
}
function parse ( $xml = NULL )
{
if ( ! is_null ( $xml ))
{
$this -> rawXML = $xml ;
}
$this -> isError = false ;
if ( ! $this -> parse_init ())
{
return false ;
}
$this -> index = 0 ;
$this -> parsed = $this -> parse_recurse ();
$this -> status = 'parsing complete' ;
return $this -> parsed ;
}
function parse_recurse ()
{
$found = array ();
$tagCount = array ();
while ( isset ( $this -> valueArray [ $this -> index ]))
{
$tag = $this -> valueArray [ $this -> index ];
$this -> index ++ ;
if ( $tag [ 'type' ] == 'close' )
{
return $found ;
}
if ( $tag [ 'type' ] == 'cdata' )
{
$tag [ 'tag' ] = $this -> cdataKey ;
$tag [ 'type' ] = 'complete' ;
}
$tagName = $tag [ 'tag' ];
if ( isset ( $tagCount [ $tagName ]))
{
if ( $tagCount [ $tagName ] == 1 )
{
$found [ $tagName ] = array ( $found [ $tagName ]);
}
$tagRef =& $found [ $tagName ][ $tagCount [ $tagName ]];
$tagCount [ $tagName ] ++ ;
}
else
{
$tagCount [ $tagName ] = 1 ;
$tagRef =& $found [ $tagName ];
}
switch ( $tag [ 'type' ])
{
case 'open' :
$tagRef = $this -> parse_recurse ();
if ( isset ( $tag [ 'attributes' ]))
{
$tagRef [ $this -> attribKey ] = $tag [ 'attributes' ];
}
if ( isset ( $tag [ 'value' ]))
{
if ( isset ( $tagRef [ $this -> cdataKey ]))
{
$tagRef [ $this -> cdataKey ] = ( array ) $tagRef [ $this -> cdataKey ];
array_unshift ( $tagRef [ $this -> cdataKey ], $tag [ 'value' ]);
}
else
{
$tagRef [ $this -> cdataKey ] = $tag [ 'value' ];
}
}
break ;
case 'complete' :
if ( isset ( $tag [ 'attributes' ]))
{
$tagRef [ $this -> attribKey ] = $tag [ 'attributes' ];
$tagRef =& $tagRef [ $this -> valueKey ];
}
if ( isset ( $tag [ 'value' ]))
{
$tagRef = $tag [ 'value' ];
}
break ;
}
}
return $found ;
}
function parse_init ()
{
$this -> parser = xml_parser_create ();
$parser = $this -> parser ;
xml_parser_set_option ( $parser , XML_OPTION_CASE_FOLDING , 0 );
xml_parser_set_option ( $parser , XML_OPTION_SKIP_WHITE , 1 );
if ( ! $res = ( bool ) xml_parse_into_struct ( $parser , $this -> rawXML , $this -> valueArray , $this -> keyArray ))
{
$this -> isError = true ;
$this -> error = 'error: ' . xml_error_string ( xml_get_error_code ( $parser )) . ' at line ' . xml_get_current_line_number ( $parser );
}
xml_parser_free ( $parser );
return $res ;
}
2008-01-30 20:33:16 +00:00
}
2013-03-13 21:54:57 -07:00