<?php
/*
	Dvbbs.php  - Version: 2.0.0
	Web: http://www.dvbbs.net
	Copyright: 2000-2006 Dvbbs.Net
	script file:board_functions.php
	Script Written by Lao Mi
	Function of the Page:论坛管理相关函数
	*/
	if(!defined('PRODUCT') || PRODUCT !='Dvbbs.php' || !defined('ISDVBBS') || !defined('VERSION')) exit('Access denied.Sorry, you can not access this file directly.');
	//取得当前最小的可用广告编号
	function get_ads_id(){
		global $db,$dv;
		$query=$db->query("select id from {$dv}fsettings where s_class=2 order by id");
		$result = $db->fetch_row($query);
		return $result[0];
		}
	//取得当前最小的可用设置编号
	function get_setting_id(){
		global $db,$dv;
		$query=$db->query("select id from {$dv}fsettings where s_class=1 order by id");
		$result = $db->fetch_row($query);
		return $result[0];
		}
	//更新所有版面层次结构。
	function forum_board_init($parentid){
			global $db,$lang,$dv,$int_orders;
			
			if($parentid==0){
				$int_orders=1;
				$db->query("update {$dv}board set depth=0,parentstr='0' where parentid=0");
				}
			$query=$db->query('select boardid,rootid,parentstr,depth,child,boardtype from '.$dv.'board where parentid='.$parentid.' order by rootid,orders');
			while($forum = $db->fetch_array($query)) {
				if($forum['parentstr']!='0'){
					$parentstr=$forum['parentstr'].','.$forum['boardid'];
				}else{
					$parentstr=$forum['boardid'];
				}
					$db->query("update ".$dv."board set depth=".(intval($forum['depth'])+1).",parentstr='".$parentstr."',rootid=".intval($forum['rootid'])."  where parentid=".intval($forum['boardid']));
					$res=$db->query("select count(*) from ".$dv."board where parentid=".intval($forum['boardid']));
					$child=@mysql_result($res, 0,0);
					$db->query("update ".$dv."board set child=".intval($child).",orders=".intval($int_orders)."  where boardid=".intval($forum['boardid']));
					$int_orders+=1;

					forum_board_init(intval($forum['boardid']));
			}	
	}


//获取所有版主列表数组
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 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;
}
//设置用户等级模块
//$int_usergroupid=null的时候是设置板主等级，如果是超级版主身份和管理员身份，不会更新等级。如果此参数为数字，则按此参数执行等级更新。
//$int_usergroupid=0的时候是把版主转为贵宾,如果原来是超级版主或管理员，不会执行更新。
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 get_usergroup_info($groupid){
	global $db,$dv;
	$query=$db->query('select usertitle,grouppic from '.$dv.'usergroups where usergroupid='.intval($groupid));
	if ($db->num_rows($query) > 0) {
		return array('usertitle'=>@mysql_result($query, 0,0),'grouppic'=>@mysql_result($query, 0,1));
		}
	}
//按用户发帖数自动更新等级模块
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);
			}
		}
	}
//更新版面排序模块
//新建论坛板面，取一个没使用的最小编号做论坛boardid
	function get_newboardid(){
	global $db,$dv;
	$boardid = array();			//添加by xiao D,解决在第一次创建新版面时，出现warning错误
	$query=$db->query('select boardid from '.$dv.'board order by boardid');
	while($board = $db->fetch_array($query)){
		$boardid[]=$board['boardid'];
		}
	$endboardid= end($boardid);
	$init_boardid= $endboardid+1;
	if ($init_boardid==444 || $init_boardid==777){
		$init_boardid+=1;
		}
	for($i=0; $i < $endboardid; $i++){
		if ((($boardid[$i]-1) != $i) && $i !=444 && $i!=777){
			$init_boardid= $i+1;
			break;
			}
		}
	return $init_boardid;
	}
//新建论坛顶级分类的时候，获取一个没使用的最小编号rootid
	function get_newrootid(){
	global $db,$dv;
	$rootid = array();			//添加by xiao D,解决在第一次创建新版面时，出现warning错误
	$query=$db->query('select distinct rootid from '.$dv.'board order by rootid');
	while($board = $db->fetch_array($query)){
		$rootid[]=$board['rootid'];
		}
	$endrootid= intval(end($rootid));
	$init_root= $endrootid+1;
	$temprootid= 1;
	for($i=0; $i < $endrootid; $i++){
		if (($rootid[$i]-1) != $i){
			$init_root=($i+1);
			break;
		}
	}
	return $init_root;
}
//取最小可用模板编号
function get_min_sid(){
	return 1;
	
	}
//取最小可用css样式编号
function get_min_cid(){
	return 1;
	
	}
?>