function getrss($url,&$items) {
global $channel;
global $use_cache,$cache_folder,$cache_valid;
if($use_cache) {
$cache_filename=$cache_folder."/".md5($url).".rss";
if(file_exists($cache_filename)) {
$t=filemtime($cache_filename);
$cache_create=((!$t)||($t<strtotime("now")-60*$cache_valid)); }
else
$cache_create=true;
if($cache_create) {
//cache not valid - create it again
$simple = file($url);
$f=fopen($cache_filename,"w");
for($i=0;$i<count($simple);$i++)
fwrite($f,$simple[$i]);
fclose($f);
$simple=implode('',$simple);
}
else
$simple = implode('',file($cache_filename));
}
else
$simple = implode('',file($url));
// определение кодировки фида
$encoding = array(
"MacArabic" => "Arabic (Macintosh)",
"CP1256" => "Arabic (Windows)",
"ISO-8859-2" => "Central European (ISO-8859-2)",
"MacCentralEurope" => "Central European (MacCE)",
"CP1250" => "Central European (Windows1250)",
"ISO-8859-5" => "Cyrillic (ISO-8859-5)",
"KOI8-R" => "Cyrillic (KOI8-R)",
"KOI8-U" => "Cyrillic (KOI8-U)",
"MacCyrillic" => "Cyrillic (MacCyrillic)",
"CP1251" => "Cyrillic (Windows1251)",
"Windows-1251" => "Cyrillic (Windows1251)",
"ISO-8859-7" => "Greek (ISO-8859-7)",
"MacGreek" => "Greek (MacGreek)",
"CP1253" => "Greek (Windows1253)",
"CP1255" => "Hebrew (Windows)",
"MacHebrew" => "Hebrew (Macintosh)",
"EUC-KR" => "Korean (EUC-KR)",
"SHIFT_JIS" => "Japanese (Shift_JIS)",
"EUC-JP" => "Japanese (EUC)",
"ISO-2022-JP" => "Japanese (JIS)",
"HZ" => "Simplified Chinese (HZ)",
"GB2312" => "Simplified Chinese (gb2312)",
"BIG5" => "Traditional Chinese (big5)",
"MacThai" => "Thai (Macintosh)",
"CP874" => "Thai (Windows)",
"ISO-8859-5" => "Turkish (Latin5)",
"MacTurkish" => "Turkish (Macintosh)",
"CP1254" => "Turkish (Windows)",
"UTF-8" => "UTF-8",
"ISO-8859-1" => "Western (Latin1)",
"Macintosh" => "Western (Macintosh)",
"CP1252" => "Western (Windows 1252)"
);
foreach ($encoding as $valenc => $item) {
if (preg_match("/$valenc/i", $simple)) $enc=$valenc;
}
echo "<a href='".$url."'>".$enc." OK</a> ";
$p = xml_parser_create();
xml_parse_into_struct($p,$simple,$vals,$index);
xml_parser_free($p);
$type=0;
$tmp[]=array("","","");
$id=0;
for($i=0;$i<count($vals);$i++) {
$vals[$i]['value']=convert_to_utf($enc, $vals[$i]['value']);
if(($vals[$i]['tag']=="CHANNEL")&&($vals[$i]['type']=="open")) $id=$vals[$i]['level']+1;
if(($type==0)&&($id==$vals[$i]['level']))
switch($vals[$i]['tag']) {
case "TITLE": $channel[0]=$vals[$i]['value']; break;
case "LINK": $channel[1]=$vals[$i]['value']; break;
case "DESCRIPTION": $channel[2]=$vals[$i]['value']; break;
case "COPYRIGHT":
case "DC:RIGHTS": $channel[3]=$vals[$i]['value']; break;
case "MANAGINGEDITOR":
case "DC:PUBLISHER": $channel[4]=$vals[$i]['value']; break;
case "PUBDATE":
case "DC:DATE": $channel[5]=$vals[$i]['value']; break;
}
else switch($vals[$i]['tag']) {
case "TITLE": $tmp[0]=$vals[$i]['value']; break;
case "LINK": $tmp[1]=$vals[$i]['value']; break;
case "DESCRIPTION": $tmp[2]=$vals[$i]['value']; break;
}
if($vals[$i]['tag']=="ITEM") {
if(($vals[$i]['type']=="open")&&($type==0)) $type=1;
if($vals[$i]['type']=="close") {
$items[]=$tmp;
$tmp[0]="";
$tmp[1]="";
$tmp[2]="";
}
}
}
//print_r($channel);
//print_r($items);
} // end function getrss
function convert_to_utf($encoding, $data)
{
if (function_exists('iconv')) {
$out = iconv($encoding, 'utf-8', $data);
}
else if (function_exists('mb_convert_encoding')) {
$out = mb_convert_encoding($data, 'utf-8', $encoding);
}
else if (function_exists('recode_string')) {
$out = recode_string($encoding .'..utf-8', $data);
}
else {
watchdog('php', t("Unsupported encoding '%s'. Please install iconv, GNU recode or mbstring for PHP.", array('%s' => $encoding)), WATCHDOG_ERROR);
return FALSE;
}
return $out;
}