]> git.ipfire.org Git - ipfire-2.x.git/blame - config/cfgroot/header.pl
installer: Fix language selection
[ipfire-2.x.git] / config / cfgroot / header.pl
CommitLineData
3ea75603
MT
1# SmoothWall CGIs
2#
3# This code is distributed under the terms of the GPL
4#
5# (c) The SmoothWall Team
6# Copyright (C) 2002 Alex Hudson - getcgihash() rewrite
7# Copyright (C) 2002 Bob Grant <bob@cache.ucr.edu> - validmac()
8# Copyright (c) 2002/04/13 Steve Bootes - add alias section, helper functions
9# Copyright (c) 2002/08/23 Mark Wormgoor <mark@wormgoor.com> validfqdn()
10# Copyright (c) 2003/09/11 Darren Critchley <darrenc@telus.net> srtarray()
11#
3ea75603
MT
12package Header;
13
14use CGI();
802d7954 15use File::Basename;
8b33e596 16use HTML::Entities();
3ea75603
MT
17use Socket;
18use Time::Local;
19
20$|=1; # line buffering
21
3ea75603
MT
22$Header::revision = 'final';
23$Header::swroot = '/var/ipfire';
0d909a4a 24$Header::graphdir='/srv/web/ipfire/html/graphs';
3ea75603
MT
25$Header::pagecolour = '#ffffff';
26#$Header::tablecolour = '#a0a0a0';
27$Header::tablecolour = '#FFFFFF';
28$Header::bigboxcolour = '#F6F4F4';
29$Header::boxcolour = '#EAE9EE';
30$Header::bordercolour = '#000000';
31$Header::table1colour = '#E0E0E0';
32$Header::table2colour = '#F0F0F0';
33$Header::colourred = '#993333';
34$Header::colourorange = '#FF9933';
35$Header::colouryellow = '#FFFF00';
36$Header::colourgreen = '#339933';
37$Header::colourblue = '#333399';
6d4147d2 38$Header::colourovpn = '#339999';
3ea75603
MT
39$Header::colourfw = '#000000';
40$Header::colourvpn = '#990099';
41$Header::colourerr = '#FF0000';
42$Header::viewsize = 150;
43$Header::errormessage = '';
44my %menuhash = ();
45my $menu = \%menuhash;
46%settings = ();
47%ethsettings = ();
d4af74b7 48%pppsettings = ();
3ea75603 49@URI = ();
3ea75603
MT
50
51### Make sure this is an SSL request
52if ($ENV{'SERVER_ADDR'} && $ENV{'HTTPS'} ne 'on') {
53 print "Status: 302 Moved\r\n";
f8e080ef 54 print "Location: https://$ENV{'SERVER_ADDR'}:444/$ENV{'PATH_INFO'}\r\n\r\n";
3ea75603
MT
55 exit 0;
56}
57
58### Initialize environment
6be4dd24
CS
59&General::readhash("${swroot}/main/settings", \%settings);
60&General::readhash("${swroot}/ethernet/settings", \%ethsettings);
d4af74b7 61&General::readhash("${swroot}/ppp/settings", \%pppsettings);
3ea75603
MT
62$language = $settings{'LANGUAGE'};
63$hostname = $settings{'HOSTNAME'};
64$hostnameintitle = 0;
65
66### Initialize language
67if ($language =~ /^(\w+)$/) {$language = $1;}
68
69### Read English Files
70if ( -d "/var/ipfire/langs/en/" ) {
71 opendir(DIR, "/var/ipfire/langs/en/");
72 @names = readdir(DIR) or die "Cannot Read Directory: $!\n";
73 foreach $name(@names) {
74 next if ($name eq ".");
75 next if ($name eq "..");
76 next if (!($name =~ /\.pl$/));
77 require "${swroot}/langs/en/${name}";
78 };
79};
80
81
82### Enable Language Files
83if ( -d "/var/ipfire/langs/${language}/" ) {
84 opendir(DIR, "/var/ipfire/langs/${language}/");
85 @names = readdir(DIR) or die "Cannot Read Directory: $!\n";
86 foreach $name(@names) {
87 next if ($name eq ".");
88 next if ($name eq "..");
89 next if (!($name =~ /\.pl$/));
90 require "${swroot}/langs/${language}/${name}";
91 };
92};
93
935f311e
MT
94our $THEME_NAME = $settings{'THEME'};
95
3ea75603
MT
96require "${swroot}/langs/en.pl";
97require "${swroot}/langs/${language}.pl";
935f311e 98eval `/bin/cat /srv/web/ipfire/html/themes/$THEME_NAME/include/functions.pl`;
3ea75603
MT
99
100sub orange_used () {
6be4dd24 101 if ($ethsettings{'CONFIG_TYPE'} =~ /^[24]$/) {
3ea75603
MT
102 return 1;
103 }
104 return 0;
105}
106
107sub blue_used () {
6be4dd24 108 if ($ethsettings{'CONFIG_TYPE'} =~ /^[34]$/) {
3ea75603
MT
109 return 1;
110 }
111 return 0;
112}
113
114sub is_modem {
6be4dd24 115 if ($ethsettings{'CONFIG_TYPE'} =~ /^[0]$/) {
3ea75603
MT
116 return 1;
117 }
118 return 0;
119}
120
121### Initialize menu
122sub genmenu {
24249567 123
3ea75603
MT
124 my %subsystemhash = ();
125 my $subsystem = \%subsystemhash;
126
3ea75603
MT
127 my %substatushash = ();
128 my $substatus = \%substatushash;
3ea75603
MT
129
130 my %subnetworkhash = ();
131 my $subnetwork = \%subnetworkhash;
132
3ea75603
MT
133 my %subserviceshash = ();
134 my $subservices = \%subserviceshash;
135
3ea75603
MT
136 my %subfirewallhash = ();
137 my $subfirewall = \%subfirewallhash;
138
3ea75603
MT
139 my %subipfirehash = ();
140 my $subipfire = \%subipfirehash;
3ea75603 141
24249567
MT
142 my %sublogshash = ();
143 my $sublogs = \%sublogshash;
3ea75603 144
b2161869
AF
145 if ( -e "/var/ipfire/main/gpl_accepted") {
146
24249567
MT
147 eval `/bin/cat /var/ipfire/menu.d/*.menu`;
148 eval `/bin/cat /var/ipfire/menu.d/*.main`;
3ea75603 149
c980f4d2 150 if (! blue_used()) {
a615cab3 151 $menu->{'05.firewall'}{'subMenu'}->{'60.wireless'}{'enabled'} = 0;
c980f4d2 152 }
6be4dd24 153 if ( $ethsettings{'CONFIG_TYPE'} =~ /^(1|2|3|4)$/ && $ethsettings{'RED_TYPE'} eq 'STATIC' ) {
a68fedca 154 $menu->{'03.network'}{'subMenu'}->{'70.aliases'}{'enabled'} = 1;
3ea75603 155 }
61027579
MT
156
157 if (&General::RedIsWireless()) {
158 $menu->{'01.system'}{'subMenu'}->{'21.wlan'}{'enabled'} = 1;
159 }
c5e3d520 160
d4af74b7 161 if ( $ethsettings{'RED_TYPE'} eq "PPPOE" && $pppsettings{'MONPORT'} ne "" ) {
c5e3d520
MT
162 $menu->{'02.status'}{'subMenu'}->{'74.modem-status'}{'enabled'} = 1;
163 }
b2161869 164 }
3ea75603
MT
165}
166
167sub showhttpheaders
168{
c43ae64f 169 print "Cache-control: private\n";
1d795ec5 170 print "Content-type: text/html; charset=UTF-8\n\n";
3ea75603
MT
171}
172
173sub is_menu_visible($) {
174 my $link = shift;
175 $link =~ s#\?.*$##;
176 return (-e $ENV{'DOCUMENT_ROOT'}."/../$link");
177}
178
179
180sub getlink($) {
181 my $root = shift;
182 if (! $root->{'enabled'}) {
183 return '';
184 }
185 if ($root->{'uri'} !~ /^$/) {
186 my $vars = '';
187 if ($root->{'vars'} !~ /^$/) {
188 $vars = '?'. $root->{'vars'};
189 }
190 if (! is_menu_visible($root->{'uri'})) {
191 return '';
192 }
193 return $root->{'uri'}.$vars;
194 }
195 my $submenus = $root->{'subMenu'};
196 if (! $submenus) {
197 return '';
198 }
199 foreach my $item (sort keys %$submenus) {
200 my $link = getlink($submenus->{$item});
201 if ($link ne '') {
202 return $link;
203 }
204 }
205 return '';
206}
207
208
209sub compare_url($) {
210 my $conf = shift;
211
212 my $uri = $conf->{'uri'};
213 my $vars = $conf->{'vars'};
214 my $novars = $conf->{'novars'};
215
216 if ($uri eq '') {
217 return 0;
218 }
219 if ($uri ne $URI[0]) {
220 return 0;
221 }
222 if ($novars) {
223 if ($URI[1] !~ /^$/) {
224 return 0;
225 }
226 }
227 if (! $vars) {
228 return 1;
229 }
230 return ($URI[1] eq $vars);
231}
232
233
234sub gettitle($) {
235 my $root = shift;
236
237 if (! $root) {
238 return '';
239 }
240 foreach my $item (sort keys %$root) {
241 my $val = $root->{$item};
242 if (compare_url($val)) {
243 $val->{'selected'} = 1;
244 if ($val->{'title'} !~ /^$/) {
245 return $val->{'title'};
246 }
247 return 'EMPTY TITLE';
248 }
249
250 my $title = gettitle($val->{'subMenu'});
251 if ($title ne '') {
252 $val->{'selected'} = 1;
253 return $title;
254 }
255 }
256 return '';
257}
258
3ea75603
MT
259sub getcgihash {
260 my ($hash, $params) = @_;
261 my $cgi = CGI->new ();
262 $hash->{'__CGI__'} = $cgi;
263 return if ($ENV{'REQUEST_METHOD'} ne 'POST');
264 if (!$params->{'wantfile'}) {
265 $CGI::DISABLE_UPLOADS = 1;
266 $CGI::POST_MAX = 512 * 1024;
267 } else {
268 $CGI::POST_MAX = 10 * 1024 * 1024;
269 }
270
271 $cgi->referer() =~ m/^https?\:\/\/([^\/]+)/;
272 my $referer = $1;
273 $cgi->url() =~ m/^https?\:\/\/([^\/]+)/;
274 my $servername = $1;
275 return if ($referer ne $servername);
276
277 ### Modified for getting multi-vars, split by |
278 %temp = $cgi->Vars();
279 foreach my $key (keys %temp) {
280 $hash->{$key} = $temp{$key};
281 $hash->{$key} =~ s/\0/|/g;
282 $hash->{$key} =~ s/^\s*(.*?)\s*$/$1/;
283 }
284
285 if (($params->{'wantfile'})&&($params->{'filevar'})) {
286 $hash->{$params->{'filevar'}} = $cgi->upload
287 ($params->{'filevar'});
288 }
289 return;
290}
291
3ea75603
MT
292
293# Test if IP is within a subnet
294# Call: IpInSubnet (Addr, Subnet, Subnet Mask)
295# Subnet can be an IP of the subnet: 10.0.0.0 or 10.0.0.1
296# Everything in dottted notation
297# Return: TRUE/FALSE
298sub IpInSubnet
299{
300 $ip = unpack('N', inet_aton(shift));
301 $start = unpack('N', inet_aton(shift));
302 $mask = unpack('N', inet_aton(shift));
303 $start &= $mask; # base of subnet...
304 $end = $start + ~$mask;
305 return (($ip >= $start) && ($ip <= $end));
306}
307
8b33e596
MT
308sub escape($) {
309 my $s = shift;
310 return HTML::Entities::encode_entities($s);
311}
312
313sub cleanhtml {
3ea75603
MT
314 my $outstring =$_[0];
315 $outstring =~ tr/,/ / if not defined $_[1] or $_[1] ne 'y';
8b33e596
MT
316
317 return escape($outstring);
3ea75603 318}
986e08d9 319
3ea75603
MT
320sub connectionstatus
321{
986e08d9
MT
322 my %pppsettings = ();
323 my %netsettings = ();
324 my $iface='';
325
326 $pppsettings{'PROFILENAME'} = 'None';
327 &General::readhash("${General::swroot}/ppp/settings", \%pppsettings);
328 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
329
330 my $profileused='';
2fc53b15 331 unless ( $netsettings{'RED_TYPE'} =~ /^(DHCP|STATIC)$/ ) {
986e08d9
MT
332 $profileused="- $pppsettings{'PROFILENAME'}";
333 }
334
986e08d9 335 my ($timestr, $connstate);
986e08d9 336
7f9f8516 337 my $connstate = "<span>$Lang::tr{'idle'} $profileused</span>";
986e08d9 338
2fc53b15
MT
339 if (-e "${General::swroot}/red/active") {
340 $timestr = &General::age("${General::swroot}/red/active");
7f9f8516 341 $connstate = "<span>$Lang::tr{'connected'} - (<span>$timestr</span>) $profileused</span>";
2fc53b15 342 } else {
7f9f8516
CS
343 if ((open(KEEPCONNECTED, "</var/ipfire/red/keepconnected") == false) && ($pppsettings{'RECONNECTION'} eq "persistent")) {
344 $connstate = "<span>$Lang::tr{'connection closed'} $profileused</span>";
345 } elsif (($pppsettings{'RECONNECTION'} eq "dialondemand") && ( -e "${General::swroot}/red/dial-on-demand")) {
346 $connstate = "<span>$Lang::tr{'dod waiting'} $profileused</span>";
986e08d9 347 } else {
7f9f8516 348 $connstate = "<span>$Lang::tr{'connecting'} $profileused</span>" if (system("ps -ef | grep -q '[p]ppd'"));
986e08d9 349 }
986e08d9 350 }
2fc53b15 351
3ea75603
MT
352 return $connstate;
353}
354
3ea75603
MT
355sub CheckSortOrder {
356#Sorting of allocated leases
357 if ($ENV{'QUERY_STRING'} =~ /^IPADDR|^ETHER|^HOSTNAME|^ENDTIME/ ) {
6be4dd24 358 my $newsort=$ENV{'QUERY_STRING'};
cab84d3a 359 &General::readhash("${swroot}/dhcp/settings", \%dhcpsettings);
3ea75603
MT
360 $act=$dhcpsettings{'SORT_LEASELIST'};
361 #Reverse actual ?
362 if ($act =~ $newsort) {
363 if ($act !~ 'Rev') {$Rev='Rev'};
364 $newsort.=$Rev
365 };
366
367 $dhcpsettings{'SORT_LEASELIST'}=$newsort;
cab84d3a 368 &General::writehash("${swroot}/dhcp/settings", \%dhcpsettings);
3ea75603
MT
369 $dhcpsettings{'ACTION'} = 'SORT'; # avoid the next test "First lauch"
370 }
371
372}
373
374sub PrintActualLeases
375{
376 &openbox('100%', 'left', $tr{'current dynamic leases'});
377 print <<END
52ca3c80 378<table width='100%' class='tbl'>
3ea75603 379<tr>
52ca3c80
AM
380<th width='25%' align='center'><a href='$ENV{'SCRIPT_NAME'}?IPADDR'><b>$tr{'ip address'}</b></a></th>
381<th width='25%' align='center'><a href='$ENV{'SCRIPT_NAME'}?ETHER'><b>$tr{'mac address'}</b></a></th>
382<th width='20%' align='center'><a href='$ENV{'SCRIPT_NAME'}?HOSTNAME'><b>$tr{'hostname'}</b></a></th>
383<th width='25%' align='center'><a href='$ENV{'SCRIPT_NAME'}?ENDTIME'><b>$tr{'lease expires'} (local time d/m/y)</b></a></th>
384<th width='5%' align='center'><b>Add to fix leases<b></th>
3ea75603
MT
385</tr>
386END
52ca3c80 387;
3ea75603 388
986e08d9 389 open(LEASES,"/var/state/dhcp/dhcpd.leases") or die "Can't open dhcpd.leases";
3ea75603
MT
390 while ($line = <LEASES>) {
391 next if( $line =~ /^\s*#/ );
392 chomp($line);
393 @temp = split (' ', $line);
394
395 if ($line =~ /^\s*lease/) {
396 $ip = $temp[1];
397 #All field are not necessarily read. Clear everything
398 $endtime = 0;
399 $ether = "";
400 $hostname = "";
401 }
402
403 if ($line =~ /^\s*ends/) {
404 $line =~ /(\d+)\/(\d+)\/(\d+) (\d+):(\d+):(\d+)/;
405 $endtime = timegm($6, $5, $4, $3, $2 - 1, $1 - 1900);
406 }
407
408 if ($line =~ /^\s*hardware ethernet/) {
409 $ether = $temp[2];
410 $ether =~ s/;//g;
411 }
412
413 if ($line =~ /^\s*client-hostname/) {
414 $hostname = "$temp[1] $temp[2] $temp[3]";
415 $hostname =~ s/;//g;
416 $hostname =~ s/\"//g;
417 }
418
419 if ($line eq "}") {
420 @record = ('IPADDR',$ip,'ENDTIME',$endtime,'ETHER',$ether,'HOSTNAME',$hostname);
421 $record = {}; # create a reference to empty hash
422 %{$record} = @record; # populate that hash with @record
423 $entries{$record->{'IPADDR'}} = $record; # add this to a hash of hashes
424 }
425 }
426 close(LEASES);
427
428 my $id = 0;
52ca3c80 429 my $col="";
3ea75603 430 foreach my $key (sort leasesort keys %entries) {
32fdd7d9 431 print "<form method='post' action='/cgi-bin/dhcp.cgi'>\n";
3ea75603
MT
432 my $hostname = &cleanhtml($entries{$key}->{HOSTNAME},"y");
433
434 if ($id % 2) {
52ca3c80
AM
435 print "<tr>";
436 $col="bgcolor='$table1colour'";
3ea75603
MT
437 }
438 else {
52ca3c80
AM
439 print "<tr>";
440 $col="bgcolor='$table2colour'";
3ea75603
MT
441 }
442
443 print <<END
52ca3c80
AM
444<td align='center' $col><input type='hidden' name='FIX_ADDR' value='$entries{$key}->{IPADDR}' />$entries{$key}->{IPADDR}</td>
445<td align='center' $col><input type='hidden' name='FIX_MAC' value='$entries{$key}->{ETHER}' />$entries{$key}->{ETHER}</td>
446<td align='center' $col><input type='hidden' name='FIX_REMARK' value='$hostname' />&nbsp;$hostname</td>
447<td align='center' $col><input type='hidden' name='FIX_ENABLED' value='on' />
3ea75603 448END
52ca3c80 449;
3ea75603
MT
450
451 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $dst) = localtime ($entries{$key}->{ENDTIME});
452 $enddate = sprintf ("%02d/%02d/%d %02d:%02d:%02d",$mday,$mon+1,$year+1900,$hour,$min,$sec);
453
454 if ($entries{$key}->{ENDTIME} < time() ){
455 print "<strike>$enddate</strike>";
456 } else {
457 print "$enddate";
458 }
32fdd7d9 459 print <<END
52ca3c80
AM
460</td><td $col><input type='hidden' name='ACTION' value='$Lang::tr{'add'}2' /><input type='submit' name='SUBMIT' value='$Lang::tr{'add'}' />
461</td></tr></form>
32fdd7d9 462END
52ca3c80 463;
3ea75603
MT
464 $id++;
465 }
466
467 print "</table>";
468 &closebox();
469}
470
471
472# This sub is used during display of actives leases
473sub leasesort {
474 if (rindex ($dhcpsettings{'SORT_LEASELIST'},'Rev') != -1)
475 {
476 $qs=substr ($dhcpsettings{'SORT_LEASELIST'},0,length($dhcpsettings{'SORT_LEASELIST'})-3);
477 if ($qs eq 'IPADDR') {
478 @a = split(/\./,$entries{$a}->{$qs});
479 @b = split(/\./,$entries{$b}->{$qs});
480 ($b[0]<=>$a[0]) ||
481 ($b[1]<=>$a[1]) ||
482 ($b[2]<=>$a[2]) ||
483 ($b[3]<=>$a[3]);
484 }else {
485 $entries{$b}->{$qs} cmp $entries{$a}->{$qs};
486 }
487 }
488 else #not reverse
489 {
490 $qs=$dhcpsettings{'SORT_LEASELIST'};
491 if ($qs eq 'IPADDR') {
492 @a = split(/\./,$entries{$a}->{$qs});
493 @b = split(/\./,$entries{$b}->{$qs});
494 ($a[0]<=>$b[0]) ||
495 ($a[1]<=>$b[1]) ||
496 ($a[2]<=>$b[2]) ||
497 ($a[3]<=>$b[3]);
498 }else {
499 $entries{$a}->{$qs} cmp $entries{$b}->{$qs};
500 }
501 }
502}
1ddefbbd
CS
503
504sub colorize {
505 my $string = $_[0];
506 my @array = split(/\//,$string);
507 my $string2 = $array[0];
508
509 if ( $string eq "*" or $string eq "" ){
510 return $string;
511 } elsif ( $string =~ "ipsec" ){
512 return "<font color='".${Header::colourvpn}."'>".$string."</font>";
513 } elsif ( $string =~ "tun" ){
514 return "<font color='".${Header::colourovpn}."'>".$string."</font>";
515 } elsif ( $string =~ "lo" or $string =~ "127.0.0.0" ){
516 return "<font color='".${Header::colourfw}."'>".$string."</font>";
517 } elsif ( $string =~ $ethsettings{'GREEN_DEV'} or &IpInSubnet($string2,$ethsettings{'GREEN_NETADDRESS'},$ethsettings{'GREEN_NETMASK'}) ){
518 return "<font color='".${Header::colourgreen}."'>".$string."</font>";
519 } elsif ( $string =~ "ppp0" or $string =~ $ethsettings{'RED_DEV'} or $string =~ "0.0.0.0" or $string =~ $ethsettings{'RED_ADDRESS'} ){
520 return "<font color='".${Header::colourred}."'>".$string."</font>";
521 } elsif ( $ethsettings{'CONFIG_TYPE'}>1 and ( $string =~ $ethsettings{'BLUE_DEV'} or &IpInSubnet($string2,$ethsettings{'BLUE_NETADDRESS'},$ethsettings{'BLUE_NETMASK'}) )){
522 return "<font color='".${Header::colourblue}."'>".$string."</font>";
523 } elsif ( $ethsettings{'CONFIG_TYPE'}>2 and ( $string =~ $ethsettings{'ORANGE_DEV'} or &IpInSubnet($string2,$ethsettings{'ORANGE_NETADDRESS'},$ethsettings{'ORANGE_NETMASK'}) )){
524 return "<font color='".${Header::colourorange}."'>".$string."</font>";
525 } else {
526 return $string;
527 }
528}