From 2322b57f463826c32e46c99bc5b7f8b8b3722f94 Mon Sep 17 00:00:00 2001 From: Stefan Schantl Date: Wed, 2 Nov 2016 14:33:34 +0100 Subject: [PATCH] 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 --- config/cfgroot/geoip-functions.pl | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) 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; -- 2.39.5