
<?php
if(!defined('IN_GATEWEI')) {
	exit('access denied');
}

	$groupinfo=array();
	$grouptype=array('自定义','内置');
	$checkname=array('不验证','Email验证','人工验证');

	if($ipost=='save'){
		if(empty($groupname)){
			gadminmessage('用户组名不能为空');
		}
	$isregister=intval($isregister);
	$checkregister=$isregister?intval($checkregister):0;
	$db->query("INSERT INTO g_groups (gid , groupname , type, isregister,checkregister) VALUES (NULL , '$groupname', 0,$isregister,$checkregister )");
	$getgid=$db->insert_id();
	$db->query("INSERT INTO g_permission (perid ,gid ,pervalue)VALUES (NULL , $getgid , 80)");
	$q=$db->query("SELECT * FROM g_permission");
	while($groups=$db->fetch_array($q)){
	$groupinfo[]=$groups['gid'].','.$groups['pervalue'];
	}
	$newsettings['userpermission']='{{'.implode($groupinfo,';').'}}';
	$groupinfo=array();
	$q=$db->query("SELECT * FROM g_groups");
	while($groups=$db->fetch_array($q)){
	$groupinfo[]=$groups['gid'].','.$groups['groupname'].','.$groups['isregister'].','.$groups['checkregister'];
	}
	$newsettings['groupnames']='{{'.implode($groupinfo,';').'}}';
	foreach ($newsettings as $key => $val){
		$db->query("REPLACE INTO g_settings (var, val) VALUES ('$key', '$val')");
	}
	w2setting();
	writelog($userid."成功添加了一个名为".$groupname."的新用户组");
	gadminmessage('添加成功',"siteadmin.php?iframe=setgroup",1);
	}

	if($ipost=='delete'){
		$i=0;
		if(isset($groupcheck)){
		foreach($groupcheck as $value){
			if($db->result($db->query("SELECT count(*) FROM g_members WHERE gid=$value"),0)){
				gadminmessage("该组中存在用户，请将用户删除或合并到其他用户组中再进行删除");
			}
			$db->query("DELETE FROM g_groups WHERE gid = $value LIMIT 1");
			$db->query("DELETE FROM g_permission  WHERE  gid = $value LIMIT 1");
			$i++;
			}
		}else{
			gadminmessage('没有用户组被删除，请勾选被删除的用户');
		}
		$groupinfo=array();
		$q=$db->query("SELECT * FROM g_permission");
		while($groups=$db->fetch_array($q)){
			$groupinfo[]=$groups['gid'].','.$groups['pervalue'];
	 }
		$newsettings['userpermission']='{{'.implode($groupinfo,';').'}}';
		$groupinfo=array();
		$q=$db->query("SELECT * FROM g_groups");
	    while($groups=$db->fetch_array($q)){
			$groupinfo[]=$groups['gid'].','.$groups['groupname'].','.$groups['isregister'].','.$groups['checkregister'];
		}
			$newsettings['groupnames']='{{'.implode($groupinfo,';').'}}';
		foreach ($newsettings as $key => $val){
			$db->query("REPLACE INTO g_settings (var, val) VALUES ('$key', '$val')");
		}
		w2setting();
		writelog($userid."成功删除了".$i."个用户组");
		gadminmessage($i.'个用户组被成功删除',"siteadmin.php?iframe=setgroup",1);

	}


	if($ipost=='update'){
	$isregister=intval($isregister);
	$checkregister=$isregister?intval($checkregister):0;
	if(empty($groupname)){
		gadminmessage('用户组名不能为空');
	}
	$db->query("UPDATE g_groups SET groupname = '$groupname', isregister = $isregister , checkregister = $checkregister WHERE gid =$setgid LIMIT 1");
	$groupinfo=array();
	$q=$db->query("SELECT * FROM g_groups");
	while($groups=$db->fetch_array($q)){
		$groupinfo[]=$groups['gid'].','.$groups['groupname'].','.$groups['isregister'].','.$groups['checkregister'];
	}
	$newsettings['groupnames']='{{'.implode($groupinfo,';').'}}';
	foreach ($newsettings as $key => $val){
			$db->query("REPLACE INTO g_settings (var, val) VALUES ('$key', '$val')");
		}
	w2setting();
	writelog($userid."修改用户组(".$groupname.")成功");
	gadminmessage('修改成功',"siteadmin.php?iframe=setgroup",1);
	}


	if($ipost=='uppermission'){
		$pervalue=bindec(intval($newauthority[19]).intval($newauthority[18]).intval($newauthority[17]).intval($newauthority[16]).intval($newauthority[15]).intval($newauthority[14]).intval($newauthority[13]).intval($newauthority[12]).intval($newauthority[11]).intval($newauthority[10]).intval($newauthority[9]).intval($newauthority[8]).intval($newauthority[7]).intval($newauthority[6]).intval($newauthority[5]).intval($newauthority[4]).intval($newauthority[3]).intval($newauthority[2]).intval($newauthority[1]));
		if($newauthority[1]){
			$pervalue=bindec('1111111111111111111');
		}
		$db->query("UPDATE g_permission SET pervalue = '$pervalue' WHERE gid =$setgid LIMIT 1");
		$q=$db->query("SELECT * FROM g_permission");
		$groupinfo=array();
		while($groupinfo=$db->fetch_array($q)){
			$new_userpermission[]=$groupinfo['gid'].','.$groupinfo['pervalue'];
		}
		$newsettings['userpermission']='{{'.implode($new_userpermission,';').'}}';
		foreach ($newsettings as $key => $val){
		$db->query("REPLACE INTO g_settings (var, val) VALUES ('$key', '$val')");
		}
		w2setting();
		writelog($userid."重置用户组(".$groupnames[$setgid][0].")权限成功");
		gadminmessage('权限设置成功',"siteadmin.php?iframe=setgroup",1);
	}
	if($ipost=='modify'){
		$q=$db->query("SELECT * FROM g_groups WHERE gid=$setgid LIMIT 1");
		$modifygroup=$db->fetch_array($q);

?>
<form method="post" action="siteadmin.php?iframe=setgroup&ipost=update">
<table>
<caption>用户组设置</caption>
  <tr>
    <th><span>修改用户组信息</span></th>
 </tr>
   <tr>
    <td><span><input type="text" name="groupname" class="formtext" value="<?php echo $modifygroup['groupname']?>">&nbsp;
	<input type="hidden" name="setgid" value="<?php echo $modifygroup['gid']?>">&nbsp;
	<input type="checkbox" name="isregister" value="1" <?php echo intval($modifygroup['isregister'])?'checked':''; ?>>是否可注册&nbsp;
	<input type="radio" name="checkregister" value="0" <?php echo intval($modifygroup['checkregister'])==0?'checked':'';?>>不验证
	<input type="radio" name="checkregister" value="1" <?php echo intval($modifygroup['checkregister'])==1?'checked':'';?>>Email验证
	<input type="radio" name="checkregister" value="2" <?php echo intval($modifygroup['checkregister'])==2?'checked':'';?>>人工验证&nbsp;<INPUT TYPE="submit" value="修改"></span></td>
 </tr>
   <tr>
          <td>&nbsp;
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
	      </td>
   </tr>
 </table>
</form>
<?php
	exit();
	}
	if($ipost=='setpermission'){
		$q=$db->query("SELECT * FROM g_permission WHERE gid=$setgid LIMIT 1");
		$permission=$db->fetch_array($q);
		$value=sprintf('%019b', $permission['pervalue']);
		
?>
<form method="post" action="siteadmin.php?iframe=setgroup&ipost=uppermission">
<table>
 <tr>
    <th>
	<INPUT TYPE="hidden" NAME="setgid" value=<?php echo $setgid?>>
	<span>权限项目</span>
	</th>
	<th><span>启用</span></th>
  <tr>
<?php if($setgid!=4){?>
    <td class="boundary"> <span>网站全局系统管理</span></td>
	
	<td class="boundary"><span><input type="checkbox" name="newauthority[1]"  value="1"	
	<?php echo $value{18}?'checked':'';?>> </span></td>
  </tr>
      <tr>
    <td class="boundary"> <span>网站文章管理</span></td>
	<td class="boundary"><span><input type="checkbox" name="newauthority[2]"  value="1"	
	<?php echo $value{17}?'checked':'';?>
	></span></td>
  </tr>
    <tr>
    <td class="boundary"> <span>网站产品管理</span></td>
	<td class="boundary"><span><input type="checkbox" name="newauthority[3]" value="1"	
	<?php echo $value{16}?'checked':'';?>
	></span></td>
  </tr>
      <tr>
    <td class="boundary"> <span>网站论坛管理</span></td>
	<td class="boundary"><span><input type="checkbox" name="newauthority[4]" value="1" 
	<?php echo $value{15}?'checked':'';?>
	></span></td>
  </tr>
    <?php }?>
<tr>
    <td class="boundary"> <span>发布文章评论</span></td>
	<td class="boundary"><span><input type="checkbox" name="newauthority[5]" value="1" 
	<?php echo $value{14}?'checked':'';?>
	></span></td>
  </tr>
  <tr>
    <td class="boundary"> <span>发布文章评论允许上传附件</span></td>
	<td class="boundary"><span><input type="checkbox" name="newauthority[6]" value="1" 
	<?php echo $value{13}?'checked':'';?>
	></span></td>
  </tr>
  <tr>
    <td class="boundary"> <span>发布产品留言</span></td>
	<td class="boundary"><span><input type="checkbox" name="newauthority[7]" value="1" 
	<?php echo $value{12}?'checked':'';?>
	></span></td>
  </tr>
  <tr>
    <td class="boundary"> <span>发布产品留言允许上传附件</span></td>
	<td class="boundary"><span><input type="checkbox" name="newauthority[8]" value="1" 
	<?php echo $value{11}?'checked':'';?>
	></span></td>
  </tr>
    <tr>
    <td class="boundary"> <span>发布论坛主题</span></td>
	<td class="boundary"><span><input type="checkbox" name="newauthority[9]" value="1" 
	<?php echo $value{10}?'checked':'';?>
	></span></td>
  </tr>
    <tr>
    <td class="boundary"> <span>发布主题时允许上传附件</span></td>
	<td class="boundary"><span><input type="checkbox" name="newauthority[10]" value="1" 
	<?php echo $value{9}?'checked':'';?>
	></span></td>
  </tr>
  <tr>
    <td class="boundary"> <span>发布论坛主题回帖</span></td>
	<td class="boundary"><span><input type="checkbox" name="newauthority[11]" value="1" 
	<?php echo $value{8}?'checked':'';?>
	></span></td>
  </tr>
  <tr>
    <td class="boundary"> <span>论坛主题回帖允许上传附件</span></td>
	<td class="boundary"><span><input type="checkbox" name="newauthority[12]" value="1" 
	<?php echo $value{7}?'checked':'';?>
	></span></td>
  </tr>
  <?php if($setgid!=4){?>
    <tr>
    <td class="boundary"> <span>置顶自己的主题</span></td>
	<td class="boundary"><span><input type="checkbox" name="newauthority[13]" value="1" 
	<?php echo $value{6}?'checked':'';?>
	></span></td>
  </tr>

    <tr>
    <td class="boundary"> <span>优先自己的主题</span></td>
	<td class="boundary"><span><input type="checkbox" name="newauthority[14]" value="1" 
	<?php echo $value{5}?'checked':'';?>
	></span></td>
  </tr>

   <tr>
    <td class="boundary"> <span>高亮显示自己的主题</span></td>
	<td class="boundary"><span><input type="checkbox" name="newauthority[15]" value="1" 
	<?php echo $value{4}?'checked':'';?>
	></span></td>
  </tr>
  <tr>
    <td class="boundary"> <span>修改自己的主题</span></td>
	<td class="boundary"><span><input type="checkbox" name="newauthority[16]" value="1" 
	<?php echo $value{3}?'checked':'';?>
	></span></td>
  </tr>
  <tr>
    <td class="boundary"> <span>删除自己的主题</span></td>
	<td class="boundary"><span><input type="checkbox" name="newauthority[17]" value="1" 
	<?php echo $value{2}?'checked':'';?>
	></span></td>
  </tr>
    <tr>
    <td class="boundary"> <span>修改自己的帖子</span></td>
	<td class="boundary"><span><input type="checkbox" name="newauthority[18]" value="1" 
	<?php echo $value{1}?'checked':'';?>
	></span></td>
  </tr>
    <tr>
    <td><span>删除自己的帖子</span></td>
	<td><span><input type="checkbox" name="newauthority[19]" value="1" 
	<?php echo $value{0}?'checked':'';?>
	></span></td>
  </tr>
  <?php }?>
       <tr>
          <td colspan="2">
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
	      </td>
   </tr>
   <?php ?>
  </table>
  <div id="submitdiv"><input name="submit" type="submit" value="提&nbsp;&nbsp;交"></div>
  </form>
<?php
	   exit();
	}

$insideq=$db->query("SELECT * FROM g_groups WHERE type=1 LIMIT 0 , 100");
$outsideq=$db->query("SELECT * FROM g_groups WHERE type=0 LIMIT 0 , 100");
?>

<form method="post" action="siteadmin.php?iframe=setgroup&ipost=save">
<table>
<caption>用户组设置</caption>
  <tr>
    <th><span>添加用户组</span></th>
 </tr>
   <tr>
    <td><span>用户组名<input type="text" name="groupname" class="formtext">&nbsp;<input type="checkbox" name="isregister" value="1">是否可注册&nbsp;<input type="radio" name="checkregister" value="0" >不验证<input type="radio" name="checkregister" value="1" >Email验证<input type="radio" name="checkregister" value="2" >人工验证&nbsp;&nbsp;<INPUT TYPE="submit" value="添加"></span></td>
 </tr>
   <tr>
          <td>&nbsp;
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
	      </td>
   </tr>
 </table>
</form>

 <table>
        <tr >
    <th colspan="4"><span>内置用户组列表</span></td>
  </tr>

  <tr>
	
	  <td width="30%"> <span>用户组名</span></td>
	  <td width="10%"> <span>类型</span></td>
	  <td width="10%"> <span>注册</span></td>
	  <td width="10%"> <span>注册验证</span></td>
	  <td width="20%"> <span>修改</span></td>
	  <td width="20%"> <span>权限设置</span></td>
     </tr>
<?php while($groups=$db->fetch_array($insideq)){?>
	  <tr>
	 <td> <span><?php echo $groups['groupname']?></span></td>
	  <td> <span><?php echo $grouptype[$groups['type']]?></span></td>
	  <td> <span><?php echo $groups['isregister']?'是':'否'?></span></td>
	  <td> <span><?php echo $checkname[$groups['checkregister']]?></span></td>
	  <td> <span>[<a href="siteadmin.php?iframe=setgroup&ipost=modify&setgid=<?php echo $groups['gid']?>">编辑</a>]</a></span></td>
	  <td> <span>[<a href="siteadmin.php?iframe=setgroup&ipost=setpermission&setgid=<?php echo $groups['gid']?>">编辑</a>]</span></td>
     </tr>
<?php }?>
	 	    <tr>
          <td colspan="6">&nbsp;
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
	      </td>
   </tr>
 </table>

 <form method="post" id="groupform">
	
<table>
        <tr >
    <th colspan="7"><span>自定义用户组列表</span></td>
  </tr>

  <tr>
    <td width="10%"> <span><input type="checkbox" name="checkall" id="group_check_all" onclick="checkall_onclick('group')">删?</span></td>
	
	  <td width="20%"> <span>用户组名</span></td>
	  <td width="10%"> <span>类型</span></td>
	   <td width="10%"> <span>注册</span></td>
	  <td width="10%"> <span>注册验证</span></td>
	  <td width="20%"> <span>修改</span></td>
	  <td width="20%"> <span>权限设置</span></td>
     </tr>
<?php 
$i=1;
while($groups=$db->fetch_array($outsideq)){
	?>
 <tr>
      <td> <span><input type="checkbox" name="groupcheck[]" id="group_check_<?php echo $i?>" value="<?php echo $groups['gid']?>"></span></td>
	  <td> <span><?php echo $groups['groupname']?></span></td>
	  <td> <span><?php echo $grouptype[$groups['type']]?></span></td>
	  	  <td> <span><?php echo $groups['isregister']?'是':'否'?></span></td>
	  <td> <span><?php echo $checkname[$groups['checkregister']]?></span></td>
	  <td> <span>[<a href="siteadmin.php?iframe=setgroup&ipost=modify&setgid=<?php echo $groups['gid']?>">编辑</a>]</span></td>
	  <td> <span>[<a href="siteadmin.php?iframe=setgroup&ipost=setpermission&setgid=<?php echo $groups['gid']?>">编辑</a>]</span></td>
  </tr>
<?php 
	$i++;	
	}?>
	 	    <tr>
          <td colspan="7">&nbsp;
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
	      </td>
   </tr>
 </table>
   <div id="submitdiv"><input name="submit" type="submit" value="提&nbsp;&nbsp;交" onclick="deletegroup()"></div>
</form>
 
 <script language="javascript">
function deletegroup()
{
var con;
con=confirm("您确定删除该用户组吗？");
if (con==true) {
	$('groupform').action="siteadmin.php?iframe=setgroup&ipost=delete"
	$('groupform').submit();
	}
}
</script>
 
