]>
git.ipfire.org Git - 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 ############################################################################
27 use Locale
::Codes
::Country
;
29 # Hash which contains country codes and their names which are special or not
31 my %not_iso_3166_location = (
32 "a1" => "Anonymous Proxy",
33 "a2" => "Satellite Provider",
34 "a3" => "Worldwide Anycast Instance",
35 "an" => "Netherlands Antilles",
36 "ap" => "Asia/Pacific Region",
38 "fx" => "France, Metropolitan",
39 "o1" => "Other Country",
43 # Directory where the libloc database and keyfile lives.
44 our $location_dir = "/usr/share/location/";
46 # Libloc database file.
47 our $database = "$location_dir/database.db";
49 # Libloc keyfile to verify the database.
50 our $keyfile = "$location_dir/signing-key.pem";
52 # Directory which contains the exported databases.
53 our $xt_geoip_db_directory = "/usr/share/xt_geoip/";
56 ## Tiny function to init the location database.
59 # Init and open the database.
60 my $db = &Location
::init
($database);
62 # Return the database handle.
67 ## Function to verify the integrity of the location database.
72 # Verify the integrity of the database.
73 if(&Location
::verify
($db_handle, $keyfile)) {
74 # Success, return "1".
78 # If we got here, return nothing.
83 ## Function to the the country code of a given address.
85 sub lookup_country_code
($$) {
86 my ($db_handle, $address) = @_;
88 # Lookup the given address.
89 my $country_code = &Location
::lookup_country_code
($db_handle, $address);
91 # Return the name of the country
95 # Function to get the flag icon for a specified country code.
96 sub get_flag_icon
($) {
99 # Webserver's root dir. (Required for generating full path)
100 my $webroot = "/srv/web/ipfire/html";
102 # Directory which contains the flag icons.
103 my $flagdir = "/images/flags";
105 # File extension of the country flags.
108 # Remove whitespaces.
111 # Convert given country code to upper case.
112 my $ccode = uc($input);
114 # Generate filename, based on the contry code in lower case
115 # and the defined file extension.
116 my $file = join('.', $ccode,$ext);
118 # Generate path inside webroot to the previously generated file.
119 my $flag_icon = join('/', $flagdir,$file);
121 # Generate absolute path to the icon file.
122 my $absolute_path = join('', $webroot,$flag_icon);
124 # Check if the a icon file exists.
125 if (-e
"$absolute_path") {
126 # Return content of flag_icon.
129 # If no icon for the specified country exists, try to use
130 # the icon for "unknown".
131 my $ccode = "unknown";
133 # Redoing all the stuff from above for the "unknown" icon.
134 my $file = join('.', $ccode, $ext);
135 my $flag_icon = join('/', $flagdir, $file);
136 my $absolute_path = join('', $webroot, $flag_icon);
138 # Check if the icon is present.
139 if (-e
"$absolute_path") {
140 # Return "unknown" icon.
146 # Function to get the county name by a given country code.
147 sub get_full_country_name
($) {
151 # Remove whitespaces.
155 # Convert input into lower case format.
156 my $code = lc($input);
158 # Handle country codes which are not in the list.
159 if ($not_iso_3166_location{$code}) {
160 # Grab location name from hash.
161 $name = $not_iso_3166_location{$code};
163 # Use perl built-in module to get the country code.
164 $name = &Locale
::Codes
::Country
::code2country
($code);
170 # Function to get all available GeoIP locations.
171 sub get_geoip_locations
() {
174 # Get listed country codes from ISO 3166-1.
175 @locations = &Locale
::Codes
::Country
::all_country_codes
();
177 # Add locations from not_iso_3166_locations.
178 foreach my $location (keys %not_iso_3166_location) {
179 push(@locations, $location);
182 # Sort locations array in alphabetical order.
183 my @sorted_locations = sort(@locations);
186 return @sorted_locations;