#!/usr/bin/perl
#########################################
#     Copyright    WM   1999-2000       #
#     Все права зашищены(1999-00).      #
#     Guestbook v.3.0			#
#     По всем вопросам - Миша           #
#     email: smisha@mail.ru             #
#     url: http://cgi.superbest.net     #
#########################################
# Установка переменных
$up = 1; #вверху новая запись (1) или внизу (0)
$date_command = "/bin/date"; #команда даты
$aemail = "smisha\@mail.ru"; #ваш email (перед @ поставьте \)
$mailprog = "/usr/sbin/sendmail"; #программа sendmail
$entr = 10; #кол-во записей на страницу
$name_color = "maroon"; # цвет имени пользователя
$answer_link_color = "silver"; # цвет фразы 'ответить на сообщение'
$enable_to_answer = "1"; # если хотите, чтобы пользователи могли отвечать, оставьте 1, если нет - 0

############################################################
$date = `$date_command +"%B %d, %Y"`; chop($date);

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
   ($name, $value) = split(/=/, $pair);
   $value =~ tr/+/ /;
   $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   $value =~ s/<!--(.|\n)*-->//g;
   $value =~ s/<([^>]|\n)*>//g;
   $FORM{$name} = $value;
}

$temp=$ENV{'QUERY_STRING'};
@pairs=split(/&/,$temp);
foreach $item(@pairs) {
	($key,$content)=split (/=/,$item,2);
	$content=~tr/+/ /;
	$content=~ s/%(..)/pack("c",hex($1))/ge;
	$FORM{$key}=$content;
}


if($FORM{'action'} eq 'add_new') {&add_new;}
if($FORM{'action'} eq 'add') {&add;}
if($FORM{'action'} eq 'show') {&show;}
if($FORM{'action'} eq 'reply') {&reply;}
if($FORM{'action'} eq 'reply_yes') {&reply_yes;}
&show;

sub show {
open(DATA,"data.txt");
@data = <DATA>;
$many = @data;
close(DATA);
  print "Content-type: text/html \n\n";
   open (HEAD, "header.txt");

	@head = <HEAD>;
	close (HEAD);
			foreach $line (@head) {
			print "$line\n";
		}

if ($up eq '1') {@data = reverse(@data);}
$end = $FORM{'m'} + $entr - 1;
$a = 0;
$a = $FORM{'m'} if ($FORM{'m'}); 
&add;
print "<br><br>";
foreach $line (@data[$FORM{'m'}..$end]) {
@g = split(/\|/,$line);
if ($g[0]) {
print "<b><font color=$name_color>$g[0]</font></b> ";
if($g[1] ne "") {print ", <b><a href=mailto:$g[1]>$g[1]</a></b> ";}
if($g[4] ne "") {print ", <b>$g[4]</b> ";}
if($g[3] ne "") {print ", <b><a href=$g[3] target=_new>$g[3]</a></b> ";}
@com = split(/\%\%/,$g[2]); 
$c = @com - 1;
print "<br>$g[5]:<br>Сообщение:<br><br>$com[0]\n";
print "<ul>\n";
foreach $reply (@com[1..$c]) {print "<li>\n&nbsp;&nbsp;&nbsp;$reply</li>\n";}
print "</ul>\n";
if ($enable_to_answer eq "1") {
print "<a href=guest.cgi?action=reply&m=$a><u><font
color=$answer_link_color>Ответить на это сообщение</font></u></a><br>";
}
print "<hr>";
}
$a++;
}
$pages = int($many / $entr) + 1;
print "Страницы: ";
for($i=1;$i<=$pages;$i++) {
$m = $i * $entr - $entr;
print "[<a href=guest.cgi?action=show&m=$m>$i</a>] ";
}
	 open (FOOT, "footer.txt");

	@foot = <FOOT>;
	close (FOOT);
			foreach $line (@foot) {
			print "$line\n";
		}	
   exit;
}

sub add {
print "<form method=POST action=\"guest.cgi?action=add_new\">\n";
print "<table border=0 cellspacing=0 cellpadding=5 width=300>\n";
print "<tr>\n";
print "	<td width=\"100\" valign=\"top\"><b>Имя</b></td>\n";
print "	<td width=\"200\"><input TYPE=\"text\" name=\"name\" size=\"30\"><br></td>\n";
print "</tr>\n";
print "<tr>\n";
print "	<td width=\"100\" valign=\"top\"><b>Город</b></td>\n";
print "	<td width=\"200\"><input TYPE=\"text\" name=\"place\" size=\"30\"><br></td>\n";
print "</tr>\n";
print "<tr>\n";
print "	<td width=\"100\" valign=\"top\"><b>Email</b></td>\n";
print "	<td width=\"200\"><input TYPE=\"text\" name=\"email\" size=\"30\"><br></td>\n";
print "</tr>\n";
print "<tr>\n";
print "	<td width=\"100\" valign=\"top\"><b>Homepage</b></td>\n";
print "	<td width=\"200\"><input TYPE=\"text\" name=\"url\" size=\"30\"><br></td>\n";
print "</tr>\n";
print "<tr>\n";
print "	<td width=\"100\" valign=\"top\"><b>Сообщение</b></td>\n";
print "	<td width=\"200\"><textarea name=\"comments\" cols=40 rows=10></textarea><br></td>\n";
print "</tr>\n";
print "\n";
print "<tr>\n";
print "	<td width=\"100\"></td>\n";
print "	<td width=\"200\"><center><input TYPE=\"submit\" VALUE=\"отправить\"> <input type=\"Reset\" VALUE=\"очистить\"></center></td>\n";
print "</tr>\n";
print "</table>\n";
print "</form>\n";
}

sub add_new {
########
if ($FORM{'comments'} eq "") {&error('Не были набраны комментарии');}
if ($FORM{'name'} eq "") {&error('Не было набрано имя');}
########
$FORM{'comments'} =~ s/\n/<br>/g;

open (DATA,">>data.txt") || die "Can't Open DATA: $!\n";
print DATA "$FORM{'name'}|$FORM{'email'}|$FORM{'comments'}|$FORM{'url'}|$FORM{'place'}|$date\n";
close(DATA);

$FORM{'comments'} =~ s/<br>/\n/g;

   open (MAIL, "|$mailprog $aemail") || die "Can't open $mailprog!\n";
   print MAIL "Reply-to: $FORM{'email'}\n";
   print MAIL "From: $FORM{'email'}\n";
   print MAIL "Subject: New entry in your guestbook\n\n";
   print MAIL "$FORM{'name'} <$FORM{'email'}> оставил(а) запись в гостевой книге!\n";
   print MAIL "  - $date\n";
   print MAIL "Сообщение: $FORM{'comments'}\n";
   close (MAIL);

#############
   print "Location: guest.cgi\n\n";
}

sub reply_yes {
########
if ($FORM{'comments'} eq "") {&error('Не были набраны комментарии');}
if ($FORM{'name'} eq "") {&error('Не было набрано имя');}
########
$FORM{'comments'} =~ s/\n/<br>/g;
open (DATA,"+<data.txt");
flock(DATA,2);
@database_array = <DATA>;
if ($up eq '1') {@database_array = reverse(@database_array);}

$m = $FORM{'m'};

$reply = "%%<font color=$name_color><b>$FORM{'name'}</font></b> ";
if ($FORM{'email'} ne "") {$reply.=", <b><a href=mailto:$FORM{'email'}>$FORM{'email'}</a></b>";}
if ($FORM{'place'} ne "") {$reply.=", <b>$FORM{'place'}</b>";}
if ($FORM{'url'} ne "") {$reply.=", <b><a href=$FORM{'url'} target=_new>$FORM{'url'}</a></b>";}
$reply.= "<br>$date<br>Cообщение:<br><br>$FORM{'comments'}<br>";
@members_array = split(/\|/,$database_array[$m]);
$members_array[2].="$reply";
$database_array[$m] = join("\|",@members_array);

seek (DATA, 0, 0);
truncate (DATA,0);
if ($up eq '1') {@database_array = reverse(@database_array);}
print DATA @database_array;
close (DATA);
##############
# Запись добавлена
   open (MAIL, "|$mailprog $FORM{'to'}") || die "Can't open $mailprog!\n";
   print MAIL "Reply-to: $FORM{'to'}\n";
   print MAIL "From: $FORM{'to'}\n";
   print MAIL "Subject: Reply to yours message\n\n";
   print MAIL "$FORM{'name'} <$FORM{'email'}> ответил(а) на ваше сообщение в гостевой книге!\n";
   print MAIL "  - $date\n";
   print MAIL "Ответ: $FORM{'comments'}\n";
   close (MAIL);
   print "Location: guest.cgi\n\n";
}

sub reply {
open(DATA,"data.txt");
@data = <DATA>;
 if ($up eq '1') {@data = reverse(@data);}
close(DATA);
@all = split(/\|/,$data[$FORM{'m'}]);
@com = split(/\%\%/,$all[2]);
   print "Content-Type: text/html \n\n";
   open (HEAD, "header.txt");

	@head = <HEAD>;
	close (HEAD);
			foreach $line (@head) {
			print "$line\n";
		}
print "<table border=0 cellspacing=0 cellpadding=5 width=685>\n";
print "<tr>\n";
print "<td width=\"635\" class=\"osnova\">\n";
print "<br><br>\n";
print "<form action=guest.cgi?action=reply_yes method=post><table>\n";
print "<input type=hidden value=$all[1] name=to>\n";
print "<input type=hidden value=\"$com[0]\" name=mes>\n";
print "<input type=hidden value=\"$FORM{'m'}\" name=m>\n";
print "<table border=0 cellspacing=0 cellpadding=5 width=300>\n";
print "<tr>\n";
print "	<td width=\"100\" valign=\"top\"><b>Имя</b></td>\n";
print "	<td width=\"200\"><input TYPE=\"text\" name=\"name\" size=\"30\"><br></td>\n";
print "</tr>\n";
print "<tr>\n";
print "	<td width=\"100\" valign=\"top\"><b>Город</b></td>\n";
print "	<td width=\"200\"><input TYPE=\"text\" name=\"place\" size=\"30\"><br></td>\n";
print "</tr>\n";
print "<tr>\n";
print "	<td width=\"100\" valign=\"top\"><b>Email</b></td>\n";
print "	<td width=\"200\"><input TYPE=\"text\" name=\"email\" size=\"30\"><br></td>\n";
print "</tr>\n";
print "<tr>\n";
print "	<td width=\"100\" valign=\"top\"><b>Homepage</b></td>\n";
print "	<td width=\"200\"><input TYPE=\"text\" name=\"url\" size=\"30\"><br></td>\n";
print "</tr>\n";
print "<tr>\n";
print "	<td width=\"100\" valign=\"top\"><b>Сообщение</b></td>\n";
print "	<td width=\"200\"><textarea name=\"comments\" cols=40 rows=10></textarea><br></td>\n";
print "</tr>\n";
print "\n";
print "<tr>\n";
print "	<td width=\"100\"></td>\n";
print "	<td width=\"200\"><center><input TYPE=\"submit\" VALUE=\"отправить\"> <input type=\"Reset\" VALUE=\"очистить\"></center></td>\n";
print "</tr>\n";
print "</table>\n";
print "</form>\n";
      	 open (FOOT, "footer.txt");

	@foot = <FOOT>;
	close (FOOT);
			foreach $line (@foot) {
			print "$line\n";
		}	
      exit;
}

######################
sub error {
my($string) = $_[0];
   print "Content-type: text/html\n\n";
   open (HEAD, "header.txt");

	@head = <HEAD>;
	close (HEAD);
			foreach $line (@head) {
			print "$line\n";
		}
print "<br><table border=0 cellspacing=0 cellpadding=0 width=685 height=400>\n";
print "<tr>\n";
print " <td width=\"25\">\n";
print "</td>\n";
print "	<td width=\"635\" class=\"osnova\" valign=top>\n";
   print "$string\n";
print "</td>\n";
print " <td width=\"25\">\n";
print "</td>\n";
print "</tr>\n";
print "</table>\n";
	 open (FOOT, "footer.txt");

	@foot = <FOOT>;
	close (FOOT);
			foreach $line (@foot) {
			print "$line\n";
		}	
   exit;
}
1;