204 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) {
267 libxml_disable_entity_loader(
true);
270 $rss = simplexml_load_string($str,
"SimpleXMLElement", LIBXML_NOCDATA);
272 if (!function_exists(
'xml_parser_create')) {
273 $this->error =
'Function xml_parser_create are not supported by your PHP';
279 $xmlparser = xml_parser_create(
null);
281 xml_parser_set_option($xmlparser, XML_OPTION_CASE_FOLDING, 0);
282 xml_parser_set_option($xmlparser, XML_OPTION_SKIP_WHITE, 1);
283 xml_parser_set_option($xmlparser, XML_OPTION_TARGET_ENCODING,
"UTF-8");
286 if (!is_resource($xmlparser) && !is_object($xmlparser)) {
287 $this->error =
"ErrorFailedToCreateParser";
291 xml_set_object($xmlparser, $this);
293 xml_set_element_handler($xmlparser,
'feed_start_element',
'feed_end_element');
295 xml_set_character_data_handler($xmlparser,
'feed_cdata');
297 $status = xml_parse($xmlparser, $str,
false);
299 xml_parser_free($xmlparser);
312 if (empty($foundintocache) && $cachedir) {
313 dol_syslog(get_class($this).
"::parser cache file ".$newpathofdestfile.
" is saved onto disk.");
317 $fp = fopen($newpathofdestfile,
'w');
323 $this->_lastfetchdate = $nowgmt;
325 print
'Error, failed to open file '.$newpathofdestfile.
' for write';
331 if (empty($rss->_format)) {
332 $rss->_format =
'rss';
333 if (empty($rss->channel)) {
334 $rss->_format =
'atom';
341 if ($rss->_format ==
'rss') {
344 if (!empty($rss->channel->language)) {
345 $this->_language =
sanitizeVal((
string) $rss->channel->language);
347 if (!empty($rss->channel->generator)) {
348 $this->_generator =
sanitizeVal((
string) $rss->channel->generator);
350 if (!empty($rss->channel->copyright)) {
351 $this->_copyright =
sanitizeVal((
string) $rss->channel->copyright);
353 if (!empty($rss->channel->lastbuilddate)) {
354 $this->_lastbuilddate =
sanitizeVal((
string) $rss->channel->lastbuilddate);
356 if (!empty($rss->channel->image->url[0])) {
357 $this->_imageurl =
sanitizeVal((
string) $rss->channel->image->url[0]);
359 if (!empty($rss->channel->link)) {
360 $this->_link =
sanitizeVal((
string) $rss->channel->link);
362 if (!empty($rss->channel->title)) {
363 $this->_title =
sanitizeVal((
string) $rss->channel->title);
365 if (!empty($rss->channel->description)) {
366 $this->_description =
sanitizeVal((
string) $rss->channel->description);
370 if (!empty($rss->channel[
'language'])) {
371 $this->_language =
sanitizeVal((
string) $rss->channel[
'language']);
373 if (!empty($rss->channel[
'generator'])) {
374 $this->_generator =
sanitizeVal((
string) $rss->channel[
'generator']);
376 if (!empty($rss->channel[
'copyright'])) {
377 $this->_copyright =
sanitizeVal((
string) $rss->channel[
'copyright']);
379 if (!empty($rss->channel[
'lastbuilddate'])) {
380 $this->_lastbuilddate =
sanitizeVal((
string) $rss->channel[
'lastbuilddate']);
382 if (!empty($rss->image[
'url'])) {
383 $this->_imageurl =
sanitizeVal((
string) $rss->image[
'url']);
385 if (!empty($rss->channel[
'link'])) {
386 $this->_link =
sanitizeVal((
string) $rss->channel[
'link']);
388 if (!empty($rss->channel[
'title'])) {
389 $this->_title =
sanitizeVal((
string) $rss->channel[
'title']);
391 if (!empty($rss->channel[
'description'])) {
392 $this->_description =
sanitizeVal((
string) $rss->channel[
'description']);
397 $items = $rss->channel->item;
399 $items = $rss->items;
402 } elseif ($rss->_format ==
'atom') {
405 if (!empty($rss->generator)) {
406 $this->_generator =
sanitizeVal((
string) $rss->generator);
408 if (!empty($rss->lastbuilddate)) {
409 $this->_lastbuilddate =
sanitizeVal((
string) $rss->modified);
411 if (!empty($rss->link->href)) {
412 $this->_link =
sanitizeVal((
string) $rss->link->href);
414 if (!empty($rss->title)) {
417 if (!empty($rss->description)) {
418 $this->_description =
sanitizeVal((
string) $rss->description);
422 if (!empty($rss->channel[
'generator'])) {
423 $this->_generator =
sanitizeVal((
string) $rss->channel[
'generator']);
426 if (!empty($rss->channel[
'modified'])) {
427 $this->_lastbuilddate =
sanitizeVal((
string) $rss->channel[
'modified']);
430 if (!empty($rss->channel[
'link'])) {
431 $this->_link =
sanitizeVal((
string) $rss->channel[
'link']);
433 if (!empty($rss->channel[
'title'])) {
434 $this->_title =
sanitizeVal((
string) $rss->channel[
'title']);
438 if (!empty($rss->channel)) {
444 $items = $tmprss[
'entry'];
447 $items = $rss->items;
455 if (is_array($items)) {
456 foreach ($items as $item) {
458 if ($rss->_format ==
'rss') {
462 $itemDescription =
sanitizeVal((
string) $item->description);
463 $itemPubDate =
sanitizeVal((
string) $item->pubDate);
469 $itemDescription =
sanitizeVal((
string) $item[
'description']);
470 $itemPubDate =
sanitizeVal((
string) $item[
'pubdate']);
472 $itemAuthor =
sanitizeVal((
string) ($item[
'author'] ??
''));
476 $itemCategory = array();
477 if (!empty($item->category) && is_array($item->category)) {
478 foreach ($item->category as $cat) {
479 $itemCategory[] = (string) $cat;
482 } elseif ($rss->_format ==
'atom') {
483 $itemLink = (isset($item[
'link']) ?
sanitizeVal((
string) $item[
'link']) :
'');
486 $itemPubDate =
sanitizeVal((
string) $item[
'created']);
488 $itemAuthor =
sanitizeVal((
string) ($item[
'author'] ? $item[
'author'] : $item[
'author_name']));
489 $itemCategory = array();
493 $itemDescription =
'';
497 $itemCategory = array();
498 print
'ErrorBadFeedFormat';
502 $this->_rssarray[$i] = array(
504 'title' => $itemTitle,
505 'description' => $itemDescription,
506 'pubDate' => $itemPubDate,
507 'category' => $itemCategory,
509 'author' => $itemAuthor
523 $this->error =
'ErrorFailedToLoadRSSFile';
542 $el = $element = strtolower($element);
543 $attrs = array_change_key_case($attrs, CASE_LOWER);
547 if (strpos($element,
':')) {
548 list($ns, $el) = explode(
':', $element, 2);
550 if ($ns and $ns !=
'rdf') {
551 $this->current_namespace = $ns;
555 if (empty($this->_format)) {
557 $this->_format =
'rss';
558 $this->feed_version =
'1.0';
559 } elseif ($el ==
'rss') {
560 $this->_format =
'rss';
561 $this->feed_version = $attrs[
'version'];
562 } elseif ($el ==
'feed') {
563 $this->_format =
'atom';
564 $this->feed_version = $attrs[
'version'];
565 $this->inchannel =
true;
570 if ($el ==
'channel') {
571 $this->inchannel =
true;
572 } elseif ($el ==
'item' || $el ==
'entry') {
573 $this->initem =
true;
574 if (isset($attrs[
'rdf:about'])) {
575 $this->current_item[
'about'] = $attrs[
'rdf:about'];
577 } elseif ($this->_format ==
'rss' && $this->current_namespace ==
'' && $el ==
'textinput') {
580 $this->intextinput =
true;
581 } elseif ($this->_format ==
'rss' && $this->current_namespace ==
'' && $el ==
'image') {
582 $this->inimage =
true;
583 } elseif ($this->_format ==
'atom' && in_array($el, $this->_CONTENT_CONSTRUCTS)) {
586 if ($el ==
'content') {
587 $el =
'atom_content';
590 $this->incontent = $el;
591 } elseif ($this->_format ==
'atom' && $this->incontent) {
594 $attrs_str = implode(
' ', array_map(
'rss_map_attrs', array_keys($attrs), array_values($attrs)));
598 array_unshift($this->stack, $el);
599 } elseif ($this->_format ==
'atom' && $el ==
'link') {
603 if (isset($attrs[
'rel']) && $attrs[
'rel'] ==
'alternate') {
605 } elseif (!isset($attrs[
'rel'])) {
608 $link_el =
'link_'.$attrs[
'rel'];
611 $this->
append($link_el, $attrs[
'href']);
614 array_unshift($this->stack, $el);
649 $el = strtolower($el);
651 if ($el ==
'item' or $el ==
'entry') {
652 $this->items[] = $this->current_item;
653 $this->current_item = array();
654 $this->initem =
false;
655 } elseif ($this->_format ==
'rss' and $this->current_namespace ==
'' and $el ==
'textinput') {
656 $this->intextinput =
false;
657 } elseif ($this->_format ==
'rss' and $this->current_namespace ==
'' and $el ==
'image') {
658 $this->inimage =
false;
659 } elseif ($this->_format ==
'atom' and in_array($el, $this->_CONTENT_CONSTRUCTS)) {
660 $this->incontent =
false;
661 } elseif ($el ==
'channel' or $el ==
'feed') {
662 $this->inchannel =
false;
663 } elseif ($this->_format ==
'atom' and $this->incontent) {
666 if ($this->stack[0] == $el) {
672 array_shift($this->stack);
674 array_shift($this->stack);
677 $this->current_namespace =
false;
726 if (!empty($this->current_namespace)) {
727 if (!empty($this->initem)) {
728 $this->
concat($this->current_item[$this->current_namespace][$el], $text);
729 } elseif (!empty($this->inchannel)) {
730 $this->
concat($this->channel[$this->current_namespace][$el], $text);
731 } elseif (!empty($this->intextinput)) {
732 $this->
concat($this->textinput[$this->current_namespace][$el], $text);
733 } elseif (!empty($this->inimage)) {
734 $this->
concat($this->image[$this->current_namespace][$el], $text);
737 if (!empty($this->initem)) {
738 $this->
concat($this->current_item[$el], $text);
739 } elseif (!empty($this->intextinput)) {
740 $this->
concat($this->textinput[$el], $text);
741 } elseif (!empty($this->inimage)) {
742 $this->
concat($this->image[$el], $text);
743 } elseif (!empty($this->inchannel)) {
744 $this->
concat($this->channel[$el], $text);