[PHP] 纯文本查看 复制代码
<?php
/**
* [Discuz!] (C)2001-2099 Comsenz Inc.
* This is NOT a freeware, use is subject to license terms
*
* $Id: function_friend.php 26635 2011-12-19 01:59:13Z zhangguosheng $
*/
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
/**
* 获取指定用户的友人列表
*
* @param int $uid 用户ID
* @param int $limit 返回结果的数量限制
* @param int $start 返回结果的起始位置(默认为0)
* @return array 返回友人列表,键为友人的ID,值为友人信息
*/
function friend_list($uid, $limit, $start=0) {
$list = array();
$query = C::t('home_friend')->fetch_all_by_uid($uid, $start, $limit, true);
foreach($query as $value) {
$list[$value['fuid']] = $value;
}
return $list;
}
/**
* 获取当前用户的友人分组列表
*
* @return array 返回友人分组列表
*/
function friend_group_list() {
global $_G;
$space = array('uid' => $_G['uid']);
space_merge($space, 'field_home');
$groups = array();
$spacegroup = empty($space['privacy']['groupname'])?array():$space['privacy']['groupname'];
for($i = 0; $i < $_G['setting']['friendgroupnum']; $i++) {
if($i == 0) {
$groups[0] = lang('friend', 'friend_group_default');
} else {
if(!empty($spacegroup[$i])) {
$groups[$i] = $spacegroup[$i];
} else {
if($i<8) {
$groups[$i] = lang('friend', 'friend_group_'.$i);
} else {
$groups[$i] = lang('friend', 'friend_group_more', array('num'=>$i));
}
}
}
}
return $groups;
}
/**
* 检查指定用户是否为当前用户的友人
*
* @param mixed $touids 可以是单个用户ID或用户ID数组
* @param int $isfull 是否获取完整友人信息,默认为0(只检查友人关系是否存在)
* @return mixed 如果是数组形式的$touids,返回布尔值;如果是单个用户ID,返回布尔值或友人信息数组
*/
function friend_check($touids, $isfull = 0) {
global $_G;
if(empty($_G['uid'])) return false;
if(is_array($touids)) {
$query = C::t('home_friend')->fetch_all_by_uid_fuid($_G['uid'], $touids);
foreach($query as $value) {
$touid = $value['fuid'];
$var = "home_friend_{$_G['uid']}_{$touid}";
$fvar = "home_friend_{$touid}_{$_G['uid']}";
$_G[$var] = $_G[$fvar] = true;
if($isfull) {
$fvarinfo = "home_friend_info_{$touid}_{$_G['uid']}";
$_G[$fvarinfo] = $value;
}
}
if(count($query) != count($touids)) {
return false;
} else {
return true;
}
} else {
$touid = $touids;
$var = "home_friend_{$_G['uid']}_{$touid}";
$fvar = "home_friend_{$touid}_{$_G['uid']}";
if(!isset($_G[$var])) {
$query = C::t('home_friend')->fetch_all_by_uid_fuid($_G['uid'], $touid);
$friend = isset($query[0]) ? $query[0] : '';
if($friend) {
$_G[$var] = $_G[$fvar] = true;
if($isfull) {
$fvarinfo = "home_friend_info_{$touid}_{$_G['uid']}";
$_G[$fvarinfo] = $friend;
}
} else {
$_G[$var] = $_G[$fvar] = false;
}
}
return $_G[$var];
}
}
/**
* 检查指定用户是否向当前用户发送了友人请求
*
* @param int $touid 目标用户ID
* @return bool 返回目标用户是否发送了友人请求
*/
function friend_request_check($touid) {
global $_G;
$var = "home_friend_request_{$touid}";
if(!isset($_G[$var])) {
$result = C::t('home_friend_request')->fetch_by_uid_fuid($_G['uid'], $touid);
$_G[$var] = $result?true:false;
}
return $_G[$var];
}
/**
* 添加友人关系
*
* @param int $touid 目标用户ID
* @param int $gid 友人分组ID,默认为0
* @param string $note 添加友人的备注信息,默认为空
* @return int 返回执行结果,-2表示目标用户是当前用户,-1表示目标用户已发送添加请求,1表示添加成功
*/
function friend_add($touid, $gid=0, $note='') {
global $_G;
if($touid == $_G['uid']) return -2;
if(friend_check($touid)) return -2;
include_once libfile('function/stat');
$freind_request = C::t('home_friend_request')->fetch_by_uid_fuid($_G['uid'], $touid);
if($freind_request) {
// 处理已存在的友人请求,建立友人关系
$setarr = array(
'uid' => $_G['uid'],
'fuid' => $freind_request['fuid'],
'fusername' => addslashes($freind_request['fusername']),
'gid' => $gid,
'dateline' => $_G['timestamp']
);
C::t('home_friend')->insert($setarr);
friend_request_delete($touid);
friend_cache($_G['uid']);
$setarr = array(
'uid' => $touid,
'fuid' => $_G['uid'],
'fusername' => $_G['username'],
'gid' => $freind_request['gid'],
'dateline' => $_G['timestamp']
);
C::t('home_friend')->insert($setarr);
addfriendlog($_G['uid'], $touid);
friend_cache($touid);
updatestat('friend');
} else {
// 处理目标用户未发送请求的情况,插入新的友人请求
$to_freind_request = C::t('home_friend_request')->fetch_by_uid_fuid($touid, $_G['uid']);
if($to_freind_request) {
return -1;
}
$setarr = array(
'uid' => $touid,
'fuid' => $_G['uid'],
'fusername' => $_G['username'],
'gid' => $gid,
'note' => $note,
'dateline' => $_G['timestamp']
);
C::t('home_friend_request')->insert($setarr);
updatestat('addfriend');
}
return 1;
}
/**
* 添加好友
*
* @param int $touid 目标用户的ID
* @param string $tousername 目标用户的用户名
* @param bool $checkrequest 是否检查好友请求,默认为true
* @return bool 返回false表示操作失败,否则成功
*/
function friend_make($touid, $tousername, $checkrequest=true) {
global $_G;
// 检查是否尝试添加自己为好友
if($touid == $_G['uid']) return false;
// 如果需要检查好友请求
if($checkrequest) {
// 删除目标用户向当前用户发出的好友请求
$to_freind_request = C::t('home_friend_request')->fetch_by_uid_fuid($touid, $_G['uid']);
if($to_freind_request) {
C::t('home_friend_request')->delete_by_uid_fuid($touid, $_G['uid']);
}
// 删除当前用户向目标用户发出的好友请求
$to_freind_request = C::t('home_friend_request')->fetch_by_uid_fuid($_G['uid'], $touid);
if($to_freind_request) {
C::t('home_friend_request')->delete_by_uid_fuid($_G['uid'], $touid);
}
}
// 插入两条好友记录,互为好友
$insertarray = array(
'uid' => $touid,
'fuid' => $_G['uid'],
'fusername' => $_G['username'],
'dateline' => $_G['timestamp'],
);
C::t('home_friend')->insert($insertarray, false, true);
$insertarray = array(
'uid' => $_G['uid'],
'fuid' => $touid,
'fusername' => $tousername,
'dateline' => $_G['timestamp'],
);
C::t('home_friend')->insert($insertarray, false, true);
// 添加好友操作日志
addfriendlog($_G['uid'], $touid);
// 更新统计信息
include_once libfile('function/stat');
updatestat('friend');
// 更新好友缓存
friend_cache($touid);
friend_cache($_G['uid']);
}
/**
* 添加好友日志
*
* @param int $uid 发起操作的用户ID
* @param int $touid 被操作的用户ID
* @param string $action 操作类型,默认为'add'
* @return bool 返回true表示日志添加成功,false表示添加失败
*/
function addfriendlog($uid, $touid, $action = 'add') {
global $_G;
// 检查是否提供了有效的用户ID
if($uid && $touid) {
$flog = array(
'uid' => $uid > $touid ? $uid : $touid,
'fuid' => $uid > $touid ? $touid : $uid,
'dateline' => $_G['timestamp'],
'action' => $action
);
DB::insert('home_friendlog', $flog, false, true);
return true;
}
return false;
}
/**
* 增加好友数量统计
*
* @param int $touid 目标用户的ID
*/
function friend_addnum($touid) {
global $_G;
// 检查是否为当前用户ID,且不为自身
if($_G['uid'] && $_G['uid'] != $touid) {
C::t('home_friend')->update_num_by_uid_fuid(1, $_G['uid'], $touid);
}
}
/**
* 更新好友缓存
*
* @param int $touid 用户ID
*/
function friend_cache($touid) {
global $_G;
// 合并用户空间信息
$tospace = array('uid' => $touid);
space_merge($tospace, 'field_home');
// 获取过滤组ID
$filtergids = empty($tospace['privacy']['filter_gid'])?array():$tospace['privacy']['filter_gid'];
$uids = array();
$count = 0;
$fcount = 0;
// 获取所有好友信息
$query = C::t('home_friend')->fetch_all_by_uid($touid, 0, 0, true);
foreach($query as $value) {
// 排除自身
if($value['fuid'] == $touid) continue;
// 达到缓存好友数量上限
if($fcount > 200) {
$count = count($query);
break;
} elseif(empty($filtergids) || !in_array($value['gid'], $filtergids)) {
$uids[] = $value['fuid'];
$fcount++;
}
$count++;
}
// 更新用户的好友列表缓存和好友数量
C::t('common_member_field_home')->update($touid, array('feedfriend'=>implode(',', $uids)));
C::t('common_member_count')->update($touid, array('friends'=>$count));
}
/**
* 删除好友请求
* @param int $touid 目标用户的UID
* @return bool 删除操作的结果,成功返回true,失败返回false
*/
function friend_request_delete($touid) {
global $_G;
// 通过UID和目标UID删除好友请求
return C::t('home_friend_request')->delete_by_uid_fuid($_G['uid'], $touid);
}
/**
* 删除好友
* @param int $touid 目标用户的UID
* @return bool 删除操作的结果,成功返回true,失败返回false
* 注:如果目标用户不是好友,则不执行删除操作并返回false。
*/
function friend_delete($touid) {
global $_G;
// 检查是否为好友,如果不是则直接返回false
if(!friend_check($touid)) return false;
// 删除好友关系
C::t('home_friend')->delete_by_uid_fuid_dual($_G['uid'], $touid);
// 如果删除操作影响了行数(即删除成功),则进行后续操作
if(DB::affected_rows()) {
// 添加删除好友的日志
addfriendlog($_G['uid'], $touid, 'delete');
// 更新好友缓存
friend_cache($_G['uid']);
friend_cache($touid);
}
}
?>