]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/captive.cgi
BUG11505: Captive Portal: no way to remove an uploaded logo
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / captive.cgi
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
22 #use strict;
23 use Encode;
24 use HTML::Entities();
25 use File::Basename;
26 use PDF::API2;
27 use constant mm => 25.4 / 72;
28
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";
36
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'},
45 2592000 => $Lang::tr{'one month'},
46 31536000 => $Lang::tr{'one year'},
47 0 => "- $Lang::tr{'unlimited'} -",
48 );
49
50 my %selected = ();
51
52 my $coupons = "${General::swroot}/captive/coupons";
53 my %couponhash = ();
54
55 my $logo = "${General::swroot}/captive/logo.dat";
56
57 my %settings=();
58 my %mainsettings;
59 my %color;
60 my %cgiparams=();
61 my %netsettings=();
62 my %checked=();
63 my $errormessage='';
64 my $clients="${General::swroot}/captive/clients";
65 my %clientshash=();
66 my $settingsfile="${General::swroot}/captive/settings";
67 unless (-e $settingsfile) { system("touch $settingsfile"); }
68
69 &Header::getcgihash(\%cgiparams);
70
71 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
72 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
73 &General::readhash("$settingsfile", \%settings) if(-f $settingsfile);
74 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
75
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
90 &Header::showhttpheaders();
91
92 if ($cgiparams{'ACTION'} eq $Lang::tr{'save'}) {
93 my $file = $cgiparams{'logo'};
94 if ($file) {
95 # Check if the file extension is PNG/JPEG
96 chomp $file;
97
98 my ($name, $path, $ext) = fileparse($file, qr/\.[^.]*$/);
99 if ($ext ne ".png" && $ext ne ".jpg" && $ext ne ".jpeg") {
100 $errormessage = $Lang::tr{'Captive wrong ext'};
101 }
102 }
103
104 $settings{'ENABLE_GREEN'} = $cgiparams{'ENABLE_GREEN'};
105 $settings{'ENABLE_BLUE'} = $cgiparams{'ENABLE_BLUE'};
106 $settings{'AUTH'} = $cgiparams{'AUTH'};
107 $settings{'TITLE'} = &Header::escape($cgiparams{'TITLE'});
108 $settings{'COLOR'} = $cgiparams{'COLOR'};
109 $settings{'SESSION_TIME'} = $cgiparams{'SESSION_TIME'};
110
111 if (!$errormessage){
112 #Check if we need to upload a new logo
113 if ($file) {
114 # Save logo
115 my ($filehandle) = CGI::upload("logo");
116
117 # XXX check filesize
118
119 open(FILE, ">$logo");
120 binmode $filehandle;
121 while (<$filehandle>) {
122 print FILE;
123 }
124 close(FILE);
125 }
126
127 &General::writehash("$settingsfile", \%settings);
128
129 # Save terms
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'} = "";
135
136 #execute binary to reload firewall rules
137 system("/usr/local/bin/captivectrl");
138
139 if ($cgiparams{'ENABLE_BLUE'} eq 'on'){
140 system("/usr/local/bin/wirelessctrl");
141 }
142 }
143 }
144
145 if ($cgiparams{'ACTION'} eq "$Lang::tr{'Captive delete logo'}") {
146 unlink $logo;
147 }
148
149 if ($cgiparams{'ACTION'} eq "$Lang::tr{'Captive generate coupons'}") {
150 #check valid remark
151 if ($cgiparams{'REMARK'} ne '' && !&validremark($cgiparams{'REMARK'})){
152 $errormessage=$Lang::tr{'fwhost err remark'};
153 }
154
155 if (!$errormessage) {
156 # Remember selected values
157 foreach my $val (("SESSION_TIME", "COUNT", "REMARK")) {
158 $settings{$val} = $cgiparams{$val};
159 }
160 &General::writehash($settingsfile, \%settings);
161
162 &General::readhasharray($coupons, \%couponhash) if (-e $coupons);
163 my $now = time();
164
165 # Expiry time in seconds
166 my $expires = $settings{'SESSION_TIME'};
167
168 my $count = $settings{'COUNT'} || 1;
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] = ""; }
190
191 $couponhash{$key}[0] = $now;
192 $couponhash{$key}[1] = $code;
193 $couponhash{$key}[2] = $expires;
194 $couponhash{$key}[3] = $settings{'REMARK'};
195 }
196
197 # Save everything to disk
198 &General::writehasharray($coupons, \%couponhash);
199 }
200 }
201
202 if ($cgiparams{'ACTION'} eq 'delete-coupon') {
203 #deletes an already generated but unused voucher
204
205 #read all generated vouchers
206 &General::readhasharray($coupons, \%couponhash) if (-e $coupons);
207 foreach my $key (keys %couponhash) {
208 if($cgiparams{'key'} eq $couponhash{$key}[0]){
209 #write logenty with decoded remark
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");
212 #delete line from hash
213 delete $couponhash{$key};
214 last;
215 }
216 }
217 #write back hash
218 &General::writehasharray($coupons, \%couponhash);
219 }
220
221 if ($cgiparams{'ACTION'} eq 'delete-client') {
222 #delete voucher and connection in use
223
224 #read all active clients
225 &General::readhasharray($clients, \%clientshash) if (-e $clients);
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
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");
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
244 &Header::openpage($Lang::tr{'Captive'}, 1, '');
245 &Header::openbigbox();
246
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();
252 }
253
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
258 <table width='100%' border="0">
259 END
260 ;
261
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'";
266
267 $checked{'ENABLE_BLUE'}{'off'} = '';
268 $checked{'ENABLE_BLUE'}{'on'} = '';
269 $checked{'ENABLE_BLUE'}{$settings{'ENABLE_BLUE'}} = "checked='checked'";
270
271 $checked{'UNLIMITED'}{'off'} = '';
272 $checked{'UNLIMITED'}{'on'} = '';
273 $checked{'UNLIMITED'}{$settings{'UNLIMITED'}} = "checked='checked'";
274
275 $selected{'AUTH'} = ();
276 $selected{'AUTH'}{'COUPON'} = "";
277 $selected{'AUTH'}{'TERMS'} = "";
278 $selected{'AUTH'}{$settings{'AUTH'}} = "selected";
279
280 if ($netsettings{'GREEN_DEV'}){
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
292 }
293
294 if ($netsettings{'BLUE_DEV'}){
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
306 }
307
308 print<<END
309 <tr>
310 <td>
311 $Lang::tr{'Captive authentication'}
312 </td>
313 <td>
314 <select name='AUTH'>
315 <option value="TERMS" $selected{'AUTH'}{'TERMS'} >$Lang::tr{'Captive terms'}</option>
316 <option value="COUPON" $selected{'AUTH'}{'COUPON'}>$Lang::tr{'Captive coupon'}</option>
317 </select>
318 </td>
319 </tr>
320 END
321 ;
322
323 if ($settings{'AUTH'} eq 'TERMS') {
324 $selected{'SESSION_TIME'} = ();
325 foreach my $session_time (keys %session_times) {
326 $selected{'SESSION_TIME'}{$session_time} = "";
327 }
328 $selected{'SESSION_TIME'}{$settings{'SESSION_TIME'}} = "selected";
329
330 print <<END;
331 <tr>
332 <td>$Lang::tr{'Captive client session expiry time'}</td>
333 <td>
334 <select name="SESSION_TIME">
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;
346 </select>
347 </td>
348 </tr>
349 END
350 }
351
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>
373 <tr>
374 <td>
375 $Lang::tr{'Captive upload logo'}
376 </td>
377 <td>
378 <input type="file" name="logo">
379 <br>$Lang::tr{'Captive upload logo recommendations'}
380 </td>
381 </tr>
382 END
383
384 if (-e $logo) {
385 print <<END;
386 <tr>
387 <td>$Lang::tr{'Captive logo uploaded'}</td>
388 <td>
389 $Lang::tr{'yes'}&nbsp;
390 <input type='submit' name='ACTION' value="$Lang::tr{'Captive delete logo'}"/>
391 </td>
392 </tr>
393 END
394 }
395
396 my $terms = &getterms();
397 print <<END;
398 <tr>
399 <td>$Lang::tr{'Captive terms'}</td>
400 <td>
401 <textarea cols="50" rows="10" name="TERMS">$terms</textarea>
402 </td>
403 </tr>
404 <tr>
405 <td></td>
406 <td align='right'>
407 <input type='submit' name='ACTION' value="$Lang::tr{'save'}"/>
408 </td>
409 </tr>
410 </table></form>
411 END
412
413 &Header::closebox();
414
415 #if settings is set to use coupons, the coupon part has to be displayed
416 if ($settings{'AUTH'} eq 'COUPON') {
417 &coupons();
418 }
419
420 # Show active clients
421 &show_clients();
422
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($_));
429 }
430 close(FILE);
431
432 return join(/\n/, @ret);
433 }
434
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
443 sub coupons() {
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
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%'>
460 <select name="SESSION_TIME">
461 END
462
463 foreach my $session_time (sort { $a <=> $b } keys %session_times) {
464 print <<END;
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>
473 </td>
474 </tr>
475 <tr>
476 <td>$Lang::tr{'remark'}</td>
477 <td>
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>
499 </td>
500 </tr>
501 </table>
502
503 <div align="right">
504 <input type="submit" name="ACTION" value="$Lang::tr{'Captive generate coupons'}">
505 </div>
506 </form>
507 END
508
509 &Header::closebox();
510
511 # Show all coupons if exist
512 if (! -z $coupons) {
513 &show_coupons();
514 }
515 }
516
517 sub show_coupons() {
518 &General::readhasharray($coupons, \%couponhash) if (-e $coupons);
519
520 #if there are already generated but unsused coupons, print a table
521 &Header::openbox('100%', 'left', $Lang::tr{'Captive issued coupons'});
522
523 print <<END;
524 <table class='tbl' border='0'>
525 <tr>
526 <th align='center' width='15%'>
527 $Lang::tr{'Captive coupon'}
528 </th>
529 <th align='center' width='15%'>$Lang::tr{'Captive expiry time'}</th>
530 <th align='center' width='65%'>$Lang::tr{'remark'}</th>
531 <th align='center' width='5%'>$Lang::tr{'delete'}</th>
532 </tr>
533 END
534
535 foreach my $key (keys %couponhash) {
536 my $expirytime = $Lang::tr{'Captive nolimit'};
537 if ($couponhash{$key}[2] > 0) {
538 $expirytime = &General::format_time($couponhash{$key}[2]);
539 }
540
541 if ($count++ % 2) {
542 $col="bgcolor='$color{'color20'}'";
543 } else {
544 $col="bgcolor='$color{'color22'}'";
545 }
546
547 print <<END;
548 <tr>
549 <td $col align="center">
550 <b>$couponhash{$key}[1]</b>
551 </td>
552 <td $col align="center">
553 $expirytime
554 </td>
555 <td $col align="center">
556 $couponhash{$key}[3]
557 </td>
558 <td $col align="center">
559 <form method='post'>
560 <input type='image' src='/images/delete.gif' align='middle' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' />
561 <input type='hidden' name='ACTION' value='delete-coupon' />
562 <input type='hidden' name='key' value='$couponhash{$key}[0]' />
563 </form>
564 </td>
565 </tr>
566 END
567 }
568
569 print "</table>";
570
571 # Download PDF
572 print <<END;
573 <div align="right">
574 <form method="POST">
575 <input type="hidden" name="ACTION" value="export-coupons">
576 <input type="submit" value="$Lang::tr{'Captive export coupons'}">
577 </form>
578 </div>
579 END
580
581 &Header::closebox();
582 }
583
584 sub show_clients() {
585 # if there are active clients which use coupons show table
586 return if ( -z $clients || ! -f $clients );
587
588 my $count=0;
589 my $col;
590
591 &Header::openbox('100%', 'left', $Lang::tr{'Captive clients'});
592
593 print <<END;
594 <table class='tbl' width='100%'>
595 <tr>
596 <th align='center' width='15%'>$Lang::tr{'Captive coupon'}</th>
597 <th align='center' width='15%'>$Lang::tr{'Captive activated'}</th>
598 <th align='center' width='15%'>$Lang::tr{'Captive expiry time'}</th>
599 <th align='center' width='10%'>$Lang::tr{'Captive mac'}</th>
600 <th align='center' width='43%'>$Lang::tr{'remark'}</th>
601 <th align='center' width='5%'>$Lang::tr{'delete'}</th>
602 </tr>
603 END
604
605 &General::readhasharray($clients, \%clientshash) if (-e $clients);
606 foreach my $key (keys %clientshash) {
607 #calculate time from clientshash (starttime)
608 my $starttime = sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime($clientshash{$key}[2]));
609
610 #calculate endtime from clientshash
611 my $endtime;
612 if ($clientshash{$key}[3] eq '0'){
613 $endtime=$Lang::tr{'Captive nolimit'};
614 } else {
615 $endtime = sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime($clientshash{$key}[2]+$clientshash{$key}[3]));
616 }
617
618 if ($count++ % 2) {
619 $col="bgcolor='$color{'color20'}'";
620 } else {
621 $col="bgcolor='$color{'color22'}'";
622 }
623
624 my $coupon = ($clientshash{$key}[4] eq "LICENSE") ? $Lang::tr{'Captive terms short'} : $clientshash{$key}[4];
625
626 print <<END;
627 <tr>
628 <td $col align="center"><b>$coupon</b></td>
629 <td $col align="center">$starttime</td>
630 <td $col align="center">$endtime</td>
631 <td $col align="center">$clientshash{$key}[0]</td>
632 <td $col align="center">$clientshash{$key}[5]</td>
633 <td $col align="center">
634 <form method='post'>
635 <input type='image' src='/images/delete.gif' align='middle' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' />
636 <input type='hidden' name='ACTION' value='delete-client' />
637 <input type='hidden' name='key' value='$clientshash{$key}[0]' />
638 </form>
639 </td>
640 </tr>
641 END
642 }
643
644 print "</table>";
645
646 &Header::closebox();
647 }
648
649 sub validremark
650 {
651 # Checks a hostname against RFC1035
652 my $remark = $_[0];
653 # Each part should be at least two characters in length
654 # but no more than 63 characters
655 if (length ($remark) < 1 || length ($remark) > 255) {
656 return 0;}
657 # Only valid characters are a-z, A-Z, 0-9 and -
658 if ($remark !~ /^[a-zäöüA-ZÖÄÜ0-9-.:;\|_()\/\s]*$/) {
659 return 0;}
660 # First character can only be a letter or a digit
661 if (substr ($remark, 0, 1) !~ /^[a-zäöüA-ZÖÄÜ0-9]*$/) {
662 return 0;}
663 # Last character can only be a letter or a digit
664 if (substr ($remark, -1, 1) !~ /^[a-zöäüA-ZÖÄÜ0-9.:;_)]*$/) {
665 return 0;}
666 return 1;
667 }
668
669 sub generate_pdf() {
670 my $pdf = PDF::API2->new();
671
672 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = gmtime(time);
673 my $timestamp = sprintf("D:%04d%02d%02d%02d%02d%02d+00;00", $year+1900, $mon+1, $mday, $hour, $min, $sec);
674
675 $pdf->info(
676 "Creator" => $Lang::tr{'Captive portal'},
677 "Title" => $Lang::tr{'Captive portal coupons'},
678 "CreationDate" => $timestamp,
679 "ModDate" => $timestamp,
680 );
681
682 # Set page size
683 $pdf->mediabox("A4");
684 $pdf->trimbox(28/mm, 27/mm, 182/mm, 270/mm);
685
686 # Set font
687 my $font = $pdf->ttfont("/usr/share/fonts/Ubuntu-R.ttf");
688
689 my $page_h_margin = 27/mm;
690 my $page_v_margin = 28/mm;
691
692 my $height = 68/mm;
693 my $width = 91/mm;
694 my $margin = 2/mm;
695
696 # Tux Image
697 my $tux_image = $pdf->image_png("/srv/web/ipfire/html/captive/assets/ipfire.png");
698 my $logo_height = 12/mm;
699 my $logo_width = 12/mm;
700
701 my @coupons = ();
702 my %coupon_expiry_times = ();
703
704 # Read coupons
705 &General::readhasharray($coupons, \%couponhash) if (-e $coupons);
706 foreach my $key (keys %couponhash) {
707 $coupon_expiry_times{$couponhash{$key}[1]} = $couponhash{$key}[2];
708 push @coupons, $couponhash{$key}[1];
709 }
710
711 while (@coupons) {
712 # Make a new page
713 my $page = $pdf->page();
714
715 # Graphics
716 $gfx = $page->gfx();
717
718 # Headline font
719 my $f_headline = $page->text();
720 $f_headline->font($font, 20);
721
722 # Subheadline font
723 my $f_subheadline = $page->text();
724 $f_subheadline->font($font, 14);
725
726 # Coupon font
727 my $f_coupon = $page->text();
728 $f_coupon->font($font, 36);
729
730 # Lifetime
731 my $f_lifetime = $page->text();
732 $f_lifetime->font($font, 14);
733
734 # Watermark font
735 my $f_watermark = $page->text();
736 $f_watermark->fillcolor("#666666");
737 $f_watermark->font($font, 10);
738
739 my $i = 0;
740 while (@coupons && $i < 8) {
741 my $coupon = shift @coupons;
742
743 # Box corners
744 my $x = ($page_v_margin / 2) + (($i % 2) ? $width : 0);
745 my $y = ($page_h_margin / 2) + (int($i / 2) * $height);
746
747 # Weidth and height of the box
748 my $w = $width - $margin;
749 my $h = $height - $margin;
750
751 # Center
752 my $cx = $x + ($w / 2);
753 my $cy = $y + ($h / 2);
754
755 # Draw border box
756 $gfx->strokecolor("#333333");
757 $gfx->linedash(1/mm, 1/mm);
758 $gfx->rect($x, $y, $w, $h);
759 $gfx->stroke();
760 $gfx->endpath();
761
762 # Headline
763 $f_headline->translate($cx, ($y + $h - $cy) / 1.7 + $cy);
764 $f_subheadline->translate($cx, ($y + $h - $cy) / 2.4 + $cy);
765
766 if ($settings{'TITLE'}) {
767 $f_headline->text_center(decode("utf8", $settings{'TITLE'}));
768 $f_subheadline->text_center(decode("utf8", $Lang::tr{'Captive WiFi coupon'}));
769 } else {
770 $f_headline->text_center(decode("utf8", $Lang::tr{'Captive WiFi coupon'}));
771 }
772
773 # Coupon
774 $f_coupon->translate($cx, $cy);
775 $f_coupon->text_center(decode("utf8", $coupon));
776
777 # Show lifetime
778 my $expiry_time = $coupon_expiry_times{$coupon};
779 $f_lifetime->translate($cx, $cy - ($y + $h - $cy) / 4);
780 if ($expiry_time > 0) {
781 my $lifetime = &General::format_time($expiry_time);
782 $f_lifetime->text_center(decode("utf8", $Lang::tr{'Captive valid for'} . " " . $lifetime));
783 } else {
784 $f_lifetime->text_center(decode("utf8", $Lang::tr{'Captive nolimit'}));
785 }
786
787 # Add watermark
788 $gfx->image($tux_image, $x + $w - $logo_width - $margin, $y + $margin, $logo_width, $logo_height);
789 $f_watermark->translate($x + $w - ($margin * 2) - $logo_width, $y + ($logo_height / 2));
790 $f_watermark->text_right("Powered by IPFire");
791
792 $i++;
793 }
794 }
795
796 # Write out the PDF document
797 return $pdf->stringify();
798 }
799
800 &Header::closebigbox();
801 &Header::closepage();