<?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 = "./";
require($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);
require($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));
}

/******************* 顯示首頁 **********************/
$userbankmoney = $userdata[user_bank];
$usernm = $userdata[uname];
$usermoney = $userdata[user_money];
$allmoney = $userdata[user_money]+$userdata[user_bank];
$gender = $userdata[user_gender];

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,"你還沒有選擇性別，本功能需要用戶選擇性別，如果你要使用本功能，請你返回修改你的論壇性別！");
}

$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);

$userwearip = '400-400-400-400-400-400-400-400-400-400-400-400-400-400-400-400-400-400-400-400-400-400-400-400-400-400';//商品的過期屬性

$cookiearray = split('-', $tempequip);
$weararray = split('-', $userwearip);
//顯示佩戴物品開始
$facecookie = $db->sql_query("SELECT * FROM week9_userdata where userid = $userdata[uid]  and dafaultuse = '1'");
$break = 1;
while ($usercaifu = $db->sql_fetchrow($facecookie))
{	
	$tempi = $usercaifu[classid];
	$cookiearray[$tempi] = $usercaifu[facepicid];
	$face_weardate = floor($usercaifu[face_wear] - (time() - $usercaifu[buydata])/8640);
	if ($face_weardate <= 0)
	{
		$db->sql_query("UPDATE week9_userdata SET dafaultuse = '3' WHERE userid=$userdata[uid] and facepicid = '$usercaifu[facepicid]' and classid= '$usercaifu[classid]' and dafaultuse='1'");
		if($usercaifu[classid] == 8||$usercaifu[classid]==9||$usercaifu[classid]==11||$usercaifu[classid]==13||$usercaifu[classid]==14||$usercaifu[classid]==18)
		{
			$userupid = $upid;			
		}
		else
		{
			$userupid = "0";
		}
		$weararray[$tempi] = $userupid;
	}
	else
	{
		$weararray[$tempi] = $face_weardate;
	}	
	
	$buydate = create_date($board_config['default_dateformat'], $usercaifu[buydata], $board_config['board_timezone']);
	if ("" == $usercaifu[face_from])
	{
		$facefrom = "個人購買";
	}
	else
	{
		$result = $db->sql_query("SELECT  username FROM xoops_users WHERE user_id='$usercaifu[face_from]'");
		$usernam=$db->sql_fetchrow($result);
		$facefrom = "$usernam[username]贈送";
	}
	$pic = "<td align=center><a href=facebuy.php?action=delface&classid=$usercaifu[classid]&id=$usercaifu[facepicid]><IMG src = images/face/".$usercaifu[classid]."/".$usercaifu[facepicid]."x.gif height=84 border=0 alt=\"點擊滑鼠左鍵脫下&#13;&#10;購買日期：$buydate&#13;&#10;來自：$facefrom\"></a>";
	eval('$peidai .= "' . gettemplate('face_data_peidai') . '";');	
	if ($break%2 == 0)
	{
		$huanhang = "</tr><tr>";
	}
	else
	{
		$huanhang = "</td>";
	}
	$break++;
	$peidai = $peidai.$huanhang;
}
$db->sql_freeresult($facecookie);
$tempequip = implode("-", $cookiearray);
$userwear = implode("-", $weararray);
$cookie = $_REQUEST[tempequip];
$userfacecookie = $tempequip;
setcookie("tempequip", "$tempequip");
//顯示佩戴物品結束

//顯示未佩戴物品開始

$facedatas = $db->sql_query("SELECT * FROM week9_userdata where userid = $userdata[uid] and dafaultuse = '0'");
$break = 1;
while ($facedata=$db->sql_fetchrow($facedatas))
{
	
    if ($facedata[face_from]=="")
		{
			$facefrom = "個人購買";
		}
		else
		{
			$result = $db->sql_query("SELECT username FROM xoops_users WHERE user_id='$facedata[face_from]'");
			$usernam=$db->sql_fetchrow($result);
			$facefrom = "$usernam[username]贈送";
		}
/*
	$result = $db->sql_query("SELECT sp_name FROM week9_data WHERE sort = '$facedata[classid]' and sp_graphic = '$facedata[facepicid].gif'");
	$spname = $db->sql_fetchrow($result);
*/
	$buydate = create_date($board_config['default_dateformat'], $facedata[buydata], $board_config['board_timezone']);
	$pic = "<img style=\"FILTER: alpha(opacity=299.177); CURSOR: hand\" alt=\"點擊滑鼠左鍵試穿&#13;&#10;購買日期：$buydate&#13;&#10;來　　自：$facefrom\" src = images/face/".$facedata[classid]."/".$facedata[facepicid]."x.gif height=84 border=0 onClick=\"javascript:CHECK('$facedata[classid]','$facedata[facepicid]','$facedata[face_fitherd]','$sex')\">";
	if($facedata[face_fitherd] == 'm')
	{
		$face_fitherd = "帥哥";
	}
	elseif($facedata[face_fitherd] == 'f')
	{
		$face_fitherd = "美女";
	}
	else
	{
		$face_fitherd = "全部";
	}
	//剩餘天數
	$face_weardate = floor($facedata[face_wear]/10 - (time() - $facedata[buydata])/86400);	

	eval('$yifu1 .= "' . gettemplate('face_data_yifu1') . '";');	
	if ($break%3 == 0)
	{
		$huanhang = "</tr><tr>";
	}
	else
	{
		$huanhang = "";
	}
	$break++;
	$yifu1 = $yifu1.$huanhang;
}
$db->sql_freeresult($facedatas);

//顯示未佩戴物品結束

eval("\$left = \"".gettemplate('face_data_left')."\";");
eval("\$center = \"".gettemplate('face_data_center')."\";");
//##### phpbb templates #######
$banklocation = ' -> <a href="'.append_sid("face.".$phpEx).'" class="nav">'.$lang['Face'].'</a>'.' -> <a href="'.append_sid("facedata.".$phpEx).'" class="nav">衣櫃</a>';
$template->set_filenames(array( 'body' => 'face_body.tpl'));
$template->assign_vars(array(
	'BANKLOCATION' => $banklocation,
	'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);

?>
