<?php
if( !defined('ISDVBBS') ){
	header('HTTP/1.0 404 Not Found');
	exit;
}
!defined('TIME_NOW') AND define('TIME_NOW', time());
/**
* Display space header html code.
*
* @param	array	example:$_argOpts=array('stats'=>$stats,'isSystem'=>true,'copyright'=>'','navEnable'=>true,'topEnable'=>true)
*
* @return	void	
*/
function spaceHeader($argopts=array()){
	global $lang,$spaceConfig_System,$spaceSystemSetting,$spaceSystemUpSetting,$spaceConfig_Syscat,$spaceConfig_Syschatcat,$spaceConfig_Default;
	global $fsetting,$spaceUserInfo,$spaceName,$isSpaceMaster,$isSpaceOwner,$membername,$userid,$userhidden,$gettitle,$ScriptName,$_DCACHE,$master;
	if( !isset($argopts['isSystem']) ){
		$argopts['isSystem'] = true;
	}
	if( !isset($argopts['topEnable']) ){
		$argopts['topEnable'] = true;
	}
	if( !isset($argopts['navEnable']) ){
		$argopts['navEnable'] = true;
	}
	require_once printout('space.header');
}//end function spaceHeader
function spaceFooter(){
	global $_GET,$_POST,$_COOKIE,$_SESSION,$db,$fsetting,$lang,$time_start,$db_footertime,$timestamp,$db_obstart;
	global $lang,$spaceConfig_System,$spaceSystemSetting,$spaceSystemUpSetting,$spaceConfig_Syscat,$spaceConfig_Syschatcat,$spaceConfig_Default;
	global $spaceUserInfo,$spaceName,$isSpaceMaster,$isSpaceOwner,$spaceUserName;
	if($db){
		$qn=$db->querynum;
	}
	if ($fsetting['isruntime'] == 1){
		$t_array	= explode(' ',microtime());
		$totaltime	= number_format(($t_array[0]+$t_array[1]-$time_start),6);
		$is_gzip=($db_obstart==1 ? "Gzip enabled" : "Gzip disabled").$db_union[3];
		$forum_speed	= "页面执行时间 $totaltime 秒, $qn 次数据查询  , $is_gzip";
	}
	if (isset($GLOBALS['_COOKIE_USERID'])) {
		set_cookie('userid', $GLOBALS['_COOKIE_USERID'], $GLOBALS['user_selected_cookiedate']);
		set_cookie('username', $GLOBALS['_COOKIE_USERNAME'], $GLOBALS['user_selected_cookiedate']);
		set_cookie('password', $GLOBALS['_COOKIE_USERPASSWD'], $GLOBALS['user_selected_cookiedate']);
		set_cookie('cometime', $GLOBALS['_COOKIE_COME_TIME'], $GLOBALS['user_selected_cookiedate']);
		set_cookie('cboardid', $GLOBALS['_COOKIE_CBOARDID'], $GLOBALS['user_selected_cookiedate']);
		set_cookie('sysusername', $GLOBALS['_COOKIE_SYS_USERNAME'], 3600);
		set_cookie('syspassword', $GLOBALS['_COOKIE_SYS_PASSWD'], 3600);
	}
	$lang = load_lang($lang,'space.footer');
	require_once printout('space.footer');
	$output = str_replace(array('<!--<!---->','<!---->','<!---->-->','{$DV_HTML_TITLE}'),array('', '', '', $GLOBALS['DV_HTML_TITLE']),ob_get_contents());
	ob_end_clean();
	$db_obstart == 1 ? ob_start('ob_gzhandler') : ob_start();
	echo $output;
	@$db->close();
	ob_end_flush();
	exit;
}
function spaceShowMsg($message,$issuc=0,$gotourl="",$dispfooter=true){
	global $fsetting,$lang,$boardid,$db;
	$lang = load_lang($lang,'space.error');
	$_tpl_description = $message;
	require_once printout('space.showerr');
	if( $dispfooter == true ){
		spaceFooter();
	} elseif( $db ){
		@$db->close();
	}
}

function &updateDiaryTopic($args = array())
{
	$userid = $args['userid'];
	if (!is_numeric($userid) OR $userid <= 0) {
		return false;
	}
	global $dv, $db;
	if (isset($args['Action'])) {
		$diarytopic = false;
		if ('insert' === $args['Action'] AND $args['TopicID'] > 0 AND (isDate($args['PostTime']) OR is_numeric($args['PostTime']))) {
			$diarytopic =& $args['DiaryTopic'];
			$diarytopic[dv_gmtimestamp(dateFormat($args['PostTime'], 'Ymd'))-getUserTimeZoneOffset()][$args['TopicID']] = $args['TopicID'];
		} elseif ('update' === $args['Action']) {
			if (empty($args['DiaryTopic']) OR !is_array($args['DiaryTopic'])) {
				return false;
			}
			if ($args['TopicID'] > 0 AND (isDate($args['RawPostTime']) OR is_numeric($args['RawPostTime'])) AND (isDate($args['NewPostTime']) OR is_numeric($args['NewPostTime']))) {
				$rawtimestamp = dv_gmtimestamp(dateFormat($args['RawPostTime'], 'Ymd'))-getUserTimeZoneOffset();
				$newtimestamp = dv_gmtimestamp(dateFormat($args['NewPostTime'], 'Ymd'))-getUserTimeZoneOffset();
				if ($rawtimestamp != $newtimestamp) {
					$diarytopic =& $args['DiaryTopic'];
					unset($diarytopic[$rawtimestamp][$args['TopicID']]);
					if (empty($diarytopic[$rawtimestamp])) {
						unset($diarytopic[$rawtimestamp]);
					}
					$diarytopic[$newtimestamp][$args['TopicID']] = $args['TopicID'];
				}
			} else {
				$diarytopic =& $args['DiaryTopic'];
			}
		} elseif('delete' === $args['Action'] AND $args['TopicID'] > 0 AND (isDate($args['PostTime']) OR is_numeric($args['PostTime']))) {
			if (empty($args['DiaryTopic']) OR !is_array($args['DiaryTopic'])) {
				return false;
			}
			$diarytopic =& $args['DiaryTopic'];
			$timwstamp = dv_gmtimestamp(dateFormat($args['PostTime'], 'Ymd'))-getUserTimeZoneOffset();
			unset($diarytopic[$timwstamp][$args['TopicID']]);
			if (empty($diarytopic[$timwstamp])) {
				unset($diarytopic[$timwstamp]);
			}
		} else {
			return false;
		}
		if (false !== $diarytopic) {
			if (!empty($diarytopic) AND is_array($diarytopic)) {
				$dbdiarytopic = $db->escape_string(serialize($diarytopic));
			} else {
				$dbdiarytopic = $db->escape_string(serialize(array()));
			}
			if (isset($dbdiarytopic) AND $db->query("UPDATE {$dv}space_user SET diary_topic='{$dbdiarytopic}' WHERE userid='{$userid}'")) {
				return $diarytopic;
			}
		}
		return false;
	} else {
		static $cache = array();
		if (!isset($cache["{$userid}"])) {
			if ($query = $db->query("SELECT /*!40001 SQL_CACHE */ posttime, topicid FROM {$dv}space_topic /*!32312 USE INDEX(diary_topic) */ WHERE userid='{$userid}' GROUP BY posttime, topicid ORDER BY posttime ASC, topicid ASC")) {
				if ($db->num_rows($query) > 0) {
					$tmparray = array();
					while ($rs = $db->fetch_array($query, 2)) {
						$timestamp = $rs[0];
						if (!empty($timestamp)) {
							$tmparray[$timestamp][$rs[1]] = $rs[1];
						}
					}
					if (!empty($tmparray)) {
						$cache["{$userid}"] =& $tmparray;
						$db->query("UPDATE {$dv}space_user SET diary_topic='".$db->escape_string(serialize($tmparray))."' WHERE userid='{$userid}'");
					}
				}
			}
		}
		return $cache["{$userid}"];
	}
}

require_once INC_PATH.'space.functions.php';
function &getUserCatFromXmlData(){
	global $dv,$db,$spaceConfig_UserCat;
	static $_blnInitialized = false;
	static $_arrspaceConfig_UserCat = array( 'result_set' => array() ,'spacetopic' => array() ,'spacelink' => array() ,'spacephoto' => array() ,'spacepost' => array() ,'spacekeyword' => array(), 'spacemusic' => array(), 'selected_templates_prev' => 'default.' );
	if( $_blnInitialized === false ){
		$_blnInitialized = true;
		if( !empty($spaceConfig_UserCat) ){
				$_arrspaceConfig_UserCat['result_set'] =& $spaceConfig_UserCat['result_set'];
				$_arrspaceConfig_UserCat['spacetopic'] =& $spaceConfig_UserCat['spacetopic'];
				$_arrspaceConfig_UserCat['spacelink'] =& $spaceConfig_UserCat['spacelink'];
				$_arrspaceConfig_UserCat['spacephoto'] =& $spaceConfig_UserCat['spacephoto'];
				$_arrspaceConfig_UserCat['spacepost'] =& $spaceConfig_UserCat['spacepost'];
				$_arrspaceConfig_UserCat['spacekeyword'] =& $spaceConfig_UserCat['spacekeyword'];
				$_arrspaceConfig_UserCat['spacemusic'] =& $spaceConfig_UserCat['spacemusic'];
		}
		if( ($_strXmlData = $db->scalar("SELECT xmldata FROM {$dv}space_user WHERE userid='{$GLOBALS['spaceUserID']}'")) !== false ){
			$_arrTmp = unserialize(base64_decode($_strXmlData));
			if( $_arrTmp !== false ){
				$_arrspaceConfig_UserCat['spacetopic'] =& $_arrTmp['spacetopic'];
				$_arrspaceConfig_UserCat['spacelink'] =& $_arrTmp['spacelink'];
				$_arrspaceConfig_UserCat['spacephoto'] =& $_arrTmp['spacephoto'];
				$_arrspaceConfig_UserCat['spacepost'] =& $_arrTmp['spacepost'];
				$_arrspaceConfig_UserCat['spacekeyword'] =& $_arrTmp['spacekeyword'];
				$_arrspaceConfig_UserCat['spacemusic'] =& $_arrTmp['spacemusic'];
			}
		}
	}
	return $_arrspaceConfig_UserCat;
}
function saveUserCatToXmlData(){
	global $dv,$db;
	$_arrspaceConfig_UserCat =& getUserCatFromXmlData();
	$db->query("UPDATE {$dv}space_user SET xmldata='".$db->escape_string(base64_encode(serialize($_arrspaceConfig_UserCat)))."' WHERE userid='{$GLOBALS['spaceUserID']}'");
}
function &rebuildUserXmlData($spaceuserid=0, $showmsg=true)
{
	if ($spaceuserid <= 0) {
		if (defined('THIS_SCRIPT')) {
			if ('space' === THIS_SCRIPT) {
				$spaceuserid = intval($GLOBALS['spaceUserID']);
			} elseif ('spacemanage' === THIS_SCRIPT OR 'spacepostings' === THIS_SCRIPT) {
				$spaceuserid = intval($GLOBALS['userid']);
			}
		} else {
			$spaceuserid = intval($GLOBALS['userid']);
		}
	} else {
		$spaceuserid = intval($spaceuserid);
	}
	global $lang,$dv,$db;
	$_arrspaceConfig_UserCat =& getUserCatFromXmlData();
	$_arrspaceConfig_UserCat['result_set'] =& $db->getResultSet("SELECT ucatid, userid, ucattitle, ucatnote, opentime, utype, topicnum, postnum, todaynum, isview, lastuptime FROM {$dv}space_usercat WHERE userid='{$spaceuserid}' ORDER BY utype, ucatid");
	if( $_arrspaceConfig_UserCat['result_set'] === false ){
		$_arrspaceConfig_UserCat['result_set'] = array();
	}

//更新首页主题数据
	rebuildTopicData(false);
//更新首页链接数据
	rebuildLinkData(false);
//更新首页图片数据
	rebuildPhotoData(false);
//更新首页评论数据
	rebuildPostData(false);
//更新关键字转换数据
	rebuildKeyWordData(false);
//更新音乐数据
	rebuildMusicData(false);
	
	saveUserCatToXmlData();

	(true === $showmsg) AND spaceShowMsg($lang['spaceError.str25'],1);
	return $_arrspaceConfig_UserCat;
}//end function rebuildUserXmlData
function rebuildTopicData($rebuildimmediately=true)	//重建空间用户首页的主题数据
{
	if ($spaceuserid <= 0) {
		if (defined('THIS_SCRIPT')) {
			if ('space' === THIS_SCRIPT) {
				$spaceuserid = intval($GLOBALS['spaceUserID']);
			} elseif ('spacemanage' === THIS_SCRIPT OR 'spacepostings' === THIS_SCRIPT) {
				$spaceuserid = intval($GLOBALS['userid']);
			}
		} else {
			$spaceuserid = intval($GLOBALS['userid']);
		}
	} else {
		$spaceuserid = intval($spaceuserid);
	}

	global $dv,$db,$lang,$spaceUserCat,$spaceSetting,$spaceName,$_objEnc;
	if( !is_numeric($spaceSetting[6]) ){
		$spaceSetting[6] = 10;
	}
	$spaceconfig_usercat =& getUserCatFromXmlData();
	if ($spaceSetting[6] < 1) {
		$spaceSetting[6] = 20;
	}
	unset($spaceconfig_usercat['spacetopic']);
	if ($query = $db->query("SELECT /*!40001 SQL_CACHE */ topicid,catid,scatid,userid,username,title,titlenote,posttime,child,hits,isview,islock,stype,lastposttime,isbest,s_key,weather FROM {$dv}space_topic /*!32312 USE INDEX(newly_topic_by_user) */ WHERE userid='{$spaceuserid}' AND stype>=0 AND islock<3 ORDER BY topicid DESC",array('pageSize'=>$spaceSetting[6]))) {
		while ($topicinfo =& $db->fetch_array($query)) {
			$titlenote = trim($topicinfo['titlenote']);
			if ('' === $titlenote) {
				$titlenote = trim($db->scalar("SELECT content FROM {$dv}space_post WHERE parentid=0 AND rootid='{$topicinfo['topicid']}'"));
			}
			if ($titlenote !== '' ) {
				$_ubbcode = stripslashes(spaceUbbCode($titlenote, false));
				$titlenote =& getTitleNote($_ubbcode, 0, $spaceSetting[2]);
				$titlenote =& _imgaddjscode($titlenote);
				$db->query("UPDATE {$dv}space_topic SET titlenote='".$db->escape_string($titlenote)."' WHERE topicid='{$topicinfo['topicid']}'");
			}
			$topicinfo['titlenote'] = $titlenote;
			$spaceconfig_usercat['spacetopic'][] =& $topicinfo;
		}
		$db->free_result($query);
	}
	if (empty($spaceconfig_usercat['spacetopic'])) {
		$spaceconfig_usercat['spacetopic'] = array();
	}
	if (true === $mustupdatedb) {
		saveUserCatToXmlData();
	}
}//end function rebuildTopicData
function rebuildLinkData($rebuildimmediately=true)	//重建空间用户首页的链接数据
{
	if ($spaceuserid <= 0) {
		if (defined('THIS_SCRIPT')) {
			if ('space' === THIS_SCRIPT) {
				$spaceuserid = intval($GLOBALS['spaceUserID']);
			} elseif ('spacemanage' === THIS_SCRIPT OR 'spacepostings' === THIS_SCRIPT) {
				$spaceuserid = intval($GLOBALS['userid']);
			}
		} else {
			$spaceuserid = intval($GLOBALS['userid']);
		}
	} else {
		$spaceuserid = intval($spaceuserid);
	}

	global $dv,$db,$lang,$spaceUserCat,$spaceSetting,$spaceName,$_objEnc;
	$_arrspaceConfig_UserCat =& getUserCatFromXmlData();
	$_arrspaceConfig_UserCat['spacelink'] =& $db->getResultSet("SELECT topicid,catid,scatid,userid,username,title,titlenote,posttime,child,hits,isview,islock,stype,lastposttime,isbest,s_key,weather,title AS content FROM {$dv}space_topic WHERE userid='{$spaceuserid}' AND stype=2 AND islock<3 ORDER BY lastposttime DESC",array('pageSize'=>5));
	if( $_arrspaceConfig_UserCat['spacelink'] === false ){
		$_arrspaceConfig_UserCat['spacelink'] = array();
	} else{
		foreach( $_arrspaceConfig_UserCat['spacelink'] as $_index => $_val ){
			$_strContent = $_val['content'].'';
			if( $_strContent === '' ){
				$_strContent = $db->scalar("SELECT content FROM {$dv}space_post WHERE parentid=0 AND rootid='{$_val['topicid']}'");
			}
			$_arrspaceConfig_UserCat['spacelink'][$_index]['content'] = $_strContent;
		}
	}
	if( $_argUpdateDataBase === true ){
		saveUserCatToXmlData();
	}
}//end function rebuildLinkData
function rebuildPhotoData($rebuildimmediately=true)	//重建空间用户首页的图片数据
{
	if ($spaceuserid <= 0) {
		if (defined('THIS_SCRIPT')) {
			if ('space' === THIS_SCRIPT) {
				$spaceuserid = intval($GLOBALS['spaceUserID']);
			} elseif ('spacemanage' === THIS_SCRIPT OR 'spacepostings' === THIS_SCRIPT) {
				$spaceuserid = intval($GLOBALS['userid']);
			}
		} else {
			$spaceuserid = intval($GLOBALS['userid']);
		}
	} else {
		$spaceuserid = intval($spaceuserid);
	}

	global $dv,$db,$lang,$spaceUserCat,$spaceSetting,$spaceName,$_objEnc;
	$_arrspaceConfig_UserCat =& getUserCatFromXmlData();
	$_arrspaceConfig_UserCat['spacephoto'] =& $db->getResultSet("SELECT id,spaceuserid,username,catid,stype,topicid,postid,istopic,title,filename,filetype,filesize,filenote,downnum,viewnum,dateandtime,previewimage,islock FROM {$dv}space_upfile WHERE stype=4 AND istopic=0 AND islock<3 AND spaceuserid='{$spaceuserid}' ORDER BY dateandtime DESC",array('pageSize'=>5));
	if( $_arrspaceConfig_UserCat['spacephoto'] === false ){
		$_arrspaceConfig_UserCat['spacephoto'] = array();
	}
	if( $_argUpdateDataBase === true ){
		saveUserCatToXmlData();
	}
}//end function rebuildPhotoData
function rebuildPostData($rebuildimmediately=true)	//重建空间用户首页的评论数据
{
	if ($spaceuserid <= 0) {
		if (defined('THIS_SCRIPT')) {
			if ('space' === THIS_SCRIPT) {
				$spaceuserid = intval($GLOBALS['spaceUserID']);
			} elseif ('spacemanage' === THIS_SCRIPT OR 'spacepostings' === THIS_SCRIPT) {
				$spaceuserid = intval($GLOBALS['userid']);
			}
		} else {
			$spaceuserid = intval($GLOBALS['userid']);
		}
	} else {
		$spaceuserid = intval($spaceuserid);
	}

	global $dv,$db,$lang,$spaceUserCat,$spaceSetting,$spaceName,$_objEnc;
	if( !is_numeric($spaceSetting[3]) ){
		$spaceSetting[3] = 10;
	}
	$_arrspaceConfig_UserCat =& getUserCatFromXmlData();
	$_arrspaceConfig_UserCat['spacepost'] =& $db->getResultSet("SELECT postid,catid,scatid,parentid,rootid,userid,username,title,SUBSTRING(content, 1, 50),jointime,ip,stype FROM {$dv}space_post WHERE spaceuserid='{$spaceuserid}' AND parentid>0 AND stype IN(0,3,4) ORDER BY jointime DESC",array('pageSize'=>$spaceSetting[3]));
	if( $_arrspaceConfig_UserCat['spacepost'] === false ){
		$_arrspaceConfig_UserCat['spacepost'] = array();
	}
	if( $_argUpdateDataBase === true ){
		saveUserCatToXmlData();
	}
}//end function rebuildPostData
function rebuildKeyWordData($rebuildimmediately=true)	//重建空间用户首页的关键字转换数据
{
	if ($spaceuserid <= 0) {
		if (defined('THIS_SCRIPT')) {
			if ('space' === THIS_SCRIPT) {
				$spaceuserid = intval($GLOBALS['spaceUserID']);
			} elseif ('spacemanage' === THIS_SCRIPT OR 'spacepostings' === THIS_SCRIPT) {
				$spaceuserid = intval($GLOBALS['userid']);
			}
		} else {
			$spaceuserid = intval($GLOBALS['userid']);
		}
	} else {
		$spaceuserid = intval($spaceuserid);
	}

	global $dv,$db,$lang,$spaceUserCat,$spaceSetting,$spaceName,$_objEnc;
	if( !is_numeric($spaceSetting[3]) ){
		$spaceSetting[3] = 10;
	}
	$_arrspaceConfig_UserCat =& getUserCatFromXmlData();
	$_arrspaceConfig_UserCat['spacekeyword'] =& $db->getResultSet("SELECT postid,catid,scatid,parentid,rootid,userid,username,title,content,jointime,ip,stype FROM {$dv}space_post WHERE spaceuserid='{$GLOBALS['spaceUserID']}' AND parentid>0 AND stype IN(0,3,4) ORDER BY jointime DESC",array('pageSize'=>$spaceSetting[3]));
	if( $_arrspaceConfig_UserCat['spacekeyword'] === false ){
		$_arrspaceConfig_UserCat['spacekeyword'] = array();
	}
	if( $_argUpdateDataBase === true ){
		saveUserCatToXmlData();
	}
}//end function rebuildKeyWordData
function rebuildMusicData($rebuildimmediately=true)	//重建空间用户首页的音乐数据
{
	if ($spaceuserid <= 0) {
		if (defined('THIS_SCRIPT')) {
			if ('space' === THIS_SCRIPT) {
				$spaceuserid = intval($GLOBALS['spaceUserID']);
			} elseif ('spacemanage' === THIS_SCRIPT OR 'spacepostings' === THIS_SCRIPT) {
				$spaceuserid = intval($GLOBALS['userid']);
			}
		} else {
			$spaceuserid = intval($GLOBALS['userid']);
		}
	} else {
		$spaceuserid = intval($spaceuserid);
	}

	global $dv,$db,$lang,$spaceUserCat,$spaceSetting,$spaceName,$_objEnc;
	if( !is_numeric($spaceSetting[3]) ){
		$spaceSetting[3] = 10;
	}
	$_arrspaceConfig_UserCat =& getUserCatFromXmlData();
	$_arrspaceConfig_UserCat['spacemusic'] =& $db->getResultSet("SELECT postid,catid,scatid,parentid,rootid,userid,username,title,SUBSTRING(content, 1, 50),jointime,ip,stype FROM {$dv}space_post WHERE spaceuserid='{$spaceuserid}' AND parentid>0 AND stype='5' ORDER BY jointime DESC",array('pageSize'=>$spaceSetting[3]));
	if( $_arrspaceConfig_UserCat['spacemusic'] === false ){
		$_arrspaceConfig_UserCat['spacemusic'] = array();
	}
	if( $_argUpdateDataBase === true ){
		saveUserCatToXmlData();
	}
}//end function rebuildMusicData
?>