<?php
require_once(dirname(__FILE__)."/config.php");
require_once(dirname(__FILE__)."/../member/inc/inc_archives_functions.php");
if(!isset($gid)) $gid = NULL;
$id = ereg_replace("[^0-9]","",$gid);
if($id < 1){
	ShowMsg("含有非法操作!.","-1");
	exit();
}
function CountStrLen($str){
	$ccLen=0;$ascLen=strlen($str);$ind=0;
	$hasCC=ereg("[xA1-xFE]",$str); //判断是否有汉字
	$hasAsc=ereg("[x01-xA0]",$str); //判断是否有ASCII字符
	if($hasCC && !$hasAsc) return strlen($str)/2;//只有汉字的情况
	if(!$hasCC && $hasAsc) return strlen($str);//只有Ascii字符的情况
	for($ind=0;$ind<$ascLen;$ind++){
		if(ord(substr($str,$ind,1))>0xa0)
		{
		$ccLen++;
		$ind++;
		}
		else
		{
		$ccLen++;
		}
	}
	return $ccLen;
}
$db = new DedeSql(false);
//取出圈子信息
$row = $db->GetOne("SELECT uid FROM #@__groups WHERE groupid=".$id);
if(!is_array($row)){
	$db->Close();
	ShowMsg("圈子不存在!","-1");
	exit();
}
if($cfg_ml->M_ID!=$row['uid']){
	$db->Close();
	ShowMsg("该圈子不在你的管辖范围内!","-1");
	exit();
}
$msg = NULL;
if(!isset($action)) $action = NULL;
if($action=="edit"){
	$notice = eregi_replace("<(iframe|script)","",$notice);
	$subject = cn_substr(trim(ClearHtml($subject)),80);
	$userip = GetIP();
	if(empty($subject)){
		$msg = "请填写公告标题！";
	}else if(empty($notice)||CountStrLen($notice>100)){
		$msg = "请填写规定长度的公告内容！";
	}else{
		$SetQuery = "UPDATE #@__group_notice SET title='$subject',notice='$notice',stime='".time()."',ip='$userip' WHERE id='$nid';";
		$db->ExecuteNoneQuery($SetQuery);
		$msg = "已经更改公告!";
	}
}else if($action=="add"){
	$notice = eregi_replace("<(iframe|script)","",$notice);
	$subject = cn_substr(trim(ClearHtml($subject)),80);
	$userip = GetIP();
	if(empty($subject)){
		$msg = "请填写公告标题！";
	}else if(empty($notice)||CountStrLen($notice>100)){
		$msg = "请填写规定长度的公告内容！";
	}else{
		$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."');";
		$db->ExecuteNoneQuery($SetQuery);
		$msg = "已经更改公告!";
	}
}
$SetQuery = NULL;
//公告信息
$row = $db->GetOne("SELECT `id`,`title`,`notice` FROM `#@__group_notice` WHERE `gid`=".$id);
if(is_array($row)) {
	$nid = $row['id'];
	$fromset = 'edit';
	$title = $row['title'];
	$notice = $row['notice'];
}else{
	$nid = 0;
	$fromset = 'add';
	$title = '';
	$notice = '';
}
unset($row);
require_once(dirname(__FILE__)."/templets/mygroup_notice.htm");
$db->Close();
?>