<?php



/**

 *      [Discuz!] (C)2001-2099 Comsenz Inc.

 *      This is NOT a freeware, use is subject to license terms

 *

 *      $Id: space_friend.php 29610 2012-04-23 03:14:51Z liulanbo $

 */



if(!defined('IN_DISCUZ')) {

	exit('Access Denied');

}



$perpage = '';

$perpage = mob_perpage($perpage);



$space = getuserbyuid($_GET['uid']);



$list = $ols = $fuids = array();

$count = 0;

$page = empty($_GET['page'])?0:intval($_GET['page']);

if($page<1) $page = 1;

$start = ($page-1)*$perpage;



if(empty($_GET['view']) || $_GET['view'] == 'all') $_GET['view'] = 'me';



ckstart($start, $perpage);



if($_GET['view'] == 'online') {

	$theurl = "home.php?mod=space&uid=$space[uid]&do=friend&view=online";

	$actives = array('me'=>' class="a"');



	space_merge($space, 'field_home');

	$onlinedata = array();

	$wheresql = '';

	if($_GET['type']=='near') {

		$theurl = "home.php?mod=space&uid=$space[uid]&do=friend&view=online&type=near";

		if(($count = C::app()->session->count_by_ip($_G['clientip']))) {

			$onlinedata = C::app()->session->fetch_all_by_ip($_G['clientip'], $start, $perpage);

		}

	} elseif($_GET['type']=='friend') {

		$theurl = "home.php?mod=space&uid=$space[uid]&do=friend&view=online&type=friend";

		if(!empty($space['feedfriend'])) {

			$onlinedata = C::app()->session->fetch_all_by_uid(explode(',', $space['feedfriend']), $start, $perpage);

		}

		$count = count($onlinedata);

	} elseif($_GET['type']=='member') {

		$theurl = "home.php?mod=space&uid=$space[uid]&do=friend&view=online&type=member";

		$wheresql = " WHERE uid>0";

		if(($count = C::app()->session->count(1))) {

			$onlinedata = C::app()->session->fetch_member(1, 2, $start, $perpage);

		}

	} else {

		$_GET['type']=='all';

		$theurl = "home.php?mod=space&uid=$space[uid]&do=friend&view=online&type=all";

		if(($count = C::app()->session->count_invisible(0))) {

			$onlinedata = C::app()->session->fetch_member(0, 2, $start, $perpage);

		}

	}



	if($count) {



		foreach($onlinedata as $value) {

			if($_GET['type']=='near') {

				if($value['uid'] == $space['uid']) {

					$count = $count-1;

					continue;

				}

			}



			if(!$value['invisible']) $ols[$value['uid']] = $value['lastactivity'];

			$list[$value['uid']] = $value;

			$fuids[$value['uid']] = $value['uid'];

		}



		if($fuids) {

			require_once libfile('function/friend');

			friend_check($space['uid'], $fuids);



			$fieldhome = C::t('common_member_field_home')->fetch_all($fuids);

			foreach(C::t('common_member')->fetch_all($fuids) as $uid => $value) {

				$value = array_merge($value, $fieldhome[$uid]);

				$value['isfriend'] = $uid==$space['uid'] || $_G["home_friend_".$space['uid'].'_'.$uid] ? 1 : 0;

				$list[$uid] = array_merge($list[$uid], $value);

			}

		}

	}

	$multi = multi($count, $perpage, $page, $theurl);



} elseif($_GET['view'] == 'visitor' || $_GET['view'] == 'trace') {



	$theurl = "home.php?mod=space&uid=$space[uid]&do=friend&view=$_GET[view]";

	$actives = array('me'=>' class="a"');



	if($_GET['view'] == 'visitor') {

		$count = C::t('home_visitor')->count_by_uid($space['uid']);

	} else {

		$count = C::t('home_visitor')->count_by_vuid($space['uid']);

	}

	if($count) {

		if($_GET['view'] == 'visitor') {

			$visitors = C::t('home_visitor')->fetch_all_by_uid($space['uid'], $start, $perpage);

		} else {

			$visitors = C::t('home_visitor')->fetch_all_by_vuid($space['uid'], $start, $perpage);

		}

		foreach($visitors as $value) {

			if($_GET['view'] == 'visitor') {

				$value['uid'] = $value['vuid'];

				$value['username'] = $value['vusername'];

			}

			$fuids[] = $value['uid'];

			$list[$value['uid']] = $value;

		}

	}

	$multi = multi($count, $perpage, $page, $theurl);



} elseif($_GET['view'] == 'blacklist') {//黑名单列表

    

	$count = C::t('home_blacklist')->count_by_uid_buid($space['uid']);

        //判断有无黑名单

        $list = array();

	if($count) {

		$backlist = C::t('home_blacklist')->fetch_all_by_uid($space['uid'], $start,$perpage);

		$members = C::t('common_member')->fetch_all(array_keys($backlist));

                if(!empty($members)){

                        foreach($members as $k=>$v){

                                $list[$k]['uid'] = $v['uid'];

                                $list[$k]['username'] = $v['username'];

                        }

                        sort($list);

                }

        }

        odz_result(array('list'=>$list));



}else{

	$theurl = "home.php?mod=space&uid=$space[uid]&do=$do";

	$actives = array('me'=>' class="a"');



	$_GET['view'] = 'me';



	$querydata = array();

	if($space['self']) {

		require_once libfile('function/friend');

		$groups = friend_group_list();

		$group = !isset($_GET['group'])?'-1':intval($_GET['group']);

		if($group > -1) {

			$querydata['gid'] = $group;

			$theurl .= "&group=$group";

		}

	}

	if($_GET['searchkey']) {

		require_once libfile('function/search');

		$querydata['searchkey'] = $_GET['searchkey'];

		$theurl .= "&searchkey=$_GET[searchkey]";

	}



	$count = C::t('home_friend')->fetch_all_search($space['uid'], $querydata['gid'], $querydata['searchkey'], true);

	$membercount = C::t('common_member_count')->fetch($_G['uid']);

	$friendnum = $membercount['friends'];

	unset($membercount);

	if($count) {



		$query = C::t('home_friend')->fetch_all_search($space['uid'], $querydata['gid'], $querydata['searchkey'], false, $start, $perpage);

		foreach($query as $value) {

			$value['uid'] = $value['fuid'];

			$_G["home_friend_".$space['uid'].'_'.$value['uid']] = $value['isfriend'] = 1;

			$fuids[$value['uid']] = $value['uid'];

			$list[$value['uid']] = $value;

		}

	} elseif(!$friendnum) {

		if(($specialuser_count = C::t('home_specialuser')->count_by_status(1))) {

			foreach(C::t('home_specialuser')->fetch_all_by_status(1, 7) as $value) {

				if($_G['uid'] !== $value['uid']) {

					$fuids[$value['uid']] = $value['uid'];

					$specialuser_list[$value['uid']] = $value;

				}

				if(count($fuids) >= 6) {

					break;

				}

			}

			$specialuser_list = getfollowflag($specialuser_list);



		}

		if(($online_count = C::app()->session->count(1)) > 1) {

			$oluids = $online_list = array();

			foreach(C::app()->session->fetch_member(1, 2, 7) as $value) {

				if($value['uid'] != $_G['uid'] && count($oluids) <= 6) {

					$fuids[$value['uid']] = $value['uid'];

					$oluids[$value['uid']] = $value['uid'];

					$online_list[$value['uid']] = $value;

				}

			}

			$online_list = getfollowflag($online_list);



			$fieldhome = C::t('common_member_field_home')->fetch_all($oluids, false, 0);

			foreach(C::t('common_member')->fetch_all($oluids, false, 0) as $uid => $value) {

				$value = array_merge($value, $fieldhome[$uid]);

				$online_list[$uid] = array_merge($online_list[$uid], $value);

			}



		}

	}



	if($space['self']) {

		$groupselect = array($group => ' class="a"');



		$maxfriendnum = checkperm('maxfriendnum');

		if($maxfriendnum) {

			$maxfriendnum = checkperm('maxfriendnum') + $space['addfriend'];

		}

	}

}



if($fuids) {

	foreach(C::app()->session->fetch_all_by_uid($fuids) as $value) {

		if(!$value['invisible']) {

			$ols[$value['uid']] = $value['lastactivity'];

		} elseif($list[$value['uid']] && !in_array($_GET['view'], array('me', 'trace', 'blacklist'))) {

			unset($list[$value['uid']]);

			$count = $count - 1;

		}

	}

	if($_GET['view'] != 'me') {

		require_once libfile('function/friend');

		friend_check($fuids);

	}

	if($list) {

		$fieldhome = C::t('common_member_field_home')->fetch_all($fuids);

		foreach(C::t('common_member')->fetch_all($fuids) as $uid => $value) {

			$value = array_merge($value, $fieldhome[$uid]);

			$value['isfriend'] = $uid==$space['uid'] || $_G["home_friend_".$space['uid'].'_'.$uid] ? 1 : 0;

			if(empty($list[$uid])) $list[$uid] = array();

			$list[$uid] = array_merge($list[$uid], $value);

		}

		$profile = C::t('common_member_profile')->fetch_all($fuids);

		foreach ($profile as $uid => $row) {

			$list[$uid]['field1'] = $row['field1'];

		}

	}

}



if($list) {

	$list = getfollowflag($list);

}





$friends = array();

$result  = array();

foreach($list as $row) {

    $onlinestatus = 0;

    if($ols[$row['uid']]) {

        $onlinestatus = $ols[$row['uid']];

    }

    

    $friends[] = array(

        'uid' => $row['uid'],

        'username' => $row['username']?$row['username']:$row['fusername'],

        'onlinestatus' => $onlinestatus,

        'regdate'=>date('Y-m-d',$row['regdate']),

        'avatar' => avatar($row['uid'], 'small', true)

    );

}

if(!empty($friends)){

    

        $friends_data =array();

        foreach($friends as $k=>$v){

                $uids[] = $v['uid']; 

        }

        $query = DB::query('SELECT c.uid,c.gender,c.birthyear,c.birthmonth,c.birthday ,c.affectivestatus,f.sightml  FROM '.DB::table('common_member_profile').' as c inner join '.DB::table('common_member_field_forum').' as f '.

            " on c.uid=f.uid WHERE c.uid in (".implode(',', $uids).")");

        while($row = DB::fetch($query)){

                foreach($friends as $k => $v ){

                        if( $v['uid'] == $row['uid']){

                                $friends[$k] = array_merge($v,$row); 

                        }

                }

        }

        

        foreach($friends as $k => $v ){

                $result[] = array(

                        'uid' => $v['uid'],

                        'gender' => empty($v['gender'])?'0':$v['gender'],

                        'age' => $v["birthyear"]?age($v["birthyear"].'-'.$v["birthmonth"].'-'.$v["birthday"]):'',

                        'affectivestatus' => $v['affectivestatus']?$v['affectivestatus']:odz_lang('secrect'),

                        'sightml' => $v['sightml']?(strip_tags($v['sightml'])?strip_tags($v['sightml']):odz_lang('no_message_left')):odz_lang('no_message_left'),

                        'username'=> $v['username'],

                        'onlinestatus'=>empty($v['onlinestatus'])?'0':$v['onlinestatus'],

                        'regdate' => $v['regdate'],

                        'avatar'=> $v['avatar'],

                        'birthyear'=>empty($v["birthyear"])?'0':$v["birthyear"],

                        'birthmonth'=>empty($v["birthmonth"])?'0':$v["birthyear"],

                        'birthday'=>empty($v["birthday"])?'0':$v["birthday"],

                        'isfriends'=>'1',

                );

        }

}



include_once (DISCUZ_ROOT.'source/discuz_version.php');

if(!empty($_G['uid'])){

        if(DISCUZ_VERSION == 'X2.5'){

            $num =  DB::fetch_all('SELECT count(*) FROM '.DB::table('home_notification')." WHERE uid = '$_G[uid]' AND type='friend' AND new=1 AND from_idtype='friendrequest' ");

        }else{

            $num =  DB::fetch_all('SELECT count(*) FROM '.DB::table('home_notification')." WHERE uid = '$_G[uid]' AND type='friend' AND new=1 AND category=2 AND from_idtype='friendrequest' ");

        }

}        

        $num = $num[0]['count(*)']?(string)$num[0]['count(*)']:'';

        

odz_result(array('list'=>$result,'num'=>$num));



function getfollowflag($data) {

	global $_G;

	if($data) {

		$follows = C::t('home_follow')->fetch_all_by_uid_followuid($_G['uid'], array_keys($data));

		foreach($data as $uid => $value) {

			$data[$uid]['follow'] = isset($follows[$uid]) ? 1 : 0;

		}

	}

	return $data;

}

?>