]> git.ipfire.org Git - ipfire-2.x.git/blobdiff - config/cfgroot/geoip-functions.pl
geoip-functions.pl: Re-write code to lookup the iso country code of a given IP-address.
[ipfire-2.x.git] / config / cfgroot / geoip-functions.pl
index 68b6f503d78019d8afc7d9f08277744d70b8b96e..e8ce8377f583190dec3a1292faa80d8b1a91a4a6 100644 (file)
 
 package GeoIP;
 
-use Locale::Country;
+require '/var/ipfire/network-functions.pl';
+
+use Locale::Codes::Country;
+
+# Path where all the GeoIP related databases are stored.
+my $geoip_database_dir = "/var/lib/GeoIP";
+
+# Database which contains all IPv4 networks.
+my $address_ipv4_database = "GeoLite2-Country-Blocks-IPv4.csv";
+
+# Database wich contains the locations data.
+my $location_database = "GeoLite2-Country-Locations-en.csv";
+
+sub lookup($) {
+       my $address = shift;
+       my $location_id;
+       my $country_code;
+
+       # Check if the given address is valid.
+       unless(&Network::check_ip_address($address)) {
+               return;
+       }
+
+       # Open the address database.
+       open(ADDRESS, "$geoip_database_dir/$address_ipv4_database") or die "Could not open $geoip_database_dir/$address_ipv4_database. $!\n";
+
+       # Loop through the file.
+       while(my $line = <ADDRESS>) {
+               # Remove newlines.
+               chomp($line);
+
+               # Split the line content.
+               my ($network, $geoname_id, $registered_country_geoname_id, $represented_country_geoname_id, $is_anonymous_proxy, $is_satellite_provider) = split(/\,/, $line);
+
+               # Check if the given address is part of the current processed network.
+               if (&Network::ip_address_in_network($address, $network)) {
+                       # Store the geoname_id for this address.
+                       $location_id = $geoname_id;
+
+                       # Break loop.
+                       last;
+               }
+       }
+
+       # Return nothing if no location_id could be found.
+       return unless($location_id);
+
+       # Close filehandle.
+       close(ADDRESS);
+
+       # Open the location database.
+       open(LOCATION, "$geoip_database_dir/$location_database") or die "Could not open $geoip_database_dir/$location_database. $!\n";
+
+       # Loop through the file.
+       while(my $line = <LOCATION>) {
+               # Remove newlines.
+               chomp($line);
+
+               # Split the line content.
+               my ($geoname_id, $locale_code, $continent_code, $continent_name, $country_iso_code, $country_name, $is_in_european_union) = split(/\,/, $line);
+
+               # Check if the correct location_id has been found.
+               if ($geoname_id eq $location_id) {
+                       # Store the county code.
+                       $country_code = $country_iso_code;
+
+                       # Break loop.
+                       last;
+               }
+       }
+
+       # Close filehandle.
+       close(LOCATION);
+
+       # Return the obtained country code.
+       return $country_code;
+}
 
 # Function to get the flag icon for a specified country code.
 sub get_flag_icon($) {
@@ -41,8 +117,8 @@ sub get_flag_icon($) {
        # Remove whitespaces.
        chomp($input);
 
-       # Convert given country code to lower case.
-       my $ccode = lc($input);
+       # Convert given country code to upper case.
+       my $ccode = uc($input);
 
        # Generate filename, based on the contry code in lower case
        # and the defined file extension.
@@ -58,6 +134,21 @@ sub get_flag_icon($) {
        if (-e "$absolute_path") {
                # Return content of flag_icon.
                return $flag_icon;
+       } else {
+               # If no icon for the specified country exists, try to use
+               # the icon for "unknown".
+               my $ccode = "unknown";
+
+               # Redoing all the stuff from above for the "unknown" icon.
+               my $file = join('.', $ccode, $ext);
+               my $flag_icon = join('/', $flagdir, $file);
+               my $absolute_path = join('', $webroot, $flag_icon);
+
+               # Check if the icon is present.
+               if (-e "$absolute_path") {
+                       # Return "unknown" icon.
+                       return $flag_icon;
+               }
        }
 }
 
@@ -81,7 +172,7 @@ sub get_full_country_name($) {
        elsif ($code eq "yu") { $name = "Yugoslavia" }
        else {
                # Use perl built-in module to get the country code.
-               $name = &Locale::Country::code2country($code);
+               $name = &Locale::Codes::Country::code2country($code);
        }
 
        return $name;