############################################ ## ## ## WebAdverts (Display) ## ## by Darryl Burgdorf ## ## (e-mail burgdorf@awsd.com) ## ## ## ## version: 3.20 ## ## last modified: 09/08/02 ## ## copyright (c) 2002 ## ## ## ## latest version is available from ## ## http://awsd.com/scripts/ ## ## ## ############################################ # NOTHING BELOW THIS LINE NEEDS TO BE ALTERED! use Fcntl; BEGIN { @AnyDBM_File::ISA = qw (DB_File GDBM_File SDBM_File ODBM_File NDBM_File) } if (eval "require DB_File") { @ISA = ('DB_File'); } else { use AnyDBM_File; $NotUsingBestDBM=1; } umask (0111); $_ = $ADVadverts_dir; /^(.+)$/; $ADVadverts_dir = $1; sub ADVsetup { $ADVtime = time; unless ($ADVUseLocking) { &ADVMasterLockOpen; if ($ADVlockerror) { return; } } if ((-M "$ADVadverts_dir/adlist.txt") > .5) { if ($ADVRandomizeList) { open (ADLIST, "$ADVadverts_dir/adlist.txt"); $adlist = @adlist = ; close (ADLIST); srand(); &ADVlistreorder; &ADVLockOpen (ADLIST, "adlist.txt"); unless ($ADVlockerror) { seek(ADLIST, 0, 0); foreach $ad (@newadlist) { print ADLIST "$ad"; } truncate (ADLIST, tell(ADLIST)); &ADVLockClose (ADLIST, "adlist.txt"); } } opendir (FILES,"$ADVadverts_dir"); @files = readdir(FILES); closedir (FILES); foreach $file (@files) { if (($file =~ /^dupcli/) || ($file =~ /^dupvie/)) { unlink "$ADVadverts_dir/$file"; } } } if ($ADVLogIP) { if ($ADVResolveIPs) { if (($ENV{'REMOTE_ADDR'} =~ /\d+\.\d+\.\d+\.\d+/) && (!($ENV{'REMOTE_HOST'}) || ($ENV{'REMOTE_HOST'} =~ /\d+\.\d+\.\d+\.\d+/))) { @domainbytes = split(/\./,$ENV{'REMOTE_ADDR'}); $packaddr = pack("C4",@domainbytes); $resolvedip = (gethostbyaddr($packaddr, 2))[0]; unless ($resolvedip =~ /^[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,6}|[0-9]{1,3})$/) { $resolvedip = ""; } if ($resolvedip) { $ENV{'REMOTE_HOST'} = $resolvedip; } } } unless ($ENV{'REMOTE_HOST'}) { $ENV{'REMOTE_HOST'} = $ENV{'REMOTE_ADDR'}; } } $DoNotLog = 0; if ($ADVIgnoredIPs) { @ignoredips = split(/ /,$ADVIgnoredIPs); foreach $ignoredip (@ignoredips) { if (($ENV{'REMOTE_HOST'} =~ /$ignoredip/i) || ($ENV{'REMOTE_ADDR'} =~ /$ignoredip/i)) { $DoNotLog = 1; } } } $harvester_list = 'bullseye|cherrypicker|crescent|emailcollector|emailsiphon|emailwolf|extractor|microsoft url|mozilla/3.mozilla/2.01|newt|nicerspro|webbandit|brutus'; $download_list = 'da \d|dnload|download|fetch|flashget|ftp|getright|gozilla|jetcar|leach|leech'; $linkchecker_list = 'analyze|check|link|netmechanic|netmind|powermarks|redalert|tooter|validat|verif|walk'; $offline_list = 'avantgo|batch|copier|httrack|msiecrawler|msproxy|netattache|netscape-proxy|offline|spacebison|teleport|webcapture|webzip'; $spider_list = 'aport|archive|ask jeeves|behold|borg|bot|catch|crawl|digger|elitesys|enfish|esense|euroseek|ferret|grab|griffon|gulliver|harvest|htdig|hubat|hunt|infoseek|java|leia|lwp-|lwp:|mantraagent|mapper|mata hari|mercator|netants|perl|quest|reader|reaper|roamer|rover|scooter|search|slurp|snatch|spider|spinne|spyder|sweep|t-h-u-n-d-e-r-s-t-o-n-e|ultraseek|url|utopy|webcollage|webster pro|webwhacker|wfarc|wget|whatuseek'; if (($ENV{'HTTP_USER_AGENT'} =~ m#$harvester_list#oi) || ($ENV{'HTTP_USER_AGENT'} =~ m#$download_list#oi) || ($ENV{'HTTP_USER_AGENT'} =~ m#$linkchecker_list#oi) || ($ENV{'HTTP_USER_AGENT'} =~ m#$offline_list#oi) || ($ENV{'HTTP_USER_AGENT'} =~ m#$spider_list#oi)) { unless (($ENV{'HTTP_USER_AGENT'} =~ m#robotics#oi) || ($ENV{'HTTP_USER_AGENT'} =~ m#hotjava#oi)) { $DoNotLog = 1; } } if ($ADVIFRAMEJSConflict) { if ($ADVQuery =~ /iframe/) { &ADVLockOpen (IFRAMETIME, "iframe.txt"); print IFRAMETIME " "; &ADVLockClose (IFRAMETIME, "iframe.txt"); } if ($ADVQuery =~ /jscript/) { if ((-e "$ADVadverts_dir/iframe.txt") && ((stat("$ADVadverts_dir/iframe.txt"))[9]+30>$ADVtime)) { if ((($ENV{'HTTP_USER_AGENT'} =~ m#msie 3#oi) || ($ENV{'HTTP_USER_AGENT'} =~ m#msie 4#oi) || ($ENV{'HTTP_USER_AGENT'} =~ m#msie 5#oi) || ($ENV{'HTTP_USER_AGENT'} =~ m#msie 6#oi)) && ($ENV{'HTTP_USER_AGENT'} !~ m#aol#oi)) { $DoNotLog = 1; } } } } $AdvertChosen = 0; ($ADVmin,$ADVhour,$ADVmday,$ADVmon,$ADVyear) = (localtime($ADVtime+($ADVHourOffset*3600)))[1,2,3,4,5]; if ($ADVmin < 10) { $ADVmin = "0$ADVmin"; } if ($ADVhour < 10) { $ADVhour = "0$ADVhour"; } if ($ADVmday < 10) { $ADVmday = "0$ADVmday"; } $ADVmon++; if ($ADVmon < 10) { $ADVmon = "0$ADVmon"; } $ADVyear = $ADVyear+1900; $TrimmedIP = $ENV{'REMOTE_ADDR'}; $TrimmedIP =~ s/(\d*\.\d*\.\d*)\.\d*/$1/; $ADVshown = 0; $ADVWrapCounter = 0; if ($ADVQuery =~ /(.*)&url=(.*)/i) { $ADVQuery = $1; $rawmodedest = $2; } if ($ADVQuery =~ /page=([^\s&;\?]*)/i) { $displaypage = $1; $NonSSI = 1; } if ($ADVQuery =~ /zone=([^\s&;\?]*)/i) { $ADVadvertzone = $1; $ADVfulladvertzone = $ADVadvertzone; if ($ADVadvertzone =~ /\+/) { @advertzones = split(/\+/,$ADVadvertzone); $ADVadvertzonecount = @advertzones; srand(); $ADVadvertzone = @advertzones[int(rand($ADVadvertzonecount))]; } } unless ($ADVadvertzone) { $ADVadvertzone = "unzoned"; } if ($ENV{'HTTP_REFERER'} =~ /page=[^\s&;\?]*/i) { $ENV{'HTTP_REFERER'} = "dummy"; } unless (defined $ENV{'HTTP_REFERER'}) { $ENV{'HTTP_REFERER'} = "dummy"; } $ENV{'HTTP_REFERER'} =~ s/([^\?]*)\?.*/$1/; $ADVCookieName = "$ENV{'HTTP_REFERER'} $displaypage $ADVfulladvertzone"; $ADVCookieName2 = "dummy $displaypage $ADVadvertzone"; %Cookie_Encode_Chars = ( '\%','%25','\+','%2B','\;','%3B','\,','%2C', '\=','%3D','\&','%26','\:\:','%3A%3A','\s','+' ); %Cookie_Decode_Chars = ( '\+',' ','\%3A\%3A','::','\%26','&','\%3D','=', '\%2C',',','\%3B',';','\%2B','+','\%25','%' ); if ($ADVQuery =~ /setdest=([^\s&;\?]*)/i) { $SetDest = $1; $SetDest =~ s/[^\w\.\-\']//g; $SetDest =~ tr/A-Z/a-z/; unless ($SetDest =~ /^[a-z,0-9]/) { $SetDest = "a".$SetDest; } $SetDest = substr($SetDest,0,25); } if ($ADVQuery =~ /member=([^\s&;\?]*)/i) { $ADVID = $1; $ADVID =~ s/[^\w\.\-\']//g; $ADVID =~ tr/A-Z/a-z/; unless ($ADVID =~ /^[a-z,0-9]/) { $ADVID = "a".$ADVID; } $ADVID = substr($ADVID,0,25); $newsubdir = substr($ADVID,0,1); $newsubdir .= "/$ADVID"; } if ($ADVQuery =~ /banner=([^\s&;\?]*)/i) { $displayad = $1; $displayad =~ s/[^\w\.\-\']//g; $displayad =~ tr/A-Z/a-z/; unless ($displayad =~ /^[a-z,0-9]/) { $displayad = "a".$displayad; } $displayad = substr($displayad,0,25); &ADVgotoad; unless ($ADVUseLocking) { &ADVMasterLockClose; } return; } unless ($NonSSI || $ADVNoPrint) { if ($ENV{'PERLXS'} eq "PerlIS") { print "HTTP/1.0 200 OK\n"; } print "Cache-Control: no-cache\n"; print "Pragma: no-cache\n"; if ($ADVQuery =~ /jscript/) { print "Content-type: application/x-javascript\n\n"; } else { print "Content-type: text/html\n\n"; } } if ($ADVRequireMember) { if ($NonSSI || ($ADVQuery =~ /iframe/) || ($ADVQuery =~ /jscript/)) { unless (($ADVID) && (-e "$ADVadverts_dir/$newsubdir/$ADVID.txt")) { unless ($ADVUseLocking) { &ADVMasterLockClose; } return; } } } &ADVDBMOpenReadOnly; ($ADVcountlines[0],$ADVcountlines[1],$ADVcountlines[2]) = split(/\n/,$DBMList{'adcount.txt'}); if (($ADVtime - $ADVcountlines[2]) > 250000000) { $ADVcountlines[0] = 1; $ADVcountlines[1] = "0"; $ADVcountlines[2] = time; } open (ADVLIST, "<$ADVadverts_dir/adlist.txt"); while (defined($ADVlist = )) { push (@ADVcountlines,$ADVlist); } close (ADVLIST); chomp (@ADVcountlines); $ADVcount = $ADVcountlines[0]; @ADVcount = split(/\|/,$ADVcount); foreach $ADVcount (@ADVcount) { ($ADVone,$ADVtwo) = split(/=/,$ADVcount); unless ($ADVtwo) { $ADVtwo = "unzoned"; } $zonecount{$ADVtwo} = $ADVone; } unless ($zonecount{$ADVadvertzone}) { $zonecount{$ADVadvertzone} = 1; } $ADVexposures = $ADVcountlines[1]; ($ADVexposures,@ADVcycles) = split(/\|/, $ADVexposures); foreach $ADVcycles (@ADVcycles) { ($ADVone,$ADVtwo) = split(/=/,$ADVcycles); unless ($ADVtwo) { $ADVtwo = "unzoned"; } $cyclecount{$ADVtwo} = $ADVone; } unless ($cyclecount{$ADVadvertzone}) { $cyclecount{$ADVadvertzone} = 1; } if ($ADVadvertzone =~ /showall-(.*)/i) { $ADVadvertzone = $1; $ShowAllShown = 0; &ADVshowall; } elsif ($ADVadvertzone =~ /showall/i) { $ADVadvertzone = "ShowAll"; $ShowAllShown = 0; &ADVshowall; } else { &ADVdisplayad; } if (($AdvertChosen < 1) && $DefaultBanner) { $ShowDefaultBanner = 1; &ADVdisplayad; } $ADVcountlines[0] = ""; foreach $key (keys %zonecount) { $ADVcountlines[0] = $ADVcountlines[0]."$zonecount{$key}=$key|"; } $ADVcountlines[1] = "$ADVexposures|"; foreach $key (keys %cyclecount) { $ADVcountlines[1] = $ADVcountlines[1]."$cyclecount{$key}=$key|"; } $DBMList{'adcount.txt'} = "$ADVcountlines[0]\n$ADVcountlines[1]\n$ADVcountlines[2]"; &ADVDBMClose; &ADVLockClose (DBMLIST, "dbmlist.txt"); unless ($ADVUseLocking) { &ADVMasterLockClose; } } sub ADVlistreorder { $randlocation = int(rand($adlist)); if ($adlist[$randlocation] eq "-") { &ADVlistreorder; return; } push (@newadlist,$adlist[$randlocation]); $adlist[$randlocation] = "-"; $adcounter ++; if ($adcounter < $adlist) { &ADVlistreorder; } } sub ADVdisplayad { if ($ShowDefaultBanner) { $ADVdisplayad = $DefaultBanner; } elsif ($SetDest) { $ADVdisplayad = $SetDest; } else { $ADVWrapCounter++; if ($ADVWrapCounter > @ADVcountlines-3) { return; } $ADVdisplayad = $ADVcountlines[$zonecount{$ADVadvertzone}+2]; $ADVcycles = $cyclecount{$ADVadvertzone}; $zonecount{$ADVadvertzone}++; if ($zonecount{$ADVadvertzone} > @ADVcountlines-3) { $zonecount{$ADVadvertzone} = 1; $cyclecount{$ADVadvertzone}++; } if ($ADVID eq $ADVdisplayad) { &ADVdisplayad; return; } } ($ADVmax,$ADVshown,$ADVvisits,$ADVimage,$ADVstart,$ADVweight, $ADVzone,$ADVraw,$ADVratio,$ADVclicksfrom) = split(/\t/,$DBMList{$ADVdisplayad}); ($ADVmax,$ADVmaxtype) = split(/\|/, $ADVmax); unless ($ADVmaxtype) { $ADVmaxtype = "E"; } ($ADVdisplayratio,$ADVdisplaycount) = split(/\|/, $ADVratio); ($ADVclicksfrom,$ADVclicksratio) = split(/\|/, $ADVclicksfrom); if ($ADVmaxtype eq "N") { $ADVmax = 0; } $ADVrealmax = $ADVmax; if (($ADVmaxtype eq "N") && ($ADVdisplayratio < 1) && ($ADVclicksratio < 1)) { $ADVrealmax = $ADVshown+1; } elsif (($ADVmaxtype eq "E") || ($ADVmaxtype eq "N")) { if ($ADVdisplayratio > 0) { $ADVrealmax += int($ADVdisplaycount/$ADVdisplayratio); } if ($ADVclicksratio > 0) { $ADVrealmax += ($ADVclicksfrom*$ADVclicksratio); } } unless ($ShowDefaultBanner || $SetDest) { if ((($ADVadvertzone ne "unzoned") && ($ADVadvertzone ne "ShowAll") && (length($ADVzone) > 2) && ($ADVzone !~ /\s$ADVadvertzone\s/)) || !($ADVimage || $ADVraw) || (($ADVraw eq "J") && !($ADVimage) && $ADVJSConflict && ($ADVQuery =~ /jscript/)) || ($ADVweight < 1) || (((($ADVmaxtype eq "E") || ($ADVmaxtype eq "N")) && ($ADVrealmax <= $ADVshown)) && ((($ADVdisplayratio < 1) && ($ADVclicksratio < 1)) || ($ADVadvertzone ne "ShowAll"))) || (($ADVmaxtype eq "C") && ($ADVrealmax <= $ADVvisits)) || (($ADVmaxtype eq "D") && ($ADVrealmax <= $ADVtime)) || ($ADVstart > $ADVtime) || ((($ADVcycles/$ADVweight) != int($ADVcycles/$ADVweight)) && ($ADVadvertzone ne "ShowAll")) || (!($ADVimage) && $NonSSI)) { &ADVdisplayad; return; } } if ($SetDest) { if (!($ADVimage || $ADVraw) || (($ADVraw eq "J") && !($ADVimage) && $ADVJSConflict && ($ADVQuery =~ /jscript/)) || ($ADVweight < 1) || (((($ADVmaxtype eq "E") || ($ADVmaxtype eq "N")) && ($ADVrealmax <= $ADVshown)) && ((($ADVdisplayratio < 1) && ($ADVclicksratio < 1)) || ($ADVadvertzone ne "ShowAll"))) || (($ADVmaxtype eq "C") && ($ADVrealmax <= $ADVvisits)) || (($ADVmaxtype eq "D") && ($ADVrealmax <= $ADVtime)) || ($ADVstart > $ADVtime)) { $SetDest = ""; &ADVdisplayad; return; } } $subdir = substr($ADVdisplayad,0,1); $subdir .= "/$ADVdisplayad"; &ADVLockOpen (ADVDISPLAY, "$subdir/$ADVdisplayad.txt"); if ($ADVlockerror) { &ADVLockClose (ADVDISPLAY, "$subdir/$ADVdisplayad.txt"); $SetDest = ""; unless ($ShowDefaultBanner) { &ADVdisplayad; } return; } @ADVdisplaylines = ; chomp (@ADVdisplaylines); ($ADVmax,$ADVshown,$ADVvisits, $ADVurl,$ADVimage,$ADVheight,$ADVwidth, $ADValt,$ADVnada1,$ADVtext,$ADVstart, $ADVweight,$ADVzone,$ADVborder,$ADVtarget, $ADVraw,$ADVratio,$ADVnada2,$ADVnada3, $ADVdisplayzone,$ADVclicksfrom) = @ADVdisplaylines; ($ADVmax,$ADVmaxtype) = split(/\|/, $ADVmax); unless ($ADVmaxtype) { $ADVmaxtype = "E"; } ($ADVdisplayratio,$ADVdisplaycount) = split(/\|/, $ADVratio); ($ADVclicksfrom,$ADVclicksratio) = split(/\|/, $ADVclicksfrom); if ($ADVmaxtype eq "N") { $ADVmax = 0; } $ADVrealmax = $ADVmax; if (($ADVmaxtype eq "N") && ($ADVdisplayratio < 1) && ($ADVclicksratio < 1)) { $ADVrealmax = $ADVshown+1; } elsif (($ADVmaxtype eq "E") || ($ADVmaxtype eq "N")) { if ($ADVdisplayratio > 0) { $ADVrealmax += int($ADVdisplaycount/$ADVdisplayratio); } if ($ADVclicksratio > 0) { $ADVrealmax += ($ADVclicksfrom*$ADVclicksratio); } } ($ADVtext,$ADVtexttype) = split(/\|/, $ADVtext); unless ($ADVtexttype) { $ADVtexttype = "B"; } if ($DupViewTime && !($DoNotLog)) { $DupView = "$TrimmedIP $ADVdisplayad"; &ADVLockOpen (DUPVIEWS, "dupviews.txt"); unless ($ADVlockerror) { if ($ADVDBMType==1) { tie (%DupViews,'AnyDBM_File',"$ADVadverts_dir/dupviews",O_RDWR|O_CREAT,0666,$DB_HASH) || &ADVDupError; } elsif ($ADVDBMType==2) { dbmopen(%DupViews,"$ADVadverts_dir/dupviews",0666) || &ADVDupError; } else { tie (%DupViews,'AnyDBM_File',"$ADVadverts_dir/dupviews",O_RDWR|O_CREAT,0666) || &ADVDupError; } unless ($ADVduperror) { if (($ADVtime-$DupViews{$DupView}) > ($DupViewTime * 60)) { $DupViews{$DupView} = $ADVtime; } else { $DoNotLog = 1; } if ($ADVDBMType==2) { dbmclose (%DupViews); } else { untie %DupViews; } } } &ADVLockClose (DUPVIEWS, "dupviews.txt"); } if ($ClickViewTime && !($DoNotLog)) { $ClickView = "$TrimmedIP $ADVdisplayad"; &ADVLockOpen (CLICKVIEWS, "dupclicks.txt"); unless ($ADVlockerror) { if ($ADVDBMType==1) { tie (%ClickViews,'AnyDBM_File',"$ADVadverts_dir/dupclicks",O_RDWR|O_CREAT,0666,$DB_HASH) || &ADVDupError; } elsif ($ADVDBMType==2) { dbmopen(%ClickViews,"$ADVadverts_dir/dupclicks",0666) || &ADVDupError; } else { tie (%ClickViews,'AnyDBM_File',"$ADVadverts_dir/dupclicks",O_RDWR|O_CREAT,0666) || &ADVDupError; } unless ($ADVduperror) { unless (($ADVtime-$ClickViews{$ClickView}) > ($ClickViewTime * 60)) { $DoNotLog = 1; } if ($ADVDBMType==2) { dbmclose (%ClickViews); } else { untie %ClickViews; } } } &ADVLockClose (CLICKVIEWS, "dupclicks.txt"); } unless ($DoNotLog) { if ($ADVLogIP) { &ADVLockOpen (IPLOG,"$subdir/$ADVdisplayad.$ADVmon$ADVmday.log","a"); unless ($ADVlockerror) { print IPLOG "$ADVhour:$ADVmin E $ENV{'REMOTE_HOST'}\n"; } &ADVLockClose (IPLOG,"$subdir/$ADVdisplayad.$ADVmon$ADVmday.log"); } $ADVexposures++; } @ADVimage = split(/\|/,$ADVimage); $imagecount = @ADVimage; srand(); $ADVdisplayimage = @ADVimage[int(rand($imagecount))]; $ADVdisplayimage =~ s//$ADVtime/g; if ($NonSSI) { if ($ENV{'PERLXS'} eq "PerlIS") { print "HTTP/1.0 302 Moved\n"; } else { print "Status: 302 Moved\n"; } if ($ADVUseCookies) { $ADVCookieValue = "$ADVdisplayad $ADVtime"; foreach $char ('\%','\+','\;','\,','\=','\&','\:\:','\s') { $ADVCookieName =~ s/$char/$Cookie_Encode_Chars{$char}/g; $ADVCookieValue =~ s/$char/$Cookie_Encode_Chars{$char}/g; } print "Set-Cookie: $ADVCookieName=$ADVCookieValue\n"; } else { &ADVLockOpen (NONSSILOG,"nonssi.log","a"); unless ($ADVlockerror) { print NONSSILOG "$ADVtime $TrimmedIP "; print NONSSILOG "$ADVCookieName | $ADVdisplayad\n"; } &ADVLockClose (NONSSILOG,"nonssi.log"); } print "Cache-Control: no-cache\n"; print "Pragma: no-cache\n"; print "Expires: Thu, 31 Dec 1998 11:59:59 GMT\n"; print "Location: $ADVdisplayimage"; if ($GraphicTimestamp) { print "?$ADVtime"; } print "\n\n"; } elsif ($ADVraw && !(($ADVraw =~ /