#!/usr/bin/perl #requires head.dat, foot.dat, index.dat, title.dat, index.html, # and files listed in index.dat # #parameter: type=ns1 or ns2 # ns1 uses foot.dat. footer of ns2 is "" only. # id= # which data to display. also used as the subscript for index.dat # url= # where to generate a html ######################## # print the cgi header # ######################## print "Content-type: text/html\n\n"; #print "Debug\n"; #print "\n"; #exit 0; ############################# #get parameters from request# ############################# $title = 'title.dat'; $index = 'index.dat'; $header = 'head.dat'; $footer = 'foot.dat'; $pwd = '../cgi-bin'; $name = ""; require "$pwd/cgi_handler.pl"; %rqpaires = &get_request(); $url = $rqpairs{'url'} . '/'; $type = $rqpairs{'type'}; $id = $rqpairs{'id'}; $name = $rqpairs{'name'}; $to = $from = 0; #print "$url $type $id\n"; ##################################################################### #assemble the title of all of the stuff (used in footer of ns1 type)# ##################################################################### $title = $url . $title; open (TITLE, $title) || print "Can't open $title\n"; while () { if (1 == $.) { chop; $title = $_; } if (2 == $.) { chop; $contents = $_; } if (3 == $.) { ($from, $to) = split("/:/"); } } close(TITLE); ############################################################# #obtain filename for print and strings for header and footer# ############################################################# $index = $url . $index; #####count lines of index.dat open(INDEX, $index) || print "Can't open $index\n"; while(<INDEX>) { ; } $lines = $.; close(INDEX); ###### make things random #### (if $id is 'rand') if ($id eq 'rand') { srand; if ($to) { $id = int(rand($to - $from + 1)) + $from; } else { $id = int(rand($lines)) + 1; } #print "$id $url $type\n"; } #####obtain needed information open(INDEX, $index); while (<INDEX>) { if ($. == ($id - 1)) { # $prev is used for button text in the header ($prev) = split(/:/); # $pid is used for <A> in the header $pid = $.; } if (1 == $id) { $prev = $contents; # $id == 1; $pid = 0; } if ($. == $id) { ($dummy, $stitle, $file) = split(/:/); # $stitle is used in the header. # $file is the name of the file to display } if ($. == ($id + 1)) { # $next is used for button text in the footer ($next) = split(/:/); # $nid is used for href in the footer $nid = $id + 1; } if ($lines == $id) { $next = $contents; # $id == $lines $nid = 0; } } #print "$prev $pid/$file $stitle/$next $nid\n"; close(INDEX); #################### # print the header # #################### $header = $url . $header; open(HEADER, $header) || print "Can't open $header\n"; while (<HEADER>) { #replace '$stitle' in head.dat with the value of variable $stitle s/\$stitle/$stitle/; print; } close(HEADER); ################################ # print the target file (body) # ################################ $body = $url . $file; $flag = 0; open(BODY, $body) || print "Can't open $body\n"; while (<BODY>) { if ($type eq "ns1") { s/\$ns1\<([^>]+)\>/$1/g; s/\$ns2\<[^>]+>//g; } if ($type eq "ns2") { s/\$ns2\<([^>]+)\>/$1/g; s/\$ns1\<[^>]+>//g; } #appends <BR> if the end of line is not > unless (/>$/) { s/$/<BR>/; } if ($name) { if (/\<[aA][ \t]+[nN][aA][mM][eE][ \t]*=[ \t]*\"$name\"/) { $flag = 1; } if ($flag) { print; } } else { print; } } close(BODY); #################### # print the footer # #################### if ($type eq 'ns2') { print "</BODY></HTML>\n"; exit 0; } $footer = $url . $footer; chop $url; open (FOOTER, $footer) || print "Can't open $footer\n"; while (<FOOTER>) { #replace '$prevf' in foot.dat with proper url if ($pid) { s/\$prevf/$pwd\/genfile.cgi?url=$url\&type=ns1\&id=$pid/; } else { s/\$prevf/$url\/$title/; } #replace '$prev' in foot.dat with the content of $prev s/\$prev/$prev/; #replace '$prevf' in foot.dat with proper url if ($nid) { s/\$nextf/$pwd\/genfile.cgi?url=$url\&type=ns1\&id=$nid/; } else { s/\$nextf/$url\/$title/; } #replace '$next' in foot.dat with the content of $next s/\$next/$next/; #replace '$prev' in foot.dat with the content of $prev s/\$contents/$url\/$title/; print; } close(FOOTER);