<?
require'global.php';

p_head();
if($by=='hour' || $by=='day' || $by=='month' || $by=='year' || $by=='week' || $by=='browse' || $by=='os') show($by);
elseif($by=='visit') 
	show_visit();
else 
	show_default();
p_end();

//  ============= Sub Functions ==============

function show_default(){
	global $xpower,$names,$keywords;
	$fname       =    $xpower[path].$xpower[d_default];
	$data        =    file($fname);
	$total       =    trim($data[7]);

	$totalday    =    ceil((strtotime($data[0])-strtotime($data[8]))/86400);
	if ($totalday ===0) $totalday=1;		// === 请注意!
	// echo $totalday;		//debug
	$average_d   =    ceil($total/$totalday);
	$average_m   =    ceil(30*$total/$totalday);
	$perfore     =    round(25*trim($data[5])/(1+date('G')));
	// 来访者信息
	$ipfrom      =    convertip($xpower[ip]);
	$browse_id   =    get_agent($keywords[browse]);
	$os_id       =    get_agent($keywords[os]);	
	$browse      =    $names[browse][$browse_id];
	$os          =    $names[os][$os_id];
	
	print<<< EOT
<div class=title><b> 网站流量统计概况 </b></div>
<table border=0 width=700 cellspacing=1 cellpadding=3 bgcolor=#333333>
<!--tr><td colspan=4 bgcolor=#f0f4f8>　</td></tr-->
<tr><td bgcolor=#f0f8ff>网站名称</td><td bgcolor=#f8f8f8> <a href=$xpower[weburl]>$xpower[webname]</a> </td>
	<td bgcolor=#f0f8ff>本站站长</td><td bgcolor=#f8f8f8> <a href=mailto:$xpower[mail]>$xpower[master]</a></td></tr>
<tr><td bgcolor=#f0f8ff>启用时间</td><td bgcolor=#f8f8f8> $data[8] </td>
    <td bgcolor=#f0f8ff>统计天数</td><td bgcolor=#f8f8f8> $totalday 天</td></tr>
<tr><td bgcolor=#f0f8ff>访问总量</td><td bgcolor=#f8f8f8> $data[7] 人次</td>
    <td bgcolor=#f0f8ff>日均流量</td><td bgcolor=#f8f8f8> $average_d 人次</td></tr>
<tr><td bgcolor=#f0f8ff>月均流量</td><td bgcolor=#f8f8f8> $average_m 人次</td>
    <td bgcolor=#f0f8ff>预计今日流量</td><td bgcolor=#f8f8f8> $perfore 人次</td></tr>
<tr><td colspan=4 bgcolor=#f0f4f8>基本流量统计信息</td></tr>
<tr><td bgcolor=#f0f8ff>今日流量</td><td bgcolor=#f8f8f8> $data[5] 人次</td>
    <td bgcolor=#f0f8ff>昨日流量</td><td bgcolor=#f8f8f8> $data[6] 人次</td></tr>
<tr><td bgcolor=#f0f8ff>本月流量</td><td bgcolor=#f8f8f8> $data[3] 人次</td>
    <td bgcolor=#f0f8ff>上月流量</td><td bgcolor=#f8f8f8> $data[4] 人次</td></tr>
<tr><td bgcolor=#f0f8ff>今年流量</td><td bgcolor=#f8f8f8> $data[1] 人次</td>
    <td bgcolor=#f0f8ff>去年流量</td><td bgcolor=#f8f8f8> $data[2] 人次</td></tr>
<tr><td colspan=4  bgcolor=#f0f4f8> 您的来访信息 </td></tr>
<tr><td bgcolor=#f0f8ff>您的浏览器</td><td bgcolor=#f8f8f8> $browse </td>
    <td bgcolor=#f0f8ff>您的操作系统</td><td bgcolor=#f8f8f8> $os </td></tr>
<tr><td bgcolor=#f0f8ff>您的IP地址</td><td bgcolor=#f8f8f8> $xpower[ip] </td>
    <td bgcolor=#f0f8ff>您的地理信息</td><td bgcolor=#f8f8f8> $ipfrom </td></tr>
</table>
EOT;
}

function show_visit(){
	global $xpower,$images,$names;
	
	$filelines     =    file($xpower[path].$xpower[d_detail]);
	$tb_num        =    count($filelines);
	for($i=0;$i<$tb_num;$i++){
		$data      =    explode('|',trim($filelines[$i]));
		$url='';
		if($data[3]) 
			$url   =    '<a href="'.$data[3].'" target=_blank>'.substr($data[3],0,25).'...</a>';
		$tb_detail.=    '<tr><td bgcolor=#f0f8ff>'.date('Y-m-d H:i:s',$data[0]).'</td><td bgcolor=#f8f8f8>'.$data[1].'</td><td bgcolor=#f0f8ff>'.$data[2].'</td><td bgcolor=#f8f8f8 title="'.$data[3].'">'.$url.'</td><td  bgcolor=#f0f8ff><img src=images/soft/' .$images[browse][$data[5]]. '.gif width=14 height=14 alt="' .$names[browse][$data[5]].'"></td><td bgcolor=#f8f8f8><img src=images/soft/' .$images[os][$data[4]]. '.gif width=14 height=14 alt="'.$names[os][$data[4]]."\"></td></tr>\n";
	}
		print <<< EOT
<div class=title><b> 最近 $tb_num 位访客人详细情况 </b></div>
<table border=0 cellspacing=1 cellpadding=3 width=700 bgcolor=#333333>
<tr><td bgcolor=#90ccff>来访时间</td><td bgcolor=#90ccff>来访IP</td><td bgcolor=#90ccff width="170">地理位置</td><td bgcolor=#90ccff>访问路径</td><td bgcolor=#90ccff>访客浏览器</td><td bgcolor=#90ccff>访客操作系统</td></tr>
$tb_detail
</table>
EOT;
}

function show($type){
	global $xpower,$names,$images;
	$filelines    =    file($xpower[path].$xpower["d_$type"]);
	$sum          =    0;
	$index        =    explode(':',trim($filelines[0]));
	$sumlines     =    $index[0]+$index[1];
	// 生成数组 $num[0] ... $num[$sumlines-1]
	for($i=1;$i<=$sumlines;$i++){
		$num[$i-1]  =    trim($filelines[$i]);
		$sum     +=    $num[$i-1];
	}
    for($i=$index[1];$i<$sumlines;$i++){	// $i=$index[1] to count($num)
    	if($sum===0)
    		$precent=0;
    	else
    		$precent=sprintf("%.2f",100*$num[$i]/$sum);
    	
    	if($num[$i]){
	    	if($type=='browse' || $type=='os' || $type=='week')	
    			$title = " title=\"○ $index[2]【".$names[$type][$i]."】流量 $num[$i] 人次\n◎ 占总流量 $precent%\"";
    		else 
    			$title = " title=\"○ $index[2]$i$index[3]流量 $num[$i] 人次\n◎ 占$index[4] $precent%\"";
    	}else
    		$title='';
    	$height=round(1.96*$precent)+4;
    	// 柱状图
    	$tb_grah.= "\n<td valign=bottom$title><img src=images/line2.gif height=$height width=10></td>";
    	// X 系列
    	if($type=='browse' || $type=='os')
    		$tb_text.='<td class=shadow1 height=25><img src=images/soft/' .$images[$type][$i]. '.gif width=14 height=14> ' .$names[$type][$i]. "</td>";
    	elseif($type=='week')
    		$tb_text.="<td class=shadow1 height=25>" .$names[$type][$i]. "</td>";
    	else
    		$tb_text.="<td class=shadow1 height=25> $i </td>";
    }
	// 标题
 	$tb_title   =    $index[2].' 每'.$index[3].'流量统计 【总计:' .$sum. '】';
    // Y 系列
    for($i=4;$i>=0;$i--){
		$tb_num.="<tr><td> $sum </td></tr>";
		$sum=ceil($sum*($i/5));
	}
    
    print <<< EOT
<table border=0 width=700 cellspacing=0 cellpadding=0>
<tr>
<td width=40 rowspan=2 height=100%>
	<table border=0 height=100%>
$tb_num
	</table>
</td>
<td>
     <div class=title><b> $tb_title </b></div>
</td>
</tr>
<tr>
<td style="background-repeat: repeat-x" background=images/table_bg.gif bgcolor=#ffffff>
	<table border=0 width=100% height=100% background=images/form_bg.gif style="border-style: solid; border-width: 0" cellspacing=0 cellpadding=0>
	<tr><td width=1 valign=top><img src=images/form_v.gif></td>
$tb_grah
	<td  width=1 valign=top><img src=images/form_v.gif></td></tr>
	<tr><td width=1></td>
$tb_text
	<td width=1></td></tr>
	</table>
</td>
</tr>
</table>
<p><p>
EOT;
}

function p_head(){
	global $PHP_SELF;
	print <<< EOT
<html><head>
<style>
<!--
td   { text-align: center; font-size: 12px }
.menu {FILTER: glow(color=#000080, strength=1); width=100%}
.shadow1{ COLOR: #333333;  FILTER: dropshadow(color=#d5d5d5, offx=2, offy=2); FONT-SIZE: 12px;}
.title{ COLOR: #333333;  FILTER: dropshadow(color=#e0e4f0, offx=2, offy=2); font-size: 16px ; width=100%}
A.menu:visited	{COLOR:#ffffff;TEXT-DECORATION: none}
A.menu:active	{COLOR:#ffffff;TEXT-DECORATION: none}
A.menu:hover	{COLOR:#ccff33;TEXT-DECORATION: none}
A.menu:link		{COLOR:#ffffff;TEXT-decoration: none;}
A:hover, A:active{COLOR:#ff0000;TEXT-DECORATION: none}
A:link,	A:visited {COLOR:#000080;TEXT-decoration:none}
-->
</style>
</head>
<body background=images/bj.gif topmargin=0>
<center>
<table border=0 width=720>
<tr>
	<td width=20><img src=images/logo.gif></td>
	<td>
<table border=0 width=100%  background=images/bg_title.gif> 
  <tr>
    <td><div class=menu><!--<a href="$PHP_SELF?by=year">历史年度流量</a>--> <a href="$PHP_SELF"  class=menu>网站概况</a>　</div></td>
    <td><div class=menu><a href="$PHP_SELF?by=month" class=menu>本年度流量</a></div></td>
    <td><div class=menu><a href="$PHP_SELF?by=day" class=menu>本月流量</a></div></td>
    <td><div class=menu><a href="$PHP_SELF?by=week" class=menu>本周流量</a></div></td>
    <td><div class=menu><a href="$PHP_SELF?by=hour" class=menu>24小时流量</a></div></td>
    <td><div class=menu><a href="$PHP_SELF?by=visit" class=menu>访客纪录</a></div></td>
    <td><div class=menu><a href="$PHP_SELF?by=browse" class=menu>浏览器</a></div></td>
    <td><div class=menu><a href="$PHP_SELF?by=os" class=menu>操作系统</a></div></td>
  </tr>
</table>
	</td>
</tr>
</table>
	&nbsp;<br>
EOT;
}

function p_end(){
	print <<< EOT
&nbsp;<br><hr width="500" size="1">
<table border=0 width=700>
<tr>
<td align=center>
<a href=http://xpower.jilinfarm.com>蓝鸟 BlueBird<SUP>Tm</SUP></a> 网站统计 1.0 版 for Debug Verion 0101<br>
CoolBeetle Online (R) all right server. <a href=http://hotage.myrice.com>酷虫在线</a> 版权所有 </td>
</tr>
</table>
EOT;
}