#! /usr/bin/perl ######################################################### # # # Voting Center Version: 1.0C (Beta) # # Last Modified: 10-05-97 # # Written By: David Tsai # # http://www.web-consult.com # # # ######################################################### # # # Copyright © 1997 David Tsai, Web Consultant. # # Permission to use this script granted. This script # # shall not be redistributed in anyway without the # # written consent of David Tsai. This script shall # # not be modified in any way without the written # # consent of David Tsai, although you may modify # # the variables in the config.cgi # # # ######################################################### require 'config.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 "\"$title\"\n"; } else { print "

$heading

\n"; } print "\n"; print "\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 "\n"; } } closedir(VOTE); print "\n"; print "\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 "\n"; } } closedir(VOTE); print "
List of current topics: (Click on a topic to view the results and to vote)
$question  \; \;- \; \; Expires: $exp
Previous Topics: (Click on a topic to view the results)
$question
\n"; print "This script made by: CGI Paradise\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 "\"$title\"\n"; } else { print "

$heading

\n"; } print "\n"; if(!$voted) { print "
\n"; print "\n"; } print "
Back to $sitename\ \;\ \; | \ \;\ \;\n"; print "Back to topic listing
\n"; print "$question  \;\[$total votes total\]\n"; foreach $key (@ANSWERS) { if($total eq '0') { $percent = 0; } else { $percent = int($VOTE{$key} / $total * 100); } $width = $percent * $length; $width = 1 unless $width; print "\n"; if(! $voted) { print "\n"; } print " $key\n"; print " $VOTE{$key}\($percent\%\)\n"; } print "Status:\n"; if(! $voted) { print "You have not voted for this topic.
\n"; } elsif($voted eq '5') { print "This topic has already expired\n"; } else { print "You have already voted for this topic.\n"; } print "\n"; print "Script made by: CGI Paradise

\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 #===========================================