#!/usr/bin/perl



###############################
# Hot Links SQL 2.1  banner-js.pl
# Created by Mike Ramirez
# Nothing below here is configurable
###############################
use CGI;
use DBI;

require 'config.pl';

my $q = CGI->new;
print $q->header;
my $dbh = DBI->connect("DBI:mysql:$dbname;$dbhost", $dblogin, $dbpass) || print "Could not connect to database<br>";

$category = $q->param("show");
if ($category eq 'ALL') {$show_all = 1} 
elsif ($category eq '') {
  $category = 'All';
}

###############################
if ($category ne 'All') {
###############################
my $sth = $dbh->prepare("SELECT * FROM adverts WHERE category = \"$category\"") || print "could not access database";
$sth->execute();
while (my $results = $sth->fetchrow_hashref) {
  my $ad_id = $results->{id};
  my $ad_cat = $results->{category};
  my $ad_title = $results->{title};
  my $ad_url = $results->{url};
  my $ad_image = $results->{image};
  my $ad_weight = $results->{weight};
  my $ad_keyw = $results->{keywords};
  push(@adverts, "$ad_id|$ad_cat|$ad_title|$ad_url|$ad_image|$ad_weight|$ad_keyw");
}
$sth->finish;
foreach $advert (@adverts) {
chomp($advert);
$loop_index = 1;
	@adinfo = split(/\|/, $advert);
	while ($loop_index <= $adinfo[5]) { # this adds the advert info to the hash (multiple times if weighted)
		$adcount++;
		$adhash{$adcount} = "$advert";
		$loop_index++;
		}
}

$randnum = rand($adcount);
$randnum =~ s/\..*//;
$randnum++;

if ($adcount) {
	foreach $key (sort keys %adhash) {
		if ($key == $randnum) {
			@showad = split(/\|/, $adhash{$key});
		}
	}
} else {
my $sth = $dbh->prepare("SELECT * FROM adverts WHERE category = 0") || print "could not access database";
$sth->execute();
while (my $results = $sth->fetchrow_hashref) {
  my $ad_id = $results->{id};
  my $ad_cat = $results->{category};
  my $ad_title = $results->{title};
  my $ad_url = $results->{url};
  my $ad_image = $results->{image};
  my $ad_weight = $results->{weight};
  my $ad_keyw = $results->{keywords};
  $defaultad = "$ad_id|$ad_cat|$ad_title|$ad_url|$ad_image|$ad_weight|$ad_keyw";
}
$sth->finish;

@showad = split(/\|/, $defaultad);

}

$sendto = $showad[3];
$sendto =~ s/([\W])/"%" . uc(sprintf("%2.2x",ord($1)))/eg;

unless (!$showad[0]) {
print <<"HTML";
	document.write(\"<br><a href='$outscript?out=ad&id=$showad[0]&sendto=$sendto' target='blank'><img src='$showad[4]' alt='$showad[2]' border=0><br><font size=$smalllink>$showad[2]</font></a>\");
HTML
}

# Exposure logging code #
    my $sth = $dbh->prepare("SELECT count FROM displaycount WHERE id = $showad[0]") || print "could not access database";
    $sth->execute();
      my $current_count=0;
      if ($sth->rows) {$current_count = $sth->fetchrow; $ad_id_exists = 1;}
      $current_count++;
      $sth->finish;
     if ($ad_id_exists == 1) {
      my $sth = $dbh->prepare("UPDATE displaycount SET count = '$current_count' WHERE id = '$showad[0]'") || print "could not access database";
      $sth->execute();
      $sth->finish;
     } else {
       my $sth = $dbh->prepare("INSERT INTO displaycount VALUES ($showad[0], $current_count)") || &error("Could not insert new row.");
       $sth->execute();
       $sth->finish;
     }
###############################
} elsif ($category eq 'All') {
#################
my $count=0;
my $sth = $dbh->prepare("SELECT * FROM adverts WHERE category != 0") || print "could not access database";
$sth->execute();
while (my $results = $sth->fetchrow_hashref) {
  $count++;
  my $ad_id = $results->{id};
  my $ad_cat = $results->{category};
  my $ad_title = $results->{title};
  my $ad_url = $results->{url};
  my $ad_image = $results->{image};
  my $ad_weight = $results->{weight};
  my $ad_keyw = $results->{keywords};
  $matchads{$count} = "$ad_id|$ad_cat|$ad_title|$ad_url|$ad_image|$ad_weight|$ad_keyw";
}
$sth->finish;

# Get the default banner
my $sth = $dbh->prepare("SELECT * FROM adverts WHERE category = 0") || print "could not access database";
$sth->execute();
  while (my $results = $sth->fetchrow_hashref) {
     my $ad_id = $results->{id};
     my $ad_cat = $results->{category};
     my $ad_title = $results->{title};
     my $ad_url = $results->{url};
     my $ad_image = $results->{image};
     my $ad_weight = $results->{weight};
     my $ad_keyw = $results->{keywords};
     $defaultad = "$ad_id|$ad_cat|$ad_title|$ad_url|$ad_image|$ad_weight|$ad_keyw";

   }
$sth->finish;
if ($count == 0) {$matchads{1} = $defaultad;};

$hashcount = 0;
foreach $key (sort keys %matchads) {
$hashcount++
}

$randnum = rand($hashcount);
$randnum =~ s/\..*//;
$randnum++;

foreach $key (sort keys %matchads) {
	if ($key == $randnum) {
		@showad = split(/\|/, $matchads{$key});
	}
}

$sendto = $showad[3];
$sendto =~ s/([\W])/"%" . uc(sprintf("%2.2x",ord($1)))/eg;

unless (!$showad[0]) {
print <<"HTML";
	document.write(\"<br><a href='$outscript?out=ad&id=$showad[0]&sendto=$sendto' target='blank'><img src='$showad[4]' alt='$showad[2]' border=0><br><font size=$smalllink>$showad[2]</font></a>\");
HTML
}

# Exposure logging code #
    my $sth = $dbh->prepare("SELECT count FROM displaycount WHERE id = $showad[0]") || print "could not access database";
    $sth->execute();
      my $current_count=0;
      if ($sth->rows) {$current_count = $sth->fetchrow; $ad_id_exists = 1;}
      $current_count++;
      $sth->finish;
     if ($ad_id_exists == 1) {
      my $sth = $dbh->prepare("UPDATE displaycount SET count = '$current_count' WHERE id = '$showad[0]'") || print "could not access database";
      $sth->execute();
      $sth->finish;
     } else {
       my $sth = $dbh->prepare("INSERT INTO displaycount VALUES ($showad[0], $current_count)") || &error("Could not insert new row.");
       $sth->execute();
       $sth->finish;
     }

  }
$dbh->disconnect;

