Handle hostnames with upper-case letters
[webmin.git] / sarg / view.cgi
1 #!/usr/local/bin/perl
2 # view_log.cgi
3 # Display the report for some log file
4
5 require './sarg-lib.pl';
6 &ReadParse();
7
8 $file = $ENV{'PATH_INFO'} || "/index.html";
9 $file =~ /\.\./ || $file =~ /\<|\>|\||\0/ && &error($text{'view_efile'});
10
11 $conf = &get_config();
12 $odir = &find_value("output_dir", $conf);
13 $odir ||= &find_value("output_dir", $conf, 1);
14 $odir || &error($text{'view_eodir'});
15 $full = "$odir$file";
16 &is_under_directory($odir, $full) || &error($text{'view_efile'});
17
18 # Show index page
19 if (-d $full && -r "$full/index.html") {
20         $full = "$full/index.html";
21         }
22
23 # Display file contents
24 if ($full =~ /\.(html|htm)$/i && !$config{'naked'}) {
25         open(FILE, $full) || &error($text{'view_eopen'}." : $full");
26         while(read(FILE, $buf, 1024)) {
27                 $data .= $buf;
28                 }
29         close(FILE);
30         if ($data =~ /<TITLE>(.*)<\/TITLE>/i) {
31                 $title = $1;
32                 }
33         $data =~ s/^[\000-\377]*<BODY[^>]*>//i;
34         $data =~ s/<\/BODY>[\000-\377]*$//i;
35
36         &ui_print_header(undef, $title || $text{'view_title'}, "");
37         print "<div id=sarg-report>\n";
38         print $data;
39         print "</div>\n";
40         &ui_print_footer("", $text{'index_return'});
41         }
42 elsif (-d $full) {
43         # Show directory listing
44         &ui_print_header(undef, $text{'view_title'}, "");
45         print "<ul>\n";
46         opendir(DIR, $full);
47         foreach $f (sort { lc($a) cmp lc($b) } readdir(DIR)) {
48                 next if ($f eq "." || $f eq "..");
49                 print "<li><a href='$f/'>$f</a>\n";
50                 }
51         closedir(DIR);
52         print "</ul>\n";
53         &ui_print_footer("", $text{'index_return'});
54         }
55 else {
56         # Show RAW file contents
57         open(FILE, $full) || &error($text{'view_eopen'}." : $full");
58         print "Content-type: ",&guess_mime_type($full, "text/plain"),"\n";
59         print "\n";
60         while(read(FILE, $buf, 1024)) {
61                 print $buf;
62                 }
63         close(FILE);
64         }
65