]>
Commit | Line | Data |
---|---|---|
8b920789 AM |
1 | #!/usr/bin/perl |
2 | ############################################################################### | |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
5 | # Copyright (C) 2016 IPFire Team <alexander.marx@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 | ||
b32d9e92 | 22 | #use strict; |
8b920789 | 23 | use HTML::Entities(); |
b32d9e92 AM |
24 | use File::Basename; |
25 | ||
8b920789 AM |
26 | # enable only the following on debugging purpose |
27 | #use warnings; | |
28 | #use CGI::Carp 'fatalsToBrowser'; | |
29 | ||
30 | require '/var/ipfire/general-functions.pl'; | |
31 | require "${General::swroot}/lang.pl"; | |
32 | require "${General::swroot}/header.pl"; | |
e14adf75 | 33 | |
278309b9 MT |
34 | my %selected = (); |
35 | ||
97b91e8a MT |
36 | my $coupons = "${General::swroot}/captive/coupons"; |
37 | my %couponhash = (); | |
38 | ||
e2752bfe MT |
39 | my $logo = "${General::swroot}/captive/logo.dat"; |
40 | ||
8b920789 AM |
41 | my %settings=(); |
42 | my %mainsettings; | |
43 | my %color; | |
44 | my %cgiparams=(); | |
45 | my %netsettings=(); | |
46 | my %checked=(); | |
47 | my $errormessage=''; | |
8b920789 | 48 | my $clients="${General::swroot}/captive/clients"; |
8b920789 AM |
49 | my %clientshash=(); |
50 | my $settingsfile="${General::swroot}/captive/settings"; | |
e14adf75 | 51 | unless (-e $settingsfile) { system("touch $settingsfile"); } |
8b920789 AM |
52 | |
53 | &Header::getcgihash(\%cgiparams); | |
54 | ||
55 | &General::readhash("${General::swroot}/main/settings", \%mainsettings); | |
56 | &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color); | |
57 | &General::readhash("$settingsfile", \%settings) if(-f $settingsfile); | |
58 | &General::readhash("${General::swroot}/ethernet/settings", \%netsettings); | |
59 | ||
60 | &Header::showhttpheaders(); | |
61 | ||
b7a126d9 | 62 | if ($cgiparams{'ACTION'} eq $Lang::tr{'save'}) { |
e2752bfe | 63 | my $file = $cgiparams{'logo'}; |
b7a126d9 MT |
64 | if ($file) { |
65 | # Check if the file extension is PNG/JPEG | |
b32d9e92 | 66 | chomp $file; |
b7a126d9 | 67 | |
b32d9e92 | 68 | my ($name, $path, $ext) = fileparse($file, qr/\.[^.]*$/); |
b7a126d9 MT |
69 | if ($ext ne ".png" && $ext ne ".jpg" && $ext ne ".jpeg") { |
70 | $errormessage = $Lang::tr{'Captive wrong ext'}; | |
b32d9e92 | 71 | } |
c7e78cc6 | 72 | } |
6945954c AM |
73 | |
74 | $settings{'ENABLE_GREEN'} = $cgiparams{'ENABLE_GREEN'}; | |
75 | $settings{'ENABLE_BLUE'} = $cgiparams{'ENABLE_BLUE'}; | |
76 | $settings{'AUTH'} = $cgiparams{'AUTH'}; | |
6945954c | 77 | $settings{'TITLE'} = $cgiparams{'TITLE'}; |
f8d35875 | 78 | $settings{'COLOR'} = $cgiparams{'COLOR'}; |
278309b9 | 79 | $settings{'SESSION_TIME'} = $cgiparams{'SESSION_TIME'}; |
6945954c | 80 | |
b32d9e92 AM |
81 | if (!$errormessage){ |
82 | #Check if we need to upload a new logo | |
e2752bfe MT |
83 | if ($file) { |
84 | # Save logo | |
85 | my ($filehandle) = CGI::upload("logo"); | |
86 | ||
87 | # XXX check filesize | |
88 | ||
89 | open(FILE, ">$logo"); | |
b32d9e92 | 90 | binmode $filehandle; |
e2752bfe MT |
91 | while (<$filehandle>) { |
92 | print FILE; | |
b32d9e92 | 93 | } |
e2752bfe | 94 | close(FILE); |
b32d9e92 | 95 | } |
c7e78cc6 | 96 | |
b32d9e92 AM |
97 | &General::writehash("$settingsfile", \%settings); |
98 | ||
9735e167 | 99 | # Save terms |
e2bd5a6e MT |
100 | $cgiparams{'TERMS'} = &Header::escape($cgiparams{'TERMS'}); |
101 | open(FH, ">:utf8", "/var/ipfire/captive/terms.txt") or die("$!"); | |
102 | print FH $cgiparams{'TERMS'}; | |
103 | close(FH); | |
104 | $cgiparams{'TERMS'} = ""; | |
9735e167 | 105 | |
b32d9e92 AM |
106 | #execute binary to reload firewall rules |
107 | system("/usr/local/bin/captivectrl"); | |
45129439 AM |
108 | |
109 | if ($cgiparams{'ENABLE_BLUE'} eq 'on'){ | |
110 | system("/usr/local/bin/wirelessctrl"); | |
111 | } | |
8b920789 | 112 | } |
8b920789 AM |
113 | } |
114 | ||
5cd9e28b | 115 | if ($cgiparams{'ACTION'} eq "$Lang::tr{'Captive generate coupon'}") { |
733932de MT |
116 | # Check expiry time |
117 | if ($cgiparams{'EXP_HOUR'} + $cgiparams{'EXP_DAY'} + $cgiparams{'EXP_WEEK'} + $cgiparams{'EXP_MONTH'} == 0 && $cgiparams{'UNLIMITED'} == '') { | |
118 | $errormessage = $Lang::tr{'Captive noexpiretime'}; | |
08061703 | 119 | } |
8b920789 | 120 | |
e01c5ab7 AM |
121 | #check valid remark |
122 | if ($cgiparams{'REMARK'} ne '' && !&validremark($cgiparams{'REMARK'})){ | |
123 | $errormessage=$Lang::tr{'fwhost err remark'}; | |
124 | } | |
125 | ||
5cd9e28b | 126 | if (!$errormessage) { |
9cba29f1 MT |
127 | # Remember selected values |
128 | foreach my $val (("UNLIMITED", "EXP_HOUR", "EXP_DAY", "EXP_WEEK", "EXP_MONTH")) { | |
129 | $settings{$val} = $cgiparams{$val}; | |
130 | } | |
131 | &General::writehash($settingsfile, \%settings); | |
132 | ||
5cd9e28b MT |
133 | &General::readhasharray($coupons, \%couponhash) if (-e $coupons); |
134 | my $now = time(); | |
135 | ||
733932de MT |
136 | # Calculate expiry time in seconds |
137 | my $expires = 0; | |
138 | ||
139 | if ($settings{'UNLIMITED'} ne 'on') { | |
140 | $expires += $settings{'EXP_HOUR'}; | |
141 | $expires += $settings{'EXP_DAY'}; | |
142 | $expires += $settings{'EXP_WEEK'}; | |
143 | $expires += $settings{'EXP_MONTH'}; | |
144 | } | |
145 | ||
5cd9e28b MT |
146 | my $count = $cgiparams{'COUNT'} || 1; |
147 | while($count-- > 0) { | |
148 | # Generate a new code | |
149 | my $code = &gencode(); | |
150 | ||
151 | # Check if the coupon code already exists | |
152 | foreach my $key (keys %couponhash) { | |
153 | if($couponhash{$key}[1] eq $code) { | |
154 | # Code already exists, so try again | |
155 | $code = ""; | |
156 | $count++; | |
157 | last; | |
158 | } | |
159 | } | |
160 | ||
161 | next if ($code eq ""); | |
162 | ||
163 | # Get a new key from hash | |
164 | my $key = &General::findhasharraykey(\%couponhash); | |
165 | ||
166 | # Initialize all fields | |
167 | foreach my $i (0 .. 3) { $couponhash{$key}[$i] = ""; } | |
8b920789 | 168 | |
5cd9e28b MT |
169 | $couponhash{$key}[0] = $now; |
170 | $couponhash{$key}[1] = $code; | |
171 | $couponhash{$key}[2] = $expires; | |
172 | $couponhash{$key}[3] = $cgiparams{'REMARK'}; | |
173 | } | |
8b920789 | 174 | |
5cd9e28b MT |
175 | # Save everything to disk |
176 | &General::writehasharray($coupons, \%couponhash); | |
8b920789 AM |
177 | } |
178 | } | |
179 | ||
97b91e8a | 180 | if ($cgiparams{'ACTION'} eq 'delete-coupon') { |
8b920789 AM |
181 | #deletes an already generated but unused voucher |
182 | ||
183 | #read all generated vouchers | |
97b91e8a MT |
184 | &General::readhasharray($coupons, \%couponhash) if (-e $coupons); |
185 | foreach my $key (keys %couponhash) { | |
186 | if($cgiparams{'key'} eq $couponhash{$key}[0]){ | |
8b920789 | 187 | #write logenty with decoded remark |
97b91e8a MT |
188 | my $rem=HTML::Entities::decode_entities($couponhash{$key}[4]); |
189 | &General::log("Captive", "Delete unused coupon $couponhash{$key}[1] $couponhash{$key}[2] hours valid expires on $couponhash{$key}[3] remark $rem"); | |
8b920789 | 190 | #delete line from hash |
97b91e8a | 191 | delete $couponhash{$key}; |
8b920789 AM |
192 | last; |
193 | } | |
194 | } | |
195 | #write back hash | |
97b91e8a | 196 | &General::writehasharray($coupons, \%couponhash); |
8b920789 AM |
197 | } |
198 | ||
97b91e8a | 199 | if ($cgiparams{'ACTION'} eq 'delete-client') { |
8b920789 AM |
200 | #delete voucher and connection in use |
201 | ||
202 | #read all active clients | |
97b91e8a | 203 | &General::readhasharray($clients, \%clientshash) if (-e $clients); |
8b920789 AM |
204 | foreach my $key (keys %clientshash) { |
205 | if($cgiparams{'key'} eq $clientshash{$key}[0]){ | |
206 | #prepare log entry with decoded remark | |
207 | my $rem=HTML::Entities::decode_entities($clientshash{$key}[7]); | |
208 | #write logentry | |
97b91e8a | 209 | &General::log("Captive", "Deleted client in use $clientshash{$key}[1] $clientshash{$key}[2] hours valid expires on $clientshash{$key}[3] remark $rem - Connection will be terminated"); |
8b920789 AM |
210 | #delete line from hash |
211 | delete $clientshash{$key}; | |
212 | last; | |
213 | } | |
214 | } | |
215 | #write back hash | |
216 | &General::writehasharray("$clients", \%clientshash); | |
217 | #reload firewallrules to kill connection of client | |
218 | system("/usr/local/bin/captivectrl"); | |
219 | } | |
220 | ||
221 | #open webpage, print header and open box | |
222 | &Header::openpage($Lang::tr{'Captive menu'}, 1, ''); | |
223 | &Header::openbigbox(); | |
224 | ||
a41fe924 MT |
225 | # If an error message exists, show a box with the error message |
226 | if ($errormessage) { | |
227 | &Header::openbox('100%', 'left', $Lang::tr{'error messages'}); | |
228 | print $errormessage; | |
229 | &Header::closebox(); | |
8b920789 AM |
230 | } |
231 | ||
a41fe924 MT |
232 | # Prints the config box on the website |
233 | &Header::openbox('100%', 'left', $Lang::tr{'Captive config'}); | |
234 | print <<END | |
235 | <form method='post' action='$ENV{'SCRIPT_NAME'}' enctype="multipart/form-data">\n | |
8b920789 AM |
236 | <table width='100%' border="0"> |
237 | <tr> | |
238 | END | |
239 | ; | |
e01c5ab7 | 240 | |
a41fe924 MT |
241 | #check which parameters have to be enabled (from settings file) |
242 | $checked{'ENABLE_GREEN'}{'off'} = ''; | |
243 | $checked{'ENABLE_GREEN'}{'on'} = ''; | |
244 | $checked{'ENABLE_GREEN'}{$settings{'ENABLE_GREEN'}} = "checked='checked'"; | |
8b920789 | 245 | |
a41fe924 MT |
246 | $checked{'ENABLE_BLUE'}{'off'} = ''; |
247 | $checked{'ENABLE_BLUE'}{'on'} = ''; | |
248 | $checked{'ENABLE_BLUE'}{$settings{'ENABLE_BLUE'}} = "checked='checked'"; | |
c7e78cc6 | 249 | |
a41fe924 MT |
250 | $checked{'UNLIMITED'}{'off'} = ''; |
251 | $checked{'UNLIMITED'}{'on'} = ''; | |
252 | $checked{'UNLIMITED'}{$settings{'UNLIMITED'}} = "checked='checked'"; | |
253 | ||
e7d16ea5 MT |
254 | $selected{'AUTH'} = (); |
255 | $selected{'AUTH'}{'COUPON'} = ""; | |
256 | $selected{'AUTH'}{'TERMS'} = ""; | |
257 | $selected{'AUTH'}{$settings{'AUTH'}} = "selected"; | |
258 | ||
a41fe924 MT |
259 | if ($netsettings{'GREEN_DEV'}){ |
260 | print "<td width='30%'>$Lang::tr{'Captive active on'} <font color='$Header::colourgreen'>Green</font></td><td><input type='checkbox' name='ENABLE_GREEN' $checked{'ENABLE_GREEN'}{'on'} /></td></tr>"; | |
261 | } | |
262 | if ($netsettings{'BLUE_DEV'}){ | |
263 | print "<td width='30%'>$Lang::tr{'Captive active on'} <font color='$Header::colourblue'>Blue</font></td><td><input type='checkbox' name='ENABLE_BLUE' $checked{'ENABLE_BLUE'}{'on'} /></td></tr>"; | |
264 | } | |
e01c5ab7 | 265 | |
e01c5ab7 | 266 | print<<END |
a41fe924 MT |
267 | </tr> |
268 | <tr> | |
269 | <td> | |
270 | $Lang::tr{'Captive authentication'} | |
271 | </td> | |
272 | <td> | |
273 | <select name='AUTH'> | |
e7d16ea5 MT |
274 | <option value="TERMS" $selected{'AUTH'}{'TERMS'} >$Lang::tr{'Captive terms'}</option> |
275 | <option value="COUPON" $selected{'AUTH'}{'COUPON'}>$Lang::tr{'Captive coupon'}</option> | |
a41fe924 MT |
276 | </select> |
277 | </td> | |
278 | </tr> | |
8b920789 AM |
279 | END |
280 | ; | |
8b920789 | 281 | |
a41fe924 MT |
282 | if ($settings{'AUTH'} eq 'TERMS') { |
283 | $selected{'SESSION_TIME'} = (); | |
284 | $selected{'SESSION_TIME'}{'0'} = ""; | |
285 | $selected{'SESSION_TIME'}{'3600'} = ""; | |
bbc69f22 | 286 | $selected{'SESSION_TIME'}{'28800'} = ""; |
a41fe924 MT |
287 | $selected{'SESSION_TIME'}{'86400'} = ""; |
288 | $selected{'SESSION_TIME'}{'604800'} = ""; | |
289 | $selected{'SESSION_TIME'}{'18144000'} = ""; | |
290 | $selected{'SESSION_TIME'}{$settings{'SESSION_TIME'}} = "selected"; | |
291 | ||
a41fe924 | 292 | print <<END; |
297ebdd4 | 293 | <tr> |
a41fe924 | 294 | <td>$Lang::tr{'Captive client session expiry time'}</td> |
297ebdd4 | 295 | <td> |
a41fe924 MT |
296 | <select name="SESSION_TIME"> |
297 | <option value="0" $selected{'SESSION_TIME'}{'0'}>- $Lang::tr{'unlimited'} -</option> | |
298 | <option value="3600" $selected{'SESSION_TIME'}{'3600'}>$Lang::tr{'one hour'}</option> | |
0a219160 | 299 | <option value="28800" $selected{'SESSION_TIME'}{'28800'}>$Lang::tr{'eight hours'}</option> |
a41fe924 MT |
300 | <option value="86400" $selected{'SESSION_TIME'}{'86400'}>$Lang::tr{'24 hours'}</option> |
301 | <option value="604800" $selected{'SESSION_TIME'}{'604800'}>$Lang::tr{'one week'}</option> | |
302 | <option value="18144000" $selected{'SESSION_TIME'}{'18144000'}>$Lang::tr{'one month'}</option> | |
303 | </select> | |
297ebdd4 MT |
304 | </td> |
305 | </tr> | |
306 | END | |
a41fe924 | 307 | } |
297ebdd4 | 308 | |
a41fe924 MT |
309 | print<<END; |
310 | <tr> | |
311 | <td colspan="2"> | |
312 | <br> | |
313 | <strong>$Lang::tr{'Captive branding'}</strong> | |
314 | </td> | |
315 | </tr> | |
316 | <tr> | |
317 | <td> | |
318 | $Lang::tr{'Captive title'} | |
319 | </td> | |
320 | <td> | |
321 | <input type='text' name='TITLE' value="$settings{'TITLE'}" size='40'> | |
322 | </td> | |
323 | </tr> | |
324 | <tr> | |
325 | <td>$Lang::tr{'Captive brand color'}</td> | |
326 | <td> | |
327 | <input type="color" name="COLOR" value="$settings{'COLOR'}"> | |
328 | </td> | |
329 | </tr> | |
a41fe924 MT |
330 | <tr> |
331 | <td> | |
81019811 | 332 | $Lang::tr{'Captive upload logo'} |
a41fe924 MT |
333 | </td> |
334 | <td> | |
335 | <input type="file" name="logo"> | |
81019811 | 336 | <br>$Lang::tr{'Captive upload logo recommendations'} |
a41fe924 MT |
337 | </td> |
338 | </tr> | |
e2752bfe | 339 | END |
827d3f61 | 340 | |
a41fe924 | 341 | if (-e $logo) { |
297ebdd4 | 342 | print <<END; |
8b920789 | 343 | <tr> |
a41fe924 MT |
344 | <td>$Lang::tr{'Captive logo uploaded'}</td> |
345 | <td>$Lang::tr{'yes'}</td> | |
8b920789 | 346 | </tr> |
a41fe924 MT |
347 | END |
348 | } | |
349 | ||
e2bd5a6e | 350 | my $terms = &getterms(); |
a41fe924 | 351 | print <<END; |
e2bd5a6e MT |
352 | <tr> |
353 | <td></td> | |
354 | <td> | |
355 | <textarea cols="50" rows="10" name="TERMS">$terms</textarea> | |
356 | </td> | |
357 | </tr> | |
a41fe924 MT |
358 | <tr> |
359 | <td></td> | |
360 | <td align='right'> | |
361 | <input type='submit' name='ACTION' value="$Lang::tr{'save'}"/> | |
362 | </td> | |
363 | </tr> | |
297ebdd4 | 364 | </table></form> |
8b920789 | 365 | END |
297ebdd4 | 366 | |
a41fe924 | 367 | &Header::closebox(); |
8b920789 | 368 | |
a41fe924 MT |
369 | #if settings is set to use coupons, the coupon part has to be displayed |
370 | if ($settings{'AUTH'} eq 'COUPON') { | |
371 | &coupons(); | |
a41fe924 MT |
372 | } |
373 | ||
792f1a3b MT |
374 | # Show active clients |
375 | &show_clients(); | |
376 | ||
a41fe924 MT |
377 | sub getterms() { |
378 | my @ret; | |
379 | ||
380 | open(FILE, "<:utf8", "/var/ipfire/captive/terms.txt"); | |
381 | while(<FILE>) { | |
382 | push(@ret, HTML::Entities::decode_entities($_)); | |
8b920789 | 383 | } |
a41fe924 MT |
384 | close(FILE); |
385 | ||
386 | return join(/\n/, @ret); | |
8b920789 AM |
387 | } |
388 | ||
8b920789 AM |
389 | sub gencode(){ |
390 | #generate a random code only letters from A-Z except 'O' and 0-9 | |
391 | my @chars = ("A".."N", "P".."Z", "0".."9"); | |
392 | my $randomstring; | |
393 | $randomstring .= $chars[rand @chars] for 1..8; | |
394 | return $randomstring; | |
395 | } | |
396 | ||
97b91e8a MT |
397 | sub coupons() { |
398 | &Header::openbox('100%', 'left', $Lang::tr{'Captive generate coupon'}); | |
4f75fbfe MT |
399 | print <<END; |
400 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
401 | <table border='0' width='100%'> | |
402 | <tr> | |
403 | <td width='30%'> | |
404 | $Lang::tr{'Captive vouchervalid'} | |
405 | </td> | |
406 | <td width='70%'> | |
407 | <table class='tbl' border='0' width='100%'> | |
408 | <tr> | |
409 | <th>$Lang::tr{'hours'}</th> | |
410 | <th>$Lang::tr{'days'}</th> | |
411 | <th>$Lang::tr{'weeks'}</th> | |
412 | <th>$Lang::tr{'months'}</th> | |
413 | <th></th> | |
414 | </tr> | |
415 | END | |
08061703 AM |
416 | |
417 | #print hour-dropdownbox | |
418 | my $hrs=3600; | |
419 | print "<tr height='40px'><td><select name='EXP_HOUR' style='width:8em;'>"; | |
420 | print "<option value='0' "; | |
421 | print " selected='selected'" if ($settings{'EXP_HOUR'} eq '0'); | |
422 | print ">--</option>"; | |
423 | for (my $i = 1; $i<25; $i++){ | |
424 | my $exp_sec = $i * $hrs; | |
425 | print "<option value='$exp_sec' "; | |
426 | print " selected='selected'" if ($settings{'EXP_HOUR'} eq $exp_sec); | |
427 | print ">$i</option>"; | |
428 | } | |
429 | print "</td><td>"; | |
430 | ||
431 | #print day-dropdownbox | |
432 | my $days=3600*24; | |
433 | print "<select name='EXP_DAY' style='width:8em;'>"; | |
434 | print "<option value='0' "; | |
435 | print " selected='selected'" if ($settings{'EXP_DAY'} eq '0'); | |
436 | print ">--</option>"; | |
437 | for (my $i = 1; $i<8; $i++){ | |
438 | my $exp_sec = $i * $days; | |
439 | print "<option value='$exp_sec' "; | |
440 | print " selected='selected'" if ($settings{'EXP_DAY'} eq $exp_sec); | |
441 | print ">$i</option>"; | |
442 | } | |
443 | print "</td><td>"; | |
444 | ||
445 | #print week-dropdownbox | |
446 | my $week=3600*24*7; | |
447 | print "<select name='EXP_WEEK' style='width:8em;'>"; | |
448 | print "<option value='0' "; | |
449 | print " selected='selected'" if ($settings{'EXP_WEEK'} eq '0'); | |
450 | print ">--</option>"; | |
451 | for (my $i = 1; $i<5; $i++){ | |
452 | my $exp_sec = $i * $week; | |
453 | print "<option value='$exp_sec' "; | |
454 | print " selected='selected'" if ($settings{'EXP_WEEK'} eq $exp_sec); | |
455 | print ">$i</option>"; | |
456 | } | |
457 | print "</td><td>"; | |
458 | ||
459 | #print month-dropdownbox | |
460 | my $month=3600*24*30; | |
461 | print "<select name='EXP_MONTH' style='width:8em;'>"; | |
462 | print "<option value='0' "; | |
463 | print " selected='selected'" if ($settings{'EXP_MONTH'} eq '0'); | |
464 | print ">--</option>"; | |
465 | for (my $i = 1; $i<13; $i++){ | |
466 | my $exp_sec = $i * $month; | |
467 | print "<option value='$exp_sec' "; | |
468 | print " selected='selected'" if ($settings{'EXP_MONTH'} eq $exp_sec); | |
469 | print ">$i</option>"; | |
470 | } | |
4f75fbfe MT |
471 | print <<END; |
472 | </td> | |
473 | <td> | |
474 | <label> | |
475 | <input type='checkbox' name='UNLIMITED' $checked{'UNLIMITED'}{'on'} /> | |
476 | $Lang::tr{'Captive nolimit'} | |
477 | </label> | |
478 | </td> | |
479 | </tr> | |
480 | </table> | |
481 | </td> | |
482 | </tr> | |
483 | <tr> | |
484 | <td>$Lang::tr{'remark'}</td> | |
485 | <td> | |
486 | <input type='text' style='width: 98%;' name='REMARK' align='left'> | |
487 | </td> | |
488 | </tr> | |
489 | </table> | |
490 | ||
491 | <div align="right"> | |
5cd9e28b MT |
492 | <select name="COUNT"> |
493 | <option value="1">1</option> | |
494 | <option value="2">2</option> | |
495 | <option value="3">3</option> | |
496 | <option value="4">4</option> | |
497 | <option value="5">5</option> | |
498 | <option value="6">6</option> | |
499 | <option value="7">7</option> | |
500 | <option value="8">8</option> | |
501 | <option value="9">9</option> | |
502 | <option value="10">10</option> | |
503 | <option value="20">20</option> | |
504 | <option value="50">50</option> | |
505 | <option value="100">100</option> | |
506 | </select> | |
507 | ||
4f75fbfe MT |
508 | <input type="submit" name="ACTION" value="$Lang::tr{'Captive generate coupon'}"> |
509 | </div> | |
510 | </form> | |
511 | END | |
08061703 | 512 | |
8b920789 | 513 | &Header::closebox(); |
97b91e8a MT |
514 | |
515 | # Show all coupons if exist | |
516 | if (! -z $coupons) { | |
517 | &show_coupons(); | |
518 | } | |
8b920789 AM |
519 | } |
520 | ||
97b91e8a | 521 | sub show_coupons() { |
a0b271e4 MT |
522 | &General::readhasharray($coupons, \%couponhash) if (-e $coupons); |
523 | ||
97b91e8a | 524 | #if there are already generated but unsused coupons, print a table |
a0b271e4 MT |
525 | &Header::openbox('100%', 'left', $Lang::tr{'Captive issued coupons'}); |
526 | ||
527 | print <<END; | |
528 | <table class='tbl' border='0'> | |
529 | <tr> | |
530 | <th align='center' width='15%'> | |
531 | $Lang::tr{'Captive coupon'} | |
532 | </th> | |
533 | <th align='center' width='15%'>$Lang::tr{'Captive expiry time'}</th> | |
534 | <th align='center' width='65%'>$Lang::tr{'remark'}</th> | |
535 | <th align='center' width='5%'>$Lang::tr{'delete'}</th> | |
536 | </tr> | |
8b920789 | 537 | END |
a0b271e4 MT |
538 | |
539 | foreach my $key (keys %couponhash) { | |
540 | my $expirytime = $Lang::tr{'Captive nolimit'}; | |
541 | if ($couponhash{$key}[2] > 0) { | |
542 | $expirytime = &General::format_time($couponhash{$key}[2]); | |
c7e78cc6 AM |
543 | } |
544 | ||
a0b271e4 | 545 | if ($count++ % 2) { |
8b920789 | 546 | $col="bgcolor='$color{'color20'}'"; |
a0b271e4 | 547 | } else { |
8b920789 | 548 | $col="bgcolor='$color{'color22'}'"; |
8b920789 | 549 | } |
c7e78cc6 | 550 | |
a0b271e4 MT |
551 | print <<END; |
552 | <tr> | |
553 | <td $col align="center"> | |
554 | <b>$couponhash{$key}[1]</b> | |
555 | </td> | |
556 | <td $col align="center"> | |
557 | $expirytime | |
558 | </td> | |
559 | <td $col align="center"> | |
560 | $couponhash{$key}[3] | |
561 | </td> | |
562 | <td $col align="center"> | |
563 | <form method='post'> | |
564 | <input type='image' src='/images/delete.gif' align='middle' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' /> | |
565 | <input type='hidden' name='ACTION' value='delete-coupon' /> | |
566 | <input type='hidden' name='key' value='$couponhash{$key}[0]' /> | |
567 | </form> | |
568 | </td> | |
569 | </tr> | |
570 | END | |
8b920789 | 571 | } |
c7e78cc6 | 572 | |
8b920789 | 573 | print "</table>"; |
a0b271e4 | 574 | |
8b920789 AM |
575 | &Header::closebox(); |
576 | } | |
577 | ||
97b91e8a | 578 | sub show_clients() { |
41964aba | 579 | # if there are active clients which use coupons show table |
8b920789 | 580 | return if ( -z $clients || ! -f $clients ); |
41964aba | 581 | |
8b920789 AM |
582 | my $count=0; |
583 | my $col; | |
41964aba MT |
584 | |
585 | &Header::openbox('100%', 'left', $Lang::tr{'Captive clients'}); | |
586 | ||
587 | print <<END; | |
588 | <table class='tbl' width='100%'> | |
589 | <tr> | |
590 | <th align='center' width='15%'>$Lang::tr{'Captive coupon'}</th> | |
591 | <th align='center' width='15%'>$Lang::tr{'Captive activated'}</th> | |
592 | <th align='center' width='15%'>$Lang::tr{'Captive expiry time'}</th> | |
593 | <th align='center' width='10%'>$Lang::tr{'Captive mac'}</th> | |
594 | <th align='center' width='43%'>$Lang::tr{'remark'}</th> | |
595 | <th align='center' width='5%'>$Lang::tr{'delete'}</th> | |
596 | </tr> | |
8b920789 | 597 | END |
41964aba | 598 | |
97b91e8a | 599 | &General::readhasharray($clients, \%clientshash) if (-e $clients); |
41964aba | 600 | foreach my $key (keys %clientshash) { |
c7e78cc6 AM |
601 | #calculate time from clientshash (starttime) |
602 | my $starttime = sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime($clientshash{$key}[2])); | |
41964aba | 603 | |
c7e78cc6 AM |
604 | #calculate endtime from clientshash |
605 | my $endtime; | |
606 | if ($clientshash{$key}[3] eq '0'){ | |
607 | $endtime=$Lang::tr{'Captive nolimit'}; | |
41964aba | 608 | } else { |
c7e78cc6 AM |
609 | $endtime = sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime($clientshash{$key}[2]+$clientshash{$key}[3])); |
610 | } | |
e01c5ab7 | 611 | |
41964aba MT |
612 | if ($count++ % 2) { |
613 | $col="bgcolor='$color{'color20'}'"; | |
614 | } else { | |
615 | $col="bgcolor='$color{'color22'}'"; | |
616 | } | |
617 | ||
618 | my $coupon = ($clientshash{$key}[4] eq "LICENSE") ? $Lang::tr{'Captive terms short'} : $clientshash{$key}[4]; | |
e01c5ab7 | 619 | |
41964aba MT |
620 | print <<END; |
621 | <tr> | |
622 | <td $col align="center"><b>$coupon</b></td> | |
623 | <td $col align="center">$starttime</td> | |
624 | <td $col align="center">$endtime</td> | |
625 | <td $col align="center">$clientshash{$key}[0]</td> | |
626 | <td $col align="center">$clientshash{$key}[5]</td> | |
627 | <td $col align="center"> | |
628 | <form method='post'> | |
629 | <input type='image' src='/images/delete.gif' align='middle' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' /> | |
630 | <input type='hidden' name='ACTION' value='delete-client' /> | |
631 | <input type='hidden' name='key' value='$clientshash{$key}[0]' /> | |
632 | </form> | |
633 | </td> | |
634 | </tr> | |
635 | END | |
8b920789 | 636 | } |
c7e78cc6 | 637 | |
8b920789 | 638 | print "</table>"; |
41964aba | 639 | |
8b920789 AM |
640 | &Header::closebox(); |
641 | } | |
642 | ||
e01c5ab7 AM |
643 | sub validremark |
644 | { | |
645 | # Checks a hostname against RFC1035 | |
646 | my $remark = $_[0]; | |
647 | # Each part should be at least two characters in length | |
648 | # but no more than 63 characters | |
649 | if (length ($remark) < 1 || length ($remark) > 255) { | |
650 | return 0;} | |
651 | # Only valid characters are a-z, A-Z, 0-9 and - | |
652 | if ($remark !~ /^[a-zäöüA-ZÖÄÜ0-9-.:;\|_()\/\s]*$/) { | |
653 | return 0;} | |
654 | # First character can only be a letter or a digit | |
655 | if (substr ($remark, 0, 1) !~ /^[a-zäöüA-ZÖÄÜ0-9]*$/) { | |
656 | return 0;} | |
657 | # Last character can only be a letter or a digit | |
658 | if (substr ($remark, -1, 1) !~ /^[a-zöäüA-ZÖÄÜ0-9.:;_)]*$/) { | |
659 | return 0;} | |
660 | return 1; | |
661 | } | |
662 | ||
8b920789 AM |
663 | &Header::closebigbox(); |
664 | &Header::closepage(); |