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

<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">
<p align="center">
<?php
	$userid = $_REQUEST["userid"];
	$year1=$_REQUEST["year1"];
	$month1=$_REQUEST["month1"];
	$month2=$_REQUEST["month2"];
	$time1="2004-01-01 00:00:01";		//统计开始时间
	$time2=strftime("%Y-%m-%d 23:59:59");		//统计结束时间
	if ($year1!="" && $month1!="" && $month2!="") {
	    $time1=$year1;
	    if (intval($month1)<10) {
	    	$time1=$time1."-0".$month1."-01 00:00:01";		//统计开始时间
		}
	    else {
	    	$time1=$time1."-".$month1."-01 00:00:01";		//统计开始时间
	    }
	    $time2=$year1;
	    if (intval($month2)<10) {
	    	$time2=$time2."-0".$month2."-01 00:00:01";
		}
	    else {
	    	$time2=$time2."-".$month2."-01 00:00:01";
	    }
		$time2=strftime("%Y-%m-01 00:00:01",strtotime("+1 month",strtotime($time2)));		//统计结束时间，下一个月开始
	}
	$str=$_REQUEST["pageno"];
	if ($str=="") {
		$str="1";
	}
	$page_no=intval($str);			//当前显示页号，因为可能需要多页显示
	$pagecount=20;		//每页显示多少条记录
	$count=0;
	$allpage=0;
	$alltime=0;
	$alllength=0;
	$db=mysql_pconnect(get_cfg_var("SIPSERVER"),get_cfg_var("SIPUSER"),get_cfg_var("SIPPWD"));
	mysql_select_db(get_cfg_var("SIPDB"),$db);
	if ($userid=="") {
		$loginname = $_REQUEST["loginname"];
		$rs = mysql_query( "select userid from users where loginname='".$loginname."' and checked='n'",$db );
		if ($row = mysql_fetch_array($rs)) {
			$userid=$row["userid"];
		}
		mysql_free_result($rs);
	}
	if ($userid=="") {
		echo("该用户ID无效!");
	}
	else {
		$sqlstr="select count(starttime),sum(usetime),sum(datalength) from history where userid='".$userid."'";
		if ($year1!="" && $month1!="" && $month2!="") {
			$sqlstr=$sqlstr." and starttime>='".$time1."' and starttime<='".$time2."'";
		}
		$rs = mysql_query( $sqlstr,$db );
		//计算总记录数，计算共有多少页
		if ($row = mysql_fetch_array($rs)) {			//有记录
			$count=$row[0];
			$alltime=$row[1];
			$alllength=round($row[2]/1024,2);
		}
		mysql_free_result($rs);
		if ($count<1) {
			echo("查询范围内没有上网记录!");
		}
		else {
			$allpage=(int)($count/$pagecount);
			if ($allpage*$pagecount<$count) {		//不是整除
				$allpage=$allpage+1;
			}
			if ($page_no>$allpage) {
				echo("页号超出记录范围!");
			}
			else {
				$sqlstr="select ipaddr,starttime,endtime,usetime,datalength from history where userid='".$userid."'";
				if ($year1!="" && $month1!="" && $month2!="") {
					$sqlstr=$sqlstr." and starttime>='".$time1."' and starttime<='".$time2."'";
				}
				$sqlstr=$sqlstr." order by starttime";
				$rs = mysql_query( $sqlstr,$db );
				if ($row = mysql_fetch_array($rs)) {			//有记录
					$n=1;
					while ($row && $n<=($page_no-1)*$pagecount) {		//移动行号直到当前页对应起始行号位置
						$row = mysql_fetch_array($rs);
						$n=$n+1;
					}
					if ($row) {
						echo("<table border=1 cellpadding=0 cellspacing=0 bordercolor=#008080>");
						echo("<tr><td>序号</td><td>IP地址</td><td>开始时间</td><td>结束时间</td><td>使用时间(秒)</td><td>流量(K)</td></tr>");
						while ($row && $n<=$page_no*$pagecount) {	//显示当前页记录
							echo("<tr>");
							echo("<td>".strval($n)."</td>");
							echo("<td>".$row["ipaddr"]."</td>");
							echo("<td>".$row["starttime"]."</td>");
							echo("<td>".$row["endtime"]."</td>");
							echo("<td>".strval($row["usetime"])."</td>");
							echo("<td>".strval($row["datalength"])."</td>");
							echo("</tr>");
							$row = mysql_fetch_array($rs);
							$n=$n+1;
						}
						echo("</table><br>");
						echo("共 ".strval($count)." 次，时间共 ".strval($alltime)." 秒，流量共 ".strval($alllength)." M。<br>");
						echo("<form method=GET action=queryhistory_result.php><input type=hidden name=userid value=".$userid."><input type=hidden name=year1 value=".$year1."><input type=hidden name=month1 value=".$month1."><input type=hidden name=month2 value=".$month2.">");
						echo("第 ".strval($page_no)." 页，共 ".strval($allpage)." 页。");
						if ($page_no>1) {			//有前一页
							echo("<a href=queryhistory_result.php?userid=".$userid."&year1=".$year1."&month1=".$month1."&month2=".$month2."&pageno=".strval($page_no-1).">前一页</a>");
						}
						if ($page_no<$allpage) {	//有后一页
							echo(" <a href=queryhistory_result.php?userid=".$userid."&year1=".$year1."&month1=".$month1."&month2=".$month2."&pageno=".strval($page_no+1).">后一页</a>");
						}
						if ($allpage>2) {	//超过2页
							echo(" 到<input type=text size=3 name=pageno maxlength=3>页");
						}
						echo("</form>");
					}
					else {
						echo("页号已经超出记录范围!");
					}
				}
				else {
					echo("查询范围内没有上网记录!");
				}
				mysql_free_result($rs);
			}
		}
	}
	mysql_close($db);
?>
</p>
<?php
if ($year1!="" && $month1!="" && $month2!="") {
?>
<p align="center">统计时间范围：从 <?php echo $time1; ?> 起，到 <?php echo $time2; ?> 止。</p>
<?php
}
?>
<hr>
<p align="center">SIP企业上网管理系统</p>
<p align="center">作者：<b>深圳市飞沃软件有限公司</b></p>


</body>

</html>
