Shoora
4/21/2019 - 10:24 PM

apache-log-parser

apache-log-parser

<?php
/*
+----------------------------------------------+
|                                              |
|         PHP apache log parser class          |
|                                              |
+----------------------------------------------+
| Filename   : apache-log-parser.php           |
| Created    : 21-Sep-05 23:28 GMT             |
| Created By : Sam Clarke                      |
| Email      : admin@free-webmaster-help.com   |
| Version    : 1.0                             |
|                                              |
+----------------------------------------------+


LICENSE

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License (GPL)
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

To read the license please visit http://www.gnu.org/copyleft/gpl.html

*/

class apache_log_parser
{

  var $bad_rows; // Number of bad rows
  var $fp; // File pointer

  function format_log_line($line)
  {
    preg_match("/^(\S+) (\S+) (\S+) \[([^:]+):(\d+:\d+:\d+) ([^\]]+)\] \"(\S+) (.*?) (\S+)\" (\S+) (\S+) (\".*?\") (\".*?\")$/", $line, $matches); // pattern to format the line
    return $matches;
  }

  function format_line($line)
  {
    $logs = $this->format_log_line($line); // format the line

    if (isset($logs[0])) // check that it formated OK
    {
      $formated_log = array(); // make an array to store the lin info in
      $formated_log['ip'] = $logs[1];
      $formated_log['identity'] = $logs[2];
      $formated_log['user'] = $logs[2];
      $formated_log['date'] = $logs[4];
      $formated_log['time'] = $logs[5];
      $formated_log['timezone'] = $logs[6];
      $formated_log['method'] = $logs[7];
      $formated_log['path'] = $logs[8];
      $formated_log['protocal'] = $logs[9];
      $formated_log['status'] = $logs[10];
      $formated_log['bytes'] = $logs[11];
      $formated_log['referer'] = $logs[12];
      $formated_log['agent'] = $logs[13];
      return $formated_log; // return the array of info
    }
    else
    {
      $this->badRows++; // if the row is not in the right format add it to the bad rows
      return false;
    }
  }

  function open_log_file($file_name)
  {
    $this->fp = fopen($file_name, 'r'); // open the file
    if (!$this->fp)
    {
      return false; // return false on fail
    }
    return true; // return true on sucsess
  }

  function close_log_file()
  {
    return fclose($this->fp); // close the file
  }

  function get_line($line_length=300)
  {
    return fgets($this->fp, $line_length); // true and get a line and return the result
  }

}
?>
<?php
$username = "user";
$password = "secret";

if (!isset($_SERVER['PHP_AUTH_USER']) || ($_SERVER['PHP_AUTH_USER'] != $username &&  $_SERVER['PHP_AUTH_PW'] != $password)) {
    header('WWW-Authenticate: Basic realm="Skitch Access Stats"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'HAHAH DENIED';
    exit;
}
?>
<html>
<head>
  <style type="text/css">  
  <!--  
  table{  
      border-spacing: 0px;  
      border-collapse: collapse;  
      width: 250px;  
  }  
  th {  
      text-align: center;  
      font-weight: bold;  
      padding: 2px;  
      border: 2px solid #FFFFFF;  
      background: #4a70aa;  
      color: #FFFFFF;  
  }  
  td {  
      text-align: center;  
      padding: 2px;  
      border: 2px solid #FFFFFF;  
      background: #e3f0f7;  
  }  

  -->  
  </style>
  </head>
<body>
  <table>
    <tr><th>Filename</th><th>Hits</th></tr>
<?php
// get list of filenames.
$filenames = array();

// create a handler for the directory
$dirname = '/home/lstoll/sites/static.lstoll.net/skitch';
$dirhandler = opendir($dirname);

// keep going until all files in directory have been read
while ($file = readdir($dirhandler)) {

    // if $file isn't this directory or its parent, 
    // add it to the results array
    if ($file != '.' && $file != '..' && $file != 'stats.php' && $file != 'apache-log-parser.php')
      // assoc array. key is filename, value is creation time.
      $filenames[$file] = filectime($dirname.'/'.$file);
}

// tidy up: close the handler
closedir($dirhandler);

// sort the array by numeric value, descending. this gives us newest file first.
arsort($filenames);

// now reset the value to 0 - we will re-use for hit count.

foreach ($filenames as $filename => $create_time) {
  $filenames[$filename] = 0;
}

 
require_once('apache-log-parser.php');

$apache_log_parser = new apache_log_parser(); // Create an apache log parser

$apache_log_parser->open_log_file('/home/lstoll/logs/static.lstoll.net_access.log'); // Make sure it opens the log file

while ($line = $apache_log_parser->get_line()) { // while it can get a line
  $parsed_line = $apache_log_parser->format_line($line); // format the line
  
  // only when the dir is 'skitch'
  if(pathinfo($parsed_line['path'],PATHINFO_DIRNAME) == "/skitch") {
    // parse out the filename component of the URL
    $filename = urldecode(pathinfo($parsed_line['path'], PATHINFO_BASENAME));
    
    // increment the count in the array, if it's in there
    if (array_key_exists($filename, $filenames)) $filenames[$filename]++;
  }
  
  
  
  
}
$apache_log_parser->close_log_file(); // close the log file

foreach($filenames as $filename => $hit_count) {
  echo "<tr><td><a href=\"http://static.lstoll.net/skitch/".urlencode($filename)."\">".$filename."</a></td><td>".$hit_count."</td></tr>";
}






?>
</table>
</body>
</html>