<?php 
/**
#	Project: PHPDISK File Storage Solution
#	This is NOT a freeware, use is subject to license terms.
#
#	Site: http://www.phpdisk.com
#
#	$Id: buddy.inc.php 67 2016-08-05 07:26:31Z along $
#
#	Copyright (C) 2008-2016 PHPDisk Team. All Rights Reserved.
#
*/

if(!defined('IN_PHPDISK') || !defined('IN_MYDISK')) {
	exit('[PHPDisk] Access Denied');
}

switch($action){
	case 'corp_depart':
		$depart_id = (int)gpc('depart_id','G',0);

		if($depart_id){
			$depart_name = @$db->result_first("select depart_name from {$tpf}departs where depart_id='$depart_id'");
			$buddy_title = "企业架构 - $depart_name";

			$sql_do = "{$tpf}users where depart_id='$depart_id' and userid<>'$pd_uid'";

			$rs = $db->fetch_one_array("select count(*) as total_num from {$sql_do}");
			$total_num = $rs['total_num'];
			$start_num = ($pg-1) * $perpage;

			$q = $db->query("select * from {$sql_do} order by userid desc limit $start_num,$perpage");
			$buddys_arr = array();
			while($rs = $db->fetch_array($q)){
				$rs['userid'] = ($action =='mybuddy') ? $rs['touserid'] : $rs['userid'];
				$rs['a_viewinfo'] = urr("mydisk","item=buddy&action=viewinfo&userid=".$rs['userid']);
				$rs['a_sendmsg'] = urr("mydisk","item=message&menu=profile&action=sendmsg&username=".rawurlencode($rs['username']));
				$rs['a_delbuddy'] = urr("mydisk","item=buddy&menu=buddy&action=delbuddy&userid={$rs['touserid']}");
				$rs['a_addbuddy'] = urr("mydisk","item=buddy&menu=buddy&action=s_addbuddy&userid={$rs['userid']}");
				$buddys_arr[] = $rs;
			}
			$db->free($q);
			unset($rs);
			
		}else{
			$buddy_title = "企业架构";
		}

		require_once template_echo('buddy',$user_tpl_dir);
		break;
	case 'mydepart':
	case 'mybuddy':
	case 'whobuddy':
	case 'invite_success':
		if($action=='mybuddy'){
			$buddy_title = __('buddy_title_mybuddy');
		}elseif($action=='whobuddy'){
			$buddy_title = __('buddy_title_whobuddy');
		}elseif($action=='invite_success'){
			$buddy_title = __('buddy_title_invite_success');
		}elseif($action=='mydepart'){
			$buddy_title = '我的同事';
		}

		if($task =='addbuddy'){
			form_auth(gpc('formhash','P',''),formhash());

			$buddy_name = trim(gpc('buddy_name','P',''));
			$go_back = true;

			if($buddy_name){
				$rs = $db->fetch_one_array("select userid from {$tpf}users where username='$buddy_name'");
				if($rs['userid']){
					$rs2 = $db->fetch_one_array("select count(*) as total from {$tpf}buddys where touserid='{$rs['userid']}' and userid='$pd_uid'");
					if($rs['userid'] == $pd_uid){
						$sysmsg[] = __('cannot_add_self');
					}elseif($rs2['total']){
						$sysmsg[] = __('already_buddy');
					}else{
						$ins = array(
						'userid' => $pd_uid,
						'touserid' => $rs['userid'],
						'in_time' => $timestamp,
						);
						$db->query("insert into {$tpf}buddys set ".$db->sql_array($ins).";");
						$sysmsg[] = __('addbuddy_success');
						$go_back = false;
					}
				}else{
					$sysmsg[] = __('username_not_found');
				}
				if($go_back){
					redirect('back',$sysmsg);
				}else{
					redirect(urr("mydisk","item=buddy&menu=buddy&action=mybuddy"),$sysmsg);
				}
			}else{
				$sysmsg[] = __('username_not_found');
				redirect('back',$sysmsg);
			}
		}else{
			$perpage = 20;
			if($action=='mydepart'){
				$depart_id = (int)get_profile($pd_uid,'depart_id');
				$sql_do = "{$tpf}users where depart_id='$depart_id' and userid<>'$pd_uid'";

				$rs = $db->fetch_one_array("select count(*) as total_num from {$sql_do}");
				$total_num = $rs['total_num'];
				$start_num = ($pg-1) * $perpage;

				$q = $db->query("select * from {$sql_do} order by userid desc limit $start_num,$perpage");
				$buddys_arr = array();
				while($rs = $db->fetch_array($q)){
					$rs['userid'] = ($action =='mybuddy') ? $rs['touserid'] : $rs['userid'];
					$rs['a_viewinfo'] = urr("mydisk","item=buddy&action=viewinfo&userid=".$rs['userid']);
					$rs['a_sendmsg'] = urr("mydisk","item=message&menu=profile&action=sendmsg&username=".rawurlencode($rs['username']));
					$rs['a_delbuddy'] = urr("mydisk","item=buddy&menu=buddy&action=delbuddy&userid={$rs['touserid']}");
					$rs['a_addbuddy'] = urr("mydisk","item=buddy&menu=buddy&action=s_addbuddy&userid={$rs['userid']}");
					$rs['in_time'] = $rs['in_time'] ? date("Y-m-d H:i",$rs['in_time']) : '--';
					$buddys_arr[] = $rs;
				}
				$db->free($q);
				unset($rs);
			}else{
				if($action =='mybuddy'){
					$sql_do = " {$tpf}buddys b, {$tpf}users u where b.userid='$pd_uid' and b.touserid=u.userid";
				}elseif($action =='invite_success'){
					$sql_do = " {$tpf}invitelog b, {$tpf}users u where b.userid='$pd_uid' and b.touserid=u.userid";
				}else{
					$sql_do = " {$tpf}buddys b, {$tpf}users u where b.touserid='$pd_uid' and b.userid=u.userid";
				}
				$rs = $db->fetch_one_array("select count(*) as total_num from {$sql_do}");
				$total_num = $rs['total_num'];
				$start_num = ($pg-1) * $perpage;

				$q = $db->query("select b.*,u.username from {$sql_do} order by bdid desc limit $start_num,$perpage");
				$buddys_arr = array();
				while($rs = $db->fetch_array($q)){
					$rs['userid'] = ($action =='mybuddy') ? $rs['touserid'] : $rs['userid'];
					$rs['a_viewinfo'] = urr("mydisk","item=buddy&action=viewinfo&userid=".$rs['userid']);
					$rs['a_sendmsg'] = urr("mydisk","item=message&menu=profile&action=sendmsg&username=".rawurlencode($rs['username']));
					$rs['a_delbuddy'] = urr("mydisk","item=buddy&menu=buddy&action=delbuddy&userid={$rs['touserid']}");
					$rs['a_addbuddy'] = urr("mydisk","item=buddy&menu=buddy&action=s_addbuddy&userid={$rs['userid']}");
					$rs['in_time'] = $rs['in_time'] ? date("Y-m-d H:i",$rs['in_time']) : '--';
					$buddys_arr[] = $rs;
				}
				$db->free($q);
				unset($rs);
			}
			$page_nav = multi($total_num, $perpage, $pg, urr("mydisk","item=buddy&menu=buddy&action=$action"));

			require_once template_echo('buddy',$user_tpl_dir);
		}
		break;

	case 'search':
		$buddy_title = __('buddy_search');

		$scope = trim(gpc('scope','G',''));
		$word = trim(gpc('word','G',''));
		$word = str_replace('%','',$word);
		$word = str_replace('　',' ',replace_inject_str($word));

		if(!$word){
			$buddys_arr = array();
		}else{

			$perpage = 20;
			if($scope =='my'){
				$sql_do = " {$tpf}buddys b, {$tpf}users u where b.userid='$pd_uid' and b.touserid=u.userid and u.username like '%{$word}%'";
			}elseif($scope =='added'){
				$sql_do = " {$tpf}buddys b, {$tpf}users u where b.touserid='$pd_uid' and b.userid=u.userid and u.username like '%{$word}%'";
			}elseif($scope =='all'){
				$sql_do = " {$tpf}users u where u.username like '%{$word}%'";
			}
			$rs = $db->fetch_one_array("select count(*) as total_num from {$sql_do}");
			$total_num = $rs['total_num'];
			$start_num = ($pg-1) * $perpage;

			if($scope =='all'){
				$q = $db->query("select * from {$sql_do} order by userid desc limit $start_num,$perpage");
			}else{
				$q = $db->query("select b.*,u.username from {$sql_do} order by bdid desc limit $start_num,$perpage");
			}
			$buddys_arr = array();
			while($rs = $db->fetch_array($q)){
				$rs['a_viewinfo'] = urr("mydisk","item=buddy&action=viewinfo&userid=".$rs['userid']);
				$rs['a_addbuddy'] = urr("mydisk","item=buddy&menu=buddy&action=s_addbuddy&userid={$rs['userid']}");
				$rs['in_time'] = $rs['in_time'] ? custom_time("Y-m-d H:i",$rs['in_time']) : '--';
				$buddys_arr[] = $rs;
			}
			$db->free($q);
			unset($rs);

			$page_nav = multi($total_num, $perpage, $pg, urr("mydisk","item=buddy&menu=buddy&action=search&scope=$scope&word=".rawurlencode($word).""));
		}
		require_once template_echo('buddy',$user_tpl_dir);

		break;

	case 'delbuddy':
		$userid = (int)gpc('userid','GP',0);
		if($task =='delbuddy'){
			form_auth(gpc('formhash','P',''),formhash());

			$ref = trim(gpc('ref','P',''));

			$db->query_unbuffered("delete from {$tpf}buddys where touserid='$userid' and userid='$pd_uid'");

			tb_redirect($ref,__('delbuddy_success'));
		}else{
			$rs = $db->fetch_one_array("select * from {$tpf}users where userid='$userid'");
			if($rs){
				$buddy_name = $rs['username'];
			}
			unset($rs);
			$ref = $_SERVER['HTTP_REFERER'];
			require_once template_echo('buddy',$user_tpl_dir);
		}
		break;

	case 's_addbuddy':
		$userid = (int)gpc('userid','GP',0);
		if($task =='s_addbuddy'){
			form_auth(gpc('formhash','P',''),formhash());

			$ref = trim(gpc('ref','P',''));

			if($userid){
				$rs = $db->fetch_one_array("select userid from {$tpf}users where userid='$userid'");
				if($rs['userid']){
					$rs2 = $db->fetch_one_array("select count(*) as total from {$tpf}buddys where touserid='{$rs['userid']}' and userid='$pd_uid'");
					if($rs['userid'] == $pd_uid){
						$sysmsg = __('cannot_add_self');
					}elseif($rs2['total']){
						$sysmsg = __('already_buddy');
					}else{
						$ins = array(
						'userid' => $pd_uid,
						'touserid' => $rs['userid'],
						'in_time' => $timestamp,
						);
						$db->query("insert into {$tpf}buddys set ".$db->sql_array($ins).";");

						$sysmsg = __('addbuddy_success');
					}
				}else{
					$sysmsg = __('username_not_found');
				}
			}
			tb_redirect($ref,$sysmsg);
		}else{
			$rs = $db->fetch_one_array("select * from {$tpf}users where userid='$userid'");
			if($rs){
				$buddy_name = $rs['username'];
			}
			unset($rs);
			$ref = $_SERVER['HTTP_REFERER'];
			require_once template_echo('buddy',$user_tpl_dir);
		}
		break;
	case 'viewinfo':
		$userid = (int)gpc('userid','G',0);
		if($userid){
			$pa = get_profile($userid);
			$arrx =array('保密','男','女');
			$pa[gender] = $arrx[$pa[gender]];
			require_once template_echo('buddy',$user_tpl_dir);
		}else{
			exit('error uid');
		}
		break;

	case 'invite':
		$invite_url = $settings['invite_register_encode'] ? $settings['phpdisk_url'].urr("account","action=register&".pd_encode('uid='.$pd_uid)) : $settings['phpdisk_url'].urr("account",'action=register&uid='.$pd_uid);
		require_once template_echo('buddy',$user_tpl_dir);

		break;
	case 'corp_depart':
		require_once template_echo('buddy',$user_tpl_dir);
		break;
	default:
		redirect(urr("mydisk","item=buddy&menu=buddy&action=mybuddy"),'',0);
}
?>