captive: Allow PDF export of coupons
[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 %selected = ();
38
39 my $coupons = "${General::swroot}/captive/coupons";
40 my %couponhash = ();
41
42 my $logo = "${General::swroot}/captive/logo.dat";
43
44 my %settings=();
45 my %mainsettings;
46 my %color;
47 my %cgiparams=();
48 my %netsettings=();
49 my %checked=();
50 my $errormessage='';
51 my $clients="${General::swroot}/captive/clients";
52 my %clientshash=();
53 my $settingsfile="${General::swroot}/captive/settings";
54 unless (-e $settingsfile) { system("touch $settingsfile"); }
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
63 if ($cgiparams{'ACTION'} eq "export-coupons") {
64 my $pdf = &generate_pdf();
65
66 print "Content-Type: application/pdf\n";
67 print "Content-Disposition: attachment; filename=captive-portal-coupons.pdf\n";
68 print "\n"; # end headers
69
70 # Send PDF
71 print $pdf;
72
73 exit(0);
74 }
75
76
77 &Header::showhttpheaders();
78
79 if ($cgiparams{'ACTION'} eq $Lang::tr{'save'}) {
80 my $file = $cgiparams{'logo'};
81 if ($file) {
82 # Check if the file extension is PNG/JPEG
83 chomp $file;
84
85 my ($name, $path, $ext) = fileparse($file, qr/\.[^.]*$/);
86 if ($ext ne ".png" && $ext ne ".jpg" && $ext ne ".jpeg") {
87 $errormessage = $Lang::tr{'Captive wrong ext'};
88 }
89 }
90
91 $settings{'ENABLE_GREEN'} = $cgiparams{'ENABLE_GREEN'};
92 $settings{'ENABLE_BLUE'} = $cgiparams{'ENABLE_BLUE'};
93 $settings{'AUTH'} = $cgiparams{'AUTH'};
94 $settings{'TITLE'} = $cgiparams{'TITLE'};
95 $settings{'COLOR'} = $cgiparams{'COLOR'};
96 $settings{'SESSION_TIME'} = $cgiparams{'SESSION_TIME'};
97
98 if (!$errormessage){
99 #Check if we need to upload a new logo
100 if ($file) {
101 # Save logo
102 my ($filehandle) = CGI::upload("logo");
103
104 # XXX check filesize
105
106 open(FILE, ">$logo");
107 binmode $filehandle;
108 while (<$filehandle>) {
109 print FILE;
110 }
111 close(FILE);
112 }
113
114 &General::writehash("$settingsfile", \%settings);
115
116 # Save terms
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'} = "";
122
123 #execute binary to reload firewall rules
124 system("/usr/local/bin/captivectrl");
125
126 if ($cgiparams{'ENABLE_BLUE'} eq 'on'){
127 system("/usr/local/bin/wirelessctrl");
128 }
129 }
130 }
131
132 if ($cgiparams{'ACTION'} eq "$Lang::tr{'Captive generate coupon'}") {
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'};
136 }
137
138 #check valid remark
139 if ($cgiparams{'REMARK'} ne '' && !&validremark($cgiparams{'REMARK'})){
140 $errormessage=$Lang::tr{'fwhost err remark'};
141 }
142
143 if (!$errormessage) {
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
150 &General::readhasharray($coupons, \%couponhash) if (-e $coupons);
151 my $now = time();
152
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
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] = ""; }
185
186 $couponhash{$key}[0] = $now;
187 $couponhash{$key}[1] = $code;
188 $couponhash{$key}[2] = $expires;
189 $couponhash{$key}[3] = $cgiparams{'REMARK'};
190 }
191
192 # Save everything to disk
193 &General::writehasharray($coupons, \%couponhash);
194 }
195 }
196
197 if ($cgiparams{'ACTION'} eq 'delete-coupon') {
198 #deletes an already generated but unused voucher
199
200 #read all generated vouchers
201 &General::readhasharray($coupons, \%couponhash) if (-e $coupons);
202 foreach my $key (keys %couponhash) {
203 if($cgiparams{'key'} eq $couponhash{$key}[0]){
204 #write logenty with decoded remark
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");
207 #delete line from hash
208 delete $couponhash{$key};
209 last;
210 }
211 }
212 #write back hash
213 &General::writehasharray($coupons, \%couponhash);
214 }
215
216 if ($cgiparams{'ACTION'} eq 'delete-client') {
217 #delete voucher and connection in use
218
219 #read all active clients
220 &General::readhasharray($clients, \%clientshash) if (-e $clients);
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
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");
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
242 # If an error message exists, show a box with the error message
243 if ($errormessage) {
244 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
245 print $errormessage;
246 &Header::closebox();
247 }
248
249 # Prints the config box on the website
250 &Header::openbox('100%', 'left', $Lang::tr{'Captive config'});
251 print <<END
252 <form method='post' action='$ENV{'SCRIPT_NAME'}' enctype="multipart/form-data">\n
253 <table width='100%' border="0">
254 <tr>
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 "<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 }
279 if ($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 }
282
283 print<<END
284 </tr>
285 <tr>
286 <td>
287 $Lang::tr{'Captive authentication'}
288 </td>
289 <td>
290 <select name='AUTH'>
291 <option value="TERMS" $selected{'AUTH'}{'TERMS'} >$Lang::tr{'Captive terms'}</option>
292 <option value="COUPON" $selected{'AUTH'}{'COUPON'}>$Lang::tr{'Captive coupon'}</option>
293 </select>
294 </td>
295 </tr>
296 END
297 ;
298
299 if ($settings{'AUTH'} eq 'TERMS') {
300 $selected{'SESSION_TIME'} = ();
301 $selected{'SESSION_TIME'}{'0'} = "";
302 $selected{'SESSION_TIME'}{'3600'} = "";
303 $selected{'SESSION_TIME'}{'28800'} = "";
304 $selected{'SESSION_TIME'}{'86400'} = "";
305 $selected{'SESSION_TIME'}{'604800'} = "";
306 $selected{'SESSION_TIME'}{'18144000'} = "";
307 $selected{'SESSION_TIME'}{$settings{'SESSION_TIME'}} = "selected";
308
309 print <<END;
310 <tr>
311 <td>$Lang::tr{'Captive client session expiry time'}</td>
312 <td>
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>
316 <option value="28800" $selected{'SESSION_TIME'}{'28800'}>$Lang::tr{'eight hours'}</option>
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>
321 </td>
322 </tr>
323 END
324 }
325
326 print<<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>
347 <tr>
348 <td>
349 $Lang::tr{'Captive upload logo'}
350 </td>
351 <td>
352 <input type="file" name="logo">
353 <br>$Lang::tr{'Captive upload logo recommendations'}
354 </td>
355 </tr>
356 END
357
358 if (-e $logo) {
359 print <<END;
360 <tr>
361 <td>$Lang::tr{'Captive logo uploaded'}</td>
362 <td>$Lang::tr{'yes'}</td>
363 </tr>
364 END
365 }
366
367 my $terms = &getterms();
368 print <<END;
369 <tr>
370 <td>$Lang::tr{'Captive terms'}</td>
371 <td>
372 <textarea cols="50" rows="10" name="TERMS">$terms</textarea>
373 </td>
374 </tr>
375 <tr>
376 <td></td>
377 <td align='right'>
378 <input type='submit' name='ACTION' value="$Lang::tr{'save'}"/>
379 </td>
380 </tr>
381 </table></form>
382 END
383
384 &Header::closebox();
385
386 #if settings is set to use coupons, the coupon part has to be displayed
387 if ($settings{'AUTH'} eq 'COUPON') {
388 &coupons();
389 }
390
391 # Show active clients
392 &show_clients();
393
394 sub getterms() {
395 my @ret;
396
397 open(FILE, "<:utf8", "/var/ipfire/captive/terms.txt");
398 while(<FILE>) {
399 push(@ret, HTML::Entities::decode_entities($_));
400 }
401 close(FILE);
402
403 return join(/\n/, @ret);
404 }
405
406 sub 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
414 sub coupons() {
415 &Header::openbox('100%', 'left', $Lang::tr{'Captive generate coupon'});
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>
432 END
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 }
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">
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
525 <input type="submit" name="ACTION" value="$Lang::tr{'Captive generate coupon'}">
526 </div>
527 </form>
528 END
529
530 &Header::closebox();
531
532 # Show all coupons if exist
533 if (! -z $coupons) {
534 &show_coupons();
535 }
536 }
537
538 sub show_coupons() {
539 &General::readhasharray($coupons, \%couponhash) if (-e $coupons);
540
541 #if there are already generated but unsused coupons, print a table
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>
554 END
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]);
560 }
561
562 if ($count++ % 2) {
563 $col="bgcolor='$color{'color20'}'";
564 } else {
565 $col="bgcolor='$color{'color22'}'";
566 }
567
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>
587 END
588 }
589
590 print "</table>";
591
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>
600 END
601
602 &Header::closebox();
603 }
604
605 sub show_clients() {
606 # if there are active clients which use coupons show table
607 return if ( -z $clients || ! -f $clients );
608
609 my $count=0;
610 my $col;
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>
624 END
625
626 &General::readhasharray($clients, \%clientshash) if (-e $clients);
627 foreach my $key (keys %clientshash) {
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]));
630
631 #calculate endtime from clientshash
632 my $endtime;
633 if ($clientshash{$key}[3] eq '0'){
634 $endtime=$Lang::tr{'Captive nolimit'};
635 } else {
636 $endtime = sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime($clientshash{$key}[2]+$clientshash{$key}[3]));
637 }
638
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];
646
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>
662 END
663 }
664
665 print "</table>";
666
667 &Header::closebox();
668 }
669
670 sub 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
690 sub 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
821 &Header::closebigbox();
822 &Header::closepage();