<?php
/**
 * @version $Id: member.php v2.0 $
 * @package PBDigg
 * @copyright Copyright (C) 2007 - 2008 PBDigg.com. All Rights Reserved.
 * @license PBDigg is free software and use is subject to license terms
 */

define('PB_CSCRIPT', 'member');
require_once('include/common.inc.php');
require_once('include/member.inc.php');
require_once('include/validate.func.php');
require_once(loadLang('member'));

$m_uid = intval($customer['uid']);
if (!$m_uid)
{
	header('Location: login.php');
	exit;
}
$actionurl = 'member.php?type='.$type.'&amp;action='.$action;

if (!$type || $type == 'profile')
{
	require_once (PBDIGG_ROOT.'include/member.class.php');
	$Member = new Member();
	if ($action == 'basic')
	{
		if ($submitted)
		{
			$exp = array('qq'=>'','msn'=>'','site'=>'','location'=>'','gender'=>'','birthday'=>'','signature'=>'','showsign'=>'');
			$updatedata = array('uid'=>$m_uid, 'email'=>$_POST['email'], 'publicemail'=>(isset($publicemail) ? '1': '0'));
			if ($qq && checkQQ($qq))
			{
				$exp['qq'] = $qq;
			}
			if ($msn && checkMSN($msn))
			{
				$exp['msn'] = $msn;
			}
			if ($site && !checkURL($site))
			{
				$exp['site'] = $site;
			}
			if ($location && !checkLocation($location))
			{
				$exp['location'] = $location;
			}
			$exp['gender'] = (int)$gender;
	
			$birthday = $year.'-'.$month.'-'.$day;
			if (preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}/i', $birthday))
			{
				$exp['birthday'] = $birthday;
			}
			$exp['signature'] = HConvert($_POST['signature']);
			require_once(PBDIGG_ROOT.'include/ubb.func.php');
			$exp['ctsig'] = ($exp['signature'] == UBB2HTML($exp['signature'],'signature')) ? '0' : '1';
			$exp['showsign'] = (isset($showsign) ? '1': '0');
			$updatedata['exp'] = $exp;
			$Member->Action('mod', $updatedata);
			redirect('修改资料成功', $actionurl);
		}
		else
		{
			$user = array();
			$SQL = 'SELECT m.email, m.publicemail, mx.qq, mx.msn, mx.site, mx.location, mx.birthday, mx.signature, mx.showsign, mx.avatar, mx.gender 
					FROM '.$db_prefix.'members m 
					LEFT JOIN '.$db_prefix.'memberexp mx 
					USING (uid) 
					WHERE m.uid = '.$m_uid;
			$user = $DB->db_fetch_one_array($SQL);
			if ($user)
			{
				radio_checked('gender_', $user['gender'], 4);
				$TPL->assign('gender_0', $gender_0);
				$TPL->assign('gender_1', $gender_1);
				$TPL->assign('gender_3', $gender_3);
				unset($gender_0, $gender_1, $gender_3);
				$ifpublicemail = ($user['publicemail']) ? 'checked="checked"' : '';
				$ifshowsign = ($user['showsign']) ? 'checked="checked"' : '';
				if (!$user['avatar'])
				{
					$user['avatar'] = $_PBENV['PB_URL'].'images/avatars/portrait/default.png';
				}
				elseif (substr($user['avatar'], 0, 7) != 'http://')
				{
					$user['avatar'] = $_PBENV['PB_URL'].'images/avatars/'.$user['avatar'];
				}
				list($year, $month, $day) = explode('-', $user['birthday']);
				require_once (PBDIGG_ROOT.'include/DateTime.php');
				$year_option = phtml_select($_YEAR, 'year', $year, 'id="year"');
				$month_option = phtml_select($_MONTH, 'month', $month, 'id="month"');
				$day_option = phtml_select($_DAY, 'day', $day, 'id="day"');
				$seo['pagetitle'] = $seo['keywords'] = $seo['description'] = $member_message['profile_basic'];
				$TPL->assign('year_option', $year_option);
				$TPL->assign('month_option', $month_option);
				$TPL->assign('day_option', $day_option);
				$TPL->assign('user', $user);
				$TPL->assign('ifshowsign', $ifshowsign);
				$TPL->assign('ifpublicemail', $ifpublicemail);
			}
			else
			{
				redirect($member_message['member_noexist'], 'login.php?action=logout');
			}
		}
	}
	elseif ($action == 'pw')
	{
		require_once(PBDIGG_ROOT.'data/manager.inc.php');
		if ($customer['username'] == $siteFounder['username'])
		{
			echo '<script>alert("'.$member_message['mod_pw_superman'].'");history.go(-1)</script>';
			exit;
		}
		if ($submitted)
		{
			if ($oldpw && $newpw && ($newpw == $rnewpw) && !checkPassword($newpw))
			{
				$newpw_md5 = md5($newpw);
				$oldpw_md5 = md5($oldpw);
				$DB->db_query("UPDATE {$db_prefix}members SET password = '$newpw_md5' WHERE uid = $m_uid AND password = '$oldpw_md5'");
				if ($DB->db_affected())
				{
					if ($pb_ucenable)
					{
						$uid = uc_user_edit($customer['username'], $oldpw, $newpw, '', 1);
					}
					sCookie('pb_auth', PEncode($m_uid."\t".$newpw_md5, $pb_sitehash));
					showMsg($member_message['mod_pw_success'], $actionurl);
				}
				else
				{
					showMsg($member_message['mod_pw_failed'], $actionurl);
				}
			}
			else
			{
				showMsg($member_message['mod_pw_error'], $actionurl);
			}
		}
		$seo['pagetitle'] = $seo['keywords'] = $seo['description'] = $member_message['mod_pw'];
	}
	elseif ($action == 'avatar')
	{
		if ($_FILES['uploadavatar']['name'])
		{
			if ($allowavatar && $pb_selfavat && $pb_avatupload)
			{
				//upload avatar
				require_once(PBDIGG_ROOT.'include/Upload.class.php');
				$ext = array('gif', 'jpeg', 'jpg', 'png');
				$rsavatar = array();
				$UPLOAD = new Upload();
				if ($UPLOAD->_count)
				{
					$files = & $UPLOAD->getFiles();
					$filepath = PBDIGG_ROOT.'images/avatars/';
					foreach ($files as $file)
					{
						if ($ck = $file->checkFile($ext, $pb_avatsize))
						{
							showMsg($ck, $actionurl);
						}
						else
						{
							$rsavatar = $DB->db_fetch_one_array("SELECT avatar FROM {$db_prefix}memberexp WHERE uid = '$m_uid'");
							if (preg_match('/^[0-9a-z]{1,32}\.(gif|jpg|jpeg|png)$/i', $rsavatar['avatar']))
							{
								PDel(PBDIGG_ROOT.'images/avatars/'.$rsavatar['avatar']);
							}
							if ($ck = $file->moveFile($filepath, $m_uid))
							{
								showMsg($ck, $actionurl);
							}
							else
							{
								$DB->db_query("UPDATE {$db_prefix}memberexp SET avatar = '".$m_uid.'.'.$file->getExtName()."' WHERE uid = ".$m_uid);
								redirect($member_message['mod_avatar_success'], $actionurl);
							}
						}
					}
				}
				else
				{
					showMsg($member_message['mod_avatar_failed'], $actionurl);
				}
			}
			else
			{
				showMsg($member_message['mod_avatar_failed'], $actionurl);
			}
		}
		elseif ($allowavatar && $pb_selfavat && $linkavatar && $linkavatar != 'http://')
		{
			$ext = array('gif', 'jpeg', 'jpg', 'png');
			$linkavatar = trim($_POST['linkavatar']);
			if ($ck = checkAvatar($linkavatar))
			{
				showMsg($ck, $actionurl);
			}
			else
			{
				$rsavatar = $DB->db_fetch_one_array("SELECT avatar FROM {$db_prefix}memberexp WHERE uid = '$m_uid'");
				if (preg_match('/^[0-9a-z]{1,32}\.(gif|jpg|jpeg|png)$/i', $rsavatar['avatar']))
				{
					PDel(PBDIGG_ROOT.'images/avatars/'.$rsavatar['avatar']);
				}
				$DB->db_query("UPDATE {$db_prefix}memberexp SET avatar = '".$linkavatar."' WHERE uid = ".$m_uid);
				redirect($member_message['mod_avatar_success'], $actionurl);
			}
		}
		elseif ($savatar)
		{
			$avatar_list = PListFile(PBDIGG_ROOT.'images/avatars/portrait', array('gif','jpg','jpeg','png'));
			$savatar = trim($_POST['savatar']);
			$savatar = substr($savatar, 0, strpos($savatar, '|'));
			if (in_array($savatar, $avatar_list))
			{
				$rsavatar = $DB->db_fetch_one_array("SELECT avatar FROM {$db_prefix}memberexp WHERE uid = '$m_uid'");
				if (preg_match('/^[0-9a-z]{1,32}\.(gif|jpg|jpeg|png)$/i', $rsavatar['avatar']))
				{
					PDel(PBDIGG_ROOT.'images/avatars/'.$rsavatar['avatar']);
				}
				$DB->db_query("UPDATE {$db_prefix}memberexp SET avatar = 'portrait/".$savatar."' WHERE uid = ".$m_uid);
				redirect($member_message['mod_avatar_success'], $actionurl);
			}
			else
			{
				showMsg($member_message['mod_avatar_illegal_avatar'], $actionurl);
			}
		}
		else
		{
			$user['avatar'] = $customer['avatar'];
			$seo['pagetitle'] = $seo['keywords'] = $seo['description'] = $member_message['mod_avatar'];
			$avatar_list = PListFile(PBDIGG_ROOT.'images/avatars/portrait', array('gif','jpg','jpeg','png'));
			$TPL->assign('allowavatar', ($allowavatar && $pb_selfavat));
			$TPL->assign('allowuploadavatar', $pb_avatupload);
			$TPL->assign('avatar_list', $avatar_list);
			$TPL->assign('user', $user);
		}
	}
	else
	{
		$seo['pagetitle'] = $seo['keywords'] = $seo['description'] = $member_message['membercp'];
		$type = 'profile';
		$customer['lastvisit'] = gdate($customer['lastvisit'], 'Y-m-d H:i');
		if ($customer['groupid'] == '6')
		{
			$TPL->assign('show_notice', '1');
		}
	}
}
elseif ($type == 'msg')
{
	$msg = $rs = array();
	$mult = $totalRecords = $start_limit = $qtype = $msgper = '';
	$seo['pagetitle'] = $seo['keywords'] = $seo['description'] = $member_message['msg'];
	$msg = $DB->db_fetch_one_array("SELECT COUNT(*) num FROM {$db_prefix}message WHERE tuid = '$m_uid' AND type = 'r' AND ifsys = 0");
	if ($msg)
	{
		$msgper = $msgmax ? round($msg['num'] / $msgmax, 5) * 100 : '100';
		if ($msgper >= 90)
		{
			$msgper = '<font style="color:#f00">'.$msgper.'</font>';
		}
	}
	$TPL->assign('msg', sprintf($member_message['msg_status'], $msg['num'], $msgmax, $msgper));
	$pagesize = (int)$pb_msg;
	if ($action == 'receivebox')
	{
		$qtype = 'r';
		$qfiled = 'tuid';
	}
	else
	{
		$qtype = 's';
		$qfiled = 'fuid';
	}
	$rs = $DB->db_fetch_one_array("SELECT COUNT(*) num FROM {$db_prefix}message WHERE $qfiled = '$m_uid' AND type = '".$qtype."'");
	$totalRecords = (int)$rs['num'];
	$page = !is_Page($page) ? 1 : $page;
	$start_limit = ($page -1) * $pagesize;
	$mult = multLink($page, $totalRecords, 'member.php?type=msg&amp;action='.$action.'&amp;', $pagesize);
	if ($action == 'receivebox')
	{
		$SQL = "SELECT ms.mid, ms.fuid, ms.tuid, ms.title, ms.content, ms.postdate, ms.ifread, ms.ifsys, m.username fauthor 
				FROM {$db_prefix}message ms 
				LEFT JOIN {$db_prefix}members m 
				ON ms.fuid = m.uid 
				WHERE ms.tuid = '$m_uid' 
				AND ms.type = 'r' 
				ORDER BY ms.postdate DESC, ms.ifread ASC 
				LIMIT ".$start_limit.", ".$pagesize;
		$DB->db_query($SQL);
		$rs = $msgifno = array();
		while ($rs = $DB->db_fetch_array())
		{
			$rs['ifread'] = $rs['ifread'] ? $member_message['msg_readed'] : $member_message['msg_unreaded'];
			$rs['postdate'] = gdate($rs['postdate'], 'Y-m-d H:i');
			$rs['title'] = PBSubstr($rs['title'], 30);
			if ($rs['ifsys'])
			{
				$rs['title'] = $member_message['sys_msg'].$rs['title'];
			}
			$msgifno[] = $rs;
		}
		$TPL->assign('mult', $mult);
		$TPL->assign('msgifno', $msgifno);
	}
	elseif ($action == 'sendbox')
	{
		$SQL = "SELECT ms.mid, ms.fuid, ms.tuid, ms.title, ms.content, ms.postdate, ms.ifread, ms.ifsys, m.username tauthor 
				FROM {$db_prefix}message ms 
				LEFT JOIN {$db_prefix}members m 
				ON ms.tuid = m.uid 
				WHERE ms.fuid = '$m_uid' 
				AND ms.type = 's' 
				ORDER BY ms.postdate DESC, ms.ifread ASC 
				LIMIT ".$start_limit.", ".$pagesize;
		$DB->db_query($SQL);
		$rs = $msgifno = array();
		while ($rs = $DB->db_fetch_array())
		{
			$rs['postdate'] = gdate($rs['postdate'], 'Y-m-d H:i');
			$rs['title'] = PBSubstr($rs['title'], 30);
			$msgifno[] = $rs;
		}
		$TPL->assign('mult', $mult);
		$TPL->assign('msgifno', $msgifno);
	}
	elseif ($action == 'read')
	{
		$mid = intval($mid);
		$SQL = "SELECT ms.mid, ms.fuid, ms.title, ms.content, ms.postdate, ms.ifsys, ms.ifread, m.username 
				FROM {$db_prefix}message ms 
				LEFT JOIN {$db_prefix}members m 
				ON ms.fuid = m.uid 
				WHERE ms.mid = $mid 
				AND ((ms.tuid = $m_uid AND ms.type = 'r') OR (ms.fuid = $m_uid AND ms.type = 's'))";
		$rs = $DB->db_fetch_one_array($SQL);
		if ($rs)
		{
			$rs['postdate'] = gdate($rs['postdate'], 'Y-m-d H:i');
			if ($rs['ifsys'])
			{
				$rs['title'] = $member_message['sys_msg'].$rs['title'];
				$rs['username'] = $member_message['sys'];
				$rs['uurl'] = '#';
			}
			else
			{
				$rs['uurl'] = 'user.php?uid='.$rs['fuid'];
			}
			if (!$rs['ifread'])
			{
				$DB->db_query("UPDATE {$db_prefix}message SET ifread = 1 WHERE mid = $mid AND tuid = $m_uid AND type = 'r'");
				//new msg
				$newmsg = $DB->db_fetch_one_array("SELECT COUNT(*) num FROM {$db_prefix}message WHERE tuid = $m_uid AND type = 'r' AND ifread = 0");
				$DB->db_query("UPDATE {$db_prefix}memberdata SET newmsg = ".(int)$newmsg['num']." WHERE uid = $m_uid");
			}
		}
		$TPL->assign('read_msg', $rs);
	}
	elseif ($action == 'write')
	{
		if (!$allowmsg)
		{
			showMsg($member_message['msg_send_limit'], 'member.php?type=msg&amp;action=receivebox');
		}
		if ($submitted)
		{
			$receiver = trim($_POST['receiver']);
			$title = trim($_POST['title']);
			$content = trim($_POST['content']);
			if (!$receiver || !$title || !$content)
			{
				showMsg($member_message['msg_conent_emtpy'], $actionurl);
			}
			$rs = $DB->db_fetch_one_array("SELECT uid FROM {$db_prefix}members WHERE username = '$receiver'");
			if (!$rs)
			{
				showMsg($member_message['msg_receiver_noexist'], $actionurl);
			}
			if ($rs['uid'] == $customer['uid'])
			{
				showMsg($member_message['msg_send_self'], $actionurl);
			}
			$tuid = $rs['uid'];
			if (!checkMsg($title))
			{
				showMsg($member_message['msg_title_banwords'], $actionurl);
			}
			if (!checkMsg($content))
			{
				showMsg($member_message['msg_content_banwords'], $actionurl);
			}
			$title = HConvert($title);
			$content = HConvert($content);
			$sendMsg[$tuid] = array($m_uid, $title, $content);
			$msave = isset($msave) ? TRUE : FALSE;
			require_once(PBDIGG_ROOT.'include/msg.inc.php');
			PMsg($sendMsg, $msave);
			redirect($member_message['msg_send_success'], 'member.php?type=msg&amp;action=receivebox');
		}
		else
		{
			$rs = array();
			if ($mid)
			{
				$mid = intval($mid);
				$SQL = "SELECT ms.title, m.username 
						FROM {$db_prefix}message ms 
						LEFT JOIN {$db_prefix}members m 
						ON ms.fuid = m.uid 
						WHERE ms.mid = $mid 
						AND ms.type = 'r' 
						AND ms.tuid = $m_uid";
				$rs = $DB->db_fetch_one_array($SQL);
				$rs['title'] = strpos($rs['title'], 'Re:') === FALSE ? 'Re:'.$rs['title'] : $rs['title'];
			}
			if ($tuid && !$rs)
			{
				$tname = $DB->db_fetch_one_array("SELECT username FROM {$db_prefix}members WHERE uid = ".intval($tuid));
				if ($tname)
				{
					$rs['username'] = $tname['username'];
				}
			}
			$TPL->assign('write_msg', $rs);
		}
	}
	elseif ($action == 'delfull')
	{
		if ($submitted)
		{
			$DB->db_query("DELETE FROM {$db_prefix}message WHERE fuid = '$m_uid' AND type = 's'");
			$DB->db_query("DELETE FROM {$db_prefix}message WHERE tuid = '$m_uid' AND type = 'r'");
			//new msg
			$newmsg = $DB->db_fetch_one_array("SELECT COUNT(*) num FROM {$db_prefix}message WHERE tuid = $m_uid AND type = 'r' AND ifread = 0");
			$DB->db_query("UPDATE {$db_prefix}memberdata SET newmsg = ".(int)$newmsg['num']." WHERE uid = $m_uid");
			redirect($member_message['msg_del_success'], 'member.php?type=msg&amp;action=receivebox');
		}
	}
	elseif ($action == 'del')
	{
		if ($mid && is_array($mid))
		{
			$mid = implode(',', $mid);
			if ($mid && preg_match('/^[0-9,]+?$/', $mid))
			{
				$DB->db_query("DELETE FROM {$db_prefix}message WHERE mid IN (".$mid.") AND ((fuid = $m_uid AND type = 's') OR (tuid = $m_uid AND type = 'r'))");
				//new msg
				$newmsg = $DB->db_fetch_one_array("SELECT COUNT(*) num FROM {$db_prefix}message WHERE tuid = $m_uid AND type = 'r' AND ifread = 0");
				$DB->db_query("UPDATE {$db_prefix}memberdata SET newmsg = ".(int)$newmsg['num']." WHERE uid = $m_uid");
			}
		}
		redirect($member_message['msg_del_success'], 'member.php?type=msg&amp;action=receivebox');
	}
	else
	{
		showMsg($member_message['undefined_action'], 'member.php');
	}
}
elseif ($type == 'my')
{
	if ($action == 'collection')
	{
		if ($job == 'add')
		{
			$tid = intval($tid);
			$rs = $result = $tids = array();
			$rs = $DB->db_fetch_one_array("SELECT tid, cid FROM {$db_prefix}threads WHERE tid = ".$tid);
			if ($DB->db_num())
			{
				$DB->db_query("SELECT tid FROM {$db_prefix}tdata WHERE uid = $m_uid AND type = 'collection'");
				while ($result = $DB->db_fetch_array())
				{
					$tids[] = $result['tid'];
				}
				if (in_array($tid, $tids))
				{
					showMsg($member_message['collection_thread_exist'], $actionurl);
				}
				if ($DB->db_num() >= $favorsmax)
				{
					showMsg($member_message['collection_storage_full'], $actionurl);
				}
				$DB->db_query("INSERT INTO {$db_prefix}tdata (cid, tid, uid, type) VALUES (".$rs['cid'].", ".$rs['tid'].", $m_uid, 'collection')");
				redirect($member_message['collection_store_success'], $actionurl);
			}
			else
			{
				showMsg($u_message['thread_noexist'], $actionurl);
			}
		}
		elseif ($job == 'del')
		{
			if (!$tids || !is_array($tids))
			{
				showMsg($u_message['parameter_error'], $actionurl);
			}
			$tids = implode(',', $tids);
			if ($tids && preg_match('~^[0-9,]+$~', $tids))
			{
				$DB->db_query("DELETE FROM {$db_prefix}tdata WHERE tid IN (".$tids.") AND uid = $m_uid AND type = 'collection'");
			}
			redirect($member_message['collection_del_success'], $actionurl);
		}
		else
		{
			$collectionnum = '';
			$SQL = "SELECT td.tid, t.uid, t.subject, t.author, c.cid, c.name cate 
					FROM {$db_prefix}tdata td 
					LEFT JOIN {$db_prefix}threads t 
					ON td.tid = t.tid 
					LEFT JOIN {$db_prefix}categories c 
					ON td.cid = c.cid 
					WHERE td.uid = '$m_uid' 
					AND type = 'collection'";
			$DB->db_query($SQL);
			$collectionnum = $DB->db_num();
			$favs = $fav = array();
			$seo['pagetitle'] = $seo['keywords'] = $seo['description'] = $member_message['collection'];
			while ($fav = $DB->db_fetch_array())
			{
				$fav['subject'] = PBSubstr($fav['subject'], $pb_indextitle);
				$favs[] = $fav;
			}
			$TPL->assign('favorite', sprintf($member_message['collection_status'], $collectionnum, $favorsmax, ($favorsmax ? round($collectionnum / $favorsmax, 5) * 100 : '100')));
			$TPL->assign('favs', $favs);
		}
	}
	elseif ($action == 'invitecode')
	{
		if ($job == 'apply')
		{
			$num = (int)$num;
			$reason = trim($_POST['reason']);
			if (!$num || !$reason)
			{
				showMsg('请填写完整申请信息');
			}
			$DB->db_query("INSERT INTO {$db_prefix}applyinvite (uid,username,applyreason,applynum,applydate) VALUES ($m_uid, '".$customer['username']."', '".HConvert($reason)."', $num, $timestamp)");
			showMsg('您的申请递交成功，等待管理员审核');
		}
		elseif ($job == 'del')
		{
			if (!$id || !is_array($id))
			{
				showMsg('参数错误', 'member.php?type=my&action=invitecode');
			}
			$newid = '';
			foreach ($id as $v)
			{
				$v = (int)$v;
				if ($v)
				{
					$newid .= $newid ? ','.$v : $v;
				}
			}
			$newid && $DB->db_query("DELETE FROM {$db_prefix}invitecode WHERE id IN ($newid) AND uid = $m_uid");
			showMsg('删除成功', 'member.php?type=my&action=invitecode');
		}
		else
		{
			$pagesize = (int)$pb_aperpage;
			$result = $DB->db_fetch_one_array("SELECT COUNT(*) num FROM {$db_prefix}invitecode WHERE uid = $m_uid");
			$totalRecords = (int)$result['num'];
			$page = !is_Page($page) ? 1 : $page;
			$start_limit = ($page -1) * $pagesize;
			$mult = multLink($page, $totalRecords, 'member.php?type=my&amp;action=invitecode&amp;', $pagesize);
			$SQL = "SELECT id,invitecode, endtime 
					FROM {$db_prefix}invitecode 
					WHERE uid = $m_uid 
					ORDER BY endtime 
					LIMIT $start_limit, $pagesize";
			$DB->db_query($SQL);
			$invitecode = array();
			while ($rs = $DB->db_fetch_array())
			{
				if ($rs['endtime'])
				{
					$rs['endtime'] = gdate($rs['endtime']);
				}
				else
				{
					$rs['endtime'] = '无限期';
				}
				$invitecode[] = $rs;
			}
			$TPL->assign('mult', $mult);
			$TPL->assign('invitecode', $invitecode);
			$seo['pagetitle'] = $seo['keywords'] = $seo['description'] = '我的邀请码';
		}
	}
	elseif ($action == 'friend')
	{
		if ($uid == $m_uid)
		{
			showMsg($member_message['friend_add_illegal'], $actionurl);
		}
		if ($job == 'add')
		{
			$uid = intval($uid);
			$rs = $result = $sendMsg = array();
			$rs = $DB->db_fetch_one_array("SELECT COUNT(*) num FROM {$db_prefix}members WHERE uid = $uid");
			if ($rs['num'])
			{
				$rs = $DB->db_fetch_one_array("SELECT COUNT(*) num FROM {$db_prefix}friends WHERE uid = $m_uid AND fuid = $uid");
				if ($rs['num'])
				{
					showMsg($member_message['friend_exist_already'], $actionurl);
				}
				$rs = $DB->db_fetch_one_array("SELECT COUNT(*) num FROM {$db_prefix}friends WHERE uid = $uid AND fuid = $m_uid AND ifconfirm = 0");
				if ($rs['num'])
				{
					header('Location: member.php?type=my&action=friend&job=confirm&uid='.$uid);
					exit;
				}
				$DB->db_query("INSERT INTO {$db_prefix}friends (fid, uid, fuid, ifconfirm, createdate) VALUES (NULL, $m_uid, $uid, 0, $timestamp)");
				$sendMsg[$uid] = array($m_uid, $customer['username'].$member_message['friend_addmsg_title'], '<u>'.$customer['username'].'</u>'.sprintf($member_message['friend_addmsg_content'], $m_uid));
				require(PBDIGG_ROOT.'include/msg.inc.php');
				PMsg($sendMsg, FALSE, 1);
				redirect($member_message['friend_add_success'], $actionurl);
			}
			else
			{
				showMsg($member_message['friend_uidnoexist'], $actionurl);
			}
		}
		elseif ($job == 'del')
		{
			$uid = intval($uid);
			if (!$uid)
			{
				showMsg($u_message['parameter_error'], $actionurl);
			}
			$DB->db_query("DELETE FROM {$db_prefix}friends WHERE (uid = $m_uid AND fuid = $uid) OR (uid = $uid AND fuid = $m_uid)");
			if ($DB->db_affected())
			{
				$DB->db_query("UPDATE {$db_prefix}memberdata SET friendnum = friendnum - 1 WHERE uid = $uid OR uid = $m_uid");
			}
			redirect($member_message['friend_del_success'], $actionurl);
		}
		elseif ($job == 'confirm')
		{
			$uid = intval($uid);
			$sendMsg = array();
			if (!$uid)
			{
				showMsg($u_message['parameter_error'], $actionurl);
			}
			$DB->db_query("UPDATE {$db_prefix}friends SET ifconfirm = 1 WHERE uid = $uid AND fuid = $m_uid LIMIT 1");
			if ($DB->db_affected())
			{
				$DB->db_query("INSERT INTO {$db_prefix}friends (fid, uid, fuid, ifconfirm, createdate) VALUES (NULL, $m_uid, $uid, 1, $timestamp)");
				$DB->db_query("UPDATE {$db_prefix}memberdata SET friendnum = friendnum + 1 WHERE uid = $uid OR uid = $m_uid");
				$sendMsg[$uid] = array($m_uid, $member_message['friend_addconfirm_title'], '<u>'.$customer['username'].'</u>'.$member_message['friend_addconfirm_content']);
				require(PBDIGG_ROOT.'include/msg.inc.php');
				PMsg($sendMsg, FALSE, 1);
				redirect($member_message['friend_confirm_success'], $actionurl);
			}
			showMsg($member_message['friend_confirm_failed'], $actionurl);
		}
		elseif ($job == 'deny')
		{
			$uid = intval($uid);
			$sendMsg = array();
			if (!$uid)
			{
				showMsg($u_message['parameter_error'], $actionurl);
			}
			$DB->db_query("DELETE FROM {$db_prefix}friends WHERE uid = $uid AND fuid = $m_uid AND ifconfirm = 0 LIMIT 1");
			$sendMsg[$uid] = array($m_uid, $member_message['friend_adddeny_title'], '<u>'.$customer['username'].'</u>'.$member_message['friend_adddeny_content']);
			require(PBDIGG_ROOT.'include/msg.inc.php');
			PMsg($sendMsg, FALSE, 1);
			showMsg($member_message['friend_deny_success'], $actionurl);
		}
		else
		{
			$pagesize = (int)$pb_aperpage;
			$result = $DB->db_fetch_one_array("SELECT COUNT(*) num FROM {$db_prefix}friends WHERE uid = $m_uid");
			$totalRecords = (int)$result['num'];
			$page = !is_Page($page) ? 1 : $page;
			$start_limit = ($page -1) * $pagesize;
			$mult = multLink($page, $totalRecords, $_PBENV['PB_URL'].'member.php?type=my&amp;action=friend&amp;', $pagesize);
			$SQL = "SELECT f.fuid, m.username, mx.avatar 
					FROM {$db_prefix}friends f 
					LEFT JOIN {$db_prefix}members m 
					ON f.fuid = m.uid 
					LEFT JOIN {$db_prefix}memberexp mx 
					ON m.uid = mx.uid 
					WHERE f.uid = $m_uid AND f.ifconfirm = 1 
					ORDER BY f.fid DESC 
					LIMIT $start_limit, $pagesize";
			$DB->db_query($SQL);
			while ($rs = $DB->db_fetch_array())
			{
				$rs['fuurl'] = 'user.php?uid='.$rs['fuid'];
				if (!$rs['avatar'])
				{
					$rs['avatar'] = $_PBENV['PB_URL'].'images/avatars/portrait/default.png';
				}
				elseif (substr($rs['avatar'], 0, 7) != 'http://')
				{
					$rs['avatar'] = $_PBENV['PB_URL'].'images/avatars/'.$rs['avatar'];
				}
				$friends[] = $rs;
			}
			$SQL = "SELECT f.uid, m.username, mx.avatar 
					FROM {$db_prefix}friends f 
					LEFT JOIN {$db_prefix}members m 
					ON f.uid = m.uid 
					LEFT JOIN {$db_prefix}memberexp mx 
					ON m.uid = mx.uid 
					WHERE f.fuid = $m_uid AND f.ifconfirm = 0 
					ORDER BY f.fid DESC";
			$DB->db_query($SQL);
			while ($rs = $DB->db_fetch_array())
			{
				if (!$rs['avatar'])
				{
					$rs['avatar'] = $_PBENV['PB_URL'].'images/avatars/portrait/default.png';
				}
				elseif (substr($rs['avatar'], 0, 7) != 'http://')
				{
					$rs['avatar'] = $_PBENV['PB_URL'].'images/avatars/'.$rs['avatar'];
				}
				$unfriends[] = $rs;
			}
			if ($unfriends)
			{
				$TPL->assign('friendconfirm', '1');
			}
			$TPL->assign('friends', $friends);
			$TPL->assign('unfriends', $unfriends);
			$TPL->assign('mult_page', $mult);
			$seo['pagetitle'] = $seo['keywords'] = $seo['description'] = $member_message['friend'];
		}
	}
}
elseif ($type == 'reactive' && ($customer['groupid'] == '6'))
{
	require_once (PBDIGG_ROOT.'include/mail.inc.php');
	require_once(loadLang('register'));
	$activecode = $customer['uid'].'|'.$timestamp.'|'.random(8);
	$activecode = rawurlencode(PEncode($activecode, $pb_sitehash));
	$mail_content = str_replace(array('[!--username--]','[!--activecode--]'), array($customer['username'], $activecode), $register_message['register_active_body']);
	PMail($customer['email'], $register_message['register_active_subject'], $mail_content);
	showMsg($register_message['register_check_email'], 'member.php');
}

$TPL->assign('type', $type);
$TPL->assign('customer', $customer);
$TPL->assign('action', $action);
$TPL->assign('actionurl', $actionurl);
$TPL->assign('menu', $member_menu);
$TPL->assign('seo', $seo);
PBOutPut('member.html');
?>
