<?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));
}

if(!isset($_GET[action]) && !isset($_POST[action]))
{
	$action="showroom";
}

if ($action=="showroom") {
	//現實保存照片開始
	$albumcookie = $db->sql_query("SELECT * FROM week9_facealbum where userid = $userdata[uid] order by albumtime DESC");
	$break = 1;
	while ($useralbum = $db->sql_fetchrow($albumcookie))
    {
		$userface = $useralbum[useralbum];
		//$week9face = "<img src=pic/$useralbum[useralbum]>";
		$week9face = "<img src=\"$useralbum[useralbum]\" border=\"0\" alt=\"Fly In Beijing\">";
        eval('$xiangpian .= "' . gettemplate('face_album_xiangpian') . '";');
		if ($break%4 == 0)
		{
			$huanhang = "</td></tr><tr><td>";
		}
		else
		{
			$huanhang = "";
		}
		$break++;
		$xiangpian = $xiangpian.$huanhang;
	}
	$db->sql_freeresult($albumcookie);
	//結束	
//##### phpbb templates #######
	$banklocation = ' -> <a href="'.append_sid("face.".$phpEx).'" class="nav">'.$lang['Face'].'</a>'.' -> <a href="'.append_sid("facealbum.".$phpEx).'" class="nav">相冊</a>';
	$template->set_filenames(array( 'body' => 'face_album.tpl'));
	$template->assign_vars(array(
		'BANKLOCATION' => $banklocation,
		'LEFT' => $left,
		'CENTER' => $xiangpian,
		'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);

}

/////////////////////添加相冊信息
if ($_GET[action] == "album")
{
	$alumcookie = "";
	$alumcookie =  $_COOKIE[tempequip];
	if ("" == $alumcookie)
	{
		message_die(GENERAL_MESSAGE,"變數錯誤！ <a href=".getenv("HTTP_REFERER")." class=nav>返回</a>");
	}
	$usercookie = $alumcookie;
	$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 = "帥哥";	
	}
	elseif($gender == 2)
	{
		$usergender = "美女";	
	}
	else
	{
		message_die(GENERAL_MESSAGE,"你還沒有選擇性別，本功能需要用戶選擇性別，如果你要使用本功能，請你返回修改你的論壇性別！ <a href=".getenv("HTTP_REFERER")." class=nav>返回</a>");
	}
	eval("\$left = \"".gettemplate('face_left')."\";");
	eval("\$center = \"".gettemplate('face_album_center')."\";");
//##### 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);
}

/////////////////////添加相冊信息
if ($_POST[action] == "savealbum")
{
	if(!extension_loaded("gd"))
	{
		message_die(GENERAL_MESSAGE,"主機沒安裝GD library <a href=".getenv("HTTP_REFERER")." class=nav>返回</a>");
	}
	$usercookie=$_POST[usercookie];
	$albumname=$_POST[albumname];
	$albumjianjie=$_POST[albumjianjie];
	if ($usercookie=="")
	{
		message_die(GENERAL_MESSAGE,"變數錯誤！ <a href=".getenv("HTTP_REFERER")." class=nav>返回</a>");
	}

	$albumname = htmlspecialchars(addslashes($_REQUEST[albumname]));
	$albumjianjie = htmlspecialchars(addslashes($_REQUEST[albumjianjie]));

	if (("" == $albumname) or (strlen($albumname) > 50 ))
	{
		message_die(GENERAL_MESSAGE,"相片名稱錯誤或者為空！ <a href=".getenv("HTTP_REFERER")." class=nav>返回</a>");
	}
	if (("" == $albumjianjie) or (strlen($albumjianjie) > 200 ))
	{
		message_die(GENERAL_MESSAGE,"相片簡介錯誤或者為空！ <a href=".getenv("HTTP_REFERER")." class=nav>返回</a>");
	}
	$facesetinfo = $result = $db->sql_query("SELECT * FROM week9_faceset");
	if ($userdata[user_money] < $facesetinfo[beiyong])
	{
		message_die(GENERAL_MESSAGE,"你的金幣不夠！請賺夠了錢再來吧！ <a href=".getenv("HTTP_REFERER")." class=nav>返回</a>");
	}

	$result = $db->sql_query("SELECT * FROM week9_faceset");
	$facesetinfo = $db->sql_fetchrow($result);
    $nr = $faceidrows[nr];
	if ($nr > 8)
	{
		message_die(GENERAL_MESSAGE,"你的相冊裏面的相片數量已經達到最大值8個，如果你還要保存請你刪除你的相冊裏面的不喜歡的形象！ <a href=".getenv("HTTP_REFERER")." class=nav>返回</a>");
	}

	$dstidfer = imagecreatetruecolor(140,226);//This function was added in PHP 4.0.6 and requires GD 2.0.1 or later
	//$dstidfer = imagecreate(140,226); // GD 1.x版的gd 請使用這段語法

	$black = imagecolorallocate ($dstidfer, 0, 0,0);
	$white = imagecolorallocate($dstidfer, 255, 255, 255);
	imagefill($dstidfer, 0, 0, $white);
	$cookiearray = split('-', $usercookie);
	for ($i=1; $i<26; $i++)
	{
		if ($cookiearray[6] != '0')
		{
			$cookiearray[8] = '0';
			$cookiearray[9] = '0';
		}
		if($cookiearray[$i] != '0')
		{
			$pic = "images/face/".$i."/".$cookiearray[$i].".gif";
			$srcidfer = imagecreatefromgif($pic);
			imagecopyresized($dstidfer, $srcidfer, 0, 0, 0, 0, 140, 226, 140, 226);
		}			
	}
	$dt = date("YmdHms");
	$string = date("Y/m/d");
	$picpath = "pic/".$userdata[uid]."_".$dt.".jpg";
	imagestring ($dstidfer, 2, 2, 2,  $string, $black);
	imagejpeg($dstidfer, "$picpath");
	imagedestroy($srcidfer);
	imagedestroy($dstidfer);
	$db->sql_query("INSERT INTO week9_facealbum (userid, useralbum, albumname, albumjianjie, albumtime) VALUES ('".$userdata[uid]."', '$picpath', '$albumname', '$albumjianjie', '".time()."')");
    $db->sql_query("UPDATE xoops_users SET user_money=user_money-$facesetinfo[beiyong] WHERE uid=$userdata[uid]");
	message_die(GENERAL_MESSAGE,"感謝您使用$board_config[sitename]提供的虛擬形象。相片保存成功！ <a href=".append_sid("facealbum.$phpEx")." class=nav>相冊</a>");
}

/////////////////////刪除相冊資訊
if ($_GET[action] == "delalbum")
{
	if ($_GET[id]=="")
	{
		message_die(GENERAL_MESSAGE,"變數錯誤！ <a href=".getenv("HTTP_REFERER")." class=nav>返回</a>");
	}
    $picrow = $result = $db->sql_query("SELECT * from week9_facealbum where id='$_GET[id]' and userid = $userdata[uid]"); 
	$db->sql_query("DELETE from week9_facealbum where id='$_GET[id]' and userid = $userdata[uid]");
	
	function delete_file($file)
	{ 
		$delete = @unlink($file); 
		clearstatcache();
		if (@file_exists($file))
		{
			$filesys = eregi_replace("/","\\",$file); 
			$delete = @system("del $filesys");
			clearstatcache();
			if (@file_exists($file))
			{
				$delete = @chmod ($file, 0777); 
				$delete = @unlink($file); 
				$delete = @system("del $filesys");
			}
		 }
		 clearstatcache();
		 if (@file_exists($file))
		 {
			 return false;
		 }
		 else
		 {
			 return true;
		 }
	} 
	delete_file($picrow[useralbum]);	
	message_die(GENERAL_MESSAGE,"感謝您使用$board_config[sitename]提供的虛擬形象。相片刪除成功！ <a href=".getenv("HTTP_REFERER")." class=nav>返回</a>");
}
?>
