<?php
define('THIS_SCRIPT', 'postdata');
include_once 'global.php';
require_once CACHE_PATH.'forum_setting.php';
$lang = load_lang($lang, 'postdata' );
checklogin();
header("content-Type: text/html; charset=".$charset);
head();
if ( strpos($sysinfo['flag'],'24,') === false ){
	show_msg($lang['readme_tit'],$lang['error.str1'],0,'','','','');
	exit;
}
PageLoad();
footer();
function Pageload(){
	global $dv,$db,$lang,$tablelist,$tablename,$action,$target_tablename,$reaction,$first_tablename,$k_condition,$select_type,$url,$searchwhat,$userforum_list,$page_size,$page,$page_count,$offset,$total,$keyword,$all_check,$p,$topicid,$select_currently,$select_all,$default_table,$_DCACHE,$fsetting;
	$url="javascript:void(window.history.back(-1))";
	$default_table=deft_table();
	switch ($action){
		case "nowused":
			tableinfo_list();
		break;
		case "del":
			del_table();
		break;
		case "update":
			update_table();
		break;
		case "createtable":
		create_table();
		break;
		case "search":
			search_forum();
		break;
		case "dataswap":
			forum_swap();
		break;
		default:
			$default_table=deft_table();
		break;
	}
	if($reaction=="searchswap"){
		search_swap();
	}
include printout('postdata');
}
function tableinfo_list(){
	global $dv,$db,$lang,$tablelist;
	$tablelist=array();
	$stmt=$db->query("select id,tablename,tabletype from {$dv}tablelist");
	while($arr=$db->fetch_array($stmt)){
		$tablelist[]=$arr;
	}
$db->free_result($stmt);
}
function deft_table(){
	global $dv,$db,$lang,$def_table;
	$d_sql=$db->scalar("select forum_nowusebbs from {$dv}setup");
	return ($d_sql);
}
function del_table(){
	global $dv,$db,$lang,$tablename,$url,$default_table;
	$default_table=deft_table();//如果是默认的表就不能删除;
	if(strcmp($tablename,$default_table)===0){
		show_msg($lang['sys.info'],$lang['error.info1'],0,'','',$url,$lang['cmdback']);
	exit;
	}
	else{
		$deltable_record=$db->query("delete from {$dv}tablelist where tablename='".$tablename."'");
		$deltable_name=$db->query("drop table ".$tablename."");
		$topicid_result=$db->query("select topicid from {$dv}topic where posttable='".$tablename."'");
		while ($topicidlist=$db->fetch_array($topicid_result)){
			$deldv_besttopic=$db->query("delete from {$dv}besttopic where rootid='".$topicidlist['topicid']."'");
		}
		$deldv_topic=$db->query("delete from {$dv}topic where posttable='".$tablename."'");
	}
	if($deltable_record){
		show_msg($lang['sys.info'],$lang['error.info2'],1,'','',$url,$lang['cmdback']);
	exit;
	}
	else{
		show_msg($lang['sys.info'],$lang['error.info3'],0,'','',$url,$lang['cmdback']);
	exit;
	}
$db->free_result($topicid_result);
}
function update_table(){
	global $dv,$db,$lang,$tablename,$url;
	$tablename=$_POST['d_table'];
	$is=get_magic_quotes_runtime();
	if( $is == 0 ){
		set_magic_quotes_runtime(1);
		$is_1 = 1;
	}
	$updateresult=$db->query("update {$dv}setup set forum_nowusebbs='".$tablename."'");
	require_once INC_PATH.'BoardCache.class.php';
	$mycache =& new BoardCache($db);
	$mycache->createForumSetting();
	if($updateresult){
		show_msg($lang['sys.info'],$lang['error.info4'],1,'','',$url,$lang['cmdback']);
	exit;
	}
	else{
		show_msg($lang['sys.info'],$lang['error.info5'],0,'','',$url,$lang['cmdback']);
	exit;
	}
	if( $is_1 == 1 ) set_magic_quotes_runtime($is);
}
function create_table(){
	global $dv,$db,$lang,$table_name,$table_stu,$url,$_DCACHE;
	$table_name=$_POST['table_name'];
	$table_stu=$_POST['table_stu'];
	$nowtablename=$_DCACHE['settings']['nowtablename'];
	if($table_name==""){
		show_msg($lang['sys.info'],$lang['error.info6'],0,'','',$url,$lang['cmdback']);
	exit;
	}
	elseif(strlen($table_name)!=7){
		show_msg($lang['sys.info'],$lang['error.info7'],0,'','',$url,$lang['cmdback']);
	exit;
	}
	elseif(!is_numeric(substr($table_name,-1))){
		show_msg($lang['sys.info'],$lang['error.info7'],0,'','',$url,$lang['cmdback']);
	exit;
	}
	elseif($table_stu==""){
		show_msg($lang['sys.info'],$lang['error.info8'],0,'','',$url,$lang['cmdback']);
	exit;
	}
	else{
		$table_count=$db->scalar("select COUNT(1) from {$dv}tablelist where tablename='".$table_name."'");
		if($table_count>=1){
			show_msg($lang['sys.info'],$lang['error.info9'],0,'','',$url,$lang['cmdback']);
		exit;
		}
		else{
			$nowtablename_fielt=$db->query("show create table ".$nowtablename."");
			$arr=$db->fetch_array($nowtablename_fielt);
			$str_createtable=str_replace($nowtablename,$table_name,$arr['Create Table']);
			if ($db->query(preg_replace('#create\s+table\s+#i', 'create table if not exists ', $str_createtable))){
				if($insert_info=$db->query("insert into {$dv}tablelist set tablename='".$table_name."',tabletype='".$table_stu."'")){
					show_msg($lang['sys.info'],$lang['error.info10'],1,'','',$url,$lang['cmdback']);
				exit;				
				}
			}
				show_msg($lang['sys.info'],$lang['error.info11'],0,'','',$url,$lang['cmdback']);
			exit;
		}
	}
}
function search_forum(){
	global $dv,$lang,$db,$searchwhat,$keyword,$tablename,$url,$userforum_list,$page_size,$page,$page_count,$offset,$total,$p;
	$userforum_list=array();
	$searchwhat=$_POST['searchwhat'];
	$keyword=str_replace(" ","",$_POST['keyword']);
	$tablename=$_POST['tablename'];
	if($keyword==''){
		$keyword=$_GET['keyword'];
	}
	if($tablename==''){
		$tablename=$_GET['tablename'];
	}
	$keyword=str_replace(" ","",$keyword);
	$is=get_magic_quotes_runtime();
	if( $is == 0 ){
		set_magic_quotes_runtime(1);
		$is_1 = 1;
	}
	if($keyword==''){
		show_msg($lang['sys.info'],$lang['search.info1'],0,'','',$url,$lang['cmdback']);
	exit;
	}
	if($searchwhat=="username"){
		$username_count=$db->scalar("select count(1) from {$dv}user where username='".$keyword."'");
		if($username_count<=0){
			show_msg($lang['sys.info'],$lang['search.info16'],0,'','',$url,$lang['cmdback']);
		exit;
		}
		else {
			$username_result=$db->query("select userid from {$dv}user where username='".$keyword."'");
			$arruserlist=$db->fetch_array($username_result);
			$total=$db->scalar("select count(1) from {$dv}topic where posttable='".$tablename."' and postuserid='".$arruserlist['userid']."'");
			if($total<=0){
				show_msg($lang['sys.info'],$lang['search.info17'],0,'','',$url,$lang['cmdback']);
			exit;
			}
			else{
				page_info(25);
				$stmt="select topicid,title,boardid,pollid,locktopic,child,postusername,postuserid,dateandtime,hits,expression,votetotal,lastpost,lastposttime,istop,isvote,isbest,posttable,smsuserlist,issmstopic,lastsmstime,topicmode,mode,getmoney,usetools,getmoneytype,hidename from {$dv}topic where posttable='".$tablename."' and postuserid='".$arruserlist['userid']."' order by lastposttime desc";
				if($userforum_result=$db->query($stmt,array('absolutePage'=>$offset,'pageSize'=>$page_size))){
					while ($arr=$db->fetch_array($userforum_result)){
						$userforum_list[]=$arr;
					}
				}
			}
		}
	}
	else{
		$total=$db->scalar("select count(1) from {$dv}topic where posttable='".$tablename."' and title like '%".$keyword."%'");
		if($total<=0){
			show_msg($lang['sys.info'],$lang['search.info17'],0,'','',$url,$lang['cmdback']);
		exit;
		}
		else{
			page_info(25);
			$stmt="select topicid,title,boardid,pollid,locktopic,child,postusername,postuserid,dateandtime,hits,expression,votetotal,lastpost,lastposttime,istop,isvote,isbest,posttable,smsuserlist,issmstopic,lastsmstime,topicmode,mode,getmoney,usetools,getmoneytype,hidename from {$dv}topic where posttable='".$tablename."' and title like '%$keyword%' order by lastposttime desc";
			if($topic_result=$db->query($stmt,array('absolutePage'=>$offset,'pageSize'=>$page_size))){
				while ($arr=$db->fetch_array($topic_result)){
					$userforum_list[]=$arr;
				}
			}
		}
	}
	if( $is_1 == 1 ) set_magic_quotes_runtime($is);
}
function search_swap(){//搜索一，帖子搜索出来后再转移;
	global $dv,$db,$lang,$first_tablename,$target_tablename,$keyword,$url,$topicid,$select_currently,$select_all,$k_condition,$select_type;
	$topicid_num=count($topicid);
	global $searchwhat,$keyword,$tablename,$url,$userforum_list,$page_size,$page,$page_count,$offset,$total,$p;
	$j=0;
	$tablename=$_GET['first_tablename'];
	$select_currently=$_POST['select_currently'];
	$target_tablename=$_POST['target_tablename'];
	$select_all=$_POST['select_all'];
	$quote_keyword=str_replace(" ","",$_GET['k_condition']);
	$quote_type=$_GET['select_type'];
	$is=get_magic_quotes_runtime();
	if( $is == 0 ){
		set_magic_quotes_runtime(1);
		$is_1 = 1;
	}
	if($tablename==$target_tablename){
		show_msg($lang['sys.info'],$lang['search.info18'],0,'','',$url,$lang['cmdback']);
	exit;
	}
	elseif($select_all=="" and $select_currently=="" and $topicid_num<=0){
		show_msg($lang['sys.info'],$lang['search.info19'],0,'','',$url,$lang['cmdback']);
	exit;
	}
	else{
		// 转选取的帖子
		if($topicid_num!=0 and $select_all=="" and $select_currently=="" or $topicid_num!=0 and $select_all=="" and $select_currently=="curr_all"){
			for($i=0;$i<$topicid_num;$i++){
				$sql=$db->query("select topicid,title from {$dv}topic where posttable='".$tablename."' and topicid='".$topicid[$i]."'");
				$rs=$db->fetch_array($sql);
				$topicid_select=$db->query("select announceid,parentid,boardid,username,postuserid,topic,body,dateandtime,length,rootid,layer,orders,isbest,ip,expression,locktopic,signflag,emailflag,isagree,isaudit,isupload,postbuyuser,ubblist,getmoney,usetools,getmoneytype from ".$tablename." where rootid='".$topicid[$i]."'");
				$arr=$db->fetch_array($topicid_select);//开始插入记录;
				$insert_topicid=$db->query("insert into ".$target_tablename." (boardid,parentid,username,announceid,topic,body,dateandtime,length,rootid,layer,orders,ip,expression,locktopic,signflag,emailflag,isbest,postuserid,isagree,isupload,isaudit,postbuyuser,ubblist) values ('".$arr['boardid']."','".$arr['parentid']."','".$arr['username']."','".$arr['announceid']."','".$arr['topic']."','".$arr['body']."','".$arr['dateandtime']."','".$arr['length']."','".$arr['rootid']."','".$arr['layer']."','".$arr['orders']."','".$arr['ip']."','".$arr['expression']."','".$arr['locktopic']."','".$arr['signflag']."','".$arr['emailflag']."','".$arr['isbest']."','".$arr['postuserid']."','".$arr['isagree']."','".$arr['isupload']."','".$arr['isaudit']."','".$arr['postbuyuser']."','".$arr['ubblist']."')");
				if(!empty($arr['isupload']) or !empty($arr['isbest'])){
					if(intval($arr['isupload'])==1 or $arr['isbest']){
						$maxid=$db->query("select max(announceid) from ".$target_tablename." where boardid='".$arr['boardid']."'");
						$maxid_list=$db->fetch_array($maxid);
						if(intval($arr['isbest'])==1){
							$isbest_update=$db->query("update {$dv}besttopic set announceid='".$maxid_list['max(announceid)']."' where rootid='".$rs['topicid']."'");
						}
						if(intval($arr['isupload'])==1){
							$isupload_update=$db->query("update {$dv}upfile set f_announceid='".$rs['topicid']."|".$maxid_list['max(announceid)']."' where f_announceid='".$rs['topicid']."|".$arr['announceid']."'");
						}
					}
				}
				$del_sourceforum=$db->query("delete from ".$tablename." where rootid='".$rs['topicid']."'");//删除原表该帖子数据;
				$update_topic=$db->query("update {$dv}topic set posttable='".$target_tablename."' where topicid='".$rs['topicid']."'");//更新该主题表名;
			}
			$record_num=$topicid_num;//插入的所选记录数;
		}
		// 转全部查询到的帖子
		elseif($select_all=="Y" and $topicid_num<=0 and $select_currently=="" or $select_all=="Y" and $topicid_num!=0 and $select_currently=="curr_all"){
			if($quote_type=="topic"){
				$stmt_search=$db->query("select a.announceid,a.parentid,a.boardid,a.username,a.postuserid,a.topic,a.body,a.dateandtime,a.length,a.rootid,a.layer,a.orders,a.isbest,a.ip,a.expression,a.locktopic,a.signflag,a.emailflag,a.isagree,a.isaudit,a.isupload,a.postbuyuser,a.ubblist,a.getmoney,a.usetools,a.getmoneytype from ".$tablename." as a inner join {$dv}topic as b on a.rootid =b.topicid where b.posttable='".$tablename."' and b.title like '%".$quote_keyword."%' order by b.lastposttime desc");
			}
			else{
				$userid=$db->scalar("select username from {$dv}user where username='".$quote_keyword."'");
				$stmt_search=$db->query("select announceid,parentid,boardid,username,postuserid,topic,body,dateandtime,length,rootid,layer,orders,isbest,ip,expression,locktopic,signflag,emailflag,isagree,isaudit,isupload,postbuyuser,ubblist,getmoney,usetools,getmoneytype from ".$tablename." where username='".$userid."' order by announceid desc");
			}
			while ($arr_list=$db->fetch_array($stmt_search)){
				$insert_topicid=$db->query("insert into ".$target_tablename." (boardid,username,announceid,topic,body,dateandtime,length,rootid,layer,orders,ip,expression,locktopic,signflag,emailflag,isbest,postuserid,isagree,isupload,isaudit,postbuyuser,ubblist) values (".$arr_list['boardid'].",'".$arr_list['username']."',".$arr_list['announceid'].",'".$arr_list['topic']."','".$arr_list['body']."','".$arr_list['dateandtime']."',".$arr_list['length'].",".$arr_list['rootid'].",'".$arr_list['layer']."',".$arr_list['orders'].",'".$arr_list['ip']."','".$arr_list['expression']."',".$arr_list['locktopic'].",'".$arr_list['signflag']."','".$arr_list['emailflag']."','".$arr_list['isbest']."',".$arr_list['postuserid'].",'".$arr_list['isagree']."','".$arr_list['isupload']."','".$arr_list['isaudit']."','".$arr_list['postbuyuser']."','".$arr_list['ubblist']."')");
				if(!empty($arr_list['isupload']) or !empty($arr_list['isbest'])){
					if(intval($arr_list['isupload'])==1 or intval($arr_list['isbest'])==0){
						$maxid=$db->query("select max(announceid) from ".$target_tablename." where boardid='".$arr_list['boardid']."'");
						$maxid_list=$db->fetch_array($maxid);
						if(intval($arr_list['isbest'])==1){
							$isbest_update=$db->query("update {$dv}besttopic set announceid='".$maxid_list['max(announceid)']."' where rootid='".$arr_list['topicid']."'");
						}
						if(intval($arr_list['isupload'])==1){
							$isupload_update=$db->query("update {$dv}upfile set f_announceid='".$arr_list['topicid']."|".$maxid_list['max(announceid)']."' where f_announceid='".$arr_list['topicid']."|".$arr_list['announceid']."'");
						}
					}
				}
				if($quote_type=="topic"){
					$del_sourceforum=$db->query("delete from ".$tablename." where topic like '%".$quote_keyword."%'");//删除原表该帖子数据;
				}
				else{
					$del_sourceforum=$db->query("delete from ".$tablename." where username='".$quote_keyword."'");//删除原表该帖子数据;
				}
				$update_topic=$db->query("update {$dv}topic set posttable='".$target_tablename."' where title like '%".$quote_keyword."%' and posttable='".$tablename."'");//更新该主题表名;
				$j+=1;
			}
			$record_num=$j;//插入的查询记录数;
		}
	}
	if($insert_topicid){
		show_msg($lang['sys.info'],$lang['search.info20'].$tablename.$lang['search.info21'].$target_tablename.$lang['search.info23'].$record_num.$lang['search.info24'],1,'','','postdata.php',$lang['cmdback']);
	exit;
	}
	else{
		show_msg($lang['sys.info'],$lang['search.info22'],0,'','','postdata.php',$lang['cmdback']);
	exit;
	}
$db->free_result($sql);
$db->free_result($topicid_select);
$db->free_result($maxid);
$db->free_result($userid);
$db->free_result($stmt_search);
	if( $is_1 == 1 ) set_magic_quotes_runtime($is);
}
function forum_swap(){//搜索二，帖子直接转移;
	global $db,$dv,$lang,$url,$sourcetable,$recordtopend,$targettable,$forum_num;
	$recordtopend=$_POST['recordtopend'];//记录的前后;
	$sourcetable=$_POST['sourcetable'];//源表;
	$targettable=$_POST['targettable'];//目标表;
	$forum_num=intval($_POST['forum_num']);//记录数;
	$is=get_magic_quotes_runtime();
	if( $is == 0 ){
		set_magic_quotes_runtime(1);
		$is_1 = 1;
	}
	if($forum_num==0){
		show_msg($lang['sys.info'],$lang['search.info25'],0,'','','postdata.php',$lang['cmdback']);
	exit;
	}
	if($sourcetable==$targettable){
		show_msg($lang['sys.info'],$lang['search.info18'],0,'','','postdata.php',$lang['cmdback']);
	exit;
	}
	if($recordtopend=="pre"){
		$orderby=" order by topicid ";
	}
	else{
		$orderby=" order by topicid desc ";
	}
	$stmt=$db->query("select topicid,title from {$dv}topic where posttable='".$sourcetable."'".$orderby,array('pageSize'=>$forum_num));
	while ($rs=$db->fetch_array($stmt)){
		$sql_source=$db->query("select announceid,parentid,boardid,username,postuserid,topic,body,dateandtime,length,rootid,layer,orders,isbest,ip,expression,locktopic,signflag,emailflag,isagree,isaudit,isupload,postbuyuser,ubblist,getmoney,usetools,getmoneytype from ".$sourcetable." where rootid='".$rs['topicid']."' order by announceid");
		while ($arr_list=$db->fetch_array($sql_source)){
			$insert_topicid=$db->query("insert into ".$targettable." (boardid,parentid,username,announceid,topic,body,dateandtime,length,rootid,layer,orders,ip,expression,locktopic,signflag,emailflag,isbest,postuserid,isagree,isupload,isaudit,postbuyuser,ubblist) values ('".$arr_list['boardid']."','".$arr_list['parentid']."','".$arr_list['username']."','".$arr['announceid']."','".$arr_list['topic']."','".$arr_list['body']."','".$arr_list['dateandtime']."','".$arr_list['length']."','".$arr_list['rootid']."','".$arr_list['layer']."','".$arr_list['orders']."','".$arr_list['ip']."','".$arr_list['expression']."','".$arr_list['locktopic']."','".$arr_list['signflag']."','".$arr_list['emailflag']."','".$arr_list['isbest']."','".$arr_list['postuserid']."','".$arr_list['isagree']."','".$arr_list['isupload']."','".$arr_list['isaudit']."','".$arr_list['postbuyuser']."','".$arr_list['ubblist']."')");
			if(!empty($arr_list['isupload']) or !empty($arr_list['isbest'])){
				if(intval($arr_list['isupload'])==1 or $arr_list['isbest']){
					$maxid=$db->query("select max(announceid) from ".$targettable." where boardid='".$arr_list['boardid']."'");
					$maxid_list=$db->fetch_array($maxid);
					if(intval($arr_list['isbest'])==1){
						$isbest_update=$db->query("update {$dv}besttopic set announceid='".$maxid_list['max(announceid)']."' where rootid='".$arr_list['topicid']."'");
					}
					if(intval($arr_list['isupload'])==1){
						$isupload_update=$db->query("update {$dv}upfile set f_announceid='".$rs['topicid']."|".$maxid_list['max(announceid)']."' where f_announceid='".$rs['topicid']."|".$arr_list['announceid']."'");
					}
				}
			}
			$del_sourceforum=$db->query("delete from ".$sourcetable." where rootid='".$arr_list['topicid']."'");//删除原表该帖子数据;
			$update_topic=$db->query("update {$dv}topic set posttable='".$targettable."' where topicid='".$arr_list['topicid']."'");//更新该主题表名;
		}
	}
	if($insert_topicid){
		show_msg($lang['sys.info'],$lang['search.info20'].$sourcetable.$lang['search.info21'].$targettable,1,'','','postdata.php',$lang['cmdback']);
	exit;
	}
	else{
		show_msg($lang['sys.info'],$lang['search.info22'],0,'','','postdata.php',$lang['cmdback']);
	exit;
	}
$db->free_result($stmt);
$db->free_result($sql_source);
	if( $is_1 == 1 ) set_magic_quotes_runtime($is);
}
?>