#! /usr/bin/perl
require 'load.cgi';
&check_exp;
&parse_query;
#=============================================
# M A I N P R O G R A M
if($QUERY{'action'} eq "") {
print "Content-Type: text/html\n\n";
print "\n";
print "
$title\n";
print "\n";
print "\n";
if($heading =~ /.*\.(gif|jpg)/i) {
print "
\n";
}
else {
print "$heading
\n";
}
print "
\n";
print "\n";
print "| List of current topics: (Click on a topic to view the
results and to vote) |
\n";
opendir(VOTE, "$base_dir") || die "Can't open directory $base_dir\n";
while($file = readdir(VOTE)) {
if($file =~ /.*\.$file_ext/ && $file !~ /\.old/) {
open(FILE, "$base_dir/$file") || die "Can't open $file\n";
$data =;
close(FILE);
$topic = $file;
$topic =~ s/\..*//g;
$data =~ s/\n//g;
@pairs = split(/&/, $data);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
if($name eq "question") {
$question = $value;
}
elsif($name eq "exp") {
$exp = $value;
}
else {
push(@ANSWERS, "$name");
$VOTE{$name} = $value;
}
}
if(!($exp =~ /never/i)) {
$exp -= &get_days;
$exp = "$exp days";
}
print "| $question  \; \;- \; \; Expires:
$exp |
\n";
}
}
closedir(VOTE);
print " |
\n";
print "| Previous Topics: (Click on a topic to view the
results) |
\n";
opendir(VOTE, "$base_dir") || die "Can't open $base_dir\n";
while($file = readdir(VOTE)) {
if($file =~ /.*\.$file_ext\.old/) {
open(FILE, "$base_dir/$file") || die "Can't open $file\n";
$data =;
close(FILE);
$topic = $file;
$topic =~ s/\..*//g;
$data =~ s/\n//g;
@pairs = split(/&/, $data);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
if($name eq "question") {
$question = $value;
}
elsif($name eq "exp") {
$exp = $value;
}
else {
push(@ANSWERS, "$name");
$VOTE{$name} = $value;
}
}
print "| $question |
\n";
}
}
closedir(VOTE);
print "
\n";
}
elsif($QUERY{'action'} eq "vote") {
&parse_form;
$topic = "$FORM{'topic'}";
&error("Form Incomplete") unless $FORM{'vote'};
&check('0');
if($voted) {
&error("You may only vote once! You have already voted on this topic before. If you feel this is a
mistake please contact the webmaster of the web site.");
}
$database = "$base_dir/$topic\.$file_ext";
open(FILE, "$database") || die "Can't open $data\n";
$data =;
close(FILE);
$data =~ s/\n//g;
@pairs = split(/&/, $data);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
push(@ANSWERS, "$name");
$VOTE{$name} = $value;
}
$casted_vote = $FORM{'vote'};
if((! $VOTE{$casted_vote}) && ($VOTE{$casted_vote} ne '0')) {
&error("Vote field not found!");
}
$VOTE{$casted_vote}++;
$flag = 0;
foreach $vote (@ANSWERS) {
if(! $flag) {
$string = "$vote=$VOTE{$vote}";
$flag = 1;
}
else {
$string = "$string&$vote=$VOTE{$vote}";
}
}
$string =~ s/\n//g;
open(FILE, ">$database") || die "Can't open $database\n";
print FILE "$string\n";
close(FILE);
&header("Your vote has been submited! The vote results have already been updated.");
}
elsif($QUERY{'action'} eq "results") {
$topic = $QUERY{'topic'};
&check('1');
$database = "$base_dir/$QUERY{'topic'}.$file_ext";
if(! -e $database) {
$database = "$database.old";
$voted = 5;
if(! -e $database) {
&error("Topic Not Found");
}
}
open(FILE, "$database") || die "Can't open $database\n";
$data =;
close(FILE);
$data =~ s/\n//g;
@pairs = split(/&/, $data);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
if($name eq "question") {
$question = $value;
}
elsif($name eq "exp") {
$exp = $value;
}
else {
push(@ANSWERS,"$name");
$VOTE{$name} = $value;
}
}
foreach $a (@ANSWERS) {
$total += $VOTE{$a};
}
print "Content-Type: text/html\n\n";
print "$title\n";
print "\n";
print "\n";
if($heading =~ /.*\.(gif|jpg)/i) {
print "
\n";
}
else {
print "$heading
\n";
}
print "\n";
if(!$voted) {
print "
\n";
}
else {
&error("The specified command entered into the query string was not found.
This may be caused by a script misconifguration or a bug in the CGI script.
This may also be caused because by the user.");
}
#===========================================
# S U B R O U T I N E S
sub check {
local($flag) = @_;
if($check >= 1 && $check ne '2') {
$hostname = $ENV{'REMOTE_HOST'};
if ($ENV{'REMOTE_HOST'} && ($ENV{'REMOTE_HOST'} !~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/)) {
$hostname = $ENV{'REMOTE_HOST'};
}
else {
&address_to_name ($ENV{'REMOTE_ADDR'});
}
$ip_file = "$base_dir/$topic\.ip";
open(FILE, "$ip_file") || die "Can't open $ip_file\n";
@lines =;
close(FILE);
$vote = 0;
foreach $line (@lines) {
$line =~ s/\n//g;
if($line =~ /$hostname/i) {
$voted = 1;
}
}
if((! $voted) && (! $flag)) {
open(FILE, ">>$ip_file") || die "Can't open $ip_file\n";
print FILE "$hostname\n";
close(FILE);
}
}
}
sub address_to_name {
local ($address) = shift(@_);
local (@octets);
local ($name, $aliases, $type, $len, $addr);
local ($ip_number);
@octets = split ('\.', $address);
$ip_number = pack ("CCCC", @octets[0..3]);
($name, $aliases, $type, $len, $addr) = gethostbyaddr ($ip_number, 2);
if ($name) {
$hostname = $name;
}
else {
$hostname = $ENV{'REMOTE_ADDR'};
}
}
# E N D O F S U B R O U T I N E S
#===========================================