#################################################### # # Quick Poll Shared Library # Version 3.1 # (C) 1999-2000 Taro Sato, All rights reserved. # See README.TXT for important information. # #################################################### require 'webpoll.conf'; sub FileLock { local ($lockfile) = "$_[0].lock"; local ($timeout) = 30; local ($loopnum) = 0; while (-e $lockfile) { sleep (1); if (++$loopnum > $timeout) { print "Content-type: text/html\n\n"; &Error("File lock time out", "The lock file, $_[0].lock, must be deleted manually from the server."); } } open(LOCKFILE, ">$lockfile") || &FileLockError; close(LOCKFILE); } sub FileUnlock { local($lockfile) = "$_[0].lock"; unlink "$lockfile"; } sub FileLockError { print "Content-type: text/html\n\n"; &Error("Cannot create a lock file", "Quick Poll failed to create a lock file."); } sub FileCreateError { print "Content-type: text/html\n\n"; &Error("Cannot create a file", "Quick Poll failed to create a required file on your server."); } sub NoFile { local ($datafile) = $_[0]; local ($dummy) = "The poll data file, $datafile, cannot be found for the specified path."; &FileUnlock ($datafile); print "Content-type: text/html\n\n"; &Error("File not found", $dummy); } sub NoDirectory { local ($datadirectory) = $_[0]; local ($dummy) = "Quick Poll failed to open the directory, $datadirectory. Check if the access permission is set correctly."; print "Content-type: text/html\n\n"; &Error("Directory cannot be accessed", $dummy); } sub ReadPollData { local($pollid) = $_[0]; (@indata) = (""); &FileLock("$datafile.$pollid.dat"); open(FILE, "$datafile.$pollid.dat") || &NoFile("$datafile.$pollid.dat"); @indata = ; close(FILE); &FileUnlock("$datafile.$pollid.dat"); for ($i = 0; $i < @indata ; $i ++) { chop($indata[$i]); } if ($indata[0] ne "QP$qpversion") { $dummy = "The file, $datafile.$pollid.dat, is not in the Quick Poll version $qpversion format."; print "Content-type: text/html\n\n"; &Error("Data file not supported", $dummy); } ($dfversion, $qheader, $expiredate, $boxwidth, $tableborder, $cellspacing, $cellpadding, $style, $submittext, $submitimage, $viewresults, $basefont, $basefontsize, $decimal, $votepre, $votesuff, $totaltext, $barwidth, $barmaxlength, $displayorder, $textcolor, $bgcolor1, $barbackground, $bgcolor2, @choices) = @indata; } sub WritePollData { local($pollid) = $_[0]; &FileLock("$datafile.$pollid.dat"); open(FILE, ">$datafile.$pollid.dat") || &FileCreateError; print FILE "QP$qpversion\n"; print FILE "$qheader\n"; print FILE "$expiredate\n"; print FILE "$boxwidth\n"; print FILE "$tableborder\n"; print FILE "$cellspacing\n"; print FILE "$cellpadding\n"; print FILE "$style\n"; print FILE "$submittext\n"; print FILE "$submitimage\n"; print FILE "$viewresults\n"; print FILE "$basefont\n"; print FILE "$basefontsize\n"; print FILE "$decimal\n"; print FILE "$votepre\n"; print FILE "$votesuff\n"; print FILE "$totaltext\n"; print FILE "$barwidth\n"; print FILE "$barmaxlength\n"; print FILE "$displayorder\n"; print FILE "$textcolor\n"; print FILE "$bgcolor1\n"; print FILE "$barbackground\n"; print FILE "$bgcolor2\n"; for ($i = 0; $i < @choices; $i += 3) { print FILE "$choices[$i]\n"; print FILE "$choices[$i+1]\n"; print FILE "$choices[$i+2]\n"; } close(FILE); &FileUnlock("$datafile.$pollid.dat"); } sub SetPollCookie { local($pollid) = $_[0]; local($expiredate) = $_[1]; $dummy = ""; if ($expiredate ne 0) { $now = time; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($now + 24 * 60 * 60 * $expiredate); $dayOfWeek = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")[$wday]; $monthName = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")[$mon]; $year += 1900 if $year < 1900; $sec = "0$sec" if $sec < 10; $min = "0$min" if $min < 10; $hour = "0$hour" if $hour < 10; $dummy = " expires=$dayOfWeek, $mday-$monthName-$year $hour:$min:$sec GMT;"; } print "Set-Cookie: "; print ("poll_id$pollid=voted;$dummy path=/\n"); } sub ParsePairs { local(@pairs) = @_; foreach $pair (@pairs) { ($key, $value) = split(/=/, $pair); $key =~ tr/+/ /; $key =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; if ($formdata{$key}) { $formdata{$key} .= ", $value"; } else { $formdata{$key} = $value; } } } sub PutFormTable { print < $qheader END if ($style eq 1) { print <
END } elsif ($style eq 0) { for ($i = 0; $i < @choices/3 ; $i ++) { $choiceID = "C"."$i"."C"; print <
$choices[$i*3] END } } elsif ($style eq 2) { for ($i = 0; $i < @choices/3 ; $i ++) { $choiceID = "C"."$i"."C"; print < $choices[$i*3]
END } } elsif ($style eq 3) { for ($i = 0; $i < @choices/3 ; $i ++) { $choiceID = "C"."$i"."C"; print <
$choices[$i*3] END } } elsif ($style eq 4) { for ($i = 0; $i < @choices/3 ; $i ++) { $choiceID = "C"."$i"."C"; print < $choices[$i*3]
END } } if ($submitimage eq "") { $dummy = ""; } else { $dummy = ""; $dummy = "\"$submittext\"" if ($submit_reset eq "reset"); } $dummy2 = ""; if ($viewresults ne "") { $dummy2 = "$viewresults"; } print <
$dummy2 $dummy
END } sub PutResultsTable { $colspan = length($displayorder) / 3; print < $qheader END $totalvotes = 0; for ($i = 0; $i < @choices ; $i += 3) { $totalvotes += $choices[$i+2]; } for ($i = 0; $i < @choices ; $i += 3) { $dummy = $totalvotes; $dummy = 1 if $dummy eq 0; $barlength = int($barmaxlength * $choices[$i+2] / $dummy); $barlength = 1 if $barlength eq 0; $dummy = int(0.5 + 10**($decimal+3) + 10**$decimal * 100 * $choices[$i+2] / $dummy); $perc1 = int(substr($dummy, 1, 3)); $perc2 = substr($dummy, 4, 4+$decimal); if ($decimal eq 0) { $perc = "$perc1"; } else { $perc = "$perc1.$perc2"; } print " \n"; for ($j = 0; $j < $colspan; $j ++) { if (substr($displayorder, $j * 3, 1) eq "C") { &PutChoice($i); } elsif (substr($displayorder, $j * 3, 1) eq "B") { $mode1 = substr($displayorder, $j * 3 + 1, 1); $mode2 = substr($displayorder, $j * 3 + 2, 1); &PutBar($i); } elsif (substr($displayorder, $j * 3, 1) eq "P") { &PutPercent($i); } elsif (substr($displayorder, $j * 3, 1) eq "N") { &PutNum($i); } } } print < $totaltext $totalvotes END } sub PutChoice { $i = $_[0]; print < $choices[$i] END } sub PutBar { $i = $_[0]; $dummy = ""; if ($mode1 eq 1) { $dummy = " $perc\%"; } elsif ($mode1 eq 2) { $dummy = " $votepre$choices[$i+2]$votesuff"; } $dummy2 = ""; if ($mode2 eq 1) { $dummy2 = "$perc\%"; } elsif ($mode2 eq 2) { $dummy2 = "$votepre$choices[$i+2]$votesuff"; } elsif ($mode2 eq 3) { $dummy2 = "$choices[$i]"; } $temp = $barmaxlength - $barlength; $temp = 1 if $temp eq 0; print < $dummy2
$dummy
END } sub PutPercent { $i = $_[0]; print < $perc% END } sub PutNum { $i = $_[0]; print < $votepre$choices[$i+2]$votesuff END } sub Error { local($error) = $_[0]; local($message) = $_[1]; print <Quick Poll Error

QUICK POLL ERROR


$error

$message


Quick Poll ver. $qpversion
(C) 2000 Taro Sato, All Rights Reserved.


END exit; } 1;