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

<?php
	$time1=$_REQUEST["time1"];
	$time2=$_REQUEST["time2"];
	$listtype=$_REQUEST["listtype"];
	$time1=str_replace("A"," ",$time1);
	$time2=str_replace("A"," ",$time2);
	$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;
	$sql="select userid,sum(usetime) as alltime from history where starttime>='".$time1."' and starttime<'".$time2."' group by userid order by alltime desc";
	if ($listtype=="2") {
		$sql="select userid,sum(datalength) as alldata from history where starttime>='".$time1."' and starttime<'".$time2."' group by userid order by alldata desc";
	}
	$rs = mysql_query( $sql,$db );
	while ($row = mysql_fetch_array($rs)) {
		$count=$count+1;
		$userid[$count]=$row[0];
		$usage[$count]=$row[1];
	}
	mysql_free_result($rs);
	if ($count>0) {
		$file=fopen("usage.xls","wb");
		if ($file) {
			if ($listtype=="2") {
				fwrite($file,"上网流量排序表\r\n");
				fwrite($file,"序号\t用户ID\t用户名称\t用户部门\t联系电话\t总上网流量(M)\r\n");
			}
			else {
				fwrite($file,"上网时间排序表\r\n");
				fwrite($file,"序号\t用户ID\t用户名称\t用户部门\t联系电话\t总上网时间(小时)\r\n");
			}
			for ($i=1;$i<=$count;$i++) {
				$rs = mysql_query( "select loginname,username,userdept,usertel from users where userid='".$userid[$i]."'",$db );
				if ($row = mysql_fetch_array($rs)) {
					if ($listtype=="2") {
						fwrite($file,strval($i)."\t".$row[0]."\t".$row[1]."\t".$row[2]."\t".$row[3]."\t".strval(round($usage[$i]/1024,2))."\r\n");
					}
					else {
						fwrite($file,strval($i)."\t".$row[0]."\t".$row[1]."\t".$row[2]."\t".$row[3]."\t".strval(round($usage[$i]/3600,2))."\r\n");
					}
				}
				mysql_free_result($rs);
			}
			fwrite($file,"统计时间范围：从 ".$time1." 起，到 ".$time2." 止。\r\n");
			fclose($file);
		}
	}
	mysql_close($db);	
?>


<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">
</head>

<body background="bkg.bmp">

<p align="center">
<?php
	if ($count>0) {
		echo("<a href=usage.xls>下载Excel数据文件</a>");
	}
	else {
		echo("没有数据！");
	}
?>
</p>

<p align="center">SIP企业上网管理系统</p>
<p align="center">作者：<b>深圳市飞沃软件有限公司</b></p>

</body>

</html>
