#!/usr/bin/perl

#<ENC>47</ENC>

package fmspm;

########################################################################
# COPYRIGHT NOTICE:
#
# Copyright 2008 FocalMedia.Net All Rights Reserved.
#
# Selling the code for this program without prior written consent 
# from FocalMedia.Net is expressly forbidden. You may not 
# redistribute this program in any shape or form.
# 
# This program is distributed "as is" and without warranty of any
# kind, either express or implied. In no event shall the liability 
# of FocalMedia.Net for any damages, losses and/or causes of action 
# exceed the total amount paid by the user for this software.
#
########################################################################

#### EDIT HERE -- FOR WINDOWS/IIS BASED INSTALLATIONS ONLY #######

#use FindBin;
#use lib $FindBin::Bin;

use CGI::Carp qw(fatalsToBrowser);
use CGI;


require Exporter;

use vars qw/@ISA @EXPORT @EXPORT_OK $copyright $prgname/;

@ISA = qw(Exporter);

@EXPORT = qw(
				 check_spamb); 

@EXPORT_OK = qw(
					);

$q = CGI->new;

$default_permissions = 0777;


#####################################



sub check_spamb
{

my ($psscript, $headerinclude, $datadirectory) = @_;


$spbotsettings = &get_file_contents10("$datadirectory/spamconf.dat");
($spambotblock, $scriptloads, $banminutes) = split (/::/, $spbotsettings);


if ($spambotblock eq "enabled")
{

$alltimes = &get_file_contents10("$datadirectory/ipspbltmp/$ipaddress");

$nr_of_execs_per_minute = $scriptloads;
$secs_to_ban = (60 * $banminutes);

### IF LOADING # NUMBER OF PAGES IN UNDER 1 MINUTE BAN IP

if ((-e "$datadirectory/ipspbltmp") < 1)
	{
	mkdir ("$datadirectory/ipspbltmp", 0777);
	$stat = chmod($default_permissions,"$datadirectory/ipspbltmp","$datadirectory/ipspbltmp");
	}


### CLEAR OUT OLD IP ADDRESSES
$ipaddress = $ENV{'REMOTE_ADDR'};
$ntime = time();

	opendir(DDIR,"$datadirectory/ipspbltmp");
	@afiles = readdir(DDIR);
	foreach $filenamea (@afiles)
		{
			if (($filenamea ne ".") and ($filenamea ne ".."))
			{
			($device,$inode,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat("$datadirectory/ipspbltmp/$filenamea");
				if ($mtime < ($ntime - $secs_to_ban))
					{
					$stat = unlink ("$datadirectory/ipspbltmp/$filenamea");
					}
			}
		}
	closedir (DDIR);


####

if ((-e "$datadirectory/ipspbltmp/$ipaddress") > 0)
	{
	
	($device,$inode,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat("$datadirectory/ipspbltmp/$ipaddress");
	
		if (($mtime + 31) > $ntime) ### IF FILE IS UNDER 1 MINUTE OLD
			{
				
				### CHECK IF THIS IP SHOULD BE BANNED
				$alltimes = &get_file_contents10("$datadirectory/ipspbltmp/$ipaddress");
				if ($alltimes =~ /BANNED/)
					{
					if ($headerinclude eq "header") { print "Content-type: text/html\n\n"; }
					print &get_file_contents10("$datadirectory/baniptemplate.html");
					exit;
					}
				
				$timecounter = 0;
				@alltime_items = split (/\n/, $alltimes);
				
				foreach $time_item (@alltime_items)
					{
					#if (($mtime + 31) > $ntime) {$timecounter++;}
					if (($time_item + 31) > $ntime) {$timecounter++;}
					}
					if ($timecounter > $nr_of_execs_per_minute)
						{
						open (RCFILE, ">> $datadirectory/ipspbltmp/$ipaddress");
							print RCFILE "\nBANNED from $psscript";
						close (RCFILE);

						($sec,$min,$hour,$mday,$mon,$year,$wday,$ydat,$isdst) = localtime($ntime);
						$year = "20" . substr($year, 1, 2); $mon++;
						
						open (SPAMLOG, ">> $datadirectory/spam.log"); 
							print SPAMLOG "Banned IP $ipaddress at $year-$mon-$mday $hour:$min:$sec from: $psscript\n";
						close (SPAMLOG);
						
						if ($headerinclude eq "header") { print "Content-type: text/html\n\n"; }
						print &get_file_contents10("$datadirectory/baniptemplate.html");
						exit;
						
						}

				### RECORD HIT
				open (RCFILE, ">> $datadirectory/ipspbltmp/$ipaddress");
					print RCFILE $ntime . "\n";
				close (RCFILE);
			
			}
			else
			{
				$alltimes = &get_file_contents10("$datadirectory/ipspbltmp/$ipaddress");
				if ($alltimes =~ /BANNED/)
					{
					if ($headerinclude eq "header") { print "Content-type: text/html\n\n"; }
					print &get_file_contents10("$datadirectory/baniptemplate.html");
					exit;
					}
			
			unlink ("$datadirectory/ipspbltmp/$ipaddress");
			open (WFFILE, "> $datadirectory/ipspbltmp/$ipaddress");
				print WFFILE $ntime . "\n";
			close (WFILE);
			}
	}
	else
	{
	open (WFFILE, "> $datadirectory/ipspbltmp/$ipaddress");
		print WFFILE $ntime . "\n";
	close (WFILE);
	}

}
}





sub get_file_contents10
{

my ($filename) = @_;
my ($filesize, $filesize, $thefile);

if ((-e "$filename") > 0) 
	{
$filesize = (-s "$filename");
open (TFILECNTS, "$filename");
	read(TFILECNTS,$thefile,$filesize);
close (TFILECNTS);
	}

return ($thefile);

}


sub print_ban
{

print "Your IP Address have been banned due to excessive usage. If you received this message in error then please contact the Administrator of this web site.";
exit;

}

1;