<?php if (!defined('access')) {die("Die gesuchte Ressource wurde entfernt oder umbenannt, oder sie steht vorübergehend nicht zur Verfügung.");} ?>
<p class="page_title">Itemshop</p>
<?php
if(in_array($_SESSION['user'], $_CONFIG['allg_svr_admins']) OR $_CONFIG['donate_shop_enable']) {

	if(!empty($_SESSION['user'])) {
		if((isset($_POST['shopBuyItem']) OR isset($_POST['shopGiftItem'])) AND !empty($_POST['sendItemToCharacter']) AND !empty($_POST['sid']) AND !empty($_POST['iid']) AND isset($_POST['currency'])) {

			$strErrorArray =  array();
			$resSelectedItemDetailsArray = @odbc_exec($odbc_connect, 'SELECT * FROM [' . $_CONFIG['db_databases']['web'] . '].dbo.[SHOP_ITEMLIST] WHERE [mid] = ' . cleanSqlInput($_POST['iid']) . ' AND [category] = ' . cleanSqlInput($_POST['sid']) . '');
			$resSelectCharacter = @odbc_exec($odbc_connect, 'SELECT [m_idPlayer], [account] FROM [' . $_CONFIG['db_databases']['chr'] . '].[dbo].[CHARACTER_TBL] WHERE [m_szName] = \'' . cleanSqlInput($_POST['sendItemToCharacter']) . '\'');

			$mixedSelectedItemDetailsArray = @odbc_fetch_array($resSelectedItemDetailsArray);
			$intCharacterID = @odbc_result($resSelectCharacter, 'm_idPlayer');
			$intCharactersAccount = @odbc_result($resSelectCharacter, 'account');
			$intCategorySubCheck = @odbc_result(@odbc_exec($odbc_connect, 'SELECT [currency] FROM [' . $_CONFIG['db_databases']['web'] . '].dbo.[SHOP_CATEGORIES_SUB] WHERE [sid] = ' . cleanSqlInput($_POST['sid']) . ''), 'currency');

			if($_POST['currency'] == 1) {
				$intCurrentBalance = @odbc_result(@odbc_exec($odbc_connect, 'SELECT [votepoints] FROM [' . $_CONFIG['db_databases']['acc'] . '].[dbo].[ACCOUNT_TBL] WHERE [account] = \'' . cleanSqlInput($_SESSION['user']) . '\''), 'votepoints');
				$strUpdateBalance = '
						UPDATE [' . $_CONFIG['db_databases']['acc'] . '].[dbo].[ACCOUNT_TBL]
                        SET [votepoints] = [votepoints] - ' . $mixedSelectedItemDetailsArray['price'] . '
                        WHERE [account] = \'' . cleanSqlInput($_SESSION['user']) . '\'
                    ';
				$strSessionSelector = 'votepoints';
			}
			else {
				$intCurrentBalance = @odbc_result(@odbc_exec($odbc_connect, 'SELECT [cash] FROM [' . $_CONFIG['db_databases']['acc'] . '].[dbo].[ACCOUNT_TBL] WHERE [account] = \'' . cleanSqlInput($_SESSION['user']) . '\''), 'cash');
				$strUpdateBalance = '
						UPDATE [' . $_CONFIG['db_databases']['acc'] . '].[dbo].[ACCOUNT_TBL]
                        SET [cash] = [cash] - ' . $mixedSelectedItemDetailsArray['price'] . '
                        WHERE [account] = \'' . cleanSqlInput($_SESSION['user']) . '\'
                    ';
				$strSessionSelector = 'cash';
			}

			if(is_null($mixedSelectedItemDetailsArray['currency'])) {
				if($intCategorySubCheck == 1) {$intCheckCurrency = 1;}
				else {$intCheckCurrency = 0;}
			}
			else {
				if($mixedSelectedItemDetailsArray['currency'] == 1) {$intCheckCurrency = 1;}
				else {$intCheckCurrency = 0;}
			}

			if(isSpamming($_POST['inputCheckTimestamp'], $_POST['inputCheckKey'], $_SESSION['inputCheckKey'])) {
				$strErrorArray[] = $_LANG['error_spam'];
			}
			if(@odbc_num_rows($resSelectedItemDetailsArray) < 1) {
				$strErrorArray[] = $_LANG['error_item_does_not_exist'];
			}
			if(@odbc_num_rows($resSelectCharacter) < 1) {
				$strErrorArray[] = $_LANG['error_no_character_with_that_name'];
			}
			if($_POST['currency'] != $intCheckCurrency) {
				$strErrorArray[] = $_LANG['error_currency_changed'];
			}
			if($mixedSelectedItemDetailsArray['price'] > $intCurrentBalance) {
				$strErrorArray[] = $_LANG['error_balace_too_low'];
			}
			if(count($strErrorArray) > 0) {
				echo createMessage($strErrorArray, 'fail');
			}
			else {
				if(isset($_POST['shopBuyItem'])) {
					if(	@odbc_exec($odbc_connect, $strUpdateBalance)
						AND
							@odbc_exec($odbc_connect, '
								INSERT INTO [' . $_CONFIG['db_databases']['web'] . '].dbo.[LOG_PURCHASES] (
									item,
									itemname,
									itemcount,
									itemcurrency,
									itemworth,
									account,
									datetime
								) VALUES (
									' . cleanSqlInput($mixedSelectedItemDetailsArray['itemid']) . ',
									\'' . cleanSqlInput($mixedSelectedItemDetailsArray['name']) . '\',
									' . cleanSqlInput($mixedSelectedItemDetailsArray['count']) . ',
									' . $intCheckCurrency . ',
									' . cleanSqlInput($mixedSelectedItemDetailsArray['price']) . ',
									\'' . cleanSqlInput($_SESSION['user']) . '\',
									\''. date($_CONFIG['web_date_format']['dbDatetime']) .'\'
								)
							')
						AND
							send_item($intCharacterID, $mixedSelectedItemDetailsArray['itemid'], $mixedSelectedItemDetailsArray['count'])
					) {
						$_SESSION[$strSessionSelector] = $_SESSION[$strSessionSelector] - $mixedSelectedItemDetailsArray['price'];
						echo createMessage($_LANG['success_item_bought'], 'success');
					}
					else {
						echo createMessage($_LANG['error_item_could_not_be_sent'], 'fail');
					}
				}
				elseif(isset($_POST['shopGiftItem'])) {
					if(	@odbc_exec($odbc_connect, $strUpdateBalance)
						AND
						@odbc_exec($odbc_connect, '
							INSERT INTO [' . $_CONFIG['db_databases']['web'] . '].dbo.[LOG_GIFTS] (
								item,
								itemname,
								itemcount,
								itemcurrency,
								itemworth,
								accfrom,
								accto,
								charto,
								datetime
							) VALUES (
								' . cleanSqlInput($mixedSelectedItemDetailsArray['itemid']) . ',
								\'' . cleanSqlInput($mixedSelectedItemDetailsArray['name']) . '\',
								' . cleanSqlInput($mixedSelectedItemDetailsArray['count']) . ',
								' . $intCheckCurrency . ',
								' . cleanSqlInput($mixedSelectedItemDetailsArray['price']) . ',
								\'' . cleanSqlInput($_SESSION['user']) . '\',
								\'' . $intCharactersAccount . '\',
								\'' . cleanSqlInput($_POST['sendItemToCharacter']) . '\',
								\''. date($_CONFIG['web_date_format']['dbDatetime']) .'\'
							)
						')
						AND
							send_item($intCharacterID, $mixedSelectedItemDetailsArray['itemid'], $mixedSelectedItemDetailsArray['count'])
					) {
						$_SESSION[$strSessionSelector] = $_SESSION[$strSessionSelector] - $mixedSelectedItemDetailsArray['price'];
						echo createMessage($_LANG['success_item_gifted'], 'success');
					}
					else {
						echo createMessage($_LANG['error_item_could_not_be_sent'], 'fail');
					}
				}
			}
		}
		echo '
		<p class="center">
			' . $_LANG['shop_your_funds'] . ' <img src="images/icons/shop/price.png" alt="' . $_CONFIG['web_currencies']['donate']['name'] . '"/> ' . $_SESSION['cash'] . ' ' . $_CONFIG['web_currencies']['donate']['symbol'] . '
			& <img src="images/icons/shop/votepoints.png" alt="' . $_CONFIG['web_currencies']['vote']['name'] . '"/> ' . $_SESSION['votepoints'] . ' ' . $_CONFIG['web_currencies']['vote']['symbol'] . '
		</p>
		<hr />';
	}

	$toggleDisplays = array(
		'subCategories' => '',
		'itemDetails' => '',
		'itemList' => ''
	);
	$mixedCategoryMainArray = getShopContent('site_shop_categories_main');

	if(is_numeric($_GET['sid']) OR is_numeric($_GET['iid'])) {
        $toggleDisplays['subCategories'] = 'display: block;';
        $toggleDisplays['itemList'] = 'display: block;';

		$mixedCategorySubArray = getShopContent('site_shop_categories_sub', 1);
		$mixedItemlistArray = getShopContent('site_shop_itemlist', 4);

		if(is_numeric($_GET['iid'])) {
			$toggleDisplays['itemDetails'] = 'display: block;';

			$mixedMainCategoryID = $mixedItemlistArray[$_GET['iid']][0];
			$mixedSubCategoryID = $mixedItemlistArray[$_GET['iid']][2];
		}
		else {
			$mixedMainCategoryID = $mixedCategorySubArray[$_GET['sid']][0];
			$mixedSubCategoryID = $_GET['sid'];
		}
	}

	echo '
<p id="categoryMainContainer" class="center">';
	foreach($mixedCategoryMainArray AS $strMainCategory) {
		if(isset($mixedMainCategoryID) AND $mixedMainCategoryID == $strMainCategory[0]) {
			$strCategoryMainActiveClass = 'active';}
		else {
			$strCategoryMainActiveClass = '';}
		echo ' <a class="categoryMain contentButton ' . $strCategoryMainActiveClass . '" uid="' . $strMainCategory[0] . '" href="#">' . $strMainCategory[1] . '</a> ';
	}
	echo '
</p>
<div id="categorySubNav" class="center">
    <p id="categorySubContainer">';
		if(isset($mixedCategorySubArray) AND count($mixedCategorySubArray) > 0) {
			foreach($mixedCategorySubArray AS $strSubCategory) {
				if($strSubCategory[0] == $mixedMainCategoryID) {
					if($mixedSubCategoryID == $strSubCategory[1]) {
						$strCategorySubActiveClass = 'active';}
					else {
						$strCategorySubActiveClass = '';}
					echo ' <a class="categorySub contentButton ' . $strCategorySubActiveClass . '" uid="' . $strSubCategory[1] . '" href="#">' . $strSubCategory[2] . '</a> ';
				}
			}
		}
		echo'
    </p>
</div>
<div id="itemDetails" class="shopBorder" style="' . $toggleDisplays['itemDetails'] . '">
    <div id="itemDetailsContainer">';
	if(isset($mixedItemlistArray) AND is_numeric($_GET['iid']) AND count($mixedItemlistArray) > 0) {
		$strItemDetails = $mixedItemlistArray[$_GET['iid']];

		echo '
			<p>
				<img src="images/icons/shop/'.$strItemDetails[10].'" class="itemDetailsIcon" alt="" />
			</p>
			<div class="itemDetails">
				<div>' . $_LANG['name'] . ': <span>' . $strItemDetails[6] . '</span><p class="clear"></p></div>
				<div>' . $_LANG['price'] . ': <span>' . getCurrencyHTML($strItemDetails[12], $strItemDetails[13], $strItemDetails[8]) . '</span></div>
				<div>' . $_LANG['amount'] . ': <span><img src="images/icons/shop/count.png" alt=""/> ' . $strItemDetails[9] . '</span></div>
		';
		if(!empty($strItemDetails[11]) AND strlen($strItemDetails[11]) > 1) {
			$strItemDetails[11] = explode("\r\n", $strItemDetails[11]);
			echo '
				<div>' . $_LANG['effects'] . ':
					<ul>';
			foreach($strItemDetails[11] as $listItem) {
				echo '<li>' . $listItem . '</li>';
			}
			echo '
					</ul>
                	<div class="clear"></div>
				</div>';
		}
		echo '
				<div>' . $_LANG['category'] . ': <span><a href="index.php?site=shop&sid=' . $strItemDetails[2] . '">' . $strItemDetails[1] . ' &raquo; ' . $strItemDetails[3] . '</a></span></div>
				<div>' . $_LANG['directItemlink'] . ': <span><a href="index.php?site=shop&iid=' . $strItemDetails[4] . '">' . $strItemDetails[6] . '</a></span></div>
			</div>';
		if(!empty($strItemDetails[7]) AND strlen($strItemDetails[7]) > 1) {
			echo '
				<div class="itemDescription">
					<span class="px140 bold">' . $_LANG['description'] . ':</span><br />' . nl2br($strItemDetails[7]) . '
				</div>';
		}
		echo '
        <div class="clear"></div>';
		if(isset($_SESSION['user'])) {
			$strCharacterArray = getAccountsCharacters(cleanSqlInput($_SESSION['user']));

			if($strItemDetails[12] == 'NULL') {
				if($strItemDetails[13] == 1) {$currency = 1;}
				else {$currency = 0;}
			}
			else {
				if($strItemDetails[12] == 1) {$currency = 1;}
				else {$currency = 0;}
			}

			echo '
        <div class="buy">
            <form method="post" action="index.php?site=shop&iid=' . $strItemDetails[4] . '">
                <input type="hidden" name="inputCheckTimestamp" value="' . time() . '" />
                <input type="hidden" name="inputCheckKey" value="' . random_string() . '" />
                <input type="hidden" name="sid" value="'.$strItemDetails[2].'" />
                <input type="hidden" name="iid" value="'.$strItemDetails[4].'" />
                <input type="hidden" name="currency" value="'.$currency.'" />
                <select name="sendItemToCharacter" class="voteitem" required="required">
                    <option selected="selected" value=""></option>';
			foreach($strCharacterArray AS $m_szName) {
				echo '<option value="' . $m_szName . '">' . $m_szName . '</option>';
			}
			echo '	</select><br />
                <input type="submit" name="shopBuyItem" value="' . $_LANG['buy'] . '"/>
            </form>
        </div>';
			echo '
        <div class="gift">
            <form method="post" action="index.php?site=shop&iid=' . $strItemDetails[4] . '">
                <input type="hidden" name="inputCheckTimestamp" value="' . time() . '" />
                <input type="hidden" name="inputCheckKey" value="' . random_string() . '" />
                <input type="hidden" name="sid" value="'.$strItemDetails[2].'" />
                <input type="hidden" name="iid" value="'.$strItemDetails[4].'" />
                <input type="hidden" name="currency" value="'.$currency.'" />
			    <input type="text" name="sendItemToCharacter" class="account" required="required" placeholder="' . $_LANG['placeholder_please_enter_character_name'] . '"/><br />
                <input type="submit" name="shopGiftItem" value="' . $_LANG['gift'] . '"/>
            </form>
        </div>';
			echo '<div class="clear"></div>';
	}
}
echo'
    </div>
</div>
<div id="itemList" class="shopBorder" style="' . $toggleDisplays['itemList'] . '">
    <div id="itemListContainer">';
	if(isset($mixedItemlistArray) AND count($mixedItemlistArray) > 0) {
		foreach($mixedItemlistArray AS $strItemArray) {
			if($strItemArray[2] == $mixedSubCategoryID) {

				if(isset($_SESSION['user']) AND $_SESSION['cash'] >= $strItemArray[8]) {
					$class = 'buy';
				}
				elseif(isset($_SESSION['user']) AND $_SESSION['cash'] < $strItemArray[8]) {
					$class = 'nobuy';
				}
				else {
					$class = 'none';
				}

				echo '
        <div class="singleItem ' . $class . '" uid="' . $strItemArray[4] . '">
            <p>' . $strItemArray[6] . '</p>
            <div class="itemShortDesc">
                <img class="itemIcon" src="images/icons/shop/'.$strItemArray[10].'" alt="' . $strItemArray[6] . '"/>
                <p>' . getCurrencyHTML($strItemArray[12], $strItemArray[13], $strItemArray[8]) . '</p>
                <p><img src="images/icons/shop/count.png" alt=""/> ' . $strItemArray[9] . 'x</p>
            </div>
        </div>';
			}
		}
	}
		echo '
	    <div class="clear"></div>
    </div>
</div>';
	}
	else {
		echo createMessage($_LANG['error_shop_disabled'], 'hint');
	}
?>
<hr />
<p class="center">
    | <img src="/images/icons/shop/votepoints.png" alt="<?php echo $_CONFIG['web_currencies']['vote']['name'];?> (<?php echo $_CONFIG['web_currencies']['vote']['symbol'];?>)"/> <?php echo $_CONFIG['web_currencies']['vote']['name'];?> (<?php echo $_CONFIG['web_currencies']['vote']['symbol'];?>)
    | <img src="/images/icons/shop/price.png" alt="<?php echo $_CONFIG['web_currencies']['donate']['name'];?> (<?php echo $_CONFIG['web_currencies']['donate']['symbol'];?>)"/> <?php echo $_CONFIG['web_currencies']['donate']['name'];?> (<?php echo $_CONFIG['web_currencies']['donate']['symbol'];?>)
    | <img src="/images/icons/shop/count.png" alt="<?php echo $_LANG['shop_amount_of_item']; ?>"/> <?php echo $_LANG['shop_amount_of_item']; ?>
    |
</p>