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



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 = trim($action);

$sp_fitherd = trim($_REQUEST[sp_fitherd]);

//自動判別性別開始 
if (empty($sp_fitherd)) { 
   if ($userdata[user_gender]==1) $sp_fitherd = "m"; 
   else if ($userdata[user_gender]==2) $sp_fitherd = "f"; 
} 
//自動判別性別結束 

$leibie = trim($_REQUEST[leibie]);

if ($sp_fitherd == "m")
{
	if ($leibie == "")
	{
		$where = "where sp_fitherd = 'm' or sp_fitherd = 't'";
	}
	else
	{
		$where = "where (sp_fitherd = 'm' or sp_fitherd = 't') and sort = '$leibie'";
	}
}
elseif ($sp_fitherd == "f")
{
	if ($leibie == "")
	{
		$where = "where sp_fitherd = 'f' or sp_fitherd = 't'";
	}
	else
	{
		$where = "where (sp_fitherd = 'f' or sp_fitherd = 't') and sort = '$leibie'";		
	}	
}
else
{
	if ($leibie == "")
	{
		$where = "";
	}
	else
	{
		$where = "where sort = '$leibie'";		
	}
}

/******************* 顯示首頁 **********************/
$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)
{
	$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';  //男性的初始值
	$sex = "m";	
}
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);

$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'");
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='$post[userid]' 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;
	}
}
$db->sql_freeresult($facecookie);
$tempequip = implode("-", $cookiearray);
$userwear = implode("-", $weararray);


$cookie = $_REQUEST[tempequip];

$userfacecookie = $tempequip;

if ("" == $cookie)
{	
	if ($userfacecookie != "")
	{
		$tempequip = $userfacecookie;
	}
	setcookie("tempequip", "$tempequip");
}

/************************************************/
$page = $_REQUEST["page"];
$postoffset = intval($_REQUEST["postoffset"]);
$perpage = 10;
if ($postoffset < 0)
{
	$postoffset = 0;
}else
{
	$postoffset = $postoffset;
}
//顯示物品內容開始
$result = $db->sql_query("SELECT COUNT(*) as nr FROM week9_data $where ");
$sqlnum =$db->sql_fetchrow($result);
$nr = $sqlnum[nr];
$pages1 = ceil($nr / $perpage); 
if ($page)
{
	$postoffset = $perpage * ($page-1);
}
else
{
	$page = $postoffset/$perpage+1;
}

if ($postoffset < 0)
{
	$postoffset = 0;
}

if ($postoffset >= $nr)
{
	$postoffset = $nr - 1;
}

$pages = "第".((($postoffset+1) < 0) ? 0 : ($postoffset+1))
    ."-" . ((($postoffset + $perpage) > $nr) ? $nr : ($postoffset+$perpage))."條，";
$pages .= "共<font color=#ff0000'> $nr </font>條記錄 <font color=#ff0000'>$pages1</font>頁&nbsp;&nbsp;";

$pages .= "前往第 <select size=\"1\" onChange=\"window.location.href='$PHP_SELF?s=".$session[sessionhash]."&sp_fitherd=$sp_fitherd&leibie=$leibie&page='+(this.options[this.selectedIndex].value)\">"; 
for($ii=1; $ii<=$pages1; $ii++) { 
    $pages .= "<option value=\"".$ii."\" ".($ii==$page?"selected":"").">".$ii."</option>"; 
} 
$pages .= "      </select> 頁 ";

if ($page > 1)//分頁顯示頁面按鈕
{
	$pages .= "<a href='$PHP_SELF?page=1&sp_fitherd=$sp_fitherd&leibie=$leibie'　class=mainmenu>"
	  ."首頁</a>　<a href='$PHP_SELF?page="
	  .($page-1)."&sp_fitherd=$sp_fitherd&leibie=$leibie'　class=mainmenu>"
	  ."前頁</a>　";
}
else
{
	$pages .= "首頁　前頁　";
}

if ($page < $pages1)//分頁顯示頁面按鈕
{
	$pages .= "<a href='$PHP_SELF?page=".($page+1)."&sp_fitherd=$sp_fitherd&leibie=$leibie'　class=mainmenu>"
	  ."後頁</a>　"
	  ."<a href='$PHP_SELF?page=$pages1&sp_fitherd=$sp_fitherd&leibie=$leibie'　class=mainmenu>"
	  ."末頁</a>";
}
else
{
	$pages .= "　後頁　末頁";
}

$break = 1;
$facedatas = $db->sql_query("SELECT * FROM week9_data $where ORDER BY id desc limit $postoffset, $perpage");
while ($facedata=$db->sql_fetchrow($facedatas))
{
	$result = $db->sql_query("SELECT name1 FROM week9_class WHERE id = '$facedata[sort]'");
	$leibiename = $db->sql_fetchrow($result);
	$facedatasp_graphic = preg_replace("/(.gif)$/", "", $facedata[sp_graphic]);
	$kucun = "$facedata[sp_number]";
	$pic = "<img src = images/face/".$facedata[sort]."/".$facedata[sp_sxgraphic]." height=84 border=0 onClick=\"javascript:CHECK('$facedata[sort]','$facedatasp_graphic','$facedata[sp_fitherd]','$sex')\">";	
	if($facedata[sp_fitherd] == 'm')
	{
		$xingbies = "帥哥"; 
	}elseif($facedata[sp_fitherd] == 'f')
	{
		$xingbies = "美女"; 
	}else
	{
		$xingbies = "全部"; 
	}
	eval('$yifu1 .= "' . gettemplate('face_yifu1') . '";');	
	if ($break%2 == 0)
	{
		$huanhang = "</tr><tr>";
	}
	else
	{
		$huanhang = "";
	}
	$break++;
	$yifu1 = $yifu1.$huanhang;
}
$db->sql_freeresult($facedatas);
//顯示物品內容結束
/*

$caifusql = $db->sql_query("SELECT (user_bank+user_money) as usermoney, username, user_id FROM xoops_users ORDER BY usermoney desc limit 0, 10");

while ($usercaifu = $db->sql_fetchrow($caifusql))
{	
	eval('$caifu .= "' . gettemplate('face_caifu') . '";');
}
*/
$leibie1 = $db->sql_query("SELECT * FROM week9_class where have = '1'");
while ($facesort = $db->sql_fetchrow($leibie1))
{
	if ($leibie == $facesort[id])
	{
		$selected = "selected";
	}
	else
	{
		$selected = "";
	}
	$faceleibiese .= "<option value=\"$facesort[id]\" $selected>$facesort[name1]</option>";
}
$db->sql_freeresult($leibie1);
$faceleibie="
<select name=\"leibie\" onchange=\"location.href='face.php?leibie='+document.form1.leibie.options[document.form1.leibie.selectedIndex].value\"> <option value=\"\">所有分類</option> $faceleibiese	</select>
";
eval("\$left = \"".gettemplate('face_left')."\";");
eval("\$center = \"".gettemplate('face_center')."\";");
//##### phpbb templates #######
$banklocation = ' -> <a href="'.append_sid("face.".$phpEx).'" class="nav">'.$lang['Face'].'</a>'.' -> <a href="'.append_sid("face.".$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);

?>
