]> git.ipfire.org Git - ipfire-2.x.git/blobdiff - config/cfgroot/geoip-functions.pl
GeoIP: Add lookup function for convenience
[ipfire-2.x.git] / config / cfgroot / geoip-functions.pl
index 68b6f503d78019d8afc7d9f08277744d70b8b96e..be50d5e142f448c4b59c9fb0df47eac5e04a2ccf 100644 (file)
 
 package GeoIP;
 
-use Locale::Country;
+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($) {
@@ -41,8 +56,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 +73,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 +111,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;