<?php
/*============================*\
|| ###########################||
|| # vBulletin PHPFORCE增强包#||
|| # -------------------------||
|| # http://www.phpforce.cn  #||
|| ###########################||
\*============================*/

// ####################### SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);

// #################### DEFINE IMPORTANT CONSTANTS #######################
define('THIS_SCRIPT', 'buypost');

// ################### PRE-CACHE TEMPLATES AND DATA ######################
// get special phrase groups
$phrasegroups = array('postbit');

// get special data templates from the datastore
$specialtemplates = array();

// pre-cache templates used by all actions
$globaltemplates = array('bank_who', 'bank_whobit');

// pre-cache templates used by specific actions
$actiontemplates = array();

// ######################### REQUIRE BACK-END ############################
require_once('./global.php');

// #######################################################################
// ######################## START MAIN SCRIPT ############################
// #######################################################################

//check usergroup of user to see if they can use this
if (!$vbulletin->userinfo['userid'])
{
	print_no_permission();
}

if (empty($_REQUEST['do']))
{
	$_REQUEST['do'] = 'buy';
}

$forumperms = fetch_permissions($threadinfo['forumid']);
if (!($forumperms & $vbulletin->bf_ugp_forumpermissions['canview']) OR !($forumperms & $vbulletin->bf_ugp_forumpermissions['canviewthreads']))
{
	print_no_permission();
}

if (!$threadinfo['threadid'] OR !$postinfo['visible'] OR !$threadinfo['visible'] OR !$postinfo['postid'] OR $threadinfo['isdeleted'] OR $postinfo['isdeleted'])
{
	eval(standard_error(fetch_error('invalidid', $vbphrase['post'], $vbulletin->options['contactuslink'])));
}

// check if there is a forum password and if so, ensure the user has it set
verify_forum_password($foruminfo['forumid'], $foruminfo['password']);

if ($_REQUEST['do'] == 'buy')
{
	$price = unserialize($postinfo['hideparam']);
	$price = $price[4];
			if($vbulletin->options['att_buy_onoff'] && $vbulletin->options['att_buy_add_money'])
			{
				$price = $price+$vbulletin->options['att_buy_money'];
			}
	$buyers = unserialize($postinfo['buyers']);

	// 已购买
	if (is_array( $buyers ) AND @in_array($vbulletin->userinfo['userid'], $buyers))
	{
		// 重定向
		$vbulletin->url = "showthread.php?$session[sessionurl_q]p=$postinfo[postid]#post$postinfo[postid]";
		eval(print_standard_redirect('redirect_buypostthanks'));
		exit;
	}

	// 现金不够
	if ($vbulletin->userinfo['money'] < $price)
	{
		eval(standard_error(fetch_error('cashnotenough')));
	}

	// 修改帖子的购买者 ID 数组
	$buyers[] = intval($vbulletin->userinfo['userid']);
	$totalearn = $price * count($buyers);
	
	$buyers = serialize($buyers);
	$postman =& datamanager_init('Post', $vbulletin, ERRTYPE_ARRAY, 'threadpost');
	$postman->set_existing($postinfo);
	$postman->set('buyers', $buyers);
	$postman->save();

	$userdata =& datamanager_init('User', $vbulletin, ERRTYPE_STANDARD);
	// 扣购买者的钱
	$userdata->set_existing($vbulletin->userinfo);
	$userdata->set('money', 'money - '.$price, false);
	$userdata->save();
	
	if (!$vbulletin->options['maxearn'] OR $totalearn <= $vbulletin->options['maxearn'])
	{
		// 加发帖者的钱
			if($vbulletin->options['att_buy_onoff'] && $vbulletin->options['att_buy_add_money'])
			{
				$price = $price-$vbulletin->options['att_buy_money'];
			}
		$userdata->set_condition('userid = '.$postinfo['userid']);
		$userdata->set('money', 'money + '.$price, false);
		$userdata->save();
	}
	
	// 重定向
	$vbulletin->url = "showthread.php?$session[sessionurl_q]p=$postinfo[postid]#post$postinfo[postid]";
	eval(print_standard_redirect('redirect_buypostthanks'));
}

// ##########################################################################
if ($_REQUEST['do'] == 'who')
{
	$buyers = $db->query_first("
		SELECT buyers
		FROM  " . TABLE_PREFIX . "post
		WHERE postid = $postid
	");
	
	$buyers = array_diff(unserialize($buyers['buyers']),array('',' '));
	
	if (!is_array($buyers))
	{
		eval('$bankwhobits = "' . $vbphrase['noonebought'] . '";');
	}
	else
	{
		$buyers = implode(', ', $buyers);
		
		$users = $db->query_read("
			SELECT userid, username
			FROM " . TABLE_PREFIX . "user
			WHERE userid in (-1, $buyers)
		");
		
		while ($user = $db->fetch_array($users))
		{
			eval('$bankwhobits .= "' . fetch_template('bank_whobit') . '";');
		}
	}
	
	eval('print_output("' . fetch_template('bank_who') . '");');
}

/*========================================*\
|| ########################################
|| Downloaded: Fri Jan 30th 2009 14:59:49
|| ########################################
\*========================================*/
?>
