<?php
if(!defined('IN_GATEWEI')) {	exit('access denied');}
$s=array('memberlist'=>0,'emaillist'=>1,'checklist'=>2,'forbidlist'=>3);
$t=array('memberlist'=>'会员列表','emaillist'=>'email未激活名单','checklist'=>'人工审核名单','forbidlist'=>'禁用用户名单');
if($ipost=="save"){
	$new_userid=trim($new_userid);
	$new_username=trim($new_username);
	$new_email=trim($new_email);
	if(empty($new_userid) || empty($new_email) || !$new_gid || empty($new_userpwd)){
		gadminmessage("用户账号、邮箱、用户组、密码不能为空");
	}
	$memberformat=preg_match("/(^[a-z0-9_]+$)/", $new_userid);
	if(strlen($new_userid)>16 || strlen($new_userid)<4){
			$memberformat=false;
		}
	if(!$memberformat){
		gadminmessage("用户账号只能是字母、数字或下划线,且字符数大于3位小于16位");
	}
	if (!isemail($new_email))	{
		gadminmessage("email地址输入不正确");
	}
	$new_userpwd=md5(trim($new_userpwd));
	$new_checkuserpwd=md5(trim($new_checkuserpwd));
	if(strcmp($new_userpwd,$new_checkuserpwd)){
		gadminmessage("两次输入的密码不一致");
	}
	if($db->num_rows($db->query("select uid from g_members where userid='$new_userid'"))){
		gadminmessage("用户名已经存在");
	}
	$new_registerip=$currentip;
	$db->query("insert into g_members (uid ,userid ,username , userface , email , gid ,userpwd ,logintimes ,registerip ,registertime) values (null , '$new_userid', '$new_username', '01.png' , '$new_email', $new_gid ,'$new_userpwd', 0 ,'$new_registerip', $timestamp);");
	$new_uid=$db->insert_id();
	$db->query("INSERT INTO g_member_info (uid , msgboxcount , lastlogintime  , logintimes , sex , phone , mobile , qq ,address)VALUES ( $new_uid , 0 , $timestamp , 0 , 0 , '' , '' , '' , '')");
	writelog($userid."成功添加了一个ID为".$new_userid."的新用户");
	if(intval($allowedemailmsg)){
			$emailtitle=str_replace(array('{name}','{sitename}'),array($new_userid,$sitename),$emailtitle);
			$emailmsg=str_replace(array('{name}','{sitename}','{adminemail}'),array($new_userid,$sitename,$emailaddress),$emailmsg);
			$emailtitle=iconv("UTF-8","GB2312//IGNORE",$emailtitle);
			$emailmsg=iconv("UTF-8","GB2312//IGNORE",$emailmsg);
			@gsendmail($new_email,$emailtitle,$emailmsg);
		}
	gadminmessage("用户添加成功","siteadmin.php?iframe=setuser&ipost=memberlist",1);
}

if($ipost=="update"){
	$modifyusername=trim($modifyusername);
	$modifyemail=trim($modifyemail);
	if(empty($modifyuserid) || empty($modifyemail) ||!$modifygid){
		gadminmessage("用户账号、邮箱、用户组不能为空");
	}
	if (!preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $modifyemail))	{
		gadminmessage("email地址输入不正确");
	}

	$db->query("update g_members a , g_member_info b set a.username = '$modifyusername', a.email ='$modifyemail' , a.gid = $modifygid , b.sex=$modifysex , b.phone='$modifyphone' , b.mobile= '$modifymobile' , b.mobile = '$modifyqq' , b.address='$modifyaddress' where a.uid =$post_uid and a.uid=b.uid");
		gadminmessage("修改成功","siteadmin.php?iframe=setuser&ipost=memberlist",1);
}

if($ipost=="delete"){
	$mcount=count($mid);
	$mid=implode($mid,',');
	$db->query("delete from g_members where uid in ($mid)");
	$db->query("delete from g_member_info where uid in ($mid)");
	$db->query("delete from g_member_mailcode where uid in ($mid)");
	writelog($userid."删除了".$mcount."个用户(".$mid.")");
	gadminmessage("删除成功","siteadmin.php?iframe=setuser&ipost=memberlist",1);
}

if($ipost=="forbid"){
	$mcount=count($mid);
	$mid=implode($mid,',');
	$db->query("update g_members set state = 3 where uid in ($mid)");
	writelog($userid."禁用".$mcount."个用户(".$mid.")");
	gadminmessage("用户已经禁用，禁用用户不能登录网站","siteadmin.php?iframe=setuser&ipost=memberlist",1);
}

if($ipost=="enable"){
	$mcount=count($mid);
	$mid=implode($mid,',');
	$db->query("update g_members set state = 0 where uid in ($mid)");
	writelog($userid."启用了".$mcount."个用户(".$mid.")");
	gadminmessage("用户已经正式启用","siteadmin.php?iframe=setuser&ipost=memberlist",1);
}

if($ipost=="certificate"){
	$mid_str=implode($mid,',');
	$db->query("update g_members set state = 0 where uid in ($mid_str)");
	if(intval($allowedemailmsg)){
		$q=$db->query("select userid , email from g_members where uid in ($mid_str)");
		$utf8_emailtitle=$emailtitle;
		$utf8_emailmsg=$emailmsg;
		while($membertinfo=$db->fetch_array($q)){
			$replace_emailtitle=$utf8_emailtitle;
			$replace_emailmsg=$utf8_emailmsg;
			$emailtitle=str_replace(array('{name}','{sitename}'),array($membertinfo['userid'],$sitename),$replace_emailtitle);
			$emailmsg=str_replace(array('{name}','{sitename}','{adminemail}'),array($membertinfo['userid'],$sitename,$emailaddress),$replace_emailmsg);
			$gb_emailtitle=iconv("UTF-8","GB2312//IGNORE",$emailtitle);
			$gb_emailmsg=iconv("UTF-8","GB2312//IGNORE",$emailmsg);
			@gsendmail($membertinfo['email'],$gb_emailtitle,$gb_emailmsg);
			sleep(5);
			}
		}
	gadminmessage("用户用户成功通过审核","siteadmin.php?iframe=setuser&ipost=memberlist",1);
}


if($ipost=="changepwd"){
	$logpwd=substr($userpwd,0,3).'***';
	$userpwd=md5(trim($userpwd));
	$checkuserpwd=md5(trim($checkuserpwd));
	if(strcmp($userpwd,$checkuserpwd)){
		gadminmessage("两次输入的密码不一致");
	}
	$db->query("update g_members set userpwd = '$userpwd' where uid=$post_uid limit 1");
	writelog($userid."重设用户ID为".$post_userid."\t的密码\t".$logpwd);
	gadminmessage("新密码设置成功","siteadmin.php?iframe=setuser&ipost=memberlist",1);
}

if($ipost=="modifypwd"){
?>
<form method="post" action="siteadmin.php?iframe=setuser&ipost=changepwd">
<table>
<caption>系统管理用户密码修改</caption>
 <tr >
    <th colspan="2"><span>设置新密码</span>
	<input type="hidden" name="post_uid" value="<?php echo $get_uid?>" >
	<input type="hidden" name="post_userid" value="<?php echo $get_userid?>" ></td>
  </tr>
 <tr>
    <td   > <span>设置新密码</span></td>
    <td  ><input name="userpwd" type="password" class="formtext"></td>
  </tr>
  <tr>
    <td   ><span>新密码确认</span></td>
    <td  ><input name="checkuserpwd" type="password" class="formtext"></td>
  </tr>
    <tr>
          <td colspan="2">&nbsp;
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
	      </td>
   </tr>
   <tr>
  <td colspan="2">
  <div id="submitdiv">
      <input type="submit" value="提&nbsp;&nbsp;交">
</div>
</td>
   </tr>
 </table>
</form>
<?php
	exit();
}

if($ipost=="modify"){
	$q=$db->query("select * from g_members a left join g_member_info b on a.uid=b.uid where a.uid=$get_uid limit 1");
	$numbers=$db->fetch_array($q);
?>
<form method="post" action="siteadmin.php?iframe=setuser&ipost=update">
<table>
<caption>系统管理用户信息修改</caption>
 <tr >
    <th colspan="2"><span>修改用户</span><input type="hidden" name="post_uid" value="<?php echo $numbers['uid']?>" ></td>
  </tr>
  <tr>
    <td  width="37%" > <span>用户账号</span></td>
    <td width="63%" ><input name="modifyuserid" type="text" class="formtext" value="<?php echo $numbers['userid']?>"  readonly ></td>
  </tr>
    <tr>
    <td   > <span>用户姓名</span></td>
    <td  ><input name="modifyusername" type="text" class="formtext" value="<?php echo $numbers['username']?>" ></td>
  </tr>
      <tr>
    <td   > <span>电子邮箱</span></td>
    <td  ><input name="modifyemail" type="text" class="formtext" value="<?php echo $numbers['email']?>" ></td>
  </tr>
        <tr>
    <td   > <span>所属用户组</span></td>
    <td  >
		<select id="modifygid" name="modifygid" class="formselect">
		<option value="0" selected >请选择用户组</option >
		<?php foreach ($groupnames as $key => $value){
	           $g_selected=($numbers['gid']==$key)?'selected':'';
		echo "<option value=\"".$key."\" ".$g_selected." >".$value[0]."</option>";
		}
		?>
      </select>
	</td>
  </tr>
    <tr>
          <td colspan="2">&nbsp;
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
	      </td>
   </tr>
<tr>
    <td  > <span>性别:</span></td>
	 <td  >
	 <input type="radio" name="modifysex" value="0" <?php if($numbers['sex']==0) {echo 'checked';}?>> 男 
	 <input type="radio" name="modifysex" value="1" <?php if($numbers['sex']==1) {echo 'checked';}?>> 女 
	 <input type="radio" name="modifysex" value="2" <?php if($numbers['sex']==2) {echo 'checked';}?>> 保密
	 </td>
  </tr>
  <tr>
    <td  > <span>电话:</span></td>
	 <td  ><input type="text" name="modifyphone" value="<?php echo $numbers['phone']?>" ></td>
  </tr>
  <tr>
    <td  > <span>手机:</span></td>
	 <td  ><input type="text" name="modifymobile" value="<?php echo $numbers['mobile']?>" ></td>
  </tr>
    <tr>
    <td  > <span>qq:</span></td>
	 <td ><input type="text" name="modifyqq" value="<?php echo $numbers['qq']?>" ></td>
  </tr>

  <tr>
    <td  > <span>联系地址:</span></td>
	 <td ><input type="text" name="modifyaddress" value="<?php echo $numbers['address']?>" ></td>
  </tr>
   <tr>
  <td colspan="2">
  <div id="submitdiv">
      <input type="submit" value="提&nbsp;&nbsp;交">
</div>
</td>
   </tr>
 
 </table>
</form>
<?php
	exit();
}


?>

<?php if($ipost=='add'){ ?>
<form method="post" action="siteadmin.php?iframe=setuser&ipost=save">

<table>
<caption>系统管理用户设置</caption>
 <tr >
    <th colspan="2"><span>添加用户</span></td>
  </tr>
  <tr>
    <td  width="37%" > <span>用户账号</span></td>
    <td width="63%" ><input name="new_userid" type="text" class="formtext"></td>
  </tr>
    <tr>
    <td   > <span>用户姓名</span></td>
    <td  ><input name="new_username" type="text" class="formtext"></td>
  </tr>
      <tr>
    <td   > <span>电子邮箱</span></td>
    <td  ><input name="new_email" type="text" class="formtext"></td>
  </tr>
        <tr>
    <td   > <span>所属用户组</span></td>
    <td  >
		<select name="new_gid" class="formselect">
		<option value="0" >请选择用户组</option>
			<?php foreach ($groupnames as $key => $value){
		echo "<option value=\"".$key."\" >".$value[0]."</option>";
		}
		?>
		   </select>
	</td>
  </tr>
        <tr>
    <td   > <span>设置密码</span></td>
    <td  ><input name="new_userpwd" type="password" class="formtext"></td>
  </tr>
  <tr>
    <td   ><span>密码确认</span></td>
    <td  ><input name="new_checkuserpwd" type="password" class="formtext"></td>
  </tr>
   <tr>
  <td colspan="2">
  <div id="submitdiv">
      <input type="submit" value="提&nbsp;&nbsp;交">
</div>
</td>
   </tr>
   <tr>
          <td colspan="2">&nbsp;
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
	      </td>
   </tr>
 </table>
</form>

<?php
exit();
}
$memberlistcolor="lightgrey";
$forbidlistcolor="lightgrey";
$checklistcolor="lightgrey";
$emaillistcolor="lightgrey";
if($ipost=="memberlist"){
	$memberlistcolor="black";
}elseif($ipost=="forbidlist"){
	$forbidlistcolor="black";
}elseif($ipost=="checklist"){
	$checklistcolor="black";
}elseif($ipost=="emaillist"){
	$emaillistcolor="black";
}
$pagerecords=10;
$curpage=isset($page)?$page:1;
$startrecord=($curpage-1)*$pagerecords;
$totalrecord=$db->result($db->query("select count(*) from g_members where state={$s[$ipost]}"),0);
$q=$db->query("select * from g_members where state={$s[$ipost]} limit $startrecord , $pagerecords ");
if($action=="query"){
	$memberformat=preg_match("/(^[a-z0-9_]+$)/", $membername);
	if(!$memberformat){
		gadminmessage("用户账号只能是字母、数字或下划线");
	}
	$q=$db->query("select * from g_members where state={$s[$ipost]} and userid like '%$membername%' limit $startrecord , $pagerecords ");
}
$multipage=multi($totalrecord, $pagerecords, $curpage, "siteadmin.php?iframe=setuser&ipost=".$ipost);
?>

<table>
<form method="post" action="siteadmin.php?iframe=setuser&ipost=<?php echo $ipost?>&action=query">
 <tr >
    <th>
	查询表单
	</th>
</tr>
</table>
     <tr>
      <td width="15%"><span>会员名称</span></td>
      <td width="85%">
	  <input type="text" name="membername" class="formtext"> 
	  <input type="submit" value="查询" ></td>  
	 </tr>
	  <tr>
          <td>
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
	      </td>
   </tr>
</form>
</table>
<form method="post" id="mlist">
<table>
        <tr >
    <th colspan="7" class="font_normal">
	<span> <a href="siteadmin.php?iframe=setuser&ipost=memberlist"><font color="<?php echo $memberlistcolor?>">会员列表</font></a> </span>&nbsp;
	<span> <a href="siteadmin.php?iframe=setuser&ipost=forbidlist"><font color="<?php echo $forbidlistcolor?>">禁用用户列表</font></a> </span>&nbsp;
	<span> <a href="siteadmin.php?iframe=setuser&ipost=checklist"><font color="<?php echo $checklistcolor?>">人工审核列表</font></a> </span> &nbsp;
	<span> <a href="siteadmin.php?iframe=setuser&ipost=emaillist"><font color="<?php echo $emaillistcolor?>">email未激活列表</font></a></span></td>
  </tr>

  <tr>
    <td width="5%"> <span><input type="checkbox" id="mid_check_all" onclick="checkall_onclick('mid')"></span></td>
	<td width="5%" align="center"> <span>修改</span></td>
	 <td width="10%" align="center"> <span>设置密码</span></td>
	  <td width="10%"> <span>用户账号</span></td>
	  <td width="20%"> <span>用户姓名</span></td>
	  <td width="30%"> <span>用户邮箱</span></td>
	  <td width="10%"> <span>用户组</span></td>
     </tr>
<?php
$i=0;
while($numbers=$db->fetch_array($q)){
?>
<tbody>
	  <tr>
    <td> <span><input type="checkbox" id="mid_check_<?php echo $i?>" name="mid[]" value="<?php echo $numbers['uid']?>"></span></td>
	<td align="center"> <span><a href="siteadmin.php?iframe=setuser&ipost=modify&get_uid=<?php echo $numbers['uid']?>"><img src="./images/siteadmin/g_admin_edit.gif" class="absmiddle"></a></span></td>
	 <td align="center"> <span><a href="siteadmin.php?iframe=setuser&ipost=modifypwd&get_uid=<?php echo $numbers['uid']?>&get_userid=<?php echo $numbers['userid']?>"><img src="./images/siteadmin/g_admin_password.gif" class="absmiddle"></a></span></td>
	  <td> <span><?php echo $numbers['userid']?></span></td>
	  <td> <span><?php echo $numbers['username']?></span></td>
	  <td> <span><?php echo $numbers['email']?></span></td>
	  <td> <span><?php echo $groupnames[$numbers['gid']][0]?></span></td>
     </tr>
</tbody>
	 <?php 
$i++;	
}
?>
	  <tr>
    <th colspan="7" class="font_normal"> 
	<?php 
	if($multipage){
		echo '<div class="pages_btns font_14">'.$multipage.'</div>';
	}
	?>
	</th>
     </tr>
	 	    <tr>
          <td colspan="7">
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
	      </td>
   </tr>
     <tr>
          <th colspan="7">&nbsp;
		  <?php if($ipost=='memberlist'){?>
			<input type="button" value="用户禁用" onclick="forbiduser()">
		  <?php }?>
		  <?php if($ipost=='forbidlist'){?>
			<input type="button" value="用户启用" onclick="enableuser()">
		  <?php }?>
			<input type="button" value="删除用户" onclick="deluser()" >
		   <input type="button" value="添加新会员" onclick="adduser()">
			 <input type="button" value="发送短消息" onclick="sendmsg()">
			 <?php if($ipost=="emaillist" || $ipost=="checklist"){?>
		   <input type="button" value="通过审核" onclick="certificate()">(如果开启了"发送欢迎Email信息"，审核过程时间较长)
		   <?php }?>
	
	      </th>
   </tr>
 </table>
</form>
<script language="javascript">

function deluser(){

$('mlist').action='siteadmin.php?iframe=setuser&ipost=delete';
if(!is_checked('mid')){
	alert('请勾选用户');
	return false;
	}
$('mlist').submit();
}

function forbiduser(){
$('mlist').action='siteadmin.php?iframe=setuser&ipost=forbid';
if(!is_checked('mid')){
	alert('请勾选用户');
	return false;
	}
	$('mlist').submit();
}

function enableuser(){
$('mlist').action='siteadmin.php?iframe=setuser&ipost=enable';
if(!is_checked('mid')){
	alert('请勾选用户');
	return false;
	}
	$('mlist').submit();
}


function adduser(){
window.location.href='siteadmin.php?iframe=setuser&ipost=add';
}


function certificate(){
$('mlist').action='siteadmin.php?iframe=setuser&ipost=certificate';
if(!is_checked('mid')){
	alert('请勾选用户');
	return false;
	}
	$('mlist').submit();
}

function sendmsg(){
	if(!is_checked('mid')){
		return alert('请勾选用户');
	}
	$('mlist').action='siteadmin.php?iframe=sendmsg&ipost=unicast';
	$('mlist').submit();
}

</script>

 
