#!/usr/local/bin/perl # CGI Navigation tree v1.00f by Mario Lang # and Reinfried O. Peter #-------------------------------------- # use CGI qw/:standard/; $server = $ENV{'SERVER_NAME'}; $server =~ s+www.++; $server =~ s+\..*++; if ("+${server}-" eq "+-") { $server = "staff" }; $scriptname = $ENV{'SCRIPT_NAME'}; $image_server = "www.cis.tugraz.at"; $navigationpath = "navigation"; $image_path = "$navigationpath/images"; $userdir = `/root/bin/finddocroot.csh $server`; chop($userdir); # Test: # $userdir = "/www/www/links"; $maxparsedepth = 7; # How many recursion-levels to scan for in config file # This affects speed... But in theory, you can # make your tree as deep as you want. # (Limited by browser window width... :) ) $title = 'Navigation'; # Cosmetic for different browser types if ($ENV{'HTTP_USER_AGENT'} =~ /^Lynx|Emacs/) { $RowHead = "\n"; $RowFoot = "
\n"; $nbsps = '  '; } else { $TableHead = "\n"; $RowHead = "\n"; $TableFoot = "
\n"; $RowFoot = "
\n"; $nbsps = '  '; } # expand contains the list of expanded nodes. if (!param('expand')) { @exp = (1); } else { @exp = split(/x/, param('expand')); } # Generate hash array for quick lookup in the while-loop %expanded = map {$_ => 1} @exp; if (!param('conf')) { $navconf = '/navigation/zid.tree'; } else { $navconf = param('conf'); } $navconf =~ s# #+#g; $ori = $navconf; if ($server eq "staff") { $url = (split /\//, $ori)[1]; $user = `/root/bin/staff.csh $url`; chop($user); $navconf =~ s+$url+$user+; } elsif ($server eq "student") { $url = (split /\//, $ori)[1]; $user = `/root/bin/student.csh $url`; chop($user); $first = substr($user,0,1); $navconf =~ s+$url+$first/$user+; } # open(FILE,"> /tmp/nav.txt"); # print FILE "Server: $server\n"; # print FILE "Dir: $userdir\n"; # print FILE "Nav: $navconf\n"; # print FILE "File $userdir$navconf\n"; # close (FILE); if (!param('target')) { $target = '_blank'; } else { $target = param('target'); } if (!param('face')) { $face = 'Verdana,Arial,Helvetica'; } else { $face = param('face'); } if (!param('bgcolor')) { $bgcolor = '666666'; } else { $bgcolor = param('bgcolor'); } if (param('background')) { $bg = param('background'); $background = "background=$bg"; $bgmode = "&$background"; } else { $background = ""; $bgmode = ""; } if (param('noicon')) { $icmode = '&noicon=1'; } if (param('onemain')) { $ssmode = '&onemain=1'; } if (param('find')) { $fmode = '&find=1'; } if (param('nohead')) { $hmode = '&nohead=1'; } if (param('eall')) { $eall = 1; } if (!param('color')) { $color = 'white'; } else { $color = param('color'); } $image_path = "http://$image_server/$image_path/$color"; if ($color eq "blue") { $textcolor = "#003366"; } else { $textcolor = $color; } $last = parseconfig(); # LastNode Calculation my $vertline = (); $tree[$last]{lastnode} = 1; # The last node is really a last node for ($i=$tree[1]{next};defined($tree[$i]{next});$i=$tree[$i]{next}) { my $depth = $tree[$i]{depth}; for ($k=1;$k<$depth;$k++) { $tree[$i]->{vline}->[$k] = $vertline[$k]; } my $j = $tree[$i]{next}; my $ndepth = $tree[$j]{depth}; $vertline[$depth] = '|'; while ($tree[$j]{depth} > $depth) { $j = $tree[$j]{next}; $ndepth = $tree[$j]{depth}; } if ($ndepth < $depth) { $tree[$i]{lastnode} = 1; } if ($tree[$i]{lastnode}) { $vertline[$depth] = ' ';} } for ($k=1;$k<$tree[$last]{depth};$k++) { $tree[$last]->{vline}->[$k] = $vertline[$k]; } # Print out the html header print < $title EOF if (!param('nohead')) { print < Collapse Expand EOF if (param('find')) { print ' Find '; } print "\"\n"; print "

\n"; } print $TableHead; # Generate the tree for ($i=1;defined($tree[$i]{text});$i=$tree[$i]{next}) { if ($tree[$i]{text} ne "") { print "\n"; print $RowHead; print "\n"; if ($tree[$i]{depth}>=2) { for ($j=1;$j<$tree[$i]{depth};$j++) { if ($tree[$i]->{vline}->[$j] eq "|") { print "\"$nbsps|$nbsps\""; } elsif ($tree[$i]->{vline}->[$j] eq " ") { print "\"$nbsps $nbsps\""; } } } if ($tree[$i]{folder}==1 && $tree[$i]{depth}>=1) { if ($expanded{$i}) { if ($tree[$i]{lastnode} == 1) { print ""; print "$nbsps-$nbsps"; } else { if ($i > 1) { print ""; } print "$nbsps-$nbsps"; } } else { if ($i > 1) { print ""; } print "$nbsps+$nbsps"; } } if ($tree[$i]{folder} == 1) { if (!(param('noicon'))) { if ($expanded{$i}) { print "\"->\""; } else { print " "; } } else { print "\""; } if ($i > 1) { print ""; } } else { # no folder print "\"<Doc>\""; if (!(param('noicon'))) { print "\" \""; } elsif ($tree[$i]{link} =~ /^mailto:/) { print "mail.gif\" border=0 hspace=0 vspace=0 align=\"absmiddle\" alt=\" \">"; } elsif ($tree[$i]{link} =~ /^news:/) { print "news.gif\" border=0 hspace=0 vspace=0 align=\"absmiddle\" alt=\" \">"; } elsif ($tree[$i]{link} =~ /rss.xml$/) { print "rss.gif\" border=0 hspace=0 vspace=0 align=\"absmiddle\" alt=\" \">"; } elsif ($tree[$i]{link} =~ /.pdf$/) { print "pdf.gif\" border=0 hspace=0 vspace=0 align=\"absmiddle\" alt=\" \">"; } elsif ($tree[$i]{link} =~ /.doc$/) { print "word.gif\" border=0 hspace=0 vspace=0 align=\"absmiddle\" alt=\" \">"; } elsif ($tree[$i]{link} =~ /.xls$/) { print "excel.gif\" border=0 hspace=0 vspace=0 align=\"absmiddle\" alt=\" \">"; } elsif ($tree[$i]{link} =~ /.zip$/) { print "download.gif\" border=0 hspace=0 vspace=0 align=\"absmiddle\" alt=\" \">"; } elsif ($tree[$i]{link} =~ /.gz$/) { print "download.gif\" border=0 hspace=0 vspace=0 align=\"absmiddle\" alt=\" \">"; } elsif ($tree[$i]{link} =~ /.tar$/) { print "download.gif\" border=0 hspace=0 vspace=0 align=\"absmiddle\" alt=\" \">"; } elsif ($tree[$i]{link} =~ /.exe$/) { print "download.gif\" border=0 hspace=0 vspace=0 align=\"absmiddle\" alt=\" \">"; } else { if ((defined($tree[$i]{target})) && ($tree[$i]{target} eq "")) { print "link.gif\" border=0 hspace=0 vspace=0 align=\"absmiddle\" alt=\" \">"; } else { print "doc.gif\" border=0 width=24 height=22 hspace=0 vspace=0 align=\"absmiddle\" alt=\" \">"; } } } else { print "\""; } } $textline = $tree[$i]{text}; # $textline =~ s/ / /g; # sonst kann man keine Formatierungen eintragen $textline = ''.$textline.''; if ($tree[$i]{link} ne '') { print "".$textline.""; } else { print $textline; } print $RowFoot; } } print $TableFoot; print < EOF sub childof { # Returns true when $_[0] is a successor of $_[1] my $child = shift; my $parent = shift; while ($child >= $parent) { if ($tree[$child]{parent} == $parent) { return 1; } $child = $tree[$child]{parent}; } return 0; } sub expandurl { return join('x', @exp)."x".$_[0]; } sub collapseurl { my $colnode = shift; my ($retstring, $j); for ($j=0;$j<=$#exp;$j++) { if (!(childof($exp[$j],$colnode) or $exp[$j] == $colnode)) { $retstring .= $exp[$j]; if ($j<$#exp) { $retstring .= 'x'; } } } return $retstring; } sub parseconfig { my ($text, $link, $d, $t); # Open the config file open(TREEDEF,"<$userdir$navconf") or die "Error, requested file not found. $!"; $count = 0; my $prev = 1; while () { if (/^$/) { next; # } elsif (/^\s*\w+\s*=\s*\d/) { # if (/^\s*OneMain\s*=\s*\d/) { # $ssmode = '&onemain=1'; # } elsif (/^SiteMap\s*=\s*1/ && !param('expand')) { # $eall = 1; # } } elsif (/foldersTree = gFld\(.*\)/) { $count++; ($text,$link,$d,$t) = $_ =~ /foldersTree = gFld\("(.*?)", "(.*?)"(\)|,\s*"(.*?)"\))/; $tree[$count] = {text => $text, link => $link, folder => 1, parent => 0, depth => 0}; if (defined($t)) { $tree[$count]{target} = $t; } $depth[0] = $count; if (param('ftit')) { $title = $text." ".$title; $title =~ s/<.*?>//g; } } elsif ($_ =~ /aux1\s*=\s*insFld\(foldersTree,\s*gFld\(.*\)\)/) { $count++; ($text,$link,$d,$t) = $_ =~ /aux1\s*=\s*insFld\(foldersTree,\s*gFld\("(.*?)",\s*"(.*?)"(\)|,\s*"(.*?)"\))\)/; $depth[1] = $count; if ($expanded{$depth[0]} || $eall) { $tree[$count] = {text => $text, link => $link, folder => 1, parent => $depth[0], depth => 1}; if (defined($t)) { $tree[$count]{target} = $t; } $tree[$prev]{next} = $count; $tree[$count]{prev} = $prev; $prev = $count; if ($eall) { push @exp, $count; } } } elsif ($_ =~ /insDoc\(.*?,\s*gLnk\(.*?\)/) { $count++; ($deph,$text,$link,$d,$t) = $_ =~ /insDoc\((.*?),\s*gLnk\(\s*"(.*?)",\s*"(.*?)"(\)|,\s*"(.*?)"\))\)/; if ($deph eq 'foldersTree') { $deph=0; } else { $deph = substr($deph,3,1); } if ($expanded{$depth[$deph]}) { $tree[$count] = {text => $text, link => $link, folder => 0, parent => $depth[$deph], depth => $deph+1}; if (defined($t)) { $tree[$count]{target} = $t; } $tree[$prev]{next} = $count; $tree[$count]{prev} = $prev; $prev = $count; } } else { for ($n=2,$p=1;$n<$maxparsedepth;$n++,$p++) { if ($_ =~ /aux$n = insFld\(aux$p,\s*gFld\(.*\)\)/) { $count++; ($text,$link,$d,$t) = $_ =~ /aux$n\s*=\s*insFld\(aux$p,\s*gFld\("(.*?)",\s*"(.*?)"(\)|,\s*"(.*?)"\))\)/; $depth[$n] = $count; if ($expanded{$depth[$p]} || $eall) { $tree[$count] = {text => $text, link => $link, folder => 1, parent => $depth[$p], depth => $n}; if (defined($t)) { $tree[$count]{target} = $t; } $tree[$prev]{next} = $count; $tree[$count]{prev} = $prev; $prev = $count; if ($eall) { push @exp, $count; } } } } } # else { print $_; } if ($eall) { # recalc hash-table %expanded = map {$_ => 1} @exp; } } close(TREEDEF); return $prev; } # This is for Emacs convenience # Local Variables: # mode: cperl # eval: (cperl-set-style "CPerl") # End: