#!/usr/bin/perl

#(C) 2003 http://wiweb.biz   #
##############################

$target="_blank";

$base_url = "http://www.searchdax.com/cgi-bin/directory.pl";

use LWP::Simple;
print "content-type: text/html\n\n";
&ReadParse;

if ($in{'mode'} ne "java") { if ($in{'cat'} eq "/" || $in{'cat'} eq "") { &startheader; } else { &header; } }

$url = $in{'cat'};
$html = get("http://dmoz.org$url");
@html = split(/<\/form>/,$html);

if ($html=~m/<table width\=\"95\%\"/) {
@html = split(/<table width\=\"95\%\"/,$html[1]);
}
else
{
@html = split(/<table border\=0 cellpadding\=0 cellspacing\=0 width\=600>/,$html[1]);
}
$html[0] =~ s/>FAQ</></ig;$html[0] =~ s/Beschreibung/Startseite/ig;$html[0] =~ s/description/Startseite/ig;$html[0] =~ s/desc\.html/\.\.\//ig;

$html[0] =~ s/img src=\"/img src=\"http:\/\/dmoz\.org\//ig;
$html[0] =~ s/a href=\"http/a target=\"$target\" href=\"http/ig;
if ($in{'mode'} eq "java") { $html[0] =~ s/a href=\"\//a href=\"$base_url?cat=\//ig; }
else {  $html[0] =~ s/a href=\"\//a href=\"directory.pl?cat=\//ig; }
if ($in{'mode'} ne "java") { print "$html[0]"; } else { $html[0] =~ s/\n//ig;$html[0] =~ s/\"//ig;$html[0] =~ s/\'//ig; print "document.write('$html[0]');"; } 

if ($in{'cat'} && $in{'cat'} ne "/") {
print "<p><a href=\"javascript:history.back()\"><b>zurück</b></a></font>\n";
}

if ($in{'mode'} ne "java") { if ($in{'cat'} eq "/" || $in{'cat'} eq "") { &startfooter; } else { &footer; } }

exit;


######## SUBROUTINEN:

### Templates
sub header { open (data, "templates/kategorie.htm") or &dienice("kann datei nicht öffnen: templates/kategorie.htm");
flock data, 2;@header=<data>;close(data);
$html="@header";
($html,$trash)=split(/%content%/, $html);
print "$html";
$html=""; 
}
sub startheader { open (data, "templates/index.htm") or &dienice("kann datei nicht öffnen: templates/index.htm");
flock data, 2;@header=<data>;close(data);
$html="@header";
($html,$trash)=split(/%content%/, $html);
print "$html";
$html=""; 
}
sub footer { open (data, "templates/kategorie.htm") or &dienice("kann datei nicht öffnen: templates/kategorie.htm");
flock data, 2;@header=<data>;close(data);
$html="@header";
($trash,$html)=split(/%content%/, $html);
$html=~s/%cat%/$in{'cat'}/ig;
print "$html";
$html=""; 
}
sub startfooter { open (data, "templates/index.htm") or &dienice("kann datei nicht öffnen: templates/index.htm");
flock data, 2;@header=<data>;close(data);
$html="@header";
($trash,$html)=split(/%content%/, $html);
print "$html";
$html=""; 
}
#############


sub ReadParse {
local (*in) = @_ if @_;
local ($i, $key, $val); if ( $ENV{'REQUEST_METHOD'} eq "GET" )
{
$in = $ENV{'QUERY_STRING'};
}
elsif ($ENV{'REQUEST_METHOD'} eq "POST")
{
read(STDIN, $input, $ENV{'CONTENT_LENGTH'});

    # split the input
    @pairs = split(/&/, $input);

    # split the name/value pairs
    foreach $pair (@pairs) {

    ($name, $value) = split(/=/, $pair);

    $name =~ tr/+/ /;
    $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    #$value =~ s/<([^>]|\n)*>//g;

    $in{$name} = $value;
    }

}
else
{
# Added for command line debugging
# Supply name/value form data as a command line argument
# Format: name1=value1\&name2=value2\&...
# (need to escape & for shell)
# Find the first argument that's not a switch (-)

$in = ( grep( !/^-/, @ARGV )) [0];
$in =~ s/\\&/&/g; } @in = split(/&/,$in);

 foreach $i (0 .. $#in) {
# Convert plus's to spaces

$in[$i] =~ s/\+/ /g;
# Split into key and value.
($key, $val) = split(/=/,$in[$i],2);
# splits on the first =.
# Convert %XX from hex numbers to alphanumeric
$key =~ s/%(..)/pack("c",hex($1))/ge;
$val =~ s/%(..)/pack("c",hex($1))/ge;

# Associate key and value. \0 is the multiple separator
$in{$key} .= "\0" if (defined($in{$key}));
$in{$key} .= $val; } return length($in);

}


