<?
$url="http://plone.gufi.org/blogs/rionda_blog/";
$author="rionda";
if ($url) {
  parse_html($url);
} else {
  show_form();
}


function parse_html($url){
	global $author;
  $allowable_tags = "<A><B><BR><BLOCKQUOTE><CENTER><DD><DL><DT><HR><I><IMG><LI><OL><P><PRE><U><UL>";

  $urlparts = parse_url($url);
  if ($urlparts[path] == "") $url .= "/";

  if ($fp = @fopen($url, "r")) {
    while (!feof($fp)) {
      $data .= fgets($fp, 128);
    }
    fclose($fp);
  }

//  print "<pre>";
//  print htmlentities($data);  

  eregi("<h1 class=\"documentFirstHeading\">(.*)</h1>", $data, $title);
  $channel_title = trim(ereg_replace("<img.*/>","",$title[1])) ;

  $itemregexp = "%simpleBlogTitle *\" *>(.+?)</div>%is";
  $match_count = preg_match_all($itemregexp, $data, $titles);
  $match_count = ($match_count > 25) ? 25 : $match_count;
 
  $itemregexp = "%simpleBlogTitle *\" *>(.+?)</div>%is";
  $match_count = preg_match_all($itemregexp, $data, $links);
  $match_count = ($match_count > 25) ? 25 : $match_count;
  
  $itemregexp = "%BlogByLine *\" *>(.+?)</div>%is";
  $match_count = preg_match_all($itemregexp, $data, $dates);
  $match_count = ($match_count > 25) ? 25 : $match_count;

  $itemregexp = "%description *\" *> *</div>(.*?)<div class=\"BlogCommandLine\">\n%is";
  $match_count = preg_match_all($itemregexp, $data, $texts);
  $match_count = ($match_count > 25) ? 25 : $match_count;

  header("Content-Type: text/xml");
$output = '<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF
 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
 xmlns="http://purl.org/rss/1.0/"
 xmlns:dc="http://purl.org/dc/elements/1.1/"
 xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel rdf:about="'.$url.'">
<title>'.$channel_title.'</title>
<link>'.$url.'</link>
<description>'.$channel_title.': ultimi post</description>
<dc:language>it_IT</dc:language>
<dc:creator>'.$author.'</dc:creator>
<dc:date>'.date("Y-m-d\TH:i:s+02:00").'</dc:date>
<items>
<rdf:Seq>
';
  for ($i=0; $i< $match_count; $i++) {
    $link = substr($links[1][$i],strpos($links[1][$i],'"')+1,strpos($links[1][$i],'"',strpos($links[1][$i],'"')+1)-9);
    $link = get_link($links[0][$i],$url);
    $output .= '  <rdf:li resource="'.$link.'"/>'."\n";
  }
$output .='</rdf:Seq>
</items>
</channel>
';


  for ($i=0; $i< $match_count; $i++) {

    $item_url = get_link($desc, $url);
    $desc = strip_tags($desc, $allowable_tags);
    $desc = htmlentities($desc);
    $link = get_link($links[0][$i],$url);
    $date = get_date($dates[0][$i]);

    $output .= "    <item rdf:about=\"$link\">\n";
    $output .= "      <title>". trim(ereg_replace("&nbsp;", " ",htmlspecialchars(strip_tags($titles[1][$i]), ENT_QUOTES, "UTF-8")))."</title>\n";
    $output .= "      <link>". $link ."</link>\n";
    $output .= "      <dc:date>". date("Y-m-d\TH:i:s+02:00",$date) ."</dc:date>\n";
    $output .= "      <dc:creator>". $author ."</dc:creator>\n";
    $output .= "      <content:encoded><![CDATA[\n". trim($texts[1][$i]) ."]]></content:encoded>\n";
    $output .= "    </item>\n";
  }

  $output .= "</rdf:RDF>\n";

  print $output;
//  print htmlentities($output);
//  print "</pre>"; 
}

function get_link($desc, $url) {
  if (stristr($desc, "href")) {
    $linkurl = stristr($desc, "href");
    $linkurl = substr($linkurl, strpos($linkurl, "\"")+1);
    $linkurl = substr($linkurl, 0, strpos($linkurl, "\""));
    $linkurl = trim($linkurl);
    return $linkurl;
  } else {
    return $url;
  }
}

function get_date($desc) {
	$rows = explode("\n", $desc);
	$myrow = $rows[2];
	$day = substr($myrow, 4, 2);
	$month = substr($myrow, 7, 2);
	$year = substr($myrow, 10, 4);
	$hour = substr($myrow, 15, 2);
	$minute = substr($myrow, 18, 2);
	if(substr($myrow, 21, 2) == "PM" && $hour < 12) $hour = $hour + 12;

	return strtotime($year."-".$month."-".$day." ".$hour.":".$minute.":00");

}

?>
