<?php
define('THIS_SCRIPT', 'boardunite');
include_once 'global.php';
$lang = load_lang($lang,'boardunite');
checklogin();
header("content-Type: text/html; charset=".$charset);
head();
if ( strpos($sysinfo['flag'],'11,') === false ){
	show_msg($lang['readme_tit'],$lang['error.str1'],0,'','','','');
	exit;
}
PageLoad();
footer();
function Pageload(){
	global $lang,$action,$db,$arraySubForum;
	switch ($action){
		case "unite":
			forum_unite();
		break;
		default:
			GetSubForum();
		break;
	}
include printout('boardunite');
}
function GetSubForum()
{	
	global $db,$dv,$arraySubForum;
	$arraySubForum = array();
	$sql=$db->query("select boardid,boardtype,depth from {$dv}board order by rootid,orders");
	while($arrList=$db->fetch_array($sql)){		
		$arraySubForum[]=$arrList;
	}
}
function forum_unite(){
	global $lang,$dv,$db,$old_board,$new_board;
	$old_board=$_POST['old_board'];
	$new_board=$_POST['new_board'];
	if($old_board==$new_board){
		show_msg($lang['error.info2'],$lang['unite.info10'],0,'','',$lang['url'],$lang['comback']);
	exit;	
	}
	else{
		$boardid_list_sql=$db->query("select boardid from {$dv}board");
		while ($b_arr=$db->fetch_array($boardid_list_sql)) {
			$boardid_list.=$b_arr['boardid'].",";
		}
		$boardid_list=substr($boardid_list,0,-1);
		$sql=$db->query("select parentstr,boardid,depth,parentid,child,rootid from {$dv}board where boardid=".$old_board."");
		$arr=$db->fetch_array($sql);
		if(empty($arr)){
			show_msg($lang['error.info2'],$lang['unite.info15'],0,'','',$lang['url'],$lang['comback']);
		exit;				
		}
		if($arr['parentstr']==0){
			$parentstr=$arr['boardid'];
			$iparentid=$arr['boardid'];
			$parentid=0;
		}
		else{
			$parentstr=$arr['parentstr'].",".$arr['boardid'];
			$iparentid=$arr['parentid'];
			$parentid=$arr['parentid'];
		}
		$iparentstr=$arr['boardid'];
		$depth=$arr['depth'];
		$child=$arr['child']+1;
		$rootid=$arr['rootid'];
		$i=0;
		if($parentid==0){
			$numid=$db->scalar("select count(1) from {$dv}board where boardid=".$new_board." and rootid=".$rootid."");
		}
		else{
			$numid=$db->scalar("select count(1) from {$dv}board where boardid=".$new_board." and parentstr like '%".$parentstr."%'");			
		}
		if($numid>0){
			show_msg($lang['error.info2'],$lang['unite.info11'],0,'','',$lang['url'],$lang['comback']);
		exit;	
		}
		$i=0;
		$stmt=$db->query("select boardid from {$dv}board where rootid=".$rootid." and parentstr like '%".$parentstr."%'");
		while ($list=$db->fetch_array($stmt)){
			if($i==0){
				$iparentstr=$list['boardid'];
			}
			else{
				$iparentstr=$iparentstr.",".$list['boardid'];
			}
			$i++;
		}
		if($i>0){
			$parentstr=$iparentstr.",".$old_board;
		}
		else{
			$parentstr=$old_board;
		}
		if($depth>0){
			$upsql=$db->query("update {$dv}board set child=child-".$child." where boardid=".$iparentid."");
		}
		for($j=1;$j<=$depth;$j++){
			$sql_1=$db->query("select parentid from {$dv}board where boardid=".$iparentid."");
			while ($set=$db->fetch_array($sql_1)){
				$upsql_1=$db->query("update {$dv}board set child=child-".$child." where boardid=".$set['parentid']."");
			}
		}
		$table_sql=$db->query("select id,tablename,tabletype from {$dv}tablelist");
		while ($t_arr=$db->fetch_array($table_sql)){
			$upsql_2=$db->query("update ".$t_arr['tablename']." set boardid=".$new_board." where boardid in (".$parentstr.")");
			$upsql_3=$db->query("update ".$t_arr['tablename']." set locktopic=".$new_board." where boardid=444 and locktopic in (".$parentstr.")");
		}
		$sql=$db->query("update {$dv}topic set boardid=".$new_board.",mode=0 where boardid in (".$parentstr.")");
		$sql=$db->query("update {$dv}besttopic set boardid=".$new_board." where boardid in (".$parentstr.")");
		$sql=$db->query("update {$dv}topic set locktopic=".$new_board." where boardid=444 and locktopic in (".$parentstr.")");
		$sql=$db->query("update {$dv}upfile set f_boardid=".$new_board." where f_boardid in (".$parentstr.")");
		$sql_num=$db->query("select sum(postnum) as pn,sum(topicnum) as tn,sum(todaynum) as dn from {$dv}board where rootid=".$rootid." and boardid in (".$parentstr.")");
		$rs=$db->fetch_array($sql_num);
		$sql=$db->query("delete from {$dv}board where rootid=".$rootid." and boardid in (".$parentstr.")");
		$sql=$db->query("delete from {$dv}useraccess where not uc_boardid in (".$boardid_list.")");
		$sql_dir=$db->query("select parentstr,boardid from {$dv}board where boardid=".$new_board."");
		$row=$db->fetch_array($sql_dir);
		if($row['parentstr']==""){
			$parentstr=$row['boardid'];
		}
		else{
			$parentstr=$row['parentstr'];
		}
		$sql=$db->query("update {$dv}board set postnum=postnum+".$rs['pn'].",topicnum=topicnum+".$rs['tn'].",todaynum=todaynum+".$rs['dn']." where boardid=".$new_board."");
		create_cache();
		if($sql){
			show_msg($lang['error.info2'],$lang['unite.info12'],1,'','',$lang['url'],$lang['comback']);
		exit;	
		}
	}
}
function create_cache(){
	global $lang, $db;
	include_once INC_PATH.'BoardCache.class.php';
	$mycache =& new BoardCache($db);
	$mycache->createAll();
}
?>