]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/logs.cgi/proxylog.dat
kernel: enable PCA953X GPIO extender for ClearFog boards
[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 %color = ();
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);
39
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'} );
46
47 my @now = localtime();
48 my $dow = $now[6]; # day of week
49 my $doy = $now[7]; # day of year (0..364)
50 my $tdoy = $now[7];
51 my $year = $now[5]+1900;
52
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'} = '';
59
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;
64
65 if ($cgiparams{'ACTION'} eq '')
66 {
67 $cgiparams{'ENABLE_FILTER'} = 'on';
68 }
69
70 if ($cgiparams{'ACTION'} eq $Lang::tr{'restore defaults'})
71 {
72 $cgiparams{'FILTER'} = "[.](gif|jpeg|jpg|png|css|js)\$";
73 $cgiparams{'ENABLE_FILTER'} = 'off';
74 }
75
76 {
77 my %save=();
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);
82 }
83
84 my $start = ($logsettings{'LOGVIEW_REVERSE'} eq 'on') ? 0x7FFFF000 : 0; #index of first line number to display
85
86 if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'})
87 {
88 my @temp = split(',',$ENV{'QUERY_STRING'});
89 $start = $temp[0];
90 $cgiparams{'MONTH'} = $temp[1];
91 $cgiparams{'DAY'} = $temp[2];
92 $cgiparams{'SOURCE_IP'} = $temp[3];
93 $cgiparams{'USERNAME'} = &Header::escape($temp[4]);
94 }
95
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)$/))
98 {
99 $cgiparams{'DAY'} = $now[3];
100 $cgiparams{'MONTH'} = $now[4];
101 }
102 elsif($cgiparams{'ACTION'} eq '>>')
103 {
104 my @temp_then=();
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];
114 } else {
115 $temp_now[3] = 1;
116 $temp_now[4] = ($temp_now[4]+1) %12;
117 @temp_then = localtime(POSIX::mktime(@temp_now) );
118 @temp_then[3] = 0;
119 }
120 $cgiparams{'MONTH'} = $temp_then[4];
121 $cgiparams{'DAY'} = $temp_then[3];
122 }
123 elsif($cgiparams{'ACTION'} eq '<<')
124 {
125 my @temp_then=();
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];
135 } else {
136 $temp_now[3] = 1;
137 $temp_now[4] = ($temp_now[4]-1) %12;
138 @temp_then = localtime(POSIX::mktime(@temp_now) );
139 @temp_then[3] = 0;
140 }
141 $cgiparams{'MONTH'} = $temp_then[4];
142 $cgiparams{'DAY'} = $temp_then[3];
143 }
144
145 # Find in which file.gz is the log. Can be calculated because WEEKLY ROTATING of *.log
146 my $gzindex;
147 my $daystr = $cgiparams{'DAY'} == 0 ? '' :$cgiparams{'DAY'} <= 9 ? "0$cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
148
149 {
150 my $xday;
151
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);
157 } else {
158 $xday = POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 );
159 $daystr = "$longmonths[$cgiparams{'MONTH'}] $daystr, $year";
160 }
161
162 # calculate end of active week (saturday 23H59)
163 my @then = ();
164 @then = localtime(time());
165 my $sunday = POSIX::mktime( 0, 0, 0, @then[3], @then[4], @then[5]);
166 $sunday += (6-$then[6]) * 86400;
167
168 # Convert delta in second to full weeks
169 $gzindex = int (($sunday-$xday)/604800 );
170 }
171
172
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;
178
179 my $lines = 0;
180 my $temp = ();
181 my $thiscode = '$temp =~ /$filter/;';
182 eval($thiscode);
183 if ($@ ne '')
184 {
185 $errormessage = "$Lang::tr{'bad ignore filter'}.$@<p>";
186 $filter = '';
187 } else {
188 my $loop = 1;
189 my $filestr = 0;
190 my $lastdatetime; # for debug
191
192 while ($gzindex >=0 && $loop) {
193 # calculate file name
194 if ($gzindex == 0) {
195 $filestr = "/var/log/squid/access.log";
196 } else {
197 $filestr = "/var/log/squid/access.log.$gzindex";
198 $filestr = "$filestr.gz" if -f "$filestr.gz";
199 }
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
209 }
210
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);
214 my $maxtime;
215 if ($cgiparams{'DAY'}==0) { # full month
216 if ($temp_now[4]++ == 12){
217 $temp_now[4] = 0;
218 $temp_now[5]++;
219 };
220 $maxtime = POSIX::mktime(@temp_now);
221 } else {
222 $maxtime = $mintime + 86400; # full day
223 }
224 READ:while (<FILE>) {
225 my ($datetime,$do,$ip,$ray,$me,$far,$url,$so) = split;
226 $ips{$ip}++;
227 $users{$so}++;
228 # for debug
229 #$lastdatetime = $datetime;
230
231 # collect lines between date && filter
232 if (( ($datetime>$mintime)&&($datetime<$maxtime)) && !($url =~ /$filter/) &&
233 ((($ip eq $sourceip) || $sourceall)) &&
234 ((($so eq $username) || $usersall)))
235 {
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";
241 } else {
242 if ($lines++ < ($start + $Header::viewsize)) {
243 push(@log,"$datetime $ip $so $url");
244 if (@log > $Header::viewsize) {
245 shift (@log);
246 }
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
250 }
251 }
252 }
253
254 # finish loop when date of lines are past maxtime
255 $loop = ($datetime < $maxtime);
256 }
257 close (FILE);
258 }
259 $gzindex--; # will try next gz file eg 40,39,38,.... because it may have holes when ipfire stopped
260 # for a long time
261
262 }
263
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--");
274 }
275 }
276
277 if ($cgiparams{'ACTION'} eq $Lang::tr{'export'})
278 {
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"; }
286 print "\r\n";
287
288 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
289
290 foreach $_ (@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";
299 } else {
300 print "$HOURdt:$MINdt:$SECdt $ip $so $url\n";
301 }
302 }
303 exit;
304 }
305
306 $selected{'SOURCE_IP'}{$cgiparams{'SOURCE_IP'}} = "selected='selected'";
307 $selected{'USERNAME'}{$cgiparams{'USERNAME'}} = "selected='selected'";
308
309 $checked{'ENABLE_FILTER'}{'off'} = '';
310 $checked{'ENABLE_FILTER'}{'on'} = '';
311 $checked{'ENABLE_FILTER'}{$cgiparams{'ENABLE_FILTER'}} = "checked='checked'";
312
313 &Header::showhttpheaders();
314
315 &Header::openpage($Lang::tr{'proxy log viewer'}, 1, '');
316
317 &Header::openbigbox('100%', 'left', '', $errormessage);
318
319 if ($errormessage) {
320 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
321 print "<font class='base'>$errormessage&nbsp;</font>\n";
322 &Header::closebox();
323 }
324
325 &Header::openbox('100%', 'left', "$Lang::tr{'settings'}:");
326
327 print <<END
328 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
329 <table width='100%'>
330 <tr>
331 <td width='10%' class='base'>$Lang::tr{'month'}:</td>
332 <td width='20%'>
333 <select name='MONTH'>
334 END
335 ;
336
337 for (my $month = 0; $month < 12; $month++)
338 {
339 print "\t<option ";
340 if ($month == $cgiparams{'MONTH'}) {
341 print "selected='selected' "; }
342 print "value='$month'>$longmonths[$month]</option>\n";
343 }
344 print <<END
345 </select>
346 </td>
347 <td width='10%' class='base' align='right'>$Lang::tr{'day'}:&nbsp;</td>
348 <td width='10%'>
349 <select name='DAY'>
350 END
351 ;
352 print "<option value='0'>$Lang::tr{'all'}</option>";
353 for (my $day = 1; $day <= 31; $day++)
354 {
355 print "\t<option ";
356 if ($day == $cgiparams{'DAY'}) {
357 print "selected='selected' "; }
358 print "value='$day'>$day</option>\n";
359 }
360 print <<END
361 </select>
362 </td>
363 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='&lt;&lt;' /></td>
364 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='&gt;&gt;' /></td>
365 <td width='25%' class='base'>$Lang::tr{'source ip'}:</td>
366 <td width='15%'>
367 <select name='SOURCE_IP'>
368 <option value='ALL' $selected{'SOURCE_IP'}{'ALL'}>$Lang::tr{'caps all'}</option>
369 END
370 ;
371 foreach my $ip (sort keys %ips) {
372 print "<option value='$ip' $selected{'SOURCE_IP'}{$ip}>$ip</option>\n"; }
373 print <<END
374 </select>
375 </td>
376 </tr>
377 <tr>
378 <td width='60%' colspan='6'>&nbsp;</td>
379 <td width='25%' class='base'>$Lang::tr{'advproxy NCSA username'}:</td>
380 <td width='15%'>
381 <select name='USERNAME'>
382 <option value='ALL' $selected{'USERNAME'}{'ALL'}>$Lang::tr{'caps all'}</option>
383 END
384 ;
385 foreach my $so (sort keys %users) {
386 $so = &Header::escape($so);
387 print "<option value='$so' $selected{'USERNAME'}{$so}>$so</option>\n"; }
388 print <<END
389 </select>
390 </td>
391 </tr>
392 <tr>
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>
397 </tr>
398 </table>
399 <div align='center'>
400 <table width='50%'>
401 <tr>
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>
405 </tr>
406 </table>
407 </div>
408 </form>
409 END
410 ;
411
412 &Header::closebox();
413 &Header::openbox('100%', 'left', $Lang::tr{'log'});
414
415
416 $start = $lines - ${Header::viewsize} if ($start >= $lines - ${Header::viewsize});
417 $start = 0 if ($start < 0);
418
419 my $prev;
420 if ($start == 0) {
421 $prev = -1;
422 } else {
423 $prev = $start - ${Header::viewsize};
424 $prev = 0 if ( $prev < 0);
425 }
426
427 my $next;
428 if ($start == $lines - ${Header::viewsize}) {
429 $next = -1;
430 } else {
431 $next = $start + ${Header::viewsize};
432 $next = $lines - ${Header::viewsize} if ($next >= $lines - ${Header::viewsize});
433 }
434
435 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
436
437 print "<p><b>$Lang::tr{'web hits'} $daystr: $lines</b></p>";
438 if ($lines != 0) { &oldernewer(); }
439 print <<END
440 <table width='100%' class='tbl'>
441 <tr>
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>
446 </tr>
447 END
448 ;
449 my $ll = 0;
450 my $col='';
451 foreach $_ (@log)
452 {
453 if ($ll % 2) {
454 print "<tr>";
455 $col="bgcolor='$color{'color20'}'"; }
456 else {
457 print "<tr>";
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);
464
465 $url =~ /(^.{0,60})/;
466 my $part = $1;
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);
472 } else {
473 $DAYdt='';
474 }
475 print <<END
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>
480 </tr>
481 END
482 ;
483 $ll++;
484 }
485
486 print "</table>";
487
488 &oldernewer();
489
490 &Header::closebox();
491
492 &Header::closebigbox();
493
494 &Header::closepage();
495
496
497 sub oldernewer
498 {
499 print <<END
500 <table width='100%'>
501 <tr>
502 END
503 ;
504
505 print "<td align='center' width='50%'>";
506 if ($prev != -1) {
507 print "<a href='/cgi-bin/logs.cgi/proxylog.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{'SOURCE_IP'},$cgiparams{'USERNAME'}'>$Lang::tr{'older'}</a>"; }
508 else {
509 print "$Lang::tr{'older'}"; }
510 print "</td>\n";
511
512 print "<td align='center' width='50%'>";
513 if ($next >= 0 ) {
514 print "<a href='/cgi-bin/logs.cgi/proxylog.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{'SOURCE_IP'},$cgiparams{'USERNAME'}'>$Lang::tr{'newer'}</a>"; }
515 else {
516 print "$Lang::tr{'newer'}"; }
517 print "</td>\n";
518
519 print <<END
520 </tr>
521 </table>
522 END
523 ;
524 }
525