]> git.ipfire.org Git - ipfire-2.x.git/blobdiff - html/cgi-bin/captive.cgi
captive: Allow PDF export of coupons
[ipfire-2.x.git] / html / cgi-bin / captive.cgi
index 4d2564afe79760616211b67a5b9fa8f89b5dfa53..c595c0e313299e9cae9567fa9570e31d74d0526a 100755 (executable)
 ###############################################################################
 
 #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;
@@ -57,6 +60,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'}) {
@@ -97,13 +114,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");
@@ -285,26 +300,20 @@ if ($settings{'AUTH'} eq 'TERMS') {
        $selected{'SESSION_TIME'} = ();
        $selected{'SESSION_TIME'}{'0'} = "";
        $selected{'SESSION_TIME'}{'3600'} = "";
+       $selected{'SESSION_TIME'}{'28800'} = "";
        $selected{'SESSION_TIME'}{'86400'} = "";
        $selected{'SESSION_TIME'}{'604800'} = "";
        $selected{'SESSION_TIME'}{'18144000'} = "";
        $selected{'SESSION_TIME'}{$settings{'SESSION_TIME'}} = "selected";
 
-       my $terms = &getterms();
        print <<END;
-               <tr>
-                       <td></td>
-                       <td>
-                               <textarea cols="50" rows="10" name="TERMS">$terms</textarea>
-                       </td>
-               </tr>
-
                <tr>
                        <td>$Lang::tr{'Captive client session expiry time'}</td>
                        <td>
                                <select name="SESSION_TIME">
                                        <option value="0"        $selected{'SESSION_TIME'}{'0'}>- $Lang::tr{'unlimited'} -</option>
                                        <option value="3600"     $selected{'SESSION_TIME'}{'3600'}>$Lang::tr{'one hour'}</option>
+                                       <option value="28800"    $selected{'SESSION_TIME'}{'28800'}>$Lang::tr{'eight hours'}</option>
                                        <option value="86400"    $selected{'SESSION_TIME'}{'86400'}>$Lang::tr{'24 hours'}</option>
                                        <option value="604800"   $selected{'SESSION_TIME'}{'604800'}>$Lang::tr{'one week'}</option>
                                        <option value="18144000" $selected{'SESSION_TIME'}{'18144000'}>$Lang::tr{'one month'}</option>
@@ -355,7 +364,14 @@ if (-e $logo) {
 END
 }
 
+my $terms = &getterms();
 print <<END;
+       <tr>
+               <td>$Lang::tr{'Captive terms'}</td>
+               <td>
+                       <textarea cols="50" rows="10" name="TERMS">$terms</textarea>
+               </td>
+       </tr>
        <tr>
                <td></td>
                <td align='right'>
@@ -370,11 +386,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;
 
@@ -517,50 +533,6 @@ 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
-               <center><table class='tbl'>
-               <tr>
-                       <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>
-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" <tr>";
-                       $col="bgcolor='$color{'color20'}'";
-               }else{
-                       $col="bgcolor='$color{'color22'}'";
-                       print" <tr>";
-               }
-               print "<td $col><center>$clientshash{$key}[4]</td><td $col><center>$starttime ";
-               print "</center></td><td $col><center>$endtime ";
-               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>";
-               $count++;
-       }
-       
-       print "</table>";
-       &Header::closebox();
 }
 
 sub show_coupons() {
@@ -617,49 +589,81 @@ END
 
        print "</table>";
 
+       # Download PDF
+       print <<END;
+               <div align="right">
+                       <form method="POST">
+                               <input type="hidden" name="ACTION" value="export-coupons">
+                               <input type="submit" value="$Lang::tr{'Captive export coupons'}">
+                       </form>
+               </div>
+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<<END
-       <center><table class='tbl' width='100%'>
-               <tr>
-                       <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>
+
+       &Header::openbox('100%', 'left', $Lang::tr{'Captive clients'});
+
+       print <<END;
+               <table class='tbl' width='100%'>
+                       <tr>
+                               <th align='center' width='15%'>$Lang::tr{'Captive coupon'}</th>
+                               <th align='center' width='15%'>$Lang::tr{'Captive activated'}</th>
+                               <th align='center' width='15%'>$Lang::tr{'Captive expiry time'}</th>
+                               <th align='center' width='10%'>$Lang::tr{'Captive mac'}</th>
+                               <th align='center' width='43%'>$Lang::tr{'remark'}</th>
+                               <th align='center' width='5%'>$Lang::tr{'delete'}</th>
+                       </tr>
 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" <tr>";
-                               $col="bgcolor='$color{'color20'}'";
-                       }else{
-                               $col="bgcolor='$color{'color22'}'";
-                               print" <tr>";
-                       }
+               if ($count++ % 2) {
+                       $col="bgcolor='$color{'color20'}'";
+               } else {
+                       $col="bgcolor='$color{'color22'}'";
+               }
+
+               my $coupon = ($clientshash{$key}[4] eq "LICENSE") ? $Lang::tr{'Captive terms short'} : $clientshash{$key}[4];
 
-                       print "<td $col><center><b>$clientshash{$key}[4]</b></td><td $col><center>$starttime ";
-                       print "</center></td><td $col><center>$endtime</center></td><td $col><center>$clientshash{$key}[0]</td><td $col><center>$clientshash{$key}[5]</center>";
-                       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>";
-                       $count++;
+               print <<END;
+                       <tr>
+                               <td $col align="center"><b>$coupon</b></td>
+                               <td $col align="center">$starttime</td>
+                               <td $col align="center">$endtime</td>
+                               <td $col align="center">$clientshash{$key}[0]</td>
+                               <td $col align="center">$clientshash{$key}[5]</td>
+                               <td $col align="center">
+                                       <form method='post'>
+                                               <input type='image' src='/images/delete.gif' align='middle' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' />
+                                               <input type='hidden' name='ACTION' value='delete-client' />
+                                               <input type='hidden' name='key' value='$clientshash{$key}[0]' />
+                                       </form>
+                               </td>
+                       </tr>
+END
        }
 
        print "</table>";
+
        &Header::closebox();
 }
 
@@ -683,5 +687,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($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();