<?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 (!$vbulletin->userinfo['userid'] OR !$vbulletin->options['dbtech_vbshop_donate_enabled'])
{
	// No permission to buy
	print_no_permission();
}

// #############################################################################
if ($_REQUEST['action'] == 'donate' OR empty($_REQUEST['action']))
{
	foreach (VBSHOP::$cache['currency'] as $currencyid => $currency)
	{
		if (!$currency['active'])
		{
			// Can't bank this
			continue;
		}
		
		// Create the archive template
		$templater = vB_Template::create('option');
			$templater->register('optionvalue', 	$currencyid);
			$templater->register('optiontitle', 	$currency['title']);
		$currencies .= $templater->render();	
	}
	
	if (!$currencies)
	{
		// No stealable currencies
		print_no_permission();
	}
	
	
	// Add to the navbits
	$navbits[''] = $pagetitle = $vbphrase['dbtech_vbshop_donation'];
	
	// Create the archive template
	$page_templater = vB_Template::create('dbtech_vbshop_donate');
		$page_templater->register('pagetitle', 	$pagetitle);
		$page_templater->register('currencies', $currencies);
	$HTML = $page_templater->render();
}

// #############################################################################
if ($_POST['action'] == 'dodonate')
{
	$vbulletin->input->clean_array_gpc('p', array(
		'amount' 	=> TYPE_UNUM,
		'currencyid'=> TYPE_UINT,
		'username' 	=> TYPE_STR,
		'message' 	=> TYPE_NOHTML,
	));
	
	if (!$currency = VBSHOP::$cache['currency'][$vbulletin->GPC['currencyid']])
	{
		// Woups, immune
		eval(standard_error(fetch_error('dbtech_vbshop_error_x', construct_phrase($vbphrase['dbtech_vbshop_invalid_x'], $vbphrase['dbtech_vbshop_currency'], $vbulletin->GPC['currencyid']))));
	}
		
	if (!$currency['active'])
	{
		// Woups, immune
		eval(standard_error(fetch_error('dbtech_vbshop_error_x', construct_phrase($vbphrase['dbtech_vbshop_invalid_x'], $vbphrase['dbtech_vbshop_currency'], $vbulletin->GPC['currencyid']))));
	}	
	
	if (!$vbulletin->GPC['amount'])
	{
		eval(standard_error(fetch_error('dbtech_vbshop_error_x', $vbphrase['dbtech_vbshop_invalid_action'])));	
	}
	
	// Donating to
	if (!$existing = $db->query_first_slave("
		SELECT *
		FROM " . TABLE_PREFIX . "user
		WHERE username = " . $db->sql_prepare(htmlspecialchars_uni($vbulletin->GPC['username']))
	))
	{
		eval(standard_error(fetch_error('invalid_user_specified')));
	}	
	
	$userpoints = $db->query_first_slave("
		SELECT " . $currency['pointscolumn'] . " AS points
		FROM " . TABLE_PREFIX . $currency['pointstable'] . "
		WHERE userid = " . $vbulletin->userinfo['userid']
	);
	
	if ($userpoints['points'] < $vbulletin->GPC['amount'])
	{
		// Not enough points, dur
		eval(standard_error(fetch_error('dbtech_vbshop_error_x', $vbphrase['dbtech_vbshop_not_enough_points'])));
	}
	
	// Send PM
	$result = VBSHOP::sendPM(
		$existing['userid'],
		construct_phrase($vbphrase['dbtech_vbshop_donation_from_x'], 
			$vbulletin->userinfo['username']
		),
		construct_phrase($vbphrase['dbtech_vbshop_donation_from_x_body'],
			$vbulletin->userinfo['username'],
			$vbulletin->GPC['amount'],
			$currency['title'],
			$vbulletin->GPC['message']
		)
	);
	if (is_array($result))
	{
		$errstr = '<ol>';
		foreach ($result as $errormsg)
		{
			$errstr .= "<li>$errormsg</li>";
		}
		$errstr .= '</ol>';
		eval(standard_error(fetch_error('dbtech_vbshop_error_x', $errstr)));		
	}
	
	$info = array(
		'currencyid' 	=> $currency['currencyid'],
		'amount' 		=> $vbulletin->GPC['amount'],
	);

	// init data manager
	$dm =& VBSHOP::initDataManager('Transactionlog', $vbulletin, ERRTYPE_ARRAY);
		$dm->set('userid', 		$vbulletin->userinfo['userid']);
		$dm->set('recipient', 	$existing['userid']);
		$dm->set('ipaddress', 	IPADDRESS);
		$dm->set('action', 		'donate');
		$dm->set('info', 		$info);
	$dm->save();
	unset($dm);		

	$db->query_write("
		UPDATE " . TABLE_PREFIX . $currency['pointstable'] . "
		SET " . $currency['pointscolumn'] . " = " . $currency['pointscolumn'] . " - " . doubleval($vbulletin->GPC['amount']) . "
		WHERE userid = " . $vbulletin->userinfo['userid']
	);
	$db->query_write("
		UPDATE " . TABLE_PREFIX . $currency['pointstable'] . "
		SET " . $currency['pointscolumn'] . " = " . $currency['pointscolumn'] . " + " . doubleval($vbulletin->GPC['amount']) . "
		WHERE userid = " . $existing['userid']
	);
	
	// Success phrase
	$vbulletin->url = 'vbshop.php?' . $vbulletin->session->vars['sessionurl'] . 'do=bank';
	eval(print_standard_redirect('redirect_dbtech_vbshop_donatethanks'));	
}

// draw cp nav bar
VBSHOP::setNavClass('donate');