<?php

define('THIS_SCRIPT', 'friendlist');
require_once 'global.php';
$lang = load_lang($lang, 'usermanager' );
$stats = $lang['FriendHeaderTitle'];
PageLoad();
LoadFooter();

function PageLoad(){
	global $FavName_id,$UserFavName,$redcolor,$dv,$db,$Redcolor,$hometitle,$stats,$lang,$userid,$membername,$_DCACHE,$imgurl;
	$arrNavMenu = array(
		0=>array('linktitle'=>$lang['HeaderTitle1'],'linkurl'=>'usermanager.php')
		,1=>array('linktitle'=>$stats)
	);
	LoadStyleJs();
	if( $userid <= 0 ){
		showmsg($lang['MustLogin']);
		exit;
	}
	$redcolor = $_DCACHE['stylecss'][19];
	if( $query = $db->query("SELECT userfav FROM {$dv}user WHERE userid={$userid}") ){
		if( $result = $db->fetch_row($query) ){
			$db->free_result($query);
		}
	}
	if( !empty($result) && is_array($result) ){
		$UserFavName = $result[0];
		unset($result);
	}
	$FavName_id = intval($GLOBALS['fid']);
	require_once printout('usermanager.navmenu');
	switch($GLOBALS['action']){
		case "creat":
			creat_fav();
			break;
		case "editfav":
			save_fav();
			break;
		case "favdel":
			del_fav();
			break;
		case "addF":
			saveF();
			break;
		case "saveF":
			saveF();
			break;
		case $lang['tpl.friend.str54']:
			MovFriend();
			break;
		case $lang['tpl.friend.str55']:
			DelFriend();
			break;
		case $lang['tpl.friend.str56']:
			AllDelFriend();
			break;
	}
	MainFunc();
}//end function PageLoad

function MainFunc(){
	global $userid,$db,$dv,$fsetting,$FavName_id,$redcolor,$lang,$UserFavName,$_DCACHE,$imgurl;
	$TableWidth = $_DCACHE['stylecss'][1];
	$fav_name = $UserFavName;
	$UserFavName = explode(',',$UserFavName);
	$CurrentPage = intval($GLOBALS['page']);
	$page_count = 0;
	if( $GLOBALS['action'] == 'search' ){
		if( is_numeric($GLOBALS['fid']) ){
			$SearchStr = ' AND f_mod='.intval($GLOBALS['fid']).' ';
		}
		if( $GLOBALS['SearchInfo'] != '' ){
			$SearchStr .= " AND f_friend='{$GLOBALS['SearchInfo']}' ";
		}
	}
	$HomePage_Img = ImgSrc($imgurl.$lang['tpl.friend.pic15']);
	$Oicq_img = ImgSrc($imgurl.$lang['tpl.friend.pic16']);
	$Msg_Img = ImgSrc($imgurl.$lang['tpl.friend.pic17']);
	$Sms_Img = ImgSrc($imgurl.$lang['tpl.friend.pic18']);
	if ($fsetting['otherpagesize'] < 1) {
		$fsetting['otherpagesize'] = 20;
	}
	$PageNum = $PageListNum = intval($fsetting['otherpagesize']);
	$totalrec = intval($db->scalar("SELECT COUNT(f_id) FROM {$dv}friend where f_userid={$userid} {$SearchStr}"));
	$Pcount = ceil( (float)$totalrec / (float)$PageListNum );
	if( $CurrentPage > $Pcount ) $CurrentPage = $Pcount;
	if( $CurrentPage < 1 ) $CurrentPage = 1;
	if( $query = $db->query("SELECT f.f_id,f.f_userid,f.f_friend,f_mod,u.useremail,u.userim FROM {$dv}friend f INNER JOIN {$dv}user u ON  f.f_friend=u.username WHERE f.f_userid={$userid} {$SearchStr} ORDER BY f.f_addtime DESC ", array('absolutePage'=>$CurrentPage,'pageSize'=>$PageListNum) ) ){
		while( $arrFriendList[] = $db->fetch_array($query) );
		if( !empty($arrFriendList) ) $db->free_result($query);
		unset($arrFriendList[count($arrFriendList)-1]);
	}
	require printout('friendlist');
}//end function MainFunc

//创建分组
function creat_fav(){
	global $lang,$dv,$db,$membername,$userid;
	$fav_name = str_replace(',','',trim($GLOBALS['FavName']));
	if( $fav_name == '' ){
		showmsg($lang['tpl.friend.strings49']);
		exit;
	} elseif( Dv_StrLength($fav_name) > 12 ){
		showmsg($lang['tpl.friend.strings42']);
		exit;
	} else{
		$fav_name = ','.htmlspecialchars($fav_name);
		$db->query("UPDATE {$dv}user SET userfav=CONCAT(userfav,'{$fav_name}') WHERE userid={$userid}");
		showmsg($lang['tpl.friend.strings48'],1,$_SERVER['HTTP_REFERER']);
	}
}//end function creat_fav

//修改分组
function save_fav(){
	global $UserFavName,$lang,$dv,$db,$membername,$userid;
	$Old_FavName = explode(',',$UserFavName);
	$fav_name = trim($GLOBALS['fav_name']);
	if( preg_match('/(^,)|(,$)/i',$fav_name) != 0 ){
		showmsg($lang['tpl.friend.strings49']);
		exit;
	}
	$arrFavName = explode(',',$fav_name);
	if( Dv_StrLength($fav_name) > 250 || count($arrFavName) > 10 || count($arrFavName) < count($Old_FavName) ){
		showmsg($lang['tpl.friend.strings42']);
		exit;
	}
	if( str_replace(',','',$fav_name) == '' ){
		showmsg($lang['tpl.friend.strings49']);
		exit;
	}
	$db->query("UPDATE {$dv}user SET userfav='".htmlspecialchars($fav_name)."' WHERE userid={$userid}");
	showmsg($lang['tpl.friend.strings48'],1,$_SERVER['HTTP_REFERER']);
}//end function save_fav

//删除分组
function del_fav(){	
	global $FavName_id,$UserFavName,$lang,$dv,$db,$membername,$userid;
	if( $FavName_id <= 2 ){//默认分组不能被删除！
		showmsg($lang['tpl.friend.strings49']);
		exit;
	}
	$Old_FavName = explode(',',$UserFavName);
	$Del_FavName = $Old_FavName[$FavName_id];
	unset($Old_FavName[$FavName_id]);
	$New_FavName = trim(implode(',',$Old_FavName));
	$New_FavName = preg_replace('/(^,)|(,$)|(,){2,}/i','',$New_FavName);
	$db->query("UPDATE {$dv}user SET userfav='{$New_FavName}' WHERE userid={$userid}");
	$db->query("DELETE FROM {$dv}friend WHERE f_userid={$userid} AND f_mod={$FavName_id}");
	showmsg($lang['tpl.friend.strings46'],1,$_SERVER['HTTP_REFERER']);
}//end function del_fav

//保存添加好友
function saveF(){
	global $lang,$dv,$db,$membername,$userid;
	if( $GLOBALS['myFriend'] == '' ){
		showmsg($lang['tpl.friend.strings35']);
		exit;
	} else{
		$incept = explode(',',urldecode(trim($GLOBALS['myFriend'])));
	}
	$Fav_id = intval($GLOBALS['Fav_id']);
	if( $Fav_id < 0 ){
		$fid = intval($GLOBALS['fid']);
		if( $fid >= 0 ) $Fav_id = $fid;
		else $Fav_id = 0;
	}
	$i = 0;
	$incept = i_addslashes($incept, 1);	//	urldecode 可以饶过 addslashes 从而达到 SQL 注入的目的，因此，必须对 $incept 进行过滤
	foreach( $incept as $val ){
		if( $i > 4 ){
			showmsg($lang['tpl.friend.strings42']);
			exit;
		}
		$Friend_Name = trim($val);
		$username = $db->scalar("SELECT username FROM {$dv}user WHERE username='{$Friend_Name}'");
		if( $username !== false ){
			$Friend_Name = $username;
		} else{
			showmsg(str_replace('{$NoUser}',$Friend_Name,$lang['tpl.friend.strings41']));
			exit;
		}
		if( $membername == $Friend_Name ){
			showmsg($lang['tpl.friend.strings40']);
			exit;
		}
		$result = $db->scalar("SELECT f_id FROM {$dv}friend WHERE f_userid={$userid} AND f_friend='{$Friend_Name}'");
		if( $result === false ){
			$db->query("INSERT INTO {$dv}friend (f_userid,f_username,f_friend,f_addtime,f_mod) VALUES ({$userid},'{$membername}','{$Friend_Name}','".TIME_NOW."',{$Fav_id})");
		} else{
			showmsg(str_replace('{$IsUser}',$Friend_Name,$lang['tpl.friend.strings44']));
			exit;
		}
		$i++;
	}
	showmsg($lang['tpl.friend.strings43'],1,$_SERVER['HTTP_REFERER']);
}//end function saveF

//批量移动
function MovFriend(){
	global $lang,$dv,$db,$membername,$userid,$action;
	if( !is_numeric($GLOBALS['Fav_id']) ){
		showmsg($lang['error.friend.strings1']);
		exit;
	}
	$Fav_id = intval($GLOBALS['Fav_id']);
	if($GLOBALS['id']==''){
		showmsg($lang['error.friend.strings1']);
		exit;
	}
	else{
		$fidList = implode(',',$GLOBALS['id']);
	}
	$db->query("UPDATE {$dv}friend SET f_mod={$Fav_id} WHERE f_userid={$userid} AND f_id IN({$fidList})");
	showmsg($lang['tpl.friend.strings47'],1,$_SERVER['HTTP_REFERER']);
}//end function MovFriend

//删除好友
function DelFriend(){
	global $lang,$dv,$db,$membername,$userid,$action;
	if($GLOBALS['id']==''){
		showmsg($lang['error.friend.strings1']);
		exit;
	}
	else{
		$delIDList = implode(',',$GLOBALS['id']);
	}
	$db->query("DELETE FROM {$dv}friend WHERE f_userid={$userid} AND f_id IN({$delIDList})");
	showmsg($lang['tpl.friend.strings46'],1,$_SERVER['HTTP_REFERER']);
}//end function DelFriend

//清空好友
function AllDelFriend(){
	global $lang,$dv,$db,$membername,$userid,$action;
	$db->query("DELETE FROM {$dv}friend WHERE f_userid={$userid}");
	showmsg($lang['tpl.friend.strings45'],1,$_SERVER['HTTP_REFERER']);
	$_SESSION['ispost'] = '0';
}//end function AllDelFriend

//图片输出
function ImgSrc($str){
	if( $str == "" ) return;
	else return "<img src=\"{$str}\" border=\"0\">";
}//end function

?>