<?php
/**
 * @version $Id: user_message.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', 'user_message');
require_once('include/common.inc.php');

$uid = (int)$uid;
$rssurl = 'rss.php?action=user&amp;uid='.$uid;
$userdata = $guestmsg = $rs = $friends = array();

if ($action)
{
	$rs = $DB->db_fetch_one_array("SELECT uid FROM {$db_prefix}members WHERE uid = $uid");
	switch ($action)
	{
		case 'comment':
			if (!$rs)
			{
				showMsg('参数错误或者没有权限');
			}
			if ($pb_reposttime)
			{
				$pb_lastcomment = gCookie('pb_lastcomment');
				if ($pb_lastcomment)
				{
					$seperateTime = $timestamp - $pb_lastcomment;
				}
				else
				{
					$seperateTime = 31536000;
				}
				if ($seperateTime < $pb_reposttime)
				{
					showMsg($u_message['refresh_limit']);
				}
				sCookie('pb_lastcomment', $timestamp, 1);
			}
			$allowcomment = $allowcomment && ($pb_ifcomment || $customer['adminid'] == '1');
			if (!$allowcomment)
			{
				showMsg($u_message['comment_nopermission']);
			}
			$comment = trim($_POST['comment']);
			require_once(PBDIGG_ROOT.'include/validate.func.php');
			if ($ck = checkComment($comment))
			{
				showMsg($ck);
			}
			$comment = HConvert($comment);
			if (!ckgdcode($captcha))
			{
				showMsg($u_message['checkcode_error']);
			}
			$ifcheck = ($pb_tcheck && !$customer['adminid']) ? 0 : 1;
			$SQL = "INSERT INTO {$db_prefix}guestmsg (`uid`, `authorid`, `author`, `content`, `ifcheck`, `postdate`, `postip`) 
					VALUES ($uid, ".$customer['uid'].", '".$customer['username']."', '$comment', '$ifcheck', '$timestamp', '".$_PBDEV['PB_IP']."')";
			$DB->db_query($SQL);
			if ($ifcheck)
			{
				redirect($u_message['user_msg_addsuccess'], 'user_message.php?uid='.$uid);
			}
			else
			{
				redirect($u_message['user_msg_readycheck'], 'user_message.php?uid='.$uid);
			}
			break;

		case 'del':
			if (!$rs || !$customer['uid'] || (($rs['uid'] !== $customer['uid']) && ($customer['adminid'] !== '1')))
			{
				showMsg('参数错误或者没有权限');
			}
			$DB->db_query("DELETE FROM {$db_prefix}guestmsg WHERE uid = $uid AND gmid = ".intval($gmid)." LIMIT 1");
			redirect($u_message['user_msg_delsuccess'], 'user_message.php?uid='.$uid);
			break;
	}
}

$SQL = "SELECT m.uid, m.username, m.email, m.regdate, m.publicemail, 
	mx.qq, mx.msn, mx.site, mx.location, mx.birthday, mx.signature, mx.avatar, mx.gender 
	FROM {$db_prefix}members m 
	LEFT JOIN {$db_prefix}memberexp mx 
	USING (uid) 
	WHERE m.uid = $uid";
$userdata = $DB->db_fetch_one_array($SQL);
if (!$userdata)
{
	showMsg($u_message['user_id_noexist'], $_PBDEV['PB_REFERER']);
}
if (!$userdata['publicemail'])
{
	$userdata['email'] = $u_message['secrecy'];
}
if (!$userdata['avatar'])
{
	$userdata['avatar'] = $_PBENV['PB_URL'].'images/avatars/portrait/default.png';
}
elseif (substr($userdata['avatar'], 0, 7) != 'http://')
{
	$userdata['avatar'] = $_PBENV['PB_URL'].'images/avatars/'.$userdata['avatar'];
}
switch ($userdata['gender'])
{
	case '0':
		$userdata['gender_name'] = $u_message['male'];
	break;
	
	case '1':
		$userdata['gender_name'] = $u_message['female'];
	break;
	
	default:
		$userdata['gender_name'] = $u_message['secrecy'];
	break;
}

$pagesize = (int)$pb_aperpage;
$result = $DB->db_fetch_one_array("SELECT COUNT(*) num FROM {$db_prefix}guestmsg WHERE uid = $uid");
$totalRecords = (int)$result['num'];
$page = !is_Page($page) ? 1 : $page;
$start_limit = ($page -1) * $pagesize;
$mult = multLink($page, $totalRecords, 'user_message.php?uid='.$uid.'&amp;', $pagesize);

$SQL = "SELECT gm.gmid, gm.authorid, gm.author, gm.content, gm.postdate, mx.avatar 
		FROM {$db_prefix}guestmsg gm 
		LEFT JOIN {$db_prefix}memberexp mx 
		ON gm.authorid = mx.uid 
		WHERE gm.uid = $uid 
		ORDER BY gm.gmid DESC 
		LIMIT $start_limit, $pagesize";
$DB->db_query($SQL);

while ($rs = $DB->db_fetch_array())
{
	if (!$rs['author'])
	{
		$rs['author'] = $pb_sitename.$u_message['net_friend'];
	}
	if ((strpos($rs['content'], '[emote]') !== FALSE) && (strpos($rs['content'], '[/emote]') !== FALSE))
	{
		$rs['content'] = preg_replace("/\[emote\]\s*([^\[\<\r\n\.]+?)\s*\[\/emote\]/is", "<img src=\"".$_PBENV['PB_URL']."images/emoticons/\\1.gif\" alt=\"\\1\" class=\"emote\" />", $rs['content']);
	}
	$rs['content'] = preg_replace('/(?:\r\n|\n\r|\r|\n)/i', '<br />', $rs['content']);
	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'];
	}
	$rs['postdate'] = formatPostTime($rs['postdate']);
	$guestmsg[]= $rs;
}

$SQL = "SELECT m.uid, 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 = $uid 
		AND f.ifconfirm = 1 
		ORDER BY f.fid DESC 
		LIMIT 0, 9";
$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'];
	}
	$friends[] = $rs;
}

$seo['pagetitle'] = $seo['keywords'] = $seo['description'] = ($userdata['username'].' - '.$u_message['user_guestmsg']);

$TPL->assign('mult_page', $mult);
$TPL->assign('m_action', (($customer['uid'] && $customer['uid'] == $uid) || ($customer['adminid'] && $allowdelatc)));
$TPL->assign('userdata', $userdata);
$TPL->assign('friends', $friends);
$TPL->assign('guestmsg', $guestmsg);
$TPL->assign('customer', $customer);
$TPL->assign('seo', $seo);
$TPL->assign('rss_url', $_PBENV['PB_URL'].$rssurl);
unset($rs, $userdata, $mult, $customer, $guestmsg, $seo, $friends);

PBOutPut('user_message.html');

?>