<?
/*
 
 本软件版权归作者所有,在投入使用之前注意获取许可
 作者：程威（wiwiboy）
 项目：PHPStat流量统计 1.0
 电话：13553166317
 邮箱：wiwiboy@126.com
 QQ号：158839753
 网址：http://www.phpstat.net
 phpstat.net保留全部权力，受相关法律和国际		  		
		公约保护，请勿非法修改、转载、散播，或用于其他赢利行为，		
		并请勿删除版权声明。
*/
/*	
	verson:v1.0 20060618
	author:wiwiboy
	last update:2006/06/10	
*/
	session_start();
	include_once '../include.inc/config.inc.php';
	include_once '../include.inc/global.inc.php';
	include_once '../publicsession.php';
	include_once '../include.inc/conn.db.inc.php';
	include_once '../include.inc/tpl.common.inc.php';
	include_once '../include.inc/function.php';
	include_once '../parse_site.php';
	
	Initialize( $_GET, array( 'type'=>'intval', 'page'=>'intval', website=>'strval','StartTime'=>'strval','EndTime'=>'strval') );
	$website = $_GET[website];
	include_once "../count/exclusion/website_".$websiteid.".php";
	include_once '../initmenu.php';
	

	if( $Types == "1" )
	{
		//记录总数
		$Res = $Conn->Execute( " select id from ". DB_PREFIX.$mainsitecode."_ip_hits where website = '$website' and time >= $StartTime and keyword != '' group by keyword" );
		$DataCount = $Res->RecordCount();		
		$PageSize = 20;
		if( $DataCount > 0 )
		{
			include_once ( '../include.inc/page_class.php' );
			include_once ( '../include.inc/page.inc.php' );
			$PageItems = MakePageItems ( $DataCount, $PageSize );
			$PageLinks = MakePageLinks ( $PageItems, '记录','条' );
			$Tpl->assign( 'PageLinks', $PageLinks );
		}
		if($PageItems[Offset] == "")$PageItems[Offset] = 0;
		$Res = $Conn->SelectLimit( " select count(*) as counts ,keyword from ". DB_PREFIX.$mainsitecode."_ip_hits where website = '$website' and time >= $StartTime and keyword != '' group by keyword ORDER BY `counts` DESC ", $PageSize ,$PageItems[Offset]);
		$sql = " select count(*) as counts ,keyword from ". DB_PREFIX.$mainsitecode."_ip_hits where website = '$website' and time >= $StartTime and keyword != '' group by keyword ORDER BY `counts` DESC ";

		while( $Tmp = @$Res->FetchRow() )
		{
			preg_match( "|(http://[^/]+?)/.*|isU", $Tmp[lastpage], $LastSite );
			$Tmp[lastsite] = trim( $LastSite[1] );
			$Tmp[times] = date("Y-m-d H:i:s",$Tmp[times]);
			$Tmp[keyword2] = urlencode($Tmp[keyword]);
			$CountAll += $Tmp[counts];
			$Datas[] = $Tmp;
			$SqlDatas[] = array('keyword'=>$Tmp[keyword],'pvcounts'=>$Tmp[counts]);
		}
	}
	
	if( $Types == "2" or $Types == "3" or $Types == "4" or $Types == "0" )
	{
		unset($Datas);
		if( $Types == "2" or $Types == "3" or $Types == "4" )
		{
			$Where = " and times >= $StartTime and times < $EndTime ";
		}
		//记录总数
		$Res = $Conn->Execute( " select count(*) as count  from $TableList[keyword] where types = 3 and website = '$website' $Where" );
		$Count = $Res->FetchRow();
		$DataCount = $Count[count];

		$PageSize = 20;
		if( $DataCount > 0 )
		{
			include_once ( '../include.inc/page_class.php' );
			include_once ( '../include.inc/page.inc.php' );
			$PageItems = MakePageItems ( $DataCount, $PageSize );
			$PageLinks = MakePageLinks ( $PageItems, '记录','条' );
			$Tpl->assign( 'PageLinks', $PageLinks );
		}
		if($PageItems[Offset] == "")$PageItems[Offset] = 0;
		$Res = $Conn->SelectLimit( " select sum(counts) as counts,keyword from $TableList[keyword] where types = 3 and website = '$website' $Where group by keyword ORDER BY `counts` DESC ", $PageSize ,$PageItems[Offset]);
		$sql = " select sum(counts) as counts,keyword from $TableList[keyword] where types = 3 and website = '$website' $Where group by keyword ORDER BY `counts` DESC ";
		while( $Tmp = @$Res->FetchRow() )
		{
			preg_match( "|(http://[^/]+?)/.*|isU", $Tmp[lastpage], $LastSite );
			$Tmp[lastsite] = trim( $LastSite[1] );
			$Tmp[times] = date("Y-m-d H:i:s",$Tmp[times]);
			$Tmp[keyword2] = urlencode($Tmp[keyword]);
			$CountAll += $Tmp[counts];
			$Datas[] = $Tmp;
			$SqlDatas[] = array('keyword'=>$Tmp[keyword],'pvcounts'=>$Tmp[counts]);
		}
	}
	
	if( count( $Datas ) > 0 )
	{
		foreach( $Datas as $Key=>$Val )
		{		
			
			$Datas[$Key][Percent] = sprintf("%01.2f", ( $Val[counts]/$CountAll ) * 100 );
			$Datas[$Key][percent] = & $Datas[$Key][Percent];
		}
	}

	$Tpl->assign( 'Datas', $Datas );

	$Tpl->assign( 'SqlDatas', urlencode(serialize ($SqlDatas) ));
	$Tpl->assign( 'SqlDatasTitle', urlencode(serialize (array('关键字','次数')) ));
	if($DataCount > $PageSize)
	$Tpl->assign( 'Sql', urlencode( $sql ) );
	$Tpl->assign( 'fname', 'keyword' );
	$Tpl->assign( 'textexport', 'true' );
	$Tpl->assign( 'xmlexport', 'true' );
	$Tpl->assign( 'cvsexport', 'true' );

	$Tpl->assign( 'website', $website );
	$Tpl->assign( 'Main', $Tpl->fetch( 'keyword.html' ) . $ScriptCode );

	$Tpl->assign( 'Title', '关键字统计 - '.$SoftWareName .$SoftWareVersion );
	$Tpl->assign( 'NowView', '关键字统计' );
	$Tpl->assign( 'QuickLink', "<a href=\"?website=$website&type=1\">今日详情</a> <a href=\"?website=$website&type=2\">昨日详情</a> <a href=\"?website=$website&type=3\">最近7日</a> <a href=\"?website=$website&type=4\">最近30日</a> <a href=\"?website=$website&type=0\">所有详情</a>" );
	$Tpl->assign( 'CopyRight', $PHPStatBottomRight );
	_out( $Tpl->fetch( 'main.html' )  );
?>