| 1 | #!/usr/bin/perl |
|---|
| 2 | |
|---|
| 3 | #################################################################### |
|---|
| 4 | # |
|---|
| 5 | # Dan Cardamore <dcardamo@novas.cx> |
|---|
| 6 | # |
|---|
| 7 | # Pollerizer version 1.0 |
|---|
| 8 | # |
|---|
| 9 | # This program gets a users input as part of a poll, and then prints |
|---|
| 10 | # a webpage with the results. |
|---|
| 11 | # |
|---|
| 12 | # The questions_file should have the following format: |
|---|
| 13 | # <number of questions> |
|---|
| 14 | # <Question in human terms> |
|---|
| 15 | # <Each option on its own line> |
|---|
| 16 | # |
|---|
| 17 | #################################################################### |
|---|
| 18 | |
|---|
| 19 | ######## BEGIN USER CONFIGURATION ################################# |
|---|
| 20 | $url = "http://www.novas.cx/"; |
|---|
| 21 | $server_root = "/home/httpd/html"; |
|---|
| 22 | $path_from_root = "/index/poll"; |
|---|
| 23 | $me = $url . $path_from_root . "/poll.pl"; |
|---|
| 24 | $questions_file = $server_root . $path_from_root . "/questions.dat"; |
|---|
| 25 | $ip_file = $server_root . $path_from_root . "/remote_ips.dat"; |
|---|
| 26 | $results_file = $server_root . $path_from_root . "/results.dat"; |
|---|
| 27 | $poll_image = $path_from_root . "/poll.gif"; |
|---|
| 28 | ######## DONE USER CONFIGURATION ################################## |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | &Parse_Form; |
|---|
| 32 | |
|---|
| 33 | $remote_ip = $ENV{'REMOTE_ADDR'}; |
|---|
| 34 | $choice = $formdata{'choice'}; |
|---|
| 35 | |
|---|
| 36 | if ($remote_ip eq "") |
|---|
| 37 | { |
|---|
| 38 | die "There was an error retrieving your IP\n"; |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | if ($choice ne "") |
|---|
| 42 | { |
|---|
| 43 | # Add the IP to the ip_file |
|---|
| 44 | open (IP_FILE, ">>$ip_file") || die "There was an error opening the ip log file.\n"; |
|---|
| 45 | flock (IP_FILE, 2); # Lock the file |
|---|
| 46 | print IP_FILE "$remote_ip\n"; |
|---|
| 47 | flock (IP_FILE, 8); # Unlock the file |
|---|
| 48 | close (IP_FILE); |
|---|
| 49 | |
|---|
| 50 | # Add the choice to the other results |
|---|
| 51 | open (RESULTS, ">>$results_file") || die "Could not open the results file.\n"; |
|---|
| 52 | print RESULTS "$choice~$remote_ip\n"; |
|---|
| 53 | close (RESULTS); |
|---|
| 54 | print "Location: $url\n\n"; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | # Test to see if the visitor has already voted. |
|---|
| 58 | open (IP_FILE, "<$ip_file") || die "There was an error opening the ip log file.\n"; |
|---|
| 59 | @addresses = <IP_FILE>; |
|---|
| 60 | close (IP_FILE); |
|---|
| 61 | |
|---|
| 62 | $type_of_page = "get"; |
|---|
| 63 | foreach $index (@addresses) |
|---|
| 64 | { |
|---|
| 65 | chop $index; |
|---|
| 66 | if ($remote_ip eq $index) |
|---|
| 67 | { |
|---|
| 68 | $type_of_page = "print"; |
|---|
| 69 | } |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | if ($type_of_page eq "get") |
|---|
| 74 | { |
|---|
| 75 | open (QUESTIONS, "<$questions_file") || die "Could not open the questions file\n"; |
|---|
| 76 | @q_file = <QUESTIONS>; |
|---|
| 77 | close (QUESTIONS); |
|---|
| 78 | |
|---|
| 79 | print "<FORM ACTION=\"$me\" METHOD=POST>\n"; |
|---|
| 80 | print "<font size=-1>\n"; |
|---|
| 81 | print "<b>$q_file[1]</b><br>\n"; # print the question |
|---|
| 82 | |
|---|
| 83 | $number_of_questions = $q_file[0]; |
|---|
| 84 | |
|---|
| 85 | for ($i = 0; $i < $number_of_questions; $i++) |
|---|
| 86 | { |
|---|
| 87 | print "<INPUT TYPE=\"radio\" NAME=\"choice\" VALUE=\"$i\">$q_file[$i + 2]<br>\n"; |
|---|
| 88 | } |
|---|
| 89 | print "<INPUT TYPE=\"submit\" VALUE=\"Vote\">\n"; |
|---|
| 90 | print "</font>\n"; |
|---|
| 91 | print "</FORM>\n"; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | if ($type_of_page eq "print") |
|---|
| 95 | { |
|---|
| 96 | open (QUESTIONS, "<$questions_file") || die "Could not get the questions file\n"; |
|---|
| 97 | @q_file = <QUESTIONS>; |
|---|
| 98 | close (QUESTIONS); |
|---|
| 99 | |
|---|
| 100 | $number = $q_file[0]; |
|---|
| 101 | |
|---|
| 102 | # clear the array |
|---|
| 103 | for ($i = 0; $i < $number; $i++) |
|---|
| 104 | { |
|---|
| 105 | $tally[$i] = 0; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | open (RESULTS, "<$results_file") || die "Could not get the results\n"; |
|---|
| 109 | @data = <RESULTS>; |
|---|
| 110 | close (RESULTS); |
|---|
| 111 | |
|---|
| 112 | $total_votes = 0; |
|---|
| 113 | foreach $index (@data) |
|---|
| 114 | { |
|---|
| 115 | @line = split(/~/, $index); |
|---|
| 116 | $tally[$line[0]]++; |
|---|
| 117 | $total_votes++; |
|---|
| 118 | } |
|---|
| 119 | print "<font size=-1>\n"; |
|---|
| 120 | print "<b>$q_file[1]</b><br>\n"; |
|---|
| 121 | for ($i = 0; $i < $number; $i++) |
|---|
| 122 | { |
|---|
| 123 | $percent = $tally[$i] / $total_votes * 100; |
|---|
| 124 | $width = $percent * 0.3; |
|---|
| 125 | $percent = sprintf("%.1f", $percent); |
|---|
| 126 | print "$q_file[$i + 2] <IMG SRC=\"$poll_image\" height=5 width=$percent>$percent\%<br>\n"; |
|---|
| 127 | } |
|---|
| 128 | print "<B>Total votes: $total_votes</B><br>\n"; |
|---|
| 129 | print "</font>\n"; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | sub Parse_Form { |
|---|
| 133 | if ($ENV{'REQUEST_METHOD'} eq 'GET') { |
|---|
| 134 | @pairs = split(/&/, $ENV{'QUERY_STRING'}); |
|---|
| 135 | } elsif ($ENV{'REQUEST_METHOD'} eq 'POST') { |
|---|
| 136 | read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); |
|---|
| 137 | @pairs = split(/&/, $buffer); |
|---|
| 138 | |
|---|
| 139 | if ($ENV{'QUERY_STRING'}) { |
|---|
| 140 | @getpairs =split(/&/, $ENV{'QUERY_STRING'}); |
|---|
| 141 | push(@pairs,@getpairs); |
|---|
| 142 | } |
|---|
| 143 | } else { |
|---|
| 144 | print "Content-type: text/html\n\n"; |
|---|
| 145 | print "<P>Use Post or Get"; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | foreach $pair (@pairs) { |
|---|
| 149 | ($key, $value) = split (/=/, $pair); |
|---|
| 150 | $key =~ tr/+/ /; |
|---|
| 151 | $key =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; |
|---|
| 152 | $value =~ tr/+/ /; |
|---|
| 153 | $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; |
|---|
| 154 | |
|---|
| 155 | $value =~s/<!--(.|\n)*-->//g; |
|---|
| 156 | |
|---|
| 157 | if ($formdata{$key}) { |
|---|
| 158 | $formdata{$key} .= ", $value"; |
|---|
| 159 | } else { |
|---|
| 160 | $formdata{$key} = $value; |
|---|
| 161 | } |
|---|
| 162 | } |
|---|
| 163 | } |
|---|