]>
git.ipfire.org Git - people/mfischer/ipfire-2.x.git/blob - config/cfgroot/geoip-functions.pl
2 ############################################################################
4 # This file is part of the IPFire Firewall. #
6 # IPFire is free software; you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation; either version 2 of the License, or #
9 # (at your option) any later version. #
11 # IPFire is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
16 # You should have received a copy of the GNU General Public License #
17 # along with IPFire; if not, write to the Free Software #
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #
20 # Copyright (C) 2015 IPFire Team <info@ipfire.org>. #
22 ############################################################################
26 use Geo
::IP
::PurePerl
;
27 use Locale
::Codes
::Country
;
29 my $geoip_database_dir = "/var/lib/GeoIP";
30 my $location_database = "GeoLite2-Country-Locations-en.csv";
37 # Load the database into memory if not already done
39 $database = Geo
::IP
::PurePerl
->new(GEOIP_MEMORY_CACHE
);
42 # Return the name of the country
43 return $database->country_code_by_name($address);
46 # Function to get the flag icon for a specified country code.
47 sub get_flag_icon
($) {
50 # Webserver's root dir. (Required for generating full path)
51 my $webroot = "/srv/web/ipfire/html";
53 # Directory which contains the flag icons.
54 my $flagdir = "/images/flags";
56 # File extension of the country flags.
62 # Convert given country code to upper case.
63 my $ccode = uc($input);
65 # Generate filename, based on the contry code in lower case
66 # and the defined file extension.
67 my $file = join('.', $ccode,$ext);
69 # Generate path inside webroot to the previously generated file.
70 my $flag_icon = join('/', $flagdir,$file);
72 # Generate absolute path to the icon file.
73 my $absolute_path = join('', $webroot,$flag_icon);
75 # Check if the a icon file exists.
76 if (-e
"$absolute_path") {
77 # Return content of flag_icon.
80 # If no icon for the specified country exists, try to use
81 # the icon for "unknown".
82 my $ccode = "unknown";
84 # Redoing all the stuff from above for the "unknown" icon.
85 my $file = join('.', $ccode, $ext);
86 my $flag_icon = join('/', $flagdir, $file);
87 my $absolute_path = join('', $webroot, $flag_icon);
89 # Check if the icon is present.
90 if (-e
"$absolute_path") {
91 # Return "unknown" icon.
97 # Function to get the county name by a given country code.
98 sub get_full_country_name
($) {
102 # Remove whitespaces.
105 # Convert input into lower case format.
106 my $code = lc($input);
108 # Handle country codes which are not in the list.
109 if ($code eq "a1") { $name = "Anonymous Proxy" }
110 elsif ($code eq "a2") { $name = "Satellite Provider" }
111 elsif ($code eq "o1") { $name = "Other Country" }
112 elsif ($code eq "ap") { $name = "Asia/Pacific Region" }
113 elsif ($code eq "eu") { $name = "Europe" }
114 elsif ($code eq "yu") { $name = "Yugoslavia" }
116 # Use perl built-in module to get the country code.
117 $name = &Locale
::Codes
::Country
::code2country
($code);
123 # Function to get all available GeoIP locations.
124 sub get_geoip_locations
() {
127 # Open the location database.
128 open(LOCATION
, "$geoip_database_dir/$location_database") or return @locations;
130 # Loop through the file.
131 while(my $line = <LOCATION
>) {
135 # Split the line content.
136 my ($geoname_id, $locale_code, $continent_code, $continent_name, $country_iso_code, $country_name, $is_in_european_union) = split(/\,/, $line);
138 # Check if the country_iso_code is upper case.
139 if($country_iso_code =~ /[A-Z]/) {
140 # Add the current ISO code.
141 push(@locations, $country_iso_code);
148 # Sort locations array in alphabetical order.
149 my @sorted_locations = sort(@locations);
152 return @sorted_locations;