function pfblockerng_uc_countries()
{
    global $g, $pfb;
    $maxmind_cont = "{$pfb['geoipshare']}/country_continent.csv";
    $maxmind_cc4 = "{$pfb['geoipshare']}/GeoIPCountryWhois.csv";
    $maxmind_cc6 = "{$pfb['geoipshare']}/GeoIPv6.csv";
    // Create folders if not exist
    $folder_array = array("{$pfb['dbdir']}", "{$pfb['logdir']}", "{$pfb['ccdir']}");
    foreach ($folder_array as $folder) {
        safe_mkdir("{$folder}", 0755);
    }
    $now = date('m/d/y G:i:s', time());
    $log = "Country code update Start [ NOW ]\n";
    if (!$g['pfblockerng_install']) {
        print "Country code update Start [ {$now} ]\n";
    }
    pfb_logger("{$log}", 3);
    if (!file_exists($maxmind_cont) || !file_exists($maxmind_cc4) || !file_exists($maxmind_cc6)) {
        $log = " [ MAXMIND UPDATE FAIL, CSV missing, using previous Country code database \n";
        if (!$g['pfblockerng_install']) {
            print $log;
        }
        pfb_logger("{$log}", 3);
        return;
    }
    // Save Date/Time stamp to MaxMind version file
    $local_tds4 = @gmdate('D, d M Y H:i:s T', @filemtime($maxmind_cc4));
    $local_tds6 = @gmdate('D, d M Y H:i:s T', @filemtime($maxmind_cc6));
    $maxmind_ver = "MaxMind GeoLite Date/Time Stamps\n";
    $maxmind_ver .= "Local_v4 \tLast-Modified: {$local_tds4}\n";
    $maxmind_ver .= "Local_v6 \tLast-Modified: {$local_tds6}\n";
    @file_put_contents("{$pfb['logdir']}/maxmind_ver", $maxmind_ver, LOCK_EX);
    // Collect ISO codes for each Continent
    $log = " Processing Continent Data\n";
    if (!$g['pfblockerng_install']) {
        print $log;
    }
    pfb_logger("{$log}", 3);
    $cont_array = array();
    if (($handle = fopen("{$maxmind_cont}", 'r')) !== FALSE) {
        while (($cc = fgetcsv($handle)) !== FALSE) {
            $cc_key = $cc[0];
            $cont_key = $cc[1];
            switch ($cont_key) {
                case 'AF':
                    $cont_array[0]['continent'] = 'Africa';
                    $cont_array[0]['iso'] .= "{$cc_key},";
                    $cont_array[0]['file4'] = "{$pfb['ccdir']}/Africa_v4.txt";
                    $cont_array[0]['file6'] = "{$pfb['ccdir']}/Africa_v6.txt";
                    break;
                case 'AS':
                    $cont_array[1]['continent'] = 'Asia';
                    $cont_array[1]['iso'] .= "{$cc_key},";
                    $cont_array[1]['file4'] = "{$pfb['ccdir']}/Asia_v4.txt";
                    $cont_array[1]['file6'] = "{$pfb['ccdir']}/Asia_v6.txt";
                    break;
                case 'EU':
                    $cont_array[2]['continent'] = 'Europe';
                    $cont_array[2]['iso'] .= "{$cc_key},";
                    $cont_array[2]['file4'] = "{$pfb['ccdir']}/Europe_v4.txt";
                    $cont_array[2]['file6'] = "{$pfb['ccdir']}/Europe_v6.txt";
                    break;
                case 'NA':
                    $cont_array[3]['continent'] = 'North America';
                    $cont_array[3]['iso'] .= "{$cc_key},";
                    $cont_array[3]['file4'] = "{$pfb['ccdir']}/North_America_v4.txt";
                    $cont_array[3]['file6'] = "{$pfb['ccdir']}/North_America_v6.txt";
                    break;
                case 'OC':
                    $cont_array[4]['continent'] = 'Oceania';
                    $cont_array[4]['iso'] .= "{$cc_key},";
                    $cont_array[4]['file4'] = "{$pfb['ccdir']}/Oceania_v4.txt";
                    $cont_array[4]['file6'] = "{$pfb['ccdir']}/Oceania_v6.txt";
                    break;
                case 'SA':
                    $cont_array[5]['continent'] = 'South America';
                    $cont_array[5]['iso'] .= "{$cc_key},";
                    $cont_array[5]['file4'] = "{$pfb['ccdir']}/South_America_v4.txt";
                    $cont_array[5]['file6'] = "{$pfb['ccdir']}/South_America_v6.txt";
                    break;
            }
        }
    }
    unset($cc);
    fclose($handle);
    // Add Maxmind Anonymous Proxy and Satellite Providers to array
    $cont_array[6]['continent'] = 'Proxy and Satellite';
    $cont_array[6]['iso'] = 'A1,A2';
    $cont_array[6]['file4'] = "{$pfb['ccdir']}/Proxy_Satellite_v4.txt";
    $cont_array[6]['file6'] = "{$pfb['ccdir']}/Proxy_Satellite_v6.txt";
    sort($cont_array);
    // Collect Country ISO data and sort to Continent arrays (IPv4 and IPv6)
    foreach (array('4', '6') as $type) {
        $log = " Processing ISO IPv{$type} Continent/Country Data\n";
        if (!$g['pfblockerng_install']) {
            print $log;
        }
        pfb_logger("{$log}", 3);
        if ($type == '4') {
            $maxmind_cc = "{$pfb['geoipshare']}/GeoIPCountryWhois.csv";
        } else {
            $maxmind_cc = "{$pfb['geoipshare']}/GeoIPv6.csv";
        }
        $iptype = "ip{$type}";
        $filetype = "file{$type}";
        if (($handle = fopen("{$maxmind_cc}", 'r')) !== FALSE) {
            while (($cc = fgetcsv($handle)) !== FALSE) {
                $cc_key = $cc[4];
                $country_key = $cc[5];
                $a_cidr = implode(',', ip_range_to_subnet_array_temp($cc[0], $cc[1]));
                foreach ($cont_array as $key => $iso) {
                    if (strpos($iso['iso'], $cc_key) !== FALSE) {
                        $cont_array[$key][$cc_key][$iptype] .= "{$a_cidr},";
                        $cont_array[$key][$cc_key]['country'] = $country_key;
                        continue;
                    }
                }
            }
        }
        unset($cc);
        fclose($handle);
        // Build Continent files
        foreach ($cont_array as $key => $iso) {
            $header = $pfb_file = $iso_key = '';
            $header .= '# Generated from MaxMind Inc. on: ' . date('m/d/y G:i:s', time()) . "\n";
            $header .= "# Continent IPv{$type}: {$cont_array[$key]['continent']}\n";
            $pfb_file = $cont_array[$key][$filetype];
            $iso_key = array_keys($iso);
            foreach ($iso_key as $ikey) {
                if (strlen($ikey) == 2) {
                    $header .= "# Country: {$iso[$ikey]['country']}\n";
                    $header .= "# ISO Code: {$ikey}\n";
                    $header .= '# Total Networks: ' . substr_count($iso[$ikey][$iptype], ',') . "\n";
                    $header .= str_replace(',', "\n", $iso[$ikey][$iptype]);
                    $iso[$ikey][$iptype] = '';
                }
            }
            @file_put_contents($pfb_file, $header, LOCK_EX);
        }
    }
}
Esempio n. 2
0
function pfblockerng_uc_countries()
{
    global $g, $pfb;
    $maxmind_cont = "{$pfb['dbdir']}/country_continent.csv";
    $maxmind_cc4 = "{$pfb['dbdir']}/GeoIPCountryWhois.csv";
    $maxmind_cc6 = "{$pfb['dbdir']}/GeoIPv6.csv";
    // Create Folders if not Exist
    $folder_array = array("{$pfb['dbdir']}", "{$pfb['logdir']}", "{$pfb['ccdir']}");
    foreach ($folder_array as $folder) {
        safe_mkdir("{$folder}", 0755);
    }
    $now = date("m/d/y G:i:s", time());
    $log = "Country Code Update Start - [ NOW ]\n\n";
    print "Country Code Update Start - [ {$now} ]\n\n";
    pfb_logger("{$log}", "3");
    if (!file_exists($maxmind_cont) || !file_exists($maxmind_cc4) || !file_exists($maxmind_cc6)) {
        $log = " [ MAXMIND UPDATE FAIL, CSV Missing, using Previous Country Code Database \n";
        print $log;
        pfb_logger("{$log}", "3");
        return;
    }
    // Save Date/Time Stamp to MaxMind version file
    $maxmind_ver = "MaxMind GeoLite Date/Time Stamps \n\n";
    $remote_tds = @implode(preg_grep("/Last-Modified/", get_headers("http://geolite.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip")));
    $maxmind_ver .= "MaxMind_v4 \t" . $remote_tds . "\n";
    $local_tds = @gmdate("D, d M Y H:i:s T", filemtime($maxmind_cc4));
    $maxmind_ver .= "Local_v4 \tLast-Modified: " . $local_tds . "\n\n";
    $remote_tds = @implode(preg_grep("/Last-Modified/", get_headers("http://geolite.maxmind.com/download/geoip/database/GeoIPv6.csv.gz")));
    $maxmind_ver .= "MaxMind_v6 \t" . $remote_tds . "\n";
    $local_tds = @gmdate("D, d M Y H:i:s T", filemtime($maxmind_cc6));
    $maxmind_ver .= "Local_v6 \tLast-Modified: " . $local_tds . "\n";
    $maxmind_ver .= "\nThese Timestamps should *match* \n";
    @file_put_contents("{$pfb['logdir']}/maxmind_ver", $maxmind_ver);
    // Collect ISO Codes for Each Continent
    $log = "Processing Continent Data\n";
    print $log;
    pfb_logger("{$log}", "3");
    $cont_array = array(array($AF), array($AS), array($EU), array($NA), array($OC), array($SA), array($AX));
    if (($handle = fopen("{$maxmind_cont}", 'r')) !== FALSE) {
        while (($cc = fgetcsv($handle)) !== FALSE) {
            $cc_key = $cc[0];
            $cont_key = $cc[1];
            switch ($cont_key) {
                case "AF":
                    $cont_array[0]['continent'] = "Africa";
                    $cont_array[0]['iso'] .= "{$cc_key},";
                    $cont_array[0]['file4'] = "{$pfb['ccdir']}/Africa_v4.txt";
                    $cont_array[0]['file6'] = "{$pfb['ccdir']}/Africa_v6.txt";
                    break;
                case "AS":
                    $cont_array[1]['continent'] = "Asia";
                    $cont_array[1]['iso'] .= "{$cc_key},";
                    $cont_array[1]['file4'] = "{$pfb['ccdir']}/Asia_v4.txt";
                    $cont_array[1]['file6'] = "{$pfb['ccdir']}/Asia_v6.txt";
                    break;
                case "EU":
                    $cont_array[2]['continent'] = "Europe";
                    $cont_array[2]['iso'] .= "{$cc_key},";
                    $cont_array[2]['file4'] = "{$pfb['ccdir']}/Europe_v4.txt";
                    $cont_array[2]['file6'] = "{$pfb['ccdir']}/Europe_v6.txt";
                    break;
                case "NA":
                    $cont_array[3]['continent'] = "North America";
                    $cont_array[3]['iso'] .= "{$cc_key},";
                    $cont_array[3]['file4'] = "{$pfb['ccdir']}/North_America_v4.txt";
                    $cont_array[3]['file6'] = "{$pfb['ccdir']}/North_America_v6.txt";
                    break;
                case "OC":
                    $cont_array[4]['continent'] = "Oceania";
                    $cont_array[4]['iso'] .= "{$cc_key},";
                    $cont_array[4]['file4'] = "{$pfb['ccdir']}/Oceania_v4.txt";
                    $cont_array[4]['file6'] = "{$pfb['ccdir']}/Oceania_v6.txt";
                    break;
                case "SA":
                    $cont_array[5]['continent'] = "South America";
                    $cont_array[5]['iso'] .= "{$cc_key},";
                    $cont_array[5]['file4'] = "{$pfb['ccdir']}/South_America_v4.txt";
                    $cont_array[5]['file6'] = "{$pfb['ccdir']}/South_America_v6.txt";
                    break;
            }
        }
    }
    unset($cc);
    fclose($handle);
    // Add Maxmind Anonymous Proxy and Satellite Providers to array
    $cont_array[6]['continent'] = "Proxy and Satellite";
    $cont_array[6]['iso'] = "A1,A2";
    $cont_array[6]['file4'] = "{$pfb['ccdir']}/Proxy_Satellite_v4.txt";
    $cont_array[6]['file6'] = "{$pfb['ccdir']}/Proxy_Satellite_v6.txt";
    // Collect Country ISO data and sort to Continent arrays (IPv4 and IPv6)
    foreach (array("4", "6") as $type) {
        $log = "Processing ISO IPv{$type} Continent/Country Data\n";
        print $log;
        pfb_logger("{$log}", "3");
        if ($type == "4") {
            $maxmind_cc = "{$pfb['dbdir']}/GeoIPCountryWhois.csv";
        } else {
            $maxmind_cc = "{$pfb['dbdir']}/GeoIPv6.csv";
        }
        $iptype = "ip{$type}";
        $filetype = "file{$type}";
        if (($handle = fopen("{$maxmind_cc}", 'r')) !== FALSE) {
            while (($cc = fgetcsv($handle)) !== FALSE) {
                $cc_key = $cc[4];
                $country_key = $cc[5];
                $a_cidr = implode(",", ip_range_to_subnet_array_temp($cc[0], $cc[1]));
                $counter = 0;
                foreach ($cont_array as $iso) {
                    if (preg_match("/\\b{$cc_key}\\b/", $iso['iso'])) {
                        $cont_array[$counter][$cc_key][$iptype] .= $a_cidr . ",";
                        $cont_array[$counter][$cc_key]['country'] = $country_key;
                        continue;
                    }
                    $counter++;
                }
            }
        }
        unset($cc);
        fclose($handle);
        // Build Continent Files
        $counter = 0;
        foreach ($cont_array as $iso) {
            $header = "";
            $pfb_file = "";
            $iso_key = "";
            $header .= "# Generated from MaxMind Inc. on: " . date("m/d/y G:i:s", time()) . "\n";
            $header .= "# Continent IPv{$type}: " . $cont_array[$counter]['continent'] . "\n";
            $pfb_file = $cont_array[$counter][$filetype];
            $iso_key = array_keys($iso);
            foreach ($iso_key as $key) {
                if (preg_match("/[A-Z]{2}|A1|A2/", $key)) {
                    $header .= "# Country: " . $iso[$key]['country'] . "\n";
                    $header .= "# ISO Code: " . $key . "\n";
                    $header .= "# Total Networks: " . substr_count($iso[$key][$iptype], ",") . "\n";
                    $header .= str_replace(",", "\n", $iso[$key][$iptype]);
                    $iso[$key][$iptype] = "";
                }
            }
            $counter++;
            @file_put_contents($pfb_file, $header, LOCK_EX);
        }
    }
}