<?

/*
FlexCMS (http://www.flexcms.com)
Flexible, Powerful, Affordable.

Contents of this file are protected by the copyright
notice found at http://www.flexcms.com/flex/pages/copyright.html,
and its usage is subject to the terms of the licensing agreement 
available at http://www.flexcms.com/flex/pages/license.html
*/



	// ========================================================================================
	// main counter script
	// ========================================================================================
	
	$query = "select * from `".$Settings['DBPrefix']."cs-Settings`";
	$result = mysql_query($query) or die (mysql_error());
	while ($row = mysql_fetch_array($result)) {
		$csSettings[$row['Name']] = $row['Value'];
	}
	
	
	if ($csSettings['MaxRecent'] > 0) {
	// randomly clean up the recent details table
	
		$RandomNum = rand(0,20);
		if (number_format($RandomNum,0) == 10) {
							
			$query = "select * from `".$Settings['DBPrefix']."cs-Recent` order by DateCode desc limit ".$csSettings['MaxRecent'].",1";
			$result = mysql_query($query) or die (mysql_error());
			$row = mysql_fetch_array($result);
			
			$query = "delete from `".$Settings['DBPrefix']."cs-Recent` where DateCode<='".$row['DateCode']."'";
			$result = mysql_query($query) or die (mysql_error());
			
			$query = "optimize table `".$Settings['DBPrefix']."cs-Recent`";
			$result = mysql_query($query) or die (mysql_error());
			
		}
			
	}
	
	
	
	if ($LoggedIn == 'y') {
		$UserOrGuest = 'u';
		$UserDetails = $CookieUsername;
	}
	else {
		$UserOrGuest = 'g';
		$UserDetails = $HTTP_SERVER_VARS["REMOTE_ADDR"];
	}
	
	// $Arguments2 is a cache busting random number
	$CurrentPage = $Arguments3;
	$ReferralPage = $Arguments4;
	$ScreenResolution = $Arguments5;
	$ColorDepth = $Arguments6;
	$BrowserName = $Arguments7;
	$BrowserVersion = $Arguments8;
	$OperatingSystem = $Arguments9;
	
	
	
	$DecodedString1 = '';
	for ($i = 0; $i < strlen($CurrentPage); $i=$i+3) {
		
		$CurrentConversion = '';
		
		$CurrentConversion = chr(substr($CurrentPage,$i,3));
		
		$DecodedString1 .= $CurrentConversion;
	
	}
	$CurrentPage = URLDecode($DecodedString1);
	
	
	if (substr($CurrentPage,0,6) != '/count') {
	
		if ($CurrentPage == '' || $CurrentPage == '/') {
			$CurrentPage = '/index';
		}
		
		
		$DecodedString2 = '';
		for ($i = 0; $i < strlen($ReferralPage); $i=$i+3) {
			
			$CurrentConversion = '';
			
			$CurrentConversion = chr(substr($ReferralPage,$i,3));
			
			$DecodedString2 .= $CurrentConversion;
		
		}
		$ReferralPage = URLDecode($DecodedString2);
		
		
		
		// if there's a slash on the end of the url, remove it
		if (substr($ReferralPage,strlen($ReferralPage)-1,1) == '/') {
			$ReferralPage = substr($ReferralPage,0,strlen($ReferralPage)-1);
		}
		
		
		if (substr($ReferralPage,0,strlen($MainURL)) == $MainURL) {
		// if this is an internal link from one page on the site to another, lose the first part as we already know it
			$ReferralPage = substr($ReferralPage,strlen($MainURL));
			
			if ($ReferralPage == '' || $ReferralPage == '/') {
				$ReferralPage = '/index';
			}
			
			if (substr($ReferralPage,(strlen($ReferralPage)-5),5) == '.html') {
			// if there's a .html on the end, lose it too
				$ReferralPage = substr($ReferralPage,0,(strlen($ReferralPage)-5));
			}
		}
		else if (substr($ReferralPage,0,strlen($BaseURL)) == $BaseURL) {
		// if this is an internal link from the main page without the filename
			$ReferralPage = substr($ReferralPage,strlen($BaseURL));
			
			if ($ReferralPage == '' || $ReferralPage == '/') {
				$ReferralPage = '/index';
			}
			
			if (substr($ReferralPage,(strlen($ReferralPage)-5),5) == '.html') {
			// if there's a .html on the end, lose it too
				$ReferralPage = substr($ReferralPage,0,(strlen($ReferralPage)-5));
			}
		} 
		else {
		// additional urls that point to the main flex site
			if ($csSettings['TrackingURLs'] != '') {
				$TrackingURLs = explode('$$$',strtolower($csSettings['TrackingURLs']));
				
				for ($i = 0; $i < count($TrackingURLs); $i++) {
					if ($TrackingURLFound != 'y') {
						if (substr(strtolower($ReferralPage),0,strlen($TrackingURLs[$i])+7) == 'http://'.$TrackingURLs[$i] || substr(strtolower($ReferralPage),0,strlen($TrackingURLs[$i])+11) == 'http://www.'.$TrackingURLs[$i]) {
					
							$ReferralPage = '/index';
							$TrackingURLFound = 'y';
					
						}
					}
				}
				
			}		
		}
					
			
		
		$query = "insert into `".$Settings['DBPrefix']."cs-Recent` (UserOrGuest,UserDetails,CurrentPage,ReferralPage,BrowserName,BrowserVersion,ScreenResolution,ColorDepth,OperatingSystem,Language,DateCode) values ('".$UserOrGuest."','".$UserDetails."','".$CurrentPage."','".$ReferralPage."','".$BrowserName."','".$BrowserVersion."','".$ScreenResolution."','".$ColorDepth."','".$OperatingSystem."','".$Language."','".GetTimestamp()."')";
		$result = mysql_query($query) or die (mysql_error());
	
	
	
		$query = "select * from `".$Settings['DBPrefix']."cs-AggregateData` where Year='".date('Y',GetTimestamp())."' and Month='".date('n',GetTimestamp())."' and Day='".date('j',GetTimestamp())."' limit 1";
		$result = mysql_query($query) or die (mysql_error());
		if (mysql_num_rows($result) > 0) {
		// day exists, just increment the number
		
			$query = "update `".$Settings['DBPrefix']."cs-AggregateData` set PageViews=PageViews+1 where Year='".date('Y',GetTimestamp())."' and Month='".date('n',GetTimestamp())."' and Day='".date('j',GetTimestamp())."' limit 1";
			$result = mysql_query($query) or die (mysql_error());
	
		}
		else {
		// day doesn't exist, so add it
		
			$query = "insert into `".$Settings['DBPrefix']."cs-AggregateData` (Year,Month,Day,PageViews) values ('".date('Y',GetTimestamp())."','".date('n',GetTimestamp())."','".date('j',GetTimestamp())."','1')";
			$result = mysql_query($query) or die (mysql_error());
	
		}
		
		
		header('Location: '.$ImagesURL.'/admin/spacer.gif');
		
	
	} // end if not trying to count '/count'!
	
	
?>
