]> git.ipfire.org Git - ipfire-2.x.git/blame - config/cfgroot/geoip-functions.pl
geoip-functions: Adjust for new flag-icons and usage of "unknown" icon.
[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
b013822b 26use Locale::Codes::Country;
1dcd8715
SS
27
28# Function to get the flag icon for a specified country code.
29sub get_flag_icon($) {
30 my ($input) = @_;
31
32 # Webserver's root dir. (Required for generating full path)
33 my $webroot = "/srv/web/ipfire/html";
34
35 # Directory which contains the flag icons.
36 my $flagdir = "/images/flags";
37
38 # File extension of the country flags.
39 my $ext = "png";
40
41 # Remove whitespaces.
42 chomp($input);
43
dfbee171
SS
44 # Convert given country code to upper case.
45 my $ccode = uc($input);
1dcd8715
SS
46
47 # Generate filename, based on the contry code in lower case
48 # and the defined file extension.
49 my $file = join('.', $ccode,$ext);
50
51 # Generate path inside webroot to the previously generated file.
52 my $flag_icon = join('/', $flagdir,$file);
53
54 # Generate absolute path to the icon file.
55 my $absolute_path = join('', $webroot,$flag_icon);
56
57 # Check if the a icon file exists.
58 if (-e "$absolute_path") {
59 # Return content of flag_icon.
60 return $flag_icon;
dfbee171
SS
61 } else {
62 # If no icon for the specified country exists, try to use
63 # the icon for "unknown".
64 my $ccode = "unknown";
65
66 # Redoing all the stuff from abouve for the "unknown" icon.
67 my $file = join('.', $ccode,$ext);
68 my $flag_icon = join('/', $flagdir,$file);
69 my $absolute_path = join('', $webroot,$flag_icon);
70
71 # Check if the icon is present.
72 if (-e "$absolute_path") {
73 # Return "unknown" icon.
74 return $flag_icon;
75 }
1dcd8715
SS
76 }
77}
78
79# Function to get the county name by a given country code.
80sub get_full_country_name($) {
81 my ($input) = @_;
82 my $name;
83
84 # Remove whitespaces.
85 chomp($input);
86
87 # Convert input into lower case format.
88 my $code = lc($input);
89
90 # Handle country codes which are not in the list.
91 if ($code eq "a1") { $name = "Anonymous Proxy" }
92 elsif ($code eq "a2") { $name = "Satellite Provider" }
93 elsif ($code eq "o1") { $name = "Other Country" }
94 elsif ($code eq "ap") { $name = "Asia/Pacific Region" }
95 elsif ($code eq "eu") { $name = "Europe" }
96 elsif ($code eq "yu") { $name = "Yugoslavia" }
97 else {
98 # Use perl built-in module to get the country code.
b013822b 99 $name = &Locale::Codes::Country::code2country($code);
1dcd8715
SS
100 }
101
102 return $name;
103}
104
1051;