#!/usr/bin/perl $|=1; @badchars=(' ','%','&','>','<',';','"','\''); $datafile1="bookstore.rdb"; $datafile2="bookstore.rdb"; $formflag="FF_"; $prog = (reverse split(/\//,$0))[0]; $qprog = $prog; $qprog =~ s/.cgi/Q.cgi/g; @month=("Spacer", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); &webhead; &ARGV2passed; &ENV2passed; if(defined($passed{'search'})){ $var{'search'}=$passed{'search'}; delete($passed{'search'}); } if($var{'search'} eq "demo new products"){ $passed{'cwd'}="/new/products"; delete $var{'search'}; } if($passed{'m2'} eq "References"){ local($temp); $temp=$datafile1; $datafile1=$datafile2; $datafile2=$temp; } &readdata; &readdatarefs; $key=(grep($item_enty{$_} eq "directory" && $item_path{$_} eq $passed{'cwd'},keys %item_name))[0]; if(defined($item_name{$key})){ $title=$item_name{$key}; } &head; #$var{'search'}="fire"; if(defined($var{'search'})){ &search; }else{ &print_items; } &tail; ###################################################################### ###################################################################### sub webhead { print "Content-type: text/html\n\n"; } ###################################################################### sub head { open(FILE,"){ $_ =~ s/.*<\/title>/<title>$title - Books for Writers<\/title>/; print;} close(FILE); &mode; if(!defined($var{'search'})){ &HyperDirectory; } } ###################################################################### sub tail { print "<P><BR>\n"; &HR; if(-e "includes/footer.html"){ if($passed{'m1'} eq "fast"){ open(FILE,"<includes/footer.html"); while(<FILE>){print;} close(FILE); }else{ open(FILE,"<includes/footer.html"); while(<FILE>){print;} close(FILE); } }else{ print "</BODY>\n"; print "</HTML>\n"; } } ###################################################################### sub readdata { if(! defined($passed{'cwd'}) || $passed{'cwd'}=~/^$/){ $passed{'cwd'}="/"; } open(DATA,"<$datafile1") || die "Error"; $count=0; while (<DATA>) { s/\r//; s/\n$//; if(/^#/){ # Ignoring a comment }else{ $count++; if($count<3){ # Ignoring the first two uncommented lines }else{ @line=split(/\t/,$_,6); $index=$line[0].$line[2].$line[1]; if($index !~ /^\s*$/){ $item_enty{$index}=$line[0]; $item_path{$index}=$line[1]; $item_name{$index}=$line[2]; $item_comm{$index}=$line[3]; $item_link{$index}=$line[4]; $item_rela{$index}=$line[5]; } } } } close(DATA); } ###################################################################### sub readdatarefs { if(! defined($passed{'cwd'}) || $passed{'cwd'}=~/^$/){ $passed{'cwd'}="/"; } open(DATA,"<$datafile2") || die "Error"; $count=0; while (<DATA>) { s/\n$//; if(/^#/){ # Ignoring a comment }else{ $count++; if($count<3){ # Ignoring the first two uncommented lines }else{ @line=split(/\t/,$_,6); $index=$line[0].$line[2].$line[1]; if($index !~ /^\s*$/){ $refs_enty{$index}=$line[0]; $refs_path{$index}=$line[1]; $refs_name{$index}=$line[2]; $refs_comm{$index}=$line[3]; $refs_link{$index}=$line[4]; $refs_rela{$index}=$line[5]; } } } } close(DATA); } ###################################################################### sub HR { if($passed{'m1'} eq "fast"){ print "<HR SIZE=\"5\" WIDTH=\"84%\"><BR>\n"; }else{ #print "<CENTER><IMG SRC=\"rule.gif\" ALT=\"",'-' x 40,"\"></CENTER><BR>\n"; print "<HR SIZE=\"5\" WIDTH=\"84%\"><BR>\n"; } } ###################################################################### sub ARGV2passed { foreach $arg (@ARGV) { ($n,$v)=split('=',$arg,2); $passed{$n}=$v; } @array=split(/(\+|\%2b)/,$ENV{'QUERY_STRING'}); foreach $arg (@array) { ($n,$v)=split('=',$arg,2); $passed{&decode($n)}=&decode($v); } } ###################################################################### sub passed2ARGS { @ARGS=(); foreach $key (sort keys %passed) { if($passed{$key} !~ /^$/){ $v2atemp = &encode($key).'='.&encode($passed{$key}); @ARGS=($v2atemp,@ARGS); } } $ARGS=join('+',@ARGS); } ###################################################################### sub HyperDirectory { print "<H1>"; if($passed{'cwd'} ne "/"){ $temp1=$passed{'cwd'}; $passed{'cwd'}="/"; &passed2ARGS; print "<font face=arial,helvetica><A HREF=\"/resources.php\">Writers Resources</A>:  <A HREF=\"/resources.php\">Books for Writers</A>: "; $passed{'cwd'}=$temp1; } $dir=""; foreach $subdir (split(/\//,$passed{'cwd'})){ if($subdir ne ""){ $dir = "$dir/$subdir"; $key=(grep($item_enty{$_} eq "directory" && $item_path{$_} eq $dir,keys %item_name))[0]; if(defined($item_name{$key})){ local($temp); $temp=$passed{'cwd'}; $passed{'cwd'}=$item_path{$key}; if($hypcount){print " - ";} $hypcount++; if($temp ne $passed{'cwd'}){ &passed2ARGS; print "<A HREF=\"${prog}?${ARGS}\">"; } $passed{'cwd'}=$temp; print "$item_name{$key}</A></font>"; }else{ #print "$subdir"; } } } print "</H1>"; if($hypcount){print "<P>\n";}else{print "\n";} } ###################################################################### sub mode { print ""; $key=(grep($refs_enty{$_} eq "directory" && $refs_path{$_} eq $passed{'cwd'},keys %refs_name))[0]; if($key ne "" || $passed{'cwd'} eq "/"){ if($passed{'m2'} eq "References"){ $passed{'m2'} = "Products"; &passed2ARGS; # print "<A HREF=\"${prog}?${ARGS}\">Related Products</A>"; $passed{'m2'} = "References"; }else{ $passed{'m2'} = "References"; &passed2ARGS; # print "<A HREF=\"${prog}?${ARGS}\">Internet References</A>"; # $passed{'m2'} = "Products"; } # print " | "; } if($passed{'m1'} eq "fast"){ $passed{'m1'}="graphics"; &passed2ARGS; print "<A HREF=\"${prog}?${ARGS}\">Descriptive mode</A>"; $passed{'m1'}="fast"; }else{ $passed{'m1'}="fast"; &passed2ARGS; #print "<A HREF=\"${prog}?${ARGS}\"><I>FAST</I> (Condensed) mode</A>"; $passed{'m1'}="graphics"; } if($passed{'m2'} ne "References"){ # print " | "; if($var{'search'} eq "NewIcon.gif"){ &passed2ARGS; # print "<A HREF=\"resources.php\">Resources:</A> <A HREF=\"resources.php\">Books:</A>"; }else{ $passed{'search'}="NewIcon.gif"; &passed2ARGS; # print "<A HREF=\"${prog}?${ARGS}\">New Products</A>"; delete($passed{'search'}); } } print "\n"; if($var{'search'} ne "NewIcon.gif"){ &passed2ARGS; print "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\" background=\"/images/headers/header_bg2.gif\"> <tr> <td background=\"/images/headers/header_bg2.gif\" width=\"280\"> <a href=\"/resources.php\"><img src=\"/images/headers/header_writersresources.gif\" width=\"184\" height=\"35\" alt=\"Resources\" border=\"0\"></a></td> <td background=\"/images/headers/header_bg2.gif\"> </td> </tr> </table>\n"; print "<FORM METHOD=\"POST\" ACTION=\"${prog}?${ARGS}\">"; print "<INPUT NAME=\"search\" VALUE=\"$var{'search'}\" TYPE=\"TEXT\"><INPUT TYPE=\"SUBMIT\" VALUE=\"Search Books:\">"; print "</FORM><BR>\n"; } print "\n"; } ###################################################################### sub ENV2passed { # Get the input read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); # Split the name-value pairs @pairs = split(/&/, $buffer); foreach $pair (@pairs){ ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $ENV{$formflag.$name} = $value; $var{$name}=$value; } } ###################################################################### sub encode { local(@args)=@_; local($char,$string); foreach $char (split(//,$args[$i])){ #search for $char in @badchars @bad=grep($_ eq $char,@badchars); $bad=$#bad+1; if($bad){ $string="$string".sprintf("%%%lx\n",ord($char)); }else{ $string="$string$char"; } } return $string; } ###################################################################### sub decode { local(@args)=@_; local($char,$string); $string=$args[0]; $string =~ tr/+/ /; $string =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; return $string; } ###################################################################### sub print_items { $key=(grep($item_enty{$_} eq "directory" && $item_path{$_} eq $passed{'cwd'},keys %item_name))[0]; if (-e $item_link{$key} && $passed{'m1'} ne "fast"){ open(HEADER,"<$item_link{$key}"); @contents = <HEADER>; print @contents; close(HEADER); } print "<DL><FONT FACE=arial,helvetica>\n"; foreach $item (sort {$a cmp $b} keys %item_name){ if($item_path{$item} =~ /^$passed{'cwd'}(.*)$/){ $rest=$+;$rest =~ s/^\///; if((($item_enty{$item} eq "directory" || $item_enty{$item} eq "pseudodir") && $rest!~/\// && $rest ne "") || ($item_enty{$item} eq "file" && $rest eq "")){ #print "<DT>[",substr($item_enty{$item},0,1),"]<A HREF=\""; print "<DT><A HREF=\""; if($item_enty{$item} eq "directory" || $item_enty{$item} eq "pseudodir"){ local($t); $t = $passed{'cwd'}; if($item_enty{$item} eq "pseudodir"){ $passed{'cwd'}=$item_name{$item}; $key=(grep($item_enty{$_} eq "directory" && $item_path{$_} eq $item_name{$item},keys %item_name))[0]; $item=$key; }else{ $passed{'cwd'}=$item_path{$item}; } &passed2ARGS; $passed{'cwd'}=$t; print "${prog}?${ARGS}"; }elsif($item_enty{$item} eq "file"){ if($passed{'m1'} eq "fast"){ print "fast"; } print "$item_link{$item}"; } print "\">$item_name{$item}</A>\n"; if($passed{'m1'} ne "fast"){print "<DD>$item_comm{$item}\n";}else{print "<DD>\n";} } } } print "</DL>\n"; } ###################################################################### ###################################################################### ###################################################################### sub search{ if($var{'search'} ne "NewIcon.gif"){ $passed2ARGS; print "<A HREF=\"resources.php\">Resources:</A> <A HREF=\"resources.php\">Books:</A>"; #print "Results of searching for '$var{'search'}':<P>\n"; print "Results of search:<P>\n"; } print "<UL>\n"; $savecwd=$passed{'cwd'}; $prev=""; foreach $item (sort {$item_path{$a} cmp $item_path{$b}} keys %item_path){ #print "[$item]\n"; #print "/usr/bin/grep -il $var{'search'} $item_link{$item}\n"; if(-e "$item_link{$item}"){ open(FILE,"/usr/bin/grep -il \"$var{'search'}\" $item_link{$item} |"); @RESULTS=<FILE>; $result=$RESULTS[0]; close(FILE); } if( $item_name{$item} =~ /$var{'search'}/i || $item_comm{$item} =~ /$var{'search'}/i || $item_link{$item} =~ /$var{'search'}/i || $item_rela{$item} =~ /$var{'search'}/i){ $result = 1; } if($result !~ /^\s*$/ && $item_path{$item} ne "/new/products"){ #print "[Results = $result]<BR>\n"; $result=""; if ($prev eq "" || $prev ne $item_path{$item}){ if($prev ne ""){print "</UL>";} $prev=$item_path{$item}; $passed{'cwd'}=$item_path{$item}; &passed2ARGS; $key=(grep($item_enty{$_} eq "directory" && $item_path{$_} eq $passed{'cwd'},keys %item_name))[0]; print "<LI><B><A HREF=\"${prog}?${ARGS}\">$item_name{$key}</A></B>\n"; print "<UL>\n"; } print "<LI><A HREF=\"$item_link{$item}\">$item_name{$item}</A>"; if($passed{'m1'} ne "fast"){print " - $item_comm{$item}\n";}else{print "\n";} } } $passed{'cwd'}=$savecwd; print "</UL></UL>\n"; }