#!/usr/bin/perl

$DATAPATH="../CGIHTML";

$baseURL="http://$ENV{'HTTP_HOST'}/";

if ($ENV{'REQUEST_METHOD'} eq "POST")
{
	#------------------------------------------#
	#                                          #
	#            М Е Т О Д    P O S T          #
	#                                          #
	#------------------------------------------#
	read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
	@args = split(/&/,$buffer);

	foreach $pair (@args) 
	{
		($name, $value) = split(/=/, $pair);

		$value =~ tr/+/ /;
		$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

		$FORM{$name} = $value;
	}

	if ($FORM{'OS'} eq "--выберите--") { &error("Выберите операционную систему"); }
	if ($FORM{'text'} eq "") { &error("Введите сообщение."); }
	if ($FORM{'name'} eq "") { &error("Введите свое имя."); }

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

	open(FILE,">>$DATAPATH/bugs.txt");
	print FILE "$FORM{'name'}\t$FORM{'text'}\t\t$FORM{'OS'} $FORM{'DirectX'} CPU: $FORM{'CPU'} $FORM{'CPUSpeed'} video:$FORM{'video'} email:$FORM{'email'} ICQ:$FORM{'ICQ'}\n";
	close(FILE);


	printhtml("$DATAPATH/ok.html");
}
else
{
	open(FILE,"$DATAPATH/bugs.txt");
	$data="";
	while ($line=<FILE>)
	{
		$line=~s/[\r\n]//g;
		$line=~s/\'/&#39;/g;
		($from,$text,$comment)=split(/\t/,$line);
		
		$data.="\nbug(\'$from\',\'$text\',\'$comment\');";
	}
	close(FILE);

	&printhtml("$DATAPATH/bugs.html");
}


sub printhtml(file)
{
	open(FILE,"$_[0]");
	read(FILE,$buffer,1_000_000);
	close(FILE);

	$buffer=~s/%DATA%/$data/g;
	$buffer=~s/%BASEURL%/$baseURL/g;
	
	print "Content-type: text/html; charset=Windows-1251\n\n";
	print $buffer;
	exit(0);
}

sub error(txt)
{
	$data=$_[0];
	&printhtml("$CGIDATA/error.html");
}
