2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2016 IPFire Team <alexander.marx@ipfire.org> #
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. #
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. #
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/>. #
20 ###############################################################################
27 use constant mm
=> 25.4 / 72;
29 # enable only the following on debugging purpose
31 #use CGI::Carp 'fatalsToBrowser';
33 require '/var/ipfire/general-functions.pl';
34 require "${General::swroot}/lang.pl";
35 require "${General::swroot}/header.pl";
38 3600 => $Lang::tr
{'one hour'},
39 14400 => $Lang::tr
{'four hours'},
40 28800 => $Lang::tr
{'eight hours'},
41 43200 => $Lang::tr
{'twelve hours'},
42 86400 => $Lang::tr
{'24 hours'},
43 604800 => $Lang::tr
{'one week'},
44 1209600 => $Lang::tr
{'two weeks'},
45 2592000 => $Lang::tr
{'one month'},
46 31536000 => $Lang::tr
{'one year'},
47 0 => "- $Lang::tr{'unlimited'} -",
52 my $coupons = "${General::swroot}/captive/coupons";
55 my $logo = "${General::swroot}/captive/logo.dat";
64 my $clients="${General::swroot}/captive/clients";
66 my $settingsfile="${General::swroot}/captive/settings";
67 unless (-e
$settingsfile) { system("touch $settingsfile"); }
69 &Header
::getcgihash
(\
%cgiparams);
71 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
72 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
73 &General
::readhash
("$settingsfile", \
%settings) if(-f
$settingsfile);
74 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
76 if ($cgiparams{'ACTION'} eq "export-coupons") {
77 my $pdf = &generate_pdf
();
79 print "Content-Type: application/pdf\n";
80 print "Content-Disposition: attachment; filename=captive-portal-coupons.pdf\n";
81 print "\n"; # end headers
90 &Header
::showhttpheaders
();
92 if ($cgiparams{'ACTION'} eq $Lang::tr
{'save'}) {
93 my $file = $cgiparams{'logo'};
95 # Check if the file extension is PNG/JPEG
98 my ($name, $path, $ext) = fileparse
($file, qr/\.[^.]*$/);
99 if ($ext ne ".png" && $ext ne ".jpg" && $ext ne ".jpeg") {
100 $errormessage = $Lang::tr
{'Captive wrong ext'};
104 $settings{'ENABLE_GREEN'} = $cgiparams{'ENABLE_GREEN'};
105 $settings{'ENABLE_BLUE'} = $cgiparams{'ENABLE_BLUE'};
106 $settings{'AUTH'} = $cgiparams{'AUTH'};
107 $settings{'TITLE'} = $cgiparams{'TITLE'};
108 $settings{'COLOR'} = $cgiparams{'COLOR'};
109 $settings{'SESSION_TIME'} = $cgiparams{'SESSION_TIME'};
112 #Check if we need to upload a new logo
115 my ($filehandle) = CGI
::upload
("logo");
119 open(FILE
, ">$logo");
121 while (<$filehandle>) {
127 &General
::writehash
("$settingsfile", \
%settings);
130 $cgiparams{'TERMS'} = &Header
::escape
($cgiparams{'TERMS'});
131 open(FH
, ">:utf8", "/var/ipfire/captive/terms.txt") or die("$!");
132 print FH
$cgiparams{'TERMS'};
134 $cgiparams{'TERMS'} = "";
136 #execute binary to reload firewall rules
137 system("/usr/local/bin/captivectrl");
139 if ($cgiparams{'ENABLE_BLUE'} eq 'on'){
140 system("/usr/local/bin/wirelessctrl");
145 if ($cgiparams{'ACTION'} eq "$Lang::tr{'Captive generate coupons'}") {
147 if ($cgiparams{'REMARK'} ne '' && !&validremark
($cgiparams{'REMARK'})){
148 $errormessage=$Lang::tr
{'fwhost err remark'};
151 if (!$errormessage) {
152 # Remember selected values
153 foreach my $val (("SESSION_TIME", "COUNT", "REMARK")) {
154 $settings{$val} = $cgiparams{$val};
156 &General
::writehash
($settingsfile, \
%settings);
158 &General
::readhasharray
($coupons, \
%couponhash) if (-e
$coupons);
161 # Expiry time in seconds
162 my $expires = $settings{'SESSION_TIME'};
164 my $count = $settings{'COUNT'} || 1;
165 while($count-- > 0) {
166 # Generate a new code
167 my $code = &gencode
();
169 # Check if the coupon code already exists
170 foreach my $key (keys %couponhash) {
171 if($couponhash{$key}[1] eq $code) {
172 # Code already exists, so try again
179 next if ($code eq "");
181 # Get a new key from hash
182 my $key = &General
::findhasharraykey
(\
%couponhash);
184 # Initialize all fields
185 foreach my $i (0 .. 3) { $couponhash{$key}[$i] = ""; }
187 $couponhash{$key}[0] = $now;
188 $couponhash{$key}[1] = $code;
189 $couponhash{$key}[2] = $expires;
190 $couponhash{$key}[3] = $settings{'REMARK'};
193 # Save everything to disk
194 &General
::writehasharray
($coupons, \
%couponhash);
198 if ($cgiparams{'ACTION'} eq 'delete-coupon') {
199 #deletes an already generated but unused voucher
201 #read all generated vouchers
202 &General
::readhasharray
($coupons, \
%couponhash) if (-e
$coupons);
203 foreach my $key (keys %couponhash) {
204 if($cgiparams{'key'} eq $couponhash{$key}[0]){
205 #write logenty with decoded remark
206 my $rem=HTML
::Entities
::decode_entities
($couponhash{$key}[4]);
207 &General
::log("Captive", "Delete unused coupon $couponhash{$key}[1] $couponhash{$key}[2] hours valid expires on $couponhash{$key}[3] remark $rem");
208 #delete line from hash
209 delete $couponhash{$key};
214 &General
::writehasharray
($coupons, \
%couponhash);
217 if ($cgiparams{'ACTION'} eq 'delete-client') {
218 #delete voucher and connection in use
220 #read all active clients
221 &General
::readhasharray
($clients, \
%clientshash) if (-e
$clients);
222 foreach my $key (keys %clientshash) {
223 if($cgiparams{'key'} eq $clientshash{$key}[0]){
224 #prepare log entry with decoded remark
225 my $rem=HTML
::Entities
::decode_entities
($clientshash{$key}[7]);
227 &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");
228 #delete line from hash
229 delete $clientshash{$key};
234 &General
::writehasharray
("$clients", \
%clientshash);
235 #reload firewallrules to kill connection of client
236 system("/usr/local/bin/captivectrl");
239 #open webpage, print header and open box
240 &Header
::openpage
($Lang::tr
{'Captive menu'}, 1, '');
241 &Header
::openbigbox
();
243 # If an error message exists, show a box with the error message
245 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
250 # Prints the config box on the website
251 &Header
::openbox
('100%', 'left', $Lang::tr
{'Captive config'});
253 <form method='post' action='$ENV{'SCRIPT_NAME'}' enctype="multipart/form-data">\n
254 <table width='100%' border="0">
258 #check which parameters have to be enabled (from settings file)
259 $checked{'ENABLE_GREEN'}{'off'} = '';
260 $checked{'ENABLE_GREEN'}{'on'} = '';
261 $checked{'ENABLE_GREEN'}{$settings{'ENABLE_GREEN'}} = "checked='checked'";
263 $checked{'ENABLE_BLUE'}{'off'} = '';
264 $checked{'ENABLE_BLUE'}{'on'} = '';
265 $checked{'ENABLE_BLUE'}{$settings{'ENABLE_BLUE'}} = "checked='checked'";
267 $checked{'UNLIMITED'}{'off'} = '';
268 $checked{'UNLIMITED'}{'on'} = '';
269 $checked{'UNLIMITED'}{$settings{'UNLIMITED'}} = "checked='checked'";
271 $selected{'AUTH'} = ();
272 $selected{'AUTH'}{'COUPON'} = "";
273 $selected{'AUTH'}{'TERMS'} = "";
274 $selected{'AUTH'}{$settings{'AUTH'}} = "selected";
276 if ($netsettings{'GREEN_DEV'}){
280 $Lang::tr{'Captive active on'}
281 <font color='$Header::colourgreen'>$Lang::tr{'green'}</font>
284 <input type='checkbox' name='ENABLE_GREEN' $checked{'ENABLE_GREEN'}{'on'} />
290 if ($netsettings{'BLUE_DEV'}){
294 $Lang::tr{'Captive active on'}
295 <font color='$Header::colourblue'>$Lang::tr{'blue'}</font>
298 <input type='checkbox' name='ENABLE_BLUE' $checked{'ENABLE_BLUE'}{'on'} />
307 $Lang::tr{'Captive authentication'}
311 <option value="TERMS" $selected{'AUTH'}{'TERMS'} >$Lang::tr{'Captive terms'}</option>
312 <option value="COUPON" $selected{'AUTH'}{'COUPON'}>$Lang::tr{'Captive coupon'}</option>
319 if ($settings{'AUTH'} eq 'TERMS') {
320 $selected{'SESSION_TIME'} = ();
321 foreach my $session_time (keys %session_times) {
322 $selected{'SESSION_TIME'}{$session_time} = "";
324 $selected{'SESSION_TIME'}{$settings{'SESSION_TIME'}} = "selected";
328 <td>$Lang::tr{'Captive client session expiry time'}</td>
330 <select name="SESSION_TIME">
333 foreach my $session_time (sort { $a <=> $b } keys %session_times) {
335 <option value="$session_time" $selected{'SESSION_TIME'}{$session_time}>
336 $session_times{$session_time}
352 <strong>$Lang::tr{'Captive branding'}</strong>
357 $Lang::tr{'Captive title'}
360 <input type='text' name='TITLE' value="$settings{'TITLE'}" size='40'>
364 <td>$Lang::tr{'Captive brand color'}</td>
366 <input type="color" name="COLOR" value="$settings{'COLOR'}">
371 $Lang::tr{'Captive upload logo'}
374 <input type="file" name="logo">
375 <br>$Lang::tr{'Captive upload logo recommendations'}
383 <td>$Lang::tr{'Captive logo uploaded'}</td>
384 <td>$Lang::tr{'yes'}</td>
389 my $terms = &getterms
();
392 <td>$Lang::tr{'Captive terms'}</td>
394 <textarea cols="50" rows="10" name="TERMS">$terms</textarea>
400 <input type='submit' name='ACTION' value="$Lang::tr{'save'}"/>
408 #if settings is set to use coupons, the coupon part has to be displayed
409 if ($settings{'AUTH'} eq 'COUPON') {
413 # Show active clients
419 open(FILE
, "<:utf8", "/var/ipfire/captive/terms.txt");
421 push(@ret, HTML
::Entities
::decode_entities
($_));
425 return join(/\n/, @ret);
429 #generate a random code only letters from A-Z except 'O' and 0-9
430 my @chars = ("A".."N", "P".."Z", "0".."9");
432 $randomstring .= $chars[rand @chars] for 1..8;
433 return $randomstring;
437 &Header
::openbox
('100%', 'left', $Lang::tr
{'Captive generate coupons'});
439 $selected{'SESSION_TIME'} = ();
440 foreach my $session_time (keys %session_times) {
441 $selected{'SESSION_TIME'}{$session_time} = "";
443 $selected{'SESSION_TIME'}{$settings{'SESSION_TIME'}} = "selected";
446 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
447 <table border='0' width='100%'>
450 $Lang::tr{'Captive vouchervalid'}
453 <select name="SESSION_TIME">
456 foreach my $session_time (sort { $a <=> $b } keys %session_times) {
458 <option value="$session_time" $selected{'SESSION_TIME'}{$session_time}>
459 $session_times{$session_time}
469 <td>$Lang::tr{'remark'}</td>
471 <input type='text' name='REMARK' size=40>
475 <td>$Lang::tr{'Captive generated coupon no'}</td>
477 <select name="COUNT">
478 <option value="1">1</option>
479 <option value="2">2</option>
480 <option value="3">3</option>
481 <option value="4">4</option>
482 <option value="5">5</option>
483 <option value="6">6</option>
484 <option value="7">7</option>
485 <option value="8">8</option>
486 <option value="9">9</option>
487 <option value="10">10</option>
488 <option value="20">20</option>
489 <option value="50">50</option>
490 <option value="100">100</option>
497 <input type="submit" name="ACTION" value="$Lang::tr{'Captive generate coupons'}">
504 # Show all coupons if exist
511 &General
::readhasharray
($coupons, \
%couponhash) if (-e
$coupons);
513 #if there are already generated but unsused coupons, print a table
514 &Header
::openbox
('100%', 'left', $Lang::tr
{'Captive issued coupons'});
517 <table class='tbl' border='0'>
519 <th align='center' width='15%'>
520 $Lang::tr{'Captive coupon'}
522 <th align='center' width='15%'>$Lang::tr{'Captive expiry time'}</th>
523 <th align='center' width='65%'>$Lang::tr{'remark'}</th>
524 <th align='center' width='5%'>$Lang::tr{'delete'}</th>
528 foreach my $key (keys %couponhash) {
529 my $expirytime = $Lang::tr
{'Captive nolimit'};
530 if ($couponhash{$key}[2] > 0) {
531 $expirytime = &General
::format_time
($couponhash{$key}[2]);
535 $col="bgcolor='$color{'color20'}'";
537 $col="bgcolor='$color{'color22'}'";
542 <td $col align="center">
543 <b>$couponhash{$key}[1]</b>
545 <td $col align="center">
548 <td $col align="center">
551 <td $col align="center">
553 <input type='image' src='/images/delete.gif' align='middle' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' />
554 <input type='hidden' name='ACTION' value='delete-coupon' />
555 <input type='hidden' name='key' value='$couponhash{$key}[0]' />
568 <input type="hidden" name="ACTION" value="export-coupons">
569 <input type="submit" value="$Lang::tr{'Captive export coupons'}">
578 # if there are active clients which use coupons show table
579 return if ( -z
$clients || ! -f
$clients );
584 &Header
::openbox
('100%', 'left', $Lang::tr
{'Captive clients'});
587 <table class='tbl' width='100%'>
589 <th align='center' width='15%'>$Lang::tr{'Captive coupon'}</th>
590 <th align='center' width='15%'>$Lang::tr{'Captive activated'}</th>
591 <th align='center' width='15%'>$Lang::tr{'Captive expiry time'}</th>
592 <th align='center' width='10%'>$Lang::tr{'Captive mac'}</th>
593 <th align='center' width='43%'>$Lang::tr{'remark'}</th>
594 <th align='center' width='5%'>$Lang::tr{'delete'}</th>
598 &General
::readhasharray
($clients, \
%clientshash) if (-e
$clients);
599 foreach my $key (keys %clientshash) {
600 #calculate time from clientshash (starttime)
601 my $starttime = sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime($clientshash{$key}[2]));
603 #calculate endtime from clientshash
605 if ($clientshash{$key}[3] eq '0'){
606 $endtime=$Lang::tr
{'Captive nolimit'};
608 $endtime = sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime($clientshash{$key}[2]+$clientshash{$key}[3]));
612 $col="bgcolor='$color{'color20'}'";
614 $col="bgcolor='$color{'color22'}'";
617 my $coupon = ($clientshash{$key}[4] eq "LICENSE") ?
$Lang::tr
{'Captive terms short'} : $clientshash{$key}[4];
621 <td $col align="center"><b>$coupon</b></td>
622 <td $col align="center">$starttime</td>
623 <td $col align="center">$endtime</td>
624 <td $col align="center">$clientshash{$key}[0]</td>
625 <td $col align="center">$clientshash{$key}[5]</td>
626 <td $col align="center">
628 <input type='image' src='/images/delete.gif' align='middle' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' />
629 <input type='hidden' name='ACTION' value='delete-client' />
630 <input type='hidden' name='key' value='$clientshash{$key}[0]' />
644 # Checks a hostname against RFC1035
646 # Each part should be at least two characters in length
647 # but no more than 63 characters
648 if (length ($remark) < 1 || length ($remark) > 255) {
650 # Only valid characters are a-z, A-Z, 0-9 and -
651 if ($remark !~ /^[a-zäöüA-ZÖÄÜ0-9-.:;\|_()\/\s
]*$/) {
653 # First character can only be a letter or a digit
654 if (substr ($remark, 0, 1) !~ /^[a-zäöüA-ZÖÄÜ0-9]*$/) {
656 # Last character can only be a letter or a digit
657 if (substr ($remark, -1, 1) !~ /^[a-zöäüA-ZÖÄÜ0-9.:;_)]*$/) {
663 my $pdf = PDF
::API2
->new();
665 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = gmtime(time);
666 my $timestamp = sprintf("D:%04d%02d%02d%02d%02d%02d+00;00", $year+1900, $mon+1, $mday, $hour, $min, $sec);
669 "Creator" => $Lang::tr
{'Captive portal'},
670 "Title" => $Lang::tr
{'Captive portal coupons'},
671 "CreationDate" => $timestamp,
672 "ModDate" => $timestamp,
676 $pdf->mediabox("A4");
677 $pdf->trimbox(28/mm, 27/mm, 182/mm, 270/mm);
680 my $font = $pdf->ttfont("/usr/share/fonts/Ubuntu-R.ttf");
682 my $page_h_margin = 27/mm
;
683 my $page_v_margin = 28/mm
;
690 my $tux_image = $pdf->image_png("/srv/web/ipfire/html/captive/assets/ipfire.png");
691 my $logo_height = 12/mm
;
692 my $logo_width = 12/mm
;
695 my %coupon_expiry_times = ();
698 &General
::readhasharray
($coupons, \
%couponhash) if (-e
$coupons);
699 foreach my $key (keys %couponhash) {
700 $coupon_expiry_times{$couponhash{$key}[1]} = $couponhash{$key}[2];
701 push @coupons, $couponhash{$key}[1];
706 my $page = $pdf->page();
712 my $f_headline = $page->text();
713 $f_headline->font($font, 20);
716 my $f_subheadline = $page->text();
717 $f_subheadline->font($font, 14);
720 my $f_coupon = $page->text();
721 $f_coupon->font($font, 36);
724 my $f_lifetime = $page->text();
725 $f_lifetime->font($font, 14);
728 my $f_watermark = $page->text();
729 $f_watermark->fillcolor("#666666");
730 $f_watermark->font($font, 10);
733 while (@coupons && $i < 8) {
734 my $coupon = shift @coupons;
737 my $x = ($page_v_margin / 2) + (($i % 2) ?
$width : 0);
738 my $y = ($page_h_margin / 2) + (int($i / 2) * $height);
740 # Weidth and height of the box
741 my $w = $width - $margin;
742 my $h = $height - $margin;
745 my $cx = $x + ($w / 2);
746 my $cy = $y + ($h / 2);
749 $gfx->strokecolor("#333333");
750 $gfx->linedash(1/mm, 1/mm);
751 $gfx->rect($x, $y, $w, $h);
756 $f_headline->translate($cx, ($y + $h - $cy) / 1.7 + $cy);
757 $f_subheadline->translate($cx, ($y + $h - $cy) / 2.4 + $cy);
759 if ($settings{'TITLE'}) {
760 $f_headline->text_center($settings{'TITLE'});
761 $f_subheadline->text_center(decode
("utf8", $Lang::tr
{'Captive WiFi coupon'}));
763 $f_headline->text_center(decode
("utf8", $Lang::tr
{'Captive WiFi coupon'}));
767 $f_coupon->translate($cx, $cy);
768 $f_coupon->text_center(decode
("utf8", $coupon));
771 my $expiry_time = $coupon_expiry_times{$coupon};
772 $f_lifetime->translate($cx, $cy - ($y + $h - $cy) / 4);
773 if ($expiry_time > 0) {
774 my $lifetime = &General
::format_time
($expiry_time);
775 $f_lifetime->text_center(decode
("utf8", $Lang::tr
{'Captive valid for'} . " " . $lifetime));
777 $f_lifetime->text_center(decode
("utf8", $Lang::tr
{'Captive nolimit'}));
781 $gfx->image($tux_image, $x + $w - $logo_width - $margin, $y + $margin, $logo_width, $logo_height);
782 $f_watermark->translate($x + $w - ($margin * 2) - $logo_width, $y + ($logo_height / 2));
783 $f_watermark->text_right("Powered by IPFire");
789 # Write out the PDF document
790 return $pdf->stringify();
793 &Header
::closebigbox
();
794 &Header
::closepage
();