#!/bin/perl

# 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

    &edit_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;
}

###
sub edit_post {
    $threadnum = $query->param('threadnum') || die;
    $date_sec = $query->param('date_sec');
    $title = $query->param('title');
    $author = $query->param('author');
    $email = $query->param('email');
    $post = $query->param('post');

    $title =~ s/[\t\n]/ /g;
    $title =~ s/\</\&lt\;/g;
    $title =~ s/\>/\&gt\;/g;
    $author =~ s/[\t\n]/ /g;
    $author =~ s/\</\&lt\;/g;
    $author =~ s/\>/\&gt\;/g;
    $email =~ s/[\t\n]//g;
    $email =~ s/\</\&lt\;/g;
    $email =~ s/\>/\&gt\;/g;
    $post =~ s/\t/   /g;
    $post =~ s/\n//g;

    open(LOCK,"../list.lock") || die;
    flock(LOCK,2);

    open(SCRATCH,">$main_thread_file.bak") || die;

    open(THREADFILE,"../DATA/$threadnum.th") || die;
    $x = 0;
    while(<THREADFILE>) {
	if ($x != $postnum) { print SCRATCH; }
	else { print SCRATCH "$title\t$author\t$email\t$date_sec\t0\t$post\n"; }
	$x++;
    }
    
    open(THREADFILE,">../DATA/$threadnum.th") || die;
    open(SCRATCH,"$main_thread_file.bak") || die;
    
    while(<SCRATCH>) {
	print THREADFILE;
    }

# if postnum == 0, edit main.th subject line
    if ($postnum == 0) {
	open(MAIN,"$main_thread_file") || die;
	open(SCRATCH,">$main_thread_file.bak") || die;
	
	while(<MAIN>) {
	    if (/^$threadnum\t/o) {
		($num,$oldtitle,$oldauthor,$oldemail,$date_mod,$replies,
		 $last_mod,$mod_val,$mod_num) = split(/\t/);
		print SCRATCH "$num\t$title\t$author\t$email\t$date_mod\t$replies\t$last_mod\t$mod_val\t$mod_num";
	    }
	    else { print SCRATCH; }
	}
	open(SCRATCH,"$main_thread_file.bak") || die;
	open(MAIN,">$main_thread_file") || die;
	while(<SCRATCH>) {
	    print MAIN;
	}
    }
    flock(LOCK,8);
    
    print "Content-type: text/html\n\n";
	 
    print <<EOF
<HTML><HEAD><TITLE>Edit a Message</TITLE></HEAD>
<BODY BGCOLOR="#FEFEFE"><H1>Message Edited</H1>
<H3>The message will look approximately like:</H3>
<HR>
$post
<HR>Use the <STRONG>Back</STRONG> button to re-edit the message, or
<A HREF="editpost.cgi">return to the beginning</A>
</BODY></HTML>
EOF
}

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

    print "Content-type: text/html\n\n";

    print <<EOF
<HTML>
<HEAD>
<TITLE>Edit A Message</TITLE>
</HEAD>
<BODY BGCOLOR="#FEFEFE">
<H1>Edit a Message</H1>
<FORM METHOD="POST" ACTION="editpost.cgi"> 
<INPUT TYPE="hidden" NAME="threadnum" VALUE="$threadnum">
<INPUT TYPE="hidden" NAME="postnum" VALUE="$postnum">
<INPUT TYPE="hidden" NAME="date_sec" VALUE="$date_sec">
<TABLE>
<TR><TD><STRONG>Title:</STRONG> </TD><TD><INPUT NAME="title" VALUE="$title"><BR></TD></TR>
<TR><TD><STRONG>Name:</STRONG> </TD><TD><INPUT NAME="author" VALUE="$author"><BR></TD></TR>
<TR><TD><STRONG>E-mail:</STRONG> </TD><TD><INPUT NAME="email" VALUE="$email"><BR></TD></TR>
</TABLE>
<HR>
<STRONG>Message:</STRONG>  <EM>Enter HTML formatting (&lt;P&gt;, &lt;BR&gt;,...)</EM><BR>
<TEXTAREA NAME="post" ROWS=10 COLS=75>
$post
</TEXTAREA>
<BR><INPUT TYPE="submit">
</FORM>
</BODY>
</HTML>
EOF
}

###
sub show_message_list {
    print "Content-type: text/html\n\n<HTML><HEAD><TITLE>Edit a Message</TITLE></HEAD><BODY BGCOLOR=\"#FEFEFE\"><H1>Edit a Message</H1>Click on the message you want to edit:<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=\"editpost.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>Edit a Message</TITLE></HEAD><BODY BGCOLOR=\"#FEFEFE\"><A HREF=\"admin.cgi\">Back to Administration</A><H1>Edit a Message</H1>Click on the thread that contains the message you want to edit:<HR>";
    
# get, print $listlength threads
    while(<DATABASE>) {
	chop;
	($num,$title,$author,$email,$date_mod,$replies,$last_mod,
	 $mod_val,$mod_num) = split(/\t/);
	
	$date = &date_format($date_mod);
	$last = &date_format($last_mod);
	
	if (!($mod_val)) {
	    print "<STRONG>$num</STRONG> $date <A HREF=\"editpost.cgi?$num\">$title</A> - $author [ $replies - <EM>$last</EM> ]<BR>\n";
	}
    }
    
    print "</BODY></HTML>";
    
    close(DATABASE);
}


