<?
/*
* ===========================================*
* 项目: Ourplus网站流量统计分析系统*
* 版本: Bate1*
* 开发: 看海*
* ===========================================*
* Copyright (c) 2004 - 2005*
* 主页: http://stat.ourplus.com*
* 信箱: fantasysisky@hotmail.com*
* 版权信息: 本软体为共享软体(shareware)提供个人网站免费使用，
                作者看海 (stat.ourplus.com) 保留全部权力，受相关法律和国际
				公约保护，请勿非法修改、转载、散播，或用于其他赢利行为，
				并请勿删除版权声明。*
* ===========================================*
*/
	session_start();
	include_once './include.inc/config.inc.php';
	include_once 'session.php';

	include_once './include.inc/conn.db.inc.php';
	include_once './include.inc/global.inc.php';
	include_once './include.inc/tpl.inc.php';
	include_once './include.inc/function.php';

	Initialize( $_GET, array( 'type'=>'intval', 'page'=>'intval', 'engine'=>'strval' ) );

	if( isset( $_GET[engine] ) )
	{
		$Engines = array(
		'baidu.com',
		'google.com',
		'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 ) .'" ';
	}

	include_once 'initmenu.php';

	//记录总数
	$Res = mysql_query( " select count(*) as count  from $TableList[referer] where types = $Types $Where" );
	$Count = mysql_fetch_array( $Res, MYSQL_ASSOC );
	$DataCount = $Count[count];

	$PageSize = 20;
	if( $DataCount > 0 )
	{
		include_once ( './include.inc/page.inc.php' );
		$PageItems = MakePageItems ( $DataCount, $PageSize );
		$PageLinks = MakePageLinks ( $PageItems, '记录','条' );
		$Tpl->assign( 'PageLinks', $PageLinks );
	}

	$Res = mysql_query( " select *  from $TableList[referer] where types = $Types $Where ORDER BY `counts` DESC LIMIT $PageItems[Offset], $PageSize " );


	while( $Tmp = @mysql_fetch_array( $Res, MYSQL_ASSOC ) )
	{
		$Datas[] = $Tmp;
	}

	//统计百分比
	$Res = mysql_query( " select sum(counts) as counts  from $TableList[referer] where types = $Types $Where" );
	$Count = mysql_fetch_array( $Res, MYSQL_ASSOC );
	$CountAll = $Count[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];
		}
	}

	if( isset( $_GET[engine] ) )
	{
		$Tpl->assign( 'Engine', true );
	}
	$Tpl->assign( 'Datas', $Datas );

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

	if( isset( $_GET[engine] ) )
	{
		$Tpl->assign( 'Title', '搜索引擎统计 - Ourplus Counter' );
		$Tpl->assign( 'NowView', '搜索引擎统计' );
		$Tpl->assign( 'QuickLink', "<a href=\"?type=1&engine=true\">今日详情</a> <a href=\"?type=2&engine=true\">昨日详情</a> <a href=\"?type=0&engine=true\">所有详情</a>" );
	}
	else
	{
		$Tpl->assign( 'Title', '来源统计 - Ourplus Counter' );
		$Tpl->assign( 'NowView', '来源统计' );
		$Tpl->assign( 'QuickLink', "<a href=\"?type=1\">今日详情</a> <a href=\"?type=2\">昨日详情</a> <a href=\"?type=0\">所有详情</a>" );
	}
	_out( $Tpl->fetch( 'main.html' )  );
?>