dolibarr 25.0.0-alpha
rssparser.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2011-2012 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
26// @phan-file-suppress PhanPluginPHPDocInWrongComment
27
32{
36 public $db;
37
41 public $error = '';
42
46 public $feed_version;
47
51 private $_format = '';
55 private $_urlRSS;
59 private $_language;
63 private $_generator;
67 private $_copyright;
71 private $_lastbuilddate;
75 private $_imageurl;
79 private $_link;
83 private $_title;
87 private $_description;
91 private $_lastfetchdate; // Last successful fetch
95 private $_rssarray = array();
96
100 private $current_namespace;
101
105 public $items = array();
109 public $current_item = array();
113 public $channel = array();
117 public $textinput = array();
121 public $image = array();
122
126 private $initem;
130 private $intextinput;
134 private $incontent;
138 private $inimage;
142 private $inchannel;
143
147 public $stack = array(); // parser stack
151 private $_CONTENT_CONSTRUCTS = array('content', 'summary', 'info', 'title', 'tagline', 'copyright');
152
153
159 public function __construct($db)
160 {
161 $this->db = $db;
162 }
163
169 public function getFormat()
170 {
171 return $this->_format;
172 }
173
179 public function getUrlRss()
180 {
181 return $this->_urlRSS;
182 }
188 public function getLanguage()
189 {
190 return $this->_language;
191 }
197 public function getGenerator()
198 {
199 return $this->_generator;
200 }
206 public function getCopyright()
207 {
208 return $this->_copyright;
209 }
215 public function getLastBuildDate()
216 {
217 return $this->_lastbuilddate;
218 }
224 public function getImageUrl()
225 {
226 return $this->_imageurl;
227 }
233 public function getLink()
234 {
235 return $this->_link;
236 }
242 public function getTitle()
243 {
244 return $this->_title;
245 }
251 public function getDescription()
252 {
253 return $this->_description;
254 }
260 public function getLastFetchDate()
261 {
262 return $this->_lastfetchdate;
263 }
269 public function getItems()
270 {
271 return $this->_rssarray;
272 }
273
283 public function parser($urlRSS, $maxNb = 0, $cachedelay = 60, $cachedir = '')
284 {
285 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
286 include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
287
288 $rss = '';
289 $str = ''; // This will contain content of feed
290
291 // Check parameters
292 if (!dol_is_url($urlRSS)) {
293 $this->error = "ErrorBadUrl";
294 return -1;
295 }
296
297 $this->_urlRSS = $urlRSS;
298 $newpathofdestfile = $cachedir.'/'.dol_hash($this->_urlRSS, '3'); // Force md5 hash (does not contain special chars)
299 $newmask = '0644';
300
301 //dol_syslog("RssParser::parser parse url=".$urlRSS." => cache file=".$newpathofdestfile);
302 $nowgmt = dol_now();
303
304 // Search into cache
305 $foundintocache = 0;
306 if ($cachedelay > 0 && $cachedir) {
307 $filedate = dol_filemtime($newpathofdestfile);
308 if ($filedate >= ($nowgmt - $cachedelay)) {
309 //dol_syslog("RssParser::parser cache file ".$newpathofdestfile." is not older than now - cachedelay (".$nowgmt." - ".$cachedelay.") so we use it.");
310 $foundintocache = 1;
311
312 $this->_lastfetchdate = $filedate;
313 } else {
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.");
315 }
316 }
317
318 // Load file into $str
319 if ($foundintocache) { // Cache file found and is not too old
320 $str = file_get_contents($newpathofdestfile);
321 } else {
322 try {
323 $result = getURLContent($this->_urlRSS, 'GET', '', 1, array(), array('http', 'https'), 0);
324
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'];
329 return -1;
330 }
331 } catch (Exception $e) {
332 $this->error = 'Error retrieving URL '.$this->_urlRSS.' - '.$e->getMessage();
333 return -2;
334 }
335 }
336
337 if ($str !== false) {
338 // Convert $str into xml
339 if (getDolGlobalString('EXTERNALRSS_USE_SIMPLEXML')) {
340 //print 'xx'.LIBXML_NOCDATA;
341 libxml_use_internal_errors(false);
342 if (LIBXML_VERSION < 20900) {
343 // Avoid load of external entities (security problem).
344 // Required only if LIBXML_VERSION < 20900
345 // @phan-suppress-next-line PhanDeprecatedFunctionInternal
346 libxml_disable_entity_loader(true);
347 }
348
349 $rss = simplexml_load_string($str, "SimpleXMLElement", LIBXML_NOCDATA);
350 } else {
351 if (!function_exists('xml_parser_create')) {
352 $this->error = 'Function xml_parser_create are not supported by your PHP';
353 return -1;
354 }
355
356 try {
357 // @phan-suppress-next-line PhanTypeMismatchArgumentInternalProbablyReal
358 $xmlparser = xml_parser_create(null);
359
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");
363 //xml_set_external_entity_ref_handler($xmlparser, 'extEntHandler'); // Seems to have no effect even when function extEntHandler exists.
364
365 if (!is_resource($xmlparser) && !is_object($xmlparser)) {
366 $this->error = "ErrorFailedToCreateParser";
367 return -1;
368 }
369
370 // Handlers given as callable arrays (xml_set_object() and handler names as strings are deprecated since PHP 8.4)
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'));
373
374 $status = xml_parse($xmlparser, $str, false);
375
376 if (PHP_VERSION_ID < 80000) {
377 xml_parser_free($xmlparser); // No effect since PHP 8.0, deprecated since PHP 8.5
378 }
379
380 $rss = $this;
381 //var_dump($status.' '.$rss->_format);exit;
382 } catch (Exception $e) {
383 $rss = null;
384 }
385
386 // The root element was not rss, rdf or feed (an HTML error page, an empty answer...): it is not a feed.
387 // Errors after the root element are tolerated, the items read before the error are kept.
388 if ($rss && empty($this->_format)) {
389 $rss = null; // Error ErrorFailedToLoadRSSFile, and the answer is not saved into the cache
390 }
391 }
392 }
393
394 // If $rss loaded
395 if ($rss) {
396 // Save file into cache
397 if (empty($foundintocache) && $cachedir) {
398 dol_syslog(get_class($this)."::parser cache file ".$newpathofdestfile." is saved onto disk.");
399 if (!dol_is_dir($cachedir)) {
400 dol_mkdir($cachedir);
401 }
402 $fp = fopen($newpathofdestfile, 'w');
403 if ($fp) {
404 fwrite($fp, $str);
405 fclose($fp);
406 dolChmod($newpathofdestfile);
407
408 $this->_lastfetchdate = $nowgmt;
409 } else {
410 print 'Error, failed to open file '.$newpathofdestfile.' for write';
411 }
412 }
413
414 unset($str); // Free memory
415
416 if (empty($rss->_format)) { // If format not detected automatically
417 $rss->_format = 'rss';
418 if (empty($rss->channel)) {
419 $rss->_format = 'atom';
420 }
421 }
422
423 $items = array();
424
425 // Save description entries
426 if ($rss->_format == 'rss') {
427 //var_dump($rss);
428 if (getDolGlobalString('EXTERNALRSS_USE_SIMPLEXML')) {
429 if (!empty($rss->channel->language)) {
430 $this->_language = sanitizeVal((string) $rss->channel->language);
431 }
432 if (!empty($rss->channel->generator)) {
433 $this->_generator = sanitizeVal((string) $rss->channel->generator);
434 }
435 if (!empty($rss->channel->copyright)) {
436 $this->_copyright = sanitizeVal((string) $rss->channel->copyright);
437 }
438 if (!empty($rss->channel->lastbuilddate)) {
439 $this->_lastbuilddate = sanitizeVal((string) $rss->channel->lastbuilddate);
440 }
441 if (!empty($rss->channel->image->url[0])) {
442 $this->_imageurl = sanitizeVal((string) $rss->channel->image->url[0]);
443 }
444 if (!empty($rss->channel->link)) {
445 $this->_link = sanitizeVal((string) $rss->channel->link);
446 }
447 if (!empty($rss->channel->title)) {
448 $this->_title = sanitizeVal((string) $rss->channel->title);
449 }
450 if (!empty($rss->channel->description)) {
451 $this->_description = sanitizeVal((string) $rss->channel->description);
452 }
453 } else {
454 //var_dump($rss->channel);
455 if (!empty($rss->channel['language'])) {
456 $this->_language = sanitizeVal((string) $rss->channel['language']);
457 }
458 if (!empty($rss->channel['generator'])) {
459 $this->_generator = sanitizeVal((string) $rss->channel['generator']);
460 }
461 if (!empty($rss->channel['copyright'])) {
462 $this->_copyright = sanitizeVal((string) $rss->channel['copyright']);
463 }
464 if (!empty($rss->channel['lastbuilddate'])) {
465 $this->_lastbuilddate = sanitizeVal((string) $rss->channel['lastbuilddate']);
466 }
467 if (!empty($rss->image['url'])) {
468 $this->_imageurl = sanitizeVal((string) $rss->image['url']);
469 }
470 if (!empty($rss->channel['link'])) {
471 $this->_link = sanitizeVal((string) $rss->channel['link']);
472 }
473 if (!empty($rss->channel['title'])) {
474 $this->_title = sanitizeVal((string) $rss->channel['title']);
475 }
476 if (!empty($rss->channel['description'])) {
477 $this->_description = sanitizeVal((string) $rss->channel['description']);
478 }
479 }
480
481 if (getDolGlobalString('EXTERNALRSS_USE_SIMPLEXML')) {
482 $items = $rss->channel->item; // With simplexml
483 } else {
484 $items = $rss->items; // With xmlparse
485 }
486 //var_dump($items);exit;
487 } elseif ($rss->_format == 'atom') {
488 //var_dump($rss);
489 if (getDolGlobalString('EXTERNALRSS_USE_SIMPLEXML')) {
490 if (!empty($rss->generator)) {
491 $this->_generator = sanitizeVal((string) $rss->generator);
492 }
493 if (!empty($rss->lastbuilddate)) {
494 $this->_lastbuilddate = sanitizeVal((string) $rss->modified);
495 }
496 if (!empty($rss->link->href)) {
497 $this->_link = sanitizeVal((string) $rss->link->href);
498 }
499 if (!empty($rss->title)) {
500 $this->_title = sanitizeVal((string) $rss->title);
501 }
502 if (!empty($rss->description)) {
503 $this->_description = sanitizeVal((string) $rss->description);
504 }
505 } else {
506 //if (!empty($rss->channel['rss_language'])) $this->_language = (string) $rss->channel['rss_language'];
507 if (!empty($rss->channel['generator'])) {
508 $this->_generator = sanitizeVal((string) $rss->channel['generator']);
509 }
510 //if (!empty($rss->channel['rss_copyright'])) $this->_copyright = (string) $rss->channel['rss_copyright'];
511 if (!empty($rss->channel['modified'])) {
512 $this->_lastbuilddate = sanitizeVal((string) $rss->channel['modified']);
513 }
514 //if (!empty($rss->image['rss_url'])) $this->_imageurl = (string) $rss->image['rss_url'];
515 if (!empty($rss->channel['link'])) {
516 $this->_link = sanitizeVal((string) $rss->channel['link']);
517 }
518 if (!empty($rss->channel['title'])) {
519 $this->_title = sanitizeVal((string) $rss->channel['title']);
520 }
521 //if (!empty($rss->channel['rss_description'])) $this->_description = (string) $rss->channel['rss_description'];
522
523 if (!empty($rss->channel)) {
524 $this->_imageurl = sanitizeVal($this->getAtomImageUrl($rss->channel));
525 }
526 }
527 if (getDolGlobalString('EXTERNALRSS_USE_SIMPLEXML')) {
528 $tmprss = xml2php($rss);
529 $items = $tmprss['entry'];
530 } else {
531 // With simplexml
532 $items = $rss->items; // With xmlparse
533 }
534 //var_dump($items);exit;
535 }
536
537 // The links of the feed are used as href: keep only http(s) links (a "javascript:" link would run in Dolibarr)
538 if (!empty($this->_link) && !preg_match('/^https?:\/\//i', $this->_link)) {
539 $this->_link = '';
540 }
541
542 $i = 0;
543
544 // Loop on each record
545 if (is_array($items)) {
546 foreach ($items as $item) {
547 //var_dump($item);exit;
548 if (is_array($item)) {
549 // Optional elements of an item, to avoid PHP warnings when they are missing
550 $item += array('link' => '', 'title' => '', 'description' => '', 'guid' => '', 'author' => '', 'author_name' => '');
551 }
552 if ($rss->_format == 'rss') {
553 if (getDolGlobalString('EXTERNALRSS_USE_SIMPLEXML')) {
554 $itemLink = sanitizeVal((string) $item->link);
555 $itemTitle = sanitizeVal((string) $item->title);
556 $itemDescription = sanitizeVal((string) $item->description);
557 $itemPubDate = sanitizeVal((string) $item->pubDate);
558 $itemId = '';
559 $itemAuthor = '';
560 } else {
561 $itemLink = sanitizeVal((string) $item['link']);
562 $itemTitle = sanitizeVal((string) $item['title']);
563 $itemDescription = sanitizeVal((string) $item['description']);
564 // pubDate (RSS 2.0) or dc:date (RSS 1.0)
565 $itemPubDate = sanitizeVal((string) ($item['pubdate'] ?? ($item['dc']['date'] ?? '')));
566 $itemId = sanitizeVal((string) $item['guid']);
567 $itemAuthor = sanitizeVal((string) ($item['author'] ?? ''));
568 }
569
570 // Loop on each category
571 $itemCategory = array();
572 if (!empty($item->category) && is_array($item->category)) {
573 foreach ($item->category as $cat) {
574 $itemCategory[] = (string) $cat;
575 }
576 }
577 } elseif ($rss->_format == 'atom') {
578 $itemLink = (isset($item['link']) ? sanitizeVal((string) $item['link']) : '');
579 $itemTitle = sanitizeVal((string) $item['title']);
580 $itemDescription = sanitizeVal($this->getAtomItemDescription($item));
581 // Atom 1.0: published or updated, Atom 0.3: issued, created or modified
582 $itemPubDate = '';
583 foreach (array('published', 'issued', 'created', 'updated', 'modified') as $datekey) {
584 if (!empty($item[$datekey]) && is_string($item[$datekey])) {
585 $itemPubDate = sanitizeVal($item[$datekey]);
586 break;
587 }
588 }
589 $itemId = sanitizeVal((string) $item['id']);
590 $itemAuthor = sanitizeVal((string) ($item['author'] ? $item['author'] : $item['author_name']));
591 $itemCategory = array();
592 } else {
593 $itemLink = '';
594 $itemTitle = '';
595 $itemDescription = '';
596 $itemPubDate = '';
597 $itemId = '';
598 $itemAuthor = '';
599 $itemCategory = array();
600 print 'ErrorBadFeedFormat';
601 }
602
603 if (!preg_match('/^https?:\/\//i', $itemLink)) {
604 $itemLink = '';
605 }
606
607 // Add record to result array
608 $this->_rssarray[$i] = array(
609 'link' => $itemLink,
610 'title' => $itemTitle,
611 'description' => $itemDescription,
612 'pubDate' => $itemPubDate,
613 'category' => $itemCategory,
614 'id' => $itemId,
615 'author' => $itemAuthor
616 );
617 //var_dump($this->_rssarray);
618
619 $i++;
620
621 if ($maxNb > 0 && $i >= $maxNb) {
622 break; // We get all records we want (0 = no limit)
623 }
624 }
625 }
626
627 return 1;
628 } else {
629 $this->error = 'ErrorFailedToLoadRSSFile';
630 return -1;
631 }
632 }
633
634
635
636 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
645 public function feed_start_element($p, $element, $attrs)
646 {
647 // phpcs:enable
648 $el = $element = strtolower($element);
649 $attrs = array_change_key_case($attrs, CASE_LOWER);
650
651 // check for a namespace, and split if found
652 $ns = false;
653 if (strpos($element, ':')) {
654 list($ns, $el) = explode(':', $element, 2);
655 }
656 if ($ns and $ns != 'rdf') {
657 $this->current_namespace = $ns;
658 }
659
660 // if feed type isn't set, then this is first element of feed identify feed from root element
661 if (empty($this->_format)) {
662 if ($el == 'rdf') {
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;
672 }
673 return;
674 }
675
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'];
682 }
683 } elseif ($this->_format == 'rss' && $this->current_namespace == '' && $el == 'textinput') {
684 // if we're in the default namespace of an RSS feed,
685 // record textinput or image fields
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)) {
690 // handle atom content constructs
691 // avoid clashing w/ RSS mod_content
692 if ($el == 'content') {
693 $el = 'atom_content';
694 }
695
696 $this->incontent = $el;
697 } elseif ($this->_format == 'atom' && $this->incontent) {
698 // if inside an Atom content construct (e.g. content or summary) field treat tags as text
699 // if tags are inlined, then flatten
700 $attrs_str = implode(' ', array_map('rss_map_attrs', array_keys($attrs), array_values($attrs)));
701
702 $this->append_content("<$element $attrs_str>");
703
704 array_unshift($this->stack, $el);
705 } elseif ($this->_format == 'atom' && $el == 'link') {
706 // Atom support many links per containing element.
707 // Magpie treats link elements of type rel='alternate'
708 // as being equivalent to RSS's simple link element.
709 if (isset($attrs['rel']) && $attrs['rel'] == 'alternate') {
710 $link_el = 'link';
711 } elseif (!isset($attrs['rel'])) {
712 $link_el = 'link';
713 } else {
714 $link_el = 'link_'.$attrs['rel'];
715 }
716
717 $this->append($link_el, $attrs['href']);
718 } else {
719 // set stack[0] to current element
720 array_unshift($this->stack, $el);
721 }
722 }
723
724
725 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
733 public function feed_cdata($p, $text)
734 {
735 // phpcs:enable
736 if ($this->_format == 'atom' and $this->incontent) {
737 $this->append_content($text);
738 } else {
739 $current_el = implode('_', array_reverse($this->stack));
740 $this->append($current_el, $text);
741 }
742 }
743
744 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
752 public function feed_end_element($p, $el)
753 {
754 // phpcs:enable
755 $el = strtolower($el);
756
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) {
770 // balance tags properly
771 // note: i don't think this is actually necessary
772 if ($this->stack[0] == $el) {
773 $this->append_content("</$el>");
774 } else {
775 $this->append_content("<$el />");
776 }
777
778 array_shift($this->stack);
779 } else {
780 array_shift($this->stack);
781 }
782
783 $this->current_namespace = false;
784 }
785
786
794 public function concat(&$str1, $str2 = "")
795 {
796 if (!isset($str1)) {
797 $str1 = "";
798 }
799 $str1 .= $str2;
800 return $str1;
801 }
802
803 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
810 public function append_content($text)
811 {
812 // phpcs:enable
813 if (!empty($this->initem)) {
814 $this->concat($this->current_item[$this->incontent], $text);
815 } elseif (!empty($this->inchannel)) {
816 $this->concat($this->channel[$this->incontent], $text);
817 }
818 }
819
827 public function append($el, $text)
828 {
829 if (!$el) {
830 return;
831 }
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);
841 }
842 } else {
843 if (!empty($this->initem)) {
844 // @phpstan-ignore-next-line argument.type
845 $this->concat($this->current_item[$el], $text); // @phan-suppress-current-line PhanTypeMismatchArgument
846 } elseif (!empty($this->intextinput)) {
847 // @phpstan-ignore-next-line argument.type
848 $this->concat($this->textinput[$el], $text); // @phan-suppress-current-line PhanTypeMismatchArgument
849 } elseif (!empty($this->inimage)) {
850 // @phpstan-ignore-next-line argument.type
851 $this->concat($this->image[$el], $text); // @phan-suppress-current-line PhanTypeMismatchArgument
852 } elseif (!empty($this->inchannel)) {
853 $this->concat($this->channel[$el], $text);
854 }
855 }
856 }
857
865 private function getAtomItemDescription(array $item, $maxlength = 500)
866 {
867 $result = "";
868
869 if (isset($item['summary'])) {
870 $result = $item['summary'];
871 } elseif (isset($item['atom_content'])) {
872 $result = $item['atom_content'];
873 }
874
875 // remove all HTML elements that can possible break the maximum size of a tooltip,
876 // like headings, image, video etc. and allow only simple style elements
877 $result = strip_tags($result, "<br><p><ul><ol><li>");
878
879 $result = str_replace("\n", "", $result);
880
881 if (strlen($result) > $maxlength) {
882 $result = substr($result, 0, $maxlength);
883 $result .= "...";
884 }
885
886 return $result;
887 }
888
895 private function getAtomImageUrl(array $feed)
896 {
897 if (!empty($feed['logo']) && is_string($feed['logo'])) {
898 return $feed['logo'];
899 }
900
901 if (!empty($feed['icon']) && is_string($feed['icon'])) {
902 return $feed['icon'];
903 }
904
905 // Elements of a namespace are stored by namespace (see append())
906 foreach (array('logo', 'icon', 'wordmark') as $key) {
907 if (!empty($feed['webfeeds'][$key]) && is_string($feed['webfeeds'][$key])) {
908 return $feed['webfeeds'][$key];
909 }
910 }
911
912 return "";
913 }
914}
915
923function rss_map_attrs($k, $v)
924{
925 return "$k=\"$v\"";
926}
927
934function xml2php($xml)
935{
936 $threads = 0;
937 $tab = false;
938 $array = array();
939 foreach ($xml->children() as $key => $value) {
940 '@phan-var-force SimpleXMLElement $value';
941 $child = xml2php($value);
942
943 //To deal with the attributes
944 foreach ($value->attributes() as $ak => $av) {
945 $child[$ak] = (string) $av;
946 }
947
948 //Let see if the new child is not in the array
949 if ($tab === false && array_key_exists($key, $array)) {
950 //If this element is already in the array we will create an indexed array
951 $tmp = $array[$key];
952 $array[$key] = null;
953 $array[$key][] = $tmp;
954 $array[$key][] = $child;
955 $tab = true;
956 } elseif ($tab === true) {
957 //Add an element in an existing array
958 $array[$key][] = $child;
959 } else {
960 //Add a simple element
961 $array[$key] = $child;
962 }
963
964 $threads++;
965 }
966
967
968 if ($threads == 0) {
969 return (string) $xml;
970 }
971
972 return $array;
973}
Class to parse RSS files.
feed_start_element($p, $element, $attrs)
Triggered when opened tag is found.
getAtomItemDescription(array $item, $maxlength=500)
Return a description/summary for one item from a ATOM feed.
concat(&$str1, $str2="")
To concat 2 strings with no warning if an operand is not defined.
getImageUrl()
getImageUrl
feed_end_element($p, $el)
Triggered when closed tag is found.
__construct($db)
Constructor.
getLanguage()
getLanguage
getTitle()
getTitle
getLastBuildDate()
getLastBuildDate
getItems()
getItems
getLink()
getLink
getGenerator()
getGenerator
getCopyright()
getCopyright
feed_cdata($p, $text)
Triggered when CDATA is found.
getLastFetchDate()
getLastFetchDate
getUrlRss()
getUrlRss
parser($urlRSS, $maxNb=0, $cachedelay=60, $cachedir='')
Parse rss URL.
append_content($text)
Enter description here ...
append($el, $text)
smart append - field and namespace aware
getDescription()
getDescription
getFormat()
getFormat
getAtomImageUrl(array $feed)
Return a URL to a image of the given ATOM feed.
dol_is_url($uri)
Return if path is an URI (the name of the method is misleading).
dol_filemtime($pathoffile)
Return time of a file.
dol_is_dir($folder)
Test if filename is a directory.
dol_now($mode='gmt')
Return date for now.
dolChmod($filepath, $newmask='')
Change mod of a file.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
getURLContent($url, $postorget='GET', $param='', $followlocation=1, $addheaders=array(), $allowedschemes=array('http', 'https'), $localurl=0, $ssl_verifypeer=-1, $timeoutconnect=0, $timeoutresponse=0, $otherCurlOptions=array(), $morelogsuffix='')
Function to get a content from an URL (use proxy if proxy defined).
xml2php($xml)
Function to convert an XML object into an array.
rss_map_attrs($k, $v)
Function to convert an XML object into an array.
dol_hash($chain, $type='0', $nosalt=0, $mode=0)
Returns a hash (non reversible encryption) of a string.