Location: PHPKode > projects > Obsessive Website Statistics > ows/scripts/recreate_logfile.php
#!/usr/bin/php
<?php
/*
	$Id: recreate_logfile.php 107 2007-09-14 16:26:46Z randomperson83 $

	Obsessive Web Statistics
    Copyright (C) 2007 Dustin Spicuzza <hide@address.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 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.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
	
*/

	$base = realpath(dirname(__FILE__) . '/../include/');
	require "$base/base.inc.php";
	require "$base/dimensions.inc.php";
	require "$base/apache_log_parser.php";
	require "$base/plugin.inc.php";
	require "$base/analysis.inc.php";
	require_cli();
	
	if ($argc < 2 || in_array($argv[1], array('--help', '-help', '-h', '-?'))){
	
		echo wordwrap(
"Usage: " . $argv[0] . " website

This script can be used to mostly reconstruct a logfile from the data in the database. Of course, any non-stored data will be lost. This uses the logfile format parameter to determine how to format the line, so the end result should be pretty close to the original logfile, but may vary slightly. However, the resulting output should be good enough to be reimported back into OWS. 
");
		die;
	}
	
	// load plugins first
	if (!load_plugins(''))
		die;
		
	if (($dimensions = compile_dimensions()) === false)
		die;
	
	$website = $argv[1];
	
	
	// first, validate the website
	if (validate_website_table($website)){
		
		// grab the log string, so that we can reformat the data into that
		$options = get_website_options($website);
		$apache_log = new ApacheLogRegex($options['log_format'],true);
	
		// grab the parsed output of the original line, and the regex to subst into.
		$_replace = $apache_log->names();
		
		$pattern = array_fill(0,count($_replace),'/\(.+?\)/e');
		$subject = stripslashes(substr($apache_log->regex(),2,strlen($apache_log->regex()) - 4));
		
		$split = preg_split("/\(.+?\)/",$subject);
		
		// lock DB
		$lock = new AnalysisLock($website);
		if ($lock->Locked())
			die;
		
		// create the query -- TODO: make it so we can filter it out or something
		if ($query = reconstruct_logfile($website)){
	
			$result = db_query($query);
			
			if (!db_is_valid_result($result))
				return show_error("Invalid query was constructed!");
	
			else if (db_has_rows($result)){
			
				
				while ($row = db_fetch_assoc($result)){
			
					reset($split);
					$str = '';
				
					// translate the date and time.. 
					$row['Date'] = array_key_exists('Date',$row) ? date('d/M/Y', strtotime($row['Date'])) : '';
					$row['Time'] = array_key_exists('Time',$row) ? date('H:i:s O', strtotime($row['Time'])) : '';
				
					foreach ($_replace as $v){
						if (array_key_exists($v,$row) && $row[$v] != "")
							$str .= addslashes($row[$v]);
						else
							$str .= '-';
							
						$str .= next($split);
					}
					
					echo "$str\n";	
				}
			}
		}
	}
	
	
?>
Return current item: Obsessive Website Statistics