280 public function parser($urlRSS, $maxNb = 0, $cachedelay = 60, $cachedir =
'')
282 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
283 include_once DOL_DOCUMENT_ROOT.
'/core/lib/geturl.lib.php';
290 $this->error =
"ErrorBadUrl";
294 $this->_urlRSS = $urlRSS;
295 $newpathofdestfile = $cachedir.
'/'.
dol_hash($this->_urlRSS,
'3');
303 if ($cachedelay > 0 && $cachedir) {
305 if ($filedate >= ($nowgmt - $cachedelay)) {
309 $this->_lastfetchdate = $filedate;
311 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.");
316 if ($foundintocache) {
317 $str = file_get_contents($newpathofdestfile);
320 $result =
getURLContent($this->_urlRSS,
'GET',
'', 1, array(), array(
'http',
'https'), 0);
322 if (!empty($result[
'content'])) {
323 $str = $result[
'content'];
324 } elseif (!empty($result[
'curl_error_msg'])) {
325 $this->error =
'Error retrieving URL '.$this->_urlRSS.
' - '.$result[
'curl_error_msg'];
329 $this->error =
'Error retrieving URL '.$this->_urlRSS.
' - '.$e->getMessage();
334 if ($str !==
false) {
338 libxml_use_internal_errors(
false);
339 if (LIBXML_VERSION < 20900) {
343 libxml_disable_entity_loader(
true);
346 $rss = simplexml_load_string($str,
"SimpleXMLElement", LIBXML_NOCDATA);
348 if (!function_exists(
'xml_parser_create')) {
349 $this->error =
'Function xml_parser_create are not supported by your PHP';
355 $xmlparser = xml_parser_create(
null);
357 xml_parser_set_option($xmlparser, XML_OPTION_CASE_FOLDING, 0);
358 xml_parser_set_option($xmlparser, XML_OPTION_SKIP_WHITE, 1);
359 xml_parser_set_option($xmlparser, XML_OPTION_TARGET_ENCODING,
"UTF-8");
362 if (!is_resource($xmlparser) && !is_object($xmlparser)) {
363 $this->error =
"ErrorFailedToCreateParser";
367 xml_set_object($xmlparser, $this);
369 xml_set_element_handler($xmlparser,
'feed_start_element',
'feed_end_element');
371 xml_set_character_data_handler($xmlparser,
'feed_cdata');
373 $status = xml_parse($xmlparser, $str,
false);
375 xml_parser_free($xmlparser);
388 if (empty($foundintocache) && $cachedir) {
389 dol_syslog(get_class($this).
"::parser cache file ".$newpathofdestfile.
" is saved onto disk.");
393 $fp = fopen($newpathofdestfile,
'w');
399 $this->_lastfetchdate = $nowgmt;
401 print
'Error, failed to open file '.$newpathofdestfile.
' for write';
407 if (empty($rss->_format)) {
408 $rss->_format =
'rss';
409 if (empty($rss->channel)) {
410 $rss->_format =
'atom';
417 if ($rss->_format ==
'rss') {
420 if (!empty($rss->channel->language)) {
421 $this->_language =
sanitizeVal((
string) $rss->channel->language);
423 if (!empty($rss->channel->generator)) {
424 $this->_generator =
sanitizeVal((
string) $rss->channel->generator);
426 if (!empty($rss->channel->copyright)) {
427 $this->_copyright =
sanitizeVal((
string) $rss->channel->copyright);
429 if (!empty($rss->channel->lastbuilddate)) {
430 $this->_lastbuilddate =
sanitizeVal((
string) $rss->channel->lastbuilddate);
432 if (!empty($rss->channel->image->url[0])) {
433 $this->_imageurl =
sanitizeVal((
string) $rss->channel->image->url[0]);
435 if (!empty($rss->channel->link)) {
436 $this->_link =
sanitizeVal((
string) $rss->channel->link);
438 if (!empty($rss->channel->title)) {
439 $this->_title =
sanitizeVal((
string) $rss->channel->title);
441 if (!empty($rss->channel->description)) {
442 $this->_description =
sanitizeVal((
string) $rss->channel->description);
446 if (!empty($rss->channel[
'language'])) {
447 $this->_language =
sanitizeVal((
string) $rss->channel[
'language']);
449 if (!empty($rss->channel[
'generator'])) {
450 $this->_generator =
sanitizeVal((
string) $rss->channel[
'generator']);
452 if (!empty($rss->channel[
'copyright'])) {
453 $this->_copyright =
sanitizeVal((
string) $rss->channel[
'copyright']);
455 if (!empty($rss->channel[
'lastbuilddate'])) {
456 $this->_lastbuilddate =
sanitizeVal((
string) $rss->channel[
'lastbuilddate']);
458 if (!empty($rss->image[
'url'])) {
459 $this->_imageurl =
sanitizeVal((
string) $rss->image[
'url']);
461 if (!empty($rss->channel[
'link'])) {
462 $this->_link =
sanitizeVal((
string) $rss->channel[
'link']);
464 if (!empty($rss->channel[
'title'])) {
465 $this->_title =
sanitizeVal((
string) $rss->channel[
'title']);
467 if (!empty($rss->channel[
'description'])) {
468 $this->_description =
sanitizeVal((
string) $rss->channel[
'description']);
473 $items = $rss->channel->item;
475 $items = $rss->items;
478 } elseif ($rss->_format ==
'atom') {
481 if (!empty($rss->generator)) {
482 $this->_generator =
sanitizeVal((
string) $rss->generator);
484 if (!empty($rss->lastbuilddate)) {
485 $this->_lastbuilddate =
sanitizeVal((
string) $rss->modified);
487 if (!empty($rss->link->href)) {
488 $this->_link =
sanitizeVal((
string) $rss->link->href);
490 if (!empty($rss->title)) {
493 if (!empty($rss->description)) {
494 $this->_description =
sanitizeVal((
string) $rss->description);
498 if (!empty($rss->channel[
'generator'])) {
499 $this->_generator =
sanitizeVal((
string) $rss->channel[
'generator']);
502 if (!empty($rss->channel[
'modified'])) {
503 $this->_lastbuilddate =
sanitizeVal((
string) $rss->channel[
'modified']);
506 if (!empty($rss->channel[
'link'])) {
507 $this->_link =
sanitizeVal((
string) $rss->channel[
'link']);
509 if (!empty($rss->channel[
'title'])) {
510 $this->_title =
sanitizeVal((
string) $rss->channel[
'title']);
514 if (!empty($rss->channel)) {
520 $items = $tmprss[
'entry'];
523 $items = $rss->items;
531 if (is_array($items)) {
532 foreach ($items as $item) {
534 if ($rss->_format ==
'rss') {
538 $itemDescription =
sanitizeVal((
string) $item->description);
539 $itemPubDate =
sanitizeVal((
string) $item->pubDate);
545 $itemDescription =
sanitizeVal((
string) $item[
'description']);
546 $itemPubDate =
sanitizeVal((
string) $item[
'pubdate']);
548 $itemAuthor =
sanitizeVal((
string) ($item[
'author'] ??
''));
552 $itemCategory = array();
553 if (!empty($item->category) && is_array($item->category)) {
554 foreach ($item->category as $cat) {
555 $itemCategory[] = (string) $cat;
558 } elseif ($rss->_format ==
'atom') {
559 $itemLink = (isset($item[
'link']) ?
sanitizeVal((
string) $item[
'link']) :
'');
562 $itemPubDate =
sanitizeVal((
string) $item[
'created']);
564 $itemAuthor =
sanitizeVal((
string) ($item[
'author'] ? $item[
'author'] : $item[
'author_name']));
565 $itemCategory = array();
569 $itemDescription =
'';
573 $itemCategory = array();
574 print
'ErrorBadFeedFormat';
578 $this->_rssarray[$i] = array(
580 'title' => $itemTitle,
581 'description' => $itemDescription,
582 'pubDate' => $itemPubDate,
583 'category' => $itemCategory,
585 'author' => $itemAuthor
599 $this->error =
'ErrorFailedToLoadRSSFile';
618 $el = $element = strtolower($element);
619 $attrs = array_change_key_case($attrs, CASE_LOWER);
623 if (strpos($element,
':')) {
624 list($ns, $el) = explode(
':', $element, 2);
626 if ($ns and $ns !=
'rdf') {
627 $this->current_namespace = $ns;
631 if (empty($this->_format)) {
633 $this->_format =
'rss';
634 $this->feed_version =
'1.0';
635 } elseif ($el ==
'rss') {
636 $this->_format =
'rss';
637 $this->feed_version = $attrs[
'version'];
638 } elseif ($el ==
'feed') {
639 $this->_format =
'atom';
640 $this->feed_version = $attrs[
'version'];
641 $this->inchannel =
true;
646 if ($el ==
'channel') {
647 $this->inchannel =
true;
648 } elseif ($el ==
'item' || $el ==
'entry') {
649 $this->initem =
true;
650 if (isset($attrs[
'rdf:about'])) {
651 $this->current_item[
'about'] = $attrs[
'rdf:about'];
653 } elseif ($this->_format ==
'rss' && $this->current_namespace ==
'' && $el ==
'textinput') {
656 $this->intextinput =
true;
657 } elseif ($this->_format ==
'rss' && $this->current_namespace ==
'' && $el ==
'image') {
658 $this->inimage =
true;
659 } elseif ($this->_format ==
'atom' && in_array($el, $this->_CONTENT_CONSTRUCTS)) {
662 if ($el ==
'content') {
663 $el =
'atom_content';
666 $this->incontent = $el;
667 } elseif ($this->_format ==
'atom' && $this->incontent) {
670 $attrs_str = implode(
' ', array_map(
'rss_map_attrs', array_keys($attrs), array_values($attrs)));
674 array_unshift($this->stack, $el);
675 } elseif ($this->_format ==
'atom' && $el ==
'link') {
679 if (isset($attrs[
'rel']) && $attrs[
'rel'] ==
'alternate') {
681 } elseif (!isset($attrs[
'rel'])) {
684 $link_el =
'link_'.$attrs[
'rel'];
687 $this->
append($link_el, $attrs[
'href']);
690 array_unshift($this->stack, $el);
725 $el = strtolower($el);
727 if ($el ==
'item' or $el ==
'entry') {
728 $this->items[] = $this->current_item;
729 $this->current_item = array();
730 $this->initem =
false;
731 } elseif ($this->_format ==
'rss' and $this->current_namespace ==
'' and $el ==
'textinput') {
732 $this->intextinput =
false;
733 } elseif ($this->_format ==
'rss' and $this->current_namespace ==
'' and $el ==
'image') {
734 $this->inimage =
false;
735 } elseif ($this->_format ==
'atom' and in_array($el, $this->_CONTENT_CONSTRUCTS)) {
736 $this->incontent =
false;
737 } elseif ($el ==
'channel' or $el ==
'feed') {
738 $this->inchannel =
false;
739 } elseif ($this->_format ==
'atom' and $this->incontent) {
742 if ($this->stack[0] == $el) {
748 array_shift($this->stack);
750 array_shift($this->stack);
753 $this->current_namespace =
false;
802 if (!empty($this->current_namespace)) {
803 if (!empty($this->initem)) {
804 $this->
concat($this->current_item[$this->current_namespace][$el], $text);
805 } elseif (!empty($this->inchannel)) {
806 $this->
concat($this->channel[$this->current_namespace][$el], $text);
807 } elseif (!empty($this->intextinput)) {
808 $this->
concat($this->textinput[$this->current_namespace][$el], $text);
809 } elseif (!empty($this->inimage)) {
810 $this->
concat($this->image[$this->current_namespace][$el], $text);
813 if (!empty($this->initem)) {
814 $this->
concat($this->current_item[$el], $text);
815 } elseif (!empty($this->intextinput)) {
816 $this->
concat($this->textinput[$el], $text);
817 } elseif (!empty($this->inimage)) {
818 $this->
concat($this->image[$el], $text);
819 } elseif (!empty($this->inchannel)) {
820 $this->
concat($this->channel[$el], $text);