<?php
/*
		[FuShow.cn] (C) 2013-2014 HXWY Inc.
		FuShow Friend V3.0				Id: ushow_top.php
		Email <service@fushow.cn>		http://fushow.cn/
*/

if(!defined('IN_FUSHOW')) {
	exit('Access Denied');
}

//分页
$perpage = 20;
$page = empty($_GET['page'])?1:intval($_GET['page']);
if($page<1) $page=1;
$start = ($page-1)*$perpage;
if(empty($_SCONFIG['networkpage'])) $start = 0;

//检查开始数
ckstart($start, $perpage);

//普通浏览模式
$cache_file = '';
$cache_time = $_SCONFIG['topcachetime'];
if($cache_time<5) $cache_time = 5;
$fuids = array();
$count = 0;
$now_pos = 0;

$pro=array();
$query = $_SGLOBAL['db']->query("SELECT id,name FROM ".tname('major_manager'));

while ($value = $_SGLOBAL['db']->fetch_array($query)) {
			$pro[] = $value;		
		}
$aaa = array();
$i=0;
foreach($pro as $value) {
	$aaa[$i++] = $value['id'];
}
if((!in_array($_GET['view'], array('online','mm','gg','credit','experience','friendnum','viewnum','updatetime','all','mg','live','isliving','islivew','islived')))&&(!in_array($_GET['view'],$aaa))) $_GET['view'] = 'show';

if ($_GET['view'] == 'show') {
	$c_sql = "SELECT COUNT(*) FROM ".tname('show');
	$sql = "SELECT space.*, field.*, main.* FROM ".tname('show')." main
		LEFT JOIN ".tname('space')." space ON space.uid=main.uid
		LEFT JOIN ".tname('spacefield')." field ON field.uid=main.uid
		ORDER BY main.credit DESC";

	//清理
	if(substr($_SGLOBAL['timestamp'], -1) == '0') {
		$_SGLOBAL['db']->query("DELETE FROM ".tname('show')." WHERE credit<1");//清理小于1的数据
	}

	//我的竞价积分
	$space['showcredit'] = getcount('show', array('uid'=>$space['uid']), 'credit');
	$space['showcredit'] = intval($space['showcredit']);

	//我的位置
	$cookie_name = 'fushow_top_'.$_GET['view'];
	if($_SCOOKIE[$cookie_name]) {
		$now_pos = $_SCOOKIE[$cookie_name];
	} else {
		$now_pos = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT COUNT(*) FROM ".tname('show')." WHERE credit>'$space[showcredit]'"), 0);
		$now_pos++;
		ssetcookie($cookie_name, $now_pos);
	}

} elseif ($_GET['view'] == 'mm') {
	if($multi_mode) {
		$c_sql = "SELECT COUNT(*) FROM ".tname('spacefield')." WHERE sex='2'";
	} else {
		$count = 100;
		$cache_file = S_ROOT.'./data/cache_top_mm.txt';
	}
	$sql = "SELECT main.*, field.* FROM ".tname('space')." main, ".tname('spacefield')." field
		WHERE field.sex='2' AND field.uid=main.uid
		ORDER BY main.viewnum DESC";

	//我的位置
	$cookie_name = 'fushow_top_'.$_GET['view'];
	if($_SCOOKIE[$cookie_name]) {
		$now_pos = $_SCOOKIE[$cookie_name];
	} else {
		if($space['sex']==2) {
			$pos_sql = "SELECT COUNT(*) FROM ".tname('space')." s, ".tname('spacefield')." f WHERE s.viewnum>'$space[viewnum]' AND f.sex='2' AND f.uid=s.uid";
			$now_pos = $_SGLOBAL['db']->result($_SGLOBAL['db']->query($pos_sql), 0);
			$now_pos++;
		} else {
			$now_pos = -1;
		}
		ssetcookie($cookie_name, $now_pos);
	}

} elseif ($_GET['view'] == 'gg') {
	if($multi_mode) {
		$c_sql = "SELECT COUNT(*) FROM ".tname('spacefield')." WHERE sex='1'";
	} else {
		$count = 100;
		$cache_file = S_ROOT.'./data/cache_top_gg.txt';
	}
	$sql = "SELECT main.*, field.* FROM ".tname('space')." main, ".tname('spacefield')." field
		WHERE field.sex='1' AND field.uid=main.uid
		ORDER BY main.viewnum DESC";
	//我的位置
	$cookie_name = 'fushow_top_'.$_GET['view'];
	if($_SCOOKIE[$cookie_name]) {
		$now_pos = $_SCOOKIE[$cookie_name];
	} else {
		if($space['sex']==1) {
			$pos_sql = "SELECT COUNT(*) FROM ".tname('space')." s, ".tname('spacefield')." f WHERE s.viewnum>'$space[viewnum]' AND f.sex='1' AND f.uid=s.uid";
			$now_pos = $_SGLOBAL['db']->result($_SGLOBAL['db']->query($pos_sql), 0);
			$now_pos++;
		} else {
			$now_pos = -1;
		}
		ssetcookie($cookie_name, $now_pos);
	}
//-------------------------------xiaohe-----------------------------------------
}elseif ($_GET['view'] == 'all') {
	if($multi_mode) {
		$c_sql = "SELECT COUNT(*) FROM ".tname('major')." m JOIN ".tname('collate')." c ON m.u_id=c.u_id WHERE c.collating=9 AND c.collated=1";
	} else {
		$count = 100;
		$cache_file = S_ROOT.'./data/cache_top_all.txt';
	}
	$sql = "SELECT 
				main.*, field.*, major.*, c.* 
			FROM 
				".tname('major')." major 
				JOIN 
				".tname('collate')." c 
				ON major.u_id=c.u_id 
				LEFT JOIN 
				".tname('space')." main 
				ON main.uid=major.u_id 
				LEFT JOIN 
				".tname('spacefield')." field 
				ON field.uid=major.u_id 
			WHERE 
				c.collating=9 AND c.collated=1 
			ORDER BY 
				main.viewnum DESC";
	//我的位置
	$cookie_name = 'fushow_top_all';
	if($_SCOOKIE[$cookie_name]) {
		$now_pos = $_SCOOKIE[$cookie_name];
	} else {
		if($space['sex']==1) {
			$pos_sql = "SELECT COUNT(*) FROM ".tname('major')." major JOIN ".tname('collate')." c ON major.u_id=c.u_id LEFT JOIN ".tname('space')." main ON main.uid=major.u_id LEFT JOIN ".tname('spacefield')." field ON field.uid=major.u_id	WHERE c.collating=9 AND c.collated=1 ORDER BY main.viewnum DESC";
			$now_pos = $_SGLOBAL['db']->result($_SGLOBAL['db']->query($pos_sql), 0);
			$now_pos++;
		} else {
			$now_pos = -1;
		}
		ssetcookie($cookie_name, $now_pos);
	}
	
}elseif ($_GET['view'] == 'live') {
	if($multi_mode) {
		$c_sql = "SELECT COUNT(*) FROM ".tname('live')." WHERE collated=1";
	} else {
		$count = 100;
		$cache_file = S_ROOT.'./data/cache_top_live.txt';
	}
	$sql = "SELECT 
				l.*, lf.* 
			FROM 
				".tname('live')." l, 
				".tname('livefield')." lf
			WHERE 
				l.liveid=lf.liveid
			AND l.collated=1 
			ORDER BY l.starttime DESC";
	//我的位置
	$cookie_name = 'fushow_top_live';
	if($_SCOOKIE[$cookie_name]) {
		$now_pos = $_SCOOKIE[$cookie_name];
	} else {
		if($space['sex']==1) {
			$pos_sql = "SELECT COUNT(*) FROM ".tname('space')." s, ".tname('spacefield')." f WHERE s.viewnum>'$space[viewnum]' AND f.uid=s.uid";
			$now_pos = $_SGLOBAL['db']->result($_SGLOBAL['db']->query($pos_sql), 0);
			$now_pos++;
		} else {
			$now_pos = -1;
		}
		ssetcookie($cookie_name, $now_pos);
	}
	
	}elseif ($_GET['view'] == 'isliving') {
	if($multi_mode) {
		$c_sql = "SELECT COUNT(*) FROM ".tname('live')." live WHERE UNIX_TIMESTAMP(now())>=live.starttime AND UNIX_TIMESTAMP(now())<=live.endtime AND live.collated=1";
	} else {
		$count = 100;
		$cache_file = S_ROOT.'./data/cache_top_isliving.txt';
	}
	$sql = "SELECT 
				l.*, lf.* 
			FROM 
				".tname('live')." l, 
				".tname('livefield')." lf
			WHERE 
				l.liveid=lf.liveid
			AND
			UNIX_TIMESTAMP(now())>=l.starttime AND UNIX_TIMESTAMP(now())<=l.endtime
			AND l.collated=1 
			ORDER BY l.starttime DESC";
	//我的位置
	$cookie_name = 'fushow_top_isliving';
	if($_SCOOKIE[$cookie_name]) {
		$now_pos = $_SCOOKIE[$cookie_name];
	} else {
		if($space['sex']==1) {
			$pos_sql = "SELECT COUNT(*) FROM ".tname('space')." s, ".tname('spacefield')." f WHERE s.viewnum>'$space[viewnum]' AND f.uid=s.uid";
			$now_pos = $_SGLOBAL['db']->result($_SGLOBAL['db']->query($pos_sql), 0);
			$now_pos++;
		} else {
			$now_pos = -1;
		}
		ssetcookie($cookie_name, $now_pos);
	}

	}elseif ($_GET['view'] == 'islivew') {
	if($multi_mode) {
		$c_sql = "SELECT COUNT(*) FROM ".tname('live')." WHERE UNIX_TIMESTAMP(now())<starttime AND collated=1";
	} else {
		$count = 100;
		$cache_file = S_ROOT.'./data/cache_top_islivew.txt';
	}
	$sql = "SELECT 
				l.*, lf.* 
			FROM 
				".tname('live')." l, 
				".tname('livefield')." lf
			WHERE 
				l.liveid=lf.liveid
			AND
			UNIX_TIMESTAMP(now())<l.starttime 
			AND l.collated=1 
			ORDER BY l.starttime DESC";
	//我的位置
	$cookie_name = 'fushow_top_islivew';
	if($_SCOOKIE[$cookie_name]) {
		$now_pos = $_SCOOKIE[$cookie_name];
	} else {
		if($space['sex']==1) {
			$pos_sql = "SELECT COUNT(*) FROM ".tname('space')." s, ".tname('spacefield')." f WHERE s.viewnum>'$space[viewnum]' AND f.uid=s.uid";
			$now_pos = $_SGLOBAL['db']->result($_SGLOBAL['db']->query($pos_sql), 0);
			$now_pos++;
		} else {
			$now_pos = -1;
		}
		ssetcookie($cookie_name, $now_pos);
	}

	}elseif ($_GET['view'] == 'islived') {
	if($multi_mode) {
		$c_sql = "SELECT COUNT(*) FROM ".tname('live')." WHERE UNIX_TIMESTAMP(now())>l.endtime AND collated=1";
	} else {
		$count = 100;
		$cache_file = S_ROOT.'./data/cache_top_islived.txt';
	}
	$sql = "SELECT 
				l.*, lf.* 
			FROM 
				".tname('live')." l, 
				".tname('livefield')." lf
			WHERE 
				l.liveid=lf.liveid
			AND
				UNIX_TIMESTAMP(now())>l.endtime
			AND l.collated=1 
			ORDER BY l.starttime DESC";
	//我的位置
	$cookie_name = 'fushow_top_islived';
	if($_SCOOKIE[$cookie_name]) {
		$now_pos = $_SCOOKIE[$cookie_name];
	} else {
		if($space['sex']==1) {
			$pos_sql = "SELECT COUNT(*) FROM ".tname('space')." s, ".tname('spacefield')." f WHERE s.viewnum>'$space[viewnum]' AND f.uid=s.uid";
			$now_pos = $_SGLOBAL['db']->result($_SGLOBAL['db']->query($pos_sql), 0);
			$now_pos++;
		} else {
			$now_pos = -1;
		}
		ssetcookie($cookie_name, $now_pos);
	}

	}elseif ($_GET['view'] == 'live' && $_GET['work'] == $_GET['work']) {
	if($multi_mode) {
		$c_sql = "SELECT COUNT(*) FROM ".tname('live')." WHERE collated=1";
	} else {
		$count = 100;
		$cache_file = S_ROOT.'./data/cache_top_live.txt';
	}
	$sql = "SELECT 
				l.*, lf.* 
			FROM 
				".tname('live')." l, 
				".tname('livefield')." lf
			WHERE 
				l.liveid=lf.liveid
			AND l.collated=1 
			ORDER BY l.starttime DESC";
	//我的位置
	$cookie_name = 'fushow_top_live';
	if($_SCOOKIE[$cookie_name]) {
		$now_pos = $_SCOOKIE[$cookie_name];
	} else {
		if($space['sex']==1) {
			$pos_sql = "SELECT COUNT(*) FROM ".tname('space')." s, ".tname('spacefield')." f WHERE s.viewnum>'$space[viewnum]' AND f.uid=s.uid";
			$now_pos = $_SGLOBAL['db']->result($_SGLOBAL['db']->query($pos_sql), 0);
			$now_pos++;
		} else {
			$now_pos = -1;
		}
		ssetcookie($cookie_name, $now_pos);
	}

}elseif ($_GET['view'] == 'mg') {
	if($multi_mode) {
		$c_sql = "SELECT COUNT(*) FROM ".tname('spacefield')."";
	} else {
		$count = 100;
		$cache_file = S_ROOT.'./data/cache_top_mg.txt';
	}
	$sql = "SELECT 
				main.*, field.* 
			FROM 
				".tname('space')." main, 
				".tname('spacefield')." field
			WHERE 
				field.uid=main.uid
			ORDER BY main.viewnum DESC";
	//我的位置
	$cookie_name = 'fushow_top_mg';
	if($_SCOOKIE[$cookie_name]) {
		$now_pos = $_SCOOKIE[$cookie_name];
	} else {
		if($space['sex']==1) {
			$pos_sql = "SELECT COUNT(*) FROM ".tname('space')." s, ".tname('spacefield')." f WHERE s.viewnum>'$space[viewnum]' AND f.uid=s.uid";
			$now_pos = $_SGLOBAL['db']->result($_SGLOBAL['db']->query($pos_sql), 0);
			$now_pos++;
		} else {
			$now_pos = -1;
		}
		ssetcookie($cookie_name, $now_pos);
	}
} elseif (in_array($_GET['view'],$aaa)){
	if($multi_mode) {
		$c_sql = "SELECT COUNT(*) FROM ".tname('major')." m JOIN ".tname('collate')." c  WHERE c.collating=9 AND c.collated=1 AND work_id=".$_GET['view'];
	} else {
		$count = 100;
		$cache_file = S_ROOT.'./data/cache_top_'.$_GET['view'].'.txt';
	}
	$sql = "SELECT main.*, field.*, major.*,c.* FROM ".tname('major')." major JOIN ".tname('collate')." c ON major.u_id=c.u_id LEFT JOIN ".tname('space')." main ON main.uid=major.u_id LEFT JOIN ".tname('spacefield')." field ON field.uid=major.u_id	WHERE c.collating=9 AND c.collated=1 AND major.work_id=".$_GET['view'] ."  ORDER BY main.viewnum DESC";
	//我的位置
	$cookie_name = 'fushow_top_'.$_GET['view'];
	if($_SCOOKIE[$cookie_name]) {
		$now_pos = $_SCOOKIE[$cookie_name];
	} else {
		$pos_sql = "SELECT COUNT(*) FROM ".tname('major')." major JOIN ".tname('collate')." c ON major.u_id=c.u_id LEFT JOIN ".tname('space')." main ON main.uid=major.u_id LEFT JOIN ".tname('spacefield')." field ON field.uid=major.u_id	WHERE c.collating=9 AND c.collated=1 AND major.work_id=".$_GET['view'] ."  ORDER BY main.viewnum DESC";
		$now_pos = $_SGLOBAL['db']->result($_SGLOBAL['db']->query($pos_sql), 0);
		$now_pos++;
		ssetcookie($cookie_name, $now_pos);
	}

} elseif ($_GET['view'] == 'credit') {
	if($multi_mode) {
		$c_sql = "SELECT COUNT(*) FROM ".tname('space');
	} else {
		$count = 100;
		$cache_file = S_ROOT.'./data/cache_top_credit.txt';
	}
	$sql = "SELECT main.*, field.* FROM ".tname('space')." main
		LEFT JOIN ".tname('spacefield')." field ON field.uid=main.uid
		ORDER BY main.credit DESC";

	//我的位置
	$cookie_name = 'fushow_top_'.$_GET['view'];
	if($_SCOOKIE[$cookie_name]) {
		$now_pos = $_SCOOKIE[$cookie_name];
	} else {
		$pos_sql = "SELECT COUNT(*) FROM ".tname('space')." s WHERE s.credit>'$space[credit]'";
		$now_pos = $_SGLOBAL['db']->result($_SGLOBAL['db']->query($pos_sql), 0);
		$now_pos++;
		ssetcookie($cookie_name, $now_pos);
	}

} elseif ($_GET['view'] == 'experience') {
	if($multi_mode) {
		$c_sql = "SELECT COUNT(*) FROM ".tname('space');
	} else {
		$count = 100;
		$cache_file = S_ROOT.'./data/cache_top_experience.txt';
	}
	$sql = "SELECT main.*, field.* FROM ".tname('space')." main
		LEFT JOIN ".tname('spacefield')." field ON field.uid=main.uid
		ORDER BY main.experience DESC";

	//我的位置
	$cookie_name = 'fushow_top_'.$_GET['view'];
	if($_SCOOKIE[$cookie_name]) {
		$now_pos = $_SCOOKIE[$cookie_name];
	} else {
		$pos_sql = "SELECT COUNT(*) FROM ".tname('space')." s WHERE s.experience>'$space[experience]'";
		$now_pos = $_SGLOBAL['db']->result($_SGLOBAL['db']->query($pos_sql), 0);
		$now_pos++;
		ssetcookie($cookie_name, $now_pos);
	}

} elseif ($_GET['view'] == 'friendnum') {
	if($multi_mode) {
		$c_sql = "SELECT COUNT(*) FROM ".tname('space');
	} else {
		$count = 100;
		$cache_file = S_ROOT.'./data/cache_top_friendnum.txt';
	}
	$sql = "SELECT main.*, field.* FROM ".tname('space')." main
		LEFT JOIN ".tname('spacefield')." field ON field.uid=main.uid
		ORDER BY main.friendnum DESC";

	//我的位置
	$cookie_name = 'fushow_top_'.$_GET['view'];
	if($_SCOOKIE[$cookie_name]) {
		$now_pos = $_SCOOKIE[$cookie_name];
	} else {
		$pos_sql = "SELECT COUNT(*) FROM ".tname('space')." s WHERE s.friendnum>'$space[friendnum]'";
		$now_pos = $_SGLOBAL['db']->result($_SGLOBAL['db']->query($pos_sql), 0);
		$now_pos++;
		ssetcookie($cookie_name, $now_pos);
	}

} elseif ($_GET['view'] == 'viewnum') {
	if($multi_mode) {
		$c_sql = "SELECT COUNT(*) FROM ".tname('space');
	} else {
		$count = 100;
		$cache_file = S_ROOT.'./data/cache_top_viewnum.txt';
	}
	$sql = "SELECT main.*, field.* FROM ".tname('space')." main
		LEFT JOIN ".tname('spacefield')." field ON field.uid=main.uid
		ORDER BY main.viewnum DESC";

	//我的位置
	$cookie_name = 'fushow_top_'.$_GET['view'];
	if($_SCOOKIE[$cookie_name]) {
		$now_pos = $_SCOOKIE[$cookie_name];
	} else {
		$pos_sql = "SELECT COUNT(*) FROM ".tname('space')." s WHERE s.viewnum>'$space[viewnum]'";
		$now_pos = $_SGLOBAL['db']->result($_SGLOBAL['db']->query($pos_sql), 0);
		$now_pos++;
		ssetcookie($cookie_name, $now_pos);
	}


} elseif ($_GET['view'] == 'online') {
	$c_sql = "SELECT COUNT(*) FROM ".tname('session');
	$sql = "SELECT field.*, space.*, main.*
		FROM ".tname('session')." main USE INDEX (lastactivity)
		LEFT JOIN ".tname('space')." space ON space.uid=main.uid
		LEFT JOIN ".tname('spacefield')." field ON field.uid=main.uid
		ORDER BY main.lastactivity DESC";
	$now_pos = -1;
} elseif ($_GET['view'] == 'updatetime') {
	$c_sql = "SELECT COUNT(*) FROM ".tname('space');
	$sql = "SELECT main.*, field.* FROM ".tname('space')." main USE INDEX (updatetime)
		LEFT JOIN ".tname('spacefield')." field ON field.uid=main.uid
	  ORDER BY main.updatetime DESC";
	$now_pos = -1;
}

$list = array();
if(empty($count)) {
	$cache_mode = false;
	$count = empty($_SCONFIG['networkpage'])?1:$_SGLOBAL['db']->result($_SGLOBAL['db']->query($c_sql),0);
	$multi = multi($count, $perpage, $page, "fushow.php?do=top&view=$_GET[view]");
} else {
	$cache_mode = true;
	$multi = '';
	$start = 0;
	$perpage = $count;

	if($cache_file && file_exists($cache_file) && $_SGLOBAL['timestamp'] - @filemtime($cache_file) < $cache_time*60) {
		$list_cache = sreadfile($cache_file);
		$list = unserialize($list_cache);
	}
}

if($count && empty($list)) {
	$query = $_SGLOBAL['db']->query("$sql LIMIT $start,$perpage");
	while ($value = $_SGLOBAL['db']->fetch_array($query)) {
		$list[$value['uid']] = $value;
	}
	if($cache_mode && $cache_file) {
		swritefile($cache_file, serialize($list));
	}
}

foreach($list as $key => $value) {
	$value['isfriend'] = ($value['uid']==$space['uid'] || ($space['friends'] && in_array($value['uid'], $space['friends'])))?1:0;
	realname_set($value['uid'], $value['username'], $value['name'], $value['namestatus']);
	$fuids[] = $value['uid'];
	$list[$key] = $value;
}

//在线状态
$ols = array();
if($fuids) {
	$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('session')." WHERE uid IN (".simplode($fuids).")");
	while ($value = $_SGLOBAL['db']->fetch_array($query)) {
		if(!$value['magichidden']) {
			$ols[$value['uid']] = $value['lastactivity'];
		} elseif ($_GET['view'] == 'online' && $list[$value['uid']]) {
			unset($list[$value['uid']]);
		}
	}
}

$actives = array($_GET['view'] => ' class="active"');

include_once template("fushow_top");

?>