<?php
	$acl=$_SESSION["acl"];
	if (strlen($acl)<1 || !strpos($acl,"<2036>")) {	//没有权限
		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 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"]);
	$ipaddr=trim($_REQUEST["ipaddr"]);
	$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 userid,type,cookiemask from tmpusers where loginname='".$loginname."'",$db);		//检查是否已有该用户ID
	if ($row = mysql_fetch_array($rs)) {
		$userid=$row["userid"];
		$usertype=$row["type"];
		$cookiemask=$row["cookiemask"];
		mysql_free_result($rs);
		$otherlength="0";
		$discount="100";
		$createtime=$timestr;
		$password=trim($_REQUEST["password"]);
		$buylength=trim($_REQUEST["buylength"]);
		$price=trim($_REQUEST["price"]);
		$money1=trim($_REQUEST["money"]);
		$money2=trim($_REQUEST["money"]);
		$onlines=trim($_REQUEST["onlines"]);
		$username=trim($_REQUEST["username"]);
		$usertel=trim($_REQUEST["usertel"]);
		$comment=trim($_REQUEST["comment"]);
		$ipaddr=trim($_REQUEST["ipaddr"]);
		$mac=trim($_REQUEST["mac"]);
		if ($buylength=="") {
			$buylength="0";
		}
		if ($onlines=="") {
			$onlines="1";
		}
		$typemode="";
		$typeunit=0;
		$rs=mysql_query("select typemode,unit from types where typeid='".$usertype."'",$db);		//检查是否已有该用户id
		if ($row = mysql_fetch_array($rs)) {
			$typemode=$row["typemode"];
			$typeunit=$row["unit"];
		}
		mysql_free_result($rs);
		$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 );
            }						
			mysql_query( "insert into users values('".$userid."','".$loginname."','".$password."','".$usertype."',".$onlines.",'".$createtime."','".$username."','".$usertel."','".$comment."','','n',".$gpid.",'".$cookiemask."','".$ipaddr."','','','".$mac."')",$db );
			$alllength=intval($buylength);
			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 );
			}
			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( "delete from tmpusers where loginname='".$loginname."'",$db);
			mysql_query( "insert into operation values('".$_SESSION["operator"]."','".$timestr."','开通上网用户，用户ID：".$userid."')",$db );
			echo("<p id=myid4 align=center>确认成功!<br></p>");
		}
		else {
			mysql_free_result($rs);
			echo("<p align=center>错误，该用户ID已经存在！</p>");
		}
	}
	else {
		mysql_free_result($rs);
		echo("<p align=center>必须输入用户ID</p>");
	}
	mysql_close($db);
?>

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

</body>

</html>
