]> git.ipfire.org Git - people/stevee/ipfire-2.x.git/commitdiff
geoip-functions: Add get_ccode_by_address function.
authorStefan Schantl <stefan.schantl@ipfire.org>
Wed, 2 Nov 2016 13:33:34 +0000 (14:33 +0100)
committerStefan Schantl <stefan.schantl@ipfire.org>
Wed, 2 Nov 2016 13:33:34 +0000 (14:33 +0100)
This function can be used to obtain the country code for
a given IP-address.

Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
config/cfgroot/geoip-functions.pl

index fc2dfdd3424b4f63a814ed0323899a6c67b2e06e..08acb5da06d43d902a28b7ab8247a172731f0c1d 100644 (file)
@@ -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;