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