205 public function parser($urlRSS, $maxNb = 0, $cachedelay = 60, $cachedir =
'')
207 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
208 include_once DOL_DOCUMENT_ROOT.
'/core/lib/geturl.lib.php';
215 $this->error =
"ErrorBadUrl";
219 $this->_urlRSS = $urlRSS;
220 $newpathofdestfile = $cachedir.
'/'.
dol_hash($this->_urlRSS, 3);
228 if ($cachedelay > 0 && $cachedir) {
230 if ($filedate >= ($nowgmt - $cachedelay)) {
234 $this->_lastfetchdate = $filedate;
236 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.");
241 if ($foundintocache) {
242 $str = file_get_contents($newpathofdestfile);
245 $result =
getURLContent($this->_urlRSS,
'GET',
'', 1, array(), array(
'http',
'https'), 0);
247 if (!empty($result[
'content'])) {
248 $str = $result[
'content'];
249 } elseif (!empty($result[
'curl_error_msg'])) {
250 $this->error =
'Error retrieving URL '.$this->_urlRSS.
' - '.$result[
'curl_error_msg'];
254 $this->error =
'Error retrieving URL '.$this->_urlRSS.
' - '.$e->getMessage();
259 if ($str !==
false) {
263 libxml_use_internal_errors(
false);
264 if (LIBXML_VERSION < 20900) {
268 libxml_disable_entity_loader(
true);
271 $rss = simplexml_load_string($str,
"SimpleXMLElement", LIBXML_NOCDATA);
273 if (!function_exists(
'xml_parser_create')) {
274 $this->error =
'Function xml_parser_create are not supported by your PHP';
280 $xmlparser = xml_parser_create(
null);
282 xml_parser_set_option($xmlparser, XML_OPTION_CASE_FOLDING, 0);
283 xml_parser_set_option($xmlparser, XML_OPTION_SKIP_WHITE, 1);
284 xml_parser_set_option($xmlparser, XML_OPTION_TARGET_ENCODING,
"UTF-8");
287 if (!is_resource($xmlparser) && !is_object($xmlparser)) {
288 $this->error =
"ErrorFailedToCreateParser";
292 xml_set_object($xmlparser, $this);
294 xml_set_element_handler($xmlparser,
'feed_start_element',
'feed_end_element');
296 xml_set_character_data_handler($xmlparser,
'feed_cdata');
298 $status = xml_parse($xmlparser, $str,
false);
300 xml_parser_free($xmlparser);
313 if (empty($foundintocache) && $cachedir) {
314 dol_syslog(get_class($this).
"::parser cache file ".$newpathofdestfile.
" is saved onto disk.");
318 $fp = fopen($newpathofdestfile,
'w');
324 $this->_lastfetchdate = $nowgmt;
326 print
'Error, failed to open file '.$newpathofdestfile.
' for write';
332 if (empty($rss->_format)) {
333 $rss->_format =
'rss';
334 if (empty($rss->channel)) {
335 $rss->_format =
'atom';
342 if ($rss->_format ==
'rss') {
345 if (!empty($rss->channel->language)) {
346 $this->_language =
sanitizeVal((
string) $rss->channel->language);
348 if (!empty($rss->channel->generator)) {
349 $this->_generator =
sanitizeVal((
string) $rss->channel->generator);
351 if (!empty($rss->channel->copyright)) {
352 $this->_copyright =
sanitizeVal((
string) $rss->channel->copyright);
354 if (!empty($rss->channel->lastbuilddate)) {
355 $this->_lastbuilddate =
sanitizeVal((
string) $rss->channel->lastbuilddate);
357 if (!empty($rss->channel->image->url[0])) {
358 $this->_imageurl =
sanitizeVal((
string) $rss->channel->image->url[0]);
360 if (!empty($rss->channel->link)) {
361 $this->_link =
sanitizeVal((
string) $rss->channel->link);
363 if (!empty($rss->channel->title)) {
364 $this->_title =
sanitizeVal((
string) $rss->channel->title);
366 if (!empty($rss->channel->description)) {
367 $this->_description =
sanitizeVal((
string) $rss->channel->description);
371 if (!empty($rss->channel[
'language'])) {
372 $this->_language =
sanitizeVal((
string) $rss->channel[
'language']);
374 if (!empty($rss->channel[
'generator'])) {
375 $this->_generator =
sanitizeVal((
string) $rss->channel[
'generator']);
377 if (!empty($rss->channel[
'copyright'])) {
378 $this->_copyright =
sanitizeVal((
string) $rss->channel[
'copyright']);
380 if (!empty($rss->channel[
'lastbuilddate'])) {
381 $this->_lastbuilddate =
sanitizeVal((
string) $rss->channel[
'lastbuilddate']);
383 if (!empty($rss->image[
'url'])) {
384 $this->_imageurl =
sanitizeVal((
string) $rss->image[
'url']);
386 if (!empty($rss->channel[
'link'])) {
387 $this->_link =
sanitizeVal((
string) $rss->channel[
'link']);
389 if (!empty($rss->channel[
'title'])) {
390 $this->_title =
sanitizeVal((
string) $rss->channel[
'title']);
392 if (!empty($rss->channel[
'description'])) {
393 $this->_description =
sanitizeVal((
string) $rss->channel[
'description']);
398 $items = $rss->channel->item;
400 $items = $rss->items;
403 } elseif ($rss->_format ==
'atom') {
406 if (!empty($rss->generator)) {
407 $this->_generator =
sanitizeVal((
string) $rss->generator);
409 if (!empty($rss->lastbuilddate)) {
410 $this->_lastbuilddate =
sanitizeVal((
string) $rss->modified);
412 if (!empty($rss->link->href)) {
413 $this->_link =
sanitizeVal((
string) $rss->link->href);
415 if (!empty($rss->title)) {
418 if (!empty($rss->description)) {
419 $this->_description =
sanitizeVal((
string) $rss->description);
423 if (!empty($rss->channel[
'generator'])) {
424 $this->_generator =
sanitizeVal((
string) $rss->channel[
'generator']);
427 if (!empty($rss->channel[
'modified'])) {
428 $this->_lastbuilddate =
sanitizeVal((
string) $rss->channel[
'modified']);
431 if (!empty($rss->channel[
'link'])) {
432 $this->_link =
sanitizeVal((
string) $rss->channel[
'link']);
434 if (!empty($rss->channel[
'title'])) {
435 $this->_title =
sanitizeVal((
string) $rss->channel[
'title']);
439 if (!empty($rss->channel)) {
445 $items = $tmprss[
'entry'];
448 $items = $rss->items;
456 if (is_array($items)) {
457 foreach ($items as $item) {
459 if ($rss->_format ==
'rss') {
463 $itemDescription =
sanitizeVal((
string) $item->description);
464 $itemPubDate =
sanitizeVal((
string) $item->pubDate);
470 $itemDescription =
sanitizeVal((
string) $item[
'description']);
471 $itemPubDate =
sanitizeVal((
string) $item[
'pubdate']);
473 $itemAuthor =
sanitizeVal((
string) ($item[
'author'] ??
''));
477 $itemCategory = array();
478 if (!empty($item->category) && is_array($item->category)) {
479 foreach ($item->category as $cat) {
480 $itemCategory[] = (string) $cat;
483 } elseif ($rss->_format ==
'atom') {
484 $itemLink = (isset($item[
'link']) ?
sanitizeVal((
string) $item[
'link']) :
'');
487 $itemPubDate =
sanitizeVal((
string) $item[
'created']);
489 $itemAuthor =
sanitizeVal((
string) ($item[
'author'] ? $item[
'author'] : $item[
'author_name']));
490 $itemCategory = array();
494 $itemDescription =
'';
498 $itemCategory = array();
499 print
'ErrorBadFeedFormat';
503 $this->_rssarray[$i] = array(
505 'title' => $itemTitle,
506 'description' => $itemDescription,
507 'pubDate' => $itemPubDate,
508 'category' => $itemCategory,
510 'author' => $itemAuthor
524 $this->error =
'ErrorFailedToLoadRSSFile';
543 $el = $element = strtolower($element);
544 $attrs = array_change_key_case($attrs, CASE_LOWER);
548 if (strpos($element,
':')) {
549 list($ns, $el) = explode(
':', $element, 2);
551 if ($ns and $ns !=
'rdf') {
552 $this->current_namespace = $ns;
556 if (empty($this->_format)) {
558 $this->_format =
'rss';
559 $this->feed_version =
'1.0';
560 } elseif ($el ==
'rss') {
561 $this->_format =
'rss';
562 $this->feed_version = $attrs[
'version'];
563 } elseif ($el ==
'feed') {
564 $this->_format =
'atom';
565 $this->feed_version = $attrs[
'version'];
566 $this->inchannel =
true;
571 if ($el ==
'channel') {
572 $this->inchannel =
true;
573 } elseif ($el ==
'item' || $el ==
'entry') {
574 $this->initem =
true;
575 if (isset($attrs[
'rdf:about'])) {
576 $this->current_item[
'about'] = $attrs[
'rdf:about'];
578 } elseif ($this->_format ==
'rss' && $this->current_namespace ==
'' && $el ==
'textinput') {
581 $this->intextinput =
true;
582 } elseif ($this->_format ==
'rss' && $this->current_namespace ==
'' && $el ==
'image') {
583 $this->inimage =
true;
584 } elseif ($this->_format ==
'atom' && in_array($el, $this->_CONTENT_CONSTRUCTS)) {
587 if ($el ==
'content') {
588 $el =
'atom_content';
591 $this->incontent = $el;
592 } elseif ($this->_format ==
'atom' && $this->incontent) {
595 $attrs_str = implode(
' ', array_map(
'rss_map_attrs', array_keys($attrs), array_values($attrs)));
599 array_unshift($this->stack, $el);
600 } elseif ($this->_format ==
'atom' && $el ==
'link') {
604 if (isset($attrs[
'rel']) && $attrs[
'rel'] ==
'alternate') {
606 } elseif (!isset($attrs[
'rel'])) {
609 $link_el =
'link_'.$attrs[
'rel'];
612 $this->
append($link_el, $attrs[
'href']);
615 array_unshift($this->stack, $el);
650 $el = strtolower($el);
652 if ($el ==
'item' or $el ==
'entry') {
653 $this->items[] = $this->current_item;
654 $this->current_item = array();
655 $this->initem =
false;
656 } elseif ($this->_format ==
'rss' and $this->current_namespace ==
'' and $el ==
'textinput') {
657 $this->intextinput =
false;
658 } elseif ($this->_format ==
'rss' and $this->current_namespace ==
'' and $el ==
'image') {
659 $this->inimage =
false;
660 } elseif ($this->_format ==
'atom' and in_array($el, $this->_CONTENT_CONSTRUCTS)) {
661 $this->incontent =
false;
662 } elseif ($el ==
'channel' or $el ==
'feed') {
663 $this->inchannel =
false;
664 } elseif ($this->_format ==
'atom' and $this->incontent) {
667 if ($this->stack[0] == $el) {
673 array_shift($this->stack);
675 array_shift($this->stack);
678 $this->current_namespace =
false;
727 if (!empty($this->current_namespace)) {
728 if (!empty($this->initem)) {
729 $this->
concat($this->current_item[$this->current_namespace][$el], $text);
730 } elseif (!empty($this->inchannel)) {
731 $this->
concat($this->channel[$this->current_namespace][$el], $text);
732 } elseif (!empty($this->intextinput)) {
733 $this->
concat($this->textinput[$this->current_namespace][$el], $text);
734 } elseif (!empty($this->inimage)) {
735 $this->
concat($this->image[$this->current_namespace][$el], $text);
738 if (!empty($this->initem)) {
739 $this->
concat($this->current_item[$el], $text);
740 } elseif (!empty($this->intextinput)) {
741 $this->
concat($this->textinput[$el], $text);
742 } elseif (!empty($this->inimage)) {
743 $this->
concat($this->image[$el], $text);
744 } elseif (!empty($this->inchannel)) {
745 $this->
concat($this->channel[$el], $text);