X-Git-Url: http://git.ipfire.org/?p=ipfire-2.x.git;a=blobdiff_plain;f=html%2Fcgi-bin%2Fcaptive.cgi;h=b7c42e797e4fb6964f3755679da8287716123740;hp=b5a7072f3b5af79a5bfa9653945243695674b6ac;hb=462bc3d1595df12dd16a5d93f86a48e5bf33178b;hpb=733932de749c1c92f8f4b38a53cdbf876dd7fe6e diff --git a/html/cgi-bin/captive.cgi b/html/cgi-bin/captive.cgi index b5a7072f3b..b7c42e797e 100755 --- a/html/cgi-bin/captive.cgi +++ b/html/cgi-bin/captive.cgi @@ -20,8 +20,11 @@ ############################################################################### #use strict; +use Encode; use HTML::Entities(); use File::Basename; +use PDF::API2; +use constant mm => 25.4 / 72; # enable only the following on debugging purpose #use warnings; @@ -31,6 +34,19 @@ require '/var/ipfire/general-functions.pl'; require "${General::swroot}/lang.pl"; require "${General::swroot}/header.pl"; +my %session_times = ( + 3600 => $Lang::tr{'one hour'}, + 14400 => $Lang::tr{'four hours'}, + 28800 => $Lang::tr{'eight hours'}, + 43200 => $Lang::tr{'twelve hours'}, + 86400 => $Lang::tr{'24 hours'}, + 604800 => $Lang::tr{'one week'}, + 1209600 => $Lang::tr{'two weeks'}, + 2592000 => $Lang::tr{'one month'}, + 31536000 => $Lang::tr{'one year'}, + 0 => "- $Lang::tr{'unlimited'} -", +); + my %selected = (); my $coupons = "${General::swroot}/captive/coupons"; @@ -57,6 +73,20 @@ unless (-e $settingsfile) { system("touch $settingsfile"); } &General::readhash("$settingsfile", \%settings) if(-f $settingsfile); &General::readhash("${General::swroot}/ethernet/settings", \%netsettings); +if ($cgiparams{'ACTION'} eq "export-coupons") { + my $pdf = &generate_pdf(); + + print "Content-Type: application/pdf\n"; + print "Content-Disposition: attachment; filename=captive-portal-coupons.pdf\n"; + print "\n"; # end headers + + # Send PDF + print $pdf; + + exit(0); +} + + &Header::showhttpheaders(); if ($cgiparams{'ACTION'} eq $Lang::tr{'save'}) { @@ -74,7 +104,7 @@ if ($cgiparams{'ACTION'} eq $Lang::tr{'save'}) { $settings{'ENABLE_GREEN'} = $cgiparams{'ENABLE_GREEN'}; $settings{'ENABLE_BLUE'} = $cgiparams{'ENABLE_BLUE'}; $settings{'AUTH'} = $cgiparams{'AUTH'}; - $settings{'TITLE'} = $cgiparams{'TITLE'}; + $settings{'TITLE'} = &Header::escape($cgiparams{'TITLE'}); $settings{'COLOR'} = $cgiparams{'COLOR'}; $settings{'SESSION_TIME'} = $cgiparams{'SESSION_TIME'}; @@ -97,13 +127,11 @@ if ($cgiparams{'ACTION'} eq $Lang::tr{'save'}) { &General::writehash("$settingsfile", \%settings); # Save terms - if ($cgiparams{'TERMS'}){ - $cgiparams{'TERMS'} = &Header::escape($cgiparams{'TERMS'}); - open(FH, ">:utf8", "/var/ipfire/captive/terms.txt") or die("$!"); - print FH $cgiparams{'TERMS'}; - close(FH); - $cgiparams{'TERMS'} = ""; - } + $cgiparams{'TERMS'} = &Header::escape($cgiparams{'TERMS'}); + open(FH, ">:utf8", "/var/ipfire/captive/terms.txt") or die("$!"); + print FH $cgiparams{'TERMS'}; + close(FH); + $cgiparams{'TERMS'} = ""; #execute binary to reload firewall rules system("/usr/local/bin/captivectrl"); @@ -114,12 +142,7 @@ if ($cgiparams{'ACTION'} eq $Lang::tr{'save'}) { } } -if ($cgiparams{'ACTION'} eq "$Lang::tr{'Captive generate coupon'}") { - # Check expiry time - if ($cgiparams{'EXP_HOUR'} + $cgiparams{'EXP_DAY'} + $cgiparams{'EXP_WEEK'} + $cgiparams{'EXP_MONTH'} == 0 && $cgiparams{'UNLIMITED'} == '') { - $errormessage = $Lang::tr{'Captive noexpiretime'}; - } - +if ($cgiparams{'ACTION'} eq "$Lang::tr{'Captive generate coupons'}") { #check valid remark if ($cgiparams{'REMARK'} ne '' && !&validremark($cgiparams{'REMARK'})){ $errormessage=$Lang::tr{'fwhost err remark'}; @@ -127,7 +150,7 @@ if ($cgiparams{'ACTION'} eq "$Lang::tr{'Captive generate coupon'}") { if (!$errormessage) { # Remember selected values - foreach my $val (("UNLIMITED", "EXP_HOUR", "EXP_DAY", "EXP_WEEK", "EXP_MONTH")) { + foreach my $val (("SESSION_TIME", "COUNT", "REMARK")) { $settings{$val} = $cgiparams{$val}; } &General::writehash($settingsfile, \%settings); @@ -135,17 +158,10 @@ if ($cgiparams{'ACTION'} eq "$Lang::tr{'Captive generate coupon'}") { &General::readhasharray($coupons, \%couponhash) if (-e $coupons); my $now = time(); - # Calculate expiry time in seconds - my $expires = 0; + # Expiry time in seconds + my $expires = $settings{'SESSION_TIME'}; - if ($settings{'UNLIMITED'} ne 'on') { - $expires += $settings{'EXP_HOUR'}; - $expires += $settings{'EXP_DAY'}; - $expires += $settings{'EXP_WEEK'}; - $expires += $settings{'EXP_MONTH'}; - } - - my $count = $cgiparams{'COUNT'} || 1; + my $count = $settings{'COUNT'} || 1; while($count-- > 0) { # Generate a new code my $code = &gencode(); @@ -171,7 +187,7 @@ if ($cgiparams{'ACTION'} eq "$Lang::tr{'Captive generate coupon'}") { $couponhash{$key}[0] = $now; $couponhash{$key}[1] = $code; $couponhash{$key}[2] = $expires; - $couponhash{$key}[3] = $cgiparams{'REMARK'}; + $couponhash{$key}[3] = $settings{'REMARK'}; } # Save everything to disk @@ -221,7 +237,7 @@ if ($cgiparams{'ACTION'} eq 'delete-client') { } #open webpage, print header and open box -&Header::openpage($Lang::tr{'Captive menu'}, 1, ''); +&Header::openpage($Lang::tr{'Captive'}, 1, ''); &Header::openbigbox(); # If an error message exists, show a box with the error message @@ -236,7 +252,6 @@ if ($errormessage) { print <\n - END ; @@ -259,14 +274,34 @@ $selected{'AUTH'}{'TERMS'} = ""; $selected{'AUTH'}{$settings{'AUTH'}} = "selected"; if ($netsettings{'GREEN_DEV'}){ - print ""; + print < + + + +END } + if ($netsettings{'BLUE_DEV'}){ - print ""; + print < + + + +END } print< - - - @@ -355,7 +386,14 @@ if (-e $logo) { END } +my $terms = &getterms(); print < + + +
$Lang::tr{'Captive active on'} Green
+ $Lang::tr{'Captive active on'} + $Lang::tr{'green'} + + +
$Lang::tr{'Captive active on'} Blue
+ $Lang::tr{'Captive active on'} + $Lang::tr{'blue'} + + +
$Lang::tr{'Captive authentication'} @@ -283,31 +318,27 @@ END if ($settings{'AUTH'} eq 'TERMS') { $selected{'SESSION_TIME'} = (); - $selected{'SESSION_TIME'}{'0'} = ""; - $selected{'SESSION_TIME'}{'3600'} = ""; - $selected{'SESSION_TIME'}{'86400'} = ""; - $selected{'SESSION_TIME'}{'604800'} = ""; - $selected{'SESSION_TIME'}{'18144000'} = ""; + foreach my $session_time (keys %session_times) { + $selected{'SESSION_TIME'}{$session_time} = ""; + } $selected{'SESSION_TIME'}{$settings{'SESSION_TIME'}} = "selected"; - my $terms = &getterms(); print < - - -
$Lang::tr{'Captive client session expiry time'}
$Lang::tr{'Captive terms'} + +
@@ -370,11 +408,11 @@ END #if settings is set to use coupons, the coupon part has to be displayed if ($settings{'AUTH'} eq 'COUPON') { &coupons(); -} else { - #otherwise we show the licensepart - &show_license_connections(); } +# Show active clients +&show_clients(); + sub getterms() { my @ret; @@ -396,7 +434,14 @@ sub gencode(){ } sub coupons() { - &Header::openbox('100%', 'left', $Lang::tr{'Captive generate coupon'}); + &Header::openbox('100%', 'left', $Lang::tr{'Captive generate coupons'}); + + $selected{'SESSION_TIME'} = (); + foreach my $session_time (keys %session_times) { + $selected{'SESSION_TIME'}{$session_time} = ""; + } + $selected{'SESSION_TIME'}{$settings{'SESSION_TIME'}} = "selected"; + print < @@ -405,108 +450,51 @@ sub coupons() { $Lang::tr{'Captive vouchervalid'} + + + +
- - - - - - - - + - -
$Lang::tr{'hours'}$Lang::tr{'days'}$Lang::tr{'weeks'}$Lang::tr{'months'}
"; - - #print day-dropdownbox - my $days=3600*24; - print ""; - - #print week-dropdownbox - my $week=3600*24*7; - print ""; - - #print month-dropdownbox - my $month=3600*24*30; - print " - -
+ +END + } + + print <
$Lang::tr{'remark'} - + +
$Lang::tr{'Captive generated coupon no'} +
- - - +
END @@ -517,134 +505,137 @@ END if (! -z $coupons) { &show_coupons(); } - - if (! -z $clients) { - &show_clients(); - } -} - -sub show_license_connections(){ - #if there are active clients, show the box with active connections - return if ( -z $clients || ! -f $clients ); - my $count=0; - my $col; - &Header::openbox('100%', 'left', $Lang::tr{'Captive voactive'}); -print< - - -END -; - #read all clients from hash and show table - &General::readhasharray($clients, \%clientshash) if (-e $clients); - foreach my $key (keys %clientshash){ - my $starttime = sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime($clientshash{$key}[2])); - my $endtime; - if ($clientshash{$key}[3] eq '0'){ - $endtime=$Lang::tr{'Captive nolimit'}; - }else{ - $endtime=sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime($clientshash{$key}[2]+$clientshash{$key}[3])); - } - - if ($count % 2){ - print" "; - $col="bgcolor='$color{'color20'}'"; - }else{ - $col="bgcolor='$color{'color22'}'"; - print" "; - } - print ""; - $count++; - } - - print "
$Lang::tr{'Captive coupon'}$Lang::tr{'Captive activated'}$Lang::tr{'Captive expire'}$Lang::tr{'Captive mac'}$Lang::tr{'delete'}
$clientshash{$key}[4]
$starttime "; - print "
$endtime "; - print "
$clientshash{$key}[0]
"; - &Header::closebox(); } sub show_coupons() { + &General::readhasharray($coupons, \%couponhash) if (-e $coupons); + #if there are already generated but unsused coupons, print a table - my $count=0; - my $col; - &Header::openbox('100%', 'left', $Lang::tr{'Captive vout'}); - print< - - + &Header::openbox('100%', 'left', $Lang::tr{'Captive issued coupons'}); + + print < + + + + + + END -; - &General::readhasharray($coupons, \%couponhash) if (-e $coupons); - foreach my $key (keys %couponhash) - { - my $starttime = sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime($couponhash{$key}[0])); - my $endtime; - if ($couponhash{$key}[2] eq '0'){ - $endtime=$Lang::tr{'Captive nolimit'}; - }else{ - $endtime=sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime(time()+$couponhash{$key}[2])); + + foreach my $key (keys %couponhash) { + my $expirytime = $Lang::tr{'Captive nolimit'}; + if ($couponhash{$key}[2] > 0) { + $expirytime = &General::format_time($couponhash{$key}[2]); } - if ($count % 2){ - print" "; + if ($count++ % 2) { $col="bgcolor='$color{'color20'}'"; - }else{ + } else { $col="bgcolor='$color{'color22'}'"; - print" "; } - print ""; - print ""; - print ""; - print ""; - print ""; - $count++; + print < + + + + + +END } print "
$Lang::tr{'Captive coupon'}$Lang::tr{'date'}$Lang::tr{'Captive expire'}$Lang::tr{'remark'}$Lang::tr{'delete'}
+ $Lang::tr{'Captive coupon'} + $Lang::tr{'Captive expiry time'}$Lang::tr{'remark'}$Lang::tr{'delete'}
$couponhash{$key}[1]
$starttime
$endtime
$couponhash{$key}[3]
+ $couponhash{$key}[1] + + $expirytime + + $couponhash{$key}[3] + +
+ + + +
+
"; + + # Download PDF + print < +
+ + +
+ +END + &Header::closebox(); } sub show_clients() { - #if there are active clients which use coupons show table + # if there are active clients which use coupons show table return if ( -z $clients || ! -f $clients ); + my $count=0; my $col; - &Header::openbox('100%', 'left', $Lang::tr{'Captive voactive'}); -print< - - + + &Header::openbox('100%', 'left', $Lang::tr{'Captive clients'}); + + print < + + + + + + + + END -; + &General::readhasharray($clients, \%clientshash) if (-e $clients); - foreach my $key (keys %clientshash) - { + foreach my $key (keys %clientshash) { #calculate time from clientshash (starttime) my $starttime = sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime($clientshash{$key}[2])); + #calculate endtime from clientshash my $endtime; if ($clientshash{$key}[3] eq '0'){ $endtime=$Lang::tr{'Captive nolimit'}; - }else{ + } else { $endtime = sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime($clientshash{$key}[2]+$clientshash{$key}[3])); } - if ($count % 2){ - print" "; - $col="bgcolor='$color{'color20'}'"; - }else{ - $col="bgcolor='$color{'color22'}'"; - print" "; - } + if ($count++ % 2) { + $col="bgcolor='$color{'color20'}'"; + } else { + $col="bgcolor='$color{'color22'}'"; + } - print ""; - $count++; + my $coupon = ($clientshash{$key}[4] eq "LICENSE") ? $Lang::tr{'Captive terms short'} : $clientshash{$key}[4]; + + print < + + + + + + + +END } print "
$Lang::tr{'Captive coupon'}$Lang::tr{'Captive activated'}$Lang::tr{'Captive expire'}$Lang::tr{'Captive mac'}$Lang::tr{'remark'}$Lang::tr{'delete'}
$Lang::tr{'Captive coupon'}$Lang::tr{'Captive activated'}$Lang::tr{'Captive expiry time'}$Lang::tr{'Captive mac'}$Lang::tr{'remark'}$Lang::tr{'delete'}
$clientshash{$key}[4]
$starttime "; - print "
$endtime
$clientshash{$key}[0]
$clientshash{$key}[5]
"; - print "
$coupon$starttime$endtime$clientshash{$key}[0]$clientshash{$key}[5] +
+ + + +
+
"; + &Header::closebox(); } @@ -668,5 +659,136 @@ sub validremark return 1; } +sub generate_pdf() { + my $pdf = PDF::API2->new(); + + my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = gmtime(time); + my $timestamp = sprintf("D:%04d%02d%02d%02d%02d%02d+00;00", $year+1900, $mon+1, $mday, $hour, $min, $sec); + + $pdf->info( + "Creator" => $Lang::tr{'Captive portal'}, + "Title" => $Lang::tr{'Captive portal coupons'}, + "CreationDate" => $timestamp, + "ModDate" => $timestamp, + ); + + # Set page size + $pdf->mediabox("A4"); + $pdf->trimbox(28/mm, 27/mm, 182/mm, 270/mm); + + # Set font + my $font = $pdf->ttfont("/usr/share/fonts/Ubuntu-R.ttf"); + + my $page_h_margin = 27/mm; + my $page_v_margin = 28/mm; + + my $height = 68/mm; + my $width = 91/mm; + my $margin = 2/mm; + + # Tux Image + my $tux_image = $pdf->image_png("/srv/web/ipfire/html/captive/assets/ipfire.png"); + my $logo_height = 12/mm; + my $logo_width = 12/mm; + + my @coupons = (); + my %coupon_expiry_times = (); + + # Read coupons + &General::readhasharray($coupons, \%couponhash) if (-e $coupons); + foreach my $key (keys %couponhash) { + $coupon_expiry_times{$couponhash{$key}[1]} = $couponhash{$key}[2]; + push @coupons, $couponhash{$key}[1]; + } + + while (@coupons) { + # Make a new page + my $page = $pdf->page(); + + # Graphics + $gfx = $page->gfx(); + + # Headline font + my $f_headline = $page->text(); + $f_headline->font($font, 20); + + # Subheadline font + my $f_subheadline = $page->text(); + $f_subheadline->font($font, 14); + + # Coupon font + my $f_coupon = $page->text(); + $f_coupon->font($font, 36); + + # Lifetime + my $f_lifetime = $page->text(); + $f_lifetime->font($font, 14); + + # Watermark font + my $f_watermark = $page->text(); + $f_watermark->fillcolor("#666666"); + $f_watermark->font($font, 10); + + my $i = 0; + while (@coupons && $i < 8) { + my $coupon = shift @coupons; + + # Box corners + my $x = ($page_v_margin / 2) + (($i % 2) ? $width : 0); + my $y = ($page_h_margin / 2) + (int($i / 2) * $height); + + # Weidth and height of the box + my $w = $width - $margin; + my $h = $height - $margin; + + # Center + my $cx = $x + ($w / 2); + my $cy = $y + ($h / 2); + + # Draw border box + $gfx->strokecolor("#333333"); + $gfx->linedash(1/mm, 1/mm); + $gfx->rect($x, $y, $w, $h); + $gfx->stroke(); + $gfx->endpath(); + + # Headline + $f_headline->translate($cx, ($y + $h - $cy) / 1.7 + $cy); + $f_subheadline->translate($cx, ($y + $h - $cy) / 2.4 + $cy); + + if ($settings{'TITLE'}) { + $f_headline->text_center(decode("utf8", $settings{'TITLE'})); + $f_subheadline->text_center(decode("utf8", $Lang::tr{'Captive WiFi coupon'})); + } else { + $f_headline->text_center(decode("utf8", $Lang::tr{'Captive WiFi coupon'})); + } + + # Coupon + $f_coupon->translate($cx, $cy); + $f_coupon->text_center(decode("utf8", $coupon)); + + # Show lifetime + my $expiry_time = $coupon_expiry_times{$coupon}; + $f_lifetime->translate($cx, $cy - ($y + $h - $cy) / 4); + if ($expiry_time > 0) { + my $lifetime = &General::format_time($expiry_time); + $f_lifetime->text_center(decode("utf8", $Lang::tr{'Captive valid for'} . " " . $lifetime)); + } else { + $f_lifetime->text_center(decode("utf8", $Lang::tr{'Captive nolimit'})); + } + + # Add watermark + $gfx->image($tux_image, $x + $w - $logo_width - $margin, $y + $margin, $logo_width, $logo_height); + $f_watermark->translate($x + $w - ($margin * 2) - $logo_width, $y + ($logo_height / 2)); + $f_watermark->text_right("Powered by IPFire"); + + $i++; + } + } + + # Write out the PDF document + return $pdf->stringify(); +} + &Header::closebigbox(); &Header::closepage();