<?php
function objectToArray($d) {
		if (is_object($d)) {
			// Gets the properties of the given object
			// with get_object_vars function
			$d = get_object_vars($d);
		}
 
		if (is_array($d)) {
			/*
			* Return array converted to object
			* Using __FUNCTION__ (Magic constant)
			* for recursive call
			*/
			return array_map(__FUNCTION__, $d);
		}
		else {
			// Return array
			return $d;
		}
}

function analytics() {
		$agent = strtolower($_SERVER['HTTP_USER_AGENT']);

		$known = array
		(
			'Internet Explorer' => 'msie',
			'Firefox' => 'firefox',
			'Firefox' => 'minefield',
			'Thunderbird' => 'thunderbird',
			'Google Chrome' => 'chrome',
			'Apple Safari' => 'safari',
			'Webkit' => 'webkit',
			'Opera' => 'opera',
			'Netscape' => 'netscape',
			'Konqueror' => 'konqueror',
			'Firefox' => 'gecko',
			'Yahoo' => 'yahoo!',
			'Google Bot' => 'googlebot',
			'Uptime Robot' => 'Uptime Robot',
			'Baido Search bot' => 'baidu',
			'unknown'
		);
		$browser_version = 'unknown';
		foreach($known as $name=>$browser)
		{
			$pattern = '#(?<browser>' . $browser . ')[/ ]+(?<version>[0-9/a-z]+(?:\.[0-9/a-z]+)?)#';

			if(preg_match($pattern, $agent, $matches))
			{
				$browser_version = $matches[2];
				if ($browser == 'msie') {
				$browser = $name.' '.$browser_version;
				} else {
				$browser = $name;
				}
				break;
			}
		}
		if($agent == 'Uptime Robot')
		{
			$browser = 'Robot';
			$browser_version = '';
		}
		if($browser == 'unknown')
		{
			return;
		}
		
		$time = time();
		$day = date('j');
		$month = date('n');
		$year = date('Y');
		$iplong = ip2long($_SERVER['REMOTE_ADDR']);
		$insert = mysql_query("INSERT INTO analytics
										(ip,datetime,browser,browser_version,day,month,year)
									VALUES    
										('$iplong','$time','$browser','$browser_version','$day','$month','$year')");
	
}
?>