#!/usr/bin/perl ################################################################### # analyse.cgi Version 1.01 ################################################################### # # Written by Jonathan Cogley # Copyright 1998. THYCOTIC WEBDESIGN # http://www.thycotic.com # E-Mail : info@thycotic.com # # This script may not be sold or distributed in any other form # without the permission of Thycotic WebDesign. # I think the popular expression is : # 'don't try to make money off of my work' # ################################################################### # # This script uses log files from HitMatic and displays them graphically. # It adds the ability to isolate viewing based on groups # # Hitmatic is a logging system written by Joe DePasquale # Hitmatic E-Mail: crypt@getcruising.com # Hitmatic Website: http://www.GetCruising.com # ################################################################### # # If you find the Analyse script useful then please register it! # Registration is only $10 (US dollars) or £10 (UK pounds) # http://www.thycotic.com/scripts # Thanks! # ################################################################### # # Revisions : # 1.1 * Added ability to view todays and yesterdays hits # * Added ability to view agent.log and visitor.log, if they # exist (they are available by using the add-on version of hitcfg.pl # also written by Thycotic WebDesign) # ################################################################### # # Installation: # # 1) Put 'analyse.cgi' in the directory with your hitmatic log # files. (hit1.dat -> hit12.dat and hit.cfg should all be in the hitmatic directory) # # 2) Ensure the first line in analyse.cgi points to Perl on your server # i.e. #!/usr/bin/perl # Use 'whereis perl' to find location of Perl. # # 3) Set the permissions on analyse.cgi to 755 (chmod 755 - makes it executable) # # 4) Try it out! Point your browser at the script ... # Don't forget to include the Hitmatic group after the script to return only # a single group or use 'ALL' to bring back everything in the logs # # e.g. http://www.whatever.com/hitmatic/analyse.cgi?A # OR # e.g. http://www.whatever.com/hitmatic/analyse.cgi?d # OR # e.g. http://www.whatever.com/hitmatic/analyse.cgi?ALL # ################################################################### ##################### ### Configuration ### ##################### ### The URL to the script itself i.e. http://whatever.com/hitmatic/analyse.cgi $SCRIPTURL = "/hitmatic/analyse.cgi"; ### The E-mail address to show when errors occur i.e. yourname\@yourcompany.com ### Dont forget the \ before the @ symbol $SCRIPTOWNER = "you\@whatever.com"; ###Customise body tag and title of pages (optional) $bodytag = ""; $title = "Hits to your website!"; ################################################################### # Thats it!! # DON'T CHANGE ANYTHING BELOW THIS LINE! ################################################################### print "Content-type: text/html\n\n"; print $bodytag; @labels = ('Foo','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'); @daysinMonth = (0,31,28,31,30,31,30,31,31,30,31,30,31); $htmltag = chr(85).chr(110).chr(114).chr(101).chr(103).chr(105).chr(115).chr(116).chr(101).chr(114).chr(101); $htmltag = $htmltag.chr(100).chr(32).chr(118).chr(101).chr(114).chr(115).chr(105).chr(111).chr(110); $incoming = $ENV{'QUERY_STRING'}; #read in argument &parse_form; &get_month; if ($Form{'group'}) { $incoming = $Form{'group'} } #find out how long incoming is $_ = $incoming; $countincoming = tr/!-z//; if ($countincoming != 1 && $incoming ne "ALL") { print "

There has been an error

"; print "Please contact $SCRIPTOWNER to report the fault, describing the situation in which it happened."; &tagline; exit; } print "
"; print "$title\n"; print "

"; if (-e "agent.log") { $AGENT = 1; } if (-e "visitor.log") { $VISITOR = 1; } if (-e "analyse.key"){ open (KEY,"; close (KEY); chomp($key); $string = "Analyse"; $length = length $string; if ($key eq crypt ($string, $length)) { $htmltag = substr($htmltag, 3, (length $htmltag) -3); $htmltag = chr(82).$htmltag; } } if ($Form{'year'}) { &show_year; } elsif ($Form{'month'}) { &show_month; } elsif ($Form{'showmain'}) { $incoming = $Form{'group'}; &show_main_count; } elsif ($Form{'showtoday'}) { &show_today; } elsif ($Form{'yesterday'}) { &show_yesterday; } elsif ($Form{'agent'}) { &show_agent; } elsif ($Form{'visitor'}) { &show_visitor; } else { &show_today; } &show_options; &tagline; ################################################################### sub show_agent { open (FILE, "agent.log"); #load today's file into an array @file = ; close (FILE); @file = sort by_number @file; print "

Agents used

"; print "\n"; print "\n"; print "\n"; print "\n"; print ""; foreach $line (@file) { ($hits, $group, $agent, $lastused) = split (/\|/,$line); if ($agent ne "") { if ($incoming eq $group || $incoming eq "ALL") { print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; } } } print "
AgentLast usedHits
$agent$lastused$hits
"; } ################################################################### sub show_visitor { open (FILE, "visitor.log"); #load today's file into an array @file = ; close (FILE); @file = sort by_number @file; print "

Visitors

"; print "\n"; print "\n"; print "\n"; print "\n"; print ""; foreach $line (@file) { ($hits, $group, $visitor, $lastvisit) = split (/\|/,$line); if ($visitor ne "") { if ($incoming eq $group || $incoming eq "ALL") { print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; } } } print "
VisitorLast visitedHits
$visitor$lastvisit$hits
"; } ################################################################### sub show_today { open (TODAYFILE, "hit1.log"); #load today's file into an array @todayfile = ; close (TODAYFILE); print "

Today\'s Hits

"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print ""; foreach $todayline (@todayfile) { @todayline = split (/\|/,$todayline); $pageline = $todayline[1]; $hostname = $todayline[4]; $agent = $todayline[5]; if ($pageline eq "") { $_ = $todayline; $countline = tr/!-z//; $time = substr($todayline, $countline - 5, 4); } else { $group = substr($pageline, 0, 1); $_ = $pageline; $countpage = tr/!-z//; $page = substr($pageline, 2, $countpage - 2); if ($incoming eq $group || $incoming eq "ALL") { print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; } } } print "
TimePageVisitorBrowser
$time$page$hostname$agent
"; } ################################################################### sub show_yesterday { open (TODAYFILE, "hit2.log"); #load today's file into an array @todayfile = ; close (TODAYFILE); print "

Yesterday\'s Hits

"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print ""; foreach $todayline (@todayfile) { @todayline = split (/\|/,$todayline); $pageline = $todayline[1]; $hostname = $todayline[4]; $agent = $todayline[5]; if ($pageline eq "") { $_ = $todayline; $countline = tr/!-z//; $time = substr($todayline, $countline - 5, 4); } else { $group = substr($pageline, 0, 1); $_ = $pageline; $countpage = tr/!-z//; $page = substr($pageline, 2, $countpage - 2); if ($incoming eq $group || $incoming eq "ALL") { print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; } } } print "
TimePageVisitorBrowser
$time$page$hostname$agent
"; } ################################################################### sub get_month { open (CFGFILE, "hit.cfg"); #load main count file into an array @cfgentries = ; close (CFGFILE); foreach $cfgline (@cfgentries) { @cfglines = split(/\|/,$cfgline); } $currentmonth = $cfglines[1]; } ################################################################### sub show_month { $k = $Form{'monthvalue'}; print "\n"; print "\n"; } print ""; print "\n"; print "\n"; } print "
"; if ($k != ($currentmonth + 1)) { if ($k == 1) { $temp = 12; } else { $temp = $k - 1; } print "
\n"; print "\n"; print "\n"; print "

Month : $labels[$k]

"; if ($k != $currentmonth) { if ($k == 12) { $temp = 1; } else { $temp = $k + 1; } print "
\n"; print "\n"; print "\n"; print "
"; $dataload[1][1] = 0; $pagecount = 1; $monthfile = "hit$k.dat"; open (LOGFILE, "$monthfile"); #load main count file into an array @monthentries = ; close (LOGFILE); #######print "opening $monthfile
\n"; $firstline = 1; foreach $pageline (@monthentries) { #cycle thru each line in current months log @pageline = split(/\|/,$pageline); if ($firstline != 1) { #####refining group and page identifier $group = substr($pageline[0], 0, 1); $_ = $pageline[0]; $countpage = tr/!-z//; $page = substr($pageline[0], 2, $countpage - 2); if ($incoming eq $group || $incoming eq "ALL") { ##### loading daily hits into dataset $dataload[$pagecount][0] = $page; for ($n = 1; $n <= $daysinMonth[$k]; $n++) { $dataload[$pagecount][$n] = $pageline[$n]; } $pagecount++; }# End grouping if } $firstline = 0; } print ""; print ""; for ($n = 1; $n <= $daysinMonth[$k]; $n++) { print "\n"; } print ""; ##### Populate the table with the dataset for ($j = 1; $j < $pagecount; $j++) { print ""; $totalpage = 0; for ($n = 1; $n <= $daysinMonth[$k]; $n++) { print ""; if ($dataload[$j][$n] ne "") { $totalpage += $dataload[$j][$n]; $totalday[$n] += $dataload[$j][$n]; } } print ""; $grandtotal += $totalpage; } #### Do the table totals print ""; for ($n = 1; $n <= $daysinMonth[$k]; $n++) { print ""; } print ""; print "
Page$nHits
$dataload[$j][0]$dataload[$j][$n]$totalpage
Totals$totalday[$n]$grandtotal
"; } ################################################################### sub show_year { print "

Hits for past year

"; $dataload[1][1] = 0; $pagecount = 1; for ($k = 1; $k < 13; $k++) { #cycle thru all the months $monthfile = "hit$k.dat"; open (LOGFILE, "$monthfile"); #load main count file into an array @monthentries = ; close (LOGFILE); #######print "opening $monthfile
\n"; $firstline = 1; foreach $pageline (@monthentries) { #cycle thru each line in current months log @pageline = split(/\|/,$pageline); if ($firstline != 1) { #####refining group and page identifier $group = substr($pageline[0], 0, 1); $_ = $pageline[0]; $countpage = tr/!-z//; $page = substr($pageline[0], 2, $countpage - 2); if ($incoming eq $group || $incoming eq "ALL") { #####determining months hits for this page $monthtotal = 0; for ($n = 1; $n < $daysinMonth[$k]; $n++) { $monthtotal += $pageline[$n]; } $found = 0; $loop = 1; ##test to see if page is already in dataset for ($loop = 1; $loop < $pagecount; $loop++) { if ($page eq $dataload[$loop][1]) { $found = 1; $dataload[$loop][$k+1] += $monthtotal; #if it is add current month total } } ##if it isnt - add it if ($found == 0) { $dataload[$pagecount][1] = $page; $dataload[$pagecount][$k+1] = $monthtotal; #######print "$pagecount $dataload[$pagecount][1] $dataload[$pagecount][$k+1]
"; $pagecount++; } }# End grouping if } $firstline = 0; } } print ""; print ""; $i = $currentmonth + 1; $doneonce = 0; while (($i != $currentmonth + 1) || $doneonce == 0) { print "\n"; if ($i == 12) { $i = 0 } $i++; $doneonce = 1; } print ""; ##### Populate the table with the dataset for ($j = 1; $j < $pagecount; $j++) { print ""; $totalyear = 0; $k = $currentmonth + 1; $doneonce = 0; while (($k != $currentmonth + 1) || $doneonce == 0) { print ""; if ($dataload[$j][$k+1] ne "") { $totalyear += $dataload[$j][$k+1]; $totalmonth[$k+1] += $dataload[$j][$k+1]; } if ($k == 12) { $k = 0 } $k++; $doneonce = 1; } print ""; $grandtotal += $totalyear; } #### Do the table totals print ""; $k = $currentmonth + 1; $doneonce = 0; while (($k != $currentmonth + 1) || $doneonce == 0) { print ""; if ($k == 12) { $k = 0 } $k++; $doneonce = 1; } print ""; print "
Page"; print "
\n"; print "\n"; print "\n"; print "
Hits
$dataload[$j][1]$dataload[$j][$k+1]$totalyear
Totals$totalmonth[$k+1]$grandtotal
"; } sub tagline { print "© 1998. Analyse Script by Thycotic WebDesign"; print "
$htmltag
"; } sub show_main_count { print "

Overall hits per page

"; open (LOGFILE, "hit.cnt"); #load main count file into an array @entries = ; @entries = sort by_number @entries; close (LOGFILE); foreach $line (@entries) { # ($hits,$rawpageline) = split(/\|/,$line); $group = substr($rawpageline, 0, 1); if ($group eq $incoming) { #######only count totals for the group $totalcount = $totalcount + $hits; } if ($incoming eq "ALL") { #######count total for all groups $totalcount = $totalcount + $hits; } } # print the hits to their website print ""; foreach $line (@entries) { #cycle thru the array of referrers ($hits,$rawpageline) = split(/\|/,$line); $group = substr($rawpageline, 0, 1); $_ = $rawpageline; $countrawpageline = tr/!-z//; $pageline = substr($rawpageline, 2, $countrawpageline - 2); if ($incoming eq $group) { #######only do if it is the group $percent = ($hits / $totalcount) * 100; $percent = sprintf("%9.1f",$percent); print ""; } if ($incoming eq "ALL") { #######do stats for all groups $percent = ($hits / $totalcount) * 100; $percent = sprintf("%9.1f",$percent); print ""; } } print ""; print "
Page%Hits
$pageline$percent$hits
$pageline$percent$hits
Total100$totalcount
"; } ################################################################### sub show_options { print "
"; print ""; print "
"; print "
\n"; print "\n"; print "\n"; print "
\n"; print "
"; print "
\n"; print "\n"; print "\n"; print "
\n"; print "
"; print "
\n"; print "\n"; print "\n"; print "\n"; print "
\n"; print "
"; print "
\n"; print "\n"; print "\n"; print "
\n"; print "
"; print "
\n"; print "\n"; print "\n"; print "
\n"; print "
"; if ($AGENT == 1 || $VISITOR == 1) { print ""; } if ($AGENT) { print ""; } if ($VISITOR) { print ""; } if ($AGENT == 1 || $VISITOR == 1) { print "
"; print "
\n"; print "\n"; print "\n"; print "
\n"; print "
"; print "
\n"; print "\n"; print "\n"; print "
\n"; print "
"; } } sub parse_form { read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @cgiPairs = split(/&/,$buffer); foreach $cgiPair (@cgiPairs) { ($name, $value) = split(/=/,$cgiPair); $name =~ s/\+/ /g; $value =~ s/\+/ /g; $name =~ s/%(..)/pack("c",hex($1))/ge; $value =~ s/%(..)/pack("c",hex($1))/ge; $Form{$name} .= "\0" if (defined($Form{$name})); $Form{$name} .= "$value"; } undef $name; undef $value; } sub by_number { if ($a > $b) { -1; } elsif ($a == $b) { 0; } elsif ($a < $b) { 1; } } exit;