<?php
require_once(dirname(__FILE__)."/config.php");
if(!$cfg_ml->IsLogin()){
	ShowMsg("未登录前不充许该操作！","-1");
	exit(); 
}
$tid = ereg_replace("[^0-9]","",$tid);
$id = ereg_replace("[^0-9]","",$id);
if($id < 1 || $tid < 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."' AND isjoin='1'"); 
	if(!is_array($row)){
		$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'];
//贴子内容信息
$rs = $db->GetOne("SELECT * FROM #@__group_threads WHERE tid='$tid'");
$subjects = $rs['subject'];
$typeids = $rs['smalltype'];
if(isset($pid) && $pid > 0){
	$rs = $db->GetOne("SELECT * FROM #@__group_posts WHERE pid='$pid'");
	$first   = $rs['first'];
	$message = $rs['message'];
	$uid = $rs['authorid'];
}
if(!isset($message)) $message = '';
if(!isset($action)) $action = '';
$message = preg_replace("'<div style=\"color:#ccc;\" id=\"lastedit\">.*?</div>'is","",$message);
if($action=="save"){
	$svali = GetCkVdValue();
  if(strtolower($vdcode)!=$svali || $svali==""){
  	$db->Close();
  	ShowMsg("认证码错误！","-1");
  	exit();
  }
  if(!isset($subject)) $subject = '';
	$subject = cn_substr(trim(ClearHtml($subject)),80);
	$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);
	$threads = str_replace('\n','<br>',$threads);
	$threads = preg_replace("'<div style=\"color:#ccc;\" id=\"lastedit\">.*?</div>'is","",$threads);	
	$userip = GetIP();
	$SetQuery = "INSERT INTO #@__group_posts(gid,tid,first,author,authorid,subject,dateline,message,useip) ";
	$SetQuery .= "VALUES('$id','$tid',0,'".$cfg_ml->M_UserName."','".$cfg_ml->M_ID."','$subject','".time()."','$threads','$userip');";
	if($db->ExecuteNoneQuery($SetQuery)){
			$uid = $cfg_ml->M_ID;
			$db->ExecuteNoneQuery("UPDATE #@__group_threads SET lastpost='".time()."',lastposter='".$cfg_ml->M_UserName."',replies=replies+1 WHERE tid='$tid';");
			Upcontuserpost($id,$uid,"replies");
			UpcontReplies($tid,$id);
	}
	$db->Close();
	ShowMsg("成功添加回复话题！","viewthread.php?id=$id&tid=$tid");
	exit();
}else if($action=="edit" && ($ismaster||$cfg_ml->M_ID==$uid)){
	$svali = GetCkVdValue();
  if(strtolower($vdcode)!=$svali || $svali==""){
  	$db->Close();
  	ShowMsg("认证码错误！","-1");
  	exit();
  }
	$subject = cn_substr(trim(ClearHtml($subject)),80);
	if(CountStrLen($subject)>80){
		$db->Close();
		ShowMsg("主题字数应该在3-80个汉字！","-1");
		exit();
	}
	$threads = cn_substr(eregi_replace("<(iframe|script)","",$threads),2000);
	if(CountStrLen($threads)>1000){
		$db->Close();
		ShowMsg("主题内容字数应该在3-1000个汉字！","-1");
		exit();
	}
	if(empty($threads)) $threads = $message;
	if(ereg("$cfg_notallowstr",$subject)||ereg("$cfg_notallowstr",$threads)){
		$db->Close();
		ShowMsg("含有非法字符!.","-1");
		exit();
	}
	if($first){
		$types = ereg_replace("[^0-9]","",$types);
		if($types<1) $types = 0;
		if(strlen($subject)<3){
			$db->Close();
			ShowMsg("主题字数应该在3-80个汉字！","-1");
			exit();
		}
	}
	$subject = preg_replace("/$cfg_replacestr/","***",$subject);
	$threads = preg_replace("/$cfg_replacestr/","***",$threads);
	$threads .= "<div style=\"color:#ccc;\" id=\"lastedit\">最后由:".$cfg_ml->M_UserName."编辑过.时间:".date('Y-m-d h:i:s',time())."</div>";
	$userip = GetIP();
	if($first) $db->ExecuteNoneQuery("UPDATE #@__group_threads SET smalltype='$types',subject='$subject' WHERE tid='$tid';");
	$db->ExecuteNoneQuery("UPDATE #@__group_posts SET subject='$subject',message='$threads' WHERE pid='$pid';");
	$db->Close();
  ShowMsg("成功修改贴子！","viewthread.php?id=$id&tid=$tid");
  exit();
}
if($do=="form") require_once(dirname(__FILE__)."/templets/reply.htm");
else if($do=="edit") require_once(dirname(__FILE__)."/templets/reply_edit.htm");
unset($rs);
$db->Close();
?>