#!/usr/bin/perl
#-----------------------------------------------------------------------------
# usertracker AWStats plugin
# This plugin adds a form in AWStats main page to allow users to see raw
# content of current log files. A filter is also available.
#-----------------------------------------------------------------------------
# Perl Required Modules: Socket
#-----------------------------------------------------------------------------
# $Revision: 1.16 $ - $Author: sacx $ - $Date: 2006/04/12 13:02:11 $


# <-----
# ENTER HERE THE USE COMMAND FOR ALL REQUIRED PERL MODULES.
use Socket;
# ----->

use strict;no strict "refs";



#-----------------------------------------------------------------------------
# PLUGIN VARIABLES
#-----------------------------------------------------------------------------
# <-----
# ENTER HERE THE MINIMUM AWSTATS VERSION REQUIRED BY YOUR PLUGIN
# AND THE NAME OF ALL FUNCTIONS THE PLUGIN MANAGE.
my $PluginNeedAWStatsVersion="6.5";
my $PluginHooksFunctions="AddHTMLBodyHeader BuildFullHTMLOutput ShowInfoHost";
# ----->

# <-----
# IF YOUR PLUGIN NEED GLOBAL VARIABLES, THEY MUST BE DECLARED HERE.
use vars qw/
$MAXLINE
/;
# ----->



#-----------------------------------------------------------------------------
# PLUGIN FUNCTION: Init_pluginname
#-----------------------------------------------------------------------------
sub Init_usertracker {
	my $InitParams=shift;
	my $checkversion=&Check_Plugin_Version($PluginNeedAWStatsVersion);

	# <-----
	# ENTER HERE CODE TO DO INIT PLUGIN ACTIONS
	debug(" Plugin usertracker: InitParams=$InitParams",1);

	if ($QueryString =~ /usertracker_maxlines=(\d+)/i) { $MAXLINE=&DecodeEncodedString("$1"); }
	else { $MAXLINE=5000; }

	# ----->

	return ($checkversion?$checkversion:"$PluginHooksFunctions");
}



#-----------------------------------------------------------------------------
# PLUGIN FUNTION: AddHTMLBodyHeader_pluginname
# UNIQUE: NO (Several plugins using this function can be loaded)
# Function called to Add HTML code at beginning of BODY section.
#-----------------------------------------------------------------------------
sub AddHTMLBodyHeader_usertracker {
	# <-----
	# Show form only if option -staticlinks not used
	#if (! $StaticLinks) { &_ShowForm(''); }
	return 1;
	# ----->
}


sub ShowInfoHost_usertracker {
    my $param="$_[0]";
    my $urlparam="pluginmode=usertracker&config=$SiteConfig";
	# <-----
	if ($param eq '__title__') {
		print "<th width=\"40\">Tracker</th>";
	}
	elsif ($param) {
		my $keyforwhois;
		if ($param =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {	# IPv4 address
			$keyforwhois=$param;
		}
		elsif ($param =~ /^[0-9A-F]*:/i) {						# IPv6 address
			$keyforwhois=$param;
		}
		else {	# Hostname
			$param =~ /([-\w]+\.[-\w]+\.(?:au|uk|jp|nz))$/ or $param =~ /([-\w]+\.[-\w]+)$/;
			$keyforwhois=$1;
		}
		print "<td align=\"left\">";
		if ($keyforwhois) { 
			#print "<a href=\"javascript:neww_usertracker('$param')\">$param</a>"; 
			
			print "<a href=\"".XMLEncode("$AWScript?$urlparam&filterusertracker")."=$param\">track</a>"; 
			}
		else { print "&nbsp;" }
		print "</td>";
	}
	else {
		print "<td>&nbsp;</td>";
	}
	return 1;
	# ----->
}


#-----------------------------------------------------------------------------
# PLUGIN FUNTION: BuildFullHTMLOutput_pluginname
# UNIQUE: NO (Several plugins using this function can be loaded)
# Function called to output an HTML page completely built by plugin instead
# of AWStats output
#-----------------------------------------------------------------------------
sub BuildFullHTMLOutput_usertracker {
	# <-----
	my $Filter='';
	if ($QueryString =~ /filterusertracker=([^&]+)/i) { $Filter=&DecodeEncodedString("$1"); }
    	my $regipv4=qr/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
    
	if ($Filter !=~ /$regipv4/o) { 
		$Filter=inet_ntoa(inet_aton($Filter));
	}
	
	# A security check
	if ($QueryString =~ /logfile=/i) { 
	    print "<br />Option logfile is not allowed while building usertracker output.<br />";
        return 0;
	}

	# Show form

	&_ShowForm($Filter);
	#print "sacx : $filterusertracker";
	# Precompiled regex Filter to speed up scan
#	if ($Filter) { $Filter=qr/$Filter/i; }
	if ($Filter) { $Filter=qr/^$Filter?/i; }
	
	print "<hr />\n";
	
	# Show raws
	my $xml=($BuildReportFormat eq 'xhtml');
	open(LOG,"$LogFile") || error("Couldn't open server log file \"$LogFile\" : $!");
	binmode LOG;	# Avoid premature EOF due to log files corrupted with \cZ or bin chars
	my $i=0;
	print "<pre>";
	while (<LOG>) {
		chomp $_; $_ =~ s/\r//;
		if ($Filter && $_ !~ /$Filter/o) { next; }
		print ($xml?XMLEncode("$_"):"$_");
		print "\n";
		if (++$i >= $MAXLINE) { last; }
	}
	print "</pre><br />\n<b>$i lines.</b><br />";
	return 1;
	# ----->
}

sub _ShowForm {
	my $address='';
	my $Filter='';
	if ($QueryString =~ /filterusertracker=([^&]+)/i) {
		$Filter=lc(&DecodeEncodedString("$1"));
	}
	
	my $regipv4=qr/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
	
	if ($Filter !=~ /$regipv4/o) { 
	    $Filter=inet_ntoa(inet_aton($Filter));
	}
#	$Filter=$address
	print "<br />\n";
	print "<form action=\"$AWScript\" style=\"padding: 0px 0px 0px 0px; margin-top: 0\">\n";
	print "<table class=\"aws_border\" border=\"0\" cellpadding=\"2\" cellspacing=\"0\" width=\"100%\">\n";
	print "<tr><td>";
	print "<table class=\"aws_data\" border=\"0\" cellpadding=\"1\" cellspacing=\"0\" width=\"100%\">\n";
	print "<tr><td><span dir=\"ltr\"><b>Show content of file '$LogFile' ($MAXLINE first lines):</b></span></td></tr>\n";
	print "<tr><td>$Message[79]: <input type=\"text\" name=\"filterusertracker\" value=\"$Filter\" /> &nbsp; &nbsp; &nbsp; Max Number of Lines: <input type=\"text\" name=\"usertracker_maxlines\" size=\"5\" value=\"$MAXLINE\" /> &nbsp; &nbsp; &nbsp; <input type=\"submit\" value=\"List\" class=\"aws_button\" />\n";
	print "<input type=\"hidden\" name=\"config\" value=\"$SiteConfig\" /><input type=\"hidden\" name=\"framename\" value=\"$FrameName\" /><input type=\"hidden\" name=\"pluginmode\" value=\"usertracker\" />";
	print "</td></tr>\n";
	print "</table>\n";
	print "</td></tr></table>\n";
	print "</form>\n";
}


1;	# Do not remove this line
