<?php
!function_exists('readover') && exit('Forbidden');
/**
* 版块权限判断
*/
if ($foruminfo['allowrp'] && !$admincheck && !allowcheck($foruminfo['allowrp'],$groupid,$winddb['groups'],$fid,$winddb['reply'])) {
	Showmsg('reply_forum_right');
}
/**
* 用户组权限判断
*/
if (!$foruminfo['allowrp'] && !$admincheck && $_G['allowrp']==0) {
	Showmsg('reply_group_right');
}
if ($article == '0') {
	$pw_tmsgs = GetTtable($tid);
	$S_sql = ',m.uid,m.groupid,m.userstatus,tm.ifsign,tm.content';
	$J_sql = "LEFT JOIN pw_members m ON m.uid=t.authorid LEFT JOIN $pw_tmsgs tm ON tm.tid=t.tid";
} else {
	$S_sql = $J_sql = '';
}
$tpcarray = $db->get_one("SELECT t.fid,t.locked,t.ifcheck,t.author,t.authorid,t.postdate,t.lastpost,t.ifmail,t.special,t.subject,t.type,t.ifshield,t.anonymous,t.ptable,t.replies $S_sql FROM pw_threads t $J_sql WHERE t.tid=".pwEscape($tid));
$pw_posts = GetPtable($tpcarray['ptable']);

$t_date = $tpcarray['postdate'];//主题发表时间 bbspostguide 中用到
if ($tpcarray['fid']!=$fid) {
	Showmsg('illegal_tid');
}
$replytitle=$tpcarray['subject'];
/**
* convert()需要$tpc_author变量
*/
$tpc_author=$tpcarray['author'];

if ($forumset['lock']&& !$isGM && $timestamp - $tpcarray['postdate'] > $forumset['lock'] * 86400 && !pwRights($isBM,'replylock')) {
	Showmsg('forum_locked');
}
if (!$isGM && !$tpcarray['ifcheck'] && !pwRights($isBM,'viewcheck')) {
	Showmsg('reply_ifcheck');
}
if (!$isGM && $tpcarray['locked']%3<>0 && !pwRights($isBM,'replylock')) {
	Showmsg('reply_lockatc');
}
$special = 0;
$icon = (int)$icon;

if (empty($_POST['step'])) {
	##主题分类
	$db_forcetype = 0;
	require_once(R_P.'require/bbscode.php');
	$hideemail = 'disabled';

	if ($action == 'quote') {
		if ($article == '0') {
			$atcarray = $tpcarray;
		} else {
			!is_numeric($pid) && Showmsg('illegal_tid');
			$atcarray = $db->get_one("SELECT p.author,p.authorid,p.subject,p.ifsign,p.postdate,p.content,p.ifshield,p.anonymous,m.uid,m.groupid,m.userstatus FROM $pw_posts p LEFT JOIN pw_members m ON m.uid=p.authorid WHERE p.pid=".pwEscape($pid));
		}
		if ($atcarray['ifshield']) {//单帖屏蔽
			$atcarray['content'] = shield($atcarray['ifshield']=='1' ? 'shield_article' : 'shield_del_article');
		} elseif (isban($atcarray,$fid)) {
			$atcarray['content'] = shield('ban_article');
		}
		$ifsign = $atcarray['ifsign'];
		$old_author = $atcarray['anonymous'] ? $db_anonymousname : $atcarray['author'];
		$replytitle = $atcarray['subject'];
		$wtof_oldfile = get_date($atcarray['postdate']);
		$old_content = $atcarray['content'];
		$old_content = preg_replace("/\[hide=(.+?)\](.+?)\[\/hide\]/is",getLangInfo('post','hide_post'),$old_content);
		$old_content = preg_replace("/\[post\](.+?)\[\/post\]/is",getLangInfo('post','post_post'),$old_content);
		$old_content = preg_replace("/\[sell=(.+?)\](.+?)\[\/sell\]/is",getLangInfo('post','sell_post'),$old_content);
		$old_content = preg_replace("/\[quote\](.*)\[\/quote\]/is","",$old_content);
		$bit_content = explode("\n",$old_content);

		if (count($bit_content) > 5) {
			$old_content = "$bit_content[0]\n$bit_content[1]\n$bit_content[2]\n$bit_content[3]\n$bit_content[4]\n.......";
		}
		if (strpos($old_content,$db_bbsurl) !== false) {
			$old_content = str_replace('p_w_picpath',$db_picpath,$old_content);
			$old_content = str_replace('p_w_upload',$db_attachname,$old_content);
		}
		$old_content = preg_replace("/\<(.+?)\>/is","",$old_content);
		$atc_content = "[quote]".($article==0 ? getLangInfo('post','info_post_1') : getLangInfo('post','info_post_2'))."\n{$old_content} [url={$db_bbsurl}/job.php?action=topost&tid=$tid&pid=$pid][img]{$db_bbsurl}/images/back.gif[/img][/url]\n[/quote]\n";
	}
	list($guidename,$forumtitle) = getforumtitle(forumindex($foruminfo['fup'],1),1);
	$guidename .= " &raquo; <span class=\"crumbs-item current\"><i> </i><strong><a href=\"read.php?tid=$tid\">$tpcarray[subject]</a></strong>";
	if (!$replytitle) {
		$atc_title = "Re:$tpcarray[subject]";
		$forumtitle = "$atc_title|$forumtitle";
	} else {
		$atc_title = "Re:$replytitle";
		$forumtitle = "$atc_title|$tpcarray[subject]|$forumtitle";
	}
	$db_metakeyword = str_replace(array('|',' - '),',',$forumtitle).'phpwind';

	require_once(R_P.'require/header.php');
	$post_reply = '';
	if ($db_showreplynum > 0) {
		$pwViewHide = $isGM || pwRights($isBM,'viewhide');
		$query = $db->query("SELECT p.author,p.authorid,p.subject,p.postdate,p.content,p.anonymous,p.ifconvert,p.ifwordsfb,p.ifshield,m.uid,m.groupid,m.userstatus FROM $pw_posts p LEFT JOIN pw_members m ON p.authorid=m.uid WHERE tid=".pwEscape($tid)."AND ifcheck='1' ORDER BY postdate DESC LIMIT 0,$db_showreplynum");

		while ($rt = $db->fetch_array($query)) {
			$tpc_author = ($rt['anonymous'] && !$pwViewHide && $windid != $rt['author']) ? $db_anonymousname : $rt['author'];
			if ($rt['ifshield']) {
				$groupid != '3' && $rt['content'] = shield($rt['ifshield'] == '1' ? 'shield_article' : 'shield_del_article');
			} elseif ($groupid != 3 && $db_shield && isban($rt,$fid)) {
				$rt['content'] = shield('ban_article');
			} else {
				$rt['ifwordsfb'] != $db_wordsfb && $rt['content'] = wordsfb($rt['content']);
				$rt['ifconvert'] == 2 && $rt['content'] = convert($rt['content'],$db_windpost);
				if (strpos($rt['content'],'[p:') !== false || strpos($rt['content'],'[s:') !== false) {
					$rt['content'] = showface($rt['content']);
				}
			}
			$post_reply .= "<table align=center width=70% cellspacing=1 cellpadding=2 style='TABLE-LAYOUT: fixed;WORD-WRAP: break-word'><tr><td width=100%>$tpc_author:$rt[subject]<br /><br />$rt[content]</td></tr></table><hr size=1 color=$tablecolor width=80%>";
		}
	}
	if ($winduid && $tpcarray['special'] == 5) {
		$debatestand = $db->get_value("SELECT standpoint FROM pw_debatedata WHERE pid='0' AND tid=".pwEscape($tid)."AND authorid=".pwEscape($winduid));
		$debatestand = (int)$debatestand;
		${'debate_'.$debatestand} = 'SELECTED';
	}
	/**
	* 索引设计时为了减少空间,回复的主题可能为空,所以默认为回复主题!
	*/
	$msg_guide = headguide($guidename);
	require_once PrintEot('post');footer();

} elseif ($_POST['step'] == 2) {

	InitGP(array('atc_anonymous','atc_hideatt'),'P');
	InitGP(array('atc_iconid'),'P',2);
	list($atc_title,$atc_content,$ifconvert,$ifwordsfb)=check_data('reply');
	/*
	*下句主要是为了节省数据的重复,可以用智能判断
	*/
	stripslashes($atc_title)=='Re:'.$replytitle && $atc_title='';

	$db_tcheck && $winddb['postcheck'] == tcheck($atc_content) && Showmsg('content_same'); //内容验证

	//!$atc_usesign && $atc_usesign=0;
	require_once(R_P.'require/postupload.php');
	$sqladd = $attachs ? array('ifupload'=>$ifupload) : array();
	$sqladd1 = '';

	if ($foruminfo['f_check']>1 && $_G['atccheck'] && !$admincheck && $groupid!=3) {
		$ifcheck = 0;
	} else {
		$ifcheck = alarm($atc_title,$atc_content);
	}
	$atc_content=trim($atc_content);
	$anonymous	= (($isGM || $forumset['anonymous'] && $_G['anonymous']) && $atc_anonymous) ? 1 : 0;
	$ifhide		= (($isGM || $foruminfo['allowhide'] && $_G['allowhidden']) && $atc_hideatt) ? 1 : 0;

	bbspostguide('Reply');

	if ($db_plist) {
		$db->update("INSERT INTO pw_pidtmp(pid) VALUES('')");
		$pid = $db->insert_id();
	} else {
		$pid = '';
	}
	$pwSQL = pwSqlSingle(array(
		'pid'		=> $pid,		'fid'		=> $fid,
		'tid'		=> $tid,		'aid'		=> $attachs,
		'author'	=> $windid,		'authorid'	=> $winddb['uid'],
		'icon'		=> $atc_iconid,	'postdate'	=> $timestamp,
		'subject'	=> $atc_title,	'userip'	=> $onlineip,
		'ifsign'	=> $atc_usesign,'ipfrom'	=> $ipfrom,
		'ifconvert'	=> $ifconvert,	'ifwordsfb'	=> $ifwordsfb,
		'ifcheck'	=> $ifcheck,	'content'	=> $atc_content,
		'anonymous'	=> $anonymous,	'ifhide'	=> $ifhide
	));
	$db->update("INSERT INTO $pw_posts SET $pwSQL");
	!$pid && $pid = $db->insert_id();
	if ($ifcheck == 1) {
		if ($windid <> $tpcarray['author']) {
			if ($db_replysendmail == 1 && ($tpcarray['ifmail'] == 1 || $tpcarray['ifmail'] == 3)) {
				$receiver  = $tpcarray['author'];
				$old_title = $read['subject'];
				$detail = $db->get_one("SELECT email,userstatus FROM pw_members WHERE uid=".pwEscape($tpcarray['authorid']));
				$send_address = $detail['email'];
				if (getstatus($detail['userstatus'],8)) {
					require_once(R_P.'require/sendemail.php');
					if (sendemail($send_address,'email_reply_subject','email_reply_content','email_additional') === true) {
						$sqladd1 = ",ifmail=ifmail-1";
					}
				}
			}
			if ($db_replysitemail && ($tpcarray['ifmail'] == 2 || $tpcarray['ifmail'] == 3)) {
				$rt = $db->get_one("SELECT mb.replyinfo,m.userstatus FROM pw_memberinfo mb LEFT JOIN pw_members m USING(uid) WHERE mb.uid=".pwEscape($tpcarray['authorid']));
				if (empty($rt)) {
					$db->update("INSERT INTO pw_memberinfo SET ".pwSqlSingle(array('uid'=>$tpcarray['authorid'],'replyinfo'=>$tid)));
				} elseif (strpos($rt['replyinfo'],",$tid,") === false) {
					$replyinfo = $rt['replyinfo'] ? $rt['replyinfo'].$tid.',' : ",$tid,";
					$db->update("UPDATE pw_memberinfo SET replyinfo=".pwEscape($replyinfo)." WHERE uid=".pwEscape($tpcarray['authorid']));
				}
				if (!getstatus($rt['userstatus'],6)) {
					$db->update("UPDATE pw_members SET userstatus=userstatus|(1<<5) WHERE uid=".pwEscape($tpcarray['authorid']));
				}
				$sqladd['ifmail'] = 4;
			}
		}
		$atc_author = $anonymous ? $db_anonymousname : $windid;
		$tpcarray['locked']<3 && $tpcarray['lastpost']<$timestamp && $sqladd['lastpost'] = $timestamp;
		$sqladd['lastposter'] = $atc_author;
		$db->update("UPDATE pw_threads SET replies=replies+1 $sqladd1,hits=hits+1,".pwSqlSingle($sqladd)." WHERE tid=".pwEscape($tid));
	}
	if ($aids) {
		$db->update("UPDATE pw_attachs SET ".pwSqlSingle(array('tid'=>$tid,'pid'=>$pid))."WHERE aid IN($aids)");
	}
	if ($winduid && $tpcarray['special'] == 5) {
		require_once(R_P.'require/postdebate.php');
	}
	$j_p = '';
	if ($ifcheck == 1) {
		if ($foruminfo['allowhtm'] && !$foruminfo['cms']) {
			include_once(R_P.'require/template.php');
		}
		if ($tpcarray['locked'] < 3) {
			lastinfo($fid,$foruminfo['allowhtm'],'reply',$foruminfo['cms'].'B');
		}
		//Start Here pwcache
		if ($db_ifpwcache&270) {
			require_once(R_P.'require/elementupdate.class.php');
			$elementupdate = new ElementUpdate($fid);
			$elementupdate->special = $tpcarray['special'];
			if ($db_ifpwcache&14) {
				$elementupdate->replySortUpdate($tid,$fid,$tpcarray['postdate'],$tpcarray['replies']+1);
			}
			if ($db_ifpwcache&256) {
				$elementupdate->newReplyUpdate($tid,$fid,$tpcarray['postdate']);
			}
			$elementupdate->updateSQL();
		}
		//End Here
		if (empty($j_p)) $j_p="read.php?tid=$tid&page=e&#a";
		refreshto($j_p,'enter_thread');
	} else {
		refreshto("thread.php?fid=$fid",'post_check');
	}
}
?>