8 require './webminlog-lib.pl';
9 our (%text, %config, %gconfig, $webmin_logfile, %in, $in);
10 &foreign_require("acl", "acl-lib.pl");
12 &error_setup($text{'search_err'});
14 # Use sensible defaults
15 $in{'tall'} = 2 if (!defined($in{'tall'}));
16 $in{'uall'} = 1 if (!defined($in{'uall'}));
17 $in{'mall'} = 1 if (!defined($in{'mall'}));
18 $in{'fall'} = 1 if (!defined($in{'fall'}));
19 $in{'dall'} = 1 if (!defined($in{'dall'}));
20 $in{'wall'} = 1 if (!defined($in{'wall'}));
22 # Parse entered time ranges
24 if ($in{'tall'} == 2) {
26 my @now = localtime(time());
27 $from = timelocal(0, 0, 0, $now[3], $now[4], $now[5]);
28 $to = timelocal(59, 59, 23, $now[3], $now[4], $now[5]);
31 elsif ($in{'tall'} == 3) {
33 my @now = localtime(time()-24*60*60);
34 $from = timelocal(0, 0, 0, $now[3], $now[4], $now[5]);
35 $to = timelocal(59, 59, 23, $now[3], $now[4], $now[5]);
38 elsif ($in{'tall'} == 4) {
40 my @week = localtime(time()-7*24*60*60);
41 $from = timelocal(0, 0, 0, $week[3], $week[4], $week[5]);
45 elsif ($in{'tall'} == 0) {
47 $from = &parse_time('from');
48 $to = &parse_time('to');
49 $to = $to ? $to + 24*60*60 - 1 : time();
57 print "Content-type: text/csv\n\n";
60 &ui_print_header(undef, $text{'search_title'}, "");
63 # Perform initial search in index
66 &build_log_index(\%index);
67 open(LOG, $webmin_logfile);
68 while(my ($id, $idx) = each %index) {
69 my ($pos, $time, $user, $module, $sid) = split(/\s+/, $idx);
71 if (($in{'uall'} == 1 ||
72 $in{'uall'} == 0 && $in{'user'} eq $user ||
73 $in{'uall'} == 3 && $in{'ouser'} eq $user ||
74 $in{'uall'} == 2 && $in{'nuser'} ne $user) &&
75 ($in{'mall'} || $in{'module'} eq $module) &&
76 (!$in{'sid'} || $in{'sid'} eq $sid ||
77 $in{'sid'} eq &acl::hash_session_id($sid)) &&
78 ($in{'tall'} || $from < $time && $to > $time)) {
79 # Passed index check .. now look at actual log entry
82 my $act = &parse_logline($line);
85 next if (!$in{'wall'} && $in{'webmin'} ne $act->{'webmin'});
87 # Check modified files
88 if ($gconfig{'logfiles'} && (!$in{'fall'} || !$in{'dall'})) {
89 # Make sure the specified file was modified
91 foreach my $d (&list_diffs($act)) {
92 my $filematch = $in{'fall'} ||
94 $d->{'object'} eq $in{'file'};
95 my $diffmatch = $in{'dall'} ||
96 $d->{'diff'} =~ /\Q$in{'diff'}\E/i;
97 if ($filematch && $diffmatch) {
104 next if (!&can_user($act->{'user'}));
105 next if (!&can_mod($act->{'module'}));
111 # Build search description
112 my $fromstr = &make_date($from, 1);
113 my $tostr = &make_date($to, 1);
116 %minfo = &get_module_info($in{'module'});
118 my $searchmsg = join(" ",
119 $in{'uall'} == 0 ? &text('search_critu',
120 "<tt>".&html_escape($in{'user'})."</tt>") :
121 $in{'uall'} == 3 ? &text('search_critu',
122 "<tt>".&html_escape($in{'ouser'})."</tt>") :
123 $in{'uall'} == 2 ? &text('search_critnu',
124 "<tt>".&html_escape($in{'nuser'})."</tt>") : "",
125 $in{'mall'} ? '' : &text('search_critm',
126 "<tt>".&html_escape($minfo{'desc'})."</tt>"),
128 $fromstr eq $tostr ? &text('search_critt2', $tostr) :
129 &text('search_critt', $fromstr, $tostr));
133 # Show search results as CSV
135 foreach my $act (sort { $b->{'time'} <=> $a->{'time'} } @match) {
136 my $m = $act->{'module'};
137 my $minfo = $m eq "global" ?
138 { 'desc' => $text{'search_global'} } :
141 my %minfo = &get_module_info($m);
142 $minfo = $minfo_cache{$m} = \%minfo;
144 my $desc = &get_action_description($act, $in{'long'});
145 $desc =~ s/<[^>]+>//g;
150 if ($config{'host_search'}) {
151 push(@cols, $act->{'webmin'});
153 push(@cols, &make_date($act->{'time'}));
154 print join(",", map { "\"$_\"" } @cols),"\n";
158 # Show search results in table
160 print "<b>",&text('search_sid', "<tt>$match[0]->{'user'}</tt>",
161 "<tt>$in{'sid'}</tt>")," ..</b><p>\n";
163 elsif ($in{'uall'} == 1 && $in{'mall'} && $in{'tall'}) {
164 print "<b>$text{'search_critall'} ..</b><p>\n";
167 my %minfo = &get_module_info($in{'module'}) if (!$in{'mall'});
168 print "<b>$text{'search_crit'} $searchmsg ...</b><p>\n";
170 print &ui_columns_start(
171 [ $text{'search_action'},
172 $text{'search_module'},
173 $text{'search_user'},
174 $text{'search_host'},
175 $config{'host_search'} ? ( $text{'search_webmin'} ) : ( ),
176 $text{'search_date'},
177 $text{'search_time'} ], "100");
178 foreach my $act (sort { $b->{'time'} <=> $a->{'time'} } @match) {
179 my @tm = localtime($act->{'time'});
180 my $m = $act->{'module'};
182 my $minfo = $m eq "global" ?
183 { 'desc' => $text{'search_global'} } :
186 # first time seeing module ..
187 my %minfo = &get_module_info($m);
188 $minfo = $minfo_cache{$m} = \%minfo;
192 my $desc = &get_action_description($act, $in{'long'});
193 my $anno = &get_annotation($act);
194 push(@cols, "<a href='view.cgi?id=$act->{'id'}".
195 "&return=".&urlize($in{'return'} || "").
196 "&returndesc=".&urlize($in{'returndesc'} || "").
197 "&search=".&urlize($in || "").
200 $cols[$#cols] .= " <img src=images/star.gif>";
202 push(@cols, $minfo->{'desc'}, $act->{'user'}, $act->{'ip'});
203 if ($config{'host_search'}) {
204 push(@cols, $act->{'webmin'});
206 push(@cols, split(/\s+/, &make_date($act->{'time'})));
207 print &ui_columns_row(\@cols);
209 print &ui_columns_end();
210 print "<a href='search.cgi/webminlog.csv?$in&csv=1'>$text{'search_csv'}</a><p>\n";
213 # Tell the user that nothing matches
214 print "<p><b>$text{'search_none2'} $searchmsg.</b><p>\n";
220 &ui_print_footer($in{'return'}, $in{'returndesc'});
223 &ui_print_footer("", $text{'index_return'});
229 my $d = $in{"$_[0]_d"};
230 my $m = $in{"$_[0]_m"};
231 my $y = $in{"$_[0]_y"};
232 return 0 if (!$d && !$y);
234 eval { $rv = timelocal(0, 0, 0, $d, $m, $y-1900) };
235 &error($text{'search_etime'}) if ($@);