Handle hostnames with upper-case letters
[webmin.git] / man / view_doc.cgi
1 #!/usr/local/bin/perl
2 # view_doc.cgi
3 # View some package doc file
4
5 require './man-lib.pl';
6 &ReadParse();
7
8 $in{'file'} = &simplify_path($in{'file'});
9 $in{'file'} !~ /[\\\&\;\`\'\"\|\*\?\~\<\>\^\(\)\[\]\{\}\$\n\r]/ ||
10         &error($text{'doc_epath'});
11 foreach $d (split(/\s+/, $config{'doc_dir'})) {
12         $ok++ if (&is_under_directory($d, $in{'file'}));
13         }
14 $ok++ if ($config{'custom_dir'} &&
15           &is_under_directory($config{'custom_dir'}, $in{'file'}));
16 $ok || &error($text{'doc_epath'});
17 if (!-r $in{'file'}) {
18         if (-r "$in{'file'}.gz") {
19                 $in{'file'} = "$in{'file'}.gz";
20                 }
21         else {
22                 &error($text{'doc_epath'});
23                 }
24         }
25
26 # Just output if this is an image
27 $mt = &guess_mime_type($in{'file'});
28 if ($mt =~ /^image\//) {
29         print "Content-type: $mt\r\n\r\n";
30         print &read_file_contents($in{'file'});
31         exit;
32         }
33
34 &ui_print_header(undef, $text{'doc_title'}, "");
35
36 # Work out compression format
37 open(FILE, $in{'file'});
38 read(FILE, $two, 2);
39 $qm = quotemeta($in{'file'});
40 if ($two eq "\037\213") {
41         close(FILE);
42         &open_execute_command(FILE, "gunzip -c $qm", 1, 1);
43         }
44 elsif ($two eq "BZ") {
45         close(FILE);
46         &open_execute_command(FILE, "bunzip2 -c $qm", 1, 1);
47         }
48 seek(FILE, 0, 0);
49
50 $out = "";
51 if ($in{'file'} =~ /\.htm/i) {
52         # Display HTML documentation
53         ($dir = $in{'file'}) =~ s/\/[^\/]+$//;
54         while($line = <FILE>) {
55                 $line =~ s/(href|src)="([^"#][^"]*)"/$1="view_doc.cgi?file=$dir\/$2"/ig;
56                 $line =~ s/(href|src)='([^'#][^']*)'/$1='view_doc.cgi?file=$dir\/$2'/ig;
57                 $line =~ s/(href|src)=([^'"\s#][^'"\s>]*)/$1='view_doc.cgi?file=$dir\/$2'/ig;
58                 $out .= $line;
59                 }
60         }
61 else {
62         # Display text file
63         $out .= "<pre>";
64         @for = split(/\s+/, $in{'for'});
65         while($line = <FILE>) {
66                 $line =~ s/.\010//g;
67                 $line = &html_escape($line);
68                 foreach $f (@for) {
69                         $line =~ s/($f)/<b>$1<\/b>/ig;
70                         }
71                 $out .= $line;
72                 }
73         $out .= "</pre>";
74         }
75 close(FILE);
76 &show_view_table(&text('doc_header', $in{'file'}), $out);
77
78 &ui_print_footer("", $text{'index_return'});
79