<?php
/*======================================================================*\
|| #################################################################### ||
|| # ---------------------------------------------------------------- # ||
|| # Copyright ©2013 Fillip Hannisdal AKA Revan/NeoRevan/Belazor 	  # ||
|| # All Rights Reserved. 											  # ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # ---------------------------------------------------------------- # ||
|| # You are not allowed to use this on your server unless the files  # ||
|| # you downloaded were done so with permission.					  # ||
|| # ---------------------------------------------------------------- # ||
|| #################################################################### ||
\*======================================================================*/

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

// #############################################################################
if ($_REQUEST['action'] == 'threadbuy' OR empty($_REQUEST['action']))
{
	$vbulletin->input->clean_array_gpc('r', array(
		'itemid' 	=> TYPE_UINT,
		'shopid' 	=> TYPE_UINT,
		'threadid' 	=> TYPE_UINT,
	));

	if (!$item = VBSHOP::$cache['item'][$vbulletin->GPC['itemid']])
	{
		// Woups, immune
		eval(standard_error(fetch_error('dbtech_vbshop_error_x', construct_phrase($vbphrase['dbtech_vbshop_invalid_x'], $vbphrase['dbtech_vbshop_item'], $vbulletin->GPC['itemid']))));
	}

	if (!$shop = VBSHOP::$cache['shop'][$vbulletin->GPC['shopid']])
	{
		// Woups, immune
		eval(standard_error(fetch_error('dbtech_vbshop_error_x', construct_phrase($vbphrase['dbtech_vbshop_invalid_x'], $vbphrase['dbtech_vbshop_shop'], $vbulletin->GPC['shopid']))));
	}

	if (!$shop['active'])
	{
		// Woups, immune
		eval(standard_error(fetch_error('dbtech_vbshop_error_x', construct_phrase($vbphrase['dbtech_vbshop_invalid_x'], $vbphrase['dbtech_vbshop_shop'], $vbulletin->GPC['shopid']))));
	}

	if (!VBSHOP::checkPermissions($vbulletin->userinfo, $shop['permissions']))
	{
		// This isn't to be displayed
		eval(standard_error(fetch_error('dbtech_vbshop_error_x', construct_phrase($vbphrase['dbtech_vbshop_invalid_x'], $vbphrase['dbtech_vbshop_shop'], $vbulletin->GPC['shopid']))));
	}

	if (!$threadinfo = fetch_threadinfo($vbulletin->GPC['threadid']))
	{
		// Woups, immune
		eval(standard_error(fetch_error('dbtech_vbshop_error_x', construct_phrase($vbphrase['dbtech_vbshop_invalid_x'], $vbphrase['thread'], $vbulletin->GPC['threadid']))));
	}

	if (!$item['active'])
	{
		// Skip this
		eval(standard_error(fetch_error('dbtech_vbshop_error_x', construct_phrase($vbphrase['dbtech_vbshop_invalid_x'], $vbphrase['dbtech_vbshop_item'], $vbulletin->GPC['itemid']))));
	}

	if (!$item['threadpage'])
	{
		// Skip this
		eval(standard_error(fetch_error('dbtech_vbshop_error_x', construct_phrase($vbphrase['dbtech_vbshop_invalid_x'], $vbphrase['dbtech_vbshop_item'], $vbulletin->GPC['itemid']))));
	}
	
	if (!VBSHOP::checkPermissions($vbulletin->userinfo, $item['permissions']))
	{
		// This item isn't to be displayed
		eval(standard_error(fetch_error('dbtech_vbshop_error_x', construct_phrase($vbphrase['dbtech_vbshop_invalid_x'], $vbphrase['dbtech_vbshop_item'], $vbulletin->GPC['itemid']))));
	}

	if (!$category = VBSHOP::$cache['category'][$item['categoryid']])
	{
		// This category isn't to be displayed
		eval(standard_error(fetch_error('dbtech_vbshop_error_x', construct_phrase($vbphrase['dbtech_vbshop_invalid_x'], $vbphrase['dbtech_vbshop_category'], $vbulletin->GPC['categoryid']))));
	}
	
	if (!$category['active'])
	{
		// This category isn't to be displayed
		eval(standard_error(fetch_error('dbtech_vbshop_error_x', construct_phrase($vbphrase['dbtech_vbshop_invalid_x'], $vbphrase['dbtech_vbshop_category'], $vbulletin->GPC['categoryid']))));
	}
	
	if (!VBSHOP::checkPermissions($vbulletin->userinfo, $category['permissions']))
	{
		// This category isn't to be displayed
		eval(standard_error(fetch_error('dbtech_vbshop_error_x', construct_phrase($vbphrase['dbtech_vbshop_invalid_x'], $vbphrase['dbtech_vbshop_category'], $vbulletin->GPC['categoryid']))));
	}
	
	if ($item['unique'] AND VBSHOP::$db->fetchOne('
		SELECT featureid
		FROM $dbtech_vbshop_purchase
		WHERE feature = ?
			AND featureid = ?
			AND userid = ?
	', array(
		'item',
		$item['itemid'],
		$vbulletin->userinfo['userid'],
	)))
	{
		// This item isn't to be displayed
		eval(standard_error(fetch_error('dbtech_vbshop_error_x', construct_phrase($vbphrase['dbtech_vbshop_invalid_x'], $vbphrase['dbtech_vbshop_item'], $vbulletin->GPC['itemid']))));
	}

	if (!$inventory = VBSHOP::$db->fetchRow('
		SELECT item.itemid, item.categoryid, shopinventory.*
		FROM $dbtech_vbshop_shopinventory AS shopinventory
		LEFT JOIN $dbtech_vbshop_item AS item ON(item.itemid = shopinventory.itemid)
		WHERE shopinventory.itemid = ?
			AND shopinventory.shopid = ?
			AND shopinventory.active = \'1\'
	', array(
		$item['itemid'],
		$shop['shopid'],
	)))
	{
		// This item isn't to be displayed
		eval(standard_error(fetch_error('dbtech_vbshop_error_x', construct_phrase($vbphrase['dbtech_vbshop_invalid_x'], $vbphrase['dbtech_vbshop_item'], $vbulletin->GPC['itemid']))));
	}

	if (!VBSHOP::check_price($inventory, $vbulletin->userinfo))
	{
		// Couldn't afford
		eval(standard_error(fetch_error('dbtech_vbshop_error_x', construct_phrase($vbphrase['dbtech_vbshop_invalid_x'], $vbphrase['dbtech_vbshop_item'], $vbulletin->GPC['itemid']))));
	}
	
	if (!$inventory['stock'])
	{
		// Out of stock
		eval(standard_error(fetch_error('dbtech_vbshop_error_x', construct_phrase($vbphrase['dbtech_vbshop_invalid_x'], $vbphrase['dbtech_vbshop_item'], $vbulletin->GPC['itemid']))));
	}	

	// Grab the full item info
	$item = array_merge($item, $inventory);
	
	 // Ensure this is set
	$item['quantity'] = 1;
	
	// Init the item
	$itemobj = VBSHOP::init_item($item);
	
	if (!$purchaseid = $itemobj->purchase($vbulletin->userinfo))
	{
		// Purchase failed
		eval(standard_error(fetch_error('dbtech_vbshop_error_x', construct_phrase($vbphrase['dbtech_vbshop_invalid_x'], $vbphrase['dbtech_vbshop_item'], $vbulletin->GPC['itemid']))));
	}

	if (method_exists($itemobj, 'configure'))
	{
		// Force allow configure
		$itemobj->config['_overrideconfigerror'] = true;

		// Hack
		$_POST['code']['contentid'] = $vbulletin->GPC['threadid'];

		// Do configuration
		$itemobj->configure($purchaseid);
	}
	
	if (method_exists($itemobj, 'bumpThread'))
	{
		// Immediately discard the item
		$itemobj->discard($purchaseid);
	}

	// Success phrase
	$vbulletin->url = 'showthread.php?' . $vbulletin->session->vars['sessionurl'] . 't=' . $vbulletin->GPC['threadid'];
	eval(print_standard_redirect('redirect_dbtech_vbshop_purchasethanks'));
}