#!/usr/bin/perl # view-poll.pl # Advanced Internet Technologies, Inc. All Rights Reserved # Created: 22 Aug 2001 # Last Modified: 18 Nov 2001 # # purpose: reads data file, adds vote, presents results require '../suiteconfig.pl'; require '../officeconfig.pl'; #-############################################# # Print The Page Header # print "Content-type: text/html\n\n"; print "$office{'header'}"; # #-############################################# sub error_die { print @_, "\n"; print "$office{'footer'}"; exit(0); } sub get_vote { open(IN,"<$poll_dir/$poll_file"); $poll_name = ; $poll_name =~ s/[\r\n]//g; $poll_query = ; $poll_query =~ s/[\r\n]//g; print "$poll_query
\n"; print "
\n"; print "
\n"; print "\n"; print "\n"; print "\n"; print "
\n"; print "View Results\n"; print "\n"; close(IN); return; } sub show_results { $tot_votes = 0; if ($FORM{"viewonly"} ne "yes" && $already_voted == 0) { &set_poll_cookie(); } $infile = "$poll_dir/$poll_file"; $tmpfile = "$infile.tmp"; open(IN,"<$infile"); open(OUT, ">$tmpfile"); $poll_name = ; $poll_name =~ s/[\r\n]//g; $poll_query = ; $poll_query =~ s/[\r\n]//g; $i = 0; while ($lin = ) { $lin =~ s/[\r\n]//g; ($opt[$i], $num[$i]) = split(/:/, $lin); if ($FORM{"choice"} eq $opt[$i] && $FORM{"viewonly"} ne "yes" && $already_voted == 0) { $num[$i]++; } $tot_votes += $num[$i]; $i++; } print OUT "$poll_name\n"; print OUT "$poll_query\n"; print <<"Beg";
Beg for($n=0;$n<$i;$n++) { if($num[$n] > 0) { $pct = $num[$n]/$tot_votes; $pct = $pct * 100; $pct = sprintf("%.0f", $pct); $otherPct = 100 - $pct; }else{ $pct = 0; } print ""; print "\n"; print OUT "$opt[$n]:$num[$n]\n"; } print <<"Beg";
$poll_query
$opt[$n]"; print ""; print "
$pct%
$num[$n]
Total:  $tot_votes votes
Beg close(OUT); close(IN); `cp -f $tmpfile $infile`; return; } sub set_poll_cookie { #Set-Cookie: $poll_file=voted"; return; } sub get_poll_cookie { $retval = 0; @cookie_pairs = split(/; /, $ENV{'HTTP_COOKIE'}); foreach $pair (@cookie_pairs) { ($name, $value) = split(/=/, $pair); if ($name eq $poll_file && $value eq "voted") { $retval = 1; last; } } return $retval; } sub GetPollList { print "

Please Select the Poll

"; opendir(POLLDIR,"$office{'polldata'}"); @files = grep { $_ ne '.' and $_ ne '..' } readdir POLLDIR; closedir POLLDIR; $i = 0; foreach $file (@files) { # get rid of the temp files if (!($file =~ /\.tmp$/)) { open(IN, "<$office{'polldata'}/$file"); $poll_name = ; $poll_name =~ s/[\r\n]//g; print "$poll_name
"; close(IN); $i++; } } print "

"; print $office{'footer'}; } if ($ENV{'REQUEST_METHOD'} eq "GET") { $buffer = $ENV{'QUERY_STRING'}; } else { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); } @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $name =~ tr/+/ /; $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $FORM{$name} = $value; } if ($FORM{"poll"} eq "") { GetPollList(); exit(); } @opts = (); $poll_dir = "$office{'polldata'}"; $poll_file = $FORM{"poll"}; if ( ! -e "$poll_dir/$poll_file") { &error_die("Could not find data for poll"); } $already_voted = &get_poll_cookie(); if ($FORM{"voted"} eq "yes" || $FORM{"viewonly"} eq "yes" || $already_voted == 1) { &show_results(); } else { &get_vote(); } print "$office{'footer'}"; exit(0);