Gro├čes Update:
[ipfire-2.x.git] / html / cgi-bin / logs.cgi / proxylog.dat
CommitLineData
cd1a2927
MT
1#!/usr/bin/perl
2#
3# SmoothWall CGIs
4#
5# This code is distributed under the terms of the GPL
6#
7# (c) The SmoothWall Team
8#
9# $Id: proxylog.dat,v 1.4.2.25 2005/07/07 17:32:24 eoberlander Exp $
10#
11
12use strict;
13
14# enable only the following on debugging purpose
15#use warnings;
16#use CGI::Carp 'fatalsToBrowser';
17
18require 'CONFIG_ROOT/general-functions.pl';
19require "${General::swroot}/lang.pl";
20require "${General::swroot}/header.pl";
21
22use POSIX();
23
24#workaround to suppress a warning when a variable is used only once
25my @dummy = ( ${Header::table2colour} );
26undef (@dummy);
27
28my %cgiparams=();
29my %logsettings=();
30my %ips=();
31my %selected=();
32my %checked=();
33my @log=();
34my $errormessage = '';
35
36my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
37 'Sep', 'Oct', 'Nov', 'Dec' );
38my @longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'},
39 $Lang::tr{'april'}, $Lang::tr{'may'}, $Lang::tr{'june'}, $Lang::tr{'july'}, $Lang::tr{'august'},
40 $Lang::tr{'september'}, $Lang::tr{'october'}, $Lang::tr{'november'},
41 $Lang::tr{'december'} );
42
43my @now = localtime();
44my $dow = $now[6]; # day of week
45my $doy = $now[7]; # day of year (0..364)
46my $tdoy = $now[7];
47my $year = $now[5]+1900;
48
49$cgiparams{'DAY'} = $now[3];
50$cgiparams{'MONTH'} = $now[4];
51$cgiparams{'SOURCE_IP'} = 'ALL';
52$cgiparams{'FILTER'} = "[.](gif|jpeg|jpg|png|css|js)\$";
53$cgiparams{'ENABLE_FILTER'} = 'off';
54$cgiparams{'ACTION'} = '';
55
56&Header::getcgihash(\%cgiparams);
57$logsettings{'LOGVIEW_REVERSE'} = 'off';
58&General::readhash("${General::swroot}/logging/settings", \%logsettings);
59${Header::viewsize} = defined ($logsettings{'LOGVIEW_VIEWSIZE'}) ? $logsettings{'LOGVIEW_VIEWSIZE'} : 150;
60
61if ($cgiparams{'ACTION'} eq '')
62{
63 $cgiparams{'ENABLE_FILTER'} = 'on';
64}
65
66if ($cgiparams{'ACTION'} eq $Lang::tr{'restore defaults'})
67{
68 $cgiparams{'FILTER'} = "[.](gif|jpeg|jpg|png|css|js)\$";
69 $cgiparams{'ENABLE_FILTER'} = 'off';
70}
71
72{
73 my %save=();
74 $save{'FILTER'} = $cgiparams{'FILTER'};
75 $save{'ENABLE_FILTER'} = $cgiparams{'ENABLE_FILTER'};
76 &General::writehash("${General::swroot}/proxy/viewersettings", \%save);
77 &General::readhash("${General::swroot}/proxy/viewersettings", \%save);
78}
79
80my $start = ($logsettings{'LOGVIEW_REVERSE'} eq 'on') ? 0x7FFFF000 : 0; #index of first line number to display
81
82if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'})
83{
84 my @temp = split(',',$ENV{'QUERY_STRING'});
85 $start = $temp[0];
86 $cgiparams{'MONTH'} = $temp[1];
87 $cgiparams{'DAY'} = $temp[2];
88 $cgiparams{'SOURCE_IP'} = $temp[3];
89}
90
91if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
92 !($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)$/))
93{
94 $cgiparams{'DAY'} = $now[3];
95 $cgiparams{'MONTH'} = $now[4];
96}
97elsif($cgiparams{'ACTION'} eq '>>')
98{
99 my @temp_then=();
100 my @temp_now = localtime(time);
101 $temp_now[4] = $cgiparams{'MONTH'};
102 $temp_now[3] = $cgiparams{'DAY'};
103 if ($cgiparams{'DAY'}) {
104 @temp_then = localtime(POSIX::mktime(@temp_now) + 86400);
105 ## Retrieve the same time on the next day +
106 ## 86400 seconds in a day
107 $cgiparams{'MONTH'} = $temp_then[4];
108 $cgiparams{'DAY'} = $temp_then[3];
109 } else {
110 $temp_now[3] = 1;
111 $temp_now[4] = ($temp_now[4]+1) %12;
112 @temp_then = localtime(POSIX::mktime(@temp_now) );
113 @temp_then[3] = 0;
114 }
115 $cgiparams{'MONTH'} = $temp_then[4];
116 $cgiparams{'DAY'} = $temp_then[3];
117}
118elsif($cgiparams{'ACTION'} eq '<<')
119{
120 my @temp_then=();
121 my @temp_now = localtime(time);
122 $temp_now[4] = $cgiparams{'MONTH'};
123 $temp_now[3] = $cgiparams{'DAY'};
124 if ($cgiparams{'DAY'}) {
125 @temp_then = localtime(POSIX::mktime(@temp_now) - 86400);
126 ## Retrieve the same time on the next day -
127 ## 86400 seconds in a day
128 $cgiparams{'MONTH'} = $temp_then[4];
129 $cgiparams{'DAY'} = $temp_then[3];
130 } else {
131 $temp_now[3] = 1;
132 $temp_now[4] = ($temp_now[4]-1) %12;
133 @temp_then = localtime(POSIX::mktime(@temp_now) );
134 @temp_then[3] = 0;
135 }
136 $cgiparams{'MONTH'} = $temp_then[4];
137 $cgiparams{'DAY'} = $temp_then[3];
138}
139
140# Find in which file.gz is the log. Can be calculated because WEEKLY ROTATING of *.log
141my $gzindex;
142my $daystr = $cgiparams{'DAY'} == 0 ? '' :$cgiparams{'DAY'} <= 9 ? "0$cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
143
144{
145 my $xday;
146
147 # Calculate time. If future date, calculate for past year !!!
148 if (( $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
149 ( $cgiparams{'MONTH'} > $now[4] ) ) {
150 $xday = POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1901 );
151 $daystr = "$longmonths[$cgiparams{'MONTH'}] $daystr, ". int($year-1);
152 } else {
153 $xday = POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 );
154 $daystr = "$longmonths[$cgiparams{'MONTH'}] $daystr, $year";
155 }
156
157 # calculate end of active week (saturday 23H59)
158 my @then = ();
159 @then = localtime(time());
160 my $sunday = POSIX::mktime( 0, 0, 0, @then[3], @then[4], @then[5]);
161 $sunday += (6-$then[6]) * 86400;
162
163 # Convert delta in second to full weeks
164 $gzindex = int (($sunday-$xday)/604800 );
165}
166
167
168my $filter = $cgiparams{'ENABLE_FILTER'} eq 'on' ? $cgiparams{'FILTER'} : '';
169my $sourceip = $cgiparams{'SOURCE_IP'};
170my $sourceall = $cgiparams{'SOURCE_IP'} eq 'ALL' ? 1 : 0;
171
172my $lines = 0;
173my $temp = ();
174my $thiscode = '$temp =~ /$filter/;';
175eval($thiscode);
176if ($@ ne '')
177{
178 $errormessage = "$Lang::tr{'bad ignore filter'}.$@<P>";
179 $filter = '';
180} else {
181 my $loop = 1;
182 my $filestr = 0;
183 my $lastdatetime; # for debug
184
185 while ($gzindex >=0 && $loop) {
186 # calculate file name
187 if ($gzindex == 0) {
188 $filestr = "/var/log/squid/access.log";
189 } else {
190 $filestr = "/var/log/squid/access.log.$gzindex";
191 $filestr = "$filestr.gz" if -f "$filestr.gz";
192 }
193 # now read file if existing
194 if (open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr))) {
195 #&General::log("reading $filestr");
196 my @temp_now = localtime(time);
197 $temp_now[4] = $cgiparams{'MONTH'};
198 $temp_now[3] = $cgiparams{'DAY'};
199 if ( ( $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
200 ( $cgiparams{'MONTH'} > $now[4] ) ) {
201 $temp_now[5]--; # past year
202 }
203
204 $temp_now[2] = $temp_now[1] = $temp_now[0] = 0; # start at 00:00:00
205 $temp_now[3] = 1 if ($cgiparams{'DAY'}==0); # All days selected, start at '1'
206 my $mintime = POSIX::mktime(@temp_now);
207 my $maxtime;
208 if ($cgiparams{'DAY'}==0) { # full month
209 if ($temp_now[4]++ == 12){
210 $temp_now[4] = 0;
211 $temp_now[5]++;
212 };
213 $maxtime = POSIX::mktime(@temp_now);
214 } else {
215 $maxtime = $mintime + 86400; # full day
216 }
217 READ:while (<FILE>) {
218 my ($datetime,$do,$ip,$ray,$me,$far,$url,$so) = split;
219 $ips{$ip}++;
220 # for debug
221 #$lastdatetime = $datetime;
222
223 # collect lines between date && filter
224 if (( ($datetime>$mintime)&&($datetime<$maxtime)) && !($url =~ /$filter/) &&
225 ((($ip eq $sourceip) || $sourceall)))
226 {
227 # when standart viewing, just keep in memory the correct slices
228 # it starts a '$start' and size is $viewport
229 # If export, then keep all lines...
230 if ($cgiparams{'ACTION'} eq $Lang::tr{'export'}){
231 $log[$lines++] = "$datetime $ip $url";
232 } else {
233 if ($lines++ < ($start + $Header::viewsize)) {
234 push(@log,"$datetime $ip $url");
235 if (@log > $Header::viewsize) {
236 shift (@log);
237 }
238 #} else { dont do this optimisation, need to count lines !
239 # $datetime = $maxtime; # we have read viewsize lines, stop main loop
240 # last READ; # exit read file
241 }
242 }
243 }
244
245 # finish loop when date of lines are past maxtime
246 $loop = ($datetime < $maxtime);
247 }
248 close (FILE);
249 }
250 $gzindex--; # will try next gz file eg 40,39,38,.... because it may have holes when ipcop stopped
251 # for a long time
252
253 }
254
255 #$errormessage="$errormessage$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
256 if (0) { # print last date record read
257 my ($SECdt, $MINdt, $HOURdt, $DAYdt, $MONTHdt, $YEARdt) = localtime($lastdatetime);
258 $SECdt = sprintf ("%.02d",$SECdt);
259 $MINdt = sprintf ("%.02d",$MINdt);
260 $HOURdt = sprintf ("%.02d",$HOURdt);
261 $DAYdt = sprintf ("%.02d",$DAYdt);
262 $MONTHdt = sprintf ("%.02d",$MONTHdt+1);
263 $YEARdt = sprintf ("%.04d",$YEARdt+1900);
264 &General::log ("$HOURdt:$MINdt:$SECdt, $DAYdt/$MONTHdt/$YEARdt--");
265 }
266}
267
268if ($cgiparams{'ACTION'} eq $Lang::tr{'export'})
269{
270 print "Content-type: text/plain\n\n";
78331e30 271 print "IPFire proxy log\r\n";
cd1a2927
MT
272 print "$Lang::tr{'date'}: $daystr\r\n";
273 print "Source IP: $cgiparams{'SOURCE_IP'}\r\n";
274 if ($cgiparams{'ENABLE_FILTER'} eq 'on') {
275 print "Ignore filter: $cgiparams{'FILTER'}\r\n"; }
276 print "\r\n";
277
278 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
279
280 foreach $_ (@log) {
281 my ($datetime,$ip,$url) = split;
282 my ($SECdt, $MINdt, $HOURdt, $DAYdt, $MONTHdt, $YEARdt) = localtime($datetime);
283 $SECdt = sprintf ("%.02d",$SECdt);
284 $MINdt = sprintf ("%.02d",$MINdt);
285 $HOURdt = sprintf ("%.02d",$HOURdt);
286 if ($cgiparams{'DAY'}==0) { # full month
287 $DAYdt = sprintf ("%.02d",$DAYdt);
288 print "$DAYdt/$HOURdt:$MINdt:$SECdt $ip $url\n";
289 } else {
290 print "$HOURdt:$MINdt:$SECdt $ip $url\n";
291 }
292 }
293 exit;
294}
295
296$selected{'SOURCE_IP'}{$cgiparams{'SOURCE_IP'}} = "selected='selected'";
297
298$checked{'ENABLE_FILTER'}{'off'} = '';
299$checked{'ENABLE_FILTER'}{'on'} = '';
300$checked{'ENABLE_FILTER'}{$cgiparams{'ENABLE_FILTER'}} = "checked='checked'";
301
302&Header::showhttpheaders();
303
304&Header::openpage($Lang::tr{'proxy log viewer'}, 1, '');
305
306&Header::openbigbox('100%', 'left', '', $errormessage);
307
308if ($errormessage) {
309 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
310 print "<font class='base'>$errormessage&nbsp;</font>\n";
311 &Header::closebox();
312}
313
314&Header::openbox('100%', 'left', "$Lang::tr{'settings'}:");
315
316print <<END
317<form method='post' action='$ENV{'SCRIPT_NAME'}'>
318<table width='100%'>
319<tr>
320 <td width='10%' class='base'>$Lang::tr{'month'}:</td>
321 <td width='20%'>
322 <select name='MONTH'>
323END
324;
325
326for (my $month = 0; $month < 12; $month++)
327{
328 print "\t<option ";
329 if ($month == $cgiparams{'MONTH'}) {
330 print "selected='selected' "; }
331 print "value='$month'>$longmonths[$month]</option>\n";
332}
333print <<END
334 </select>
335 </td>
336 <td width='10%' class='base' align='right'>$Lang::tr{'day'}:&nbsp;</td>
337 <td width='10%'>
338 <select name='DAY'>
339END
340;
341print "<option value='0'>$Lang::tr{'all'}</option>";
342for (my $day = 1; $day <= 31; $day++)
343{
344 print "\t<option ";
345 if ($day == $cgiparams{'DAY'}) {
346 print "selected='selected' "; }
347 print "value='$day'>$day</option>\n";
348}
349print <<END
350 </select>
351 </td>
352 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='&lt;&lt;' /></td>
353 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='&gt;&gt;' /></td>
354 <td width='25%' class='base'>$Lang::tr{'source ip'}:</td>
355 <td width='15%'>
356 <select name='SOURCE_IP'>
357 <option value='ALL' $selected{'SOURCE_IP'}{'ALL'}>$Lang::tr{'caps all'}</option>
358END
359;
360foreach my $ip (keys %ips) {
361 print "<option value='$ip' $selected{'SOURCE_IP'}{$ip}>$ip</option>\n"; }
362print <<END
363 </select>
364 </td>
365</tr>
366<tr>
367 <td class='base'>$Lang::tr{'ignore filter'}:</td>
368 <td colspan='5'><input type='text' name='FILTER' value='$cgiparams{'FILTER'}' size='40' /></td>
369 <td class='base'>$Lang::tr{'enable ignore filter'}:</td>
370 <td><input type='checkbox' name='ENABLE_FILTER' value='on' $checked{'ENABLE_FILTER'}{'on'} /></td>
371</tr>
372</table>
373<div align='center'>
374<table width='50%'>
375<tr>
376 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'restore defaults'}' /></td>
377 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
378 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'export'}' /></td>
379</tr>
380</table>
381</div>
382</form>
383END
384;
385
386&Header::closebox();
387&Header::openbox('100%', 'left', $Lang::tr{'log'});
388
389
390$start = $lines - ${Header::viewsize} if ($start >= $lines - ${Header::viewsize});
391$start = 0 if ($start < 0);
392
393my $prev;
394 if ($start == 0) {
395 $prev = -1;
396 } else {
397 $prev = $start - ${Header::viewsize};
398 $prev = 0 if ( $prev < 0);
399 }
400
401my $next;
402 if ($start == $lines - ${Header::viewsize}) {
403 $next = -1;
404 } else {
405 $next = $start + ${Header::viewsize};
406 $next = $lines - ${Header::viewsize} if ($next >= $lines - ${Header::viewsize});
407 }
408
409if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
410
411print "<p><b>$Lang::tr{'web hits'} $daystr: $lines</b></p>";
412if ($lines != 0) { &oldernewer(); }
413print <<END
414<table width='100%'>
415<tr>
416<td width='10%' align='center' class='boldbase'><b>$Lang::tr{'time'}</b></td>
417<td width='15%' align='center' class='boldbase'><b>$Lang::tr{'source ip'}</b></td>
418<td width='75%' align='center' class='boldbase'><b>$Lang::tr{'website'}</b></td>
419</tr>
420END
421;
422my $ll = 0;
423foreach $_ (@log)
424{
425 if ($ll % 2) {
426 print "<tr bgcolor='${Header::table1colour}'>\n"; }
427 else {
428 print "<tr bgcolor='${Header::table2colour}'>\n"; }
429 my ($datetime,$ip,$url) = split;
430 my ($SECdt, $MINdt, $HOURdt, $DAYdt, $MONTHdt, $YEARdt) = localtime($datetime);
431 $SECdt = sprintf ("%.02d",$SECdt);
432 $MINdt = sprintf ("%.02d",$MINdt);
433 $HOURdt = sprintf ("%.02d",$HOURdt);
434
435 $url =~ /(^.{0,60})/;
436 my $part = $1;
437 unless (length($part) < 60) { $part = "${part}..."; }
438 $url = &Header::cleanhtml($url,"y");
439 $part = &Header::cleanhtml($part,"y");
440 if ($cgiparams{'DAY'}==0) { # full month
441 $DAYdt = sprintf ("%.02d/",$DAYdt);
442 } else {
443 $DAYdt='';
444 }
445 print <<END
446 <td align='center'>$DAYdt$HOURdt:$MINdt:$SECdt</td>
447 <td align='center'>$ip</td>
448 <td align='left'><a href='$url' title='$url' target='_new'>$part</a></td>
449</tr>
450END
451 ;
452 $ll++;
453}
454
455print "</table>";
456
457&oldernewer();
458
459&Header::closebox();
460
461&Header::closebigbox();
462
463&Header::closepage();
464
465
466sub oldernewer
467{
468print <<END
469<table width='100%'>
470<tr>
471END
472;
473
474print "<td align='center' width='50%'>";
475if ($prev != -1) {
476 print "<a href='/cgi-bin/logs.cgi/proxylog.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{'SOURCE_IP'}'>$Lang::tr{'older'}</a>"; }
477else {
478 print "$Lang::tr{'older'}"; }
479print "</td>\n";
480
481print "<td align='center' width='50%'>";
482if ($next >= 0 ) {
483 print "<a href='/cgi-bin/logs.cgi/proxylog.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{'SOURCE_IP'}'>$Lang::tr{'newer'}</a>"; }
484else {
485 print "$Lang::tr{'newer'}"; }
486print "</td>\n";
487
488print <<END
489</tr>
490</table>
491END
492;
493}
494