#!/usr/bin/perl

require "pvd-lib.pl";
getheader();
require "pvd-index.cfg";

$in{coder}="win" if($in{coder}=~ /windows-1251/i);
$in{coder}="dos" if($in{coder}=~ /cp866/i);
$in{coder}="koi" if($in{coder}=~ /koi8-r/i);
$in{coder}="iso" if($in{coder}=~ /iso-8859-5/i);

# проверка внесенных данных
$check_errors=0; $err_message=''; $err_message0='';$err_messagetxt='';
foreach(sort {$a <=> $b} @gbform) {
    $check_errors0=0;
    @ttemp_mas=split(/\,/);
    $temp_mess="message$ttemp_mas[0]";
    
    if ($ttemp_mas[5] < length($in{$temp_mess}) and $ttemp_mas[4]=~ /^text|utext|etext/i) {
        $check_errors=1;
        $err_message_size.=" Длинна введенной вами информации в поле [$ttemp_mas[3]] превысила лимит. ";
    }
    if ($ttemp_mas[2] =~ /Y/i and !$in{$temp_mess}) {
        $check_errors=1;
        $err_message_mandatory.=" Не введено обязательное поле [$ttemp_mas[3]]. ";
    }
    if ($ttemp_mas[4]=~ /^textarea/i) {
        $in{$temp_mess}=~ s/[\x0D\x0A]/\x0A/igm;
        $in{$temp_mess}=~ s/\x0A\x0A/\x02/igm;
    }
    $addto_cookie{$temp_mess}=1 if ($ttemp_mas[1] =~ /Y/i);
    $err_messagetxt.="$temp_mess\x01$in{$temp_mess}\n";
    if ($ttemp_mas[4]=~ /^etext/i and $in{$temp_mess}) {
        if ($in{$temp_mess}!~ /([\w,\-,\.]+\@[\w,\-,\.]+\.\w{2,3})/i) {
            $check_errors=1;
            $err_message0.="<br>Неправильный e-mail [$in{$temp_mess}] в поле $ttemp_mas[3]."
        }
    }
    elsif ($ttemp_mas[4]=~ /^utext/i and $in{$temp_mess}) {
        if ($in{$temp_mess}!~ /(http:\/\/[\w,\.,\-,\&,\/,\~,\?]+)/i) {
            $check_errors=1;
            $err_message0.="<br>Неправильный url [$in{$temp_mess}] в поле $ttemp_mas[3]."
        }
    }
    elsif ($ttemp_mas[4]=~ /^select|radio|iradio/i) {
        for $ti (5..$#ttemp_mas) {
            if ($ttemp_mas[$ti]==$in{$temp_mess}) {$check_errors0=1}
        }
        if (!$check_errors0) {$check_errors=1;$err_message0.="Нет такого значения [$in{$temp_mess}] в поле $ttemp_mas[3].";
        }

    }
}
no_ban();
if ($check_errors) {
    $temp_fname="".time."";
    open TEMP,">temp/$temp_fname";
    $err_message.="." if ($err_message);
    print TEMP "$err_message_ban$err_message_size$err_message$err_message0\n$err_messagetxt";
    close TEMP;
    if ($ENV{HTTP_REFERER}=~ /\?/) {
        print "Location: ".(substr($ENV{HTTP_REFERER},0,rindex($ENV{HTTP_REFERER},"\?")))."?message=$temp_fname\n\n";}
    else {print "Location: $ENV{HTTP_REFERER}?message=$temp_fname\n\n";}
}
else {
    post_to_file();
    $ENV{HTTP_REFERER}=~ s/\&message=(\d+)//igm;
    $ENV{HTTP_REFERER}=~ s/\?message=(\d+)//igm;
    print "Location: $ENV{HTTP_REFERER}\n";
    cookie_add();
}



sub post_to_file {
my $temp_string=time;
foreach(sort {$a <=> $b} @gbform) {
    @ttemp_mas=split(/\,/);
    $temp_mess="message$ttemp_mas[0]";
    if ($in{$temp_mess}) {
    $in{$temp_mess}=encoder($in{$temp_mess},$in{coder},$message{coder});
    $in{$temp_mess} =~ s/\"/\&quot\;/igm;
    $in{$temp_mess} =~ s/</\&lt\;/igm;
    $in{$temp_mess} =~ s/>/\&gt\;/igm;
#    $in{$temp_mess} =~ s/(\s\W{1,}){2,}/$1/igm;
    } else {$in{$temp_mess}=''}
    if ($ttemp_mas[4]=~ /^textarea/i) {
        $in{$temp_mess}=chehttp($in{$temp_mess});
        $in{$temp_mess}=chemail($in{$temp_mess});
        $temp_string.="\x01$in{$temp_mess}";
    } else {
        $temp_string.="\x01$in{$temp_mess}";
    }
}
    open (TEMP,">>$message{dbpath}/pvd-index.msg"); flock(TEMP, 2); binmode TEMP;
    if ((-s "$message{dbpath}/pvd-index.msg")>5) {
        print TEMP "\x0A",$temp_string,"\x01",$ENV{'REMOTE_ADDR'}
    } else {
        print TEMP $temp_string,"\x01",$ENV{'REMOTE_ADDR'}}
    flock(TEMP, 8);
    close TEMP;

}

#------------------------------------------------------#
sub cookie_add {
   (@days) = ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
   (@months) = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
   ($sec,$min,$hour,$mday,$mon,$year,$wday) = localtime(time+31536000);
   $sec = "0" . $sec if $sec < 10; $min = "0" . $min if $min < 10;
   $hour = "0" . $hour if $hour < 10; $year += 1900;
   $cookdate = "$days[$wday], $mday-$months[$mon]-$year $hour:$min:$sec GMT";

   while(($key,$value) = each(%in)){
    if ($key=~ /^message(\d+)/i and $addto_cookie{$key}) {
        if ($value) {
        print "Set-Cookie: $key=$value; expires=$cookdate;";
        print " path=/; domain=$ENV{'HTTP_HOST'};\n";
        }
    }
   }
   print "\n";
}

#------------------------------------------------------#
sub no_ban {
# Проверка на бан
    my $r_a=$ENV{'REMOTE_ADDR'};
    if (-e "$message{dbpath}/pvd-index.ban") {
    open(BAN,"<$message{dbpath}/pvd-index.ban");
        while (<BAN>) {
         $line_ip=$_;$line_ip=chdel($line_ip);
         if ($r_a =~ /^$line_ip/ and $line_ip) {
         $check_errors=1;
         $err_message_ban="Вам запрещено вносить сообщения!<br>";
         last;
         }
        }
    close(BAN);
    }
}
