22/**
33 * Copyright 2007 Deutsches Forschungszentrum fuer Kuenstliche Intelligenz
44 * or its licensors, as applicable.
5- * Copyright 2008-2015 Markus Goldstein
5+ * Copyright 2008-2019 Markus Goldstein
66 *
77 * You may not use this file except under the terms of the accompanying license.
88 *
2121 *
2222*/
2323
24- if (!isset ($ _GET ["ip " ] ))
24+ if (!isset ($ _GET ["ip " ] )) {
2525 $ ip = $ _SERVER ["REMOTE_ADDR " ];
26- else
26+ // if IPv6, replace by server IP
27+ if (!filter_var ($ ip , FILTER_VALIDATE_IP ,FILTER_FLAG_IPV4 )) {
28+ $ ip = "37.221.195.97 " ; // $_SERVER["SERVER_ADDR"];
29+ }
30+ } else {
2731 $ ip = strip_tags (trim ($ _GET ["ip " ]));
32+ }
2833
34+ $ 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> " ;
2935$ file = fopen ("ip2country.db " , "r " ) or die ("Unable to open IP DB! " );
30- $ last_modified = date ("l, dS F Y, h:i a " , filemtime ("ip2country.zip " ));
31- $ db_size = ceil (filesize ("ip2country.zip " )/1024 );
32- $ count = 0 ;
33- while (!feof ($ file )) {
34- list ($ start_ip [$ count ], $ end_ip [$ count ], $ country [$ count ]) = explode (" " ,fgets ($ file ));
35- $ count ++;
36+ if (file_exists ("ip2country.zip " )) {
37+ $ last_modified = date ("l, dS F Y, h:i a " , filemtime ("ip2country.zip " ));
38+ $ db_size = ceil (filesize ("ip2country.zip " )/1024 );
39+ $ 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/> " ;
3640}
37- $ error_code = 0 ;
38- list ($ a , $ b , $ c , $ d ) = explode (". " ,$ ip );
3941
40- if ((is_numeric ($ a ) && is_numeric ($ b ) && is_numeric ($ c ) && is_numeric ($ d )) &&
41- (($ a >= 0 && $ a <= 255 ) && ($ b >= 0 && $ b <= 255 ) && ($ c >= 0 && $ c <= 255 ) && ($ d >= 0 && $ d <=255 ))){
42- $ ipvalue = (int )$ a *256 *256 *256 + (int )$ b *256 *256 + (int )$ c *256 + (int )$ d ;
43- }
44- else {
45- $ comment = "You entered an invalid IP address! " ;
46- fclose ($ file );
47- $ error_code = 1 ;
42+ while (!feof ($ file )) {
43+ $ line = fgets ($ file );
44+ if (substr_count ($ line ," " ) == 2 ) {
45+ list ($ start_ip [], $ end_ip [], $ country []) = explode (" " ,$ line );
4846 }
47+ }
4948
49+ $ isValid = filter_var ($ ip , FILTER_VALIDATE_IP ,FILTER_FLAG_IPV4 );
50+ if ($ isValid ) {
51+ list ($ a , $ b , $ c , $ d ) = explode (". " ,$ ip );
52+ $ ipvalue = (int )$ a *256 *256 *256 + (int )$ b *256 *256 + (int )$ c *256 + (int )$ d ;
53+ } else {
54+ $ comment = "You entered an invalid IP address! " ;
55+ fclose ($ file );
56+ }
5057
51- if ($ error_code == 0 ) {
52- $ cn = $ country [binSearch (0 ,$ count- 1 ,$ ipvalue )];
53- $ cn_name = "Unknown " ;
54- $ flag = strtolower ($ cn );
55- $ handle = fopen ("countries.txt " ,"r " );
58+ if ($ isValid ) {
59+ $ cn = $ country [binSearch (0 ,count ( $ start_ip ) ,$ ipvalue )];
60+ $ cn_name = "Unknown " ;
61+ $ flag = strtolower ($ cn );
62+ $ handle = fopen ("countries.txt " ,"r " ) or die ( " Unable to open countries.txt! " );
5663
57- while ( ($ data = fgetcsv ($ handle , 1000 , "; " )) !== FALSE ) {
58- if (trim ($ data [1 ]) == trim ($ cn )) {
59- $ cn_name = ucfirst (strtolower ($ data [0 ]));
60- }
64+ while ( ($ data = fgetcsv ($ handle , 1000 , "; " )) !== FALSE ) {
65+ if (trim ($ data [1 ]) == trim ($ cn )) {
66+ $ cn_name = ucfirst (strtolower ($ data [0 ]));
6167 }
62-
63- fclose ($ file );
6468 }
69+ fclose ($ file );
70+ }
6571
6672function binSearch ($ start ,$ end ,$ search ) {
6773 global $ start_ip , $ end_ip ;
@@ -94,17 +100,16 @@ function ip2int($ip) {
94100<H1>Country Lookup Demo</H1>
95101<p>
96102Here we provide an on-line demo of our <a href="https://github.com/Markus-Go/ip-countryside/" target="_BLANK">ip-countryside</a> project.<br>
97- The database is generated weekly and was last created on <?php print $ last_modified ; ?> (CEST).<br>
98- It can be dowloaded as a <a href="ip2country.zip">zip-file</a> (<?php echo $ db_size ; ?> kb).
99- < For more details please contact <a href="https://www.goldiges.de/contact" target="_BLANK">Markus Goldstein</a>.
103+ <?php print $ dlText ?>
104+ For more details please contact <a href="https://www.goldiges.de/contact" target="_BLANK">Markus Goldstein</a>.
100105</p>
101106<div align="center">
102107<form method="get" action="<?php echo $ _SERVER ['SCRIPT_NAME ' ];?> ">
103108<font face="Arial" size="2"> IP Address: <input type="text" size="30" maxlength="15" name="ip" value="<?php print $ ip ; ?> "> </font><br />
104109<input type="submit" value="Find">
105110</form>
106111</div>
107- </br></br >
112+ <br/><br/ >
108113
109114
110115<table width="600" border="1" cellspacing="0" cellpadding="3" align="center">
@@ -116,7 +121,7 @@ function ip2int($ip) {
116121 <td><font face="Arial" size="2">Country Flag:</font></td>
117122 <td><font face="Arial" size="2"><b>
118123 <?php
119- if ($ error_code == 1 || empty ($ cn ) ) {
124+ if (! $ isValid || empty ($ cn ) ) {
120125 print ("- " );
121126 }
122127 else
@@ -128,7 +133,7 @@ function ip2int($ip) {
128133 <td><font face="Arial" size="2">ISO Country Code: </font></td>
129134 <td><font face="Arial" size="2"><b>
130135 <?php
131- if ($ error_code == 1 ||empty ($ cn )) {
136+ if (! $ isValid ||empty ($ cn )) {
132137 print ("- " );
133138 }
134139 else
@@ -140,7 +145,7 @@ function ip2int($ip) {
140145 <td><font face="Arial" size="2">Country:</font> </td>
141146 <td><font face="Arial" size="2"><b>
142147 <?php
143- if ($ error_code == 1 || empty ($ cn )) {
148+ if (! $ isValid || empty ($ cn )) {
144149 print ("- " );
145150 }
146151 else
@@ -152,7 +157,7 @@ function ip2int($ip) {
152157 <td><font face="Arial" size="2">Comment: </font> </td>
153158 <td><font face="Arial" size="2"><b>
154159 <?php
155- if ($ error_code == 1 ) {
160+ if (! $ isValid ) {
156161 print ($ comment );
157162 }
158163 else if (empty ($ cn ))
0 commit comments