-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathawstatsDataParser.class.php
166 lines (140 loc) · 3.33 KB
/
awstatsDataParser.class.php
1
<?phpclass awstatsDataParser { function __construct($filename) { $this->filename = $filename; $this->parse(); } public $info = array(); public function parse() { $part = ''; $log = file($this->filename); foreach ($log as $line) { $line = trim($line); $pos = $this->findPart($line, 'BEGIN_'); if ($pos!==false) { $part = $pos; } else { $pos = $this->findPart($line, 'END_'); if ($pos!==false) { $part = ''; } } if ($pos===false) { if (!isset($this->info[$part])) { $this->info[$part] = array(); } switch ($part) { case '': case 'MAP': case 'MISC': break; case 'GENERAL': $this->parseGeneral($line, $part); break; case 'TIME': $this->parseHour($line, $part); break; case 'DAY': $this->parseDay($line, $part); break; case 'VISITOR':// $this->parseVisitor($line, $part); break; case 'DOWNLOADS': $this->parseDownloads($line, $part); break; case 'OS': $this->parseOS($line, $part); break; case 'PAGEREFS': $this->parsePageRefs($line, $part); break; case 'UNKNOWNREFERERBROWSER': case 'UNKNOWNREFERER': case 'BROWSER': $this->parseBrowser($line, $part); break; default: $this->parseDefault($line, $part); } } }}function parseGeneral($line, $part) { $value = explode(' ', $line); $key = array_shift($value); if ($key=='FirstTime' || $key=='LastTime') { $this->info[$part][$key] = $value[0]; }}function parseHour($line, $part) { $value = explode(' ', $line); $key = array_shift($value); $this->info[$part][$key] = array( 'Pages'=> $value[0], 'Hits'=> $value[1], 'Bandwidth'=> $value[2], );}function parseDay($line, $part) { $value = explode(' ', $line); $key = array_shift($value); $this->info[$part][$key] = array( 'Pages'=> $value[0], 'Hits'=> $value[1], 'Bandwidth'=> $value[2], );}function parsePageRefs($line, $part) { $value = explode(' ', $line); $key = array_shift($value); $this->info[$part][$key] = array( 'Pages'=> $value[0], 'Hits'=> $value[1], );}function parseVisitor($line, $part) { $value = explode(' ', $line); $key = array_shift($value); $this->info[$part][$key] = array( 'Pages'=> $value[0], 'Hits'=> $value[1], 'Bandwidth'=> $value[2], );}function parseDownloads($line, $part) { $value = explode(' ', $line); $key = array_shift($value); $this->info[$part][$key] = array( 'Downloads'=> $value[0], 'Hits'=> $value[1], 'Bandwidth'=> $value[2], );}function parseOS($line, $part) { $value = explode(' ', $line); $this->info[$part][$value[0]] = $value[1];}function parseBrowser($line, $part) { $value = explode(' ', $line); if (isset($value[1])) { $this->info[$part][$value[0]] = $value[1]; }}function parseDefault($line, $part) { $value = explode(' ', $line); $key = array_shift($value); $this->info[$part][$key] = $value;} public function findPart($line, $key) { $pos = strpos($line, $key); if ($pos!==false && $pos==0) { $keyLength = strlen($key); $pos2 = strpos($line, ' '); if ($pos2===false) $pos2 = strlen($line); $part = substr($line, strlen($key), $pos2-$keyLength); return $part; } return false;}}