<?php 

/**
 * Return true if the request ip belongs to appnexus
 * @return boolean
 */
 
/* include_once 'lib/php/include/db.php';

class appnexus_ips_mCache extends Memcache {
    static private $instance = null;
    //static private $port = 11211;
    static private $port = 22123;
    static function mGet() {
        if(self::$instance == null) {
            self::$instance = new Memcache;
            //self::$instance->addServer('192.168.30.32', self::$port, false);
            self::$instance->addServer('127.0.0.1', self::$port);
        }
        return self::$instance;
    }
} */

if(!function_exists('is_appnexus_ip')) {

    function is_appnexus_ip()
    {
        $remote = $_SERVER['HTTP_X_FORWARDED_FOR'];
        //$fwd = $_SERVER['HTTP_X_FORWARDED_FOR'];
        
        $t = explode(".",$remote);
        $ip_check = $t[0].".".$t[1].".";
        
        $ip_blacklist = array("68.67.", "50.22.", "8.10.", "8.12.", "8.19.", "37.252.", "64.208.", "64.210.", "103.243.", "104.254.", "185.33.", "204.13.", "52.1.", "78.109.");
        if(in_array($ip_check, $ip_blacklist)) {           
            $redis = new Redis();
            $redis->connect("127.0.0.1", 22121);
            $redis->hIncrBy("apnx_ip_".date("Ymd"), "ip", 1);
            return true;
        }
        
        return false;
        
        //disable zip code blocking - it's blocking way too many impressions. 
        //################# IMP: IF ENABLING AGAIN, uncomment cache and include above ####################
        $mkey = md5($remote);
        if(!$blocked_ip = appnexus_ips_mCache::mGet()->get($mkey)){
            $db = connectToReadProxy();
            $selectsql = "select postal from envoyip4 where inet_aton('$remote') <= endip limit 1;";
    		if ($result = mysql_query($selectsql,$db)){
    			if($row=mysql_fetch_assoc($result)){
    				$postal = $row['postal'];
    			}
    		}
    		if($postal){
    		    //check if blacklisted
    		    $sql = "SELECT * from `rep_appnexus_postal` where postal='$postal' LIMIT 1";
    		    $postal_mkey = md5($sql);
    		    if(!$blocked_ip = appnexus_ips_mCache::mGet()->get($postal_mkey)){
                    $res = mysql_query($sql, $db);
                    if ($res && mysql_num_rows($res) == 1)
                        $blocked_ip = "BLOCKED";
                    else
                        $blocked_ip = "NONE";
                    appnexus_ips_mCache::mGet()->set($postal_mkey, $blocked_ip, 0, 60*60*24);
                }
    		}
    		else
    		    $blocked_ip = "NONE";
            mysql_close($db);
            appnexus_ips_mCache::mGet()->set($mkey, $blocked_ip, 0, 60*60*24);
        }
        
        if($blocked_ip == "BLOCKED"){
            $redis = new Redis();
            $redis->connect("127.0.0.1", 22121);
            $redis->hIncrBy("apnx_ip_".date("Ymd"), "zip", 1);
            return true;
        }
        
        
        return false;
    }
}

?>