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