#!/usr/local/bin/perl ################################################################## # Copyright 1997 Jim Sauer # # This script was written by Jim Sauer (jim@theaction.com). # # # # # # # # This software is covered by the GNU General Public License # # (http://www.theaction.com/scripts/gnu.html) # # # # You may not remove or modify this header except to update the # # links. # # # # You have permission to do anything you want with this script. # # except restrict its further use. # # # # I would appreciate it if you e-mailed me to let me know how # # you used/modified the script. # # I would also appreciate a link to http://theaction/scripts. # # This script comes with no promise or warrantee including # # fitness for a particular purpose. Use it at your own risk. # # I cannot be responsible for any unfortunate # # consequences resulting from the use of this script. # # Read the GNU General Publiic License. # ################################################################## # # # If you modify this script add documentation here. # # # ################################################################## # # Set these variables: $log_file = "polerslt.html" ; # path and name of logfile $unix = 1; # 1 for UNIX; 0 for NT ################################################################# ############## Skip down to the Choices array ################# ################################################################# if (! $unix) { print "HTTP/1.0 200 OK\n"; } print "Content-Type: text/html \n\n"; # This section is required to read in the variables and get the date. &ReadForms; # Redefine survey variables. $the_choice = $in{the_choice} ; $who = $in{who} ; $name = $in{name} ; $email = $in{email} ; $addchoice = $in{addchoice}; $other_size = 1; require 'ctime.pl'; $Date = &ctime(time); chomp($Date); ####################### CHOICES ARRAY ############################## # All choices in the survey should be elements in the @choices array. # Include them here in the same order as they are arranged on the html page. # If there is an 'other' ie. a 'write in' choice it must be the last element. # Don't forget the quotes. @choices=("pizza", "veggies", "junk food", "spam", "steak", "ice cream", "other"); ###################### RADIO BUTTONS ############################## # This takes care of including the radiobutton selection in the log entry. # Modify these statements to correspond to the selections you have made # For the html page radio button section. You may add more 'elsif' sections # to make more choices. # First choice if ($who eq "1") { $whois = "is an expert Web developer" ; } # Second to next to last choice elsif ($who eq "2") { $whois = "is a first class web surfer" ; } # Default else { $whois = "lives on earth" ; } ################## WARNING!!!! ###################### # Do not edit past this point unless you know what you are doing! $ch_size=@choices; #other position = op $op = @choices -1; # Initialize @count for ($i=0;$i<=$op;$i++){ $count[1] = 0; } if ($the_choice eq "other") { $add=on; $the_choice = $addchoice; } # Read Pole file open (POLE,"$log_file") || die "Can't Open $log_file: $!\n"; @LINES=; close(POLE); $SIZE=@LINES; # Open Link File to Output open (POLE,">$log_file") || die "Can't Open $log_file: $!\n"; # Write new data and rewrite file. for ($i=0;$i<=$SIZE;$i++) { $_=$LINES[$i]; if (//) { print POLE "\n"; ############### WARNING !!!! ################################### ### If you are going to edit the following; note that changing the last word 'likes' ### requires placing the "new last word' into the script below, wherever 'likes' appears. ### Also note that altering the spacing in that part of the script may cause problems. ### In addition please be aware that the same editing must also occur below again. print POLE "

On $Date we learned that $name " ; print POLE " who $whois really likes $the_choice\.\n"; } else { print POLE $_; } } close (POLE); # Write temporary copy of file for immediate viewing. # Add new entry for ($i=0;$i<=$SIZE;$i++) { $_=$LINES[$i]; if (//) { print "\n"; ################################################################# ### If you edited the text above you should do the same here. ### print ""; print "

On $Date we learned that $name " ; print " who $whois really likes $the_choice\."; } # Read File and Count choices else { # Count choices for ($j=0;$j<=($ch_size -1);$j++) { if (/likes(.*)\./){ #### likes ##### if (/$choices[$j]/) { $count[$j] = $count[$j]+1; if ($j > $op ){ $count[$op] = $count[6]+1; } $other = off; } } } # Count 'other' choices #### likes ##### if (/likes(.*)\./ && $other ne off and $+ ne " "){ $count[$op] = $count[$op]+1; $choices[$ch_size] = $+; $count[$ch_size] = $count[$ch_size]+1; $ch_size = $ch_size +1; } $other = on; print $_; } } ########################################################## # Take care of adding in new choices (not in POLE file). $new = on; for ($j=0;$j<=($ch_size -1);$j++) { if ($choices[$j] =~ /$the_choice/) { $new = off; } } if ($new eq on) { $choices[$ch_size] = $the_choice; $count[$ch_size] = 0; $ch_size = $ch_size +1; } ###################################################### print ""; # Calculate size of a unit in the bar graph. for ($n=0;$n<=($ch_size+1);$n++) { if ($count[$n] > $max) { $max=$count[$n]; } if ($max==0){ $max = 1; } } ########################################### # Increment 'other' for a write-in vote. if ($the_choice eq $addchoice){ $count[$op]=$count[$op]+1; } #Print out the table and graph of the survey results. print ""; for ($k=0;$k<=($ch_size -1);$k++) { # Increment count for new choice if ($choices[$k] =~ /$the_choice/) { $count[$k]=$count[$k]+1; } print ""; } print "
"; #print table if ($k == $op) { if ($count[$k] == 0) {$count[$k] ="no";} if ($count[$k] == 1) { print " there was $count[$k] write in vote"; } else { print " there were $count[$k] write in votes"; } } else { if ($count[$k] > 1) { print "$choices[$k] has $count[$k] votes "; } elsif ($count[$k] == 1) { print "$choices[$k] has $count[$k] vote "; } else { print "$choices[$k] has no votes "; } } print ""; $total=$total + $count[$k]; $votescast=$count[$k]; &VoteGraph($votescast); print "
"; # Count and print total votes. $total=$total - $count[$op]; print "Total votes cast were $total
"; # Temporary area for variable calculation. print "

most recent entry

"; exit; ###################################################################### #####################Subroutines###################################### # Draw the graph sub VoteGraph { for ($i=1;$i<=$votescast;$i++) { &BarGraph } } ######################################## sub BarGraph { if ($max > 3) { $wid=int(300/$max); } if ($max == 1) { $wid=10;} if ($max == 2) { $wid=20;} if ($max == 3) { $wid=30;} print "\".\""; } ###################################### sub ReadForms { # Get the input read(STDIN, $STDIN_SIZE, $ENV{'CONTENT_LENGTH'}); # split form data @entry = split(/&/, $STDIN_SIZE); # translate + and % encoding foreach $entry (@entry) { ($name, $value) = split(/=/, $entry); $name =~ tr/+/ /; $value =~ tr/+/ /; $name =~ s/%(..)/pack("c",hex($1))/ge; $value =~ s/%(..)/pack("c",hex($1))/ge; $in{$name} = $value; } return @in; }