<?

/*
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
*/



	// ========================================================================================
	// basic stats
	// ========================================================================================
	
	$PageTitle = '<img src="'.$ImagesURL.'/admin/admincp_stats.gif" width="24" height="18"> '.$MsgText[4][6];
	$BrowserTitle = $MsgText[4][6];
	$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > <a href="' . $MainURL . '/options.html">'.$MsgText[0][4].'</a> > '.$MsgText[4][6];
	
	if ($LoggedIn == 'y') {
		
		ReadPermissions();
		Global $Permissions;
		
		if ($Permissions[401] == 'y') {
		
		
			$IPquery = "select * from `".$Settings['DBPrefix']."core-ReverseIPs`";
			$IPresult = mysql_query($IPquery) or die (mysql_error());
			while ($IPdata = mysql_fetch_array($IPresult)) {
				$PastLookups[$IPdata['IPAddress1'].'.'.$IPdata['IPAddress2'].'.'.$IPdata['IPAddress3'].'.'.$IPdata['IPAddress4']] = $IPdata['Hostname'];
			}
	
			function HostnameLookup($IPAddress) {
			
				Global $PastLookups;
				
				if ($PastLookups[$IPAddress] != '') {
					return $PastLookups[$IPAddress];
				}
				else {
					$PastLookups[$IPAddress] = @gethostbyaddr($IPAddress);
					$IPAddressParts = explode('.',$IPAddress);
					
					$IPquery = "select * from `".$Settings['DBPrefix']."core-ReverseIPs` where IPAddress1='".$IPAddressParts[0]."' and IPAddress2='".$IPAddressParts[1]."' and IPAddress3='".$IPAddressParts[2]."' and IPAddress4='".$IPAddressParts[3]."' limit 1";
					$IPresult = mysql_query($IPquery) or die (mysql_error());
					if (mysql_num_rows($IPresult) < 1) {
					
						$IPquery = "insert into `".$Settings['DBPrefix']."core-ReverseIPs` (IPAddress1, IPAddress2, IPAddress3, IPAddress4, Hostname, DateCode) values ('".$IPAddressParts[0]."', '".$IPAddressParts[1]."', '".$IPAddressParts[2]."', '".$IPAddressParts[3]."', '".$PastLookups[$IPAddress]."', '".GetTimestamp()."')";
						$IPresult = mysql_query($IPquery) or die (mysql_error());
						
					}
					
					return $PastLookups[$IPAddress];
				}
			
			}
		
				
			
			$TodayStartTimestamp = mktime(0,0,0,date('n',GetTimestamp()),date('j',GetTimestamp()),date('Y',GetTimestamp()));
			
			
			$UserPageViews = 1;
			$UsersList = '';
			$UserCounts = '';
			$UsersAddedToList = '';
			
			$UsersList[] = $CookieUsername;
			$UserCounts[$CookieUsername] = 1;
			$UsersAddedToList[$CookieUsername] = 'y';
			
			$UsersBreakdown = '';
			
			$query = "select * from `".$Settings['DBPrefix']."cs-Recent` where DateCode>='".$TodayStartTimestamp."' and UserOrGuest='u'";
			$result = mysql_query($query) or die (mysql_error());
			while ($row = mysql_fetch_array($result)) {
				
				$UserPageViews++;
				$UserCounts[$row['UserDetails']]++;
				
				if ($UsersAddedToList[$row['UserDetails']] == '') {
					$UsersAddedToList[$row['UserDetails']] = 'y';
					$UsersList[] = $row['UserDetails'];
				}
			
			}
			
			
			// sort by title
			for ($k = 0; $k < count($UsersList); $k++) {
				for ($j = 0; $j < (count($UsersList)-1); $j++) {
				
					if ($UsersList[$j] > $UsersList[$j+1]) {
					
						$Swap2 = $UsersList[$j+1];
						
						$UsersList[$j+1] = $UsersList[$j];
						
						$UsersList[$j] = $Swap2;
					
					}
					
				}
			}
			
			// sort by count
			for ($k = 0; $k < count($UsersList); $k++) {
				for ($j = 0; $j < (count($UsersList)-1); $j++) {
				
					if ($UserCounts[$UsersList[$j]] < $UserCounts[$UsersList[$j+1]]) {
					
						$Swap2 = $UsersList[$j+1];
						
						$UsersList[$j+1] = $UsersList[$j];
						
						$UsersList[$j] = $Swap2;
					
					}
					
				}
			}
			
			if (is_array($UsersList)) {
				for ($j = 0; $j < count($UsersList); $j++) {
			
					$UsersBreakdown .= '<br>'.$UsersList[$j].': '.$UserCounts[$UsersList[$j]].' ('.number_format(($UserCounts[$UsersList[$j]]/$UserPageViews)*100,1).'%)';
				
				}			
			}
			
			
			
			$GuestPageViews = 0;
			$GuestsList = '';
			$GuestCounts = '';
			$GuestsAddedToList = '';
			$GuestsBreakdown = '';
			
			$query = "select * from `".$Settings['DBPrefix']."cs-Recent` where DateCode>='".$TodayStartTimestamp."' and UserOrGuest='g'";
			$result = mysql_query($query) or die (mysql_error());
			while ($row = mysql_fetch_array($result)) {
				
				$GuestPageViews++;
				$GuestCounts[$row['UserDetails']]++;
				
				if ($GuestsAddedToList[$row['UserDetails']] == '') {
					$GuestsAddedToList[$row['UserDetails']] = 'y';
					$GuestsList[] = $row['UserDetails'];
				}
			
			}
			
			// sort by title
			for ($k = 0; $k < count($GuestsList); $k++) {
				for ($j = 0; $j < (count($GuestsList)-1); $j++) {
				
					if ($GuestsList[$j] > $GuestsList[$j+1]) {
					
						$Swap2 = $GuestsList[$j+1];
						
						$GuestsList[$j+1] = $GuestsList[$j];
						
						$GuestsList[$j] = $Swap2;
					
					}
					
				}
			}
			
			// sort by count
			for ($k = 0; $k < count($GuestsList); $k++) {
				for ($j = 0; $j < (count($GuestsList)-1); $j++) {
				
					if ($GuestCounts[$GuestsList[$j]] < $GuestCounts[$GuestsList[$j+1]]) {
					
						$Swap2 = $GuestsList[$j+1];
						
						$GuestsList[$j+1] = $GuestsList[$j];
						
						$GuestsList[$j] = $Swap2;
					
					}
					
				}
			}
			
			if (is_array($GuestsList)) {
				for ($j = 0; $j < count($GuestsList); $j++) {
			
					$GuestsBreakdown .= '<br>'.$GuestsList[$j].' ('.HostnameLookup($GuestsList[$j]).'): '.$GuestCounts[$GuestsList[$j]].' ('.number_format(($GuestCounts[$GuestsList[$j]]/$GuestPageViews)*100,1).'%)';
			
				}
			}
			
			
			$TodayPageViews = ($GuestPageViews+$UserPageViews);
			
			$PageContents .= '<br><b>(*'.$MsgText[4][59].')</b><br><br><br><b>'.$MsgText[4][60].'*:</b><br>'.$MsgText[4][61].': '.number_format($GuestPageViews,0).' ('.number_format(($GuestPageViews/$TodayPageViews)*100,1).'%)'.'<br>'.$MsgText[4][62].': '.number_format($UserPageViews,0).' ('.number_format(($UserPageViews/$TodayPageViews)*100,1).'%)'.'<br>'.$MsgText[4][63].': '.number_format($TodayPageViews,0).'<br><br>'.$MsgText[4][64].':'.$UsersBreakdown.'<br><br>'.$MsgText[4][65].':'.$GuestsBreakdown;


	
			
			
			
			$UserPageViews = 0;
			$UsersList = '';
			$UserCounts = '';
			$UsersAddedToList = '';
			
			//$UsersList[] = $CookieUsername;
			//$UserCounts[$CookieUsername] = 1;
			//$UsersAddedToList[$CookieUsername] = 'y';
			
			$UsersBreakdown = '';

			$query = "select * from `".$Settings['DBPrefix']."cs-Recent` where DateCode>='".($TodayStartTimestamp-(60*60*24))."' and DateCode<='".$TodayStartTimestamp."' and UserOrGuest='u'";
			$result = mysql_query($query) or die (mysql_error());
			while ($row = mysql_fetch_array($result)) {
				
				$UserPageViews++;
				$UserCounts[$row['UserDetails']]++;
				
				if ($UsersAddedToList[$row['UserDetails']] == '') {
					$UsersAddedToList[$row['UserDetails']] = 'y';
					$UsersList[] = $row['UserDetails'];
				}
			
			}
			
			
			// sort by title
			for ($k = 0; $k < count($UsersList); $k++) {
				for ($j = 0; $j < (count($UsersList)-1); $j++) {
				
					if ($UsersList[$j] > $UsersList[$j+1]) {
					
						$Swap2 = $UsersList[$j+1];
						
						$UsersList[$j+1] = $UsersList[$j];
						
						$UsersList[$j] = $Swap2;
					
					}
					
				}
			}
			
			// sort by count
			for ($k = 0; $k < count($UsersList); $k++) {
				for ($j = 0; $j < (count($UsersList)-1); $j++) {
				
					if ($UserCounts[$UsersList[$j]] < $UserCounts[$UsersList[$j+1]]) {
					
						$Swap2 = $UsersList[$j+1];
						
						$UsersList[$j+1] = $UsersList[$j];
						
						$UsersList[$j] = $Swap2;
					
					}
					
				}
			}
			
			if (is_array($UsersList)) {
				for ($j = 0; $j < count($UsersList); $j++) {
			
					$UsersBreakdown .= '<br>'.$UsersList[$j].': '.$UserCounts[$UsersList[$j]].' ('.number_format(($UserCounts[$UsersList[$j]]/$UserPageViews)*100,1).'%)';
			
				}
			}
			
			
			
			$GuestPageViews = 0;
			$GuestsList = '';
			$GuestCounts = '';
			$GuestsAddedToList = '';
			$GuestsBreakdown = '';
			
			$query = "select * from `".$Settings['DBPrefix']."cs-Recent` where DateCode>='".($TodayStartTimestamp-(60*60*24))."' and DateCode<='".$TodayStartTimestamp."' and UserOrGuest='g'";
			$result = mysql_query($query) or die (mysql_error());
			while ($row = mysql_fetch_array($result)) {
				
				$GuestPageViews++;
				$GuestCounts[$row['UserDetails']]++;
				
				if ($GuestsAddedToList[$row['UserDetails']] == '') {
					$GuestsAddedToList[$row['UserDetails']] = 'y';
					$GuestsList[] = $row['UserDetails'];
				}
			
			}
			
			
			// sort by title
			for ($k = 0; $k < count($GuestsList); $k++) {
				for ($j = 0; $j < (count($GuestsList)-1); $j++) {
				
					if ($GuestsList[$j] > $GuestsList[$j+1]) {
					
						$Swap2 = $GuestsList[$j+1];
						
						$GuestsList[$j+1] = $GuestsList[$j];
						
						$GuestsList[$j] = $Swap2;
					
					}
					
				}
			}
			
			// sort by count
			for ($k = 0; $k < count($GuestsList); $k++) {
				for ($j = 0; $j < (count($GuestsList)-1); $j++) {
				
					if ($GuestCounts[$GuestsList[$j]] < $GuestCounts[$GuestsList[$j+1]]) {
					
						$Swap2 = $GuestsList[$j+1];
						
						$GuestsList[$j+1] = $GuestsList[$j];
						
						$GuestsList[$j] = $Swap2;
					
					}
					
				}
			}
			
			if (is_array($GuestsList)) {
				for ($j = 0; $j < count($GuestsList); $j++) {
			
					$GuestsBreakdown .= '<br>'.$GuestsList[$j].' ('.HostnameLookup($GuestsList[$j]).'): '.$GuestCounts[$GuestsList[$j]].' ('.number_format(($GuestCounts[$GuestsList[$j]]/$GuestPageViews)*100,1).'%)';
			
				}
			}
			
			
			$YesterdayPageViews = ($GuestPageViews+$UserPageViews);
			
			if ($YesterdayPageViews > 0) {
				$GPct = number_format(($GuestPageViews/$YesterdayPageViews)*100,1);
				$UPct = number_format(($UserPageViews/$YesterdayPageViews)*100,1);
			}
			else {
				$GPct = 0;
				$UPct = 0;
			}
			
			$PageContents .= '<br><br><br><b>'.$MsgText[4][66].'*:</b><br>'.$MsgText[4][61].': '.number_format($GuestPageViews,0).' ('.$GPct.'%)'.'<br>'.$MsgText[4][62].': '.number_format($UserPageViews,0).' ('.$UPct.'%)'.'<br>'.$MsgText[4][63].': '.number_format($YesterdayPageViews,0).'<br><br>'.$MsgText[4][64].':'.$UsersBreakdown.'<br><br>'.$MsgText[4][65].':'.$GuestsBreakdown;
			
			
			
			
			
			$UserPageViews = 1;
			$UsersList = '';
			$UserCounts = '';
			$UsersAddedToList = '';
			
			$UsersList[] = $CookieUsername;
			$UserCounts[$CookieUsername] = 1;
			$UsersAddedToList[$CookieUsername] = 'y';
			
			$UsersBreakdown = '';

			$query = "select * from `".$Settings['DBPrefix']."cs-Recent` where DateCode>='".($TodayStartTimestamp-(60*60*24*6))."' and UserOrGuest='u'";
			$result = mysql_query($query) or die (mysql_error());
			while ($row = mysql_fetch_array($result)) {
				
				$UserPageViews++;
				$UserCounts[$row['UserDetails']]++;
				
				if ($UsersAddedToList[$row['UserDetails']] == '') {
					$UsersAddedToList[$row['UserDetails']] = 'y';
					$UsersList[] = $row['UserDetails'];
				}
			
			}
			
			
			// sort by title
			for ($k = 0; $k < count($UsersList); $k++) {
				for ($j = 0; $j < (count($UsersList)-1); $j++) {
				
					if ($UsersList[$j] > $UsersList[$j+1]) {
					
						$Swap2 = $UsersList[$j+1];
						
						$UsersList[$j+1] = $UsersList[$j];
						
						$UsersList[$j] = $Swap2;
					
					}
					
				}
			}
			
			// sort by count
			for ($k = 0; $k < count($UsersList); $k++) {
				for ($j = 0; $j < (count($UsersList)-1); $j++) {
				
					if ($UserCounts[$UsersList[$j]] < $UserCounts[$UsersList[$j+1]]) {
					
						$Swap2 = $UsersList[$j+1];
						
						$UsersList[$j+1] = $UsersList[$j];
						
						$UsersList[$j] = $Swap2;
					
					}
					
				}
			}
			
			if (is_array($UsersList)) {
				for ($j = 0; $j < count($UsersList); $j++) {
			
					$UsersBreakdown .= '<br>'.$UsersList[$j].': '.$UserCounts[$UsersList[$j]].' ('.number_format(($UserCounts[$UsersList[$j]]/$UserPageViews)*100,1).'%)';
			
				}
			}
			
			
			
			$GuestPageViews = 0;
			$GuestsList = '';
			$GuestCounts = '';
			$GuestsAddedToList = '';
			$GuestsBreakdown = '';
			
			$query = "select * from `".$Settings['DBPrefix']."cs-Recent` where DateCode>='".($TodayStartTimestamp-(60*60*24*6))."' and UserOrGuest='g'";
			$result = mysql_query($query) or die (mysql_error());
			while ($row = mysql_fetch_array($result)) {
				
				$GuestPageViews++;
				$GuestCounts[$row['UserDetails']]++;
				
				if ($GuestsAddedToList[$row['UserDetails']] == '') {
					$GuestsAddedToList[$row['UserDetails']] = 'y';
					$GuestsList[] = $row['UserDetails'];
				}
			
			}
			
			
			// sort by title
			for ($k = 0; $k < count($GuestsList); $k++) {
				for ($j = 0; $j < (count($GuestsList)-1); $j++) {
				
					if ($GuestsList[$j] > $GuestsList[$j+1]) {
					
						$Swap2 = $GuestsList[$j+1];
						
						$GuestsList[$j+1] = $GuestsList[$j];
						
						$GuestsList[$j] = $Swap2;
					
					}
					
				}
			}
			
			// sort by count
			for ($k = 0; $k < count($GuestsList); $k++) {
				for ($j = 0; $j < (count($GuestsList)-1); $j++) {
				
					if ($GuestCounts[$GuestsList[$j]] < $GuestCounts[$GuestsList[$j+1]]) {
					
						$Swap2 = $GuestsList[$j+1];
						
						$GuestsList[$j+1] = $GuestsList[$j];
						
						$GuestsList[$j] = $Swap2;
					
					}
					
				}
			}
			
			if (is_array($GuestsList)) {
				for ($j = 0; $j < count($GuestsList); $j++) {
			
					$GuestsBreakdown .= '<br>'.$GuestsList[$j].' ('.HostnameLookup($GuestsList[$j]).'): '.$GuestCounts[$GuestsList[$j]].' ('.number_format(($GuestCounts[$GuestsList[$j]]/$GuestPageViews)*100,1).'%)';
			
				}
			}
			
			
			$SevenDaysPageViews = ($GuestPageViews+$UserPageViews);
			
			$PageContents .= '<br><br><br><b>'.$MsgText[4][67].'*:</b><br>'.$MsgText[4][61].': '.number_format($GuestPageViews,0).' ('.number_format(($GuestPageViews/$SevenDaysPageViews)*100,1).'%)'.'<br>'.$MsgText[4][62].': '.number_format($UserPageViews,0).' ('.number_format(($UserPageViews/$SevenDaysPageViews)*100,1).'%)'.'<br>'.$MsgText[4][63].': '.number_format($SevenDaysPageViews,0).'<br><br>'.$MsgText[4][64].':'.$UsersBreakdown.'<br><br>'.$MsgText[4][65].':'.$GuestsBreakdown;
			
				
		
				
		
		} // end if permissions are sufficient
		else {
		
			$PageContents = $MsgText[0][97];
		
		}

	}
	else {
	
		RedirectToLogin();
	
	}

	// ========================================================================================




?>
