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";
368 xml_set_element_handler($xmlparser, array($this,
'feed_start_element'), array($this,
'feed_end_element'));
369 xml_set_character_data_handler($xmlparser, array($this,
'feed_cdata'));
371 $status = xml_parse($xmlparser, $str,
false);
373 if (PHP_VERSION_ID < 80000) {
374 xml_parser_free($xmlparser);
385 if ($rss && empty($this->_format)) {
394 if (empty($foundintocache) && $cachedir) {
395 dol_syslog(get_class($this).
"::parser cache file ".$newpathofdestfile.
" is saved onto disk.");
399 $fp = fopen($newpathofdestfile,
'w');
405 $this->_lastfetchdate = $nowgmt;
407 print
'Error, failed to open file '.$newpathofdestfile.
' for write';
413 if (empty($rss->_format)) {
414 $rss->_format =
'rss';
415 if (empty($rss->channel)) {
416 $rss->_format =
'atom';
423 if ($rss->_format ==
'rss') {
426 if (!empty($rss->channel->language)) {
427 $this->_language =
sanitizeVal((
string) $rss->channel->language);
429 if (!empty($rss->channel->generator)) {
430 $this->_generator =
sanitizeVal((
string) $rss->channel->generator);
432 if (!empty($rss->channel->copyright)) {
433 $this->_copyright =
sanitizeVal((
string) $rss->channel->copyright);
435 if (!empty($rss->channel->lastbuilddate)) {
436 $this->_lastbuilddate =
sanitizeVal((
string) $rss->channel->lastbuilddate);
438 if (!empty($rss->channel->image->url[0])) {
439 $this->_imageurl =
sanitizeVal((
string) $rss->channel->image->url[0]);
441 if (!empty($rss->channel->link)) {
442 $this->_link =
sanitizeVal((
string) $rss->channel->link);
444 if (!empty($rss->channel->title)) {
445 $this->_title =
sanitizeVal((
string) $rss->channel->title);
447 if (!empty($rss->channel->description)) {
448 $this->_description =
sanitizeVal((
string) $rss->channel->description);
452 if (!empty($rss->channel[
'language'])) {
453 $this->_language =
sanitizeVal((
string) $rss->channel[
'language']);
455 if (!empty($rss->channel[
'generator'])) {
456 $this->_generator =
sanitizeVal((
string) $rss->channel[
'generator']);
458 if (!empty($rss->channel[
'copyright'])) {
459 $this->_copyright =
sanitizeVal((
string) $rss->channel[
'copyright']);
461 if (!empty($rss->channel[
'lastbuilddate'])) {
462 $this->_lastbuilddate =
sanitizeVal((
string) $rss->channel[
'lastbuilddate']);
464 if (!empty($rss->image[
'url'])) {
465 $this->_imageurl =
sanitizeVal((
string) $rss->image[
'url']);
467 if (!empty($rss->channel[
'link'])) {
468 $this->_link =
sanitizeVal((
string) $rss->channel[
'link']);
470 if (!empty($rss->channel[
'title'])) {
471 $this->_title =
sanitizeVal((
string) $rss->channel[
'title']);
473 if (!empty($rss->channel[
'description'])) {
474 $this->_description =
sanitizeVal((
string) $rss->channel[
'description']);
479 $items = $rss->channel->item;
481 $items = $rss->items;
484 } elseif ($rss->_format ==
'atom') {
487 if (!empty($rss->generator)) {
488 $this->_generator =
sanitizeVal((
string) $rss->generator);
490 if (!empty($rss->lastbuilddate)) {
491 $this->_lastbuilddate =
sanitizeVal((
string) $rss->modified);
493 if (!empty($rss->link->href)) {
494 $this->_link =
sanitizeVal((
string) $rss->link->href);
496 if (!empty($rss->title)) {
499 if (!empty($rss->description)) {
500 $this->_description =
sanitizeVal((
string) $rss->description);
504 if (!empty($rss->channel[
'generator'])) {
505 $this->_generator =
sanitizeVal((
string) $rss->channel[
'generator']);
508 if (!empty($rss->channel[
'modified'])) {
509 $this->_lastbuilddate =
sanitizeVal((
string) $rss->channel[
'modified']);
512 if (!empty($rss->channel[
'link'])) {
513 $this->_link =
sanitizeVal((
string) $rss->channel[
'link']);
515 if (!empty($rss->channel[
'title'])) {
516 $this->_title =
sanitizeVal((
string) $rss->channel[
'title']);
520 if (!empty($rss->channel)) {
526 $items = $tmprss[
'entry'];
529 $items = $rss->items;
535 if (!empty($this->_link) && !preg_match(
'/^https?:\/\//i', $this->_link)) {
542 if (is_array($items)) {
543 foreach ($items as $item) {
545 if (is_array($item)) {
547 $item += array(
'link' =>
'',
'title' =>
'',
'description' =>
'',
'guid' =>
'',
'author' =>
'',
'author_name' =>
'');
549 if ($rss->_format ==
'rss') {
553 $itemDescription =
sanitizeVal((
string) $item->description);
554 $itemPubDate =
sanitizeVal((
string) $item->pubDate);
560 $itemDescription =
sanitizeVal((
string) $item[
'description']);
561 $itemPubDate =
sanitizeVal((
string) $item[
'pubdate']);
563 $itemAuthor =
sanitizeVal((
string) ($item[
'author'] ??
''));
567 $itemCategory = array();
568 if (!empty($item->category) && is_array($item->category)) {
569 foreach ($item->category as $cat) {
570 $itemCategory[] = (string) $cat;
573 } elseif ($rss->_format ==
'atom') {
574 $itemLink = (isset($item[
'link']) ?
sanitizeVal((
string) $item[
'link']) :
'');
577 $itemPubDate =
sanitizeVal((
string) $item[
'created']);
579 $itemAuthor =
sanitizeVal((
string) ($item[
'author'] ? $item[
'author'] : $item[
'author_name']));
580 $itemCategory = array();
584 $itemDescription =
'';
588 $itemCategory = array();
589 print
'ErrorBadFeedFormat';
592 if (!preg_match(
'/^https?:\/\//i', $itemLink)) {
597 $this->_rssarray[$i] = array(
599 'title' => $itemTitle,
600 'description' => $itemDescription,
601 'pubDate' => $itemPubDate,
602 'category' => $itemCategory,
604 'author' => $itemAuthor
610 if ($maxNb > 0 && $i >= $maxNb) {
618 $this->error =
'ErrorFailedToLoadRSSFile';
637 $el = $element = strtolower($element);
638 $attrs = array_change_key_case($attrs, CASE_LOWER);
642 if (strpos($element,
':')) {
643 list($ns, $el) = explode(
':', $element, 2);
645 if ($ns and $ns !=
'rdf') {
646 $this->current_namespace = $ns;
650 if (empty($this->_format)) {
652 $this->_format =
'rss';
653 $this->feed_version =
'1.0';
654 } elseif ($el ==
'rss') {
655 $this->_format =
'rss';
656 $this->feed_version = (string) ($attrs[
'version'] ??
'');
657 } elseif ($el ==
'feed') {
658 $this->_format =
'atom';
659 $this->feed_version = (string) ($attrs[
'version'] ??
'');
660 $this->inchannel =
true;
665 if ($el ==
'channel') {
666 $this->inchannel =
true;
667 } elseif ($el ==
'item' || $el ==
'entry') {
668 $this->initem =
true;
669 if (isset($attrs[
'rdf:about'])) {
670 $this->current_item[
'about'] = $attrs[
'rdf:about'];
672 } elseif ($this->_format ==
'rss' && $this->current_namespace ==
'' && $el ==
'textinput') {
675 $this->intextinput =
true;
676 } elseif ($this->_format ==
'rss' && $this->current_namespace ==
'' && $el ==
'image') {
677 $this->inimage =
true;
678 } elseif ($this->_format ==
'atom' && in_array($el, $this->_CONTENT_CONSTRUCTS)) {
681 if ($el ==
'content') {
682 $el =
'atom_content';
685 $this->incontent = $el;
686 } elseif ($this->_format ==
'atom' && $this->incontent) {
689 $attrs_str = implode(
' ', array_map(
'rss_map_attrs', array_keys($attrs), array_values($attrs)));
693 array_unshift($this->stack, $el);
694 } elseif ($this->_format ==
'atom' && $el ==
'link') {
698 if (isset($attrs[
'rel']) && $attrs[
'rel'] ==
'alternate') {
700 } elseif (!isset($attrs[
'rel'])) {
703 $link_el =
'link_'.$attrs[
'rel'];
706 $this->
append($link_el, $attrs[
'href']);
709 array_unshift($this->stack, $el);
744 $el = strtolower($el);
746 if ($el ==
'item' or $el ==
'entry') {
747 $this->items[] = $this->current_item;
748 $this->current_item = array();
749 $this->initem =
false;
750 } elseif ($this->_format ==
'rss' and $this->current_namespace ==
'' and $el ==
'textinput') {
751 $this->intextinput =
false;
752 } elseif ($this->_format ==
'rss' and $this->current_namespace ==
'' and $el ==
'image') {
753 $this->inimage =
false;
754 } elseif ($this->_format ==
'atom' and in_array($el, $this->_CONTENT_CONSTRUCTS)) {
755 $this->incontent =
false;
756 } elseif ($el ==
'channel' or $el ==
'feed') {
757 $this->inchannel =
false;
758 } elseif ($this->_format ==
'atom' and $this->incontent) {
761 if ($this->stack[0] == $el) {
767 array_shift($this->stack);
769 array_shift($this->stack);
772 $this->current_namespace =
false;
821 if (!empty($this->current_namespace)) {
822 if (!empty($this->initem)) {
823 $this->
concat($this->current_item[$this->current_namespace][$el], $text);
824 } elseif (!empty($this->inchannel)) {
825 $this->
concat($this->channel[$this->current_namespace][$el], $text);
826 } elseif (!empty($this->intextinput)) {
827 $this->
concat($this->textinput[$this->current_namespace][$el], $text);
828 } elseif (!empty($this->inimage)) {
829 $this->
concat($this->image[$this->current_namespace][$el], $text);
832 if (!empty($this->initem)) {
833 $this->
concat($this->current_item[$el], $text);
834 } elseif (!empty($this->intextinput)) {
835 $this->
concat($this->textinput[$el], $text);
836 } elseif (!empty($this->inimage)) {
837 $this->
concat($this->image[$el], $text);
838 } elseif (!empty($this->inchannel)) {
839 $this->
concat($this->channel[$el], $text);