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