<?php

require_once(dirname(__FILE__)."/global.php");

if($webdb[close_count]){
	die("document.write('统计系统暂停!');");
}

$SQL='';

$fid=intval($fid);

if(!$fid)
{
	echo "alert('FID不存在!!')";exit;
}

if($fromurl&&$webdb[ForbidCountDomain]){
	$detail=explode("\r\n",$webdb[ForbidCountDomain]);
	foreach( $detail AS $key=>$value){
		if(!$value){
			continue;
		}
		if(strstr($fromurl,$value)){
			$fromurl='';
		}
	}
}

if($fromurl&&strstr($fromurl,$webdb[www_url])){
	$fromurl='';
}
$stat_client=get_cookie("stat_client_$fid");
$detail=explode("\t",$stat_client);
$SID=$detail[0];
$time_day=date("j",$timestamp);
$time_year=date("Y",$timestamp);
$time_month=date("n",$timestamp);
$time_week=date("W",$timestamp);
$time_hour=date("G",$timestamp);
$fromurl_md5=md5("$onlineip$fromurl");

/**
*不同一天或更换IP或不同来源地址时,需要重新统计用户在线详情
**/

if($detail[1]!=$time_day||$detail[2]!=$fromurl_md5){
	$hits=1;
	$windows_type=osinfo();
	$ie_type=browseinfo();

	if($lfjid){
		$db->query("DELETE FROM `{$pre}count_user` WHERE `username`='$lfjid'");
	}elseif($SID){
		$db->query("DELETE FROM `{$pre}count_user` WHERE `id`='$SID'");
	}else{
		$db->query("DELETE FROM `{$pre}count_user` WHERE `ip`='$onlineip'");
	}
	
	$db->query("INSERT INTO `{$pre}count_user` (`fid`, `time_day`, `username`, `lasttime`, `ip`, `ip_address`, `fromurl`, `weburl`, `windows_type`, `ie_type`, `windows_lang`, `screen_size`, `hits`) VALUES ('$fid','$time_day','$lfjid','$timestamp','$onlineip','$ip_address','$fromurl','$FROMURL','$windows_type','$ie_type','$windows_lang','$screen_size','$hits')");
	$SID=$db->insert_id();
	
	set_cookie("stat_client_$fid","$SID\t$time_day\t$fromurl_md5",3600*24);

	$webdb[MaxOnlineUser] || $webdb[MaxOnlineUser]=1000;
	$rs=$db->get_one("SELECT COUNT(id) AS NUM FROM `{$pre}count_user`");
	if($rs[NUM]>$webdb[MaxOnlineUser]){
		$num=$rs[NUM]-$webdb[MaxOnlineUser];
		$db->query("DELETE FROM `{$pre}count_user` ORDER BY id ASC LIMIT $num");
	}
}
else
{
	$db->query("UPDATE `{$pre}count_user` SET `username`='$lfjid',`lasturl`='$FROMURL',`lasttime`='$timestamp',hits=hits+1 WHERE id='$SID'");
}


/**
*统计当天的UV与IP数,IP比较准确,UV的话,如果用户清空COOKIE的话,就会统计失败
**/

if(get_cookie("stat_client_uv_$fid")!=$time_day)
{
	set_cookie("stat_client_uv_$fid",$time_day,3600*24);
	$SQL=",uv=uv+1";
	$rs=$db->get_one("SELECT COUNT(id) AS NUM FROM `{$pre}count_user` WHERE time_day='$time_day' AND ip='$onlineip' AND fid='$fid'");
	if(!$rs||$rs[NUM]==1){
		$SQL .=",ip=ip+1";
	}
}


//每年每天,不同的分类,具有不同的ID
$id = date('y').substr(number_format(date('z')/1000,3),2).$fid;

if(!$db->get_one("SELECT id FROM `{$pre}count_stat` WHERE id='$id'"))
{
	$pv=$uv=$ip=1;
	$windows_type=$ie_type=$windows_lang=$screen_size=$from_domain='';
	$db->query("INSERT INTO `{$pre}count_stat` (`id`,`fid`, `year`, `month`, `week`, `day`, `hour`, `pv`, `uv`, `ip`, `windows_type`, `ie_type`, `windows_lang`, `screen_size`, `from_domain`) VALUES ('$id','$fid','$time_year','$time_month','$time_week','$time_day','','$pv','$uv','$ip','$windows_type','$ie_type','$windows_lang','$screen_size','$from_domain')");

	if($webdb[KeepTodayCount]){	
		$db->query("DELETE FROM `{$pre}count_user` WHERE time_day!='$time_day' ");
	}
}
else
{
	$db->query("UPDATE `{$pre}count_stat` SET `pv`=`pv`+1$SQL WHERE id='$id'");
}


//获取浏览器类型
function browseinfo() {
	$browser="";
	$browserver="";
	$Browsers =array("Lynx","MOSAIC","AOL","Opera","JAVA","MacWeb","WebExplorer","OmniWeb");
	$Agent = $_SERVER["HTTP_USER_AGENT"]?$_SERVER["HTTP_USER_AGENT"]:$HTTP_SERVER_VARS["HTTP_USER_AGENT"];
	for ($i=0; $i<=7; $i++) {
		if (strpos($Agent,$Browsers[$i])) {
			$browser = $Browsers[$i];
			$browserver ="";
		}
	}
	if (ereg("Mozilla",$Agent) && !ereg("MSIE",$Agent)) {
		$temp =explode("(", $Agent); $Part=$temp[0];
		$temp =explode("/", $Part); $browserver=$temp[1];
		$temp =explode(" ",$browserver); $browserver=$temp[0];
		$browserver =preg_replace("/([\d\.]+)/","\\1",$browserver);
		$browserver = " $browserver";
		$browser = "Netscape Navigator";
	}
	if (ereg("Mozilla",$Agent) && ereg("Opera",$Agent)) {
		$temp =explode("(", $Agent); $Part=$temp[1];
		$temp =explode(")", $Part); $browserver=$temp[1];
		$temp =explode(" ",$browserver);$browserver=$temp[2];
		$browserver =preg_replace("/([\d\.]+)/","\\1",$browserver);
		$browserver = " $browserver";
		$browser = "Opera";
	}
	if (ereg("Mozilla",$Agent) && ereg("MSIE",$Agent)) {
		$temp = explode("(", $Agent); $Part=$temp[1];
		$temp = explode(";",$Part); $Part=$temp[1];
		$temp = explode(" ",$Part);$browserver=$temp[2];
		$browserver =preg_replace("/([\d\.]+)/","\\1",$browserver);
		$browserver = " $browserver";
		$browser = "IE";
	}
	if ($browser!="") {
		$browseinfo = "$browser$browserver";
	}else {
		$browseinfo = "未知的浏览器";
	}
	return $browseinfo;
}

//获取操作系统类型
function osinfo() {
	$os="";
	$Agent =$_SERVER["HTTP_USER_AGENT"]?$_SERVER["HTTP_USER_AGENT"]:$HTTP_SERVER_VARS["HTTP_USER_AGENT"];
	if (eregi('win',$Agent) && strpos($Agent, '95')) {
		$os="Windows 95";
	}elseif (eregi('win 9x',$Agent) && strpos($Agent, '4.90')) {
		$os="Windows ME";
	}elseif (eregi('win',$Agent) && ereg('98',$Agent)) {
		$os="Windows 98";
	}elseif (eregi('win',$Agent) && eregi('nt 5\.0',$Agent)) {
		$os="Windows 2000";
	}elseif (eregi('win',$Agent) && eregi('nt 5\.1',$Agent)) { 
		$os="Windows XP"; 
	}elseif (eregi('win',$Agent) && eregi('nt',$Agent)) {
		$os="Windows NT";
	}elseif (eregi('win',$Agent) && ereg('32',$Agent)) {
		$os="Windows 32";
	}elseif (eregi('linux',$Agent)) {
		$os="Linux";
	}elseif (eregi('unix',$Agent)) {
		$os="Unix";
	}elseif (eregi('sun',$Agent) && eregi('os',$Agent)) {
		$os="SunOS";
	}elseif (eregi('ibm',$Agent) && eregi('os',$Agent)) {
		$os="IBM OS/2";
	}elseif (eregi('Mac',$Agent) && eregi('PC',$Agent)) {
		$os="Macintosh";
	}elseif (eregi('PowerPC',$Agent)) {
		$os="PowerPC";
	}elseif (eregi('AIX',$Agent)) {
		$os="AIX";
	}elseif (eregi('HPUX',$Agent)) {
		$os="HPUX";
	}elseif (eregi('NetBSD',$Agent)) {
		$os="NetBSD";
	}elseif (eregi('BSD',$Agent)) {
		$os="BSD";
	}elseif (ereg('OSF1',$Agent)) {
		$os="OSF1";
	}elseif (ereg('IRIX',$Agent)) {
		$os="IRIX";
	}elseif (eregi('FreeBSD',$Agent)) {
		$os="FreeBSD";
	}
	if ($os=='') $os = "Unknown";
	return $os;
}


?>