<?

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



	// ========================================================================================
	// sessions
	// ========================================================================================
	
	$PageTitle = '<img src="'.$ImagesURL.'/admin/admincp_stats.gif" width="24" height="18"> Statistics - Sessions Yesterday';
	$BrowserTitle = 'Statistics - Sessions Yesterday';
	$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > <a href="' . $MainURL . '/options.html">'.$MsgText[0][4].'</a> > Statistics - Sessions Yesterday';
	
	if ($LoggedIn == 'y') {
		
		ReadPermissions();
		Global $Permissions;
		
		if ($Permissions[401] == 'y') {
		
			$TodayStartTimestamp = mktime(0,0,0,date('n',GetTimestamp()),date('j',GetTimestamp()),date('Y',GetTimestamp()));
			
			
			$PageContents .= '<table border="0" cellpadding="2" cellspacing="0">';
			
		
			// grab all recent visits data from today and the last four hours of yesterday (60*60*4)
			$query = "select * from `".$Settings['DBPrefix']."cs-Recent` where DateCode>='".($TodayStartTimestamp-(60*60*4)-(60*60*24))."' and DateCode<'".$TodayStartTimestamp."' order by UserOrGuest desc, UserDetails asc, DateCode asc";
			$result = mysql_query($query) or die (mysql_error());
			while ($row = mysql_fetch_array($result)) {
			
				// if a different username or ip, or more than 30 mins later
				if ($row['UserDetails'] != $LastUserDetails || $row['DateCode'] > ($LastDateCode+(60*30))) {
					// new session
					if ($LastDateCode >= $TodayStartTimestamp-(60*60*24)) {
						$PageContents .= $LastSession;
						
						if ($LastPagesCount > 1) {
						// if more than one page was hit this session, calculate the total time spent between the pages
							$SessionLengthCalc = ($LastDateCode-$LastSessionStart);
							if ($SessionLengthCalc < 60) {
								$TotalSessionLength = 'Total Session Time*: '.number_format($SessionLengthCalc,0).' seconds';
							}
							else if ($SessionLengthCalc < 3600){
								$Minutes = 0;
								$Seconds = $SessionLengthCalc;
								while ($Seconds > 59) {
									$Minutes++;
									$Seconds-=60;
								}
								
								$TotalSessionLength = 'Total Session Time*: '.number_format($Minutes,0).' minutes, '.number_format($Seconds,0).' seconds';
							}
							else {
								$Minutes = 0;
								$Seconds = $SessionLengthCalc;
								while ($Seconds > 59) {
									$Minutes++;
									$Seconds-=60;
								}
							
								$Hours = 0;
								while ($Minutes > 59) {
									$Hours++;
									$Minutes-=60;
								}
											
								$TotalSessionLength = 'Total Session Time*: '.number_format($Hours,0).' hours, '.number_format($Minutes,0).' minutes, '.number_format($Seconds,0).' seconds';
							}
						
						
							$PageContents .= '<tr><td colspan="3"><font face="'.$FontFace.'" size="'.$FontSize.'"><br>'.$TotalSessionLength.'</td></tr>';
						}
						
					}
								
									
					if ($row['UserOrGuest'] == 'g') {								
						$LastSession = '</table><br><br><b>'.$row['UserDetails'].'</b> ('.@gethostbyaddr($row['UserDetails']).')<br>Referred From: '.$row['ReferralPage'].'<br><br><table border="0" cellpadding="2" cellspacing="0">';
					}
					else {
						$LastSession = '</table><br><br><b>'.$row['UserDetails'].'</b><br>Referred From: '.$row['ReferralPage'].'<br><br><table border="0" cellpadding="2" cellspacing="0">';
					}

					$LastSessionStart = $row['DateCode'];
					$LastPagesCount = 0;
				}
				
				
				if ($LastUserDetails == $row['UserDetails'] && $row['DateCode'] < ($LastDateCode+(60*30))) {
					$TimeSinceLastAction = ($row['DateCode']-$LastDateCode);
					
					if ($TimeSinceLastAction < 60) {
						$TimeSpent = ' ('.number_format($TimeSinceLastAction,0).' seconds later)';
					}
					else {
						$Minutes = 0;
						$Seconds = $TimeSinceLastAction;
						while ($Seconds > 59) {
							$Minutes++;
							$Seconds-=60;
						}
						
						if ($Seconds < 10) {
							$Seconds = '0'.$Seconds;
						}	
							
						$TimeSpent = ' ('.$Minutes.':'.$Seconds.' later)';
					}
				}
				else {
					$TimeSpent = '';
				}
				
				
				if ($LastPage == $row['CurrentPage'] && $row['DateCode'] < ($LastDateCode+(60*30))) {
					$LastSession .= '<tr><td><font face="'.$FontFace.'" size="'.$FontSize.'">Refreshed '.$row['CurrentPage'].'</td><td><font face="'.$FontFace.'" size="'.$FontSize.'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td><font face="'.$FontFace.'" size="'.$FontSize.'">'.date("g:i:sa",$row['DateCode']).$TimeSpent.'</td></tr>';
				}
				else {
					$LastSession .= '<tr><td><font face="'.$FontFace.'" size="'.$FontSize.'">'.$row['CurrentPage'].'</td><td><font face="'.$FontFace.'" size="'.$FontSize.'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td><font face="'.$FontFace.'" size="'.$FontSize.'">'.date("g:i:sa",$row['DateCode']).$TimeSpent.'</td></tr>';
				}
			
				
				$LastUserDetails = $row['UserDetails'];
				$LastDateCode = $row['DateCode'];
				$LastPage = $row['CurrentPage'];
				$LastPagesCount++;
			
			}
			
			
			$PageContents .= '</table>';	
	
		
		} // end if permissions are sufficient
		else {
		
			$PageContents = 'We\'re sorry, but your current permissions do not give you access to this area.  If you believe this to be in error, please contact the site administrators.';
		
		}

	}
	else {
	
		RedirectToLogin();
	
	}

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




?>
