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

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script language="jscript">
//如果添加了新组则刷新组列表
function setresult()
{
    if (document.form1.isadd.value=="y")
        parent.gp.location.href="querygp.php";
}
//检查数据有效性
function checkdata()
{
    if (document.form1.gpname.value=="")
        alert("组别名不能为空");
    else
        document.form1.submit();
}
</script>
<title>添加用户组</title>
<base target="_self">

<body background="bkg.bmp" style="background-attachment: fixed" onload="setresult();">
<p align="center"><font size="3"><b>添加用户组</b></font></p>
<center>
<?php
$gpname=trim($_REQUEST["gpname"]);
$comment=trim($_REQUEST["comment"]);
$ispostback=$_REQUEST["ispostback"];             //是否为回送
$isadd="n";                                         //反映是否添加了新组
if ($ispostback=="y") {
   if ($gpname!="") {
 	  $timestr=strftime("%Y-%m-%d %H:%M:%S");
      $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 * 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."','".$comment."')",$db);
         mysql_query( "insert into operation values('".$_SESSION["operator"]."','".$timestr."','创建新用户组，组号：".$gpid.",组名：".$gpname."')",$db );
         echo(gpname."添加成功");
         $isadd="y";
	  }
      else {
         mysql_free_result($rs);
         echo("已经存在该组,不用再次添加");
      }
      mysql_close($db);
   }
   else {
      echo("组名不能为空");
   }
}
?>
<form name="form1" method="POST" action="addgroup.php" target="_self">
<table border="1" bordercolor="#008080" cellspacing="0" cellpadding="0" width="224">
   <tr>
      <td align="center" width="49">
      组名
      </td>
      <td align="left" width="169">&nbsp;
      <input type="text" name="gpname" size="10" maxlength="50">
      </td>
   </tr>
   <tr>
      <td align="center" width="49">
      备注
      </td>
      <td align="left" width="169">&nbsp;
      <input type="text" name="comment" size="20">
      </td>
   </tr>
   <tr>
      <td align="center" width="220" colspan="2">
      <input type="hidden" name="ispostback" value="y">
      <input type="hidden" name="isadd" value="<?php echo $isadd; ?>">
      <input type="button" name="b1" value="确认" onclick="checkdata();">
      </td>
   </tr>
</table>
</form>
</center>
</body>

</html>