<commit_before><?php
/**
 * Copyright 2007 Deutsches Forschungszentrum fuer Kuenstliche Intelligenz 
 * or its licensors, as applicable.
 * Copyright 2008-2015 <NAME>
 *
 * You may not use this file except under the terms of the accompanying license.
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * Project: ip-countryside
 * File: lookup.php
 * Purpose: PHP lookup demo client
 * Responsible: <NAME>
 * Primary Repository: https://github.com/Markus-Go/ip-countryside/
 * Web Sites: madm.dfki.de, www.goldiges.de/ip-countryside
 *
*/

if (!isset($_GET["ip"] ))
    $ip = $_SERVER["REMOTE_ADDR"];
else
    $ip = strip_tags(trim($_GET["ip"]));

$file = fopen("ip2country.db", "r") or die("Unable to open IP DB!");
$last_modified = date("l, dS F Y, h:i a", filemtime("ip2country.zip"));
$db_size = ceil(filesize("ip2country.zip")/1024);
$count = 0;
while(!feof($file)) {
    list($start_ip[$count], $end_ip[$count], $country[$count]) = split(" ",fgets($file));
    $count++;
}
    $error_code = 0;
    list($a, $b, $c, $d) = split("\.",$ip);

    if ((is_numeric($a) && is_numeric($b) && is_numeric($c) && is_numeric($d)) && 
          (($a >= 0 && $a <= 255) && ($b >= 0 && $b <= 255) && ($c >= 0 && $c <= 255) && ($d >= 0 && $d <=255))){
            $ipvalue = (int)$a*256*256*256 + (int)$b*256*256 + (int)$c*256 + (int)$d;
        }
    else {
        $comment = "You entered an invalid IP address!";
        fclose($file);    
        $error_code = 1;
    }


    if ($error_code == 0) {
        $cn = $country[binSearch(0,$count-1,$ipvalue)];
        $cn_name = "Unknown";
        $flag = strtolower($cn); 
        $handle = fopen ("countries.txt","r");

        while ( ($data = fgetcsv ($handle, 1000, ";")) !== FALSE ) {
            if(trim($data[1]) == trim($cn)) {
                $cn_name = ucfirst(strtolower($data[0]));
            }
        }

        fclose($file);
    }

function binSearch($start,$end,$search) {
    global $start_ip, $end_ip;
    $mid = $start+(int)(($end-$start)/2);
    if ($end < $start) {
        return -1;
    }
    if ($search < (int)$start_ip[$mid]) {
        return binSearch($start, $mid-1, $search);
    }
    else if ($search > (int)$end_ip[$mid]) {
        return binSearch($mid+1, $end, $search);
      }
    else return $mid;
}

function ip2int($ip) {
    list($a, $b, $c, $d) = split("\.",$ip);
    return (int)$a*256*256*256 + (int)$b*256*256 + (int)$c*256 + (int)$d;
}

?>

<html>
<head> 
<title>Country Lookup Demo</title><commit_msg>Make `lookup.php` PHP 7 compatiple<commit_after><?php
/**
 * Copyright 2007 Deutsches Forschungszentrum fuer Kuenstliche Intelligenz 
 * or its licensors, as applicable.
 * Copyright 2008-2015 <NAME>
 *
 * You may not use this file except under the terms of the accompanying license.
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * Project: ip-countryside
 * File: lookup.php
 * Purpose: PHP lookup demo client
 * Responsible: <NAME>
 * Primary Repository: https://github.com/Markus-Go/ip-countryside/
 * Web Sites: madm.dfki.de, www.goldiges.de/ip-countryside
 *
*/

if (!isset($_GET["ip"] ))
    $ip = $_SERVER["REMOTE_ADDR"];
else
    $ip = strip_tags(trim($_GET["ip"]));

$file = fopen("ip2country.db", "r") or die("Unable to open IP DB!");
$last_modified = date("l, dS F Y, h:i a", filemtime("ip2country.zip"));
$db_size = ceil(filesize("ip2country.zip")/1024);
$count = 0;
while(!feof($file)) {
    list($start_ip[$count], $end_ip[$count], $country[$count]) = explode(" ",fgets($file));
    $count++;
}
    $error_code = 0;
    list($a, $b, $c, $d) = explode(".",$ip);

    if ((is_numeric($a) && is_numeric($b) && is_numeric($c) && is_numeric($d)) && 
          (($a >= 0 && $a <= 255) && ($b >= 0 && $b <= 255) && ($c >= 0 && $c <= 255) && ($d >= 0 && $d <=255))){
            $ipvalue = (int)$a*256*256*256 + (int)$b*256*256 + (int)$c*256 + (int)$d;
        }
    else {
        $comment = "You entered an invalid IP address!";
        fclose($file);    
        $error_code = 1;
    }


    if ($error_code == 0) {
        $cn = $country[binSearch(0,$count-1,$ipvalue)];
        $cn_name = "Unknown";
        $flag = strtolower($cn); 
        $handle = fopen ("countries.txt","r");

        while ( ($data = fgetcsv ($handle, 1000, ";")) !== FALSE ) {
            if(trim($data[1]) == trim($cn)) {
                $cn_name = ucfirst(strtolower($data[0]));
            }
        }

        fclose($file);
    }

function binSearch($start,$end,$search) {
    global $start_ip, $end_ip;
    $mid = $start+(int)(($end-$start)/2);
    if ($end < $start) {
        return -1;
    }
    if ($search < (int)$start_ip[$mid]) {
        return binSearch($start, $mid-1, $search);
    }
    else if ($search > (int)$end_ip[$mid]) {
        return binSearch($mid+1, $end, $search);
      }
    else return $mid;
}

function ip2int($ip) {
    list($a, $b, $c, $d) = explode(".",$ip);
    return (int)$a*256*256*256 + (int)$b*256*256 + (int)$c*256 + (int)$d;
}

?>

<html>
<head> 
<title>Country Lookup Demo</title>
{'fraction_non_alphanumeric': 0.19575768470249866, 'fraction_numerical': 0.03181736473126011, 'mean_word_length': 3.0672514619883042, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 27, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5657286', 'n_tokens_mistral': 2090, 'n_tokens_neox': 1920, 'n_words': 491}