<?php
/**
 * @version 1.0
 * @author [飄在北京] <week9@263.net>
 * @write 2003-03-29
 * 社區虛擬形象系統
 * 版權所有: 飄在北京
 * http://www.week9.com
 */

error_reporting(7);
define('IN_PHPBB', 1);
$phpbb_root_path = "./";
include_once($phpbb_root_path . 'extension.inc');
include_once($phpbb_root_path . 'common.'.$phpEx);
include_once($phpbb_root_path."includes/functions_face.php");
$userdata = session_pagestart($user_ip, PAGE_FACE);
init_userprefs($userdata);

if ( !$userdata['session_logged_in'] )
{
	$redirect = ( isset($post_id) ) ? POST_POST_URL . "=$post_id" : POST_TOPIC_URL . "=$topic_id";
	$redirect .= ( isset($start) ) ? "&start=$start" : '';
	redirect(append_sid("login.$phpEx?redirect=viewtopic.$phpEx&$redirect", true));
}

$action = trim($action);
if (!isset($action) or $action=="") {
  $action="showroom";
}

if ($action=="showroom") {
	$faceclass = $_REQUEST[classid]; 
	$faceid = trim($_REQUEST[id]); 
	$userbankmoney = $userdata[user_bank];
	$usernm = $userdata[uname];
	$usermoney = $userdata[user_money];
	$allmoney = $userdata[user_money]+$userdata[user_bank];
	$gender = $userdata[user_gender];
	if ($userdata[user_avatar_type] == 4)
	{
		$checked1 = "checked";
		$checked2 = "";
	}else
	{
		$checked2 = "checked";
		$checked1 = "";
	}

	if ($gender == 1)
	{
		$sex = "m";
		$usergender = "帥哥";
		$tempequip = '0-0-0-0-0-0-0-0-init-init-0-init-0-init-init-0-0-0-init-0-0-0-0-0-0-0';	
	}
	elseif($gender == 2)
	{
		$usergender = "美女";
		$tempequip = '0-0-0-0-0-0-0-0-initf-initf-0-initf-0-initf-initf-0-0-0-initf-0-0-0-0-0-0-0'; 
		$sex = "f";		
	}
	else
	{
		message_die(GENERAL_MESSAGE,"你還沒有選擇性別，本功能需要用戶選擇性別，如果你要使用本功能，請你返回修改你的論壇性別！ <a href=".getenv("HTTP_REFERER")." class=nav>返回</a>");
	}
	$facecookie1 = $db->sql_query("SELECT * FROM week9_userdata where userid = $userdata[uid]  and dafaultuse = '1'");
	while ($usercaifu1 = $db->sql_fetchrow($facecookie1))
	{
		if (($usercaifu1[face_fitherd] != $sex) and ($usercaifu1[face_fitherd] != 't'))
		{
			$db->sql_query("UPDATE week9_userdata SET 
										  dafaultuse = '0'
								where userid = $userdata[uid] and classid = '$usercaifu1[classid]' and facepicid = '$usercaifu1[facepicid]' 
								 ");
		}
	}
	$db->sql_freeresult($facecookie1);
		$facecookie = $db->sql_query("SELECT * FROM week9_userdata where userid = $userdata[uid]  and dafaultuse = '1'");
		//$facecookie = $db->sql_query("SELECT * FROM week9_userdata where userid = $userdata[uid] and dafaultuse = '1' and (face_fitherd = '$sex' or face_fitherd = 't')");
		$break = 1;
		while ($usercaifu = $db->sql_fetchrow($facecookie))
		{
			$cookiearray = split('-', $tempequip);		
			$tempequip = "0";
			if ($cookiearray[6] != '0')
			{
				$cookiearray[8] = '0';
				$cookiearray[9] = '0';
			}
			for ($i=1; $i<26; $i++)
			{
				if($i != $usercaifu[classid])
				{
					$temp = $cookiearray[$i];
					$tempequip .= "-".$temp."";
				}
				else
				{
					$tempequip .= "-".$usercaifu[facepicid]."";
				}
			}
			$rtempequip = $tempequip;
		}
	$db->sql_freeresult($facecookie);
	$userfacecookie = $rtempequip;

	setcookie("tempequip", "$tempequip");

	//顯示未佩戴物品開始
	$result = $db->sql_query("SELECT * FROM week9_faceset limit 1"); 
	$week9faceset = $db->sql_fetchrow($result);
	eval("\$left = \"".gettemplate('face_left')."\";");
	eval("\$center = \"".gettemplate('face_select_center')."\";");
}
/////////////////////修改設置

if ($_POST['action']=="set") {
	if(!isset($_POST[setface]))
	{
		message_die(GENERAL_MESSAGE,"沒有選擇設定 <a href=".getenv("HTTP_REFERER")." class=nav>返回</a>");
	}
	$setface = trim($_POST[setface]);
	if($setface=="y")
	{
		$db->sql_query("UPDATE xoops_users SET user_avatar_type = 4 WHERE uid=$userdata[uid]");
	}else
	{
		$db->sql_query("UPDATE xoops_users SET user_avatar_type = 0 WHERE uid=$userdata[uid]");
	}
	message_die(GENERAL_MESSAGE,"感謝您使用$board_config[sitename]提供的虛擬形象。 <a href=".getenv("HTTP_REFERER")." class=nav>返回</a>");
}

////////贈送形象
if ($HTTP_POST_VARS['action']=="song")
{
	$songname = trim($_POST["songusername"]);
	$faceclass = trim($_REQUEST[faceclass]);
    $faceid = trim($_REQUEST[faceid]);
	if ($songname=="")
	{
		message_die(GENERAL_MESSAGE,"你還沒有添寫你的好友的名字！ <a href=".getenv("HTTP_REFERER")." class=nav>返回</a>");
	}else
	{
		$result = $db->sql_query("SELECT uid,uname FROM xoops_users WHERE uname='".addslashes(htmlspecialchars($songname))."'");
		$userna=$db->sql_fetchrow($result);
        if ($userna=="" || $userna[uid]==$userdata[uid])
		{
			message_die(GENERAL_MESSAGE,"你還沒有添寫你的好友的名字！ <a href=".getenv("HTTP_REFERER")." class=nav>返回</a>");
		}
		else
		{

			$result = $db->sql_query("SELECT * FROM week9_userdata WHERE userid = $userdata[uid] and  facepicid = '$faceid' and classid = '$faceclass' and dafaultuse='0'"); 
			$faceidrows =$db->sql_fetchrow($result);
			if ($faceidrows=="")
			{
				message_die(GENERAL_MESSAGE,"你的商品庫裏沒有閒置的此商品，請你購買以後或者解下你佩戴的商品。 <a href=".getenv("HTTP_REFERER")." class=nav>返回</a>");
			}
			else
			{
				$result = $db->sql_query("SELECT * FROM week9_faceset");
				$facesetinfo =$db->sql_fetchrow($result);
				$result = $db->sql_query("SELECT COUNT(*) AS nr FROM week9_userdata WHERE userid = '$userna[userid]' and (dafaultuse = '1' or dafaultuse = '0')"); 
				$facemumrows =$db->sql_fetchrow($result);
				$numnr = $facemumrows[nr];
				if ($numnr > $facesetinfo[yigui])
				{
					message_die(GENERAL_MESSAGE,"對方衣櫃裏面衣服的數量已經達到最大值$facesetinfo[yigui]個，不能在接受贈送的衣服！ <a href=".getenv("HTTP_REFERER")." class=nav>返回</a>");
				}

				$result = $db->sql_query("SELECT * FROM week9_userdata WHERE userid = $userna[uid] and  facepicid = '$faceid' and classid = '$faceclass' and dafaultuse != '9'");
				$rows = $db->sql_fetchrow($result);
				if("" != $rows)
				{
					message_die(GENERAL_MESSAGE,"對方已經存在這個商品，請你換另一個對方不存在的商品贈送。 <a href=".getenv("HTTP_REFERER")." class=nav>返回</a>");
				}
				else
				{
					$db->sql_query("INSERT INTO week9_userdata (userid, classid, facepicid, face_fitherd, face_wear, facenum, dafaultuse, buydata, face_from) VALUES (".$userna[uid].", '".$faceidrows[classid]."', '".$faceidrows[facepicid]."', '".$faceidrows[face_fitherd]."', '".$faceidrows[face_wear]."', '1', '0', '".$faceidrows[buydata]."', ".$userdata[uid].")");
					
					$db->sql_query("DELETE FROM week9_userdata where userid = $userdata[uid] and classid = '".$faceidrows[classid]."' and facepicid = '".$faceidrows[facepicid]."' and dafaultuse = '0' LIMIT 1");
					include_once($phpbb_root_path . 'includes/bbcode.'.$phpEx);
					$sql = "INSERT  INTO phpbb_privmsgs(privmsgs_type,privmsgs_subject, privmsgs_from_userid, privmsgs_to_userid, privmsgs_date, privmsgs_ip)
					VALUES (5,'虛擬形象贈送通知', ".$userdata[uid].",".$userna[uid].",".time(). ",'".$user_ip. "')";
					$db->sql_query($sql);
					$privmsg_sent_id=$db->sql_nextid();
					$sql = "INSERT  INTO phpbb_privmsgs_text(privmsgs_text_id, privmsgs_bbcode_uid, privmsgs_text)
					VALUES ($privmsg_sent_id, '".make_bbcode_uid()."', '您收到了".$userdata[uname]."送給你的虛擬形象，您可以到你的衣櫃佩戴使用。')";
					$db->sql_query($sql);
					$sql = "update xoops_users set user_new_privmsg=1 where uid=".$userna[uid]."";
					$db->sql_query($sql);
					message_die(GENERAL_MESSAGE,"感謝您使用$board_config[sitename]提供的虛擬形象。 <a href=".getenv("HTTP_REFERER")." class=nav>返回</a>");
				}
			}
		}
	} 
}
//##### phpbb templates #######

	$template->set_filenames(array( 'body' => 'face_body.tpl'));
	$template->assign_vars(array(
		'LEFT' => $left,
		'CENTER' => $center,
		'SITENAME'=>$board_config['sitename']
	));
	//
	// Start output of page
	//
	include($phpbb_root_path . 'includes/page_header.' . $phpEx);

	//
	// Generate the page
	//
	$template->pparse('body');

	include($phpbb_root_path . 'includes/page_tail.' . $phpEx);

?>
