]>
Commit | Line | Data |
---|---|---|
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; |
8b920789 | 23 | use HTML::Entities(); |
b32d9e92 AM |
24 | use File::Basename; |
25 | ||
8b920789 AM |
26 | # enable only the following on debugging purpose |
27 | #use warnings; | |
28 | #use CGI::Carp 'fatalsToBrowser'; | |
29 | ||
30 | require '/var/ipfire/general-functions.pl'; | |
31 | require "${General::swroot}/lang.pl"; | |
32 | require "${General::swroot}/header.pl"; | |
e14adf75 | 33 | |
278309b9 MT |
34 | my %selected = (); |
35 | ||
97b91e8a MT |
36 | my $coupons = "${General::swroot}/captive/coupons"; |
37 | my %couponhash = (); | |
38 | ||
e2752bfe MT |
39 | my $logo = "${General::swroot}/captive/logo.dat"; |
40 | ||
8b920789 AM |
41 | my %settings=(); |
42 | my %mainsettings; | |
43 | my %color; | |
44 | my %cgiparams=(); | |
45 | my %netsettings=(); | |
46 | my %checked=(); | |
47 | my $errormessage=''; | |
8b920789 | 48 | my $clients="${General::swroot}/captive/clients"; |
8b920789 AM |
49 | my %clientshash=(); |
50 | my $settingsfile="${General::swroot}/captive/settings"; | |
e14adf75 | 51 | unless (-e $settingsfile) { system("touch $settingsfile"); } |
8b920789 AM |
52 | |
53 | &Header::getcgihash(\%cgiparams); | |
54 | ||
55 | &General::readhash("${General::swroot}/main/settings", \%mainsettings); | |
56 | &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color); | |
57 | &General::readhash("$settingsfile", \%settings) if(-f $settingsfile); | |
58 | &General::readhash("${General::swroot}/ethernet/settings", \%netsettings); | |
59 | ||
60 | &Header::showhttpheaders(); | |
61 | ||
b7a126d9 | 62 | if ($cgiparams{'ACTION'} eq $Lang::tr{'save'}) { |
e2752bfe | 63 | my $file = $cgiparams{'logo'}; |
b7a126d9 MT |
64 | if ($file) { |
65 | # Check if the file extension is PNG/JPEG | |
b32d9e92 | 66 | chomp $file; |
b7a126d9 | 67 | |
b32d9e92 | 68 | my ($name, $path, $ext) = fileparse($file, qr/\.[^.]*$/); |
b7a126d9 MT |
69 | if ($ext ne ".png" && $ext ne ".jpg" && $ext ne ".jpeg") { |
70 | $errormessage = $Lang::tr{'Captive wrong ext'}; | |
b32d9e92 | 71 | } |
c7e78cc6 | 72 | } |
6945954c AM |
73 | |
74 | $settings{'ENABLE_GREEN'} = $cgiparams{'ENABLE_GREEN'}; | |
75 | $settings{'ENABLE_BLUE'} = $cgiparams{'ENABLE_BLUE'}; | |
76 | $settings{'AUTH'} = $cgiparams{'AUTH'}; | |
6945954c | 77 | $settings{'TITLE'} = $cgiparams{'TITLE'}; |
f8d35875 | 78 | $settings{'COLOR'} = $cgiparams{'COLOR'}; |
278309b9 | 79 | $settings{'SESSION_TIME'} = $cgiparams{'SESSION_TIME'}; |
6945954c | 80 | |
b32d9e92 AM |
81 | if (!$errormessage){ |
82 | #Check if we need to upload a new logo | |
e2752bfe MT |
83 | if ($file) { |
84 | # Save logo | |
85 | my ($filehandle) = CGI::upload("logo"); | |
86 | ||
87 | # XXX check filesize | |
88 | ||
89 | open(FILE, ">$logo"); | |
b32d9e92 | 90 | binmode $filehandle; |
e2752bfe MT |
91 | while (<$filehandle>) { |
92 | print FILE; | |
b32d9e92 | 93 | } |
e2752bfe | 94 | close(FILE); |
b32d9e92 | 95 | } |
c7e78cc6 | 96 | |
b32d9e92 AM |
97 | &General::writehash("$settingsfile", \%settings); |
98 | ||
9735e167 MT |
99 | # Save terms |
100 | if ($cgiparams{'TERMS'}){ | |
101 | $cgiparams{'TERMS'} = &Header::escape($cgiparams{'TERMS'}); | |
102 | open(FH, ">:utf8", "/var/ipfire/captive/terms.txt") or die("$!"); | |
103 | print FH $cgiparams{'TERMS'}; | |
104 | close(FH); | |
105 | $cgiparams{'TERMS'} = ""; | |
b32d9e92 | 106 | } |
9735e167 | 107 | |
b32d9e92 AM |
108 | #execute binary to reload firewall rules |
109 | system("/usr/local/bin/captivectrl"); | |
45129439 AM |
110 | |
111 | if ($cgiparams{'ENABLE_BLUE'} eq 'on'){ | |
112 | system("/usr/local/bin/wirelessctrl"); | |
113 | } | |
8b920789 | 114 | } |
8b920789 AM |
115 | } |
116 | ||
5cd9e28b | 117 | if ($cgiparams{'ACTION'} eq "$Lang::tr{'Captive generate coupon'}") { |
733932de MT |
118 | # Check expiry time |
119 | if ($cgiparams{'EXP_HOUR'} + $cgiparams{'EXP_DAY'} + $cgiparams{'EXP_WEEK'} + $cgiparams{'EXP_MONTH'} == 0 && $cgiparams{'UNLIMITED'} == '') { | |
120 | $errormessage = $Lang::tr{'Captive noexpiretime'}; | |
08061703 | 121 | } |
8b920789 | 122 | |
e01c5ab7 AM |
123 | #check valid remark |
124 | if ($cgiparams{'REMARK'} ne '' && !&validremark($cgiparams{'REMARK'})){ | |
125 | $errormessage=$Lang::tr{'fwhost err remark'}; | |
126 | } | |
127 | ||
5cd9e28b | 128 | if (!$errormessage) { |
9cba29f1 MT |
129 | # Remember selected values |
130 | foreach my $val (("UNLIMITED", "EXP_HOUR", "EXP_DAY", "EXP_WEEK", "EXP_MONTH")) { | |
131 | $settings{$val} = $cgiparams{$val}; | |
132 | } | |
133 | &General::writehash($settingsfile, \%settings); | |
134 | ||
5cd9e28b MT |
135 | &General::readhasharray($coupons, \%couponhash) if (-e $coupons); |
136 | my $now = time(); | |
137 | ||
733932de MT |
138 | # Calculate expiry time in seconds |
139 | my $expires = 0; | |
140 | ||
141 | if ($settings{'UNLIMITED'} ne 'on') { | |
142 | $expires += $settings{'EXP_HOUR'}; | |
143 | $expires += $settings{'EXP_DAY'}; | |
144 | $expires += $settings{'EXP_WEEK'}; | |
145 | $expires += $settings{'EXP_MONTH'}; | |
146 | } | |
147 | ||
5cd9e28b MT |
148 | my $count = $cgiparams{'COUNT'} || 1; |
149 | while($count-- > 0) { | |
150 | # Generate a new code | |
151 | my $code = &gencode(); | |
152 | ||
153 | # Check if the coupon code already exists | |
154 | foreach my $key (keys %couponhash) { | |
155 | if($couponhash{$key}[1] eq $code) { | |
156 | # Code already exists, so try again | |
157 | $code = ""; | |
158 | $count++; | |
159 | last; | |
160 | } | |
161 | } | |
162 | ||
163 | next if ($code eq ""); | |
164 | ||
165 | # Get a new key from hash | |
166 | my $key = &General::findhasharraykey(\%couponhash); | |
167 | ||
168 | # Initialize all fields | |
169 | foreach my $i (0 .. 3) { $couponhash{$key}[$i] = ""; } | |
8b920789 | 170 | |
5cd9e28b MT |
171 | $couponhash{$key}[0] = $now; |
172 | $couponhash{$key}[1] = $code; | |
173 | $couponhash{$key}[2] = $expires; | |
174 | $couponhash{$key}[3] = $cgiparams{'REMARK'}; | |
175 | } | |
8b920789 | 176 | |
5cd9e28b MT |
177 | # Save everything to disk |
178 | &General::writehasharray($coupons, \%couponhash); | |
8b920789 AM |
179 | } |
180 | } | |
181 | ||
97b91e8a | 182 | if ($cgiparams{'ACTION'} eq 'delete-coupon') { |
8b920789 AM |
183 | #deletes an already generated but unused voucher |
184 | ||
185 | #read all generated vouchers | |
97b91e8a MT |
186 | &General::readhasharray($coupons, \%couponhash) if (-e $coupons); |
187 | foreach my $key (keys %couponhash) { | |
188 | if($cgiparams{'key'} eq $couponhash{$key}[0]){ | |
8b920789 | 189 | #write logenty with decoded remark |
97b91e8a MT |
190 | my $rem=HTML::Entities::decode_entities($couponhash{$key}[4]); |
191 | &General::log("Captive", "Delete unused coupon $couponhash{$key}[1] $couponhash{$key}[2] hours valid expires on $couponhash{$key}[3] remark $rem"); | |
8b920789 | 192 | #delete line from hash |
97b91e8a | 193 | delete $couponhash{$key}; |
8b920789 AM |
194 | last; |
195 | } | |
196 | } | |
197 | #write back hash | |
97b91e8a | 198 | &General::writehasharray($coupons, \%couponhash); |
8b920789 AM |
199 | } |
200 | ||
97b91e8a | 201 | if ($cgiparams{'ACTION'} eq 'delete-client') { |
8b920789 AM |
202 | #delete voucher and connection in use |
203 | ||
204 | #read all active clients | |
97b91e8a | 205 | &General::readhasharray($clients, \%clientshash) if (-e $clients); |
8b920789 AM |
206 | foreach my $key (keys %clientshash) { |
207 | if($cgiparams{'key'} eq $clientshash{$key}[0]){ | |
208 | #prepare log entry with decoded remark | |
209 | my $rem=HTML::Entities::decode_entities($clientshash{$key}[7]); | |
210 | #write logentry | |
97b91e8a | 211 | &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 |
212 | #delete line from hash |
213 | delete $clientshash{$key}; | |
214 | last; | |
215 | } | |
216 | } | |
217 | #write back hash | |
218 | &General::writehasharray("$clients", \%clientshash); | |
219 | #reload firewallrules to kill connection of client | |
220 | system("/usr/local/bin/captivectrl"); | |
221 | } | |
222 | ||
223 | #open webpage, print header and open box | |
224 | &Header::openpage($Lang::tr{'Captive menu'}, 1, ''); | |
225 | &Header::openbigbox(); | |
226 | ||
a41fe924 MT |
227 | # If an error message exists, show a box with the error message |
228 | if ($errormessage) { | |
229 | &Header::openbox('100%', 'left', $Lang::tr{'error messages'}); | |
230 | print $errormessage; | |
231 | &Header::closebox(); | |
8b920789 AM |
232 | } |
233 | ||
a41fe924 MT |
234 | # Prints the config box on the website |
235 | &Header::openbox('100%', 'left', $Lang::tr{'Captive config'}); | |
236 | print <<END | |
237 | <form method='post' action='$ENV{'SCRIPT_NAME'}' enctype="multipart/form-data">\n | |
8b920789 AM |
238 | <table width='100%' border="0"> |
239 | <tr> | |
240 | END | |
241 | ; | |
e01c5ab7 | 242 | |
a41fe924 MT |
243 | #check which parameters have to be enabled (from settings file) |
244 | $checked{'ENABLE_GREEN'}{'off'} = ''; | |
245 | $checked{'ENABLE_GREEN'}{'on'} = ''; | |
246 | $checked{'ENABLE_GREEN'}{$settings{'ENABLE_GREEN'}} = "checked='checked'"; | |
8b920789 | 247 | |
a41fe924 MT |
248 | $checked{'ENABLE_BLUE'}{'off'} = ''; |
249 | $checked{'ENABLE_BLUE'}{'on'} = ''; | |
250 | $checked{'ENABLE_BLUE'}{$settings{'ENABLE_BLUE'}} = "checked='checked'"; | |
c7e78cc6 | 251 | |
a41fe924 MT |
252 | $checked{'UNLIMITED'}{'off'} = ''; |
253 | $checked{'UNLIMITED'}{'on'} = ''; | |
254 | $checked{'UNLIMITED'}{$settings{'UNLIMITED'}} = "checked='checked'"; | |
255 | ||
e7d16ea5 MT |
256 | $selected{'AUTH'} = (); |
257 | $selected{'AUTH'}{'COUPON'} = ""; | |
258 | $selected{'AUTH'}{'TERMS'} = ""; | |
259 | $selected{'AUTH'}{$settings{'AUTH'}} = "selected"; | |
260 | ||
a41fe924 MT |
261 | if ($netsettings{'GREEN_DEV'}){ |
262 | 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>"; | |
263 | } | |
264 | if ($netsettings{'BLUE_DEV'}){ | |
265 | 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>"; | |
266 | } | |
e01c5ab7 | 267 | |
e01c5ab7 | 268 | print<<END |
a41fe924 MT |
269 | </tr> |
270 | <tr> | |
271 | <td> | |
272 | $Lang::tr{'Captive authentication'} | |
273 | </td> | |
274 | <td> | |
275 | <select name='AUTH'> | |
e7d16ea5 MT |
276 | <option value="TERMS" $selected{'AUTH'}{'TERMS'} >$Lang::tr{'Captive terms'}</option> |
277 | <option value="COUPON" $selected{'AUTH'}{'COUPON'}>$Lang::tr{'Captive coupon'}</option> | |
a41fe924 MT |
278 | </select> |
279 | </td> | |
280 | </tr> | |
8b920789 AM |
281 | END |
282 | ; | |
8b920789 | 283 | |
a41fe924 MT |
284 | if ($settings{'AUTH'} eq 'TERMS') { |
285 | $selected{'SESSION_TIME'} = (); | |
286 | $selected{'SESSION_TIME'}{'0'} = ""; | |
287 | $selected{'SESSION_TIME'}{'3600'} = ""; | |
288 | $selected{'SESSION_TIME'}{'86400'} = ""; | |
289 | $selected{'SESSION_TIME'}{'604800'} = ""; | |
290 | $selected{'SESSION_TIME'}{'18144000'} = ""; | |
291 | $selected{'SESSION_TIME'}{$settings{'SESSION_TIME'}} = "selected"; | |
292 | ||
293 | my $terms = &getterms(); | |
294 | print <<END; | |
297ebdd4 | 295 | <tr> |
a41fe924 | 296 | <td></td> |
297ebdd4 | 297 | <td> |
a41fe924 | 298 | <textarea cols="50" rows="10" name="TERMS">$terms</textarea> |
297ebdd4 MT |
299 | </td> |
300 | </tr> | |
a41fe924 | 301 | |
297ebdd4 | 302 | <tr> |
a41fe924 | 303 | <td>$Lang::tr{'Captive client session expiry time'}</td> |
297ebdd4 | 304 | <td> |
a41fe924 MT |
305 | <select name="SESSION_TIME"> |
306 | <option value="0" $selected{'SESSION_TIME'}{'0'}>- $Lang::tr{'unlimited'} -</option> | |
307 | <option value="3600" $selected{'SESSION_TIME'}{'3600'}>$Lang::tr{'one hour'}</option> | |
308 | <option value="86400" $selected{'SESSION_TIME'}{'86400'}>$Lang::tr{'24 hours'}</option> | |
309 | <option value="604800" $selected{'SESSION_TIME'}{'604800'}>$Lang::tr{'one week'}</option> | |
310 | <option value="18144000" $selected{'SESSION_TIME'}{'18144000'}>$Lang::tr{'one month'}</option> | |
311 | </select> | |
297ebdd4 MT |
312 | </td> |
313 | </tr> | |
314 | END | |
a41fe924 | 315 | } |
297ebdd4 | 316 | |
a41fe924 MT |
317 | print<<END; |
318 | <tr> | |
319 | <td colspan="2"> | |
320 | <br> | |
321 | <strong>$Lang::tr{'Captive branding'}</strong> | |
322 | </td> | |
323 | </tr> | |
324 | <tr> | |
325 | <td> | |
326 | $Lang::tr{'Captive title'} | |
327 | </td> | |
328 | <td> | |
329 | <input type='text' name='TITLE' value="$settings{'TITLE'}" size='40'> | |
330 | </td> | |
331 | </tr> | |
332 | <tr> | |
333 | <td>$Lang::tr{'Captive brand color'}</td> | |
334 | <td> | |
335 | <input type="color" name="COLOR" value="$settings{'COLOR'}"> | |
336 | </td> | |
337 | </tr> | |
a41fe924 MT |
338 | <tr> |
339 | <td> | |
81019811 | 340 | $Lang::tr{'Captive upload logo'} |
a41fe924 MT |
341 | </td> |
342 | <td> | |
343 | <input type="file" name="logo"> | |
81019811 | 344 | <br>$Lang::tr{'Captive upload logo recommendations'} |
a41fe924 MT |
345 | </td> |
346 | </tr> | |
e2752bfe | 347 | END |
827d3f61 | 348 | |
a41fe924 | 349 | if (-e $logo) { |
297ebdd4 | 350 | print <<END; |
8b920789 | 351 | <tr> |
a41fe924 MT |
352 | <td>$Lang::tr{'Captive logo uploaded'}</td> |
353 | <td>$Lang::tr{'yes'}</td> | |
8b920789 | 354 | </tr> |
a41fe924 MT |
355 | END |
356 | } | |
357 | ||
358 | print <<END; | |
359 | <tr> | |
360 | <td></td> | |
361 | <td align='right'> | |
362 | <input type='submit' name='ACTION' value="$Lang::tr{'save'}"/> | |
363 | </td> | |
364 | </tr> | |
297ebdd4 | 365 | </table></form> |
8b920789 | 366 | END |
297ebdd4 | 367 | |
a41fe924 | 368 | &Header::closebox(); |
8b920789 | 369 | |
a41fe924 MT |
370 | #if settings is set to use coupons, the coupon part has to be displayed |
371 | if ($settings{'AUTH'} eq 'COUPON') { | |
372 | &coupons(); | |
373 | } else { | |
374 | #otherwise we show the licensepart | |
375 | &show_license_connections(); | |
376 | } | |
377 | ||
378 | sub getterms() { | |
379 | my @ret; | |
380 | ||
381 | open(FILE, "<:utf8", "/var/ipfire/captive/terms.txt"); | |
382 | while(<FILE>) { | |
383 | push(@ret, HTML::Entities::decode_entities($_)); | |
8b920789 | 384 | } |
a41fe924 MT |
385 | close(FILE); |
386 | ||
387 | return join(/\n/, @ret); | |
8b920789 AM |
388 | } |
389 | ||
8b920789 AM |
390 | sub gencode(){ |
391 | #generate a random code only letters from A-Z except 'O' and 0-9 | |
392 | my @chars = ("A".."N", "P".."Z", "0".."9"); | |
393 | my $randomstring; | |
394 | $randomstring .= $chars[rand @chars] for 1..8; | |
395 | return $randomstring; | |
396 | } | |
397 | ||
97b91e8a MT |
398 | sub coupons() { |
399 | &Header::openbox('100%', 'left', $Lang::tr{'Captive generate coupon'}); | |
4f75fbfe MT |
400 | print <<END; |
401 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
402 | <table border='0' width='100%'> | |
403 | <tr> | |
404 | <td width='30%'> | |
405 | $Lang::tr{'Captive vouchervalid'} | |
406 | </td> | |
407 | <td width='70%'> | |
408 | <table class='tbl' border='0' width='100%'> | |
409 | <tr> | |
410 | <th>$Lang::tr{'hours'}</th> | |
411 | <th>$Lang::tr{'days'}</th> | |
412 | <th>$Lang::tr{'weeks'}</th> | |
413 | <th>$Lang::tr{'months'}</th> | |
414 | <th></th> | |
415 | </tr> | |
416 | END | |
08061703 AM |
417 | |
418 | #print hour-dropdownbox | |
419 | my $hrs=3600; | |
420 | print "<tr height='40px'><td><select name='EXP_HOUR' style='width:8em;'>"; | |
421 | print "<option value='0' "; | |
422 | print " selected='selected'" if ($settings{'EXP_HOUR'} eq '0'); | |
423 | print ">--</option>"; | |
424 | for (my $i = 1; $i<25; $i++){ | |
425 | my $exp_sec = $i * $hrs; | |
426 | print "<option value='$exp_sec' "; | |
427 | print " selected='selected'" if ($settings{'EXP_HOUR'} eq $exp_sec); | |
428 | print ">$i</option>"; | |
429 | } | |
430 | print "</td><td>"; | |
431 | ||
432 | #print day-dropdownbox | |
433 | my $days=3600*24; | |
434 | print "<select name='EXP_DAY' style='width:8em;'>"; | |
435 | print "<option value='0' "; | |
436 | print " selected='selected'" if ($settings{'EXP_DAY'} eq '0'); | |
437 | print ">--</option>"; | |
438 | for (my $i = 1; $i<8; $i++){ | |
439 | my $exp_sec = $i * $days; | |
440 | print "<option value='$exp_sec' "; | |
441 | print " selected='selected'" if ($settings{'EXP_DAY'} eq $exp_sec); | |
442 | print ">$i</option>"; | |
443 | } | |
444 | print "</td><td>"; | |
445 | ||
446 | #print week-dropdownbox | |
447 | my $week=3600*24*7; | |
448 | print "<select name='EXP_WEEK' style='width:8em;'>"; | |
449 | print "<option value='0' "; | |
450 | print " selected='selected'" if ($settings{'EXP_WEEK'} eq '0'); | |
451 | print ">--</option>"; | |
452 | for (my $i = 1; $i<5; $i++){ | |
453 | my $exp_sec = $i * $week; | |
454 | print "<option value='$exp_sec' "; | |
455 | print " selected='selected'" if ($settings{'EXP_WEEK'} eq $exp_sec); | |
456 | print ">$i</option>"; | |
457 | } | |
458 | print "</td><td>"; | |
459 | ||
460 | #print month-dropdownbox | |
461 | my $month=3600*24*30; | |
462 | print "<select name='EXP_MONTH' style='width:8em;'>"; | |
463 | print "<option value='0' "; | |
464 | print " selected='selected'" if ($settings{'EXP_MONTH'} eq '0'); | |
465 | print ">--</option>"; | |
466 | for (my $i = 1; $i<13; $i++){ | |
467 | my $exp_sec = $i * $month; | |
468 | print "<option value='$exp_sec' "; | |
469 | print " selected='selected'" if ($settings{'EXP_MONTH'} eq $exp_sec); | |
470 | print ">$i</option>"; | |
471 | } | |
4f75fbfe MT |
472 | print <<END; |
473 | </td> | |
474 | <td> | |
475 | <label> | |
476 | <input type='checkbox' name='UNLIMITED' $checked{'UNLIMITED'}{'on'} /> | |
477 | $Lang::tr{'Captive nolimit'} | |
478 | </label> | |
479 | </td> | |
480 | </tr> | |
481 | </table> | |
482 | </td> | |
483 | </tr> | |
484 | <tr> | |
485 | <td>$Lang::tr{'remark'}</td> | |
486 | <td> | |
487 | <input type='text' style='width: 98%;' name='REMARK' align='left'> | |
488 | </td> | |
489 | </tr> | |
490 | </table> | |
491 | ||
492 | <div align="right"> | |
5cd9e28b MT |
493 | <select name="COUNT"> |
494 | <option value="1">1</option> | |
495 | <option value="2">2</option> | |
496 | <option value="3">3</option> | |
497 | <option value="4">4</option> | |
498 | <option value="5">5</option> | |
499 | <option value="6">6</option> | |
500 | <option value="7">7</option> | |
501 | <option value="8">8</option> | |
502 | <option value="9">9</option> | |
503 | <option value="10">10</option> | |
504 | <option value="20">20</option> | |
505 | <option value="50">50</option> | |
506 | <option value="100">100</option> | |
507 | </select> | |
508 | ||
4f75fbfe MT |
509 | <input type="submit" name="ACTION" value="$Lang::tr{'Captive generate coupon'}"> |
510 | </div> | |
511 | </form> | |
512 | END | |
08061703 | 513 | |
8b920789 | 514 | &Header::closebox(); |
97b91e8a MT |
515 | |
516 | # Show all coupons if exist | |
517 | if (! -z $coupons) { | |
518 | &show_coupons(); | |
519 | } | |
520 | ||
521 | if (! -z $clients) { | |
522 | &show_clients(); | |
523 | } | |
8b920789 AM |
524 | } |
525 | ||
526 | sub show_license_connections(){ | |
527 | #if there are active clients, show the box with active connections | |
528 | return if ( -z $clients || ! -f $clients ); | |
529 | my $count=0; | |
530 | my $col; | |
531 | &Header::openbox('100%', 'left', $Lang::tr{'Captive voactive'}); | |
532 | print<<END | |
533 | <center><table class='tbl'> | |
534 | <tr> | |
97b91e8a | 535 | <th align='center' width='15%'>$Lang::tr{'Captive coupon'}</th><th th align='center' width='15%'>$Lang::tr{'Captive activated'}</th><th th align='center' width='15%'>$Lang::tr{'Captive expire'}</th><th align='center' width='50%'><font size='1'>$Lang::tr{'Captive mac'}</th><th th align='center' width='5%'>$Lang::tr{'delete'}</th></tr> |
8b920789 AM |
536 | END |
537 | ; | |
538 | #read all clients from hash and show table | |
97b91e8a | 539 | &General::readhasharray($clients, \%clientshash) if (-e $clients); |
8b920789 | 540 | foreach my $key (keys %clientshash){ |
c7e78cc6 AM |
541 | my $starttime = sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime($clientshash{$key}[2])); |
542 | my $endtime; | |
543 | if ($clientshash{$key}[3] eq '0'){ | |
544 | $endtime=$Lang::tr{'Captive nolimit'}; | |
545 | }else{ | |
546 | $endtime=sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime($clientshash{$key}[2]+$clientshash{$key}[3])); | |
547 | } | |
548 | ||
8b920789 AM |
549 | if ($count % 2){ |
550 | print" <tr>"; | |
551 | $col="bgcolor='$color{'color20'}'"; | |
552 | }else{ | |
553 | $col="bgcolor='$color{'color22'}'"; | |
554 | print" <tr>"; | |
555 | } | |
c7e78cc6 AM |
556 | print "<td $col><center>$clientshash{$key}[4]</td><td $col><center>$starttime "; |
557 | print "</center></td><td $col><center>$endtime "; | |
97b91e8a | 558 | print "</td><td $col><center>$clientshash{$key}[0]</td><td $col><form method='post'><center><input type='image' src='/images/delete.gif' align='middle' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' /><form method='post'><input type='hidden' name='ACTION' value='delete-client' /><input type='hidden' name='key' value='$clientshash{$key}[0]' /></form></tr>"; |
8b920789 AM |
559 | $count++; |
560 | } | |
561 | ||
562 | print "</table>"; | |
563 | &Header::closebox(); | |
564 | } | |
565 | ||
97b91e8a | 566 | sub show_coupons() { |
a0b271e4 MT |
567 | &General::readhasharray($coupons, \%couponhash) if (-e $coupons); |
568 | ||
97b91e8a | 569 | #if there are already generated but unsused coupons, print a table |
a0b271e4 MT |
570 | &Header::openbox('100%', 'left', $Lang::tr{'Captive issued coupons'}); |
571 | ||
572 | print <<END; | |
573 | <table class='tbl' border='0'> | |
574 | <tr> | |
575 | <th align='center' width='15%'> | |
576 | $Lang::tr{'Captive coupon'} | |
577 | </th> | |
578 | <th align='center' width='15%'>$Lang::tr{'Captive expiry time'}</th> | |
579 | <th align='center' width='65%'>$Lang::tr{'remark'}</th> | |
580 | <th align='center' width='5%'>$Lang::tr{'delete'}</th> | |
581 | </tr> | |
8b920789 | 582 | END |
a0b271e4 MT |
583 | |
584 | foreach my $key (keys %couponhash) { | |
585 | my $expirytime = $Lang::tr{'Captive nolimit'}; | |
586 | if ($couponhash{$key}[2] > 0) { | |
587 | $expirytime = &General::format_time($couponhash{$key}[2]); | |
c7e78cc6 AM |
588 | } |
589 | ||
a0b271e4 | 590 | if ($count++ % 2) { |
8b920789 | 591 | $col="bgcolor='$color{'color20'}'"; |
a0b271e4 | 592 | } else { |
8b920789 | 593 | $col="bgcolor='$color{'color22'}'"; |
8b920789 | 594 | } |
c7e78cc6 | 595 | |
a0b271e4 MT |
596 | print <<END; |
597 | <tr> | |
598 | <td $col align="center"> | |
599 | <b>$couponhash{$key}[1]</b> | |
600 | </td> | |
601 | <td $col align="center"> | |
602 | $expirytime | |
603 | </td> | |
604 | <td $col align="center"> | |
605 | $couponhash{$key}[3] | |
606 | </td> | |
607 | <td $col align="center"> | |
608 | <form method='post'> | |
609 | <input type='image' src='/images/delete.gif' align='middle' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' /> | |
610 | <input type='hidden' name='ACTION' value='delete-coupon' /> | |
611 | <input type='hidden' name='key' value='$couponhash{$key}[0]' /> | |
612 | </form> | |
613 | </td> | |
614 | </tr> | |
615 | END | |
8b920789 | 616 | } |
c7e78cc6 | 617 | |
8b920789 | 618 | print "</table>"; |
a0b271e4 | 619 | |
8b920789 AM |
620 | &Header::closebox(); |
621 | } | |
622 | ||
97b91e8a | 623 | sub show_clients() { |
41964aba | 624 | # if there are active clients which use coupons show table |
8b920789 | 625 | return if ( -z $clients || ! -f $clients ); |
41964aba | 626 | |
8b920789 AM |
627 | my $count=0; |
628 | my $col; | |
41964aba MT |
629 | |
630 | &Header::openbox('100%', 'left', $Lang::tr{'Captive clients'}); | |
631 | ||
632 | print <<END; | |
633 | <table class='tbl' width='100%'> | |
634 | <tr> | |
635 | <th align='center' width='15%'>$Lang::tr{'Captive coupon'}</th> | |
636 | <th align='center' width='15%'>$Lang::tr{'Captive activated'}</th> | |
637 | <th align='center' width='15%'>$Lang::tr{'Captive expiry time'}</th> | |
638 | <th align='center' width='10%'>$Lang::tr{'Captive mac'}</th> | |
639 | <th align='center' width='43%'>$Lang::tr{'remark'}</th> | |
640 | <th align='center' width='5%'>$Lang::tr{'delete'}</th> | |
641 | </tr> | |
8b920789 | 642 | END |
41964aba | 643 | |
97b91e8a | 644 | &General::readhasharray($clients, \%clientshash) if (-e $clients); |
41964aba | 645 | foreach my $key (keys %clientshash) { |
c7e78cc6 AM |
646 | #calculate time from clientshash (starttime) |
647 | my $starttime = sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime($clientshash{$key}[2])); | |
41964aba | 648 | |
c7e78cc6 AM |
649 | #calculate endtime from clientshash |
650 | my $endtime; | |
651 | if ($clientshash{$key}[3] eq '0'){ | |
652 | $endtime=$Lang::tr{'Captive nolimit'}; | |
41964aba | 653 | } else { |
c7e78cc6 AM |
654 | $endtime = sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime($clientshash{$key}[2]+$clientshash{$key}[3])); |
655 | } | |
e01c5ab7 | 656 | |
41964aba MT |
657 | if ($count++ % 2) { |
658 | $col="bgcolor='$color{'color20'}'"; | |
659 | } else { | |
660 | $col="bgcolor='$color{'color22'}'"; | |
661 | } | |
662 | ||
663 | my $coupon = ($clientshash{$key}[4] eq "LICENSE") ? $Lang::tr{'Captive terms short'} : $clientshash{$key}[4]; | |
e01c5ab7 | 664 | |
41964aba MT |
665 | print <<END; |
666 | <tr> | |
667 | <td $col align="center"><b>$coupon</b></td> | |
668 | <td $col align="center">$starttime</td> | |
669 | <td $col align="center">$endtime</td> | |
670 | <td $col align="center">$clientshash{$key}[0]</td> | |
671 | <td $col align="center">$clientshash{$key}[5]</td> | |
672 | <td $col align="center"> | |
673 | <form method='post'> | |
674 | <input type='image' src='/images/delete.gif' align='middle' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' /> | |
675 | <input type='hidden' name='ACTION' value='delete-client' /> | |
676 | <input type='hidden' name='key' value='$clientshash{$key}[0]' /> | |
677 | </form> | |
678 | </td> | |
679 | </tr> | |
680 | END | |
8b920789 | 681 | } |
c7e78cc6 | 682 | |
8b920789 | 683 | print "</table>"; |
41964aba | 684 | |
8b920789 AM |
685 | &Header::closebox(); |
686 | } | |
687 | ||
e01c5ab7 AM |
688 | sub validremark |
689 | { | |
690 | # Checks a hostname against RFC1035 | |
691 | my $remark = $_[0]; | |
692 | # Each part should be at least two characters in length | |
693 | # but no more than 63 characters | |
694 | if (length ($remark) < 1 || length ($remark) > 255) { | |
695 | return 0;} | |
696 | # Only valid characters are a-z, A-Z, 0-9 and - | |
697 | if ($remark !~ /^[a-zäöüA-ZÖÄÜ0-9-.:;\|_()\/\s]*$/) { | |
698 | return 0;} | |
699 | # First character can only be a letter or a digit | |
700 | if (substr ($remark, 0, 1) !~ /^[a-zäöüA-ZÖÄÜ0-9]*$/) { | |
701 | return 0;} | |
702 | # Last character can only be a letter or a digit | |
703 | if (substr ($remark, -1, 1) !~ /^[a-zöäüA-ZÖÄÜ0-9.:;_)]*$/) { | |
704 | return 0;} | |
705 | return 1; | |
706 | } | |
707 | ||
8b920789 AM |
708 | &Header::closebigbox(); |
709 | &Header::closepage(); |