#!/usr/npac/bin/perl 
$data_dir = "/npac/server/foxsden00/sroot/droot/psf/f/cgi/data"; 
$script_name = "http://boss.npac.syr.edu/cgi-bin/pamfox/vote.pl"; 
$valid_referer = ""; 
$url_file = "/npac/server/foxsden00/sroot/droot/psf/f/cgi/url_file.txt"; 
$img_url = "/npac/server/foxsden00/sroot/droot/psf/f/cgi/img"; 
$img1 = "blue.jpg"; 
$img2 = "magenta.jpg"; 
$img3 = "orange.jpg"; 
$img4 = "red.jpg"; 
$img5 = "purple.jpg"; 
$img6 = "yellow.jpg"; 
$img7 = "pink.jpg"; 
$img8 = "green.jpg"; 
$img9 = "cyan.jpg"; 
$img10 = "gray.jpg"; 
$mult = 10; 
$reprimand = <<EOM; 
<html><head><title>Shame On You!</title></head> 
<body bgcolor=ffffff><center><h1>Shame On You!</h1></center> 
It appears as though you just tried to vote more than once on this 
particular subject. You ought to be ashamed of yourself. Your first vote 
will count, but not any of your subsequent votes.<p> 
Go back with your browser to return to the page from where you came. 
</body></html> 
EOM 
1; 
$min_vote = 1; 
$average_header = <<EOM; 
<html><head><title>Average Ratings</title></head> 
<body bgcolor=ffffff><center><h1>Average Ratings</h1></center> 
Here is a table of the average ratings you have given for each of my 
scripts. This table is compiled by my <a href="../vote.shtml">vote</a> 
script.<p><ul> 
EOM 
1; 
$average_footer = <<EOM; 
</ul> 
Return to the main page about my <a href="../vote.shtml">vote</a> script.<p> 
Return to my main <a href="../index.html">scripts</a> page. 
</body></html> 
EOM 
1; 
##################### 
if ($ENV{'QUERY_STRING'} eq "vote") { 
&referer; 
&parse_form; 
&get_variables; 
&create; 
&value; 
&modify_data; 
&return_page; 
exit; 
} 
elsif ($ENV{'QUERY_STRING'} eq "average") { 
&average; 
} 
else { 
$data_file = $ARGV[0]; 
# print "Content-type: text/html\n\n"; 
&create; 
&value; 
&graph; 
exit; 
} 

########### 
# Check Referer 
sub referer { 
if($valid_referer) { 
if ($ENV{'HTTP_REFERER'} =~ /http:\/\/([^\/]+)/) { 
if (!($1 =~ /$valid_referer/)) { 
print "Content-type: text/html\n\n"; 
print "<html><head><Title>Invalid Referer</title></head>\n"; 
print "<body bgcolor=ffffff>\n"; 
print "<h1>Your vote was cast from a page on an invalid domain and will not be counted.</h1>\n"; 
exit; 
} 
} 
} 
} 
########### 
# Create data file 
sub create { 

if (-e "$data_dir/$data_file") { 
} 
else { 
open(DATA,">$data_dir/$data_file"); 
print DATA "$mult:0:0:0:0:0:0:0:0:0:0\n"; 
close(DATA); 
} 
} 
########### 
# Get values from data file 
sub value { 

open(DATA,"$data_dir/$data_file"); 
$data_line = <DATA>; 
(@data) = split(/:/,$data_line); 
$total = $data[1] + $data[2] + $data[3] + $data[4] + $data[5] + $data[6] + $data[7] + $data[8] + $data[9] +
$data[10]; 
if ($total eq "0") { 
$avg = "--"; 
} 
else { 
$uavg = (($data[1] * 1) + ($data[2] * 2) + ($data[3] * 3) + ($data[4] * 4) + ($data[5] * 5) + ($data[6] * 6) +
($data[7] * 7) + ($data[8] * 8) + ($data[9] * 9) + ($data[10] * 10)) / $total; 
$avg = (int(100 * $uavg) / 100); 
} 
$multiplier = $data[0]; 
$number1 = $data[1] * $multiplier; 
$number2 = $data[2] * $multiplier; 
$number3 = $data[3] * $multiplier; 
$number4 = $data[4] * $multiplier; 
$number5 = $data[5] * $multiplier; 
$number6 = $data[6] * $multiplier; 
$number7 = $data[7] * $multiplier; 
$number8 = $data[8] * $multiplier; 
$number9 = $data[9] * $multiplier; 
$number10 = $data[10] * $multiplier; 
} 

############## 
#Print graph to page 
sub graph { 
print "<center>"; 
print "<table><tr align=center valign=bottom>\n"; 
print "<td><img src=\"$img_url/$img1\" width=10 height=$number1 alt=\"1:\">\n"; 
print "<br><b>$data[1]</b><br></td>\n"; 
print "<td><img src=\"$img_url/$img2\" width=10 height=$number2 alt=\"2:\">\n"; 
print "<br><b>$data[2]</b><br></td>\n"; 
print "<td><img src=\"$img_url/$img3\" width=10 height=$number3 alt=\"3:\">\n"; 
print "<br><b>$data[3]</b><br></td>\n"; 
print "<td><img src=\"$img_url/$img4\" width=10 height=$number4 alt=\"4:\">\n"; 
print "<br><b>$data[4]</b><br></td>\n"; 
print "<td><img src=\"$img_url/$img5\" width=10 height=$number5 alt=\"5:\">\n"; 
print "<br><b>$data[5]</b><br></td>\n"; 
print "<td><img src=\"$img_url/$img6\" width=10 height=$number6 alt=\"6:\">\n"; 
print "<br><b>$data[6]</b><br></td>\n"; 
print "<td><img src=\"$img_url/$img7\" width=10 height=$number7 alt=\"7:\">\n"; 
print "<br><b>$data[7]</b><br></td>\n"; 
print "<td><img src=\"$img_url/$img8\" width=10 height=$number8 alt=\"8:\">\n"; 
print "<br><b>$data[8]</b><br></td>\n"; 
print "<td><img src=\"$img_url/$img9\" width=10 height=$number9 alt=\"9:\">\n"; 
print "<br><b>$data[9]</b><br></td>\n"; 
print "<td><img src=\"$img_url/$img10\" width=10 height=$number10 alt=\"10:\">\n"; 
print "<br><b>$data[10]</b><br></td>\n"; 
print "<td><br>Average:<br><b>$avg</b><br></td>\n"; 
print "</tr><tr align=center valign=top>\n"; 
print "<td><form method=post action=\"$script_name?vote\">\n"; 
print "1<br><input type=radio name=\"rating\" value=\"1\"><br></td>\n"; 
print "<td>2<br><input type=radio name=\"rating\" value=\"2\"><br></td>\n"; 
print "<td>3<br><input type=radio name=\"rating\" value=\"3\"><br></td>\n"; 
print "<td>4<br><input type=radio name=\"rating\" value=\"4\"><br></td>\n"; 
print "<td>5<br><input type=radio name=\"rating\" value=\"5\"><br></td>\n"; 
print "<td>6<br><input type=radio name=\"rating\" value=\"6\"><br></td>\n"; 
print "<td>7<br><input type=radio name=\"rating\" value=\"7\"><br></td>\n"; 
print "<td>8<br><input type=radio name=\"rating\" value=\"8\"><br></td>\n"; 
print "<td>9<br><input type=radio name=\"rating\" value=\"9\"><br></td>\n"; 
print "<td>10<br><input type=radio name=\"rating\" value=\"10\"><br></td>\n"; 
print "<td><input type=submit value=\"Vote!\"></td></tr></table>\n"; 
print "<input type=hidden name=\"data_file\" value=\"$data_file\">\n"; 
print "</form></center>\n"; 
} 
################# 
# Parse Form 
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; 
$FORM{$name} = $value; 
} 

} 
############ 
# Get variables from form 
sub get_variables { 
if ($FORM{'rating'}) { 
$rating = "$FORM{'rating'}"; 
} 
if ($FORM{'data_file'}) { 
$data_file = "$FORM{'data_file'}"; 
} 
} 

############# 
# Modify data file 
sub modify_data { 

foreach(@data) { 
$_ =~ s/\n//g; 
} 
if ($ENV{'REMOTE_ADDR'} eq $data[11]) { 
print "Content-type: text/html\n\n"; 
print "$reprimand"; 
exit; 
} 
else { 
if ($rating != "1") { 
$value1 = $data[1]; 
} 
else{ 
$value1 = $data[1] +1; 
} 
if ($rating != "2") { 
$value2 = $data[2]; 
} 
else{ 
$value2 = $data[2] +1; 
} 
if ($rating != "3") { 
$value3 = $data[3]; 
} 
else{ 
$value3 = $data[3] +1; 
} 
if ($rating != "4") { 
$value4 = $data[4]; 
} 
else{ 
$value4 = $data[4] +1; 
} 
if ($rating != "5") { 
$value5 = $data[5]; 
} 
else{ 
$value5 = $data[5] +1; 
} 
if ($rating != "6") { 
$value6 = $data[6]; 
} 
else{ 
$value6 = $data[6] +1; 
} 
if ($rating != "7") { 
$value7 = $data[7]; 
} 
else{ 
$value7 = $data[7] +1; 
} 
if ($rating != "8") { 
$value8 = $data[8]; 
} 
else{ 
$value8 = $data[8] +1; 
} 
if ($rating != "9") { 
$value9 = $data[9]; 
} 
else{ 
$value9 = $data[9] +1; 
} 
if ($rating != "10") { 
$value10 = $data[10]; 
} 
else{ 
$value10 = $data[10] + 1; 
} 
open(DATA,">$data_dir/$data_file") || die $!; 
print DATA
"$multiplier:$value1:$value2:$value3:$value4:$value5:$value6:$value7:$value8:$value9:$value10:$ENV{'REMOTE_ADDR'}\n";

close (DATA); 
} 
} 
############# 
# Return page 
sub return_page { 
print "Location: $ENV{'HTTP_REFERER'}\n\n"; 
} 

############ 
# Average all files and print out table 

sub average { 

opendir (DATA, "$data_dir"); 
@datalist = grep(!/^\.\.?$/, readdir (DATA)); 
closedir (DATA); 
foreach$data_file(@datalist) { 
open(FILE,"$data_dir/$data_file") || die $!; 
$filetemp = <FILE>; 
close(FILE); 
chop $filetemp; 
@data = split(/:/,$filetemp); 
$total = $data[1] + $data[2] + $data[3] + $data[4] + $data[5] + $data[6] + $data[7] + $data[8] + $data[9] +
$data[10]; 
if ($total eq "0") { 
$avg = "0"; 
} 
else { 
$uavg = (($data[1] * 1) + ($data[2] * 2) + ($data[3] * 3) + ($data[4] * 4) + ($data[5] * 5) + ($data[6] * 6) +
($data[7] * 7) + ($data[8] * 8) + ($data[9] * 9) + ($data[10] * 10)) / $total; 
$avg = (int(100 * $uavg) / 100); 
} 
if ($total >= $min_vote) { 
$rankings{$data_file} = $avg; 
} 
$listed = 0; 
open(URLS,"$url_file"); 
while(<URLS>) { 
($datatest,$url) = split(/::/,$_); 
if($datatest eq $data_file) { 
$listed = 1; 
$permurl = $url; 
} 
} 
close(URLS); 
if($listed == 1) { 
$urls{$data_file} = $permurl; 
} 
else { 
$urls{$data_file} = $data_file; 
} 
} 
sub bynumber { $rankings{$b} <=> $rankings{$a}; } 

foreach (sort bynumber keys %rankings) { 
push(@titles,"$_"); 
push(@averages,"$rankings{$_}"); 
} 
foreach (sort bynumber keys %urls) { 
push(@urllist,"$urls{$_}"); 
} 

$num = 0; 
@links = @titles; 
print "Content-type: text/html\n\n"; 
print "$average_header\n"; 
print "<table><tr><th>Rank</th><th>Subject</th><th>Average</th></tr>\n"; 
foreach $title (@titles) { 
$number = $num + 1; 
# $title =~ s/.shtml//g; 
# $title =~ s/.html//g; 
# $title =~ s/_/ /g; 
# $title = ucfirst $title; 
if($urllist[$num] eq $title) { 
print "<tr><td align=right>$number.</td><td>$title</td><td>$averages[$num]</td></tr>\n"; 
} 
else { 
print "<tr><td align=right>$number.</td><td><a
href=\"$urllist[$num]\">$title</a></td><td>$averages[$num]</td></tr>\n"; 
} 
$num = $num + 1; 
} 
print "</table>\n"; 
print "$average_footer\n"; 
exit; 
} 
