derek3000 has asked for the wisdom of the Perl Monks concerning the following question:
The actual paths are weird because they are test files. And I'm running it on win32. Valid files and their short names are stored in the file that $links contains. This ensures that people can't try to node=../../../etc/passwd. The $is_script thing skips printing a header if the file contains java script, since most of that stuff would be in between the head tags.
Thanks,
derek3000
#!//Hebe/I$/perl/bin/perl -w # #index.pl #fetches content of link #and prints it out along with #consistent headers and footers. use strict; #like always use CGI; #duh use CGI::Carp; #now we can actually find error messages my $links = 'h:\perl scripts\links.txt'; my @lines; my $q = new CGI; my $node = $q->param('node'); my $is_script = $q->param('script') || 'no'; #list of valid filenames and handles(index) goes here my %known_files; my %top_level; populate_hash($links); #check to see if input is valid if(exists($known_files{$node})){ open(NODE, $known_files{$node}) or die "Can't open file. Valid input though. $!\n"; while(<NODE>){ push(@lines, $_); } close NODE; } else { die "Don't fool around with the url, punk.\n"; } #do the output if($is_script eq 'yes'){ middle(); bottom(); } else { top(); middle(); bottom(); } #Subs under here #-------------------------------------------------------- #------------------------------------------- #gets hash keys and paths for valid files, #also populates second hash for menu sub populate_hash{ my $linkfile = shift; open(LINKS, $linkfile) or die "Can't get file for fresh links! $!\n"; while(<LINKS>){ (my $KEY, my $VALUE) = split /=/; $known_files{$KEY} = $VALUE; if($KEY =~ /announcements|links|news|info/i){ $top_level{$KEY} = $VALUE; } } close LINKS; } #------------------------------------------- #prints html header along with standard menu, #and soon a graphical header. sub top{ print $q->header('text/html'); print $q->start_html(-title=>$node, -author=>'schmidtd@co.delaware.pa.us', -BGCOLOR=>'white'); menu(); } #------------------------------------------ #this will probably stay the same. #it prints out the page-specific content. sub middle{ foreach(@lines){ print $_; } } #------------------------------------------ #prints out anything we want on the bottom, #such as a redundant navigation scheme, #or a link to the disclaimer, or both, #when we get it. sub bottom{ print $q->end_html; } #------------------------------------------ #prints out menu from hash of files. # sub menu{ print "|"; foreach(sort keys %top_level){ print $q->a( {href=>"h:\\perl scripts\\index.pl?node=$_"}, "$_"); print "|"; } }
|
---|