<?php

/*********************************************************************************************
CartPlog Lite License
==============================================================================================
This software is as-is, no warranty of any kind, use at your own risk. You are granted free
use of this software. However, you are not permitted under any circumstances to redistribute
the software, whether in whole or in part. Also, the 'powered by' links must remain intact
and visible. Make sure to understand that this is *not GPL software. Do not redistribute it,
and do not use parts of it in other software. This software is copyrighted in its entirety
to 'calorie' a/k/a © ThinkDing LLC 2010, all rights reserved. The software author/entity are
in no way responsible for your site content. All copyright notices and 'powered by' links
must not be changed or removed. Do not use this software if obtained from a warez site!
*********************************************************************************************/

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

// #################### DEFINE IMPORTANT CONSTANTS ########################
define('THIS_SCRIPT', 'cartplog');
define('CSRF_PROTECTION', true);
define('CARTPLOG_VERSION', 'Lite v.3.0.0');

if (empty($_REQUEST['do']))
{
	$_REQUEST['do'] = 'viewindex';
}
if ($_REQUEST['do'] == 'postback')
{
	define('SKIP_SESSIONCREATE', 1);
}

// ################### PRE-CACHE TEMPLATES AND DATA #######################
$phrasegroups = array('cartplogfrontend');
$specialtemplates = array();
$globaltemplates = array();
$actiontemplates = array(
	'checkout' => array(
		'cartplog_basket_item',
		'cartplog_shell_blank',
		'cartplog_view_cart'
	),
	'viewcart' => array(
		'cartplog_basket_item',
		'cartplog_shell_blank',
		'cartplog_view_cart'
	),
	'viewcategory' => array(
		'cartplog_category_item',
		'cartplog_shell_blank',
		'cartplog_view_category'
	),
	'viewhistory' => array(
		'cartplog_history_item',
		'cartplog_history_list',
		'cartplog_shell_blank',
		'cartplog_view_history'
	),
	'viewindex' => array(
		'cartplog_category_list',
		'cartplog_basket_summary',
		'cartplog_product_item',
		'cartplog_shell_blank',
		'cartplog_view_index'
	),
	'viewitem' => array(
		'cartplog_shell_blank',
		'cartplog_view_item'
	)
);

// ####################### REQUIRE VB BACK-END ############################
// cartplog in main forum directory
require_once('./global.php');
require_once(DIR . '/includes/cartplog_functions.php');

// ###################### CHECK CARTPLOG ACCESS ###########################
if ($_REQUEST['do'] != 'postback' && !$vbulletin->options['cartplog_active'])
{
	print_no_permission(); // allow paypal to post back even if cartplog is disabled so sales get recorded !!!
}
if ($_REQUEST['do'] == 'postback')
{
	$vbulletin->nozip = true; // set no zip for paypal post backs
}

// #################### INITIALIZE SOME VARIABLES #########################
$cartplog = array(); // do not remove this !!!

// do not hide, change, remove, etcetera !!!
$cartplog['powered_by'] = '<br /><div class="css_smallfont" align="center"><a href="http://www.photoplog.com/">' . $vbphrase['cartplog_powered_by'] . ' ' . CARTPLOG_VERSION . '</a></div>';

$cartplog_navbits = $vbphrase['cartplog'];

// ##################### SET CURRENCY INFORMATION #########################
$cartplog['currencies'] = array(1 => 'AUD', 2 => 'CAD', 3 => 'EUR', 4 => 'GBP', 5 => 'USD'); // keep this section as-is
$cartplog['currency_code'] = $cartplog['currencies'][$vbulletin->options['cartplog_currency']]; // the default currency
$cartplog['currencies'] = array($vbulletin->options['cartplog_currency'] => $cartplog['currency_code']);

if ($vbulletin->options['cartplog_allowaud']) { $cartplog['currencies'][1] = 'AUD'; }
if ($vbulletin->options['cartplog_allowcad']) { $cartplog['currencies'][2] = 'CAD'; }
if ($vbulletin->options['cartplog_alloweur']) { $cartplog['currencies'][3] = 'EUR'; }
if ($vbulletin->options['cartplog_allowgbp']) { $cartplog['currencies'][4] = 'GBP'; }
if ($vbulletin->options['cartplog_allowusd']) { $cartplog['currencies'][5] = 'USD'; }

$cartplog['currency_count'] = count($cartplog['currencies']); // will have at least one element for default currency

// #################### INITIALIZE COOKIE SESSION #########################
if ($_REQUEST['do'] != 'postback')
{
	$vbulletin->input->clean_array_gpc('c', array(
		COOKIE_PREFIX . 'cartplog' => TYPE_NOHTML
	));

	$cartplog['cookie'] = substr($vbulletin->GPC[COOKIE_PREFIX . 'cartplog'], 0, -3);
	$cartplog['currency'] = strtoupper(substr($vbulletin->GPC[COOKIE_PREFIX . 'cartplog'], -3));

	$cartplog_reset = 0;
	if (!in_array($cartplog['currency'], $cartplog['currencies']))
	{
		$cartplog_reset = 1;
		$cartplog['currency'] = $cartplog['currency_code'];
	}
	if ($cartplog['currency_count'] > 1)
	{
		$cartplog['currency_code'] = $cartplog['currency'];
	}

	if ($cartplog['cookie'])
	{
		$cartplog_count_check = $db->query_first("SELECT COUNT(*) AS cnt
			FROM " . TABLE_PREFIX . "cartplog_sessions
			WHERE sessionid = '" . $db->escape_string($cartplog['cookie']) . "'
			AND completed = 0
		");

		if ($cartplog_count_check['cnt'] != 1)
		{
			$cartplog['cookie'] = '';
			$cartplog['currency'] = $cartplog['currency_code'];
			vbsetcookie('cartplog', '', false);
		}
		else if ($cartplog_reset) // make sure an active currency is set
		{
			vbsetcookie('cartplog', '', false);
			vbsetcookie('cartplog', $cartplog['cookie'] . $cartplog['currency']);

			$cartplog_basket_check = $db->query_first("SELECT products
				FROM " . TABLE_PREFIX . "cartplog_sessions
				WHERE sessionid = '" . $db->escape_string($cartplog['cookie']) . "'
			");

			if ($cartplog_basket_check['products'])
			{
				$cartplog_basket_check['products'] = unserialize($cartplog_basket_check['products']);
				if (!is_array($cartplog_basket_check['products']))
				{
					$cartplog_basket_check['products'] = array();
				}

				foreach ($cartplog_basket_check['products'] AS $productid => $productarray)
				{
					$cartplog_product_info = $db->query_first("SELECT price
						FROM " . TABLE_PREFIX . "cartplog_products
						WHERE productid = " . intval($productid) . "
					");

					$cartplog_product_info_price = unserialize($cartplog_product_info['price']);
					$cartplog_product_info['price'] = $cartplog_product_info_price[$vbulletin->userinfo['usergroupid']][$cartplog['currency']];
					$cartplog_basket_check['products'][$productid]['price'] = number_format(round(floatval($cartplog_product_info['price']), 2), 2, '.', '');
					$cartplog_basket_check['products'][$productid]['currency'] = $cartplog['currency'];
				}

				$cartplog_basket_check['products'] = serialize($cartplog_basket_check['products']);

				$db->query_write("UPDATE " . TABLE_PREFIX . "cartplog_sessions
					SET products = '" . $db->escape_string($cartplog_basket_check['products']) . "'
					WHERE sessionid = '" . $db->escape_string($cartplog['cookie']) . "'
				");
			}
		}
	}

	if (empty($cartplog['cookie']))
	{
		$cartplog_alphabet = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ`-=[]\;\',./~!@#$%^&*()_+{}|:"<>?';
		$cartplog_hash = mt_rand() . uniqid(microtime(), true) . $cartplog_alphabet[mt_rand(0,93)];
		$cartplog['cookie'] = md5($cartplog_hash);

		$cartplog_flag = true;
		while ($cartplog_flag)
		{
			$cartplog_count_check = $db->query_first("SELECT COUNT(*) AS cnt
				FROM " . TABLE_PREFIX . "cartplog_sessions
				WHERE sessionid = '" . $db->escape_string($cartplog['cookie']) . "'
			");

			if ($cartplog_count_check['cnt'])
			{
				$cartplog_hash = mt_rand() . uniqid(microtime(), true) . $cartplog_alphabet[mt_rand(0,93)];
				$cartplog['cookie'] = md5($cartplog_hash);
			}
			else
			{
				$cartplog_flag = false;
				$db->query_write("INSERT INTO " . TABLE_PREFIX . "cartplog_sessions
					(sessionid, completed, dateline, products)
					VALUES
					(
						'" . $db->escape_string($cartplog['cookie']) . "',
						0,
						" . intval(TIMENOW) . ",
						'a:0:{}'
					)
				");
				vbsetcookie('cartplog', $cartplog['cookie'] . $cartplog['currency_code']);
			}
		}
	}
}

// ######################## START MAIN SCRIPT #############################
if ($_GET['do'] == 'changecurrency')
{
	if ($cartplog['currency_count'] <= 1)
	{
		$cartplog_url = 'cartplog.php?' . $vbulletin->session->vars['sessionurl'] . 'do=viewindex';
		exec_header_redirect($cartplog_url);
		exit();
	}

	$vbulletin->input->clean_array_gpc('g', array(
		'cartplogcurrency' => TYPE_NOHTML
	));
	$cartplog['currency'] = strtoupper($vbulletin->GPC['cartplogcurrency']);

	if (!in_array($cartplog['currency'], $cartplog['currencies']))
	{
		$cartplog['currency'] = $cartplog['currency_code'];
	}

	vbsetcookie('cartplog', '', false);
	vbsetcookie('cartplog', $cartplog['cookie'] . $cartplog['currency']);

	$cartplog_basket_check = $db->query_first("SELECT products
		FROM " . TABLE_PREFIX . "cartplog_sessions
		WHERE sessionid = '" . $db->escape_string($cartplog['cookie']) . "'
	");

	if ($cartplog_basket_check['products'])
	{
		$cartplog_basket_check['products'] = unserialize($cartplog_basket_check['products']);
		if (!is_array($cartplog_basket_check['products']))
		{
			$cartplog_basket_check['products'] = array();
		}

		foreach ($cartplog_basket_check['products'] AS $productid => $productarray)
		{
			$cartplog_product_info = $db->query_first("SELECT price
				FROM " . TABLE_PREFIX . "cartplog_products
				WHERE productid = " . intval($productid) . "
			");

			$cartplog_product_info_price = unserialize($cartplog_product_info['price']);
			$cartplog_product_info['price'] = $cartplog_product_info_price[$vbulletin->userinfo['usergroupid']][$cartplog['currency']];
			$cartplog_basket_check['products'][$productid]['price'] = number_format(round(floatval($cartplog_product_info['price']), 2), 2, '.', '');
			$cartplog_basket_check['products'][$productid]['currency'] = $cartplog['currency'];
		}

		$cartplog_basket_check['products'] = serialize($cartplog_basket_check['products']);

		$db->query_write("UPDATE " . TABLE_PREFIX . "cartplog_sessions
			SET products = '" . $db->escape_string($cartplog_basket_check['products']) . "'
			WHERE sessionid = '" . $db->escape_string($cartplog['cookie']) . "'
		");
	}

	$cartplog_url = 'cartplog.php?' . $vbulletin->session->vars['sessionurl'] . 'do=viewindex';
	exec_header_redirect($cartplog_url);
	exit();
}

if ($_REQUEST['do'] == 'viewindex')
{
	// full table scan but no one should have millions of items
	$cartplog_product_infos = $db->query_read("SELECT productid, catid, thumb, title, price, sales
		FROM " . TABLE_PREFIX . "cartplog_products
		ORDER BY RAND()
	");

	$cartplog['product_list'] = '';
	$cartplog['item_count'] = 0;
	$cartplog_max_productid = 0;
	$cartplog_max_salescnt = 0;

	while ($cartplog_product_info = $db->fetch_array($cartplog_product_infos))
	{
		$cartplog_product_info_price = unserialize($cartplog_product_info['price']);
		if (!isset($cartplog_product_info_price[$vbulletin->userinfo['usergroupid']]['active']))
		{
			continue; // skip products not available to shopper's usergroup
		}

		$cartplog['item_count'] ++;
		$cartplog['product_productid'] = intval($cartplog_product_info['productid']);
		$cartplog['product_catid'] = intval($cartplog_product_info['catid']);
		$cartplog['product_thumb'] = strval($cartplog_product_info['thumb']);
		$cartplog['product_title'] = htmlspecialchars_uni($cartplog_product_info['title']);

		$cartplog_product_info['price'] = $cartplog_product_info_price[$vbulletin->userinfo['usergroupid']][$cartplog['currency_code']];
		$cartplog['product_price'] = number_format(round(floatval($cartplog_product_info['price']), 2), 2, '.', '');

		$cartplog['product_sales'] = intval($cartplog_product_info['sales']);

		$cartplog_max_productid = max($cartplog_max_productid, $cartplog['product_productid']);
		$cartplog_max_salescnt = max($cartplog_max_salescnt, $cartplog['product_sales']);

		//eval('$cartplog[\'product_list\'] .= "' . fetch_template('cartplog_product_item') . '";');
		eval(cartplog_fetch_template('cartplog_product_item', 'cartplog[\'product_list\']', true));

		if ($cartplog['item_count'] % 3 == 0)
		{
			$cartplog['product_list'] .= '</tr><tr>';
		}
	}

	$db->free_result($cartplog_product_infos);

	$cartplog['product_list'] = eregi_replace(preg_quote('</tr><tr>') . '$', '', $cartplog['product_list']);

	while ($cartplog['item_count'] % 3 != 0)
	{
		$cartplog['product_list'] .= '<td class="css_alt1" align="center" valign="bottom">&nbsp;</td>';
		$cartplog['item_count'] ++;
	}

	$cartplog['item_count'] = 0;

	$cartplog_product_info = $db->query_first("SELECT productid, catid, thumb, title, price
		FROM " . TABLE_PREFIX . "cartplog_products
		WHERE productid = " . intval($cartplog_max_productid) . "
	");

	$cartplog['newest_product'] = '';

	$cartplog_product_info_price = unserialize($cartplog_product_info['price']);
	if (isset($cartplog_product_info_price[$vbulletin->userinfo['usergroupid']]['active']))
	{
		// show products that are available to shopper's usergroup
		$cartplog['product_productid'] = intval($cartplog_product_info['productid']);
		$cartplog['product_catid'] = intval($cartplog_product_info['catid']);
		$cartplog['product_thumb'] = strval($cartplog_product_info['thumb']);
		$cartplog['product_title'] = htmlspecialchars_uni($cartplog_product_info['title']);

		$cartplog_product_info['price'] = $cartplog_product_info_price[$vbulletin->userinfo['usergroupid']][$cartplog['currency_code']];
		$cartplog['product_price'] = number_format(round(floatval($cartplog_product_info['price']), 2), 2, '.', '');

		//eval('$cartplog[\'newest_product\'] .= "' . fetch_template('cartplog_product_item') . '";');
		eval(cartplog_fetch_template('cartplog_product_item', 'cartplog[\'newest_product\']', true));
	}

	$db->free_result($cartplog_product_info);

	$cartplog_product_infos = $db->query_read("SELECT productid, catid, thumb, title, price
		FROM " . TABLE_PREFIX . "cartplog_products
		WHERE sales = " . intval($cartplog_max_salescnt) . "
		ORDER BY RAND()
		LIMIT 5
	");

	$cartplog['popular_products'] = '';

	while ($cartplog_product_info = $db->fetch_array($cartplog_product_infos))
	{
		$cartplog_product_info_price = unserialize($cartplog_product_info['price']);
		if (!isset($cartplog_product_info_price[$vbulletin->userinfo['usergroupid']]['active']))
		{
			continue; // skip products not available to shopper's usergroup
		}

		$cartplog['product_productid'] = intval($cartplog_product_info['productid']);
		$cartplog['product_catid'] = intval($cartplog_product_info['catid']);
		$cartplog['product_thumb'] = strval($cartplog_product_info['thumb']);
		$cartplog['product_title'] = htmlspecialchars_uni($cartplog_product_info['title']);

		$cartplog_product_info['price'] = $cartplog_product_info_price[$vbulletin->userinfo['usergroupid']][$cartplog['currency_code']];
		$cartplog['product_price'] = number_format(round(floatval($cartplog_product_info['price']), 2), 2, '.', '');

		//eval('$cartplog[\'popular_products\'] .= "' . fetch_template('cartplog_product_item') . '";');
		eval(cartplog_fetch_template('cartplog_product_item', 'cartplog[\'popular_products\']', true));
		$cartplog['popular_products'] .= '</tr><tr>';
	}

	$db->free_result($cartplog_product_infos);

	$cartplog['popular_products'] = eregi_replace(preg_quote('</tr><tr>') . '$', '', $cartplog['popular_products']);

	// full table scan but no one should have millions of items
	$cartplog_category_infos = $db->query_read("SELECT cartplog_products.productid, cartplog_categories.catid,
		cartplog_categories.title, cartplog_categories.products, cartplog_products.price
		FROM " . TABLE_PREFIX . "cartplog_categories AS cartplog_categories
		LEFT JOIN " . TABLE_PREFIX . "cartplog_products AS cartplog_products ON (cartplog_products.catid = cartplog_categories.catid)
		ORDER BY cartplog_categories.displayorder
	");

	$cartplog['category_list'] = '';
	$cartplog_current_catid = -999;
	$cartplog_category_count = 0;

	while ($cartplog_category_info = $db->fetch_array($cartplog_category_infos))
	{
		$cartplog_current_catid = intval($cartplog_category_info['catid']);
		if ($cartplog['category_catid'] != $cartplog_current_catid)
		{
			if ($cartplog_category_count > 0)
			{
				//eval('$cartplog[\'category_list\'] .= "' . fetch_template('cartplog_category_list') . '";');
				eval(cartplog_fetch_template('cartplog_category_list', 'cartplog[\'category_list\']', true));
			}
			$cartplog['category_catid'] = intval($cartplog_category_info['catid']);
			$cartplog['category_products'] = intval($cartplog_category_info['products']);
			$cartplog['category_title'] = htmlspecialchars_uni($cartplog_category_info['title']);
		}
		$cartplog_category_count = 1;

		$cartplog_category_info_price = unserialize($cartplog_category_info['price']);
		if ($cartplog_category_info['products'] > 0 && !isset($cartplog_category_info_price[$vbulletin->userinfo['usergroupid']]['active']))
		{
			// skip products not available to shopper's usergroup
			$cartplog['category_products'] --;
		}
	}
	if ($db->num_rows($cartplog_category_infos))
	{
		//eval('$cartplog[\'category_list\'] .= "' . fetch_template('cartplog_category_list') . '";');
		eval(cartplog_fetch_template('cartplog_category_list', 'cartplog[\'category_list\']', true));
	}

	$db->free_result($cartplog_category_infos);

	$cartplog_basket_check = $db->query_first("SELECT products
		FROM " . TABLE_PREFIX . "cartplog_sessions
		WHERE sessionid = '" . $db->escape_string($cartplog['cookie']) . "'
	");

	$cartplog['basket_count'] = 0;
	$cartplog['basket_total'] = 0;

	if ($cartplog_basket_check['products'])
	{
		$cartplog_basket_check['products'] = unserialize($cartplog_basket_check['products']);
		if (!is_array($cartplog_basket_check['products']))
		{
			$cartplog_basket_check['products'] = array();
		}
		foreach ($cartplog_basket_check['products'] AS $cartplog_basket_productid => $cartplog_basket_array)
		{
			$cartplog['basket_count'] += intval($cartplog_basket_array['quantity']);
			$cartplog['basket_total'] += round(intval($cartplog_basket_array['quantity']) * floatval($cartplog_basket_array['price']), 2);
		}
	}
	else
	{
		$cartplog_basket_check['products'] = array();
	}

	$db->free_result($cartplog_basket_check);

	$cartplog['basket_total'] = number_format($cartplog['basket_total'], 2, '.', '');

	//eval('$cartplog[\'basket_summary\'] = "' . fetch_template('cartplog_basket_summary') . '";');
	eval(cartplog_fetch_template('cartplog_basket_summary', 'cartplog[\'basket_summary\']', false));

	$cartplog_navbits = $vbphrase['cartplog_shopping_cart'];
	//eval('$cartplog[\'html\'] = "' . fetch_template('cartplog_view_index') . '";');
	eval(cartplog_fetch_template('cartplog_view_index', 'cartplog[\'html\']', false));
}

if ($_REQUEST['do'] == 'viewcategory')
{
	$vbulletin->input->clean_array_gpc('r', array(
		'cartplogcatid' => TYPE_UINT,
	));
	$cartplog['categoryid'] = $vbulletin->GPC['cartplogcatid'];

	$cartplog_product_infos = $db->query_read("SELECT productid, thumb, title, price
		FROM " . TABLE_PREFIX . "cartplog_products
		WHERE catid = " . intval($cartplog['categoryid']) . "
	");

	$cartplog['category_items'] = '';

	if ($cartplog_product_infos)
	{
		while ($cartplog_product_info = $db->fetch_array($cartplog_product_infos))
		{
			$cartplog_product_info_price = unserialize($cartplog_product_info['price']);
			if (!isset($cartplog_product_info_price[$vbulletin->userinfo['usergroupid']]['active']))
			{
				continue; // skip products not available to shopper's usergroup
			}

			$cartplog['productid'] = intval($cartplog_product_info['productid']);
			$cartplog['thumb'] = strval($cartplog_product_info['thumb']);
			$cartplog['title'] = htmlspecialchars_uni($cartplog_product_info['title']);

			$cartplog_product_info['price'] = $cartplog_product_info_price[$vbulletin->userinfo['usergroupid']][$cartplog['currency_code']];
			$cartplog['price'] = number_format(round(floatval($cartplog_product_info['price']), 2), 2, '.', '');

			//eval('$cartplog[\'category_items\'] .= "' . fetch_template('cartplog_category_item') . '";');
			eval(cartplog_fetch_template('cartplog_category_item', 'cartplog[\'category_items\']', true));
		}

		$db->free_result($cartplog_product_infos);

		$cartplog_navbits = $vbphrase['cartplog_view_category'];
		//eval('$cartplog[\'html\'] = "' . fetch_template('cartplog_view_category') . '";');
		eval(cartplog_fetch_template('cartplog_view_category', 'cartplog[\'html\']', false));
	}
	else
	{
		$cartplog_url = 'cartplog.php?' . $vbulletin->session->vars['sessionurl'] . 'do=viewindex';
		exec_header_redirect($cartplog_url);
		exit();
	}
}

if ($_REQUEST['do'] == 'viewcart' || $_REQUEST['do'] == 'checkout')
{
	$cartplog['checkout'] = 0;
	if ($_REQUEST['do'] == 'checkout')
	{
		if (!$vbulletin->userinfo['userid'])
		{
			print_no_permission(); // do not remove this - must have a userid to check out!!!!!
		}

		$db->query_write("UPDATE " . TABLE_PREFIX . "cartplog_sessions
			SET userid = " . intval($vbulletin->userinfo['userid']) . "
			WHERE sessionid = '" . $db->escape_string($cartplog['cookie']) . "'
		");

		$cartplog['checkout'] = 1;

		$cartplog_paypal_api = $db->query_first("SELECT settings
			FROM " . TABLE_PREFIX . "paymentapi
			WHERE classname = 'paypal'
		");

		$cartplog['ppemail'] = '';
		$cartplog['primaryemail'] = '';

		$cartplog_paypal_api['settings'] = unserialize($cartplog_paypal_api['settings']);
		if (is_array($cartplog_paypal_api['settings']))
		{
			foreach ($cartplog_paypal_api['settings'] AS $cartplog_paypal_api_title => $cartplog_paypal_api_array)
			{
				if ($cartplog_paypal_api_title == 'ppemail')
				{
					$cartplog['ppemail'] = strtolower($cartplog_paypal_api_array['value']);
				}
				if ($cartplog_paypal_api_title == 'primaryemail')
				{
					$cartplog['primaryemail'] = strtolower($cartplog_paypal_api_array['value']);
				}
			}
		}
	}

	$cartplog_basket_check = $db->query_first("SELECT products
		FROM " . TABLE_PREFIX . "cartplog_sessions
		WHERE sessionid = '" . $db->escape_string($cartplog['cookie']) . "'
	");

	$cartplog['basket_items'] = '';
	$cartplog['basket_total'] = 0;
	$cartplog['basket_hidden'] = '';
	$cartplog['xnum'] = 0;

	if ($cartplog_basket_check['products'])
	{
		$cartplog_basket_check['products'] = unserialize($cartplog_basket_check['products']);
		if (!is_array($cartplog_basket_check['products']))
		{
			$cartplog_basket_check['products'] = array();
		}
		foreach ($cartplog_basket_check['products'] AS $cartplog_basket_productid => $cartplog_basket_array)
		{
			$cartplog['basket_productid'] = intval($cartplog_basket_productid);
			$cartplog['basket_quantity'] = intval($cartplog_basket_array['quantity']);
			$cartplog['basket_thumb'] = strval($cartplog_basket_array['thumb']);
			$cartplog['basket_title'] = htmlspecialchars_uni($cartplog_basket_array['title']);
			$cartplog['basket_price'] = number_format(round(floatval($cartplog_basket_array['price']), 2), 2, '.', '');
			$cartplog['basket_options'] = nl2br(htmlspecialchars_uni($cartplog_basket_array['options']));
			$cartplog['basket_lineprice'] = number_format(round($cartplog['basket_quantity'] * $cartplog['basket_price'], 2), 2, '.', '');
			$cartplog['basket_total'] += $cartplog['basket_lineprice'];

			//eval('$cartplog[\'basket_items\'] .= "' . fetch_template('cartplog_basket_item') . '";');
			eval(cartplog_fetch_template('cartplog_basket_item', 'cartplog[\'basket_items\']', true));

			if ($cartplog['checkout'])
			{
				$cartplog['xnum'] ++;
				$cartplog['basket_options'] = str_replace('<br />', ', ', $cartplog['basket_options']);
				if (!vbstrlen($cartplog['basket_options']))
				{
					$cartplog['basket_options'] = $vbphrase['none'];
				}

				$cartplog['basket_hidden'] .= '
					<input type="hidden" name="item_name_' . $cartplog['xnum'] . '" value="' . $cartplog['basket_title'] . '" />
					<input type="hidden" name="item_number_' . $cartplog['xnum'] . '" value="' . $cartplog['cookie'] . '" />
					<input type="hidden" name="amount_' . $cartplog['xnum'] . '" value="' . $cartplog['basket_price'] . '" />
					<input type="hidden" name="quantity_' . $cartplog['xnum'] . '" value="' . $cartplog['basket_quantity'] . '" />
					<input type="hidden" name="shipping_' . $cartplog['xnum'] . '" value="0.00" />
					<input type="hidden" name="on0_' . $cartplog['xnum'] . '" value="' . $vbphrase['options'] . '" />
					<input type="hidden" name="os0_' . $cartplog['xnum'] . '" value="' . $cartplog['basket_options'] . '" />
				';
			}
		}
	}
	else
	{
		$cartplog_basket_check['products'] = array();
	}

	$cartplog['basket_total'] = number_format($cartplog['basket_total'], 2, '.', '');

	$cartplog_navbits = $vbphrase['cartplog_view_cart'];
	if ($cartplog['checkout'])
	{
		$cartplog_navbits = $vbphrase['cartplog_checkout'];
	}
	//eval('$cartplog[\'html\'] = "' . fetch_template('cartplog_view_cart') . '";');
	eval(cartplog_fetch_template('cartplog_view_cart', 'cartplog[\'html\']', false));
}

if ($_REQUEST['do'] == 'viewhistory')
{
	if (!$vbulletin->userinfo['userid'])
	{
		print_no_permission(); // do not remove this - must have a userid to view history!!!!!
	}

	$cartplog_transaction_infos = $db->query_read("SELECT *
		FROM " . TABLE_PREFIX . "cartplog_transactions
		WHERE userid = " . intval($vbulletin->userinfo['userid']) . "
		ORDER BY dateline DESC
	");

	$cartplog['history_items'] = '';
	$cartplog['history_list'] = '';

	while ($cartplog_transaction_info = $db->fetch_array($cartplog_transaction_infos))
	{
		$cartplog['history_items'] = '';
		$cartplog['sessionid'] = htmlspecialchars_uni($cartplog_transaction_info['sessionid']);
		$cartplog['total'] = number_format(round(floatval($cartplog_transaction_info['total']), 2), 2, '.', '');
		$cartplog['status'] = htmlspecialchars_uni($cartplog_transaction_info['status']);
		$cartplog['date'] = vbdate($vbulletin->options['dateformat'], intval($cartplog_transaction_info['dateline']));

		$cartplog_transaction_info['products'] = unserialize($cartplog_transaction_info['products']);
		if (!is_array($cartplog_transaction_info['products']))
		{
			$cartplog_transaction_info['products'] = array();
		}
		foreach ($cartplog_transaction_info['products'] AS $cartplog_transaction_productid => $cartplog_transaction_array)
		{
			$cartplog['productid'] = intval($cartplog_transaction_productid);
			$cartplog['quantity'] = intval($cartplog_transaction_array['quantity']);
			$cartplog['thumb'] = strval($cartplog_transaction_array['thumb']);
			$cartplog['title'] = htmlspecialchars_uni($cartplog_transaction_array['title']);
			$cartplog['price'] = number_format(round(floatval($cartplog_transaction_array['price']), 2), 2, '.', '');
			$cartplog['currency'] = strval($cartplog_transaction_array['currency']);
			$cartplog['options'] = nl2br(htmlspecialchars_uni($cartplog_transaction_array['options']));
			$cartplog['lineprice'] = number_format(round($cartplog['quantity'] * $cartplog['price'], 2), 2, '.', '');

			if ($cartplog['thumb'])
			{
				$cartplog['thumb'] = '<img style="' . $vbulletin->options['cartplog_thumbcss'] . '" src="' . $cartplog['thumb'] . '" alt="" border="0" />';
			}
			else
			{
				$cartplog['thumb'] = '<img style="' . $vbulletin->options['cartplog_thumbcss'] . '" src="../' . $vbulletin->options['cleargifurl'] . '" alt="" border="0" />';
			}

			//eval('$cartplog[\'history_items\'] .= "' . fetch_template('cartplog_history_item') . '";');
			eval(cartplog_fetch_template('cartplog_history_item', 'cartplog[\'history_items\']', true));
		}

		//eval('$cartplog[\'history_list\'] .= "' . fetch_template('cartplog_history_list') . '";');
		eval(cartplog_fetch_template('cartplog_history_list', 'cartplog[\'history_list\']', true));
	}

	$db->free_result($cartplog_transaction_infos);

	if (!$cartplog['history_list'])
	{
		$cartplog['history_list'] = '<tr><td class="css_alt1">' . $vbphrase['none'] . '</td></tr>';
	}

	$cartplog_navbits = $vbphrase['cartplog_view_history'];
	//eval('$cartplog[\'html\'] = "' . fetch_template('cartplog_view_history') . '";');
	eval(cartplog_fetch_template('cartplog_view_history', 'cartplog[\'html\']', false));
}

if ($_REQUEST['do'] == 'viewitem')
{
	$vbulletin->input->clean_array_gpc('r', array(
		'cartplogproductid' => TYPE_UINT,
	));
	$cartplog['productid'] = $vbulletin->GPC['cartplogproductid'];

	$cartplog_product_check = $db->query_first("SELECT *
		FROM " . TABLE_PREFIX . "cartplog_products
		WHERE productid = " . intval($cartplog['productid']) . "
	");

	if ($cartplog_product_check)
	{
		$cartplog_product_check_price = unserialize($cartplog_product_check['price']);
		if (!isset($cartplog_product_check_price[$vbulletin->userinfo['usergroupid']]['active']))
		{
			// skip products not available to shopper's usergroup
			$cartplog_url = 'cartplog.php?' . $vbulletin->session->vars['sessionurl'] . 'do=viewindex';
			exec_header_redirect($cartplog_url);
			exit();
		}

		$cartplog['product_productid'] = intval($cartplog_product_check['productid']);
		$cartplog['product_thumb'] = strval($cartplog_product_check['thumb']);
		$cartplog['product_title'] = htmlspecialchars_uni($cartplog_product_check['title']);
		$cartplog['product_description'] = strval($cartplog_product_check['description']);

		$cartplog_product_check['price'] = $cartplog_product_check_price[$vbulletin->userinfo['usergroupid']][$cartplog['currency_code']];
		$cartplog['product_price'] = number_format(round(floatval($cartplog_product_check['price']), 2), 2, '.', '');

		$cartplog['product_options'] = '';
		$cartplog_product_options = array_map('trim', unserialize($cartplog_product_check['options']));
		if (!is_array($cartplog_product_options))
		{
			$cartplog_product_options = array();
		}
		foreach ($cartplog_product_options AS $cartplog_options_key => $cartplog_options_title)
		{
			if (vbstrlen($cartplog_options_title))
			{
				$cartplog['options_title'] = htmlspecialchars_uni($cartplog_options_title);
				$cartplog['product_options'] .= '<option value="' . $cartplog['options_title'] . '">' . $cartplog['options_title'] . '</option>';
				$cartplog['product_options'] .= "\n";
			}
		}
		if ($cartplog['product_options'])
		{
			$cartplog['product_options'] = '<select name="cartplogoptions">' . $cartplog['product_options'] . '</select>';
		}

		$cartplog_navbits = $vbphrase['cartplog_view_item'];
		//eval('$cartplog[\'html\'] = "' . fetch_template('cartplog_view_item') . '";');
		eval(cartplog_fetch_template('cartplog_view_item', 'cartplog[\'html\']', false));
	}
	else
	{
		$cartplog_url = 'cartplog.php?' . $vbulletin->session->vars['sessionurl'] . 'do=viewindex';
		exec_header_redirect($cartplog_url);
		exit();
	}
}

if ($_REQUEST['do'] == 'additem')
{
	$vbulletin->input->clean_array_gpc('r', array(
		'cartplogproductid' => TYPE_UINT,
		'cartplogoptions' => TYPE_STR,
		'cartplogsubmit' => TYPE_STR
	));
	$cartplog['productid'] = $vbulletin->GPC['cartplogproductid'];
	$cartplog['options'] = $vbulletin->GPC['cartplogoptions'];
	$cartplog['submit'] = $vbulletin->GPC['cartplogsubmit'];

	if ($cartplog['submit'] == $vbphrase['cartplog_continue_shopping'])
	{
		$cartplog_url = 'cartplog.php?' . $vbulletin->session->vars['sessionurl'] . 'do=viewindex';
		exec_header_redirect($cartplog_url);
		exit();
	}

	$cartplog_product_check = $db->query_first("SELECT *
		FROM " . TABLE_PREFIX . "cartplog_products
		WHERE productid = " . intval($cartplog['productid']) . "
	");

	if ($cartplog_product_check)
	{
		$cartplog_product_check_price = unserialize($cartplog_product_check['price']);
		if (!isset($cartplog_product_check_price[$vbulletin->userinfo['usergroupid']]['active']))
		{
			// skip products not available to shopper's usergroup
			$cartplog_url = 'cartplog.php?' . $vbulletin->session->vars['sessionurl'] . 'do=viewcart';
			exec_header_redirect($cartplog_url);
			exit();
		}

		if ($cartplog_product_check['options'])
		{
			$cartplog_product_check['options'] = array_map('trim', unserialize($cartplog_product_check['options']));
			if (is_array($cartplog_product_check['options']) && vbstrlen($cartplog['options']))
			{
				$cartplog_search_key = array_search($cartplog['options'], $cartplog_product_check['options']);
				if ($cartplog_search_key === false)
				{
					$cartplog['options'] = '';
				}
			}
			else
			{
				$cartplog['options'] = '';
			}
		}
		else
		{
			$cartplog['options'] = '';
		}

		$cartplog_basket_check = $db->query_first("SELECT products
			FROM " . TABLE_PREFIX . "cartplog_sessions
			WHERE sessionid = '" . $db->escape_string($cartplog['cookie']) . "'
		");

		if ($cartplog_basket_check['products'])
		{
			$cartplog_basket_check['products'] = unserialize($cartplog_basket_check['products']);
			if (!is_array($cartplog_basket_check['products']))
			{
				$cartplog_basket_check['products'] = array();
			}

			if ($cartplog_basket_check['products'][$cartplog['productid']])
			{
				$cartplog_basket_check['products'][$cartplog['productid']]['quantity'] ++;

				if (vbstrlen($cartplog_basket_check['products'][$cartplog['productid']]['options']) && vbstrlen($cartplog['options']))
				{
					$cartplog_basket_check['products'][$cartplog['productid']]['options'] .= ', ' . $cartplog['options'];
				}
				else if (vbstrlen($cartplog['options']))
				{
					$cartplog_basket_check['products'][$cartplog['productid']]['options'] = $cartplog['options'];
				}
			}
			else
			{
				$cartplog_product_check['price'] = $cartplog_product_check_price[$vbulletin->userinfo['usergroupid']][$cartplog['currency_code']];
				$cartplog_product_check['price'] = number_format(round(floatval($cartplog_product_check['price']), 2), 2, '.', '');

				$cartplog_basket_check['products'][$cartplog['productid']] = array(
					'quantity' => 1,
					'thumb' => $cartplog_product_check['thumb'],
					'title' => $cartplog_product_check['title'],
					'price' => $cartplog_product_check['price'],
					'currency' => $cartplog['currency_code'],
					'options' => $cartplog['options']
				);
			}

			$cartplog_basket_check['products'] = serialize($cartplog_basket_check['products']);

			$db->query_write("UPDATE " . TABLE_PREFIX . "cartplog_sessions
				SET products = '" . $db->escape_string($cartplog_basket_check['products']) . "'
				WHERE sessionid = '" . $db->escape_string($cartplog['cookie']) . "'
			");
		}
	}

	$cartplog_url = 'cartplog.php?' . $vbulletin->session->vars['sessionurl'] . 'do=viewcart';
	exec_header_redirect($cartplog_url);
	exit();
}

if ($_REQUEST['do'] == 'removeitem')
{
	$vbulletin->input->clean_array_gpc('r', array(
		'cartplogproductid' => TYPE_UINT
	));
	$cartplog['productid'] = $vbulletin->GPC['cartplogproductid'];

	$cartplog_basket_check = $db->query_first("SELECT products
		FROM " . TABLE_PREFIX . "cartplog_sessions
		WHERE sessionid = '" . $db->escape_string($cartplog['cookie']) . "'
	");

	if ($cartplog_basket_check['products'])
	{
		$cartplog_basket_check['products'] = unserialize($cartplog_basket_check['products']);
		if (!is_array($cartplog_basket_check['products']))
		{
			$cartplog_basket_check['products'] = array();
		}

		unset($cartplog_basket_check['products'][$cartplog['productid']]);

		$cartplog_basket_check['products'] = serialize($cartplog_basket_check['products']);

		$db->query_write("UPDATE " . TABLE_PREFIX . "cartplog_sessions
			SET products = '" . $db->escape_string($cartplog_basket_check['products']) . "'
			WHERE sessionid = '" . $db->escape_string($cartplog['cookie']) . "'
		");
	}

	$cartplog_url = 'cartplog.php?' . $vbulletin->session->vars['sessionurl'] . 'do=viewcart';
	exec_header_redirect($cartplog_url);
	exit();
}

if ($_REQUEST['do'] == 'updatequantity')
{
	$vbulletin->input->clean_array_gpc('r', array(
		'cartplogquantity' => TYPE_ARRAY,
		'cartplogsubmit' => TYPE_STR
	));
	$cartplog['quantity'] = $vbulletin->GPC['cartplogquantity'];
	$cartplog['submit'] = $vbulletin->GPC['cartplogsubmit'];

	if ($cartplog['submit'] == $vbphrase['cartplog_continue_shopping'])
	{
		$cartplog_url = 'cartplog.php?' . $vbulletin->session->vars['sessionurl'] . 'do=viewindex';
		exec_header_redirect($cartplog_url);
		exit();
	}
	if ($cartplog['submit'] == $vbphrase['cartplog_checkout'])
	{
		$cartplog_url = 'cartplog.php?' . $vbulletin->session->vars['sessionurl'] . 'do=checkout';
		exec_header_redirect($cartplog_url);
		exit();
	}

	$cartplog_basket_check = $db->query_first("SELECT products
		FROM " . TABLE_PREFIX . "cartplog_sessions
		WHERE sessionid = '" . $db->escape_string($cartplog['cookie']) . "'
	");

	if ($cartplog_basket_check['products'])
	{
		$cartplog_basket_check['products'] = unserialize($cartplog_basket_check['products']);
		if (!is_array($cartplog_basket_check['products']))
		{
			$cartplog_basket_check['products'] = array();
		}

		if (!empty($cartplog_basket_check['products']))
		{
			foreach ($cartplog['quantity'] AS $cartplog_basket_productid => $cartplog_basket_quantity)
			{
				$cartplog_basket_productid = max(0, intval($cartplog_basket_productid));
				$cartplog_basket_quantity = max(0, intval($cartplog_basket_quantity));

				if (!$cartplog_basket_quantity)
				{
					unset($cartplog_basket_check['products'][$cartplog_basket_productid]);
				}
				else
				{
					$cartplog_basket_check['products'][$cartplog_basket_productid]['quantity'] = $cartplog_basket_quantity;
				}
			}

			if (empty($cartplog_basket_check['products']))
			{
				$cartplog_basket_check['products'] = array();
			}

			$cartplog_basket_check['products'] = serialize($cartplog_basket_check['products']);

			$db->query_write("UPDATE " . TABLE_PREFIX . "cartplog_sessions
				SET products = '" . $db->escape_string($cartplog_basket_check['products']) . "'
				WHERE sessionid = '" . $db->escape_string($cartplog['cookie']) . "'
			");
		}
	}

	$cartplog_url = 'cartplog.php?' . $vbulletin->session->vars['sessionurl'] . 'do=viewcart';
	exec_header_redirect($cartplog_url);
	exit();
}

if ($_REQUEST['do'] == 'postback')
{
	$cartplog_paypal_postback = 'cmd=_notify-validate';
	foreach ($_POST AS $cartplog_paypal_key => $cartplog_paypal_value)
	{
		if (!empty($cartplog_paypal_value))
		{
			$cartplog_paypal_postback .= '&' . $cartplog_paypal_key . '=' . urlencode($cartplog_paypal_value);
		}
	}

	$cartplog_paypal_result = false;
	$cartplog_used_curl = false;
	if (function_exists('curl_init') && $cartplog_paypal_curl = curl_init())
	{
		curl_setopt($cartplog_paypal_curl, CURLOPT_URL, 'http://www.paypal.com/cgi-bin/webscr');
		curl_setopt($cartplog_paypal_curl, CURLOPT_POST, true); // for x-www-form-urlencoded
		curl_setopt($cartplog_paypal_curl, CURLOPT_POSTFIELDSIZE, strlen($cartplog_paypal_postback));
		curl_setopt($cartplog_paypal_curl, CURLOPT_POSTFIELDS, $cartplog_paypal_postback);
		curl_setopt($cartplog_paypal_curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($cartplog_paypal_curl, CURLOPT_TIMEOUT, 30);
		$cartplog_paypal_result = curl_exec($cartplog_paypal_curl); // retuns false on failure
		curl_close($cartplog_paypal_curl);
		if ($cartplog_paypal_result !== false)
		{
			$cartplog_used_curl = true;
		}
	}

	if (!$cartplog_used_curl)
	{
		$cartplog_paypal_header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
		$cartplog_paypal_header .= "Host: www.paypal.com\r\n";
		$cartplog_paypal_header .= "Content-Type: application/x-www-form-urlencoded\r\n";
		$cartplog_paypal_header .= "Content-Length: " . strlen($cartplog_paypal_postback) . "\r\n\r\n";
		$cartplog_paypal_header .= $cartplog_paypal_postback;
		$cartplog_paypal_errno = $cartplog_paypal_errstr = '';
		if ($cartplog_paypal_fsockopen = fsockopen('www.paypal.com', 80, $cartplog_paypal_errno, $cartplog_paypal_errstr, 30))
		{
			stream_set_timeout($cartplog_paypal_fsockopen, 30);
			fwrite($cartplog_paypal_fsockopen, $cartplog_paypal_header);
			while (!feof($cartplog_paypal_fsockopen))
			{
				$cartplog_paypal_result = fgets($cartplog_paypal_fsockopen, 1024);
			}
			fclose($cartplog_paypal_fsockopen);
		}
	}

	if ($cartplog_paypal_result == 'VERIFIED')
	{
		$vbulletin->input->clean_array_gpc('p', array(
			'item_number1' => TYPE_NOHTML,
			'business' => TYPE_NOHTML,
			'receiver_email' => TYPE_NOHTML,
			'mc_gross' => TYPE_NUM,
			'payment_status' => TYPE_NOHTML,
			'txn_id' => TYPE_NOHTML,
			'txn_type' => TYPE_NOHTML
		));

		$cartplog['item_number'] = $vbulletin->GPC['item_number1'];
		$cartplog['business'] = $vbulletin->GPC['business'];
		$cartplog['receiver_email'] = $vbulletin->GPC['receiver_email'];
		$cartplog['mc_gross'] = $vbulletin->GPC['mc_gross'];
		$cartplog['payment_status'] = $vbulletin->GPC['payment_status'];
		$cartplog['txn_id'] = $vbulletin->GPC['txn_id'];
		$cartplog['txn_type'] = $vbulletin->GPC['txn_type'];

		$cartplog_paypal_api = $db->query_first("SELECT settings
			FROM " . TABLE_PREFIX . "paymentapi
			WHERE classname = 'paypal'
		");

		$cartplog['ppemail'] = '';
		$cartplog['primaryemail'] = '';

		$cartplog_paypal_api['settings'] = unserialize($cartplog_paypal_api['settings']);
		if (is_array($cartplog_paypal_api['settings']))
		{
			foreach ($cartplog_paypal_api['settings'] AS $cartplog_paypal_api_title => $cartplog_paypal_api_array)
			{
				if ($cartplog_paypal_api_title == 'ppemail')
				{
					$cartplog['ppemail'] = strtolower($cartplog_paypal_api_array['value']);
				}
				if ($cartplog_paypal_api_title == 'primaryemail')
				{
					$cartplog['primaryemail'] = strtolower($cartplog_paypal_api_array['value']);
				}
			}
		}

		if ($vbulletin->GPC['business'] == $cartplog['ppemail'] || $vbulletin->GPC['receiver_email'] == $cartplog['primaryemail'])
		{
			$cartplog_basket_check = $db->query_first("SELECT cartplog_sessions.*, user.username
				FROM " . TABLE_PREFIX . "cartplog_sessions AS cartplog_sessions, " . TABLE_PREFIX . "user AS user
				WHERE cartplog_sessions.userid = user.userid
				AND cartplog_sessions.sessionid = '" . $db->escape_string($cartplog['item_number']) . "'
			");

			$cartplog['sessionid'] = strval($cartplog_basket_check['sessionid']);
			$cartplog['userid'] = intval($cartplog_basket_check['userid']);
			$cartplog['products'] = strval($cartplog_basket_check['products']);
			$cartplog['username'] = strval($cartplog_basket_check['username']);

			$cartplog['basket_total'] = 0;
			$cartplog['popular'] = array();
			$cartplog['products2'] = '';

			if ($cartplog_basket_check['products'])
			{
				$cartplog_basket_check['products'] = unserialize($cartplog['products']);
				if (!is_array($cartplog_basket_check['products']))
				{
					$cartplog_basket_check['products'] = array();
				}
				foreach ($cartplog_basket_check['products'] AS $cartplog_basket_productid => $cartplog_basket_array)
				{
					$cartplog['basket_total'] += round(intval($cartplog_basket_array['quantity']) * floatval($cartplog_basket_array['price']), 2);
					$cartplog['popular'][] = intval($cartplog_basket_productid);
					if (!vbstrlen($cartplog_basket_array['options']))
					{
						$cartplog_basket_array['options'] = $vbphrase['none'];
					}
					$cartplog['products2'] .= "\n\t" . $vbphrase['cartplog_quantity'] . ": " . $cartplog_basket_array['quantity']
						. "\n\t" . $vbphrase['title'] . ": " . $cartplog_basket_array['title']
						. "\n\t" . $vbphrase['options'] . ": " . $cartplog_basket_array['options'] . "\n";
				}
			}

			if (!$cartplog['products2'])
			{
				$cartplog['products2'] = $vbphrase['cartplog_unknown'];
			}
			$cartplog['basket_total'] = number_format($cartplog['basket_total'], 2, '.', '');
			$cartplog['mc_gross'] = number_format(floatval($cartplog['mc_gross']), 2, '.', '');

			$cartplog_transaction_info = $db->query_first("SELECT txnid
				FROM " . TABLE_PREFIX . "cartplog_transactions
				WHERE txnid = '" . $db->escape_string($cartplog['txn_id']) . "'
				AND class = 'paypal'
			");

			if (!$cartplog_transaction_info)
			{
				$db->query_write("INSERT INTO " . TABLE_PREFIX . "cartplog_transactions
					(sessionid, txnid, userid, total, status, class, dateline, products)
					VALUES
					(
						'" . $db->escape_string($cartplog['sessionid']) . "',
						'" . $db->escape_string($cartplog['txn_id']) . "',
						" . intval($cartplog['userid']) . ",
						" . number_format(floatval($cartplog['mc_gross']), 2, '.', '') . ",
						'" . $db->escape_string($cartplog['payment_status']) . "',
						'paypal',
						" . intval(TIMENOW) . ",
						'" . $db->escape_string($cartplog['products']) . "'
					)
				");

				$db->query_write("UPDATE " . TABLE_PREFIX . "cartplog_sessions
					SET completed = 1
					WHERE userid = " . intval($cartplog['userid']) . "
					AND sessionid = '" . $db->escape_string($cartplog['sessionid']) . "'
				");
			}

			$cartplog['products'] = $cartplog['products2'];
			$cartplog_phrase = 'cartplog_payment_unknown';
			if ($cartplog['txn_type'] == 'cart' && $cartplog['payment_status'] == 'Completed' && $cartplog['basket_total'] == $cartplog['mc_gross'])
			{
				$cartplog_phrase = 'cartplog_payment_received';
				if (!empty($cartplog['popular']))
				{
					$db->query_write("UPDATE " . TABLE_PREFIX . "cartplog_products
						SET sales = sales + 1
						WHERE productid IN (" . implode(',', $cartplog['popular']) . ")
					");
				}
			}
			else if ($cartplog['payment_status'] == 'Refunded' || $cartplog['payment_status'] == 'Reversed')
			{
				$cartplog_phrase = 'cartplog_payment_reversed';
			}

			if ($vbulletin->options['paymentemail'])
			{
				if (!$vbphrase)
				{
					$vbphrase = init_language();
				}

				$cartplog['username'] = unhtmlspecialchars($cartplog['username']);
				$cartplog_subject = $cartplog_message = '';
				eval(fetch_email_phrases($cartplog_phrase, 0, '', 'cartplog_'));

				$cartplog_emails = explode(' ', $vbulletin->options['paymentemail']);
				foreach ($cartplog_emails AS $cartplog_email)
				{
					$cartplog_email = trim($cartplog_email);
					if ($cartplog_email)
					{
						vbmail($cartplog_email, $cartplog_subject, $cartplog_message, true);
					}
				}
			}

			if (SAPI_NAME == 'cgi' OR SAPI_NAME == 'cgi-fcgi')
			{
				header('Status: 200 OK');
			}
			else
			{
				header('HTTP/1.1 200 OK');
			}
		}
	}
}

if ($_REQUEST['do'] != 'postback')
{
	$navbits = array();
	$navbits['cartplog.php' . $vbulletin->session->vars['sessionurl_q']] = htmlspecialchars_uni($vbphrase['cartplog']);
	$navbits[''] = htmlspecialchars_uni($cartplog_navbits);
	$navbits = construct_navbits($navbits);

	//eval('$navbar = "' . fetch_template('navbar') . '";');
	//eval(cartplog_fetch_template('navbar', 'navbar', false));
	$navbar = render_navbar_template($navbits);

	// do not hide, change, remove, etcetera !!!
	$footer = $cartplog['powered_by'] . $footer;

	//eval('print_output("' . fetch_template('cartplog_shell_blank') . '");');
	eval(cartplog_fetch_template('cartplog_shell_blank', 'cartplog[\'output\']', false));
	print_output($cartplog['output']);
}

?>