#!/usr/local/bin/perl
#
# Program: bpwork.pl
# Author: Drummond Miles
# Email: info@gadnet.com
#
# Created: 29/12/98
# Last Modified: 12/09/99 - removed references to $cgipath for ease of cross-platform installation
# Copyright G.A.D.Miles 1998. All Rights Reserved
#
# Download script from http://www.gadnet.com/bplus
#
# This program is being distibuted as freeware. It can be freely downloaded and used.
# It may be modified as long as the copyright information remains intact.
#
# Any use of this program is entirely at the risk of the user. No liability will be
# accepted by the author.
#
# This code must not be sold, even in modified form, without the written permission
# of the author. This code must also not be distributed without the permission of the 
# author.

open (STDERR, ">>bperror.log");
require "bpvars.txt";
require "bpsubs.cgi";
if ($ENV{'QUERY_STRING'} =~ /advert=.*/)
{
	&gotosite;
}
if ($ENV{'QUERY_STRING'} =~ /ID=([^\s&]*)&/)
{
	$thisuser = $1;
	&getad;
	&updateuser;
}

sub getad
{
	srand;
	open (MEMBERS, "+<$bpdata/members.txt");
	$lockfail = &lock("MEMBERS");
	if ($lockfail)
	{
		print "Location: $defaultbanner\n\n";
		return 1;
	}
	@members = <MEMBERS>;
	close (MEMBERS);
	$total = @members;
	$pickmember = int(rand($total));
	$adfound = "";
	for ($zzz=0; $zzz<$total; $zzz++)
	{
		$index = $zzz + $pickmember;
		if ($index > $total)
		{
			$index = $index - $total;
		} 
# Make sure someone isn't displaying a banner for their own site
		chop $members[$index] if ($members[$index] =~ /\n$/);
		next if $members[$index] eq $thisuser;
		open (CHECKFILE, "+<$bpdata/$members[$index].txt");
		$lockfail = &lock("CHECKFILE");
		next if $lockfail;
		@checklines = <CHECKFILE>;
		close (CHECKFILE);
# Ignore unverified members
		chop $checklines[13] if ($checklines[13] =~ /\n$/);
		next if !$checklines[13];
		chop $checklines[3] if ($checklines[3] =~ /\n$/);
# Ignore if they have not provided a banner URL
		next if !$checklines[3];
		$credits = ((int($checklines[4] / $checklines[5])) + $checklines[14]) - $checklines[6];

# Ignore if they have not earned any credits

		next if $credits <= 0;
		chop $checklines[6] if ($checklines[6] =~ /\n$/);
		$checklines[6]++;
		if (-e "$bpdata/adurls.txt")
		{
			$nowtime = time;
			open (ADURLS, "+<$bpdata/adurls.txt");
			$lockfail = &lock("ADURLS");
			if ($lockfail)
			{
				print "Location: $defaultbanner\n\n";
				return 1;
			}
			@adurls = <ADURLS>;
			seek (ADURLS,0,0);
			truncate (ADURLS,0);
			foreach $adurls (@adurls)
			{
				($adtime,$ignore) = split(/\|/,$adurls);
				if (($nowtime - $adtime) < 1200)
				{
					print ADURLS "$adurls";
				}
			}
		}
		else
		{
			open (ADURLS, ">$bpdata/adurls.txt");
			$lockfail = &lock("ADURLS");
			if ($lockfail)
			{
				print "Location: $defaultbanner\n\n";
				return 1;
			}
		}
            if (defined ($ENV{'HTTP_REFERER'}))
		{
			$referer = $ENV{'HTTP_REFERER'};
		}
		else
		{
			$referer = 'none';
		}
		$adtime = time;
		seek (ADURLS,0,2);
		print ADURLS "$adtime|$referer|$ENV{'REMOTE_ADDR'}|$members[$index]\n";
		close (ADURLS);
		open (CHECKFILE, "+<$bpdata/$members[$index].txt");
		$lockfail = &lock("CHECKFILE");
		if ($lockfail)
		{
			print "Location: $defaultbanner\n\n";
			return 1;
		}
		seek (CHECKFILE,0,0);
		foreach $checklines (@checklines)
		{
			chop $checklines if ($checklines =~ /\n$/);
			print CHECKFILE "$checklines\n";
		}
		close (CHECKFILE);
		$adfound = $checklines[3];
		$adurl = $checklines[2];
		last;
	}
}

sub updateuser
{
	open (USERFILE, "+<$bpdata/$thisuser.txt");
	$lockfail = &lock("USERFILE");
	if ($lockfail)
	{
		print "Location: $defaultbanner\n\n";
		return 1;
	}
	@userfile = <USERFILE>;
	chop $userfile[4] if ($userfile[4] =~ /\n$/);	
	$userfile[4]++;
#	$year = (localtime)[5] + 1900;
#	$month = (localtime)[4] + 1;
#	if ($month < 10)
#	{
#		$month = "0".$month;
#	}
#	$day = (localtime)[3];
#	if ($day < 10)
#	{
#		$day = "0".$day;
#	}
	$userfile[11] = time;
	seek (USERFILE,0,0);
	foreach $userfile (@userfile)
      {
		chop $userfile if ($userfile =~ /\n$/);
		print USERFILE "$userfile\n";
	}
	close (USERFILE);
	if ($adfound)
	{
		print "Location: $adfound\n\n";
	}
	else
	{
		print "Location: $defaultbanner\n\n";
	}
}

sub gotosite
{
	$adfound = "";
	open (ADURLS, "+<$bpdata/adurls.txt");
	$lockfail = &lock("ADURLS");
	if ($lockfail)
	{
		print "Location: $defaulturl\n\n";
		return 0;
	}
	@adurls = <ADURLS>;
	close (ADURLS);
	@reversads = reverse (@adurls);
	foreach $adurl (@reversads)
	{
		($adtime,$referer,$remote,$member) = split(/\|/,$adurl);
		next if ($referer ne $ENV{'HTTP_REFERER'}) || ($referer eq 'none');
		next if $remote ne $ENV{'REMOTE_ADDR'};
		$adfound = 1;
		last;
	}

# Update clickthrus

	if ($adfound)
	{
		chop $member if ($member =~ /\n$/);
		open (USERFILE, "+<$bpdata/$member.txt");
		$lockfail = &lock("USERFILE");
		if ($lockfail)
		{
			print "Location: $defaulturl\n\n";
			return 0;
		}
		@userfile = <USERFILE>;
		chop $userfile[7] if ($userfile[7] =~ /\n$/);
		$userfile[7]++;
		seek (USERFILE,0,0);
		foreach $userfile (@userfile)
      	{
			chop $userfile if ($userfile =~ /\n$/);
			print USERFILE "$userfile\n";
		}
		close (USERFILE);
		print "Location: $userfile[2]\n\n";
	}
	else
	{
		print "Location: $defaulturl\n\n";
	}
}

