(float)$pn) { $ret .= "> "; } elseif ($num < (float)$pn) { $ret .= "< "; } else { $ret .= "= "; } $ret .= $pn; return $ret; } function Browser($str) { if (stripos($str,"Opera") !== FALSE) return "Opera"; elseif (stripos($str,"Firefox") !== FALSE) return "Firefox"; elseif (stripos($str,"Lynx") !== FALSE) return "Lynx"; elseif (stripos($str,"MSIE") !== FALSE) return "MSIE"; elseif (stripos($str,"Chrome") !== FALSE) return "Chrome"; elseif (stripos($str,"rekonq") !== FALSE) return "rekonq"; elseif (stripos($str,"Safari") !== FALSE) return "Safari"; elseif (stripos($str,"Gecko") !== FALSE) return "Gecko-like"; elseif (stripos($str,"WebKit") !== FALSE) return "WebKit-like"; else return "Unknown"; } if (function_exists("imagefilledarc")) { $GD = true; function drawPie($c,$p,$date=false) { global $stime; global $filen; global $sep; $sc = array_sum($c); $xc = array(0x199EFF,0xED19FF,0xFF7A19,0x2BFF19); $xn = "mgaccesslog.".$stime."/"; for ($i=1;$i<=12;$i++) { $xn .= chr(mt_rand(97,122)); } asort($c); $im = imagecreatetruecolor(600,300); imagefilledellipse($im,150,150,281,281,0x8CCEFF); imagefill($im,0,0,pow(256,3)-1); imagestring($im,2,5,5,"Top 4:",0); imagestring($im,2,5,19,"Pie Chart",0); $lp = 0; foreach ($c as $i=>$v) { $color = (isset($xc[$i])) ? $xc[$i] : 0; imagefilledarc($im,150,150,280,280,$lp,$lp+(($v/$sc)*360),$color,IMG_ARC_PIE); $lp += ($v/$sc)*360; } foreach ($c as $i=>$v) { $y1 = 80+(30*$i); $onto = (strpos($p[$i],$sep) !== FALSE) ? strpos($p[$i],$sep) : 50; if ($date) { $caption = date("d M Y",(int)substr($p[$i],0,$onto)); } else { $caption = (isset($p[$i])) ? substr($p[$i],0,$onto) : "Others"; } $color = (isset($xc[$i])) ? $xc[$i] : 0; imagefilledrectangle($im,320,$y1,330,$y1+10,$color); imagestring($im,2,335,$y1-2,utf8_decode($caption),0); } imagepng($im,$xn.".png"); imagedestroy($im); return $xn; } function drawBar($cp,$fs=4,$date=false) { global $stime; ksort($cp); $xn = "mgaccesslog.".$stime."/"; for ($i=1;$i<=12;$i++) { $xn .= chr(mt_rand(97,122)); } $im = imagecreatetruecolor(20+(count($cp)*32)+20,360); imagefill($im,0,0,16777215); $xcol = floor(255/(count($cp)+8)); $i = 0; $s = max($cp); imageline($im,20,18,20+(count($cp)*32),18,0); imagestring($im,4,20+(array_search($s,array_values($cp))*32),2,$s,0x333333); foreach ($cp as $c=>$p) { $pos = 20+($i*32); imagefilledrectangle($im,$pos,330,$pos+22,330-(($p/$s)*310),imagecolorallocate($im,$xcol*$i,$xcol*$i,255)); $cap = (!$date) ? str_repeat("0",2-strlen((string)$c)).$c : date("dM",$c); $dist = (!$date) ? 3 : -1; imagestring($im,$fs,$pos+$dist,335,$cap,0); $i++; } imagepng($im,$xn.".png"); return $xn; } } function STOP() { global $a; if (!isset($a)) $a = microtime(true); else return (string)(microtime(true)-$a); } function checkifgz($file) { $fp = fopen($file,"rb"); $cod = fread($fp,3); fclose($fp); if ($cod == chr(0x1F).chr(0x8B).chr(0x08)) { $hresult = fopen("data://text/plain;base64,".base64_encode(implode('',gzfile($file))),"r"); } else { $hresult = fopen($file,"r"); } return $hresult; } function NotFalse($arg1,$arg2) { if ($arg1 === FALSE) { return $arg2; } else { return $arg1; } } function GetInput($EVE,$EVS) { global $obstarted; global $config; if ($obstarted) return false; if (empty($config)) { echo $EVE; $xt = trim(fgets(STDIN)); } else { $xt = $config["mgaccesslog"][$EVS]; } if (strtolower($xt) == "y") { return false; } elseif (strtolower($xt) == "n") { return true; } elseif ($xt == "") { return false; } elseif (is_numeric($xt)) { return (int)$xt; } else { return $xt; } } if (empty($argv[1]) && isset($_GET["log"])) { $obstarted = true; ob_start(); $filen = $_GET["log"]; } else { $filen = $argv[1]; } $config = array(); if ($obstarted) { $argv[2] = $_GET["config"]; } if (isset($argv[2]) && file_exists($argv[2])) { $config = parse_ini_file($argv[2],true); } echo "+-+-+-+-+-+-+-+-+-+-+-+ |m|g|a|c|c|e|s|s|l|o|g| 2.0 +-+-+-+-+-+-+-+-+-+-+-+ by Max R. P. Großmann mgaccesslog.1e5b.de\n\n"; if (empty($filen)) { fputs(STDERR,"Well, what's the filename?\n"); exit; } if (!file_exists($filen)) { fputs(STDERR,"File does not exist\n"); exit; } if (!($file = @checkifgz($filen))) { fputs(STDERR,"File cannot be opened\n"); exit; } foreach (glob("mgaccesslog.*/*.png") as $sf) { unlink($sf); } foreach (glob("mgaccesslog.*/") as $sf) { if (is_dir($sf)) rmdir($sf); foreach (glob("*.".substr($sf,12,10).".html") as $ff) { unlink($ff); } } $fromd = getInput("From (valid English date!) [*]: ","From"); $fromd = (strtotime($fromd) !== FALSE) ? strtotime($fromd) : 0; $tod = getInput("To (valid English date!) [Now]: ","To"); $tod = (strtotime($tod) !== FALSE) ? strtotime($tod) : time(); if ($GD) { $GD = (bool)!GetInput("Do you want nice pie charts? (Y/n) [Y]: ","PieCharts"); } $users = array(); $acc = array(); $ccunt = array("HEAD"=>0,"GET"=>0,"POST"=>0,"PUT"=>0,"PROPFIND"=>0, "DELETE"=>0,"TRACE"=>0,"OPTIONS"=>0,"CONNECT"=>0, "PATCH"=>0); $codes = array(); $httpv = array("HTTP/1.0"=>0,"HTTP/1.1"=>0); $pages = array(); $count = array("browser"=>array(),"bots"=>array()); $str = "mgaccesslog—".$filen."

".date("r").": ".$filen."

"; $counted = array(); $refes = array(); $hours = array(); $search = array(); $reals = array(); $traffic = 0; $lines = 0; $tline = substr_count(file_get_contents($filen),"\n"); $sep = ""; for ($siu=0;$siu<=32;$siu++) { $sep .= chr(mt_rand(1,33)); } $sep = md5(crypt($sep).date("r")); mkdir("mgaccesslog.".$stime); $str .= "

UID: ".$sep."

"; STOP(); while (!feof($file)) { $fu = fgetcsv($file,1024*1024," "); if (count($fu) == 0 || !is_array($fu)) continue; $fdd = strtotime(str_replace("/"," ",substr($fu[3],1,11))); if (($fdd >= $fromd) && ($fdd <= $tod)) { $lines++; unset($fu[1],$fu[2],$fu[4]); $fu = array_values($fu); $fu[1] = substr($fu[1],1); $uid = $fu[0].$sep.$fu[6]; $method = strtoupper(strstr($fu[2]," ",true)); $ccunt[$method]++; if (strpos($fu[6],"http:") === FALSE && strpos($fu[6],"@") === FALSE && strpos(strtolower($fu[6]),"bot") === FALSE && strpos(strtolower($fu[6]),"crawl") === FALSE && strpos(strtolower($fu[6]),"spider") === FALSE && strpos(strtolower($fu[6]),"internal dummy connection") === FALSE && strpos(strtolower($fu[6]),"twitterfeed") === FALSE) { $xbro = Browser($fu[6]); if (isset($count["browser"][$xbro])) $count["browser"][$xbro]++; else $count["browser"][$xbro] = 1; if (!isset($counted[$fdd][$uid])) { if (isset($acc[$fdd])) $acc[$fdd]++; else $acc[$fdd] = 1; $counted[$fdd][$uid] = true; $hour = (int)substr($fu[1],12,2); if (isset($hours[$hour])) $hours[$hour]++; else $hours[$hour] = 1; } if (isset($users[$uid])) $users[$uid]++; else $users[$uid] = 1; } else { if (isset($count["bots"][$fdd][$fu[6]])) $count["bots"][$fdd][$fu[6]]++; else $count["bots"][$fdd][$fu[6]] = 1; } if (isset($codes[$fu[3]])) $codes[$fu[3]]++; else $codes[$fu[3]] = 1; $httpv[trim(strrchr($fu[2]," "))]++; $fu[7] = (!empty($fu[7])) ? "http://".$fu[7] : ""; $spag = $fu[7].substr($fu[2],strpos($fu[2]," ")+1,-9); if (isset($pages[$spag])) $pages[$spag]++; else $pages[$spag] = 1; $traffic += $fu[4]; if (isset($refes[$fu[5]])) $refes[$fu[5]]++; else $refes[$fu[5]] = 1; if (strlen($fu[5]) > 2) { $pul = @parse_url($fu[5],PHP_URL_HOST); $fo = $fu[5]; $fu[5] = str_replace(array("aq","oq"),"xx",$fu[5]); if (strpos($fu[5],"q=") !== FALSE || (strpos($fu[5],"p=") !== FALSE && strpos($fu[5],"showthread.php") === FALSE)) { $ppos = NotFalse(strpos($fu[5],"q="),strpos($fu[5],"p=")); $xpos = explode("&",substr($fu[5],$ppos)); $see = urldecode(substr($xpos[0],2)).$sep.$pul.$sep.$spag; $reals[$see] = $fo; if (isset($search[$see])) $search[$see]++; else $search[$see] = 1; } } } } $timed = STOP(); unset($refes["-"]); asort($count["browser"]); asort($ccunt); asort($users); asort($codes); asort($httpv); asort($pages); asort($refes); asort($search); asort($hours); foreach ($count["bots"] as $d=>$cc) { asort($count["bots"][$d]); $count["bots"][$d] = array_reverse($count["bots"][$d],true); } $count["browser"] = array_reverse($count["browser"],true); $users = array_reverse($users,true); $ccunt = array_reverse($ccunt,true); $codes = array_reverse($codes,true); $httpv = array_reverse($httpv,true); $pages = array_reverse($pages,true); $refes = array_reverse($refes,true); $search = array_reverse($search,true); $hours = array_reverse($hours,true); $str .= "

Table of Contents

"; $str .= ""; $str .= "

General Information

"; $str .= ""; $str .= ""; $str .= ""; $str .= ""; $str .= ""; $str .= "
NameValue
From".date("r",$fromd)."
To".date("r",$tod)."
Traffic".number_format($traffic/1024)." KiB
"; if (!getInput("\nDo you need information about HTTP Methods? (Y/n) [Y]: ","Methods")) { $ccsum = array_sum($ccunt); $str .= "

Methods

"; if ($GD) { $str .= "Pie chart"; } $str .= ""; $str .= ""; foreach ($ccunt as $meth=>$c) { if ($c == 0) continue; $str .= ""; $str .= ""; $str .= ""; } $str .= ""; $str .= "
MethodCountsPercentage
".$meth."".$c."".nround(($c/$ccsum)*100,2)."%
Sum Σ".$ccsum."
"; } if (!getInput("Do you need information about Search Terms? (Y/n) [Y]: ","Search")) { $sesum = array_sum($search); $str .= "

Searches

"; if ($GD) { $str .= "Pie chart"; } $str .= ""; $str .= ""; foreach ($search as $meth=>$c) { if ($c == 0) continue; $metr = explode($sep,$meth); $str .= ""; $str .= ""; $str .= ""; } $str .= ""; $str .= "
Search Term (Entry Page)EngineCountsPercentage
".htmlentities(utf8_decode($metr[0]))."".trim($metr[1])."".$c."".nround(($c/$sesum)*100,2)."%
Sum Σ".$sesum."
"; } if (!getInput("Do you need information about the Most Visited Pages? (Y/n) [Y]: ","MostVisited")) { $hman = NotFalse(getInput("How many? [30]: ","MostVisitedN"),30); $pasum = array_sum($pages); $pages = array_slice($pages,0,$hman,true); $str .= "

Most Visited Pages – Top ".$hman."

"; if ($GD) { $str .= "Pie chart"; } $str .= ""; $str .= ""; foreach ($pages as $meth=>$c) { if ($c == 0) continue; $str .= ""; $str .= ""; $str .= ""; } $str .= ""; $str .= "
PageCountsPercentage
".$meth."".$c."".nround(($c/$ccsum)*100,2)."%
Sum Σ".$pasum."
"; } if (!getInput("Do you need information about HTTP Versions? (Y/n) [Y]: ","Versions")) { $htsum = array_sum($httpv); $str .= "

HTTP Versions

"; if ($GD) { $str .= "Pie chart"; } $str .= ""; $str .= ""; foreach ($httpv as $meth=>$c) { $str .= ""; $str .= ""; $str .= ""; } $str .= ""; $str .= "
NameCountsPercentage
".$meth."".$c."".nround(($c/$htsum)*100,2)."%
Sum Σ".$htsum."
"; } if (!getInput("Do you need information about the Number of Real Visitors? (Y/n) [Y]: ","Visitors")) { $ddsum = array_sum($acc); $str .= "

Real Visitors per Date

"; if ($GD) { $str .= "Pie chart"; } $str .= ""; $str .= ""; $days = 0; foreach ($acc as $meth=>$c) { $str .= ""; $tod = ($meth == mktime(0,0,0)) ? "*" : ""; $str .= ""; $str .= ""; $days++; } $str .= ""; $str .= ""; $str .= "
DateVisitorsPercentage
".date("d M Y",$meth).$tod."".$c."".nround(($c/$ddsum)*100,2)."%
Sum Σ".$ddsum."
∅ per day".nround($ddsum/$days,1)."
"; } if (!getInput("Do you need information about the Number of Real Visitors per Hour? (Y/n) [Y]: ","VisitorsHour")) { $hosum = array_sum($hours); $str .= "

Real Visitors per Hour

"; if ($GD) { $str .= "Pie chart"; } $str .= ""; $str .= ""; $days = 0; foreach ($hours as $meth=>$c) { $meth = (string)$meth; $str .= ""; $str .= ""; $str .= ""; $days++; } $str .= ""; $str .= ""; $str .= "
HourVisitorsPercentage
".str_repeat("0",2-strlen($meth)).$meth.":XX".$c."".nround(($c/$hosum)*100,2)."%
Sum Σ".$ddsum."
∅ per hour".nround($ddsum/$days,1)."
"; } if (!getInput("Do you need information about Referers? (Y/n) [Y]: ","Referers")) { $rfsum = array_sum($refes); $refes = array_slice($refes,0,35,true); $str .= "

Referers – Top 35

"; if ($GD) { $str .= "Pie chart"; } $str .= ""; $str .= ""; foreach ($refes as $meth=>$c) { $str .= ""; $str .= ""; $str .= ""; } $str .= ""; $str .= "
URLCountsPercentage
".$meth."".$c."".nround(($c/$htsum)*100,2)."%
Sum Σ".$rfsum."
"; } if (!getInput("Do you need information about HTTP Status Codes? (Y/n) [Y]: ","StatusCodes")) { $ersum = array_sum($codes); $str .= "

HTTP Status Codes

"; if ($GD) { $str .= "Pie chart"; } $str .= ""; $str .= ""; foreach ($codes as $meth=>$c) { $ucn = (substr((string)$meth,0,1) == "4" || substr((string)$meth,0,1) == "5") ? " style='background:red'" : ""; $str .= ""; $str .= ""; $str .= ""; } $str .= ""; $str .= "
CodeCountsPercentage
".$meth."".$c."".nround(($c/$ersum)*100,2)."%
Sum Σ".$ersum."
"; } if (!getInput("Do you need information about the Eagerest Visitors? (Y/n) [Y]: ","Eagerest")) { $hman = NotFalse(getInput("How many? [25]: ","EagerestN"),25); $uusum = array_sum($users); $users = array_slice($users,0,$hman,true); $str .= "

Eagerest Visitors – Top ".$hman."

"; if ($GD) { $str .= "Pie chart"; } $str .= ""; $str .= ""; foreach ($users as $meth=>$c) { $str .= ""; $fux = explode($sep,$meth); $str .= ""; $str .= ""; } $str .= "
IPUser AgentCountsPercentage
".$fux[0]."".$fux[1]."".$c."".nround(($c/$uusum)*100,2)."%
"; } if (!getInput("Do you need information about User Agents? (Y/n) [Y]: ","UserAgents")) { $hman = 10; $cbsum = array_sum($count["browser"]); $count["browser"] = array_slice($count["browser"],0,$hman,true); $str .= "

User Agents

"; if ($GD) { $str .= "Pie chart"; } $str .= ""; $str .= ""; foreach ($count["browser"] as $meth=>$c) { $str .= ""; $str .= ""; $str .= ""; } $str .= "
User AgentCountsPercentage
".$meth."".$c."".nround(($c/$cbsum)*100,2)."%
"; } if (!getInput("Do you need information about Bots? (Y/n) [Y]: ","Bots")) { $hman = NotFalse(getInput("How many? [7]: ","BotsN"),7); $str .= "

Bots – Top ".$hman."

"; foreach ($count["bots"] as $d=>$fun) { $str .= "

".date("d M Y",$d)."

"; $str .= ""; $bbsum = array_sum($fun); $fun = array_slice($fun,0,$hman,true); $str .= ""; foreach ($fun as $meth=>$c) { $str .= ""; $str .= ""; $str .= ""; } $str .= "
BotCountsPercentage
".$meth."".$c."".nround(($c/$bbsum)*100,2)."%
"; } } $str .= "

Generated by mgaccesslog 2.0.

"; if (!$obstarted) { if (!file_put_contents($filen.".html",$str)) { fputs(STDERR,"Unable to write in file\n"); exit; } } echo "\n"; echo $timed." s\n"; echo $lines." lines\n"; echo round($lines/$timed)." lines/s\n\n"; echo " -> ".$filen.".html\n"; if ($obstarted) { ob_clean(); echo $str; ob_end_flush(); } ?>