function GetRSSFeed($url, $cache, $showit) { global $carpconf, $CarpRedirs; $carpconf['desctags'] = preg_replace("/(^\\|)|(\\|\$)/", '', preg_replace("/\\|+/", "|", preg_replace("#/?(script|embed|object|applet|iframe)#i", '', $carpconf['descriptiontags']))); $carpconf['desctags'] = str_replace('|', '\\b|', $carpconf['desctags']) . '\\b'; // 3 lines for backwards compatibility if ($carpconf['corder'] !== false) { $carpconf['cborder'] = $carpconf['corder']; } if ($carpconf['bc'] !== false) { $carpconf['bcb'] = $carpconf['bc']; } if ($carpconf['ac'] !== false) { $carpconf['acb'] = $carpconf['ac']; } $rss_parser = new RSSParser(); $rss_parser->showit = $showit; $rss_parser->channelborder = explode(',', preg_replace('/[^a-z0-9,]/', '', strtolower($carpconf['cborder']))); $rss_parser->channelaorder = explode(',', preg_replace('/[^a-z0-9,]/', '', strtolower($carpconf['caorder']))); $rss_parser->SetItemOrder($carpconf['iorder']); // the next 2 lines are for backward compatibility and will eventually be removed if ($carpconf['ilinktarget'] != '-1') { $carpconf['linktarget'] = $carpconf['ilinktarget']; } else { if ($carpconf['clinktarget'] != '-1') { $carpconf['linktarget'] = $carpconf['clinktarget']; } } if (preg_match("/[^0-9]/", $carpconf['linktarget'])) { $rss_parser->linktargets[$carpconf['linktarget']] = ' target="' . $carpconf['linktarget'] . '"'; } $rss_parser->filterinfield = array(); if (strlen($carpconf['filterin'])) { $rss_parser->filterin = explode('|', strtolower($carpconf['filterin'])); for ($i = count($rss_parser->filterin) - 1; $i >= 0; $i--) { if (strpos($rss_parser->filterin[$i], ':') !== false) { list($rss_parser->filterinfield[$i], $rss_parser->filterin[$i]) = explode(':', $rss_parser->filterin[$i], 2); } else { $rss_parser->filterinfield[$i] = ''; } } } else { $rss_parser->filterin = array(); } $rss_parser->filteroutfield = array(); if (strlen($carpconf['filterout'])) { $rss_parser->filterout = explode('|', strtolower($carpconf['filterout'])); for ($i = count($rss_parser->filterout) - 1; $i >= 0; $i--) { if (strpos($rss_parser->filterout[$i], ':') !== false) { list($rss_parser->filteroutfield[$i], $rss_parser->filterout[$i]) = explode(':', $rss_parser->filterout[$i], 2); } else { $rss_parser->filteroutfield[$i] = ''; } } } else { $rss_parser->filterout = array(); } /********************************************************* * Note (2006-10-13, Adam Franco) * * I have reworked this code so that the data is fetched * first and the character encoding checked before the * parser is created. As well, if a non-PHP-Supported * encoding is found, the data will first be converted to * UTF-8 using iconv. *********************************************************/ if ($fp = OpenRSSFeed($url)) { while ($data = fread($fp, 4096)) { // Set up the XML Parser if (!isset($xml_parser)) { // If an input enconding is specified, use that if ($carpconf['encodingin']) { $xml_parser = xml_parser_create(strtoupper($carpconf['encodingin'])); } else { if (preg_match('/<\\?xml[^>]*encoding=[\'"]([^\'"]+)[\'"][^>]*\\?>/i', $data, $matches)) { $sourceEncoding = strtoupper($matches[1]); // print "<pre>".basename(__FILE__)." Line ".__LINE__.":\nEncoding found: ".$sourceEncoding."</pre>"; if ($sourceEncoding == 'UTF-8' || $sourceEncoding == 'ISO-8859-1' || $sourceEncoding == 'ASCII') { $xml_parser = xml_parser_create($sourceEncoding); unset($sourceEncoding); } else { $xml_parser = xml_parser_create("UTF-8"); } } else { // print "<pre>".basename(__FILE__)." Line ".__LINE__.":\nNo encoding found, assuming UTF-8</pre>"; $xml_parser = xml_parser_create("UTF-8"); } } if (strlen($carpconf['encodingout'])) { xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, $carpconf['encodingout']); } xml_set_object($xml_parser, $rss_parser); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); $CarpRedirs = array(); $rss_parser->PrepTagPairs($carpconf['desctags']); } if (isset($sourceEncoding) && function_exists("iconv")) { // print "<pre>Converting $sourceEncoding to UTF-8</pre>"; $data = iconv($sourceEncoding, "UTF-8", $data); } // Doubly escape any necessary ampersands. $data = preg_replace("/&(?!lt|gt|amp|apos|quot|nbsp)(.*\\b)/is", "&\\1\\2", $data); if (!xml_parse($xml_parser, $data, feof($fp))) { CarpError("XML error: " . xml_error_string(xml_get_error_code($xml_parser)) . " at line " . xml_get_current_line_number($xml_parser)); fclose($fp); xml_parser_free($xml_parser); return; } $data = ''; } fclose($fp); if ($showit) { if ($carpconf['shownoitems'] && !$rss_parser->itemcount) { CarpOutput($carpconf['noitems']); } else { CarpOutput($rss_parser->top . $carpconf['bitems'] . $rss_parser->body . $carpconf['aitems'] . $rss_parser->bottom . $carpconf['poweredby']); } } if ($cache) { if ($cfp = OpenCacheWrite()) { if ($carpconf['shownoitems'] && !$rss_parser->itemcount) { fwrite($cfp, $carpconf['noitems']); } else { fwrite($cfp, ($showit ? $rss_parser->top . $carpconf['bitems'] : 'cb: :' . $rss_parser->top . "\n" . 'ca: :' . $rss_parser->bottom . "\n") . $rss_parser->body . ($showit ? $carpconf['aitems'] . $rss_parser->bottom . $carpconf['poweredby'] : '')); } CloseCacheWrite($cfp); } else { CarpError("Unable to create/open RSS cache file.", 0); } } xml_parser_free($xml_parser); } else { if ($showit && strlen($carpconf['cachefile']) && file_exists($carpconf['cachefile'])) { CarpOutput(file($carpconf['cachefile'])); } else { if ($showit) { CarpError('Can\'t open remote newsfeed.', 0); } } } }
function GetRSSFeed($url, $cache, $showit) { global $carpconf, $CaRPRedirs; $carpconf['desctags'] = preg_replace("/\\|/", '\\b|', $carpconf['descriptiontags']) . '\\b'; $xml_parser = xml_parser_create($carpconf['encodingin']); if (strlen($carpconf['encodingout'])) { xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, $carpconf['encodingout']); } $rss_parser = new RSSParser(); $rss_parser->showit = $showit; //!!! remove the next line eventually if (strcmp($carpconf['newwindow'], '0')) { $carpconf['linktarget'] = $carpconf['newwindow']; } if (preg_match("/[^0-9]/", $carpconf['linktarget'])) { $rss_parser->linktargets[$carpconf['linktarget']] = ' target="' . $carpconf['linktarget'] . '"'; } $rss_parser->filterinfield = array(); if (strlen($carpconf['filterin'])) { $rss_parser->filterin = explode('|', strtolower($carpconf['filterin'])); for ($i = count($rss_parser->filterin) - 1; $i >= 0; $i--) { if (strpos($rss_parser->filterin[$i], ':') !== false) { list($rss_parser->filterinfield[$i], $rss_parser->filterin[$i]) = explode(':', $rss_parser->filterin[$i], 2); } } } else { $rss_parser->filterin = array(); } $rss_parser->filteroutfield = array(); if (strlen($carpconf['filterout'])) { $rss_parser->filterout = explode('|', strtolower($carpconf['filterout'])); for ($i = count($rss_parser->filterout) - 1; $i >= 0; $i--) { if (strpos($rss_parser->filterout[$i], ':') !== false) { list($rss_parser->filteroutfield[$i], $rss_parser->filterout[$i]) = explode(':', $rss_parser->filterout[$i], 2); } } } else { $rss_parser->filterout = array(); } xml_set_object($xml_parser, $rss_parser); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); $CaRPRedirs = array(); if ($fp = OpenRSSFeed($url)) { $rss_parser->PrepTagPairs($carpconf['descriptiontags']); while ($data = preg_replace("/&(?!lt|gt|amp|apos|quot)(.*\\b)/is", "&\\1\\2", fread($fp, 4096))) { if (!xml_parse($xml_parser, $data, feof($fp))) { CaRPError("XML error: " . xml_error_string(xml_get_error_code($xml_parser)) . " at line " . xml_get_current_line_number($xml_parser)); fclose($fp); xml_parser_free($xml_parser); return; } } fclose($fp); $parser_output = $rss_parser->top . $carpconf['preitems'] . $rss_parser->body . $carpconf['postitems'] . $carpconf['poweredby']; if ($showit) { CarpOutput($parser_output); } if ($cache) { if ($cfp = OpenCacheWrite()) { fwrite($cfp, $showit ? $parser_output : ''); CloseCacheWrite($cfp); } else { CaRPError("Unable to create/open RSS cache file.", 0); } } xml_parser_free($xml_parser); } else { CarpOutput(file($carpconf['cachefile'])); } }