]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/logs.cgi/proxylog.dat
5 # This code is distributed under the terms of the GPL
7 # (c) The SmoothWall Team
12 # enable only the following on debugging purpose
14 #use CGI::Carp 'fatalsToBrowser';
16 require '/var/ipfire/general-functions.pl';
17 require "${General::swroot}/lang.pl";
18 require "${General::swroot}/header.pl";
22 #workaround to suppress a warning when a variable is used only once
23 my @dummy = ( ${Header
::table2colour
} );
33 my $errormessage = '';
36 my %mainsettings = ();
37 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
38 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
40 my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
41 'Sep', 'Oct', 'Nov', 'Dec' );
42 my @longmonths = ( $Lang::tr
{'january'}, $Lang::tr
{'february'}, $Lang::tr
{'march'},
43 $Lang::tr
{'april'}, $Lang::tr
{'may'}, $Lang::tr
{'june'}, $Lang::tr
{'july'}, $Lang::tr
{'august'},
44 $Lang::tr
{'september'}, $Lang::tr
{'october'}, $Lang::tr
{'november'},
45 $Lang::tr
{'december'} );
47 my @now = localtime();
48 my $dow = $now[6]; # day of week
49 my $doy = $now[7]; # day of year (0..364)
51 my $year = $now[5]+1900;
53 $cgiparams{'DAY'} = $now[3];
54 $cgiparams{'MONTH'} = $now[4];
55 $cgiparams{'SOURCE_IP'} = 'ALL';
56 $cgiparams{'FILTER'} = "[.](gif|jpeg|jpg|png|css|js)\$";
57 $cgiparams{'ENABLE_FILTER'} = 'off';
58 $cgiparams{'ACTION'} = '';
60 &Header
::getcgihash
(\
%cgiparams);
61 $logsettings{'LOGVIEW_REVERSE'} = 'off';
62 &General
::readhash
("${General::swroot}/logging/settings", \
%logsettings);
63 ${Header
::viewsize
} = defined ($logsettings{'LOGVIEW_VIEWSIZE'}) ?
$logsettings{'LOGVIEW_VIEWSIZE'} : 150;
65 if ($cgiparams{'ACTION'} eq '')
67 $cgiparams{'ENABLE_FILTER'} = 'on';
70 if ($cgiparams{'ACTION'} eq $Lang::tr
{'restore defaults'})
72 $cgiparams{'FILTER'} = "[.](gif|jpeg|jpg|png|css|js)\$";
73 $cgiparams{'ENABLE_FILTER'} = 'off';
78 $save{'FILTER'} = $cgiparams{'FILTER'};
79 $save{'ENABLE_FILTER'} = $cgiparams{'ENABLE_FILTER'};
80 &General
::writehash
("${General::swroot}/proxy/viewersettings", \
%save);
81 &General
::readhash
("${General::swroot}/proxy/viewersettings", \
%save);
84 my $start = ($logsettings{'LOGVIEW_REVERSE'} eq 'on') ?
0x7FFFF000 : 0; #index of first line number to display
86 if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr
{'update'})
88 my @temp = split(',',$ENV{'QUERY_STRING'});
90 $cgiparams{'MONTH'} = $temp[1];
91 $cgiparams{'DAY'} = $temp[2];
92 $cgiparams{'SOURCE_IP'} = $temp[3];
93 $cgiparams{'USERNAME'} = &Header
::escape
($temp[4]);
96 if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
97 !($cgiparams{'DAY'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)$/))
99 $cgiparams{'DAY'} = $now[3];
100 $cgiparams{'MONTH'} = $now[4];
102 elsif($cgiparams{'ACTION'} eq '>>')
105 my @temp_now = localtime(time);
106 $temp_now[4] = $cgiparams{'MONTH'};
107 $temp_now[3] = $cgiparams{'DAY'};
108 if ($cgiparams{'DAY'}) {
109 @temp_then = localtime(POSIX
::mktime
(@temp_now) + 86400);
110 ## Retrieve the same time on the next day +
111 ## 86400 seconds in a day
112 $cgiparams{'MONTH'} = $temp_then[4];
113 $cgiparams{'DAY'} = $temp_then[3];
116 $temp_now[4] = ($temp_now[4]+1) %12;
117 @temp_then = localtime(POSIX
::mktime
(@temp_now) );
120 $cgiparams{'MONTH'} = $temp_then[4];
121 $cgiparams{'DAY'} = $temp_then[3];
123 elsif($cgiparams{'ACTION'} eq '<<')
126 my @temp_now = localtime(time);
127 $temp_now[4] = $cgiparams{'MONTH'};
128 $temp_now[3] = $cgiparams{'DAY'};
129 if ($cgiparams{'DAY'}) {
130 @temp_then = localtime(POSIX
::mktime
(@temp_now) - 86400);
131 ## Retrieve the same time on the next day -
132 ## 86400 seconds in a day
133 $cgiparams{'MONTH'} = $temp_then[4];
134 $cgiparams{'DAY'} = $temp_then[3];
137 $temp_now[4] = ($temp_now[4]-1) %12;
138 @temp_then = localtime(POSIX
::mktime
(@temp_now) );
141 $cgiparams{'MONTH'} = $temp_then[4];
142 $cgiparams{'DAY'} = $temp_then[3];
145 # Find in which file.gz is the log. Can be calculated because WEEKLY ROTATING of *.log
147 my $daystr = $cgiparams{'DAY'} == 0 ?
'' :$cgiparams{'DAY'} <= 9 ?
"0$cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
152 # Calculate time. If future date, calculate for past year !!!
153 if (( $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
154 ( $cgiparams{'MONTH'} > $now[4] ) ) {
155 $xday = POSIX
::mktime
( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1901 );
156 $daystr = "$longmonths[$cgiparams{'MONTH'}] $daystr, ". int($year-1);
158 $xday = POSIX
::mktime
( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 );
159 $daystr = "$longmonths[$cgiparams{'MONTH'}] $daystr, $year";
162 # calculate end of active week (saturday 23H59)
164 @then = localtime(time());
165 my $sunday = POSIX
::mktime
( 0, 0, 0, @then[3], @then[4], @then[5]);
166 $sunday += (6-$then[6]) * 86400;
168 # Convert delta in second to full weeks
169 $gzindex = int (($sunday-$xday)/604800 );
173 my $filter = $cgiparams{'ENABLE_FILTER'} eq 'on' ?
$cgiparams{'FILTER'} : '';
174 my $sourceip = $cgiparams{'SOURCE_IP'};
175 my $sourceall = $cgiparams{'SOURCE_IP'} eq 'ALL' ?
1 : 0;
176 my $username = $cgiparams{'USERNAME'};
177 my $usersall = $cgiparams{'USERNAME'} eq 'ALL' ?
1 : 0;
181 my $thiscode = '$temp =~ /$filter/;';
185 $errormessage = "$Lang::tr{'bad ignore filter'}.$@<p>";
190 my $lastdatetime; # for debug
192 while ($gzindex >=0 && $loop) {
193 # calculate file name
195 $filestr = "/var/log/squid/access.log";
197 $filestr = "/var/log/squid/access.log.$gzindex";
198 $filestr = "$filestr.gz" if -f
"$filestr.gz";
200 # now read file if existing
201 if (open (FILE
,($filestr =~ /.gz$/ ?
"gzip -dc $filestr |" : $filestr))) {
202 #&General::log("reading $filestr");
203 my @temp_now = localtime(time);
204 $temp_now[4] = $cgiparams{'MONTH'};
205 $temp_now[3] = $cgiparams{'DAY'};
206 if ( ( $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
207 ( $cgiparams{'MONTH'} > $now[4] ) ) {
208 $temp_now[5]--; # past year
211 $temp_now[2] = $temp_now[1] = $temp_now[0] = 0; # start at 00:00:00
212 $temp_now[3] = 1 if ($cgiparams{'DAY'}==0); # All days selected, start at '1'
213 my $mintime = POSIX
::mktime
(@temp_now);
215 if ($cgiparams{'DAY'}==0) { # full month
216 if ($temp_now[4]++ == 12){
220 $maxtime = POSIX
::mktime
(@temp_now);
222 $maxtime = $mintime + 86400; # full day
224 READ
:while (<FILE
>) {
225 my ($datetime,$do,$ip,$ray,$me,$far,$url,$so) = split;
229 #$lastdatetime = $datetime;
231 # collect lines between date && filter
232 if (( ($datetime>$mintime)&&($datetime<$maxtime)) && !($url =~ /$filter/) &&
233 ((($ip eq $sourceip) || $sourceall)) &&
234 ((($so eq $username) || $usersall)))
236 # when standart viewing, just keep in memory the correct slices
237 # it starts a '$start' and size is $viewport
238 # If export, then keep all lines...
239 if ($cgiparams{'ACTION'} eq $Lang::tr
{'export'}){
240 $log[$lines++] = "$datetime $ip $so $url";
242 if ($lines++ < ($start + $Header::viewsize
)) {
243 push(@log,"$datetime $ip $so $url");
244 if (@log > $Header::viewsize
) {
247 #} else { dont do this optimisation, need to count lines !
248 # $datetime = $maxtime; # we have read viewsize lines, stop main loop
249 # last READ; # exit read file
254 # finish loop when date of lines are past maxtime
255 $loop = ($datetime < $maxtime);
259 $gzindex--; # will try next gz file eg 40,39,38,.... because it may have holes when ipfire stopped
264 #$errormessage="$errormessage$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
265 if (0) { # print last date record read
266 my ($SECdt, $MINdt, $HOURdt, $DAYdt, $MONTHdt, $YEARdt) = localtime($lastdatetime);
267 $SECdt = sprintf ("%.02d",$SECdt);
268 $MINdt = sprintf ("%.02d",$MINdt);
269 $HOURdt = sprintf ("%.02d",$HOURdt);
270 $DAYdt = sprintf ("%.02d",$DAYdt);
271 $MONTHdt = sprintf ("%.02d",$MONTHdt+1);
272 $YEARdt = sprintf ("%.04d",$YEARdt+1900);
273 &General
::log ("$HOURdt:$MINdt:$SECdt, $DAYdt/$MONTHdt/$YEARdt--");
277 if ($cgiparams{'ACTION'} eq $Lang::tr
{'export'})
279 print "Content-type: text/plain\n\n";
280 print "IPFire proxy log\r\n";
281 print "$Lang::tr{'date'}: $daystr\r\n";
282 print "Source IP: $cgiparams{'SOURCE_IP'}\r\n";
283 print "Username: $cgiparams{'USERNAME'}\r\n";
284 if ($cgiparams{'ENABLE_FILTER'} eq 'on') {
285 print "Ignore filter: $cgiparams{'FILTER'}\r\n"; }
288 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
291 my ($datetime,$ip,$so,$url) = split;
292 my ($SECdt, $MINdt, $HOURdt, $DAYdt, $MONTHdt, $YEARdt) = localtime($datetime);
293 $SECdt = sprintf ("%.02d",$SECdt);
294 $MINdt = sprintf ("%.02d",$MINdt);
295 $HOURdt = sprintf ("%.02d",$HOURdt);
296 if ($cgiparams{'DAY'}==0) { # full month
297 $DAYdt = sprintf ("%.02d",$DAYdt);
298 print "$DAYdt/$HOURdt:$MINdt:$SECdt $ip $so $url\n";
300 print "$HOURdt:$MINdt:$SECdt $ip $so $url\n";
306 $selected{'SOURCE_IP'}{$cgiparams{'SOURCE_IP'}} = "selected='selected'";
307 $selected{'USERNAME'}{$cgiparams{'USERNAME'}} = "selected='selected'";
309 $checked{'ENABLE_FILTER'}{'off'} = '';
310 $checked{'ENABLE_FILTER'}{'on'} = '';
311 $checked{'ENABLE_FILTER'}{$cgiparams{'ENABLE_FILTER'}} = "checked='checked'";
313 &Header
::showhttpheaders
();
315 &Header
::openpage
($Lang::tr
{'proxy log viewer'}, 1, '');
317 &Header
::openbigbox
('100%', 'left', '', $errormessage);
320 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
321 print "<font class='base'>$errormessage </font>\n";
325 &Header
::openbox
('100%', 'left', "$Lang::tr{'settings'}:");
328 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
331 <td width='10%' class='base'>$Lang::tr{'month'}:</td>
333 <select name='MONTH'>
337 for (my $month = 0; $month < 12; $month++)
340 if ($month == $cgiparams{'MONTH'}) {
341 print "selected='selected' "; }
342 print "value='$month'>$longmonths[$month]</option>\n";
347 <td width='10%' class='base' align='right'>$Lang::tr{'day'}: </td>
352 print "<option value='0'>$Lang::tr{'all'}</option>";
353 for (my $day = 1; $day <= 31; $day++)
356 if ($day == $cgiparams{'DAY'}) {
357 print "selected='selected' "; }
358 print "value='$day'>$day</option>\n";
363 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='<<' /></td>
364 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='>>' /></td>
365 <td width='25%' class='base'>$Lang::tr{'source ip'}:</td>
367 <select name='SOURCE_IP'>
368 <option value='ALL' $selected{'SOURCE_IP'}{'ALL'}>$Lang::tr{'caps all'}</option>
371 foreach my $ip (sort keys %ips) {
372 print "<option value='$ip' $selected{'SOURCE_IP'}{$ip}>$ip</option>\n"; }
378 <td width='60%' colspan='6'> </td>
379 <td width='25%' class='base'>$Lang::tr{'advproxy NCSA username'}:</td>
381 <select name='USERNAME'>
382 <option value='ALL' $selected{'USERNAME'}{'ALL'}>$Lang::tr{'caps all'}</option>
385 foreach my $so (sort keys %users) {
386 $so = &Header
::escape
($so);
387 print "<option value='$so' $selected{'USERNAME'}{$so}>$so</option>\n"; }
393 <td class='base'>$Lang::tr{'ignore filter'}:</td>
394 <td colspan='5'><input type='text' name='FILTER' value='$cgiparams{'FILTER'}' size='40' /></td>
395 <td class='base'>$Lang::tr{'enable ignore filter'}:</td>
396 <td><input type='checkbox' name='ENABLE_FILTER' value='on' $checked{'ENABLE_FILTER'}{'on'} /></td>
402 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'restore defaults'}' /></td>
403 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
404 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'export'}' /></td>
413 &Header
::openbox
('100%', 'left', $Lang::tr
{'log'});
416 $start = $lines - ${Header
::viewsize
} if ($start >= $lines - ${Header
::viewsize
});
417 $start = 0 if ($start < 0);
423 $prev = $start - ${Header
::viewsize
};
424 $prev = 0 if ( $prev < 0);
428 if ($start == $lines - ${Header
::viewsize
}) {
431 $next = $start + ${Header
::viewsize
};
432 $next = $lines - ${Header
::viewsize
} if ($next >= $lines - ${Header
::viewsize
});
435 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
437 print "<p><b>$Lang::tr{'web hits'} $daystr: $lines</b></p>";
438 if ($lines != 0) { &oldernewer
(); }
440 <table width='100%' class='tbl'>
442 <th width='10%' align='center' class='boldbase'><b>$Lang::tr{'time'}</b></th>
443 <th width='15%' align='center' class='boldbase'><b>$Lang::tr{'source ip'}</b></th>
444 <th width='12%' align='center' class='boldbase'><b>$Lang::tr{'advproxy NCSA username'}</b></th>
445 <th width='63%' align='center' class='boldbase'><b>$Lang::tr{'website'}</b></th>
455 $col="bgcolor='$color{'color20'}'"; }
458 $col="bgcolor='$color{'color22'}'"; }
459 my ($datetime,$ip,$so,$url) = split;
460 my ($SECdt, $MINdt, $HOURdt, $DAYdt, $MONTHdt, $YEARdt) = localtime($datetime);
461 $SECdt = sprintf ("%.02d",$SECdt);
462 $MINdt = sprintf ("%.02d",$MINdt);
463 $HOURdt = sprintf ("%.02d",$HOURdt);
465 $url =~ /(^.{0,60})/;
467 unless (length($part) < 60) { $part = "${part}..."; }
468 $url = &Header
::cleanhtml
($url,"y");
469 $part = &Header
::cleanhtml
($part,"y");
470 if ($cgiparams{'DAY'}==0) { # full month
471 $DAYdt = sprintf ("%.02d/",$DAYdt);
476 <td align='center' $col>$DAYdt$HOURdt:$MINdt:$SECdt</td>
477 <td align='center' $col>$ip</td>
478 <td align='center' $col>$so</td>
479 <td align='left' $col><a href='$url' title='$url' target='_new'>$part</a></td>
492 &Header
::closebigbox
();
494 &Header
::closepage
();
505 print "<td align='center' width='50%'>";
507 print "<a href='/cgi-bin/logs.cgi/proxylog.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{'SOURCE_IP'},$cgiparams{'USERNAME'}'>$Lang::tr{'older'}</a>"; }
509 print "$Lang::tr{'older'}"; }
512 print "<td align='center' width='50%'>";
514 print "<a href='/cgi-bin/logs.cgi/proxylog.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{'SOURCE_IP'},$cgiparams{'USERNAME'}'>$Lang::tr{'newer'}</a>"; }
516 print "$Lang::tr{'newer'}"; }