Handle hostnames with upper-case letters
[webmin.git] / webalizer / index.cgi
1 #!/usr/local/bin/perl
2 # index.cgi
3 # Display available apache or squid logfiles
4
5 require './webalizer-lib.pl';
6 &foreign_require("cron", "cron-lib.pl");
7
8 # Check if webalizer is actually installed
9 if (!&has_command($config{'webalizer'})) {
10         &ui_print_header(undef, $text{'index_title'}, "", undef, 1, 1, 0,
11                 &help_search_link("webalizer", "man", "doc", "google"));
12         print &text('index_ewebalizer', "<tt>$config{'webalizer'}</tt>",
13                   "$gconfig{'webprefix'}/config.cgi?$module_name"),"<p>\n";
14
15         &foreign_require("software", "software-lib.pl");
16         $lnk = &software::missing_install_link(
17                         "webalizer", $text{'index_webalizer'},
18                         "../$module_name/", $text{'index_title'});
19         print $lnk,"<p>\n" if ($lnk);
20
21         &ui_print_footer("/", $text{'index'});
22         exit;
23         }
24
25 # Get the version number
26 $webalizer_version = &get_webalizer_version(\$out);
27 if (!$webalizer_version) {
28         &ui_print_header(undef, $text{'index_title'}, "", undef, 1, 1, 0,
29                 &help_search_link("webalizer", "man", "doc", "google"));
30         print &text('index_egetversion',
31                           "<tt>$config{'webalizer'} -v</tt>",
32                           "<pre>$out</pre>"),"<p>\n";
33         &ui_print_footer("/", $text{'index'});
34         exit;
35         }
36
37 if ($webalizer_version < 2) {
38         &main_header();
39         print &text('index_eversion', "<tt>$config{'webalizer'}</tt>",
40                           "$webalizer_version", "2.0"),"<p>\n";
41         &ui_print_footer("/", $text{'index'});
42         exit;
43         }
44
45 # Check if the config file exists
46 if (!-r $config{'webalizer_conf'}) {
47         &main_header();
48         print &text('index_econf', "<tt>$config{'webalizer_conf'}</tt>",
49                   "$gconfig{'webprefix'}/config.cgi?$module_name"),"<p>\n";
50         &ui_print_footer("/", $text{'index'});
51         exit;
52         }
53
54 # Query apache and squid for their logfiles
55 @logs = &get_all_logs();
56
57 # Remove in-accessible logs, and redirect if only one
58 @logs = grep { &can_edit_log($_->{'file'}) } @logs;
59 if (@logs == 1 && -r $logs[0]->{'file'} &&
60     $access{'noconfig'} && !$access{'add'} && !$access{'global'}) {
61         # User can only edit/view one log file ..
62         local $l = $logs[0];
63         if ($access{'view'}) {
64                 &redirect("view_log.cgi/".&urlize(&urlize($l->{'file'})).
65                           "/index.html");
66                 }
67         else {
68                 &redirect("edit_log.cgi?file=".&urlize($l->{'file'}).
69                           "&type=$l->{'type'}&custom=$l->{'custom'}");
70                 }
71         exit;
72         }
73
74 &main_header();
75 @links = ( );
76 if (@logs) {
77         if (!$access{'view'}) {
78                 print &ui_form_start("mass.cgi", "post");
79                 push(@links, &select_all_link("d"),
80                              &select_invert_link("d"));
81                 }
82         push(@links, "<a href='edit_log.cgi?new=1'>$text{'index_add'}</a>")
83                 if (!$access{'view'} && $access{'add'});
84         print &ui_links_row(\@links);
85         local @tds = ( "width=5" );
86         print &ui_columns_start([ $access{'view'} ? ( ) : ( "" ),
87                                   $text{'index_path'},
88                                   $text{'index_type'},
89                                   $text{'index_size'},
90                                   $text{'index_sched'},
91                                   $text{'index_rep'} ], 100, 0, \@tds);
92         foreach $l (@logs) {
93                 next if ($done{$l->{'file'}}++);
94                 local @files = &all_log_files($l->{'file'});
95                 next if (!@files);
96                 local $lconf = &get_log_config($l->{'file'});
97                 local @cols;
98                 local $short = $l->{'file'};
99                 if (length($short) > 40) {
100                         $short = "...".substr($short, -40);
101                         }
102                 if ($access{'view'}) {
103                         push(@cols, $short);
104                         }
105                 else {
106                         push(@cols, "<a href='edit_log.cgi?file=".
107                                    &urlize($l->{'file'}).
108                                    "&type=$l->{'type'}&custom=$l->{'custom'}'>".
109                                    "$short</a>");
110                         }
111                 push(@cols, &text('index_type'.$l->{'type'}));
112                 local ($size, $latest);
113                 foreach $f (@files) {
114                         local @st = stat($f);
115                         $size += $st[7];
116                         $latest = $st[9] if ($st[9] > $latest);
117                         }
118                 $latest = $latest ? localtime($latest) : "<br>";
119                 push(@cols, $size ? &nice_size($size) : $text{'index_empty'});
120                 push(@cols, $lconf->{'sched'} ?
121                         &text('index_when', &cron::when_text($lconf)) :
122                         $text{'no'});
123                 if ($lconf->{'dir'} && -r "$lconf->{'dir'}/index.html") {
124                         push(@cols, "<a href='view_log.cgi/".
125                                     &urlize(&urlize($l->{'file'})).
126                                     "/index.html'>$text{'index_view'}</a>");
127                         }
128                 else {
129                         push(@cols, "");
130                         }
131                 if ($access{'view'}) {
132                         print &ui_columns_row(\@cols);
133                         }
134                 elsif (!%$lconf) {
135                         print &ui_columns_row([ "<img src=images/empty.gif>",
136                                                 @cols ]);
137                         }
138                 else {
139                         print &ui_checked_columns_row(\@cols, \@tds, "d",
140                                                       $l->{'file'});
141                         }
142                 }
143         print &ui_columns_end();
144         }
145 else {
146         print "<p><b>$text{'index_nologs'}</b><p>\n";
147         push(@links, "<a href='edit_log.cgi?new=1'>$text{'index_add'}</a>")
148                 if (!$access{'view'} && $access{'add'});
149         }
150 print &ui_links_row(\@links);
151 if (@logs && !$access{'view'}) {
152         print &ui_form_end([ [ "enable", $text{'index_enable'} ],
153                              [ "disable", $text{'index_disable'} ] ]);
154         }
155
156 if (!$access{'view'} && $access{'global'}) {
157         print &ui_hr();
158         print "<form action=edit_global.cgi>\n";
159         print "<table width=100%><tr>\n";
160         print "<td><input type=submit value='$text{'index_global'}'></td>\n";
161         print "<td>$text{'index_globaldesc'}</td>\n";
162         print "</tr></table></form>\n";
163         }
164
165 &ui_print_footer("/", $text{'index'});
166
167 sub main_header
168 {
169 local $prog = &get_webalizer_prog();
170 &ui_print_header(undef, $text{'index_title'}, "", undef, 1, 1, 0,
171         &help_search_link($prog, "man", "doc", "google"),
172         undef, undef, &text('index_version_'.$prog, $webalizer_version));
173 }
174