<?php
!defined('M_P') && exit('Forbidden');
!$winduid && Showmsg('not_login');
include_once(D_P.'data/bbscache/o_config.php');
require_once(R_P.'require/showimg.php');

InitGP(array('type','u'));
$u = (int)$u;
!$u && $u = $winduid;
$thisbase = $basename."q=friend&";

if ($type == 'find') {
	InitGP(array('step','page'));
	if ($step == 2) {
		InitGP('accord');
		$where = '';
		if ($accord == 'uid') {
			InitGP('uid','P',2);
			!$uid && Showmsg('mode_o_find_uid_err');
			$uid == $winduid && Showmsg('mode_o_find_self');
			$where .= 'uid='.pwEscape($uid);
		} elseif ($accord == 'username') {
			InitGP('username','',1);
			!$username && Showmsg('illegal_author');
			$addpage = 'type=find&step=2&accord=username&username='.rawurlencode($username);
			$input_username = $username;
			$sql_username = $username.'%';
			$where .= 'username LIKE'.pwEscape($sql_username);
			$username == $windid && Showmsg('mode_o_find_self');
		} elseif ($accord == 'email') {
			InitGP('email','P',1);
			if (!preg_match("/^[-a-zA-Z0-9_\.]+@([0-9A-Za-z][0-9A-Za-z-]+\.)+[A-Za-z]{2,5}$/",$email)) {
				Showmsg('mode_o_email_format_err');
			}
			$email == $winddb['email'] && Showmsg('mode_o_find_self');
			$where .= 'email='.pwEscape($email);
		} else {
			Showmsg('undefined_action');
		}
		$perpage = $db_perpage ? $db_perpage : '20';
		(int)$page<1 && $page = 1;
		$limit = pwLimit(($page-1)*$perpage,$perpage);
		$query = $db->query("SELECT uid,username,email,icon as face,regdate,honor FROM pw_members WHERE $where "."ORDER BY regdate DESC ".$limit);
		while ($rt = $db->fetch_array($query)) {
			$rt['face'] = showfacedesign($rt['face'],1);
			$rt['regdate'] = get_date($rt['regdate']);
			$rt['honor'] = substrs($rt['honor'],90);
			$members[] = $rt;
		}
		@extract($db->get_one("SELECT COUNT(*) AS count FROM pw_members WHERE $where"));
		if ($count > $perpage) {
			require_once(R_P.'require/forum.php');
			$pages = numofpage($count,$page,ceil($count/$perpage),"$thisbase$addpage&");
		}
	}
	$username = $windid;
	list($faceimg) = showfacedesign($winddb['icon'],1);
} elseif ($type == 'invite') {
	empty($o_invite) && Showmsg('mode_o_invite_close');
	require_once(D_P.'data/bbscache/dbreg.php');
	@include_once(D_P.'data/bbscache/inv_config.php');
	$inv_open && Showmsg('mode_o_invite_have');
	!$rg_allowregister && Showmsg('mode_o_invite_reg');
	list($faceimg) = showfacedesign($winddb['icon'],1);
	InitGP('step');
	$hash = appkey($winduid);
	$invite_url = $db_bbsurl.'/'.$basename.'q=invite&u='.$winduid.'&hash='.$hash;
	if ($step == 2) {
		InitGP(array('emails','extranote'),'P',1);
		strlen($emails)>200 && Showmsg('mode_o_email_toolang');
		strlen('extranote')>200 && Showmsg('mode_o_extra_toolang');
		$emails = explode(',',$emails);
		count($emails)>5 && Showmsg('mode_o_email_toolang');
		if ($emails) {
			foreach ($emails as $key=>$email) {
				$emails[$key] = trim($email);
				if (!$email) {
					unset($emails[$key]);
				} elseif (!preg_match("/^[-a-zA-Z0-9_\.]+@([0-9A-Za-z][0-9A-Za-z-]+\.)+[A-Za-z]{2,5}$/",$email)) {
					Showmsg('mode_o_email_format_err');
				}
			}
		}
		!$emails && Showmsg('mode_o_email_empty');
		if (!preg_match("/^http:/",$faceurl)) {
			$faceurl = $db_bbsurl.'/'.$faceurl;
		}
		require_once(R_P.'require/sendemail.php');
		foreach ($emails as $email) {
			sendemail($email,'email_mode_o_title','email_mode_o_content');
		}
		Showmsg('operate_success');
	}
	$username = $windid;
} else {
	$count	= $winddb['f_num'];
	$perpage= 20;
	$page	= (int) GetGP('page');
	$page<1 && $page = 1;
	$start	= ($page-1)*$perpage;
	$numofpage = ceil($count/$perpage);

	$sql	= '';
	$ftype	= false;
	if ($u!=$winduid) {
		$friend = $db->get_one("SELECT uid,username,icon FROM pw_members WHERE uid=".pwEscape($u));
		list($faceimg) = showfacedesign($friend['icon'],1);
		$username = $friend['username'];
	} else {
		InitGP('ftid','',2);
		if (isset($_GET['ftid'])) $ftype = (int) GetGP('ftid');
		list($faceimg) = showfacedesign($winddb['icon'],1);
		$username = $windid;
		$query = $db->query("SELECT * FROM pw_friendtype WHERE uid=".pwEscape($winduid)." ORDER BY ftid");
		$friendtype = array();
		while ($rt = $db->fetch_array($query)) {
			$friendtype[$rt['ftid']] = $rt;
		}
	}
	$friends = getFriends($u,$start,$perpage,$ftype,1);
}

require_once(R_P.'require/header.php');
require_once PrintEot('m_friend');
footer();
?>