<?php
############ 定义一些变量 可以修改 ##############
$xpower[webname]	= '酷虫在线';
$xpower[weburl]		= 'http://xpower.jilinfarm.com';
$xpower[master]		= '蓝鸟';
$xpower[mail]		= 'moth3k@163.com';
$xpower['maxlen']	= 100;			//
$xpower['ippath']	= 'ip/';
$xpower['path']		= 'data/';		//访问者数据存储路径
$xpower['d_default']= 'default.txt';//存储概况信息
$xpower['d_detail']	= 'detail.txt';	//存储访问者时间（年月日）
$xpower['d_browse']	= 'browse.txt';	//存储访问者浏览器
$xpower['d_os']		= 'os.txt';	    //存储操作系统
$xpower['d_hour']	= 'hour.txt';	//存储统计24小时流量数据
$xpower['d_day']	= 'day.txt';	//存储统计日流量数据
$xpower['d_week']	= 'week.txt';   //存储统计周流量数据
$xpower['d_month']	= 'month.txt';	//存储统计月流量数据
$xpower['d_year']	= 'year.txt';	//存储统计年流量数据

$xpower['ip']		= $HTTP_SERVER_VARS['REMOTE_ADDR']; 		//访问者的IP
$xpower['ipfrom']	= '';		//把IP转换成地理位置的值
$xpower['time']		= time();	//当前时间

############## 以下内容不必修改 ####################
$names      =  array(	// 名称
	'week'  => array('星期日','星期一','星期二','星期三','星期四','星期五','星期六'),
	'browse'=> array('NetCaptor','IE 6.X','IE 5.x','IE 4.x','Netscape','Opera','其他'),
	'os'    => array('Windows2000','WindowsXP','WindowsNT','Windows98','Unix','Linux','SunOS','FreeBSD','Mac','Other')
);
$images     =  array(	// 图标
	'os'	=> array('winnt','winxp','winnt','windows','linux','linux','sunos','freebsd','mac','other'),
	'browse'=> array('netcaptor','msie6','msie','msie','netscape','opera','other')
);
$keywords   =  array(	// 关键字
	'os'    => array('Windows NT 5.0','Windows NT 5.1','Windows NT','Windows 98','unix','linux','SunOS','BSD','Mac'),
	'browse'=> array('NetCaptor','MSIE 6','MSIE 5','MSIE 4','Netscape','Opera')
	);
//------------------变量定义结束------------------//


function readfromfile($file_name) { //Read file
	$filenum=fopen($file_name,'r');
	flock($filenum,LOCK_SH);
	$file_data=fread($filenum,filesize($file_name));
	fclose($filenum);
	return $file_data;
}

function writetofile($file_name,$data,$method='w') { //Write file
	$filenum=fopen($file_name,$method);
	flock($filenum,LOCK_EX);
	$file_int=fwrite($filenum,$data);
	fclose($filenum);
	return $file_int;
}
//分析 $HTTP_USER_AGENT 返回 id 号
function get_agent($keywords){
	global $HTTP_USER_AGENT;
	for($i=1;$i<count($keywords);$i++){
			if(strpos($HTTP_USER_AGENT,$keywords[$i])) return $i;
	}
	return $i;
}

function convertip($ip){
	global $xpower;
	$ip_var=explode('.',$ip);
	if ($ip_var[0]==127) $address= "本地机器";
	elseif ($ip_var[0]==192) $address= "局域网机器";
	else {
	// here is very good to seek ip address
		if (file_exists("$xpower[ippath]/$ip_var[0].csv")) $filelines=file("$xpower[ippath]/$ip_var[0].csv");
		else $filelines=file('$xpower[ippath]/0.csv');
		for ($i=0; $i<=3; $i++) {
			 $ip_var[$i]=sprintf("%03d", $ip_var[$i]);
		}
		$ip=implode('.',$ip_var);
	
		$a=0;
		$b=count($filelines);
		while($a<($b-1)){
			$t=floor(($a+$b)/2);
			$tmp=explode(',',$filelines[$t]);
			if(strcmp($ip,$tmp[0])>=0 && strcmp($ip,$tmp[1])<=0 ){
				$address=trim($tmp[2].$tmp[3]);
				break;
			}
			if(strcmp($ip,$tmp[0])>0) $a=$t;
			else $b=$t;
			if(!$a<($b-1)) $address='未知地区';
		}
	}
	return $address;
}
?>