<?

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



	// ========================================================================================
	// stats - pageviews graphs
	// ========================================================================================
	
	$PageTitle = '<img src="'.$ImagesURL.'/admin/admincp_stats.gif" width="24" height="18"> '.$MsgText[4][3];
	$BrowserTitle = $MsgText[4][3];
	$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > <a href="' . $MainURL . '/options.html">'.$MsgText[0][4].'</a> > '.$MsgText[4][3];
	
	if ($LoggedIn == 'y') {
		
		ReadPermissions();
		Global $Permissions;
		
		if ($Permissions[401] == 'y') {
		


			$CurrentYear = date('Y',GetTimestamp());
			$CurrentMonth = date('n',GetTimestamp());
			$CurrentDay = date('j',GetTimestamp());

			$CurrentHour = date('G',GetTimestamp());
			$CurrentMinute = date('j',GetTimestamp());
			$CurrentSecond = date('s',GetTimestamp());

						
			

			$RDec = hexdec(substr($Colors['BG'],1,2));
			$GDec = hexdec(substr($Colors['BG'],3,2));
			$BDec = hexdec(substr($Colors['BG'],5,2));

			if ($RDec >= 128) {
				$RHex = sprintf("%02X",intval($RDec*0.9));
			}
			else {
				$RHex = sprintf("%02X",intval($RDec*1.1));
			}
			
			if ($GDec >= 128) {
				$GHex = sprintf("%02X",intval($GDec*0.9));
			}
			else {
				$GHex = sprintf("%02X",intval($GDec*1.1));
			}
			
			if ($BDec >= 128) {
				$BHex = sprintf("%02X",intval($BDec*0.9));
			}
			else {
				$BHex = sprintf("%02X",intval($BDec*1.1));
			}

			$AccentBG = '#'.$RHex.$GHex.$BHex;



			$RDec = hexdec(substr($Colors['Text'],1,2));
			$GDec = hexdec(substr($Colors['Text'],3,2));
			$BDec = hexdec(substr($Colors['Text'],5,2));


			if ($RDec >= 128) {
				$RHex = sprintf("%02X",intval($RDec*0.6));
			}
			else {
				$RHex = sprintf("%02X",intval($RDec*1.4));
			}
			
			if ($GDec >= 128) {
				$GHex = sprintf("%02X",intval($GDec*0.6));
			}
			else {
				$GHex = sprintf("%02X",intval($GDec*1.4));
			}
			
			if ($BDec >= 128) {
				$BHex = sprintf("%02X",intval($BDec*0.6));
			}
			else {
				$BHex = sprintf("%02X",intval($BDec*1.4));
			}

			$AccentText = '#'.$RHex.$GHex.$BHex;





			
			$PageContents .= '<br><b>'.$MsgText[4][34].':</b><br><br>';

			
			$Dates = '';
			$Counts = '';
			$TotalCount = '';
			
			$LargestFound = -1;
			
			$Days = 30;
						
			for ($x = 0; $x < $Days; $x++) {
			
				//$Timestamp = (GetTimestamp() - (60*60*24*($Days-1)) + (60*60*24*$x)) + (60*60);
				$Timestamp = (GetTimestamp() - (60*60*24*($Days-1)) + (60*60*24*$x));
				
				$Year = date('Y',$Timestamp);
				$Month = date('n',$Timestamp);
				$Day = date('j',$Timestamp);
			
			
				$query = "select * from `".$Settings['DBPrefix']."cs-AggregateData` where Year='".$Year."' and Month='".$Month."' and Day='".$Day."' limit 1";
				$result = mysql_query($query) or die (mysql_error());
				$row = mysql_fetch_array($result);
				
				$Dates[] = date('M j', mktime(0,0,0,$Month,$Day,$Year));
				$NumberDates[] = date('j', mktime(0,0,0,$Month,$Day,$Year));
				$WDays[] = date('w', mktime(0,0,0,$Month,$Day,$Year));
				$Counts[] = $row['PageViews'];
				
				
				$ProjectionCount = 0;
				if ($Year == $CurrentYear && $Month == $CurrentMonth && $Day == $CurrentDay && $CurrentHour > 8 && $row['PageViews'] > 0) {
				
					$DayEndTimestamp = (60*60*24);
										
					$CurrentTimestamp = (($CurrentHour*60*60)+($CurrentMinute*60)+$CurrentSecond);
					
					$TimeCompleted = ($CurrentTimestamp/$DayEndTimestamp);
					
					//print 'M Start: '.$MonthStartTimestamp.'<br>M End: '.$MonthEndTimestamp.'<br><br>C Timestamp: '.$CurrentTimestamp.'<br><br>Time Completed: '.$TimeCompleted;
					
					$ProjectionCount = ($row['PageViews']/$TimeCompleted);
				
				}
				
				
				if ($ProjectionCount > 0) {
					if ($ProjectionCount > $LargestFound) {
						$LargestFound = $ProjectionCount;
					}
				}				
				else if ($row['PageViews'] > $LargestFound) {
					$LargestFound = $row['PageViews'];
				}

								
				$TotalCount += $row['PageViews'];
							
			}
			
			
			
			$PageContents .= '<table border="0" cellpadding="2" cellspacing="0" border="0"><tr><td width="20">&nbsp;</td><td><table border="0" cellpadding="2" cellspacing="0" border="0"><tr>
			      <td align="center"><font face="'.$FontFace.'" size="'.($FontSize-1).'"><b>'.$MsgText[4][35].'</b></td>
			      <td align="center"><font face="'.$FontFace.'" size="'.($FontSize-1).'">&nbsp;&nbsp;<b>'.$MsgText[4][28].'</b>&nbsp;&nbsp;</td>
			      <td width="300"><font face="'.$FontFace.'" size="'.($FontSize-1).'"><b>'.$MsgText[4][29].'</b></td>
    			</tr>';
			
			$SizeMultiplier = (100/(($LargestFound/$TotalCount)*100));
						
			
			for ($x = 0; $x < $Days; $x++) {

				if ($WDays[$x] == 0 || $WDays[$x] == 6) {
					$RowBG = $AccentBG;
				}
				else {
					$RowBG = $Colors['BG'];
				}


				//print $x.'-'.$Dates[$x].'<br>';				

				if ($NumberDates[$x] == $CurrentDay && $x > 20 && $ProjectionCount > 0) {

					$ThisPercent = ($Counts[$x]/$TotalCount);
					$ThisWidth = number_format($ThisPercent * 250 * $SizeMultiplier,0);
					
					$ProjectionPercent = ($ProjectionCount/$TotalCount);
					$ProjectionWidth = (number_format($ProjectionPercent * 250 * $SizeMultiplier,0)-$ThisWidth-1);


					$PageContents .= '<tr>
			      	<td align="center" bgcolor="'.$RowBG.'"><font face="'.$FontFace.'" size="'.$FontSize.'">&nbsp;&nbsp;' . $Dates[$x] . '&nbsp;&nbsp;</td>
				      <td align="center" bgcolor="'.$RowBG.'"><font face="'.$FontFace.'" size="'.$FontSize.'">' . number_format($Counts[$x],0) . '<br>&nbsp;&nbsp;&nbsp;(' . number_format($ProjectionCount,0) . ')&nbsp;&nbsp;&nbsp;</td>
				      <td width="300" bgcolor="'.$RowBG.'"><font face="'.$FontFace.'" size="'.$FontSize.'"><table width="'.($ThisWidth+$ProjectionWidth).'" height="8" border="0" cellpadding="0" cellspacing="0" border="0"><tr><td width="'.$ThisWidth.'" bgcolor="'.$Colors['Text'].'"><img src="'.$ImagesURL.'/admin/spacer.gif" width="1" height="1" border="0" alt=""></td><td width="1" bgcolor="'.$Colors['BG'].'"><img src="'.$ImagesURL.'/admin/spacer.gif" width="1" height="1" border="0" alt=""></td><td width="'.$ProjectionWidth.'" bgcolor="'.$AccentText.'"><img src="'.$ImagesURL.'/admin/spacer.gif" width="1" height="1" border="0" alt=""></td></tr></table></td>
    					</tr>';


				}
				else {
			
					$ThisPercent = ($Counts[$x]/$TotalCount);
					$ThisWidth = number_format($ThisPercent * 250 * $SizeMultiplier,0); 
				
					$PageContents .= '<tr>
			      	<td align="center" bgcolor="'.$RowBG.'"><font face="'.$FontFace.'" size="'.$FontSize.'">&nbsp;&nbsp;' . $Dates[$x] . '&nbsp;&nbsp;</td>
				      <td align="center" bgcolor="'.$RowBG.'"><font face="'.$FontFace.'" size="'.$FontSize.'">&nbsp;&nbsp;&nbsp;' . number_format($Counts[$x],0) . '&nbsp;&nbsp;&nbsp;</td>
				      <td width="300" bgcolor="'.$RowBG.'"><font face="'.$FontFace.'" size="'.$FontSize.'"><table width="'.$ThisWidth.'" height="8" border="0" cellpadding="0" cellspacing="0" border="0"><tr><td bgcolor="'.$Colors['Text'].'"><img src="'.$ImagesURL.'/admin/spacer.gif" width="1" height="1" border="0" alt=""></td></tr></table></td>
    					</tr>';

				}
			
			}
			
			$PageContents .= '</table></td></tr></table>';


			
			
			
			$PageContents .= '<br><br><br><b>'.$MsgText[4][36].':</b><br><br>';

			
			$Dates = '';
			$Counts = '';
			$TotalCount = '';
			
			$LargestFound = -1;
			
						
			$Day = 1;
			
			
			$Months = 24;
						
			for ($x = 0; $x < $Months; $x++) {
			
				$Timestamp = mktime(0,0,0,($CurrentMonth-22+$x),0,$CurrentYear);
				
				$Year = date('Y',$Timestamp);
				$Month = date('n',$Timestamp);
							
			
				$query = "select sum(PageViews) as PageViews from `".$Settings['DBPrefix']."cs-AggregateData` where Year='".$Year."' and Month='".$Month."' limit 1";
				$result = mysql_query($query) or die (mysql_error());
				$row = mysql_fetch_array($result);
				
				$Dates[] = date('M Y', mktime(0,0,0,$Month,$Day,$Year));
				$NumberDates[] = date('j', mktime(0,0,0,$Month,$Day,$Year));

				
				$Counts[] = $row['PageViews'];
				
				$ProjectionCount = 0;
				if ($Year == $CurrentYear && $Month == $CurrentMonth && $CurrentDay > 3 && $row['PageViews'] > 0) {
				
					$MonthEndTimestamp = (mktime(0,0,0,$Month+1,1,$Year)-1);
					$MonthStartTimestamp = mktime(0,0,0,$Month,1,$Year);
					
					$CurrentTimestamp = GetTimestamp();
					
					$TimeCompleted = (($CurrentTimestamp-$MonthStartTimestamp)/($MonthEndTimestamp-$MonthStartTimestamp));
					
					//print 'M Start: '.$MonthStartTimestamp.'<br>M End: '.$MonthEndTimestamp.'<br><br>C Timestamp: '.$CurrentTimestamp.'<br><br>Time Completed: '.$TimeCompleted;
					
					$ProjectionCount = ($row['PageViews']/$TimeCompleted);
				
				}
				
				
				if ($ProjectionCount > 0) {
					if ($ProjectionCount > $LargestFound) {
						$LargestFound = $ProjectionCount;
					}
				}				
				else if ($row['PageViews'] > $LargestFound) {
					$LargestFound = $row['PageViews'];
				}
				
				$TotalCount += $row['PageViews'];
							
			}
			
			
			
			$PageContents .= '<table border="0" cellpadding="2" cellspacing="0" border="0"><tr><td width="20">&nbsp;</td><td><table border="0" cellpadding="2" cellspacing="0" border="0"><tr>
			      <td align="center"><font face="'.$FontFace.'" size="'.($FontSize-1).'"><b>'.$MsgText[4][37].'</b></td>
			      <td align="center"><font face="'.$FontFace.'" size="'.($FontSize-1).'">&nbsp;&nbsp;<b>'.$MsgText[4][28].'</b>&nbsp;&nbsp;</td>
			      <td width="300"><font face="'.$FontFace.'" size="'.($FontSize-1).'"><b>'.$MsgText[4][29].'</b></td>
    			</tr>';
			
			$SizeMultiplier = (100/(($LargestFound/$TotalCount)*100));
						
			
			for ($x = 0; $x < $Months; $x++) {
			
				if ($x == ($Months-1) && $ProjectionCount > 0) {
				
					$ThisPercent = ($Counts[$x]/$TotalCount);
					$ThisWidth = number_format($ThisPercent * 250 * $SizeMultiplier,0);
					
					$ProjectionPercent = ($ProjectionCount/$TotalCount);
					$ProjectionWidth = (number_format($ProjectionPercent * 250 * $SizeMultiplier,0)-$ThisWidth-1);
					 
			
					$PageContents .= '<tr>
			      <td align="center"><font face="'.$FontFace.'" size="'.$FontSize.'">&nbsp;&nbsp;' . $Dates[$x] . '&nbsp;&nbsp;</td>
			      <td align="center"><font face="'.$FontFace.'" size="'.$FontSize.'">' . number_format($Counts[$x],0) . '<br>&nbsp;&nbsp;&nbsp;(' . number_format($ProjectionCount,0) . ')&nbsp;&nbsp;&nbsp;</td>
			      <td width="300"><font face="'.$FontFace.'" size="'.$FontSize.'"><table width="'.($ThisWidth+$ProjectionWidth).'" height="8" border="0" cellpadding="0" cellspacing="0" border="0"><tr><td width="'.$ThisWidth.'" bgcolor="'.$Colors['Text'].'"><img src="'.$ImagesURL.'/admin/spacer.gif" width="1" height="1" border="0" alt=""></td><td width="1" bgcolor="'.$Colors['BG'].'"><img src="'.$ImagesURL.'/admin/spacer.gif" width="1" height="1" border="0" alt=""></td><td width="'.$ProjectionWidth.'" bgcolor="'.$AccentText.'"><img src="'.$ImagesURL.'/admin/spacer.gif" width="1" height="1" border="0" alt=""></td></tr></table></td>
    			</tr>';
				
				}
				else {
				
					$ThisPercent = ($Counts[$x]/$TotalCount);
					$ThisWidth = number_format($ThisPercent * 250 * $SizeMultiplier,0); 
			
					$PageContents .= '<tr>
			      <td align="center"><font face="'.$FontFace.'" size="'.$FontSize.'">&nbsp;&nbsp;' . $Dates[$x] . '&nbsp;&nbsp;</td>
			      <td align="center"><font face="'.$FontFace.'" size="'.$FontSize.'">&nbsp;&nbsp;&nbsp;' . number_format($Counts[$x],0) . '&nbsp;&nbsp;&nbsp;</td>
			      <td width="300"><font face="'.$FontFace.'" size="'.$FontSize.'"><table width="'.$ThisWidth.'" height="8" border="0" cellpadding="0" cellspacing="0" border="0"><tr><td bgcolor="'.$Colors['Text'].'"><img src="'.$ImagesURL.'/admin/spacer.gif" width="1" height="1" border="0" alt=""></td></tr></table></td>
    			</tr>';
				
				}
			
			}
			
			$PageContents .= '</table></td></tr></table>';
			
			
			
			$PageContents .= '<br><br><br><b>'.$MsgText[4][38].':</b><br><br>';

			
			$Counts = '';
			$TotalCount = '';
			
			$LargestFound = -1;
			
					
			$query = "select DateCode from `".$Settings['DBPrefix']."cs-Recent`";
			$result = mysql_query($query) or die (mysql_error());
			while ($row = mysql_fetch_array($result)) {
				
				$Counts[date('G',$row['DateCode'])]++;
									
				$TotalCount++;
							
			}
			
			for ($x = 0; $x < 24; $x++) {
				if ($Counts[$x] > $LargestFound) {
					$LargestFound = $Counts[$x];
				}
			}
			
			
			$Times[0] = '12 AM';
			$Times[1] = '1 AM';
			$Times[2] = '2 AM';
			$Times[3] = '3 AM';
			$Times[4] = '4 AM';
			$Times[5] = '5 AM';
			$Times[6] = '6 AM';
			$Times[7] = '7 AM';
			$Times[8] = '8 AM';
			$Times[9] = '9 AM';
			$Times[10] = '10 AM';
			$Times[11] = '11 AM';
			$Times[12] = '12 PM';
			$Times[13] = '1 PM';
			$Times[14] = '2 PM';
			$Times[15] = '3 PM';
			$Times[16] = '4 PM';
			$Times[17] = '5 PM';
			$Times[18] = '6 PM';
			$Times[19] = '7 PM';
			$Times[20] = '8 PM';
			$Times[21] = '9 PM';
			$Times[22] = '10 PM';
			$Times[23] = '11 PM';
			
			
			$PageContents .= '<table border="0" cellpadding="2" cellspacing="0" border="0"><tr><td width="20">&nbsp;</td><td><table border="0" cellpadding="2" cellspacing="0" border="0"><tr>
			      <td align="center"><font face="'.$FontFace.'" size="'.($FontSize-1).'"><b>'.$MsgText[4][39].'</b></td>
			      <td align="center"><font face="'.$FontFace.'" size="'.($FontSize-1).'">&nbsp;&nbsp;<b>'.$MsgText[4][28].'</b>&nbsp;&nbsp;</td>
			      <td width="300"><font face="'.$FontFace.'" size="'.($FontSize-1).'"><b>'.$MsgText[4][29].'</b></td>
    			</tr>';
			
			$SizeMultiplier = (100/(($LargestFound/$TotalCount)*100));
						
			
			for ($x = 0; $x < 24; $x++) {
			
				$ThisPercent = ($Counts[$x]/$TotalCount);
				$ThisWidth = number_format($ThisPercent * 250 * $SizeMultiplier,0); 
			
				$PageContents .= '<tr>
			      <td align="center"><font face="'.$FontFace.'" size="'.$FontSize.'">&nbsp;&nbsp;' . $Times[$x] . '&nbsp;&nbsp;</td>
			      <td align="center"><font face="'.$FontFace.'" size="'.$FontSize.'">&nbsp;&nbsp;&nbsp;' . number_format($Counts[$x],0) . '&nbsp;&nbsp;&nbsp;</td>
			      <td width="300"><font face="'.$FontFace.'" size="'.$FontSize.'"><table width="'.$ThisWidth.'" height="8" border="0" cellpadding="0" cellspacing="0" border="0"><tr><td bgcolor="'.$Colors['Text'].'"><img src="'.$ImagesURL.'/admin/spacer.gif" width="1" height="1" border="0" alt=""></td></tr></table></td>
    			</tr>';
			
			}
			
			$PageContents .= '</table></td></tr></table>';
			
			
					
		
		} // end if permissions are sufficient
		else {
		
			$PageContents = $MsgText[0][97];
		
		}

	}
	else {
	
		RedirectToLogin();
	
	}

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




?>
