]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/geoip-block.cgi
captive: One month is only 30 days instead of 210
[ipfire-2.x.git] / html / cgi-bin / geoip-block.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2014 IPFire Developemnt Team <info@ipfire.org> #
6 # #
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. #
11 # #
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. #
16 # #
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/>. #
19 # #
20 ###############################################################################
21
22 use strict;
23 # enable only the following on debugging purpose
24 #use warnings;
25 #use CGI::Carp 'fatalsToBrowser';
26
27 require '/var/ipfire/general-functions.pl';
28 require "${General::swroot}/geoip-functions.pl";
29 require "${General::swroot}/lang.pl";
30 require "${General::swroot}/header.pl";
31 require "/usr/lib/firewall/firewall-lib.pl";
32
33 my $notice;
34 my $settingsfile = "${General::swroot}/firewall/geoipblock";
35
36 my %color = ();
37 my %mainsettings = ();
38 my %settings = ();
39 my %cgiparams = ();
40
41 # Read configuration file.
42 &General::readhash("$settingsfile", \%settings);
43
44 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
45 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
46
47 &Header::showhttpheaders();
48
49 #Get GUI values
50 &Header::getcgihash(\%cgiparams);
51
52 # Call subfunction to get all available locations.
53 my @locations = &fwlib::get_geoip_locations();
54
55 if ($cgiparams{'ACTION'} eq $Lang::tr{'save'}) {
56 # Check if we want to disable geoipblock.
57 if (exists $cgiparams{'GEOIPBLOCK_ENABLED'}) {
58 $settings{'GEOIPBLOCK_ENABLED'} = "on";
59 } else {
60 $settings{'GEOIPBLOCK_ENABLED'} = "off";
61 }
62
63 # Loop through our locations array to prevent from
64 # non existing countries or code.
65 foreach my $cn (@locations) {
66 # Check if blocking for this country should be enabled/disabled.
67 if (exists $cgiparams{$cn}) {
68 $settings{$cn} = "on";
69 } else {
70 $settings{$cn} = "off";
71 }
72 }
73
74 &General::writehash("$settingsfile", \%settings);
75
76 # Mark the firewall config as changed.
77 &General::firewall_config_changed();
78
79 # Assign reload notice. We directly can use
80 # the notice from p2p block.
81 $notice = $Lang::tr{'p2p block save notice'};
82 }
83
84 &Header::openpage($Lang::tr{'geoipblock configuration'}, 1, '');
85
86 # Print notice that a firewall reload is required.
87 if ($notice) {
88 &Header::openbox('100%', 'left', $Lang::tr{'notice'});
89 print "<font class='base'>$notice</font>";
90 &Header::closebox();
91 }
92
93 # Checkbox pre-selection.
94 my $checked;
95 if ($settings{'GEOIPBLOCK_ENABLED'} eq "on") {
96 $checked = "checked='checked'";
97 }
98
99 # Print box to enable/disable geoipblock.
100 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
101
102 &Header::openbox('100%', 'center', $Lang::tr{'geoipblock'});
103 print <<END;
104 <table width='95%'>
105 <tr>
106 <td width='25%' class='base'>$Lang::tr{'geoipblock enable feature'}
107 <td><input type='checkbox' name='GEOIPBLOCK_ENABLED' $checked></td>
108 </tr>
109 <tr>
110 <td colspan='2'><br></td>
111 </tr>
112 </table>
113
114 <hr>
115
116 <table width='95%'>
117 <tr>
118 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}'></td>
119 </tr>
120 </table>
121 END
122
123 &Header::closebox();
124
125 &Header::openbox('100%', 'center', $Lang::tr{'geoipblock block countries'});
126 ### JAVA SCRIPT ###
127 print <<END;
128 <script>
129 // Function to allow checking all checkboxes at once.
130 function check_all() {
131 \$("#countries").find(":checkbox").prop("checked", true);
132 }
133
134 function uncheck_all() {
135 \$("#countries").find(":checkbox").prop("checked", false);
136 }
137 </script>
138
139 <table width='95%' class='tbl' id="countries">
140 <tr>
141 <td width='5%' align='center' bgcolor='$color{'color20'}'></td>
142 <td width='5%' align='center' bgcolor='$color{'color20'}'>
143 <b>$Lang::tr{'flag'}</b>
144 </td>
145 <td width='5%' align='center' bgcolor='$color{'color20'}'>
146 <b>$Lang::tr{'countrycode'}</b>
147 </td>
148 <td with='35%' align='left' bgcolor='$color{'color20'}'>
149 <b>$Lang::tr{'country'}</b>
150 </td>
151
152 <td width='5%' bgcolor='$color{'color20'}'>&nbsp;</td>
153
154 <td width='5%' align='center' bgcolor='$color{'color20'}'></td>
155 <td width='5%' align='center' bgcolor='$color{'color20'}'>
156 <b>$Lang::tr{'flag'}</b>
157 </td>
158 <td width='5%' align='center' bgcolor='$color{'color20'}'>
159 <b>$Lang::tr{'countrycode'}</b>
160 </td>
161 <td with='35%' align='left' bgcolor='$color{'color20'}'>
162 <b>$Lang::tr{'country'}</b>
163 </td>
164 </tr>
165 END
166
167 my $lines;
168 my $lines2;
169 my $col;
170 foreach my $location (@locations) {
171 # Country code in upper case. (DE)
172 my $ccode_uc = $location;
173
174 # County code in lower case. (de)
175 my $ccode_lc = lc($location);
176
177 # Full name of the country based on the country code.
178 my $cname = &GeoIP::get_full_country_name($ccode_lc);
179
180 # Get flag icon for of the country.
181 my $flag_icon = &GeoIP::get_flag_icon($ccode_uc);
182
183 my $flag;
184 # Check if a flag for the country is available.
185 if ($flag_icon) {
186 $flag="<img src='$flag_icon' alt='$ccode_uc' title='$ccode_uc'>";
187 } else {
188 $flag="<b>N/A</b>";
189 }
190
191 # Checkbox pre-selection.
192 my $checked;
193 if ($settings{$ccode_uc} eq "on") {
194 $checked = "checked='checked'";
195 }
196
197 # Colour lines.
198 if ($lines % 2) {
199 $col="bgcolor='$color{'color20'}'";
200 } else {
201 $col="bgcolor='$color{'color22'}'";
202 }
203
204 # Grouping elements.
205 my $line_start;
206 my $line_end;
207 if ($lines2 % 2) {
208 # Increase lines (background color by once.
209 $lines++;
210
211 # Add empty column in front.
212 $line_start="<td $col>&nbsp;</td>";
213
214 # When the line number can be diveded by "2",
215 # we are going to close the line.
216 $line_end="</tr>";
217 } else {
218 # When the line number is not divideable by "2",
219 # we are starting a new line.
220 $line_start="<tr>";
221 $line_end;
222 }
223
224 print "$line_start<td align='center' $col><input type='checkbox' name='$ccode_uc' $checked></td>\n";
225 print "<td align='center' $col>$flag</td>\n";
226 print "<td align='center' $col>$ccode_uc</td>\n";
227 print "<td align='left' $col>$cname</td>$line_end\n";
228
229 # Finish column when the last element in the array has passed and we have an uneven amount of items.
230 if(! ($lines2 % 2) && ($location eq $locations[-1] )) {
231 print "<td $col>&nbsp;</td>\n";
232 print "<td $col>&nbsp;</td>\n";
233 print "<td $col>&nbsp;</td>\n";
234 print "<td $col>&nbsp;</td>\n";
235 print "<td $col>&nbsp;</td></tr>\n";
236 }
237
238 $lines2++;
239 }
240
241 print <<END;
242 </table>
243
244 <table width='95%'>
245 <tr>
246 <td align='right'>
247 <a href="javascript:check_all()">$Lang::tr{'check all'}</a> /
248 <a href="javascript:uncheck_all()">$Lang::tr{'uncheck all'}</a>
249 </td>
250 </tr>
251 <tr>
252 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}'></td>
253 </tr>
254 </table>
255
256 <hr>
257
258 <table width='70%'>
259 <tr>
260 <td width='5%'><img src='/images/on.gif'></td>
261 <td>$Lang::tr{'geoipblock country is blocked'}</td>
262 <td width='5%'><img src='/images/off.gif'></td>
263 <td>$Lang::tr{'geoipblock country is allowed'}</td>
264 </tr>
265 </table>
266 END
267
268 &Header::closebox();
269 print"</form>\n";
270
271 &Header::closebigbox();
272 &Header::closepage();