<?

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



	// ========================================================================================
	// referrals
	// ========================================================================================
	
	$PageTitle = '<img src="'.$ImagesURL.'/admin/admincp_stats.gif" width="24" height="18"> '.$MsgText[4][5];
	$BrowserTitle = $MsgText[4][5];
	$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > <a href="' . $MainURL . '/options.html">'.$MsgText[0][4].'</a> > '.$MsgText[4][5];
	
	if ($LoggedIn == 'y') {
		
		ReadPermissions();
		Global $Permissions;
		
		if ($Permissions[401] == 'y') {
		
			$TodayStartTimestamp = mktime(0,0,0,date('n',GetTimestamp()),date('j',GetTimestamp()),date('Y',GetTimestamp()));
		
			
			$PagesList = '';
			$PageCounts = '';
			$PageAddedToList = '';
			
			//$PagesList[] = $MsgText[4][42];
			//$PageCounts[$MsgText[4][42]] = 1;
			//$PageAddedToList[$MsgText[4][42]] = 'y';
			//$TodayPageViews++;
			
			$PagesBreakdown = '';

			$query = "select * from `".$Settings['DBPrefix']."cs-Recent` where DateCode>='".$TodayStartTimestamp."'";
			$result = mysql_query($query) or die (mysql_error());
			while ($row = mysql_fetch_array($result)) {
						
				if (substr($row['ReferralPage'],0,1) != '/' && $row['ReferralPage'] != '') {
				
					$URLPieces = explode('/',strtolower($row['ReferralPage']));
					
					$row['ReferralPage'] = str_replace('www.','',$URLPieces[2]);
									
					if ($URLPieces[2] != '') {
					
						$TodayPageViews++;
									
						$PageCounts[$row['ReferralPage']]++;
				
						if ($PageAddedToList[$row['ReferralPage']] == '') {
							$PageAddedToList[$row['ReferralPage']] = 'y';
							$PagesList[] = $row['ReferralPage'];
						}
					
					}
					
				} 
							
							
			}
			
			
			if (is_array($PagesList)) {
			
			// sort by title
			for ($k = 0; $k < count($PagesList); $k++) {
				for ($j = 0; $j < (count($PagesList)-1); $j++) {
				
					if ($PagesList[$j] > $PagesList[$j+1]) {
					
						$Swap2 = $PagesList[$j+1];
						
						$PagesList[$j+1] = $PagesList[$j];
						
						$PagesList[$j] = $Swap2;
					
					}
					
				}
			}
			
			// sort by count
			for ($k = 0; $k < count($PagesList); $k++) {
				for ($j = 0; $j < (count($PagesList)-1); $j++) {
				
					if ($PageCounts[$PagesList[$j]] < $PageCounts[$PagesList[$j+1]]) {
					
						$Swap2 = $PagesList[$j+1];
						
						$PagesList[$j+1] = $PagesList[$j];
						
						$PagesList[$j] = $Swap2;
					
					}
					
				}
			}
			
			for ($j = 0; $j < count($PagesList); $j++) {
			
				//if (substr($PagesList[$j],0,4) == 'http') {
				//	$PagesBreakdown .= '<br>'.'<a href="'.$PagesList[$j].'" target="_blank">'.$PagesList[$j].'</a>'.': '.$PageCounts[$PagesList[$j]].' ('.number_format(($PageCounts[$PagesList[$j]]/$TodayPageViews)*100,1).'%)';
				//}
				//else {
					if ($TodayPageViews > 0) {
						$PagesBreakdown .= '<br>'.$PagesList[$j].': '.$PageCounts[$PagesList[$j]].' ('.number_format(($PageCounts[$PagesList[$j]]/$TodayPageViews)*100,1).'%)';
					}
				//}
				
			
			}
			
			}
			
			$PageContents .= '<br><b>('.$MsgText[4][30].')</b><br><br><br><b>'.$MsgText[4][44].':</b>'.$PagesBreakdown;
			
			
			
			
			$PagesList = '';
			$PageCounts = '';
			$PageAddedToList = '';
			
			//$PagesList[] = $MsgText[4][42];
			//$PageCounts[$MsgText[4][42]] = 1;
			//$PageAddedToList[$MsgText[4][42]] = 'y';
			//$YesterdayPageViews++;
			
			$PagesBreakdown = '';

			$query = "select * from `".$Settings['DBPrefix']."cs-Recent` where DateCode>='".($TodayStartTimestamp-(60*60*24))."' and DateCode<'".$TodayStartTimestamp."'";
			$result = mysql_query($query) or die (mysql_error());
			while ($row = mysql_fetch_array($result)) {
			
				if (substr($row['ReferralPage'],0,1) != '/' && $row['ReferralPage'] != '') {
				
					$URLPieces = explode('/',strtolower($row['ReferralPage']));
					
					$row['ReferralPage'] = str_replace('www.','',$URLPieces[2]);
									
					if ($URLPieces[2] != '') {
					
						$YesterdayPageViews++;
									
						$PageCounts[$row['ReferralPage']]++;
				
						if ($PageAddedToList[$row['ReferralPage']] == '') {
							$PageAddedToList[$row['ReferralPage']] = 'y';
							$PagesList[] = $row['ReferralPage'];
						}
					
					}
					
				} 
			
			}
			
			if (is_array($PagesList)) {
			
			// sort by title
			for ($k = 0; $k < count($PagesList); $k++) {
				for ($j = 0; $j < (count($PagesList)-1); $j++) {
				
					if ($PagesList[$j] > $PagesList[$j+1]) {
					
						$Swap2 = $PagesList[$j+1];
						
						$PagesList[$j+1] = $PagesList[$j];
						
						$PagesList[$j] = $Swap2;
					
					}
					
				}
			}
			
			// sort by count
			for ($k = 0; $k < count($PagesList); $k++) {
				for ($j = 0; $j < (count($PagesList)-1); $j++) {
				
					if ($PageCounts[$PagesList[$j]] < $PageCounts[$PagesList[$j+1]]) {
					
						$Swap2 = $PagesList[$j+1];
						
						$PagesList[$j+1] = $PagesList[$j];
						
						$PagesList[$j] = $Swap2;
					
					}
					
				}
			}
			
			for ($j = 0; $j < count($PagesList); $j++) {
			
				//if (substr($PagesList[$j],0,4) == 'http') {
				//	$PagesBreakdown .= '<br>'.'<a href="'.$PagesList[$j].'" target="_blank">'.$PagesList[$j].'</a>'.': '.$PageCounts[$PagesList[$j]].' ('.number_format(($PageCounts[$PagesList[$j]]/$YesterdayPageViews)*100,1).'%)';
				//}
				//else {
				if ($YesterdayPageViews > 0) {
					$PagesBreakdown .= '<br>'.$PagesList[$j].': '.$PageCounts[$PagesList[$j]].' ('.number_format(($PageCounts[$PagesList[$j]]/$YesterdayPageViews)*100,1).'%)';
				}
				//}
			
			}
			
			}
			
			$PageContents .= '<br><br><br><b>'.$MsgText[4][45].':</b>'.$PagesBreakdown;
			
			
			
			
		
			
			$PagesList = '';
			$PageCounts = '';
			$PageAddedToList = '';
			
			//$PagesList[] = $MsgText[4][42];
			//$PageCounts[$MsgText[4][42]] = 1;
			//$PageAddedToList[$MsgText[4][42]] = 'y';
			//$SevenDaysPageViews++;
			
			$PagesBreakdown = '';

			$query = "select * from `".$Settings['DBPrefix']."cs-Recent` where DateCode>='".($TodayStartTimestamp-(60*60*24*6))."'";
			$result = mysql_query($query) or die (mysql_error());
			while ($row = mysql_fetch_array($result)) {
			
				if (substr($row['ReferralPage'],0,1) != '/' && $row['ReferralPage'] != '') {
				
					$URLPieces = explode('/',strtolower($row['ReferralPage']));
					
					$row['ReferralPage'] = str_replace('www.','',$URLPieces[2]);
					
					if ($URLPieces[2] != '') {
					
						$SevenDaysPageViews++;
									
						$PageCounts[$row['ReferralPage']]++;
				
						if ($PageAddedToList[$row['ReferralPage']] == '') {
							$PageAddedToList[$row['ReferralPage']] = 'y';
							$PagesList[] = $row['ReferralPage'];
						}
					
					}
					
				} 
			
			}
			
			if (is_array($PagesList)) {
			
			// sort by title
			for ($k = 0; $k < count($PagesList); $k++) {
				for ($j = 0; $j < (count($PagesList)-1); $j++) {
				
					if ($PagesList[$j] > $PagesList[$j+1]) {
					
						$Swap2 = $PagesList[$j+1];
						
						$PagesList[$j+1] = $PagesList[$j];
						
						$PagesList[$j] = $Swap2;
					
					}
					
				}
			}
			
			// sort by count
			for ($k = 0; $k < count($PagesList); $k++) {
				for ($j = 0; $j < (count($PagesList)-1); $j++) {
				
					if ($PageCounts[$PagesList[$j]] < $PageCounts[$PagesList[$j+1]]) {
					
						$Swap2 = $PagesList[$j+1];
						
						$PagesList[$j+1] = $PagesList[$j];
						
						$PagesList[$j] = $Swap2;
					
					}
					
				}
			}
			
			for ($j = 0; $j < count($PagesList); $j++) {
			
				//if (substr($PagesList[$j],0,4) == 'http') {
				//	$PagesBreakdown .= '<br>'.'<a href="'.$PagesList[$j].'" target="_blank">'.$PagesList[$j].'</a>'.': '.$PageCounts[$PagesList[$j]].' ('.number_format(($PageCounts[$PagesList[$j]]/$SevenDaysPageViews)*100,1).'%)';
				//}
				//else {
				if ($SevenDaysPageViews > 0) {
					$PagesBreakdown .= '<br>'.$PagesList[$j].': '.$PageCounts[$PagesList[$j]].' ('.number_format(($PageCounts[$PagesList[$j]]/$SevenDaysPageViews)*100,1).'%)';
				}
				//}
			
			}
			
			}
			
			
			$PageContents .= '<br><br><br><b>'.$MsgText[4][46].':</b>'.$PagesBreakdown;
			
			
			
			
			


			
		
		} // end if permissions are sufficient
		else {
		
			$PageContents = $MsgText[0][97];
		
		}

	}
	else {
	
		RedirectToLogin();
	
	}

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




?>
