#!/usr/local/bin/perl -w # SET THE FOLLOWING VARIABLES: # Format of count file is a $time variable followed by names of all ads, # static, text, and rotating. $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! $exclusive_lock = 2; $unlock_lock = 8; #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. $cfh = select (STDOUT); $| = 1; select ($cfh); # 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 in the DISPLAY file open (DISPLAY, "+>>$ads_dir/$displayad.txt") || &return_error (500, "Ad Display Error", "Could not open ad file $ads_dir/$displayad.txt."); &read_display_vars; # Increment number of times shown. Write out DISPLAY file. Unlock it. $shown++; &write_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 { open (DISPLAY, "+>>$ads_dir/$gotclick.txt") || &return_error (500, "Ad Log Error", "Could not open ad file $ads_dir/$filename.txt."); &read_display_vars; $visits++; &logunique; &write_display_vars($gotclick); 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 { seek DISPLAY, 0,0; @lines = ; chomp @lines; # Name each of the variables in the DISPLAY file. ($shown,$visits,$uniques,$cents,$url,$text,$image,$height,$width,$target,$javascript,$statsurl,$userid,$passcode,$bannersurl ) = @lines; } sub write_display_vars { local ($filename) = @_; $myfile = "$ads_dir/".$filename.".txt"; seek DISPLAY, 0, 0; # rewind truncate $myfile, 0; # empty the file print DISPLAY "$shown\n"; print DISPLAY "$visits\n"; print DISPLAY "$uniques\n"; print DISPLAY "$cents\n"; print DISPLAY "$url\n"; print DISPLAY "$text\n"; print DISPLAY "$image\n"; print DISPLAY "$height\n"; print DISPLAY "$width\n"; print DISPLAY "$target\n"; print DISPLAY "$javascript\n"; print DISPLAY "$statsurl\n"; print DISPLAY "$userid\n"; print DISPLAY "$passcode\n"; print DISPLAY "$bannersurl\n"; close DISPLAY; } sub logunique { $ip_addr = "$ENV{'REMOTE_HOST'}"; $referer = $ENV{'HTTP_REFERER'}; $numdate = time(); ##$hrs = 48; ##$cutoff = $numdate - ($hrs * 3600); $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 IP address & time & referer for each unique hit $uniques++; 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 { # Read through the log, looking for another hit open (LOG, "+>>" . $logfile); flock (LOG, $exclusive_lock); seek LOG, 0, 0; @content = ; # Print IP address for hit only if it's unique (and eventually not recent) if ( not (grep /^$ip_addr/, @content)) { $uniques++; seek LOG, 2, 0; #@ end of log print LOG "$ip_addr $numdate $referer\n"; } close LOG; } } }# end sub sub already_clicked { # returns true if this visitor clicked on this ad before local ($filename) = @_; $ip_addr = "$ENV{'REMOTE_HOST'}"; $logfile = "$logs_dir/$filename.log"; if ( !((-e $logfile) && (-r $logfile)) ) { &return_error (500, "Ad Log Error", "Cannot read log file $logfile $!.");} else { # Read through the log, looking for another hit open (LOG, "<" . $logfile); seek LOG, 0, 0; @loglines = ; close LOG; foreach $line ( @loglines ) { if ($line =~ /^$ip_addr/) { return 1; } } } } # end sub