/** * Parse rss URL * * @param string $urlRSS Url to parse * @param int $maxNb Max nb of records to get (0 for no limit) * @param int $cachedelay 0=No cache, nb of seconds we accept cache files (cachedir must also be defined) * @param string $cachedir Directory where to save cache file * @return int <0 if KO, >0 if OK */ public function parser($urlRSS, $maxNb = 0, $cachedelay = 60, $cachedir = '') { global $conf; include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php'; $rss = ''; $str = ''; // This will contain content of feed // Check parameters if (!dol_is_url($urlRSS)) { $this->error = "ErrorBadUrl"; return -1; } $this->_urlRSS = $urlRSS; $newpathofdestfile = $cachedir . '/' . dol_hash($this->_urlRSS, 3); // Force md5 hash (does not contains special chars) $newmask = '0644'; //dol_syslog("RssPArser::parser parse url=".$urlRSS." => cache file=".$newpathofdestfile); $nowgmt = dol_now(); // Search into cache $foundintocache = 0; if ($cachedelay > 0 && $cachedir) { $filedate = dol_filemtime($newpathofdestfile); if ($filedate >= $nowgmt - $cachedelay) { //dol_syslog("RssParser::parser cache file ".$newpathofdestfile." is not older than now - cachedelay (".$nowgmt." - ".$cachedelay.") so we use it."); $foundintocache = 1; $this->_lastfetchdate = $filedate; } else { 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."); } } // Load file into $str if ($foundintocache) { $str = file_get_contents($newpathofdestfile); } else { try { ini_set("user_agent", "Dolibarr ERP-CRM RSS reader"); ini_set("max_execution_time", $conf->global->MAIN_USE_RESPONSE_TIMEOUT); ini_set("default_socket_timeout", $conf->global->MAIN_USE_RESPONSE_TIMEOUT); $opts = array('http' => array('method' => "GET")); if (!empty($conf->global->MAIN_USE_CONNECT_TIMEOUT)) { $opts['http']['timeout'] = $conf->global->MAIN_USE_CONNECT_TIMEOUT; } if (!empty($conf->global->MAIN_PROXY_USE)) { $opts['http']['proxy'] = 'tcp://' . $conf->global->MAIN_PROXY_HOST . ':' . $conf->global->MAIN_PROXY_PORT; } //var_dump($opts);exit; $context = stream_context_create($opts); $str = file_get_contents($this->_urlRSS, false, $context); } catch (Exception $e) { print 'Error retrieving URL ' . $this->urlRSS . ' - ' . $e->getMessage(); } } if ($str !== false) { // Convert $str into xml if (!empty($conf->global->EXTERNALRSS_USE_SIMPLEXML)) { //print 'xx'.LIBXML_NOCDATA; libxml_use_internal_errors(false); $rss = simplexml_load_string($str, "SimpleXMLElement", LIBXML_NOCDATA); } else { $xmlparser = xml_parser_create(''); if (!is_resource($xmlparser)) { $this->error = "ErrorFailedToCreateParser"; return -1; } xml_set_object($xmlparser, $this); xml_set_element_handler($xmlparser, 'feed_start_element', 'feed_end_element'); xml_set_character_data_handler($xmlparser, 'feed_cdata'); $status = xml_parse($xmlparser, $str); xml_parser_free($xmlparser); $rss = $this; //var_dump($rss->_format);exit; } } // If $rss loaded if ($rss) { // Save file into cache if (empty($foundintocache) && $cachedir) { dol_syslog(get_class($this) . "::parser cache file " . $newpathofdestfile . " is saved onto disk."); if (!dol_is_dir($cachedir)) { dol_mkdir($cachedir); } $fp = fopen($newpathofdestfile, 'w'); fwrite($fp, $str); fclose($fp); if (!empty($conf->global->MAIN_UMASK)) { $newmask = $conf->global->MAIN_UMASK; } @chmod($newpathofdestfile, octdec($newmask)); $this->_lastfetchdate = $nowgmt; } unset($str); // Free memory if (empty($rss->_format)) { $rss->_format = 'rss'; if (empty($rss->channel)) { $rss->_format = 'atom'; } } $items = array(); // Save description entries if ($rss->_format == 'rss') { //var_dump($rss); if (!empty($conf->global->EXTERNALRSS_USE_SIMPLEXML)) { if (!empty($rss->channel->language)) { $this->_language = (string) $rss->channel->language; } if (!empty($rss->channel->generator)) { $this->_generator = (string) $rss->channel->generator; } if (!empty($rss->channel->copyright)) { $this->_copyright = (string) $rss->channel->copyright; } if (!empty($rss->channel->lastbuilddate)) { $this->_lastbuilddate = (string) $rss->channel->lastbuilddate; } if (!empty($rss->channel->image->url[0])) { $this->_imageurl = (string) $rss->channel->image->url[0]; } if (!empty($rss->channel->link)) { $this->_link = (string) $rss->channel->link; } if (!empty($rss->channel->title)) { $this->_title = (string) $rss->channel->title; } if (!empty($rss->channel->description)) { $this->_description = (string) $rss->channel->description; } } else { //var_dump($rss->channel); if (!empty($rss->channel['language'])) { $this->_language = (string) $rss->channel['language']; } if (!empty($rss->channel['generator'])) { $this->_generator = (string) $rss->channel['generator']; } if (!empty($rss->channel['copyright'])) { $this->_copyright = (string) $rss->channel['copyright']; } if (!empty($rss->channel['lastbuilddate'])) { $this->_lastbuilddate = (string) $rss->channel['lastbuilddate']; } if (!empty($rss->image['url'])) { $this->_imageurl = (string) $rss->image['url']; } if (!empty($rss->channel['link'])) { $this->_link = (string) $rss->channel['link']; } if (!empty($rss->channel['title'])) { $this->_title = (string) $rss->channel['title']; } if (!empty($rss->channel['description'])) { $this->_description = (string) $rss->channel['description']; } } if (!empty($conf->global->EXTERNALRSS_USE_SIMPLEXML)) { $items = $rss->channel->item; } else { $items = $rss->items; } // With xmlparse //var_dump($items);exit; } else { if ($rss->_format == 'atom') { //var_dump($rss); if (!empty($conf->global->EXTERNALRSS_USE_SIMPLEXML)) { if (!empty($rss->generator)) { $this->_generator = (string) $rss->generator; } if (!empty($rss->lastbuilddate)) { $this->_lastbuilddate = (string) $rss->modified; } if (!empty($rss->link->href)) { $this->_link = (string) $rss->link->href; } if (!empty($rss->title)) { $this->_title = (string) $rss->title; } if (!empty($rss->description)) { $this->_description = (string) $rss->description; } } else { //if (!empty($rss->channel['rss_language'])) $this->_language = (string) $rss->channel['rss_language']; if (!empty($rss->channel['generator'])) { $this->_generator = (string) $rss->channel['generator']; } //if (!empty($rss->channel['rss_copyright'])) $this->_copyright = (string) $rss->channel['rss_copyright']; if (!empty($rss->channel['modified'])) { $this->_lastbuilddate = (string) $rss->channel['modified']; } //if (!empty($rss->image['rss_url'])) $this->_imageurl = (string) $rss->image['rss_url']; if (!empty($rss->channel['link'])) { $this->_link = (string) $rss->channel['link']; } if (!empty($rss->channel['title'])) { $this->_title = (string) $rss->channel['title']; } //if (!empty($rss->channel['rss_description'])) $this->_description = (string) $rss->channel['rss_description']; } if (!empty($conf->global->EXTERNALRSS_USE_SIMPLEXML)) { $tmprss = xml2php($rss); $items = $tmprss['entry']; } else { $items = $rss->items; } // With xmlparse //var_dump($items);exit; } } $i = 0; // Loop on each record if (is_array($items)) { foreach ($items as $item) { //var_dump($item);exit; if ($rss->_format == 'rss') { if (!empty($conf->global->EXTERNALRSS_USE_SIMPLEXML)) { $itemLink = (string) $item->link; $itemTitle = (string) $item->title; $itemDescription = (string) $item->description; $itemPubDate = (string) $item->pubDate; $itemId = ''; $itemAuthor = ''; } else { $itemLink = (string) $item['link']; $itemTitle = (string) $item['title']; $itemDescription = (string) $item['description']; $itemPubDate = (string) $item['pubdate']; $itemId = (string) $item['guid']; $itemAuthor = (string) $item['author']; } // Loop on each category $itemCategory = array(); if (is_array($item->category)) { foreach ($item->category as $cat) { $itemCategory[] = (string) $cat; } } } else { if ($rss->_format == 'atom') { if (!empty($conf->global->EXTERNALRSS_USE_SIMPLEXML)) { $itemLink = isset($item['link']['href']) ? (string) $item['link']['href'] : ''; $itemTitle = (string) $item['title']; $itemDescription = (string) $item['summary']; $itemPubDate = (string) $item['created']; $itemId = (string) $item['id']; $itemAuthor = (string) ($item['author'] ? $item['author'] : $item['author_name']); } else { $itemLink = isset($item['link']['href']) ? (string) $item['link']['href'] : ''; $itemTitle = (string) $item['title']; $itemDescription = (string) $item['summary']; $itemPubDate = (string) $item['created']; $itemId = (string) $item['id']; $itemAuthor = (string) ($item['author'] ? $item['author'] : $item['author_name']); } } else { print 'ErrorBadFeedFormat'; } } // Add record to result array $this->_rssarray[$i] = array('link' => $itemLink, 'title' => $itemTitle, 'description' => $itemDescription, 'pubDate' => $itemPubDate, 'category' => $itemCategory, 'id' => $itemId, 'author' => $itemAuthor); //var_dump($this->_rssarray); $i++; if ($i > $maxNb) { break; } // We get all records we want } } return 1; } else { $this->error = 'ErrorFailedToLoadRSSFile'; return -1; } }
if ($actionsave) { $db->begin(); $i = 1; $errorsaved = 0; $error = 0; $tabparam = array(); // Save agendas while ($i <= $MAXAGENDA) { $name = trim(GETPOST('AGENDA_EXT_NAME_' . $id . '_' . $i, 'alpha')); $src = trim(GETPOST('AGENDA_EXT_SRC_' . $id . '_' . $i, 'alpha')); $color = trim(GETPOST('AGENDA_EXT_COLOR_' . $id . '_' . $i, 'alpha')); if ($color == '-1') { $color = ''; } $enabled = trim(GETPOST('AGENDA_EXT_ENABLED_' . $id . '_' . $i, 'alpha')); if (!empty($src) && !dol_is_url($src)) { setEventMessage($langs->trans("ErrorParamMustBeAnUrl"), 'errors'); $error++; $errorsaved++; break; } $tabparam['AGENDA_EXT_NAME_' . $id . '_' . $i] = $name; $tabparam['AGENDA_EXT_SRC_' . $id . '_' . $i] = $src; $tabparam['AGENDA_EXT_COLOR_' . $id . '_' . $i] = $color; $tabparam['AGENDA_EXT_ENABLED_' . $id . '_' . $i] = $enabled; $i++; } if (!$error) { $result = dol_set_user_param($db, $conf, $fuser, $tabparam); if (!$result > 0) { $error++;
/** * testDolOther * * @return boolean */ public function testDolOther() { global $conf,$user,$langs,$db; $conf=$this->savconf; $user=$this->savuser; $langs=$this->savlangs; $db=$this->savdb; $url='http://www.dolibarr.org'; $result=dol_is_url($url); print __METHOD__." result=".$result."\n"; $this->assertTrue($result); $url='https://www.dolibarr.org'; $result=dol_is_url($url); print __METHOD__." result=".$result."\n"; $this->assertTrue($result); $url='file://www.dolibarr.org/download/file.zip'; $result=dol_is_url($url); print __METHOD__." result=".$result."\n"; $this->assertTrue($result); return $result; }