]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - html/cgi-bin/captive.cgi
Merge remote-tracking branch 'origin/next'
[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
af6c5929
MT
37my %session_times = (
38 3600 => $Lang::tr{'one hour'},
39 14400 => $Lang::tr{'four hours'},
40 28800 => $Lang::tr{'eight hours'},
41 43200 => $Lang::tr{'twelve hours'},
42 86400 => $Lang::tr{'24 hours'},
43 604800 => $Lang::tr{'one week'},
44 1209600 => $Lang::tr{'two weeks'},
ad1204e4 45 2592000 => $Lang::tr{'one month'},
af6c5929
MT
46 31536000 => $Lang::tr{'one year'},
47 0 => "- $Lang::tr{'unlimited'} -",
48);
49
278309b9
MT
50my %selected = ();
51
97b91e8a
MT
52my $coupons = "${General::swroot}/captive/coupons";
53my %couponhash = ();
54
e2752bfe
MT
55my $logo = "${General::swroot}/captive/logo.dat";
56
8b920789
AM
57my %settings=();
58my %mainsettings;
59my %color;
60my %cgiparams=();
61my %netsettings=();
62my %checked=();
63my $errormessage='';
8b920789 64my $clients="${General::swroot}/captive/clients";
8b920789
AM
65my %clientshash=();
66my $settingsfile="${General::swroot}/captive/settings";
e14adf75 67unless (-e $settingsfile) { system("touch $settingsfile"); }
8b920789
AM
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
a54350cd
MT
76if ($cgiparams{'ACTION'} eq "export-coupons") {
77 my $pdf = &generate_pdf();
78
79 print "Content-Type: application/pdf\n";
80 print "Content-Disposition: attachment; filename=captive-portal-coupons.pdf\n";
81 print "\n"; # end headers
82
83 # Send PDF
84 print $pdf;
85
86 exit(0);
87}
88
89
8b920789
AM
90&Header::showhttpheaders();
91
b7a126d9 92if ($cgiparams{'ACTION'} eq $Lang::tr{'save'}) {
e2752bfe 93 my $file = $cgiparams{'logo'};
b7a126d9
MT
94 if ($file) {
95 # Check if the file extension is PNG/JPEG
b32d9e92 96 chomp $file;
b7a126d9 97
b32d9e92 98 my ($name, $path, $ext) = fileparse($file, qr/\.[^.]*$/);
b7a126d9
MT
99 if ($ext ne ".png" && $ext ne ".jpg" && $ext ne ".jpeg") {
100 $errormessage = $Lang::tr{'Captive wrong ext'};
b32d9e92 101 }
c7e78cc6 102 }
6945954c
AM
103
104 $settings{'ENABLE_GREEN'} = $cgiparams{'ENABLE_GREEN'};
105 $settings{'ENABLE_BLUE'} = $cgiparams{'ENABLE_BLUE'};
106 $settings{'AUTH'} = $cgiparams{'AUTH'};
6945954c 107 $settings{'TITLE'} = $cgiparams{'TITLE'};
f8d35875 108 $settings{'COLOR'} = $cgiparams{'COLOR'};
278309b9 109 $settings{'SESSION_TIME'} = $cgiparams{'SESSION_TIME'};
6945954c 110
b32d9e92
AM
111 if (!$errormessage){
112 #Check if we need to upload a new logo
e2752bfe
MT
113 if ($file) {
114 # Save logo
115 my ($filehandle) = CGI::upload("logo");
116
117 # XXX check filesize
118
119 open(FILE, ">$logo");
b32d9e92 120 binmode $filehandle;
e2752bfe
MT
121 while (<$filehandle>) {
122 print FILE;
b32d9e92 123 }
e2752bfe 124 close(FILE);
b32d9e92 125 }
c7e78cc6 126
b32d9e92
AM
127 &General::writehash("$settingsfile", \%settings);
128
9735e167 129 # Save terms
e2bd5a6e
MT
130 $cgiparams{'TERMS'} = &Header::escape($cgiparams{'TERMS'});
131 open(FH, ">:utf8", "/var/ipfire/captive/terms.txt") or die("$!");
132 print FH $cgiparams{'TERMS'};
133 close(FH);
134 $cgiparams{'TERMS'} = "";
9735e167 135
b32d9e92
AM
136 #execute binary to reload firewall rules
137 system("/usr/local/bin/captivectrl");
45129439
AM
138
139 if ($cgiparams{'ENABLE_BLUE'} eq 'on'){
140 system("/usr/local/bin/wirelessctrl");
141 }
8b920789 142 }
8b920789
AM
143}
144
af6c5929 145if ($cgiparams{'ACTION'} eq "$Lang::tr{'Captive generate coupons'}") {
e01c5ab7
AM
146 #check valid remark
147 if ($cgiparams{'REMARK'} ne '' && !&validremark($cgiparams{'REMARK'})){
148 $errormessage=$Lang::tr{'fwhost err remark'};
149 }
150
5cd9e28b 151 if (!$errormessage) {
9cba29f1 152 # Remember selected values
af6c5929 153 foreach my $val (("SESSION_TIME", "COUNT", "REMARK")) {
9cba29f1
MT
154 $settings{$val} = $cgiparams{$val};
155 }
156 &General::writehash($settingsfile, \%settings);
157
5cd9e28b
MT
158 &General::readhasharray($coupons, \%couponhash) if (-e $coupons);
159 my $now = time();
160
af6c5929
MT
161 # Expiry time in seconds
162 my $expires = $settings{'SESSION_TIME'};
733932de 163
af6c5929 164 my $count = $settings{'COUNT'} || 1;
5cd9e28b
MT
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] = ""; }
8b920789 186
5cd9e28b
MT
187 $couponhash{$key}[0] = $now;
188 $couponhash{$key}[1] = $code;
189 $couponhash{$key}[2] = $expires;
af6c5929 190 $couponhash{$key}[3] = $settings{'REMARK'};
5cd9e28b 191 }
8b920789 192
5cd9e28b
MT
193 # Save everything to disk
194 &General::writehasharray($coupons, \%couponhash);
8b920789
AM
195 }
196}
197
97b91e8a 198if ($cgiparams{'ACTION'} eq 'delete-coupon') {
8b920789
AM
199 #deletes an already generated but unused voucher
200
201 #read all generated vouchers
97b91e8a
MT
202 &General::readhasharray($coupons, \%couponhash) if (-e $coupons);
203 foreach my $key (keys %couponhash) {
204 if($cgiparams{'key'} eq $couponhash{$key}[0]){
8b920789 205 #write logenty with decoded remark
97b91e8a
MT
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");
8b920789 208 #delete line from hash
97b91e8a 209 delete $couponhash{$key};
8b920789
AM
210 last;
211 }
212 }
213 #write back hash
97b91e8a 214 &General::writehasharray($coupons, \%couponhash);
8b920789
AM
215}
216
97b91e8a 217if ($cgiparams{'ACTION'} eq 'delete-client') {
8b920789
AM
218 #delete voucher and connection in use
219
220 #read all active clients
97b91e8a 221 &General::readhasharray($clients, \%clientshash) if (-e $clients);
8b920789
AM
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
97b91e8a 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");
8b920789
AM
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
a41fe924
MT
243# If an error message exists, show a box with the error message
244if ($errormessage) {
245 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
246 print $errormessage;
247 &Header::closebox();
8b920789
AM
248}
249
a41fe924
MT
250# Prints the config box on the website
251&Header::openbox('100%', 'left', $Lang::tr{'Captive config'});
252print <<END
253 <form method='post' action='$ENV{'SCRIPT_NAME'}' enctype="multipart/form-data">\n
8b920789 254 <table width='100%' border="0">
8b920789
AM
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 276if ($netsettings{'GREEN_DEV'}){
f3217495
MT
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>
287END
a41fe924 288}
f3217495 289
a41fe924 290if ($netsettings{'BLUE_DEV'}){
f3217495
MT
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>
301END
a41fe924 302}
e01c5ab7 303
e01c5ab7 304print<<END
a41fe924
MT
305 <tr>
306 <td>
307 $Lang::tr{'Captive authentication'}
308 </td>
309 <td>
310 <select name='AUTH'>
e7d16ea5
MT
311 <option value="TERMS" $selected{'AUTH'}{'TERMS'} >$Lang::tr{'Captive terms'}</option>
312 <option value="COUPON" $selected{'AUTH'}{'COUPON'}>$Lang::tr{'Captive coupon'}</option>
a41fe924
MT
313 </select>
314 </td>
315 </tr>
8b920789
AM
316END
317;
8b920789 318
a41fe924
MT
319if ($settings{'AUTH'} eq 'TERMS') {
320 $selected{'SESSION_TIME'} = ();
af6c5929
MT
321 foreach my $session_time (keys %session_times) {
322 $selected{'SESSION_TIME'}{$session_time} = "";
323 }
a41fe924
MT
324 $selected{'SESSION_TIME'}{$settings{'SESSION_TIME'}} = "selected";
325
a41fe924 326 print <<END;
297ebdd4 327 <tr>
a41fe924 328 <td>$Lang::tr{'Captive client session expiry time'}</td>
297ebdd4 329 <td>
a41fe924 330 <select name="SESSION_TIME">
af6c5929
MT
331END
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>
338END
339 }
340
341 print <<END;
a41fe924 342 </select>
297ebdd4
MT
343 </td>
344 </tr>
345END
a41fe924 346}
297ebdd4 347
a41fe924
MT
348print<<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>
a41fe924
MT
369 <tr>
370 <td>
81019811 371 $Lang::tr{'Captive upload logo'}
a41fe924
MT
372 </td>
373 <td>
374 <input type="file" name="logo">
81019811 375 <br>$Lang::tr{'Captive upload logo recommendations'}
a41fe924
MT
376 </td>
377 </tr>
e2752bfe 378END
827d3f61 379
a41fe924 380if (-e $logo) {
297ebdd4 381 print <<END;
8b920789 382 <tr>
a41fe924
MT
383 <td>$Lang::tr{'Captive logo uploaded'}</td>
384 <td>$Lang::tr{'yes'}</td>
8b920789 385 </tr>
a41fe924
MT
386END
387}
388
e2bd5a6e 389my $terms = &getterms();
a41fe924 390print <<END;
e2bd5a6e 391 <tr>
ebfb8996 392 <td>$Lang::tr{'Captive terms'}</td>
e2bd5a6e
MT
393 <td>
394 <textarea cols="50" rows="10" name="TERMS">$terms</textarea>
395 </td>
396 </tr>
a41fe924
MT
397 <tr>
398 <td></td>
399 <td align='right'>
400 <input type='submit' name='ACTION' value="$Lang::tr{'save'}"/>
401 </td>
402 </tr>
297ebdd4 403 </table></form>
8b920789 404END
297ebdd4 405
a41fe924 406&Header::closebox();
8b920789 407
a41fe924
MT
408#if settings is set to use coupons, the coupon part has to be displayed
409if ($settings{'AUTH'} eq 'COUPON') {
410 &coupons();
a41fe924
MT
411}
412
792f1a3b
MT
413# Show active clients
414&show_clients();
415
a41fe924
MT
416sub getterms() {
417 my @ret;
418
419 open(FILE, "<:utf8", "/var/ipfire/captive/terms.txt");
420 while(<FILE>) {
421 push(@ret, HTML::Entities::decode_entities($_));
8b920789 422 }
a41fe924
MT
423 close(FILE);
424
425 return join(/\n/, @ret);
8b920789
AM
426}
427
8b920789
AM
428sub 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
97b91e8a 436sub coupons() {
af6c5929
MT
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
4f75fbfe
MT
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%'>
af6c5929 453 <select name="SESSION_TIME">
4f75fbfe 454END
08061703 455
af6c5929 456 foreach my $session_time (sort { $a <=> $b } keys %session_times) {
4f75fbfe 457 print <<END;
af6c5929
MT
458 <option value="$session_time" $selected{'SESSION_TIME'}{$session_time}>
459 $session_times{$session_time}
460 </option>
461END
462 }
463
464 print <<END;
465 </select>
4f75fbfe
MT
466 </td>
467 </tr>
468 <tr>
469 <td>$Lang::tr{'remark'}</td>
470 <td>
af6c5929
MT
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>
4f75fbfe
MT
492 </td>
493 </tr>
494 </table>
495
496 <div align="right">
af6c5929 497 <input type="submit" name="ACTION" value="$Lang::tr{'Captive generate coupons'}">
4f75fbfe
MT
498 </div>
499 </form>
500END
08061703 501
8b920789 502 &Header::closebox();
97b91e8a
MT
503
504 # Show all coupons if exist
505 if (! -z $coupons) {
506 &show_coupons();
507 }
8b920789
AM
508}
509
97b91e8a 510sub show_coupons() {
a0b271e4
MT
511 &General::readhasharray($coupons, \%couponhash) if (-e $coupons);
512
97b91e8a 513 #if there are already generated but unsused coupons, print a table
a0b271e4
MT
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>
8b920789 526END
a0b271e4
MT
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]);
c7e78cc6
AM
532 }
533
a0b271e4 534 if ($count++ % 2) {
8b920789 535 $col="bgcolor='$color{'color20'}'";
a0b271e4 536 } else {
8b920789 537 $col="bgcolor='$color{'color22'}'";
8b920789 538 }
c7e78cc6 539
a0b271e4
MT
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>
559END
8b920789 560 }
c7e78cc6 561
8b920789 562 print "</table>";
a0b271e4 563
a54350cd
MT
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>
572END
573
8b920789
AM
574 &Header::closebox();
575}
576
97b91e8a 577sub show_clients() {
41964aba 578 # if there are active clients which use coupons show table
8b920789 579 return if ( -z $clients || ! -f $clients );
41964aba 580
8b920789
AM
581 my $count=0;
582 my $col;
41964aba
MT
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>
8b920789 596END
41964aba 597
97b91e8a 598 &General::readhasharray($clients, \%clientshash) if (-e $clients);
41964aba 599 foreach my $key (keys %clientshash) {
c7e78cc6
AM
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]));
41964aba 602
c7e78cc6
AM
603 #calculate endtime from clientshash
604 my $endtime;
605 if ($clientshash{$key}[3] eq '0'){
606 $endtime=$Lang::tr{'Captive nolimit'};
41964aba 607 } else {
c7e78cc6
AM
608 $endtime = sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime($clientshash{$key}[2]+$clientshash{$key}[3]));
609 }
e01c5ab7 610
41964aba
MT
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];
e01c5ab7 618
41964aba
MT
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>
634END
8b920789 635 }
c7e78cc6 636
8b920789 637 print "</table>";
41964aba 638
8b920789
AM
639 &Header::closebox();
640}
641
e01c5ab7
AM
642sub 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
a54350cd
MT
662sub 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'}) {
0545cba7 760 $f_headline->text_center(decode("utf8", $settings{'TITLE'}));
a54350cd
MT
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
8b920789
AM
793&Header::closebigbox();
794&Header::closepage();