#!/usr/bin/perl

use File::Basename;

$wdir="/usr/local/www/cgi-bin/www.gazeta.ru/chat2";
print "Content-type: text/html\n\n";

%formdata=();
%filenames=();

if( $ENV{'CONTENT_TYPE'}=~/multipart/ )
{
    binmode STDIN;
    read(STDIN, $query, $ENV{'CONTENT_LENGTH'});

    $query=~/\A(.+?)(\015|\012)/;
    $boundary = $1;

    (@allkeys) = split/$boundary/,$query;

    foreach $mykey (@allkeys) {

        if($mykey=~/name="(.+?)"/) {
            $keyname=$1;
            $mykey=~s/\A(.+?)\015{0,1}\012\015{0,1}\012//s;
            $keyinfo=$1;

            if( $keyinfo=~/filename="(.+?)"/ ) {
                $filename = $1;
            } else { $filename=""; }

            $mykey=~s/\015{0,1}\012\Z//;

            $formdata{$keyname}=$mykey;
            $filenames{$keyname}=$filename;
        }
    }
} else {
if( $ENV{'REQUEST_METHOD'} eq 'POST' )
{
    read(STDIN, $query, $ENV{'CONTENT_LENGTH'});

    @params=split(/&/, $query);
    foreach $keyvalueparam (@params) {
        ($keyparam, $valueparam) = split(/=/, $keyvalueparam);
        $valueparam=~tr/+/ /;
        $valueparam=~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
        $formdata{$keyparam}=$valueparam;
    }
} else
{
    $query = $ENV{'QUERY_STRING'};

    @params=split(/&/, $query);
    foreach $keyvalueparam (@params) {
        ($keyparam, $valueparam) = split(/=/, $keyvalueparam);
        $valueparam=~tr/+/ /;
        $valueparam=~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
        $formdata{$keyparam}=$valueparam;
    }
}
}


if ( $formdata{action} eq 'bannow')
    {
#    print "!";
    local $/=undef;
    open (TPLIN, "$wdir/banlist"); $tpl=<TPLIN>; close(TPLIN);
    local $/="\n";
    
    $ip="$formdata{'zloip'}l";
    $ip=~s/\./l/g;
    $tpl=~s/$ip +\n//g;
    $ip=substr("$ip                                   ",0,16);
    $tpl.="$ip\n";

    open (TT, ">$wdir/banlist");
    print TT $tpl;
    close(TT);
    }

if ($formdata{'action'} eq 'unban')
    {

    local $/=undef;
    open (TPLIN, "$wdir/banlist"); $tpl=<TPLIN>; close(TPLIN);
    local $/="\n";
    
    $ip="$formdata{'powah'}l";
    $ip=~s/\./l/g;
    $tpl=~s/$ip +\n//g;

    open (TT, ">$wdir/banlist");
    print TT $tpl;
    close(TT);
    }

local $/=undef;
open (TPLIN, "$wdir/tpl/banpowah.html"); $tpl=<TPLIN>; close(TPLIN);
local $/="\n";

($nicked)=($tpl=~m/::nickpowah(.*?)nickpowah::/s);

$tonick="";

opendir (DIR, "$wdir/users/") or die "Shit!";
while (defined ($file=readdir(DIR)))
{
$tnc=$nicked;

($ext)=($file=~m/user\..+?\.(.*)/);
($ip)=($file=~m/user\.(.+?)l\d+\..*/);
$ip=~s/l/\./g;

#print "$file--$ext--$ip\n";

$tnc=~s/::ip::/$ip/;
$tnc=~s/::name::/$ext/;

if ($ext ne "") {$tonick.=$tnc;}

}
closedir (DIR);

$tpl=~s/::nickpowah(.*?)nickpowah::/$tonick/s;


($nicked)=($tpl=~m/::banned(.*?)banned::/s);

$tonick="";


open (a,"$wdir/banlist");
while (<a>)
{
$tnc=$nicked;

$ip=$_;
$ip=~s/[a-z]/\./g;
$ip=~s/\.0+/\./g;
($ip)=($ip=~m/(.*)\./);

$tnc=~s/::ip::/$ip/g;

$tonick.=$tnc;

#print "$ip\n";
}
close(a);

$tpl=~s/::banned(.*?)banned::/$tonick/s;

print $tpl;
