Skip to content

Commit

Permalink
Making lookup.php PHP7 and IPv6 friendly.
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus-Go committed Dec 15, 2019
1 parent 8780aa8 commit 5261c4a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 39 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ you can <a
href='https://github.com/Markus-Go/ip-countryside/blob/downloads/ip2country.zip?raw=true'>download
it here</a> (updated weekly).</b>

A demo can be found <a href='https://www.goldiges.de/ip-countryside/' target='_BLANK'>here</a>.

Detailed Information
--------------------

Expand Down Expand Up @@ -91,7 +93,7 @@ Copyright/ License/ Credits
---------------------------

Copyright 2006-2007 Deutsches Forschungszentrum fuer Kuenstliche Intelligenz
Copyright 2008-2015 Markus Goldstein
Copyright 2008-2020 Markus Goldstein

This is free software. Licensed under the [Apache License, Version 2.0](LICENSE.txt).
There is NO WARRANTY, to the extent permitted by law.
Expand Down
81 changes: 43 additions & 38 deletions lookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Copyright 2007 Deutsches Forschungszentrum fuer Kuenstliche Intelligenz
* or its licensors, as applicable.
* Copyright 2008-2015 Markus Goldstein
* Copyright 2008-2019 Markus Goldstein
*
* You may not use this file except under the terms of the accompanying license.
*
Expand All @@ -21,47 +21,53 @@
*
*/

if (!isset($_GET["ip"] ))
if (!isset($_GET["ip"] )) {
$ip = $_SERVER["REMOTE_ADDR"];
else
// if IPv6, replace by server IP
if (!filter_var($ip, FILTER_VALIDATE_IP,FILTER_FLAG_IPV4)) {
$ip = "37.221.195.97"; // $_SERVER["SERVER_ADDR"];
}
} else {
$ip = strip_tags(trim($_GET["ip"]));
}

$dlText = "The database is generated weekly. It can be dowloaded as a <a href=\"https://github.com/Markus-Go/ip-countryside/raw/downloads/ip2country.zip\">zip-file</a>.<br>";
$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++;
if (file_exists("ip2country.zip")) {
$last_modified = date("l, dS F Y, h:i a", filemtime("ip2country.zip"));
$db_size = ceil(filesize("ip2country.zip")/1024);
$dlText = "The database is generated weekly and was last created on " . $last_modified . " (CEST).<br> It can be dowloaded as a <a href=\"ip2country.zip\">zip-file</a> (" . $db_size . " kb).<br/>";
}
$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;
while(!feof($file)) {
$line = fgets($file);
if (substr_count($line," ") == 2) {
list($start_ip[], $end_ip[], $country[]) = explode(" ",$line);
}
}

$isValid = filter_var($ip, FILTER_VALIDATE_IP,FILTER_FLAG_IPV4);
if ($isValid) {
list($a, $b, $c, $d) = explode(".",$ip);
$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);
}

if ($error_code == 0) {
$cn = $country[binSearch(0,$count-1,$ipvalue)];
$cn_name = "Unknown";
$flag = strtolower($cn);
$handle = fopen ("countries.txt","r");
if ($isValid) {
$cn = $country[binSearch(0,count($start_ip),$ipvalue)];
$cn_name = "Unknown";
$flag = strtolower($cn);
$handle = fopen ("countries.txt","r") or die("Unable to open countries.txt!");

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

fclose($file);
}
fclose($file);
}

function binSearch($start,$end,$search) {
global $start_ip, $end_ip;
Expand Down Expand Up @@ -94,17 +100,16 @@ function ip2int($ip) {
<H1>Country Lookup Demo</H1>
<p>
Here we provide an on-line demo of our <a href="https://github.com/Markus-Go/ip-countryside/" target="_BLANK">ip-countryside</a> project.<br>
The database is generated weekly and was last created on <?php print $last_modified; ?> (CEST).<br>
It can be dowloaded as a <a href="ip2country.zip">zip-file</a> (<?php echo $db_size; ?> kb).
< For more details please contact <a href="https://www.goldiges.de/contact" target="_BLANK">Markus Goldstein</a>.
<?php print $dlText ?>
For more details please contact <a href="https://www.goldiges.de/contact" target="_BLANK">Markus Goldstein</a>.
</p>
<div align="center">
<form method="get" action="<?php echo $_SERVER['SCRIPT_NAME'];?>">
<font face="Arial" size="2"> IP Address: <input type="text" size="30" maxlength="15" name="ip" value="<?php print $ip; ?>"> </font><br />
<input type="submit" value="Find">
</form>
</div>
</br></br>
<br/><br/>


<table width="600" border="1" cellspacing="0" cellpadding="3" align="center">
Expand All @@ -116,7 +121,7 @@ function ip2int($ip) {
<td><font face="Arial" size="2">Country Flag:</font></td>
<td><font face="Arial" size="2"><b>
<?php
if ($error_code == 1 || empty($cn) ) {
if (!$isValid || empty($cn) ) {
print ("-");
}
else
Expand All @@ -128,7 +133,7 @@ function ip2int($ip) {
<td><font face="Arial" size="2">ISO Country Code: </font></td>
<td><font face="Arial" size="2"><b>
<?php
if ($error_code == 1 ||empty($cn)) {
if (!$isValid ||empty($cn)) {
print ("-");
}
else
Expand All @@ -140,7 +145,7 @@ function ip2int($ip) {
<td><font face="Arial" size="2">Country:</font> </td>
<td><font face="Arial" size="2"><b>
<?php
if ($error_code == 1 || empty($cn)) {
if (!$isValid || empty($cn)) {
print ("-");
}
else
Expand All @@ -152,7 +157,7 @@ function ip2int($ip) {
<td><font face="Arial" size="2">Comment: </font> </td>
<td><font face="Arial" size="2"><b>
<?php
if ($error_code == 1) {
if (!$isValid ) {
print ($comment);
}
else if (empty($cn))
Expand Down

0 comments on commit 5261c4a

Please sign in to comment.