283 public function parser($urlRSS, $maxNb = 0, $cachedelay = 60, $cachedir =
'')
285 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
286 include_once DOL_DOCUMENT_ROOT.
'/core/lib/geturl.lib.php';
293 $this->error =
"ErrorBadUrl";
297 $this->_urlRSS = $urlRSS;
298 $newpathofdestfile = $cachedir.
'/'.
dol_hash($this->_urlRSS,
'3');
306 if ($cachedelay > 0 && $cachedir) {
308 if ($filedate >= ($nowgmt - $cachedelay)) {
312 $this->_lastfetchdate = $filedate;
314 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.");
319 if ($foundintocache) {
320 $str = file_get_contents($newpathofdestfile);
323 $result =
getURLContent($this->_urlRSS,
'GET',
'', 1, array(), array(
'http',
'https'), 0);
325 if (!empty($result[
'content'])) {
326 $str = $result[
'content'];
327 } elseif (!empty($result[
'curl_error_msg'])) {
328 $this->error =
'Error retrieving URL '.$this->_urlRSS.
' - '.$result[
'curl_error_msg'];
332 $this->error =
'Error retrieving URL '.$this->_urlRSS.
' - '.$e->getMessage();
337 if ($str !==
false) {
341 libxml_use_internal_errors(
false);
342 if (LIBXML_VERSION < 20900) {
346 libxml_disable_entity_loader(
true);
349 $rss = simplexml_load_string($str,
"SimpleXMLElement", LIBXML_NOCDATA);
351 if (!function_exists(
'xml_parser_create')) {
352 $this->error =
'Function xml_parser_create are not supported by your PHP';
358 $xmlparser = xml_parser_create(
null);
360 xml_parser_set_option($xmlparser, XML_OPTION_CASE_FOLDING, 0);
361 xml_parser_set_option($xmlparser, XML_OPTION_SKIP_WHITE, 1);
362 xml_parser_set_option($xmlparser, XML_OPTION_TARGET_ENCODING,
"UTF-8");
365 if (!is_resource($xmlparser) && !is_object($xmlparser)) {
366 $this->error =
"ErrorFailedToCreateParser";
371 xml_set_element_handler($xmlparser, array($this,
'feed_start_element'), array($this,
'feed_end_element'));
372 xml_set_character_data_handler($xmlparser, array($this,
'feed_cdata'));
374 $status = xml_parse($xmlparser, $str,
false);
376 if (PHP_VERSION_ID < 80000) {
377 xml_parser_free($xmlparser);
388 if ($rss && empty($this->_format)) {
397 if (empty($foundintocache) && $cachedir) {
398 dol_syslog(get_class($this).
"::parser cache file ".$newpathofdestfile.
" is saved onto disk.");
402 $fp = fopen($newpathofdestfile,
'w');
408 $this->_lastfetchdate = $nowgmt;
410 print
'Error, failed to open file '.$newpathofdestfile.
' for write';
416 if (empty($rss->_format)) {
417 $rss->_format =
'rss';
418 if (empty($rss->channel)) {
419 $rss->_format =
'atom';
426 if ($rss->_format ==
'rss') {
429 if (!empty($rss->channel->language)) {
430 $this->_language =
sanitizeVal((
string) $rss->channel->language);
432 if (!empty($rss->channel->generator)) {
433 $this->_generator =
sanitizeVal((
string) $rss->channel->generator);
435 if (!empty($rss->channel->copyright)) {
436 $this->_copyright =
sanitizeVal((
string) $rss->channel->copyright);
438 if (!empty($rss->channel->lastbuilddate)) {
439 $this->_lastbuilddate =
sanitizeVal((
string) $rss->channel->lastbuilddate);
441 if (!empty($rss->channel->image->url[0])) {
442 $this->_imageurl =
sanitizeVal((
string) $rss->channel->image->url[0]);
444 if (!empty($rss->channel->link)) {
445 $this->_link =
sanitizeVal((
string) $rss->channel->link);
447 if (!empty($rss->channel->title)) {
448 $this->_title =
sanitizeVal((
string) $rss->channel->title);
450 if (!empty($rss->channel->description)) {
451 $this->_description =
sanitizeVal((
string) $rss->channel->description);
455 if (!empty($rss->channel[
'language'])) {
456 $this->_language =
sanitizeVal((
string) $rss->channel[
'language']);
458 if (!empty($rss->channel[
'generator'])) {
459 $this->_generator =
sanitizeVal((
string) $rss->channel[
'generator']);
461 if (!empty($rss->channel[
'copyright'])) {
462 $this->_copyright =
sanitizeVal((
string) $rss->channel[
'copyright']);
464 if (!empty($rss->channel[
'lastbuilddate'])) {
465 $this->_lastbuilddate =
sanitizeVal((
string) $rss->channel[
'lastbuilddate']);
467 if (!empty($rss->image[
'url'])) {
468 $this->_imageurl =
sanitizeVal((
string) $rss->image[
'url']);
470 if (!empty($rss->channel[
'link'])) {
471 $this->_link =
sanitizeVal((
string) $rss->channel[
'link']);
473 if (!empty($rss->channel[
'title'])) {
474 $this->_title =
sanitizeVal((
string) $rss->channel[
'title']);
476 if (!empty($rss->channel[
'description'])) {
477 $this->_description =
sanitizeVal((
string) $rss->channel[
'description']);
482 $items = $rss->channel->item;
484 $items = $rss->items;
487 } elseif ($rss->_format ==
'atom') {
490 if (!empty($rss->generator)) {
491 $this->_generator =
sanitizeVal((
string) $rss->generator);
493 if (!empty($rss->lastbuilddate)) {
494 $this->_lastbuilddate =
sanitizeVal((
string) $rss->modified);
496 if (!empty($rss->link->href)) {
497 $this->_link =
sanitizeVal((
string) $rss->link->href);
499 if (!empty($rss->title)) {
502 if (!empty($rss->description)) {
503 $this->_description =
sanitizeVal((
string) $rss->description);
507 if (!empty($rss->channel[
'generator'])) {
508 $this->_generator =
sanitizeVal((
string) $rss->channel[
'generator']);
511 if (!empty($rss->channel[
'modified'])) {
512 $this->_lastbuilddate =
sanitizeVal((
string) $rss->channel[
'modified']);
515 if (!empty($rss->channel[
'link'])) {
516 $this->_link =
sanitizeVal((
string) $rss->channel[
'link']);
518 if (!empty($rss->channel[
'title'])) {
519 $this->_title =
sanitizeVal((
string) $rss->channel[
'title']);
523 if (!empty($rss->channel)) {
529 $items = $tmprss[
'entry'];
532 $items = $rss->items;
538 if (!empty($this->_link) && !preg_match(
'/^https?:\/\//i', $this->_link)) {
545 if (is_array($items)) {
546 foreach ($items as $item) {
548 if (is_array($item)) {
550 $item += array(
'link' =>
'',
'title' =>
'',
'description' =>
'',
'guid' =>
'',
'author' =>
'',
'author_name' =>
'');
552 if ($rss->_format ==
'rss') {
556 $itemDescription =
sanitizeVal((
string) $item->description);
557 $itemPubDate =
sanitizeVal((
string) $item->pubDate);
563 $itemDescription =
sanitizeVal((
string) $item[
'description']);
565 $itemPubDate =
sanitizeVal((
string) ($item[
'pubdate'] ?? ($item[
'dc'][
'date'] ??
'')));
567 $itemAuthor =
sanitizeVal((
string) ($item[
'author'] ??
''));
571 $itemCategory = array();
572 if (!empty($item->category) && is_array($item->category)) {
573 foreach ($item->category as $cat) {
574 $itemCategory[] = (string) $cat;
577 } elseif ($rss->_format ==
'atom') {
578 $itemLink = (isset($item[
'link']) ?
sanitizeVal((
string) $item[
'link']) :
'');
583 foreach (array(
'published',
'issued',
'created',
'updated',
'modified') as $datekey) {
584 if (!empty($item[$datekey]) && is_string($item[$datekey])) {
590 $itemAuthor =
sanitizeVal((
string) ($item[
'author'] ? $item[
'author'] : $item[
'author_name']));
591 $itemCategory = array();
595 $itemDescription =
'';
599 $itemCategory = array();
600 print
'ErrorBadFeedFormat';
603 if (!preg_match(
'/^https?:\/\//i', $itemLink)) {
608 $this->_rssarray[$i] = array(
610 'title' => $itemTitle,
611 'description' => $itemDescription,
612 'pubDate' => $itemPubDate,
613 'category' => $itemCategory,
615 'author' => $itemAuthor
621 if ($maxNb > 0 && $i >= $maxNb) {
629 $this->error =
'ErrorFailedToLoadRSSFile';
648 $el = $element = strtolower($element);
649 $attrs = array_change_key_case($attrs, CASE_LOWER);
653 if (strpos($element,
':')) {
654 list($ns, $el) = explode(
':', $element, 2);
656 if ($ns and $ns !=
'rdf') {
657 $this->current_namespace = $ns;
661 if (empty($this->_format)) {
663 $this->_format =
'rss';
664 $this->feed_version =
'1.0';
665 } elseif ($el ==
'rss') {
666 $this->_format =
'rss';
667 $this->feed_version = (string) ($attrs[
'version'] ??
'');
668 } elseif ($el ==
'feed') {
669 $this->_format =
'atom';
670 $this->feed_version = (string) ($attrs[
'version'] ??
'');
671 $this->inchannel =
true;
676 if ($el ==
'channel') {
677 $this->inchannel =
true;
678 } elseif ($el ==
'item' || $el ==
'entry') {
679 $this->initem =
true;
680 if (isset($attrs[
'rdf:about'])) {
681 $this->current_item[
'about'] = $attrs[
'rdf:about'];
683 } elseif ($this->_format ==
'rss' && $this->current_namespace ==
'' && $el ==
'textinput') {
686 $this->intextinput =
true;
687 } elseif ($this->_format ==
'rss' && $this->current_namespace ==
'' && $el ==
'image') {
688 $this->inimage =
true;
689 } elseif ($this->_format ==
'atom' && in_array($el, $this->_CONTENT_CONSTRUCTS)) {
692 if ($el ==
'content') {
693 $el =
'atom_content';
696 $this->incontent = $el;
697 } elseif ($this->_format ==
'atom' && $this->incontent) {
700 $attrs_str = implode(
' ', array_map(
'rss_map_attrs', array_keys($attrs), array_values($attrs)));
704 array_unshift($this->stack, $el);
705 } elseif ($this->_format ==
'atom' && $el ==
'link') {
709 if (isset($attrs[
'rel']) && $attrs[
'rel'] ==
'alternate') {
711 } elseif (!isset($attrs[
'rel'])) {
714 $link_el =
'link_'.$attrs[
'rel'];
717 $this->
append($link_el, $attrs[
'href']);
720 array_unshift($this->stack, $el);
755 $el = strtolower($el);
757 if ($el ==
'item' or $el ==
'entry') {
758 $this->items[] = $this->current_item;
759 $this->current_item = array();
760 $this->initem =
false;
761 } elseif ($this->_format ==
'rss' and $this->current_namespace ==
'' and $el ==
'textinput') {
762 $this->intextinput =
false;
763 } elseif ($this->_format ==
'rss' and $this->current_namespace ==
'' and $el ==
'image') {
764 $this->inimage =
false;
765 } elseif ($this->_format ==
'atom' and in_array($el, $this->_CONTENT_CONSTRUCTS)) {
766 $this->incontent =
false;
767 } elseif ($el ==
'channel' or $el ==
'feed') {
768 $this->inchannel =
false;
769 } elseif ($this->_format ==
'atom' and $this->incontent) {
772 if ($this->stack[0] == $el) {
778 array_shift($this->stack);
780 array_shift($this->stack);
783 $this->current_namespace =
false;
832 if (!empty($this->current_namespace)) {
833 if (!empty($this->initem)) {
834 $this->
concat($this->current_item[$this->current_namespace][$el], $text);
835 } elseif (!empty($this->inchannel)) {
836 $this->
concat($this->channel[$this->current_namespace][$el], $text);
837 } elseif (!empty($this->intextinput)) {
838 $this->
concat($this->textinput[$this->current_namespace][$el], $text);
839 } elseif (!empty($this->inimage)) {
840 $this->
concat($this->image[$this->current_namespace][$el], $text);
843 if (!empty($this->initem)) {
845 $this->
concat($this->current_item[$el], $text);
846 } elseif (!empty($this->intextinput)) {
848 $this->
concat($this->textinput[$el], $text);
849 } elseif (!empty($this->inimage)) {
851 $this->
concat($this->image[$el], $text);
852 } elseif (!empty($this->inchannel)) {
853 $this->
concat($this->channel[$el], $text);