202 public function parser($urlRSS, $maxNb = 0, $cachedelay = 60, $cachedir =
'')
206 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
207 include_once DOL_DOCUMENT_ROOT.
'/core/lib/geturl.lib.php';
214 $this->error =
"ErrorBadUrl";
218 $this->_urlRSS = $urlRSS;
219 $newpathofdestfile = $cachedir.
'/'.
dol_hash($this->_urlRSS, 3);
227 if ($cachedelay > 0 && $cachedir) {
229 if ($filedate >= ($nowgmt - $cachedelay)) {
233 $this->_lastfetchdate = $filedate;
235 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.");
240 if ($foundintocache) {
241 $str = file_get_contents($newpathofdestfile);
244 $result =
getURLContent($this->_urlRSS,
'GET',
'', 1, array(), array(
'http',
'https'), 0);
246 if (!empty($result[
'content'])) {
247 $str = $result[
'content'];
248 } elseif (!empty($result[
'curl_error_msg'])) {
249 $this->error =
'Error retrieving URL '.$this->_urlRSS.
' - '.$result[
'curl_error_msg'];
253 $this->error =
'Error retrieving URL '.$this->_urlRSS.
' - '.$e->getMessage();
258 if ($str !==
false) {
262 libxml_use_internal_errors(
false);
263 if (LIBXML_VERSION < 20900) {
266 libxml_disable_entity_loader(
true);
269 $rss = simplexml_load_string($str,
"SimpleXMLElement", LIBXML_NOCDATA);
271 if (!function_exists(
'xml_parser_create')) {
272 $this->error =
'Function xml_parser_create are not supported by your PHP';
277 $xmlparser = xml_parser_create(
null);
279 if (!is_resource($xmlparser) && !is_object($xmlparser)) {
280 $this->error =
"ErrorFailedToCreateParser";
284 xml_set_object($xmlparser, $this);
285 xml_set_element_handler($xmlparser,
'feed_start_element',
'feed_end_element');
286 xml_set_character_data_handler($xmlparser,
'feed_cdata');
288 $status = xml_parse($xmlparser, $str,
false);
290 xml_parser_free($xmlparser);
302 if (empty($foundintocache) && $cachedir) {
303 dol_syslog(get_class($this).
"::parser cache file ".$newpathofdestfile.
" is saved onto disk.");
307 $fp = fopen($newpathofdestfile,
'w');
313 $this->_lastfetchdate = $nowgmt;
315 print
'Error, failed to open file '.$newpathofdestfile.
' for write';
321 if (empty($rss->_format)) {
322 $rss->_format =
'rss';
323 if (empty($rss->channel)) {
324 $rss->_format =
'atom';
331 if ($rss->_format ==
'rss') {
334 if (!empty($rss->channel->language)) {
335 $this->_language =
sanitizeVal((
string) $rss->channel->language);
337 if (!empty($rss->channel->generator)) {
338 $this->_generator =
sanitizeVal((
string) $rss->channel->generator);
340 if (!empty($rss->channel->copyright)) {
341 $this->_copyright =
sanitizeVal((
string) $rss->channel->copyright);
343 if (!empty($rss->channel->lastbuilddate)) {
344 $this->_lastbuilddate =
sanitizeVal((
string) $rss->channel->lastbuilddate);
346 if (!empty($rss->channel->image->url[0])) {
347 $this->_imageurl =
sanitizeVal((
string) $rss->channel->image->url[0]);
349 if (!empty($rss->channel->link)) {
350 $this->_link =
sanitizeVal((
string) $rss->channel->link);
352 if (!empty($rss->channel->title)) {
353 $this->_title =
sanitizeVal((
string) $rss->channel->title);
355 if (!empty($rss->channel->description)) {
356 $this->_description =
sanitizeVal((
string) $rss->channel->description);
360 if (!empty($rss->channel[
'language'])) {
361 $this->_language =
sanitizeVal((
string) $rss->channel[
'language']);
363 if (!empty($rss->channel[
'generator'])) {
364 $this->_generator =
sanitizeVal((
string) $rss->channel[
'generator']);
366 if (!empty($rss->channel[
'copyright'])) {
367 $this->_copyright =
sanitizeVal((
string) $rss->channel[
'copyright']);
369 if (!empty($rss->channel[
'lastbuilddate'])) {
370 $this->_lastbuilddate =
sanitizeVal((
string) $rss->channel[
'lastbuilddate']);
372 if (!empty($rss->image[
'url'])) {
373 $this->_imageurl =
sanitizeVal((
string) $rss->image[
'url']);
375 if (!empty($rss->channel[
'link'])) {
376 $this->_link =
sanitizeVal((
string) $rss->channel[
'link']);
378 if (!empty($rss->channel[
'title'])) {
379 $this->_title =
sanitizeVal((
string) $rss->channel[
'title']);
381 if (!empty($rss->channel[
'description'])) {
382 $this->_description =
sanitizeVal((
string) $rss->channel[
'description']);
387 $items = $rss->channel->item;
389 $items = $rss->items;
392 } elseif ($rss->_format ==
'atom') {
395 if (!empty($rss->generator)) {
396 $this->_generator =
sanitizeVal((
string) $rss->generator);
398 if (!empty($rss->lastbuilddate)) {
399 $this->_lastbuilddate =
sanitizeVal((
string) $rss->modified);
401 if (!empty($rss->link->href)) {
402 $this->_link =
sanitizeVal((
string) $rss->link->href);
404 if (!empty($rss->title)) {
407 if (!empty($rss->description)) {
408 $this->_description =
sanitizeVal((
string) $rss->description);
412 if (!empty($rss->channel[
'generator'])) {
413 $this->_generator =
sanitizeVal((
string) $rss->channel[
'generator']);
416 if (!empty($rss->channel[
'modified'])) {
417 $this->_lastbuilddate =
sanitizeVal((
string) $rss->channel[
'modified']);
420 if (!empty($rss->channel[
'link'])) {
421 $this->_link =
sanitizeVal((
string) $rss->channel[
'link']);
423 if (!empty($rss->channel[
'title'])) {
424 $this->_title =
sanitizeVal((
string) $rss->channel[
'title']);
428 if (!empty($rss->channel)) {
434 $items = $tmprss[
'entry'];
437 $items = $rss->items;
445 if (is_array($items)) {
446 foreach ($items as $item) {
448 if ($rss->_format ==
'rss') {
452 $itemDescription =
sanitizeVal((
string) $item->description);
453 $itemPubDate =
sanitizeVal((
string) $item->pubDate);
459 $itemDescription =
sanitizeVal((
string) $item[
'description']);
460 $itemPubDate =
sanitizeVal((
string) $item[
'pubdate']);
462 $itemAuthor =
sanitizeVal((
string) ($item[
'author'] ??
''));
466 $itemCategory = array();
467 if (!empty($item->category) && is_array($item->category)) {
468 foreach ($item->category as $cat) {
469 $itemCategory[] = (string) $cat;
472 } elseif ($rss->_format ==
'atom') {
474 $itemLink = (isset($item[
'link']) ?
sanitizeVal((
string) $item[
'link']) :
'');
477 $itemPubDate =
sanitizeVal((
string) $item[
'created']);
479 $itemAuthor =
sanitizeVal((
string) ($item[
'author'] ? $item[
'author'] : $item[
'author_name']));
481 $itemLink = (isset($item[
'link']) ?
sanitizeVal((
string) $item[
'link']) :
'');
484 $itemPubDate =
sanitizeVal((
string) $item[
'created']);
486 $itemAuthor =
sanitizeVal((
string) ($item[
'author'] ? $item[
'author'] : $item[
'author_name']));
488 $itemCategory = array();
490 $itemCategory = array();
493 $itemDescription =
'';
497 print
'ErrorBadFeedFormat';
501 $this->_rssarray[$i] = array(
504 'description'=>$itemDescription,
505 'pubDate'=>$itemPubDate,
506 'category'=>$itemCategory,
508 'author'=>$itemAuthor
522 $this->error =
'ErrorFailedToLoadRSSFile';
541 $el = $element = strtolower($element);
542 $attrs = array_change_key_case($attrs, CASE_LOWER);
546 if (strpos($element,
':')) {
547 list($ns, $el) = explode(
':', $element, 2);
549 if ($ns and $ns !=
'rdf') {
550 $this->current_namespace = $ns;
554 if (empty($this->_format)) {
556 $this->_format =
'rss';
557 $this->feed_version =
'1.0';
558 } elseif ($el ==
'rss') {
559 $this->_format =
'rss';
560 $this->feed_version = $attrs[
'version'];
561 } elseif ($el ==
'feed') {
562 $this->_format =
'atom';
563 $this->feed_version = $attrs[
'version'];
564 $this->inchannel =
true;
569 if ($el ==
'channel') {
570 $this->inchannel =
true;
571 } elseif ($el ==
'item' || $el ==
'entry') {
572 $this->initem =
true;
573 if (isset($attrs[
'rdf:about'])) {
574 $this->current_item[
'about'] = $attrs[
'rdf:about'];
576 } elseif ($this->_format ==
'rss' && $this->current_namespace ==
'' && $el ==
'textinput') {
579 $this->intextinput =
true;
580 } elseif ($this->_format ==
'rss' && $this->current_namespace ==
'' && $el ==
'image') {
581 $this->inimage =
true;
582 } elseif ($this->_format ==
'atom' && in_array($el, $this->_CONTENT_CONSTRUCTS)) {
585 if ($el ==
'content') {
586 $el =
'atom_content';
589 $this->incontent = $el;
590 } elseif ($this->_format ==
'atom' && $this->incontent) {
593 $attrs_str = join(
' ', array_map(
'map_attrs', array_keys($attrs), array_values($attrs)));
597 array_unshift($this->stack, $el);
598 } elseif ($this->_format ==
'atom' && $el ==
'link') {
602 if (isset($attrs[
'rel']) && $attrs[
'rel'] ==
'alternate') {
604 } elseif (!isset($attrs[
'rel'])) {
607 $link_el =
'link_'.$attrs[
'rel'];
610 $this->
append($link_el, $attrs[
'href']);
613 array_unshift($this->stack, $el);
648 $el = strtolower($el);
650 if ($el ==
'item' or $el ==
'entry') {
651 $this->items[] = $this->current_item;
652 $this->current_item = array();
653 $this->initem =
false;
654 } elseif ($this->_format ==
'rss' and $this->current_namespace ==
'' and $el ==
'textinput') {
655 $this->intextinput =
false;
656 } elseif ($this->_format ==
'rss' and $this->current_namespace ==
'' and $el ==
'image') {
657 $this->inimage =
false;
658 } elseif ($this->_format ==
'atom' and in_array($el, $this->_CONTENT_CONSTRUCTS)) {
659 $this->incontent =
false;
660 } elseif ($el ==
'channel' or $el ==
'feed') {
661 $this->inchannel =
false;
662 } elseif ($this->_format ==
'atom' and $this->incontent) {
665 if ($this->stack[0] == $el) {
671 array_shift($this->stack);
673 array_shift($this->stack);
676 $this->current_namespace =
false;
725 if (!empty($this->current_namespace)) {
726 if (!empty($this->initem)) {
727 $this->
concat($this->current_item[$this->current_namespace][$el], $text);
728 } elseif (!empty($this->inchannel)) {
729 $this->
concat($this->channel[$this->current_namespace][$el], $text);
730 } elseif (!empty($this->intextinput)) {
731 $this->
concat($this->textinput[$this->current_namespace][$el], $text);
732 } elseif (!empty($this->inimage)) {
733 $this->
concat($this->image[$this->current_namespace][$el], $text);
736 if (!empty($this->initem)) {
737 $this->
concat($this->current_item[$el], $text);
738 } elseif (!empty($this->intextinput)) {
739 $this->
concat($this->textinput[$el], $text);
740 } elseif (!empty($this->inimage)) {
741 $this->
concat($this->image[$el], $text);
742 } elseif (!empty($this->inchannel)) {
743 $this->
concat($this->channel[$el], $text);