#!/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 $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 $poll = $query->param('poll');

my $ip = $query->param('addr');
my $session = TClubMD5::md5_hex($who.$ip.$salt);

die("ins_vote.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 poll_results (b_participant,a_question_id,c_vote) values ('$who','$poll','t')";


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;
}
}
