<?php include('inc/header.php'); ?>
<?php if(empty($_GET['search'])) {$_GET['search'] = '';} ?>
<h1>Suche</h1>
<div class="container">
<form name="search" method="get">
	<input type="text" name="search" placeholder="Account-/Charaktername oder IP" required="required" style="width: 253px; margin: 4px 0;" value="<?php echo $_GET['search'];?>"/><br />
	<input type="submit" name="character" value="Charaktersuche" />
	<input type="submit" name="account" value="Accountsuche" />
	<input type="submit" name="ip" value="IP-Suche" />
</form>
<hr />
<?php
if((isset($_GET['character']) || isset($_GET['account']) || isset($_GET['ip'])) AND !empty($_GET['search'])) {

	$strSearchString = mssql_escape_string($_GET['search']);

	if(isset($_GET['character'])) {
		$resCharacter = odbc_exec($odbc_connect, '
			SELECT  char.[m_szName],
					char.[m_idPlayer],
					char.[account],
					char.[m_nLevel],
					char.[m_nJob],
					char.[CreateTime],
					char.[isblock],
					bank.[m_BankPw]
			FROM    [' . $_CONFIG['db_databases']['chr'] . '].dbo.[CHARACTER_TBL] AS char
				LEFT JOIN [' . $_CONFIG['db_databases']['chr'] . '].dbo.[BANK_TBL] AS bank ON bank.[m_idPlayer] = char.[m_idPlayer]
			WHERE   [m_szName] LIKE \'%' . $strSearchString . '%\'
		');
		$resCharacterCheck = odbc_exec($odbc_connect, '
			SELECT  [m_szName]
			FROM    [' . $_CONFIG['db_databases']['chr'] . '].dbo.[CHARACTER_TBL] AS char
			WHERE   [m_szName] = \'' . $strSearchString . '\'
		');


		if(odbc_num_rows($resCharacter) == 1 OR odbc_num_rows($resCharacterCheck) == 1) {
			$resIPIngame = odbc_exec($odbc_connect, '
				SELECT  [remoteIP],
						[Start_Time]
				FROM    [' . $_CONFIG['db_databases']['log'] . '].dbo.[LOG_LOGIN_TBL]
				WHERE   [m_idPlayer] = \'' . @odbc_result($resCharacter, 'm_idPlayer') . '\'
			');
			$resAccount = odbc_exec($odbc_connect, '
				SELECT  [BlockTime],
						[regdate]
				FROM    [' . $_CONFIG['db_databases']['acc'] . '].dbo.[ACCOUNT_TBL_DETAIL]
				WHERE   [account] = \'' . @odbc_result($resCharacter, 'account') . '\'
			');


			echo '
<div style="float: left; width: 50%;">
	<h2>Accountinformationen</h2>
	<p><span class="label">Account:</span><a href="search.php?search=' . odbc_result($resCharacter, 'account') . '&account">' . odbc_result($resCharacter, 'account') . '</a></p>
	<p><span class="label">Registriert am:</span>' . date('d.m.Y, H:i', strtotime(odbc_result($resAccount, 'regdate'))) . ' Uhr</p>
	<p><span class="label">Gebannt:</span>' . isAccountBanned(odbc_result($resAccount, 'blocktime')) . '</p>
	<br />
	<h2>Charakterinformationen</h2>
	<p><span class="label">Name:</span>' . htmlspecialchars($strSearchString) . '</p>
	<p><span class="label">Erstellt am:</span>' . date('d.m.Y, H:s', strtotime(odbc_result($resCharacter, 'CreateTime'))) . ' Uhr</p>
	<p><span class="label">Status:</span>' . checkCharacterState(odbc_result($resCharacter, 'isblock')) . '</p>
	<br />
	<p><span class="label">Level / Platin:</span> ' . odbc_result($resCharacter, 'm_nLevel') . '</p>
	<p><span class="label">Klasse:</span> ' . getjob(odbc_result($resCharacter, 'm_nJob')) . '</p>
	<p><span class="label">Bankpasswort:</span>' . odbc_result($resCharacter, 'm_BankPw') . '</p>
</div>
<div style="float: left; width:50%;">
	<h2>Logins dieses Charakters</h2>';
			while($strIPArray = odbc_fetch_array($resIPIngame)) {
				echo '
	<p>' . date('d.m.Y, H:i', strtotime($strIPArray['Start_Time'])) . ' Uhr: <a href="search.php?search=' . $strIPArray['remoteIP'] . '&ip">' . $strIPArray['remoteIP'] . '</a></p>';
			}
			echo '
</div>';
		}
		elseif(odbc_num_rows($resCharacter) > 1) {
			echo '
<div style="float: left; width: 50%;">
	<h2>Meintest du ...</h2>
	<ul>';
			while($strCharacterArray = odbc_fetch_array($resCharacter)) {
				echo '
		<li style="padding: 5px 0;"><a href="search.php?search=' . $strCharacterArray['m_szName'] . '&character">' . $strCharacterArray['m_szName'] . '</a></li>
			   ';
			}
			echo '
	</ul>
</div>';
		}
		else {
			echo '<div class="fail">Kein Charakter gefunden.</div>';
		}
	}
	if(isset($_GET['account'])) {


		$resAccount = @odbc_exec($odbc_connect, '
			SELECT  [account],
					[BlockTime],
					[regdate]
			FROM    [' . $_CONFIG['db_databases']['acc'] . '].dbo.[ACCOUNT_TBL_DETAIL]
			WHERE   [account] LIKE \'%' . $strSearchString . '%\'
		');
		$resAccountCheck = @odbc_exec($odbc_connect, '
			SELECT  [account]
			FROM    [' . $_CONFIG['db_databases']['acc'] . '].dbo.[ACCOUNT_TBL_DETAIL]
			WHERE   [account] = \'' . $strSearchString . '\'
		');


		if(odbc_num_rows($resAccount) == 1 OR odbc_num_rows($resAccountCheck) == 1) {


			$resCharacter = @odbc_exec($odbc_connect, '
				SELECT  [m_szName],
						[m_idPlayer],
						[isblock]
				FROM    [' . $_CONFIG['db_databases']['chr'] . '].dbo.[CHARACTER_TBL]
				WHERE   [account] = \'' . $strSearchString . '\'
			');
			$resIPIngame = odbc_exec($odbc_connect, '
				SELECT  [remoteIP],
						[Start_Time]
				FROM    [' . $_CONFIG['db_databases']['log'] . '].dbo.[LOG_LOGIN_TBL]
				WHERE   [account] = \'' . $strSearchString . '\'
			');


			echo '
<div style="float: left; width: 50%;">
	<h2>Accountinformationen</h2>
	<p><span class="label">Account:</span>' . htmlspecialchars($strSearchString) . '</a></p>
	<p><span class="label">Registriert am:</span>' . date('d.m.Y, H:i', strtotime(odbc_result($resAccount, 'regdate'))) . ' Uhr</p>
	<p><span class="label">Gebannt:</span>' . isAccountBanned(odbc_result($resAccount, 'blocktime')) . '</p>
	<br />
	<h2>Charaktere</h2>
	<ul>';
			while($strCharacterArray = odbc_fetch_array($resCharacter)) {
				echo '
		<li style="padding: 5px 0;"><a href="search.php?search=' . $strCharacterArray['m_szName'] . '&character">' . $strCharacterArray['m_szName'] . '</a> <i>(' . checkCharacterState($strCharacterArray['isblock']) . ')</i></li>
			   ';
			}
			echo '
	</ul>
</div>
<div style="float: left; width:50%;">
	<h2>Logins dieses Accounts</h2>';
			while($strIPArray =  odbc_fetch_array($resIPIngame)) {
				echo '
	<p>' . date('d.m.Y, H:i', strtotime($strIPArray['Start_Time'])) . ' Uhr: <a href="search.php?search=' . $strIPArray['remoteIP'] . '&ip">' . $strIPArray['remoteIP'] . '</a></p>
				';
			}
			echo '
</div>';
		}
		elseif(odbc_num_rows($resAccount) > 1) {
			echo '
<div style="float: left; width: 50%;">
	<h2>Meintest du ...</h2>
	<ul>';
			while($strAccountArray = odbc_fetch_array($resAccount)) {
				echo '
		<li style="padding: 5px 0;"><a href="search.php?search=' . $strAccountArray['account'] . '&account">' . $strAccountArray['account'] . '</a></li>
			   ';
			}
			echo '
	</ul>
</div>';
		}
		else {
			echo '<div class="fail">Kein Account gefunden.</div>';
		}
	}
	if(isset($_GET['ip'])) {
		$resIP = @odbc_exec($odbc_connect, '
            SELECT  [m_szName],
                    ip.[account],
                    ip.[TotalPlayTime],
                    ip.[Start_Time]
            FROM    [' . $_CONFIG['db_databases']['log'] . '].dbo.[LOG_LOGIN_TBL] AS ip
                LEFT JOIN [' . $_CONFIG['db_databases']['chr'] . '].dbo.[CHARACTER_TBL] AS c ON c.[m_idPlayer] = ip.[m_idPlayer]
            WHERE   [remoteIP] LIKE \'%' . $strSearchString . '%\'
            ORDER BY [Start_Time] DESC
		');

		echo '<ul>';
		while($strIPArray =  odbc_fetch_array($resIP)) {
			echo '
				<li style="padding: 5px 0;">' . date('d.m.Y, H:i', strtotime($strIPArray['Start_Time'])) . ' Uhr:  <a href="search.php?search=' . $strIPArray['account'] . '&account">' . $strIPArray['account'] . '</a> / <a href="search.php?search=' . $strIPArray['m_szName'] . '&character">' . $strIPArray['m_szName'] . '</a> über ' . $strIPArray['TotalPlayTime'] . ' Sekunden</li>
				';
		}
		echo '</ul>';
	}
}
?>
</div>
<?php include('inc/footer.php'); ?>