<?php
//----------------------------------------------------------------------
// 创想中文修改版
// 技术支持：http://www.cx365.com/stat/
// 程序演示：http://www.cx365.com/php/stat/
//  Stat Therefor 2002 alpha 0110  (创想中文修改版 )
//----------------------------------------------------------------------
// Copyright(C) Dai Zhikang, Crossday Studio, 2002. All rights reserved
//
//          所以网络空间  www.crossday.com  www.therefor.net
//----------------------------------------------------------------------
//     免费软件  ALWAYS FREEWARE!  *技术支持 <webmaster@therefor.net>
//----------------------------------------------------------------------
//   欢 迎 您 指 出 存 在 的 b u g 和 问 题 ,  提 出 您 的 宝 贵 意 见
//----------------------------------------------------------------------
// 软件为作者利用数天的休息时间辛勤劳作而写成,请您保护我所保留的很少一点
// 权利, 您在使用本软件时请勿擅自修改页面上作者署名及版权信息,谢谢!谢谢!
//----------------------------------------------------------------------

require "config.inc.php";

function data_init($num,$handle=0){
global $data;
for($i=0;$i<=$num;$i++) $data[$i]=intval($data[$i]);
if($handle) $data[$handle]++;
}

function file_write($fwrite,$filename){
@$data=fopen($filename,"w");
@flock($data,3);
@fwrite($data,$fwrite);
@fclose($data);
}

function restart($num,$handle,$filename){
for($i=0;$i<$handle;$i++) $fwrite=$fwrite."0\n";
$fwrite=$fwrite."1\n";
for($i=$handle+1;$i<=$num;$i++) $fwrite=$fwrite."0\n";
file_write($fwrite,$filename);
}

function refresh($num,$handle,$filename){
global $data;
data_init($num);
for($i=0;$i<$handle;$i++) $fwrite=$fwrite.$data[$i]."\n";
$fwrite=$fwrite.($data[$handle]+1)."\n";
  //echo"<br>--$fwrite";  //debug
for($i=$handle+1;$i<=$num;$i++) $fwrite=$fwrite.$data[$i]."\n";
  //echo"<br>++$fwrite";  //debug
file_write($fwrite,$filename);
}

function convertip($ip){
$ip_detail=explode(".",$ip);
if (file_exists("ip/$ip_detail[0].txt")) $ip_data=file("ip/$ip_detail[0].txt");
else $ip_data=file("ip/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]) return $ip_data_detail[2];
}
if ($ip_detail[0]=="127") return "本地机器";
return "";
}

#################################################################################

$config[curr_user]="$config[data]/$user";

if(!file_exists("$config[curr_user]/start.dat"))
  file_write(((date("Y")-1)*365+date("z")-1).".date ".date("Y-m-d"),
             "$config[curr_user]/start.dat");


//记录单机Pvs(cookie支持)于pageview.dat中
$pvs=$config[pvs];
if(!$stat_pid or $stat_pid!="$SERVER_NAME$PHP_SELF") {

  $stat_pid="$SERVER_NAME$PHP_SELF";
  setcookie("stat_pid",$stat_pid,time()+$config[cookietime]);
  $pageview=0;

}

$pageview++;
setcookie("pageview",$pageview,time()+$config[cookietime]);

@$data=file("$config[curr_user]/pageview.dat");
data_init($pvs+1);

if($pageview<=($pvs+1)) {

  $data[$pageview-1]++;
  $data[$pageview-2]--;

}

$fwrite="";
for($i=0;$i<=($pvs-1);$i++) {

  $j=$i+1;
  $fwrite=$fwrite.$data[$i].".Pvs $j\n";

}

$fwrite="$fwrite$data[$pvs].Pvs$pvs+";
file_write($fwrite,"$config[curr_user]/pageview.dat");


//记录$config[detail]个用户的详细数据到detail.dat
@$data=file("$config[curr_user]/detail.dat");
$fwrite=date("y-n-j")." ".date("G:i:s").$config[division].
        $REMOTE_ADDR.$config[division].
        convertip($REMOTE_ADDR).$config[division].
        $HTTP_REFERER.$config[division].
        $referer."\n";

for($i=0;$i<=$config[detail]-2;$i++)
  $fwrite=$fwrite.trim($data[$i])."\n";

file_write($fwrite,"$config[curr_user]/detail.dat");


//读取及准备记录总浏览数及当年、月、日浏览数于click.dat
$curr_date[1]=date("y");
$curr_date[2]=date("m");
$curr_date[3]=date("d");
@$data=file("$config[curr_user]/click.dat");
$data[0]=intval($data[0])+1;
$flag="";  //日期改变标志
for($i=1;$i<=3;$i++) {
  if(intval(substr($data[$i],-4))==$curr_date[$i]) {
    $data[$i]=intval($data[$i]);
    $data[$i]++;
  }else $flag=$flag.$i;  //判断年月日何值改变 $flag存在1,2即年、月发生改变
  if($flag) $data[$i]=1;
}

$count_total=$data[0];
$count_month=$data[2];
$count_today=$data[3];


if(!$config[anti_refresh] or ($config[anti_refresh] and $pageview==1)) {  //判断是否刷新

  //写入click.dat
  file_write("$data[0].total\n$data[1].year   $curr_date[1]\n$data[2].month   $curr_date[2]\n$data[3].day   $curr_date[3]","$config[curr_user]/click.dat");

  //判断浏览器与操作系统并记录于agent.dat中
  $info=$HTTP_USER_AGENT;
  @$data=file("$config[curr_user]/agent.dat");
  data_init(13);

  if(strpos($info,"NetCaptor")) $data[5]++;	//NetCaptor
  elseif(strpos($info,"MSIE 6")) $data[2]++;	//MSIE 6.x
  elseif(strpos($info,"MSIE 5")) $data[1]++;	//MSIE 5.x
  elseif(strpos($info,"MSIE 4")) $data[0]++;	//MSIE 4.x
  elseif(strpos($info,"Netscape")) $data[3]++;	//Netscape
  elseif(strpos($info,"Opera")) $data[4]++;	//Opera
  else $data[6]++;				//未知浏览器

  if(strpos($info,"Windows NT 5.0")) $data[9]++;		//Win 2000
  elseif(strpos($info,"Windows NT 5.1")) $data[10]++;	//Win XP
  elseif(strpos($info,"Windows NT")) $data[8]++;		//Win NT
  elseif(strpos($info,"Windows 9")) $data[7]++;		//Win 9x
  elseif(strpos($info,"unix")
         or strpos($info,"linux")
         or strpos($info,"SunOS")
         or strpos($info,"BSD"))
         $data[11]++;				//Unix/Linux/SunOS/NetBSD/FreeBSD
  elseif(strpos($info,"Mac")) $data[12]++;	//Mac
  else $data[13]++;				//未知系统
  file_write("$data[0].IE4.x\n$data[1].IE5.x\n$data[2].IE6.x\n$data[3].Netscape\n$data[4].Opera\n$data[5].NetCaptor\n$data[6].otherBrowser\n$data[7].Win9x\n$data[8].WinNT\n$data[9].Win2000\n$data[10].WinXP\n$data[11].Linux\n$data[12].MacOS\n$data[13].otherOS","$config[curr_user]/agent.dat");


  //记录访问量时段分布于hour.dat
  @$data=file("$config[curr_user]/hour.dat");
  refresh(22,date("G"),"$config[curr_user]/hour.dat");


  //记录当年,当月详细访问量
  if(is_int(strpos($flag,"1"))) {

    restart(11,date("m")-1,"$config[curr_user]/month.dat");
    restart(date("t")-1,date("j")-1,"$config[curr_user]/day.dat");

  }elseif(is_int(strpos($flag,"2"))) {

    @$data=file("$config[curr_user]/month.dat");
    refresh(11,date("m")-1,"$config[curr_user]/month.dat");
    restart(date("t")-1,date("j")-1,"$config[curr_user]/day.dat");

  }else {

    @$data=file("$config[curr_user]/month.dat");
    refresh(11,date("m")-1,"$config[curr_user]/month.dat");

    @$data=file("$config[curr_user]/day.dat");
    data_init(date("j")-1);
    refresh(date("t")-1,date("j")-1,"$config[curr_user]/day.dat");

  }
}

if($user!="") $user="?user=$user";

if($style=="null") {

}elseif($style=="counter") {

  echo"document.write(\"<table width='88' border='0' cellspacing='0' cellpadding='0' height='31' background='\"+url+\"/images/stat_counter.gif'>\");";
  echo"document.write(\"  <tr>\");";
  echo"document.write(\"    <td height='3' width='24'></td>\");";
  echo"document.write(\"    <td height='3' width='57'></td>\");";
  echo"document.write(\"    <td height='3' width='7'></td>\");";
  echo"document.write(\"  </tr>\");";
  echo"document.write(\"  <tr>\");";
  echo"document.write(\"    <td height='16'></td>\");";
  echo"document.write(\"    <td height='16' align='center' valign='top'><marquee behavior='loop' scrollDelay='100' scrollAmount='3' style='font-size: 12px; line-height=15px'>\");";
  echo"document.write(\"      <a href='\"+url+\"/index.php$user' target='_blank' style='color: #ffffff; text-decoration: none'>\");";
  echo"document.write(\"        <font face='Arial, Verdana, san-serif' color='#ffffff'>总访问量:".$count_total." 本月访问:".$count_month." 今日访问:".$count_today."</font>\");";
  echo"document.write(\"      </a></marquee>\");";
  echo"document.write(\"    </td>\");";
  echo"document.write(\"    <td height='16'></td>\");";
  echo"document.write(\"  </tr>\");";
  echo"document.write(\"  <tr>\");";
  echo"document.write(\"    <td height='12'></td>\");";
  echo"document.write(\"    <td height='12'></td>\");";
  echo"document.write(\"    <td height='12'></td>\");";
  echo"document.write(\"  </tr>\");";
  echo"document.write(\"</table>\");";

}elseif($style=="icon") {

  echo"document.write(\"<a href='\"+url+\"/index.php$user' target='_blank'><img src='\"+url+\"/images/stat_icon.gif' height='20' width='20' border='0' alt='总访问量 ".$count_total."|本月访问 ".$count_month."|今日访问 ".$count_today."'></a>\");";	

}elseif($style=="text") {

  echo"document.write(\"总访问量:".$count_total."&nbsp;&nbsp;&nbsp;本月访问:".$count_month."&nbsp;&nbsp;&nbsp;今日访问:".$count_today."&nbsp;&nbsp;&nbsp;你的访问:".$pageview."\");";

}else {

  echo"document.write(\"<a href='\"+url+\"/index.php$user'><img src='\"+url+\"/images/stat_icon.gif' height='20' width='20' border='0'></a>\");";	

}

?>