<?php
session_start();
require($_SERVER['DOCUMENT_ROOT']."/global.php");
InitGP(array('action','d_id','type','title','d_o','pub_c','title','content','img','discuss_id',));
$tpl_path .= '/discuss';


if ($action) {
	$_SESSION['pub_c'] !== $pub_c && Showmsg('submit_invalid');
	$timestamp-$_SESSION['pub_chk_times'] < 5  &&  Showmsg('repeat');
	if ($img == 'http://' || strlen($img)) $img = '';
	$user_id = GetCookie('user_id');
	$user_name_2 = GetCookie('user_name_2');
	if (!$user_id || !$user_name_2) Showmsg('log');
}

if ($action == 'pub') {
	if (empty($pub_c) || empty($d_id) || empty($title) || empty($content)) Showmsg('empty');

	$sql = "SELECT discuss_pub FROM ".$db_tablepre."discuss WHERE discuss_id=".$d_id." AND (discuss_pub=1 or discuss_presentation LIKE '%".$user_name_2."%' )";
	$result = sql_return($sql,3) || Showmsg('discuss_pub_err_close');
	$sql = "INSERT INTO ".$db_tablepre."discuss_index (d_i_title,d_i_uid,d_i_u_name_2,d_i_lastReply_uid,d_i_r_u_name_2,d_i_last_uptimes,d_i_discuss_id)  VALUES ('".$title."',".$user_id.", '".$user_name_2."', ".$user_id.", '".$user_name_2."','".$timestamp."',".$d_id.")";
	$query = sql_return($sql);
	$sql = "INSERT INTO ".$db_tablepre."discuss_main  (d_m_uid,d_m_u_name_2,d_m_title,d_m_content,d_m_img,d_m_uptimes,d_m_d_i_id)  VALUES (".$user_id.", '".$user_name_2."','".$title."', '".$content."', '".$img."','".$timestamp."',".sql_return($query,'Insert_ID').")";
	$sql_num ="UPDATE ".$db_tablepre."discuss SET discuss_pub_num=discuss_pub_num+1, discuss_count_num=discuss_count_num+10 WHERE discuss_id=".$d_id;
	$query && sql_return($sql) && sql_return($sql_num) && Showmsg('post_Success');
	//var_dump($result);
}
elseif ($action == 'rep'){
	if (empty($pub_c) || empty($d_id) || empty($d_o) || empty($discuss_id) || empty($title) || empty($content)) Showmsg('empty');

	$sql = "INSERT INTO ".$db_tablepre."discuss_main  (d_m_uid,d_m_u_name_2,d_m_title,d_m_content,d_m_img,d_m_uptimes,d_m_order,d_m_d_i_id )  VALUES (".$user_id.", '".$user_name_2."','".$title."', '".$content."', '".$img."','".$timestamp."',".++$d_o.",".$d_id.")";
	$sql_num ="UPDATE ".$db_tablepre."discuss SET discuss_pub_num=discuss_reply_num+1, discuss_count_num=discuss_count_num+5 WHERE discuss_id=".$discuss_id;
	$sql_reply_num ="UPDATE ".$db_tablepre."discuss_index SET d_i_reply=d_i_reply+1 WHERE d_i_id=".$d_id;
	sql_return($sql) && sql_return($sql_num) && sql_return($sql_reply_num) && Showmsg('post_Success');
}
else {
	$_SESSION['pub_c'] = $pub_c = num_rand(18);
	$_SESSION['pub_chk_times'] = $timestamp;
	if ($type == 'rep') $title =  '回复:'.urldecode($title);
}


require PrintEot($self_template);
?>