Handle hostnames with upper-case letters
[webmin.git] / apache / search_virt.cgi
1 #!/usr/local/bin/perl
2 # search_virt.cgi
3 # Display a list of virtual servers matching some search
4
5 require './apache-lib.pl';
6 &ReadParse();
7
8 # add the default server
9 $conf = &get_config();
10 if (&can_edit_virt()) {
11         push(@vname, $text{'index_defserv'});
12         push(@vlink, "virt_index.cgi");
13         push(@vdesc, $text{'index_defdesc1'});
14         push(@vaddr, $text{'index_any'});
15         push(@vaddr2, '');
16         push(@vport, $text{'index_any'});
17         push(@vport2, '');
18         push(@vserv, &def(scalar(&find_directive("ServerName", $conf)),
19                           $text{'index_auto'}));
20         push(@vserv2, scalar(&find_directive("ServerName", $conf)));
21         push(@vroot, &def(scalar(&find_directive("DocumentRoot", $conf)),
22                           $text{'index_auto'}));
23         push(@vroot2, scalar(&find_directive("DocumentRoot", $conf)));
24         }
25
26 # add other servers
27 @virt = &find_directive_struct("VirtualHost", $conf);
28 if ($httpd_modules{'core'} >= 1.3) {
29         # build list of name-based virtual host IP addresses
30         @nv = &find_directive("NameVirtualHost", $conf);
31         foreach $nv (@nv) {
32                 $nv{&to_ipaddress($nv)}++;
33                 }
34         }
35 elsif ($httpd_modules{'core'} >= 1.2) {
36         # only one name-based virtual host IP address - the default address
37         $ba = &find_directive("ServerName", $conf);
38         $nv{&to_ipaddress($ba ? $ba : &get_system_hostname())}++;
39         }
40 @virt = grep { &can_edit_virt($_) } @virt;
41 foreach $v (@virt) {
42         $vm = $v->{'members'};
43         if ($v->{'value'} =~ /^(\S+):(\S+)$/) {
44                 $addr = $1;
45                 $port = $2;
46                 }
47         else {
48                 $addr = $v->{'value'};
49                 if ($httpd_modules{'core'} < 2.0) {
50                         $port = &def(&find_directive("Port", $conf), 80);
51                         }
52                 else {
53                         $port = "*";
54                         }
55                 }
56         push(@vname, $text{'index_virt'});
57         push(@vlink, "virt_index.cgi?virt=".&indexof($v, @$conf));
58         $sname = scalar(&find_directive("ServerName", $vm));
59         if ($addr ne "_default_" && $addr ne "*" && $nv{&to_ipaddress($addr)}) {
60                 push(@vdesc, &text('index_vname', "<tt>$sname</tt>",
61                                    "<tt>$addr</tt>"));
62                 }
63         elsif (($addr eq "_default_" || $addr eq "*") && $port eq "*") {
64                 push(@vdesc, $text{'index_vdef'});
65                 $vdesc[0] = $text{'index_defdesc2'};
66                 }
67         elsif ($addr eq "_default_" || $addr eq "*") {
68                 push(@vdesc, &text('index_vport', $port));
69                 }
70         elsif ($port eq "*") {
71                 push(@vdesc, &text('index_vaddr', "<tt>$addr</tt>"));
72                 }
73         else {
74                 push(@vdesc, &text('index_vaddrport', "<tt>$addr</tt>", $port));
75                 }
76         push(@vaddr, $addr eq "_default_" || $addr eq "*" ? $text{'index_any'}
77                                                           : $addr);
78         push(@vaddr2, $addr eq "_default_" || $addr eq '*' ? '' : $addr);
79         push(@vport, $port eq "*" ? $text{'index_any'} : $port);
80         push(@vport2, $port eq "*" ? '' : $port);
81         push(@vserv, &def(&find_vdirective("ServerName", $vm, $conf),
82                           $text{'index_auto'}));
83         push(@vserv2, scalar(&find_vdirective("ServerName", $vm, $conf)));
84         push(@vroot, &def(&find_vdirective("DocumentRoot", $vm, $conf),
85                           $text{'index_default'}));
86         push(@vroot2, scalar(&find_vdirective("DocumentRoot", $vm, $conf)));
87         }
88
89 # do the search
90 for($i=0; $i<@vname; $i++) {
91         local $f = $in{'field'} eq 'name' ? $vserv2[$i] :
92                    $in{'field'} eq 'port' ? $vport2[$i] :
93                    $in{'field'} eq 'addr' ? $vaddr2[$i] : $vroot2[$i];
94         if ($in{'match'} == 0 && $f eq $in{'what'} ||
95             $in{'match'} == 1 && eval { $f =~ /\Q$in{'what'}\E/i } ||
96             $in{'match'} == 2 && $f ne $in{'what'} ||
97             $in{'match'} == 3 && eval { $f !~ /\Q$in{'what'}\E/i }) {
98                 push(@match, $i);
99                 }
100         }
101
102 # show the results
103 if (@match == 1 && 0) {
104         &redirect($vlink[$vmatch[0]]);
105         }
106 else {
107         &ui_print_header(undef, $text{'search_title'}, "");
108         if (@match == 0) {
109                 print "<p><b>$text{'search_notfound'}</b>. <p>\n";
110                 }
111         else {
112                 print "<table width=100% border=1>\n";
113                 print "<tr $tb> <td><b>$text{'index_type'}</b></td> ",
114                       "<td><b>$text{'index_addr'}</b></td> ",
115                       "<td><b>$text{'index_port'}</b></td> ",
116                       "<td><b>$text{'index_name'}</b></td> ",
117                       "<td><b>$text{'index_root'}</b></td> </tr>\n";
118                 foreach $i (@match) {
119                         print "<tr $cb>\n";
120                         print "<td><a href='$vlink[$i]'>$vname[$i]</a></td>\n";
121                         print "<td>$vaddr[$i]</td>\n";
122                         print "<td>$vport[$i]</td>\n";
123                         print "<td>$vserv[$i]</td>\n";
124                         print "<td>$vroot[$i]</td>\n";
125                         print "</tr>\n";
126                         }
127                 print "</table><br>\n";
128                 }
129         &ui_print_footer("", $text{'index_return'});
130         }
131