Rss reader use the timeout and proxy if setup

This commit is contained in:
Laurent Destailleur 2011-12-17 12:38:34 +01:00
parent 3687dc8be9
commit 437a6f699e

View File

@ -1,19 +1,19 @@
<?php <?php
/* Copyright (C) 2011 Laurent Destailleur <eldy@users.sourceforge.net> /* Copyright (C) 2011 Laurent Destailleur <eldy@users.sourceforge.net>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
/** /**
* \file htdocs/core/class/rssparser.class.php * \file htdocs/core/class/rssparser.class.php
@ -25,116 +25,150 @@ class RssParser
var $db; var $db;
var $error; var $error;
protected $_format=''; protected $_format='';
protected $_urlRSS; protected $_urlRSS;
protected $_language; protected $_language;
protected $_generator; protected $_generator;
protected $_copyright; protected $_copyright;
protected $_lastbuilddate; protected $_lastbuilddate;
protected $_imageurl; protected $_imageurl;
protected $_link; protected $_link;
protected $_title; protected $_title;
protected $_description; protected $_description;
protected $_lastfetchdate; // Last successful fetch protected $_lastfetchdate; // Last successful fetch
protected $_rssarray=array(); protected $_rssarray=array();
// Accessors // Accessors
public function getFormat() { return $this->_format; } public function getFormat() {
public function getUrlRss() { return $this->_urlRSS; } return $this->_format;
public function getLanguage() { return $this->_language; } }
public function getGenerator() { return $this->_generator; } public function getUrlRss() {
public function getCopyright() { return $this->_copyright; } return $this->_urlRSS;
public function getLastBuildDate() { return $this->_lastbuilddate; } }
public function getImageUrl() { return $this->_imageurl; } public function getLanguage() {
public function getLink() { return $this->_link; } return $this->_language;
public function getTitle() { return $this->_title; } }
public function getDescription() { return $this->_description; } public function getGenerator() {
public function getLastFetchDate() { return $this->_lastfetchdate; } return $this->_generator;
public function getItems() { return $this->_rssarray; } }
public function getCopyright() {
return $this->_copyright;
}
public function getLastBuildDate() {
return $this->_lastbuilddate;
}
public function getImageUrl() {
return $this->_imageurl;
}
public function getLink() {
return $this->_link;
}
public function getTitle() {
return $this->_title;
}
public function getDescription() {
return $this->_description;
}
public function getLastFetchDate() {
return $this->_lastfetchdate;
}
public function getItems() {
return $this->_rssarray;
}
// For parsing with xmlparser // For parsing with xmlparser
var $stack = array(); // parser stack var $stack = array(); // parser stack
var $_CONTENT_CONSTRUCTS = array('content', 'summary', 'info', 'title', 'tagline', 'copyright'); var $_CONTENT_CONSTRUCTS = array('content', 'summary', 'info', 'title', 'tagline', 'copyright');
/** /**
* Constructor * Constructor
* *
* @param DoliDB $db Database handler * @param DoliDB $db Database handler
*/ */
public function RssParser($db) public function RssParser($db)
{ {
$this->db=$db; $this->db=$db;
} }
/** /**
* Parse rss URL * Parse rss URL
* *
* @param urlRSS Url to parse * @param urlRSS Url to parse
* @param maxNb Max nb of records to get (0 for no limit) * @param maxNb Max nb of records to get (0 for no limit)
* @param cachedelay 0=No cache, nb of seconds we accept cache files (cachedir must also be defined) * @param cachedelay 0=No cache, nb of seconds we accept cache files (cachedir must also be defined)
* @param cachedir Directory where to save cache file * @param cachedir Directory where to save cache file
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*/ */
public function parser($urlRSS, $maxNb=0, $cachedelay=60, $cachedir='') public function parser($urlRSS, $maxNb=0, $cachedelay=60, $cachedir='')
{ {
include_once(DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'); global $conf;
$str=''; // This will contain content of feed include_once(DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php');
// Check parameters $str=''; // This will contain content of feed
if (! dol_is_url($urlRSS))
{
$this->error="ErrorBadUrl";
return -1;
}
$this->_urlRSS = $urlRSS; // Check parameters
$newpathofdestfile=$cachedir.'/'.dol_hash($this->_urlRSS); if (! dol_is_url($urlRSS))
$newmask='0644';
//dol_syslog("RssPArser::parser parse url=".$urlRSS." => cache file=".$newpathofdestfile);
$nowgmt = dol_now();
// Search into cache
$foundintocache=0;
if ($cachedelay > 0 && $cachedir)
{ {
$filedate=dol_filemtime($newpathofdestfile); $this->error="ErrorBadUrl";
if ($filedate >= ($nowgmt - $cachedelay)) return -1;
{
//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
{
dol_syslog("RssParser::parser cache file ".$newpathofdestfile." is not found or older than now - cachedelay (".$nowgmt." - ".$cachedelay.") so we can't use it.");
}
} }
// Load file into $str $this->_urlRSS = $urlRSS;
if ($foundintocache) // Cache file found and is not too old $newpathofdestfile=$cachedir.'/'.dol_hash($this->_urlRSS);
{ $newmask='0644';
$str = file_get_contents($newpathofdestfile);
}
else
{
try {
ini_set("user_agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
ini_set("max_execution_time", 10);
$str = file_get_contents($this->_urlRSS);
}
catch (Exception $e) {
print 'Error retrieving URL '.$this->urlRSS.' - '.$e->getMessage();
}
}
// Convert $str into xml //dol_syslog("RssPArser::parser parse url=".$urlRSS." => cache file=".$newpathofdestfile);
if (! empty($conf->global->EXTERNALRSS_USE_SIMPLEXML)) $nowgmt = dol_now();
// Search into cache
$foundintocache=0;
if ($cachedelay > 0 && $cachedir)
{
$filedate=dol_filemtime($newpathofdestfile);
if ($filedate >= ($nowgmt - $cachedelay))
{
//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
{
dol_syslog("RssParser::parser cache file ".$newpathofdestfile." is not found or older than now - cachedelay (".$nowgmt." - ".$cachedelay.") so we can't use it.");
}
}
// 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();
}
}
// Convert $str into xml
if (! empty($conf->global->EXTERNALRSS_USE_SIMPLEXML))
{ {
//print 'xx'.LIBXML_NOCDATA; //print 'xx'.LIBXML_NOCDATA;
libxml_use_internal_errors(false); libxml_use_internal_errors(false);
@ -143,7 +177,9 @@ class RssParser
else else
{ {
$xmlparser=xml_parser_create(''); $xmlparser=xml_parser_create('');
if (!is_resource($xmlparser)) { $this->error="ErrorFailedToCreateParser"; return -1; } if (!is_resource($xmlparser)) {
$this->error="ErrorFailedToCreateParser"; return -1;
}
xml_set_object($xmlparser, $this); xml_set_object($xmlparser, $this);
xml_set_element_handler($xmlparser, 'feed_start_element', 'feed_end_element' ); xml_set_element_handler($xmlparser, 'feed_start_element', 'feed_end_element' );
@ -154,185 +190,191 @@ class RssParser
//var_dump($rss->_format);exit; //var_dump($rss->_format);exit;
} }
// If $rss loaded // If $rss loaded
if ($rss) if ($rss)
{ {
// Save file into cache // Save file into cache
if (empty($foundintocache) && $cachedir) if (empty($foundintocache) && $cachedir)
{ {
dol_syslog("RssParser::parser cache file ".$newpathofdestfile." is saved onto disk."); dol_syslog("RssParser::parser cache file ".$newpathofdestfile." is saved onto disk.");
if (! dol_is_dir($cachedir)) dol_mkdir($cachedir); if (! dol_is_dir($cachedir)) dol_mkdir($cachedir);
$fp = fopen($newpathofdestfile, 'w'); $fp = fopen($newpathofdestfile, 'w');
fwrite($fp, $str); fwrite($fp, $str);
fclose($fp); fclose($fp);
if (! empty($conf->global->MAIN_UMASK)) $newmask=$conf->global->MAIN_UMASK; if (! empty($conf->global->MAIN_UMASK)) $newmask=$conf->global->MAIN_UMASK;
@chmod($newpathofdestfile, octdec($newmask)); @chmod($newpathofdestfile, octdec($newmask));
$this->_lastfetchdate=$nowgmt; $this->_lastfetchdate=$nowgmt;
} }
unset($str); // Free memory unset($str); // Free memory
if (empty($rss->_format)) // If format not detected automatically if (empty($rss->_format)) // If format not detected automatically
{ {
$rss->_format='rss'; $rss->_format='rss';
if (empty($rss->channel)) $rss->_format='atom'; if (empty($rss->channel)) $rss->_format='atom';
} }
$items=array(); $items=array();
// Save description entries // Save description entries
if ($rss->_format == 'rss') if ($rss->_format == 'rss')
{ {
//var_dump($rss); //var_dump($rss);
if (! empty($conf->global->EXTERNALRSS_USE_SIMPLEXML)) if (! empty($conf->global->EXTERNALRSS_USE_SIMPLEXML))
{ {
if (!empty($rss->channel->language)) $this->_language = (string) $rss->channel->language; 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->generator)) $this->_generator = (string) $rss->channel->generator;
if (!empty($rss->channel->copyright)) $this->_copyright = (string) $rss->channel->copyright; 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->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->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->link)) $this->_link = (string) $rss->channel->link;
if (!empty($rss->channel->title)) $this->_title = (string) $rss->channel->title; if (!empty($rss->channel->title)) $this->_title = (string) $rss->channel->title;
if (!empty($rss->channel->description)) $this->_description = (string) $rss->channel->description; if (!empty($rss->channel->description)) $this->_description = (string) $rss->channel->description;
} }
else else
{ {
//var_dump($rss->channel); //var_dump($rss->channel);
if (!empty($rss->channel['language'])) $this->_language = (string) $rss->channel['language']; 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['generator'])) $this->_generator = (string) $rss->channel['generator'];
if (!empty($rss->channel['copyright'])) $this->_copyright = (string) $rss->channel['copyright']; 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['lastbuilddate'])) $this->_lastbuilddate = (string) $rss->channel['lastbuilddate'];
if (!empty($rss->image['url'])) $this->_imageurl = (string) $rss->image['url']; 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['link'])) $this->_link = (string) $rss->channel['link'];
if (!empty($rss->channel['title'])) $this->_title = (string) $rss->channel['title']; if (!empty($rss->channel['title'])) $this->_title = (string) $rss->channel['title'];
if (!empty($rss->channel['description'])) $this->_description = (string) $rss->channel['description']; if (!empty($rss->channel['description'])) $this->_description = (string) $rss->channel['description'];
} }
if (! empty($conf->global->EXTERNALRSS_USE_SIMPLEXML)) $items=$rss->channel->item; // With simplexml if (! empty($conf->global->EXTERNALRSS_USE_SIMPLEXML)) $items=$rss->channel->item; // With simplexml
else $items=$rss->items; // With xmlparse else $items=$rss->items; // With xmlparse
//var_dump($items);exit; //var_dump($items);exit;
} }
else if ($rss->_format == 'atom') else if ($rss->_format == 'atom')
{ {
//var_dump($rss); //var_dump($rss);
if (! empty($conf->global->EXTERNALRSS_USE_SIMPLEXML)) if (! empty($conf->global->EXTERNALRSS_USE_SIMPLEXML))
{ {
if (!empty($rss->generator)) $this->_generator = (string) $rss->generator; if (!empty($rss->generator)) $this->_generator = (string) $rss->generator;
if (!empty($rss->lastbuilddate)) $this->_lastbuilddate = (string) $rss->modified; if (!empty($rss->lastbuilddate)) $this->_lastbuilddate = (string) $rss->modified;
if (!empty($rss->link->href)) $this->_link = (string) $rss->link->href; if (!empty($rss->link->href)) $this->_link = (string) $rss->link->href;
if (!empty($rss->title)) $this->_title = (string) $rss->title; if (!empty($rss->title)) $this->_title = (string) $rss->title;
if (!empty($rss->description)) $this->_description = (string) $rss->description; if (!empty($rss->description)) $this->_description = (string) $rss->description;
} }
else else
{ {
//if (!empty($rss->channel['rss_language'])) $this->_language = (string) $rss->channel['rss_language']; //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['generator'])) $this->_generator = (string) $rss->channel['generator'];
//if (!empty($rss->channel['rss_copyright'])) $this->_copyright = (string) $rss->channel['rss_copyright']; //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->channel['modified'])) $this->_lastbuilddate = (string) $rss->channel['modified'];
//if (!empty($rss->image['rss_url'])) $this->_imageurl = (string) $rss->image['rss_url']; //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['link'])) $this->_link = (string) $rss->channel['link'];
if (!empty($rss->channel['title'])) $this->_title = (string) $rss->channel['title']; 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($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 if (! empty($conf->global->EXTERNALRSS_USE_SIMPLEXML)) {
else $items=$rss->items; // With xmlparse $tmprss=xml2php($rss); $items=$tmprss['entry'];
//var_dump($items);exit; } // With simplexml
} else $items=$rss->items; // With xmlparse
//var_dump($items);exit;
}
$i = 0; $i = 0;
// Loop on each record
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 // Loop on each record
$itemCategory=array(); if (is_array($items))
if (is_array($item->category)) {
{ foreach($items as $item)
foreach ($item->category as $cat) {
{ //var_dump($item);exit;
$itemCategory[] = (string) $cat; if ($rss->_format == 'rss')
} {
} if (! empty($conf->global->EXTERNALRSS_USE_SIMPLEXML))
} {
else if ($rss->_format == 'atom') $itemLink = (string) $item->link;
{ $itemTitle = (string) $item->title;
if (! empty($conf->global->EXTERNALRSS_USE_SIMPLEXML)) $itemDescription = (string) $item->description;
{ $itemPubDate = (string) $item->pubDate;
$itemLink = (string) $item['link']['href']; $itemId = '';
$itemTitle = (string) $item['title']; $itemAuthor = '';
$itemDescription = (string) $item['summary']; }
$itemPubDate = (string) $item['created']; else
$itemId = (string) $item['id']; {
$itemAuthor = (string) ($item['author']?$item['author']:$item['author_name']); $itemLink = (string) $item['link'];
} $itemTitle = (string) $item['title'];
else $itemDescription = (string) $item['description'];
{ $itemPubDate = (string) $item['pubdate'];
$itemLink = (string) $item['link']['href']; $itemId = (string) $item['guid'];
$itemTitle = (string) $item['title']; $itemAuthor = (string) $item['author'];
$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 // Loop on each category
$this->_rssarray[$i] = array( $itemCategory=array();
'link'=>$itemLink, if (is_array($item->category))
'title'=>$itemTitle, {
'description'=>$itemDescription, foreach ($item->category as $cat)
'pubDate'=>$itemPubDate, {
'category'=>$itemCategory, $itemCategory[] = (string) $cat;
'id'=>$itemId, }
'author'=>$itemAuthor); }
//var_dump($this->_rssarray); }
else if ($rss->_format == 'atom')
{
if (! empty($conf->global->EXTERNALRSS_USE_SIMPLEXML))
{
$itemLink = (string) $item['link']['href'];
$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
{
$itemLink = (string) $item['link']['href'];
$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';
$i++; // 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);
if ($i > $maxNb) break; // We get all records we want $i++;
}
return 1; if ($i > $maxNb) break; // We get all records we want
} }
else }
{
$this->error='ErrorFailedToLoadRSSFile'; return 1;
return -1; }
} else
} {
$this->error='ErrorFailedToLoadRSSFile';
return -1;
}
}
/** /**
* Triggered when opened tag is found * Triggered when opened tag is found
* *
* @param $p * @param $p
* @param $element Tag * @param $element Tag
* @param $attrs Attributes of tags * @param $attrs Attributes of tags
*/ */
function feed_start_element($p, $element, &$attrs) function feed_start_element($p, $element, &$attrs)
{ {
$el = $element = strtolower($element); $el = $element = strtolower($element);
@ -381,17 +423,17 @@ class RssParser
// if we're in the default namespace of an RSS feed, // if we're in the default namespace of an RSS feed,
// record textinput or image fields // record textinput or image fields
elseif ( elseif (
$this->_format == 'rss' and $this->_format == 'rss' and
$this->current_namespace == '' and $this->current_namespace == '' and
$el == 'textinput' ) $el == 'textinput' )
{ {
$this->intextinput = true; $this->intextinput = true;
} }
elseif ( elseif (
$this->_format == 'rss' and $this->_format == 'rss' and
$this->current_namespace == '' and $this->current_namespace == '' and
$el == 'image' ) $el == 'image' )
{ {
$this->inimage = true; $this->inimage = true;
} }
@ -414,9 +456,9 @@ class RssParser
{ {
// if tags are inlined, then flatten // if tags are inlined, then flatten
$attrs_str = join(' ', $attrs_str = join(' ',
array_map('map_attrs', array_map('map_attrs',
array_keys($attrs), array_keys($attrs),
array_values($attrs) ) ); array_values($attrs) ) );
$this->append_content("<$element $attrs_str>" ); $this->append_content("<$element $attrs_str>" );
@ -446,12 +488,12 @@ class RssParser
} }
/** /**
* Triggered when CDATA is found * Triggered when CDATA is found
* *
* @param $p * @param $p
* @param $text Tag * @param $text Tag
*/ */
function feed_cdata($p, $text) function feed_cdata($p, $text)
{ {
if ($this->_format == 'atom' and $this->incontent) if ($this->_format == 'atom' and $this->incontent)
@ -464,12 +506,12 @@ class RssParser
} }
} }
/** /**
* Triggered when closed tag is found * Triggered when closed tag is found
* *
* @param $p * @param $p
* @param $el Tag * @param $el Tag
*/ */
function feed_end_element($p, $el) function feed_end_element($p, $el)
{ {
$el = strtolower($el); $el = strtolower($el);
@ -517,12 +559,12 @@ class RssParser
} }
/** /**
* To concat 2 string with no warning if an operand is not defined * To concat 2 string with no warning if an operand is not defined
* *
* @param $str1 * @param $str1
* @param $str2 * @param $str2
*/ */
function concat(&$str1, $str2="") function concat(&$str1, $str2="")
{ {
if (!isset($str1) ) { if (!isset($str1) ) {
@ -531,8 +573,8 @@ class RssParser
$str1 .= $str2; $str1 .= $str2;
} }
/** /**
*/ */
function append_content($text) function append_content($text)
{ {
if ( $this->initem ) { if ( $this->initem ) {
@ -543,9 +585,9 @@ class RssParser
} }
} }
/** /**
* smart append - field and namespace aware * smart append - field and namespace aware
*/ */
function append($el, $text) function append($el, $text)
{ {
if (!$el) { if (!$el) {
@ -591,51 +633,51 @@ class RssParser
*/ */
function xml2php($xml) function xml2php($xml)
{ {
$fils = 0; $fils = 0;
$tab = false; $tab = false;
$array = array(); $array = array();
foreach($xml->children() as $key => $value) foreach($xml->children() as $key => $value)
{ {
$child = xml2php($value); $child = xml2php($value);
//To deal with the attributes //To deal with the attributes
foreach($value->attributes() as $ak=>$av) foreach($value->attributes() as $ak=>$av)
{ {
$child[$ak] = (string) $av; $child[$ak] = (string) $av;
} }
//Let see if the new child is not in the array //Let see if the new child is not in the array
if($tab==false && in_array($key,array_keys($array))) if($tab==false && in_array($key,array_keys($array)))
{ {
//If this element is already in the array we will create an indexed array //If this element is already in the array we will create an indexed array
$tmp = $array[$key]; $tmp = $array[$key];
$array[$key] = NULL; $array[$key] = NULL;
$array[$key][] = $tmp; $array[$key][] = $tmp;
$array[$key][] = $child; $array[$key][] = $child;
$tab = true; $tab = true;
} }
elseif($tab == true) elseif($tab == true)
{ {
//Add an element in an existing array //Add an element in an existing array
$array[$key][] = $child; $array[$key][] = $child;
} }
else else
{ {
//Add a simple element //Add a simple element
$array[$key] = $child; $array[$key] = $child;
} }
$fils++; $fils++;
} }
if($fils==0) if($fils==0)
{ {
return (string) $xml; return (string) $xml;
} }
return $array; return $array;
} }