<?php

$filter="N";

if ($_SERVER["HTTP_X_FORWARDED_FOR"]){
    $proxy=cjoverkill_sanityze($_SERVER["REMOTE_ADDR"]);
    $filter_ip=cjoverkill_sanityze($_SERVER["HTTP_X_FORWARDED_FOR"]);
}
else {
    $filter_ip=cjoverkill_sanityze($_SERVER["REMOTE_ADDR"]);
    $proxy="";
}

$filter_url="";
$filter_cntry="";
$filter_method=cjoverkill_sanityze($_SERVER["REQUEST_METHOD"]);
$filter_client=cjoverkill_sanityze($_SERVER["HTTP_USER_AGENT"]);
$whitelisted=0;
$nofilter=$_GET["nofilter"];
// If no filter disabling from the command prompt, then filter
if ($nofilter!="1"){
    $sql=@mysql_query("SELECT filter_url_default, country_filter, client_filter, method_filter, ip_filter  
			FROM cjoverkill_settings") OR
      cjoverkill_db_error(mysql_error());
    $tmp=@mysql_fetch_array($sql);
    extract($tmp);
    
    // Stage 0 --> Check if this one is whitelisted.
    $whitelisted=0;
    $sqlwhite=@mysql_query("SELECT COUNT(*) AS cntwhite FROM
			     cjoverkill_filter_ipwhite WHERE
			     INET_ATON('$filter_ip')>=ip_from AND
			     INET_ATON('$filter_ip')<=ip_from
			     ") OR
      cjoverkill_db_error(mysql_error());
    $tmp=@mysql_fetch_array($sqlwhite);
    extract($tmp);
    if ($cntwhite!=0){
	$whitelisted=1;
    }
    
    // Stage 1 --> Filter per IP and request method
    if ($method_filter==1 && $whitelisted!=1) {
	$sql1=@mysql_query("SELECT COUNT(*) AS cnt FROM
			     cjoverkill_filter_method, cjoverkill_filter_ip WHERE
		     (INET_ATON('$filter_ip')>=cjoverkill_filter_ip.ip_from AND
		      INET_ATON('$filter_ip')<=cjoverkill_filter_ip.ip_to) OR
		     (cjoverkill_filter_method.method LIKE '%$filter_method%' AND
		      cjoverkill_filter_method.allow!='1')
		     ") OR
	  cjoverkill_db_error(mysql_error());
	$tmp=@mysql_fetch_array($sql1);
	extract($tmp);
	if ($cnt!=0){
	    header("Location: $filter_url_default");
	    cjoverkill_disconnect();
	    exit;
	}
    }
    
    // Stage 2 --> Filter by client
    if ($client_filter==1 && $whitelisted!=1){
	$sql2=@mysql_query("SELECT client FROM cjoverkill_filter_client") OR
	  cjoverkill_db_error(mysql_error());
	while ($tmp2=@mysql_fetch_array($sql2)){
	    extract($tmp2);
	    $val= ".?".$client.".?";
	    if (eregi ($val, $filter_client)) {
		header("Location: $filter_url_default");
		cjoverkill_disconnect();
		exit;
	    }
	}
    }
    // Stage 3 --> Filter per country + Get country for the country stats
    if ($country_filter==1){
	$sql3=@mysql_query("SELECT cjoverkill_filter_country.url AS filter_url,
			     cjoverkill_filter_country.c2code AS country_code,
			     cjoverkill_filter_country.filter AS country_filter_db
			     FROM cjoverkill_filter_country, cjoverkill_filter_base WHERE
			     INET_ATON('$filter_ip')>=cjoverkill_filter_base.ip_from AND
			     INET_ATON('$filter_ip')<=cjoverkill_filter_base.ip_to AND
			     cjoverkill_filter_base.c2code=cjoverkill_filter_country.c2code") OR
	  cjoverkill_db_error(mysql_error());
	$tmp=@mysql_fetch_array($sql3);
	if (@mysql_num_rows($sql3)>0) {
	    extract($tmp);
	    if ($country_filter_db=="1" && $whitelisted!=1) {
		header("Location: $filter_url");
		cjoverkill_disconnect();
		exit;
	    }
	}
	else {
	    $country_code="00";
	}
    }
}
    

?>
