]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - 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
CommitLineData
1dcd8715
SS
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
24package GeoIP;
25
0ca3baed 26use Geo::IP::PurePerl;
b013822b 27use Locale::Codes::Country;
1dcd8715 28
d499e86b
MT
29my $geoip_database_dir = "/var/lib/GeoIP";
30my $location_database = "GeoLite2-Country-Locations-en.csv";
31
0ca3baed 32my $database;
00793c27 33
e34dbea7
SS
34# Hash which contains country codes and their names which are special or not
35# part of ISO 3166-1.
36my %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
00793c27
MT
48sub lookup($) {
49 my $address = shift;
50
0ca3baed
SS
51 # Load the database into memory if not already done
52 if (!$database) {
53 $database = Geo::IP::PurePerl->new(GEOIP_MEMORY_CACHE);
00793c27
MT
54 }
55
0ca3baed
SS
56 # Return the name of the country
57 return $database->country_code_by_name($address);
00793c27
MT
58}
59
1dcd8715
SS
60# Function to get the flag icon for a specified country code.
61sub 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
dfbee171
SS
76 # Convert given country code to upper case.
77 my $ccode = uc($input);
1dcd8715
SS
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;
dfbee171
SS
93 } else {
94 # If no icon for the specified country exists, try to use
95 # the icon for "unknown".
96 my $ccode = "unknown";
97
b1ad5b8b
MT
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);
dfbee171
SS
102
103 # Check if the icon is present.
104 if (-e "$absolute_path") {
105 # Return "unknown" icon.
106 return $flag_icon;
107 }
1dcd8715
SS
108 }
109}
110
111# Function to get the county name by a given country code.
112sub 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.
e34dbea7
SS
123 if ($not_iso_3166_location{$code}) {
124 # Grab location name from hash.
125 $name = $not_iso_3166_location{$code};
126 } else {
1dcd8715 127 # Use perl built-in module to get the country code.
b013822b 128 $name = &Locale::Codes::Country::code2country($code);
1dcd8715
SS
129 }
130
131 return $name;
132}
133
30c59cbb
SS
134# Function to get all available GeoIP locations.
135sub get_geoip_locations() {
abe21498 136 my @locations = ();
30c59cbb 137
e34dbea7
SS
138 # Get listed country codes from ISO 3166-1.
139 @locations = &Locale::Codes::Country::all_country_codes();
30c59cbb 140
e34dbea7
SS
141 # Add locations from not_iso_3166_locations.
142 foreach my $location (keys %not_iso_3166_location) {
143 push(@locations, $location);
30c59cbb
SS
144 }
145
30c59cbb
SS
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
1dcd8715 1541;