<?php
!function_exists('readover') && exit('Forbidden');

##主题分类
$t_typedb = array();
$t_per = 0;$t_exits = 0;$article=0;
$t_db  = $foruminfo['t_type'];
if ($t_db) {
	$t_typedb = explode("\t",$t_db);
	$t_typedb = array_unique ($t_typedb);
	$t_per = $t_typedb[0];unset($t_typedb[0]);
	foreach ($t_typedb as $value) {
		if ($value) $t_exits = 1;
	}
}
$db_forcetype = $t_exits && $t_per=='2' && !$admincheck ? 1 : 0; // 是否需要强制主题分类

if ($foruminfo['allowpost'] && !$admincheck && !allowcheck($foruminfo['allowpost'],$groupid,$winddb['groups'],$fid,$winddb['post'])) {
	Showmsg('postnew_forum_right');
}
if ($special == '1' && !$foruminfo['allowpost'] && !$admincheck && $_G['allownewvote'] == 0) {
	Showmsg('postnew_group_vote');
} elseif ($special == '2') {
	!$_G['allowactive'] && Showmsg('postnew_group_active');
	$sel_0 = 'checked';$sel_1 = $sel_2 = '';
} elseif ($special == '3') {
	!$_G['allowreward'] && Showmsg('postnew_group_reward');
} elseif ($special == '4') {
	!$_G['allowgoods'] && Showmsg('postnew_group_goods');
} elseif ($special == '5') {
	!$_G['allowdebate'] && Showmsg('postnew_group_debate');
}
if (!$foruminfo['allowpost'] && !$admincheck && $_G['allowpost'] == 0) {
	Showmsg('postnew_group_right');
}
$icon = (int)$icon;

if (empty($_POST['step'])) {

	if ($special == 3) {
		$cbselect = '';
		list(,$rw_b_val,$rw_a_val,$rw_credit) = explode("\t",$forumset['rewarddb']);
		foreach (explode(',',$rw_credit) as $key => $val) {
			$cbselect .= "<option value=\"$val\">".(is_numeric($val) ? $_CREDITDB[$val][0] : ${'db_'.$val.'name'})."</option>";
		}
		$caselect = $cbselect;
	} elseif ($special == 4) {
		$trade = array('num' => 1, 'transport' => 1, 'type' => 0);
		$tinfo = $db->get_value("SELECT tradeinfo FROM pw_memberinfo WHERE uid=".pwEscape($winduid));
		$paym_1 = $paym_2 = $paym_4 = '';
		if (is_array($tinfo = unserialize($tinfo))) {
			$tinfo['alipay'] && $paym_2 = 'checked';
			$tinfo['tradetype'] && $trade['tradetype'] = $tinfo['tradetype'];
		}
		unset($tinfo);
		$tspt_1 = 'checked';
	}

	list($guidename,$forumtitle) = getforumtitle(forumindex($foruminfo['fup'],1));
	$db_metakeyword = str_replace(array('|',' - '),',',$forumtitle).'phpwind';

	require_once(R_P.'require/header.php');
	list($msg_guide,$forumlist) = headguide($guidename);

	require_once PrintEot('post');footer();

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

	##主题分类
	//强制分类
	if (!$p_type || empty($t_typedb[$p_type]) || ($t_per==0 && !$admincheck)) {
		$w_type = 0;
	} else {
		$w_type = $p_type;
	}
	$db_forcetype && $w_type=='0' && Showmsg('force_tid_select');
	InitGP(array('atc_anonymous','atc_newrp','atc_tags','atc_hideatt','magicid','magicname'),'P');
	InitGP(array('atc_iconid','atc_email','digest'),'P',2);
	$atc_tags = ($db_iftag && $atc_tags) ? check_tag($atc_tags) : '';
	list($atc_title,$atc_content,$ifconvert,$ifwordsfb) = check_data($action);
	$db_tcheck && $winddb['postcheck'] == tcheck($atc_content) && Showmsg('content_same'); //内容验证

	if ($special == '1') {

		!$_POST['vt_select'] && Showmsg('postfunc_noempty');
		InitGP(array('vt_select'),'P');
		InitGP(array('multiplevote','timelimit','modifiable','mostvotes','previewable'),'P',2);
		$votearray = array();
		$vt_select = explode("\n",$vt_select);
		foreach ($vt_select as $voteoption) {
			$voteoption = trim($voteoption);
			if ($voteoption) {
				$votearray[] = array(stripslashes($voteoption),0);
			}
		}
		$vtcount = count($votearray);
		if ($vtcount > $db_selcount) {
			Showmsg('vote_num_limit');
		}
		if (empty($multiplevote)) {
			$mostvotes = 1;
		} elseif (!is_numeric($mostvotes) || $mostvotes > $vtcount || $mostvotes < 1) {
			$mostvotes = $vtcount;
		}
		$timelimit < 0 && $timelimit = 0;
		$voteopts = addslashes(serialize($votearray));

	} elseif ($special == '2') {

		InitGP(array('act_subject','act_location','act_starttime','act_deadline','act_endtime','act_sex'),'P');
		InitGP(array('act_num','act_costs'),'P',2);
		!($act_subject && $act_starttime && $act_deadline) && Showmsg('active_data_empty');
		$act_starttime= PwStrtoTime($act_starttime);
		$act_endtime  = PwStrtoTime($act_endtime);
		$act_deadline = PwStrtoTime($act_deadline);
		$act_starttime < $timestamp && Showmsg('starttime_limit');
		$act_deadline < $timestamp && Showmsg('deadline_limit');
		$act_endtime && $act_deadline > $act_endtime && Showmsg('deadline_endtime_limit');
		$act_endtime && $act_starttime > $act_endtime && Showmsg('endtime_limit');
		$act_num<1 && $act_num=0;
		$act_costs<1 && $act_costs=0;

	} elseif ($special == '3') {

		require_once(R_P.'require/postreward.php');

	} elseif ($special == '4') {

		InitGP(array('goodsname','price','costprice','locus','mailfee','expressfee','emsfee'));
		InitGP(array('degree','ptype','goodsnum','paymethod','transport'), 2);
		!$goodsname && $goodsname = $atc_title;
		if (!is_numeric($costprice) || $costprice <= 0) {
			Showmsg('goods_setprice');
		}
		$goodsnum < 1 && Showmsg('goods_num_error');
		$paymethod && $paymethod = array_sum($paymethod);
		$paymethod < 1 && Showmsg('goods_pay_error');
		!is_numeric($price) && $price = 0;

		if ($transport) {
			!is_numeric($mailfee) && $mailfee = 0;
			!is_numeric($expressfee) && $expressfee = 0;
			!is_numeric($emsfee) && $emsfee = 0;
			if (!$mailfee && !$expressfee && !$emsfee) {
				Showmsg('goods_logistics');
			}
		} else {
			$mailfee = $expressfee = $emsfee = 0;
		}
		$goodsicon = '';

	} elseif ($special == '5') {
		require_once(R_P.'require/postdebate.php');
	} else {
		$special = 0;
	}
	$magic = '';
	require_once(R_P.'require/postupload.php');

	if ($digest && !pwRights($isBM,'typeadmin')) {
		$digest = 0;
	}
	if (($foruminfo['f_check'] == 1 || $foruminfo['f_check'] == 3) && $_G['atccheck'] && !$admincheck && $groupid != 3) {
		$ifcheck = 0;
	} else {
		$ifcheck = alarm($atc_title,$atc_content);
	}
	$anonymous  = (($isGM || $forumset['anonymous'] && $_G['anonymous']) && $atc_anonymous) ? 1 : 0;
	$ifhide		= (($isGM || $foruminfo['allowhide'] && $_G['allowhidden']) && $atc_hideatt) ? 1 : 0;
	$lastposter = $anonymous ? $db_anonymousname : $windid;
	$atc_email  = $db_replysendmail ? $atc_email : 0;
	$db_replysitemail && $atc_newrp && $atc_email += 2;

	$ifmagic	= 0;
	if ($db_windmagic && $magicid) {
		$ifmagic	= 1;
		$magic		= $magicid."\t".$magicname;
	}
	$t_date = $timestamp;//主题发表时间 bbspostguide 中用到
	bbspostguide();
	$pwSQL = pwSqlSingle(array(
		'fid'		=> $fid,		'icon'		=> $atc_iconid,
		'author'	=> $windid,		'authorid'	=> $winddb['uid'],
		'subject'	=> $atc_title,	'ifcheck'	=> $ifcheck,
		'type'		=> $w_type,		'postdate'	=> $timestamp,
		'lastpost'	=> $timestamp,	'lastposter'=> $lastposter,
		'hits'		=> 1,			'replies'	=> 0,
		'topped'	=> 0,			'digest'	=> $digest,
		'special '	=> $special,	'state'		=> 0,
		'ifupload'	=> $ifupload,	'ifmail'	=> $atc_email,
		'anonymous'	=> $anonymous,	'ptable'	=> $db_ptable,
		'ifmagic'	=> $ifmagic,	'ifhide'	=> $ifhide
	));
	$db->update("INSERT INTO pw_threads SET $pwSQL");
	$tid = $db->insert_id();
	$pw_tmsgs = GetTtable($tid);
	if ($db_iftag) {
		if ($atc_tags) {
			insert_tag($tid,$atc_tags);
		}
		$atc_tags .= "\t".relate_tag($atc_title,$atc_content);
	}
	$pwSQL = pwSqlSingle(array(
		'tid'		=> $tid,
		'aid'		=> $attachs,
		'userip'	=> $onlineip,
		'ifsign'	=> $atc_usesign,
		'buy'		=> '',
		'ipfrom'	=> $ipfrom,
		'tags'		=> $atc_tags,
		'ifconvert'	=> $ifconvert,
		'ifwordsfb'	=> $ifwordsfb,
		'content'	=> $atc_content,
		'magic'		=> $magic
	));
	$db->update("INSERT INTO $pw_tmsgs SET $pwSQL");
	$digest && $db->update("UPDATE pw_memberdata SET digests=digests+1 WHERE uid=".pwEscape($winduid));
	if ($aids) {
		$db->update("UPDATE pw_attachs SET tid=".pwEscape($tid)." WHERE aid IN($aids)");
	}
	switch ($special) {
		case 1:
			$db->update("INSERT INTO pw_polls SET " . pwSqlSingle(array(
				'tid'			=> $tid,			'voteopts'		=> $voteopts,
				'modifiable'	=> $modifiable,		'previewable'	=> $previewable,
				'multiple'		=> $multiplevote,	'mostvotes'		=> $mostvotes,
				'timelimit'		=> $timelimit
			)));
			break;
		case 2:
			$db->update("INSERT INTO pw_activity SET" . pwSqlSingle(array(
				'tid'		=> $tid,			'subject'	=> $act_subject,
				'admin'		=> $winduid,		'starttime'	=> $act_starttime,
				'endtime'	=> $act_endtime,	'location'	=> $act_location,
				'num'		=> $act_num,		'sexneed'	=> $act_sex,
				'costs'		=> $act_costs,		'deadline'	=> $act_deadline
			)));
			break;
		case 3:
			$db->update("INSERT INTO pw_reward SET" . pwSqlSingle(array(
				'tid'		=> $tid,				'cbtype'	=> $ctype['best'],
				'catype'	=> $ctype['active'],	'cbval'		=> $bonus['best'],
				'caval'		=> $bonus['active'],	'timelimit'	=> $timelimit
			)));
			break;
		case 4:
			$db->update("INSERT INTO pw_trade SET" . pwSqlSingle(array(
				'tid'		=> $tid,			'uid'		=> $winduid,
				'name'		=> $goodsname,		'icon'		=> $goodsicon,
				'degree'	=> $degree,			'type'		=> $ptype,
				'num'		=> $goodsnum,		'price'		=> $price,
				'costprice'	=> $costprice,		'locus'		=> $locus,
				'paymethod'	=> $paymethod,		'transport'	=> $transport,
				'mailfee'	=> $mailfee,		'expressfee'=> $expressfee,
				'emsfee'	=> $emsfee
			)));
			break;
		case 5:
			$db->update("INSERT INTO pw_debates SET" . pwSqlSingle(array(
				'tid'		=> $tid,			'authorid'	=> $winduid,
				'postdate'	=> $timestamp,		'obtitle'	=> $obtitle,
				'retitle'	=> $retitle,		'endtime'	=> $endtime,
				'umpire'	=> $umpire,			'judge'		=> 0
			)));
			break;
	}

	unset($j_p);

	if ($ifcheck == 1) {
		if ($foruminfo['allowhtm'] && !$foruminfo['cms']) {
			include_once(R_P.'require/template.php');
		}
		lastinfo($fid,$foruminfo['allowhtm'],'new',$foruminfo['cms'].'B');

		if (!$foruminfo['allowvisit'] && $foruminfo['f_type'] != 'hidden' && !$foruminfo['password'] && !$foruminfo['forumsell']) {
			pwAddFeed($winduid,'post',array('subject' => $atc_title, 'tid' => $tid, 'fid' => $fid));
		}
		//Start elementupdate
		if ($db_ifpwcache & 128 || (($db_ifpwcache & 512) && $elementpic)) {
			require_once(R_P.'require/elementupdate.class.php');
			$elementupdate = new ElementUpdate($fid);
			if ($db_ifpwcache & 128) {
				$elementupdate->newSubjectUpdate($tid,$fid,$timestamp,$special);
			}
			if (($db_ifpwcache & 512) && $elementpic) {
				$elementupdate->newPicUpdate($elementpic['aid'],$fid,$tid,$elementpic['attachurl'],$elementpic['ifthumb']);
			}
			$elementupdate->updateSQL();
		}
		//End elementupdate
	}
	if (empty($j_p) || $foruminfo['cms']) $j_p = "read.php?tid=$tid";
	refreshto($j_p,$ifcheck ? 'enter_thread' : 'post_check');
}
?>