]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame_incremental - html/cgi-bin/captive/index.cgi
captive: Fix some template updates that I forgot
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / captive / index.cgi
... / ...
CommitLineData
1#!/usr/bin/perl
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2016 Alexander Marx 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
22use strict;
23use CGI ':standard';
24use URI::Escape;
25use HTML::Entities();
26use HTML::Template;
27
28# enable only the following on debugging purpose
29#use warnings;
30#use CGI::Carp 'fatalsToBrowser';
31
32require '/var/ipfire/general-functions.pl';
33require "${General::swroot}/lang.pl";
34
35my $coupons = "${General::swroot}/captive/coupons";
36my %couponhash = ();
37
38my %clientshash=();
39my %cgiparams=();
40my %settings=();
41my $clients="${General::swroot}/captive/clients";
42my $settingsfile="${General::swroot}/captive/settings";
43my $errormessage;
44my $url=param('redirect');
45
46#Create /var/ipfire/captive/clients if not exist
47unless (-f $clients){ system("touch $clients"); }
48
49#Get GUI variables
50&getcgihash(\%cgiparams);
51
52#Read settings
53&General::readhash("$settingsfile", \%settings) if(-f $settingsfile);
54
55# Actions
56if ($cgiparams{'ACTION'} eq "SUBMIT") {
57 # Get client IP address
58 my $ip_address = $ENV{X_FORWARDED_FOR} || $ENV{REMOTE_ADDR};
59
60 # Retrieve the MAC address from the ARP table
61 my $mac_address = &Network::get_hardware_address($ip_address);
62
63 &General::readhasharray("$clients", \%clientshash);
64 my $key = &General::findhasharraykey(\%clientshash);
65
66 # Create a new client line
67 foreach my $i (0 .. 5) { $clientshash{$key}[$i] = ""; }
68
69 # MAC address of the client
70 $clientshash{$key}[0] = $mac_address;
71
72 # IP address of the client
73 $clientshash{$key}[1] = $ip_address;
74
75 # Current time
76 $clientshash{$key}[2] = time();
77
78 if ($settings{"AUTH"} eq "COUPON") {
79 &General::readhasharray($coupons, \%couponhash);
80
81 # Convert coupon input to uppercase
82 $cgiparams{'COUPON'} = uc $cgiparams{'COUPON'};
83
84 # Walk through all valid coupons and find the right one
85 my $found = 0;
86 foreach my $coupon (keys %couponhash) {
87 if ($couponhash{$coupon}[1] eq $cgiparams{'COUPON'}) {
88 $found = 1;
89
90 # Copy expiry time
91 $clientshash{$key}[3] = $couponhash{$coupon}[2];
92
93 # Save coupon code
94 $clientshash{$key}[4] = $cgiparams{'COUPON'};
95
96 # Copy coupon remark
97 $clientshash{$key}[5] = $couponhash{$coupon}[3];
98
99 # Delete used coupon
100 delete $couponhash{$coupon};
101 &General::writehasharray($coupons, \%couponhash);
102
103 last;
104 }
105 }
106
107 if ($found == 1) {
108 &General::log("Captive", "Internet access granted via coupon ($clientshash{$key}[4]) for $ip_address until $clientshash{$key}[3]");
109 } else {
110 $errormessage = $Lang::tr{"Captive invalid coupon"};
111 }
112
113 # License
114 } else {
115 # Copy session expiry time
116 $clientshash{$key}[3] = $settings{'SESSION_TIME'} || "0";
117
118 # No coupon code
119 $clientshash{$key}[4] = "TERMS";
120
121 &General::log("Captive", "Internet access granted via license agreement for $ip_address until $clientshash{$key}[3]");
122 }
123
124 # If no errors were found, save configruation and reload
125 if (!$errormessage) {
126 &General::writehasharray("$clients", \%clientshash);
127
128 system("/usr/local/bin/captivectrl");
129
130 # Redirect client to the original URL
131 print "Status: 302 Moved Temporarily\n";
132 print "Location: $url\n";
133 print "Connection: close\n\n";
134 exit 0;
135 }
136}
137
138my $tmpl = HTML::Template->new(
139 filename => "/srv/web/ipfire/html/captive/template.html",
140 die_on_bad_params => 0
141);
142
143$tmpl->param(REDIRECT_URL => $url);
144
145# Coupon
146if ($settings{'AUTH'} eq "COUPON") {
147 $tmpl->param(COUPON => 1);
148 $tmpl->param(L_HEADING => $Lang::tr{'Captive coupon'});
149} else {
150 $tmpl->param(L_HEADING => $Lang::tr{'Captive terms'});
151}
152
153$tmpl->param(TITLE => $settings{'TITLE'});
154$tmpl->param(COLOR => $settings{'COLOR'});
155$tmpl->param(ERROR => $errormessage);
156
157$tmpl->param(TERMS => &getterms());
158
159# Some translated strings
160$tmpl->param(L_ACTIVATE => $Lang::tr{'Captive ACTIVATE'});
161$tmpl->param(L_GAIN_ACCESS => $Lang::tr{'Captive GAIN ACCESS'});
162$tmpl->param(L_AGREE_TERMS => $Lang::tr{'Captive agree tac'});
163
164# Print header
165print "Pragma: no-cache\n";
166print "Cache-control: no-cache\n";
167print "Connection: close\n";
168print "Content-type: text/html\n\n";
169
170# Print rendered template
171print $tmpl->output();
172
173sub getcgihash {
174 my ($hash, $params) = @_;
175 my $cgi = CGI->new ();
176 $hash->{'__CGI__'} = $cgi;
177 return if ($ENV{'REQUEST_METHOD'} ne 'POST');
178 if (!$params->{'wantfile'}) {
179 $CGI::DISABLE_UPLOADS = 1;
180 $CGI::POST_MAX = 1024 * 1024;
181 } else {
182 $CGI::POST_MAX = 10 * 1024 * 1024;
183 }
184 $cgi->referer() =~ m/^http?\:\/\/([^\/]+)/;
185 my $referer = $1;
186 $cgi->url() =~ m/^http?\:\/\/([^\/]+)/;
187 my $servername = $1;
188 return if ($referer ne $servername);
189
190 ### Modified for getting multi-vars, split by |
191 my %temp = $cgi->Vars();
192 foreach my $key (keys %temp) {
193 $hash->{$key} = $temp{$key};
194 $hash->{$key} =~ s/\0/|/g;
195 $hash->{$key} =~ s/^\s*(.*?)\s*$/$1/;
196 }
197
198 if (($params->{'wantfile'})&&($params->{'filevar'})) {
199 $hash->{$params->{'filevar'}} = $cgi->upload
200 ($params->{'filevar'});
201 }
202 return;
203}
204
205sub getterms() {
206 my @terms = ();
207
208 open(my $handle, "<:utf8", "/var/ipfire/captive/terms.txt");
209 while(<$handle>) {
210 $_ = HTML::Entities::decode_entities($_);
211 push(@terms, $_);
212 }
213 close($handle);
214
215 my $terms = join("\n", @terms);
216
217 # Format paragraphs
218 $terms =~ s/\n\n/<\/p>\n<p>/g;
219
220 return $terms;
221}