All pastes #76256 Raw Edit

Unnamed

public text v1 · immutable
·published 2006-07-01 09:44 UTC
rendered paste body
#!/usr/bin/php5
<?php
mysql_connect('localhost', '', '');
mysql_select_db('');

if (!isset($_SERVER['argv'][1]))
{
	exit;
}

static $last;
while (true)
{
	$data = str_replace(array("\n", "\r"), '', fread(STDIN, 1024));;
	
	if (md5($data) == $last && !empty($data))
	{
		continue;
	}

	$parse = parse_input($data);

	foreach ($parse as $k=>$v)
	{
		if (is_int($v))
		{
			$parse[$k] = intval($v);
		}
		else 
		{
			$parse[$k] = '\'' . mysql_real_escape_string($v) . '\'';
		}
	}

	$table = 'logs_' . str_replace('.', '', $_SERVER['argv'][1]);	
	mysql_query('INSERT INTO ' . $table . '(' . implode(',', array_keys($parse)) . ') VALUES(' . implode(',', array_values($parse)) . ')') or die (mysql_error());
	
	$last = md5($data);
	//sleep(1);
}

mysql_close();

// functions
function parse_input($input)
{
	$defaults = array('remote_ip', 'remote_log', 'remote_user', 'request_time', 'http_request', 'request_code', 'request_size', 'referer_uri', 'user_agent');
	$matches = array();
	preg_match('#([0-9]{0,3}.[0-9]{0,3}.[0-9]{0,3}.[0-9]{0,3}) (\-|[a-zA-Z0-9\.]) (\-|[a-zA-Z0-9@\.\-]+) \[(.+?)\] "(.+?)" ([0-9]{3}) (-|[0-9]+) "(.+?)" "(.+?)"#', $input, $matches);
	
	array_shift($matches);
	$matches[3] = parse_time($matches[3]);

	$results = array();
	foreach ($matches as $k=>$v)
	{
		$results[$defaults[$k]] = $matches[$k];
	}
	unset($matches);
	
	return $results;
}

function parse_time($string)
{
	$string = preg_replace('#([0-9]{0,2})\/([a-zA-Z]+)\/([0-9]{0,4})\:#', '\2 \1 \3 ', $string);
	return strtotime($string);
}