<?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		
	$startname=trim($_REQUEST["startname"]);    //起始ID
    $typeid=$_REQUEST["usertype"];              //用户类型号
	$passtype=$_REQUEST["passtype"];	//生成密码方式
	$setword=trim($_REQUEST["setword"]);    //指定的密码
	$iserr="n";     //是否有错误发生,n为否,y为是
	//批量生成用户ID
	if ($startname=="") { 
	   echo("起始ID不能为空");
	   $iserr="y";
	}
	else {
	   if ($typeid=="") {
	      echo("用户类型不能为空");
	      $iserr="y";
	   }
       else {
	      $count=trim($_REQUEST["count"]);
	      if ($count=="") {
	         echo("批次数量不能为空");
	         $iserr="y";
		  }
	      else {
	         $count=intval($count)-1;   //该批次用户数量
	         if ($count<0) {
	            echo("批次数量必须大于等于1");
	            $iserr="y";
			 }
	         else {
	            $str="";
	            $numb="";
	            $loginname[0]=$startname;
                for($i=0;$i<strlen($startname);$i++) {         //提取起始ID的文字部分和数字部分
                    if (ord(substr($startname,$i))>47&&ord(substr($startname,$i))<58) {	//数字字符
                       $str=substr($startname,0,$i);
                       $numb=substr($startname,$i);
                       break;
                    }
                }
                if ($numb=="") {                   //生成批次用户ID,下一个ID为上一个ID的数字部分加1
                   echo("起始ID必须以数字结尾");
                   $iserr="y";
				}
                else {
                   if (strlen($numb)<strlen(strval(intval($numb)+$count))) {
                      echo("数字位数过少");
                      $iserr="y";
				   }
                   else {
                      for($n=1;$n<=$count;$n++) {
                          $numb=strval(intval($numb)+1);
                          $loginname[$n]=$str.$numb;
                      }
                   }
                }
             }
          }
       }
    }
    $db=mysql_pconnect(get_cfg_var("SIPSERVER"),get_cfg_var("SIPUSER"),get_cfg_var("SIPPWD"));
  	mysql_select_db(get_cfg_var("SIPDB"),$db);
    if ($iserr=="n") {           //如果以上操作未发生错误则验证批次ID中是否有已经存在的,如果有已经存在的ID则出错并提示
       $errinfo="";
       for ($n=0;$n<=$count;$n++) {
           $rs1=mysql_query("select loginname from users where loginname='".$loginname[$n]."'",$db);
	       $rs2=mysql_query("select loginname from batusers where loginname='".$loginname[$n]."'",$db);
           if ( ($row1 = mysql_fetch_array($rs1)) || ($row2 = mysql_fetch_array($rs2)) ) {
		       mysql_free_result($rs1);
		       mysql_free_result($rs2);
               $iserr="y";
               $errinfo="有重复的用户ID";  
               break;
           }
	       mysql_free_result($rs1);
		   mysql_free_result($rs2);
       }
    }
    //如果没有错误则将数据添加到数据库中
    if ($iserr=="n") {
		$timestr=strftime("%Y-%m-%d %H:%M:%S");
       $buylength=trim($_REQUEST["buylength"]);               //用户购买数量
       $otherlength=trim($_REQUEST["otherlength"]);           //赠送数量
       $discount=trim($_REQUEST["discount"]);                 //折扣
       $onlines=trim($_REQUEST["onlines"]);                   //允许同时上网人数
		if ($buylength=="") {
			$buylength="0";
		}
		if ($otherlength=="") {
			$otherlength="0";
		}
		if ($discount=="") {
			$discount="100";
		}
		if ($onlines=="") {
			$onlines="1";
		}
       $comment=trim($_REQUEST["comment"]);                   //备注信息
       $money1=trim($_REQUEST["money1"]);                     //每用户应收金额
       $money2=trim($_REQUEST["money2"]);                     //每用户实收金额
       $totalmoney1=trim($_REQUEST["totalmoney1"]);           //批次应收总金额
       $totalmoney2=trim($_REQUEST["totalmoney2"]);           //批次实收总金额
       $gpname=$_REQUEST["gpname"];                           //组名
       $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 );
        }						
       $active=$_REQUEST["active"];     //是否激活n为不激活,y为激活
       $rs=mysql_query("select name,price,typemode,unit from types where typeid='".$typeid."'",$db);        //提取用户类型信息
	   if ($row = mysql_fetch_array($rs)) {
          $price=$row["price"];
          $type_name=$row["name"];
          $typemode=$row["typemode"];
          $typeunit=$row["unit"];
       }
       mysql_free_result($rs);
	   $batchid=0;
	   $rs=mysql_query("select max(batchid) from batchs",$db);	//查询最大的批次号
	   if ($row = mysql_fetch_array($rs)) {
			$batchid=$row[0];
			if (is_null($batchid)) {
				$batchid=0;
			}
	   }
	   mysql_free_result($rs);
	   $batchid=$batchid+1;
       $sqlstr="insert into batchs(batchid,startuserid,createtime,type,onlines,length,count,money1,otherlength,discount,money2,active,gpid,comment)";
       $sqlstr=$sqlstr." values(".strval($batchid).",'".$startname."','".$timestr."','".$typeid."','".$onlines."','".$buylength."','".strval($count+1)."','".strval($totalmoney1)."','".$otherlength."','".$discount."','".strval($totalmoney2)."','".$active."',".$gpid.",'".$comment."')";
       mysql_query($sqlstr,$db);
       mysql_query( "insert into operation values('".$_SESSION["operator"]."','".$timestr."','批量注册用户，批次号：".strval($batchid)."')",$db );
       //生成数据文件
	   $file=fopen("tmp.dat","wb");
	   if ($file) fwrite($file,"用户ID".chr(09)."用户密码"."\r\n");
       $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
			 }
		  }
	   }
	   $n=0;
       for ($n=0;$n<=$count;$n++) {   //为该批次的每个用户插入数据
			$password="";
			if ($passtype=="1") {		//自动随机密码生成
				$password="";
				for ($i=1;$i<=10;$i++) {				//生成10位随机密码字符串，密码全部是数字
					$password=$password.chr(rand(48,57));
				}
			}
			else {				//指定密码
			   $password=$setword;
			}
		   $userid=$loginname[$n]."-".strval($n+1)."-".strval($batchid);		//生成一个内部用户唯一编号
		   $sqlstr="insert into batusers(userid,loginname,password,type,onlines,length,createtime,money1,otherlength,discount,money2,batchid,active,gpid)";
		   $sqlstr=$sqlstr." values('".$userid."','".$loginname[$n]."','".$password."','".$typeid."','".$onlines."','".$buylength."','".$timestr."','".strval($money1)."','".$otherlength."','".$discount."','".strval($money2)."','".strval($batchid)."','".$active."',".$gpid.")";       
           mysql_query($sqlstr,$db);
           if ($active=="y") {
				$cookiemask="";
				for ($i=1;$i<=20;$i++) {						//生成20位随机密码字符串，密码全部是大写字符
					$cookiemask=$cookiemask.chr(rand(65,90));
				}
				  $sqlstr="insert into users values('".$userid."','".$loginname[$n]."','".$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,".$buylength.",".$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."',".$buylength.",0,".$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);
					 }
				  }
           }
           if ($file) fwrite($file,$loginname[$n].chr(09).$password."\r\n");
       }
       if ($file) fclose($file);
       if ($active=="y") {
          mysql_query( "insert into operation values('".$_SESSION["operator"]."','".$timestr."','批量激活用户，批次号：".strval($batchid)."')",$db );
       }
       //如果有错误发生则回滚事务并输出错误提示,没有则提交
      echo("操作成功<p><A href=tmp.dat target=_blank>下载数据文件(记事本可打开)</A></p>");
      echo("<table  border='1' bordercolor='#008080' cellspacing='0' cellpadding='0'>");
      echo("<tr><td align=center width=100>批次号</td><td align=center width=250>ID</td><td align=center width=100>用户数量</td></tr>");
      echo("<tr><td align=center width=100>".strval($batchid)."</td><td align=center width=250>".$startname."～".$loginname[$count]."</td><td align=center width=100>".strval($count+1)."</td></tr>");    
      echo("</table>");
	}
    else {
       if ($errinfo!="") {
          echo($errinfo."<br>请重新选择注册的用户ID范围");
       }
    }
    mysql_close($db); 
?>

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

</body>

</html>
