This commit is contained in:
Laurent Destailleur 2022-07-16 10:59:36 +02:00
parent 14eb6c24b8
commit ad2ced727a

View File

@ -51,6 +51,12 @@ class RssParser
private $current_namespace; private $current_namespace;
private $initem;
private $intextinput;
private $incontent;
private $inimage;
private $inchannel;
// For parsing with xmlparser // For parsing with xmlparser
public $stack = array(); // parser stack public $stack = array(); // parser stack
private $_CONTENT_CONSTRUCTS = array('content', 'summary', 'info', 'title', 'tagline', 'copyright'); private $_CONTENT_CONSTRUCTS = array('content', 'summary', 'info', 'title', 'tagline', 'copyright');
@ -254,21 +260,26 @@ class RssParser
return -1; return -1;
} }
$xmlparser = xml_parser_create(''); try {
$xmlparser = xml_parser_create(null);
if (!is_resource($xmlparser) && !is_object($xmlparser)) { if (!is_resource($xmlparser) && !is_object($xmlparser)) {
$this->error = "ErrorFailedToCreateParser"; $this->error = "ErrorFailedToCreateParser";
return -1; return -1;
}
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, false);
xml_parser_free($xmlparser);
$rss = $this;
//var_dump($status.' '.$rss->_format);exit;
} catch (Exception $e) {
$rss = null;
} }
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($status.' '.$rss->_format);exit;
} }
} }
@ -443,7 +454,7 @@ class RssParser
// Loop on each category // Loop on each category
$itemCategory = array(); $itemCategory = array();
if (is_array($item->category)) { if (!empty($item->category) && is_array($item->category)) {
foreach ($item->category as $cat) { foreach ($item->category as $cat) {
$itemCategory[] = (string) $cat; $itemCategory[] = (string) $cat;
} }
@ -514,7 +525,7 @@ class RssParser
* @param array $attrs Attributes of tags * @param array $attrs Attributes of tags
* @return void * @return void
*/ */
public function feed_start_element($p, $element, &$attrs) public function feed_start_element($p, $element, $attrs)
{ {
// phpcs:enable // phpcs:enable
$el = $element = strtolower($element); $el = $element = strtolower($element);
@ -681,9 +692,9 @@ class RssParser
public function append_content($text) public function append_content($text)
{ {
// phpcs:enable // phpcs:enable
if ($this->initem) { if (!empty($this->initem)) {
$this->concat($this->current_item[$this->incontent], $text); $this->concat($this->current_item[$this->incontent], $text);
} elseif ($this->inchannel) { } elseif (!empty($this->inchannel)) {
$this->concat($this->channel[$this->incontent], $text); $this->concat($this->channel[$this->incontent], $text);
} }
} }
@ -700,24 +711,24 @@ class RssParser
if (!$el) { if (!$el) {
return; return;
} }
if ($this->current_namespace) { if (!empty($this->current_namespace)) {
if ($this->initem) { if (!empty($this->initem)) {
$this->concat($this->current_item[$this->current_namespace][$el], $text); $this->concat($this->current_item[$this->current_namespace][$el], $text);
} elseif ($this->inchannel) { } elseif (!empty($this->inchannel)) {
$this->concat($this->channel[$this->current_namespace][$el], $text); $this->concat($this->channel[$this->current_namespace][$el], $text);
} elseif ($this->intextinput) { } elseif (!empty($this->intextinput)) {
$this->concat($this->textinput[$this->current_namespace][$el], $text); $this->concat($this->textinput[$this->current_namespace][$el], $text);
} elseif ($this->inimage) { } elseif (!empty($this->inimage)) {
$this->concat($this->image[$this->current_namespace][$el], $text); $this->concat($this->image[$this->current_namespace][$el], $text);
} }
} else { } else {
if ($this->initem) { if (!empty($this->initem)) {
$this->concat($this->current_item[$el], $text); $this->concat($this->current_item[$el], $text);
} elseif ($this->intextinput) { } elseif (!empty($this->intextinput)) {
$this->concat($this->textinput[$el], $text); $this->concat($this->textinput[$el], $text);
} elseif ($this->inimage) { } elseif (!empty($this->inimage)) {
$this->concat($this->image[$el], $text); $this->concat($this->image[$el], $text);
} elseif ($this->inchannel) { } elseif (!empty($this->inchannel)) {
$this->concat($this->channel[$el], $text); $this->concat($this->channel[$el], $text);
} }
} }