#!/usr/local/bin/perl ## ### # # instantpoll.cgi # v .75 # created By KeyBoard Jockeys, CopyLeft 1999 # http://www.baratta.com/keyboard ### ## ## Directions To Use Instant Poll.. ## (You must have the ability to use the cmd ## function with SSI or parsed-html & have ## cgi-lib.pl installed or available) ## ## Create a file with the following: ## ## PollOne (example file...chmod to 644) ## ## CallingPage|/full/path/to/page ## Question|Who is the best player of the 90's? ## Answers|3 ## 1|Jerry Rice|/www/path/to/image|0 ## 2|Barry Sanders|/www/path/to/image|0 ## 3|John Elway|/www/path/to/image|0 ## Total|0 ## Expires|7-26-1999 ## FontFace|Arial ## BackGroundColorHeader|#FFFFCC ## BackGroundColorBody|#FFFFCC ## HeaderStart|August Question ## HeaderResults|August Results ## ## (Make sure the date uses '-' ## Month-Day-Year Format and the year is 4 ## digits. No date means poll never expires.) ## ## Add the following code to your HTML ## ## ## ## (Replace full/path/to/PollOne with the path/file name of your Poll file.) ## ## ## The program will check to see if there is a ## cookie posted on the browser and, if no ## cookie - print the Poll Question. If there is ## a cookie or the expiration date has past, it will post the answers. ## ## Add the following Tags in the bottom of the page that calls instantpoll.cgi ## Between the and tags. ## ##
## ## ## ##
## ## Trust me on that. IE has a bug that will cache the page if the tags ## are in the top of the page. ## ## -- Apache Web Services -- ## You may need to add the following to your .htaccess file ## ## AddHandler server-parsed poll.html ## ## change poll.html to whatever you HTML file name is calling the script. ## or give AddHandler an extention and change your page to that exention. ## e.g. poll.shtml ## ## -- Netscape Web Services -- ## You need to make your file .shtml instead of .html ## ## #BEGIN {unshift @INC, '/full/path/to/personal/libraries' }; # Uncomment above line or put cgi-lib.pl in same directory. require "cgi-lib.pl"; MAIN: { my ( $varFullProgramNameAndPath, $varWebProgramNameAndPath, %input, $ret, @PollFile, @CookieString, $var,$Key,$Value,$i, $varExpireDate,$varGMTExpireDate, $varToday,$month,$monthname,$day,$year, %MonthConvert, $ExpMonth,$ExpDay,$ExpYear, $varDescription,$varImagePath,$varCount, $lockfile,$tempfile, $varPollResults, $varCallingPage, ); ########################### #### User Tweaked Variables ########################### $varFullProgramNameAndPath = "/full/path/to/instantpoll.cgi"; $varWebProgramNameAndPath = "/web/path/to/instantpoll.cgi"; $varFontFace = "Arial"; ########################### ## Reading and parsing the data. Save the return value to $ret $ret = &ReadParse(\%input); ## A bit of error checking never hurt anyone if (!defined $ret) { &ErrorNoInfo; } ## Looking for Poll Variable if ($input{'Poll'} eq "" || $input{'Poll'} eq " " ) { &ErrorNoPollName; } @PollString = split("/",$input{'Poll'}); $varPollName = $PollString[$#PollString]; ##Reading Poll File if (-w $input{'Poll'}) { open (POLLFILE, $input{'Poll'}); @PollFile = ; close(POLLFILE); } else { &ErrorWithPollFile; } # end if (-w $input{'Poll'}) ## Removing Line Feeds for ($i=0;$i<=$#PollFile;$i++) { chomp($PollFile[$i]); } # End for ($i=0;$i<=$#PollFile;$i++) ## Storing Poll File Info for Quick Access foreach $var (@PollFile) { ($Key,$Value)=split(/\|/,$var,2); if ($Key eq "CallingPage") { $varCallingPage=$Value; } # End if ($Key eq "CallingPage") if ($Key eq "Question") { $varPollQuestion=$Value; } # End if ($Key eq "Question") if ($Key eq "Answers") { $varNumberofAnswers=$Value; } # End if ($Key eq "Answers") if ($Key eq "Total") { $varTotalVotes = $Value; } # End if ($Key eq "Total") if ($Key eq "Expires") { $varExpireDate=$Value; } # End if ($Key eq "Expires") if ($Key eq "FontFace") { $varFontFace=$Value; } # End if ($Key eq "FontFace") if ($Key eq "BackGroundColorHeader") { $varBackGroundColorHeader=$Value; } # End if ($Key eq "BackGroundColorHeader") if ($Key eq "BackGroundColorBody") { $varBackGroundColorBody=$Value; } # End if ($Key eq "BackGroundColorBody") if ($Key eq "HeaderStart") { $varHeaderStart=$Value; } # End if ($Key eq "HeaderStart") if ($Key eq "HeaderResults") { $varHeaderResults=$Value; } # End if ($Key eq "HeaderResults") } # End foreach $var (@PollFile) ## Setting up Background Colors for Table if ($varBackGroundColorHeader ne "") { $varTableBackGroundColorHeader = "bgcolor=$varBackGroundColorHeader"; } else { $varTableBackGroundColorHeader = ""; } if ($varBackGroundColorBody eq "") { $varTableBackGroundColorBody = ""; } else { $varTableBackGroundColorBody = "bgcolor=$varBackGroundColorBody"; } ## Checking Poll Expiration Date if ($varExpireDate eq "") { &BuildPollForm; exit; } else { $varToday = `date \'\+\%b \%e \%Y\'`; ($monthname,$day,$year) = split(/ /,$varToday,3); %MonthConvert = ("Jan" => "1","Feb" => "2","Mar" => "3","Apr" => "4","May" => "5","Jun" => "6", "Jul" => "7","Aug" => "8","Sep" => "9","Oct" => "10","Nov" => "11","Dec" => "12"); %GMTMonthConvert = ("1" => "Jan","2" => "Feb","3" => "Mar","4" => "Apr","5" => "May","6" => "Jun", "7" => "Jul","8" => "Aug","9" => "Sep","10" => "Oct","11" => "Nov","12" => "Dec"); $month = $MonthConvert{$monthname}; ($ExpMonth,$ExpDay,$ExpYear) = split(/\-/,$varExpireDate,3); $varGMTExpireDate = $ExpDay . "\-" . $GMTMonthConvert{$ExpMonth} . "\-" . $ExpYear; if (($year > $ExpYear) || ($month > $ExpMonth && $year == $ExpYear) || ($day > $ExpDay && $month == $ExpMonth && $year == $ExpYear)) { &PrintPollResults; exit; } # End } # End if ($varExpireDate eq "") ## Looking for Existing Cookie @CookieString = split(/\; /, $ENV{HTTP_COOKIE}); foreach $var (@CookieString) { ($Key,$Value) = split(/\=/, $var); if ($Key eq $varPollName) { &PrintPollResults; exit; } # End if ($Key eq "$input{'Poll')") } # End foreach $var (@CookieString) ## Looking for NewVote Variable if ($input{'NewVote'} eq "Yes" && $ENV{'REQUEST_METHOD'} ne "GET") { &ProcessVotePrintPollResults; exit; } # End if ($input{'NewVote'} eq "Yes") ## Building Poll Form &BuildPollForm; ############### ## Sub Routines ############### sub ErrorNoInfo { print "Content-type: text/html\n\n"; print " \n"; print "Script Processing Error - No Info passed to script check SSI"; print " \n"; exit; } # End sub ErrorNoInfo sub ErrorNoPollName { print "Content-type: text/html\n\n"; print " \n"; print "Script Processing Error - No Poll Value"; print " \n"; exit; } # End sub ErrorNoInfo sub ErrorWithPollFile { print "Content-type: text/html\n\n"; print " \n"; print "Script Processing Error - Can't find/open/read Poll File"; print " \n"; exit; } # End sub ErrorWithPollFile sub ErrorWithLockFile { print "Content-type: text/html\n\n"; print " \n"; print "Script Processing Error - Lock file exists wait timeout"; print " \n"; exit; } # End sub ErrorWithPollFile sub ErrorWritingPollFile { print "Content-type: text/html\n\n"; print " \n"; print "Script Processing Error - Can't write temp file for poll results"; print " \n"; exit; } # End sub ErrorWithPollFile sub ProcessVotePrintPollResults { ## Processing Vote $varNoVote="NoVote"; for ($i=0;$i<=$#PollFile;$i++) { ($Key,$Value)=split(/\|/,$PollFile[$i],2); if ($Key eq $input{'Answer'}) { ($varDescription,$varImagePath,$varCount) = split(/\|/,$Value,3); $varCount++; $PollFile[$i] = $Key ."\|" . $varDescription ."\|" . $varImagePath ."\|" . $varCount; $varNoVote="VoteCounted"; } # End if ($Key eq "Answers") if ($Key eq "Total") { $varTotalVotes++; $PollFile[$i] = $Key ."\|" . $varTotalVotes; } # End if ($Key eq "Total") } # End for ($i=0,$i<=($#PollFile);$i++) if ($varNoVote eq "NoVote") { for ($i=0;$i<=$#PollFile;$i++) { ($Key,$Value)=split(/\|/,$PollFile[$i],2); if ($Key eq "Total") { $varTotalVotes--; $PollFile[$i] = $Key ."\|" . $varTotalVotes; } # End if ($Key eq "Total") } # End for ($i=0,$i<=($#PollFile);$i++) print "Content-type: text/html\n\n"; } else { ## Saving Poll Results ## Looking for Database LockFile $lockfile = $input{'Poll'} . "\.LockFile"; $tempfile = $input{'Poll'} . "\.TempFile"; $i=0; while (-e $lockfile) { sleep 3; if ($i > 9) { &ErrorWithLockFile;} $i++ } # End while (-e $lockfile) # Create Lock File `touch $lockfile`; ##Creating New Poll File if (open (NEWPOLLFILE, ">$tempfile")) { foreach $var (@PollFile) { print NEWPOLLFILE "$var\n"; } # End foreach $var (@PollFile) close(NEWPOLLFILE); } else { &ErrorWritingPollFile; } # end if (open (NEWPOLLFILE, ">$tempfile")) rename($tempfile,$input{'Poll'}); # Removing Lock File unlink($lockfile); ## expires=Tuesday, $varGMTExpireDate 00:00:00 GMT; #Setting Voting Cookie print "Content-type: text/html\n"; print "Set-Cookie: $varPollName=Voted; expires=Tuesday, $varGMTExpireDate 23:59:00 GMT;path=/; \n\n"; } # End if ($varNoVote eq "NoVote") ## Getting Poll Results $varPollResults = ""; $varPollResults .= " \n"; $varPollResults .= " \n"; $varPollResults .= " \n"; for ($i=1;$i<=$varNumberofAnswers;$i++) { foreach $var (@PollFile) { ($Key,$Value)=split(/\|/,$var,2); if ($Key eq $i) { ($varDescription,$varImagePath,$varCount) = split(/\|/,$Value,3); if ($varCount == 0) { $varPollResults .= " \n"; } else { $varResult = substr((($varCount / $varTotalVotes)*100),0,4); $varPollResults .= " \n"; } # End ($varCount == 0) } # End if ($Key eq $i) } # End foreach $var (@PollFile) } # End for ($i=1;$i=<$varNumberofAnswers;$i++) $varPollResults .= " \n"; $varPollResults .= " \n"; $varPollResults .= "
$varHeaderResults
$varPollQuestion

  0\%$varDescription

  $varResult\%$varDescription
Total Votes: $varTotalVotes
Poll Expires: $varExpireDate
\n"; ## Open calling page replace comment with $varPollResults open (HTML, $varCallingPage); while () { s/\<\!\-\-\#exec cmd\=\"$varFullProgramNameAndPath Poll\=$input{'Poll'}\" \-\-\>/$varPollResults/; print $_; } # End while () close (HTML); } # End sub ProcessVotePrintPollResults sub PrintPollResults { print " \n"; print " \n"; print " \n"; for ($i=1;$i<=$varNumberofAnswers;$i++) { foreach $var (@PollFile) { ($Key,$Value)=split(/\|/,$var,2); if ($Key eq $i) { ($varDescription,$varImagePath,$varCount) = split(/\|/,$Value,3); if ($varCount == 0) { print " \n"; } else { $varResult = substr((($varCount / $varTotalVotes)*100),0,4); print " \n"; } # End ($varCount == 0) } # End if ($Key eq $i) } # End foreach $var (@PollFile) } # End for ($i=1;$i=<$varNumberofAnswers;$i++) print " \n"; print " \n"; print "
$varHeaderResults
$varPollQuestion

  0\%$varDescription

  $varResult\%$varDescription
Total Votes: $varTotalVotes
Poll Expires: $varExpireDate
\n"; } # End sub PrintPollResults sub BuildPollForm { print "
\n"; print " \n"; print " \n"; print " \n"; for ($i=1;$i<=$varNumberofAnswers;$i++) { foreach $var (@PollFile) { ($Key,$Value)=split(/\|/,$var,2); if ($Key eq $i) { ($varDescription,$varImagePath,$varCount) = split(/\|/,$Value,3); print " \n"; } # End if ($Key eq $i) } # End foreach $var (@PollFile) } # End for ($i=1;$i=<$varNumberofAnswers;$i++) print " \n"; print " \n"; print " \n"; print "
$varHeaderStart
$varPollQuestion
$varDescription
You may only vote once.
Poll Expires: $varExpireDate
\n"; } # End sub BuildPollForm } # end MAIN: exit;