]> git.ipfire.org Git - ipfire-2.x.git/blob - config/cfgroot/geoip-functions.pl
Remove orphaned Apache patch
[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 sub lookup($) {
35 my $address = shift;
36
37 # Load the database into memory if not already done
38 if (!$database) {
39 $database = Geo::IP::PurePerl->new(GEOIP_MEMORY_CACHE);
40 }
41
42 # Return the name of the country
43 return $database->country_code_by_name($address);
44 }
45
46 # Function to get the flag icon for a specified country code.
47 sub get_flag_icon($) {
48 my ($input) = @_;
49
50 # Webserver's root dir. (Required for generating full path)
51 my $webroot = "/srv/web/ipfire/html";
52
53 # Directory which contains the flag icons.
54 my $flagdir = "/images/flags";
55
56 # File extension of the country flags.
57 my $ext = "png";
58
59 # Remove whitespaces.
60 chomp($input);
61
62 # Convert given country code to upper case.
63 my $ccode = uc($input);
64
65 # Generate filename, based on the contry code in lower case
66 # and the defined file extension.
67 my $file = join('.', $ccode,$ext);
68
69 # Generate path inside webroot to the previously generated file.
70 my $flag_icon = join('/', $flagdir,$file);
71
72 # Generate absolute path to the icon file.
73 my $absolute_path = join('', $webroot,$flag_icon);
74
75 # Check if the a icon file exists.
76 if (-e "$absolute_path") {
77 # Return content of flag_icon.
78 return $flag_icon;
79 } else {
80 # If no icon for the specified country exists, try to use
81 # the icon for "unknown".
82 my $ccode = "unknown";
83
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);
88
89 # Check if the icon is present.
90 if (-e "$absolute_path") {
91 # Return "unknown" icon.
92 return $flag_icon;
93 }
94 }
95 }
96
97 # Function to get the county name by a given country code.
98 sub get_full_country_name($) {
99 my ($input) = @_;
100 my $name;
101
102 # Remove whitespaces.
103 chomp($input);
104
105 # Convert input into lower case format.
106 my $code = lc($input);
107
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" }
115 else {
116 # Use perl built-in module to get the country code.
117 $name = &Locale::Codes::Country::code2country($code);
118 }
119
120 return $name;
121 }
122
123 # Function to get all available GeoIP locations.
124 sub get_geoip_locations() {
125 my @locations = ();
126
127 # Open the location database.
128 open(LOCATION, "$geoip_database_dir/$location_database") or return @locations;
129
130 # Loop through the file.
131 while(my $line = <LOCATION>) {
132 # Remove newlines.
133 chomp($line);
134
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);
137
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);
142 }
143 }
144
145 # Close filehandle.
146 close(LOCATION);
147
148 # Sort locations array in alphabetical order.
149 my @sorted_locations = sort(@locations);
150
151 # Return the array..
152 return @sorted_locations;
153 }
154
155
156 1;