<?php



// get all entries of this month 

function get_blog_entries(&$entries,$y,$m){

  $dir = 'content/'.$y.'/'.$m;
  if ( is_dir( $dir ) ) {
    if ( $dh = @opendir($dir) ) {

      while (($file = readdir( $dh ) ) !== false ) {
	if ( is_file( $dir.'/'.$file ) ) {
		
	  $ext = strtolower( strrchr( $file, '.' ) );
	  if ( $ext == '.txt' || $ext == '.gz' ) {
	    $entries[$dir.'/'.$file] = NULL;
	  }
	}
      }
    }
  }
}


// get re-current events

function get_recurrent_events(&$entries){

  $eventfile = 'content/repeat_events.txt';
  if (!file_exists($eventfile)){return;}
  $lines = file($eventfile);
  foreach ($lines as $line) {
    list($file,$rep) = explode('|',$line);
    $rep=chop($rep);
    $entries[$file] = $rep;
  }
}



// upcoming events up to a certain date
// (but not more than $max events!)

function menu_upcoming_events ( $max) {
  global $lang_string;

  $now_y = date('y');
  $now_m = date('m');
  $now_d = date('d');

  if ($max == NULL){
    $max = 10;
  }
		
  $entry_file_array = blog_entry_listing();
		
  // Grab the next X number of entries
  $events = array();

  for ( $i = 0; $i < count( $entry_file_array ); $i++ ) {
    $entry_y = substr($entry_file_array[$i],5,2);
    $entry_m = substr($entry_file_array[$i],7,2);
    $entry_d = substr($entry_file_array[$i],9,2);


    if ($entry_y>$now_y){
      array_push( $events, $entry_file_array[ $i ] );
    }
    elseif($entry_y == $now_y && $entry_m > $now_m ){
      array_push( $events, $entry_file_array[ $i ] );
    }
    elseif($entry_y == $now_y && $entry_m == $now_m && $entry_d > $now_d ){
      array_push( $events, $entry_file_array[ $i ] );
    }
  }


  $events = array_reverse($events);
  $file_array = array();
  for ( $i = 0; $i < min( $max, count( $events ) ); $i++ ) {
    array_push( $file_array, $events[$i] );
  }

  // Read entry files
  $contents = array();
  for ( $i = 0; $i < count( $file_array ); $i++ ) {
    list($entry_filename,$year_dir,$month_dir) = explode('|',$file_array[$i]);
    array_push( $contents, array( 'path' => ( 'content/' . $year_dir . '/' . $month_dir . '/' . $entry_filename ),
				  'entry' => $entry_filename,
				  'year' => $year_dir,
				  'month' => $month_dir ) );
  }
		
  $str = '';
  if ( $contents ) {
    if ( ( dirname($_SERVER['PHP_SELF']) == '\\' || dirname($_SERVER['PHP_SELF']) == '/' ) ) {
      // Hosted at root.
      $base_permalink_url = 'http://'.$_SERVER['HTTP_HOST'].'/';
    } else {
      // Hosted in sub-directory.
      $base_permalink_url = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/';
    }
    
    for ( $i = 0; $i <= count( $contents ) - 1; $i++ ) {
      $blog_entry_data = blog_entry_to_array( $contents[$i]['path'] );
      
      $entry_array = array();
      $entry_array['subject'] = blog_to_html( $blog_entry_data["SUBJECT"], false, false );
      
      $entry = sb_strip_extension( $contents[$i]['entry'] );
      
      $entry_array['permalink']['url'] = $base_permalink_url . 'index.php?entry=' . $entry;
      
      $str = $str . '<a href="' . $entry_array['permalink']['url'] . '">' . $entry_array['subject'] . '</a><br />';
    }
  }
  
  $result = array();
  //$result['title'] = $lang_string['menu_most_recent_entries'];
  $result['title'] = 'Upcoming';
  $result['content'] = $str;
		
  return ( $result );
}




function save_repeat_event ($entry,$repeat){
    $eventfile = "content/repeat_events.txt";
    $str = $entry . '|' . $repeat ."\n";

    $fh = fopen($eventfile, 'a');
    if (flock($fh, LOCK_EX)) {
      fwrite($fh, $str);
      flock($fh, LOCK_UN);
    }
    fclose($fh);
}

// this function checks the repeat_events.txt file and creates
// repeated events if necessary ....

function repeat_events ($year,$month){
  $eventfile = "content/repeat_events.txt";
  if (!file_exists($eventfile)){return;}

  ##-------------------------------------------------------
  ## check last modification time of the event file
  ## and compare it to the mtime of the last time we tried
  ## repeat_events for this month
  ## --> check events only if there are recent changes!

  $event_mtime=filemtime($eventfile);
  if (!file_exists('content/repeat')) {
    $oldumask = umask(0);
    $ok = my_mkdir('content/repeat', 0777 );
    umask($oldumask);
  }
  $flagfile='content/repeat/'.$year.$month;
  if (file_exists($flagfile)){
    $flag_mtime=filemtime($flagfile);
    if ($flag_mtime > $event_mtime){return;}
  }

  ## touch the flag to save the current time

  touch($flagfile);

  $events = file($eventfile);
  foreach ($events as $event) {
    list($entry,$repeat) = split('\|',$event);
    list($base,$y,$m,$file) = split('\/',$entry);
    list($r_y,$r_m) = split(":",$repeat);
    // $d = substr($file,9,2);
    $count=0;
    do {
      $y += $r_y;
      $m += $r_m;
      if ($m > 12){$y++;$m=0;}

      if (($y == $year) && ($m == $month)){

	if ($y<10){$y='0'.$y;}
	if ($m<10){$m='0'.$m;}

	$newEntry = substr_replace($file,$y.$m,5,4);
	$flagfile = 'content/repeat/'.$newEntry;

	if (!file_exists($flagfile)){
	  touch($flagfile);

	  $oldEntry = blog_entry_to_array( $entry );

	  $H = date('H', $oldEntry[ 'DATE' ] );
	  $i = date('i', $oldEntry[ 'DATE' ] );
	  $s = date('s', $oldEntry[ 'DATE' ] );
	  $d = date('d', $oldEntry[ 'DATE' ] );
	  $categories = explode (",",$oldEntry['CATEGORIES']);

	  write_event($oldEntry['SUBJECT'],$oldEntry['CONTENT'],
		      $oldEntry['TB_PING'],$categories,
		      $y,$m,$d,$H,$i,$s,'--');
	}
      }
      ////////////////////////////////////////////////////////
      // this is a hack to avoid infinite loops
      $count++;
      if ($count>100){break;}     // stop after 100 loops
    }
    while (($y <= $year) || ($m <= $month));
  }
}




///////////////////////////////////////////////////////
// update repeat_events.txt
///////////////////////////////////////////////////////


function update_repeat_event($entry,$file,$repeat){
  $eventfile = "content/repeat_events.txt";
  if (!file_exists($eventfile)){return;}
  $events = file($eventfile);
  $str = '';
  $ok = false;
  foreach ($events as $event){

    if (substr($event,14,18) == $entry){
      if ($repeat != '--'){
	$str .= $file . '|' .$repeat."\n";
      }
      $ok = true;
    }
    else{
      $str .= $event; 
    }
  }

  // doesn't exist yet --> create!

  if (! $ok){
    if ($repeat != '--'){
      $str .= $file . '|' . $repeat."\n";
    }
  }
  sb_write_file( $eventfile, $str );
}



function repeat_form($selected){
  $html = '<td><select name="repeat">';
  if ($selected == '--'){
    $html .= '<option selected label="--" value="--">--</option>';
  }
  else{
    $html .= '<option label="--" value="--">--</option>';
  }
  if ($selected == '1:0'){
    $html .= 
      '<option selected label="every year" value="1:0">every year</option>';
  }
  else{
    $html .= '<option label="every year" value="1:0">every year</option>';
  }
  if ($selected == '0:1'){
    $html .= 
      '<option selected label="every month" value="0:1">every month</option>';
  }
  else{
    $html .= '<option label="every month" value="0:1">every month</option>';
  }
  $html .= '</select></td>';
  return $html;
}






function write_pubevent ( $comment_name, $comment_email, $comment_url, 
			  $comment_remember, $comment_text, 
			  $y,$m,$d,$H,$i,$s ) {
    // Save new entry or update old entry
    //
    global $blog_config, $sb_info;

    // create a dummy-post

    $categories = array();
    $ok = write_event ( "What's happening on $d/$m/$y?", 
			"Click on 'comment' to check events on $d/$m/$y!\n", 
			'', $categories, $y,$m,$d,6,0,0 );

    if (! $ok){return "something went wrong!";}

    $blog_date = mktime( 6, 0, 0, $m, $d, $y );
    $stamp = $y.$m.$d.'-'.date('His',$blog_date);
    $entry = 'entry'.$stamp;

    //clearstatcache();
		
    if ( $comment_remember!=0 )
    {
	setcookie( "comment_name", $comment_name, time()+60*60*24*30);
	setcookie( "comment_email", $comment_email, time()+60*60*24*30);
	setcookie( "comment_url", $comment_url, time()+60*60*24*30);
    }
		
    // We're going to assume that the y and m directories exist...
    $basedir = "content/";
    $dir = $basedir.$y."/".$m."/".$entry;

    if (!file_exists($dir)) {
	$oldumask = umask(0);
	$ok = my_mkdir($dir, 0777 );
	umask($oldumask);
	if (!$ok) {
	    // There is a bug in some versions of PHP that will
	    // cause mkdir to fail if there is a trailing "/".
	    //
	    // Thanks to Matt - http://agent.chaosnet.org
	    return ( $dir );
	}
    }

    $dir = $dir."/comments";
		
    if (!file_exists($dir)) {
	$oldumask = umask(0);
	$ok = my_mkdir($dir, 0777 );
	umask($oldumask);
	if (!$ok) {
	    // There is a bug in some versions of PHP that will
	    // cause mkdir to fail if there is a trailing "/".
	    //
	    // Thanks to Matt - http://agent.chaosnet.org
	    return ( $dir );
	}
    }
		
    $dir = $dir . "/";

    $comment_date = mktime( $H, $i, $s, $m, $d, $y );    
//    $comment_date = time();
		
    $stamp = date("ymd-His");
    if ( $blog_config[ "blog_enable_gzip_txt" ] ) {
	$entryFile = $dir."comment".$stamp.".txt.gz";
    } else {
	$entryFile = $dir."comment".$stamp.".txt";
    }
		
    // Save the file
    $save_data = array();
    $save_data[ "VERSION" ] = $sb_info[ "version" ];
    $save_data[ "NAME" ] = clean_post_text( $comment_name );
    $save_data[ "DATE" ] = $comment_date;
    $save_data[ "CONTENT" ] = clean_post_text( $comment_text );
    if ( $comment_email != "" ) {
	$save_data[ "EMAIL" ] = clean_post_text( $comment_email );
    }
    if ( $comment_url != "" ) {
	$save_data[ "URL" ] = clean_post_text( $comment_url );
    }

    // Implode the array
    $str = implode_with_keys( $save_data );
		
    // Save the file		
    $result = sb_write_file( $entryFile, $str );
			
    if ( $result ) {
	// Update Most Recent List
	add_most_recent( "comment".$stamp, $y, $m, $entry );
	 		
	if ( $blog_config[ "blog_email_notification" ] ) {
	    // Send Email Notification:
	    
	    $subject="New comment posted at " . $blog_config[ "blog_title" ];
	    $body="<b>Name:</b> " . $save_data[ "NAME" ] . "<br />\n";
	    if ( array_key_exists( "EMAIL", $save_data ) ) {
		$body=$body . "<b>Email:</b> <a href=\"mailto:" . $save_data[ "EMAIL" ] . "\">" . $save_data[ "EMAIL" ] . "</a><br />\n";
	    }
	    if ( array_key_exists( "URL", $save_data ) ) {
		$body=$body . "<b>Homepage:</b> <a href=\"" . $save_data[ "URL" ] . "\">" . $save_data[ "URL" ] . "</a><br />\n";
	    }
	    $body=$body . "<br />\n";
	    $body=$body . "<b>Comment:</b><br />\n";
				
	    if ( ( dirname($_SERVER['PHP_SELF']) == '\\' || dirname($_SERVER['PHP_SELF']) == '/' ) ) {
		// Hosted at root.
		$base_url = "http://".$_SERVER["HTTP_HOST"]."/";
	    } else {
		// Hosted in sub-directory.
		$base_url = "http://".$_SERVER["HTTP_HOST"].dirname($_SERVER["PHP_SELF"])."/";
	    }
				
	    $body = $body . '<a href="' . $base_url . 'comments.php?y=' . $y . '&amp;m=' . $m . '&amp;entry=' . $entry . '">' . $base_url . 'comments.php?y=' . $y . '&amp;m=' . $m . '&amp;entry=' . $entry . "</a><br />\n<br />\n";
	    $body = $body . '<i>On ' . format_date( $comment_date ) . ', ' . $comment_name . " wrote:</i><br />\n<br />\n" . blog_to_html( $comment_text, true, false );
				
	    // Send the Email
	    if ( array_key_exists( "EMAIL", $save_data ) ) {
		sb_mail( $save_data[ "EMAIL" ], $blog_config[ 'blog_email' ], $subject, $body, false );
	    } else {
		sb_mail( $blog_config[ 'blog_email' ], $blog_config[ 'blog_email' ], $subject, $body, false );
	    }
	}
	 		
	return ( true );
    } else {
	// Error:
	// Probably couldn't create file...
	return ( $entryFile );
    }
}



function write_event ( $blog_subject, $blog_text, $tb_ping, $blog_categories, 
		       $y,$m,$d,$H,$i,$s,$repeat='--',$editor='default' ) {
  // Save new entry or update old entry
  //
  // $updateFile will either be NULL or the name of the file
  // which is being updated (i.e. entry040603-140634)
  global $blog_config, $sb_info;
		
  $filename='config/~blog_entry_listing.tmp';
  sb_delete_file( $filename );
		
  $save_data = array();
  $save_data[ 'VERSION' ] = $sb_info[ 'version' ];
  $save_data[ 'SUBJECT' ] = clean_post_text( $blog_subject );
  $save_data[ 'EDITOR' ] = $editor;
  if ($editor=='fck'){
    $save_data[ 'CONTENT' ] = $blog_text;
  }
  else{
    $save_data[ 'CONTENT' ] = sb_parse_url( clean_post_text( $blog_text ) );
  }
  if ( count( $blog_categories ) > 0 ) {
    $save_data[ 'CATEGORIES' ] = implode( ",", $blog_categories );
  }
  if ( $tb_ping !== '' ) {
    $save_data[ 'TB_PING' ] = clean_post_text( $tb_ping );
  }
			
  if (!file_exists('content')) {
    $oldumask = umask(0);
    $ok = my_mkdir('content', 0777 );
    umask($oldumask);
  }
		
  $dir = 'content/';
  if (!file_exists($dir.$y)) {
    $oldumask = umask(0);
    $ok = my_mkdir($dir.$y, 0777 );
    umask($oldumask);
    if (!$ok) {
      return ( 'Couldn\'t creating directory: '.$dir.$y );
    }
  }
  if (!file_exists($dir.$y.'/'.$m)) {
    $oldumask = umask(0);
    $ok = my_mkdir($dir.$y.'/'.$m, 0777 );
    umask($oldumask);
    if (!$ok) {
      return ( 'Couldn\'t creating directory: '.$dir.$y.'/'.$m );
    }
  }

  // Create an entry
  //
  // This is going to be a new entry.
  // We might need to create some directories.
  //
  // The directory and file structure is:
  // 'content/YY/MM/entryYYMMDD-HHMMSS.txt'
  // 'F j, Y, g:i a'


  //  $blog_date = time();

  $tmp_y = $y;
  $tmp_m = $m;
  $tmp_d = $d;
  if ($y == 'xx'){$tmp_y=2000;}   // xx is used for gallery ratings/comments
  if ($m == 'xx'){$tmp_m=0;}   // we don't want this for mktime
  if ($d == 'xx'){$tmp_d=0;}

  do {
    $blog_date = mktime( $H, $i, $s, $tmp_m, $tmp_d, $tmp_y );
    $stamp = $y.$m.$d.'-'.date('His',$blog_date);
    if ( $blog_config[ 'blog_enable_gzip_txt' ] ) {
      $entryFile = $dir.$y.'/'.$m.'/'.'entry'.$stamp.'.txt.gz';
    } else {
      $entryFile = $dir.$y.'/'.$m.'/'.'entry'.$stamp.'.txt';
    }
    if ($s < 59){$s++;}               // add a second
    elseif ($i < 59){$i++;$s=0;}      // or a minute
    else {break;}                     // that's enough!
  }
  while (file_exists($entryFile));

  // if the entry exists already --- just return true
  // (probably not the best solution ... but ...)

  if (file_exists($entryFile)){
      return true;
  }

  $permalink = 'index.php?y='.$y.'&amp;m='.$m.'&amp;entry=entry'.$stamp;
  $save_data[ 'DATE' ] = $blog_date;
		
  // Implode the array
  $str = implode_with_keys( $save_data );

  // Save the file		
  $result = sb_write_file( $entryFile, $str );
		
  // Result
  if ( $result ) {
    // Send Pings
    $url_array = explode( ',', $blog_config[ 'blog_ping_urls' ] );
    if ( is_array( $url_array ) ) {
      for ( $i = 0; $i < count( $url_array ); $i++ ) {
	sb_ping( $url_array[ $i ] );
      }
    }
    
    // Trackback pings?
    if ( $blog_config[ 'blog_trackback_enabled' ] ) {
      trackback_ping( $tb_ping, $blog_subject, $permalink, $blog_text );
    }

    if (isset($repeat)){
      if ($repeat != '--'){
	save_repeat_event($entryFile,$repeat);
      }
    }
    return ('entry'.$stamp) ;
  } else {
    return ( false );
  }
}




function move_entry($entry,$year,$month,$day){

  $old_y = substr($entry,5,2);
  $old_m = substr($entry,7,2);
  $old_d = substr($entry,9,2);

  $file='content/'.$old_y.'/'.$old_m.'/'.$entry;
  $ext='.txt';
  if (file_exists($file.'.txt.gz')){
    $ext = '.txt.gz';
  }

  if (($old_y != $year) || ($old_m != $month) || ($old_d != $day)){
    list($oldentry,$stamp) = explode('-',$entry);
    $hour = substr($stamp,0,2);
    $min = substr($stamp,2,2);
    $sec = substr($stamp,4,2);
    $dir = 'content/'.$year.'/'.$month.'/';
    $newfile = 'entry'.$year.$month.$day.'-'.$hour.$min.$sec;
    while (file_exists($dir.$newfile.$ext)){
      $sec++;
      if ($sec>59){$min++;}
      if ($min>59){$hour++;}
      if ($sec<10){$sec='0'.$sec;}
      if ($min<10){$min='0'.$min;}
      if ($hour<10){$hour='0'.$hour;}
      $newfile = 'entry'.$year.$month.$day.'-'.$hour.$min.$sec;
    }
    if (!file_exists('content/'.$year)) {
      $oldumask = umask(0);
      $ok = my_mkdir('content/'.$year, 0777 );
      umask($oldumask);
    }
    if (!file_exists('content/'.$year.'/'.$month)) {
      $oldumask = umask(0);
      $ok = my_mkdir('content/'.$year.'/'.$month, 0777 );
      umask($oldumask);
    }
  
    if (file_exists($file.$ext)){
      // rename entry directory if iis exists
      if (file_exists($file)){
	rename($file,$dir.$newfile);
      }
      // rename entry file
      if (rename($file.$ext,$dir.$newfile.$ext)){
	$filename='config/~blog_entry_listing.tmp';
	sb_delete_file( $filename );
	return $dir.$newfile.$ext;
      }
    }
  }
  return $file.$ext;
}


function menu_display_blognav_events () {
    // Returns the blog entry navigation calendar as HTML.
    global $month, $year, $day, $lang_string;

    $str = read_events_calendar( $month, $year, $day );

    $result = array();
    $result['title'] = $lang_string['menu_archive'];
    $result['content'] = $str;
    
    return ( $result );
}


// ------------------------
// calendar
// ------------------------
function read_events_calendar ( $m, $y, $d ) {
    global $lang_string, $user_colors;
//    global $logged_in;
		
    if ( !isset( $m ) ) {
	$m = date( 'm' );
    }
    if ( !isset( $y ) ) {
	$y = date( 'y' );
    }
    if ( !isset( $d ) ) {
	$d = date( 'd' );
    }
		
    $entries = sb_folder_listing( 'content/' . $y . '/' . $m . '/', array( '.txt', '.gz' ) );
    // Loop Through Days
    for ( $i = 0; $i < count( $entries ); $i++ ) {
	$temp_index = substr( $entries[$i], 9, 2 )-1;
	$temp_entry = substr( $entries[$i], 0, 11 );

	// Count the number of entries on this day
	$counts[$temp_index] = 1;
	for ( $j = $i + 1; $j < count( $entries ); $j++ ) {
	    if ( $temp_entry == substr( $entries[$j], 0, 11 ) ) {
		$counts[$temp_index]++;
	    } 
	    else {
		break;
	    }
	}
	$i = $j - 1;
    }
		
    $date_string = mktime(0, 0, 0, $m, 1, $y ); //The date string we need for some info... saves space ^_^
    $day_start = date( 'w', $date_string ); //The number of the 1st day of the week
    if ( strftime( '%w', mktime( 0, 0, 0, 1, 1, 2007 ) )!=0 )
    {
	$day_start = ( $day_start + 6 ) % 7;
    }
    //Calculate the previous/next month/year
    if ( $m < 12 )
    {
	$next_month = $m + 1;
	$next_year = $y + 2000;
    }
    else
    {
	$next_year = $y + 1;
	$next_month = 1;
    }
    if ( $m > 1 )
    {
	$previous_month = $m - 1;
	$previous_year = $y;
    }
    else
    {
	$previous_year = $y - 1;
	$previous_month = 12;
    }

    $str = '<table border="0" cellpadding="0" cellspacing="0" align="center" class="calendar">
		<tr>
		<td align="center"><a href="' . $_SERVER['PHP_SELF'] . '?y=' . sprintf( '%02d', $previous_year % 100 ) . '&amp;m=' . sprintf( '%02d', $previous_month ) .'">&laquo;</a></td>
		<td align="center" colspan="5"><b>' . ucwords( strftime( '%B %Y', $date_string) ) . '</b></td>
		<td align="center"><a href="' . $_SERVER['PHP_SELF'] . '?y=' . sprintf( '%02d', $next_year % 100 ) . '&amp;m=' . sprintf( '%02d', $next_month ) .'">&raquo;</a></td>
		</tr>
		<tr>';
    for ( $i=0; $i<7; $i++ )
    {
	if ( $day_start!=0 )
	{
	    $str = $str . '<td>' . ucwords( strftime( '%a', mktime(0, 0, 0, 1, ($i+1)%7, 1990 ) ) ) . '</td>';
	}
	else
	{
	    $str = $str . '<td>' . ucwords( strftime( '%a', mktime(0, 0, 0, 1, ($i+8)%7, 1990 ) ) ) . '</td>';
	}
    }
		
    $str = $str . '</tr><tr>';
    
    //The empty columns before the 1st day of the week
    for ( $i = 0; $i<$day_start; $i++ )
    {
	$str = $str . '<td>&nbsp;</td>';
    }
    $current_position = $day_start; //The current (column) position of the current day from the loop
    $total_days_in_month = date( 't', $date_string); //The total days in the month for the end of the loop

    //Loop all the days from the month
    for ( $i = 1; $i<=$total_days_in_month; $i++)
    {
	$str = $str . '<td align="center">';

//	if (( $logged_in ) && 
	if (( $counts[$i-1] == 0 ) || 
	    ( mktime(0,0,0,$m,$i,$y) == mktime(0,0,0,$m,$d,$y) )){
	    $str = $str . '<a style="text-decoration:none" href="event_add.php?d=' . sprintf( '%02d', $i) . '&amp;m=' . sprintf( '%02d', $m ) . '&amp;y=' . sprintf( '%02d', $y % 100 ) . '" title="' . $counts[$i-1] . '">' . $i . '</a>';
//	    $str = $str . '<a href="event_add.php?d=' . sprintf( '%02d', $i) . '&m=' . sprintf( '%02d', $m ) . '&y=' . sprintf( '%02d', $y % 100 ) . '" title="' . $counts[$i-1] . '">' . $i . '</a>';
	}
//	elseif ( $counts[$i-1] > 0 )
	else{
	    $str = $str . '<a style="text-decoration:underline" href="index.php?d=' . sprintf( '%02d', $i) . '&amp;m=' . sprintf( '%02d', $m ) . '&amp;y=' . sprintf( '%02d', $y % 100 ) . '" title="' . $counts[$i-1] . '">' . $i . '</a>';
	}
//	else
//	{
//	    $str = $str . $i;
//	}
	$str = $str . '</td>';
	
	$current_position++;
			
	if ( $current_position == 7 )
	{
	    $str = $str . '</tr><tr>';
	    $current_position = 0;
	}
    }
    $end_day = 7-$current_position; //There are

    //Fill the last columns
    for ( $i = 0; $i<$end_day; $i++ )
    {
	$str = $str . '<td></td>';
    }
    $str = $str . '</tr><tr>';
    $str = $str . '<td colspan=7 align="center">' . strftime( '<a href="index.php?y=%y&amp;m=%m&amp;d=%d">%x') . '</a></td></tr></table>'; // Close the table
    return( $str );
}
	
?>