<?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);
if($cfg_group_click==1){
	$row = $db->GetOne("SELECT * FROM #@__group_user WHERE gid='$id' AND uid='".$cfg_ml->M_ID."'"); 
	if(!is_array($row)){
		$db->Close();	
		ShowMsg("错误,您还不能在该圈子发贴,请申请成为该圈子会员！","-1");
		exit(); 
	}else if($row['isjoin']==0){
		$db->Close();	
		ShowMsg("错误,您还没有通过审核,请联系圈主！","-1");
		exit(); 
	}
}
if(!isset($action)) $action = '';
if($action=="save"){
	$svali = GetCkVdValue();
  if(strtolower($vdcode)!=$svali || $svali==""){
  	$db->Close();
  	ShowMsg("认证码错误！","-1");
  	exit();
  }
  if(!isset($types)) $types = 0;
	$types = ereg_replace("[^0-9]","",$types);
	if($types<1) $types = 0;
	$subject = cn_substr(trim(ClearHtml($subject)),80);
	if(CountStrLen($subject)<3||CountStrLen($subject)>80){
		$db->Close();
		ShowMsg("主题字数应该在3-80个汉字！","-1");
		exit();
	}
	$threads = eregi_replace("<(iframe|script)","",$threads);
	//V.5.0.0 for Groups max post threads words
	if(!isset($cfg_group_words)) $cfg_group_words = 1000;
	if(CountStrLen($threads)<3 || CountStrLen($threads)>$cfg_group_words){
		$db->Close();
		ShowMsg("主题内容字数应该在3-1000个汉字！","-1");
		exit();
	}
	if(ereg("$cfg_notallowstr",$subject)||ereg("$cfg_notallowstr",$threads)){
		$db->Close();
		ShowMsg("含有非法字符!.","-1");
		exit();
	}
	$subject = preg_replace("/$cfg_replacestr/","***",$subject);
	$threads = preg_replace("/$cfg_replacestr/","***",$threads);
	$userip = GetIP();
	$SetQuery = "INSERT INTO #@__group_threads(gid,smalltype,subject,author,authorid,dateline,lastpost,lastposter) ";
	$SetQuery .= "VALUES('$id','$types','$subject','".$cfg_ml->M_UserName."','".$cfg_ml->M_ID."','".time()."','".time()."','".$cfg_ml->M_UserName."');";
	if($db->ExecuteNoneQuery($SetQuery)){
		$tid = $db->GetLastID();
		$SetQuery = "INSERT INTO #@__group_posts(gid,tid,first,author,authorid,subject,dateline,message,useip) ";
		$SetQuery .= "VALUES('$id','$tid',1,'".$cfg_ml->M_UserName."','".$cfg_ml->M_ID."','$subject','".time()."','$threads','$userip');";
		if($db->ExecuteNoneQuery($SetQuery)){
			Upcountgroups($id);
			$uid = $cfg_ml->M_ID;
			Upcontuserpost($id,$uid,"post");
		}
		$db->Close();
		ShowMsg("成功发表一话题！","viewthread.php?id=$id&tid=$tid");
		exit();
	}else{
		echo $db->GetError();
		$db->Close();
		ShowMsg("出错了！","-1");
		exit();
	}
}

$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();
}
$title = $rs['groupname'];
$des = $rs['des'];
if(empty($des)) $des = "本圈还没有介绍说明.";
$master = $rs['ismaster'];
//检测管理员
$ismaster    = @explode(",",$master);
$ismaster	 = in_array($cfg_ml->M_UserName,$ismaster);
if(empty($cfg_ml->M_UserName)) $ismaster = 0;
$smalltype = $rs['smalltype'];
$userid	= $rs['uid'];
require_once(dirname(__FILE__)."/templets/postform.htm");
unset($rs);
$db->Close();
?>