From: Stefan Schantl Date: Wed, 2 Nov 2016 13:33:34 +0000 (+0100) Subject: geoip-functions: Add get_ccode_by_address function. X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2322b57f463826c32e46c99bc5b7f8b8b3722f94;p=people%2Fstevee%2Fipfire-2.x.git geoip-functions: Add get_ccode_by_address function. This function can be used to obtain the country code for a given IP-address. Signed-off-by: Stefan Schantl --- diff --git a/config/cfgroot/geoip-functions.pl b/config/cfgroot/geoip-functions.pl index fc2dfdd342..08acb5da06 100644 --- a/config/cfgroot/geoip-functions.pl +++ b/config/cfgroot/geoip-functions.pl @@ -24,6 +24,7 @@ package GeoIP; use Locale::Codes::Country; +use Geo::IP::PurePerl; # Function to get the flag icon for a specified country code. sub get_flag_icon($) { @@ -102,4 +103,22 @@ sub get_full_country_name($) { return $name; } +# Function to get the country code for a given IP-address. +sub get_ccode_by_address ($) { + my ($address) = @_; + + # Use the GeoIP perl module to obtain the + # corresponding country code. + my $gi = Geo::IP::PurePerl->new(); + + # Grab the country code for the given address. + my $ccode = $gi->country_code_by_name($address); + + # Convert the country code to upper case format. + my $fcode = uc($ccode); + + # Retun the obtained country code. + return $fcode; +} + 1;