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