<?
/*
 
 本软件版权归作者所有,在投入使用之前注意获取许可
 作者：程威（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-2-23
*/
/*
	2006-10-09 新增加的文件，修改了‘访问明晰’的查看方式
*/
	session_start();

	ini_set('max_execution_time',5000);
	include_once './include.inc/config.inc.php';
	include_once './include.inc/global.inc.php';
	include_once './session.php';
	include_once './include.inc/conn.db.inc.php';
	include_once './include.inc/tpl.inc.php';
	include_once './include.inc/function.php';
	include_once './parse_site.php';
	Initialize( $_GET, array( 'website'=>'strval','Date_Num'=>'intval','TotalDays'=>'intval','loop'=>'strval' ,'endtime'=>'intval' ) );
	$website = $_GET[website];
	include_once "./count/exclusion/website_".$websiteid.".php";
	include_once './initmenu.php';
	
	write_to_file(COUNT_DIRNAME."/count/get_".$website."_record.phpstat",0);
	
	$Res = $Conn->Execute("select count(id) as existsrecord from ". DB_PREFIX.$mainsitecode."_ip_limit where website = '$website' and time < $_GET[endtime] limit 1");
	$Row = $Res->FetchRow();
	$isrecord = intval($Row[existsrecord]);

	if($website != "" and $isrecord > '0')
	{	
		$StartTime = $_GET[endtime];
		$EndTime   = $_GET[endtime] - 86400;
		$Sql = "SELECT count(id) as counts , pageurl,pagetitle from ". DB_PREFIX.$mainsitecode."_ip_hits where time < $StartTime and time >= $EndTime and website = '$website' and pageurl != '' group by pageurl order by counts desc limit 50;";

		$Res = @$Conn->Execute( $Sql );
		unset($Tmp);
		while($Tmp = $Res->FetchRow())
		{		
			//echo $Tmp[counts];
			$Sql = "INSERT INTO " . $TableList[page] . " ( `website`,`page`,`pagetitle`, `times`, `counts`,`types` ) VALUES ( '{$website}','{$Tmp[pageurl]}','{$Tmp[pagetitle]}','{$EndTime}', '{$Tmp[counts]}', '3');";
			@$Conn->Execute( $Sql );
		}

		//来路页面历史记录
		$Sql = "SELECT count(id) as counts , pagefrom from ". DB_PREFIX.$mainsitecode."_ip_limit where time < $StartTime and time >= $EndTime and website = '$website'  group by pagefrom order by counts desc limit 50;";
		$Res = @$Conn->Execute( $Sql );
		unset($Tmp);
		while($Tmp = $Res->FetchRow())
		{			
			$Sql = "INSERT INTO " . $TableList[referer] . " ( `website`,`referer`, `times`, `counts`,`types` ) VALUES ( '{$website}','{$Tmp[pagefrom]}','{$EndTime}', '{$Tmp[counts]}', '3' );";
			@$Conn->Execute( $Sql );
		}
		
		//来路站点历史记录
		$Sql = "SELECT count(id) as counts , pagefromsite from ". DB_PREFIX.$mainsitecode."_ip_limit where time < $StartTime and time >= $EndTime and website = '$website'  group by pagefromsite order by counts desc limit 50;";
		$Res = @$Conn->Execute( $Sql );
		unset($Tmp);
		while($Tmp = $Res->FetchRow())
		{			
			$Sql = "INSERT INTO " . $TableList[referer] . " ( `website`,`referer`, `times`, `counts`,`types` ) VALUES ( '{$website}','{$Tmp[pagefromsite]}','{$EndTime}', '{$Tmp[counts]}', '4' );";
			@$Conn->Execute( $Sql );
		}

		//入口页面历史记录
		$Sql = "SELECT count(id) as counts , pageurl,pagetitle from ". DB_PREFIX.$mainsitecode."_ip_limit where time < $StartTime and time >= $EndTime and website = '$website' and pageurl != ''  group by pageurl order by counts desc limit 50;";
		$Res = @$Conn->Execute( $Sql );
		unset($Tmp);
		while($Tmp = $Res->FetchRow())
		{			
			$Sql = "INSERT INTO " . $TableList[page] . " ( `website`,`page`,`pagetitle`, `times`, `counts`,`types` ) VALUES ( '{$website}','{$Tmp[pageurl]}','{$Tmp[pagetitle]}','{$EndTime}', '{$Tmp[counts]}', '4' );";
			@$Conn->Execute( $Sql );
		}
		//国家历史记录
		$Sql = "SELECT count(id) as counts , country from ". DB_PREFIX.$mainsitecode."_ip_limit where time < $StartTime and time >= $EndTime and website = '$website' and country != ''  group by country order by counts desc limit 50;";
		$Res = @$Conn->Execute( $Sql );
		unset($Tmp);
		while($Tmp = $Res->FetchRow())
		{		
			$Sql = "INSERT INTO " . $TableList[country] . " ( `website`,`country`, `times`, `counts`,`types` ) VALUES ( '{$website}','{$Tmp[country]}','{$EndTime}', '{$Tmp[counts]}', '3' );";
			@$Conn->Execute( $Sql );
		}

		//省份历史记录
		$Sql = "SELECT count(id) as counts , province,country from ". DB_PREFIX.$mainsitecode."_ip_limit where time < $StartTime and time >= $EndTime and website = '$website' and province != ''  group by province order by counts desc limit 50;";
		$Res = @$Conn->Execute( $Sql );
		unset($Tmp);
		while($Tmp = $Res->FetchRow())
		{		
			$Sql = "INSERT INTO " . $TableList[province] . " ( `website`,`province`, `times`, `counts`,`types`,`country` ) VALUES ( '{$website}','{$Tmp[province]}','{$EndTime}', '{$Tmp[counts]}', '3' , '{$Tmp[country]}');";
			@$Conn->Execute( $Sql );
		}


		//城市历史记录
		$Sql = "SELECT count(id) as counts , city ,province from ". DB_PREFIX.$mainsitecode."_ip_limit where time < $StartTime and time >= $EndTime and website = '$website' and city != ''  group by city order by counts desc limit 50;";
		$Res = @$Conn->Execute( $Sql );
		unset($Tmp);
		while($Tmp = $Res->FetchRow())
		{		
			$Sql = "INSERT INTO " . $TableList[city] . " ( `website`,`city`, `times`, `counts`,`types`,`province` ) VALUES ( '{$website}','{$Tmp[city]}','{$EndTime}', '{$Tmp[counts]}', '3','{$Tmp[province]}' );";
			@$Conn->Execute( $Sql );
		}

		//地区历史记录
		$Sql = "SELECT count(id) as counts , address ,city from ". DB_PREFIX.$mainsitecode."_ip_limit where time < $StartTime and time >= $EndTime and website = '$website' and address != ''  group by address order by counts desc limit 50;";
		$Res = @$Conn->Execute( $Sql );
		unset($Tmp);
		while($Tmp = $Res->FetchRow())
		{		
			$Sql = "INSERT INTO " . $TableList[area] . " ( `website`,`area`, `times`, `counts`,`types`,`city` ) VALUES ( '{$website}','{$Tmp[address]}','{$EndTime}', '{$Tmp[counts]}', '3','{$Tmp[city]}' );";
			@$Conn->Execute( $Sql );
		}

		//关键字历史记录
		$Sql = "SELECT count(id) as counts , keyword ,pagefromsite,pagefrom from ". DB_PREFIX.$mainsitecode."_ip_hits  where time < $StartTime and time >= $EndTime and website = '$website' and keyword != '' group by keyword ,pagefromsite order by counts desc limit 100;";
		$Res = @$Conn->Execute( $Sql );
		unset($Tmp);
		while($Tmp = $Res->FetchRow())
		{	
			
			$Sql = "INSERT INTO " . $TableList[keyword] . " ( `website`,`keyword`, `times`, `lastpage`,`fromsite`,`counts`,`types` ) VALUES ( '{$website}','{$Tmp[keyword]}','{$EndTime}','{$Tmp[pagefrom]}','{$Tmp[pagefromsite]}', '{$Tmp[counts]}', '3' );";
			@$Conn->Execute( $Sql );
		}

		//网络蜘蛛字历史记录
		$Sql = "Update " . $TableList[bot] . " set types = 3 where times < $StartTime and times >= $EndTime and website = '$website' and types = 1;";
		$Res = @$Conn->Execute( $Sql );
		
		

		//回头客历史记录
		$Sql = "SELECT count(id) as counts , returner from ". DB_PREFIX.$mainsitecode."_ip_limit where time < $StartTime and time >= $EndTime and website = '$website' and returner != '0'  group by returner  order by counts desc limit 50 ;";
		$Res = @$Conn->Execute( $Sql );
		unset($Tmp);
		while($Tmp = $Res->FetchRow())
		{		
			$Sql = "INSERT INTO " . $TableList[returner] . " ( `website`,`returner`, `times`, `counts`,`types` ) VALUES ( '{$website}','{$Tmp[returner]}','{$EndTime}', '{$Tmp[counts]}', '3' );";
			@$Conn->Execute( $Sql );
		}
		
		//访问者新近度
		$Sql = "SELECT count(id) as counts ,returnday as days from ". DB_PREFIX.$mainsitecode."_ip_limit where time < $StartTime and time >= $EndTime and website = '$website'  and returner != '0' group by days  order by counts desc limit 50;";
		$Res = @$Conn->Execute( $Sql );
		unset($Tmp);
		while($Tmp = $Res->FetchRow())
		{		
			$Sql = "INSERT INTO " . $TableList[returner] . " ( `website`,`returner`, `times`, `counts`,`types` ) VALUES ( '{$website}','{$Tmp[days]}','{$EndTime}', '{$Tmp[counts]}', '4' );";
			@$Conn->Execute( $Sql );
		}
		
		//访问者浏览器
		$Sql = "SELECT count(id) as counts ,browser from ". DB_PREFIX.$mainsitecode."_ip_limit where time < $StartTime and time >= $EndTime and website = '$website' and browser != ''  group by browser ;";
		$Res = @$Conn->Execute( $Sql );
		unset($Tmp);
		while($Tmp = $Res->FetchRow())
		{		
			$Sql = "INSERT INTO " . $TableList[browser] . " ( `website`,`browser`, `times`, `counts`,`types` ) VALUES ( '{$website}','{$Tmp[browser]}','{$EndTime}', '{$Tmp[counts]}', '3' );";
			@$Conn->Execute( $Sql );
		}
		//访问者浏览器颜色
		$Sql = "SELECT count(id) as counts ,color from ". DB_PREFIX.$mainsitecode."_ip_limit where time < $StartTime and time >= $EndTime and website = '$website' and color != ''  group by color ;";
		$Res = @$Conn->Execute( $Sql );
		unset($Tmp);
		while($Tmp = $Res->FetchRow())
		{		
			$Sql = "INSERT INTO " . $TableList[color] . " ( `website`,`color`, `times`, `counts`,`types` ) VALUES ( '{$website}','{$Tmp[color]}','{$EndTime}', '{$Tmp[counts]}', '3' );";
			@$Conn->Execute( $Sql );
		}
		//访问者浏览器语言
		$Sql = "SELECT count(id) as counts ,language from ". DB_PREFIX.$mainsitecode."_ip_limit where time < $StartTime and time >= $EndTime and website = '$website' and language != ''  group by language ;";
		$Res = @$Conn->Execute( $Sql );
		unset($Tmp);
		while($Tmp = $Res->FetchRow())
		{		
			$Sql = "INSERT INTO " . $TableList[language] . " ( `website`,`language`, `times`, `counts`,`types` ) VALUES ( '{$website}','{$Tmp[language]}','{$EndTime}', '{$Tmp[counts]}', '3' );";
			@$Conn->Execute( $Sql );
		}
		//访问者时区
		$Sql = "SELECT count(id) as counts ,timezone from ". DB_PREFIX.$mainsitecode."_ip_limit where time < $StartTime and time >= $EndTime and website = '$website' and timezone != ''  group by timezone ;";
		$Res = @$Conn->Execute( $Sql );
		unset($Tmp);
		while($Tmp = $Res->FetchRow())
		{		
			$Sql = "INSERT INTO " . $TableList[timezone] . " ( `website`,`timezone`, `times`, `counts`,`types` ) VALUES ( '{$website}','{$Tmp[timezone]}','{$EndTime}', '{$Tmp[counts]}', '3' );";
			@$Conn->Execute( $Sql );
		}
		//访问者系统
		$Sql = "SELECT count(id) as counts ,system from ". DB_PREFIX.$mainsitecode."_ip_limit where time < $StartTime and time >= $EndTime and website = '$website' and system != ''  group by system ;";
		$Res = @$Conn->Execute( $Sql );
		unset($Tmp);
		while($Tmp = $Res->FetchRow())
		{		
			$Sql = "INSERT INTO " . $TableList[system] . " ( `website`,`system`, `times`, `counts`,`types` ) VALUES ( '{$website}','{$Tmp[system]}','{$EndTime}', '{$Tmp[counts]}', '3' );";
			@$Conn->Execute( $Sql );
		}
		//访问者屏幕尺寸
		$Sql = "SELECT count(id) as counts ,screensize from ". DB_PREFIX.$mainsitecode."_ip_limit where time < $StartTime and time >= $EndTime and website = '$website' and screensize != ''  group by screensize ;";
		$Res = @$Conn->Execute( $Sql );
		unset($Tmp);
		while($Tmp = $Res->FetchRow())
		{		
			$Sql = "INSERT INTO " . $TableList[screen] . " ( `website`,`screen`, `times`, `counts`,`types` ) VALUES ( '{$website}','{$Tmp[screensize]}','{$EndTime}', '{$Tmp[counts]}', '3' );";
			@$Conn->Execute( $Sql );
		}
		//JAVA设置
		$Sql = "SELECT count(id) as counts ,java from ". DB_PREFIX.$mainsitecode."_ip_limit where time < $StartTime and time >= $EndTime and website = '$website' and java != '0'  group by java ;";
		$Res = @$Conn->Execute( $Sql );
		unset($Tmp);
		while($Tmp = $Res->FetchRow())
		{		
			$Sql = "INSERT INTO " . $TableList[plug] . " ( `website`,`plug`, `times`, `counts`,`types` ) VALUES ( '{$website}','java','{$EndTime}', '{$Tmp[counts]}', '3' );";
			@$Conn->Execute( $Sql );
		}
		//JAVA设置(未安装)
		$Sql = "SELECT count(id) as counts ,java from ". DB_PREFIX.$mainsitecode."_ip_limit where time < $StartTime and time >= $EndTime and website = '$website' and java = '0'  group by java ;";
		$Res = @$Conn->Execute( $Sql );
		unset($Tmp);
		while($Tmp = $Res->FetchRow())
		{		
			$Sql = "INSERT INTO " . $TableList[plug] . " ( `website`,`plug`, `times`, `counts`,`types` ) VALUES ( '{$website}','java','{$EndTime}', '{$Tmp[counts]}', '4' );";
			@$Conn->Execute( $Sql );
		}
		//FLASH设置
		$Sql = "SELECT count(id) as counts ,flash from ". DB_PREFIX.$mainsitecode."_ip_limit where time < $StartTime and time >= $EndTime and website = '$website' and flash != '0'  group by flash ;";
		$Res = @$Conn->Execute( $Sql );
		unset($Tmp);
		while($Tmp = $Res->FetchRow())
		{		
			$Sql = "INSERT INTO " . $TableList[plug] . " ( `website`,`plug`, `times`, `counts`,`types` ) VALUES ( '{$website}','flash','{$EndTime}', '{$Tmp[counts]}', '3' );";
			@$Conn->Execute( $Sql );
		}
		//FLASH设置(未安装)
		$Sql = "SELECT count(id) as counts ,flash from ". DB_PREFIX.$mainsitecode."_ip_limit where time < $StartTime and time >= $EndTime and website = '$website' and flash = '0'  group by flash ;";
		$Res = @$Conn->Execute( $Sql );
		unset($Tmp);
		while($Tmp = $Res->FetchRow())
		{		
			$Sql = "INSERT INTO " . $TableList[plug] . " ( `website`,`plug`, `times`, `counts`,`types` ) VALUES ( '{$website}','flash','{$EndTime}', '{$Tmp[counts]}', '4' );";
			@$Conn->Execute( $Sql );
		}
		//访问者访问时长
		$Sql = "SELECT timeslong from ". DB_PREFIX.$mainsitecode."_ip_limit where website = '$website' and time < $StartTime and time >= $EndTime ;";
		$Res = @$Conn->Execute( $Sql );
		while($Tmp = $Res->FetchRow())
		{					
			if(( $Tmp[timeslong] ) <= 10)
				$onLimeTime[0]++;
			elseif(( $Tmp[timeslong] ) <= 30)
				$onLimeTime[1]++;
			elseif(( $Tmp[timeslong] ) <= 60)
				$onLimeTime[2]++;
			elseif(( $Tmp[timeslong] ) <= 180)
				$onLimeTime[3]++;
			elseif(( $Tmp[timeslong] ) <= 600)
				$onLimeTime[4]++;
			elseif(( $Tmp[timeslong] ) <= 1800)
				$onLimeTime[5]++;
			else
				$onLimeTime[6]++;
		
		}
			
		//}
		$onLineTimes = array(10,30,60,180,600,1800,1801);
		if(count($onLimeTime) > 0)
		foreach($onLimeTime as $key => $value)
		{
			$Sql = "INSERT INTO " . $TableList[returner] . " ( `website`,`returner`, `times`, `counts`,`types` ) VALUES ( '{$website}','{$onLineTimes[$key]}','{$EndTime}', '{$value}', '5' );";
			@$Conn->Execute( $Sql );
		}


		//访问者访问深度
		$Sql = "SELECT count(*) as count,pagecount from ". DB_PREFIX.$mainsitecode."_ip_limit where website = '$website'  and time < $StartTime and time >= $EndTime group by pagecount ;";
		$Res = @$Conn->Execute( $Sql );
		while($Tmp = $Res->FetchRow())
		{				
				if($Tmp[pagecount] <= 1)
					$onLimeDepth[0] = $Tmp[count];
				elseif($Tmp[pagecount] <= 2)
					$onLimeDepth[1] = $Tmp[count];
				elseif($Tmp[pagecount] <= 3)
					$onLimeDepth[2] = $Tmp[count];
				elseif($Tmp[pagecount] <= 4)
					$onLimeDepth[3] = $Tmp[count];
				elseif($Tmp[pagecount] <= 5)
					$onLimeDepth[4] = $Tmp[count];
				elseif($Tmp[pagecount] <= 6)
					$onLimeDepth[5] = $Tmp[count];
				elseif($Tmp[pagecount] <= 7)
					$onLimeDepth[6] = $Tmp[count];
				elseif($Tmp[pagecount] <= 8)
					$onLimeDepth[7] = $Tmp[count];
				elseif($Tmp[pagecount] <= 9)
					$onLimeDepth[8] = $Tmp[count];
				elseif($Tmp[pagecount] <= 10)
					$onLimeDepth[9] = $Tmp[count];
				elseif($Tmp[pagecount] <= 11)
					$onLimeDepth[10] = $Tmp[count];
				elseif($Tmp[pagecount] <= 12)
					$onLimeDepth[11] = $Tmp[count];
				elseif($Tmp[pagecount] <= 13)
					$onLimeDepth[12] = $Tmp[count];
				elseif($Tmp[pagecount] <= 14)
					$onLimeDepth[13] = $Tmp[count];
				elseif($Tmp[pagecount] <= 15)
					$onLimeDepth[14] = $Tmp[count];
				elseif($Tmp[pagecount] <= 16)
					$onLimeDepth[15] = $Tmp[count];
				elseif($Tmp[pagecount] <= 17)
					$onLimeDepth[16] = $Tmp[count];
				elseif($Tmp[pagecount] <= 18)
					$onLimeDepth[17] = $Tmp[count];
				elseif($Tmp[pagecount] <= 19)
					$onLimeDepth[18] = $Tmp[count];
				elseif($Tmp[pagecount] <= 20)
					$onLimeDepth[19] = $Tmp[count];
				else
					$onLimeDepth[20] += $Tmp[count];		
		}
		
		if(count($onLimeDepth) > 0)
		foreach($onLimeDepth as $key => $value)
		{	
			if($value > 0)
			{	
				$key1 = $key+1;
				$Sql = "INSERT INTO " . $TableList[returner] . " ( `website`,`returner`, `times`, `counts`,`types` ) VALUES ( '{$website}','{$key1}','{$EndTime}', '{$value}', '6' );";
				@$Conn->Execute( $Sql );
			}
		}

		$_GET[endtime] = $EndTime;		
		echo "<meta http-equiv=\"refresh\" content=\"0;URL=?website=$website&endtime=$_GET[endtime]\">";		
	}
	if($isrecord <= 0)
	{
		
		$StartTime = mktime(0,0,0,date('m'),date('d'),date('Y')) - SAVE_OLD_DAYS * 86400;
		$TodayTime = mktime(0,0,0,date('m'),date('d'),date('Y'));	
			
		@$Conn->Execute( "DELETE FROM ". DB_PREFIX.$mainsitecode."_ip_limit where website = '$website' and time < $TodayTime" );
		@$Conn->Execute( "DELETE FROM ". DB_PREFIX.$mainsitecode."_ip_hits where website = '$website' and time < $TodayTime" );		
		
		$Conn->Execute( " OPTIMIZE TABLE ". DB_PREFIX.$mainsitecode."_ip_limit " );
		$Conn->Execute( " OPTIMIZE TABLE ". DB_PREFIX.$mainsitecode."_ip_hits " );
		@unlink(COUNT_DIRNAME."/count/get_".$website."_record.phpstat");
		
		
	}
		
?>