39 private $_format =
'';
44 private $_lastbuilddate;
48 private $_description;
49 private $_lastfetchdate;
50 private $_rssarray = array();
53 public $stack = array();
54 private $_CONTENT_CONSTRUCTS = array(
'content',
'summary',
'info',
'title',
'tagline',
'copyright');
74 return $this->_format;
84 return $this->_urlRSS;
93 return $this->_language;
102 return $this->_generator;
111 return $this->_copyright;
120 return $this->_lastbuilddate;
129 return $this->_imageurl;
147 return $this->_title;
156 return $this->_description;
165 return $this->_lastfetchdate;
174 return $this->_rssarray;
187 public function parser($urlRSS, $maxNb = 0, $cachedelay = 60, $cachedir =
'')
191 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
199 $this->error =
"ErrorBadUrl";
203 $this->_urlRSS = $urlRSS;
204 $newpathofdestfile = $cachedir.
'/'.
dol_hash($this->_urlRSS, 3);
212 if ($cachedelay > 0 && $cachedir)
215 if ($filedate >= ($nowgmt - $cachedelay))
220 $this->_lastfetchdate = $filedate;
222 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.");
229 $str = file_get_contents($newpathofdestfile);
232 ini_set(
"user_agent",
"Dolibarr ERP-CRM RSS reader");
233 ini_set(
"max_execution_time", $conf->global->MAIN_USE_RESPONSE_TIMEOUT);
234 ini_set(
"default_socket_timeout", $conf->global->MAIN_USE_RESPONSE_TIMEOUT);
236 $opts = array(
'http'=>array(
'method'=>
"GET"));
237 if (!empty($conf->global->MAIN_USE_CONNECT_TIMEOUT)) $opts[
'http'][
'timeout'] = $conf->global->MAIN_USE_CONNECT_TIMEOUT;
238 if (!empty($conf->global->MAIN_PROXY_USE)) $opts[
'http'][
'proxy'] =
'tcp://'.$conf->global->MAIN_PROXY_HOST.
':'.$conf->global->MAIN_PROXY_PORT;
240 $context = stream_context_create($opts);
242 $str = file_get_contents($this->_urlRSS,
false, $context);
244 print 'Error retrieving URL '.$this->_urlRSS.
' - '.$e->getMessage();
251 if (!empty($conf->global->EXTERNALRSS_USE_SIMPLEXML))
254 libxml_use_internal_errors(
false);
255 $rss = simplexml_load_string($str,
"SimpleXMLElement", LIBXML_NOCDATA);
257 if (!function_exists(
'xml_parser_create')) {
258 $this->error =
'Function xml_parser_create are not supported by your PHP';
262 $xmlparser = xml_parser_create(
'');
263 if (!is_resource($xmlparser)) {
264 $this->error =
"ErrorFailedToCreateParser";
268 xml_set_object($xmlparser, $this);
269 xml_set_element_handler($xmlparser,
'feed_start_element',
'feed_end_element');
270 xml_set_character_data_handler($xmlparser,
'feed_cdata');
271 $status = xml_parse($xmlparser, $str);
272 xml_parser_free($xmlparser);
282 if (empty($foundintocache) && $cachedir)
284 dol_syslog(get_class($this).
"::parser cache file ".$newpathofdestfile.
" is saved onto disk.");
286 $fp = fopen($newpathofdestfile,
'w');
291 if (!empty($conf->global->MAIN_UMASK)) $newmask = $conf->global->MAIN_UMASK;
292 @chmod($newpathofdestfile, octdec($newmask));
294 $this->_lastfetchdate = $nowgmt;
296 print 'Error, failed to open file '.$newpathofdestfile.
' for write';
302 if (empty($rss->_format))
304 $rss->_format =
'rss';
305 if (empty($rss->channel)) $rss->_format =
'atom';
311 if ($rss->_format ==
'rss') {
313 if (!empty($conf->global->EXTERNALRSS_USE_SIMPLEXML)) {
314 if (!empty($rss->channel->language)) $this->_language = (string) $rss->channel->language;
315 if (!empty($rss->channel->generator)) $this->_generator = (
string) $rss->channel->generator;
316 if (!empty($rss->channel->copyright)) $this->_copyright = (
string) $rss->channel->copyright;
317 if (!empty($rss->channel->lastbuilddate)) $this->_lastbuilddate = (
string) $rss->channel->lastbuilddate;
318 if (!empty($rss->channel->image->url[0])) $this->_imageurl = (
string) $rss->channel->image->url[0];
319 if (!empty($rss->channel->link)) $this->_link = (
string) $rss->channel->link;
320 if (!empty($rss->channel->title)) $this->_title = (
string) $rss->channel->title;
321 if (!empty($rss->channel->description)) $this->_description = (
string) $rss->channel->description;
324 if (!empty($rss->channel[
'language'])) $this->_language = (
string) $rss->channel[
'language'];
325 if (!empty($rss->channel[
'generator'])) $this->_generator = (
string) $rss->channel[
'generator'];
326 if (!empty($rss->channel[
'copyright'])) $this->_copyright = (
string) $rss->channel[
'copyright'];
327 if (!empty($rss->channel[
'lastbuilddate'])) $this->_lastbuilddate = (
string) $rss->channel[
'lastbuilddate'];
328 if (!empty($rss->image[
'url'])) $this->_imageurl = (
string) $rss->image[
'url'];
329 if (!empty($rss->channel[
'link'])) $this->_link = (
string) $rss->channel[
'link'];
330 if (!empty($rss->channel[
'title'])) $this->_title = (
string) $rss->channel[
'title'];
331 if (!empty($rss->channel[
'description'])) $this->_description = (
string) $rss->channel[
'description'];
334 if (!empty($conf->global->EXTERNALRSS_USE_SIMPLEXML)) $items = $rss->channel->item;
335 else $items = $rss->items;
337 } elseif ($rss->_format ==
'atom') {
339 if (!empty($conf->global->EXTERNALRSS_USE_SIMPLEXML))
341 if (!empty($rss->generator)) $this->_generator = (string) $rss->generator;
342 if (!empty($rss->lastbuilddate)) $this->_lastbuilddate = (
string) $rss->modified;
343 if (!empty($rss->link->href)) $this->_link = (
string) $rss->link->href;
344 if (!empty($rss->title)) $this->_title = (
string) $rss->title;
345 if (!empty($rss->description)) $this->_description = (
string) $rss->description;
348 if (!empty($rss->channel[
'generator'])) $this->_generator = (
string) $rss->channel[
'generator'];
350 if (!empty($rss->channel[
'modified'])) $this->_lastbuilddate = (
string) $rss->channel[
'modified'];
352 if (!empty($rss->channel[
'link'])) $this->_link = (
string) $rss->channel[
'link'];
353 if (!empty($rss->channel[
'title'])) $this->_title = (
string) $rss->channel[
'title'];
356 if (!empty($rss->channel)) {
360 if (!empty($conf->global->EXTERNALRSS_USE_SIMPLEXML)) {
361 $tmprss =
xml2php($rss); $items = $tmprss[
'entry'];
363 else $items = $rss->items;
370 if (is_array($items)) {
371 foreach ($items as $item) {
373 if ($rss->_format ==
'rss') {
374 if (!empty($conf->global->EXTERNALRSS_USE_SIMPLEXML)) {
375 $itemLink = (string) $item->link;
376 $itemTitle = (
string) $item->title;
377 $itemDescription = (string) $item->description;
378 $itemPubDate = (
string) $item->pubDate;
382 $itemLink = (string) $item[
'link'];
383 $itemTitle = (string) $item[
'title'];
384 $itemDescription = (string) $item[
'description'];
385 $itemPubDate = (string) $item[
'pubdate'];
386 $itemId = (string) $item[
'guid'];
387 $itemAuthor = (string) $item[
'author'];
391 $itemCategory = array();
392 if (is_array($item->category)) {
393 foreach ($item->category as $cat) {
394 $itemCategory[] = (string) $cat;
397 } elseif ($rss->_format ==
'atom') {
398 if (!empty($conf->global->EXTERNALRSS_USE_SIMPLEXML)) {
399 $itemLink = (isset($item[
'link']) ? (string) $item[
'link'] :
'');
400 $itemTitle = (string) $item[
'title'];
402 $itemPubDate = (string) $item[
'created'];
403 $itemId = (string) $item[
'id'];
404 $itemAuthor = (string) ($item[
'author'] ? $item[
'author'] : $item[
'author_name']);
406 $itemLink = (isset($item[
'link']) ? (string) $item[
'link'] :
'');
407 $itemTitle = (string) $item[
'title'];
409 $itemPubDate = (string) $item[
'created'];
410 $itemId = (string) $item[
'id'];
411 $itemAuthor = (string) ($item[
'author'] ? $item[
'author'] : $item[
'author_name']);
413 $itemCategory = array();
415 $itemCategory = array();
418 $itemDescription =
'';
422 print 'ErrorBadFeedFormat';
426 $this->_rssarray[$i] = array(
429 'description'=>$itemDescription,
430 'pubDate'=>$itemPubDate,
431 'category'=>$itemCategory,
433 'author'=>$itemAuthor
439 if ($i > $maxNb)
break;
445 $this->error =
'ErrorFailedToLoadRSSFile';
464 $el = $element = strtolower($element);
465 $attrs = array_change_key_case($attrs, CASE_LOWER);
469 if (strpos($element,
':'))
471 list($ns, $el) = explode(
':', $element, 2);
473 if ($ns and $ns !=
'rdf')
475 $this->current_namespace = $ns;
479 if (empty($this->_format))
482 $this->_format =
'rss';
483 $this->feed_version =
'1.0';
484 } elseif ($el ==
'rss') {
485 $this->_format =
'rss';
486 $this->feed_version = $attrs[
'version'];
487 } elseif ($el ==
'feed') {
488 $this->_format =
'atom';
489 $this->feed_version = $attrs[
'version'];
490 $this->inchannel =
true;
495 if ($el ==
'channel')
497 $this->inchannel =
true;
498 } elseif ($el ==
'item' or $el ==
'entry')
500 $this->initem =
true;
501 if (isset($attrs[
'rdf:about'])) {
502 $this->current_item[
'about'] = $attrs[
'rdf:about'];
509 $this->_format ==
'rss' and
510 $this->current_namespace ==
'' and
513 $this->intextinput =
true;
515 $this->_format ==
'rss' and
516 $this->current_namespace ==
'' and
519 $this->inimage =
true;
523 elseif ($this->_format ==
'atom' and in_array($el, $this->_CONTENT_CONSTRUCTS))
526 if ($el ==
'content') {
527 $el =
'atom_content';
530 $this->incontent = $el;
534 elseif ($this->_format ==
'atom' and $this->incontent)
537 $attrs_str = join(
' ', array_map(
'map_attrs', array_keys($attrs), array_values($attrs)));
541 array_unshift($this->stack, $el);
548 elseif ($this->_format ==
'atom' and $el ==
'link')
550 if (isset($attrs[
'rel']) && $attrs[
'rel'] ==
'alternate')
553 } elseif (!isset($attrs[
'rel']))
557 $link_el =
'link_'.$attrs[
'rel'];
560 $this->
append($link_el, $attrs[
'href']);
564 array_unshift($this->stack, $el);
580 if ($this->_format ==
'atom' and $this->incontent)
584 $current_el = join(
'_', array_reverse($this->stack));
585 $this->
append($current_el, $text);
600 $el = strtolower($el);
602 if ($el ==
'item' or $el ==
'entry')
604 $this->items[] = $this->current_item;
605 $this->current_item = array();
606 $this->initem =
false;
607 } elseif ($this->_format ==
'rss' and $this->current_namespace ==
'' and $el ==
'textinput')
609 $this->intextinput =
false;
610 } elseif ($this->_format ==
'rss' and $this->current_namespace ==
'' and $el ==
'image')
612 $this->inimage =
false;
613 } elseif ($this->_format ==
'atom' and in_array($el, $this->_CONTENT_CONSTRUCTS))
615 $this->incontent =
false;
616 } elseif ($el ==
'channel' or $el ==
'feed')
618 $this->inchannel =
false;
619 } elseif ($this->_format ==
'atom' and $this->incontent) {
622 if ($this->stack[0] == $el)
629 array_shift($this->stack);
631 array_shift($this->stack);
634 $this->current_namespace =
false;
645 public function concat(&$str1, $str2 =
"")
664 $this->
concat($this->current_item[$this->incontent], $text);
665 } elseif ($this->inchannel) {
666 $this->
concat($this->channel[$this->incontent], $text);
682 if ($this->current_namespace)
685 $this->
concat($this->current_item[$this->current_namespace][$el], $text);
686 } elseif ($this->inchannel) {
687 $this->
concat($this->channel[$this->current_namespace][$el], $text);
688 } elseif ($this->intextinput) {
689 $this->
concat($this->textinput[$this->current_namespace][$el], $text);
690 } elseif ($this->inimage) {
691 $this->
concat($this->image[$this->current_namespace][$el], $text);
695 $this->
concat($this->current_item[$el], $text);
696 } elseif ($this->intextinput) {
697 $this->
concat($this->textinput[$el], $text);
698 } elseif ($this->inimage) {
699 $this->
concat($this->image[$el], $text);
700 } elseif ($this->inchannel) {
701 $this->
concat($this->channel[$el], $text);
717 if (isset($item[
'summary']))
719 $result = $item[
'summary'];
720 } elseif (isset($item[
'atom_content']))
722 $result = $item[
'atom_content'];
727 $result = strip_tags($result,
"<br><p><ul><ol><li>");
729 $result = str_replace(
"\n",
"", $result);
731 if (strlen($result) > $maxlength)
733 $result = substr($result, 0, $maxlength);
748 if (isset($feed[
'icon']))
750 return $feed[
'logo'];
753 if (isset($feed[
'icon']))
755 return $feed[
'logo'];
758 if (isset($feed[
'webfeeds:logo']))
760 return $feed[
'webfeeds:logo'];
763 if (isset($feed[
'webfeeds:icon']))
765 return $feed[
'webfeeds:icon'];
768 if (isset($feed[
'webfeeds:wordmark']))
770 return $feed[
'webfeeds:wordmark'];
789 foreach ($xml->children() as $key => $value)
794 foreach ($value->attributes() as $ak=>$av)
796 $child[$ak] = (string) $av;
800 if ($tab ===
false && in_array($key, array_keys($array)))
805 $array[$key][] = $tmp;
806 $array[$key][] = $child;
808 } elseif ($tab ===
true)
811 $array[$key][] = $child;
814 $array[$key] = $child;
823 return (
string) $xml;
dol_hash($chain, $type= '0')
Returns a hash of a string.
dol_now($mode= 'auto')
Return date for now.
dol_is_dir($folder)
Test if filename is a directory.
$conf db
API class for accounts.
dol_is_url($url)
Return if path is an URL.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
print
Draft customers invoices.
dol_filemtime($pathoffile)
Return time of a file.
if(!defined('CSRFCHECK_WITH_TOKEN')) define('CSRFCHECK_WITH_TOKEN'
Draft customers invoices.
dol_mkdir($dir, $dataroot= '', $newmask=null)
Creation of a directory (this can create recursive subdir)