Firewallhitgraphs gefixt.
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / logs.cgi / urlfilter.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 # $Id: urlfilter.dat,v 1.4 2005/05/15 00:00:00 marco Exp $
10 #
11
12 use strict;
13
14 # enable only the following on debugging purpose
15 #use warnings;
16 #use CGI::Carp 'fatalsToBrowser';
17
18 use POSIX();
19
20 require '/var/ipfire/general-functions.pl';
21 require "${General::swroot}/lang.pl";
22 require "${General::swroot}/header.pl";
23
24 my $dbdir = "${General::swroot}/urlfilter/blacklists";
25 my $logdir = "/var/log/squidGuard";
26
27 my %cgiparams=();
28 my %logsettings=();
29 my %filtersettings=();
30 my %selected=();
31
32 my $errormessage='';
33
34 my @log=();
35 my $logarch='';
36 my $date='';
37 my $time='';
38 my $pid='';
39 my @loginfo=();
40 my @ip=();
41 my @category=();
42 my $dsturl='';
43 my $site='';
44 my $attr1='';
45 my $attr2='';
46
47 my $start=0;
48 my $prev=0;
49 my $next=0;
50 my $lines=0;
51 my @slice=();
52
53 my @then=();
54 my @temp=();
55 my @temp_then=();
56 my @temp_now=();
57 my $lastleap='';
58
59 my $day='';
60 my $daystr='';
61 my $month='';
62 my $monthstr='';
63 my $longmonthstr='';
64 my $year='';
65
66 my $logday='';
67 my $logmonth='';
68 my $logyear='';
69
70 my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
71         'Sep', 'Oct', 'Nov', 'Dec' );
72 my @longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'},
73         $Lang::tr{'april'}, $Lang::tr{'may'}, $Lang::tr{'june'}, $Lang::tr{'july'}, $Lang::tr{'august'},
74         $Lang::tr{'september'}, $Lang::tr{'october'}, $Lang::tr{'november'},
75         $Lang::tr{'december'} );
76
77 my @now = localtime(time);
78 my $doy = $now[7];
79 my $tdoy = $now[7];
80 my $year = $now[5]+1900;
81
82 $cgiparams{'DAY'} = $now[3];
83 $cgiparams{'MONTH'} = $now[4];
84 $cgiparams{'LOGTYPE'} = 'urlfilter';
85
86 &Header::getcgihash(\%cgiparams);
87 $logsettings{'LOGVIEW_REVERSE'} = 'off';
88 &General::readhash("${General::swroot}/logging/settings", \%logsettings);
89 if (-e "${General::swroot}/urlfilter/settings")
90 {
91         &General::readhash("${General::swroot}/urlfilter/settings", \%filtersettings);
92 }
93
94 $now[4] = $cgiparams{'MONTH'}+1;
95 if($now[4] < 10) {
96         $now[4] = "0$now[4]"; }
97
98 $start = -1;
99
100 if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'})
101 {
102         @temp = split(',',$ ENV{'QUERY_STRING'});
103         $start = $temp[0];
104         $cgiparams{'MONTH'} = $temp[1];
105         $cgiparams{'DAY'} = $temp[2];  
106         $cgiparams{'LOGTYPE'} = $temp[3];  
107 }
108
109 if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
110         !($cgiparams{'DAY'} =~ /^(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)$/))
111 {
112         $cgiparams{'DAY'} = $now[3];
113         $cgiparams{'MONTH'} = $now[4];
114 }
115 elsif($cgiparams{'ACTION'} eq '>>')
116 {
117         @temp_now = localtime(time);
118         $temp_now[4] = $cgiparams{'MONTH'};
119         $temp_now[3] = $cgiparams{'DAY'};
120         @temp_then = localtime(POSIX::mktime(@temp_now) + 86400);
121            ## Retrieve the same time on the next day -
122            ## 86400 seconds in a day
123         $cgiparams{'MONTH'} = $temp_then[4];
124         $cgiparams{'DAY'} = $temp_then[3];
125 }
126 elsif($cgiparams{'ACTION'} eq '<<')
127 {
128         @temp_now = localtime(time);
129         $temp_now[4] = $cgiparams{'MONTH'};
130         $temp_now[3] = $cgiparams{'DAY'};
131         @temp_then = localtime(POSIX::mktime(@temp_now) - 86400);
132            ## Retrieve the same time on the previous day -
133            ## 86400 seconds in a day
134         $cgiparams{'MONTH'} = $temp_then[4];
135         $cgiparams{'DAY'} = $temp_then[3];
136 }
137
138 $monthstr = $shortmonths[$cgiparams{'MONTH'}];
139 $longmonthstr = $longmonths[$cgiparams{'MONTH'}];
140
141 if (($cgiparams{'DAY'} ne $now[3]) || ($cgiparams{'MONTH'} ne $now[4]))
142 {
143         @then = ();
144         if ( (  $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
145                         ( $cgiparams{'MONTH'} > $now[4] ) ) {
146                 @then = localtime(POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1901 ));
147         } else {
148                 @then = localtime(POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 ));
149         }
150         $tdoy = $then[7];
151         $lastleap=($year-1)%4; 
152         if ($tdoy>$doy) {
153                 if ($lastleap == 0 && $tdoy < 60) {
154                         $doy=$tdoy+366;
155                 } else {
156                         $doy=$doy+365;
157                 }
158         }
159 }
160
161 $day = $cgiparams{'DAY'};
162 if ($day <= 9) {
163         $daystr = " $day";
164 } else {
165         $daystr = $day;
166 }
167 if ($day < 10) {
168         $day = "0$day";
169 }
170
171 &processevent;
172
173 if ($cgiparams{'ACTION'} eq $Lang::tr{'export'})
174 {
175         print "Content-type: text/plain\n\n";
176         print "IPCop URL filter log\r\n";
177         print "Section: $cgiparams{'LOGTYPE'}\r\n";
178         print "Date: $daystr $longmonthstr\r\n"; 
179         print "\r\n";
180
181         if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
182
183         foreach (@log)
184         {
185                 ($date,$time,$pid,@loginfo) = split(/ /);
186                 @ip = split(/\//,$loginfo[2]);
187                 @category = split(/\//,$loginfo[0]);
188                 $dsturl = $loginfo[1];
189                 $loginfo[3] =~ s/\%5c/\\/;
190                 print "$date ";
191                 print "$time ";
192                 if ($cgiparams{'LOGTYPE'} eq 'squidGuard')
193                 {
194                         print "$pid ";
195                         print "@loginfo";
196                 } else {
197                         print "$category[1] ";
198                         print "$ip[0] ";
199                         if ($filtersettings{'ENABLE_USERNAME_LOG'} eq 'on') { print "$loginfo[3] "; }
200                         print "$dsturl";
201                         print "\n";
202                 }
203         }
204
205         exit;
206 }
207
208 &Header::showhttpheaders();
209
210 &Header::openpage($Lang::tr{'urlfilter log viewer'}, 1, '');
211
212 &Header::openbigbox('100%', 'left', '', $errormessage);
213
214 if ($errormessage) {
215         &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
216         print "<font class='base'>$errormessage&nbsp;</font>\n";
217         &Header::closebox();
218 }
219
220 &Header::openbox('100%', 'left', "$Lang::tr{'settings'}:");
221
222 $selected{'LOGTYPE'}{$cgiparams{'LOGTYPE'}} = "selected='selected'";
223
224 print <<END
225 <form method='post' action="$ENV{'SCRIPT_NAME'}">
226 <table width='100%'>
227 <tr>
228         <td width='8%' class='base'>$Lang::tr{'section'}</td>
229         <td width='15%' class='base'>
230         <select name='LOGTYPE' size='1'>
231         <option value='urlfilter' $selected{'LOGTYPE'}{'urlfilter'} = "selected='selected'">$Lang::tr{'urlfilter url filter'}</option>
232         <option value='squidGuard' $selected{'LOGTYPE'}{'squidGuard'} = "selected='selected'">squidGuard</option>
233         </select>
234         </td>
235
236         <td width='8%' class='base'>$Lang::tr{'month'}:</td>
237         <td width='15%'>
238         <select name='MONTH'>
239 END
240 ;
241 for ($month = 0; $month < 12; $month++)
242 {
243         print "\t<option ";
244         if ($month == $cgiparams{'MONTH'}) {
245                 print 'selected="selected" '; }
246         print "value='$month'>$longmonths[$month]</option>\n";
247 }
248 print <<END
249         </select>
250         </td>
251         <td width='8%' class='base'>$Lang::tr{'day'}:</td>
252         <td width='15%'>
253         <select name='DAY'>
254 END
255 ;
256 for ($day = 1; $day <= 31; $day++) 
257 {
258         print "\t<option ";
259         if ($day == $cgiparams{'DAY'}) {
260                 print 'selected="selected" '; }
261         print "value='$day'>$day</option>\n";
262 }
263 print <<END
264         </select>
265         </td>
266         <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='&lt;&lt;' /></td>
267         <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='&gt;&gt;' /></td>
268         <td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
269         <td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'export'}' /></td>
270 </tr>
271 </table>
272 </form>
273 END
274 ;
275
276 &Header::closebox();
277
278 &Header::openbox('100%', 'left', $Lang::tr{'log'});
279
280 $lines = @log;
281
282 if ($start == -1) { $start = $lines - $Header::viewsize; }
283 if ($start >= $lines - $Header::viewsize) { $start = $lines - $Header::viewsize; };
284 if ($start < 0) { $start = 0; }
285
286 $prev = $start - $Header::viewsize;
287 $next = $start + $Header::viewsize;
288
289 if ($prev < 0) { $prev = 0; }
290 if ($next >= $lines) { $next = -1 }
291 if ($start == 0) { $prev = -1; }
292
293 if ($cgiparams{'LOGTYPE'} eq 'urlfilter')
294 {
295         print "<b>$Lang::tr{'urlfilter log summary'} $longmonthstr $daystr: $lines</b><p>\n";
296 }
297
298 @slice = splice(@log, $start, $Header::viewsize);
299
300 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @slice = reverse @slice; }
301
302 if ($lines)
303 {
304
305         $lines = 0;
306
307         print "<table width='100%'>\n";
308         unless ($cgiparams{'LOGTYPE'} eq 'squidGuard')
309         {
310                 print "<tr>\n";
311                 print "<td align='center'><b>$Lang::tr{'urlfilter time'}</b></td>\n";
312                 print "<td align='center'><b>$Lang::tr{'urlfilter category'}</b></td>\n";
313                 print "<td align='center'><b>$Lang::tr{'urlfilter client'}</b></td>\n";
314                 if ($filtersettings{'ENABLE_USERNAME_LOG'} eq 'on') { print "<td align='center'><b>$Lang::tr{'urlfilter username'}</b></td>\n"; }
315                 print "<td align='center'><b>$Lang::tr{'urlfilter dst'}</b></td>\n";
316                 print "</tr>\n";
317         }
318
319         foreach (@slice)
320         {
321                 undef $attr1; undef $attr2;
322                 $lines++;
323                 ($date,$time,$pid,@loginfo) = split(/ /);
324                 @ip = split(/\//,$loginfo[2]);
325                 @category = split(/\//,$loginfo[0]);
326                 $dsturl = $loginfo[1];
327                 $loginfo[3] =~ s/\%5c/\\/;
328                 if ($cgiparams{'LOGTYPE'} eq 'squidGuard') {
329                         if (@loginfo[0] =~ /squidGuard/) { $attr1 .= "<b>"; $attr2 .= "</b>"; }
330                         if (@loginfo[1] =~ /ready/) { $attr1 .= "<b><font color='$Header::colourgreen'>"; $attr2 .= "</font></b>"; }
331                         if (@loginfo[2] =~ /emergency/) { $attr1 .= "<b><font color='$Header::colourerr'>"; $attr2 .= "</font></b>"; }
332                         print "<tr>\n";
333                 } else {
334                         if ($lines % 2) { print "<tr bgcolor='$Header::table1colour'>\n"; } else { print "<tr bgcolor='$Header::table2colour'>\n"; } 
335                 }
336                 if ($cgiparams{'LOGTYPE'} eq 'squidGuard') {
337                         print "<td nowrap>$time &nbsp; $pid &nbsp; $attr1@loginfo$attr2</td>\n";
338                 } else {
339                         print "<td width='10%' align='center' nowrap>$time</td>\n";
340                         print "<td width='11%' align='center' nowrap>$category[1]</td>\n";
341                         print "<td width='15%' align='center' nowrap>$ip[0]</td>\n";
342                         if ($filtersettings{'ENABLE_USERNAME_LOG'} eq 'on')
343                         {
344                                 print "<td width='12%' align='center' nowrap>$loginfo[3]</td>\n";
345                                 $site = substr($dsturl,0,55);
346                                 if (length($dsturl) > 55) { $site .= "..."; }
347                         } else {
348                                 $site = substr($dsturl,0,69);
349                                 if (length($dsturl) > 69) { $site .= "..."; }
350                         }
351                         #print "<td>$site</td>\n";
352                         print "<td><a href='$dsturl' title='$dsturl' target='_blank'>$site</a></td>\n";
353                 }
354                 print "</tr>\n";
355         }
356
357         print "</table><br>\n";
358
359 }
360
361 &oldernewer();
362
363 &Header::closebox();
364
365 &Header::closebigbox();
366
367 &Header::closepage();
368
369 # -------------------------------------------------------------------
370
371 sub processevent
372 {
373         my $filestr='';
374
375         undef @log;
376         if ($cgiparams{'LOGTYPE'} eq 'squidGuard')
377         {
378                 $filestr = "$logdir/squidGuard.log";
379                 foreach $logarch (<$filestr.*.gz>)
380                 {
381                         open (LOG,"gzip -dc $logarch |");
382                         foreach (<LOG>) {
383                                 ($date,$time,$pid,@loginfo) = split(/ /);
384                                 ($logyear,$logmonth,$logday) = split(/-/,$date);
385                                 if (($logyear == $year) && ($logmonth == $cgiparams{'MONTH'}+1) && ($logday == $day)) { push(@log,$_) }
386                         }
387                         close(LOG);
388                 }
389                 open (LOG,$filestr);
390                 foreach (<LOG>) {
391                         ($date,$time,$pid,@loginfo) = split(/ /);
392                         ($logyear,$logmonth,$logday) = split(/-/,$date);
393                         if (($logyear == $year) && ($logmonth == $cgiparams{'MONTH'}+1) && ($logday == $day)) { push(@log,$_) }
394                 }
395                 close(LOG);
396         } else {
397                 foreach $logarch (<$logdir/*.gz>)
398                 {
399                         if ($logarch !~ /squidGuard\.log/) {
400                                 open (LOG,"gzip -dc $logarch |");
401                                 foreach (<LOG>) {
402                                         ($date,$time,$pid,@loginfo) = split(/ /);
403                                         ($logyear,$logmonth,$logday) = split(/-/,$date);
404                                         if (($logyear == $year) && ($logmonth == $cgiparams{'MONTH'}+1) && ($logday == $day)) { push(@log,$_) }
405                                 }
406                                 close(LOG);
407                         }
408                 }
409                 foreach $filestr (<$logdir/*.log>)
410                 {
411                         if ($filestr !~ /squidGuard\.log/) {
412                                 open (LOG,$filestr);
413                                 foreach (<LOG>) {
414                                         ($date,$time,$pid,@loginfo) = split(/ /);
415                                         ($logyear,$logmonth,$logday) = split(/-/,$date);
416                                         if (($logyear == $year) && ($logmonth == $cgiparams{'MONTH'}+1) && ($logday == $day)) { push(@log,$_) }
417                                 }
418                                 close(LOG);
419                         }
420                 }
421                 @log = sort { substr($a,11,8) cmp substr($b,11,8) } @log; 
422
423         }
424 }
425
426 # -------------------------------------------------------------------
427
428 sub oldernewer
429 {
430         print "<table width='100%'>";
431         print "<tr>";
432
433         print "<td align='center' width='50%'>";
434         if ($prev != -1) {
435                 print "<a href='$ENV{'SCRIPT_NAME'}?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{'LOGTYPE'}'>$Lang::tr{'older'}</a>"; }
436         else {
437                 print "$Lang::tr{'older'}"; }
438         print "</td>\n";
439
440         print "<td align='center' width='50%'>";
441         if ($next != -1) {
442                 print "<a href='$ENV{'SCRIPT_NAME'}?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{'LOGTYPE'}'>$Lang::tr{'newer'}</a>"; }
443         else {
444                 print "$Lang::tr{'newer'}"; }
445         print "</td>\n";
446
447         print "</tr>";
448         print "</table>";
449 }
450
451 # -------------------------------------------------------------------