#!/usr/bin/perl
print "Content-type: text/html\n\n" ;

use File::Copy;

require "blendiii.pl";
require "headeriii.pl";

my($trueid,$truename) = getUserFromSession();
$login = getUserGreet($trueid,$truename);

print getHeader("message board",$login);


print <<__EOQ__;
<table width=800><tr><td>
__EOQ__

$boarddir = "/home/kirkjerk/SITES/MAIN/loveblender.com/board";
$surveyfile = "/home/kirkjerk/SITES/MAIN/DATA/data-loveblender/survey/current";

#script prints out n days worth of comments


&ReadParse(*input);

#get number of days, assume 3

$DAYSECONDS = 24 *60*60;
$daycount = $input{"daycount"};
if($daycount eq "") {
    $daycount = 3;
}

$msg = $input{"msg"};


#open(READ,$surveyfile);
#$askid =  <READ>;
#chomp $askid;
#$ask = <READ>;
#chomp $ask;
#close READ;

#print<<__EOQ__;
#<table align="right" bgcolor="#CCCCFF" width=200><tr><td align="center">
#Try the Blender of Love<br>
#<a href="/2007august/contest.html">2007 End of Summer<br>Writing Challenge</a>!<br>
#</td></tr></table><br>
#__EOQ__



    $numcomments = 0;
$daysback = 0;
$combuf = "";

    while($daysback < $daycount || $numcomments < 5){
	$timestamp = time - ($daysback * $DAYSECONDS);
	printComments($timestamp);
	$daysback++;
}
for($ptr = $daysback-1; $ptr >= 0; $ptr--){
	$timestamp = time - ($ptr * $DAYSECONDS);
	printComments($timestamp,1);
}



if($msg ne ""){
    print qq(<a name="new"><font color="red"><b>Your message has been added below</b></font></a>);
}
print<<__EOQ__;

<table width=468 align="center" bgcolor="#EEEEEE" cellpadding=2 cellspacing=0>
<FORM ACTION="comments.cgi">  
<tr><td align="center" bgcolor="white">


<I><B>Showing Last <INPUT TYPE="TEXT" SIZE=2 NAME="daycount" VALUE="$daysback"> Days with Messages
</B></I><INPUT  TYPE =SUBMIT VALUE="Go">
<br>
<font size=2><a href="#latest">jump to latest comments</a><br><br></font>

<!-- <a href="/politics.cgi">Click Here for Blender Soapbox Forum</a>-->

</td>
</form>
</tr>

<tr><td  style="font-family:times,serif">
$combuf
</td></tr>

<tr><td  style="font-family:times,serif">
<b><a name="latest"><font size="+1" style="font-family:verdana,arial,helvetica,sans-serif;">Add A Message:</font></a></b><br>

__EOQ__


printCommentForm($trueid,$truename);

print<<__EOQ__;
</td></tr>





<tr>
<td align="right" bgcolor="#FFFFFF">
<br><br>
<script language="JavaScript">
<!--
    google_ad_client = 'pub-8393964454751607';
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = '468x60_as';
// -->
</script>
<script language="JavaScript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</td>
</tr>


</table>

</td></tr></table>

</body>
</html>


__EOQ__



sub printComments {
    my($timestamp,$show) = @_;
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($timestamp);

$showmonth = $mon+1;
if(length($showmonth) == 1) {
    $showmonth = "0".$showmonth;
}
$showyear = $year + 1900;
$showmday = $mday;
if(length($showmday) == 1) {
    $showmday = "0".$showmday;
}

    if(-e "$boarddir/$showyear/$showmonth/$showmday") {
$smallbuf == "";
	open(READ, "$boarddir/$showyear/$showmonth/$showmday") || print "Could not open $boarddir/$showyear/$showmonth/$showmday: $!";
	while(defined($nextline=<READ>)){
	    if($nextline =~ /^From: \<a href\=\"\/blend\/av\.cgi\?/){
		$nextline = qq(<div align="left"><b>$nextline</b>);
	$numcomments++;
	    }
	    if($nextline =~ /^Date:.*\<P\>/){
		$nextline = qq(<b>$nextline</b></div>);
	    }
	    if($show){
	    $combuf.= $nextline;
	}
	}
	close READ;

    }

}


sub ReadParse {
  local (*in) = @_ if @_;
  local ($i, $key, $val);
  # Read in text
  if (&MethGet) {
    $in = $ENV{'QUERY_STRING'};
  } elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
    read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
  }
  @in = split(/&/,$in);
  foreach $i (0 .. $#in) {
    # Convert plus's to spaces
    $in[$i] =~ s/\+/ /g;
    # Split into key and value.  
    ($key, $val) = split(/=/,$in[$i],2); # splits on the first =.
    # Convert %XX from hex numbers to alphanumeric
    $key =~ s/%(..)/pack("c",hex($1))/ge;
    $val =~ s/%(..)/pack("c",hex($1))/ge;
    # Associate key and value
    $in{$key} .= "\0" if (defined($in{$key})); # \0 is the multiple separator
    $in{$key} .= $val;
  }
  return length($in); 
}
# PrintHeader
# Returns the magic line which tells WWW that we're an HTML document
sub PrintHeader {
  return "Content-type: text/html\n\n";
}
# MethGet
# Return true if this cgi call was using the GET request, false otherwise
sub MethGet {
  return ($ENV{'REQUEST_METHOD'} eq "GET");
}
# MyURL
# Returns a URL to the script
sub MyURL  {
  return  'http://' . $ENV{'SERVER_NAME'} .  $ENV{'SCRIPT_NAME'};
}
# CgiError
# Prints out an error message which which containes appropriate headers,
# markup, etcetera.
# Parameters:
#  If no parameters, gives a generic error message
#  Otherwise, the first parameter will be the title and the rest will 
#  be given as different paragraphs of the body
sub CgiError {
  local (@msg) = @_;
  local ($i,$name);
  if (!@msg) {
    $name = &MyURL;
    @msg = ("Error: script $name encountered fatal error");
  };
  print &PrintHeader;
  print "<html><head><title>$msg[0]</title></head>\n";
  print "<body><h1>$msg[0]</h1>\n";
  foreach $i (1 .. $#msg) {
    print "<p>$msg[$i]</p>\n";
  }
  print "</body></html>\n";
}


