]>
git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/geoip-block.cgi
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2014 IPFire Developemnt Team <info@ipfire.org> #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
23 # enable only the following on debugging purpose
25 #use CGI::Carp 'fatalsToBrowser';
27 require '/var/ipfire/general-functions.pl';
28 require "${General::swroot}/lang.pl";
29 require "${General::swroot}/header.pl";
31 # Directory which contains flag icons.
32 my $flagdir = "/srv/web/ipfire/html/images/flags";
33 # File extension of the country flags.
34 my $extension = "png";
36 my $settingsfile = "${General::swroot}/firewall/geoipblock";
39 my %mainsettings = ();
43 # Read configuration file.
44 &General
::readhash
("$settingsfile", \
%settings);
46 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
47 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
49 &Header
::showhttpheaders
();
52 &Header
::getcgihash
(\
%cgiparams);
54 # Call subfunction to get all available locations.
55 my @locations = &get_geoip_locations
();
57 if ($cgiparams{'ACTION'} eq $Lang::tr
{'save'}) {
58 # Check if we want to disable geoipblock.
59 if (exists $cgiparams{'GEOIPBLOCK_ENABLED'}) {
60 $settings{'GEOIPBLOCK_ENABLED'} = "on";
62 $settings{'GEOIPBLOCK_ENABLED'} = "off";
65 # Loop through our locations array to prevent from
66 # non existing countries or code.
67 foreach my $cn (@locations) {
68 # Check if blocking for this country should be enabled/disabled.
69 if (exists $cgiparams{$cn}) {
70 $settings{$cn} = "on";
72 $settings{$cn} = "off";
76 &General
::writehash
("$settingsfile", \
%settings);
78 # &General::firewall_config_changed();
80 # $notice = $Lang::tr{'p2p block save notice'};
83 &Header
::openpage
($Lang::tr
{'geoipblock configuration'}, 1, '');
85 # Checkbox pre-selection.
87 if ($settings{'GEOIPBLOCK_ENABLED'} eq "on") {
88 $checked = "checked='checked'";
91 # Print box to enable/disable geoipblock.
92 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
94 &Header
::openbox
('100%', 'center', $Lang::tr
{'geoipblock'});
98 <td width='25%' class='base'>$Lang::tr{'geoipblock enable feature'}
99 <td><input type='checkbox' name='GEOIPBLOCK_ENABLED' $checked></td>
102 <td colspan='2'><br></td>
110 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}'></td>
117 &Header
::openbox
('100%', 'center', $Lang::tr
{'geoipblock block countries'});
121 // Function to allow checking all checkboxes at once.
122 function check_all() {
123 \$("#countries").find(":checkbox").prop("checked", true);
126 function uncheck_all() {
127 \$("#countries").find(":checkbox").prop("checked", false);
131 <table width='95%' class='tbl' id="countries">
133 <td width='5%' align='center' bgcolor='$color{'color20'}'></td>
134 <td width='5%' align='center' bgcolor='$color{'color20'}'>
135 <b>$Lang::tr{'flag'}</b>
137 <td width='5%' align='center' bgcolor='$color{'color20'}'>
138 <b>$Lang::tr{'countrycode'}</b>
140 <td with='35%' align='left' bgcolor='$color{'color20'}'>
141 <b>$Lang::tr{'country'}</b>
144 <td width='5%' bgcolor='$color{'color20'}'> </td>
146 <td width='5%' align='center' bgcolor='$color{'color20'}'></td>
147 <td width='5%' align='center' bgcolor='$color{'color20'}'>
148 <b>$Lang::tr{'flag'}</b>
150 <td width='5%' align='center' bgcolor='$color{'color20'}'>
151 <b>$Lang::tr{'countrycode'}</b>
153 <td with='35%' align='left' bgcolor='$color{'color20'}'>
154 <b>$Lang::tr{'country'}</b>
162 foreach my $location (@locations) {
163 # Country code in upper case. (DE)
164 my $ccode_uc = $location;
166 # County code in lower case. (de)
167 my $ccode_lc = lc($location);
169 # Full name of the country based on the country code.
170 my $cname = &General
::get_full_country_name
($ccode_lc);
172 # Generate flag filename, based on the lower case written contry code
173 # and the defined file extension of the image files. (de.png)
174 my $flagfile = join('.', $ccode_lc,$extension);
176 # Generate the full path to the flagfile, based on the given path and
177 # the previously generated filename.
178 my $flagpath = join('/', $flagdir,$flagfile);
181 # Check if a flag for the country is available.
182 if (-e
"$flagpath") {
183 $flag="<img src='/images/flags/$flagfile' alt='$ccode_uc' title='$ccode_uc'>";
188 # Checkbox pre-selection.
190 if ($settings{$ccode_uc} eq "on") {
191 $checked = "checked='checked'";
196 $col="bgcolor='$color{'color20'}'";
198 $col="bgcolor='$color{'color22'}'";
205 # Increase lines (background color by once.
208 # Add empty column in front.
209 $line_start="<td $col> </td>";
211 # When the line number can be diveded by "2",
212 # we are going to close the line.
215 # When the line number is not divideable by "2",
216 # we are starting a new line.
221 print "$line_start<td align='center' $col><input type='checkbox' name='$ccode_uc' $checked></td>\n";
222 print "<td align='center' $col>$flag</td>\n";
223 print "<td align='center' $col>$ccode_uc</td>\n";
224 print "<td align='left' $col>$cname</td>$line_end\n";
235 <a href="javascript:check_all()">$Lang::tr{'check all'}</a> /
236 <a href="javascript:uncheck_all()">$Lang::tr{'uncheck all'}</a>
240 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}'></td>
248 <td width='5%'><img src='/images/on.gif'></td>
249 <td>$Lang::tr{'geoipblock country is blocked'}</td>
250 <td width='5%'><img src='/images/off.gif'></td>
251 <td>$Lang::tr{'geoipblock country is allowed'}</td>
259 &Header
::closebigbox
();
260 &Header
::closepage
();
262 sub get_geoip_locations
() {
263 # Path to the directory which contains the binary geoip
265 my $directory="/usr/share/xt_geoip/BE";
267 # Array with the final contry codes list.
270 # Open location and do a directory listing.
271 opendir(DIR
, "$directory");
272 my @locations = readdir(DIR
);
275 # Loop through the directory listing, and cut of the file extensions.
276 foreach my $location (sort @locations) {
278 next if($location =~ /^\.$/);
279 next if($location =~ /^\.\.$/);
281 # Remove whitespaces.
284 # Cut-off file extension.
285 my ($contry_code, $extension) = split(/\./, $location);
287 # Add country code to array.
288 push(@contry_codes, $contry_code);
291 return @contry_codes;