]> git.ipfire.org Git - ipfire-2.x.git/blobdiff - html/cgi-bin/captive/index.cgi
captive: Serve logo through an extra CGI script
[ipfire-2.x.git] / html / cgi-bin / captive / index.cgi
index bcaf8561af3275ccecf5e757b8453af207bf0d13..698384b6a5cac2b1938fd3d3c357c6f63284910b 100755 (executable)
@@ -32,15 +32,14 @@ use HTML::Template;
 require '/var/ipfire/general-functions.pl';
 require "${General::swroot}/lang.pl";
 
-#Set Variables
-my %voucherhash=();
+my $coupons = "${General::swroot}/captive/coupons";
+my %couponhash = ();
+
 my %clientshash=();
 my %cgiparams=();
 my %settings=();
-my $voucherout="${General::swroot}/captive/voucher_out";
 my $clients="${General::swroot}/captive/clients";
 my $settingsfile="${General::swroot}/captive/settings";
-my $redir=0;
 my $errormessage;
 my $url=param('redirect');
 
@@ -55,8 +54,8 @@ unless (-f $clients){ system("touch $clients"); }
 
 # Actions
 if ($cgiparams{'ACTION'} eq "SUBMIT") {
-       #Get Clients IP-Address
-       my $ip_address = $ENV{X_FORWARDED_FOR} || $ENV{REMOTE_ADDR} ||"";
+       # Get client IP address
+       my $ip_address = $ENV{X_FORWARDED_FOR} || $ENV{REMOTE_ADDR};
 
        # Retrieve the MAC address from the ARP table
        my $mac_address = &Network::get_hardware_address($ip_address);
@@ -64,74 +63,78 @@ if ($cgiparams{'ACTION'} eq "SUBMIT") {
        &General::readhasharray("$clients", \%clientshash);
        my $key = &General::findhasharraykey(\%clientshash);
 
-       if (!$errormessage){
-               foreach my $i (0 .. 5) { $clientshash{$key}[$i] = "";}
+       # Create a new client line
+       foreach my $i (0 .. 5) { $clientshash{$key}[$i] = ""; }
 
-               $clientshash{$key}[0] = $mac_address;                   #mac address of actual client
-               $clientshash{$key}[1] = $ip_address;                    #ip address of actual client
-               $clientshash{$key}[2] = time();                                 #actual time in unix seconds (timestamp of first conenction)
-               $clientshash{$key}[3] = $settings{'EXPIRE'};    #Expire time in seconds (1day, 1 week ....)
-               $clientshash{$key}[4] = $Lang::tr{'Captive auth_lic'};  #Type of license (license or voucher)
-               $clientshash{$key}[5] = '';
+       # MAC address of the client
+       $clientshash{$key}[0] = $mac_address;
 
-               &General::writehasharray("$clients", \%clientshash);
-               system("/usr/local/bin/captivectrl");
-               &General::log("Captive", "Internet Access granted via license-agreement for $ip_address until $clientshash{$key}[3]");
-               $redir=1;
-       }       
-}
+       # IP address of the client
+       $clientshash{$key}[1] = $ip_address;
 
-if ($cgiparams{'ACTION'} eq "SUBMIT") {
-       my $ip_address;
-       my $granted=0;
-       #Convert voucherinput to uppercase
-       $cgiparams{'VOUCHER'} = uc $cgiparams{'VOUCHER'};
-       #Get Clients IP-Address
-       $ip_address = $ENV{X_FORWARDED_FOR} || $ENV{REMOTE_ADDR} ||"";
-       #Ask arp to give the corresponding MAC-Address
-       my $mac_address = &Network::get_hardware_address($ip_address);
-       #Check if voucher is valid and write client to clients file, delete voucher from voucherout
-       &General::readhasharray("$voucherout", \%voucherhash);
-       &General::readhasharray("$clients", \%clientshash);
-       foreach my $key (keys %voucherhash) {
-               if($voucherhash{$key}[1] eq $cgiparams{'VOUCHER'}){
-                       #Voucher valid, write to clients, then delete from voucherout
-                       my $key1 = &General::findhasharraykey(\%clientshash);
-                       foreach my $i (0 .. 5) { $clientshash{$key1}[$i] = "";}
-
-                       $clientshash{$key1}[0] = $mac_address;
-                       $clientshash{$key1}[1] = $ip_address;
-                       $clientshash{$key1}[2] = time();
-                       $clientshash{$key1}[3] = $voucherhash{$key}[2];
-                       $clientshash{$key1}[4] = $cgiparams{'VOUCHER'};
-                       $clientshash{$key1}[5] = HTML::Entities::decode_entities($voucherhash{$key}[3]);
-
-                       &General::writehasharray("$clients", \%clientshash);
-                       &General::log("Captive", "Internet Access granted via voucher no. $clientshash{$key1}[4] for $ip_address until $clientshash{$key}[3] Remark: $clientshash{$key1}[7]");
-
-                       delete $voucherhash{$key};
-                       &General::writehasharray("$voucherout", \%voucherhash);
-                       $granted=1;
-                       last;
+       # Current time
+       $clientshash{$key}[2] = time();
+
+       if ($settings{"AUTH"} eq "COUPON") {
+               &General::readhasharray($coupons, \%couponhash);
+
+               # Convert coupon input to uppercase
+               $cgiparams{'COUPON'} = uc $cgiparams{'COUPON'};
+
+               # Walk through all valid coupons and find the right one
+               my $found = 0;
+               foreach my $coupon (keys %couponhash) {
+                       if ($couponhash{$coupon}[1] eq $cgiparams{'COUPON'}) {
+                               $found = 1;
+
+                               # Copy expiry time
+                               $clientshash{$key}[3] = $couponhash{$coupon}[2];
+
+                               # Save coupon code
+                               $clientshash{$key}[4] = $cgiparams{'COUPON'};
+
+                               # Copy coupon remark
+                               $clientshash{$key}[5] = $couponhash{$coupon}[3];
+
+                               # Delete used coupon
+                               delete $couponhash{$coupon};
+                               &General::writehasharray($coupons, \%couponhash);
+
+                               last;
+                       }
+               }
+
+               if ($found == 1) {
+                       &General::log("Captive", "Internet access granted via coupon ($clientshash{$key}[4]) for $ip_address until $clientshash{$key}[3]");
+               } else {
+                       $errormessage = $Lang::tr{"Captive invalid coupon"};
                }
+
+       # License
+       } else {
+               # Copy session expiry time
+               $clientshash{$key}[3] = $settings{'SESSION_TIME'} || "0";
+
+               # No coupon code
+               $clientshash{$key}[4] = "LICENSE";
+
+               &General::log("Captive", "Internet access granted via license agreement for $ip_address until $clientshash{$key}[3]");
        }
-       if($granted==1){
+
+       # If no errors were found, save configruation and reload
+       if (!$errormessage) {
+               &General::writehasharray("$clients", \%clientshash);
+
                system("/usr/local/bin/captivectrl");
-               $redir=1;
-       }else{
-               $errormessage="$Lang::tr{'Captive invalid_voucher'}";
-       }
-}
 
-if($redir == 1){
-       print "Status: 302 Moved Temporarily\n";
-       print "Location: $url\n";
-       print "Connection: close\n";
-       print "\n";
-       exit 0;
+               # Redirect client to the original URL
+               print "Status: 302 Moved Temporarily\n";
+               print "Location: $url\n";
+               print "Connection: close\n\n";
+               exit 0;
+       }
 }
 
-#Open HTML Page, load header and css
 my $tmpl = HTML::Template->new(
        filename => "/srv/web/ipfire/html/captive/template.html",
        die_on_bad_params => 0
@@ -140,20 +143,21 @@ my $tmpl = HTML::Template->new(
 $tmpl->param(REDIRECT_URL => $url);
 
 # Voucher
-if ($settings{'AUTH'} eq "VOUCHER") {
-       $tmpl->param(VOUCHER  => 1);
+if ($settings{'AUTH'} eq "COUPON") {
+       $tmpl->param(COUPON => 1);
 }
 
 $tmpl->param(TITLE => $settings{'TITLE'});
+$tmpl->param(COLOR => $settings{'COLOR'});
 $tmpl->param(ERROR => $errormessage);
 
-$tmpl->param(TAC => &gettac());
+$tmpl->param(TERMS => &getterms());
 
 # Some translated strings
 $tmpl->param(L_ACTIVATE        => $Lang::tr{'Captive ACTIVATE'});
 $tmpl->param(L_GAIN_ACCESS     => $Lang::tr{'Captive GAIN ACCESS'});
-$tmpl->param(L_HEADING_TAC     => $Lang::tr{'Captive heading tac'});
-$tmpl->param(L_HEADING_VOUCHER => $Lang::tr{'Captive heading voucher'});
+$tmpl->param(L_HEADING_COUPON  => $Lang::tr{'Captive coupon'});
+$tmpl->param(L_HEADING_TERMS   => $Lang::tr{'Captive terms'});
 $tmpl->param(L_AGREE_TAC       => $Lang::tr{'Captive agree tac'});
 
 # Print header
@@ -197,20 +201,20 @@ sub getcgihash {
        return;
 }
 
-sub gettac() {
-       my @tac = ();
+sub getterms() {
+       my @terms = ();
 
-       open(my $handle, "<:utf8", "/var/ipfire/captive/agb.txt" ) or die("$!");
+       open(my $handle, "<:utf8", "/var/ipfire/captive/terms.txt");
        while(<$handle>) {
                $_ = HTML::Entities::decode_entities($_);
-               push(@tac, $_);
+               push(@terms, $_);
        }
        close($handle);
 
-       my $tac = join("\n", @tac);
+       my $terms = join("\n", @terms);
 
        # Format paragraphs
-       $tac =~ s/\n\n/<\/p>\n<p>/g;
+       $terms =~ s/\n\n/<\/p>\n<p>/g;
 
-       return $tac;
+       return $terms;
 }