<?php
session_start();
define('access', true);
include_once('../../includes/xinc_detection.php');
include('../../includes/xinc_config.php');
include('inc/functions.inc.php');

if(in_array($_SESSION['user'], $_CONFIG['allg_svr_admins'])) {
	odbc_exec($odbc_connect, 'USE [' . $_CONFIG['db_databases']['acc'] . ']');
		// Functions	
		
	if($_GET['a'] == 'denyValidation') {
		@odbc_exec($odbc_connect, 'DELETE FROM [' . $_CONFIG['db_databases']['web'] . '].[dbo].[VALIDATE] WHERE [key] = \''.mssql_escape_string($_GET['key']).'\'');
	}
	
	if($_GET['a'] == 'denyValidation') {
		@odbc_exec($odbc_connect, 'DELETE FROM [' . $_CONFIG['db_databases']['web'] . '].[dbo].[VALIDATE] WHERE [key] = \''.mssql_escape_string($_GET['key']).'\'');
	}
	
	if($_GET['a'] == 'acceptValidation') {
		$key = mssql_escape_string($_GET['key']);
		$validateArray = @odbc_exec($odbc_connect, 'SELECT * FROM [' . $_CONFIG['db_databases']['web'] . '].[dbo].[VALIDATE] WHERE [key] = \''.$key.'\'');
		$type = @odbc_result($validateArray, 'type');
		
		if($type == 0) {
			$account = odbc_result($validateArray, 'account');
			$pass = odbc_result($validateArray, 'fieldBravo');
			$email = odbc_result($validateArray, 'fieldAlfa');
			$gefra = odbc_result($validateArray, 'fieldEcho');
			$geant = odbc_result($validateArray, 'fieldCharlie');
			$bdate = odbc_result($validateArray, 'fieldDelta');
			$ip = odbc_result($validateArray, 'fieldFoxtrot');

			@odbc_exec($odbc_connect, 'EXEC [' . $_CONFIG['db_databases']['acc'] . '].[dbo].[usp_createAccount] @account=\''.$account.'\', @pw=\''.$pass.'\', @email=\''.$email.'\', @gefra=\''.$gefra.'\', @geant=\''.$geant.'\', @birthday = \'' . $bdate . '\', @ip=\''.$ip.'\', @lang=\'' . $_CONFIG['web_default_lang'] . '\'');
			@odbc_exec($odbc_connect, 'DELETE FROM [' . $_CONFIG['db_databases']['web'] . '].[dbo].[VALIDATE] WHERE [key] = \''.$key.'\'');
		}
	}
	
	odbc_exec($odbc_connect, 'USE [' . $_CONFIG['db_databases']['web'] . ']');
	
	if($_GET['a'] == 'pscdelete') {
		odbc_exec($odbc_connect, 'UPDATE [LOG_DONATIONS] SET [state] = 2 WHERE id = '.mssql_escape_string($_GET['id']));
	}
	
	if($_GET['a'] == 'reqitmaccept') {
		odbc_exec($odbc_connect, 'UPDATE [web_requestitem] SET status=\'1\' WHERE reqid=\''.mssql_escape_string($_GET['reqid']).'\'');
		$query = odbc_exec($odbc_connect, 'SELECT * FROM [web_requestitem] WHERE reqid=\''.mssql_escape_string($_GET['reqid']).'\'');
		$result = odbc_fetch_array($query);
		send_item($result['playerid'], $result['itemid'], $result['itemcount']);
	}
	
	if($_GET['a'] == 'reqitmdeny') {
		odbc_exec($odbc_connect, 'UPDATE [web_requestitem] SET status=\'2\' WHERE reqid=\''.mssql_escape_string($_GET['reqid']).'\'');
	}
	
	if($_GET['a'] == 'pscdone') {
		echo 'Erfolgreich gutgeschrieben <span style="float: right;color: #f30;cursor:pointer;" onclick="$(\'#box\').hide();">x</span><hr/>';
		echo '<span style="font-size: 11px;">Du hast' . $_GET['account'] . ' die ' . $_GET['points'] . ' dPs erfolgreich gutgeschrieben.</span><hr/>';
		odbc_exec($odbc_connect, '
			UPDATE [LOG_DONATIONS] SET state = 1 WHERE id = '.mssql_escape_string($_GET['id']).'
			UPDATE [' . $_CONFIG['db_databases']['acc'] . '].dbo.[ACCOUNT_TBL] SET cash=cash+\''.mssql_escape_string($_GET['points']).'\' WHERE account=\''.mssql_escape_string($_GET['account']).'\'
		');
	}
	
	if($_GET['a'] == 'pscundone') {
		echo 'Erfolgreich zurückgezogen <span style="float: right;color: #f30;cursor:pointer;" onclick="$(\'#box\').hide();">x</span><hr/>';
		echo '<span style="font-size: 11px;">Du hast ' . $_GET['account'] . ' die ' . $_GET['points'] . ' dPs wieder entfernt.</span><hr/>';
		odbc_exec($odbc_connect, '
			UPDATE [LOG_DONATIONS] SET state = 0 WHERE id = '.mssql_escape_string($_GET['id']).'
			UPDATE [' . $_CONFIG['db_databases']['acc'] . '].dbo.[ACCOUNT_TBL] SET cash=cash-\''.mssql_escape_string($_GET['points']).'\' WHERE account=\''.mssql_escape_string($_GET['account']).'\'
		');
	}
	
	if($_GET['a'] == 'banpopup') {
		$years = array(1970,date('Y'),date('Y')+1,date('Y')+2,date('Y')+3,date('Y')+10);
		$month = array('01'=>'Januar', '02'=>'Februar', '03'=>'M&auml;rz', '04'=>'April', '05'=>'Mai', '06'=>'Juni', '07'=>'Juli', '08'=>'August', '09'=>'September',10=>'Oktober', 11=>'November', 12 =>'Dezember');
		$days = array('01','02','03','04','05','06','07','08','09',10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31);
		echo 'Ban Account: <b>'.$_GET['account'].'</b> <span style="float: right;color: #f30;cursor:pointer;" onclick="$(\'#banpopup\').hide();">x</span><hr/>';
		echo '<form method="post">
				<select name="year" required="required">';
		foreach ($years as $year) 			{if(date('Y') == $year) {$selected=' selected';} else {$selected = '';} echo '<option value="'.$year.'"'.$selected.'>'.$year.'</option>';}
		echo '	</select> <select name="month" required="required">';
		foreach ($month as $month => $name) {if(date('m') == $month) {$selected=' selected';} else {$selected = '';} echo '<option value="'.$month.'"'.$selected.'>'.$name.'</option>';}
		echo '	</select> <select name="day" required="required">';
		foreach ($days as $day) 			{if(date('d') == $day) {$selected=' selected';} else {$selected = '';} echo '<option value="'.$day.'"'.$selected.'>'.$day.'</option>';}
		echo '	</select> 
				<br /><br /><input type="text" name="reason" value="" placeholder="Reason" required="required"/> <input type="submit" name="ban_submit" value="Submit" /></form>';
	}
	
	if($_GET['a'] == 'gamemaster') {
		if($_GET['gm'] == 'yes') {
			odbc_exec($odbc_connect, 'USE [' . $_CONFIG['db_databases']['chr'] . '] UPDATE [CHARACTER_TBL] SET m_chAuthority=\'L\' WHERE account=\''.mssql_escape_string($_GET['account']).'\'');
			odbc_exec($odbc_connect, 'USE [' . $_CONFIG['db_databases']['acc'] . '] UPDATE [ACCOUNT_TBL_DETAIL] SET m_chLoginAuthority=\'L\' WHERE account=\''.mssql_escape_string($_GET['account']).'\'');
		} else {
			odbc_exec($odbc_connect, 'USE [' . $_CONFIG['db_databases']['chr'] . '] UPDATE [CHARACTER_TBL] SET m_chAuthority=\'F\' WHERE account=\''.mssql_escape_string($_GET['account']).'\'');
			odbc_exec($odbc_connect, 'USE [' . $_CONFIG['db_databases']['acc'] . '] UPDATE [ACCOUNT_TBL_DETAIL] SET m_chLoginAuthority=\'F\' WHERE account=\''.mssql_escape_string($_GET['account']).'\'');
		}
	}
	
	if($_GET['a'] == 'faq_edit') {
		$select = odbc_exec($odbc_connect, 'SELECT * FROM [web_faq] WHERE faqid=\''.mssql_escape_string($_GET['faqid']).'\'');
		$result = odbc_fetch_array($select);
		echo 'Edit FAQ <span style="float: right;color: #f30;cursor:pointer;" onclick="$(\'#box\').hide();">x</span><hr/>
		<form method="post">
			<input type="hidden" name="edit_faqid" value="'.$_GET['faqid'].'" />
			<table id="table">
				<tr>
					<td id="key">Question:</td>
					<td id="value"><input type="text" name="edit_question" value="'.$result['question'].'" style="width: 300px;" /></td>
				</tr>
				<tr>
					<td id="key" valign="top">Answer:</td>
					<td id="value"><textarea name="edit_answer" style="width:304px; height:150px;">'.$result['answer'].'</textarea></td>
				</tr>
				<tr>
					<td></td>
					<td id="value"><input type="submit" name="savefaqsbm" value="Save FAQ" /></td>
				</tr>
			</table>
		</form>';
	}
	
	if($_GET['a'] == 'faq_delete') {
		odbc_exec($odbc_connect, 'DELETE FROM [web_faq] WHERE faqid=\''.mssql_escape_string($_GET['faqid']).'\'');
		echo '<div class="success">The FAQ has successfully been deleted!</div>';
	}
	
	
	if($_GET['a'] == 'rule_edit') {
		$select = odbc_exec($odbc_connect, 'SELECT * FROM [web_rules] WHERE id=\''.mssql_escape_string($_GET['id']).'\'');
		$result = odbc_fetch_array($select);
		echo 'Paragraphen bearbeiten <span style="float: right;color: #f30;cursor:pointer;" onclick="$(\'#box\').hide();">x</span><hr/>
		<form method="post">
			<input type="hidden" name="edit_faqid" value="'.$_GET['id'].'" />
			<table id="table">
				<tr>
					<td id="key">Paragraph:</td>
					<td id="value"><input type="text" name="edit_list" value="'.$result['list'].'" maxlength="2" size="2" /></td>
				</tr>
				<tr>
					<td id="key">Titel:</td>
					<td id="value"><input type="text" name="edit_question" value="'.$result['paragraph'].'" style="width: 300px;" /></td>
				</tr>
				<tr>
					<td id="key" valign="top">Inhalt:</td>
					<td id="value"><textarea name="edit_answer" style="width:304px; height:150px;">'.$result['text'].'</textarea></td>
				</tr>
				<tr>
					<td></td>
					<td id="value"><input type="submit" name="saverule" value="Paragraph abspeichern" /></td>
				</tr>
			</table>
		</form>';
	}
	
	if($_GET['a'] == 'rule_delete') {
		odbc_exec($odbc_connect, 'DELETE FROM [web_rules] WHERE id=\''.mssql_escape_string($_GET['id']).'\'');
		echo '<div class="success">Der Paragraph wurde erfolgreich gel&ouml;scht!</div>';
	}
	
	
	
	if($_GET['a'] == 'banlogs') {
		if(!isset($_GET['page'])) $_GET['page'] = 1;
		$page = $_GET['page'];
		$perpage = 20;
		$select = odbc_exec($odbc_connect, 'SELECT * FROM [LOG_BANS] ORDER BY datetime DESC');
		$resultset = array();
		$counter = 0;
		while($row = odbc_fetch_array($select)) {
			$resultset[$counter++] = $row;
		}
		$all = odbc_exec($odbc_connect, 'SELECT * FROM [LOG_BANS]');
		$count = odbc_num_rows($all);
		$countpages = $count / $perpage;
		echo '<table id="table" style="width: 100%;">
			<tr>
				<td id="key">Account</td>
				<td id="key">By admin</td>
				<td id="key" style="text-align: center;"><table align="center"><tr><td><img src="../images/date.png" /></td><td>Date/Time</td></tr></table></td>
				<td id="key" style="text-align: center;">Banned</td>
			</tr>';
		for($i = $perpage * ($page-1);$i < $perpage*$page;$i++) {
			if(isset($resultset[$i])) {
				odbc_exec($odbc_connect, 'USE [' . $_CONFIG['db_databases']['acc'] . ']');
				$resbanned = odbc_exec($odbc_connect, 'SELECT * FROM [ACCOUNT_TBL_DETAIL] WHERE account=\''.mssql_escape_string($resultset[$i]['account']).'\'');
				$resultbanned = odbc_fetch_array($resbanned);
				echo '<tr id="banid_'.$resultset[$i]['banid'].'" class="row">
					<td id="value">'.$resultset[$i]['account'].'</td>
					<td id="value">'.$resultset[$i]['byadmin'].'</td>
					<td id="value" style="text-align: center;">'.date('Y/m/d H:i', strtotime($resultset[$i]['datetime'])).'</td>
					<td id="value" style="text-align: center;">';
					if(strtotime($resultbanned['BlockTime']) > strtotime(date('Ymd'))) {
						echo '<a title="Account \''.$resultset[$i]['account'].'\' is still banned." rel="tooltip"><img src="../images/banned.png" /></a>';
					} else {
						echo '<a title="Account \''.$resultset[$i]['account'].'\' is not banned anymore." rel="tooltip"><img src="../images/unbanned.png" /></a>';
					}
				echo '</td>
				</tr>';
			}
		}
		echo '</table><br/>';
		for($i=0; $i < $countpages; $i++) {
			$k = $i + 1;
			if($page == $k) {
				 echo '<span id="page"><b>'.$k.'</b></span>';
			} else {
				echo '<span id="page"><a href="javascript:banPage('.$k.');">'.$k.'</a></span>';
			}
		}
	}
	
	if($_GET['a'] == 'reqitm') {
		if(!isset($_GET['page'])) $_GET['page'] = 1;
		$page = $_GET['page'];
		$perpage = 20;
		$select = odbc_exec($odbc_connect, 'SELECT * FROM [web_requestitem] ORDER BY datetime DESC');
		$resultset = array();
		$counter = 0;
		while($row = odbc_fetch_array($select)) {
			$resultset[$counter++] = $row;
		}
		$all = odbc_exec($odbc_connect, 'SELECT * FROM [web_requestitem]');
		$count = odbc_num_rows($all);
		$countpages = $count / $perpage;
		echo '<div id="text"></div>';
		echo '<div id="box"></div>';
		echo '<table id="table" style="width: 100%;">
			<tr>
				<td id="key">GameMaster</td>
				<td id="key">Item ID:Name</td>
				<td id="key">Count</td>
				<td id="key">Cause</td>
				<td id="key" style="text-align: center;">Status</td>
				<td id="key" style="text-align: center;"><table align="center"><tr><td><img src="../images/date.png" /></td><td>Date/Time</td></tr></table></td>
				<td id="key" style="width: 60px;"></td>
			</tr>';
		for($i = $perpage * ($page-1);$i < $perpage*$page;$i++) {
			if(isset($resultset[$i])) {
				odbc_exec($odbc_connect, 'USE [' . $_CONFIG['db_databases']['chr'] . ']');
				$charname = odbc_exec($odbc_connect, 'SELECT m_szName FROM [CHARACTER_TBL] WHERE m_idPlayer=\''.mssql_escape_string($resultset[$i]['playerid']).'\'');
				echo '<tr id="reqid_'.$resultset[$i]['reqid'].'" class="row">
					<td id="value">'.odbc_result($charname, 'm_szName').'</td>
					<td id="value">'.$resultset[$i]['itemid'].':'.$resultset[$i]['itemname'].'</td>
					<td id="value">'.$resultset[$i]['itemcount'].'</td>
					<td id="value">'.$resultset[$i]['cause'].'</td>
					<td id="value" style="text-align: center;"><span id="reqid_'.$resultset[$i]['reqid'].'_status">';
				if($resultset[$i]['status'] == 0) {
					echo 'Open';
				} else {
					echo 'Closed';
				}
				echo '</span></td>
					<td id="value" style="text-align: center;">'.date('Y/m/d H:i', strtotime($resultset[$i]['datetime'])).'</td>
					<td id="value">';
				if($resultset[$i]['status'] == 0) {
						echo' <a href="javascript:reqitmAccept('.$resultset[$i]['reqid'].');" id="reqid_'.$resultset[$i]['reqid'].'_accept" title="Accept" rel="tooltip"><img src="../images/success.png" /></a>
						<a href="javascript:reqitmDeny('.$resultset[$i]['reqid'].');" id="reqid_'.$resultset[$i]['reqid'].'_deny" title="Deny" rel="tooltip"><img src="../images/fail.png" /></a>';
				}
				echo'
					</td>
				</tr>';
			}
		}
		echo '</table><br/>';
		for($i=0; $i < $countpages; $i++) {
			$k = $i + 1;
			if($page == $k) {
				 echo '<span id="page"><b>'.$k.'</b></span>';
			} else {
				echo '<span id="page"><a href="javascript:reqitmPage('.$k.');">'.$k.'</a></span>';
			}
		}
	}
	
	if($_GET['a'] == 'psclogs') {
echo '	<div id="pageinc">
	<h2>Suche nach Account-ID</h2>
	<table id="table" style="width:100%;">
		<tr><td colspan="6"><form method="post"><input type="text" name="account" style="width:150px;" value="'.$_POST['account'].'"/> <input type="submit" value="Suche starten" name="searchpsc"/></form></td></tr>';
if(isset($_POST['searchpsc'])) {
$_POST['account'] = htmlspecialchars($_POST['account']);
$abf_q = odbc_exec($odbc_connect, 'SELECT * FROM [web_psclogs] WHERE [account] LIKE \'%'.$_POST['account'].'%\' ORDER BY datetime DESC ');
echo '	<tr>
			<td width="19%" id="key">Account</td>
			<td width="16%" id="key">PIN Code</td>
			<td width="15%" id="key">Password</td>
			<td width="20%" id="key"><table align="center"><tr><td><img src="../images/price.png" /></td><td>Worth</td></tr></table></td>
			<td width="22%" id="key" style="text-align: center;"><table align="center"><tr><td><img src="../images/date.png" /></td><td>Date/Time</td></tr></table></td>
			<td width="8%" id="key"></td>
		</tr>';
if(odbc_num_rows($abf_q) > 0) {
	while($abf_a = @odbc_fetch_array($abf_q))
	echo '<tr class="row">
			<td id="value">'.str_ireplace($_POST['account'], '<b>'.$_POST['account'].'</b>', $abf_a['account']).'</td>
			<td id="value">'.$abf_a['pincode'].'</td>
			<td id="value">'.$abf_a['password'].'</td>
			<td id="value" align="center">'.$abf_a['worth'].' &euro;</td>
			<td id="value" align="center">'.date('Y/m/d H:i', strtotime($abf_a['datetime'])).'</td>
			<td id="value"><img src="../images/delete.png" /></td>
		</tr>';
} else {echo '<tr class="row"><td colspan="6">Es wurden keine Einträge gefunden, die den Suchtag <b>'.$_POST['account'].'</b> beinhalten.</td></tr>';}
} else {$_POST['account'] = '';}
echo '</table><br /><br />';
		if(!isset($_GET['page'])) $_GET['page'] = 1;
		$page = $_GET['page'];
		$perpage = 20;
		$select = odbc_exec($odbc_connect, 'SELECT * FROM [web_psclogs] ORDER BY datetime DESC');
		$resultset = array();
		$counter = 0;
		while($row = odbc_fetch_array($select)) {
			$resultset[$counter++] = $row;
		}
		$all = odbc_exec($odbc_connect, 'SELECT * FROM [web_psclogs]');
		$count = odbc_num_rows($all);
		$countpages = $count / $perpage;
		echo '<div id="text"></div>';
		echo '<div id="box"></div><h2>Spendenlogs</h2>';
		echo '<table id="table" style="width: 100%;">
			<tr>
				<td width="6%" id="key">Typ</td>
				<td width="20%" id="key">Account</td>
				<td width="16%" id="key">PIN Code</td>
				<td width="15%" id="key">Password</td>
				<td width="15%" id="key"><table align="center"><tr><td><img src="../images/price.png" /></td><td>Worth</td></tr></table></td>
				<td width="20%" id="key" style="text-align: center;"><table align="center"><tr><td><img src="../images/date.png" /></td><td>Date/Time</td></tr></table></td>
				<td width="8%" id="key"></td>
			</tr>';
		for($i = $perpage * ($page-1);$i < $perpage*$page;$i++) {
			if(isset($resultset[$i])) {
	if($resultset[$i]['type'] == 0) {$resultset[$i]['type'] = 'PSC';}
	elseif ($resultset[$i]['type'] == 1) {$resultset[$i]['type'] = 'Paypal'; $resultset[$i]['pincode'] = $resultset[$i]['email']; $resultset[$i]['password'] = $resultset[$i]['name'];}
	
	echo '<tr id="pscid_'.$resultset[$i]['pscid'].'" class="row">
		<td id="value">'.$resultset[$i]['type'].'</td>
		<td id="value">'.$resultset[$i]['account'].'</td>
		<td id="value">'.$resultset[$i]['pincode'].'</td>
		<td id="value">'.$resultset[$i]['password'].'</td>
		<td id="value" style="text-align: center;">';
				if($resultset[$i]['done'] == 1) {
					echo '<table align="center"><tr><td>'.$resultset[$i]['worth'].' &euro;</td><td><a href="javascript:('.$resultset[$i]['pscid'].', '.$resultset[$i]['account'].', \''.$resultset[$i]['worth'].'\');" title="Give dPoints." rel="tooltip"><img src="../images/go.gif" id="givedp_'.$resultset[$i]['pscid'].'" style="display: none;" /></td></tr></table>';
				} else {
					echo '<table align="center"><tr><td>'.$resultset[$i]['worth'].' &euro;</td><td><a href="javascript:giveDP('.$resultset[$i]['pscid'].', '.$resultset[$i]['account'].', \''.$resultset[$i]['worth'].'\');" title="Give dPoints." rel="tooltip"><img src="../images/go.gif" id="givedp_'.$resultset[$i]['pscid'].'" /></td></tr></table>';
				}
				echo '</td>
					<td id="value" style="text-align: center;">'.date('Y/m/d H:i', strtotime($resultset[$i]['datetime'])).'</td>
					<td id="value">';
				if($resultset[$i]['done'] == 1) {
					echo '<a href="javascript:pscUndone('.$resultset[$i]['pscid'].');" id="pscid_'.$resultset[$i]['pscid'].'_done" title="Status: Done<br/> (Set to \'\'undone\'\')" rel="tooltip"><img src="../images/unbanned.png" /></a>';
				} else {
					echo '<a href="javascript:pscDone('.$resultset[$i]['pscid'].');" id="pscid_'.$resultset[$i]['pscid'].'_undone" title="Status: Undone<br/> (Set to \'\'done\'\')" rel="tooltip"><img src="../images/banned.png" /></a>';
				}
				echo '<a href="javascript:psclogs_delete('.$resultset[$i]['pscid'].');" title="Delete" rel="tooltip"><img src="../images/delete.png" /></a>
					</td>
				</tr>';
			}
		}
		echo '</table><br/>';
		for($i=0; $i < $countpages; $i++) {
			$k = $i + 1;
			if($k == 51) {echo '<br /><br />';}
			if(strlen($k) == 1) {$k = '0'.$k;}
			if($page == $k) {
				 echo '<span id="page"><b>'.$k.'</b></span>';
			} else {
				echo '<span id="page"><a href="javascript:pscPage('.$k.');">'.$k.'</a></span>';
			}
		}
	}
	
	if($_GET['a'] == 'giftlogs') {
		if(!isset($_GET['page'])) $_GET['page'] = 1;
		$page = $_GET['page'];
		$perpage = 30;
		$select = odbc_exec($odbc_connect, 'SELECT * FROM [LOG_GIFTS] ORDER BY datetime DESC');
		$resultset = array();
		$counter = 0;
		while($row = odbc_fetch_array($select)) {
			$resultset[$counter++] = $row;
		}
		$all = odbc_exec($odbc_connect, 'SELECT * FROM [LOG_GIFTS]');
		$count = odbc_num_rows($all);
		$countpages = $count / $perpage;
		echo '<table id="table" style="width: 100%;" cellspacing="0" cellpadding="3">
			<tr>
				<td id="key">Von Account</td>
				<td id="key">An Account (Charakter)</td>
				<td id="key">Item</td>
				<td id="key" style="text-align: center;"><img src="../images/count.png" /> Menge</td>
				<td id="key" style="text-align: center;"><img src="../images/price.png" /> dPs</td>
				<td id="key" style="text-align: center;"><img src="../images/date.png" /> Datum / Uhrzeit</td>
			</tr>';
		for($i = $perpage * ($page-1);$i < $perpage*$page;$i++) {
			if(isset($resultset[$i])) {
				$mall = odbc_exec($odbc_connect, 'SELECT * FROM [web_mall] WHERE mid=\''.$resultset[$i]['item'].'\'');
				$item = odbc_fetch_array($mall);
				echo '<tr id="giftid_'.$resultset[$i]['giftid'].'" class="row">
					<td id="value">'.$resultset[$i]['accfrom'].'</td>
					<td id="value">'.$resultset[$i]['accto'].' ('.$resultset[$i]['charto'].')</td>
					<td id="value">'.$resultset[$i]['itemname'].'</td>
					<td id="value" style="text-align: center;">'.$resultset[$i]['itemcount'].'</td>
					<td id="value" style="text-align: center;">'.$resultset[$i]['itemworth'].'</td>
					<td id="value" style="text-align: center;">'.date('d.m.Y, H:i', strtotime($resultset[$i]['datetime'])).'</td>
				</tr>';
			}
		}
		echo '</table><br/>';
		for($i=0; $i < $countpages; $i++) {
			$k = $i + 1;
			if($page == $k) {
				 echo '<span id="page"><b>'.$k.'</b></span>';
			} else {
				echo '<span id="page"><a href="javascript:giftPage('.$k.');">'.$k.'</a></span>';
			}
		}
	}
	
	if($_GET['a'] == 'buylogs') {
		if(!isset($_GET['page'])) $_GET['page'] = 1;
		$page = $_GET['page'];
		$perpage = 30;
		$select = odbc_exec($odbc_connect, 'SELECT * FROM [LOG_PURCHASES] ORDER BY datetime DESC');
		$resultset = array();
		$counter = 0;
		while($row = odbc_fetch_array($select)) {
			$resultset[$counter++] = $row;
		}
		$all = odbc_exec($odbc_connect, 'SELECT * FROM [LOG_PURCHASES]');
		$count = odbc_num_rows($all);
		$countpages = $count / $perpage;
	echo '<table id="table" style="width: 100%;" cellspacing="0" cellpadding="3">
		<tr>
			<td id="key">Account</td>
			<td id="key">Item</td>
			<td id="key" style="text-align: center;"><img src="../images/count.png" /> Menge</td>
			<td id="key" style="text-align: center;"><img src="../images/price.png" /> dPs</td>
			<td id="key" style="text-align: center;"><img src="../images/date.png" /> Datum / Uhrzeit</td>
		</tr>';
		for($i = $perpage * ($page-1);$i < $perpage*$page;$i++) {
			if(isset($resultset[$i])) {
			echo '
			<tr id="bid_'.$resultset[$i]['bid'].'" class="row">
				<td id="value">'.$resultset[$i]['account'].'</td>
				<td id="value">'.$resultset[$i]['itemname'].'</td>
				<td id="value" style="text-align: center;">'.$resultset[$i]['itemcount'].'</td>
				<td id="value" style="text-align: center;">'.$resultset[$i]['itemworth'].'</td>
				<td id="value" style="text-align: center;">'.date('d.m.Y, H:i', strtotime($resultset[$i]['datetime'])).'</td>
			</tr>';
			}
		}
		echo '</table><br/>';
		for($i=0; $i < $countpages; $i++) {
			$k = $i + 1;
			if($page == $k) {
				 echo '<span id="page"><b>'.$k.'</b></span>';
			} else {
				echo '<span id="page"><a href="javascript:buyPage('.$k.');">'.$k.'</a></span>';
			}
		}
	}
	
	if($_GET['a'] == 'save_setting') {
		setConfigValue($_GET['type'], $_GET['new']);
		echo $_GET['new'];
	}
	
	if($_GET['a'] == 'shopitempage') {
		if(!isset($_GET['page'])) $_GET['page'] = 1;
		$page = $_GET['page'];
		$perpage = 20;
		$select = odbc_exec($odbc_connect, 'SELECT * FROM [web_mall] ORDER BY mid DESC');
		$resultset = array();
		$counter = 0;
		while($row = odbc_fetch_array($select)) {
			$resultset[$counter++] = $row;
		}
		$all = odbc_exec($odbc_connect, 'SELECT * FROM [web_mall]');
		$countall = odbc_num_rows($all);
		$countpages = $countall / $perpage;
		echo '<table id="table" width="100%">
		<tr>
			<td id="key" width="06%">ID</td>
			<td id="key" width="20%">Item</td>
			<td id="key" width="32%">Description</td>
			<td id="key" width="06%" align="center">Count</td>
			<td id="key" width="06%" align="center">Price</td>
			<td id="key" width="12%" align="center">Category</td>
			<td id="key" width="12%" align="center">Icon</td>
			<td id="key" width="06%"></td>
		</tr>';
		for($i = $perpage * ($page-1);$i < $perpage*$page;$i++)
		{
			if(isset($resultset[$i])) {
				$category = odbc_exec($odbc_connect, 'SELECT category FROM [web_mallcategories] WHERE mcatid=\''.$resultset[$i]['category'].'\'');
				$description = $resultset[$i]['description'];
				echo '<tr id="mid_'.$resultset[$i]['mid'].'" class="row">
					<td id="value">'.$resultset[$i]['itemid'].'</td>
					<td id="value">'.$resultset[$i]['name'].'</td>
					<td id="value"><textarea>'.$description.'</textarea></td>
					<td id="value" align="center">'.$resultset[$i]['count'].'</td>
					<td id="value" align="center">'.$resultset[$i]['price'].'</td>
					<td id="value" align="center">'.odbc_result($category, 'category').'</td>
					<td id="value" align="center"><a href="../img/mall/'.$resultset[$i]['icon'].'">'.$resultset[$i]['icon'].'</a></td>
					<td id="value"><a href="javascript:shopitem_edit('.$resultset[$i]['mid'].');" title="Edit" rel="tooltip"><img src="../images/edit.png" /></a> <a href="javascript:shopitem_delete('.$resultset[$i]['mid'].');" title="Delete" rel="tooltip"><img src="../images/delete.png" /></a></td>
				</tr>';
			}
		}
		echo '</table><br/>';
		for($i=0; $i < $countpages; $i++) {
			$k = $i + 1;
			if($page == $k) {
				echo '<span id="page"><b>'.$k.'</b></span>';
			} else {
				echo '<span id="page"><a href="javascript:shopItemPage('.$k.')">'.$k.'</a></span>';
			}
		}
	}



	if($_GET['a'] == 'shopitem_edit') {
		$select = odbc_exec($odbc_connect, 'SELECT * FROM [SHOP_ITEMLIST] WHERE mid=\''.mssql_escape_string($_GET['mid']).'\'');
		$result = odbc_fetch_array($select);
		$categories = odbc_exec($odbc_connect, '
			SELECT sub.[sid], sub.[name], main.[name] AS "mainCat" FROM [SHOP_CATEGORIES_SUB] AS "sub"
				LEFT JOIN [SHOP_CATEGORIES_MAIN] AS "main" ON main.[mid] = sub.[mid]
			WHERE sub.[active] = 1 AND main.[active] = 1
			ORDER BY main.mid ASC, sub.[sid] ASC
		');

		$strSwitchCurrencyNULL = ''; $strSwitchCurrency0 = ''; $strSwitchCurrency1 = '';
		if(is_null($result['currency'])) {$strSwitchCurrencyNULL = 'selected';} elseif($result['currency'] == 0) {$strSwitchCurrency0 = 'selected';} elseif($result['currency'] == 1) {$strSwitchCurrency1 = 'selected';}

		echo 'Edit Shop Item <span style="float: right;color: #f30;cursor:pointer;" onclick="$(\'#box\').hide();">x</span><hr/>
		<form method="post">
			<input type="hidden" name="edit_mid" value="'.$_GET['mid'].'" />
			<table id="table">
			<tr>
				<td id="key">Item ID:</td>
				<td id="value"><input type="text" name="edit_itemid" value="'.$result['itemid'].'" style="width: 300px;" /></td>
			</tr>
			<tr>
				<td id="key">Itemname:</td>
				<td id="value"><input type="text" name="edit_item" value="'.$result['name'].'" style="width: 300px;" /></td>
			</tr>
			<tr>
				<td id="key" style="vertical-align: top;">Beschreibung:</td>
				<td id="value"><textarea name="edit_description" style="width: 304px; height:100px;">'.$result['description'].'</textarea></td>
			</tr>
			<tr>
				<td id="key" style="vertical-align: top;">Effekte:</td>
				<td id="value"><textarea name="edit_effects" style="width: 304px; height:60px;">'.$result['effects'].'</textarea></td>
			</tr>
			<tr>
				<td id="key">Währung:</td>
				<td id="value"><select name="edit_currency">
					<option value="" ' . $strSwitchCurrencyNULL .  '>Optional</option>
					<option value="0" ' . $strSwitchCurrency0 .  '>dPs</option>
					<option value="1" ' . $strSwitchCurrency1 .  '>Votepoints</option></select></td>
			</tr>
			<tr>
				<td id="key">Preis:</td>
				<td id="value"><input type="text" name="edit_price" value="'.$result['price'].'" style="width: 50px;" /></td>
			</tr>
			<tr>
				<td id="key">Menge:</td>
				<td id="value"><input type="text" name="edit_count" value="'.$result['count'].'" style="width: 50px;" /></td>
			</tr>
			<tr>
				<td id="key">Kategorie:</td>
				<td id="value">
					<select name="edit_category" style="width: 300px;">';
				while($category = odbc_fetch_array($categories)) {
					if($category['sid'] == $result['category']) {
						echo '<option value="'.$category['sid'].'" selected>'.$category['mainCat'].' &raquo; '.$category['name'].'</option>';
					} else {
						echo '<option value="'.$category['sid'].'">'.$category['mainCat'].' &raquo; '.$category['name'].'</option>';
					}
				}
				echo '</select>
				</td>
			</tr>
			<tr>
				<td id="key">Icon:</td>
				<td id="value"><input type="text" name="edit_icon" value="'.$result['icon'].'" style="width: 200px;" /></td>
			</tr>
				<tr>
					<td></td>
					<td id="value"><input type="submit" name="saveshopitemsbm" value="Save Item" /></td>
				</tr>
			</table>
		</form>';
	}
	
	if($_GET['a'] == 'account_delete') {
		odbc_exec($odbc_connect, $_GET['b']);
		echo '<div class="success">The item has successfully been deleted!</div>';
	}
	
	if($_GET['a'] == 'shopitem_delete') {
		odbc_exec($odbc_connect, 'DELETE FROM [SHOP_ITEMLIST] WHERE mid=\''.mssql_escape_string($_GET['mid']).'\'');
		echo '<div class="success">The item has successfully been deleted!</div>';
	}
	
	if($_GET['a'] == 'shopcat_edit') {
		$select = odbc_exec($odbc_connect, 'SELECT * FROM [web_mallcategories] WHERE mcatid=\''.mssql_escape_string($_GET['mcatid']).'\'');
		$result = odbc_fetch_array($select);
		echo 'Edit Shop Category <span style="float: right;color: #f30;cursor:pointer;" onclick="$(\'#box\').hide();">x</span><hr/>
		<form method="post">
			<input type="hidden" name="edit_mcatid" value="'.$_GET['mcatid'].'" />
			<table id="table">
				<tr>
					<td id="key">ID:</td>
					<td id="value"><input type="text" name="edit_category" value="'.$result['mcatid'].'" style="width: 300px;" /></td>
				</tr>
				<tr>
					<td id="key">Category:</td>
					<td id="value"><input type="text" name="edit_category" value="'.$result['category'].'" style="width: 300px;" /></td>
				</tr>
				<tr>
					<td></td>
					<td id="value"><input type="submit" name="saveshopcatsbm" value="Save Category" /></td>
				</tr>
			</table>
		</form>';
	}




	if($_GET['a'] == 'editCategoryMain') {
		$select = @odbc_fetch_array(odbc_exec($odbc_connect, 'SELECT * FROM [' . $_CONFIG['db_databases']['web'] . '].[dbo].[SHOP_CATEGORIES_MAIN] WHERE mid=\''.mssql_escape_string($_GET['mid']).'\''));

		if($select['active'] == 0) {$strSwitchActive0 = 'selected';} else {$strSwitchActive1 = 'selected';}

		echo '
		<strong>Bearbeite Hauptkategorie</strong> <span style="float: right;color: #f30;cursor:pointer;" onclick="$(\'#box\').hide();">x</span><hr/>
		<form method="post"><input type="hidden" name="oldmid" style="width: 200px;" value="' . $_GET['mid'] . '"/>
			<table id="table">
				<tr>
					<td id="key">ID:</td>
					<td id="value"><input type="text" name="mid" style="width: 200px;" value="' . $select['mid'] . '"/></td>
				</tr>
				<tr>
					<td id="key">Name:</td>
					<td id="value"><input type="text" name="name" style="width: 200px;" value="' . $select['name'] . '"/></td>
				</tr>
				<tr>
					<td id="key">Aktiv?</td>
					<td id="value"><select name="active"><option value="0" ' . $strSwitchActive0 . '>Nein</option><option value="1"' . $strSwitchActive1 . '>Ja</option></select></td>
				</tr>
				<tr>
					<td></td>
					<td id="value"><input type="submit" name="updateCategoryMain" value="Speichere Hauptkategorie" /></td>
				</tr>
			</table>
		</form>';
	}
	if($_GET['a'] == 'deleteCategoryMain') {
		odbc_exec($odbc_connect, 'DELETE FROM [SHOP_CATEGORIES_MAIN] WHERE mid=\''.mssql_escape_string($_GET['mid']).'\'');
		echo '<div class="success">Die Hauptkategorie wurde erfolgreich gelöscht.</div>';
	}

	if($_GET['a'] == 'editCategorySub') {
		$select = @odbc_fetch_array(odbc_exec($odbc_connect, 'SELECT * FROM [' . $_CONFIG['db_databases']['web'] . '].[dbo].[SHOP_CATEGORIES_SUB] WHERE sid=\''.mssql_escape_string($_GET['sid']).'\''));

		if($select['currency'] == 0) {$strSwitchCurrency0 = 'selected';} else {$strSwitchCurrency1 = 'selected';}
		if($select['active'] == 0) {$strSwitchActive0 = 'selected';} else {$strSwitchActive1 = 'selected';}

		$resSelectCategoriesMain = odbc_exec($odbc_connect, 'SELECT * FROM [' . $_CONFIG['db_databases']['web'] . '].[dbo].[SHOP_CATEGORIES_MAIN] ORDER BY [mid] ASC');
		echo '
		<strong>Bearbeite Unterkategorie</strong> <span style="float: right;color: #f30;cursor:pointer;" onclick="$(\'#box\').hide();">x</span><hr/>
		<form method="post"><input type="hidden" name="oldsid" style="width: 200px;" value="' . $_GET['sid'] . '"/>
			<table id="table">
				<tr>
					<td id="key">ID:</td>
					<td id="value"><input type="text" name="sid" style="width: 200px;" value="' . $select['sid'] . '"/></td>
				</tr>
				<tr>
					<td id="key">Hauptkategorie:</td>
					<td id="value"><select name="mid">';
						while($result = odbc_fetch_array($resSelectCategoriesMain)) {echo '<option value="' . $result['mid'] . '" '; if($select['mid'] == $result['mid']) {echo 'selected';}  echo ' >' . $result['name'] . '</option>';}
echo '				</td>
				</tr>
				<tr>
					<td id="key">Name:</td>
					<td id="value"><input type="text" name="name" style="width: 200px;" value="' . $select['name'] . '"/></td>
				</tr>
				<tr>
					<td id="key">Währung:</td>
					<td id="value"><select name="currency"><option value="0" ' . $strSwitchCurrency0 . '>dPs</option><option value="1" ' . $strSwitchCurrency1 . '>Votepoints</option></select></td>
				</tr>
				<tr>
					<td id="key">Aktiv?</td>
					<td id="value"><select name="active"><option value="0" ' . $strSwitchActive0 . '>Nein</option><option value="1"' . $strSwitchActive1 . '>Ja</option></select></td>
				</tr>
				<tr>
					<td></td>
					<td id="value"><input type="submit" name="updateCategorySub" value="Speichere Unterkategorie" /></td>
				</tr>
			</table>
		</form>';
	}
	if($_GET['a'] == 'deleteCategorySub') {
		odbc_exec($odbc_connect, 'DELETE FROM [SHOP_CATEGORIES_SUB] WHERE sid=\''.mssql_escape_string($_GET['sid']).'\'');
		echo '<div class="success">Die Unterkategorie wurde erfolgreich gelöscht.</div>';
	}

























	
	if($_GET['a'] == 'save_shopsetting') {
		setConfigValue('shop_'.$_GET['type'], $_GET['new']);
		echo $_GET['new'];
	}
	
	if($_GET['a'] == 'category_edit') {
		$select = odbc_exec($odbc_connect, 'SELECT * FROM [web_newscategories] WHERE ncatid=\''.mssql_escape_string($_GET['catid']).'\'');
		$result = odbc_fetch_array($select);
		echo 'Edit News <span style="float: right;color: #f30;cursor:pointer;" onclick="$(\'#box2\').hide();">x</span><hr/>
		<form method="post">
			<input type="hidden" name="edit_catid" value="'.$_GET['catid'].'" />
			<table id="table">
				<tr>
					<td id="key">Title:</td>
					<td id="value"><input type="text" name="edit_cattitle" value="'.$result['title'].'" style="width: 300px;" /></td>
				</tr>
				<tr>
					<td id="key">Image:</td>
					<td id="value"><input type="text" name="edit_catimage" value="'.$result['icon'].'" style="width: 300px;" /></td>
				</tr>
				<tr>
					<td></td>
					<td id="value"><input type="submit" name="savecatsbm" value="Save Category" /></td>
				</tr>
			</table>
		</form>';
	}
	
	if($_GET['a'] == 'category_delete') {
		odbc_exec($odbc_connect, 'DELETE FROM [web_newscategories] WHERE ncatid=\''.mssql_escape_string($_GET['catid']).'\'');
		echo '<div class="success">The category has successfully been deleted!</div>';
	}
	
	if($_GET['a'] == 'news_edit') {
		$select = odbc_exec($odbc_connect, 'SELECT * FROM [web_news] WHERE nid=\''.mssql_escape_string($_GET['nid']).'\'');
		$result = odbc_fetch_array($select);
		$categories = odbc_exec($odbc_connect, 'SELECT * FROM [web_newscategories]');
		$textcontent = $result['text'];
		echo '<span style="display: none;">'.$textcontent.'</span>';
		echo 'Edit News <span style="float: right;color: #f30;cursor:pointer;" onclick="$(\'#box\').hide();">x</span><hr/>
		<form method="post">
			<input type="hidden" name="edit_nid" value="'.$_GET['nid'].'" />
			<table id="table">
				<tr>
					<td id="key">Title:</td>
					<td id="value"><input type="text" name="edit_title" value="'.$result['title'].'" style="width: 300px;" /></td>
				</tr>
				<tr>
					<td id="key">Category:</td>
					<td id="value">
						<select name="edit_category" style="width: 306px;">';
						while($category = odbc_fetch_array($categories)) {
							if($category['ncatid'] == $result['category']) {
								echo '<option value="'.$category['ncatid'].'" selected>'.$category['title'].'</option>';
							} else {
								echo '<option value="'.$category['ncatid'].'">'.$category['title'].'</option>';
							}
						}
						echo '</select>
					</td>
				</tr>
				<tr>
					<td id="key">Forenlink:</td>
					<td id="value"><input type="text" name="edit_forenlink" value="'.$result['forenlink'].'" style="width: 300px;" /></td>
				</tr>
				<tr>
					<td id="key">Content:</td>
					<td id="value"><textarea name="edit_content" style="width: 500px; height: 200px;">'.$textcontent.'</textarea></td>
				</tr>
				<tr>
					<td></td>
					<td id="value"><input type="submit" name="savenewssbm" value="Save News" /></td>
				</tr>
			</table>
		</form>';
	}
	
	if($_GET['a'] == 'news_delete') {
		odbc_exec($odbc_connect, 'DELETE FROM [web_news] WHERE nid=\''.mssql_escape_string($_GET['nid']).'\'');
		echo '<div class="success">The news has successfully been deleted!</div>';
	}
	
	if($_GET['a'] == 'download_edit') {
		$select = odbc_exec($odbc_connect, 'SELECT * FROM [web_downloads] WHERE dlid=\''.mssql_escape_string($_GET['did']).'\'');
		$result = odbc_fetch_array($select);
		echo 'Edit Download <span style="float: right;color: #f30;cursor:pointer;" onclick="$(\'#box\').hide();">x</span><hr/>
		<form action="" method="post">
			<input type="hidden" name="edit_did" value="'.$_GET['did'].'" />
			<table id="table">
				<tr>
					<td id="key">Title:</td>
					<td id="value"><input type="text" name="edit_title" value="'.$result['title'].'" style="width: 300px;" /></td>
				</tr>
				<tr>
					<td id="key">Link:</td>
					<td id="value"><input type="text" name="edit_link" value="'.$result['link'].'" style="width: 300px;" /></td>
				</tr>
				<tr>
					<td id="key" style="vertical-align: top;">Description:</td>
					<td id="value"><textarea name="edit_description" style="width: 300px; height: 50px;">'.$result['description'].'</textarea></td>
				</tr>
				<tr>
					<td></id>
					<td id="value"><input type="submit" name="savedownloadsbm" value="Save Download" /></id>
				</tr>
			</table>
		</form>';
	}
	
	if($_GET['a'] == 'download_delete') {
		odbc_exec($odbc_connect, 'DELETE FROM [web_downloads] WHERE dlid=\''.mssql_escape_string($_GET['did']).'\'');
		echo '<div class="success">The download has successfully been deleted!</div>';
	}
	
	if($_GET['a'] == 'partner_delete') {
		odbc_exec($odbc_connect, 'DELETE FROM [web_partner] WHERE pid=\''.mssql_escape_string($_GET['pid']).'\'');
		echo '<div class="success">The partner has successfully been deleted!</div>';
	}
	
	if($_GET['a'] == 'partner_edit') {
		$select = odbc_exec($odbc_connect, 'SELECT * FROM [web_partner] WHERE pid=\''.mssql_escape_string($_GET['pid']).'\'');
		$result = odbc_fetch_array($select);
		echo 'Edit Partner <span style="float: right;color: #f30;cursor:pointer;" onclick="$(\'#box\').hide();">x</span><hr/>
		<form action="" method="post">
			<input type="hidden" name="edit_pid" value="'.$_GET['pid'].'" />
			<table id="table">
				<tr>
					<td id="key">Title:</id>
					<td id="value"><input type="text" name="edit_title" value="'.$result['title'].'" style="width: 300px;" /></id>
				</tr>
				<tr>
					<td id="key">Link:</id>
					<td id="value"><input type="text" name="edit_link" value="'.$result['link'].'" style="width: 300px;" /></id>
				</tr>
				<tr>
					<td id="key">Banner:</id>
					<td id="value"><input type="text" name="edit_banner" value="'.$result['banner'].'" style="width: 300px;" /></id>
				</tr>
				<tr>
					<td></id>
					<td id="value"><input type="submit" name="savepartnersbm" value="Save Partner" /></id>
				</tr>
			</table>
		</form>';
	}
	
	if($_GET['a'] == 'save_mail') {
		setConfigValue('mail_'.$_GET['type'], $_GET['new']);
		echo $_GET['new'];
	}
}
else {
	echo 'Access denied. Sorry.';
}
?>