#!/usr/local/bin/perl # SET THE FOLLOWING VARIABLES: $hrs = 48; $mydir = "webvert"; $gotoad = "http://www.glamazon.com/cgi-bin/$mydir/ads.pl"; $baseref = "/home/web/glamazon/cgi-bin/$mydir"; #$adcount = $baseref ."/adcount.txt"; $adbanners = $baseref ."/adbanners.txt"; $rotator = $baseref ."/rotator.txt"; $ads_dir = $baseref ."/ads"; $logs_dir = $baseref ."/logs"; # DO NOT MODIFY ANYTHING BELOW THIS LINE, # UNLESS YOU'RE SURE YOU KNOW WHAT YOU'RE DOING! #Parse incoming name=value pairs if ($ENV{'QUERY_STRING'}) { @pairs = split(/&/, $ENV{'QUERY_STRING'}); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $INPUT{$name} = $value; } } $gotclick = $INPUT{"gotclick"}; $displayad = $INPUT{"displayad"}; # Name of the ad to display - empty if rotating banner ad. #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; } sub displayad { #Print out the HTML Header if we need it #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. open (BANNERS, "<$adbanners"); seek BANNERS, 0,0; @bannernames = ; close BANNERS; foreach $line (@bannernames) { chop ($line) if ($line =~ /\n$/); } foreach $line (@bannernames) { push(@a,$line) if ( ($line =~ /\S/) && (! &already_clicked($line) ) ) ; } @bannernames = @a; ## filter out the spaces and the ones we have already seen if (! @bannernames) { &print_thanks; exit(0);} ##none left? we have already clicked them all, exit else { $displayad = "get one!"; ##pick an ad we have not clicked yet while (! ( grep(/^$displayad/, @bannernames))) { open (ROTATOR, "<$rotator"); $rotvar = ; close ROTATOR; $rotvar++; if ($rotvar > (@bannernames - 1)) {$rotvar = 0}; open (ROTATOR, ">$rotator") || die "Could not open $rotator\n"; print ROTATOR $rotvar; close ROTATOR; if ($bannernames[$rotvar] =~ /\S/) {$displayad = $bannernames[$rotvar]}; } } } if (&already_clicked($displayad)) { &print_thanks; exit(0);} else { &read_display_vars($displayad); # Print out the tag. if ($text) { print STDOUT "$text
\n"; } if ($image) { print STDOUT "\n"; print STDOUT "\"$text\""; } } exit(0); } sub print_thanks #Say thank you nice surfer { print STDOUT "\Thank You For Clicking My Sponsors\!\Type Control\-D to Bookmark My Site\Please Visit Me Again Later!\<\/FONT\>\n"; } # This is called when the user clicks on an ad. sub gotoad { &read_display_vars($gotclick); if (! &already_clicked($gotclick)) {&logunique;} print "Location: $url ","\n\n"; exit(0); } sub return_error { local ($status, $keyword, $message) = @_; print "Content-type: text/html\n"; print "Status: $status $keyword\n\n"; print < CGI Program Error

$keyword


$message
End_of_Error exit(1); } sub read_display_vars { local ($filename) = @_; open (DISPLAY, "<$ads_dir/$filename.txt") || &return_error (500, "Ad Log Error", "Could not open ad file $ads_dir/$filename.txt."); seek DISPLAY, 0,0; @lines = ; close DISPLAY; chomp @lines; # Name each of the variables in the DISPLAY file. ($cents,$url,$text,$image,$height,$width,$target,$javascript,$statsurl,$userid,$passcode,$bannersurl ) = @lines; } sub logunique { $ip_addr = $ENV{'REMOTE_ADDR'}; $referer = $ENV{'HTTP_REFERER'}; $numdate = time(); $logfile = "$logs_dir/$gotclick.log"; if (! (-e $logfile)) { #if the logfile doesn't exist, it must be created. if (open (LOG, ">" . $logfile)) { flock (LOG, $exclusive_lock); print (LOG "$ip_addr $numdate $referer\n"); flock (LOG, $unlock_lock); close (LOG); if (! (chmod 0666, $logfile) ) { &return_error (500, "Ad Log Error", "Cannot chmod log file."); } } else { &return_error (500, "Ad Log Error", "Cannot create log file."); } } else { #if the logfile exists if (! ((-r $logfile) && (-w $logfile)) ) { &return_error (500, "Ad Log Error", "Cannot read or write log file $logfile"); } else { open (LOG, ">>" . $logfile); flock (LOG, $exclusive_lock); seek LOG, 2, 0; #@ end of log print LOG "$ip_addr $numdate $referer\n"; flock (LOG, $unlock_lock); close LOG; } } }# end sub sub already_clicked { # returns true if this visitor clicked on this ad before local ($filename) = @_; $numdate = time(); $cutoff = $numdate - ($hrs * 3600); $ip_addr = "$ENV{'REMOTE_ADDR'}"; $logfile = "$logs_dir/$filename.log"; if ( !(-e $logfile)) { return 0;} else { # Read through the log, looking for another hit open (LOG, "<" . $logfile); seek LOG, 0, 0; @loglines = ; close LOG; foreach $line ( @loglines ) { ($my_ip, $my_time, $my_referer ) = split(/ /,$line); if (($my_time > $cutoff) && ($my_ip =~ /^$ip_addr/)){ return 1; } } } } # end sub