<?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();
}
$master = $rs['ismaster'];
$userid	= $rs['uid'];
//检测管理员
$ismaster = @explode(",",$master);
$ismaster = in_array($cfg_ml->M_UserName,$ismaster);
if(empty($cfg_ml->M_UserName)) $ismaster = 0;
if($userid==$cfg_ml->M_ID) $ismaster = 1;

if(!$ismaster){
	$db->Close();
	ShowMsg("您尚未当前操作权限！","-1");
	exit();
}
$title = $rs['groupname'].":公告编辑";
//获得公告信息
$rs = $db->GetOne("SELECT * FROM #@__group_notice WHERE gid='$id' ORDER BY stime DESC");
if(!isset($action)) $action = '';
if($action=="save"){
	$notice = eregi_replace("<(iframe|script)","",$notice);
	$subject = cn_substr(trim(ClearHtml($subject)),80);
	$userip = GetIP();
	if(empty($subject)){
		$db->Close();
		ShowMsg("请填写公告标题！","-1");
		exit();
	}
	if(empty($notice)||CountStrLen($notice>100)){
		$db->Close();
		ShowMsg("请填写规定长度的公告内容！","-1");
		exit();
	}
	if($nid<1){
		$SetQuery = "INSERT INTO #@__group_notice(uname,userid,title,notice,stime,gid,ip) VALUES('".$cfg_ml->M_UserName."','".$cfg_ml->M_ID."','".$subject."','".$notice."','".time()."','$id','".$userip."');";
	}else{
		$SetQuery = "UPDATE #@__group_notice SET uname='".$cfg_ml->M_UserName."',userid='".$cfg_ml->M_ID."',title='$subject',notice='$notice',stime='".time()."',ip='$userip' WHERE id='$nid';";
	}
	$db->ExecuteNoneQuery($SetQuery);
	echo $db->GetError();
	$db->Close();
	ShowMsg("已经更改公告!","group.php?id={$id}");
	exit();
}
if(!is_array($rs)){
	$subject = "公告!";
	$nid = 0;
	$notice = "系统:本圈刚开张,大家多发贴.送人鲜花 手有余香,让我们为公益事业加油!";
}else{
	$nid = $rs['id'];
	$subject = $rs['title'];
	$notice  = $rs['notice'];
}


require_once(dirname(__FILE__)."/templets/noticeform.htm");
$db->Close();
?>