#!/bin/perl -w require "adtracklib.pl"; # SET THE FOLLOWING VARIABLES: $hrs = 6; $exit_page = "http://www.glamazon.com/exit.shtml"; #Parse incoming name=value pairs &ReadFormInput; $gotclick = $INPUT{"gotclick"}; # Name of the display ad that was clicked. $displayad = $INPUT{"displayad"}; # Name of the ad to display (optional - this is used to # "hardwire" the appearance of a particular display ad # in a particular ad spot. $sponsor = $INPUT{"sponsor"}; # Name of ad sponsor. Using this, we can run different ads from # the same sponsor, but if the user clicks one ad from this # sponsor, all ads from this sponsor are screened out.(required!) $type = $INPUT{"type"}; # Type of ad to display (textlink, banner, stamp)(required!) $site = $INPUT{"site"}; # Name of site this ad is coming in from (required!) &InitSiteVars($INPUT{'site'}); $ip_addr = "$ENV{'REMOTE_ADDR'}"; # make into global variable $ip_logfile = "$logs_dir/$ip_addr.reap"; $gotoad = "http://www.glamazon.com/cgi/adtrack/lib.adtrack.cgi?site=$site"; #Turn off buffering so that output is displayed in correct order. $| = 1; # Redirect all of the error messages to STDOUT (i.e the browser). open (STDERR, ">&STDOUT"); if ($gotclick) { &gotoad; } else { &displayad; } exit; sub displayad { #Print out the HTML Header print "Content-type: text/html\n\n"; if ( !$displayad ) { # Here's where the banner rotation is done. # If $displayad is empty pick an ad to display. # Banner file contains names of rotating banners. if ($type eq "textlink") {$adfile = $textlinks;} elsif ($type eq "banner") {$adfile = $banners; } elsif ($type eq "stamp") {$adfile = $stamps;} open (BANNERS, "<$adfile") || return_error("500","Banner File Error","Could not open file $adfile"); while () { chop; ($ad_name,$ad_sponsor) = split(/ /,$_,2); $show_banners{$ad_name} = $ad_sponsor; } close BANNERS; if (-e $ip_logfile) { $numdate = time(); $cutoff = $numdate - ($hrs * 3600); open (LOG, "<" . $ip_logfile) || &return_error( 500, "Ad Log Error in displayad","Could not open logfile $ip_logfile"); seek LOG, 0, 0; @loglines = ; close LOG; foreach $line ( @loglines ) { # Figure out which sponsors have been clicked. # Delete ad_names of sponsors that have been clicked. ($my_time, $my_referer, $my_ad, $my_sponsor ) = split(/ /,$line); chop($my_sponsor); foreach $key (keys %show_banners) { if (($my_time > $cutoff) && ($show_banners{$key} eq $my_sponsor)) { delete $show_banners{$key}; } } } } # What if there's nothing left? # Just say thanks and leave. @available = keys %show_banners; if (! @available) {&print_thanks; exit(0);} # Pick a random ad_name from the ones available. srand; $displayad = splice(@available, rand(@available), 1); # Delete the ad_name you have picked. delete $show_banners{$displayad}; }#displayad else { if (&already_clicked($displayad)) { &print_thanks; exit(0);} } # Print out the banner or textlink HTML. &PrintAdvert($displayad); }# end sub displayad sub print_thanks { #Say "Thank you, nice surfer!" print ""; print "Thank You For Clicking My Sponsors!
"; print "Type Control-D to Bookmark My Site
"; print "Please Visit Me Again Later!
\n"; } # This is called when the user clicks on an ad. sub gotoad { print "Location: $exit_page ","\n\n"; $x = &already_clicked; #&return_error( 500, "Ad Log Error","In GoToAd gotclick = $gotclick already_clicked returns $x"); if (! $x) { &logunique;} } sub logunique { # Get referer info if it is available $referer = $ENV{'HTTP_REFERER'}; if (!$referer) {$referer = "-";} # Get the time $numdate = time(); $logfile = "$logs_dir/$gotclick.log"; $temp_ip_logfile = "$logs_dir/$ip_addr.tmp"; if (! (-e $ip_logfile)) { #if the log file does not exist if (open (IP_LOG, ">" . $temp_ip_logfile)) { print (IP_LOG "$numdate $referer $gotclick $sponsor\n"); close (IP_LOG); if (! (chmod 0666, $temp_ip_logfile) ) { &return_error (500, "Ad Log Error", "Cannot chmod log file $temp_ip_logfile."); } if (! rename ($temp_ip_logfile, $ip_logfile )) { &return_error (500, "Ad Log Error", "Cannot rename log file $temp_ip_logfile, to $ip_logfile."); } } else { &return_error (500, "Ad Log Error", "Cannot create log file $temp_ip_logfile.");} } else { #&return_error( 500, "Ad Log Error","In logunique ip addr = $ip_addr referer = $referer numdate = $numdate logfile = $logfile ip_logfile = $ip_logfile temp_ip_logfile = $temp_ip_logfile"); #if the logfile exists if (! ((-r $ip_logfile) && (-w $ip_logfile)) ) { &return_error (500, "Ad Log Error","Cannot read or write log file $ip_logfile"); } else { open (LOG, ">>" . $ip_logfile); seek LOG, 2, 0; #@ end of log print LOG "$numdate $referer $gotclick $sponsor\n"; close LOG; } } }# end sub sub already_clicked { # returns true if this visitor clicked on an ad for this sponsor # before the cutoff time #&return_error (500, "Ad Log Error", "my_time = $my_time, cutoff = $cutoff, my_ad = $my_ad, gotclick = $gotclick."); $numdate = time(); $cutoff = $numdate - ($hrs * 3600); if ( !(-e $ip_logfile)) { return 0;} else { open (LOG, "<" . $ip_logfile); seek LOG, 0, 0; @loglines = ; close LOG; foreach $line ( @loglines ) { ($my_time, $my_referer, $my_ad, $my_sponsor ) = split(/ /,$line); chop($my_ad); if (($my_time > $cutoff) && ($my_sponsor eq $sponsor)){ return 1; } } } } # end sub