The major sporting event of 2016 was the Olympic Games in Rio. Out of those Games there were two significant highlights, Usain Bolt completing the triple triple - the 100m/200m/4x100m relay from three consecutive Olympics. The other athlete of the Games was Michael Phelps, who came out of retirement to add another six medals to his record tally.
Leicester City, 1000-1 long shots at the start of the season, completed the most improbable sporting fairy-tale in living memory, winning the English Premier League Premiership. In another drought breaking win, the Chicago Cubs defeated the Indians in seven games to win their first World Series in 108 years. Finally, NBA star Kobe Bryant capped off a stellar career, scoring 60 points in his final game.
Here are those five highlights listed in a poll, which one do you think is the greatest highlight?
#!/usr/bin/perl -w
# poll.cgi
###############################################################
# #
# Any use of this program is entirely at the risk of the #
# user. No liability will be accepted by the author. #
# #
# This code must not be distributed or sold, even in modified #
# form, without the written permission of the author. #
# #
###############################################################
use strict;
# Load variables and class libraries, trap all errors
eval {
# Get script location (UNIX & Windows)
($0 =~ m,(.*)/[^/]+,) && unshift (@INC, "$1");
# Get script location (Windows)
($0 =~ m,(.*)\\[^\\]+,) && unshift (@INC, "$1");
# Load global variables and subroutines
require "common.inc";
require "settings.inc";
# Load class libraries
use CGI::Carp qw(fatalsToBrowser);
use CGI qw(:standard);
use lib::Date;
use lib::FileDB;
use strict;
};
if ($@) {
print "Content-type: text/plain\n\n",
"Failed to load global variables and class libraries:\n$@\n",
"Please make sure that all program files were uploaded in ASCII mode,\n",
"permissions have been set correctly and all files are in correct location.\n",
"Refer to program manual for more detail.";
exit;
}
# Execute script
eval { &main; };
if ($@) {
print "Content-type: text/plain\n\n",
"An unexpected error has occurred:\n$@\n";
exit;
}
######################## MAIN PROGRAM BLOCK ########################
sub main
{
# Denial of service attack prevention
$CGI::POST_MAX = 1024000; # max form post size
$CGI::DISABLE_UPLOADS = 1; # no file uploads
my $FORM = new CGI;
my $poll_id;
if ($FORM->param('poll_id')) {
$poll_id = $FORM->param('poll_id');
} else {
croak "Poll ID $ENV{'POLL_ID'} was not supplied.";
}
# Demo mode
my $demo_check = $FORM->param('demo');
# Return URL
my $return_url = $FORM->param('return_url');
# Read polls
my $file_db = new lib::FileDB;
$file_db->open_file(-file_name => "$main::DATA_PATH/POLLS.cgi");
$file_db->close_file();
my %poll_info = $file_db->get_record(-condition => "poll_id=$poll_id");
# Record not found
croak "Could not locate poll (ID=$poll_id)." if !%poll_info;
# Read poll options
$file_db->open_file(-file_name => "$main::DATA_PATH/$poll_id.cgi");
$file_db->close_file();
my %poll_options_info = $file_db->get_records();
my $number_of_options = 0;
$number_of_options = scalar(@{$poll_options_info{'option_num'}}) if %poll_options_info;
# Load poll template
require "$main::TMPL_PATH/$poll_info{'template'}.inc";
# Get current date
my $date = new lib::Date;
my $current_date = $date->current_date();
# Check if it's time to clean IP log
if ($poll_info{'end_date'} > $current_date && $poll_info{'ip_cleaning_time'} && ($current_date - $poll_info{'ip_cleaning_date'}) >= $poll_info{'ip_cleaning_time'}) {
my $tmp = $current_date - $poll_info{'ip_cleaning_date'};
&reset_ip_log($poll_id, $current_date);
}
# HTML output
my $html_out = qq(
";
# Display poll
print "Content-type: text/html\n\n";
print $html_out;
exit;
}
########################### SUB ROUTINES ###########################
# Check visitor's IP address and cookies:
# return 1 if already voted, 0 otherwise
sub see_if_voted
{
my ($poll_id, $current_date, $ip_cleaning_time) = @_;
my $FORM = new CGI;
# Check cookies
my $cookie_name = "pm_" . $poll_id . "_voted";
if ($main::USE_COOKIE_TRACKING && $FORM->cookie($cookie_name) > 0 && ($current_date - $FORM->cookie($cookie_name)) < $ip_cleaning_time) {
return 1;
# Check IP db
} else {
my $file_db = new lib::FileDB;
$file_db->open_file(-file_name => "$main::DATA_PATH/$poll_id.log");
$file_db->close_file();
my %ip_info = $file_db->get_record(-condition => "ip_address=$ENV{'REMOTE_ADDR'}");
if (%ip_info) {
return 1;
} else {
return 0;
}
}
}
# Record vote
sub record_vote
{
my ($poll_id, $current_date) = @_;
my $FORM = new CGI;
my $option_num = $FORM->param('option');
# Set cookie
if ($main::USE_COOKIE_TRACKING) {
my $cookie_name = "pm_" . $poll_id . "_voted";
print "Set-Cookie: $cookie_name=$current_date; expires=Sat, 10-Jan-2099 10:10:10 GMT; path=/\n";
}
my $file_db = new lib::FileDB;
# Update poll file
$file_db->open_file( -file_name => "$main::DATA_PATH/$poll_id.cgi",
-open_to => "update"
);
my %poll_options_info = $file_db->get_record(-condition => "option_num=$option_num");
$file_db->update_records( -condition => "option_num=$option_num",
-fields => ['option_votes'],
-values => [++$poll_options_info{'option_votes'}]
);
$file_db->commit_changes();
$file_db->close_file();
# Record IP address
$file_db->open_file( -file_name => "$main::DATA_PATH/$poll_id.log",
-open_to => "append"
);
$file_db->insert_record(-values => [$ENV{'REMOTE_ADDR'}]);
$file_db->commit_changes();
$file_db->close_file();
}
# Build horizontal poll
sub build_horizontal_poll
{
my ($number_of_options, $total_votes, $show_votes, $show_percent, $show_bars, $bar_width, $bar_length, $row_separator, $column_separator, %poll_options_info) = @_;
# Initialize rows and temporary variables
my (@rows, $rows_str, $percent_votes, $percent_bar);
for (my $m = 0; $m < $number_of_options; $m++) {
# Option text column
$rows[$m] = "
\n";
$rows[$m] .= qq(| $poll_options_info{'option_text'}[$m] | \n);
if ($show_votes) {
$rows[$m] .= qq($column_separator$poll_options_info{'option_votes'}[$m] | \n);
}
# Vote percentage column
if ($show_percent) {
if ($total_votes == 0) {
$percent_votes = 0;
} else {
$percent_votes = int(($poll_options_info{'option_votes'}[$m] / $total_votes) * 100);
if (((($poll_options_info{'option_votes'}[$m] / $total_votes) * 100) - $percent_votes) >= 0.5) {
$percent_votes++; # round off
}
}
$rows[$m] .= qq($column_separator ($percent_votes%) | \n);
}
# Graphical bars column
if ($show_bars) {
if ($total_votes < 1) {
$percent_bar = 1;
} else {
$percent_bar = int(($poll_options_info{'option_votes'}[$m] / $total_votes) * $bar_length);
$percent_bar = 1 if $percent_bar < 1;
}
$rows[$m] .= qq($column_separator |
);
}
# Complete row
$rows[$m] .= "
\n";
}
# Combine all rows
$rows_str = join $row_separator, @rows;
return $rows_str;
}
# Build vertical poll
sub build_vertical_poll
{
my ($number_of_options, $total_votes, $show_votes, $show_percent, $show_bars, $bar_width, $bar_length, $row_separator, $column_separator, %poll_options_info) = @_;
# Initialize rows, columns and temporary variables
my (@rows, @columns, $rows_str, $columns_str, $percent_votes, $percent_bar);
# Graphical bars row
if ($show_bars) {
@columns = ();
for (my $p = 0; $p < $number_of_options; $p++) {
if ($total_votes == 0) {
$percent_bar = 1;
} else {
$percent_bar = int(($poll_options_info{'option_votes'}[$p] / $total_votes) * $bar_length);
$percent_bar = 1 if $percent_bar < 1;
}
$columns[$p] = qq(
| );
}
$columns_str = join $column_separator, @columns;
push(@rows, qq(
$columns_str\n
\n));
}
# Vote percentage row
if ($show_percent) {
@columns = ();
for (my $q = 0; $q < $number_of_options; $q++) {
if ($total_votes == 0) {
$percent_votes = 0;
} else {
$percent_votes = int(($poll_options_info{'option_votes'}[$q] / $total_votes) * 100);
if (((($poll_options_info{'option_votes'}[$q] / $total_votes) * 100) - $percent_votes) >= 0.5) {
$percent_votes++; # round off
}
}
$columns[$q] = qq(
$percent_votes% | );
}
$columns_str = join $column_separator, @columns;
push(@rows, qq(
\n$columns_str
\n));
}
# Votes row
if ($show_votes) {
@columns = ();
for (my $r = 0; $r < $number_of_options; $r++) {
$columns[$r] = qq(
$poll_options_info{'option_votes'}[$r] | );
}
$columns_str = join $column_separator, @columns;
push(@rows, qq(
\n$columns_str
\n));
}
# Option text row
@columns = ();
for (my $t = 0; $t < $number_of_options; $t++) {
$columns[$t] = qq(
$poll_options_info{'option_text'}[$t] | );
}
$columns_str = join $column_separator, @columns;
push(@rows, qq(
\n$columns_str
\n));
# Combine all rows
$rows_str = join $row_separator, @rows;
return $rows_str;
}
# Reset IP Log
sub reset_ip_log
{
my ($poll_id, $current_date) = @_;
my $file_db = new lib::FileDB;
# Reset IP log
$file_db->open_file(-file_name => "$main::DATA_PATH/$poll_id.log",
-open_to => "overwrite"
);
$file_db->insert_record(-values => ["IP_ADDRESS"]);
$file_db->commit_changes();
$file_db->close_file();
# Update POLLS table
$file_db->open_file( -file_name => "$main::DATA_PATH/POLLS.cgi",
-open_to => "update"
);
$file_db->update_records( -condition => "poll_id=$poll_id",
-fields => ['ip_cleaning_date'],
-values => [$current_date]
);
$file_db->commit_changes();
$file_db->close_file();
}