]>
git.ipfire.org Git - people/pmueller/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";
34 # Hash which contains country codes and their names which are special or not
36 my %not_iso_3166_location = (
37 "a1" => "Anonymous Proxy",
38 "a2" => "Satellite Provider",
39 "a3" => "Worldwide Anycast Anstance",
40 "an" => "Netherlands Antilles",
41 "ap" => "Asia/Pacific Region",
43 "fx" => "France, Metropolitan",
44 "o1" => "Other Country",
51 # Load the database into memory if not already done
53 $database = Geo
::IP
::PurePerl
->new(GEOIP_MEMORY_CACHE
);
56 # Return the name of the country
57 return $database->country_code_by_name($address);
60 # Function to get the flag icon for a specified country code.
61 sub get_flag_icon
($) {
64 # Webserver's root dir. (Required for generating full path)
65 my $webroot = "/srv/web/ipfire/html";
67 # Directory which contains the flag icons.
68 my $flagdir = "/images/flags";
70 # File extension of the country flags.
76 # Convert given country code to upper case.
77 my $ccode = uc($input);
79 # Generate filename, based on the contry code in lower case
80 # and the defined file extension.
81 my $file = join('.', $ccode,$ext);
83 # Generate path inside webroot to the previously generated file.
84 my $flag_icon = join('/', $flagdir,$file);
86 # Generate absolute path to the icon file.
87 my $absolute_path = join('', $webroot,$flag_icon);
89 # Check if the a icon file exists.
90 if (-e
"$absolute_path") {
91 # Return content of flag_icon.
94 # If no icon for the specified country exists, try to use
95 # the icon for "unknown".
96 my $ccode = "unknown";
98 # Redoing all the stuff from above for the "unknown" icon.
99 my $file = join('.', $ccode, $ext);
100 my $flag_icon = join('/', $flagdir, $file);
101 my $absolute_path = join('', $webroot, $flag_icon);
103 # Check if the icon is present.
104 if (-e
"$absolute_path") {
105 # Return "unknown" icon.
111 # Function to get the county name by a given country code.
112 sub get_full_country_name
($) {
116 # Remove whitespaces.
119 # Convert input into lower case format.
120 my $code = lc($input);
122 # Handle country codes which are not in the list.
123 if ($not_iso_3166_location{$code}) {
124 # Grab location name from hash.
125 $name = $not_iso_3166_location{$code};
127 # Use perl built-in module to get the country code.
128 $name = &Locale
::Codes
::Country
::code2country
($code);
134 # Function to get all available GeoIP locations.
135 sub get_geoip_locations
() {
138 # Get listed country codes from ISO 3166-1.
139 @locations = &Locale
::Codes
::Country
::all_country_codes
();
141 # Add locations from not_iso_3166_locations.
142 foreach my $location (keys %not_iso_3166_location) {
143 push(@locations, $location);
146 # Sort locations array in alphabetical order.
147 my @sorted_locations = sort(@locations);
150 return @sorted_locations;