<?
 /*------------------------------------------------------------------------
 程序名称：yucount v 1.0
 版权所有: snowai
 E-mail　: snowai@163.com
 OICQ　　: 191494
 PHP文本计数器,仿雷傲统计版面，免费使用,如有问题或更好修改建议请来信告知
 配置说明见config.php
 调用方式：<script src="counter.php"></script>
------------------------------------------------------------------------*/
  include("config.php");
  $nowhour=date("H");
  $nowtime=date("Y-m-d H:i:s");
  $nowday=date("Y-m-d");
  $nowmonth=date("Y-m");
  $ip=getenv("REMOTE_ADDR");
  
//----------------save the counter info---------------------  
function writeText($path,$content,$type)
{
  $fp=fopen($path,$type);
  flock($fp,LOCK_EX);
  fwrite($fp,$content);
  fclose($fp);
}
//-----------------the User's Browse------------------------  
function browseinfo() 
{
  $browser="";$browserver="";
  $Browsers =array("Lynx","MOSAIC","AOL","Opera","JAVA","MacWeb","WebExplorer","OmniWeb");
  $Agent = $GLOBALS["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 = "Internet Explorer";
  }
  if ($browser!="") 
  {
    $browseinfo = "$browser$browserver";
  }
  else 
  {
    $browseinfo = "Unknown";
  }
  return $browseinfo;
}

//-----------------the User's OS------------------------
function osinfo() 
{
  $os="";
  $Agent = $GLOBALS["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',$Agent)) {
  $os="Windows NT";
  }
  elseif (eregi('win',$Agent) && eregi('nt 5\.1',$Agent)) { 
  $os="Windows XP"; 
  } 
  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;
}  

//+-------where the IP comes from------------------
function convert_ip($ip) 
{
	$ip_detail=explode(".",$ip);
	if ($ip_detail[0]=="127") return "本地机器";
	if (file_exists("ipdata/$ip_detail[0].txt")) $ip_data=file("ipdata/$ip_detail[0].txt");
	else $ip_data=file("ipdata/0.txt");
	for ($i=0; $i<=3; $i++) 
	{
		$ip_detail[$i] = sprintf("%03d", $ip_detail[$i]);
	}
	$ip=join(".",$ip_detail);
	$count=count($ip_data);
	$count=$count-1;
	for ($i=0; $i<=$count; $i++) 
	{
		$ip_data_detail=explode("|",$ip_data[$i]);
		if ($ip>=$ip_data_detail[0] && $ip<=$ip_data_detail[1]) 
		{
		  $fromwhere=$ip_data_detail[2].$ip_data_detail[3];
		  return $fromwhere;
		}
	}
	return "未知地区";
}

function stats($path,$sort)
{
  $data=file($path);
  $text="";
  for ($i=0;$i< count($data);$i++)
  {
    $content=trim($data[$i]);
    if (strpos($content,$sort)===0)
    {
      $tag=1;
      $content=explode("|",$content);
      $clicks=$content[1]+1;
      $text.=$content[0]."|".$clicks."\n";
    }
    else
    {
      $text.=$content."\n";
    }
  }
  if (empty($tag))
  $text.=$sort."|1\n";
  writeText($path,$text,"w");
} 
//-------------initialize---------------------
if (!file_exists($count))
{ 
  writeText($count,"","w");
}
if (!file_exists($explorer))
{ 
  writeText($explorer,"","w");
}
if (!file_exists($lastdata)) 
{ 
  writeText($lastdata,"","w");
}
if (!file_exists($place)) 
{ 
  writeText($place,"","w");
} 
if (!file_exists($system)) 
{ 
  writeText($system,"","w");
} 
if (!file_exists($month))
{
  $text="";
  for ($i=1;$i<= 12;$i++)
  {
    $m = sprintf("%02d",$i);
    $text.=date("Y")."-".$m."|0\n";
  }
  writeText($month,$text,"w");
}  

if (!file_exists($hour))
{
  $text="";
  for ($i=0;$i< 24;$i++)
  {
    $m = sprintf("%02d",$i);
    $text.=$m.":00-".$m.":59|0\n";
  }
  writeText($hour,$text,"w");
}  
  
//---$totalcount:总访问数,$monthcount:月访问数,$todaycount:日访问数,$countdays:统计天数
  $data=file($count);
  $content=explode("|",$data[0]);
  $totalcount=$content[0]+1;
  $lastday=trim($content[4]);//上次访问日期
  $lastmonth=substr($lastday,0,7);
  $monthcount=$nowmonth==$lastmonth?$content[1]+1:1;
  $todaycount=$lastday==$nowday?$content[2]+1:1;
  $countdays=$lastday==$nowday?$content[3]:$content[3]+1;
  $text=$totalcount."|".$monthcount."|".$todaycount."|".$countdays."|".$nowday."\n";
  $average=round($totalcount/$countdays);
  writeText($count,$text,"w");

//---------------统计最后N条访问记录-----------------------
  $data=file($lastdata);
  $num=count($data);
  $statnum=$statnum>$num?$num:$statnum-1;
  $os=osinfo();
  $browser=browseinfo();
  $area=convert_ip($ip);
  $content=$REMOTE_ADDR.'('.$area.')|'.$browser.' '.$os.'|'.$nowtime."\n";
  for($i=0;$i< $statnum;$i++)
  {
    $text=trim($data[$i])."\n";
    $content.=$text;
  }
  writeText($lastdata,$content,"w");
//---------------------按月统计------------------------------
  $data=file($month);
  $text="";
  for ($i=1;$i<=12;$i++)
  {
    $content=trim($data[$i-1]);
    if (strpos($content,$nowmonth)===0)
    {
      $content=explode("|",$content);
      $clicks=$content[1]+1;
      $text.=$content[0]."|".$clicks."\n";
    }
    else
    {
      $text.=$content."\n";
    }
  }
  writeText($month,$text,"w");
//--------------------按时间段统计--------------------------
  $data=file($hour);
  $text="";
  for ($i=0;$i< 24;$i++)
  {
    $content=trim($data[$i]);
    if (strpos($content,$nowhour)===0)
    {
      $content=explode("|",$content);
      $clicks=$content[1]+1;
      $text.=$content[0]."|".$clicks."\n";
    }
    else
    {
      $text.=$content."\n";
    }
  }
  writeText($hour,$text,"w");
  
//----按浏览器stats($explorer,$browser),地区($place,$area),操作系统($system,$os)统计----
  stats($explorer,$browser);
  stats($place,$area);
  stats($system,$os);

//---------------输出数据----------
   echo  "document.write(\"<form name=counter><input readonly type=text onclick=window.open('view.php') name=countshow size=16 style='font-size:9pt; cursor:hand; color:#FFFFff; background-color:#000000; text-align:center;border:3 ridge #00ff00'></form>\");s=new Array;s[0]='总访问量 $totalcount 次 ';s[1]='本月访问量 $monthcount 次 ';s[2]='今日访问量 $todaycount 次 ';s[3]=' 统计天数 $countdays 天 ';s[4]='日平均访问 $average 次';s[5]='IP:$ip ';var id, p=0;function show(){document.counter.countshow.value=s[p];p=p+1;if (p>5){p=0;}id=setTimeout('show()', 2000);}show()";    
?>