fixed html tags
[mirror/userdir-ldap-cgi.git] / machines.cgi
1 #!/usr/bin/perl
2 # $Id: machines.cgi,v 1.5 2000/01/23 05:36:58 tausq Exp $
3
4 # (c) 1999 Randolph Chung. Licensed under the GPL. <tausq@debian.org>
5
6 use lib '.';
7 use strict vars;
8 #use Apache::Registry;
9 use CGI;
10 use Util;
11 use Net::LDAP qw(:all);
12
13 my (%attrs, @attrorder, %summaryattrs, @summaryorder);
14
15 # This defines the description of the fields, and which fields are retrieved
16 %attrs = ('hostname' => 'Host name',
17           'admin' => 'Admin contact',
18           'architecture' => 'Architecture',
19           'distribution' => 'Distribution',
20           'access' => 'Access',
21           'sponsor' => 'Sponsor',
22           'sponsor-admin' => 'Sponsor admin',
23           'location' => 'Location',
24           'machine' => 'Processor',
25           'memory' => 'Memory',
26           'disk' => 'Disk space',
27           'bandwidth' => 'Bandwidth',
28           'notes' => 'Notes',
29           'createtimestamp' => 'Entry created',
30           'modifytimestamp' => 'Entry modified'
31          );
32
33 # This defines what fields are displayed, and in what order
34 @attrorder = ('hostname', 'admin', 'architecture', 'distribution', 'access',
35               'sponsor', 'sponsor-admin', 'location', 'machine', 'memory',
36               'disk', 'bandwidth', 'notes', 'createtimestamp', 'modifytimestamp');
37
38 # ditto for summary
39 %summaryattrs = ('hostname' => 'Host name',
40                  'host'     => 'just for a link',
41                  'architecture' => 'Architecture',
42                  'access' => 'Access');
43                  
44 @summaryorder = ('hostname', 'architecture', 'access');          
45
46 # Global settings...
47 my %config = &Util::ReadConfigFile;
48
49 my ($ldap, $mesg, $dn, $entries, $data, %output, $key, $hostlist, $hostdetails, $selected, %summary);
50 sub DieHandler {
51   $ldap->unbind if (defined($ldap));
52 }
53
54 $SIG{__DIE__} = \&DieHandler;
55
56 my $query = new CGI;
57 my $host = lc($query->param('host'));
58
59 &Util::HTMLSendHeader;
60 $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
61 $mesg;
62 $ldap->bind;
63
64 $mesg = $ldap->search(base  => $config{hostbasedn}, filter => 'host=*');
65 $mesg->code && &Util::HTMLError($mesg->error);
66 $entries = $mesg->as_struct;
67
68 foreach $dn (sort {$entries->{$a}->{host}->[0] <=> $entries->{$b}->{host}->[0]} keys(%$entries)) {
69   $data = $entries->{$dn};
70
71   my $thishost = $data->{host}->[0];
72   $selected = "";
73   
74   if (lc($thishost) eq $host) {
75     $output{havehostdata} = 1;
76
77     foreach $key (keys(%attrs)) {
78       $output{$key} = $data->{$key}->[0];
79     }
80
81     # Modified/created time. TODO: maybe add is the name of the creator/modifier
82     $output{modifytimestamp} = &Util::FormatTimestamp($output{modifytimestamp});
83     $output{createtimestamp}  = &Util::FormatTimestamp($output{createtimestamp});
84     
85     # Format email addresses
86     $output{admin} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{admin}, $output{admin});
87     $output{'sponsor-admin'} = sprintf("<a href=\"mailto:%s\">%s</a>", $output{'sponsor-admin'}, $output{'sponsor-admin'});
88     
89     # URL
90     my ($sponsor, $url) = undef;
91     $output{sponsor} = undef;
92     foreach $sponsor (@{$data->{sponsor}}) {
93       $sponsor =~ m#((http|ftp)://\S+)#i;
94       $url = $1;
95       $sponsor =~ s/$url//;
96       $output{sponsor} .= "<br>" if ($output{sponsor});
97       if ($url) {
98         $output{sponsor} .= sprintf("<a href=\"%s\">%s</a>", $url, $sponsor);
99       } else {
100         $output{sponsor} .= $sponsor;
101       }
102     }
103     
104     $selected = " selected ";    
105   }
106   
107   $hostlist .= "<option value=\"$thishost\"$selected>$thishost\n";
108   
109   # collect summary info
110   foreach $key (keys(%summaryattrs)) {
111     $summary{$thishost}{$key} = $data->{$key}->[0];
112   }
113   
114   $summary{$thishost}{hostname} = sprintf("<a href=\"machines.cgi?host=%s\">%s</a>",
115                                           $summary{$thishost}{host}, $summary{$thishost}{hostname});
116 }
117 $ldap->unbind;
118
119 if ($output{havehostdata}) {
120   $hostdetails = "<h1>Information about $output{hostname}</h1>\n";
121   $hostdetails .= "<ul>\n";
122   foreach $key (@attrorder) {
123     if ($output{$key}) {
124       $hostdetails .= "<li><b>$attrs{$key}:</b> $output{$key}\n";
125     }
126   }
127   $hostdetails .= "</ul>\n";
128 } else {
129   # display summary info
130   $hostdetails = "<h1>Summary</h1>\n";
131   $hostdetails .= "<table border=1 width=90%>\n<tr>";
132   foreach $key (@summaryorder) {
133     $hostdetails .= "<th>$summaryattrs{$key}</th>";
134   }
135   $hostdetails .= "</tr>\n";
136   
137   foreach $host (sort(keys(%summary))) {
138     $hostdetails .= "<tr>";
139     foreach $key (@summaryorder) {
140       $hostdetails .= "<td>$summary{$host}{$key}&nbsp;</td>";
141     }
142     $hostdetails .= "</tr>\n";
143   }
144   $hostdetails .= "</table>\n";
145 }
146
147 # Finally, we can write the output... yuck...
148 open (F, "<$config{hosthtml}") || &Util::HTMLError("Cannot open host template");
149 while (<F>) {
150   s/~hostlist~/$hostlist/;
151   s/~hostdetails~/$hostdetails/;
152   print;
153 }
154 close F;