]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - html/cgi-bin/captive.cgi
captive: Allow PDF export of coupons
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / captive.cgi
CommitLineData
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 23use Encode;
8b920789 24use HTML::Entities();
b32d9e92 25use File::Basename;
a54350cd
MT
26use PDF::API2;
27use 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
33require '/var/ipfire/general-functions.pl';
34require "${General::swroot}/lang.pl";
35require "${General::swroot}/header.pl";
e14adf75 36
278309b9
MT
37my %selected = ();
38
97b91e8a
MT
39my $coupons = "${General::swroot}/captive/coupons";
40my %couponhash = ();
41
e2752bfe
MT
42my $logo = "${General::swroot}/captive/logo.dat";
43
8b920789
AM
44my %settings=();
45my %mainsettings;
46my %color;
47my %cgiparams=();
48my %netsettings=();
49my %checked=();
50my $errormessage='';
8b920789 51my $clients="${General::swroot}/captive/clients";
8b920789
AM
52my %clientshash=();
53my $settingsfile="${General::swroot}/captive/settings";
e14adf75 54unless (-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
63if ($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 79if ($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 132if ($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 197if ($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 216if ($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
243if ($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'});
251print <<END
252 <form method='post' action='$ENV{'SCRIPT_NAME'}' enctype="multipart/form-data">\n
8b920789
AM
253 <table width='100%' border="0">
254 <tr>
255END
256;
e01c5ab7 257
a41fe924
MT
258#check which parameters have to be enabled (from settings file)
259$checked{'ENABLE_GREEN'}{'off'} = '';
260$checked{'ENABLE_GREEN'}{'on'} = '';
261$checked{'ENABLE_GREEN'}{$settings{'ENABLE_GREEN'}} = "checked='checked'";
8b920789 262
a41fe924
MT
263$checked{'ENABLE_BLUE'}{'off'} = '';
264$checked{'ENABLE_BLUE'}{'on'} = '';
265$checked{'ENABLE_BLUE'}{$settings{'ENABLE_BLUE'}} = "checked='checked'";
c7e78cc6 266
a41fe924
MT
267$checked{'UNLIMITED'}{'off'} = '';
268$checked{'UNLIMITED'}{'on'} = '';
269$checked{'UNLIMITED'}{$settings{'UNLIMITED'}} = "checked='checked'";
270
e7d16ea5
MT
271$selected{'AUTH'} = ();
272$selected{'AUTH'}{'COUPON'} = "";
273$selected{'AUTH'}{'TERMS'} = "";
274$selected{'AUTH'}{$settings{'AUTH'}} = "selected";
275
a41fe924
MT
276if ($netsettings{'GREEN_DEV'}){
277 print "<td width='30%'>$Lang::tr{'Captive active on'} <font color='$Header::colourgreen'>Green</font></td><td><input type='checkbox' name='ENABLE_GREEN' $checked{'ENABLE_GREEN'}{'on'} /></td></tr>";
278}
279if ($netsettings{'BLUE_DEV'}){
280 print "<td width='30%'>$Lang::tr{'Captive active on'} <font color='$Header::colourblue'>Blue</font></td><td><input type='checkbox' name='ENABLE_BLUE' $checked{'ENABLE_BLUE'}{'on'} /></td></tr>";
281}
e01c5ab7 282
e01c5ab7 283print<<END
a41fe924
MT
284 </tr>
285 <tr>
286 <td>
287 $Lang::tr{'Captive authentication'}
288 </td>
289 <td>
290 <select name='AUTH'>
e7d16ea5
MT
291 <option value="TERMS" $selected{'AUTH'}{'TERMS'} >$Lang::tr{'Captive terms'}</option>
292 <option value="COUPON" $selected{'AUTH'}{'COUPON'}>$Lang::tr{'Captive coupon'}</option>
a41fe924
MT
293 </select>
294 </td>
295 </tr>
8b920789
AM
296END
297;
8b920789 298
a41fe924
MT
299if ($settings{'AUTH'} eq 'TERMS') {
300 $selected{'SESSION_TIME'} = ();
301 $selected{'SESSION_TIME'}{'0'} = "";
302 $selected{'SESSION_TIME'}{'3600'} = "";
bbc69f22 303 $selected{'SESSION_TIME'}{'28800'} = "";
a41fe924
MT
304 $selected{'SESSION_TIME'}{'86400'} = "";
305 $selected{'SESSION_TIME'}{'604800'} = "";
306 $selected{'SESSION_TIME'}{'18144000'} = "";
307 $selected{'SESSION_TIME'}{$settings{'SESSION_TIME'}} = "selected";
308
a41fe924 309 print <<END;
297ebdd4 310 <tr>
a41fe924 311 <td>$Lang::tr{'Captive client session expiry time'}</td>
297ebdd4 312 <td>
a41fe924
MT
313 <select name="SESSION_TIME">
314 <option value="0" $selected{'SESSION_TIME'}{'0'}>- $Lang::tr{'unlimited'} -</option>
315 <option value="3600" $selected{'SESSION_TIME'}{'3600'}>$Lang::tr{'one hour'}</option>
0a219160 316 <option value="28800" $selected{'SESSION_TIME'}{'28800'}>$Lang::tr{'eight hours'}</option>
a41fe924
MT
317 <option value="86400" $selected{'SESSION_TIME'}{'86400'}>$Lang::tr{'24 hours'}</option>
318 <option value="604800" $selected{'SESSION_TIME'}{'604800'}>$Lang::tr{'one week'}</option>
319 <option value="18144000" $selected{'SESSION_TIME'}{'18144000'}>$Lang::tr{'one month'}</option>
320 </select>
297ebdd4
MT
321 </td>
322 </tr>
323END
a41fe924 324}
297ebdd4 325
a41fe924
MT
326print<<END;
327 <tr>
328 <td colspan="2">
329 <br>
330 <strong>$Lang::tr{'Captive branding'}</strong>
331 </td>
332 </tr>
333 <tr>
334 <td>
335 $Lang::tr{'Captive title'}
336 </td>
337 <td>
338 <input type='text' name='TITLE' value="$settings{'TITLE'}" size='40'>
339 </td>
340 </tr>
341 <tr>
342 <td>$Lang::tr{'Captive brand color'}</td>
343 <td>
344 <input type="color" name="COLOR" value="$settings{'COLOR'}">
345 </td>
346 </tr>
a41fe924
MT
347 <tr>
348 <td>
81019811 349 $Lang::tr{'Captive upload logo'}
a41fe924
MT
350 </td>
351 <td>
352 <input type="file" name="logo">
81019811 353 <br>$Lang::tr{'Captive upload logo recommendations'}
a41fe924
MT
354 </td>
355 </tr>
e2752bfe 356END
827d3f61 357
a41fe924 358if (-e $logo) {
297ebdd4 359 print <<END;
8b920789 360 <tr>
a41fe924
MT
361 <td>$Lang::tr{'Captive logo uploaded'}</td>
362 <td>$Lang::tr{'yes'}</td>
8b920789 363 </tr>
a41fe924
MT
364END
365}
366
e2bd5a6e 367my $terms = &getterms();
a41fe924 368print <<END;
e2bd5a6e 369 <tr>
ebfb8996 370 <td>$Lang::tr{'Captive terms'}</td>
e2bd5a6e
MT
371 <td>
372 <textarea cols="50" rows="10" name="TERMS">$terms</textarea>
373 </td>
374 </tr>
a41fe924
MT
375 <tr>
376 <td></td>
377 <td align='right'>
378 <input type='submit' name='ACTION' value="$Lang::tr{'save'}"/>
379 </td>
380 </tr>
297ebdd4 381 </table></form>
8b920789 382END
297ebdd4 383
a41fe924 384&Header::closebox();
8b920789 385
a41fe924
MT
386#if settings is set to use coupons, the coupon part has to be displayed
387if ($settings{'AUTH'} eq 'COUPON') {
388 &coupons();
a41fe924
MT
389}
390
792f1a3b
MT
391# Show active clients
392&show_clients();
393
a41fe924
MT
394sub getterms() {
395 my @ret;
396
397 open(FILE, "<:utf8", "/var/ipfire/captive/terms.txt");
398 while(<FILE>) {
399 push(@ret, HTML::Entities::decode_entities($_));
8b920789 400 }
a41fe924
MT
401 close(FILE);
402
403 return join(/\n/, @ret);
8b920789
AM
404}
405
8b920789
AM
406sub gencode(){
407 #generate a random code only letters from A-Z except 'O' and 0-9
408 my @chars = ("A".."N", "P".."Z", "0".."9");
409 my $randomstring;
410 $randomstring .= $chars[rand @chars] for 1..8;
411 return $randomstring;
412}
413
97b91e8a
MT
414sub coupons() {
415 &Header::openbox('100%', 'left', $Lang::tr{'Captive generate coupon'});
4f75fbfe
MT
416 print <<END;
417 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
418 <table border='0' width='100%'>
419 <tr>
420 <td width='30%'>
421 $Lang::tr{'Captive vouchervalid'}
422 </td>
423 <td width='70%'>
424 <table class='tbl' border='0' width='100%'>
425 <tr>
426 <th>$Lang::tr{'hours'}</th>
427 <th>$Lang::tr{'days'}</th>
428 <th>$Lang::tr{'weeks'}</th>
429 <th>$Lang::tr{'months'}</th>
430 <th></th>
431 </tr>
432END
08061703
AM
433
434 #print hour-dropdownbox
435 my $hrs=3600;
436 print "<tr height='40px'><td><select name='EXP_HOUR' style='width:8em;'>";
437 print "<option value='0' ";
438 print " selected='selected'" if ($settings{'EXP_HOUR'} eq '0');
439 print ">--</option>";
440 for (my $i = 1; $i<25; $i++){
441 my $exp_sec = $i * $hrs;
442 print "<option value='$exp_sec' ";
443 print " selected='selected'" if ($settings{'EXP_HOUR'} eq $exp_sec);
444 print ">$i</option>";
445 }
446 print "</td><td>";
447
448 #print day-dropdownbox
449 my $days=3600*24;
450 print "<select name='EXP_DAY' style='width:8em;'>";
451 print "<option value='0' ";
452 print " selected='selected'" if ($settings{'EXP_DAY'} eq '0');
453 print ">--</option>";
454 for (my $i = 1; $i<8; $i++){
455 my $exp_sec = $i * $days;
456 print "<option value='$exp_sec' ";
457 print " selected='selected'" if ($settings{'EXP_DAY'} eq $exp_sec);
458 print ">$i</option>";
459 }
460 print "</td><td>";
461
462 #print week-dropdownbox
463 my $week=3600*24*7;
464 print "<select name='EXP_WEEK' style='width:8em;'>";
465 print "<option value='0' ";
466 print " selected='selected'" if ($settings{'EXP_WEEK'} eq '0');
467 print ">--</option>";
468 for (my $i = 1; $i<5; $i++){
469 my $exp_sec = $i * $week;
470 print "<option value='$exp_sec' ";
471 print " selected='selected'" if ($settings{'EXP_WEEK'} eq $exp_sec);
472 print ">$i</option>";
473 }
474 print "</td><td>";
475
476 #print month-dropdownbox
477 my $month=3600*24*30;
478 print "<select name='EXP_MONTH' style='width:8em;'>";
479 print "<option value='0' ";
480 print " selected='selected'" if ($settings{'EXP_MONTH'} eq '0');
481 print ">--</option>";
482 for (my $i = 1; $i<13; $i++){
483 my $exp_sec = $i * $month;
484 print "<option value='$exp_sec' ";
485 print " selected='selected'" if ($settings{'EXP_MONTH'} eq $exp_sec);
486 print ">$i</option>";
487 }
4f75fbfe
MT
488 print <<END;
489 </td>
490 <td>
491 <label>
492 <input type='checkbox' name='UNLIMITED' $checked{'UNLIMITED'}{'on'} />
493 $Lang::tr{'Captive nolimit'}
494 </label>
495 </td>
496 </tr>
497 </table>
498 </td>
499 </tr>
500 <tr>
501 <td>$Lang::tr{'remark'}</td>
502 <td>
503 <input type='text' style='width: 98%;' name='REMARK' align='left'>
504 </td>
505 </tr>
506 </table>
507
508 <div align="right">
5cd9e28b
MT
509 <select name="COUNT">
510 <option value="1">1</option>
511 <option value="2">2</option>
512 <option value="3">3</option>
513 <option value="4">4</option>
514 <option value="5">5</option>
515 <option value="6">6</option>
516 <option value="7">7</option>
517 <option value="8">8</option>
518 <option value="9">9</option>
519 <option value="10">10</option>
520 <option value="20">20</option>
521 <option value="50">50</option>
522 <option value="100">100</option>
523 </select>
524
4f75fbfe
MT
525 <input type="submit" name="ACTION" value="$Lang::tr{'Captive generate coupon'}">
526 </div>
527 </form>
528END
08061703 529
8b920789 530 &Header::closebox();
97b91e8a
MT
531
532 # Show all coupons if exist
533 if (! -z $coupons) {
534 &show_coupons();
535 }
8b920789
AM
536}
537
97b91e8a 538sub show_coupons() {
a0b271e4
MT
539 &General::readhasharray($coupons, \%couponhash) if (-e $coupons);
540
97b91e8a 541 #if there are already generated but unsused coupons, print a table
a0b271e4
MT
542 &Header::openbox('100%', 'left', $Lang::tr{'Captive issued coupons'});
543
544 print <<END;
545 <table class='tbl' border='0'>
546 <tr>
547 <th align='center' width='15%'>
548 $Lang::tr{'Captive coupon'}
549 </th>
550 <th align='center' width='15%'>$Lang::tr{'Captive expiry time'}</th>
551 <th align='center' width='65%'>$Lang::tr{'remark'}</th>
552 <th align='center' width='5%'>$Lang::tr{'delete'}</th>
553 </tr>
8b920789 554END
a0b271e4
MT
555
556 foreach my $key (keys %couponhash) {
557 my $expirytime = $Lang::tr{'Captive nolimit'};
558 if ($couponhash{$key}[2] > 0) {
559 $expirytime = &General::format_time($couponhash{$key}[2]);
c7e78cc6
AM
560 }
561
a0b271e4 562 if ($count++ % 2) {
8b920789 563 $col="bgcolor='$color{'color20'}'";
a0b271e4 564 } else {
8b920789 565 $col="bgcolor='$color{'color22'}'";
8b920789 566 }
c7e78cc6 567
a0b271e4
MT
568 print <<END;
569 <tr>
570 <td $col align="center">
571 <b>$couponhash{$key}[1]</b>
572 </td>
573 <td $col align="center">
574 $expirytime
575 </td>
576 <td $col align="center">
577 $couponhash{$key}[3]
578 </td>
579 <td $col align="center">
580 <form method='post'>
581 <input type='image' src='/images/delete.gif' align='middle' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' />
582 <input type='hidden' name='ACTION' value='delete-coupon' />
583 <input type='hidden' name='key' value='$couponhash{$key}[0]' />
584 </form>
585 </td>
586 </tr>
587END
8b920789 588 }
c7e78cc6 589
8b920789 590 print "</table>";
a0b271e4 591
a54350cd
MT
592 # Download PDF
593 print <<END;
594 <div align="right">
595 <form method="POST">
596 <input type="hidden" name="ACTION" value="export-coupons">
597 <input type="submit" value="$Lang::tr{'Captive export coupons'}">
598 </form>
599 </div>
600END
601
8b920789
AM
602 &Header::closebox();
603}
604
97b91e8a 605sub show_clients() {
41964aba 606 # if there are active clients which use coupons show table
8b920789 607 return if ( -z $clients || ! -f $clients );
41964aba 608
8b920789
AM
609 my $count=0;
610 my $col;
41964aba
MT
611
612 &Header::openbox('100%', 'left', $Lang::tr{'Captive clients'});
613
614 print <<END;
615 <table class='tbl' width='100%'>
616 <tr>
617 <th align='center' width='15%'>$Lang::tr{'Captive coupon'}</th>
618 <th align='center' width='15%'>$Lang::tr{'Captive activated'}</th>
619 <th align='center' width='15%'>$Lang::tr{'Captive expiry time'}</th>
620 <th align='center' width='10%'>$Lang::tr{'Captive mac'}</th>
621 <th align='center' width='43%'>$Lang::tr{'remark'}</th>
622 <th align='center' width='5%'>$Lang::tr{'delete'}</th>
623 </tr>
8b920789 624END
41964aba 625
97b91e8a 626 &General::readhasharray($clients, \%clientshash) if (-e $clients);
41964aba 627 foreach my $key (keys %clientshash) {
c7e78cc6
AM
628 #calculate time from clientshash (starttime)
629 my $starttime = sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime($clientshash{$key}[2]));
41964aba 630
c7e78cc6
AM
631 #calculate endtime from clientshash
632 my $endtime;
633 if ($clientshash{$key}[3] eq '0'){
634 $endtime=$Lang::tr{'Captive nolimit'};
41964aba 635 } else {
c7e78cc6
AM
636 $endtime = sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime($clientshash{$key}[2]+$clientshash{$key}[3]));
637 }
e01c5ab7 638
41964aba
MT
639 if ($count++ % 2) {
640 $col="bgcolor='$color{'color20'}'";
641 } else {
642 $col="bgcolor='$color{'color22'}'";
643 }
644
645 my $coupon = ($clientshash{$key}[4] eq "LICENSE") ? $Lang::tr{'Captive terms short'} : $clientshash{$key}[4];
e01c5ab7 646
41964aba
MT
647 print <<END;
648 <tr>
649 <td $col align="center"><b>$coupon</b></td>
650 <td $col align="center">$starttime</td>
651 <td $col align="center">$endtime</td>
652 <td $col align="center">$clientshash{$key}[0]</td>
653 <td $col align="center">$clientshash{$key}[5]</td>
654 <td $col align="center">
655 <form method='post'>
656 <input type='image' src='/images/delete.gif' align='middle' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' />
657 <input type='hidden' name='ACTION' value='delete-client' />
658 <input type='hidden' name='key' value='$clientshash{$key}[0]' />
659 </form>
660 </td>
661 </tr>
662END
8b920789 663 }
c7e78cc6 664
8b920789 665 print "</table>";
41964aba 666
8b920789
AM
667 &Header::closebox();
668}
669
e01c5ab7
AM
670sub validremark
671{
672 # Checks a hostname against RFC1035
673 my $remark = $_[0];
674 # Each part should be at least two characters in length
675 # but no more than 63 characters
676 if (length ($remark) < 1 || length ($remark) > 255) {
677 return 0;}
678 # Only valid characters are a-z, A-Z, 0-9 and -
679 if ($remark !~ /^[a-zäöüA-ZÖÄÜ0-9-.:;\|_()\/\s]*$/) {
680 return 0;}
681 # First character can only be a letter or a digit
682 if (substr ($remark, 0, 1) !~ /^[a-zäöüA-ZÖÄÜ0-9]*$/) {
683 return 0;}
684 # Last character can only be a letter or a digit
685 if (substr ($remark, -1, 1) !~ /^[a-zöäüA-ZÖÄÜ0-9.:;_)]*$/) {
686 return 0;}
687 return 1;
688}
689
a54350cd
MT
690sub generate_pdf() {
691 my $pdf = PDF::API2->new();
692
693 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = gmtime(time);
694 my $timestamp = sprintf("D:%04d%02d%02d%02d%02d%02d+00;00", $year+1900, $mon+1, $mday, $hour, $min, $sec);
695
696 $pdf->info(
697 "Creator" => $Lang::tr{'Captive portal'},
698 "Title" => $Lang::tr{'Captive portal coupons'},
699 "CreationDate" => $timestamp,
700 "ModDate" => $timestamp,
701 );
702
703 # Set page size
704 $pdf->mediabox("A4");
705 $pdf->trimbox(28/mm, 27/mm, 182/mm, 270/mm);
706
707 # Set font
708 my $font = $pdf->ttfont("/usr/share/fonts/Ubuntu-R.ttf");
709
710 my $page_h_margin = 27/mm;
711 my $page_v_margin = 28/mm;
712
713 my $height = 68/mm;
714 my $width = 91/mm;
715 my $margin = 2/mm;
716
717 # Tux Image
718 my $tux_image = $pdf->image_png("/srv/web/ipfire/html/captive/assets/ipfire.png");
719 my $logo_height = 12/mm;
720 my $logo_width = 12/mm;
721
722 my @coupons = ();
723 my %coupon_expiry_times = ();
724
725 # Read coupons
726 &General::readhasharray($coupons, \%couponhash) if (-e $coupons);
727 foreach my $key (keys %couponhash) {
728 $coupon_expiry_times{$couponhash{$key}[1]} = $couponhash{$key}[2];
729 push @coupons, $couponhash{$key}[1];
730 }
731
732 while (@coupons) {
733 # Make a new page
734 my $page = $pdf->page();
735
736 # Graphics
737 $gfx = $page->gfx();
738
739 # Headline font
740 my $f_headline = $page->text();
741 $f_headline->font($font, 20);
742
743 # Subheadline font
744 my $f_subheadline = $page->text();
745 $f_subheadline->font($font, 14);
746
747 # Coupon font
748 my $f_coupon = $page->text();
749 $f_coupon->font($font, 36);
750
751 # Lifetime
752 my $f_lifetime = $page->text();
753 $f_lifetime->font($font, 14);
754
755 # Watermark font
756 my $f_watermark = $page->text();
757 $f_watermark->fillcolor("#666666");
758 $f_watermark->font($font, 10);
759
760 my $i = 0;
761 while (@coupons && $i < 8) {
762 my $coupon = shift @coupons;
763
764 # Box corners
765 my $x = ($page_v_margin / 2) + (($i % 2) ? $width : 0);
766 my $y = ($page_h_margin / 2) + (int($i / 2) * $height);
767
768 # Weidth and height of the box
769 my $w = $width - $margin;
770 my $h = $height - $margin;
771
772 # Center
773 my $cx = $x + ($w / 2);
774 my $cy = $y + ($h / 2);
775
776 # Draw border box
777 $gfx->strokecolor("#333333");
778 $gfx->linedash(1/mm, 1/mm);
779 $gfx->rect($x, $y, $w, $h);
780 $gfx->stroke();
781 $gfx->endpath();
782
783 # Headline
784 $f_headline->translate($cx, ($y + $h - $cy) / 1.7 + $cy);
785 $f_subheadline->translate($cx, ($y + $h - $cy) / 2.4 + $cy);
786
787 if ($settings{'TITLE'}) {
788 $f_headline->text_center($settings{'TITLE'});
789 $f_subheadline->text_center(decode("utf8", $Lang::tr{'Captive WiFi coupon'}));
790 } else {
791 $f_headline->text_center(decode("utf8", $Lang::tr{'Captive WiFi coupon'}));
792 }
793
794 # Coupon
795 $f_coupon->translate($cx, $cy);
796 $f_coupon->text_center(decode("utf8", $coupon));
797
798 # Show lifetime
799 my $expiry_time = $coupon_expiry_times{$coupon};
800 $f_lifetime->translate($cx, $cy - ($y + $h - $cy) / 4);
801 if ($expiry_time > 0) {
802 my $lifetime = &General::format_time($expiry_time);
803 $f_lifetime->text_center(decode("utf8", $Lang::tr{'Captive valid for'} . " " . $lifetime));
804 } else {
805 $f_lifetime->text_center(decode("utf8", $Lang::tr{'Captive nolimit'}));
806 }
807
808 # Add watermark
809 $gfx->image($tux_image, $x + $w - $logo_width - $margin, $y + $margin, $logo_width, $logo_height);
810 $f_watermark->translate($x + $w - ($margin * 2) - $logo_width, $y + ($logo_height / 2));
811 $f_watermark->text_right("Powered by IPFire");
812
813 $i++;
814 }
815 }
816
817 # Write out the PDF document
818 return $pdf->stringify();
819}
820
8b920789
AM
821&Header::closebigbox();
822&Header::closepage();