<?php
	$acl=$_SESSION["acl"];
	if (strlen($acl)<1 || !strpos($acl,"<2028>")) {	//没有权限
		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 6.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">
<center>
<?php		
  $userid=trim($_REQUEST["userid"]);
  if ($userid!="") {
     $db=mysql_pconnect(get_cfg_var("SIPSERVER"),get_cfg_var("SIPUSER"),get_cfg_var("SIPPWD"));
     mysql_select_db(get_cfg_var("SIPDB"),$db);
     $sqlstr="select b.loginname,b.length,b.otherlength,b.money1,b.type,b.onlines,b.password,b.gpid,t.price,t.typemode,t.unit from batusers b,types t where t.typeid=b.type and b.userid='".$userid."' and b.active='n'";
     $rs=mysql_query($sqlstr,$db);                    //提取用户信息
     if ($row = mysql_fetch_array($rs)) {
          $loginname=$row[0];
          $buylength=$row[1];
          $money1=$row[3];
          $otherlength=$row[2];
          $typeid=$row[4];
          $onlines=strval($row[5]);
          $password=$row[6];
          $price=strval($row[8]);     
          $typemode=$row[9];     
          $typeunit=$row[10];     
          $discount=trim($_REQUEST["discount"]); 
          $money2=$_REQUEST["money2"];   //用户的实收金额
          if ($discount=="") {
             $discount="100";
          }
          $gp_id=strval($row[7]);
          if ($gp_id==0) {
             $gp_id="";
          }
          $gpname=trim($_REQUEST["gpname"]);  
          mysql_free_result($rs); 
		  $timestr=strftime("%Y-%m-%d %H:%M:%S");
          $gpid="0";                                              //初始化组别号
          $isnewgp="n";                                           //是否为新组
            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 );
               echo("所输入组别为新组别，已成功创建<br>");
            }						
          $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
			    }
		     }
	      }
			$cookiemask="";
			for ($i=1;$i<=20;$i++) {						//生成20位随机密码字符串，密码全部是大写字符
				$cookiemask=$cookiemask.chr(rand(65,90));
			}
          $sqlstr="insert into users values('".$userid."','".$loginname."','".$password."','".$typeid."',".$onlines.",'".$timestr."','','','','','n',".$gpid.",'".$cookiemask."','','','','')";
          mysql_query($sqlstr,$db);
	      if ($typemode=="0") {			//免费
		     $alllength=0;
		     mysql_query( "insert into addtimelist values('".$userid."','".$timestr."','".$typeid."',0,0,0,0,".strval($money1).",".$discount.",".strval($money2).",'1')" ,$db);
		  } elseif ($typemode=="2") {		//先使用后结算
		     mysql_query( "insert into usertime values('".$userid."','".$timestr."',0,0,'n')" ,$db);	//只有时间有效
		     mysql_query( "insert into addtimelist values('".$userid."','".$timestr."','".$typeid."',0,0,0,0,".strval($money1).",".$discount.",".strval($money2).",'1')" ,$db);
		  } else {
			 if ($typeunit==6 || $typeunit==7) {		//控制流量
		        mysql_query( "insert into usertime values('".$userid."','".$timestr."',0,".strval($alllength).",'n')" ,$db);
		        mysql_query( "insert into addtimelist values('".$userid."','".$timestr."','".$typeid."',0,".strval($buylength).",".strval($otherlength).",".strval($price).",".strval($money1).",".$discount.",".strval($money2).",'1')" ,$db);
			 } else {											//控制时间
			    mysql_query( "insert into usertime values('".$userid."','".$timestr."',".strval($alllength).",0,'n')" ,$db);
			    mysql_query( "insert into addtimelist values('".$userid."','".$timestr."','".$typeid."',".strval($buylength).",0,".strval($otherlength).",".strval($price).",".strval($money1).",".$discount.",".strval($money2).",'1')" ,$db);
			 }
			if ($money2!="0" && $money2!="") {	//有购买款资金流
				mysql_query( "insert into cash values('".$userid."','".$timestr."',".$money2.",0,'1','".$_SESSION["operator"]."')" ,$db);
			}
		  }
		  mysql_query("update batusers set active='y' where userid='".$userid."' and active='n'",$db);
          mysql_query( "insert into operation values('".$_SESSION["operator"]."','".$timestr."','激活用户，用户id：".$userid."')" ,$db);
          if ($gp_id!=$gpid) {        //如果在激活时改变了用户的组则插入日志记录
             if ($gpid!="") {
                mysql_query( "insert into operation values('".$_SESSION["operator"]."','".$timestr."','添加用组别成员,组号：".$gpid.",用户id：".$userid."')" ,$db);
             } else {
                mysql_query( "insert into operation values('".$_SESSION["operator"]."','".$timestr."','删除用组别成员,组号：".$gp_id.",用户id：".$userid."')" ,$db);
             }
          }
          echo("激活成功");
     } else {
          echo("该用户不存在或已经被激活");
     }
     mysql_close($db); 
  }
  else {
  	 echo("用户ID不能为空");
  }
?>
<table border="1"  bordercolor="#008080" cellspacing="0" cellpadding="0">
   <tr>
      <td align=center width=100>用户ID</td>
      <td align=center width=200>&nbsp;&nbsp;<?php echo $loginname; ?></td>
   </tr>
   <tr>
      <td align=center width=100>用户密码</td>
      <td align=center width=200>&nbsp;&nbsp;<?php echo $password; ?></td>
   </tr>
</table>

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

</body>

</html>
