<?php
  /* show the entries from the file 

     never mind the thought of dividing entries, just read the file 
     corresponding to this month and add links to previous month?
     
     so, what does this thing do?
     this basically adds the style information, adds the title and
     also adds the navigation part to the data file

     now, the part I don't know is how to change to next or previous
     month? I should use this like a form generator or something like
     that? 

     use post data and stuff? check the post action before setting the
     file name... and the form is actually the link to next/prev month
     on the page link? we can give it a try...
     well, should use get instead...

     hopefully, the get_file_contents thing can utilize memory mapped IO
     for faster file access (won't be large anyway...-_-)
  */

  //check if we have config.php
$t_start = array_sum(explode(' ', microtime()));
file_exists("config.php") or die("No config.php");
require("config.php"); 

//have to open the file corresponding to this month...
if(isset($_GET['ds'])){
  //SECURITY RISK!!!!!!! BE WARNED!!!!!!!!
  $filename = $_GET['ds'];
  //should reg expr to check if the thing is of the form YYYY-MM
  //before proceeding...well, whatever...-_-;
if(!file_exists("$filename")) {die("Don't try to fool my freetalk!!");}
  // after verifying the thing is correct, then we can set nextmon
  // and prevmon... I ruled out the possility of jumping ahead a month
  // because that's unlikely given the nature of this program...
 }
 else{
   $filename = date('Y-n');
   
 }
//now filename is set, we can use this to extrac the year and month
$yr = substr($filename,0,4);//extract first 4 digits for year
$mm = substr($filename,5,2);//extract last 2 digits for month

// also, have to check if 

if($mm  == "1"){
  //prev = yr -1, 12
  $prev_mon_s = $yr - 1;
  $prev_mon_s = $prev_mon_s."-12";
  $next_mon_s = $mm+1;
  $next_mon_s = $yr."-".$next_mon_s;
 }
 else if($mm == "12"){
   //next = yr+1, 01
  $prev_mon_s = $mm - 1;
  $prev_mon_s = $yr."-"."$prev_mon_s";
  $next_mon_s = $yr + 1;
  $next_mon_s = $next_mon_s."-1";
 }
 else{
  $prev_mon_s = $mm - 1;
  $prev_mon_s = $yr."-"."$prev_mon_s";
  $next_mon_s = $mm+1;
  $next_mon_s = $yr."-".$next_mon_s;

 }

//echo $prev_mon_s."....".$next_mon_s;
//check if the file exists before setting the two crap...

if(strcmp($filename, date('Y-n'))==0){
    //check if there's next month... if the filename is the same
    // as current output of date(Y-m), then there's no nextmon
    $nextmon = 0; 
  }
  else{
    //only if the length is not zero (file exists) do nextmon get set
    if(file_exists($next_mon_s)){
      $nextmon = 1;
    }
    else
      $nextmon = 0;
  }

// needs to calculate prevmonth, and check if the file exist...
//check if prev mon exist... if it does, premon = 1

//calculate prevmon from filename, then check if the file exist
// if it does, set prevmon to 1, otherwise it's 0
if(file_exists($prev_mon_s)){
  $prevmon = 1;
 }
 else{
   $prevmon = 0;
 }

//don't really need this, we can check at actual showing to reduce I/O accesses.
// $file = @fopen("$filename","r") or die("cannot open datafile ".$filename." for read");
?>
<?php
//start the header of the page...
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<LINK HREF="<?php echo $style_filename?>" REL="stylesheet" TYPE="text/css">
<link rel="alternate" type="application/rss+xml" title="<?=$title?>" href="<?=$xml_addr?>">
<title><?php echo $title?></title>
<meta http-equiv=Content-Type content="text/html; charset=UTF-8">
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.ticrf.org.tw/chinese/html/06-rating-v11.htm" l gen true for "http://ikas.kghs.net/~catg/" r (s 3 l 3 v 3 o 0))'>
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-127900-1";
urchinTracker();
</script>
</head>
<body>
<DIV ID="container">

<?php
//check if the title needs to be enabled
if($show_title == 1){
?>
<div id=top><H1 class="titletext"><?php echo $title?></H1></div>
<?php
    }
?>

<?php
//check if the top nav bar needs to be enabled
if($show_topnav == 1){
?>
  <div id=top_nav>
    <?php /*show nav bar, figure how to include htmls*/ 
    echo file_get_contents("$nav_template");
    ?>
    </div>
<?php
    }
?>


<div id=jump>
<p class="jump_text">
← newer
  <?php  
	  // generate the file lists as a string so it can be shown twice without
	  // being recomputed...

	  $month_text = "";
	  
	  if(strcmp($filename,date('Y-n')) !=0 && file_exists(date('Y-n'))){
		  //show the Latest crap only when it actually exists!
		  $month_text = "<A HREF=\"".$_SERVER['PHP_SELF']."?ds=".date('Y-n')."\">[ Latest(".date('Y-n').") ]</a>";
		  }

  //show the nav to other months first...
  //check if previous month exist? then call the crap

if($nextmon == 1){
  //  something goes in here
	$month_text = $month_text."<A HREF=\"".$_SERVER['PHP_SELF']."?ds=".$next_mon_s."\">[ ".$next_mon_s." ]</A>";

	}
$month_text = $month_text."[ ".$filename." ]";

if($prevmon == 1){
$month_text =$month_text."<A HREF=\"".$_SERVER['PHP_SELF']."?ds=".$prev_mon_s."\">[ ".$prev_mon_s." ]</A>";
    }

    echo $month_text;
    
?>
 older →
</p>
</div>

<div id="content">

<?php
	if(file_exists("$filename")){
	echo file_get_contents("$filename");
	}
	else{
	echo $no_entry_msg;
		}
?>
</div>

<?php
if($show_botnav == 1){
?>
  <div id=bot_nav>
  <p class="jump_text">
  ← newer <?php echo $month_text; ?> older →
  </p>
    <?php /*show nav bar, figure how to include htmls*/ 
        echo file_get_contents("$nav_template");
  // now print out the selection part...
  $m_navtext = "<P>";
  $tm = getdate();
  
  $cyr = $tm['year']; $cmn = $tm['mon'];
  
for($y = $cyr; $y >= $fyr; $y--){
	$m_navtext = $m_navtext.$y.": ";
	if($y == $cyr){
		for($m = $cmn; $m >= 1; $m--){
			$m_navtext = $m_navtext."<A HREF=\"".$_SERVER['PHP_SELF']."?ds=".$y."-".$m."\">".$m."</A> ";
		}
	}
	else if($y == $fyr){
		for($m = 12; $m >= $fmn; $m--){
			$m_navtext = $m_navtext."<A HREF=\"".$_SERVER['PHP_SELF']."?ds=".$y."-".$m."\">".$m."</A> ";
		}

	}
	else{
		for($m = 12; $m >= 1; $m--){
			$m_navtext = $m_navtext."<A HREF=\"".$_SERVER['PHP_SELF']."?ds=".$y."-".$m."\">".$m."</A> ";
		}

	}
	$m_navtext = $m_navtext."<BR>";

  }
  $m_navtext = $m_navtext."</P>";
  echo $m_navtext;
?>	
    </div>
<?php
    }
?>

<?php 
if($show_license==1){
?>
  <div id=license>
    <?php 
    echo file_get_contents("$license");
    ?>
    
    </div>
<?php
 }
$exec_time = array_sum(explode(' ', microtime())) - $t_start;
echo ("Execution time is $exec_time seconds.");
?>

<p>
 <a href="http://jigsaw.w3.org/css-validator/">
  <img style="border:0;width:88px;height:31px"
       src="http://jigsaw.w3.org/css-validator/images/vcss" 
       alt="Valid CSS!">
 </a>
      <a href="http://validator.w3.org/check/referer"><img border="0"
          src="http://www.w3.org/Icons/valid-html401"
          alt="Valid HTML 4.01!" height="31" width="88"></a>
 </p>
</DIV>
</body>
</html>
