<? 
header('Content-type: text/xml'); 
include_once("inc/database.open.inc.php");
//FUNCTIONS
function trimtekst ($s,$aantal,$bid) {
// limit the length of the given string to $MAX_LENGTH char
// If it is more, it keeps the first $MAX_LENGTH-3 characters 
// and adds "..."
// It counts HTML char such as &aacute; as 1 char.
//
	if ($aantal == "" || $aantal == "0") {
		$aantal = "50";
	}
  $MAX_LENGTH = $aantal;
  $str_to_count = html_entity_decode($s);
  if (strlen($str_to_count) <= $MAX_LENGTH) {
   return $s;
  }

  $s2 = substr($str_to_count, 0, $MAX_LENGTH - 3);
  $s2 .= "...<b>Kijk verder op mijn weblog // <a href=\"http://www.mikevanhoenselaar.nl/bericht.php?bid=" .$bid . "\" title=\"Weblog Mike van Hoenselaar\" name=\"Weblog Mike van Hoenselaar\">www.mikevanhoenselaar.nl</a></b>";
  return $s2;
} 
function haalaantalreactiesbericht($bid) {
	$query = "SELECT * FROM reacties WHERE bericht_ID = '$bid'";
	$result = mysql_query($query);
	if($result) {
		$aantal = mysql_num_rows($result);
	} else {
		$aantal = "0";
	} 
	return $aantal;
}
function haalberichtnaam($bid) {
	$query = "SELECT * FROM berichten WHERE bericht_ID = '$bid'";
	$result = mysql_query($query);
	if($result) {
		$row = mysql_fetch_assoc($result);
		$bidnaam = $row["bericht_kop"];
	} else {
		$bidnaam = "None";
	}
	return $bidnaam;
}
function dateFormat($sDateFormat, $sDateField, $kolomnaam, $sTaal = 'en'){
 
    // format een datetime veld uit mysql met de argumenten van de functie date()

    $aDate = array();

    $aDate['d'] = 'DATE_FORMAT(' . $sDateField . ", '%d')";
    $aDate['g'] = 'DATE_FORMAT(' . $sDateField . ", '%l')";
    $aDate['G'] = 'DATE_FORMAT(' . $sDateField . ", '%k')";
    $aDate['h'] = 'DATE_FORMAT(' . $sDateField . ", '%h')";
    $aDate['H'] = 'DATE_FORMAT(' . $sDateField . ", '%H')";
    $aDate['i'] = 'DATE_FORMAT(' . $sDateField . ", '%i')";
    $aDate['j'] = 'DATE_FORMAT(' . $sDateField . ", '%e')";
    $aDate['m'] = 'DATE_FORMAT(' . $sDateField . ", '%m')";
    $aDate['n'] = 'DATE_FORMAT(' . $sDateField . ", '%c')";
    $aDate['s'] = 'DATE_FORMAT(' . $sDateField . ", '%S')";
    $aDate['Y'] = 'DATE_FORMAT(' . $sDateField . ", '%Y')";
    $aDate['y'] = 'DATE_FORMAT(' . $sDateField . ", '%y')";
    $aDate['z'] = 'DATE_FORMAT(' . $sDateField . ", '%j')";
    $aDate['w'] = 'DATE_FORMAT(' . $sDateField . ", '%w')";

    $aDate['%'] = '%%'; 

    switch ($sTaal){
      case 'nl':

        $aDate['D'] = 'ELT((WEEKDAY('. $sDateField .")+1), 
        'ma','di','wo','do','vr','za','zo')";

        $aDate['F'] = 'ELT(MONTH(' .    $sDateField . "),    
        'Januari','Februari','Maart','April','Mei','Juni',
        'Juli','Augustus','September','Oktober','November',
        'December')";

        $aDate['l'] = 'ELT((WEEKDAY(' . $sDateField . ")+1), 
        'maandag','dinsdag','woensdag','donderdag','vrijdag',
        'zaterdag','zondag')";

        $aDate['m'] = 'ELT(MONTH(' .    $sDateField . "),    
        'jan','feb','mrt','apr','mei','jun','jul',
        'aug','sep','okt','nov','dec')";

        break;       

      case 'en':

        $aDate['D'] = 'DATE_FORMAT(' . $sDateField . ", '%a')";
        $aDate['F'] = 'DATE_FORMAT(' . $sDateField . ", '%M')";
        $aDate['l'] = 'DATE_FORMAT(' . $sDateField . ", '%W')";
        $aDate['m'] = 'DATE_FORMAT(' . $sDateField . ", '%b')";
        break;
    }

    $aReturn = array();

    $iFormatLength = strlen($sDateFormat);

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

      // replace argumenten per karakter

      $sChar = substr($sDateFormat, $i, 1);

      if (array_key_exists($sChar, $aDate)){

        $aReturn[] = $aDate[$sChar];

      } else {

        $aReturn[] = "'" . $sChar . "'";

      }

    }
    return 'CONCAT(' . implode(',', $aReturn) . ') AS ' . $kolomnaam;
  } 
include("feedcreator.class.php");


$rss = new UniversalFeedCreator();
$rss->useCached();
if (empty($_GET["bid"])) {
	$bidnaam = "";
	$bestandsnaam = "";
	$bidquery = "";
	$title = "Trendensity by Mike van Hoenselaar // Reacties ";
	$link = "";
} else {
	//alleen 1 bericht
	$bidnaam = haalberichtnaam($_GET["bid"]);
	$bestandsnaam = "1";
	$bidquery = "WHERE bericht_ID = '" . $_GET["bid"] . "' ";
	$title = "Trendensity by Mike van Hoenselaar // Reactie(s) op '" . $bidnaam . "'"; 
	$link = "/bericht.php?bid=" . $_GET["bid"];
}
$rss->title = $title;
$rss->language = "nl";
$rss->editor = "mike@mikevanhoenselaar.nl";
$rss->webmaster = "mike@mikevanhoenselaar.nl";
$rss->description = "Mike van Hoenselaar, a global view of his life. He shows you new views, new perspectives and new ideas in the trendensity web world. Set trends don't follow them."; 

$rss->link = "http://www.mikevanhoenselaar.nl" . $link; 
$rss->syndicationURL = "http://www.mikevanhoenselaar.nl/".$PHP_SELF;

$image = new FeedImage();
$image->title = "Trendensity by Mike van hoenselaar v2 // Logo"; 
$image->url = "http://www.mikevanhoenselaar.nl/images/headerimage.jpg"; 
$image->link = "http://www.mikevanhoenselaar.nl"; 
$image->description = "Trendensity by Mike van Hoenselaar v2 // logo for distribution on the web."; 
$rss->image = $image;

// get your news items from somewhere, e.g. your database:
$res = mysql_query("SELECT *," . dateFormat('l j F Y H:i', 'reactie_datum','datum') . " FROM reacties " . $bidquery . "ORDER BY reactie_ID DESC");
if ($res) {
	$aantal = mysql_num_rows($res);
	if($aantal >= "1") {
		while ($data = mysql_fetch_assoc($res)) {
			$item = new FeedItem();
			$bid 	= $data["bericht_ID"];
			$rid 	= $data["reactie_ID"];
			$door 	= $data["reactie_door"];
			$email 	= $data["reactie_email"];
			$berichtnaam			= haalberichtnaam($data["bericht_ID"]);
			if (empty($_GET["bid"])) {
				$item->title 			= "Reactie op " . $berichtnaam; 
			} else {
				$item->title 			= "Reactie door " . $door;
			}
			$item->link 			= "http://www.mikevanhoenselaar.nl/bericht.php?bid=" . $bid . "#reactie" . $rid; 
			
			$item->description 		= nl2br(html_entity_decode(stripslashes($data["reactie_inhoud"]))) . "<p><a href=\"http://www.mikevanhoenselaar.nl/bericht.php?bid=".$bid."#plaatsreactie\">Plaats ook een reactie</a></p>";	
			$item->date 			= $data["datum"];
			$item->source 			= "http://www.mikevanhoenselaar.nl";
			$item->author 			= $door . ", " . $email;
			
			$rss->addItem($item);
		}
	}
}

if(empty($bestandsnaam)) {
	$bestandsnaam = "feed-reacties.xml";
} else {
	$bestandsnaam = "feed-reacties-" . strtolower($bidnaam) . ".xml";
}
$rss->saveFeed("RSS2.0", $bestandsnaam);
?> 