|
#!/usr/local/bin/perl
#
#######################
# show_banners.cgi #
#####################################################################
#
# This is a part of the banner study application, this script looks
# through the text file with all banner views, and decides which one
# to display based on number of views.
#
#####################################################################
use CGI;
use CGI::Carp qw(fatalsToBrowser);
#####################
# CONFIGURATION
#####################
require "config.pl"; # configuration file
$templateFile = "templates/show_banners.htmlf";
#######
# MAIN
#######
print "Content-type: text/html\n\n";
#Read the views data file
open (GATE, "$viewsFile") or die ("Could not open views file\n");
@ViewsData = ;
close (GATE);
foreach $pair(@ViewsData) { #Convert views data to hash
chomp($pair);
($ThisBanner,$ThisViews) = split(/\|/,$pair);
$ViewsHash{$ThisBanner} = $ThisViews;
}
@Banner = sort { $ViewsHash{$a} <=> $ViewsHash{$b} } keys %ViewsHash; #And we sort by views
#We are going to ramdomize the order a little bit
srand($$|time);
for ($count = 0; $count<5; $count++){
$RandomA = int(rand(4));
$RandomB = int(rand(4));
$temp = $Banner[$RandomA];
$Banner[$RandomA] = $Banner[$RandomB];
$Banner[$RandomB] = $temp;
}
#Display the HTML for the 4 banners
$Banner1 = $Banner[0];
$Banner2 = $Banner[1];
$Banner3 = $Banner[2];
$Banner4 = $Banner[3];
print &Template ($templateFile);
# Now we add one to the count for those banners
$ViewsHash{$Banner[0]} += 1;
$ViewsHash{$Banner[1]} += 1;
$ViewsHash{$Banner[2]} += 1;
$ViewsHash{$Banner[3]} += 1;
# And we print back out to the views file
$buffer = '';
foreach $key (keys %ViewsHash){
$buffer .= $key."|".$ViewsHash{$key}."\n";
}
open (GATE, ">$viewsFile") or die ("Could not open views file\n");
print GATE "$buffer";
close(GATE);
#################################################################
# This will read the template file and fill in the variables from the script
#################################################################
sub Template {
local(*FILE); # filehandle
local($file); # file path
local($HTML); # HTML data
$file = $_[0] || die "Template : No template file specified\n";
open(FILE, "<$file") || die "Template : Couldn't open $file : $!\n";
while () { $HTML .= $_; }
close(FILE);
$HTML =~ s/\$(\w+)/${$1}/g;
return $HTML;
}
|