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