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