<?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 = "face.$phpEx";
	$redirect .= ( isset($user_id) ) ? '&user_id=' . $user_id : '';
	header('Location: ' . append_sid("login.$phpEx?redirect=$redirect", true));
}

$action = $_REQUEST[action];
$faceclass = $_REQUEST[classid];
$faceid = trim($_REQUEST[id]);
$cookie = $_REQUEST[tempequip];
$leibie = $_REQUEST[leibie];

if ($action == "buy")
{
	$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 = $userdata[uid] 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>");
	}

	$pic = $faceid.".gif";
	$result = $db->sql_query("SELECT * FROM week9_data WHERE sp_graphic = '$pic' and sort = '$faceclass'"); 
	$faceinfo = $db->sql_fetchrow($result);
	if ($faceinfo[sp_number] <= 0)
	{
		message_die(GENERAL_MESSAGE,"庫存不足！請聯繫管理員增加商品！ <a href=".getenv("HTTP_REFERER")." class=nav>返回購物中心</a>");
	}
	
	if ($userdata[user_money] < $faceinfo[sp_money])
	{
		message_die(GENERAL_MESSAGE,"你的金幣不夠！請賺夠了錢再來買吧！ <a href=".getenv("HTTP_REFERER")." class=nav>返回購物中心</a>");
	}
	else
	{
		$result = $db->sql_query("SELECT COUNT(*) AS nr FROM week9_userdata WHERE userid = $userdata[uid] and  facepicid = '$faceid' and classid = '$faceclass'"); 
		$faceidrows = $db->sql_fetchrow($result);
		$nr = $faceidrows[nr];
		if (0 != $nr)
		{
			message_die(GENERAL_MESSAGE,"你已經購買了這個產品，如果你想送給別人，那請你點擊贈送按鈕 <a href=".getenv("HTTP_REFERER")." class=nav>返回購物中心</a>");
		}
		else
		{
			$facenum = 1;
			$dafaultuse = 0;
			$db->sql_query("INSERT INTO week9_userdata (userid, classid, facepicid, face_fitherd, face_wear, facenum, dafaultuse, buydata) VALUES (".$userdata[uid].", '".$faceinfo[sort]."', '$faceid', '".$faceinfo[sp_fitherd]."', '".$faceinfo[sp_wear]."', '$facenum', '$dafaultuse', ".time().")");            
			$db->sql_query("UPDATE xoops_users SET user_money=user_money-$faceinfo[sp_money] WHERE uid=$userdata[uid]");
			$db->sql_query("UPDATE week9_data SET sp_number=sp_number-1 WHERE sp_graphic = '$pic' and sort = '$faceclass'"); 
		} 
		message_die(GENERAL_MESSAGE,"感謝您使用$board_config[sitename] 提供的虛擬形象。形象購買成功！ <a href=".getenv("HTTP_REFERER")." class=nav>返回購物中心</a>");
	}
}

if ($action=="saveallface")
{
	$cookiearray = split('-', $cookie);
	$c_num = count($cookiearray);
    $sex2 = $userdata[user_gender];
	if ($sex2 == '1')
	{
		$d_sex = 'init';
	}
	elseif($sex2 == '2')
	{
		$d_sex = 'initf';
	}
	else
	{
		message_die(GENERAL_MESSAGE,"你沒有選擇性別，不能進行此操作！ <a href=".getenv("HTTP_REFERER")." class=nav>返回衣櫃</a>");
	}
	$db->sql_query("UPDATE week9_userdata SET dafaultuse = '0'	where userid = $userdata[uid] and dafaultuse = '1'");    
	for ($i = 0; $i < 26; $i++)
	{
		if(($cookiearray[$i] != '0') and ($cookiearray[$i] != $d_sex)) 
		{
			$pic = $cookiearray[$i].".gif";
			$result = $db->sql_query("SELECT COUNT(*) AS nr FROM week9_userdata WHERE userid = $userdata[uid] and  facepicid = '$cookiearray[$i]' and classid = '$i'"); 			
			$faceidrows = $db->sql_fetchrow($result);
			$nr = $faceidrows[nr];
			if (0 != $nr)
			{
				$result = $db->sql_query("SELECT * FROM week9_data WHERE sp_graphic = '$pic' and sort = '$i'");				
				$faceinfo = $db->sql_fetchrow($result);
				$dafaultuse = 1;
				$db->sql_query("UPDATE week9_userdata SET 
										  dafaultuse = '$dafaultuse'
								where userid = $userdata[uid] and classid = '$faceinfo[sort]' and facepicid = '$cookiearray[$i]' 
								 ");
				
			}
			else
			{
				message_die(GENERAL_MESSAGE,"你沒有購買這個商品，請先購買！ <a href=".getenv("HTTP_REFERER")." class=nav>返回衣櫃</a>");
			}
		}
	}
	message_die(GENERAL_MESSAGE,"感謝您使用$board_config[sitename] 提供的虛擬形象。形象修改成功！ <a href=".getenv("HTTP_REFERER")." class=nav>返回衣櫃</a>");
}

if ($action=="delface")
{	
	$db->sql_query("UPDATE week9_userdata SET dafaultuse = '0' where userid = $userdata[uid] and classid = '$faceclass' and facepicid = '$faceid' and dafaultuse = '1'");	
	message_die(GENERAL_MESSAGE,"感謝您使用$board_config[sitename] 提供的虛擬形象。形象成功脫下！ <a href=".getenv("HTTP_REFERER")." class=nav>返回衣櫃</a>");
}

if ($action=="delbuyface")
{
	$db->sql_query("UPDATE week9_userdata SET dafaultuse = '3' where userid = $userdata[uid] and classid = '$faceclass' and facepicid = '$faceid' and dafaultuse = '0' LIMIT 1");	
	message_die(GENERAL_MESSAGE,"感謝您使用$board_config[sitename] 提供的虛擬形象。形象成功放進舊衣服。 <a href=".getenv("HTTP_REFERER")." class=nav>返回衣櫃</a>");
}

if ($action=="delfavoriteface")
{
	$db->sql_query("delete from week9_userdata where userid = $userdata[uid] and classid = '$faceclass' and facepicid = '$faceid' and dafaultuse = '9' LIMIT 1");	
	message_die(GENERAL_MESSAGE,"感謝您使用$board_config[sitename] 提供的虛擬形象。收藏的形象已經成功刪除。 <a href=".getenv("HTTP_REFERER")." class=nav>返回收藏夾</a>");
}

if ($action=="setfavorite")
{
	$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 = $userdata[uid] and dafaultuse = '9'"); 
	$facemumrows = $db->sql_fetchrow($result);
    $numnr = $facemumrows[nr];
	if ($numnr > $facesetinfo[shoucang])
	{
		message_die(GENERAL_MESSAGE,"你的收藏夾裏面衣服的數量已經達到最大值$facesetinfo[shoucang]個，請你整理收藏夾！ <a href=".getenv("HTTP_REFERER")." class=nav>返回購物中心</a>");
	}
	$pic = $faceid.".gif";
	$result = $db->sql_query("SELECT * FROM week9_data WHERE sp_graphic = '$pic' and sort = '$faceclass'");	
	$faceinfo = $db->sql_fetchrow($result);
	$result = $db->sql_query("SELECT COUNT(*) AS nr FROM week9_userdata WHERE userid = $userdata[uid] and  facepicid = '$faceid' and classid = '$faceclass'"); 
	$faceidrows = $db->sql_fetchrow($result);
	$nr = $faceidrows[nr];
	if (0 != $nr)
	{
		message_die(GENERAL_MESSAGE,"你已經購買或者收藏了這個產品！！ <a href=".getenv("HTTP_REFERER")." class=nav>返回購物中心</a>");
	}
	else
	{
		$facenum = 1;
		$dafaultuse = 9;
		$db->sql_query("INSERT INTO week9_userdata (userid, classid, facepicid, face_fitherd, face_wear, facenum, dafaultuse, buydata) VALUES (".$userdata[uid].", '".$faceinfo[sort]."', '$faceid', '".$faceinfo[sp_fitherd]."', '".$faceinfo[sp_wear]."', '$facenum', '$dafaultuse', ".time().")"); 		
	}
	message_die(GENERAL_MESSAGE,"感謝您使用$board_config[sitename] 提供的虛擬形象。形象收藏成功！ <a href=".getenv("HTTP_REFERER")." class=nav>返回購物中心</a>");
}

if ($action=="buyfavorite")
{
	$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 = $userdata[uid]  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>");
	}
	$pic = $faceid.".gif";
	$result = $db->sql_query("SELECT * FROM week9_data WHERE sp_graphic = '$pic' and sort = '$faceclass'"); 
	$faceinfo = $db->sql_fetchrow($result);
	if ($faceinfo[sp_number] <= 0)
	{
		message_die(GENERAL_MESSAGE,"庫存不足！ <a href=".getenv("HTTP_REFERER")." class=nav>返回收藏夾</a>");
	}
	if ($userdata[user_money] < $faceinfo[sp_money])
	{
		message_die(GENERAL_MESSAGE,"你的金幣不夠！請賺夠了錢再來買吧！ <a href=".getenv("HTTP_REFERER")." class=nav>返回收藏夾</a>");
	}
	else
	{
		$result = $db->sql_query("SELECT COUNT(*) AS nr FROM week9_userdata WHERE userid = $userdata[uid] and  facepicid = '$faceid' and classid = '$faceclass' and dafaultuse != '9'"); 
		$faceidrows = $db->sql_fetchrow($result);
		$nr = $faceidrows[nr];
		if (0 != $nr)
		{
			message_die(GENERAL_MESSAGE,"你已經購買了這個產品，如果你想送給別人，那請你點擊贈送按鈕 <a href=".getenv("HTTP_REFERER")." class=nav>返回收藏夾</a>");
		}
		else
		{
			$db->sql_query("UPDATE week9_userdata SET dafaultuse = '0' , buydata =  ".time()." WHERE userid=$userdata[uid] and facepicid = '$faceid' and classid= '$faceinfo[sort]' and dafaultuse='9'");          
			$db->sql_query("UPDATE xoops_users SET user_money=user_money-$faceinfo[sp_money] WHERE uid=$userdata[uid]");
			$db->sql_query("UPDATE week9_data SET sp_number=sp_number-1 WHERE sp_graphic = '$pic' and sort = '$faceclass'"); 
		}
		message_die(GENERAL_MESSAGE,"感謝您使用$board_config[sitename] 提供的虛擬形象。形象購買成功！ <a href=".getenv("HTTP_REFERER")." class=nav>返回收藏夾</a>");
	}
}

if ($action=="chongzhi")
{
	$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 = $userdata[uid] 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>");
	}
	$pic = $faceid.".gif";
	$result = $db->sql_query("SELECT * FROM week9_data WHERE sp_graphic = '$pic' and sort = '$faceclass'");	
	$faceinfo = $db->sql_fetchrow($result);
	if ($userdata[user_money] < $faceinfo[sp_money])
	{
		message_die(GENERAL_MESSAGE,"你的金幣不夠！請賺夠了錢再來充值吧！ <a href=".getenv("HTTP_REFERER")." class=nav>返回舊衣服</a>");
	}
	else
	{		
		$result = $db->sql_query("SELECT COUNT(*) AS nr FROM week9_userdata WHERE userid = $userdata[uid] and  facepicid = '$faceid' and classid = '$faceclass'  and (dafaultuse = '1' or dafaultuse = '0')"); 
		$faceidrows = $db->sql_fetchrow($result);
		$nr = $faceidrows[nr];
		if (0 != $nr)
		{
			message_die(GENERAL_MESSAGE,"你已經購買了這個產品，不能在充值了，請你直接刪除吧 <a href=".getenv("HTTP_REFERER")." class=nav>返回舊衣服</a>");
		}
		else
		{			
			$db->sql_query("UPDATE week9_userdata SET dafaultuse = '0' , buydata =  ".time()." WHERE userid=$userdata[uid] and facepicid = '$faceid' and classid= '$faceinfo[sort]' and dafaultuse='3' LIMIT 1");           
			$db->sql_query("UPDATE xoops_users SET user_money=user_money-$faceinfo[sp_money] WHERE uid=$userdata[uid]");
		} 
		message_die(GENERAL_MESSAGE,"感謝您使用$board_config[sitename] 提供的虛擬形象。形象充值成功！ <a href=".getenv("HTTP_REFERER")." class=nav>返回舊衣服</a>");
	}
}

if ($action=="deleteface")
{
	$pic = $faceid.".gif";
	$db->sql_query("delete from week9_userdata where userid = $userdata[uid] and classid = '$faceclass' and facepicid = '$faceid' and dafaultuse = '3' LIMIT 1");
	$db->sql_query("UPDATE week9_data SET sp_number=sp_number+1 WHERE sp_graphic = '$pic' and sort = '$faceclass'"); 
	$result = $db->sql_query("SELECT * FROM week9_faceset");	
	$facesetinfo = $db->sql_fetchrow($result);
	$result = $db->sql_query("SELECT * FROM week9_data WHERE sp_graphic = '$pic' and sort = '$faceclass'");
	$faceinfo =  $db->sql_fetchrow($result);
	$jiaoyiface = floor($faceinfo[sp_money]/$facesetinfo[zhekou]);
	$db->sql_query("UPDATE xoops_users SET user_money=user_money+$jiaoyiface WHERE uid=$userdata[uid]");
	message_die(GENERAL_MESSAGE,"感謝您使用$board_config[sitename] 提供的虛擬形象。形象成功刪除！ <a href=".getenv("HTTP_REFERER")." class=nav>返回舊衣服</a>");
}

if ($action=="jiaoyi")
{
	$beizhu = htmlspecialchars(addslashes($_REQUEST[beizhu]));	
	$sellprice=trim(addslashes($_REQUEST[sellprice]));
//    for($i=0;$i<strlen($sellprice);$i++)
    if (!is_numeric($sellprice) || empty($sellprice))
    {
		message_die(GENERAL_MESSAGE,"金錢數值錯誤！！ <a href=javascript:history.back(); class=nav>返回</a>");
    }
	if (strlen($beizhu) > 200 )
	{
		message_die(GENERAL_MESSAGE,"備註太長！ <a href=javascript:history.back(); class=nav>返回</a>");
	}
	
	$result = $db->sql_query("SELECT * FROM week9_faceset");
	$facesetinfo = $db->sql_fetchrow($result);
	$facesetinfo[ershou];
	$result = $db->sql_query("SELECT COUNT(*) AS nr FROM week9_ershou"); 
	$facemumrows = $db->sql_fetchrow($result);
    $numnr = $facemumrows[nr];
	if ($numnr > $facesetinfo[ershou])
	{
		message_die(GENERAL_MESSAGE,"二手市場裏的商品數量已經達到最大值$facesetinfo[ershou]個，請等到商品交易完畢或者系統清理以後在操作！<a href=".append_sid("facebad.$phpEx")." class=nav>返回舊衣服</a>");
	}
	$result = $db->sql_query("SELECT * FROM week9_userdata WHERE userid = $userdata[uid] and classid = '$faceclass' and facepicid = '$faceid' and dafaultuse = '3' LIMIT 1");
	$faceinfo = $db->sql_fetchrow($result);
	$db->sql_query("INSERT INTO week9_ershou (userid, classid, facepicid, face_fitherd, face_wear, sellprice, buydata, selldata, beizhu) VALUES (".$userdata[uid].", '".$faceinfo[classid]."', '".$faceinfo[facepicid]."', '".$faceinfo[face_fitherd]."', '".$faceinfo[face_wear]."', '$sellprice', '".$faceinfo[buydata]."', ".time().", '$beizhu')"); 
	$db->sql_query("delete from week9_userdata where userid = $userdata[uid] and classid = '$faceclass' and facepicid = '$faceid' and dafaultuse = '3' LIMIT 1");
	message_die(GENERAL_MESSAGE,"感謝您使用$board_config[sitename] 提供的虛擬形象。你的商品已經成功的進入交易大廳 <a href=".append_sid("facebad.$phpEx")." class=nav>返回舊衣服</a>");
}

if ($action=="buyershouface")
{
	$userid = $_REQUEST[userid];
	$pic = $faceid.".gif";	
	$result = $db->sql_query("SELECT * FROM week9_ershou where userid = '$userid' and classid = '$faceclass' and facepicid = '$faceid'");
	$faceinfo = $db->sql_fetchrow($result);
    $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 = $userdata[uid]  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>");
	}
	if ($userdata[user_money] < $faceinfo[sellprice])
	{
		message_die(GENERAL_MESSAGE,"你的金幣不夠！請賺夠了錢再來買吧！ <a href=".getenv("HTTP_REFERER")." class=nav>返回拍賣場</a>");
	}
	else
	{
		$result = $db->sql_query("SELECT COUNT(*) AS nr FROM week9_userdata WHERE userid = $userdata[uid] and  facepicid = '$faceid' and classid = '$faceclass'"); 
		$faceidrows = $db->sql_fetchrow($result);
		$nr = $faceidrows[nr];
		if (0 != $nr)
		{
			message_die(GENERAL_MESSAGE,"你已經購買了這個產品，如果你想送給別人，那請你點擊贈送按鈕 <a href=".getenv("HTTP_REFERER")." class=nav>返回拍賣場</a>");
		}
		else
		{
			$db->sql_query("UPDATE xoops_users SET user_money=user_money-$faceinfo[sellprice] WHERE uid=$userdata[uid]");
	        $db->sql_query("UPDATE xoops_users SET user_money=user_money+$faceinfo[sellprice] WHERE uid=$userid");
	        $db->sql_query("INSERT INTO week9_userdata (userid, classid, facepicid, face_fitherd, face_wear, facenum, dafaultuse, buydata) VALUES (".$userdata[uid].", '".$faceinfo[classid]."', '".$faceinfo[facepicid]."', '".$faceinfo[face_fitherd]."', '".$faceinfo[face_wear]."', '1', '3', '".$faceinfo[buydata]."')");
	        $db->sql_query("delete from week9_ershou where userid = $userid and classid = '$faceclass' and facepicid = '$faceid'");
			message_die(GENERAL_MESSAGE,"感謝您使用$board_config[sitename] 提供的虛擬形象。形象購買成功並放入舊衣服裡 <a href=".getenv("HTTP_REFERER")." class=nav>返回拍賣場</a>");
		}
	}
}
?>
