<?php
define('THIS_SCRIPT', 'spaceindex');
require_once 'space_global.php';
$fsetting['styletpl'] = $_DCACHE['stylecss'][0];
$fsetting['styletpl_prev'] = '';
$lang = load_lang($lang, 'spaceindex' );
$_strArchiveLink = '';
$_Count_ArrArchiveLink = count($arrArchiveLink);
if( $_Count_ArrArchiveLink == 5 ){
	$_strArchiveLink = 'ShowAll';
} else{
	$_strArchiveLink = str_replace('.html','',strtolower($arrArchiveLink[0]));
}


$spaceuploadbasedir = basename($fsetting['forumuploadname']).'/'.basename($spaceSystemUpSetting[19]).'/';
switch($_strArchiveLink){
	case 'show_user':
		spaceHeader(array('stats'=>$lang['HeaderTitle1_1'],'isSystem'=>true,'topEnable'=>true,'navEnable'=>true));
		require printout('spaceindex.show_user');
		break;
	case 'show_topic':
		if( $_Count_ArrArchiveLink < 2 ){
			$_intArrIndex = 0;
		} else{
			if( $arrArchiveLink[1] != "" ){
				$_arrTmp = explode('-',$arrArchiveLink[1]);
				$_intCount = count($_arrTmp);
				if( $_intCount == 4 ){
					$_selType = is_numeric($_arrTmp[2]) ? $_arrTmp[2] : 0;
					$_keyWord = $_arrTmp[1];
					$_strTmp = str_replace('.html','',$_arrTmp[3]);
					$_iPage = is_numeric($_strTmp) ? $_strTmp : 0;
					$_intArrIndex = $_arrTmp[0];
				} elseif( $_intCount == 2 ){
					$_strTmp = str_replace('.html','',$_arrTmp[1]);
					$_iPage = is_numeric($_strTmp) ? $_strTmp : 0;
					$_intArrIndex = $_arrTmp[0];
				} else{
					$_iPage = 1;
					$_intArrIndex = str_replace('.html','',$_arrTmp[0]);
				}
			} else{
				$_intArrIndex = 0;
			}
		}
		$_strDescriptions = $lang['HeaderTitle1_2'][$_intArrIndex];
		spaceHeader(array('stats'=>$_strDescriptions,'isSystem'=>true,'topEnable'=>true,'navEnable'=>true));
		require printout('spaceindex.show_topic');
		break;
	default:
		spaceHeader(array('stats'=>$lang['HeaderTitle1'],'isSystem'=>true,'topEnable'=>true,'navEnable'=>true));
		require printout('spaceindex');
		break;
}
spaceFooter();

function &getIndexSpaceCache($mustcreatecachefile = true, $cacheexpire = 3)
{
	global $dv, $db, $IsSqlDataBase, $spaceuploadbasedir, $spaceConfig_System;
	static $arrcache = array();
	if (false !== $mustcreatecachefile) {
		unset($arrcache);
		$arrcache = array();
	}
	if (empty($arrcache)) {
		require_once INC_PATH.'FileHandler.class.php';
		$cachefile = CACHE_PATH.'space/index.cache.php';
		$datenow = TIME_NOW;
		if (false === $mustcreatecachefile) {
			if (is_file($cachefile)) {
				$lastuptime = filemtime($cachefile);
			} else {
				$lastuptime = $datenow;
				$mustcreatecachefile = true;
			}
			if ($cacheexpire <= 0) {
				$cacheexpire = 3;	//	缓存到期时间，以分钟为单位
			}
		}
		if ((true === $mustcreatecachefile) OR dateDiff('minute', $lastuptime, $datenow) > $cacheexpire) {
			$arrstmt = array(
				//最新发表
				 'newly_post' => array(10, "SELECT /*!40001 SQL_CACHE */ topicid, catid, scatid, posttime, title, (CASE WHEN LENGTH(title)>40 THEN CONCAT(SUBSTRING(title, 1, 30), '...') ELSE title END) AS SubTitle, stype, userid, 'newly_post' AS arrkey FROM {$dv}space_topic /*!32312 USE INDEX(PRIMARY) */ ORDER BY topicid DESC")
				//个人空间排行——用户发主题帖总数排名前10
				,'hot_space' => array(10, "SELECT /*!40001 SQL_CACHE */ userid, username, (CASE WHEN LENGTH(username)>4 THEN CONCAT(SUBSTRING(username, 1, 4), '...') ELSE username END) AS SubUserName, nickname, (CASE WHEN LENGTH(nickname)>4 THEN CONCAT(SUBSTRING(nickname, 1, 4), '...') ELSE nickname END) AS SubNickName, spacename, spacetitle, syscatid, topicnum, 'hot_space' AS arrkey FROM {$dv}space_user /*!32312 USE INDEX(topicnum) */ ORDER BY topicnum DESC")
				//热门文章——回帖总数排名前10的主题帖
				,'hot_article' => array(10, "SELECT /*!40001 SQL_CACHE */ topicid, catid, scatid, userid, username, title, (CASE WHEN LENGTH(title)>34 THEN CONCAT(SUBSTRING(title, 1, 17), '...') ELSE title END) AS SubTitle, posttime, stype, 'hot_article' AS arrkey FROM {$dv}space_topic /*!32312 USE INDEX(child) */ ORDER BY child DESC")
				//最新图片/相册
				,'newly_photos' => array(12, "SELECT /*!40001 SQL_CACHE */ id, spaceuserid,userid,username,catid,stype,topicid,postid,istopic,title,(CASE WHEN LENGTH(title)>=6 THEN SUBSTRING(title, 1, 6) ELSE title END) AS SubTitle,filename,sfilename,filetype,filesize,filenote,downnum,viewnum,dateandtime,previewimage,islock,'newly_photos' AS arrkey FROM {$dv}space_upfile /*!32312 newly_photo_list */ WHERE stype=4 AND islock<3 AND filetype=1 GROUP BY topicid ORDER BY id DESC")
				//最新文章
				,'newly_article' => array(10, "SELECT /*!40001 SQL_CACHE */ topicid, catid, scatid, userid, username, title, (CASE WHEN LENGTH(title)>34 THEN CONCAT(SUBSTRING(title, 1, 17), '...') ELSE title END) AS SubTitle, posttime, stype, 'newly_article' AS arrkey FROM {$dv}space_topic /*!32312 USE INDEX(sort_topic_list) */ WHERE stype=0 AND islock<3 ORDER BY topicid DESC")
				//最新收藏
				,'newly_favorite' => array(10, "SELECT /*!40001 SQL_CACHE */ topicid, catid, scatid, userid, username, title, (CASE WHEN LENGTH(title)>34 THEN CONCAT(SUBSTRING(title, 1, 17), '...') ELSE title END) AS SubTitle, posttime, stype, 'newly_favorite' AS arrkey FROM {$dv}space_topic /*!32312 USE INDEX(sort_topic_list) */ WHERE stype=1 AND islock<3 ORDER BY topicid DESC")
				//最新书签
				,'newly_bookmark' => array(10, "SELECT /*!40001 SQL_CACHE */ topicid, catid, scatid, userid, username, title, (CASE WHEN LENGTH(title)>34 THEN CONCAT(SUBSTRING(title, 1, 17), '...') ELSE title END) AS SubTitle, posttime, stype, 'newly_bookmark' AS arrkey FROM {$dv}space_topic /*!32312 USE INDEX(sort_topic_list) */ WHERE stype=2 AND islock<3 ORDER BY topicid DESC")
				//最新播客
				,'newly_music' => array(10, "SELECT /*!40001 SQL_CACHE */ topicid, catid, scatid, userid, username, title, (CASE WHEN LENGTH(title)>12 THEN CONCAT(SUBSTRING(title, 1, 12), '...') ELSE title END) AS SubTitle, posttime, stype, 'newly_music' AS arrkey FROM {$dv}space_topic /*!32312 USE INDEX(sort_topic_list) */ WHERE stype=5 AND islock<3 ORDER BY topicid DESC")
				//推荐阅读——最新精华帖
				,'newly_besttopic' => array(10, "SELECT /*!40001 SQL_CACHE */ topicid, catid, scatid, userid, username, title, (CASE WHEN LENGTH(title)>13 THEN CONCAT(SUBSTRING(title, 1, 13), '...') ELSE title END) AS SubTitle, posttime, stype, 'newly_besttopic' AS arrkey FROM {$dv}space_topic /*!32312 USE INDEX(sort_topic_list) */ WHERE isbest='1' AND islock<3 ORDER BY topicid DESC")
			);
			foreach ($arrstmt AS $key => $arrval) {//生成缓存数据
				if (empty($arrval[1])) {
					continue;
				}
				if ($query = $db->query($arrval[1], array('pageSize' => $arrval[0]))) {
					if ($db->num_rows($query) > 0) {
						while ($dbinfo =& $db->fetch_array($query)) {
							$arrcache[$key][] = $dbinfo;
						}
						$db->free_result($query);
					}
				}
			}
			//最新交易
			if ($query = $db->query("SELECT /*!40001 SQL_CACHE */ topicid, catid, scatid, userid, username, title, (CASE WHEN LENGTH(title)>6 THEN CONCAT(SUBSTRING(title, 1, 4), '...') ELSE title END) AS SubTitle, posttime, stype, 'newly_buy' AS arrkey FROM {$dv}space_topic /*!32312 USE INDEX(sort_topic_list) */ WHERE stype=3 AND islock<3 ORDER BY topicid DESC", array('pageSize' => 6))) {
				$skinpath = $spaceConfig_System['s_path'];
				while ($buyinfo =& $db->fetch_array($query)) {
					$buyinfo['previewimage'] = "{$skinpath}images/nophoto.gif";
					if ($query2 = $db->query("SELECT filename, previewimage FROM {$dv}space_upfile WHERE topicid='{$buyinfo['topicid']}' AND filetype=1", array('pageSize' => 1))) {
						if ($upfilinfo =& $db->fetch_array($query2)) {
							if (!empty($upfilinfo['previewimage'])) {
								$buyinfo['previewimage'] = $spaceuploadbasedir.$upfilinfo['previewimage'];
							} else {
								$buyinfo['previewimage'] = $spaceuploadbasedir.$upfilinfo['filename'];
							}
						}
					}
					$arrcache['newly_buy'][] =& $buyinfo;
				}//end while
				$db->free_result($query);
			}
			if (!empty($arrcache) AND is_array($arrcache)) {//写入文件
				$cachedata = "<?php \r\n\$_DCACHE['space.index.cache'] = ".Dv_varExport($arrcache, true, true).";\r\n?>";
				FileHandler::find_writable_dir(FH_DEFAULT_WRITABLE_DIR);
				FileHandler::file_put_contents($cachefile, $cachedata);
				@chmod($cachefile, 0777);
				return $arrcache;
			} else {
				return false;
			}
		}
		if (file_exists($cachefile)) {
			require_once $cachefile;
			return $_DCACHE['space.index.cache'];
		}
	} else {
		return $arrcache;
	}
}

function &showuser_getspaceUser($_argCatID, $page = 1){
	global $dv,$db,$spaceSystemSetting;
	if( $page < 1 ) $page = 1;
	if ($spaceSystemSetting[7] < 1) {
		$spaceSystemSetting[7] = 20;
	}
	$_arrReturnVal = array('RecordCount'=>0,'AbsolutePage'=>$page,'PageSize'=>$spaceSystemSetting[7],'PageCount'=>0,'ResultSet'=>false);
	$_arrReturnVal['RecordCount'] = $db->scalar("SELECT COUNT(1) FROM {$dv}space_user");
	if( $_arrReturnVal['RecordCount'] > 0 ){
		$_arrReturnVal['PageCount'] = ceil((float)$_arrReturnVal['RecordCount']/(float)$_arrReturnVal['PageSize']);
		if( $_arrReturnVal['AbsolutePage'] > $_arrReturnVal['PageCount'] ){
			$_arrReturnVal['AbsolutePage'] = $_arrReturnVal['PageCount'];
		}
		if( $_arrReturnVal['AbsolutePage'] < 1 ){
			$_arrReturnVal['AbsolutePage'] = 1;
		}
		$_arrReturnVal['ResultSet'] = $db->getResultSet("SELECT  userid, username, (CASE WHEN LENGTH(username)>4 THEN CONCAT(SUBSTRING(username, 1, 4), '...') ELSE username END) AS SubUserName, nickname, (CASE WHEN LENGTH(nickname)>4 THEN CONCAT(SUBSTRING(nickname, 1, 4), '...') ELSE nickname END) AS SubNickName,spacename,spacetitle, (CASE WHEN LENGTH(spacetitle)>20 THEN CONCAT(SUBSTRING(spacetitle, 1, 17), '...') ELSE username END) AS SubSpaceTitle,spacechildtitle,joinspacetime,topicnum,favnum,photonum,postnum,todaynum,trackbacks,spacesize,syscatid,lastuptime,skinid FROM {$dv}space_user ORDER BY lastuptime  DESC",array('absolutePage'=>$_arrReturnVal['AbsolutePage'],'pageSize'=>$_arrReturnVal['PageSize']));
	}
	return $_arrReturnVal;
}

function &showtopic_getTopicList($stype, $scatid, $opts = array())
{
	global $dv, $db, $spaceSystemSetting;
	$absolutepage = trim($opts['absolute_page']);
	if ($absolutepage < 1) {
		$absolutepage = 1;
	}
	if ($spaceSystemSetting[7] < 1) {
		$spaceSystemSetting[7] = 20;
	}
	$returnval = array('RecordCount'=>0,'AbsolutePage'=>$absolutepage,'PageSize'=>$spaceSystemSetting[7],'PageCount'=>0,'ResultSet'=>false);
	if ($stype >= 0 AND $stype > 5) {
		return $returnval;
	}
	$commonwherstmt = '';
	$keyword = trim($opts['key_word']);
	if ($keyword !== '') {
		$sel = trim($opts['sel']);
		if (!is_numeric($sel)) {
			$sel = 0;
		}
		if (1 == $sel) {
			$commonwherstmt .= " AND username LIKE '%{$keyword}%'";
		} else {
			$commonwherstmt .= " AND title LIKE '%{$keyword}%'";
		}
	}
	if (4 == $stype) {	//	stype == 4 为相册
		$countstmt = "SELECT COUNT(1) FROM {$dv}space_upfile WHERE islock<3 AND filetype=1 AND stype='4' AND istopic=0 {$commonwherstmt}";
		$stmt = "SELECT id,spaceuserid,userid,username,catid,stype,topicid,postid,istopic,title,filename,sfilename,filetype,filesize,filenote,downnum,viewnum,dateandtime,previewimage,islock FROM {$dv}space_upfile WHERE islock<3 AND filetype=1 AND stype=4 AND istopic=0 {$commonwherstmt} ORDER BY dateandtime DESC";
	} else {
		if ($stype >= 0) {
			$wherestmt = " AND stype='{$stype}' ";
		} else {
			$wherestmt = " AND stype<>'4' ";
		}
		if ($scatid > 0) {
			$wherestmt .= " AND scatid='{$scatid}'";
		}
		$countstmt = "SELECT COUNT(1) FROM {$dv}space_topic WHERE 1=1 {$wherestmt} AND islock<1 {$commonwherstmt}";
		$stmt = "SELECT topicid,catid,scatid,userid,username,title,titlenote,posttime,child,hits,isview,islock,stype,lastposttime,isbest,s_key,weather,visituser,paymoney,paynumber,paytime,trackbacks FROM {$dv}space_topic /*!32312 USE INDEX(topic_list_by_type) */ WHERE 1=1 {$wherestmt} AND islock<1 {$commonwherstmt} ORdER BY lastposttime DESC";
	}
	$returnval['RecordCount'] = $db->scalar($countstmt);
	if ($returnval['RecordCount'] > 0) {
		$returnval['PageCount'] = ceil((float)$returnval['RecordCount']/(float)$returnval['PageSize']);
		if( $returnval['AbsolutePage'] > $returnval['PageCount'] ){
			$returnval['AbsolutePage'] = $returnval['PageCount'];
		}
		if( $returnval['AbsolutePage'] < 1 ){
			$returnval['AbsolutePage'] = 1;
		}
		$returnval['ResultSet'] = $db->getResultSet($stmt,array('absolutePage'=>$returnval['AbsolutePage'],'pageSize'=>$returnval['PageSize']));
	}
	return $returnval;
}//end function showtopic_getTopicList

?>