#!/usr/bin/perl
use warnings;
use strict;
use CGI;
use POSIX qw(ceil);
print 'Content-Type: text/html',"\n\n"; # print the content-type of our doc!
# Create neccesary directory.
mkdir("polldatabase") if (not -e 'polldatabase');
;
my $cgi = new CGI; # Create our CGI object!
# where to store the votes.
my $database = 'polldatabase/operatingsystems.txt';
# where to store a list of IP's that voted, this should be deleted when starting a new vote.
my $lastip = 'polldatabase/lastip.txt';
# Read value passed to script.
my ($poll) = $cgi->param('poll');
=for vuln
open(LASTIP, '<', $lastip);
my @ips = <LASTIP>;
close(LASTIP);
foreach my $ip (@ips)
{
chomp($ip);
if($ENV{'REMOTE_ADDR'} eq $ip && $poll ne 'SHOWRESULT')
{
print '<html>
<head>
<title> :. Denlab </title>
<meta HTTP-EQUIV="REFRESH" content="3; url=placevote.cgi?poll=SHOWRESULT">
</head>
<body>
<h1>
Sorry, you cant vote twice!
</h1>
You will be redirected to the poll results in 3 seconds.
</body>
</html>';
exit(0);
}
}
=cut
# The format of the database, create the database file.
if(not -e $database)
{
open(DB,'>',$database);
print DB 'WINDOWS:0
LINUX:0
BSD:0
MACOSX:0
OTHER:0';
close(DB);
}
# Store the vote
open(ADDVOTE,'<',$database);
my @votes = <ADDVOTE>;
close(ADDVOTE);
# This piece of code reads the votes, and then adds to the neccesary entry.
for(my $i = 0; $i <= $#votes;$i++)
{
my ($entry,$value) = split(/:/,$votes[$i],2);
$value++; # add to the vote by 1.
$votes[$i] =~ s/$poll:\d*/$poll:$value/; # regex to add the new vote to the database.
}
# print the new votes to the database.
open(ADDTODB, '>',$database);
print ADDTODB @votes;
close(ADDTODB);
# $total holds the total number of votes, @entries holds the entries, and @poll holds the votes.
my ($total);
my (@entries,@poll);
foreach my $castvote (@votes)
{
my ($entry,$number) = split(/:/,$castvote,2); # split the db entry into category/vote.
$total += $number; # add the vote to the total.
push(@entries,$entry); # add $entry to @entries.
push(@poll,$number); # add $number to @poll .
}
# The next section of code just prints the HTML.
print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<!--[if !IE]>-->
<link href="../includes/mainstyle.css" rel="stylesheet">
<!--<![endif]-->
<!--[if IE] -->
<!--[if IE]>
<link href="../includes/IEstyle.css" rel="stylesheet">
<![endif]-->
<title>:. denlab - online denlab servers</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
</head>
<body>
<div id="top"><center><img src="../imgs/tux.png"></center></div>
<div id="line"> </div>
<div id="contain_menu">
<div class="menuitem"> <a href="../index.php">Home</a></div>
<div class="menuitem"> <a href="../code.php">Code</a></div>
<div class="menuitem"> <a href="../projects.php">Projects</a></div>
<div class="menuitem"> <a href="../papers.php">Papers</a></div>
<div class="menuitem"> <a href="../submitpaper.php.cgi">user papers</a></div>
<div class="menuitem"> <a href="../about.php">About</a></div>
</div>';
print '<br>
<div class="content">Poll results
</div>
<div class="text">
<div id="resultcontainer">
Poll Results - votes ',$total,'
<table id=voteresult cellpadding=0 cellspacing=2>';
# loop through $entries and print the results to the html document.
my $percent;
for(my $i = 0;$i <= $#entries;$i++)
{
print "<tr>
<td>$entries[$i]</td>",'<td width=100px><img src="../percent.gif" id="percent_img" width=',$percent,'% height="10px"></td><td> ',$percent = ceil($poll[$i] / $total * 100),"%</td>
</tr>";
}
print '</table>
';
print '
</tr>
</table>
</div>
</div>';
print '</body> </html>';
open(IPLAST, '>>', $lastip) or die("$!");
print IPLAST $ENV{'REMOTE_ADDR'},"\n";
close(IPLAST);