<?php
	$acl=$_SESSION["acl"];
	if (strlen($acl)<1 || !strpos($acl,"<2002>")) {	//没有权限
		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>

<script language="javascript">
function printuser()
{
	myid1.background="";
	document.all.myid2.style.display="none";
	document.all.myid3.style.display="none";
	document.all.myid4.style.display="none";
	document.all.myid5.style.display="none";
	document.all.myid6.style.display="none";
	document.all.myid7.style.display="none";
	document.all.myid8.style.display="none";
	print();
}
</script>

</head>

<body id="myid1" background="bkg.bmp">

<p id="myid2" align="center"><font size="5"><b>注册用户响应</b></font></p>
<hr id="myid3" color="#999966">

<?php
	$timestr=strftime("%Y-%m-%d %H:%M:%S");
	$loginname=trim($_REQUEST["loginname"]);
	$password=trim($_REQUEST["password"]);
	$usertype=trim($_REQUEST["usertype"]);
	$buylength=trim($_REQUEST["buylength"]);
	$otherlength=trim($_REQUEST["otherlength"]);
	$money1=trim($_REQUEST["money1"]);
	$money2=trim($_REQUEST["money2"]);
	$money3=trim($_REQUEST["money3"]);
	$onlines=trim($_REQUEST["onlines"]);
	$username=trim($_REQUEST["username"]);
	$usertel=trim($_REQUEST["usertel"]);
	$comment=trim($_REQUEST["comment"]);
	$ipaddr=trim($_REQUEST["ipaddr"]);
	$mac=trim($_REQUEST["mac"]);
	$discount=trim($_REQUEST["discount"]);
	if ($buylength=="") {
		$buylength="0";
	}
	if ($otherlength=="") {
		$otherlength="0";
	}
	if ($discount=="") {
		$discount="100";
	}
	if ($onlines=="") {
		$onlines="1";
	}
	if ($loginname!="" && $usertype!="") {						//必须输入用户ID和类型
		$db=mysql_pconnect(get_cfg_var("SIPSERVER"),get_cfg_var("SIPUSER"),get_cfg_var("SIPPWD"));
		mysql_select_db(get_cfg_var("SIPDB"),$db);
		$rs = mysql_query("select loginname from users where loginname='".$loginname."' and checked='n'",$db);		//检查是否已有该用户ID
		if ( ($row = mysql_fetch_array($rs))==FALSE ) {    //没有找到，还没有开通该用户
		    mysql_free_result($rs);
		    $gpname=trim($_REQUEST["gpname"]);                       //组名
            $gpid="0";                                                 //初始化组别号
            $isnewgp="n";
            $gpinfo="";
            if ($gpname!="") {   
               $rs=mysql_query("select * from groups where gpname='".$gpname."'",$db);     //判断是否需要建立新用户组
               if ( ($row = mysql_fetch_array($rs))==FALSE ) {
                  mysql_free_result($rs);
				  $gpid=strval(time()-strtotime("2005-01-01 01:01:01"));	//生成一个唯一组ID号
                  mysql_query("insert into groups(gpid,gpname,createtime,comment) values(".$gpid.",'".$gpname."','".$timestr."','')",$db);
                  $gpinfo="所输入组别为新组别，已成功创建<br><br>";
                  $isnewgp="y";
				}
               else {
                  mysql_free_result($rs);
               }        
            }
            if ($gpname!="") {        //提取组别号
               $rs=mysql_query("select gpid from groups where gpname='".$gpname."'",$db);
			   $row = mysql_fetch_array($rs);
               $gpid=strval($row["gpid"]);
               mysql_free_result($rs);
            }
            if ($isnewgp=="y") {
               mysql_query("insert into operation values('".$_SESSION["operator"]."','".$timestr."','创建新用户组，组号：".$gpid.",组名：".$gpname."')",$db );
            }						
			$rs=mysql_query( "select price,typemode,unit from types where typeid='".$usertype."'",$db );
			$row = mysql_fetch_array($rs);
			$price=strval($row["price"]);
			$typemode=strval($row["typemode"]);
			$typeunit=strval($row["unit"]);
			mysql_free_result($rs);
			$userid=$loginname."-".strval(time()-strtotime("2005-01-01 01:01:01"));		//生成一个内部用户唯一编号
			if (strlen($userid)>20) {		//长度大于20，只使用登录名称，以便和电脑名称兼容
				$userid=$loginname;
			}
			$createtime=$timestr;
			$cookiemask="";
			for ($i=1;$i<=20;$i++) {						//生成20位随机密码字符串，密码全部是大写字符
				$cookiemask=$cookiemask.chr(rand(65,90));
			}
			mysql_query( "insert into users values('".$userid."','".$loginname."','".$password."','".$usertype."',".$onlines.",'".$createtime."','".$username."','".$usertel."','".$comment."','','n',".$gpid.",'".$cookiemask."','".$ipaddr."','','','".$mac."')",$db );
			$alllength=intval($buylength)+intval($otherlength);
			if ($typeunit==5) {			//包年
				$alllength=intval($buylength)*12;			//实际控制按照月来控制
				if ($otherlength!="") {	//有优惠
					$alllength=$alllength+intval($otherlength);		//优惠单位是月
				}
			}
			else {
				if ($typeunit==7) {			//计流量(G)
					$alllength=intval($buylength)*1024;			//实际控制按照M来控制	
					if ($otherlength!="") {	//有优惠
						$alllength=$alllength+intval($otherlength);		//优惠单位是M
					}
				}
			}
			if ($typemode=="0") {			//免费
				$alllength=0;
				mysql_query( "insert into addtimelist values('".$userid."','".$createtime."','".$usertype."',0,0,0,0,0,0,0,'1')",$db );
			}
			elseif ($typemode=="2") {		//先使用后结算
				mysql_query( "insert into usertime values('".$userid."','".$createtime."',0,0,'n')",$db );	//只有时间有效
				mysql_query( "insert into addtimelist values('".$userid."','".$createtime."','".$usertype."',0,0,0,0,0,0,0,'1')",$db );
				if ($money3!="0" && $money3!="") {	//有押金资金流
					mysql_query( "insert into cash values('".$userid."','".$createtime."',".$money3.",".$money3.",'2','".$_SESSION["operator"]."')" ,$db);
				}
			}
			else {
				if ($typeunit==6 || $typeunit==7) {		//控制流量
					mysql_query( "insert into usertime values('".$userid."','".$createtime."',0,".strval($alllength).",'n')",$db );
					mysql_query( "insert into addtimelist values('".$userid."','".$createtime."','".$usertype."',0,".$buylength.",".$otherlength.",".$price.",".strval($money1).",".$discount.",".strval($money2).",'1')",$db );
				}
				else {											//控制时间
					mysql_query( "insert into usertime values('".$userid."','".$createtime."',".strval($alllength).",0,'n')",$db );
					mysql_query( "insert into addtimelist values('".$userid."','".$createtime."','".$usertype."',".$buylength.",0,".$otherlength.",".$price.",".strval($money1).",".$discount.",".strval($money2).",'1')",$db );
				}
				if ($money2!="0" && $money2!="") {	//有购买款资金流
					mysql_query( "insert into cash values('".$userid."','".$createtime."',".$money2.",0,'1','".$_SESSION["operator"]."')" ,$db);
				}
			}
			mysql_query( "insert into operation values('".$_SESSION["operator"]."','".$timestr."','开通上网用户，用户ID：".$userid."')",$db );
			echo("<p id=myid4 align=center>".$gpinfo."用户开通成功!<br><br>以下内容请用户记住！<br></p>");
			
			echo("<div align='center'>");
			echo("  <center>");
			echo("  <table border='1' width='358' bordercolor='#008080' cellspacing='0' cellpadding='0' height='67'>");
			echo("    <tr>");
			echo("      <td width='151' height='33'>上网用户ID</td>");
			echo("      <td width='191' height='33' align='center'><font size='5'>".$loginname."</font></td>");
			echo("    </tr>");
			echo("    <tr>");
			echo("      <td width='151' height='34'>上网用户密码</td>");
			echo("      <td width='191' height='34' align='center'><font size='5'>".$password."</font></td>");
			echo("    </tr>");
			echo("  </table>");
			echo("  </center>");
			echo("</div>");
			echo("<p></p>");
			echo("<form id=myid5>");
			echo("  <p align=center><input type=button value=打印 onclick='javascript:printuser();'></p>");
			echo("</form>");
		}
		else {
			mysql_free_result($rs);
			echo("<p align=center>错误，该用户ID已经存在！</p>");
		}
		mysql_close($db);
	}
	else {
		echo("<p align=center>必须输入用户ID和类型</p>");
	}
?>

<hr id="myid6">
<p id="myid7" align="center">SIP宽带上网计费管理系统</p>
<p id="myid8" align="center">作者：<b>深圳市飞沃软件有限公司</b></p>

</body>

</html>
