#!/usr/bin/perl
# CGI Navigation tree v1.00g by Mario Lang
# and Reinfried O. Peter
#--------------------------------------
#
use CGI qw/:standard/;
use URI::Escape;
$client = $ENV{'HTTP_CLIENT_IP'};
my @ipad = split(/\./, $client);
my $tripple = "$ipad[0]" . '.' . "$ipad[1]" . '.' . "$ipad[2]";
# ahrefs.com: loest alle Moeglichkeiten auf
if (($tripple eq "54.36.148") ||
($tripple eq "54.36.149") ||
($tripple eq "54.36.150") ||
($tripple eq "195.154.122") ||
($tripple eq "195.154.123") ||
($tripple eq "195.154.126") ||
($tripple eq "195.154.127")) {
print "gesperrt";
exit;
}
$server = $ENV{'SERVER_NAME'};
$server =~ s+www.++;
$server =~ s+\..*++;
if ("+${server}-" eq "+-") { $server = "institute" };
$scriptname = $ENV{'SCRIPT_NAME'};
# $image_server = "institute.tugraz.at";
# $navigationpath = "navigation";
# $image_path = "$navigationpath/images";
$image_server = "";
$image_path = "navicons";
$userdir = `/root/bin/finddocroot.csh $server`;
chop($userdir);
# Test:
# $userdir = "/var/www/institute/cis/homepage";
#
$maxparsedepth = 35; # 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';
if (!param('conf')) {
$navconf = '/navigation/zid.tree';
$navconf = '/index.tree';
} else {
$navconf = param('conf');
$navconf = uri_unescape($navconf);
$navconf =~ s/<([^>]|\n)*>//g;
$navconf =~ s/.*>//g;
$navconf =~ s/<.*//g;
}
$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);
# Novell
# $first = substr($user,0,1);
# $navconf =~ s+$url+$first/$user+;
# Microsoft
$navconf =~ s+$url+$user+;
}
# Cosmetic for different browser types
if ($ENV{'HTTP_USER_AGENT'} =~ /^Lynx|Emacs/) {
$RowHead = "\n";
$RowFoot = "
\n";
$nbsps = ' ';
} else {
$TableHead = "\n";
if ((index($navconf,"/gruppen/") eq 0) && ($navconf ne "/gruppen/gesamtbaum.tree")) {
$Scroll = "\n";
$TableHead = "$Scroll $TableHead";
$RowHead = "\n";
} else {
$RowHead = " \n";
}
$RowFoot = " | \n";
$TableFoot = " |
\n";
$nbsps = ' ';
}
# expand contains the list of expanded nodes.
if (!param('expand')) { @exp = (1); }
else { @exp = grep(/^\d+$/, split(/x/, param('expand'))); }
# Generate hash array for quick lookup in the while-loop
%expanded = map {$_ => 1} @exp;
# 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');
$target = uri_unescape($target);
$target =~ s/<([^>]|\n)*>//g;
$target =~ s/.*>//g;
$target =~ s/<.*//g;
}
if (!param('face')) {
$face = 'Verdana,Arial,Helvetica';
} else {
$face = param('face');
$face = uri_unescape($face);
$face =~ s/<([^>]|\n)*>//g;
$face =~ s/.*>//g;
$face =~ s/<.*//g;
}
if (!param('bgcolor')) {
$bgcolor = 'ffffff';
} else {
$bgcolor = param('bgcolor');
$bgcolor = uri_unescape($bgcolor);
$bgcolor =~ s/<([^>]|\n)*>//g;
$bgcolor =~ s/.*>//g;
$bgcolor =~ s/<.*//g;
}
if (param('background')) {
$bg = param('background');
$bg = uri_unescape($bg);
$bg =~ s/<([^>]|\n)*>//g;
$bg =~ s/.*>//g;
$bg =~ s/<.*//g;
$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 = 'black';
} else {
$color = param('color');
$color = uri_unescape($color);
$color =~ s/<([^>]|\n)*>//g;
$color =~ s/.*>//g;
$color =~ s/<.*//g;
}
# $image_path = "https://$image_server/$image_path/$color";
$image_path = "$image_server/$image_path/$color";
if ($color eq "blue") {
$textcolor = "#003399";
$linkcolor = "textcolor";
} else {
$textcolor = $color;
$linkcolor = "lightgrey";
}
$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
$ind = index($userdir, "/www/users/cis/save");
if (($ind == 0) && (!param('nohead'))) {
print "Content-type: text/html\n\n";
$udir = "${userdir}$navconf";
$udir =~ s/tree/shtml/;
system ("/www/cron/webserver/head.csh $udir");
} else {
print <
$title
EOF
}
if (!param('nohead')) {
print <
EOF
if (param('find')) {
print '';
}
# print "\n";
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 "";
} elsif ($tree[$i]->{vline}->[$j] eq " ") {
print "";
}
}
}
if ($tree[$i]{folder}==1 && $tree[$i]{depth}>=1) {
if ($expanded{$i}) {
if ($tree[$i]{lastnode} == 1) {
print "";
print "";
} else {
if ($i > 1) {
print "";
}
print "";
}
} else {
if ($i > 1) {
print "";
}
print "";
}
}
if ($tree[$i]{folder} == 1) {
if (!(param('noicon'))) {
if ($expanded{$i}) {
print "";
} else {
print "";
}
} else {
print "";
}
if ($i > 1) {
print "";
}
} else { # no folder
print "";
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;
if (($ind == 0) && (!param('nohead'))) {
system ("/www/cron/webserver/foot.csh $userdir/welcome.shtml");
} else {
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 error();
$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 {
# HIER gilt nur bis Tiefe 9!
# $deph = substr($deph,3,1);
$deph =~ s/aux//;
}
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;
}
sub error {
print <
Error
EOF
print "Die Konfigurationsdatei $navconf gibt es im Ordner $userdir nicht.\n";
exit;
}