2 # Common functions for editing the webalizer config file
4 BEGIN { push(@INC, ".."); };
8 $cron_cmd = "$module_config_directory/webalizer.pl";
9 $custom_logs_file = "$module_config_directory/custom-logs";
10 %access = &get_module_acl();
12 # Use sample config if needed
13 if (!-r $config{'webalizer_conf'} && -r $config{'alt_conf'}) {
14 ©_source_dest($config{'alt_conf'}, $config{'webalizer_conf'});
17 # get_config([logfile])
18 # Parse the webalizer config file for a single logfile or global
23 $file = &config_file_name($_[0]);
25 $file = $config{'webalizer_conf'} if (!$file || !-r $file);
32 if (/^\s*(\S+)\s+(.*)/) {
33 push(@rv, { 'name' => $1,
37 'index' => scalar(@rv) });
45 # save_directive(&config, name, [value]*)
48 local ($conf, $name, @values) = @_;
49 local @old = &find($name, $conf);
50 local $lref = &read_file_lines($conf->[0]->{'file'});
52 for($i=0; $i<@old || $i<@values; $i++) {
53 if ($i < @old && $i < @values) {
54 # Just replacing a line
55 $lref->[$old[$i]->{'line'}] = "$name $values[$i]";
59 splice(@$lref, $old[$i]->{'line'}, 1);
60 &renumber($conf, $old[$i]->{'line'}, -1);
62 elsif ($i < @values) {
65 # after the last one of the same type
66 splice(@$lref, $old[$#old]->{'line'}+1, 0,
68 &renumber($conf, $old[$#old]->{'line'}+1, 1);
72 push(@$lref, "$name $values[$i]");
78 # renumber(&config, line, offset)
81 foreach $c (@{$_[0]}) {
82 $c->{'line'} += $_[2] if ($c->{'line'} >= $_[1]);
86 # config_file_name(logfile)
92 return "$module_config_directory/$p.conf";
99 foreach $c (@{$_[1]}) {
100 push(@rv, $c) if (lc($c->{'name'}) eq lc($_[0]));
102 return wantarray ? @rv : $rv[0];
105 # find_value(name, &config)
108 local @rv = map { $_->{'value'} } &find(@_);
109 return wantarray ? @rv : $rv[0];
112 # all_log_files(file)
113 # Given a base log file name, returns a list of all log files in the same
114 # directory that start with the same name
117 $_[0] =~ /^(.*)\/([^\/]+)$/;
122 foreach $f (readdir(DIR)) {
123 if ($f =~ /^\Q$base\E/ && -f "$dir/$f") {
124 push(@rv, "$dir/$f");
131 # get_log_config(path)
132 # Get the configuration for some log file
136 &read_file(&log_config_name($_[0]), \%rv) || return undef;
140 # save_log_config(path, &config)
143 return &write_file(&log_config_name($_[0]), $_[1]);
146 # log_config_name(path)
152 return "$module_config_directory/$p.log";
155 # generate_report(file, handle, escape)
156 # Generates a new webalizer report to the configured directory, and sends
157 # any output to the given file handle. Returns 1 if any of the log files
162 local $lconf = &get_log_config($_[0]);
163 local @all = &all_log_files($_[0]);
165 print $h "Log file $_[0] does not exist\n";
170 local @st = stat($a);
173 local $prog = &get_webalizer_prog();
174 local $type = $lconf->{'type'} == 1 ? "" :
175 $lconf->{'type'} == 2 ? "-F squid" :
176 $lconf->{'type'} == 3 ? "-F ftp" : "";
177 local $cfile = &config_file_name($_[0]);
178 local $conf = -r $cfile ? "-c $cfile" : "";
179 if ($lconf->{'over'} && !&is_readonly_mode()) {
180 unlink("$lconf->{'dir'}/webalizer.current");
181 unlink("$lconf->{'dir'}/webalizer.hist");
183 unlink("$lconfig->{'dir'}/__db.dns_cache.db");
184 local $user = $lconf->{'user'} || "root";
185 if ($user ne "root" && -r $cfile) {
188 if (!-d $lconf->{'dir'}) {
189 mkdir($lconf->{'dir'}, 0755);
190 if ($user ne "root") {
191 local @uinfo = getpwnam($user);
192 chown($uinfo[2], $uinfo[3], $lconf->{'dir'});
196 foreach $a (sort { $mtime{$a} <=> $mtime{$b} } @all) {
197 local $cmd = "$config{'webalizer'} $conf -o ".
198 quotemeta($lconf->{'dir'})." $type -p ".quotemeta($a);
199 if ($user ne "root") {
200 $cmd = &command_as_user($user, 0, $cmd);
202 &open_execute_command(OUT, "$cmd 2>&1", 1);
204 print $h $_[2] ? &html_escape($_) : $_;
208 &additional_log("exec", undef, $cmd);
213 # spaced_buttons(button, ...)
216 local $pc = int(100 / scalar(@_));
217 print "<table width=100%><tr>\n";
219 local $al = $b eq $_[0] ? "align=left" :
220 $b eq $_[@_-1] ? "align=right" : "align=center";
221 print "<td width=$pc% $al>$b</td>\n";
229 open(LOGS, $custom_logs_file);
230 local @rv = map { /^(.*\S)\s+(\S+)/; { 'file' => $1, 'type' => $2 } } <LOGS>;
235 # write_custom_logs(log, ...)
236 sub write_custom_logs
238 &open_tempfile(LOGS, ">$custom_logs_file");
239 &print_tempfile(LOGS, map { "$_->{'file'} $_->{'type'}\n" } @_);
240 &close_tempfile(LOGS);
246 foreach $d (split(/\s+/, $access{'dir'})) {
247 local $ok = &is_under_directory($d, $_[0]);
253 # get_webalizer_version(&out)
254 # Returns the Webalizer version number, and puts output into the given scalar
256 sub get_webalizer_version
258 local $out = `$config{'webalizer'} -v 2>&1 </dev/null`;
259 if ($out =~ /awffull +(\S+)/) {
263 if ($? || $out !~ /\sV(\S+)/) {
265 $out = `$config{'webalizer'} -V 2>&1 </dev/null`;
268 return $out =~ /\sV(\S+)/ ? $1 : undef;
271 # get_webalizer_prog()
272 # Returns either 'webalizer' or 'awffull'
273 sub get_webalizer_prog
275 return $config{'webalizer'} =~ /awffull/i ? "awffull" : "webalizer";
279 # Returns a list of all log files the module can report on
282 # Query apache and squid for their logfiles
283 local %auto = map { $_, 1 } split(/,/, $config{'auto'});
285 if (&foreign_installed("apache") && $auto{'apache'}) {
286 &foreign_require("apache", "apache-lib.pl");
287 local $conf = &apache::get_config();
288 local @dirs = ( &apache::find_all_directives($conf, "CustomLog"),
289 &apache::find_all_directives($conf, "TransferLog") );
290 local $root = &apache::find_directive_struct("ServerRoot", $conf);
293 local $lf = $d->{'words'}->[0];
294 if ($lf =~ /^\|\S+writelogs.pl\s+\S+\s+(\S+)/) {
295 # Virtualmin log writer .. use real file
298 next if ($lf =~ /^\|/);
300 $lf = "$root->{'words'}->[0]/$lf";
303 local $line = <FILE>;
305 if (!$line || $line =~ /^([a-zA-Z0-9\.\-\:]+)\s+\S+\s+\S+\s+\[\d+\/[a-zA-z]+\/\d+:\d+:\d+:\d+\s+[0-9\+\-]+\]/) {
306 push(@logs, { 'file' => $lf,
312 # Add log file from Squid
313 if (&foreign_installed("squid") && $auto{'squid'}) {
314 &foreign_require("squid", "squid-lib.pl");
315 local $conf = &squid::get_config();
316 local $log = &squid::find_value("cache_access_log", $conf);
317 $log = "$squid::config{'log_dir'}/access.log"
318 if (!$log && -d $squid::config{'log_dir'});
319 push(@logs, { 'file' => $log,
320 'type' => 2 }) if ($log);
323 # Add log file from proftpd
324 if (&foreign_installed("proftpd") && $auto{'proftpd'}) {
325 &foreign_require("proftpd", "proftpd-lib.pl");
326 local $conf = &proftpd::get_config();
327 local $global = &proftpd::find_directive_struct("Global", $conf);
328 local $log = &proftpd::find_directive("TransferLog", $global->{'members'}) || "/var/log/xferlog";
329 push(@logs, { 'file' => $log, 'type' => 3 });
332 # Add log file from wu-ftpd
333 if (&foreign_installed("wuftpd") && $auto{'wuftpd'}) {
334 local %wconfig = &foreign_config("wuftpd");
335 push(@logs, { 'file' => $wconfig{'log_file'}, 'type' => 3 });
338 # Add custom logfiles
339 push(@logs, map { $_->{'custom'} = 1; $_ } &read_custom_logs());
344 # lconf_to_cron(&lconf, &job)
345 # Copy fields from a webalizer config to a cron job
348 local ($lconf, $job) = @_;
349 $job->{'special'} = $lconf->{'special'};
350 $job->{'mins'} = $lconf->{'mins'};
351 $job->{'hours'} = $lconf->{'hours'};
352 $job->{'days'} = $lconf->{'days'};
353 $job->{'months'} = $lconf->{'months'};
354 $job->{'weekdays'} = $lconf->{'weekdays'};