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