<?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){
global $graph_data;
for($i=0;$i<=$num;$i++) $graph_data[$i]=intval($graph_data[$i]);
}

function data_clear($num){
global $graph_data;
for($i=0;$i<=$num;$i++) $graph_data[$i]=0;
}

function total(){
global $config;
$start=file("$config[curr_user]/start.dat");
$total=(date("Y")-1)*365+date("z")-intval($start[0]);
return $total;
}

function autocut($chars,$maxlength){
if(strlen($chars)>=$maxlength) {
  $chars=substr($chars,0,$maxlength-3)."..";
  return $chars;
}else {
  return $chars;
}
}

function graph($graph_title,$data_num,$data_grad,$data_interval,$bar_width,$bar_space){
global $graph_data;
global $data_title;

$table_height="100";
$data_max=$graph_data[0];
$data_sum=$graph_data[0];
for($i=1;$i<$data_num;$i++) {
  if($graph_data[$i]>$data_max) $data_max=$graph_data[$i];
  $data_sum+=$graph_data[$i];
}
if(!$data_max) $data_max=1;
$degree=$data_max/$data_grad;
$body_width=intval((460-$bar_space-25)/$data_num*1.2);
?>

<!--  <?echo$graph_title;?> 图表开始  -->
<table border="0" width="460" cellspacing="0" cellpadding="0">
  <tr>
    <td width="100%" height="25" background="images/graph_border1.gif" align="left" valign="top">
      <b style="font-family: 宋体;line-height: 21px">&nbsp;&nbsp;<?echo$graph_title;?>统计</b>
    </td>
  </tr>
  <tr>
    <td width="100%" background="images/graph_border2.gif" align="center">
      <table border="0" height="100%" cellspacing="0" cellpadding="0">
        <tr>
          <td width="<?echo$body_width;?>" valign="top">
            <table border="0" width="100%" height="100%" align="right" cellspacing="0" cellpadding="0">
<?
    for($i=$data_grad;$i>=1;$i--) {
      echo"              <tr>\n";
      echo"                <td width=\"100%\" align=\"right\"><span style=\"font-family: 宋体\">&nbsp;&nbsp;</span>" ;echo(round($degree*$i));echo"</td>\n";
      echo"              </tr>\n";
    }
?>
            </table>
          </td>
<?
    for($i=0;$i<=$data_num-1;$i++) {
      echo"          <td width=\"$body_width\" height=\"$table_height\" align=\"center\" valign=\"bottom\">\n";
      echo"            <table border=\"0\" width=\"$bar_width\" cellspacing=\"0\" cellpadding=\"0\" class=\"graph\">\n";
      echo"              <tr>\n";
      echo"                <td width=\"100%\" valign=\"bottom\" align=\"center\"><img border=\"0\" src=\"images/bar_top_$bar_width.gif\" width=\"$bar_width\"></td>\n";
      echo"              </tr>\n";
      echo"             <tr>\n";
      echo"                <td width=\"100%\" height=\"";echo(intval($graph_data[$i]*$table_height/$data_max)-$bar_width/2);echo"\" background=\"images/bar_body_$bar_width.gif\" valign=\"bottom\"></td>\n";
      echo"             </tr>\n";
      echo"            </table>\n";
      echo"          </td>\n";
    }
    echo"        <tr>\n";
    echo"          <td align=\"right\">0</td>\n";
    for($i=0;$i<=$data_num-1;$i++) {
      echo"          <td height=30 align=\"center\">";
      if(is_int($i/$data_interval))  echo $data_title[$i];
      echo"</td>\n";
    }
?>
          <td width="<?echo$bar_space;?>"></td>
        </tr>
      </table>
    </td>
  </tr>
  <tr>
    <td width="100%" height="35" background="images/graph_border3.gif" valign="bottom"><b style="font-family: 宋体;line-height: 21px">&nbsp;&nbsp;<?echo$graph_title;?>数据</b></td>
  </tr>
  <tr>
    <td width="100%" background="images/graph_border2.gif">
      <table width="95%" height="100%" border="0" cellspacing="0" cellpadding="0" align="center" valign="middle" background="images/tablebg.gif">
        <tr>
          <td>
            <table width="80%" border="0" cellspacing="0" cellpadding="0" align="center">
<?
      for($i=0;$i<=$data_num-1;$i++) {
        @$data_percent=intval($graph_data[$i]*1000/$data_sum)/10;
        echo"              <tr valign=\"middle\">\n";
        echo"                <td height=\"20\" width=\"34%\" align=\"left\">".$data_title[$i]."</td>\n";
        echo"                <td height=\"20\" width=\"33%\" align=\"center\">".$data_percent."%</td>\n";
        echo"                <td height=\"20\" width=\"33%\" align=\"right\">".$graph_data[$i]."</td>\n";
        echo"              </tr>\n";
      }
?>
            </table>
          </td>
        </tr>
      </table>
    </td>
  </tr>
  <tr>
    <td width="100%" height="10" background="images/graph_border4.gif"></td>
  </tr>
</table>
<!--  <?echo$graph_title;?> 图表结束  -->

<?
}

function table($table_title,$data_num,$data_detail=0) {
global $config;
global $table_data;
global $data_title;
?>

<!--  <?echo$table_title;?> 表格开始  -->
<table width="460" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td background="images/graph_border1.gif" height="25" valign="top">
      <b style="font-family: 宋体;line-height: 21px">&nbsp;&nbsp;<?echo$table_title;?></b>
    </td>
  </tr>
  <tr>
    <td background="images/graph_border2.gif">
      <table width="460" border="0" cellspacing="0" cellpadding="0">
        <tr> 
          <td width="11">&nbsp;</td>
          <td background="images/tablebg.gif" align="center"> 
            <table width="98%" border="0" cellspacing="0" cellpadding="0">
<?
      if(!$data_detail) {
      	for($i=0;$i<=$data_num-1;$i++) {
          echo"              <tr>\n";
          echo"                <td height=\"20\" align=\"left\">".$data_title[$i]."</td>\n";
          echo"                <td height=\"20\" align=\"right\">".$table_data[$i]."</td>\n";
          echo"              </tr>";
        }
      }else {
        echo"               <tr>\n";
        echo"                 <td height=\"20\" align=\"center\">&nbsp;时 间&nbsp;</td>\n";
        echo"                 <td height=\"20\" width=\"1\"></td>\n";
 
        echo"                 <td height=\"20\" align=\"center\">&nbsp;&nbsp;&nbsp;I P&nbsp;&nbsp;&nbsp;</td>\n";
        echo"                 <td height=\"20\" width=\"1\"></td>\n";
 
        echo"                 <td height=\"20\" align=\"center\">&nbsp;地 区&nbsp;</td>\n";
        echo"                 <td height=\"20\" width=\"1\"></td>\n";
 
        echo"                 <td height=\"20\" align=\"center\">&nbsp;&nbsp;页 面&nbsp;&nbsp;</td>\n";
        echo"                 <td height=\"20\" width=\"1\"></td>\n";

        echo"                 <td height=\"20\" align=\"center\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;来 自&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>\n";
        echo"               </tr>\n";
      	for($i=0;$i<=$data_detail-1;$i++) {
          $table_data_processed[0]=autocut($table_data[$i][2],9);
          $table_data_processed[1]=eregi_replace("^".$config[user_base]."|http://$string","$string",$table_data[$i][3]);
          $table_data_processed[1]=autocut($table_data_processed[1],12);
          $table_data_processed[2]=eregi_replace("^http://$string","$string",$table_data[$i][4]);
          $table_data_processed[2]=autocut($table_data_processed[2],28);
          echo"               <tr>\n";
          echo"                 <td height=\"20\" align=\"center\" class=\"small\">".$table_data[$i][0]."</td>\n";
          echo"                 <td height=\"20\" width=\"1\" bgcolor=\"#F7EF72\"></td>\n";
          echo"                 <td height=\"20\" align=\"center\" class=\"small\">".$table_data[$i][1]."</td>\n";
          echo"                 <td height=\"20\" width=\"1\" bgcolor=\"#F7EF72\"></td>\n";
          echo"                 <td height=\"20\" align=\"center\"><font title=\"".$table_data[$i][2]."\">".$table_data_processed[0]."</font></td>\n";
          echo"                 <td height=\"20\" width=\"1\" bgcolor=\"#F7EF72\"></td>\n";
          echo"                 <td height=\"20\" align=\"center\" class=\"small\"><a href=\"".$table_data[$i][3]."\" title=\"".$table_data[$i][3]."\" target=\"_blank\">".$table_data_processed[1]."</a></td>\n";
          echo"                 <td height=\"20\" width=\"1\" bgcolor=\"#F7EF72\"></td>\n";
          echo"                 <td height=\"20\" align=\"center\" class=\"small\"><a href=\"".$table_data[$i][4]."\" title=\"".$table_data[$i][4]."\" target=\"_blank\">".$table_data_processed[2]."</a></td>\n";
          echo"               </tr>\n";
        }
      }
?>
            </table>
          </td>
          <td width="5"></td>
          <td width="11" background="images/graph_border5.gif">&nbsp;</td>
          <td width="9"></td>
        </tr>
      </table>
    </td>
  </tr>
  <tr>
    <td background="images/graph_border4.gif" height="10"></td>
  </tr>
</table>
<!--  <?echo$table_title;?> 表格结束  -->

<?
}
########################################################################################

include "header.inc";

if($user!="" and is_dir("$config[data]/$user")) {
  $config[curr_user]="$config[data]/$user";
  include "$config[curr_user]/config.inc.php";  
}else {
  $config[curr_user]=$config[data];
  $config[user_name]=$config[name];
  $config[user_base]=$config[base];
}

$curr_date[1]=date("y");  //检查日期改变情况
$curr_date[2]=date("m");
$curr_date[3]=date("d");
$data=file("$config[curr_user]/click.dat");
$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; 
}

echo"\n<!--  ST REPORT  -->\n";
//graph()函数调用格式:
//graph(表格标题,数据数量,刻度数量,数据标签间隔显示,数据条图像宽度,空白调配)

if($type=="general") {

  $data=file("$config[curr_user]/click.dat");
  if(ereg("1",$flag)) $data[1]=$data[2]=$data[3]=0;
  elseif(ereg("2",$flag)) $data[2]=$data[3]=0;
  elseif(ereg("3",$flag)) $data[3]=0;
  $table_data[0]="<a href=\"$config[user_base]\" target=\"_blank\">$config[user_name]</a>";
  $table_data[1]=total();
  $table_data[2]=intval($data[0]);
  $table_data[3]=intval($data[1]);
  $table_data[4]=intval($data[2]);
  $table_data[5]=intval($data[3]);
  $table_data[6]=round($table_data[2]/$table_data[1]);
  $table_data[7]=round(25*$table_data[5]/(1+date("G")));
  $data_title=array("站点名称","总统计天数","总访问量","今年访问量","本月访问量","今日访问量","平均每日访问量","预计今日访问量");
  table("站点概况",8);

  echo"<br>";
  $fdata=fopen("$config[curr_user]/detail.dat","r");
  for($i=0;$i<10;$i++) {
    $data=Chop(fgets($fdata,500));
    $table_data[$i]=explode($config[division],$data);
  }
  table("最后10位访客详情",0,10);

}elseif($type=="year") {

  if(ereg("1",$flag)) data_clear(11);
  else {
    $graph_data=file("$config[curr_user]/month.dat");
    data_init(11);
  }
  for($i=0;$i<=11;$i++) $data_title[$i]=($i+1)."月";
  graph("本年度按月访问",12,5,1,15,15);

  echo"<br>";
  if(ereg("1",$flag) or ereg("2",$flag)) data_clear(date("t")-1);
  else {
    $graph_data=file("$config[curr_user]/day.dat");
    data_init(date("t")-1);
  }
  for($i=0;$i<=date("t")-1;$i++) $data_title[$i]=$i+1;
  graph("本月份按日访问",date("t"),5,3,10,20);

}elseif($type=="hour") {

  $graph_data=file("$config[curr_user]/hour.dat");
  data_init(23);
  for($i=0;$i<=23;$i++) $data_title[$i]=$i;
  graph("总访问量小时分布",24,5,2,10,15);
  
}elseif($type=="detail") {

  $fdata=fopen("$config[curr_user]/detail.dat","r");
  for($i=0;$i<=$config[detail]-1;$i++) {
    $data=Chop(fgets($fdata,500));
    $table_data[$i]=explode($config[division],$data);
  }
  table("最后".$config[detail]."位访客详情",0,$config[detail]);

}elseif($type=="pageview") {

  $graph_data=file("$config[curr_user]/pageview.dat");
  data_init($config[pvs]+1);
  for($i=0;$i<=$config[pvs]-1;$i++) $data_title[$i]=$i+1;
  $data_title[$config[pvs]]="＞".$config[pvs];
  graph("页面流量<span style=\"font-family: Arial\">pageview</span>",$config[pvs]+1,5,1,15,15);

}elseif($type=="agent") {
	
  $graph_data=file("$config[curr_user]/agent.dat");
  data_init(13);
  $data_title=array("IE 4.x","IE 5.x","IE 6.x","Netscape","Opera","NetCaptor","其他");
  graph("客户端浏览器",7,5,1,30,40);

  echo"<br>"; 
  for($i=0;$i<=6;$i++) $graph_data[$i]=$graph_data[$i+7];
  $data_title=array("Win9x","WinNT","Win2000","WinXP","类Unix","MacOS","其他");
  graph("客户端操作系统",7,5,1,30,40);

}else die("未定义操作!");

include "footer.inc";
?>