#!/usr/bin/perl
#

use 5.006;
use CGI;
use DBI;
use lib qw(/mnt/sda/tmm/handlers/genc);
use TClubMD5;
use Fcntl ':flock';

my $server = "127.0.0.1";
my $user = $primary_user;
my $passwd = $primary_passwd;
my $dbase = $primary_dbase;
my $port = $primary_port;

my $timeshift = 0;

my $query = new CGI;

print "Content-Type: text/html; charset=KOI8-R\n\n";

my $type = $query->param('type');

my $who = $query->param('who');
my $token = $query->param('token');
my $ru_en = $query->param('l');

my $banner = $query->param('ban');
my $ip = $query->param('addr');
my $params = $query->param('params');

my $session = TClubMD5::md5_hex($who.$ip.$salt);

die("ins_ban.pl: token is $token, session is $session!\n") if ($token ne $session);
die("psssooff!\n") if (in_list($who,$pssoff_guys_string));

($dbase,$server,$port,$user,$passwd) = TClubMD5::get_connect_req($ru_en);

$dbconn=DBI->connect("dbi:Pg:dbname=$dbase;port=$port;host=$server",$user, $passwd);

my $cmd = "insert into banners (ruser,ip,banner,date_and_time,click,params) values ('$who','$ip','$banner',current_timestamp,'t','$params')";

if ($use_server && $server ne $primary_server) {
$dbconn = DBI->connect("dbi:Pg:dbname=$primary_dbase;port=$primary_port;host=$primary_server",$primary_user, $primary_passwd);
$dbconn->{LongReadLen} = 16384;
}

my $result=$dbconn->prepare($cmd);
$result->execute;
&dBaseError($dbconn, $cmd) if (!defined($result));

$dbconn->disconnect;
$query->delete_all;
exit;

sub dBaseError {

    my ($check, $message) = @_;
    print "<H4><FONT COLOR=BLACK><P>$message<BR>Error: ".$check->errstr."</FONT></H4>";
    die("Action failed on command:$message  Error_was:$DBI::errstr");
};

sub in_list {
my $guy = shift;
my $str = shift;
my $comma = shift;

if (defined($comma)) {
return 1 if ($str eq $guy || $str =~ /^$guy\,/ || $str =~ /\,$guy\,/ || $str =~ /\,$guy$/);
return 0;
} else {
return 1 if ($str eq $guy || $str =~ /^$guy\#/ || $str =~ /\#$guy\#/ || $str =~ /\#$guy$/);
return 0;
}
}
