]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/geoip-block.cgi
geoip-block.cgi: Requires firewall-lib.pl.
[people/pmueller/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}/lang.pl";
29 require "${General::swroot}/header.pl";
30 require "/usr/lib/firewall/firewall-lib.pl";
31
32 # Directory which contains flag icons.
33 my $flagdir = "/srv/web/ipfire/html/images/flags";
34 # File extension of the country flags.
35 my $extension = "png";
36
37 my $notice;
38 my $settingsfile = "${General::swroot}/firewall/geoipblock";
39
40 my %color = ();
41 my %mainsettings = ();
42 my %settings = ();
43 my %cgiparams = ();
44
45 # Read configuration file.
46 &General::readhash("$settingsfile", \%settings);
47
48 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
49 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
50
51 &Header::showhttpheaders();
52
53 #Get GUI values
54 &Header::getcgihash(\%cgiparams);
55
56 # Call subfunction to get all available locations.
57 my @locations = &fwlib::get_geoip_locations();
58
59 if ($cgiparams{'ACTION'} eq $Lang::tr{'save'}) {
60 # Check if we want to disable geoipblock.
61 if (exists $cgiparams{'GEOIPBLOCK_ENABLED'}) {
62 $settings{'GEOIPBLOCK_ENABLED'} = "on";
63 } else {
64 $settings{'GEOIPBLOCK_ENABLED'} = "off";
65 }
66
67 # Loop through our locations array to prevent from
68 # non existing countries or code.
69 foreach my $cn (@locations) {
70 # Check if blocking for this country should be enabled/disabled.
71 if (exists $cgiparams{$cn}) {
72 $settings{$cn} = "on";
73 } else {
74 $settings{$cn} = "off";
75 }
76 }
77
78 &General::writehash("$settingsfile", \%settings);
79
80 # Mark the firewall config as changed.
81 &General::firewall_config_changed();
82
83 # Assign reload notice. We directly can use
84 # the notice from p2p block.
85 $notice = $Lang::tr{'p2p block save notice'};
86 }
87
88 &Header::openpage($Lang::tr{'geoipblock configuration'}, 1, '');
89
90 # Print notice that a firewall reload is required.
91 if ($notice) {
92 &Header::openbox('100%', 'left', $Lang::tr{'notice'});
93 print "<font class='base'>$notice</font>";
94 &Header::closebox();
95 }
96
97 # Checkbox pre-selection.
98 my $checked;
99 if ($settings{'GEOIPBLOCK_ENABLED'} eq "on") {
100 $checked = "checked='checked'";
101 }
102
103 # Print box to enable/disable geoipblock.
104 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
105
106 &Header::openbox('100%', 'center', $Lang::tr{'geoipblock'});
107 print <<END;
108 <table width='95%'>
109 <tr>
110 <td width='25%' class='base'>$Lang::tr{'geoipblock enable feature'}
111 <td><input type='checkbox' name='GEOIPBLOCK_ENABLED' $checked></td>
112 </tr>
113 <tr>
114 <td colspan='2'><br></td>
115 </tr>
116 </table>
117
118 <hr>
119
120 <table width='95%'>
121 <tr>
122 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}'></td>
123 </tr>
124 </table>
125 END
126
127 &Header::closebox();
128
129 &Header::openbox('100%', 'center', $Lang::tr{'geoipblock block countries'});
130 ### JAVA SCRIPT ###
131 print <<END;
132 <script>
133 // Function to allow checking all checkboxes at once.
134 function check_all() {
135 \$("#countries").find(":checkbox").prop("checked", true);
136 }
137
138 function uncheck_all() {
139 \$("#countries").find(":checkbox").prop("checked", false);
140 }
141 </script>
142
143 <table width='95%' class='tbl' id="countries">
144 <tr>
145 <td width='5%' align='center' bgcolor='$color{'color20'}'></td>
146 <td width='5%' align='center' bgcolor='$color{'color20'}'>
147 <b>$Lang::tr{'flag'}</b>
148 </td>
149 <td width='5%' align='center' bgcolor='$color{'color20'}'>
150 <b>$Lang::tr{'countrycode'}</b>
151 </td>
152 <td with='35%' align='left' bgcolor='$color{'color20'}'>
153 <b>$Lang::tr{'country'}</b>
154 </td>
155
156 <td width='5%' bgcolor='$color{'color20'}'>&nbsp;</td>
157
158 <td width='5%' align='center' bgcolor='$color{'color20'}'></td>
159 <td width='5%' align='center' bgcolor='$color{'color20'}'>
160 <b>$Lang::tr{'flag'}</b>
161 </td>
162 <td width='5%' align='center' bgcolor='$color{'color20'}'>
163 <b>$Lang::tr{'countrycode'}</b>
164 </td>
165 <td with='35%' align='left' bgcolor='$color{'color20'}'>
166 <b>$Lang::tr{'country'}</b>
167 </td>
168 </tr>
169 END
170
171 my $lines;
172 my $lines2;
173 my $col;
174 foreach my $location (@locations) {
175 # Country code in upper case. (DE)
176 my $ccode_uc = $location;
177
178 # County code in lower case. (de)
179 my $ccode_lc = lc($location);
180
181 # Full name of the country based on the country code.
182 my $cname = &General::get_full_country_name($ccode_lc);
183
184 # Generate flag filename, based on the lower case written contry code
185 # and the defined file extension of the image files. (de.png)
186 my $flagfile = join('.', $ccode_lc,$extension);
187
188 # Generate the full path to the flagfile, based on the given path and
189 # the previously generated filename.
190 my $flagpath = join('/', $flagdir,$flagfile);
191
192 my $flag;
193 # Check if a flag for the country is available.
194 if (-e "$flagpath") {
195 $flag="<img src='/images/flags/$flagfile' alt='$ccode_uc' title='$ccode_uc'>";
196 } else {
197 $flag="<b>N/A</b>";
198 }
199
200 # Checkbox pre-selection.
201 my $checked;
202 if ($settings{$ccode_uc} eq "on") {
203 $checked = "checked='checked'";
204 }
205
206 # Colour lines.
207 if ($lines % 2) {
208 $col="bgcolor='$color{'color20'}'";
209 } else {
210 $col="bgcolor='$color{'color22'}'";
211 }
212
213 # Grouping elements.
214 my $line_start;
215 my $line_end;
216 if ($lines2 % 2) {
217 # Increase lines (background color by once.
218 $lines++;
219
220 # Add empty column in front.
221 $line_start="<td $col>&nbsp;</td>";
222
223 # When the line number can be diveded by "2",
224 # we are going to close the line.
225 $line_end="</tr>";
226 } else {
227 # When the line number is not divideable by "2",
228 # we are starting a new line.
229 $line_start="<tr>";
230 $line_end;
231 }
232
233 print "$line_start<td align='center' $col><input type='checkbox' name='$ccode_uc' $checked></td>\n";
234 print "<td align='center' $col>$flag</td>\n";
235 print "<td align='center' $col>$ccode_uc</td>\n";
236 print "<td align='left' $col>$cname</td>$line_end\n";
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();