#!/usr/bin/perl
#
# Small banner rotate
#
use strict;
use DBI;
use CGI qw/:standard/;
#
# Params:
#
# banner_id - ID # of the banner that was clicked on
#
#
my $bid = param('bid');
my $nohead = param('nohead');
#
# Get current list of banners
#
my (@banners, @bannername, @imagename, @urls);
if( get_banners() ) {
if( $bid =~ /^\d+$/ ) {
#
# Record this click-thru
#
write_log();
#
# Redirect client to DEST
#
sendaway();
} else {
#
# Show a random banner
#
my $numbanners = @banners;
my $randnum = int( rand $numbanners );
my $image_name = $imagename[$randnum];
my $bannerid = $banners[$randnum];
if( !$nohead ) {
print header;
}
if( $urls[$randnum] ) {
print qq{};
} else {
print qq{
};
}
}
} else {
#
# Unable to open the banners file
#
}
#############
sub get_banners() {
if( open( FILE, "<./long_banner.dat" ) ) {
while( ) {
my $temp = $_;
chomp( $temp );
if( $temp !~ /^#/ ) {
my @parts = split( /\|/, $temp );
push( @banners, $parts[0] );
push( @bannername, $parts[1] );
push( @imagename, $parts[2] );
push( @urls, $parts[3] );
}
}
if( close( FILE ) ) {
return( 1 );
}
}
return( 0 );
}
sub sendaway() {
if( !$nohead ) {
print header;
}
# Locate this specific banner id as specified in the dat file
my $count = 0;
my $banner_id;
foreach my $this_id ( @banners ) {
print STDERR "THIS $this_id\n";
if( $this_id == $bid ) {
print STDERR "FOUND bid=$bid\n";
$banner_id = $count;
}
$count++;
}
if( $urls[$banner_id] ) {
#
# Send an HTTP-EQUIV
#
my $url = $urls[$banner_id];
print qq{};
} else {
print qq{};
}
}
sub write_log() {
#
# Write this info to the database
#
my $dbh = db_connect() || die();
if( $dbh->do("INSERT INTO banners ( date_created, banner_id, banner_name, ip, browser, referrer ) VALUES ( now(), '$bid', '" . $bannername[$bid] . "', '" . $ENV{'REMOTE_ADDR'} . "', '" . $ENV{'HTTP_USER_AGENT'} . "', '" . $ENV{'HTTP_REFERER'} . "' )") ) {
#
#
#
return( 1 );
} else {
#
# Could not enter this into the database
#
}
return( 0 );
}
sub db_connect() {
my ($database, $hostname, $username, $port, $dsn, $user, $password, $driver, $dbh, $sth , $sql);
$database = "malawimayhem";
$hostname = "localhost";
$username = "malawimayhem";
$password = "fi5hr0ck!";
$driver = "mysql";
$dsn = "DBI:$driver:database=$database;host=$hostname";
$dbh = DBI->connect($dsn, $username, $password) || die( "Can't connect to the database!" );
return( $dbh );
}