#!/bin/perl

# moderate.cgi (modification of editpost.cgi)
# because this is an administrator's tool, there are not too many special 
# checks (after all, the admin should have some special privileges)

require "config";

use lib '../..';
use CGI;
$query = new CGI;

if (($postnum = $query->param('postnum')) || ($postnum eq '0')) {
# if data is passed using POST (determined by checking to see if a value is
# associated with the name "postnum"), then use the info to edit the
# appropriate post
# special check for $postnum == 0, since 0 is false

    &approve_post;
}

elsif (@keywords = $query->keywords) {
# if data is passed using ISINDEX (determined by checking to see if a value is
# associated with the name "keywords"

    ($threadnum, $postnum) = split(/,/,$keywords[0],2);

    if ($postnum || ($postnum eq '0')) {
	&print_edit_form;
    }
    else {
	&show_message_list;
    }
}

else {
# if no data is passed, print out threads
    
    &show_thread_list;
}

### Subroutines
sub approve_post {
    $threadnum = $query->param('threadnum') || die;
    $approved = $query->param(approve);
    
    open(LOCK,"../list.lock") || die;  flock(LOCK,2);
    
    open(MAIN,"$main_thread_file") || die;
    open(SCRATCH,">$main_thread_file.bak") || die;
    
# modify thread file
    open(THREADFILE,"../DATA/$threadnum.th") || die;
    $x = 0;
    while(<THREADFILE>) {
	($title,$author,$email,$date_sec,$mod_val,$post) = split(/\t/,$_,6);
	if ($mod_val) {
	    if ($x != $postnum) { print SCRATCH; }
	    elsif ($approved) { 
		print SCRATCH "$title\t$author\t$email\t$date_sec\t0\t$post";
	    }
	    $x++;
	}
	else {
	    print SCRATCH;
	}
    }
    
    open(THREADFILE,">../DATA/$threadnum.th") || die;
    open(SCRATCH,"$main_thread_file.bak") || die;
    
    while(<SCRATCH>) {
	print THREADFILE;
    }
    
# modify main_thread_file
    open(SCRATCH,">$main_thread_file.bak") || die;
    while (<MAIN>) {
	chop;
	if (/^$threadnum\t/) {
	    ($thread_num,$titre,$name,$mail,$date,$replies,$last_mod,
	     $mod_val,$mod_num) = split(/\t/,$_);
	    if ($approved) {
		$mod_num--;
		$replies++;
		print SCRATCH "$thread_num\t$titre\t$name\t$mail\t$date\t$replies\t$last_mod\t0\t$mod_num\n";
	    }
	    elsif ($replies > -1) {
		$mod_num--;
		print SCRATCH "$thread_num\t$titre\t$name\t$mail\t$date\t$replies\t$last_mod\t0\t$mod_num\n";
	    }
	    else {
		next;
	    }
	}
	else {
	    print SCRATCH $_, "\n";
	}
    }
    
    close SCRATCH;
    open(SCRATCH,"$main_thread_file.bak");
    open(MAIN,">$main_thread_file") || die;
    
    while(<SCRATCH>) {
	print MAIN;
    }
    
    flock(LOCK,8);
    
    print "Content-type: text/html\n\n";
	 
    print qq(
<HTML><HEAD><TITLE>Message Moderation</TITLE></HEAD>
<BODY BGCOLOR="#FEFEFE">
	     );
	 
    if ($approved) {
	print "<H1>Message Approved</H1>";
    }
    else {
	print "<H1>Message Rejected and Deleted</H1>";
    }
    
    print qq(
<A HREF="moderate.cgi">return to the beginning</A>
</BODY></HTML>
	     );
}

###
sub print_edit_form {
    open(THREADFILE,"../DATA/$threadnum.th") || die;
    
    $x = 0;
    while(<THREADFILE>) {
	($title,$author,$email,$date_sec,$mod_val,$post) = split(/\t/,$_,6);
	if ($mod_val) {
	    if ($x == $postnum) {
		last;
	    }
	    $x++;
	}
    }
    
    $post =~ s/\<BR\>/\<BR\>\n/gi;
    
    print "Content-type: text/html\n\n";

    print <<EOF
<HTML>
<HEAD>
<TITLE>Approve/Reject A Message</TITLE>
</HEAD>
<BODY BGCOLOR="#FEFEFE">
<H1>Approve/Reject a Message</H1>
<FORM METHOD="POST" ACTION="moderate.cgi"> 
<INPUT TYPE="hidden" NAME="threadnum" VALUE="$threadnum">
<INPUT TYPE="hidden" NAME="postnum" VALUE="$postnum">
<TABLE>
<TR><TD><STRONG>Title:</STRONG> </TD><TD>$title<BR></TD></TR>
<TR><TD><STRONG>Name:</STRONG> </TD><TD>$author<BR></TD></TR>
<TR><TD><STRONG>E-mail:</STRONG> </TD><TD>$email<BR></TD></TR>
</TABLE>
<STRONG>Message:</STRONG><BR>
$post
<P>
<INPUT TYPE="radio" NAME="approve" VALUE="1"> Approve<BR>
<INPUT TYPE="radio" NAME="approve" VALUE="0"> Reject<P>
<INPUT TYPE="submit">
</FORM>
</BODY>
</HTML>
EOF
}

###
sub show_message_list {
    print "Content-type: text/html\n\n<HTML><HEAD><TITLE>Approve Message</TITLE></HEAD><BODY BGCOLOR=\"#FEFEFE\"><P><A HREF=\"moderate.cgi\">Thread list</A><H1>Approve/Reject a Message</H1>These messages require approval:<HR>";

    open(THREADFILE,"../DATA/$threadnum.th") || die;
    
    $x = 0;
    while (<THREADFILE>) {
	($title,$author,$email,$date_sec,$mod_val,$post) = split(/\t/,$_,6);
	
	if ($mod_val) {
	    $date = &date_format($date_sec);
	    
	    print "$date  <STRONG><A HREF=\"moderate.cgi?$threadnum,$x\">$title</A></STRONG> - $author $email<BR>";
	    $x++;
	}
    }
    
    print "</BODY></HTML>";
}

###
sub show_thread_list {
    open(DATABASE,"$main_thread_file") || die;

    print "Content-type: text/html\n\n<HTML><HEAD><TITLE>Approve Message</TITLE></HEAD><BODY BGCOLOR=\"#FEFEFE\"><P><A HREF=\"admin.cgi\">Administration Page</A><H1>Approve/Reject a Message</H1>These threads have messages that require approval:<HR>";
    
# get, print $listlength threads	
    while(<DATABASE>) {
	chop;
	($num,$title,$author,$email,$date_mod,$replies,$last_mod,
	 $mod_val,$mod_num) = split(/\t/);
	
	if ($mod_num) {
	    
	    $date = &date_format($date_mod);
	    $last = &date_format($last_mod);
	    
	    print "<STRONG>$num</STRONG> $date <A HREF=\"moderate.cgi?$num\">$title</A> - $author [ <EM>$mod_num</EM> ]<BR>\n";
	}
    }
    
    print "</BODY></HTML>";
    
    close(DATABASE);
}


 




