X-Git-Url: http://git.ipfire.org/?p=ipfire-2.x.git;a=blobdiff_plain;f=config%2Fcfgroot%2Fgeoip-functions.pl;h=c8ff47d2e78f879dcbbddd964d6ccc3e1a579f0f;hp=623169eaf8d5f473b0831a33ed639fe64a2ea082;hb=0ca3baedfc623a968c7e38c356352b4f64c9f695;hpb=b1ad5b8b6c0d974e63eccd5ec7ecd24894bc8283 diff --git a/config/cfgroot/geoip-functions.pl b/config/cfgroot/geoip-functions.pl index 623169eaf8..c8ff47d2e7 100644 --- a/config/cfgroot/geoip-functions.pl +++ b/config/cfgroot/geoip-functions.pl @@ -23,8 +23,23 @@ package GeoIP; +use Geo::IP::PurePerl; use Locale::Codes::Country; +my $database; + +sub lookup($) { + my $address = shift; + + # Load the database into memory if not already done + if (!$database) { + $database = Geo::IP::PurePerl->new(GEOIP_MEMORY_CACHE); + } + + # Return the name of the country + return $database->country_code_by_name($address); +} + # Function to get the flag icon for a specified country code. sub get_flag_icon($) { my ($input) = @_; @@ -102,4 +117,37 @@ sub get_full_country_name($) { return $name; } +# Function to get all available GeoIP locations. +sub get_geoip_locations() { + my @locations; + + # 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 = ) { + # 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 country_iso_code is upper case. + if($country_iso_code =~ /[A-Z]/) { + # Add the current ISO code. + push(@locations, $country_iso_code); + } + } + + # Close filehandle. + close(LOCATION); + + # Sort locations array in alphabetical order. + my @sorted_locations = sort(@locations); + + # Return the array.. + return @sorted_locations; +} + + 1;