<?
/*
 
 本软件版权归作者所有,在投入使用之前注意获取许可
 作者：程威（wiwiboy）
 项目：PHPStat流量统计 1.0
 电话：13553166317
 邮箱：wiwiboy@126.com
 QQ号：158839753
 网址：http://www.phpstat.net
 phpstat.net保留全部权力，受相关法律和国际		  		
		公约保护，请勿非法修改、转载、散播，或用于其他赢利行为，		
		并请勿删除版权声明。
*/
/*	
	verson:v1.0 20060618
	author:wiwiboy
	last update:2007/01/01	$time 23:16
*/

	
	$Res = $Conn->Execute( " select sitedes from $TableList[site] where website REGEXP '$regexpwebsite'" );
	while($Tmp = $Res->FetchRow())
	{
		$sitedes[] = $Tmp[sitedes];
	}
	$Tpl->assign( 'sitedesc', implode(' | ',$sitedes ));

	$Res = $Conn->Execute( " select times,week,month,sum(day_count) as day_count,sum(day_count_ip) as day_count_ip from $TableList[day_data] where `times` >= $StartTime and `times` < $EndTime and website REGEXP '$regexpwebsite' group by times" );

	while( $Tmp = $Res->FetchRow() )
	{	
		$w = date("w",$Tmp[times]) == "0" ? "7" : date("w",$Tmp[times]) ;
		$m = date("m",$Tmp[times]);
		if( $_GET[type] == '1' OR $_GET[type] == '2')
		{			
			$WeekNum			= array('7'=>'<FONT COLOR=\"#0000ff\">星期日</FONT>',
											'1'=>'星期一',
											'2'=>'星期二',
											'3'=>'星期三',
											'4'=>'星期四',
											'5'=>'星期五',
											'6'=>'<FONT COLOR=\"#0000ff\">星期六</FONT>');
			$Tmp[week]			= $WeekNum[$w];		
			$Tmp[day_count_agv]	= sprintf("%01.2f",$Tmp[day_count]/$Tmp[day_count_ip]);
			$Tmp[day_count]		= splitstr($Tmp["day_count"]);
			$Tmp[day_count_ip]	= splitstr($Tmp["day_count_ip"]);
		}
		if( $w >= 1 and ( $_GET[type] == '2' or $_GET[type] == '3' ))
		{	
			$i++;
			$WTmp[timestmp]		= "本周小记";
			$WTmp[times]		= date("m月d",$Tmp[times]-($i-1)*86400).date("-d",$Tmp[times]).",第".date("W",$Tmp[times])."周";
			$WTmp[day_count]	+= $Tmp[day_count];
			$WTmp[day_count_ip]	+= $Tmp[day_count_ip];
		}
		
		if( $_GET[type] == '1' OR $_GET[type] == '2')
		{
			$Tmp[times]			= date("Y-m-d",$Tmp[times]);		
			$DayDatas[]			= $Tmp;
		}

		//本周小记部分
		if( $w == 7 and ( $_GET[type] == '2' or $_GET[type] == '3' ))
		{	
			$WTmp[day_count_agv]= sprintf("%01.2f",$WTmp[day_count]/$WTmp[day_count_ip]);
			$WTmp[day_count]	= splitstr($WTmp[day_count]);
			$WTmp[day_count_ip]	= splitstr($WTmp[day_count_ip]);
			$WeekDatas[]		= $WTmp;
			$DayDatas[]			= $WTmp;
			unset($WTmp);
			$i = 0;
		}
		if( $_GET[type] == '3' )
		{	
			$MTmp[$m][times]		  = $Tmp[times];
			$MTmp[$m][month_count]	  += $Tmp[day_count];
			$MTmp[$m][month_count_ip] += $Tmp[day_count_ip];
			$YTmp[0][times]			  = $Tmp[times];
			$YTmp[0][year_count]	  += $Tmp[day_count];
			$YTmp[0][year_count_ip]   += $Tmp[day_count_ip];
		}
		
	}
	
	$Tpl->assign( 'DayDatas', $DayDatas );
	
	//读取24小时统计信息
	
	
	$Res = $Conn->Execute( " select sum(hour0) as hour0, sum(hour1) as hour1, sum(hour2) as hour2, sum(hour3) as hour3, sum(hour4) as hour4, sum(hour5) as hour5, sum(hour6) as hour6, sum(hour7) as hour7, sum(hour8) as hour8, sum(hour9) as hour9, sum(hour10) as hour10, sum(hour11) as hour11, sum(hour12) as hour12, sum(hour13) as hour13, sum(hour14) as hour14, sum(hour15) as hour15, sum(hour16) as hour16, sum(hour17) as hour17, sum(hour18) as hour18, sum(hour19) as hour19, sum(hour20) as hour20, sum(hour21) as hour21, sum(hour22) as hour22, sum(hour23) as hour23, sum(hourip0) as hourip0, sum(hourip1) as hourip1, sum(hourip2) as hourip2, sum(hourip3) as hourip3, sum(hourip4) as hourip4, sum(hourip5) as hourip5, sum(hourip6) as hourip6, sum(hourip7) as hourip7, sum(hourip8) as hourip8, sum(hourip9) as hourip9, sum(hourip10) as hourip10, sum(hourip11) as hourip11, sum(hourip12) as hourip12, sum(hourip13) as hourip13, sum(hourip14) as hourip14, sum(hourip15) as hourip15, sum(hourip16) as hourip16, sum(hourip17) as hourip17, sum(hourip18) as hourip18, sum(hourip19) as hourip19, sum(hourip20) as hourip20, sum(hourip21) as hourip21, sum(hourip22) as hourip22, sum(hourip23) as hourip23 from $TableList[day_data] where `times` >= $StartTime and `times` < $EndTime and website REGEXP '$regexpwebsite'" );
	$Row = $Res->FetchRow();
	$MaxCount = 0;
	$CountIpAll = $CountAll = 0;
	for( $i = 0; $i < 24; $i++ )
	{	
		$CountIpAll += $Row["hourip$i"];
		$CountAll += $Row["hour$i"];
		$MaxCount = $Row["hour$i"] > $MaxCount ? $Row["hour$i"] : $MaxCount;
		$MinCount = $Row["hour$i"] < $MinCount ? $Row["hour$i"] : $MinCount;
		$Data_Tu[] = $i.','.$Row["hourip$i"].','.$Row["hour$i"];
	}

	$CountIpAll = $CountIpAll > 0 ? $CountIpAll : 0;
	$CountAll = $CountAll > 0 ? $CountAll : 0;
	$MaxCount = $MaxCount > 0 ? $MaxCount : 0;
	$MinCount = $MinCount > 0 ? $MinCount : 0;

	$Quotiety = $MaxCount == 0 ? 0 : PILLAR_HEIGHT / $MaxCount;
	$HourLineCount = intval( $MaxCount / 5 );
	
	for( $i = 0; $i < 24; $i++ )
	{
		$Row["hourippercent$i"]		 = $CountIpAll == 0 ? 0 : sprintf("%01.2f", ( $Row["hourip$i"]/$CountIpAll ) * 100 );
		$Row["hourippillarheight$i"] = $Quotiety * $Row["hourip$i"];
		$Row["hourippillarheight$i"] = $Row["hourippillarheight$i"] < 1 ? 1 : $Row["hourippillarheight$i"];

		$Row["hourpercent$i"]		 = $CountAll == 0 ? 0 : sprintf("%01.2f", ( $Row["hour$i"]/$CountAll ) * 100 );
		$Row["hourpillarheight$i"]   = ( $Quotiety * $Row["hour$i"] ) - $Row["hourippillarheight$i"];
		$Row["hourpillarheight$i"]	 = $Row["hourpillarheight$i"] < 1 ? 1 : $Row["hourpillarheight$i"];        
		
		$Row["hourip$i"]			 = splitstr($Row["hourip$i"]);
		$Row["splithour$i"]			 = splitstr($Row["hour$i"]);
		$HourData[$i]			= array(
								'hourippercent' =>$Row["hourippercent$i"], 
								'hourippillarheight' => $Row["hourippillarheight$i"],
								'hourpercent' => $Row["hourpercent$i"],
								'hourpillarheight' => $Row["hourpillarheight$i"],
								'hourip' => $Row["hourip$i"],
								'hour' => $Row["hour$i"],
								'splithour' => $Row["splithour$i"],
								'time' => $i
								);
		$MaxMinCountArray[]			 = $Row["hour$i"];
		$DayHour[$i] = $i;
	}
	$Tpl->assign( 'MaxCount', max($MaxMinCountArray) );
	$Tpl->assign( 'MinCount', min($MaxMinCountArray) );
	$Data_Tu = implode(";",$Data_Tu);
	$Tpl->assign( 'DataTime', $DataTime );
	$Tpl->assign( 'DayHour', $DayHour );
	$Tpl->assign( 'HourData', $HourData );

	//结束读取当天24小时统计信息	


	//受访页面统计报表
	unset($CountAll);
	$Res = $Conn->SelectLimit( " select `page`,sum(counts) as `counts` from $TableList[page] where website REGEXP '$regexpwebsite' and `types` = 3 and `times` >= $StartTime and `times` < $EndTime GROUP BY `page` ORDER BY `counts` DESC ",$limit,0 );
	while( $Tmp = $Res->FetchRow() )
	{	
		$Tmp[countssplit] = splitstr($Tmp[counts]);
		if( substr( $Tmp[page],0,4 ) != 'http' )
			$Tmp[pageurl] = $siteurl.$Tmp[page];
		else
			$Tmp[pageurl] = $Tmp[page];
		$CountAll += $Tmp[counts];
		$Pages[]	  = $Tmp;
	}
	if( count( $Pages ) > 0 )
	{
		foreach( $Pages as $Key=>$Val )
		{
			$Pages[$Key][percent] = sprintf("%01.2f", ( $Val[counts]/$CountAll ) * 100 );
		}
	}
	$Tpl->assign( 'Pages', $Pages );

	//语言统计报表 
	unset($CountAll);
	$Res = $Conn->SelectLimit( " select `language`,sum(counts) as `counts` from $TableList[language] where `types` = 3 and website REGEXP '$regexpwebsite' and `times` >= $StartTime and `times` < $EndTime GROUP BY `language` ORDER BY `counts` DESC ",$limit,0 );

	while( $Tmp = @$Res->FetchRow() )
	{	
		$Tmp[countssplit] = splitstr($Tmp[counts]);
		$Tmp[language] = GetLanguage( $Tmp[language] );
		$CountAll += $Tmp[counts];
		$Language[] = $Tmp;
	}
	if( count( $Language ) > 0 )
	{
		foreach( $Language as $Key=>$Val )
		{
			$Language[$Key][percent] = sprintf("%01.2f", ( $Val[counts]/$CountAll ) * 100 );
		}
	}
	$Tpl->assign( 'Language', $Language );
	//语言统计报表

	//浏览器统计报表 
	unset($CountAll);
	$Res = $Conn->SelectLimit( " select `browser`,sum(counts) as `counts` from $TableList[browser] where `types` = 3 and website REGEXP '$regexpwebsite' and `times` >= $StartTime and `times` < $EndTime GROUP BY `browser` ORDER BY `counts` DESC ",$limit,0 );

	while( $Tmp = @$Res->FetchRow() )
	{	
		$Tmp[countssplit] = splitstr($Tmp[counts]);
		$Tmp[browser] = $Tmp[browser];
		$CountAll += $Tmp[counts];
		$Browser[] = $Tmp;
	}
	if( count( $Browser ) > 0 )
	{
		foreach( $Browser as $Key=>$Val )
		{
			$Browser[$Key][percent] = sprintf("%01.2f", ( $Val[counts]/$CountAll ) * 100 );
		}
	}
	$Tpl->assign( 'Browser', $Browser );
	//浏览器统计报表

	// 操作系统统计报表 
	unset($CountAll);
	$Res = $Conn->SelectLimit( " select `system`,sum(counts) as `counts` from $TableList[system] where `types` = 3 and website REGEXP '$regexpwebsite' and `times` >= $StartTime  and `times` < $EndTime GROUP BY `system` ORDER BY `counts` DESC ",$limit,0 );

	while( $Tmp = @$Res->FetchRow() )
	{	
		$Tmp[countssplit] = splitstr($Tmp[counts]);
		$CountAll += $Tmp[counts];
		$Tmp[system] = GetSystem( $Tmp[system] );
		$System[] = $Tmp;
	}
	if( count( $System ) > 0 )
	{
		foreach( $System as $Key=>$Val )
		{
			$System[$Key][percent] = sprintf("%01.2f", ( $Val[counts]/$CountAll ) * 100 );
		}
	}
	$Tpl->assign( 'System', $System );
	//操作系统统计报表

	//显示器尺寸统计报表 
	unset($CountAll);
	$Res = $Conn->SelectLimit( " select `screen`,sum(counts) as `counts` from $TableList[screen] where `types` = 3 and website REGEXP '$regexpwebsite' and `times` >= $StartTime  and `times` < $EndTime GROUP BY `screen` ORDER BY `counts` DESC ",$limit,0 );

	while( $Tmp = @$Res->FetchRow() )
	{	
		$Tmp[countssplit] = splitstr($Tmp[counts]);
		$CountAll += $Tmp[counts];
		$Screen[] = $Tmp;
	}
	if( count( $Screen ) > 0 )
	{
		foreach( $Screen as $Key=>$Val )
		{
			$Screen[$Key][percent] = sprintf("%01.2f", ( $Val[counts]/$CountAll ) * 100 );
		}
	}
	$Tpl->assign( 'Screen', $Screen );
	//显示器尺寸统计报表

	//显示器色位统计报表 
	unset($CountAll);
	$Res = $Conn->SelectLimit( " select `color`,sum(counts) as `counts` from $TableList[color] where `types` = 3 and website REGEXP '$regexpwebsite' and `times` >= $StartTime and `times` < $EndTime GROUP BY `color` ORDER BY `counts` DESC ",$limit,0 );

	while( $Tmp = @$Res->FetchRow() )
	{	
		$Tmp[countssplit] = splitstr($Tmp[counts]);
		$CountAll += $Tmp[counts];
		$Color[] = $Tmp;
	}
	if( count( $Color ) > 0 )
	{
		foreach( $Color as $Key=>$Val )
		{
			$Color[$Key][percent] = sprintf("%01.2f", ( $Val[counts]/$CountAll ) * 100 );
		}
	}
	$Tpl->assign( 'Color', $Color );
	//显示器色位统计报表

	//搜索引擎来路统计报表
	unset($CountAll);
	$Engines = array(
		'baidu.com',
		'google.com',
		'google.cn',
		'search.sohu.com',
		'search.sina.com.cn',
		'so.163.com',
		'websearch.yahoo.com',
		'search.lycos.com',
		'seek.3721.com',
		'search.qq.com',
		'search.tom.com',
		'sogou.com',
		'search.21cn.com',
		'search.aol.com'
		);
	
	
	
	$Where = ' and referer REGEXP "' . implode( '|', $Engines ) .'" ';
	$Res = $Conn->SelectLimit( " select `referer`,sum(counts) as `counts` from $TableList[referer] where types = 4 and website REGEXP '$regexpwebsite' $Where and `times` >= $StartTime and `times` < $EndTime GROUP BY `referer` ORDER BY `counts` DESC ",$limit,0);		
	while( $Tmp = $Res->FetchRow() )
	{	
		$Tmp[countssplit] = splitstr($Tmp[counts]);
		$CountAll += $Tmp[counts];
		$Searchs[] = $Tmp;
	}		
	if( count( $Searchs ) > 0 )
	{
		foreach( $Searchs as $Key=>$Val )
		{
			$Searchs[$Key][percent] = sprintf("%01.2f", ( $Val[counts]/$CountAll ) * 100 );
		}
	}

	$Tpl->assign( 'Searchs', $Searchs );
	//结束搜索引擎来路统计报表

	//关键字统计报表 
	unset($CountAll);
	$Res = $Conn->SelectLimit( " select `keyword`,sum(counts) as `counts` from $TableList[keyword] where `types` = 3 and website REGEXP '$regexpwebsite' and `times` >= $StartTime and `times` < $EndTime GROUP BY `keyword` ORDER BY `counts` DESC ",$limit,0 );

	while( $Tmp = @$Res->FetchRow() )
	{	
		$Tmp[countssplit] = splitstr($Tmp[counts]);
		$CountAll += $Tmp[counts];
		$Keywords[] = $Tmp;
	}
	if( count( $Keywords ) > 0 )
	{
		foreach( $Keywords as $Key=>$Val )
		{
			$Keywords[$Key][percent] = sprintf("%01.2f", ( $Val[counts]/$CountAll ) * 100 );
		}
	}

	$Tpl->assign( 'Keywords', $Keywords );
	//结束关键字统计报表 

	//搜索&关键字统计报表
	
	$Res = $Conn->SelectLimit( " select sum(counts) as counts,fromsite from $TableList[keyword] where `types` = 3 and website REGEXP '$regexpwebsite' and `times` >= $StartTime  and `times` < $EndTime group by `fromsite` ORDER BY `counts` DESC ",$limit,0 );

	while( $Tmp = @$Res->FetchRow() )
	{	
		$SubRes = $Conn->SelectLimit( " select `keyword`,sum(counts) as `counts` from $TableList[keyword] where `types` = 3 and website REGEXP '$regexpwebsite' and `times` >= $StartTime and `times` < $EndTime and fromsite = '$Tmp[fromsite]' GROUP BY `keyword` ORDER BY `counts` DESC ",$limit,0 );
		unset($Keywords);unset($CountAll);
		while( $SubTmp = @$SubRes->FetchRow() )
		{	
			$SubTmp[countssplit] = splitstr($SubTmp[counts]);
			$SubTmp[engines] = $Tmp[fromsite];
			$CountAll += $SubTmp[counts];
			$Keywords[] = $SubTmp;
		}
		if( count( $Keywords ) > 0 )
		{
			foreach( $Keywords as $Key=>$Val )
			{
				$Keywords[$Key][percent] = sprintf("%01.2f", ( $Val[counts]/$CountAll ) * 100 );
			}
		}
		$Tmp[engines] = $Tmp[fromsite];	
		$Tmp[sub] = $Keywords;	
		$SearchKeywords[] = $Tmp;
	}
	$Tpl->assign( 'SearchKeywords', $SearchKeywords );
	//结束搜索&关键字统计报表 


	//国家统计报表 
	unset($CountAll);
	$Res = $Conn->SelectLimit( " select `country`,sum(counts) as `counts` from $TableList[country] where `types` = 3 and website REGEXP '$regexpwebsite' and `times` >= $StartTime  and `times` < $EndTime GROUP BY `country` ORDER BY `counts` DESC ",$limit,0 );

	while( $Tmp = @$Res->FetchRow() )
	{	
		$Tmp[countssplit] = splitstr($Tmp[counts]);
		$CountAll += $Tmp[counts];
		$Country[] = $Tmp;
	}
	if( count( $Country ) > 0 )
	{
		foreach( $Country as $Key=>$Val )
		{
			$Country[$Key][percent] = sprintf("%01.2f", ( $Val[counts]/$CountAll ) * 100 );
		}
	}
	$Tpl->assign( 'Country', $Country );
	//国家统计报表 

	//省份统计报表 
	unset($CountAll);
	$Res = $Conn->SelectLimit( " select `province`,sum(counts) as `counts` from $TableList[province] where `types` = 3 and website REGEXP '$regexpwebsite' and `times` >= $StartTime and `times` < $EndTime GROUP BY `province` ORDER BY `counts` DESC ",$limit,0 );

	while( $Tmp = @$Res->FetchRow() )
	{	
		$Tmp[countssplit] = splitstr($Tmp[counts]);
		$CountAll += $Tmp[counts];
		$Province[] = $Tmp;
	}
	if( count( $Province ) > 0 )
	{
		foreach( $Province as $Key=>$Val )
		{
			$Province[$Key][percent] = sprintf("%01.2f", ( $Val[counts]/$CountAll ) * 100 );
		}
	}
	$Tpl->assign( 'Province', $Province );
	//省份统计报表
	
	//城市统计报表 
	unset($CountAll);
	$Res = $Conn->SelectLimit( " select `city`,sum(counts) as `counts` from $TableList[city] where `types` = 3 and website REGEXP '$regexpwebsite' and `times` >= $StartTime and `times` < $EndTime GROUP BY `city` ORDER BY `counts` DESC ",$limit,0 );

	while( $Tmp = @$Res->FetchRow() )
	{	
		$Tmp[countssplit] = splitstr($Tmp[counts]);
		$CountAll += $Tmp[counts];
		$City[] = $Tmp;
	}
	if( count( $City ) > 0 )
	{
		foreach( $City as $Key=>$Val )
		{
			$City[$Key][percent] = sprintf("%01.2f", ( $Val[counts]/$CountAll ) * 100 );
		}
	}
	$Tpl->assign( 'City', $City );
	//城市统计报表

	$Tpl->assign( 'Website', $website );
	$Tpl->assign( 'ReportTitle', date("Y-m-d",$StartTime)."日报表" );

?>