Also support [[link|wiki links with alternate link text]].
[mirror/userdir-ldap-cgi.git] / machines.cgi
index 8fe4bfa..4cfe3a8 100755 (executable)
@@ -4,6 +4,7 @@
 # (c) 1999 Randolph Chung. Licensed under the GPL. <tausq@debian.org>
 # (c) 2004 Martin Schulze. Licensed under the GPL. <joey@debian.org>
 # (c) 2006 Ryan Murray. Licensed under the GPL. <rmurray@debian.org>
+# (c) 2008 Martin Zobel-Helas. Licensed under the GPL. <zobel@debian.org>
 
 use lib '.';
 use strict vars;
@@ -82,13 +83,38 @@ sub sshfingerprint {
     return $hrfpr;
 }
 
+sub wiki_link($) {
+       my ($in) = @_;
+       $in =~ s#\[\[(.*?)\|(.*?)\]\]#<a href="http://$1">$2</a>#g;
+       $in =~ s#\[\[(.*?)\]\]#<a href="http://$1">$1</a>#g;
+       return $in;
+}
+
+sub purposes_uplist($) {
+       my ($purposes) = @_;
+       my $out = undef;
+
+       if (scalar @$purposes >= 1) {
+               $out = "<ul>".
+                       join("", map { 
+                               "<li>".wiki_link($_)."</li>\n";
+                         } sort {my $A=$a; my $B=$b; $A =~ s/[\[\]]//g; $B =~ s/[\[\]]//g; $A cmp $B} @{$data->{purpose}}
+                       ).
+                       "</ul>";
+       }
+       return $out;
+}
+
 $SIG{__DIE__} = \&DieHandler;
 
 my $query = new CGI;
 my $host = lc($query->param('host'));
+my $sortby = lc($query->param('sortby')) || "host";
+my $sortorder = lc($query->param('sortorder')) || "asc";
 
 &Util::HTMLSendHeader;
 $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
+&Util::UpgradeConnection($ldap) unless $config{usessl} eq 'False';
 $mesg;
 $ldap->bind;
 
@@ -146,9 +172,9 @@ foreach $dn (sort {$entries->{$a}->{host}->[0] cmp $entries->{$b}->{host}->[0]}
       }
     }
 
-       #Reformat purposes to be pleasing for the human eye:
-       $output{purpose} = join(",", @{$data->{purpose}});
-    
+    my $purpose = purposes_uplist($data->{purpose});
+    $output{purpose} = $purpose if defined $purpose;
+
     $selected = " selected ";    
   }
   
@@ -180,11 +206,25 @@ if ($output{havehostdata}) {
   $hostdetails = "<h1>Summary</h1>\n";
   $hostdetails .= "<table border=\"1\" width=\"90%\">\n<tr>";
   foreach $key (@summaryorder) {
-    $hostdetails .= "<th>$summaryattrs{$key}</th>";
+    if ($sortby ne $key) {
+      $hostdetails .= "<th><a href=\"machines.cgi?sortby=$key&sortorder=asc\">$summaryattrs{$key}</a></th>";
+    } else {
+      if ($sortorder ne "dsc") {
+        $hostdetails .= "<th><a href=\"machines.cgi?sortby=$key&sortorder=dsc\">$summaryattrs{$key}</a></th>";
+      } else {
+        $hostdetails .= "<th><a href=\"machines.cgi?sortby=$key&sortorder=asc\">$summaryattrs{$key}</a></th>";
+      }
+    }
   }
   $hostdetails .= "</tr>\n";
   
-  foreach $host (sort(keys(%summary))) {
+  my @sorted;
+  if ($sortorder eq "asc") {
+     @sorted = sort {($summary{$a}->{$sortby} cmp $summary{$b}->{$sortby}) || ($summary{$a}->{'host'} cmp $summary{$b}->{'host'})} keys(%summary)
+  } else {
+     @sorted = sort {($summary{$b}->{$sortby} cmp $summary{$a}->{$sortby}) || ($summary{$a}->{'host'} cmp $summary{$b}->{'host'})} keys(%summary)
+  }
+  foreach $host (@sorted) {
     $hostdetails .= "<tr>";
     foreach $key (@summaryorder) {
       $hostdetails .= "<td>$summary{$host}{$key}&nbsp;</td>";