]>
Commit | Line | Data |
---|---|---|
91634dbe SS |
1 | #!/usr/bin/perl |
2 | ############################################################################### | |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
1367738d | 5 | # Copyright (C) 2007-2020 IPFire Team <info@ipfire.org> # |
91634dbe SS |
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'; | |
273618da | 28 | require "${General::swroot}/location-functions.pl"; |
91634dbe SS |
29 | require "${General::swroot}/lang.pl"; |
30 | require "${General::swroot}/header.pl"; | |
192a8266 | 31 | require "/usr/lib/firewall/firewall-lib.pl"; |
91634dbe | 32 | |
593c3227 | 33 | my $notice; |
273618da | 34 | my $settingsfile = "${General::swroot}/firewall/locationblock"; |
91634dbe SS |
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); | |
8186b372 | 45 | &General::readhash("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \%color); |
91634dbe SS |
46 | |
47 | &Header::showhttpheaders(); | |
48 | ||
49 | #Get GUI values | |
50 | &Header::getcgihash(\%cgiparams); | |
51 | ||
52 | # Call subfunction to get all available locations. | |
273618da | 53 | my @locations = &Location::Functions::get_locations(); |
91634dbe SS |
54 | |
55 | if ($cgiparams{'ACTION'} eq $Lang::tr{'save'}) { | |
273618da SS |
56 | # Check if we want to disable locationblock. |
57 | if (exists $cgiparams{'LOCATIONBLOCK_ENABLED'}) { | |
58 | $settings{'LOCATIONBLOCK_ENABLED'} = "on"; | |
91634dbe | 59 | } else { |
273618da | 60 | $settings{'LOCATIONBLOCK_ENABLED'} = "off"; |
91634dbe SS |
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 | ||
593c3227 SS |
76 | # Mark the firewall config as changed. |
77 | &General::firewall_config_changed(); | |
78 | ||
705e9489 MF |
79 | # Assign reload notice. |
80 | $notice = $Lang::tr{'fw rules reload notice'}; | |
91634dbe SS |
81 | } |
82 | ||
273618da | 83 | &Header::openpage($Lang::tr{'locationblock configuration'}, 1, ''); |
91634dbe | 84 | |
593c3227 SS |
85 | # Print notice that a firewall reload is required. |
86 | if ($notice) { | |
87 | &Header::openbox('100%', 'left', $Lang::tr{'notice'}); | |
88 | print "<font class='base'>$notice</font>"; | |
89 | &Header::closebox(); | |
90 | } | |
91 | ||
91634dbe SS |
92 | # Checkbox pre-selection. |
93 | my $checked; | |
273618da | 94 | if ($settings{'LOCATIONBLOCK_ENABLED'} eq "on") { |
91634dbe SS |
95 | $checked = "checked='checked'"; |
96 | } | |
97 | ||
273618da | 98 | # Print box to enable/disable locationblock. |
91634dbe SS |
99 | print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n"; |
100 | ||
273618da | 101 | &Header::openbox('100%', 'center', $Lang::tr{'locationblock'}); |
91634dbe SS |
102 | print <<END; |
103 | <table width='95%'> | |
104 | <tr> | |
1367738d | 105 | <td width='50%' class='base'>$Lang::tr{'locationblock enable feature'} |
273618da | 106 | <td><input type='checkbox' name='LOCATIONBLOCK_ENABLED' $checked></td> |
91634dbe SS |
107 | </tr> |
108 | <tr> | |
109 | <td colspan='2'><br></td> | |
110 | </tr> | |
111 | </table> | |
112 | ||
113 | <hr> | |
114 | ||
115 | <table width='95%'> | |
116 | <tr> | |
117 | <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}'></td> | |
118 | </tr> | |
119 | </table> | |
120 | END | |
121 | ||
122 | &Header::closebox(); | |
123 | ||
273618da | 124 | &Header::openbox('100%', 'center', $Lang::tr{'locationblock block countries'}); |
91634dbe SS |
125 | ### JAVA SCRIPT ### |
126 | print <<END; | |
127 | <script> | |
128 | // Function to allow checking all checkboxes at once. | |
129 | function check_all() { | |
130 | \$("#countries").find(":checkbox").prop("checked", true); | |
131 | } | |
132 | ||
133 | function uncheck_all() { | |
134 | \$("#countries").find(":checkbox").prop("checked", false); | |
135 | } | |
136 | </script> | |
137 | ||
138 | <table width='95%' class='tbl' id="countries"> | |
139 | <tr> | |
140 | <td width='5%' align='center' bgcolor='$color{'color20'}'></td> | |
141 | <td width='5%' align='center' bgcolor='$color{'color20'}'> | |
142 | <b>$Lang::tr{'flag'}</b> | |
143 | </td> | |
144 | <td width='5%' align='center' bgcolor='$color{'color20'}'> | |
145 | <b>$Lang::tr{'countrycode'}</b> | |
146 | </td> | |
147 | <td with='35%' align='left' bgcolor='$color{'color20'}'> | |
148 | <b>$Lang::tr{'country'}</b> | |
149 | </td> | |
150 | ||
151 | <td width='5%' bgcolor='$color{'color20'}'> </td> | |
152 | ||
153 | <td width='5%' align='center' bgcolor='$color{'color20'}'></td> | |
154 | <td width='5%' align='center' bgcolor='$color{'color20'}'> | |
155 | <b>$Lang::tr{'flag'}</b> | |
156 | </td> | |
157 | <td width='5%' align='center' bgcolor='$color{'color20'}'> | |
158 | <b>$Lang::tr{'countrycode'}</b> | |
159 | </td> | |
160 | <td with='35%' align='left' bgcolor='$color{'color20'}'> | |
161 | <b>$Lang::tr{'country'}</b> | |
162 | </td> | |
163 | </tr> | |
164 | END | |
165 | ||
166 | my $lines; | |
167 | my $lines2; | |
168 | my $col; | |
169 | foreach my $location (@locations) { | |
170 | # Country code in upper case. (DE) | |
171 | my $ccode_uc = $location; | |
172 | ||
173 | # County code in lower case. (de) | |
174 | my $ccode_lc = lc($location); | |
175 | ||
176 | # Full name of the country based on the country code. | |
273618da | 177 | my $cname = &Location::Functions::get_full_country_name($ccode_lc); |
91634dbe | 178 | |
e472a10d | 179 | # Get flag icon for of the country. |
273618da | 180 | my $flag_icon = &Location::Functions::get_flag_icon($ccode_uc); |
91634dbe SS |
181 | |
182 | my $flag; | |
183 | # Check if a flag for the country is available. | |
e472a10d SS |
184 | if ($flag_icon) { |
185 | $flag="<img src='$flag_icon' alt='$ccode_uc' title='$ccode_uc'>"; | |
91634dbe SS |
186 | } else { |
187 | $flag="<b>N/A</b>"; | |
188 | } | |
189 | ||
190 | # Checkbox pre-selection. | |
191 | my $checked; | |
192 | if ($settings{$ccode_uc} eq "on") { | |
193 | $checked = "checked='checked'"; | |
194 | } | |
195 | ||
196 | # Colour lines. | |
197 | if ($lines % 2) { | |
198 | $col="bgcolor='$color{'color20'}'"; | |
199 | } else { | |
200 | $col="bgcolor='$color{'color22'}'"; | |
201 | } | |
202 | ||
203 | # Grouping elements. | |
204 | my $line_start; | |
205 | my $line_end; | |
206 | if ($lines2 % 2) { | |
207 | # Increase lines (background color by once. | |
208 | $lines++; | |
209 | ||
210 | # Add empty column in front. | |
211 | $line_start="<td $col> </td>"; | |
212 | ||
213 | # When the line number can be diveded by "2", | |
214 | # we are going to close the line. | |
215 | $line_end="</tr>"; | |
216 | } else { | |
217 | # When the line number is not divideable by "2", | |
218 | # we are starting a new line. | |
219 | $line_start="<tr>"; | |
220 | $line_end; | |
221 | } | |
222 | ||
223 | print "$line_start<td align='center' $col><input type='checkbox' name='$ccode_uc' $checked></td>\n"; | |
224 | print "<td align='center' $col>$flag</td>\n"; | |
225 | print "<td align='center' $col>$ccode_uc</td>\n"; | |
226 | print "<td align='left' $col>$cname</td>$line_end\n"; | |
0ee211ee SS |
227 | |
228 | # Finish column when the last element in the array has passed and we have an uneven amount of items. | |
229 | if(! ($lines2 % 2) && ($location eq $locations[-1] )) { | |
230 | print "<td $col> </td>\n"; | |
231 | print "<td $col> </td>\n"; | |
232 | print "<td $col> </td>\n"; | |
233 | print "<td $col> </td>\n"; | |
234 | print "<td $col> </td></tr>\n"; | |
235 | } | |
236 | ||
91634dbe SS |
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> | |
273618da | 260 | <td>$Lang::tr{'locationblock country is blocked'}</td> |
91634dbe | 261 | <td width='5%'><img src='/images/off.gif'></td> |
273618da | 262 | <td>$Lang::tr{'locationblock country is allowed'}</td> |
91634dbe SS |
263 | </tr> |
264 | </table> | |
265 | END | |
266 | ||
267 | &Header::closebox(); | |
268 | print"</form>\n"; | |
269 | ||
270 | &Header::closebigbox(); | |
271 | &Header::closepage(); |