<?php
define('THIS_SCRIPT', 'admin_boardset');
define('THIS_ADMIN_HIDE', 'admin_boardset');
include_once 'global.php';
$lang = load_lang($lang, 'admin_boardset' );
$gettitle = $lang['template1'];//论坛标题;
if($boardid>0){
	$stats = $TopicInfo['title'].$gettitle;
}
if ( $boardid == 0 ){
	$stats = $lang['template1'];//论坛标题;
	$arrNavMenu = array(
		0=>array('linktitle'=>$stats,'linkurl'=>'admin_boardset.php')
	);
	head(1,0,0,$arrNavMenu);
}else{
	$gettitle = $stats;
	head(0);
}
PageLoad();
footer();
function PageLoad(){
	global $filetype,$lang,$fsetting,$CurrentPage,$PageNum,$totalrec,$star,$pagelistnum,$Pcount,$toback,$tonext,$userinfo,$search,$hot_list,$usergroupid,$gettitle,$TopicInfo,$boardinfo,$forum,$db,$dv,$boardtitle,$_DCACHE;
	global $new_list,$file_list,$tab,$maxpage,$boardid,$username,$send,$action,$title_num,$userid,$showfile,$vipuser,$boardmaster,$superboardmaster,$master,$membername,$hot_boardid,$news_boardid,$upload_url,$forumshowcache,$ads_cache;
	if($usergroupid!=1 && $usergroupid!=2 && $usergroupid!=3){//判断是否登录;
		showmsg($lang['Error.strings1']);
		exit;
	}
	if ($boardid==0){
		$query=$db->query("select boardid,boardtype,showtype,parentid,readme,indeximg,boardmaster,child,postnum,topicnum,todaynum,lastpost,allowpost,ishidden,ischeckout from {$dv}board where depth<2 order by rootid,orders");
		$showboardlist = 1;
		require_once CACHE_PATH.'forum_index.php';
	}else{
		if($boardinfo['child'] > 0){
			$query=$db->query("select boardid,boardtype,parentid,readme,indeximg,boardmaster,child,postnum,topicnum,todaynum,lastpost,allowpost,ishidden,ischeckout from {$dv}board where boardid=$boardid or parentid=$boardid order by rootid,orders");
			$showboardlist = 1;
		}
	}
	$forum_today = $forum_topic = $forum_posts = 0;
	if($showboardlist == 1){
		while($forum =& $db->fetch_array($query)) {
			//Start 隐含版面处理
			if(1 == $forum['ishidden'] AND $membername) {
				include_once CACHE_PATH.'board/board_all.php';
				$_strboardmaster = $_DCACHE['all_forums'][$forum['boardid']]['boardmaster'];
				//	预留超级版主和管理员的权限判断
				((1 == $groupsetting[37]) OR (false !== strpos("|{$_strboardmaster}|", "|{$membername}|"))) AND ($forum['ishidden'] = 0);
			}
			//End 隐含版面处理
			if ($forum['allowpost'] == 1 and $forum['child']>0){
				$forum_topic += $forum['topicnum'];//主题数;
				$forum_posts += $forum['postnum'];//贴子数;
				$forum_today += $forum['todaynum'];
			}
			if ($forum['allowpost'] == 0 and $forum['child']>0 and $forum['parentid']>0){
				$forum_topic += $forum['topicnum'];//主题数;
				$forum_posts += $forum['postnum'];//贴子数;
				$forum_today += $forum['todaynum'];
			}
			$_forum[] =& $forum;
		}
	}
	if($action=='saveeditinfo'){
		savebasicinfo();
	}
	elseif($action=='saveboardads'){
		saveadsinfo();		
	}
	include_once printout('admin_boardset');
}

function & currboardinfo() {
	global $db,$dv,$lang,$userid,$boardid;
	$boardid=$_GET['boardid'];
	$beboardid=$db->scalar("select count(1) from {$dv}board where boardid={$boardid}");
	if($beboardid<=0){
		showmsg($lang['tpl.htm.str27']);
		exit;
	}
	$sql=$db->query("select boardid,boardtype,showtype,parentid,parentstr,depth,rootid,child,readme,boardmaster,postnum,topicnum,indeximg,todaynum,boarduser,lastpost,orders,board_setting,board_user,isgroupsetting,boardtopstr,cid,rules,allowpost,ishidden,ischeckout,ads_id,setting_id,audit_settings_id from {$dv}board where boardid={$boardid}");
	while ($arr=$db->fetch_array($sql)) {
		$list[]=$arr;
	}
	$db->free_result($sql);
	return $list;
}
function savebasicinfo() {
	global $db,$dv,$lang,$userid,$boardid;
	$boardtype=$_POST['boardtype'];
	$boardreadme=$_POST['boardreadme'];
	$boardrules=$_POST['boardrules'];
	$boardmaster=$_POST['boardmaster'];
	$boardid=$_GET['boardid'];
	$beboardid=$db->scalar("select count(1) from {$dv}board where boardid={$boardid}");
	if($beboardid<=0){
		showmsg($lang['tpl.htm.str27']);
		exit;
	}
	$master=explode('|',$boardmaster);
	$boardmaster='';
	foreach($master as $bmaster){
		if (trim($bmaster)!=''){
			if ($boardmaster ==''){$boardmaster=trim($bmaster);}else{$boardmaster.='|'.trim($bmaster);}
		}
	}
	$master=explode('|',$boardmaster);
	$masterlist=get_masterlist($boardid);
	$all_masterlist=get_all_masterlist();
	//检查是否有撤职版主
	foreach($masterlist as $oldmaster){
		if (!in_array($oldmaster,$master)){
			$un_master[]=$oldmaster;
		}
	}
	foreach($master as $bmaster){
		if (!in_array($bmaster,$all_masterlist)){
			$new_master[]=$bmaster;
		}
	}
	$db->query("update {$dv}board set boardtype='{$boardtype}',readme='{$boardreadme}',rules='{$boardrules}',boardmaster='{$boardmaster}' where boardid=$boardid");

	//删除缓存
	del_static_board($boardid);

	if (is_array($un_master)){
		$all_masterlist1=get_all_masterlist();
		foreach($un_master as $username){
			if (!in_array($username,$all_masterlist1)){//撤消职务了
				if ($_POST['unset_master']=='0'){
					//转为贵宾
					set_usergroup($username,0);
				}else{
					//自动更新等级
					reset_grade($username);
				}
			}
		}
	}
	if (is_array($new_master) ){
		foreach($new_master as $username){
			set_usergroup($username);
		}
	}
	include_once INC_PATH.'BoardCache.class.php';
	$mycache =& new BoardCache($db);
	$mycache->createAll();
	unset ($mycache);
	showmsg($lang['tpl.htm.str29']);
	exit;
}
//获取某板面版主列表数组
function get_masterlist($int_boardid){
		global $db,$dv,$lang;
		$masterlist=array();
		$query=$db->query('select boardmaster from '.$dv.'board where boardid='.intval($int_boardid));
		while($master = $db->fetch_array($query)){
		$boardmaster=$master['boardmaster'];
		$mst=explode('|',$boardmaster);
		foreach($mst as $board_master){
			if (trim($board_master)!=''){
				if(!in_array(trim($board_master),$masterlist) ){
					$masterlist[]=$board_master;
				}
			}	
		}
	}
	return $masterlist;
}
//获取所有版主列表数组
function get_all_masterlist(){
		global $db,$dv,$lang;
		$all_masterlist=array();
		$query=$db->query('select boardmaster from '.$dv.'board order by rootid,orders');
		while($master = $db->fetch_array($query)){
		$boardmaster=$master['boardmaster'];
		$mst=explode('|',$boardmaster);
		foreach($mst as $board_master){
		if (trim($board_master)!=''){
			if(!in_array(trim($board_master),$all_masterlist) ){
			$all_masterlist[]=$board_master;
		}
		}	
		}
	}
	return $all_masterlist;
}


function set_usergroup($username,$int_usergroupid=null){
	global $db,$dv;
	if ($username!=''){
		if ($int_usergroupid==null){
		$userinfo=get_usergroup_info(3);
		$db->query("update ".$dv."user set usergroupid= 3 ,userclass='".addslashes($userinfo['usertitle'])."',titlepic='".addslashes($userinfo['grouppic'])."' where usergroupid > 3 and username='".addslashes($username)."'");
		}else if($int_usergroupid==0){
			$userinfo=get_usergroup_info(8);
		$db->query("update ".$dv."user set usergroupid= 8 ,userclass='".addslashes($userinfo['usertitle'])."',titlepic='".addslashes($userinfo['grouppic'])."' where usergroupid > 2 and username='".addslashes($username)."'");
			}else{
			$userinfo=get_usergroup_info($int_usergroupid);
			$db->query("update ".$dv."user set usergroupid= ".intval($int_usergroupid)." ,userclass='".addslashes($userinfo['usertitle'])."',titlepic='".addslashes($userinfo['grouppic'])."' where  username='".addslashes($username)."'");
		}
	}
}


function reset_grade($username){
	global $db,$dv;
	$query=$db->query("select userpost from ".$dv."user where username='".addslashes($username)."'");
	if ($db->num_rows($query) > 0) {
		$userpost=@mysql_result($query, 0,0);
		$query=$db->query("select usergroupid,minarticle from ".$dv."usergroups  where parentgid=3 order by minarticle");
			while($usergroup = $db->fetch_array($query)) {
				$usergroupid=$usergroup['usergroupid'];
				if (!($userpost > $usergroup['minarticle'])){
					break;
				}
			}
		if ($usergroupid!=null){
		set_usergroup($username,$usergroupid);
		}
	}
}

function &boardmaster_limit() {
	global $lang,$userid,$username,$db,$dv,$membername,$boardid;
	$boardid=$_GET['boardid'];
	$becurrboardmaster=true;
	if($beadmin=$db->scalar("select count(1) from {$dv}user where (usergroupid=1 or usergroupid=2) and username='{$membername}'")){
		$becurrboardmaster=true;		
	}
	else{
		$boardmasterstr=$db->scalar("select boardmaster from {$dv}board where boardid={$boardid}");
		$boardarr=explode('|',$boardmasterstr);
		if(!in_array($membername,$boardarr)){
			$becurrboardmaster=false;
		}
	}
	return $becurrboardmaster;
}
function &boardmaster_parent() {
	global $lang,$userid,$username,$db,$dv,$membername,$boardid;
	$boardid=$_GET['boardid'];
	$query1=$db->query("select parentstr from {$dv}board where boardid={$boardid}");
	if($arr1=$db->fetch_array($query1)){
		if($arr1['parentstr'] != 0){
			$query=$db->query("select boardmaster from {$dv}board where boardid in(".$arr1['parentstr'].")");
			while($arr=$db->fetch_array($query)){
				$boardmasterstr= $arr["boardmaster"];
				$boardarr=explode('|',$boardmasterstr);
				if(in_array($membername,$boardarr)){
					return true;;
				}
			}
		}
	}
	return false;
}

function split_replace($string){
	$string=str_replace('&lt;&gt;','&amp;lt;&amp;gt;',$string);
	return  str_replace('<>','&lt;&gt;',$string);
	}

function getads_project() {//得到广告方案
	global $dv,$db,$lang,$userid,$boardid;
	$beboardid=$db->scalar("select count(1) from {$dv}board where boardid={$boardid}");
	if($beboardid<=0){
		showmsg($lang['tpl.htm.str27']);
		exit;
	}
	$adsid=$db->scalar("select ads_id from {$dv}board where boardid={$boardid}");
	$sql=$db->query("select id,type,setting,description,s_class,c_orders from {$dv}fsettings where id={$adsid}");
	while ($arr=$db->fetch_array($sql)) {
		$list[]=$arr;
	}
	$db->free_result($sql);
	return $list;
}

function saveadsinfo() {
	global $dv,$db,$lang,$userid,$ads_cache;
	$adsid=$_GET['adsid'];
	$ads_float_linkurl = preg_match('/^http:\/\//', $_POST['float_adsurl']) ? $_POST['float_adsurl'] : 'http://'.$_POST['float_adsurl'];
	$ads_picad_linkurl = preg_match('/^http:\/\//', $_POST['fixupadsurl']) ? $_POST['fixupadsurl'] : 'http://'.$_POST['fixupadsurl'];
	$ads_con=split_replace($_POST['adsintotop']).'<>'.split_replace($_POST['adsintoend'])
	.'<>'.split_replace($_POST['beopen_floatads'])
	.'<>'.split_replace($_POST['float_adspic'])
	.'<>'.split_replace($ads_float_linkurl)
	.'<>'.split_replace($_POST['float_adswidth'])
	.'<>'.split_replace($_POST['float_adsheight'])
	.'<>'.split_replace($_POST['beopen_fixupads'])
	.'<>'.split_replace($_POST['fixupadspic'])
	.'<>'.split_replace($ads_picad_linkurl)
	.'<>'.split_replace($_POST['fixupadswidth'])
	.'<>'.split_replace($_POST['fixupadsheight'])
	.'<>'.split_replace($_POST['beopenmiddle'])
	.'<>'.split_replace($_POST['forum_adscode'])
	.'<>'.split_replace($_POST['beopen_wordads'])
	.'<>'.split_replace($_POST['adsplace'])
	.'<>'.split_replace($_POST['adsnum'])
	.'<>'.split_replace($_POST['wordadsinfo']);
	$ads_con=str_replace('<!--','',str_replace('//-->','',$ads_con));
	$db->query("update ".$dv."fsettings set setting='$ads_con' where s_class=2 and id=". intval($adsid));
	require_once(INC_PATH.'BoardCache.class.php');
	$ads_cache =& new BoardCache($db);
	$ads_cache->createAll();
	$ads_cache->createForumSetting();

	//删除缓存
	del_static_board($_GET['boardid']);
	showmsg($lang['tpl.htm.str28']);
	exit;
}
?>