<?php

define('THIS_SCRIPT', 'infolist');
require_once 'global.php';

$stats = $lang['head.info'];
PageLoad();

function PageLoad(){
	global $t,$db,$fsetting,$lang,$superboardmaster,$master,$boardmaster,$Pcount,$pagelimited;
	//加载语言包
	$lang = load_lang($lang, 'infolist' );
	$stats = $lang['head.info'];
	switch($t){
		case 'even':
			ShowEven();
			break;
		case 'paper':
			ShowPaper();
			break;
		case 'smallpaper':
			ShowSmallPaper();
			break;
		case 'toplist':
		default:
			ShowToplist();
			break;
	}
	//关闭数据库连接
	//加载footer模板
	if (!admin_boardset_check())
	footer();
}//end function PageLoad

function ShowToplist(){
	global $stats,$db,$dv,$orders,$keyword,$fsetting,$lang,$page,$_DCACHE,$imgurl;
	$where = '';
	if( $fsetting['search_user'] == 0 ) $keyword = '';
	if( !empty($keyword) ){
		$where = " WHERE u.username='{$keyword}'";
	}
	$stmt = 'SELECT /*!40001 SQL_CACHE */ u.username,u.useremail,u.userclass,u.userim,u.userpost,u.joindate,u.userwealth,u.userid FROM '.$dv.'user AS u '.$where;
	switch($orders){
		case 2:
			$select2 = "selected";
			if( $fsetting['search_topuser'] == 0 ){
				head(1, 0, 0, $stats);
				showmsg($lang['template.strings15'],0,'index.php');
				exit;
			}
			$stmt .= " ORDER BY u.joindate DESC";
			break;
		case 3:
			$select3 = "selected";
			if( $fsetting['search_topuser'] === 0 ){
				head(1, 0, 0, $stats);
				showmsg($lang['template.strings16'],0,'index.php');
				exit;
			}
			$stmt .= " ORDER BY u.userwealth DESC";
			break;
		case 7:
			$select7 = "selected";
			if( $fsetting['search_alluser'] === 0 ){
				head(1, 0, 0, $stats);
				showmsg($lang['template.strings14'],0,'index.php');
				exit;
			}
			$stmt .= " ORDER BY u.userid DESC";
			break;
		case 8:
			$select8 = "selected";
			if( $fsetting['search_master'] === 0 ){
				head(1, 0, 0, $stats);
				showmsg($lang['template.strings13'],0,'index.php');
				exit;
			}
			$stmt = "SELECT /*!40001 SQL_CACHE */ username,useremail,userclass,userim,userpost,joindate,userwealth,userid FROM {$dv}user WHERE usergroupid<=3".('' !== $keyword ? " AND username='{$keyword}'" : "").' ORDER BY usergroupid,userpost DESC';
			break;
		case 1:
			$select1 = "selected";
			if( $fsetting['search_topuser'] === 0 ){
				head(1, 0, 0, $stats);
				showmsg($lang['template.strings17'],0,'index.php');
				exit;
			}
			$stmt .= " ORDER BY u.userpost DESC";
			break;
		default:
			$gettitle = $lang['toplist.gettitle1'];
			$select1 = "selected";
			if( $fsetting['search_topuser'] === 0 ){
				head(1, 0, 0, $stats);
				showmsg($lang['tpl.even.str12'],0,'index.php');
				exit;
			}
			$stmt .= " ORDER BY u.userpost DESC";
			break;
	}
	$pagelistnum =& $fsetting['search_usernum'];
	$toplistnum = $pagelistnum;
	if( empty($gettitle) ) {
		$gettitle = $lang['titlebyorders'][$orders];
	}
	if(intval($_GET['orders'])==7){
		$arrNavMenu = array(
						  0=>array('linktitle'=>$lang['template.strings6'],'linkurl'=>'infolist.php?t=toplist')
						 ,1=>array('linktitle'=>$lang['tpl.topl.str7'])
					);
		$gettitle=$lang['tpl.topl.str7'];
	}
	else{
		$arrNavMenu = array(
						 0=>array('linktitle'=>$lang['template.strings6'],'linkurl'=>'infolist.php?t=toplist')
						,1=>array('linktitle'=>$gettitle)
					);
	}
	head(1,0,0,$arrNavMenu);
	if( ($query = $db->query("Select /*!40001 SQL_CACHE */ forum_usernum From {$dv}setup") ) ){
		$result =& $db->fetch_row($query);
		$db->free_result($query);
		$usernum = intval($result[0]);
	}
	if ($fsetting['otherpagesize'] < 1) {
		$fsetting['otherpagesize'] = 20;
	}
	$pagelimited = $fsetting['otherpagesize'];
	if( $orders == 7 && $keyword == '' ){
		$listnum = $usernum;
	}elseif( $orders !=7 && $keyword == '' ){
		$listnum = $toplistnum;
	} else{
		$query = $db->query("SELECT /*!40001 SQL_CACHE */ COUNT(1) FROM {$dv}user AS u {$where}");
		$result =& $db->fetch_row($query);
		$db->free_result($query);
		$listnum = intval($result[0]);
	}
	$Pcount = ceil(floatval($listnum/$pagelimited));
	$page = intval($page);
	if( $page > $Pcount ){
		$page = $Pcount;
	} elseif( $page < 1 ){
		$page = 1;
	}
	if ($page > 150) {
		showmsg($lang['tpl.topl.str25'], 0);
		exit;
	}
	$_infoList = array();
	if( ($query = $db->query( $stmt, array('absolutePage'=>$page,'pageSize'=>$pagelimited) ) ) ){
		while( $infoList =& $db->fetch_array($query) ){
			$_infoList[] =& $infoList;
		}
		$db->free_result($query);
	}
	$width = $_DCACHE['stylecss'][1];
	$alertcolor = $_DCACHE['stylecss'][19];
	$oicqpic = $lang['template.pic1'];
	$homepagepic = $lang['template.pic2'];
	$msgpic = $lang['template.pic3'];
	$action = 't=toplist&';
	$boardid = '0&orders='.$orders.'&gettitle'.$orders.'='.urlencode($gettitle);
	//加载主体模板
	require_once printout('infolist.toplist');
}//end function ShowToplist

function ShowEven(){
	global $stats,$db,$dv,$orders,$keyword,$fsetting,$lang,$page;
	BoardEven();
}//end function ShowEven
function BoardEven(){
	global $stats,$db,$dv,$orders,$keyword,$fsetting,$lang,$page,$Pcount,$pagelimited;
	$listnum = 0;
	if ($fsetting['otherpagesize'] < 1) {
		$fsetting['otherpagesize'] = 20;
	}
	if( empty($gettitle) ) $gettitle =& $GLOBALS['gettitle'.$orders];
	$boardid = intval($GLOBALS['boardid']);
	if( $boardid == 0 ){
		$gettitle = $lang['template.strings4'];
	} else{
		$gettitle = $lang['template.strings5'];
	}
	head();
	if( $keyword != '' ){
		$where = " AND (l.l_touser LIKE '%{$keyword}%' OR l.l_content LIKE '%{$keyword}%' OR l.l_username LIKE '%{$keyword}%') ";
	}
	if( $boardid > 0 ){
		$query = $db->query("SELECT /*!40001 SQL_CACHE */ COUNT(1) FROM {$dv}log AS l WHERE l.l_boardid={$boardid} AND l.l_type>2 {$where}");
		$result =& $db->fetch_row($query);
		$listnum = intval($result[0]);
		$stmt = "SELECT /*!40001 SQL_CACHE */ l.l_id,l.l_announceid,l.l_boardid,l.l_touser,l.l_username,l.l_content,l.l_addtime,l.l_ip,l.l_type FROM {$dv}log AS l WHERE l.l_boardid={$boardid} AND l.l_type>2 {$where} ORDER BY l.l_addtime DESC ";
	} else{
		$query = $db->query("SELECT /*!40001 SQL_CACHE */ COUNT(1) FROM {$dv}log AS l WHERE l.l_type>2 {$where}");
		$result =& $db->fetch_row($query);
		$listnum = intval($result[0]);
		$stmt = "SELECT /*!40001 SQL_CACHE */ l.l_id,l.l_announceid,l.l_boardid,l.l_touser,l.l_username,l.l_content,l.l_addtime,l.l_ip,l.l_type FROM {$dv}log AS l WHERE l.l_type>2 {$where} ORDER BY l.l_addtime DESC ";
	}
	$pagelimited =20 ;//$fsetting['otherpagesize'];
	$Pcount = ceil(floatval($listnum/$pagelimited));
	$page = intval($page);
	if( $page > $Pcount ){
		$page = $Pcount;
	} elseif( $page < 1 ){
		$page = 1;
	}
	if( ($query = $db->query( $stmt, array('absolutePage'=>$page,'pageSize'=>$pagelimited) ) ) ){
		$_infoList = array();
		while( $result =& $db->fetch_array($query) ){
			$_infoList[] =& $result;
		}
		$db->free_result($query);
		$isshow = CheckShow();
	} else{
	}
	$action = 't=even&';
	//加载主体模板
	require_once printout('infolist.even');
}//end function BoardEven

function ShowPaper(){
	global $action,$stats,$db,$dv,$orders,$keyword,$fsetting,$lang,$page,$superboardmaster,$master,$boardmaster;
	if( !($boardmaster || $master || $superboardmaster) ){
	}
	if( $action == 'delpaper' ){
		Batch();
	} else{
		BoardPaper();
	}
}//end function ShowPaper
function Batch(){
	global $boardid,$action,$stats,$db,$dv,$orders,$keyword,$fsetting,$lang,$page,$sid,$superboardmaster,$master,$boardmaster;
	$userid = intval(get_cookie('UserID'));
	if( $userid == 0 ){
	} else{
	}
	if( ($master || $superboardmaster || $bordmaster) && $groupSetting27 == 1 ){
		$isAdminPaper = true;
	} else{
		$isAdminPaper = false;
	}
	if( $userGroupID > 3 && $groupSetting27 == 1 ){
		$isAdminPaper = true;
	} else{
	}
	if( $foundUserPer && intval($groupSetting27) == 1 ){
		$isAdminPaper = true;
	} elseif( $foundUserPer && intval($groupSetting27) == 0 ){
		$isAdminPaper = false;
	}
	$sidList= implode(',',$sid);
	if( checkIDList($sidList) ){
		if( $db->query("DELETE FROM {$dv}smallpaper WHERE s_id IN({$sidList})") ){
			$boardid = intval($boardid);
			$lang['tpl.str2'] = str_replace('{$boardtype}', $boardtype, $lang['tpl.str2']); 
			$db->query("INSERT INTO {$dv}log (l_AnnounceID,l_BoardID,l_touser,l_username,l_content,l_ip,l_type,l_addtime) VALUES(0, {$boardid}, {$lang['tpl.str1']}, '".get_cookie('UserName')."', {$lang['tpl.str2']}, '".forumusertrueip()."', 3,'".TIME_NOW."')");
		}
	}
	head(1,0,0,$arrNavMenu);
	$sucmsg =& $lang['template.strings2'];
	//加载主体模板
	require_once printout('board_action_result');
}//end function Batch
function BoardPaper(){
	global $action,$stats,$db,$dv,$orders,$keyword,$fsetting,$lang,$page,$_DCACHE;
	$listnum = 0;
	$boardid = intval($GLOBALS['boardid']);
	if( $boardid == 0 ){
		$arrNavMenu = array(
						 0=>array('linktitle'=>$lang['template.strings0'])
					);
		$query = $db->query("SELECT /*!40001 SQL_CACHE */ COUNT(1) FROM {$dv}smallpaper AS sp");
		$result =& $db->fetch_row($query);
		$listnum = intval($result[0]);
		$stmt = "SELECT /*!40001 SQL_CACHE */ sp.* FROM {$dv}smallpaper AS sp ORDER BY sp.s_addtime DESC";
	} else{
		$arrNavMenu = array(
						  0=>array('linktitle'=>$lang['template.strings1'])
					);
		$query = $db->query("SELECT /*!40001 SQL_CACHE */ COUNT(1) FROM {$dv}smallpaper AS sp WHERE sp.s_boardid={$boardid}");
		$result =& $db->fetch_row($query);
		$listnum = intval($result[0]);
		$stmt = "SELECT /*!40001 SQL_CACHE */ sp.* FROM {$dv}smallpaper AS sp WHERE sp.s_boardid={$boardid} ORDER BY sp.s_addtime DESC";
	}
	if ($fsetting['otherpagesize'] < 1) {
		$fsetting['otherpagesize'] = 20;
	}
	$pagelimited = $fsetting['otherpagesize'];
	$Pcount = ceil(floatval($listnum/$pagelimited));
	$page = intval($page);
	if( $page > $Pcount ){
		$page = $Pcount;
	} elseif( $page < 1 ){
		$page = 1;
	}
	if (admin_boardset_check())
	LoadStyleJs();
	else
	head(1,0,0,$arrNavMenu);

	if( ($query = $db->query( $stmt, array('absolutePage'=>$page,'pageSize'=>$pagelimited) ) ) ){
		$_infoList = array();
		while( $result =& $db->fetch_array($query) ){
			$_infoList[] =& $result;
		}
		$db->free_result($query);
	} else{
	}
	$action = 't=paper&';
	$width = $_DCACHE['stylecss'][1];
	$alertcolor = $_DCACHE['stylecss'][19];
	//加载主体模板
	require_once printout('infolist.paper');
}//end function BoardPaper

function ShowSmallPaper(){
	global $action,$stats,$db,$dv,$orders,$keyword,$fsetting,$lang,$page;
	if( $action == 'savepaper' ){
		SavePaper();
	} else{
		SmallPaper_Main();
	}
}//end ShowSmallPaper
function SavePaper(){
	global $stats,$db,$dv,$orders,$keyword,$fsetting,$lang,$page;
	$username =& $GLOBALS['username'];
	$password =& $GLOBALS['password'];
	$title =& $GLOBALS['title'];
	$Content =& $GLOBALS['Content'];
	if( $query = $db->query("SELECT /*!40001 SQL_CACHE */ userWealth FROM {$dv}user WHERE UserName='{$username}'") ){
		$result =& $db->fetch_array($query);
		if( intval($result['UserWealth']) < intval($groupSetting46) ){
		} else{
			$val['UserWealth'] = intval($val['UserWealth']) - intval($groupSetting46);
			$db->query("UPDATE {$dv}user SET UserWealth={$val['UserWealth']} WHERE UserName='{$username}'");
		}
	} else{
	}
	$boardid = intval($GLOBALS['boardid']);
	if( $db->query("INSERT INTO {$dv}smallpaper SET s_boardid={$boardid} ,s_username='{$username}' ,s_title='{$title}' ,s_content='{$Content}' ,S_Addtime='".TIME_NOW."'") ){
	}
	$arrNavMenu = array(
					  0=>array('linktitle'=>$lang['template.strings1'])
				);
	head(1,0,0,$arrNavMenu);
	$sucmsg =& $lang['template.strings27'];
	//加载主体模板
	require_once printout('board_action_result');
}//end function SavePaper()
function SmallPaper_Main(){
	global $stats,$db,$dv,$orders,$keyword,$fsetting,$lang,$page;
	$db->query("DELETE FROM {$dv}smallpaper WHERE s_addtime<'".addDays(TIME_NOW,-1)."'" );
	$tmp = $db->affected_rows();
	if( $tmp > 0 ){
	} else{
	}
	$arrNavMenu = array(
					  0=>array('linktitle'=>$lang['template.strings1'])
				);
	head(1,0,0,$arrNavMenu);
	$action = '';
	//加载主体模板
	require_once printout('infolist.smallpaper');
}//end function SmallPaper_Main()

function CheckShow(){
	global $boardid,$master,$superboardmaster;
	$boardid = intval($GLOBALS['boardid']);
	if( $master || $superboardmaster ){
		return true;
	} elseif( $boardid != 0 ){
		if( is_numeric($boardSetting) ){
			if( intval($boardSetting) == 1 ){
				return true;
			} else{
				return false;
			}
		}
	}
	else return false;
}//end function CheckShow

?>