]>
Commit | Line | Data |
---|---|---|
8b920789 AM |
1 | #!/usr/bin/perl |
2 | ############################################################################### | |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
5 | # Copyright (C) 2016 IPFire Team <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 | ||
b32d9e92 | 22 | #use strict; |
a54350cd | 23 | use Encode; |
8b920789 | 24 | use HTML::Entities(); |
b32d9e92 | 25 | use File::Basename; |
a54350cd MT |
26 | use PDF::API2; |
27 | use constant mm => 25.4 / 72; | |
b32d9e92 | 28 | |
8b920789 AM |
29 | # enable only the following on debugging purpose |
30 | #use warnings; | |
31 | #use CGI::Carp 'fatalsToBrowser'; | |
32 | ||
33 | require '/var/ipfire/general-functions.pl'; | |
34 | require "${General::swroot}/lang.pl"; | |
35 | require "${General::swroot}/header.pl"; | |
e14adf75 | 36 | |
278309b9 MT |
37 | my %selected = (); |
38 | ||
97b91e8a MT |
39 | my $coupons = "${General::swroot}/captive/coupons"; |
40 | my %couponhash = (); | |
41 | ||
e2752bfe MT |
42 | my $logo = "${General::swroot}/captive/logo.dat"; |
43 | ||
8b920789 AM |
44 | my %settings=(); |
45 | my %mainsettings; | |
46 | my %color; | |
47 | my %cgiparams=(); | |
48 | my %netsettings=(); | |
49 | my %checked=(); | |
50 | my $errormessage=''; | |
8b920789 | 51 | my $clients="${General::swroot}/captive/clients"; |
8b920789 AM |
52 | my %clientshash=(); |
53 | my $settingsfile="${General::swroot}/captive/settings"; | |
e14adf75 | 54 | unless (-e $settingsfile) { system("touch $settingsfile"); } |
8b920789 AM |
55 | |
56 | &Header::getcgihash(\%cgiparams); | |
57 | ||
58 | &General::readhash("${General::swroot}/main/settings", \%mainsettings); | |
59 | &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color); | |
60 | &General::readhash("$settingsfile", \%settings) if(-f $settingsfile); | |
61 | &General::readhash("${General::swroot}/ethernet/settings", \%netsettings); | |
62 | ||
a54350cd MT |
63 | if ($cgiparams{'ACTION'} eq "export-coupons") { |
64 | my $pdf = &generate_pdf(); | |
65 | ||
66 | print "Content-Type: application/pdf\n"; | |
67 | print "Content-Disposition: attachment; filename=captive-portal-coupons.pdf\n"; | |
68 | print "\n"; # end headers | |
69 | ||
70 | # Send PDF | |
71 | print $pdf; | |
72 | ||
73 | exit(0); | |
74 | } | |
75 | ||
76 | ||
8b920789 AM |
77 | &Header::showhttpheaders(); |
78 | ||
b7a126d9 | 79 | if ($cgiparams{'ACTION'} eq $Lang::tr{'save'}) { |
e2752bfe | 80 | my $file = $cgiparams{'logo'}; |
b7a126d9 MT |
81 | if ($file) { |
82 | # Check if the file extension is PNG/JPEG | |
b32d9e92 | 83 | chomp $file; |
b7a126d9 | 84 | |
b32d9e92 | 85 | my ($name, $path, $ext) = fileparse($file, qr/\.[^.]*$/); |
b7a126d9 MT |
86 | if ($ext ne ".png" && $ext ne ".jpg" && $ext ne ".jpeg") { |
87 | $errormessage = $Lang::tr{'Captive wrong ext'}; | |
b32d9e92 | 88 | } |
c7e78cc6 | 89 | } |
6945954c AM |
90 | |
91 | $settings{'ENABLE_GREEN'} = $cgiparams{'ENABLE_GREEN'}; | |
92 | $settings{'ENABLE_BLUE'} = $cgiparams{'ENABLE_BLUE'}; | |
93 | $settings{'AUTH'} = $cgiparams{'AUTH'}; | |
6945954c | 94 | $settings{'TITLE'} = $cgiparams{'TITLE'}; |
f8d35875 | 95 | $settings{'COLOR'} = $cgiparams{'COLOR'}; |
278309b9 | 96 | $settings{'SESSION_TIME'} = $cgiparams{'SESSION_TIME'}; |
6945954c | 97 | |
b32d9e92 AM |
98 | if (!$errormessage){ |
99 | #Check if we need to upload a new logo | |
e2752bfe MT |
100 | if ($file) { |
101 | # Save logo | |
102 | my ($filehandle) = CGI::upload("logo"); | |
103 | ||
104 | # XXX check filesize | |
105 | ||
106 | open(FILE, ">$logo"); | |
b32d9e92 | 107 | binmode $filehandle; |
e2752bfe MT |
108 | while (<$filehandle>) { |
109 | print FILE; | |
b32d9e92 | 110 | } |
e2752bfe | 111 | close(FILE); |
b32d9e92 | 112 | } |
c7e78cc6 | 113 | |
b32d9e92 AM |
114 | &General::writehash("$settingsfile", \%settings); |
115 | ||
9735e167 | 116 | # Save terms |
e2bd5a6e MT |
117 | $cgiparams{'TERMS'} = &Header::escape($cgiparams{'TERMS'}); |
118 | open(FH, ">:utf8", "/var/ipfire/captive/terms.txt") or die("$!"); | |
119 | print FH $cgiparams{'TERMS'}; | |
120 | close(FH); | |
121 | $cgiparams{'TERMS'} = ""; | |
9735e167 | 122 | |
b32d9e92 AM |
123 | #execute binary to reload firewall rules |
124 | system("/usr/local/bin/captivectrl"); | |
45129439 AM |
125 | |
126 | if ($cgiparams{'ENABLE_BLUE'} eq 'on'){ | |
127 | system("/usr/local/bin/wirelessctrl"); | |
128 | } | |
8b920789 | 129 | } |
8b920789 AM |
130 | } |
131 | ||
5cd9e28b | 132 | if ($cgiparams{'ACTION'} eq "$Lang::tr{'Captive generate coupon'}") { |
733932de MT |
133 | # Check expiry time |
134 | if ($cgiparams{'EXP_HOUR'} + $cgiparams{'EXP_DAY'} + $cgiparams{'EXP_WEEK'} + $cgiparams{'EXP_MONTH'} == 0 && $cgiparams{'UNLIMITED'} == '') { | |
135 | $errormessage = $Lang::tr{'Captive noexpiretime'}; | |
08061703 | 136 | } |
8b920789 | 137 | |
e01c5ab7 AM |
138 | #check valid remark |
139 | if ($cgiparams{'REMARK'} ne '' && !&validremark($cgiparams{'REMARK'})){ | |
140 | $errormessage=$Lang::tr{'fwhost err remark'}; | |
141 | } | |
142 | ||
5cd9e28b | 143 | if (!$errormessage) { |
9cba29f1 MT |
144 | # Remember selected values |
145 | foreach my $val (("UNLIMITED", "EXP_HOUR", "EXP_DAY", "EXP_WEEK", "EXP_MONTH")) { | |
146 | $settings{$val} = $cgiparams{$val}; | |
147 | } | |
148 | &General::writehash($settingsfile, \%settings); | |
149 | ||
5cd9e28b MT |
150 | &General::readhasharray($coupons, \%couponhash) if (-e $coupons); |
151 | my $now = time(); | |
152 | ||
733932de MT |
153 | # Calculate expiry time in seconds |
154 | my $expires = 0; | |
155 | ||
156 | if ($settings{'UNLIMITED'} ne 'on') { | |
157 | $expires += $settings{'EXP_HOUR'}; | |
158 | $expires += $settings{'EXP_DAY'}; | |
159 | $expires += $settings{'EXP_WEEK'}; | |
160 | $expires += $settings{'EXP_MONTH'}; | |
161 | } | |
162 | ||
5cd9e28b MT |
163 | my $count = $cgiparams{'COUNT'} || 1; |
164 | while($count-- > 0) { | |
165 | # Generate a new code | |
166 | my $code = &gencode(); | |
167 | ||
168 | # Check if the coupon code already exists | |
169 | foreach my $key (keys %couponhash) { | |
170 | if($couponhash{$key}[1] eq $code) { | |
171 | # Code already exists, so try again | |
172 | $code = ""; | |
173 | $count++; | |
174 | last; | |
175 | } | |
176 | } | |
177 | ||
178 | next if ($code eq ""); | |
179 | ||
180 | # Get a new key from hash | |
181 | my $key = &General::findhasharraykey(\%couponhash); | |
182 | ||
183 | # Initialize all fields | |
184 | foreach my $i (0 .. 3) { $couponhash{$key}[$i] = ""; } | |
8b920789 | 185 | |
5cd9e28b MT |
186 | $couponhash{$key}[0] = $now; |
187 | $couponhash{$key}[1] = $code; | |
188 | $couponhash{$key}[2] = $expires; | |
189 | $couponhash{$key}[3] = $cgiparams{'REMARK'}; | |
190 | } | |
8b920789 | 191 | |
5cd9e28b MT |
192 | # Save everything to disk |
193 | &General::writehasharray($coupons, \%couponhash); | |
8b920789 AM |
194 | } |
195 | } | |
196 | ||
97b91e8a | 197 | if ($cgiparams{'ACTION'} eq 'delete-coupon') { |
8b920789 AM |
198 | #deletes an already generated but unused voucher |
199 | ||
200 | #read all generated vouchers | |
97b91e8a MT |
201 | &General::readhasharray($coupons, \%couponhash) if (-e $coupons); |
202 | foreach my $key (keys %couponhash) { | |
203 | if($cgiparams{'key'} eq $couponhash{$key}[0]){ | |
8b920789 | 204 | #write logenty with decoded remark |
97b91e8a MT |
205 | my $rem=HTML::Entities::decode_entities($couponhash{$key}[4]); |
206 | &General::log("Captive", "Delete unused coupon $couponhash{$key}[1] $couponhash{$key}[2] hours valid expires on $couponhash{$key}[3] remark $rem"); | |
8b920789 | 207 | #delete line from hash |
97b91e8a | 208 | delete $couponhash{$key}; |
8b920789 AM |
209 | last; |
210 | } | |
211 | } | |
212 | #write back hash | |
97b91e8a | 213 | &General::writehasharray($coupons, \%couponhash); |
8b920789 AM |
214 | } |
215 | ||
97b91e8a | 216 | if ($cgiparams{'ACTION'} eq 'delete-client') { |
8b920789 AM |
217 | #delete voucher and connection in use |
218 | ||
219 | #read all active clients | |
97b91e8a | 220 | &General::readhasharray($clients, \%clientshash) if (-e $clients); |
8b920789 AM |
221 | foreach my $key (keys %clientshash) { |
222 | if($cgiparams{'key'} eq $clientshash{$key}[0]){ | |
223 | #prepare log entry with decoded remark | |
224 | my $rem=HTML::Entities::decode_entities($clientshash{$key}[7]); | |
225 | #write logentry | |
97b91e8a | 226 | &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"); |
8b920789 AM |
227 | #delete line from hash |
228 | delete $clientshash{$key}; | |
229 | last; | |
230 | } | |
231 | } | |
232 | #write back hash | |
233 | &General::writehasharray("$clients", \%clientshash); | |
234 | #reload firewallrules to kill connection of client | |
235 | system("/usr/local/bin/captivectrl"); | |
236 | } | |
237 | ||
238 | #open webpage, print header and open box | |
239 | &Header::openpage($Lang::tr{'Captive menu'}, 1, ''); | |
240 | &Header::openbigbox(); | |
241 | ||
a41fe924 MT |
242 | # If an error message exists, show a box with the error message |
243 | if ($errormessage) { | |
244 | &Header::openbox('100%', 'left', $Lang::tr{'error messages'}); | |
245 | print $errormessage; | |
246 | &Header::closebox(); | |
8b920789 AM |
247 | } |
248 | ||
a41fe924 MT |
249 | # Prints the config box on the website |
250 | &Header::openbox('100%', 'left', $Lang::tr{'Captive config'}); | |
251 | print <<END | |
252 | <form method='post' action='$ENV{'SCRIPT_NAME'}' enctype="multipart/form-data">\n | |
8b920789 | 253 | <table width='100%' border="0"> |
8b920789 AM |
254 | END |
255 | ; | |
e01c5ab7 | 256 | |
a41fe924 MT |
257 | #check which parameters have to be enabled (from settings file) |
258 | $checked{'ENABLE_GREEN'}{'off'} = ''; | |
259 | $checked{'ENABLE_GREEN'}{'on'} = ''; | |
260 | $checked{'ENABLE_GREEN'}{$settings{'ENABLE_GREEN'}} = "checked='checked'"; | |
8b920789 | 261 | |
a41fe924 MT |
262 | $checked{'ENABLE_BLUE'}{'off'} = ''; |
263 | $checked{'ENABLE_BLUE'}{'on'} = ''; | |
264 | $checked{'ENABLE_BLUE'}{$settings{'ENABLE_BLUE'}} = "checked='checked'"; | |
c7e78cc6 | 265 | |
a41fe924 MT |
266 | $checked{'UNLIMITED'}{'off'} = ''; |
267 | $checked{'UNLIMITED'}{'on'} = ''; | |
268 | $checked{'UNLIMITED'}{$settings{'UNLIMITED'}} = "checked='checked'"; | |
269 | ||
e7d16ea5 MT |
270 | $selected{'AUTH'} = (); |
271 | $selected{'AUTH'}{'COUPON'} = ""; | |
272 | $selected{'AUTH'}{'TERMS'} = ""; | |
273 | $selected{'AUTH'}{$settings{'AUTH'}} = "selected"; | |
274 | ||
a41fe924 | 275 | if ($netsettings{'GREEN_DEV'}){ |
f3217495 MT |
276 | print <<END; |
277 | <tr> | |
278 | <td width='30%'> | |
279 | $Lang::tr{'Captive active on'} | |
280 | <font color='$Header::colourgreen'>$Lang::tr{'green'}</font> | |
281 | </td> | |
282 | <td> | |
283 | <input type='checkbox' name='ENABLE_GREEN' $checked{'ENABLE_GREEN'}{'on'} /> | |
284 | </td> | |
285 | </tr> | |
286 | END | |
a41fe924 | 287 | } |
f3217495 | 288 | |
a41fe924 | 289 | if ($netsettings{'BLUE_DEV'}){ |
f3217495 MT |
290 | print <<END; |
291 | <tr> | |
292 | <td width='30%'> | |
293 | $Lang::tr{'Captive active on'} | |
294 | <font color='$Header::colourblue'>$Lang::tr{'blue'}</font> | |
295 | </td> | |
296 | <td> | |
297 | <input type='checkbox' name='ENABLE_BLUE' $checked{'ENABLE_BLUE'}{'on'} /> | |
298 | </td> | |
299 | </tr> | |
300 | END | |
a41fe924 | 301 | } |
e01c5ab7 | 302 | |
e01c5ab7 | 303 | print<<END |
a41fe924 MT |
304 | <tr> |
305 | <td> | |
306 | $Lang::tr{'Captive authentication'} | |
307 | </td> | |
308 | <td> | |
309 | <select name='AUTH'> | |
e7d16ea5 MT |
310 | <option value="TERMS" $selected{'AUTH'}{'TERMS'} >$Lang::tr{'Captive terms'}</option> |
311 | <option value="COUPON" $selected{'AUTH'}{'COUPON'}>$Lang::tr{'Captive coupon'}</option> | |
a41fe924 MT |
312 | </select> |
313 | </td> | |
314 | </tr> | |
8b920789 AM |
315 | END |
316 | ; | |
8b920789 | 317 | |
a41fe924 MT |
318 | if ($settings{'AUTH'} eq 'TERMS') { |
319 | $selected{'SESSION_TIME'} = (); | |
320 | $selected{'SESSION_TIME'}{'0'} = ""; | |
321 | $selected{'SESSION_TIME'}{'3600'} = ""; | |
bbc69f22 | 322 | $selected{'SESSION_TIME'}{'28800'} = ""; |
a41fe924 MT |
323 | $selected{'SESSION_TIME'}{'86400'} = ""; |
324 | $selected{'SESSION_TIME'}{'604800'} = ""; | |
325 | $selected{'SESSION_TIME'}{'18144000'} = ""; | |
326 | $selected{'SESSION_TIME'}{$settings{'SESSION_TIME'}} = "selected"; | |
327 | ||
a41fe924 | 328 | print <<END; |
297ebdd4 | 329 | <tr> |
a41fe924 | 330 | <td>$Lang::tr{'Captive client session expiry time'}</td> |
297ebdd4 | 331 | <td> |
a41fe924 MT |
332 | <select name="SESSION_TIME"> |
333 | <option value="0" $selected{'SESSION_TIME'}{'0'}>- $Lang::tr{'unlimited'} -</option> | |
334 | <option value="3600" $selected{'SESSION_TIME'}{'3600'}>$Lang::tr{'one hour'}</option> | |
0a219160 | 335 | <option value="28800" $selected{'SESSION_TIME'}{'28800'}>$Lang::tr{'eight hours'}</option> |
a41fe924 MT |
336 | <option value="86400" $selected{'SESSION_TIME'}{'86400'}>$Lang::tr{'24 hours'}</option> |
337 | <option value="604800" $selected{'SESSION_TIME'}{'604800'}>$Lang::tr{'one week'}</option> | |
338 | <option value="18144000" $selected{'SESSION_TIME'}{'18144000'}>$Lang::tr{'one month'}</option> | |
339 | </select> | |
297ebdd4 MT |
340 | </td> |
341 | </tr> | |
342 | END | |
a41fe924 | 343 | } |
297ebdd4 | 344 | |
a41fe924 MT |
345 | print<<END; |
346 | <tr> | |
347 | <td colspan="2"> | |
348 | <br> | |
349 | <strong>$Lang::tr{'Captive branding'}</strong> | |
350 | </td> | |
351 | </tr> | |
352 | <tr> | |
353 | <td> | |
354 | $Lang::tr{'Captive title'} | |
355 | </td> | |
356 | <td> | |
357 | <input type='text' name='TITLE' value="$settings{'TITLE'}" size='40'> | |
358 | </td> | |
359 | </tr> | |
360 | <tr> | |
361 | <td>$Lang::tr{'Captive brand color'}</td> | |
362 | <td> | |
363 | <input type="color" name="COLOR" value="$settings{'COLOR'}"> | |
364 | </td> | |
365 | </tr> | |
a41fe924 MT |
366 | <tr> |
367 | <td> | |
81019811 | 368 | $Lang::tr{'Captive upload logo'} |
a41fe924 MT |
369 | </td> |
370 | <td> | |
371 | <input type="file" name="logo"> | |
81019811 | 372 | <br>$Lang::tr{'Captive upload logo recommendations'} |
a41fe924 MT |
373 | </td> |
374 | </tr> | |
e2752bfe | 375 | END |
827d3f61 | 376 | |
a41fe924 | 377 | if (-e $logo) { |
297ebdd4 | 378 | print <<END; |
8b920789 | 379 | <tr> |
a41fe924 MT |
380 | <td>$Lang::tr{'Captive logo uploaded'}</td> |
381 | <td>$Lang::tr{'yes'}</td> | |
8b920789 | 382 | </tr> |
a41fe924 MT |
383 | END |
384 | } | |
385 | ||
e2bd5a6e | 386 | my $terms = &getterms(); |
a41fe924 | 387 | print <<END; |
e2bd5a6e | 388 | <tr> |
ebfb8996 | 389 | <td>$Lang::tr{'Captive terms'}</td> |
e2bd5a6e MT |
390 | <td> |
391 | <textarea cols="50" rows="10" name="TERMS">$terms</textarea> | |
392 | </td> | |
393 | </tr> | |
a41fe924 MT |
394 | <tr> |
395 | <td></td> | |
396 | <td align='right'> | |
397 | <input type='submit' name='ACTION' value="$Lang::tr{'save'}"/> | |
398 | </td> | |
399 | </tr> | |
297ebdd4 | 400 | </table></form> |
8b920789 | 401 | END |
297ebdd4 | 402 | |
a41fe924 | 403 | &Header::closebox(); |
8b920789 | 404 | |
a41fe924 MT |
405 | #if settings is set to use coupons, the coupon part has to be displayed |
406 | if ($settings{'AUTH'} eq 'COUPON') { | |
407 | &coupons(); | |
a41fe924 MT |
408 | } |
409 | ||
792f1a3b MT |
410 | # Show active clients |
411 | &show_clients(); | |
412 | ||
a41fe924 MT |
413 | sub getterms() { |
414 | my @ret; | |
415 | ||
416 | open(FILE, "<:utf8", "/var/ipfire/captive/terms.txt"); | |
417 | while(<FILE>) { | |
418 | push(@ret, HTML::Entities::decode_entities($_)); | |
8b920789 | 419 | } |
a41fe924 MT |
420 | close(FILE); |
421 | ||
422 | return join(/\n/, @ret); | |
8b920789 AM |
423 | } |
424 | ||
8b920789 AM |
425 | sub gencode(){ |
426 | #generate a random code only letters from A-Z except 'O' and 0-9 | |
427 | my @chars = ("A".."N", "P".."Z", "0".."9"); | |
428 | my $randomstring; | |
429 | $randomstring .= $chars[rand @chars] for 1..8; | |
430 | return $randomstring; | |
431 | } | |
432 | ||
97b91e8a MT |
433 | sub coupons() { |
434 | &Header::openbox('100%', 'left', $Lang::tr{'Captive generate coupon'}); | |
4f75fbfe MT |
435 | print <<END; |
436 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
437 | <table border='0' width='100%'> | |
438 | <tr> | |
439 | <td width='30%'> | |
440 | $Lang::tr{'Captive vouchervalid'} | |
441 | </td> | |
442 | <td width='70%'> | |
443 | <table class='tbl' border='0' width='100%'> | |
444 | <tr> | |
445 | <th>$Lang::tr{'hours'}</th> | |
446 | <th>$Lang::tr{'days'}</th> | |
447 | <th>$Lang::tr{'weeks'}</th> | |
448 | <th>$Lang::tr{'months'}</th> | |
449 | <th></th> | |
450 | </tr> | |
451 | END | |
08061703 AM |
452 | |
453 | #print hour-dropdownbox | |
454 | my $hrs=3600; | |
455 | print "<tr height='40px'><td><select name='EXP_HOUR' style='width:8em;'>"; | |
456 | print "<option value='0' "; | |
457 | print " selected='selected'" if ($settings{'EXP_HOUR'} eq '0'); | |
458 | print ">--</option>"; | |
459 | for (my $i = 1; $i<25; $i++){ | |
460 | my $exp_sec = $i * $hrs; | |
461 | print "<option value='$exp_sec' "; | |
462 | print " selected='selected'" if ($settings{'EXP_HOUR'} eq $exp_sec); | |
463 | print ">$i</option>"; | |
464 | } | |
465 | print "</td><td>"; | |
466 | ||
467 | #print day-dropdownbox | |
468 | my $days=3600*24; | |
469 | print "<select name='EXP_DAY' style='width:8em;'>"; | |
470 | print "<option value='0' "; | |
471 | print " selected='selected'" if ($settings{'EXP_DAY'} eq '0'); | |
472 | print ">--</option>"; | |
473 | for (my $i = 1; $i<8; $i++){ | |
474 | my $exp_sec = $i * $days; | |
475 | print "<option value='$exp_sec' "; | |
476 | print " selected='selected'" if ($settings{'EXP_DAY'} eq $exp_sec); | |
477 | print ">$i</option>"; | |
478 | } | |
479 | print "</td><td>"; | |
480 | ||
481 | #print week-dropdownbox | |
482 | my $week=3600*24*7; | |
483 | print "<select name='EXP_WEEK' style='width:8em;'>"; | |
484 | print "<option value='0' "; | |
485 | print " selected='selected'" if ($settings{'EXP_WEEK'} eq '0'); | |
486 | print ">--</option>"; | |
487 | for (my $i = 1; $i<5; $i++){ | |
488 | my $exp_sec = $i * $week; | |
489 | print "<option value='$exp_sec' "; | |
490 | print " selected='selected'" if ($settings{'EXP_WEEK'} eq $exp_sec); | |
491 | print ">$i</option>"; | |
492 | } | |
493 | print "</td><td>"; | |
494 | ||
495 | #print month-dropdownbox | |
496 | my $month=3600*24*30; | |
497 | print "<select name='EXP_MONTH' style='width:8em;'>"; | |
498 | print "<option value='0' "; | |
499 | print " selected='selected'" if ($settings{'EXP_MONTH'} eq '0'); | |
500 | print ">--</option>"; | |
501 | for (my $i = 1; $i<13; $i++){ | |
502 | my $exp_sec = $i * $month; | |
503 | print "<option value='$exp_sec' "; | |
504 | print " selected='selected'" if ($settings{'EXP_MONTH'} eq $exp_sec); | |
505 | print ">$i</option>"; | |
506 | } | |
4f75fbfe MT |
507 | print <<END; |
508 | </td> | |
509 | <td> | |
510 | <label> | |
511 | <input type='checkbox' name='UNLIMITED' $checked{'UNLIMITED'}{'on'} /> | |
512 | $Lang::tr{'Captive nolimit'} | |
513 | </label> | |
514 | </td> | |
515 | </tr> | |
516 | </table> | |
517 | </td> | |
518 | </tr> | |
519 | <tr> | |
520 | <td>$Lang::tr{'remark'}</td> | |
521 | <td> | |
522 | <input type='text' style='width: 98%;' name='REMARK' align='left'> | |
523 | </td> | |
524 | </tr> | |
525 | </table> | |
526 | ||
527 | <div align="right"> | |
5cd9e28b MT |
528 | <select name="COUNT"> |
529 | <option value="1">1</option> | |
530 | <option value="2">2</option> | |
531 | <option value="3">3</option> | |
532 | <option value="4">4</option> | |
533 | <option value="5">5</option> | |
534 | <option value="6">6</option> | |
535 | <option value="7">7</option> | |
536 | <option value="8">8</option> | |
537 | <option value="9">9</option> | |
538 | <option value="10">10</option> | |
539 | <option value="20">20</option> | |
540 | <option value="50">50</option> | |
541 | <option value="100">100</option> | |
542 | </select> | |
543 | ||
4f75fbfe MT |
544 | <input type="submit" name="ACTION" value="$Lang::tr{'Captive generate coupon'}"> |
545 | </div> | |
546 | </form> | |
547 | END | |
08061703 | 548 | |
8b920789 | 549 | &Header::closebox(); |
97b91e8a MT |
550 | |
551 | # Show all coupons if exist | |
552 | if (! -z $coupons) { | |
553 | &show_coupons(); | |
554 | } | |
8b920789 AM |
555 | } |
556 | ||
97b91e8a | 557 | sub show_coupons() { |
a0b271e4 MT |
558 | &General::readhasharray($coupons, \%couponhash) if (-e $coupons); |
559 | ||
97b91e8a | 560 | #if there are already generated but unsused coupons, print a table |
a0b271e4 MT |
561 | &Header::openbox('100%', 'left', $Lang::tr{'Captive issued coupons'}); |
562 | ||
563 | print <<END; | |
564 | <table class='tbl' border='0'> | |
565 | <tr> | |
566 | <th align='center' width='15%'> | |
567 | $Lang::tr{'Captive coupon'} | |
568 | </th> | |
569 | <th align='center' width='15%'>$Lang::tr{'Captive expiry time'}</th> | |
570 | <th align='center' width='65%'>$Lang::tr{'remark'}</th> | |
571 | <th align='center' width='5%'>$Lang::tr{'delete'}</th> | |
572 | </tr> | |
8b920789 | 573 | END |
a0b271e4 MT |
574 | |
575 | foreach my $key (keys %couponhash) { | |
576 | my $expirytime = $Lang::tr{'Captive nolimit'}; | |
577 | if ($couponhash{$key}[2] > 0) { | |
578 | $expirytime = &General::format_time($couponhash{$key}[2]); | |
c7e78cc6 AM |
579 | } |
580 | ||
a0b271e4 | 581 | if ($count++ % 2) { |
8b920789 | 582 | $col="bgcolor='$color{'color20'}'"; |
a0b271e4 | 583 | } else { |
8b920789 | 584 | $col="bgcolor='$color{'color22'}'"; |
8b920789 | 585 | } |
c7e78cc6 | 586 | |
a0b271e4 MT |
587 | print <<END; |
588 | <tr> | |
589 | <td $col align="center"> | |
590 | <b>$couponhash{$key}[1]</b> | |
591 | </td> | |
592 | <td $col align="center"> | |
593 | $expirytime | |
594 | </td> | |
595 | <td $col align="center"> | |
596 | $couponhash{$key}[3] | |
597 | </td> | |
598 | <td $col align="center"> | |
599 | <form method='post'> | |
600 | <input type='image' src='/images/delete.gif' align='middle' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' /> | |
601 | <input type='hidden' name='ACTION' value='delete-coupon' /> | |
602 | <input type='hidden' name='key' value='$couponhash{$key}[0]' /> | |
603 | </form> | |
604 | </td> | |
605 | </tr> | |
606 | END | |
8b920789 | 607 | } |
c7e78cc6 | 608 | |
8b920789 | 609 | print "</table>"; |
a0b271e4 | 610 | |
a54350cd MT |
611 | # Download PDF |
612 | print <<END; | |
613 | <div align="right"> | |
614 | <form method="POST"> | |
615 | <input type="hidden" name="ACTION" value="export-coupons"> | |
616 | <input type="submit" value="$Lang::tr{'Captive export coupons'}"> | |
617 | </form> | |
618 | </div> | |
619 | END | |
620 | ||
8b920789 AM |
621 | &Header::closebox(); |
622 | } | |
623 | ||
97b91e8a | 624 | sub show_clients() { |
41964aba | 625 | # if there are active clients which use coupons show table |
8b920789 | 626 | return if ( -z $clients || ! -f $clients ); |
41964aba | 627 | |
8b920789 AM |
628 | my $count=0; |
629 | my $col; | |
41964aba MT |
630 | |
631 | &Header::openbox('100%', 'left', $Lang::tr{'Captive clients'}); | |
632 | ||
633 | print <<END; | |
634 | <table class='tbl' width='100%'> | |
635 | <tr> | |
636 | <th align='center' width='15%'>$Lang::tr{'Captive coupon'}</th> | |
637 | <th align='center' width='15%'>$Lang::tr{'Captive activated'}</th> | |
638 | <th align='center' width='15%'>$Lang::tr{'Captive expiry time'}</th> | |
639 | <th align='center' width='10%'>$Lang::tr{'Captive mac'}</th> | |
640 | <th align='center' width='43%'>$Lang::tr{'remark'}</th> | |
641 | <th align='center' width='5%'>$Lang::tr{'delete'}</th> | |
642 | </tr> | |
8b920789 | 643 | END |
41964aba | 644 | |
97b91e8a | 645 | &General::readhasharray($clients, \%clientshash) if (-e $clients); |
41964aba | 646 | foreach my $key (keys %clientshash) { |
c7e78cc6 AM |
647 | #calculate time from clientshash (starttime) |
648 | my $starttime = sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime($clientshash{$key}[2])); | |
41964aba | 649 | |
c7e78cc6 AM |
650 | #calculate endtime from clientshash |
651 | my $endtime; | |
652 | if ($clientshash{$key}[3] eq '0'){ | |
653 | $endtime=$Lang::tr{'Captive nolimit'}; | |
41964aba | 654 | } else { |
c7e78cc6 AM |
655 | $endtime = sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime($clientshash{$key}[2]+$clientshash{$key}[3])); |
656 | } | |
e01c5ab7 | 657 | |
41964aba MT |
658 | if ($count++ % 2) { |
659 | $col="bgcolor='$color{'color20'}'"; | |
660 | } else { | |
661 | $col="bgcolor='$color{'color22'}'"; | |
662 | } | |
663 | ||
664 | my $coupon = ($clientshash{$key}[4] eq "LICENSE") ? $Lang::tr{'Captive terms short'} : $clientshash{$key}[4]; | |
e01c5ab7 | 665 | |
41964aba MT |
666 | print <<END; |
667 | <tr> | |
668 | <td $col align="center"><b>$coupon</b></td> | |
669 | <td $col align="center">$starttime</td> | |
670 | <td $col align="center">$endtime</td> | |
671 | <td $col align="center">$clientshash{$key}[0]</td> | |
672 | <td $col align="center">$clientshash{$key}[5]</td> | |
673 | <td $col align="center"> | |
674 | <form method='post'> | |
675 | <input type='image' src='/images/delete.gif' align='middle' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' /> | |
676 | <input type='hidden' name='ACTION' value='delete-client' /> | |
677 | <input type='hidden' name='key' value='$clientshash{$key}[0]' /> | |
678 | </form> | |
679 | </td> | |
680 | </tr> | |
681 | END | |
8b920789 | 682 | } |
c7e78cc6 | 683 | |
8b920789 | 684 | print "</table>"; |
41964aba | 685 | |
8b920789 AM |
686 | &Header::closebox(); |
687 | } | |
688 | ||
e01c5ab7 AM |
689 | sub validremark |
690 | { | |
691 | # Checks a hostname against RFC1035 | |
692 | my $remark = $_[0]; | |
693 | # Each part should be at least two characters in length | |
694 | # but no more than 63 characters | |
695 | if (length ($remark) < 1 || length ($remark) > 255) { | |
696 | return 0;} | |
697 | # Only valid characters are a-z, A-Z, 0-9 and - | |
698 | if ($remark !~ /^[a-zäöüA-ZÖÄÜ0-9-.:;\|_()\/\s]*$/) { | |
699 | return 0;} | |
700 | # First character can only be a letter or a digit | |
701 | if (substr ($remark, 0, 1) !~ /^[a-zäöüA-ZÖÄÜ0-9]*$/) { | |
702 | return 0;} | |
703 | # Last character can only be a letter or a digit | |
704 | if (substr ($remark, -1, 1) !~ /^[a-zöäüA-ZÖÄÜ0-9.:;_)]*$/) { | |
705 | return 0;} | |
706 | return 1; | |
707 | } | |
708 | ||
a54350cd MT |
709 | sub generate_pdf() { |
710 | my $pdf = PDF::API2->new(); | |
711 | ||
712 | my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = gmtime(time); | |
713 | my $timestamp = sprintf("D:%04d%02d%02d%02d%02d%02d+00;00", $year+1900, $mon+1, $mday, $hour, $min, $sec); | |
714 | ||
715 | $pdf->info( | |
716 | "Creator" => $Lang::tr{'Captive portal'}, | |
717 | "Title" => $Lang::tr{'Captive portal coupons'}, | |
718 | "CreationDate" => $timestamp, | |
719 | "ModDate" => $timestamp, | |
720 | ); | |
721 | ||
722 | # Set page size | |
723 | $pdf->mediabox("A4"); | |
724 | $pdf->trimbox(28/mm, 27/mm, 182/mm, 270/mm); | |
725 | ||
726 | # Set font | |
727 | my $font = $pdf->ttfont("/usr/share/fonts/Ubuntu-R.ttf"); | |
728 | ||
729 | my $page_h_margin = 27/mm; | |
730 | my $page_v_margin = 28/mm; | |
731 | ||
732 | my $height = 68/mm; | |
733 | my $width = 91/mm; | |
734 | my $margin = 2/mm; | |
735 | ||
736 | # Tux Image | |
737 | my $tux_image = $pdf->image_png("/srv/web/ipfire/html/captive/assets/ipfire.png"); | |
738 | my $logo_height = 12/mm; | |
739 | my $logo_width = 12/mm; | |
740 | ||
741 | my @coupons = (); | |
742 | my %coupon_expiry_times = (); | |
743 | ||
744 | # Read coupons | |
745 | &General::readhasharray($coupons, \%couponhash) if (-e $coupons); | |
746 | foreach my $key (keys %couponhash) { | |
747 | $coupon_expiry_times{$couponhash{$key}[1]} = $couponhash{$key}[2]; | |
748 | push @coupons, $couponhash{$key}[1]; | |
749 | } | |
750 | ||
751 | while (@coupons) { | |
752 | # Make a new page | |
753 | my $page = $pdf->page(); | |
754 | ||
755 | # Graphics | |
756 | $gfx = $page->gfx(); | |
757 | ||
758 | # Headline font | |
759 | my $f_headline = $page->text(); | |
760 | $f_headline->font($font, 20); | |
761 | ||
762 | # Subheadline font | |
763 | my $f_subheadline = $page->text(); | |
764 | $f_subheadline->font($font, 14); | |
765 | ||
766 | # Coupon font | |
767 | my $f_coupon = $page->text(); | |
768 | $f_coupon->font($font, 36); | |
769 | ||
770 | # Lifetime | |
771 | my $f_lifetime = $page->text(); | |
772 | $f_lifetime->font($font, 14); | |
773 | ||
774 | # Watermark font | |
775 | my $f_watermark = $page->text(); | |
776 | $f_watermark->fillcolor("#666666"); | |
777 | $f_watermark->font($font, 10); | |
778 | ||
779 | my $i = 0; | |
780 | while (@coupons && $i < 8) { | |
781 | my $coupon = shift @coupons; | |
782 | ||
783 | # Box corners | |
784 | my $x = ($page_v_margin / 2) + (($i % 2) ? $width : 0); | |
785 | my $y = ($page_h_margin / 2) + (int($i / 2) * $height); | |
786 | ||
787 | # Weidth and height of the box | |
788 | my $w = $width - $margin; | |
789 | my $h = $height - $margin; | |
790 | ||
791 | # Center | |
792 | my $cx = $x + ($w / 2); | |
793 | my $cy = $y + ($h / 2); | |
794 | ||
795 | # Draw border box | |
796 | $gfx->strokecolor("#333333"); | |
797 | $gfx->linedash(1/mm, 1/mm); | |
798 | $gfx->rect($x, $y, $w, $h); | |
799 | $gfx->stroke(); | |
800 | $gfx->endpath(); | |
801 | ||
802 | # Headline | |
803 | $f_headline->translate($cx, ($y + $h - $cy) / 1.7 + $cy); | |
804 | $f_subheadline->translate($cx, ($y + $h - $cy) / 2.4 + $cy); | |
805 | ||
806 | if ($settings{'TITLE'}) { | |
807 | $f_headline->text_center($settings{'TITLE'}); | |
808 | $f_subheadline->text_center(decode("utf8", $Lang::tr{'Captive WiFi coupon'})); | |
809 | } else { | |
810 | $f_headline->text_center(decode("utf8", $Lang::tr{'Captive WiFi coupon'})); | |
811 | } | |
812 | ||
813 | # Coupon | |
814 | $f_coupon->translate($cx, $cy); | |
815 | $f_coupon->text_center(decode("utf8", $coupon)); | |
816 | ||
817 | # Show lifetime | |
818 | my $expiry_time = $coupon_expiry_times{$coupon}; | |
819 | $f_lifetime->translate($cx, $cy - ($y + $h - $cy) / 4); | |
820 | if ($expiry_time > 0) { | |
821 | my $lifetime = &General::format_time($expiry_time); | |
822 | $f_lifetime->text_center(decode("utf8", $Lang::tr{'Captive valid for'} . " " . $lifetime)); | |
823 | } else { | |
824 | $f_lifetime->text_center(decode("utf8", $Lang::tr{'Captive nolimit'})); | |
825 | } | |
826 | ||
827 | # Add watermark | |
828 | $gfx->image($tux_image, $x + $w - $logo_width - $margin, $y + $margin, $logo_width, $logo_height); | |
829 | $f_watermark->translate($x + $w - ($margin * 2) - $logo_width, $y + ($logo_height / 2)); | |
830 | $f_watermark->text_right("Powered by IPFire"); | |
831 | ||
832 | $i++; | |
833 | } | |
834 | } | |
835 | ||
836 | # Write out the PDF document | |
837 | return $pdf->stringify(); | |
838 | } | |
839 | ||
8b920789 AM |
840 | &Header::closebigbox(); |
841 | &Header::closepage(); |