array # for rss type feed, each item must have the 'title', 'description', 'pubDate' and 'link' keys # for atom type feed, info must have the 'title', 'id', 'updated, 'link' & 'content' keys function makeRSSdate($date=NULL){// format DD-MM-YY HH:MM => rfc2822 (rss compatible) if ($date){ date_default_timezone_set('Europe/Paris'); $time = strtotime($date); return date("r", $time); } } function no_nl($string){$tab = array( CHR(13) => " ", CHR(10) => " " ); return strtr($string,$tab); } function array2feed($array=null){ if (!$array){return false;} if (empty($array['infos']['type'])){$array['infos']['type']='rss';}else{$array['infos']['type']=strtolower($array['infos']['type']);} if (empty($array['infos']['description'])){$array['infos']['description']='';} $r="\n";$t="\t"; $tpl=array('rss'=>array(),'atom'=>array()); $tpl['rss']['header']=''.$r.'' .$r.$t.''.$r; $tpl['atom']['header']=''.$r; $tpl['rss']['footer']=$t.''.$r; $tpl['atom']['footer']=''.$r; $tpl['rss']['content-type']='Content-Type: application/rss+xml; charset=utf-8'; $tpl['atom']['content-type']='Content-Type: application/atom+xml; charset=utf-8'; header($tpl[$array['infos']['type']]['content-type']); $feed=$tpl[$array['infos']['type']]['header']; //create the feed's info content foreach($array['infos'] as $key=>$value){ if ($array['infos']['type']=='atom'){ // ATOM if ($key=='link'){$feed.=$t.$t.''.$r;} elseif ($key=='author'){$feed.=$t.$t.''.$value.''.$r;} elseif ($key=='licence'){$feed.=$t.$t.'<'.$key.' href="'.$value.'" rel="license"/>'.$r;} // in atom feed, licence is the link to the licence type elseif ($key!='version'&&$key!='type'){$feed.=$t.$t.'<'.$key.'>'.$value.''.$r;} }else{ // RSS if ($key!='version'&&$key!='type'){$feed.=$t.$t.'<'.$key.'>'.$value.''.$r;} } } //then the items content foreach ($array['items'] as $item){ if ($array['infos']['type']=='atom'){ $feed.=$t.$t.$t.''.$r;}else{$feed.=$t.$t.$t.''.$r;} foreach($item as $key=>$value){ if ($array['infos']['type']=='atom'){ // ATOM if ($key=='link'){$feed.=$t.$t.$t.$t.''.$r;} elseif ($key=='content'){$feed.=$t.$t.$t.$t.''.htmlspecialchars($value).''.$r;} elseif (!empty($value)){$feed.=$t.$t.$t.$t.'<'.$key.'>'.no_nl($value).''.$r;} }else{ // RSS if ($key=='pubDate'||$key=='title'||$key=='link'){$feed.=$t.$t.$t.$t.'<'.$key.'>'.htmlspecialchars($value).''.$r;} elseif ($key=='date'){} elseif($key=='guid'){ $feed.=$t.$t.$t.$t.''.$r;} elseif(!empty($value)){$feed.=$t.$t.$t.$t.'<'.$key.'>'.$r;} } } if ($array['infos']['type']=='atom'){ $feed.=$t.$t.$t.''.$r;}else{$feed.=$t.$t.$t.''.$r;} } $feed.=$tpl[$array['infos']['type']]['footer']; echo $feed; } ?>