<?php
	$acl=$_SESSION["acl"];
	if (strlen($acl)<1 || !strpos($acl,"<28>")) {	//没有权限
		ob_clean();
		header("Location: accesserror.html");
		exit;
	}
?>

<?php
	//统计时间段是统计日期0点至统计日期24点
	$strYear=$_REQUEST["year"];
	$strMonth=$_REQUEST["month"];
	$strDay=$_REQUEST["day"];
	$time1=strftime("%Y-%m-%d 00:00:01");		//统计开始时间
	$time2=strftime("%Y-%m-%d 23:59:59");		//统计结束时间
	if ($strYear!="" && $strMonth!="" && $strDay!="") {
	    $time1=$strYear;
	    if (intval($strMonth)<10) {
	    	$time1=$time1."-0".$strMonth;
		}
	    else {
	    	$time1=$time1."-".$strMonth;
	    }
	    if (intval($strDay)<10) {
	    	$time1=$time1."-0".$strDay." 00:00:01";		//统计开始时间
		}
	    else {
	    	$time1=$time1."-".$strDay." 00:00:01";		//统计开始时间
	    }
	    $time2=$strYear;
	    if (intval($strMonth)<10) {
	    	$time2=$time2."-0".$strMonth;
		}
	    else {
	    	$time2=$time2."-".$strMonth;
	    }
	    if (intval($strDay)<10) {
	    	$time2=$time2."-0".$strDay." 23:59:59";		//统计结束时间
		}
	    else {
	    	$time2=$time2."-".$strDay." 23:59:59";		//统计结束时间
	    }
	}
	$pagecount=20;		//每页显示多少条记录
	$str=$_REQUEST["pageno"];
	if ($str=="") {
		$str="1";
	}
	$page_no=intval($str);			//当前显示页号，因为可能需要多页显示
	$db=mysql_pconnect(get_cfg_var("SIPSERVER"),get_cfg_var("SIPUSER"),get_cfg_var("SIPPWD"));
	mysql_select_db(get_cfg_var("SIPDB"),$db);
	$count=0;
	$allpage=0;
	$rs = mysql_query( "select userid,sum(datalength) as alldata from history where starttime>='".$time1."' and starttime<'".$time2."' group by userid order by alldata desc",$db );
	while ($row = mysql_fetch_array($rs)) {
		$count=$count+1;
		$userid[$count]=$row[0];
		$alldata[$count]=$row[1];
	}
	mysql_free_result($rs);
?>


<html>

<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>上网流量日排序表</title>
</head>

<body background="bkg.bmp">

<p align="center"><font size="5"><b>上网流量日排序表</b></font></p>
<hr color="#999966">

<div align="center">
<center>
<table border="1" cellpadding="0" cellspacing="0" bordercolor="#008080">
  <tr>
    <td align="center">序号</td>
    <td align="center">用户ID</td>
    <td align="center">用户名称</td>
    <td align="center">用户部门</td>
    <td align="center">联系电话</td>
    <td align="center">总上网流量(M)</td>
  </tr>
<?php
	if ($count>0) {
		$allpage=(int)($count/$pagecount);
		if ($allpage*$pagecount<$count) {		//不是整除
			$allpage=$allpage+1;
		}
		if ($page_no<=$allpage) {		//页号没有超出记录范围
			$n=1;
			while ($n<=$count && $n<=($page_no-1)*$pagecount) {		//移动行号直到当前页对应起始行号位置
				$n=$n+1;
			}
			while ($n<=$count && $n<=$page_no*$pagecount) {		//显示当前页记录
				$rs = mysql_query( "select loginname,username,userdept,usertel from users where userid='".$userid[$n]."'",$db );
				if ($row = mysql_fetch_array($rs)) {
					echo("<tr>");
					echo("<td>".strval($n)."</td>");
					echo("<td><a href=queryuser_content.php?userid=".$userid[$n]." target=_blank>".$row[0]."</a></td>");
					echo("<td>".$row[1]."&nbsp;</td>");
					echo("<td>".$row[2]."&nbsp;</td>");
					echo("<td>".$row[3]."&nbsp;</td>");
					echo("<td align=center>");
					echo( strval(round($alldata[$n]/1024,2)) );
					echo("</td>");
					echo("</tr>");
				}
				mysql_free_result($rs);
				$n=$n+1;
			}
		}
	}
	mysql_close($db);	
?>
</table>

<?php
	if ($count>0) {
		echo("<form method=GET action=flowreport4.php><input type=hidden name=year value=".$strYear."><input type=hidden name=month value=".$strMonth."><input type=hidden name=day value=".$strDay.">");
		echo("共".strval($count)."人。第 ".strval($page_no)." 页，共 ".strval($allpage)." 页。");
		if ($page_no>1) {			//有前一页
			echo("<a href=flowreport4.php?year=".$strYear."&month=".$strMonth."&day=".$strDay."&pageno=".strval($page_no-1).">前一页</a>");
		}
		if ($page_no<$allpage) {	//有后一页
			echo(" <a href=flowreport4.php?year=".$strYear."&month=".$strMonth."&day=".$strDay."&pageno=".strval($page_no+1).">后一页</a>");
		}
		if ($allpage>2) {	//超过2页
			echo(" 到<input type=text size=3 name=pageno maxlength=3>页");
		}
		echo("</form><br><br>");
		$timestr1=str_replace(" ","A",$time1);
		$timestr2=str_replace(" ","A",$time2);
		echo("<p align=center><a href=output.php?type=2&time1=".$timestr1."&time2=".$timestr2.">生成Excel文件</a></p>");
	}
?>

  </center>
</div>

<p align="center">　</p>

<p align="center">统计时间范围：从 <?php echo $time1; ?> 起，到 <?php echo $time2; ?> 止。</p> 
<hr>
<p align="center">SIP企业上网管理系统</p>
<p align="center">作者：<b>深圳市飞沃软件有限公司</b></p>

</body>

</html>
