]>
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 AM |
77 | $settings{'TITLE'} = $cgiparams{'TITLE'}; |
78 | $settings{'UNLIMITED'} = $cgiparams{'UNLIMITED'}; | |
f8d35875 | 79 | $settings{'COLOR'} = $cgiparams{'COLOR'}; |
278309b9 | 80 | $settings{'SESSION_TIME'} = $cgiparams{'SESSION_TIME'}; |
6945954c | 81 | |
b32d9e92 AM |
82 | if (!$errormessage){ |
83 | #Check if we need to upload a new logo | |
e2752bfe MT |
84 | if ($file) { |
85 | # Save logo | |
86 | my ($filehandle) = CGI::upload("logo"); | |
87 | ||
88 | # XXX check filesize | |
89 | ||
90 | open(FILE, ">$logo"); | |
b32d9e92 | 91 | binmode $filehandle; |
e2752bfe MT |
92 | while (<$filehandle>) { |
93 | print FILE; | |
b32d9e92 | 94 | } |
e2752bfe | 95 | close(FILE); |
b32d9e92 | 96 | } |
c7e78cc6 | 97 | |
b32d9e92 AM |
98 | &General::writehash("$settingsfile", \%settings); |
99 | ||
9735e167 MT |
100 | # Save terms |
101 | if ($cgiparams{'TERMS'}){ | |
102 | $cgiparams{'TERMS'} = &Header::escape($cgiparams{'TERMS'}); | |
103 | open(FH, ">:utf8", "/var/ipfire/captive/terms.txt") or die("$!"); | |
104 | print FH $cgiparams{'TERMS'}; | |
105 | close(FH); | |
106 | $cgiparams{'TERMS'} = ""; | |
b32d9e92 | 107 | } |
9735e167 | 108 | |
b32d9e92 AM |
109 | #execute binary to reload firewall rules |
110 | system("/usr/local/bin/captivectrl"); | |
45129439 AM |
111 | |
112 | if ($cgiparams{'ENABLE_BLUE'} eq 'on'){ | |
113 | system("/usr/local/bin/wirelessctrl"); | |
114 | } | |
8b920789 | 115 | } |
8b920789 AM |
116 | } |
117 | ||
97b91e8a MT |
118 | if ($cgiparams{'ACTION'} eq "$Lang::tr{'Captive generate coupon'}"){ |
119 | # Generates a new coupon | |
8b920789 | 120 | |
08061703 AM |
121 | #calculate expiredate |
122 | my $expire; | |
123 | if ($settings{'UNLIMITED'} eq 'on'){ | |
124 | $expire = $Lang::tr{'Captive nolimit'}; | |
125 | }else{ | |
126 | $settings{'EXPIRE'} = $cgiparams{'EXP_HOUR'}+$cgiparams{'EXP_DAY'}+$cgiparams{'EXP_WEEK'}+$cgiparams{'EXP_MONTH'}; | |
127 | $expire = sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime(time()+$settings{'EXPIRE'})); | |
128 | } | |
129 | ||
130 | #Check Expiretime | |
131 | if($cgiparams{'EXP_HOUR'}+$cgiparams{'EXP_DAY'}+$cgiparams{'EXP_WEEK'}+$cgiparams{'EXP_MONTH'} == 0 && $cgiparams{'UNLIMITED'} == ''){ | |
132 | $errormessage=$Lang::tr{'Captive noexpiretime'}; | |
133 | } | |
97b91e8a MT |
134 | #check if we already have a coupon with same code |
135 | &General::readhasharray($coupons, \%couponhash) if (-e $coupons); | |
136 | foreach my $key (keys %couponhash) { | |
137 | if($couponhash{$key}[1] eq $cgiparams{'CODE'}){ | |
8b920789 AM |
138 | $errormessage=$Lang::tr{'Captive err doublevoucher'}; |
139 | last; | |
140 | } | |
141 | } | |
142 | ||
e01c5ab7 AM |
143 | #check valid remark |
144 | if ($cgiparams{'REMARK'} ne '' && !&validremark($cgiparams{'REMARK'})){ | |
145 | $errormessage=$Lang::tr{'fwhost err remark'}; | |
146 | } | |
147 | ||
8b920789 AM |
148 | #if no error detected, write to disk |
149 | if (!$errormessage){ | |
150 | my $date=time(); #seconds in utc | |
151 | ||
152 | #first get new key from hash | |
97b91e8a | 153 | my $key=&General::findhasharraykey (\%couponhash); |
8b920789 | 154 | #initialize all fields with '' |
97b91e8a | 155 | foreach my $i (0 .. 3) { $couponhash{$key}[$i] = "";} |
8b920789 | 156 | #define fields |
97b91e8a MT |
157 | $couponhash{$key}[0] = $date; |
158 | $couponhash{$key}[1] = $cgiparams{'CODE'}; | |
159 | $couponhash{$key}[2] = $settings{'EXPIRE'}; | |
160 | $couponhash{$key}[3] = $cgiparams{'REMARK'}; | |
8b920789 | 161 | #write values to disk |
97b91e8a | 162 | &General::writehasharray($coupons, \%couponhash); |
8b920789 AM |
163 | |
164 | #now prepare log entry, get expiring date for voucher and decode remark for logfile | |
97b91e8a MT |
165 | my $expdate=localtime(time()+$couponhash{$key}[3]); |
166 | my $rem=HTML::Entities::decode_entities($couponhash{$key}[4]); | |
8b920789 AM |
167 | |
168 | #write logfile entry | |
97b91e8a | 169 | &General::log("Captive", "Generated new coupon $couponhash{$key}[1] $couponhash{$key}[2] hours valid expires on $expdate remark $rem"); |
8b920789 AM |
170 | } |
171 | } | |
172 | ||
97b91e8a | 173 | if ($cgiparams{'ACTION'} eq 'delete-coupon') { |
8b920789 AM |
174 | #deletes an already generated but unused voucher |
175 | ||
176 | #read all generated vouchers | |
97b91e8a MT |
177 | &General::readhasharray($coupons, \%couponhash) if (-e $coupons); |
178 | foreach my $key (keys %couponhash) { | |
179 | if($cgiparams{'key'} eq $couponhash{$key}[0]){ | |
8b920789 | 180 | #write logenty with decoded remark |
97b91e8a MT |
181 | my $rem=HTML::Entities::decode_entities($couponhash{$key}[4]); |
182 | &General::log("Captive", "Delete unused coupon $couponhash{$key}[1] $couponhash{$key}[2] hours valid expires on $couponhash{$key}[3] remark $rem"); | |
8b920789 | 183 | #delete line from hash |
97b91e8a | 184 | delete $couponhash{$key}; |
8b920789 AM |
185 | last; |
186 | } | |
187 | } | |
188 | #write back hash | |
97b91e8a | 189 | &General::writehasharray($coupons, \%couponhash); |
8b920789 AM |
190 | } |
191 | ||
97b91e8a | 192 | if ($cgiparams{'ACTION'} eq 'delete-client') { |
8b920789 AM |
193 | #delete voucher and connection in use |
194 | ||
195 | #read all active clients | |
97b91e8a | 196 | &General::readhasharray($clients, \%clientshash) if (-e $clients); |
8b920789 AM |
197 | foreach my $key (keys %clientshash) { |
198 | if($cgiparams{'key'} eq $clientshash{$key}[0]){ | |
199 | #prepare log entry with decoded remark | |
200 | my $rem=HTML::Entities::decode_entities($clientshash{$key}[7]); | |
201 | #write logentry | |
97b91e8a | 202 | &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 |
203 | #delete line from hash |
204 | delete $clientshash{$key}; | |
205 | last; | |
206 | } | |
207 | } | |
208 | #write back hash | |
209 | &General::writehasharray("$clients", \%clientshash); | |
210 | #reload firewallrules to kill connection of client | |
211 | system("/usr/local/bin/captivectrl"); | |
212 | } | |
213 | ||
214 | #open webpage, print header and open box | |
215 | &Header::openpage($Lang::tr{'Captive menu'}, 1, ''); | |
216 | &Header::openbigbox(); | |
217 | ||
218 | #call error() to see if we have to print an errormessage on website | |
219 | &error(); | |
220 | ||
221 | #call config() to display the configuration box | |
222 | &config(); | |
223 | ||
9735e167 MT |
224 | sub getterms(){ |
225 | my @ret; | |
226 | ||
227 | open(FILE, "<:utf8", "/var/ipfire/captive/terms.txt"); | |
228 | while(<FILE>) { | |
229 | push(@ret, HTML::Entities::decode_entities($_)); | |
230 | } | |
231 | close(FILE); | |
232 | ||
233 | return join(/\n/, @ret); | |
8b920789 AM |
234 | } |
235 | ||
236 | sub config(){ | |
237 | #prints the config box on the website | |
238 | &Header::openbox('100%', 'left', $Lang::tr{'Captive config'}); | |
239 | print <<END | |
b32d9e92 | 240 | <form method='post' action='$ENV{'SCRIPT_NAME'}' enctype="multipart/form-data">\n |
8b920789 AM |
241 | <table width='100%' border="0"> |
242 | <tr> | |
243 | END | |
244 | ; | |
245 | #check which parameters have to be enabled (from settings file) | |
246 | $checked{'ENABLE_GREEN'}{'off'} = ''; | |
247 | $checked{'ENABLE_GREEN'}{'on'} = ''; | |
248 | $checked{'ENABLE_GREEN'}{$settings{'ENABLE_GREEN'}} = "checked='checked'"; | |
e01c5ab7 | 249 | |
8b920789 AM |
250 | $checked{'ENABLE_BLUE'}{'off'} = ''; |
251 | $checked{'ENABLE_BLUE'}{'on'} = ''; | |
252 | $checked{'ENABLE_BLUE'}{$settings{'ENABLE_BLUE'}} = "checked='checked'"; | |
253 | ||
c7e78cc6 AM |
254 | $checked{'UNLIMITED'}{'off'} = ''; |
255 | $checked{'UNLIMITED'}{'on'} = ''; | |
256 | $checked{'UNLIMITED'}{$settings{'UNLIMITED'}} = "checked='checked'"; | |
257 | ||
8b920789 AM |
258 | if ($netsettings{'GREEN_DEV'}){ |
259 | 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>"; | |
260 | } | |
261 | if ($netsettings{'BLUE_DEV'}){ | |
262 | 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>"; | |
263 | } | |
e01c5ab7 | 264 | |
e01c5ab7 | 265 | print<<END |
297ebdd4 | 266 | </tr> |
8b920789 | 267 | <tr> |
827d3f61 | 268 | <td> |
8b920789 AM |
269 | $Lang::tr{'Captive authentication'} |
270 | </td> | |
827d3f61 | 271 | <td> |
9735e167 | 272 | <select name='AUTH'> |
8b920789 AM |
273 | END |
274 | ; | |
9735e167 MT |
275 | print "<option value='TERMS' "; |
276 | print " selected='selected'" if ($settings{'AUTH'} eq 'TERMS'); | |
277 | print ">$Lang::tr{'Captive terms'}</option>"; | |
e01c5ab7 | 278 | |
97b91e8a MT |
279 | print "<option value='COUPON' "; |
280 | print " selected='selected'" if ($settings{'AUTH'} eq 'COUPON'); | |
281 | print ">$Lang::tr{'Captive coupon'}</option>"; | |
e01c5ab7 | 282 | |
8b920789 AM |
283 | print<<END |
284 | </select> | |
285 | </td> | |
286 | </tr> | |
8b920789 AM |
287 | END |
288 | ; | |
9735e167 | 289 | if ($settings{'AUTH'} eq 'TERMS') { |
278309b9 MT |
290 | $selected{'SESSION_TIME'} = (); |
291 | $selected{'SESSION_TIME'}{'0'} = ""; | |
292 | $selected{'SESSION_TIME'}{'3600'} = ""; | |
293 | $selected{'SESSION_TIME'}{'86400'} = ""; | |
294 | $selected{'SESSION_TIME'}{'604800'} = ""; | |
295 | $selected{'SESSION_TIME'}{'18144000'} = ""; | |
296 | $selected{'SESSION_TIME'}{$settings{'SESSION_TIME'}} = "selected"; | |
297 | ||
9735e167 MT |
298 | my $terms = &getterms(); |
299 | print <<END; | |
300 | <tr> | |
301 | <td></td> | |
302 | <td> | |
303 | <textarea cols="50" rows="10" name="TERMS">$terms</textarea> | |
304 | </td> | |
305 | </tr> | |
278309b9 MT |
306 | |
307 | <tr> | |
308 | <td>$Lang::tr{'Captive client session expiry time'}</td> | |
309 | <td> | |
310 | <select name="SESSION_TIME"> | |
311 | <option value="0" $selected{'SESSION_TIME'}{'0'}>- $Lang::tr{'unlimited'} -</option> | |
312 | <option value="3600" $selected{'SESSION_TIME'}{'3600'}>$Lang::tr{'one hour'}</option> | |
313 | <option value="86400" $selected{'SESSION_TIME'}{'86400'}>$Lang::tr{'24 hours'}</option> | |
314 | <option value="604800" $selected{'SESSION_TIME'}{'604800'}>$Lang::tr{'one week'}</option> | |
315 | <option value="18144000" $selected{'SESSION_TIME'}{'18144000'}>$Lang::tr{'one month'}</option> | |
316 | </select> | |
317 | </td> | |
318 | </tr> | |
9735e167 MT |
319 | END |
320 | } | |
8b920789 | 321 | |
297ebdd4 MT |
322 | print<<END; |
323 | <tr> | |
324 | <td colspan="2"> | |
325 | <br> | |
326 | <strong>$Lang::tr{'Captive branding'}</strong> | |
327 | </td> | |
328 | </tr> | |
329 | <tr> | |
330 | <td> | |
331 | $Lang::tr{'Captive title'} | |
332 | </td> | |
333 | <td> | |
334 | <input type='text' name='TITLE' value="$settings{'TITLE'}" size='40'> | |
335 | </td> | |
336 | </tr> | |
337 | <tr> | |
338 | <td>$Lang::tr{'Captive brand color'}</td> | |
339 | <td> | |
340 | <input type="color" name="COLOR" value="$settings{'COLOR'}"> | |
341 | </td> | |
342 | </tr> | |
343 | END | |
344 | ||
e2752bfe | 345 | # Logo Upload |
827d3f61 MT |
346 | print <<END; |
347 | <tr> | |
348 | <td> | |
349 | $Lang::tr{'Captive logo_upload'} | |
350 | <br> | |
351 | $Lang::tr{'Captive logo_upload1'} | |
352 | </td> | |
353 | <td> | |
e2752bfe | 354 | <input type="file" name="logo"> |
827d3f61 MT |
355 | </td> |
356 | </tr> | |
357 | END | |
358 | ||
e2752bfe MT |
359 | if (-e $logo) { |
360 | print <<END; | |
361 | <tr> | |
362 | <td>$Lang::tr{'Captive logo uploaded'}</td> | |
363 | <td>$Lang::tr{'yes'}</td> | |
364 | </tr> | |
365 | END | |
b32d9e92 | 366 | } |
827d3f61 | 367 | |
297ebdd4 | 368 | print <<END; |
8b920789 | 369 | <tr> |
827d3f61 | 370 | <td></td> |
8b920789 | 371 | <td align='right'> |
827d3f61 | 372 | <input type='submit' name='ACTION' value="$Lang::tr{'save'}"/> |
8b920789 AM |
373 | </td> |
374 | </tr> | |
297ebdd4 | 375 | </table></form> |
8b920789 | 376 | END |
297ebdd4 | 377 | |
8b920789 AM |
378 | &Header::closebox(); |
379 | ||
97b91e8a MT |
380 | #if settings is set to use coupons, the coupon part has to be displayed |
381 | if ($settings{'AUTH'} eq 'COUPON'){ | |
382 | &coupons(); | |
8b920789 AM |
383 | }else{ |
384 | #otherwise we show the licensepart | |
385 | &show_license_connections(); | |
386 | } | |
387 | } | |
388 | ||
8b920789 AM |
389 | sub gencode(){ |
390 | #generate a random code only letters from A-Z except 'O' and 0-9 | |
391 | my @chars = ("A".."N", "P".."Z", "0".."9"); | |
392 | my $randomstring; | |
393 | $randomstring .= $chars[rand @chars] for 1..8; | |
394 | return $randomstring; | |
395 | } | |
396 | ||
97b91e8a MT |
397 | sub coupons() { |
398 | &Header::openbox('100%', 'left', $Lang::tr{'Captive generate coupon'}); | |
08061703 AM |
399 | print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>"; |
400 | print "<table border='0' width='100%'>"; | |
401 | print "<tr><td width='30%'><br>$Lang::tr{'Captive vouchervalid'}</td><td width='70%'><br>"; | |
402 | ||
403 | print "<table class='tbl' border='0' width='100%'>"; | |
404 | print "<th>$Lang::tr{'hours'}</th><th>$Lang::tr{'days'}</th><th>$Lang::tr{'weeks'}</th><th>$Lang::tr{'months'}</th><th></th><th></th>"; | |
405 | ||
406 | #print hour-dropdownbox | |
407 | my $hrs=3600; | |
408 | print "<tr height='40px'><td><select name='EXP_HOUR' style='width:8em;'>"; | |
409 | print "<option value='0' "; | |
410 | print " selected='selected'" if ($settings{'EXP_HOUR'} eq '0'); | |
411 | print ">--</option>"; | |
412 | for (my $i = 1; $i<25; $i++){ | |
413 | my $exp_sec = $i * $hrs; | |
414 | print "<option value='$exp_sec' "; | |
415 | print " selected='selected'" if ($settings{'EXP_HOUR'} eq $exp_sec); | |
416 | print ">$i</option>"; | |
417 | } | |
418 | print "</td><td>"; | |
419 | ||
420 | #print day-dropdownbox | |
421 | my $days=3600*24; | |
422 | print "<select name='EXP_DAY' style='width:8em;'>"; | |
423 | print "<option value='0' "; | |
424 | print " selected='selected'" if ($settings{'EXP_DAY'} eq '0'); | |
425 | print ">--</option>"; | |
426 | for (my $i = 1; $i<8; $i++){ | |
427 | my $exp_sec = $i * $days; | |
428 | print "<option value='$exp_sec' "; | |
429 | print " selected='selected'" if ($settings{'EXP_DAY'} eq $exp_sec); | |
430 | print ">$i</option>"; | |
431 | } | |
432 | print "</td><td>"; | |
433 | ||
434 | #print week-dropdownbox | |
435 | my $week=3600*24*7; | |
436 | print "<select name='EXP_WEEK' style='width:8em;'>"; | |
437 | print "<option value='0' "; | |
438 | print " selected='selected'" if ($settings{'EXP_WEEK'} eq '0'); | |
439 | print ">--</option>"; | |
440 | for (my $i = 1; $i<5; $i++){ | |
441 | my $exp_sec = $i * $week; | |
442 | print "<option value='$exp_sec' "; | |
443 | print " selected='selected'" if ($settings{'EXP_WEEK'} eq $exp_sec); | |
444 | print ">$i</option>"; | |
445 | } | |
446 | print "</td><td>"; | |
447 | ||
448 | #print month-dropdownbox | |
449 | my $month=3600*24*30; | |
450 | print "<select name='EXP_MONTH' style='width:8em;'>"; | |
451 | print "<option value='0' "; | |
452 | print " selected='selected'" if ($settings{'EXP_MONTH'} eq '0'); | |
453 | print ">--</option>"; | |
454 | for (my $i = 1; $i<13; $i++){ | |
455 | my $exp_sec = $i * $month; | |
456 | print "<option value='$exp_sec' "; | |
457 | print " selected='selected'" if ($settings{'EXP_MONTH'} eq $exp_sec); | |
458 | print ">$i</option>"; | |
459 | } | |
460 | print "</td>"; | |
461 | print "<td> <input type='checkbox' name='UNLIMITED' $checked{'UNLIMITED'}{'on'} /></td><td> <b>$Lang::tr{'Captive nolimit'}</b></td>"; | |
462 | print "</tr></table>"; | |
463 | print "</td></tr>"; | |
464 | print "<tr><td><br>$Lang::tr{'remark'}</td><td><br><input type='text' style='width: 98%;' name='REMARK' align='left'></td></tr>"; | |
465 | print "<tr><td> </td><td></td></tr></table><br><br>"; | |
8b920789 | 466 | $cgiparams{'CODE'} = &gencode(); |
97b91e8a | 467 | print "<div align='right'><input type='submit' name='ACTION' value='$Lang::tr{'Captive generate coupon'}'><input type='hidden' name='CODE' value='$cgiparams{'CODE'}'></form></div>"; |
08061703 | 468 | |
8b920789 | 469 | &Header::closebox(); |
97b91e8a MT |
470 | |
471 | # Show all coupons if exist | |
472 | if (! -z $coupons) { | |
473 | &show_coupons(); | |
474 | } | |
475 | ||
476 | if (! -z $clients) { | |
477 | &show_clients(); | |
478 | } | |
8b920789 AM |
479 | } |
480 | ||
481 | sub show_license_connections(){ | |
482 | #if there are active clients, show the box with active connections | |
483 | return if ( -z $clients || ! -f $clients ); | |
484 | my $count=0; | |
485 | my $col; | |
486 | &Header::openbox('100%', 'left', $Lang::tr{'Captive voactive'}); | |
487 | print<<END | |
488 | <center><table class='tbl'> | |
489 | <tr> | |
97b91e8a | 490 | <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 |
491 | END |
492 | ; | |
493 | #read all clients from hash and show table | |
97b91e8a | 494 | &General::readhasharray($clients, \%clientshash) if (-e $clients); |
8b920789 | 495 | foreach my $key (keys %clientshash){ |
c7e78cc6 AM |
496 | my $starttime = sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime($clientshash{$key}[2])); |
497 | my $endtime; | |
498 | if ($clientshash{$key}[3] eq '0'){ | |
499 | $endtime=$Lang::tr{'Captive nolimit'}; | |
500 | }else{ | |
501 | $endtime=sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime($clientshash{$key}[2]+$clientshash{$key}[3])); | |
502 | } | |
503 | ||
8b920789 AM |
504 | if ($count % 2){ |
505 | print" <tr>"; | |
506 | $col="bgcolor='$color{'color20'}'"; | |
507 | }else{ | |
508 | $col="bgcolor='$color{'color22'}'"; | |
509 | print" <tr>"; | |
510 | } | |
c7e78cc6 AM |
511 | print "<td $col><center>$clientshash{$key}[4]</td><td $col><center>$starttime "; |
512 | print "</center></td><td $col><center>$endtime "; | |
97b91e8a | 513 | 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 |
514 | $count++; |
515 | } | |
516 | ||
517 | print "</table>"; | |
518 | &Header::closebox(); | |
519 | } | |
520 | ||
97b91e8a MT |
521 | sub show_coupons() { |
522 | #if there are already generated but unsused coupons, print a table | |
8b920789 AM |
523 | my $count=0; |
524 | my $col; | |
525 | &Header::openbox('100%', 'left', $Lang::tr{'Captive vout'}); | |
526 | print<<END | |
e01c5ab7 | 527 | <center><table class='tbl' border='0'> |
8b920789 | 528 | <tr> |
97b91e8a | 529 | <th align='center' width='15%'>$Lang::tr{'Captive coupon'}</th><th align='center' width='15%'>$Lang::tr{'date'}</th><th th align='center' width='15%'>$Lang::tr{'Captive expire'}</th><th align='center' width='60%'>$Lang::tr{'remark'}</th><th align='center' width='5%'>$Lang::tr{'delete'}</th></tr> |
8b920789 AM |
530 | END |
531 | ; | |
97b91e8a MT |
532 | &General::readhasharray($coupons, \%couponhash) if (-e $coupons); |
533 | foreach my $key (keys %couponhash) | |
8b920789 | 534 | { |
97b91e8a | 535 | my $starttime = sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime($couponhash{$key}[0])); |
c7e78cc6 | 536 | my $endtime; |
97b91e8a | 537 | if ($couponhash{$key}[2] eq '0'){ |
c7e78cc6 AM |
538 | $endtime=$Lang::tr{'Captive nolimit'}; |
539 | }else{ | |
97b91e8a | 540 | $endtime=sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime(time()+$couponhash{$key}[2])); |
c7e78cc6 AM |
541 | } |
542 | ||
8b920789 AM |
543 | if ($count % 2){ |
544 | print" <tr>"; | |
545 | $col="bgcolor='$color{'color20'}'"; | |
546 | }else{ | |
547 | $col="bgcolor='$color{'color22'}'"; | |
548 | print" <tr>"; | |
549 | } | |
c7e78cc6 | 550 | |
97b91e8a | 551 | print "<td $col><center><b>$couponhash{$key}[1]</b></td>"; |
c7e78cc6 | 552 | print "<td $col><center>$starttime</td>"; |
e01c5ab7 | 553 | print "<td $col><center>$endtime</td>"; |
97b91e8a MT |
554 | print "<td $col align='center'>$couponhash{$key}[3]</td>"; |
555 | print "<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-coupon' /><input type='hidden' name='key' value='$couponhash{$key}[0]' /></form></tr>"; | |
8b920789 AM |
556 | $count++; |
557 | } | |
c7e78cc6 | 558 | |
8b920789 AM |
559 | print "</table>"; |
560 | &Header::closebox(); | |
561 | } | |
562 | ||
97b91e8a MT |
563 | sub show_clients() { |
564 | #if there are active clients which use coupons show table | |
8b920789 AM |
565 | return if ( -z $clients || ! -f $clients ); |
566 | my $count=0; | |
567 | my $col; | |
568 | &Header::openbox('100%', 'left', $Lang::tr{'Captive voactive'}); | |
569 | print<<END | |
c7e78cc6 | 570 | <center><table class='tbl' width='100%'> |
8b920789 | 571 | <tr> |
97b91e8a | 572 | <th align='center' width='15%'>$Lang::tr{'Captive coupon'}</th><th th align='center' width='15%'>$Lang::tr{'Captive activated'}</th><th align='center' width='15%'>$Lang::tr{'Captive expire'}</th><th align='center' width='10%'>$Lang::tr{'Captive mac'}</th><th align='center' width='43%'>$Lang::tr{'remark'}</th><th th align='center' width='5%'>$Lang::tr{'delete'}</th></tr> |
8b920789 AM |
573 | END |
574 | ; | |
97b91e8a | 575 | &General::readhasharray($clients, \%clientshash) if (-e $clients); |
8b920789 AM |
576 | foreach my $key (keys %clientshash) |
577 | { | |
c7e78cc6 AM |
578 | #calculate time from clientshash (starttime) |
579 | my $starttime = sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime($clientshash{$key}[2])); | |
580 | #calculate endtime from clientshash | |
581 | my $endtime; | |
582 | if ($clientshash{$key}[3] eq '0'){ | |
583 | $endtime=$Lang::tr{'Captive nolimit'}; | |
584 | }else{ | |
585 | $endtime = sub{sprintf '%02d.%02d.%04d %02d:%02d', $_[3], $_[4]+1, $_[5]+1900, $_[2], $_[1] }->(localtime($clientshash{$key}[2]+$clientshash{$key}[3])); | |
586 | } | |
e01c5ab7 | 587 | |
8b920789 AM |
588 | if ($count % 2){ |
589 | print" <tr>"; | |
590 | $col="bgcolor='$color{'color20'}'"; | |
591 | }else{ | |
592 | $col="bgcolor='$color{'color22'}'"; | |
593 | print" <tr>"; | |
594 | } | |
e01c5ab7 | 595 | |
c7e78cc6 AM |
596 | print "<td $col><center><b>$clientshash{$key}[4]</b></td><td $col><center>$starttime "; |
597 | print "</center></td><td $col><center>$endtime</center></td><td $col><center>$clientshash{$key}[0]</td><td $col><center>$clientshash{$key}[5]</center>"; | |
97b91e8a | 598 | print "</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 |
599 | $count++; |
600 | } | |
c7e78cc6 | 601 | |
8b920789 AM |
602 | print "</table>"; |
603 | &Header::closebox(); | |
604 | } | |
605 | ||
e01c5ab7 AM |
606 | sub validremark |
607 | { | |
608 | # Checks a hostname against RFC1035 | |
609 | my $remark = $_[0]; | |
610 | # Each part should be at least two characters in length | |
611 | # but no more than 63 characters | |
612 | if (length ($remark) < 1 || length ($remark) > 255) { | |
613 | return 0;} | |
614 | # Only valid characters are a-z, A-Z, 0-9 and - | |
615 | if ($remark !~ /^[a-zäöüA-ZÖÄÜ0-9-.:;\|_()\/\s]*$/) { | |
616 | return 0;} | |
617 | # First character can only be a letter or a digit | |
618 | if (substr ($remark, 0, 1) !~ /^[a-zäöüA-ZÖÄÜ0-9]*$/) { | |
619 | return 0;} | |
620 | # Last character can only be a letter or a digit | |
621 | if (substr ($remark, -1, 1) !~ /^[a-zöäüA-ZÖÄÜ0-9.:;_)]*$/) { | |
622 | return 0;} | |
623 | return 1; | |
624 | } | |
625 | ||
8b920789 AM |
626 | sub error{ |
627 | #if an errormessage exits, show a box with errormessage | |
628 | if ($errormessage) { | |
629 | &Header::openbox('100%', 'left', $Lang::tr{'error messages'}); | |
630 | print "<class name='base'>$errormessage\n"; | |
631 | print " </class>\n"; | |
632 | &Header::closebox(); | |
633 | } | |
634 | } | |
635 | ||
636 | &Header::closebigbox(); | |
637 | &Header::closepage(); |