#!/usr/bin/kitasperlas

$guestlocation = "http://www.druskonis.lt/zmones/artas/guestbook.htm";
$guestpath = "/var/www/docs/vija/druskonis/zmones/artas/guestbook.htm";
$cgilocation = "http://www.elnet.lt/cgi-bin/vija/guested.cgi";
$mailprog = '/usr/sbin/sendmail';
$youmail = 'artas@is.lt';
$yourname = "Arturas";
$getdate = "/bin/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;
	$INPUT{$name} = $value;
}

$date = `$getdate +"%A, %B %d, %Y at %T (%Z)"`;
chop($date);

&noname unless $INPUT{'name'};

open (FILE,"$guestpath");
@lines = <FILE>;
close(FILE);

open (GUEST,">$guestpath");

foreach $line (@lines) {
	if ($line =~ /<!--begin-->/) { 
		print GUEST ("<!--begin-->\n");
	if ($INPUT{'url'}) {
	print GUEST ("<tr><td width=25%><center><a href=\"$INPUT{'url'}\">$INPUT{'name'}</center></td>\n");
	} else {
print GUEST ("<tr><td width=25%><center>$INPUT{'name'}</center></td>\n");
		}
print GUEST ("<td width=25%><center><a href=\"mailto:$INPUT{'email'}\">");
print GUEST ("$INPUT{'email'}</a></td></center>\n");
print GUEST ("<td width=20%><center>$INPUT{'city'} </td></center>\n");
print GUEST (" <td width=20%><center>$INPUT{'country'}</td></center>\n");
print GUEST (" <td width=10%><center><b>$INPUT{'number'}</b></td></center></tr>\n");
#####		print GUEST ("-$date<hr>\n\n");

	} else {

		print GUEST ("$line");

	}

}

close (GUEST);

open (MAIL, "|$mailprog $youmail");

if ($INPUT{'email'}) {
	print MAIL ("From: $INPUT{'name'} <$INPUT{'email'}>\n");
} else {
	print MAIL ("From: $INPUT{'name'}\n");
}
print MAIL ("Subject: Guestbook Signing\n\n");
print MAIL ("$INPUT{'name'} has signed your guestbook.\n\n");
print MAIL ("Here is the new entry:\n");
print MAIL ("$INPUT{'name'}\n");
if ($INPUT{'email'}) {
	print MAIL ("$INPUT{'email'}\n");
}
if ($INPUT{'city'}) {
	print MAIL ("$INPUT{'city'}, ");
}
if ($INPUT{'state'}) {
	print MAIL ("$INPUT{'state'} ");
}
if ($INPUT{'country'}) {
	print MAIL (" $INPUT{'country'}\n");
}
if ($INPUT{'howfound'}) {
	print MAIL ("* $INPUT{'howfound'} *\n");
}
if ($INPUT{'url'}) {
        print MAIL ("$INPUT{'title'} url: $INPUT{'url'}\n");
}
if ($INPUT{'comments'}) {
	print MAIL ("$mailcomments\n");
}
print MAIL ("-$date\n");
close (MAIL);

# Send info to visitor

if ($INPUT{'email'}) {
	if ($INPUT{'email'} =~ /(.*)@(.*)/) {
		open (MAIL, "|-") || exec $mailprog,$INPUT{'email'} || die "Can't open $mailprog!\n";
		print MAIL ("From: $yourname <$youmail>\n");
		print MAIL ("Subject: Aciu, Thanks\n\n");
		print MAIL ("$INPUT{'name'},\n\n");
		print MAIL ("Aciu. Malonu, kad naudojates Pasaulio Ruoniu knyga !\n  ");
		print MAIL ("Savo ivesta informacija galite paziureti: $guestlocation\n");
		print MAIL ("Viso ko geriausio !\n\n");
		print MAIL ("Thank you for signing WorldSealsbook!\n  ");
		print MAIL ("You can see your entry at $guestlocation\n");
		print MAIL ("Thanks again!\n\n");
		print MAIL ("- $yourname\n\n");
		close (MAIL);
	}
}

&htmlafter;

# Error Messages

sub noname {

print ("Content-type: text/html\n\n");
print ("<html><head><title>Klaida</title></head>\n");
print ("<body><h1>J&ucirc;s nepara&eth;&euml;te savo vardo
<br>You Didn't Leave Your Name</h1>\n");
print ("You didn't add your name so your entry to the ");
print ("Sealsbook was not added.<hr>");
print ("<br><b>Please go back and add your name.</b><br>\n");
print ("</body></html>\n");

exit;

}

# Print Follow Up HTML

sub htmlafter {

print ("Content-Type: text/html\n\n");
print ("<html><head><title>Thank You</title></head>\n");
print ("<body><h1>Thank You For Signing The WorldSealsbook</h1>\n");
print ("Thank you for filling in WorldSealsbook.  Your entry has ");
print ("been added.<hr>\n");
print ("Here is what you added:<br><br>\n");
print ("<b>$INPUT{'name'}</b><br>\n");
if ($INPUT{'email'}) {
	print ("<a href=\"mailto:$INPUT{'email'}\">");
	print ("$INPUT{'email'}</a><br>");
}
if ($INPUT{'city'}) {
	print ("<b>$INPUT{'city'}, </b>");
}
if ($INPUT{'country'}) {
	print (" <b>$INPUT{'country'}</b><br>");
}
if ($INPUT{'url'}) {
	print ("<a href=\"$INPUT{'url'}\">$INPUT{'url'}</a><br>");
}
if ($INPUT{'comments'}) {
	print ("$INPUT{'comments'}<br>");
}
print ("-$date<hr>");
print ("<a href=\"$guestlocation\">Back to the Guestbook</a>. \n");         
print ("If you do not see your addition, hit RELOAD<br>\n");

exit;

}
