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