]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - config/cfgroot/geoip-functions.pl
geoip-locations.pl: Rework method to grab and handling GeoIP locations.
[people/pmueller/ipfire-2.x.git] / config / cfgroot / geoip-functions.pl
1 #!/usr/bin/perl -w
2 ############################################################################
3 # #
4 # This file is part of the IPFire Firewall. #
5 # #
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. #
10 # #
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. #
15 # #
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 #
19 # #
20 # Copyright (C) 2015 IPFire Team <info@ipfire.org>. #
21 # #
22 ############################################################################
23
24 package GeoIP;
25
26 use Geo::IP::PurePerl;
27 use Locale::Codes::Country;
28
29 my $geoip_database_dir = "/var/lib/GeoIP";
30 my $location_database = "GeoLite2-Country-Locations-en.csv";
31
32 my $database;
33
34 # Hash which contains country codes and their names which are special or not
35 # part of ISO 3166-1.
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",
42 "eu" => "Europe",
43 "fx" => "France, Metropolitan",
44 "o1" => "Other Country",
45 "yu" => "Yugoslavia"
46 );
47
48 sub lookup($) {
49 my $address = shift;
50
51 # Load the database into memory if not already done
52 if (!$database) {
53 $database = Geo::IP::PurePerl->new(GEOIP_MEMORY_CACHE);
54 }
55
56 # Return the name of the country
57 return $database->country_code_by_name($address);
58 }
59
60 # Function to get the flag icon for a specified country code.
61 sub get_flag_icon($) {
62 my ($input) = @_;
63
64 # Webserver's root dir. (Required for generating full path)
65 my $webroot = "/srv/web/ipfire/html";
66
67 # Directory which contains the flag icons.
68 my $flagdir = "/images/flags";
69
70 # File extension of the country flags.
71 my $ext = "png";
72
73 # Remove whitespaces.
74 chomp($input);
75
76 # Convert given country code to upper case.
77 my $ccode = uc($input);
78
79 # Generate filename, based on the contry code in lower case
80 # and the defined file extension.
81 my $file = join('.', $ccode,$ext);
82
83 # Generate path inside webroot to the previously generated file.
84 my $flag_icon = join('/', $flagdir,$file);
85
86 # Generate absolute path to the icon file.
87 my $absolute_path = join('', $webroot,$flag_icon);
88
89 # Check if the a icon file exists.
90 if (-e "$absolute_path") {
91 # Return content of flag_icon.
92 return $flag_icon;
93 } else {
94 # If no icon for the specified country exists, try to use
95 # the icon for "unknown".
96 my $ccode = "unknown";
97
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);
102
103 # Check if the icon is present.
104 if (-e "$absolute_path") {
105 # Return "unknown" icon.
106 return $flag_icon;
107 }
108 }
109 }
110
111 # Function to get the county name by a given country code.
112 sub get_full_country_name($) {
113 my ($input) = @_;
114 my $name;
115
116 # Remove whitespaces.
117 chomp($input);
118
119 # Convert input into lower case format.
120 my $code = lc($input);
121
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};
126 } else {
127 # Use perl built-in module to get the country code.
128 $name = &Locale::Codes::Country::code2country($code);
129 }
130
131 return $name;
132 }
133
134 # Function to get all available GeoIP locations.
135 sub get_geoip_locations() {
136 my @locations = ();
137
138 # Get listed country codes from ISO 3166-1.
139 @locations = &Locale::Codes::Country::all_country_codes();
140
141 # Add locations from not_iso_3166_locations.
142 foreach my $location (keys %not_iso_3166_location) {
143 push(@locations, $location);
144 }
145
146 # Sort locations array in alphabetical order.
147 my @sorted_locations = sort(@locations);
148
149 # Return the array..
150 return @sorted_locations;
151 }
152
153
154 1;