#!/usr/local/bin/perl

$guestbookurl = "gk.htm";

$styles_url = "styles.css";

$guestbookreal = "gk.htm";

$cgiurl = "gk.pl";

&parse_form;

sub parse_form {

	if ($ENV{'REQUEST_METHOD'} eq "POST") {$mode = 0}

	if ($ENV{'REQUEST_METHOD'} eq "GET") {$mode = 1}

	if ($mode == 0) {read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'})}

	if ($mode == 1) {$buffer = $ENV{'QUERY_STRING'}}

	@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;

		$FORM{$name} = $value;

	}

}

&no_name("Вы не указали Ваш комментарий!") unless $FORM{'comments'};

&no_name("Вы не указали Ваше имя") unless $FORM{'realname'};

open (FILE,"$guestbookreal");

@LINES=<FILE>;

close(FILE);

$SIZE=@LINES;

open (GUEST,">$guestbookreal");

for ($i=0;$i<=$SIZE;$i++) {

	$_=$LINES[$i];

	if (/<!--go_write-->/) { 

		print GUEST "<!--go_write-->\n";

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

		print GUEST "<b>$FORM{'comments'}</b><br>\n";

		if ($FORM{'url'}) {

			print GUEST "<a href=\"$FORM{'url'}\">$FORM{'realname'}</a>";

		} else {

			print GUEST "$FORM{'realname'}";

		}

		if ( $FORM{'username'} ){

			print GUEST " \&lt;<a href=\"mailto:$FORM{'username'}\">";

			print GUEST "$FORM{'username'}</a>\&gt;";

		}

		print GUEST "<br>\n";

		if ( $FORM{'city'} ){

			print GUEST "$FORM{'city'},";

		}

		if ( $FORM{'country'} ){

			print GUEST " $FORM{'country'}";

		}

		print GUEST " - $date<hr size=\"1\" width=\"90%\">\n\n";

	} else {

			print GUEST $_;


	}

}

close (GUEST);

&print_head("Ваш комментарий добавлен!");

print "<h1 align=center>Ваш комментарий добавлен!</h1><br>\n"; 

print "Данные, введенные вами, добавлены в гостевую книгу:<br>\n"; 

print "<table border=\"1\">\n"; 

print "<tr>\n"; 

print "<td>Ваше имя</td>\n"; 

print "<td>\n"; 

print "$FORM{'realname'}</td>\n"; 

print "</tr>\n"; 

print "<tr>\n"; 

print "<td>E-mail</td>\n"; 

print "<td>\n"; 

print "$FORM{'username'}</td>\n"; 

print "</tr>\n"; 

print "<tr>\n"; 

print "<td>Домашняя<br>\n"; 

print "страничка</td>\n"; 

print "<td>\n"; 

print "$FORM{'url'}</td>\n"; 

print "</tr>\n"; 

print "<tr>\n"; 

print "<td colspan=\"2\">Город:\n"; 

print "$FORM{'city'},\n"; 

print "Страна:\n"; 

print "$FORM{'country'}\n"; 

print "</td>\n"; 

print "</tr>\n"; 

print "<tr>\n"; 

print "<td>Комментарии:</td>\n"; 

print "<td>$FORM{'comments'}</td>\n"; 

print "</tr>\n"; 

print "</table>\n"; 

print "<p><a href=\"$guestbookurl\">Вернуться в гостевую книгу</a></p>";

&print_end;


sub no_name {

&print_head($_[0]);

print "<h1 align=center>$_[0]</h1><br>\n"; 

print "<form action=\"$cgiurl\" method=\"POST\">\n"; 

print "<table border=\"0\">\n"; 

print "<tr>\n"; 

print "<td>Ваше имя</td>\n"; 

print "<td><input type=\"text\" size=\"23\"\n"; 

print "name=\"realname\" value=\"$FORM{'realname'}\"></td>\n"; 

print "</tr>\n"; 

print "<tr>\n"; 

print "<td>E-mail</td>\n"; 

print "<td><input type=\"text\" size=\"23\"\n"; 

print "name=\"username\" value=\"$FORM{'username'}\"></td>\n"; 

print "</tr>\n"; 

print "<tr>\n"; 

print "<td>Домашняя<br>\n"; 

print "страничка</td>\n"; 

print "<td><input type=\"text\" size=\"23\"\n"; 

print "name=\"url\" value=\"$FORM{'url'}\"></td>\n"; 

print "</tr>\n"; 

print "<tr>\n"; 

print "<td colspan=\"2\">Город: <input\n"; 

print "type=\"text\" size=\"15\" name=\"city\" value=\"$FORM{'city'}\">,\n"; 

print "Страна: <input type=\"text\"\n"; 

print "size=\"15\" name=\"country\"  value=\"$FORM{'country'}\"\n"; 

print "</td>\n"; 

print "</tr>\n"; 

print "<tr>\n"; 

print "<td colspan=\"2\">Комментарии:<br>\n"; 

print "<textarea name=\"comments\" rows=\"6\"\n"; 

print "cols=\"40\">$FORM{'comments'}</textarea></td>\n"; 

print "</tr>\n"; 

print "<tr>\n"; 

print "<td align=\"center\"><font size=\"2\"><input\n"; 

print "type=\"submit\"\n"; 

print "value=\"ДОБАВИТЬ\"></font></td>\n"; 

print "<td><font size=\"2\"><input\n"; 

print "type=\"reset\" value=\"ОЧИСТИТЬ\"></font></td>\n"; 

print "</tr>\n"; 

print "</table>\n"; 

print "</form>\n<!-VirtualAvenueBanner->\n"; 

&print_end;

exit;

}

sub print_head {

	print "Content-type: text/html";

	print "\n\n";

	print "<html><head>";  

	print "<title>$_[0]</title>\n"; 

	print "<link rel=\"STYLESHEET\" type=\"text/css\" href=\"$styles_url\"></head>\n";

	print "<body bgcolor=\"white\" topmargin=0  marginheight=0>\n";

}

sub print_end {

	print "<div align=center>cgimaster\n<a href=\"mailto:redtiger\@mail.ru\">Leonid P</a>\n</div>\n</body>\n</html>\n";

}
