#! /usr/bin/perl
$this_dir = $0;
$this_dir =~ s/\/[^\/]*$// && unshift(@INC,"$this_dir");
$this_dir =~ s/\\[^\\]*$// && unshift(@INC,"$this_dir");
require 'library.cgi';
require 'config.cgi';
&parse_form;
&check_exp;
if($FORM{'action'} eq "") {
# Output Voting Center main display page.
opendir(BASE,"$base_dir") || &error("Unable to open the Voting Center directory");
&header;
print qq!
Current Topics |
!;
$exp_topics = 0;
while($file = readdir(BASE)) {
if($file =~ /^(.*)\.info$/) {
$name = $1;
if(-e "$base_dir/$name.exp") {
$exp_topics = 1;
next;
}
open(INFO,"$base_dir/$file") || &error("Unable to open Voting Center file: $file");
@MYINFO =;
close(INFO);
if($MYINFO[2] =~ /never/i) {
$exp_date = "Never";
}
else {
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($MYINFO[2]);
$year += 1900;
$exp_date = "$months[$mon] $mday, $year ($days[$wday])";
}
print qq!
$MYINFO[1] |
Vote |
Results |
Expires: $exp_date |
!;
}
}
print "
\n";
closedir(BASE);
if($exp_topics) {
opendir(BASE,"$base_dir") || &error("Unable to open the Voting Center directory");
print qq!
Previous Topics |
!;
while($file = readdir(BASE)) {
if($file =~ /^(.*)\.info$/) {
$name = $1;
if(! -e "$base_dir/$name.exp") {
next;
}
open(INFO,"$base_dir/$file") || &error("Unable to open Voting Center file: $file");
@MYINFO =;
close(INFO);
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($MYINFO[2]);
$year += 1900;
$exp_date = "$months[$mon] $mday, $year ($days[$wday])";
print qq!
$MYINFO[1] |
Results |
!;
}
}
print "
\n";
closedir(BASE);
}
&footer;
}
elsif($FORM{'action'} eq "show") {
if(! -e "$base_dir/$FORM{'topic'}.info") {
&error("The topic, $FORM{'topic'}, does not exist.");
}
open(INFO,"$base_dir/$FORM{'topic'}.info") || &error("Unable to open Voting Center file: $FORM{'topic'}.info");
@MYINFO =;
close(INFO);
chomp($MYINFO[0]);
chomp($MYINFO[4]);
@answers = split(/\&\&/,$MYINFO[4]);
open(DAT,"$base_dir/$FORM{'topic'}.dat") || &error("Unable to open Voting Center file: $FORM{'topic'}.dat");
@MYDATA =;
close(DAT);
# Output results page for specified topic
&header;
print qq!
Results
$MYINFO[1] ($MYINFO[0] votes) |
!;
$i = 0;
foreach $answer (@answers) {
chomp($MYDATA[$i]);
if($MYINFO[0] > 0) {
$percentage = int($MYDATA[$i] / $MYINFO[0] * 100);
$bar = $percentage * $bar_length;
}
else {
$percentage = 0;
$bar = 5;
}
print qq!
$answer
|
$MYDATA[$i] ($percentage\%)
|
!;
$i++;
}
print qq!
!;
&footer;
}
elsif($FORM{'action'} eq "vpage") {
# Output Voting Page for the specified topic
open(INFO,"$base_dir/$FORM{'topic'}.info") || &error("Unable to open Voting Center file: $FORM{'topic'}.info");
@MYINFO =;
close(INFO);
chomp($MYINFO[4]);
@answers = split(/\&\&/,$MYINFO[4]);
&header;
print qq!
!;
&footer;
}
elsif($FORM{'action'} eq "vote") {
# Log the vote for the specified topic
if(&validate("$FORM{'topic'}")) {
&error("Our records show that you have already voted for this topic. Please only vote once per topic.");
}
if(! -e "$base_dir/$FORM{'topic'}.info") {
&error("The topic you are trying to participate in, $FORM{'topic'}, was not found.");
}
if(-e "$base_dir/$FORM{'topic'}.exp") {
&error("The topic you are trying to vote in has already expired and is no
longer accepting votes. However, you can see the results of the poll here.");
}
&file_lock("$FORM{'topic'}");
open(INFO,"$base_dir/$FORM{'topic'}.info") || &error("Unable to open Voting Center file: $FORM{'topic'}.info");
@MYINFO =;
close(INFO);
open(DAT,"$base_dir/$FORM{'topic'}.dat") || &error("Unable to open Voting Center file: $FORM{'topic'}.dat");
@MYDATA = ;
close(DAT);
$vote = $FORM{'vote'};
chomp($MYDATA[$vote]);
$MYDATA[$vote] += 1;
$MYDATA[$vote] .= "\n";
open(DAT,">$base_dir/$FORM{'topic'}.dat") || &error("Unable to write to Voting Center file: $FORM{'topic'}.dat");
print DAT @MYDATA;
close(DAT);
chomp($MYINFO[0]);
$MYINFO[0] += 1;
$MYINFO[0] .= "\n";
open(INFO,">$base_dir/$FORM{'topic'}.info") || &error("Unable to write to Voting Center file: $FORM{'topic'}.info");
print INFO @MYINFO;
close(INFO);
&file_unlock("$FORM{'topic'}");
&loguser("$FORM{'topic'}");
if($FORM{'type'} eq "ssi") {
print "Content-Type: text/html\n";
print "Location: $ENV{'HTTP_REFERER'}?results\n\n";
exit;
}
chomp($MYINFO[3]);
if($MYINFO[3] =~ /.*\:\/\/.*\..*/) {
print "Content-Type: text/html\n";
print "Location: $MYINFO[3]\n\n";
exit;
}
&header;
print qq!
Your vote has been casted
Thank you for participating in our poll. The results have already been updated and you
can view them here.
!;
&footer;
}
else {
&error("Invalid Data: Voting Center does not understand the parameters you specified");
}