dolibarr 23.0.4
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 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
102 public $items = array();
106 public $current_item = array();
110 public $channel = array();
114 public $textinput = array();
118 public $image = array();
119
123 private $initem;
127 private $intextinput;
131 private $incontent;
135 private $inimage;
139 private $inchannel;
140
144 public $stack = array(); // parser stack
148 private $_CONTENT_CONSTRUCTS = array('content', 'summary', 'info', 'title', 'tagline', 'copyright');
149
150
156 public function __construct($db)
157 {
158 $this->db = $db;
159 }
160
166 public function getFormat()
167 {
168 return $this->_format;
169 }
170
176 public function getUrlRss()
177 {
178 return $this->_urlRSS;
179 }
185 public function getLanguage()
186 {
187 return $this->_language;
188 }
194 public function getGenerator()
195 {
196 return $this->_generator;
197 }
203 public function getCopyright()
204 {
205 return $this->_copyright;
206 }
212 public function getLastBuildDate()
213 {
214 return $this->_lastbuilddate;
215 }
221 public function getImageUrl()
222 {
223 return $this->_imageurl;
224 }
230 public function getLink()
231 {
232 return $this->_link;
233 }
239 public function getTitle()
240 {
241 return $this->_title;
242 }
248 public function getDescription()
249 {
250 return $this->_description;
251 }
257 public function getLastFetchDate()
258 {
259 return $this->_lastfetchdate;
260 }
266 public function getItems()
267 {
268 return $this->_rssarray;
269 }
270
280 public function parser($urlRSS, $maxNb = 0, $cachedelay = 60, $cachedir = '')
281 {
282 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
283 include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
284
285 $rss = '';
286 $str = ''; // This will contain content of feed
287
288 // Check parameters
289 if (!dol_is_url($urlRSS)) {
290 $this->error = "ErrorBadUrl";
291 return -1;
292 }
293
294 $this->_urlRSS = $urlRSS;
295 $newpathofdestfile = $cachedir.'/'.dol_hash($this->_urlRSS, '3'); // Force md5 hash (does not contain special chars)
296 $newmask = '0644';
297
298 //dol_syslog("RssParser::parser parse url=".$urlRSS." => cache file=".$newpathofdestfile);
299 $nowgmt = dol_now();
300
301 // Search into cache
302 $foundintocache = 0;
303 if ($cachedelay > 0 && $cachedir) {
304 $filedate = dol_filemtime($newpathofdestfile);
305 if ($filedate >= ($nowgmt - $cachedelay)) {
306 //dol_syslog("RssParser::parser cache file ".$newpathofdestfile." is not older than now - cachedelay (".$nowgmt." - ".$cachedelay.") so we use it.");
307 $foundintocache = 1;
308
309 $this->_lastfetchdate = $filedate;
310 } else {
311 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.");
312 }
313 }
314
315 // Load file into $str
316 if ($foundintocache) { // Cache file found and is not too old
317 $str = file_get_contents($newpathofdestfile);
318 } else {
319 try {
320 $result = getURLContent($this->_urlRSS, 'GET', '', 1, array(), array('http', 'https'), 0);
321
322 if (!empty($result['content'])) {
323 $str = $result['content'];
324 } elseif (!empty($result['curl_error_msg'])) {
325 $this->error = 'Error retrieving URL '.$this->_urlRSS.' - '.$result['curl_error_msg'];
326 return -1;
327 }
328 } catch (Exception $e) {
329 $this->error = 'Error retrieving URL '.$this->_urlRSS.' - '.$e->getMessage();
330 return -2;
331 }
332 }
333
334 if ($str !== false) {
335 // Convert $str into xml
336 if (getDolGlobalString('EXTERNALRSS_USE_SIMPLEXML')) {
337 //print 'xx'.LIBXML_NOCDATA;
338 libxml_use_internal_errors(false);
339 if (LIBXML_VERSION < 20900) {
340 // Avoid load of external entities (security problem).
341 // Required only if LIBXML_VERSION < 20900
342 // @phan-suppress-next-line PhanDeprecatedFunctionInternal
343 libxml_disable_entity_loader(true);
344 }
345
346 $rss = simplexml_load_string($str, "SimpleXMLElement", LIBXML_NOCDATA);
347 } else {
348 if (!function_exists('xml_parser_create')) {
349 $this->error = 'Function xml_parser_create are not supported by your PHP';
350 return -1;
351 }
352
353 try {
354 // @phan-suppress-next-line PhanTypeMismatchArgumentInternalProbablyReal
355 $xmlparser = xml_parser_create(null);
356
357 xml_parser_set_option($xmlparser, XML_OPTION_CASE_FOLDING, 0);
358 xml_parser_set_option($xmlparser, XML_OPTION_SKIP_WHITE, 1);
359 xml_parser_set_option($xmlparser, XML_OPTION_TARGET_ENCODING, "UTF-8");
360 //xml_set_external_entity_ref_handler($xmlparser, 'extEntHandler'); // Seems to have no effect even when function extEntHandler exists.
361
362 if (!is_resource($xmlparser) && !is_object($xmlparser)) {
363 $this->error = "ErrorFailedToCreateParser";
364 return -1;
365 }
366
367 // Handlers given as callable arrays (xml_set_object() and handler names as strings are deprecated since PHP 8.4)
368 xml_set_element_handler($xmlparser, array($this, 'feed_start_element'), array($this, 'feed_end_element'));
369 xml_set_character_data_handler($xmlparser, array($this, 'feed_cdata'));
370
371 $status = xml_parse($xmlparser, $str, false);
372
373 if (PHP_VERSION_ID < 80000) {
374 xml_parser_free($xmlparser); // No effect since PHP 8.0, deprecated since PHP 8.5
375 }
376
377 $rss = $this;
378 //var_dump($status.' '.$rss->_format);exit;
379 } catch (Exception $e) {
380 $rss = null;
381 }
382
383 // The root element was not rss, rdf or feed (an HTML error page, an empty answer...): it is not a feed.
384 // Errors after the root element are tolerated, the items read before the error are kept.
385 if ($rss && empty($this->_format)) {
386 $rss = null; // Error ErrorFailedToLoadRSSFile, and the answer is not saved into the cache
387 }
388 }
389 }
390
391 // If $rss loaded
392 if ($rss) {
393 // Save file into cache
394 if (empty($foundintocache) && $cachedir) {
395 dol_syslog(get_class($this)."::parser cache file ".$newpathofdestfile." is saved onto disk.");
396 if (!dol_is_dir($cachedir)) {
397 dol_mkdir($cachedir);
398 }
399 $fp = fopen($newpathofdestfile, 'w');
400 if ($fp) {
401 fwrite($fp, $str);
402 fclose($fp);
403 dolChmod($newpathofdestfile);
404
405 $this->_lastfetchdate = $nowgmt;
406 } else {
407 print 'Error, failed to open file '.$newpathofdestfile.' for write';
408 }
409 }
410
411 unset($str); // Free memory
412
413 if (empty($rss->_format)) { // If format not detected automatically
414 $rss->_format = 'rss';
415 if (empty($rss->channel)) {
416 $rss->_format = 'atom';
417 }
418 }
419
420 $items = array();
421
422 // Save description entries
423 if ($rss->_format == 'rss') {
424 //var_dump($rss);
425 if (getDolGlobalString('EXTERNALRSS_USE_SIMPLEXML')) {
426 if (!empty($rss->channel->language)) {
427 $this->_language = sanitizeVal((string) $rss->channel->language);
428 }
429 if (!empty($rss->channel->generator)) {
430 $this->_generator = sanitizeVal((string) $rss->channel->generator);
431 }
432 if (!empty($rss->channel->copyright)) {
433 $this->_copyright = sanitizeVal((string) $rss->channel->copyright);
434 }
435 if (!empty($rss->channel->lastbuilddate)) {
436 $this->_lastbuilddate = sanitizeVal((string) $rss->channel->lastbuilddate);
437 }
438 if (!empty($rss->channel->image->url[0])) {
439 $this->_imageurl = sanitizeVal((string) $rss->channel->image->url[0]);
440 }
441 if (!empty($rss->channel->link)) {
442 $this->_link = sanitizeVal((string) $rss->channel->link);
443 }
444 if (!empty($rss->channel->title)) {
445 $this->_title = sanitizeVal((string) $rss->channel->title);
446 }
447 if (!empty($rss->channel->description)) {
448 $this->_description = sanitizeVal((string) $rss->channel->description);
449 }
450 } else {
451 //var_dump($rss->channel);
452 if (!empty($rss->channel['language'])) {
453 $this->_language = sanitizeVal((string) $rss->channel['language']);
454 }
455 if (!empty($rss->channel['generator'])) {
456 $this->_generator = sanitizeVal((string) $rss->channel['generator']);
457 }
458 if (!empty($rss->channel['copyright'])) {
459 $this->_copyright = sanitizeVal((string) $rss->channel['copyright']);
460 }
461 if (!empty($rss->channel['lastbuilddate'])) {
462 $this->_lastbuilddate = sanitizeVal((string) $rss->channel['lastbuilddate']);
463 }
464 if (!empty($rss->image['url'])) {
465 $this->_imageurl = sanitizeVal((string) $rss->image['url']);
466 }
467 if (!empty($rss->channel['link'])) {
468 $this->_link = sanitizeVal((string) $rss->channel['link']);
469 }
470 if (!empty($rss->channel['title'])) {
471 $this->_title = sanitizeVal((string) $rss->channel['title']);
472 }
473 if (!empty($rss->channel['description'])) {
474 $this->_description = sanitizeVal((string) $rss->channel['description']);
475 }
476 }
477
478 if (getDolGlobalString('EXTERNALRSS_USE_SIMPLEXML')) {
479 $items = $rss->channel->item; // With simplexml
480 } else {
481 $items = $rss->items; // With xmlparse
482 }
483 //var_dump($items);exit;
484 } elseif ($rss->_format == 'atom') {
485 //var_dump($rss);
486 if (getDolGlobalString('EXTERNALRSS_USE_SIMPLEXML')) {
487 if (!empty($rss->generator)) {
488 $this->_generator = sanitizeVal((string) $rss->generator);
489 }
490 if (!empty($rss->lastbuilddate)) {
491 $this->_lastbuilddate = sanitizeVal((string) $rss->modified);
492 }
493 if (!empty($rss->link->href)) {
494 $this->_link = sanitizeVal((string) $rss->link->href);
495 }
496 if (!empty($rss->title)) {
497 $this->_title = sanitizeVal((string) $rss->title);
498 }
499 if (!empty($rss->description)) {
500 $this->_description = sanitizeVal((string) $rss->description);
501 }
502 } else {
503 //if (!empty($rss->channel['rss_language'])) $this->_language = (string) $rss->channel['rss_language'];
504 if (!empty($rss->channel['generator'])) {
505 $this->_generator = sanitizeVal((string) $rss->channel['generator']);
506 }
507 //if (!empty($rss->channel['rss_copyright'])) $this->_copyright = (string) $rss->channel['rss_copyright'];
508 if (!empty($rss->channel['modified'])) {
509 $this->_lastbuilddate = sanitizeVal((string) $rss->channel['modified']);
510 }
511 //if (!empty($rss->image['rss_url'])) $this->_imageurl = (string) $rss->image['rss_url'];
512 if (!empty($rss->channel['link'])) {
513 $this->_link = sanitizeVal((string) $rss->channel['link']);
514 }
515 if (!empty($rss->channel['title'])) {
516 $this->_title = sanitizeVal((string) $rss->channel['title']);
517 }
518 //if (!empty($rss->channel['rss_description'])) $this->_description = (string) $rss->channel['rss_description'];
519
520 if (!empty($rss->channel)) {
521 $this->_imageurl = sanitizeVal($this->getAtomImageUrl($rss->channel));
522 }
523 }
524 if (getDolGlobalString('EXTERNALRSS_USE_SIMPLEXML')) {
525 $tmprss = xml2php($rss);
526 $items = $tmprss['entry'];
527 } else {
528 // With simplexml
529 $items = $rss->items; // With xmlparse
530 }
531 //var_dump($items);exit;
532 }
533
534 // The links of the feed are used as href: keep only http(s) links (a "javascript:" link would run in Dolibarr)
535 if (!empty($this->_link) && !preg_match('/^https?:\/\//i', $this->_link)) {
536 $this->_link = '';
537 }
538
539 $i = 0;
540
541 // Loop on each record
542 if (is_array($items)) {
543 foreach ($items as $item) {
544 //var_dump($item);exit;
545 if (is_array($item)) {
546 // Optional elements of an item, to avoid PHP warnings when they are missing
547 $item += array('link' => '', 'title' => '', 'description' => '', 'guid' => '', 'author' => '', 'author_name' => '');
548 }
549 if ($rss->_format == 'rss') {
550 if (getDolGlobalString('EXTERNALRSS_USE_SIMPLEXML')) {
551 $itemLink = sanitizeVal((string) $item->link);
552 $itemTitle = sanitizeVal((string) $item->title);
553 $itemDescription = sanitizeVal((string) $item->description);
554 $itemPubDate = sanitizeVal((string) $item->pubDate);
555 $itemId = '';
556 $itemAuthor = '';
557 } else {
558 $itemLink = sanitizeVal((string) $item['link']);
559 $itemTitle = sanitizeVal((string) $item['title']);
560 $itemDescription = sanitizeVal((string) $item['description']);
561 $itemPubDate = sanitizeVal((string) $item['pubdate']);
562 $itemId = sanitizeVal((string) $item['guid']);
563 $itemAuthor = sanitizeVal((string) ($item['author'] ?? ''));
564 }
565
566 // Loop on each category
567 $itemCategory = array();
568 if (!empty($item->category) && is_array($item->category)) {
569 foreach ($item->category as $cat) {
570 $itemCategory[] = (string) $cat;
571 }
572 }
573 } elseif ($rss->_format == 'atom') {
574 $itemLink = (isset($item['link']) ? sanitizeVal((string) $item['link']) : '');
575 $itemTitle = sanitizeVal((string) $item['title']);
576 $itemDescription = sanitizeVal($this->getAtomItemDescription($item));
577 $itemPubDate = sanitizeVal((string) $item['created']);
578 $itemId = sanitizeVal((string) $item['id']);
579 $itemAuthor = sanitizeVal((string) ($item['author'] ? $item['author'] : $item['author_name']));
580 $itemCategory = array();
581 } else {
582 $itemLink = '';
583 $itemTitle = '';
584 $itemDescription = '';
585 $itemPubDate = '';
586 $itemId = '';
587 $itemAuthor = '';
588 $itemCategory = array();
589 print 'ErrorBadFeedFormat';
590 }
591
592 if (!preg_match('/^https?:\/\//i', $itemLink)) {
593 $itemLink = '';
594 }
595
596 // Add record to result array
597 $this->_rssarray[$i] = array(
598 'link' => $itemLink,
599 'title' => $itemTitle,
600 'description' => $itemDescription,
601 'pubDate' => $itemPubDate,
602 'category' => $itemCategory,
603 'id' => $itemId,
604 'author' => $itemAuthor
605 );
606 //var_dump($this->_rssarray);
607
608 $i++;
609
610 if ($maxNb > 0 && $i >= $maxNb) {
611 break; // We get all records we want (0 = no limit)
612 }
613 }
614 }
615
616 return 1;
617 } else {
618 $this->error = 'ErrorFailedToLoadRSSFile';
619 return -1;
620 }
621 }
622
623
624
625 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
634 public function feed_start_element($p, $element, $attrs)
635 {
636 // phpcs:enable
637 $el = $element = strtolower($element);
638 $attrs = array_change_key_case($attrs, CASE_LOWER);
639
640 // check for a namespace, and split if found
641 $ns = false;
642 if (strpos($element, ':')) {
643 list($ns, $el) = explode(':', $element, 2);
644 }
645 if ($ns and $ns != 'rdf') {
646 $this->current_namespace = $ns;
647 }
648
649 // if feed type isn't set, then this is first element of feed identify feed from root element
650 if (empty($this->_format)) {
651 if ($el == 'rdf') {
652 $this->_format = 'rss';
653 $this->feed_version = '1.0';
654 } elseif ($el == 'rss') {
655 $this->_format = 'rss';
656 $this->feed_version = (string) ($attrs['version'] ?? '');
657 } elseif ($el == 'feed') {
658 $this->_format = 'atom';
659 $this->feed_version = (string) ($attrs['version'] ?? '');
660 $this->inchannel = true;
661 }
662 return;
663 }
664
665 if ($el == 'channel') {
666 $this->inchannel = true;
667 } elseif ($el == 'item' || $el == 'entry') {
668 $this->initem = true;
669 if (isset($attrs['rdf:about'])) {
670 $this->current_item['about'] = $attrs['rdf:about'];
671 }
672 } elseif ($this->_format == 'rss' && $this->current_namespace == '' && $el == 'textinput') {
673 // if we're in the default namespace of an RSS feed,
674 // record textinput or image fields
675 $this->intextinput = true;
676 } elseif ($this->_format == 'rss' && $this->current_namespace == '' && $el == 'image') {
677 $this->inimage = true;
678 } elseif ($this->_format == 'atom' && in_array($el, $this->_CONTENT_CONSTRUCTS)) {
679 // handle atom content constructs
680 // avoid clashing w/ RSS mod_content
681 if ($el == 'content') {
682 $el = 'atom_content';
683 }
684
685 $this->incontent = $el;
686 } elseif ($this->_format == 'atom' && $this->incontent) {
687 // if inside an Atom content construct (e.g. content or summary) field treat tags as text
688 // if tags are inlined, then flatten
689 $attrs_str = implode(' ', array_map('rss_map_attrs', array_keys($attrs), array_values($attrs)));
690
691 $this->append_content("<$element $attrs_str>");
692
693 array_unshift($this->stack, $el);
694 } elseif ($this->_format == 'atom' && $el == 'link') {
695 // Atom support many links per containing element.
696 // Magpie treats link elements of type rel='alternate'
697 // as being equivalent to RSS's simple link element.
698 if (isset($attrs['rel']) && $attrs['rel'] == 'alternate') {
699 $link_el = 'link';
700 } elseif (!isset($attrs['rel'])) {
701 $link_el = 'link';
702 } else {
703 $link_el = 'link_'.$attrs['rel'];
704 }
705
706 $this->append($link_el, $attrs['href']);
707 } else {
708 // set stack[0] to current element
709 array_unshift($this->stack, $el);
710 }
711 }
712
713
714 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
722 public function feed_cdata($p, $text)
723 {
724 // phpcs:enable
725 if ($this->_format == 'atom' and $this->incontent) {
726 $this->append_content($text);
727 } else {
728 $current_el = implode('_', array_reverse($this->stack));
729 $this->append($current_el, $text);
730 }
731 }
732
733 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
741 public function feed_end_element($p, $el)
742 {
743 // phpcs:enable
744 $el = strtolower($el);
745
746 if ($el == 'item' or $el == 'entry') {
747 $this->items[] = $this->current_item;
748 $this->current_item = array();
749 $this->initem = false;
750 } elseif ($this->_format == 'rss' and $this->current_namespace == '' and $el == 'textinput') {
751 $this->intextinput = false;
752 } elseif ($this->_format == 'rss' and $this->current_namespace == '' and $el == 'image') {
753 $this->inimage = false;
754 } elseif ($this->_format == 'atom' and in_array($el, $this->_CONTENT_CONSTRUCTS)) {
755 $this->incontent = false;
756 } elseif ($el == 'channel' or $el == 'feed') {
757 $this->inchannel = false;
758 } elseif ($this->_format == 'atom' and $this->incontent) {
759 // balance tags properly
760 // note: i don't think this is actually necessary
761 if ($this->stack[0] == $el) {
762 $this->append_content("</$el>");
763 } else {
764 $this->append_content("<$el />");
765 }
766
767 array_shift($this->stack);
768 } else {
769 array_shift($this->stack);
770 }
771
772 $this->current_namespace = false;
773 }
774
775
783 public function concat(&$str1, $str2 = "")
784 {
785 if (!isset($str1)) {
786 $str1 = "";
787 }
788 $str1 .= $str2;
789 return $str1;
790 }
791
792 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
799 public function append_content($text)
800 {
801 // phpcs:enable
802 if (!empty($this->initem)) {
803 $this->concat($this->current_item[$this->incontent], $text);
804 } elseif (!empty($this->inchannel)) {
805 $this->concat($this->channel[$this->incontent], $text);
806 }
807 }
808
816 public function append($el, $text)
817 {
818 if (!$el) {
819 return;
820 }
821 if (!empty($this->current_namespace)) {
822 if (!empty($this->initem)) {
823 $this->concat($this->current_item[$this->current_namespace][$el], $text);
824 } elseif (!empty($this->inchannel)) {
825 $this->concat($this->channel[$this->current_namespace][$el], $text);
826 } elseif (!empty($this->intextinput)) {
827 $this->concat($this->textinput[$this->current_namespace][$el], $text);
828 } elseif (!empty($this->inimage)) {
829 $this->concat($this->image[$this->current_namespace][$el], $text);
830 }
831 } else {
832 if (!empty($this->initem)) {
833 $this->concat($this->current_item[$el], $text);
834 } elseif (!empty($this->intextinput)) {
835 $this->concat($this->textinput[$el], $text);
836 } elseif (!empty($this->inimage)) {
837 $this->concat($this->image[$el], $text);
838 } elseif (!empty($this->inchannel)) {
839 $this->concat($this->channel[$el], $text);
840 }
841 }
842 }
843
851 private function getAtomItemDescription(array $item, $maxlength = 500)
852 {
853 $result = "";
854
855 if (isset($item['summary'])) {
856 $result = $item['summary'];
857 } elseif (isset($item['atom_content'])) {
858 $result = $item['atom_content'];
859 }
860
861 // remove all HTML elements that can possible break the maximum size of a tooltip,
862 // like headings, image, video etc. and allow only simple style elements
863 $result = strip_tags($result, "<br><p><ul><ol><li>");
864
865 $result = str_replace("\n", "", $result);
866
867 if (strlen($result) > $maxlength) {
868 $result = substr($result, 0, $maxlength);
869 $result .= "...";
870 }
871
872 return $result;
873 }
874
881 private function getAtomImageUrl(array $feed)
882 {
883 if (isset($feed['icon'])) {
884 return $feed['logo'];
885 }
886
887 if (isset($feed['icon'])) {
888 return $feed['logo'];
889 }
890
891 if (isset($feed['webfeeds:logo'])) {
892 return $feed['webfeeds:logo'];
893 }
894
895 if (isset($feed['webfeeds:icon'])) {
896 return $feed['webfeeds:icon'];
897 }
898
899 if (isset($feed['webfeeds:wordmark'])) {
900 return $feed['webfeeds:wordmark'];
901 }
902
903 return "";
904 }
905}
906
914function rss_map_attrs($k, $v)
915{
916 return "$k=\"$v\"";
917}
918
925function xml2php($xml)
926{
927 $threads = 0;
928 $tab = false;
929 $array = array();
930 foreach ($xml->children() as $key => $value) {
931 '@phan-var-force SimpleXMLElement $value';
932 $child = xml2php($value);
933
934 //To deal with the attributes
935 foreach ($value->attributes() as $ak => $av) {
936 $child[$ak] = (string) $av;
937 }
938
939 //Let see if the new child is not in the array
940 if ($tab === false && array_key_exists($key, $array)) {
941 //If this element is already in the array we will create an indexed array
942 $tmp = $array[$key];
943 $array[$key] = null;
944 $array[$key][] = $tmp;
945 $array[$key][] = $child;
946 $tab = true;
947 } elseif ($tab === true) {
948 //Add an element in an existing array
949 $array[$key][] = $child;
950 } else {
951 //Add a simple element
952 $array[$key] = $child;
953 }
954
955 $threads++;
956 }
957
958
959 if ($threads == 0) {
960 return (string) $xml;
961 }
962
963 return $array;
964}
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())
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.