<?php
require_once(dirname(__FILE__)."/config.php");
if(!$cfg_ml->IsLogin()){
	ShowMsg("未登录前不充许该操作！","-1");
	exit(); 
}
$id = ereg_replace("[^0-9]","",$id);
if($id < 1) {
	ShowMsg("错误,未定义的操作！","-1");
	exit(); 
}
$db = new DedeSql(false);
$rs = $db->GetOne("SELECT * FROM #@__groups WHERE groupid='$id'");
if(!is_array($rs)){
	$db->Close();
	ShowMsg("圈子不存在,或被删除！","-1");
	exit(); 
}else if($rs['ishidden']){
	$db->Close();
	ShowMsg("圈子被管理员屏蔽中！","-1");
	exit();
}
if(!isset($do)) $do = '';
if($do=="ok"&&isset($uid)){
	$master = explode(",",$rs['ismaster']);
	$row = $db->GetOne("SELECT username FROM #@__group_user WHERE uid='$uid' AND gid='$id'");
	if(in_array($row['username'],$master)){
		$db->Close();
		ShowMsg("错误,".$row['username']."已经是管理员！","group.php?id=$id");
		exit();
	}
	array_push($master,$row['username']);
	$master = array_filter($master);
	$ismaster = implode(",",$master);
	$db->ExecuteNoneQuery("UPDATE #@__groups SET ismaster='{$ismaster}' WHERE groupid='$id'");
	$db->Close();
	$msg = "已将 {$username},设为管理员!";
	ShowMsg("{$msg}","group.php?id=$id");
	exit();
}
$title = $rs['groupname'];
//检测管理员
$ismaster    = @explode(",",$rs['ismaster']);
$ismaster	 = in_array($cfg_ml->M_UserName,$ismaster);
if(empty($cfg_ml->M_UserName)) $ismaster = 0;
$uid  = $rs['uid'];
if($ismaster){
	$db->Close();
	ShowMsg("您已经是本圈管理员！","-1");
	exit(); 
}
$rs = $db->GetOne("SELECT * FROM #@__group_user WHERE gid='$id' AND uid='".$cfg_ml->M_ID."' AND isjoin='1'");
if(!is_array($rs)){
	$db->Close();
	ShowMsg("您不是该圈子正式会员,无法申请管理员！","group.php?id=$id");
	exit(); 
}else{
	$subject = $cfg_ml->M_UserName.",向您申请为圈子:".$title.",的管理员.";
	$url = "/group/regadmin.php?do=ok&id=".$id."&uid=".$cfg_ml->M_ID;
	$message = $subject." [<font style=\"color:red\"><a href=\"".$url."\">同意通过</a></font>]";
	$db->ExecuteNoneQuery("INSERT INTO #@__pms(msgfrom,msgfromid,msgtoid,folder,new,subject,dateline,message) VALUES('".$cfg_ml->M_LoginID."','".$cfg_ml->M_ID."','".$uid."','inbox',1,'".$subject."','".time()."','".$message."');");
	//更新目标用户新邮件数目
	$row = $db->GetOne("Select COUNT(*) AS c From #@__pms where msgtoid='$uid' AND folder='inbox' AND `new`='1'");
	if($row['c'] > 0) $db->ExecuteNoneQuery("UPDATE #@__member SET newpm='".$row['c']."' WHERE ID='{$uid}'");
	//更新对方缓存
	$cfg_ml->FushCache($uid);
	$db->Close();
	ShowMsg("系统已为你提交申请","-1");
	exit();
}
?>