<?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 ############################
// #######################################################################

// Grab shopping cart
$shoppingcart = ($vbulletin->userinfo['userid'] ? VBSHOP::$db->fetchRow('
	SELECT *
	FROM $dbtech_vbshop_shoppingcart
	WHERE userid = ?
', array($vbulletin->userinfo['userid'])) : array());

// Ensure this is array
$shoppingcart = (is_array($shoppingcart) ? $shoppingcart : array());

// Fetch list of items
$shoppingcart['items'] = (strlen($shoppingcart['items']) ? @unserialize($shoppingcart['items']) : array());

// #############################################################################
if ($_POST['action'] == 'addtocart')
{
	$vbulletin->input->clean_array_gpc('p', array(
		'shopid'  	  	=> TYPE_UINT,
		'itemid'  	  	=> TYPE_UINT,
	));
	
	if (!$shop = VBSHOP::$cache['shop'][$vbulletin->GPC['shopid']])
	{
		VBSHOP::outputJSON(array(
			'error' => 'error'
		));
	}
	
	if (!$item = VBSHOP::$cache['item'][$vbulletin->GPC['itemid']])
	{
		VBSHOP::outputJSON(array(
			'error' => 'error'
		));
	}
	
	if (!$item['active'])
	{
		VBSHOP::outputJSON(array(
			'error' => 'error'
		));
	}
	
	if (!VBSHOP::checkPermissions($vbulletin->userinfo, $shop['permissions']))
	{
		VBSHOP::outputJSON(array(
			'error' => 'error'
		));
	}
	
	if (!VBSHOP::checkPermissions($vbulletin->userinfo, $item['permissions']))
	{
		VBSHOP::outputJSON(array(
			'error' => 'error'
		));
	}
	
	/*
	if (!((int)$item['shop'] & (int)$shop['bitfield']))
	{
		VBSHOP::outputJSON(array(
			'error' => 'error'
		));
	}
	 */
		
	if (!VBSHOP::checkPermissions($vbulletin->userinfo, VBSHOP::$cache['category'][$item['categoryid']]['permissions']))
	{
		VBSHOP::outputJSON(array(
			'error' => 'error'
		));
	}
	
	if ($item['uniqueitem'])
	{
		if ($uniquecheck = $db->query_first_slave("
			SELECT *		
			FROM " . TABLE_PREFIX . "dbtech_vbshop_purchase
			WHERE feature = 'item'
				AND featureid = " . intval($vbulletin->GPC['itemid']) . "
				AND userid = " . intval($vbulletin->userinfo['userid']) . "
		"))
		{
			// This is an unique item
			VBSHOP::outputJSON(array(
				'error' => 'error'
			));
		}
	}

	// Store item ids
	$itemids = array();
	$itemids[] = $vbulletin->GPC['itemid'];
	foreach ((array)$shoppingcart['items'] as $shopid => $items)
	{
		foreach ($items as $itemid => $quantity)
		{
			if ($itemid == $vbulletin->GPC['itemid'] AND $item['uniqueitem'])
			{
				// This is an unique item
				VBSHOP::outputJSON(array(
					'error' => 'error'
				));
			}

			if (!$cartItem = VBSHOP::$cache['item'][$itemid])
			{
				// Skip this
				unset($shoppingcart['items'][$shopid][$itemid]);
				continue;
			}
			
			if (!$cartItem['active'])
			{
				// Skip this
				unset($shoppingcart['items'][$shopid][$itemid]);
				continue;
			}

			// Store the itemid
			$itemids[] = $itemid;
		}
	}
	
	// Store item info
	$iteminfo = array();
	$iteminfo_q = $db->query_read_slave("
		SELECT *		
		FROM " . TABLE_PREFIX . "dbtech_vbshop_shopinventory
		WHERE itemid IN(" . implode(',', $itemids) . ")
			AND active = 1
	");
	while ($iteminfo_r = $db->fetch_array($iteminfo_q))
	{
		// Index by shopid and itemid
		$iteminfo[$iteminfo_r['shopid']][$iteminfo_r['itemid']] = $iteminfo_r;
	}
	$db->free_result($iteminfo_q);
	unset($iteminfo_r);
	
	if (!is_array($iteminfo[$vbulletin->GPC['shopid']][$vbulletin->GPC['itemid']]))
	{
		VBSHOP::outputJSON(array(
			'error' => 'error'
		));
	}
	
	// Merge these
	$item = array_merge($item, $iteminfo[$vbulletin->GPC['shopid']][$vbulletin->GPC['itemid']]);
	
	$userinfo = $vbulletin->userinfo;
	foreach ((array)$shoppingcart['items'] as $shopid => $items)
	{
		foreach ($items as $itemid => $quantity)
		{
			if (!$currency = VBSHOP::$cache['currency'][$iteminfo[$shopid][$itemid]['currencyid']])
			{
				// This currency doesn't exist anymore
				continue;
			}
							
			// Decrement our available points
			$userinfo[$currency['pointscolumn']] -= ($iteminfo[$shopid][$itemid]['price'] * $quantity);
		}
	}
		
	if (!VBSHOP::check_price($item, $userinfo) OR $item['stock'] == 0)
	{
		VBSHOP::outputJSON(array(
			'error' => 'error'
		));
	}
	
	// Increment the item counter
	$shoppingcart['items'][$vbulletin->GPC['shopid']][$vbulletin->GPC['itemid']]++;
	
	// Update shopping cart
	$db->query_write("
		REPLACE INTO " . TABLE_PREFIX . "dbtech_vbshop_shoppingcart
			(userid, items)
		VALUES (
			" . intval($vbulletin->userinfo['userid']) . ",
			" . $db->sql_prepare(trim(serialize($shoppingcart['items']))) . "
		)
	");
	
	VBSHOP::outputJSON(array(
		'success' => 'success'
	));
}

// #############################################################################
if ($_POST['action'] == 'updatecart')
{
	$vbulletin->input->clean_array_gpc('p', array(
		'cart'   	=> TYPE_ARRAY,
	));
	
	if (!$vbulletin->userinfo['userid'])
	{
		VBSHOP::outputXML(array(
			'error' 		=> 'error',
		));
	}

	foreach ((array)$vbulletin->GPC['cart'] as $shopid => $items)
	{
		foreach ($items as $itemid => $iteminfo)
		{
			if (!$item = VBSHOP::$cache['item'][$itemid])
			{
				// Skip this
				unset($shoppingcart['items'][$shopid][$itemid]);
				continue;
			}
			
			if (!$item['active'])
			{
				// Skip this
				unset($shoppingcart['items'][$shopid][$itemid]);
				continue;
			}

			if (!$iteminfo['quantity'])
			{
				// Remove this item
				unset($shoppingcart['items'][$shopid][$itemid]);
			}
			else
			{
				// Update cart
				$shoppingcart['items'][$shopid][$itemid] = $iteminfo['quantity'];
			}
		}
	}

	// Update shopping cart
	$db->query_write("
		REPLACE INTO " . TABLE_PREFIX . "dbtech_vbshop_shoppingcart
			(userid, items)
		VALUES (
			" . intval($vbulletin->userinfo['userid']) . ",
			" . $db->sql_prepare(trim(serialize($shoppingcart['items']))) . "
		)
	");

	// Include the selected files
	require(DIR . '/dbtech/vbshop/includes/cart.php');	

	VBSHOP::outputXML(array(
		'cartcontents' 	=> $cartcontents,
		'redirect'		=> (!count($shoppingcart['items']) ? '1' : '0')
	));
}

?>