#!/usr/npac/bin/perl
########################################################################
# Script:       | Poll It                                              #
# Version:      | 1.0                                                  #
# By:           | Jason Berry (CGI World)                              #
# Contact:      | jason@cgi-world.com                                  #
# WWWeb:        | http://www.cgi-world.com                             #
# Copyright:    | 1996-98 CGI World                                    #
# Last Updated: | Jan 27, 1998                                         #
#               |                                                      #
########################################################################
# If you're using this please let me know, if you edit this script in  #
# any way, please let me know. You may have a good idea. Any comments  #
# or ideas are accepted!                                               #
########################################################################
# SCRIPT NOTICE                                                        #
#                                                                      #
# Poll It! may be used and modified by anyone. As long                 #
# as this copyright notice, and the comments above are present. By     #
# using Poll It! you agree that the author is not libel for any        #
# possible problems resulting from its use.                            #
#                                                                      #
########################################################################
# Define Variables #

$pollfile = "/npac/server/foxsden00/sroot/droot/simfanatic/simalliance/poll-it/pollfile.dta";
$headerfile = "/npac/server/foxsden00/sroot/droot/simfanatic/simalliance/poll-it/header.dta";
$motdfile = "/npac/server/foxsden00/sroot/droot/simfanatic/simalliance/poll-it//motd.dta";
$lastpollfile = "/npac/server/foxsden00/sroot/droot/simfanatic/simalliance/poll-it/lastpoll.dta";

# If these file paths are set to a directory which is set to mode
# 777 the script will make them automaticaly. If you do not use
# a directory which is set to 777, you must create these files 
# manualy, and chmod them to 777.

$bg_file = "";

# If you do not want to use a background
#  color leave between the quotes blank.

$bgcolor = "black";
$textcolor = "white";
$linkcolor = "white";
# Colors for your PollIt! pages. 
# Background Color, Text Color, Link Color

$results_header = "Current Poll Stats";

$header_on_main_page = "SimAlliance Voting Center";

$lastpoll_header = "SimAlliance Previous Poll Results";

# Type in the text you want for the headers of your results
# page, last poll page, and the main vote page.

$viewlastpoll = "Results from the previous poll!";
$link_to_last_poll = "Last Poll Results";

# Text on main page for the previous poll results
# Link text to go to poll

$border_size = "1";

# Size of HTML Table Border.

$table_size = "85%";

# This is the percentage of how big the poll page table should be
# displayed in the browser.

$yourpassword = "ftphttp";  
# Your password for editing your online Polls from the web!

$your_url = "http://simalliance.home.ml.org";
# Displayed as a link to get back to your site

$submit_text = "submit your vote";
# What the submit buttons says

############################################################
# Don't Edit Under Here, unless you know what you're doing!
# & please tell me if you do edit under here.
############################################################

############################
# Ok now the fun part!
############################
if(! $ENV{'QUERY_STRING'}) { 
   &get_votes;   
   &main;
}

if($ENV{'QUERY_STRING'} eq "add"){
   &parse_form;
   &get_variables;
   &get_number;
   &get_votes;
   &add;   
}

if($ENV{'QUERY_STRING'} eq "results"){
   &parse_form;
   &get_variables;
   &get_votes;
   &results;
}

if($ENV{'QUERY_STRING'} eq "lastpoll"){
   &lastpoll;
}

else {
   &parse_form;
   &get_variables;
   &get_votes;
}
#######################
# Parse Form Subroutine
#######################
 
sub parse_form {

   # Get the input
    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

   # Split the name-value pairs
   @pairs = split(/&/, $buffer);

   foreach $pair (@pairs) {
      ($name, $value) = split(/=/, $pair);

      # Un-Webify plus signs and %-encoding
      $value =~ tr/+/ /;
      $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
      $value =~ s/<!--(.|\n)*-->//g;

      $value =~ s/<([^>]|\n)*>//g;
      $FORM{$name} = $value;
   }

   if($name eq "newvoter") {
   push(@options,$value);
   }

}
########################
# Get Variables
########################
sub get_variables {

 if ($FORM{'ip'}) {
      $ip = $FORM{'ip'};
      }  
   if ($FORM{'poll'}) {
      $poll = $FORM{'poll'};
      }
   if ($FORM{'newvoter'}) {
      $newvoter = $FORM{'newvoter'};
      }	
   if ($FORM{'vote'}) {
      $vote = $FORM{'vote'};
      }  
   if ($FORM{'delete'}) {
      $delete = $FORM{'delete'};
      }
   if ($FORM{'player'}) {
      $player = $FORM{'player'};
      }
   if ($FORM{'setheaders'}) {
      $setheaders = $FORM{'setheaders'};
      }
   if ($FORM{'enddate'}) {
      $enddate = $FORM{'enddate'};
      }
   if ($FORM{'motd'}) {
      $motd = $FORM{'motd'};
      }
   if ($FORM{'lastpoll'}) {
      $lastpoll = $FORM{'lastpoll'};
      }
   if ($FORM{'uname'}) {
      $uname = $FORM{'uname'};
      }
}
############################
# Get Data Number Subroutine
############################
sub get_number {

   open(NUMBER,"$pollfile");
   @mvps = <NUMBER>;
   close(NUMBER);
   $mvpcount = @mvps;
   $count = 0;
   while($count < $mvpcount) {
      ($player,$votes) = split(/::/,$mvps[$count]);
      if($player eq "$poll") {
         $votes++;
         $mvps[$count] = "$player\::$votes\n"; 
	 } 
      $count++;
      }
   open(NUMBER,">$pollfile");
   foreach$mvp(@mvps) {
      print NUMBER "$mvp";
      }
   close(NUMBER);
}
########################
# Admin Add
########################
sub add {
   
   if($uname eq "$yourpassword") {

      if ($newvoter) {
         (@newvoters) = split(/\//,$newvoter);
         open(POLL,">>$pollfile");
         foreach$newv(@newvoters) {
            print POLL "$newv\::0\n";
         }
      close(POLL);
   }
   if ($delete eq "yes") {
   open(NUMBERS,">$pollfile");
   print NUMBERS "";
   close(NUMBERS);
   }  
   
   if ($delete eq "all") { 
   &delete_all;
   }
   
   if ($lastpoll eq "yes") { 
   &make_lastpoll;
   }   
   if($setheaders) {
   (@headers) = split(/\//,$setheaders);
   open(HEADERS,">$headerfile");
   foreach$header(@headers) { 
   print HEADERS "$header\::";
   }
   close(HEADERS);
   }
   
   if($motd) {
   open(MOTD,">$motdfile");
   print MOTD "$motd";
   close(MOTD);
   }   
}
   print "Content-type: text/html\n\n";
   print "<html><body background=\"$bg_file\" bgcolor=\"$bgcolor\" text=\"$textcolor\" LINK=\"$linkcolor\" VLINK=\"$linkcolor\"><center><form method=\"post\" action=\"$ENV{'SCRIPT_NAME'}?add\">\n";
   print "<TABLE Border=\"$border_size\"><TD COLSPAN=2>\n";
   print "<CENTER><h2><TT><A HREF=\"$ENV{'SCRIPT_NAME'}\">Poll Page</A> | <A HREF=\"$ENV{'SCRIPT_NAME'}?lastpoll\"> View Last Poll </A><p></TD><TR>\n";
   print "<TD Colspan=2><Center><b>Your Password:</b> <input type=text size=20 name=\"uname\"></td></tr>\n";
   print "<tr><TD Valign=Top>Add New Voting Options: </td><td><Center><input  size=40 name=\"newvoter\"><br>\n";
   print "<FONT Size=2><B>Options/split/bye/backslashes</b></Font></TD></TR>\n";
   print "<TR><TD VAlign=Top><P>";
   print "Change Vote Headers:</TD><TD><Center> <input type=text name=\"setheaders\" size=40><br>\n";
   print "<FONT Size=2><B>Vote/Description/Current Results/End Date</B></Font></TD></TR>\n";
   print "<TR><TD colspan=2><P>";
   print "Delete All Vote Options: <input type=checkbox name=\"delete\" value=\"yes\">\n";
   print "<b>Erase All:</b> <input type=checkbox name=\"delete\" value=\"all\"> Create Last Poll Page: <input type=checkbox name=\"lastpoll\" value=\"yes\"></TD></TR>";
   print "<TR><TD Colspan=2><center>Message of the Day:<br> <textarea Rows=2 Cols=42 name=\"motd\"></textarea><BR><P><B>Poll It! Version 1.0  by <A HREF=\"http://www.iservices.net/cgiworld\">CGI World</A></TD></TR>";
   print "</TABLE><br><input type=submit value=\"Change Option\">\n";
   print "* <input type=reset value=\"Clear\"></form><hr size=1 width=60%>\n";
   &short_display;
   print "</html>\n";
}
##############
# Main
##############
sub main {
   &display;
}
##############
# Display
##############
sub display { 

   open(NUMBERS,"$pollfile");
   @mvps = <NUMBERS>;
   close(NUMBERS);
   print "Content-type: text/html\n\n";
   print "<html><body background=\"$bg_file\" bgcolor=\"$bgcolor\" text=\"$textcolor\" link=\"$linkcolor\" vlink=\"$linkcolor\"><center><table width=$table_size border=$border_size cellpadding=9>\n";
   print "<Form method=\"post\" action=\"$ENV{'SCRIPT_NAME'}?results\">\n";
   print "<tr><td colspan=4><center><h2><tt>$header_on_main_page</tt></h2></td></tr>\n";
   print "<td width=25% VAlign=TOP rowspan=50><Center><b><u>Message Of the Day</u>:</Center><hr width=80%>\n";
   &motd;
   print "</b><hr Width=80%>$viewlastpoll <BR><center><h3><TT> <A HREF=\"$ENV{'SCRIPT_NAME'}?lastpoll\">$link_to_last_poll</A></h3></tt></center></td>\n";
   open(HEADER,"$headerfile");
   @headers = <HEADER>;
   close(HEADER);
   foreach$header(@headers) {
      ($header1,$header2,$header3,$enddate) = split(/::/,$header);
      print "<TD><B>$header1</TD><TD><B>$header2</TD><TD><b>$header3</TD></tr>\n";
      }
   foreach$mvp(@mvps) {
      ($player,$votes) = split(/::/,$mvp);
      print "<tr><TD><INPUT Type=Radio name=\"poll\" value=\"$player\"><td>$player</td><td><center><b>$votes</b> Out of <b>[$votecount]</b>  Votes</td></tr>\n";
      }
   print "<tr><td colspan=3><input type=submit name=submit value=\"$submit_text\"><br>\n";
   print"<P>Poll Ends: <B><tt>$enddate</b></tt><br><br><B>Poll It! Version 1.0</b> Created by <a href=\"http://www.iservices.net/cgiworld/\">interdreams limited</A></td>\n";
   print "</form></table></center></body></html>\n";
   
}
##############################
# Print The Message of the Day
##############################
sub motd { 

   open(MOTD,"$motdfile");
   @motd = <MOTD>;
   close(MOTD);
   print "<Font Size=+1><Tt>@motd </TT></Font>";
}
####################
# Admin View Display
####################
sub short_display { 
   &get_number;
   &get_votes;
   open(NUMBERS,"$pollfile");
   @mvps = <NUMBERS>;
   close(NUMBERS);
   print "<table Cellpadding=9 border=$border_size><Form method=\"post\" action=\"$ENV{'SCRIPT_NAME'}\">\n";
   open(HEADER,"$headerfile");
   @headers = <HEADER>;
   close(HEADER);
   foreach$header(@headers) {
      ($header1,$header2,$header3) = split(/::/,$header);
      print "<tr><TD><B>$header2</TD><TD><b>$header3</TD></tr>\n";
      }
    foreach$mvp(@mvps) {
       ($player,$votes) = split(/::/,$mvp);
       print "<tr><td><b><h3><TT>$player</td><td><center><h3><TT>$votes Out Of [ $votecount ]</td></tr>\n";
       }
    print "</form></table></body></html>\n";
}
###########################
# Get Total Number of Votes
###########################
sub get_votes {
    
  $votecount = 0;
   open(NUMBERS,"$pollfile");
   @mvps = <NUMBERS>;
   close(NUMBERS);
   foreach$mvp(@mvps) {
      ($player,$votes) = split(/::/,$mvp);
      $votecount = $votecount + $votes  ;
   }
}
#################
# Results Page
#################
sub results {
  
   print "Content-type: text/html\n\n";
   print "<html><body background=\"$bg_file\" bgcolor=\"$bgcolor\" text=\"$textcolor\" link=\"$linkcolor\" vlink=\"$linkcolor\"><center>\n";
   print "<h2><tt>$results_header</h2></tt>\n";
   print "<P><h3><tt> Your vote was recorded for: $poll</tt></h3>\n";
   &short_display;
   print "<P>";
   print "<center><h3><tt><A HREF=\"$ENV{'SCRIPT_NAME'}?lastpoll\"> View Last Poll</A></h3></TT>\n";
   print "</center></body></html>";
}
####################
# Deletes everything
####################
sub delete_all { 

   open(POLL,">$pollfile");
   print POLL "";
   close(POLL);
   open(MOTD,">$motdfile");
   print MOTD "";
   close(MOTD);
   open(HEADER,">$headerfile");
   print HEADER "";
   close(HEADER);
   
}
#######################
# Create Last Poll File
sub make_lastpoll { 
   open(POLL,"$pollfile");
   @poll = <POLL>;
   close(POLL);
   open(LASTPOLL,">$lastpollfile");
   print LASTPOLL @poll;
   close(LASTPOLL);
}
#################
# Last Poll Page
sub lastpoll { 

   $votecount = 0;
   open(NUMBERS,"$lastpollfile");
   @mvps = <NUMBERS>;
   close(NUMBERS);
        
      foreach$mvp(@mvps) {
         ($player,$votes) = split(/::/,$mvp);
         $votecount = $votecount + $votes  ;
      }
   
      print "Content-type: text/html\n\n";
      print "<html><body background=\"$bg_file\" bgcolor=\"$bgcolor\" text=\"$textcolor\" LINK=$linkcolor VLINK=$linkcolor><CENTER>\n";
      open(NUMBERS,"$lastpollfile");
      @options = <NUMBERS>;
      close(NUMBERS);
      print "<table border=$border_size Width=65%><td colspan=2><center><h2><tt>$lastpoll_header</h2></tt>\n";
      print "<Font Size=4><TT><A HREF=\"$your_url\"> $your_url </A></TT></font></TT></h2></td></tr>\n";
      foreach$option(@options) {
         ($option,$votes) = split(/::/,$option);
         print "<tr><td><b>$option </td><td> $votes out of $votecount Total Votes Submited  </td></tr>\n";
      }
   print "</table></body></html>";
} 

###############################
# Isn't this a great script :)
###############################

