]> git.ipfire.org Git - ipfire-2.x.git/blame - html/cgi-bin/captive.cgi
captive: Allow selecting the session expiry time for terms
[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
8b920789
AM
39my %settings=();
40my %mainsettings;
41my %color;
42my %cgiparams=();
43my %netsettings=();
44my %checked=();
45my $errormessage='';
8b920789 46my $clients="${General::swroot}/captive/clients";
8b920789
AM
47my %clientshash=();
48my $settingsfile="${General::swroot}/captive/settings";
b32d9e92 49my $logopath = "/srv/web/ipfire/html/captive/logo";
e14adf75 50unless (-e $settingsfile) { system("touch $settingsfile"); }
8b920789
AM
51
52&Header::getcgihash(\%cgiparams);
53
54&General::readhash("${General::swroot}/main/settings", \%mainsettings);
55&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
56&General::readhash("$settingsfile", \%settings) if(-f $settingsfile);
57&General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
58
59&Header::showhttpheaders();
60
61#actions
62if ($cgiparams{'ACTION'} eq "$Lang::tr{'save'}"){
b32d9e92
AM
63 my $file = $cgiparams{'uploaded_file'};
64 if ($file){
65 #Check if extension is png
66 chomp $file;
67 my ($name, $path, $ext) = fileparse($file, qr/\.[^.]*$/);
68 if ($ext ne ".png"){
69 $errormessage=$Lang::tr{'Captive wrong ext'};
70 }
c7e78cc6 71 }
6945954c
AM
72
73 $settings{'ENABLE_GREEN'} = $cgiparams{'ENABLE_GREEN'};
74 $settings{'ENABLE_BLUE'} = $cgiparams{'ENABLE_BLUE'};
75 $settings{'AUTH'} = $cgiparams{'AUTH'};
6945954c
AM
76 $settings{'TITLE'} = $cgiparams{'TITLE'};
77 $settings{'UNLIMITED'} = $cgiparams{'UNLIMITED'};
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
83 if($file){
84 #Save File
85 my ($filehandle) = CGI::upload('uploaded_file');
86 open (UPLOADFILE, ">$logopath/logo.png");
87 binmode $filehandle;
88 while ( <$filehandle> ) {
89 print UPLOADFILE;
90 }
91 close (UPLOADFILE);
92
93 #Open file to check if dimensions are within rang
94 open (PNG , "<$logopath/logo.png");
95 local $/;
96 my $PNG1=<PNG>;
97 close(PNG);
98 my ($width,$height)=&pngsize($PNG1);
52383f58
AM
99 if($width > 1920 || $height > 800 || $width < 1280 || $height < 400){
100 $errormessage.="$Lang::tr{'Captive invalid logosize'} <br>Filedimensions width: $width height: $height ";
b32d9e92
AM
101 unlink("$logopath/logo.png");
102 }
103 }
c7e78cc6 104
b32d9e92
AM
105 &General::writehash("$settingsfile", \%settings);
106
9735e167
MT
107 # Save terms
108 if ($cgiparams{'TERMS'}){
109 $cgiparams{'TERMS'} = &Header::escape($cgiparams{'TERMS'});
110 open(FH, ">:utf8", "/var/ipfire/captive/terms.txt") or die("$!");
111 print FH $cgiparams{'TERMS'};
112 close(FH);
113 $cgiparams{'TERMS'} = "";
b32d9e92 114 }
9735e167 115
b32d9e92
AM
116 #execute binary to reload firewall rules
117 system("/usr/local/bin/captivectrl");
45129439
AM
118
119 if ($cgiparams{'ENABLE_BLUE'} eq 'on'){
120 system("/usr/local/bin/wirelessctrl");
121 }
8b920789 122 }
8b920789
AM
123}
124
97b91e8a
MT
125if ($cgiparams{'ACTION'} eq "$Lang::tr{'Captive generate coupon'}"){
126 # Generates a new coupon
8b920789 127
08061703
AM
128 #calculate expiredate
129 my $expire;
130 if ($settings{'UNLIMITED'} eq 'on'){
131 $expire = $Lang::tr{'Captive nolimit'};
132 }else{
133 $settings{'EXPIRE'} = $cgiparams{'EXP_HOUR'}+$cgiparams{'EXP_DAY'}+$cgiparams{'EXP_WEEK'}+$cgiparams{'EXP_MONTH'};
134 $expire = sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime(time()+$settings{'EXPIRE'}));
135 }
136
137 #Check Expiretime
138 if($cgiparams{'EXP_HOUR'}+$cgiparams{'EXP_DAY'}+$cgiparams{'EXP_WEEK'}+$cgiparams{'EXP_MONTH'} == 0 && $cgiparams{'UNLIMITED'} == ''){
139 $errormessage=$Lang::tr{'Captive noexpiretime'};
140 }
97b91e8a
MT
141 #check if we already have a coupon with same code
142 &General::readhasharray($coupons, \%couponhash) if (-e $coupons);
143 foreach my $key (keys %couponhash) {
144 if($couponhash{$key}[1] eq $cgiparams{'CODE'}){
8b920789
AM
145 $errormessage=$Lang::tr{'Captive err doublevoucher'};
146 last;
147 }
148 }
149
e01c5ab7
AM
150 #check valid remark
151 if ($cgiparams{'REMARK'} ne '' && !&validremark($cgiparams{'REMARK'})){
152 $errormessage=$Lang::tr{'fwhost err remark'};
153 }
154
8b920789
AM
155 #if no error detected, write to disk
156 if (!$errormessage){
157 my $date=time(); #seconds in utc
158
159 #first get new key from hash
97b91e8a 160 my $key=&General::findhasharraykey (\%couponhash);
8b920789 161 #initialize all fields with ''
97b91e8a 162 foreach my $i (0 .. 3) { $couponhash{$key}[$i] = "";}
8b920789 163 #define fields
97b91e8a
MT
164 $couponhash{$key}[0] = $date;
165 $couponhash{$key}[1] = $cgiparams{'CODE'};
166 $couponhash{$key}[2] = $settings{'EXPIRE'};
167 $couponhash{$key}[3] = $cgiparams{'REMARK'};
8b920789 168 #write values to disk
97b91e8a 169 &General::writehasharray($coupons, \%couponhash);
8b920789
AM
170
171 #now prepare log entry, get expiring date for voucher and decode remark for logfile
97b91e8a
MT
172 my $expdate=localtime(time()+$couponhash{$key}[3]);
173 my $rem=HTML::Entities::decode_entities($couponhash{$key}[4]);
8b920789
AM
174
175 #write logfile entry
97b91e8a 176 &General::log("Captive", "Generated new coupon $couponhash{$key}[1] $couponhash{$key}[2] hours valid expires on $expdate remark $rem");
8b920789
AM
177 }
178}
179
97b91e8a 180if ($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 199if ($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
225#call error() to see if we have to print an errormessage on website
226&error();
227
228#call config() to display the configuration box
229&config();
230
9735e167
MT
231sub getterms(){
232 my @ret;
233
234 open(FILE, "<:utf8", "/var/ipfire/captive/terms.txt");
235 while(<FILE>) {
236 push(@ret, HTML::Entities::decode_entities($_));
237 }
238 close(FILE);
239
240 return join(/\n/, @ret);
8b920789
AM
241}
242
243sub config(){
244 #prints the config box on the website
245 &Header::openbox('100%', 'left', $Lang::tr{'Captive config'});
246 print <<END
b32d9e92 247 <form method='post' action='$ENV{'SCRIPT_NAME'}' enctype="multipart/form-data">\n
8b920789
AM
248 <table width='100%' border="0">
249 <tr>
250END
251;
252 #check which parameters have to be enabled (from settings file)
253 $checked{'ENABLE_GREEN'}{'off'} = '';
254 $checked{'ENABLE_GREEN'}{'on'} = '';
255 $checked{'ENABLE_GREEN'}{$settings{'ENABLE_GREEN'}} = "checked='checked'";
e01c5ab7 256
8b920789
AM
257 $checked{'ENABLE_BLUE'}{'off'} = '';
258 $checked{'ENABLE_BLUE'}{'on'} = '';
259 $checked{'ENABLE_BLUE'}{$settings{'ENABLE_BLUE'}} = "checked='checked'";
260
c7e78cc6
AM
261 $checked{'UNLIMITED'}{'off'} = '';
262 $checked{'UNLIMITED'}{'on'} = '';
263 $checked{'UNLIMITED'}{$settings{'UNLIMITED'}} = "checked='checked'";
264
8b920789
AM
265 if ($netsettings{'GREEN_DEV'}){
266 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>";
267 }
268 if ($netsettings{'BLUE_DEV'}){
269 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>";
270 }
e01c5ab7 271
e01c5ab7 272print<<END
297ebdd4 273 </tr>
8b920789 274 <tr>
827d3f61 275 <td>
8b920789
AM
276 $Lang::tr{'Captive authentication'}
277 </td>
827d3f61 278 <td>
9735e167 279 <select name='AUTH'>
8b920789
AM
280END
281;
9735e167
MT
282 print "<option value='TERMS' ";
283 print " selected='selected'" if ($settings{'AUTH'} eq 'TERMS');
284 print ">$Lang::tr{'Captive terms'}</option>";
e01c5ab7 285
97b91e8a
MT
286 print "<option value='COUPON' ";
287 print " selected='selected'" if ($settings{'AUTH'} eq 'COUPON');
288 print ">$Lang::tr{'Captive coupon'}</option>";
e01c5ab7 289
8b920789
AM
290 print<<END
291 </select>
292 </td>
293 </tr>
8b920789
AM
294END
295;
9735e167 296 if ($settings{'AUTH'} eq 'TERMS') {
278309b9
MT
297 $selected{'SESSION_TIME'} = ();
298 $selected{'SESSION_TIME'}{'0'} = "";
299 $selected{'SESSION_TIME'}{'3600'} = "";
300 $selected{'SESSION_TIME'}{'86400'} = "";
301 $selected{'SESSION_TIME'}{'604800'} = "";
302 $selected{'SESSION_TIME'}{'18144000'} = "";
303 $selected{'SESSION_TIME'}{$settings{'SESSION_TIME'}} = "selected";
304
9735e167
MT
305 my $terms = &getterms();
306 print <<END;
307 <tr>
308 <td></td>
309 <td>
310 <textarea cols="50" rows="10" name="TERMS">$terms</textarea>
311 </td>
312 </tr>
278309b9
MT
313
314 <tr>
315 <td>$Lang::tr{'Captive client session expiry time'}</td>
316 <td>
317 <select name="SESSION_TIME">
318 <option value="0" $selected{'SESSION_TIME'}{'0'}>- $Lang::tr{'unlimited'} -</option>
319 <option value="3600" $selected{'SESSION_TIME'}{'3600'}>$Lang::tr{'one hour'}</option>
320 <option value="86400" $selected{'SESSION_TIME'}{'86400'}>$Lang::tr{'24 hours'}</option>
321 <option value="604800" $selected{'SESSION_TIME'}{'604800'}>$Lang::tr{'one week'}</option>
322 <option value="18144000" $selected{'SESSION_TIME'}{'18144000'}>$Lang::tr{'one month'}</option>
323 </select>
324 </td>
325 </tr>
9735e167
MT
326END
327 }
8b920789 328
297ebdd4
MT
329 print<<END;
330 <tr>
331 <td colspan="2">
332 <br>
333 <strong>$Lang::tr{'Captive branding'}</strong>
334 </td>
335 </tr>
336 <tr>
337 <td>
338 $Lang::tr{'Captive title'}
339 </td>
340 <td>
341 <input type='text' name='TITLE' value="$settings{'TITLE'}" size='40'>
342 </td>
343 </tr>
344 <tr>
345 <td>$Lang::tr{'Captive brand color'}</td>
346 <td>
347 <input type="color" name="COLOR" value="$settings{'COLOR'}">
348 </td>
349 </tr>
350END
351
b32d9e92 352 #Logo Upload
827d3f61
MT
353 print <<END;
354 <tr>
355 <td>
356 $Lang::tr{'Captive logo_upload'}
357 <br>
358 $Lang::tr{'Captive logo_upload1'}
359 </td>
360 <td>
361 <INPUT TYPE='file' NAME='uploaded_file' SIZE=30 MAXLENGTH=80>
362 </td>
363 </tr>
364END
365
b32d9e92
AM
366 #Show Logo in webinterface with 1/2 size if set
367 if (-f "$logopath/logo.png"){
827d3f61 368 print"<tr><td>$Lang::tr{'Captive logo_set'}</td>";
b32d9e92
AM
369 print"<td><img src='/captive/logo/logo.png' alt='$logopath/logo.png' width='25%' height='25%' /></td></tr>";
370 }else{
827d3f61 371 print"<tr><td>$Lang::tr{'Captive logo_set'}</td>";
b32d9e92
AM
372 print"<td><br>$Lang::tr{'no'}</td></tr>";
373 }
827d3f61 374
297ebdd4 375 print <<END;
8b920789 376 <tr>
827d3f61 377 <td></td>
8b920789 378 <td align='right'>
827d3f61 379 <input type='submit' name='ACTION' value="$Lang::tr{'save'}"/>
8b920789
AM
380 </td>
381 </tr>
297ebdd4 382 </table></form>
8b920789 383END
297ebdd4 384
8b920789
AM
385 &Header::closebox();
386
97b91e8a
MT
387 #if settings is set to use coupons, the coupon part has to be displayed
388 if ($settings{'AUTH'} eq 'COUPON'){
389 &coupons();
8b920789
AM
390 }else{
391 #otherwise we show the licensepart
392 &show_license_connections();
393 }
394}
395
8b920789
AM
396sub gencode(){
397 #generate a random code only letters from A-Z except 'O' and 0-9
398 my @chars = ("A".."N", "P".."Z", "0".."9");
399 my $randomstring;
400 $randomstring .= $chars[rand @chars] for 1..8;
401 return $randomstring;
402}
403
97b91e8a
MT
404sub coupons() {
405 &Header::openbox('100%', 'left', $Lang::tr{'Captive generate coupon'});
08061703
AM
406 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>";
407 print "<table border='0' width='100%'>";
408 print "<tr><td width='30%'><br>$Lang::tr{'Captive vouchervalid'}</td><td width='70%'><br>";
409
410 print "<table class='tbl' border='0' width='100%'>";
411 print "<th>$Lang::tr{'hours'}</th><th>$Lang::tr{'days'}</th><th>$Lang::tr{'weeks'}</th><th>$Lang::tr{'months'}</th><th></th><th></th>";
412
413 #print hour-dropdownbox
414 my $hrs=3600;
415 print "<tr height='40px'><td><select name='EXP_HOUR' style='width:8em;'>";
416 print "<option value='0' ";
417 print " selected='selected'" if ($settings{'EXP_HOUR'} eq '0');
418 print ">--</option>";
419 for (my $i = 1; $i<25; $i++){
420 my $exp_sec = $i * $hrs;
421 print "<option value='$exp_sec' ";
422 print " selected='selected'" if ($settings{'EXP_HOUR'} eq $exp_sec);
423 print ">$i</option>";
424 }
425 print "</td><td>";
426
427 #print day-dropdownbox
428 my $days=3600*24;
429 print "<select name='EXP_DAY' style='width:8em;'>";
430 print "<option value='0' ";
431 print " selected='selected'" if ($settings{'EXP_DAY'} eq '0');
432 print ">--</option>";
433 for (my $i = 1; $i<8; $i++){
434 my $exp_sec = $i * $days;
435 print "<option value='$exp_sec' ";
436 print " selected='selected'" if ($settings{'EXP_DAY'} eq $exp_sec);
437 print ">$i</option>";
438 }
439 print "</td><td>";
440
441 #print week-dropdownbox
442 my $week=3600*24*7;
443 print "<select name='EXP_WEEK' style='width:8em;'>";
444 print "<option value='0' ";
445 print " selected='selected'" if ($settings{'EXP_WEEK'} eq '0');
446 print ">--</option>";
447 for (my $i = 1; $i<5; $i++){
448 my $exp_sec = $i * $week;
449 print "<option value='$exp_sec' ";
450 print " selected='selected'" if ($settings{'EXP_WEEK'} eq $exp_sec);
451 print ">$i</option>";
452 }
453 print "</td><td>";
454
455 #print month-dropdownbox
456 my $month=3600*24*30;
457 print "<select name='EXP_MONTH' style='width:8em;'>";
458 print "<option value='0' ";
459 print " selected='selected'" if ($settings{'EXP_MONTH'} eq '0');
460 print ">--</option>";
461 for (my $i = 1; $i<13; $i++){
462 my $exp_sec = $i * $month;
463 print "<option value='$exp_sec' ";
464 print " selected='selected'" if ($settings{'EXP_MONTH'} eq $exp_sec);
465 print ">$i</option>";
466 }
467 print "</td>";
468 print "<td>&nbsp;&nbsp;&nbsp;<input type='checkbox' name='UNLIMITED' $checked{'UNLIMITED'}{'on'} /></td><td>&nbsp;<b>$Lang::tr{'Captive nolimit'}</b></td>";
469 print "</tr></table>";
470 print "</td></tr>";
471 print "<tr><td><br>$Lang::tr{'remark'}</td><td><br><input type='text' style='width: 98%;' name='REMARK' align='left'></td></tr>";
472 print "<tr><td>&nbsp</td><td></td></tr></table><br><br>";
8b920789 473 $cgiparams{'CODE'} = &gencode();
97b91e8a 474 print "<div align='right'><input type='submit' name='ACTION' value='$Lang::tr{'Captive generate coupon'}'><input type='hidden' name='CODE' value='$cgiparams{'CODE'}'></form></div>";
08061703 475
8b920789 476 &Header::closebox();
97b91e8a
MT
477
478 # Show all coupons if exist
479 if (! -z $coupons) {
480 &show_coupons();
481 }
482
483 if (! -z $clients) {
484 &show_clients();
485 }
8b920789
AM
486}
487
488sub show_license_connections(){
489 #if there are active clients, show the box with active connections
490 return if ( -z $clients || ! -f $clients );
491 my $count=0;
492 my $col;
493 &Header::openbox('100%', 'left', $Lang::tr{'Captive voactive'});
494print<<END
495 <center><table class='tbl'>
496 <tr>
97b91e8a 497 <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
498END
499;
500 #read all clients from hash and show table
97b91e8a 501 &General::readhasharray($clients, \%clientshash) if (-e $clients);
8b920789 502 foreach my $key (keys %clientshash){
c7e78cc6
AM
503 my $starttime = sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime($clientshash{$key}[2]));
504 my $endtime;
505 if ($clientshash{$key}[3] eq '0'){
506 $endtime=$Lang::tr{'Captive nolimit'};
507 }else{
508 $endtime=sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime($clientshash{$key}[2]+$clientshash{$key}[3]));
509 }
510
8b920789
AM
511 if ($count % 2){
512 print" <tr>";
513 $col="bgcolor='$color{'color20'}'";
514 }else{
515 $col="bgcolor='$color{'color22'}'";
516 print" <tr>";
517 }
c7e78cc6
AM
518 print "<td $col><center>$clientshash{$key}[4]</td><td $col><center>$starttime ";
519 print "</center></td><td $col><center>$endtime ";
97b91e8a 520 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
521 $count++;
522 }
523
524 print "</table>";
525 &Header::closebox();
526}
527
97b91e8a
MT
528sub show_coupons() {
529 #if there are already generated but unsused coupons, print a table
8b920789
AM
530 my $count=0;
531 my $col;
532 &Header::openbox('100%', 'left', $Lang::tr{'Captive vout'});
533 print<<END
e01c5ab7 534 <center><table class='tbl' border='0'>
8b920789 535 <tr>
97b91e8a 536 <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
537END
538;
97b91e8a
MT
539 &General::readhasharray($coupons, \%couponhash) if (-e $coupons);
540 foreach my $key (keys %couponhash)
8b920789 541 {
97b91e8a 542 my $starttime = sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime($couponhash{$key}[0]));
c7e78cc6 543 my $endtime;
97b91e8a 544 if ($couponhash{$key}[2] eq '0'){
c7e78cc6
AM
545 $endtime=$Lang::tr{'Captive nolimit'};
546 }else{
97b91e8a 547 $endtime=sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime(time()+$couponhash{$key}[2]));
c7e78cc6
AM
548 }
549
8b920789
AM
550 if ($count % 2){
551 print" <tr>";
552 $col="bgcolor='$color{'color20'}'";
553 }else{
554 $col="bgcolor='$color{'color22'}'";
555 print" <tr>";
556 }
c7e78cc6 557
97b91e8a 558 print "<td $col><center><b>$couponhash{$key}[1]</b></td>";
c7e78cc6 559 print "<td $col><center>$starttime</td>";
e01c5ab7 560 print "<td $col><center>$endtime</td>";
97b91e8a
MT
561 print "<td $col align='center'>$couponhash{$key}[3]</td>";
562 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
563 $count++;
564 }
c7e78cc6 565
8b920789
AM
566 print "</table>";
567 &Header::closebox();
568}
569
97b91e8a
MT
570sub show_clients() {
571 #if there are active clients which use coupons show table
8b920789
AM
572 return if ( -z $clients || ! -f $clients );
573 my $count=0;
574 my $col;
575 &Header::openbox('100%', 'left', $Lang::tr{'Captive voactive'});
576print<<END
c7e78cc6 577 <center><table class='tbl' width='100%'>
8b920789 578 <tr>
97b91e8a 579 <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
580END
581;
97b91e8a 582 &General::readhasharray($clients, \%clientshash) if (-e $clients);
8b920789
AM
583 foreach my $key (keys %clientshash)
584 {
c7e78cc6
AM
585 #calculate time from clientshash (starttime)
586 my $starttime = sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime($clientshash{$key}[2]));
587 #calculate endtime from clientshash
588 my $endtime;
589 if ($clientshash{$key}[3] eq '0'){
590 $endtime=$Lang::tr{'Captive nolimit'};
591 }else{
592 $endtime = sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime($clientshash{$key}[2]+$clientshash{$key}[3]));
593 }
e01c5ab7 594
8b920789
AM
595 if ($count % 2){
596 print" <tr>";
597 $col="bgcolor='$color{'color20'}'";
598 }else{
599 $col="bgcolor='$color{'color22'}'";
600 print" <tr>";
601 }
e01c5ab7 602
c7e78cc6
AM
603 print "<td $col><center><b>$clientshash{$key}[4]</b></td><td $col><center>$starttime ";
604 print "</center></td><td $col><center>$endtime</center></td><td $col><center>$clientshash{$key}[0]</td><td $col><center>$clientshash{$key}[5]</center>";
97b91e8a 605 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
606 $count++;
607 }
c7e78cc6 608
8b920789
AM
609 print "</table>";
610 &Header::closebox();
611}
612
e01c5ab7
AM
613sub validremark
614{
615 # Checks a hostname against RFC1035
616 my $remark = $_[0];
617 # Each part should be at least two characters in length
618 # but no more than 63 characters
619 if (length ($remark) < 1 || length ($remark) > 255) {
620 return 0;}
621 # Only valid characters are a-z, A-Z, 0-9 and -
622 if ($remark !~ /^[a-zäöüA-ZÖÄÜ0-9-.:;\|_()\/\s]*$/) {
623 return 0;}
624 # First character can only be a letter or a digit
625 if (substr ($remark, 0, 1) !~ /^[a-zäöüA-ZÖÄÜ0-9]*$/) {
626 return 0;}
627 # Last character can only be a letter or a digit
628 if (substr ($remark, -1, 1) !~ /^[a-zöäüA-ZÖÄÜ0-9.:;_)]*$/) {
629 return 0;}
630 return 1;
631}
632
b32d9e92
AM
633sub pngsize {
634 my $Buffer = shift;
635 my ($width,$height) = ( undef, undef );
636
637 if ($Buffer =~ /IHDR(.{8})/) {
638 my $PNG = $1;
639 ($width,$height) = unpack( "NN", $PNG );
640 } else {
52383f58
AM
641 $width="invalid";
642 $height= "invalid";
b32d9e92
AM
643 };
644 return ($width,$height);
645}
646
8b920789
AM
647sub error{
648 #if an errormessage exits, show a box with errormessage
649 if ($errormessage) {
650 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
651 print "<class name='base'>$errormessage\n";
652 print "&nbsp;</class>\n";
653 &Header::closebox();
654 }
655}
656
657&Header::closebigbox();
658&Header::closepage();