2011-08-28 17:29:01 +02:00
< ? php
2012-08-27 18:04:00 +02:00
/* Copyright ( C ) 2011 - 2012 Laurent Destailleur < eldy @ users . sourceforge . net >
2011-08-28 17:29:01 +02:00
*
2011-12-17 12:38:34 +01:00
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2011-12-17 12:38:34 +01:00
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http :// www . gnu . org / licenses />.
*/
2011-08-28 17:29:01 +02:00
/**
* \file htdocs / core / class / rssparser . class . php
* \ingroup core
2012-08-27 18:04:00 +02:00
* \brief File of class to parse RSS feeds
*/
/**
* Class to parse RSS files
2011-08-28 17:29:01 +02:00
*/
class RssParser
{
var $db ;
var $error ;
2012-03-19 17:18:11 +01:00
private $_format = '' ;
private $_urlRSS ;
private $_language ;
private $_generator ;
private $_copyright ;
private $_lastbuilddate ;
private $_imageurl ;
private $_link ;
private $_title ;
private $_description ;
private $_lastfetchdate ; // Last successful fetch
private $_rssarray = array ();
2011-12-17 12:38:34 +01:00
2012-08-28 12:38:07 +02:00
// For parsing with xmlparser
var $stack = array (); // parser stack
var $_CONTENT_CONSTRUCTS = array ( 'content' , 'summary' , 'info' , 'title' , 'tagline' , 'copyright' );
/**
* Constructor
*
* @ param DoliDB $db Database handler
*/
public function __construct ( $db )
{
$this -> db = $db ;
}
2012-08-27 18:04:00 +02:00
2012-02-21 00:18:48 +01:00
/**
* getFormat
*
* @ return string
*/
2012-08-27 18:04:00 +02:00
public function getFormat ()
{
2011-12-17 12:38:34 +01:00
return $this -> _format ;
}
2012-08-27 18:04:00 +02:00
2012-02-21 00:18:48 +01:00
/**
* getUrlRss
*
* @ return string
*/
2012-08-27 18:04:00 +02:00
public function getUrlRss ()
{
2011-12-17 12:38:34 +01:00
return $this -> _urlRSS ;
}
2012-02-21 00:18:48 +01:00
/**
* getLanguage
*
* @ return string
*/
2012-08-27 18:04:00 +02:00
public function getLanguage ()
{
2011-12-17 12:38:34 +01:00
return $this -> _language ;
}
2012-02-21 00:18:48 +01:00
/**
* getGenerator
*
* @ return string
*/
2012-08-27 18:04:00 +02:00
public function getGenerator ()
{
2011-12-17 12:38:34 +01:00
return $this -> _generator ;
}
2012-02-21 00:18:48 +01:00
/**
* getCopyright
*
* @ return string
*/
2012-08-27 18:04:00 +02:00
public function getCopyright ()
{
2011-12-17 12:38:34 +01:00
return $this -> _copyright ;
}
2012-02-21 00:18:48 +01:00
/**
* getLastBuildDate
*
* @ return string
*/
2012-08-27 18:04:00 +02:00
public function getLastBuildDate ()
{
2011-12-17 12:38:34 +01:00
return $this -> _lastbuilddate ;
}
2012-02-21 00:18:48 +01:00
/**
* getImageUrl
*
* @ return string
*/
2012-08-27 18:04:00 +02:00
public function getImageUrl ()
{
2011-12-17 12:38:34 +01:00
return $this -> _imageurl ;
}
2012-02-21 00:18:48 +01:00
/**
* getLink
*
* @ return string
*/
2012-08-27 18:04:00 +02:00
public function getLink ()
{
2011-12-17 12:38:34 +01:00
return $this -> _link ;
}
2012-02-21 00:18:48 +01:00
/**
* getTitle
*
* @ return string
*/
2012-08-27 18:04:00 +02:00
public function getTitle ()
{
2011-12-17 12:38:34 +01:00
return $this -> _title ;
}
2012-02-21 00:18:48 +01:00
/**
* getDescription
*
* @ return string
*/
2012-08-27 18:04:00 +02:00
public function getDescription ()
{
2011-12-17 12:38:34 +01:00
return $this -> _description ;
}
2012-02-21 00:18:48 +01:00
/**
* getLastFetchDate
*
* @ return string
*/
2012-08-27 18:04:00 +02:00
public function getLastFetchDate ()
{
2011-12-17 12:38:34 +01:00
return $this -> _lastfetchdate ;
}
2012-02-21 00:18:48 +01:00
/**
* getItems
*
* @ return string
*/
2012-08-27 18:04:00 +02:00
public function getItems ()
2011-12-17 12:38:34 +01:00
{
2012-08-27 18:04:00 +02:00
return $this -> _rssarray ;
2011-12-17 12:38:34 +01:00
}
/**
* Parse rss URL
*
2012-02-21 00:18:48 +01:00
* @ param string $urlRSS Url to parse
* @ param int $maxNb Max nb of records to get ( 0 for no limit )
* @ param int $cachedelay 0 = No cache , nb of seconds we accept cache files ( cachedir must also be defined )
2014-04-23 15:05:00 +02:00
* @ param string $cachedir Directory where to save cache file
2012-02-21 00:18:48 +01:00
* @ return int < 0 if KO , > 0 if OK
2011-12-17 12:38:34 +01:00
*/
public function parser ( $urlRSS , $maxNb = 0 , $cachedelay = 60 , $cachedir = '' )
{
global $conf ;
2012-08-23 02:04:35 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2011-12-17 12:38:34 +01:00
2013-06-28 17:43:30 +02:00
$rss = '' ;
2011-12-17 12:38:34 +01:00
$str = '' ; // This will contain content of feed
// Check parameters
if ( ! dol_is_url ( $urlRSS ))
{
$this -> error = " ErrorBadUrl " ;
return - 1 ;
}
$this -> _urlRSS = $urlRSS ;
2014-10-18 16:08:15 +02:00
$newpathofdestfile = $cachedir . '/' . dol_hash ( $this -> _urlRSS , 3 ); // Force md5 hash (does not contains special chars)
2011-12-17 12:38:34 +01:00
$newmask = '0644' ;
//dol_syslog("RssPArser::parser parse url=".$urlRSS." => cache file=".$newpathofdestfile);
$nowgmt = dol_now ();
// Search into cache
$foundintocache = 0 ;
2011-08-28 17:29:01 +02:00
if ( $cachedelay > 0 && $cachedir )
{
2011-12-17 12:38:34 +01:00
$filedate = dol_filemtime ( $newpathofdestfile );
2011-08-28 17:29:01 +02:00
if ( $filedate >= ( $nowgmt - $cachedelay ))
2011-12-17 12:38:34 +01:00
{
//dol_syslog("RssParser::parser cache file ".$newpathofdestfile." is not older than now - cachedelay (".$nowgmt." - ".$cachedelay.") so we use it.");
$foundintocache = 1 ;
$this -> _lastfetchdate = $filedate ;
}
else
{
2013-06-28 17:43:30 +02:00
dol_syslog ( get_class ( $this ) . " ::parser cache file " . $newpathofdestfile . " is not found or older than now - cachedelay ( " . $nowgmt . " - " . $cachedelay . " ) so we can't use it. " );
2011-12-17 12:38:34 +01:00
}
}
// Load file into $str
if ( $foundintocache ) // Cache file found and is not too old
{
$str = file_get_contents ( $newpathofdestfile );
}
else
{
try {
ini_set ( " user_agent " , " Dolibarr ERP-CRM RSS reader " );
ini_set ( " max_execution_time " , $conf -> global -> MAIN_USE_RESPONSE_TIMEOUT );
ini_set ( " default_socket_timeout " , $conf -> global -> MAIN_USE_RESPONSE_TIMEOUT );
$opts = array ( 'http' => array ( 'method' => " GET " ));
if ( ! empty ( $conf -> global -> MAIN_USE_CONNECT_TIMEOUT )) $opts [ 'http' ][ 'timeout' ] = $conf -> global -> MAIN_USE_CONNECT_TIMEOUT ;
if ( ! empty ( $conf -> global -> MAIN_PROXY_USE )) $opts [ 'http' ][ 'proxy' ] = 'tcp://' . $conf -> global -> MAIN_PROXY_HOST . ':' . $conf -> global -> MAIN_PROXY_PORT ;
//var_dump($opts);exit;
$context = stream_context_create ( $opts );
$str = file_get_contents ( $this -> _urlRSS , false , $context );
}
catch ( Exception $e ) {
print 'Error retrieving URL ' . $this -> urlRSS . ' - ' . $e -> getMessage ();
}
}
2013-06-28 17:43:30 +02:00
if ( $str !== false )
2011-08-28 17:29:01 +02:00
{
2013-06-28 17:43:30 +02:00
// Convert $str into xml
if ( ! empty ( $conf -> global -> EXTERNALRSS_USE_SIMPLEXML ))
{
//print 'xx'.LIBXML_NOCDATA;
libxml_use_internal_errors ( false );
$rss = simplexml_load_string ( $str , " SimpleXMLElement " , LIBXML_NOCDATA );
}
else
{
$xmlparser = xml_parser_create ( '' );
if ( ! is_resource ( $xmlparser )) {
$this -> error = " ErrorFailedToCreateParser " ; return - 1 ;
}
2014-09-27 16:00:11 +02:00
2013-06-28 17:43:30 +02:00
xml_set_object ( $xmlparser , $this );
xml_set_element_handler ( $xmlparser , 'feed_start_element' , 'feed_end_element' );
xml_set_character_data_handler ( $xmlparser , 'feed_cdata' );
$status = xml_parse ( $xmlparser , $str );
xml_parser_free ( $xmlparser );
$rss = $this ;
//var_dump($rss->_format);exit;
}
2011-08-28 17:29:01 +02:00
}
2014-09-27 16:00:11 +02:00
2011-12-17 12:38:34 +01:00
// If $rss loaded
if ( $rss )
{
// Save file into cache
if ( empty ( $foundintocache ) && $cachedir )
{
2013-06-28 17:43:30 +02:00
dol_syslog ( get_class ( $this ) . " ::parser cache file " . $newpathofdestfile . " is saved onto disk. " );
2011-12-17 12:38:34 +01:00
if ( ! dol_is_dir ( $cachedir )) dol_mkdir ( $cachedir );
$fp = fopen ( $newpathofdestfile , 'w' );
2011-08-28 17:29:01 +02:00
fwrite ( $fp , $str );
fclose ( $fp );
2011-12-17 12:38:34 +01:00
if ( ! empty ( $conf -> global -> MAIN_UMASK )) $newmask = $conf -> global -> MAIN_UMASK ;
@ chmod ( $newpathofdestfile , octdec ( $newmask ));
$this -> _lastfetchdate = $nowgmt ;
}
unset ( $str ); // Free memory
if ( empty ( $rss -> _format )) // If format not detected automatically
{
$rss -> _format = 'rss' ;
if ( empty ( $rss -> channel )) $rss -> _format = 'atom' ;
}
$items = array ();
// Save description entries
if ( $rss -> _format == 'rss' )
{
//var_dump($rss);
if ( ! empty ( $conf -> global -> EXTERNALRSS_USE_SIMPLEXML ))
{
if ( ! empty ( $rss -> channel -> language )) $this -> _language = ( string ) $rss -> channel -> language ;
if ( ! empty ( $rss -> channel -> generator )) $this -> _generator = ( string ) $rss -> channel -> generator ;
if ( ! empty ( $rss -> channel -> copyright )) $this -> _copyright = ( string ) $rss -> channel -> copyright ;
if ( ! empty ( $rss -> channel -> lastbuilddate )) $this -> _lastbuilddate = ( string ) $rss -> channel -> lastbuilddate ;
if ( ! empty ( $rss -> channel -> image -> url [ 0 ])) $this -> _imageurl = ( string ) $rss -> channel -> image -> url [ 0 ];
if ( ! empty ( $rss -> channel -> link )) $this -> _link = ( string ) $rss -> channel -> link ;
if ( ! empty ( $rss -> channel -> title )) $this -> _title = ( string ) $rss -> channel -> title ;
if ( ! empty ( $rss -> channel -> description )) $this -> _description = ( string ) $rss -> channel -> description ;
}
else
{
//var_dump($rss->channel);
if ( ! empty ( $rss -> channel [ 'language' ])) $this -> _language = ( string ) $rss -> channel [ 'language' ];
if ( ! empty ( $rss -> channel [ 'generator' ])) $this -> _generator = ( string ) $rss -> channel [ 'generator' ];
if ( ! empty ( $rss -> channel [ 'copyright' ])) $this -> _copyright = ( string ) $rss -> channel [ 'copyright' ];
if ( ! empty ( $rss -> channel [ 'lastbuilddate' ])) $this -> _lastbuilddate = ( string ) $rss -> channel [ 'lastbuilddate' ];
if ( ! empty ( $rss -> image [ 'url' ])) $this -> _imageurl = ( string ) $rss -> image [ 'url' ];
if ( ! empty ( $rss -> channel [ 'link' ])) $this -> _link = ( string ) $rss -> channel [ 'link' ];
if ( ! empty ( $rss -> channel [ 'title' ])) $this -> _title = ( string ) $rss -> channel [ 'title' ];
if ( ! empty ( $rss -> channel [ 'description' ])) $this -> _description = ( string ) $rss -> channel [ 'description' ];
}
if ( ! empty ( $conf -> global -> EXTERNALRSS_USE_SIMPLEXML )) $items = $rss -> channel -> item ; // With simplexml
else $items = $rss -> items ; // With xmlparse
//var_dump($items);exit;
}
else if ( $rss -> _format == 'atom' )
{
//var_dump($rss);
if ( ! empty ( $conf -> global -> EXTERNALRSS_USE_SIMPLEXML ))
{
if ( ! empty ( $rss -> generator )) $this -> _generator = ( string ) $rss -> generator ;
if ( ! empty ( $rss -> lastbuilddate )) $this -> _lastbuilddate = ( string ) $rss -> modified ;
if ( ! empty ( $rss -> link -> href )) $this -> _link = ( string ) $rss -> link -> href ;
if ( ! empty ( $rss -> title )) $this -> _title = ( string ) $rss -> title ;
if ( ! empty ( $rss -> description )) $this -> _description = ( string ) $rss -> description ;
}
else
{
//if (!empty($rss->channel['rss_language'])) $this->_language = (string) $rss->channel['rss_language'];
if ( ! empty ( $rss -> channel [ 'generator' ])) $this -> _generator = ( string ) $rss -> channel [ 'generator' ];
//if (!empty($rss->channel['rss_copyright'])) $this->_copyright = (string) $rss->channel['rss_copyright'];
if ( ! empty ( $rss -> channel [ 'modified' ])) $this -> _lastbuilddate = ( string ) $rss -> channel [ 'modified' ];
//if (!empty($rss->image['rss_url'])) $this->_imageurl = (string) $rss->image['rss_url'];
if ( ! empty ( $rss -> channel [ 'link' ])) $this -> _link = ( string ) $rss -> channel [ 'link' ];
if ( ! empty ( $rss -> channel [ 'title' ])) $this -> _title = ( string ) $rss -> channel [ 'title' ];
//if (!empty($rss->channel['rss_description'])) $this->_description = (string) $rss->channel['rss_description'];
}
if ( ! empty ( $conf -> global -> EXTERNALRSS_USE_SIMPLEXML )) {
$tmprss = xml2php ( $rss ); $items = $tmprss [ 'entry' ];
} // With simplexml
else $items = $rss -> items ; // With xmlparse
//var_dump($items);exit;
}
$i = 0 ;
// Loop on each record
if ( is_array ( $items ))
{
foreach ( $items as $item )
{
//var_dump($item);exit;
if ( $rss -> _format == 'rss' )
{
if ( ! empty ( $conf -> global -> EXTERNALRSS_USE_SIMPLEXML ))
{
$itemLink = ( string ) $item -> link ;
$itemTitle = ( string ) $item -> title ;
$itemDescription = ( string ) $item -> description ;
$itemPubDate = ( string ) $item -> pubDate ;
$itemId = '' ;
$itemAuthor = '' ;
}
else
{
$itemLink = ( string ) $item [ 'link' ];
$itemTitle = ( string ) $item [ 'title' ];
$itemDescription = ( string ) $item [ 'description' ];
$itemPubDate = ( string ) $item [ 'pubdate' ];
$itemId = ( string ) $item [ 'guid' ];
$itemAuthor = ( string ) $item [ 'author' ];
}
// Loop on each category
$itemCategory = array ();
if ( is_array ( $item -> category ))
{
foreach ( $item -> category as $cat )
{
$itemCategory [] = ( string ) $cat ;
}
}
}
else if ( $rss -> _format == 'atom' )
{
if ( ! empty ( $conf -> global -> EXTERNALRSS_USE_SIMPLEXML ))
{
2015-06-24 18:29:29 +02:00
$itemLink = ( isset ( $item [ 'link' ][ 'href' ]) ? ( string ) $item [ 'link' ][ 'href' ] : '' );
2011-12-17 12:38:34 +01:00
$itemTitle = ( string ) $item [ 'title' ];
$itemDescription = ( string ) $item [ 'summary' ];
$itemPubDate = ( string ) $item [ 'created' ];
$itemId = ( string ) $item [ 'id' ];
$itemAuthor = ( string ) ( $item [ 'author' ] ? $item [ 'author' ] : $item [ 'author_name' ]);
}
else
{
2015-06-24 18:29:29 +02:00
$itemLink = ( isset ( $item [ 'link' ][ 'href' ]) ? ( string ) $item [ 'link' ][ 'href' ] : '' );
2011-12-17 12:38:34 +01:00
$itemTitle = ( string ) $item [ 'title' ];
$itemDescription = ( string ) $item [ 'summary' ];
$itemPubDate = ( string ) $item [ 'created' ];
$itemId = ( string ) $item [ 'id' ];
$itemAuthor = ( string ) ( $item [ 'author' ] ? $item [ 'author' ] : $item [ 'author_name' ]);
}
}
else print 'ErrorBadFeedFormat' ;
// Add record to result array
$this -> _rssarray [ $i ] = array (
'link' => $itemLink ,
'title' => $itemTitle ,
'description' => $itemDescription ,
'pubDate' => $itemPubDate ,
'category' => $itemCategory ,
'id' => $itemId ,
'author' => $itemAuthor );
//var_dump($this->_rssarray);
$i ++ ;
if ( $i > $maxNb ) break ; // We get all records we want
}
}
return 1 ;
}
else
{
$this -> error = 'ErrorFailedToLoadRSSFile' ;
return - 1 ;
}
}
/**
* Triggered when opened tag is found
*
2012-02-21 00:18:48 +01:00
* @ param string $p Start
2012-01-10 01:31:06 +01:00
* @ param string $element Tag
2014-09-27 16:00:11 +02:00
* @ param array $attrs Attributes of tags
2012-01-10 01:31:06 +01:00
* @ return void
2011-12-17 12:38:34 +01:00
*/
2011-08-28 17:29:01 +02:00
function feed_start_element ( $p , $element , & $attrs )
{
$el = $element = strtolower ( $element );
$attrs = array_change_key_case ( $attrs , CASE_LOWER );
// check for a namespace, and split if found
$ns = false ;
2012-02-21 00:18:48 +01:00
if ( strpos ( $element , ':' ))
{
2011-09-20 11:40:27 +02:00
list ( $ns , $el ) = explode ( ':' , $element , 2 );
2011-08-28 17:29:01 +02:00
}
2012-02-21 00:18:48 +01:00
if ( $ns and $ns != 'rdf' )
{
2011-08-28 17:29:01 +02:00
$this -> current_namespace = $ns ;
}
// if feed type isn't set, then this is first element of feed identify feed from root element
if ( empty ( $this -> _format ))
{
if ( $el == 'rdf' ) {
$this -> _format = 'rss' ;
$this -> feed_version = '1.0' ;
}
elseif ( $el == 'rss' ) {
$this -> _format = 'rss' ;
$this -> feed_version = $attrs [ 'version' ];
}
elseif ( $el == 'feed' ) {
$this -> _format = 'atom' ;
$this -> feed_version = $attrs [ 'version' ];
$this -> inchannel = true ;
}
return ;
}
if ( $el == 'channel' )
{
$this -> inchannel = true ;
}
elseif ( $el == 'item' or $el == 'entry' )
{
$this -> initem = true ;
if ( isset ( $attrs [ 'rdf:about' ]) ) {
$this -> current_item [ 'about' ] = $attrs [ 'rdf:about' ];
}
}
// if we're in the default namespace of an RSS feed,
// record textinput or image fields
elseif (
2011-12-17 12:38:34 +01:00
$this -> _format == 'rss' and
$this -> current_namespace == '' and
$el == 'textinput' )
2011-08-28 17:29:01 +02:00
{
$this -> intextinput = true ;
}
elseif (
2011-12-17 12:38:34 +01:00
$this -> _format == 'rss' and
$this -> current_namespace == '' and
$el == 'image' )
2011-08-28 17:29:01 +02:00
{
$this -> inimage = true ;
}
2011-09-20 12:30:56 +02:00
// handle atom content constructs
2011-08-28 17:29:01 +02:00
elseif ( $this -> _format == 'atom' and in_array ( $el , $this -> _CONTENT_CONSTRUCTS ) )
{
// avoid clashing w/ RSS mod_content
if ( $el == 'content' ) {
$el = 'atom_content' ;
}
$this -> incontent = $el ;
}
// if inside an Atom content construct (e.g. content or summary) field treat tags as text
elseif ( $this -> _format == 'atom' and $this -> incontent )
{
// if tags are inlined, then flatten
2012-01-10 01:31:06 +01:00
$attrs_str = join ( ' ' , array_map ( 'map_attrs' , array_keys ( $attrs ), array_values ( $attrs )));
2011-08-28 17:29:01 +02:00
2012-02-21 00:18:48 +01:00
$this -> append_content ( " < $element $attrs_str > " );
2011-08-28 17:29:01 +02:00
2011-09-20 11:40:27 +02:00
array_unshift ( $this -> stack , $el );
2011-08-28 17:29:01 +02:00
}
// Atom support many links per containging element.
// Magpie treats link elements of type rel='alternate'
// as being equivalent to RSS's simple link element.
//
elseif ( $this -> _format == 'atom' and $el == 'link' )
{
2015-02-03 13:05:28 +01:00
if ( isset ( $attrs [ 'rel' ]) && $attrs [ 'rel' ] == 'alternate' )
2011-08-28 17:29:01 +02:00
{
$link_el = 'link' ;
}
else {
$link_el = 'link_' . $attrs [ 'rel' ];
}
$this -> append ( $link_el , $attrs [ 'href' ]);
}
// set stack[0] to current element
else {
array_unshift ( $this -> stack , $el );
}
}
2011-12-17 12:38:34 +01:00
/**
* Triggered when CDATA is found
*
2012-02-21 00:18:48 +01:00
* @ param string $p P
2012-01-10 01:31:06 +01:00
* @ param string $text Tag
* @ return void
2011-12-17 12:38:34 +01:00
*/
2011-09-03 02:27:02 +02:00
function feed_cdata ( $p , $text )
{
2011-08-28 17:29:01 +02:00
if ( $this -> _format == 'atom' and $this -> incontent )
{
2011-09-20 11:40:27 +02:00
$this -> append_content ( $text );
2011-08-28 17:29:01 +02:00
}
2012-02-21 00:18:48 +01:00
else
{
2011-08-28 17:29:01 +02:00
$current_el = join ( '_' , array_reverse ( $this -> stack ));
$this -> append ( $current_el , $text );
}
}
2011-12-17 12:38:34 +01:00
/**
* Triggered when closed tag is found
*
2012-02-21 00:18:48 +01:00
* @ param string $p P
2012-01-10 01:31:06 +01:00
* @ param string $el Tag
* @ return void
2011-12-17 12:38:34 +01:00
*/
2011-09-03 02:27:02 +02:00
function feed_end_element ( $p , $el )
{
2011-08-28 17:29:01 +02:00
$el = strtolower ( $el );
2012-02-21 00:18:48 +01:00
if ( $el == 'item' or $el == 'entry' )
2011-08-28 17:29:01 +02:00
{
$this -> items [] = $this -> current_item ;
$this -> current_item = array ();
$this -> initem = false ;
}
elseif ( $this -> _format == 'rss' and $this -> current_namespace == '' and $el == 'textinput' )
{
$this -> intextinput = false ;
}
elseif ( $this -> _format == 'rss' and $this -> current_namespace == '' and $el == 'image' )
{
$this -> inimage = false ;
}
elseif ( $this -> _format == 'atom' and in_array ( $el , $this -> _CONTENT_CONSTRUCTS ) )
{
$this -> incontent = false ;
}
elseif ( $el == 'channel' or $el == 'feed' )
{
$this -> inchannel = false ;
}
elseif ( $this -> _format == 'atom' and $this -> incontent ) {
// balance tags properly
// note: i don't think this is actually neccessary
if ( $this -> stack [ 0 ] == $el )
{
$this -> append_content ( " </ $el > " );
}
else {
$this -> append_content ( " < $el /> " );
}
2011-09-20 11:40:27 +02:00
array_shift ( $this -> stack );
2011-08-28 17:29:01 +02:00
}
else {
2011-09-20 11:40:27 +02:00
array_shift ( $this -> stack );
2011-08-28 17:29:01 +02:00
}
$this -> current_namespace = false ;
}
2011-12-17 12:38:34 +01:00
/**
* To concat 2 string with no warning if an operand is not defined
*
2014-09-27 16:00:11 +02:00
* @ param string $str1 Str1
2012-01-10 01:31:06 +01:00
* @ param string $str2 Str2
* @ return string String cancatenated
2011-12-17 12:38:34 +01:00
*/
2011-09-03 02:27:02 +02:00
function concat ( & $str1 , $str2 = " " )
{
2011-08-28 17:29:01 +02:00
if ( ! isset ( $str1 ) ) {
$str1 = " " ;
}
$str1 .= $str2 ;
}
2011-12-17 12:38:34 +01:00
/**
2012-01-10 01:31:06 +01:00
* Enter description here ...
*
* @ param string $text Text
* @ return void
2011-12-17 12:38:34 +01:00
*/
2011-09-03 02:27:02 +02:00
function append_content ( $text )
{
2011-08-28 17:29:01 +02:00
if ( $this -> initem ) {
2011-09-20 11:40:27 +02:00
$this -> concat ( $this -> current_item [ $this -> incontent ], $text );
2011-08-28 17:29:01 +02:00
}
elseif ( $this -> inchannel ) {
2011-09-20 11:40:27 +02:00
$this -> concat ( $this -> channel [ $this -> incontent ], $text );
2011-08-28 17:29:01 +02:00
}
}
2011-12-17 12:38:34 +01:00
/**
* smart append - field and namespace aware
2012-01-10 01:31:06 +01:00
*
* @ param string $el El
* @ param string $text Text
* @ return void
2011-12-17 12:38:34 +01:00
*/
2011-09-03 02:27:02 +02:00
function append ( $el , $text )
{
2011-08-28 17:29:01 +02:00
if ( ! $el ) {
return ;
}
if ( $this -> current_namespace )
{
if ( $this -> initem ) {
2012-01-10 01:31:06 +01:00
$this -> concat ( $this -> current_item [ $this -> current_namespace ][ $el ], $text );
2011-08-28 17:29:01 +02:00
}
elseif ( $this -> inchannel ) {
2012-01-10 01:31:06 +01:00
$this -> concat ( $this -> channel [ $this -> current_namespace ][ $el ], $text );
2011-08-28 17:29:01 +02:00
}
elseif ( $this -> intextinput ) {
2012-01-10 01:31:06 +01:00
$this -> concat ( $this -> textinput [ $this -> current_namespace ][ $el ], $text );
2011-08-28 17:29:01 +02:00
}
elseif ( $this -> inimage ) {
2012-01-10 01:31:06 +01:00
$this -> concat ( $this -> image [ $this -> current_namespace ][ $el ], $text );
2011-08-28 17:29:01 +02:00
}
}
else {
if ( $this -> initem ) {
2012-01-10 01:31:06 +01:00
$this -> concat ( $this -> current_item [ $el ], $text );
2011-08-28 17:29:01 +02:00
}
elseif ( $this -> intextinput ) {
2012-01-10 01:31:06 +01:00
$this -> concat ( $this -> textinput [ $el ], $text );
2011-08-28 17:29:01 +02:00
}
elseif ( $this -> inimage ) {
2012-01-10 01:31:06 +01:00
$this -> concat ( $this -> image [ $el ], $text );
2011-08-28 17:29:01 +02:00
}
elseif ( $this -> inchannel ) {
2012-01-10 01:31:06 +01:00
$this -> concat ( $this -> channel [ $el ], $text );
2011-08-28 17:29:01 +02:00
}
}
}
}
/**
* Function to convert an XML object into an array
2012-01-10 01:31:06 +01:00
*
2014-04-23 15:11:26 +02:00
* @ param SimpleXMLElement $xml Xml
2012-01-10 01:31:06 +01:00
* @ return void
2011-08-28 17:29:01 +02:00
*/
function xml2php ( $xml )
{
2011-12-17 12:38:34 +01:00
$fils = 0 ;
$tab = false ;
$array = array ();
foreach ( $xml -> children () as $key => $value )
{
$child = xml2php ( $value );
//To deal with the attributes
foreach ( $value -> attributes () as $ak => $av )
{
$child [ $ak ] = ( string ) $av ;
}
//Let see if the new child is not in the array
2018-09-25 08:16:05 +02:00
if ( $tab === false && in_array ( $key , array_keys ( $array )))
2011-12-17 12:38:34 +01:00
{
//If this element is already in the array we will create an indexed array
$tmp = $array [ $key ];
$array [ $key ] = NULL ;
$array [ $key ][] = $tmp ;
$array [ $key ][] = $child ;
$tab = true ;
}
2018-09-25 08:16:05 +02:00
elseif ( $tab === true )
2011-12-17 12:38:34 +01:00
{
//Add an element in an existing array
$array [ $key ][] = $child ;
}
else
{
//Add a simple element
$array [ $key ] = $child ;
}
$fils ++ ;
}
if ( $fils == 0 )
{
return ( string ) $xml ;
}
return $array ;
2011-08-28 17:29:01 +02:00
}