<?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 />
    <div id="banpopup"></div>
<?php

if(isset($_POST['ban_submit'])) {
    $blockTimeBeforeNow = strtotime(odbc_result(odbc_exec($odbc_connect, 'SELECT [BlockTime] FROM [' . $_CONFIG['db_databases']['acc'] . '].dbo.[ACCOUNT_TBL_DETAIL] WHERE [account] = \'' . mssql_escape_string($_POST['account']) . '\''), 'BlockTime'));
    $blockTimeNow = strtotime($_POST['year'].$_POST['month'].$_POST['day']);
    odbc_exec($odbc_connect, '
        UPDATE [' . $_CONFIG['db_databases']['acc'] . '].dbo.[ACCOUNT_TBL_DETAIL]
        SET BlockTime = \''.mssql_escape_string($_POST['year']).mssql_escape_string($_POST['month']).mssql_escape_string($_POST['day']).'\'
		WHERE account = \''.mssql_escape_string($_POST['account']).'\'');
    odbc_exec($odbc_connect, '
        INSERT INTO [' . $_CONFIG['db_databases']['web'] . '].dbo.[LOG_BANS] (
            account,
            before,
            now,
            byadmin,
            reason,
            datetime
        ) VALUES(
            \''.mssql_escape_string($_POST['account']).'\',
            ' . $blockTimeBeforeNow . ',
            ' . $blockTimeNow . ',
            \''.mssql_escape_string($_SESSION['user']).'\',
            \''.mssql_escape_string($_POST['reason']).'\',
            \''.mssql_escape_string(date('d.m.Y H:i:s')).'\'
        )
	');
}

if((isset($_GET['character']) || isset($_GET['account']) || isset($_GET['ip'])) AND !empty($_GET['search'])) {

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

	if(isset($_GET['character'])) {

        $resCharacter = odbc_exec($odbc_connect, '
			SELECT  char.[m_szName],
					char.[m_idPlayer],
					char.[MultiServer],
					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] = \'' . $strSearchString . '\'
		');
		$resCharacterCheck = odbc_exec($odbc_connect, '
			SELECT  [m_szName]
			FROM    [' . $_CONFIG['db_databases']['chr'] . '].dbo.[CHARACTER_TBL]
			WHERE   [m_szName] LIKE \'%' . $strSearchString . '%\'
		');

		if(odbc_num_rows($resCharacter) == 1) {

			$resAccount = odbc_exec($odbc_connect, '
				SELECT  [BlockTime],
						[regdate],
						[regIP]
				FROM    [' . $_CONFIG['db_databases']['acc'] . '].dbo.[ACCOUNT_TBL_DETAIL]
				WHERE   [account] = \'' . @odbc_result($resCharacter, 'account') . '\'
			');
			$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') . '\'
				ORDER BY [Start_Time] DESC
			');
            if(isAccountBanned(odbc_result($resAccount, 'blocktime')) != 'Nein') {
                $resBanInformation = odbc_exec($odbc_connect, 'SELECT TOP 1 [byAdmin], [reason] FROM [' . $_CONFIG['db_databases']['web'] . '].dbo.[LOG_BANS] WHERE [account] = \'' . @odbc_result($resCharacter, 'account') . '\' ORDER BY [datetime] DESC');
                $strBannedReason = odbc_result($resBanInformation, 'reason');
                $strBannedBy = odbc_result($resBanInformation, 'byAdmin');

                if(empty($strBannedReason)) {
                    $strBannedReason = '?';
                }
                if(empty($strBannedBy)) {
                    $strBannedBy = '?';
                }
            }
            else {
                $strBannedReason = '-';
                $strBannedBy = '-';
            }

			echo '
<h2>Verfügbare Optionen</h2>
<ul style="margin: 0 0 0 14px; padding: 5px 0;">
    <li style="display: inline; margin: 0 5px;">&raquo; <a onclick="banPopup(\''.@odbc_result($resCharacter, 'account').'\');">Bannfunktion</a></li>
    <li style="display: inline; margin: 0 5px;">&raquo; <a href="search_trades.php?characterID='.@odbc_result($resCharacter, 'm_idPlayer').'">Handelslogs</a></li>
    <li style="display: inline; margin: 0 5px;">&raquo; <a href="search_mails.php?characterID='.@odbc_result($resCharacter, 'm_idPlayer').'">Maillogs</a></li>
    <li style="display: inline; margin: 0 5px;">&raquo; <a href="search_inventory.php?characterID='.@odbc_result($resCharacter, 'm_idPlayer').'">Inventar</a></li>
</ul>
<br class="clear" />

<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">Registrations-IP:</span>' . odbc_result($resAccount, 'regIP') . '&nbsp;</p>
	<p><span class="label">Gebannt:</span>' . isAccountBanned(odbc_result($resAccount, 'blocktime')) . '</p>
	<p><span class="label">Von:</span>' . $strBannedBy . '</p>
	<p><span class="label">Grund:</span>' . $strBannedReason . '</p>
	<br />
	<h2>Charakterinformationen</h2>
	<p><span class="label">ID:</span>' . odbc_result($resCharacter, 'm_idPlayer') . '</p>
	<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>' . checkCharacterOnlineState(odbc_result($resCharacter, 'MultiServer')) . checkCharacterState(odbc_result($resCharacter, 'isblock'), 'char') . '</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 id="tradepopup"></div>
<div id="loginList" style="float: left; width:50%;">
	<h2>Logins: Ingame</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($resCharacterCheck) >= 1) {
			echo '
<div style="float: left; width: 50%;">
	<h2>Meintest du ...</h2>
	<ul>';
			while($strCharacterArray = odbc_fetch_array($resCharacterCheck)) {
				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],
					[regIP]
			FROM    [' . $_CONFIG['db_databases']['acc'] . '].dbo.[ACCOUNT_TBL_DETAIL]
			WHERE   [account] = \'' . $strSearchString . '\'
		');
		$resAccountCheck = @odbc_exec($odbc_connect, '
			SELECT  [account]
			FROM    [' . $_CONFIG['db_databases']['acc'] . '].dbo.[ACCOUNT_TBL_DETAIL]
			WHERE   [account] LIKE \'%' . $strSearchString . '%\'
		');

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

			$resCharacter = @odbc_exec($odbc_connect, '
				SELECT  [m_szName],
						[m_idPlayer],
						[isblock],
						[MultiServer]
				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 . '\'
				ORDER BY [Start_Time] DESC
			');
			$resIPWebsite = odbc_exec($odbc_connect, '
				SELECT  [host],
						[ip],
						[timestamp],
						[browser]
				FROM    [' . $_CONFIG['db_databases']['web'] . '].dbo.[LOG_LOGIN]
				WHERE   [account] = \'' . $strSearchString . '\'
				ORDER BY [timestamp] DESC
			');

            if(isAccountBanned(odbc_result($resAccount, 'blocktime')) != 'Nein') {
                $resBanInformation = odbc_exec($odbc_connect, 'SELECT TOP 1 [byAdmin], [reason] FROM [' . $_CONFIG['db_databases']['web'] . '].dbo.[LOG_BANS] WHERE [account] = \'' . @odbc_result($resAccount, 'account') . '\' ORDER BY [datetime] DESC');
                $strBannedReason = odbc_result($resBanInformation, 'reason');
                $strBannedBy = odbc_result($resBanInformation, 'byAdmin');
            }
            else {
                $strBannedReason = '-';
                $strBannedBy = '-';
            }

			echo '
<h2>Verfügbare Optionen</h2>
<ul style="margin: 0 0 0 14px; padding: 5px 0;">
    <li style="display: inline; margin: 0 5px;">&raquo; <a onclick="banPopup(\'' . @odbc_result($resAccount, 'account') . '\');">Bannfunktion</a></li>
</ul>
<br class="clear" />
<div style="float: left; width: 50%;">
	<h2>Accountinformationen</h2>
	<p><span class="label">Account:</span>' . @odbc_result($resAccount, '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">Registrations-IP:</span>' . odbc_result($resAccount, 'regIP') . '&nbsp;</p>
	<p><span class="label">Gebannt:</span>' . isAccountBanned(odbc_result($resAccount, 'blocktime')) . '</p>
	<p><span class="label">Von:</span>' . $strBannedBy . '</p>
	<p><span class="label">Grund:</span>' . $strBannedReason . '</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>(' . checkCharacterOnlineState($strCharacterArray['MultiServer']) . ''  . checkCharacterState($strCharacterArray['isblock']) . ')</i></li>
			   ';
			}
			echo '
	</ul>
</div>
<div style="float: left; width:50%;">
	<div style="width: 50%; float: left;">
	    <h2>Logins: Ingame</h2>';
			while($strIPIngameArray =  odbc_fetch_array($resIPIngame)) {
				echo '
	<p>' . date('d.m.Y, H:i', strtotime($strIPIngameArray['Start_Time'])) . ' Uhr: <a href="search.php?search=' . $strIPIngameArray['remoteIP'] . '&ip">' . $strIPIngameArray['remoteIP'] . '</a></p>
				';
			}
			echo '
    </div>
	<div style="width: 50%; float: right;">
	    <h2>Logins: Website</h2>';
            while($strIPWebsiteArray =  odbc_fetch_array($resIPWebsite)) {
                echo '
	<p>' . date('d.m.Y, H:i', $strIPWebsiteArray['timestamp']) . ' Uhr: <a href="search.php?search=' . $strIPWebsiteArray['ip'] . '&ip" rel="tooltip" title="Browser: ' . $strIPWebsiteArray['browser'] . '">' . $strIPWebsiteArray['ip'] . ' / ' . $strIPWebsiteArray['host'] . '</a></p>
				';
            }
			echo '
    </div>
</div>';
		}
		elseif(odbc_num_rows($resAccountCheck) >= 1) {
			echo '
<div style="float: left; width: 50%;">
	<h2>Meintest du ...</h2>
	<ul>';
			while($strAccountArray = odbc_fetch_array($resAccountCheck)) {
				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'])) {
		$resIPIngame = @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
		');
        $resIPWebsite = odbc_exec($odbc_connect, '
				SELECT  [account],
				        [host],
						[timestamp],
						[browser]
				FROM    [' . $_CONFIG['db_databases']['web'] . '].dbo.[LOG_LOGIN]
				WHERE   [ip] LIKE \'%' . $strSearchString . '%\'
				ORDER BY [timestamp] DESC
		');

		echo '
		<div style="float: left; width: 50%;">
		    <h2>Ingamelogins</h2>
		    <ul>';
		while($strIPIngameArray =  odbc_fetch_array($resIPIngame)) {
			echo '
				<li style="padding: 5px 0;">' . date('d.m.Y, H:i', strtotime($strIPIngameArray['Start_Time'])) . ' Uhr:  <a href="search.php?search=' . $strIPIngameArray['account'] . '&account">' . $strIPIngameArray['account'] . '</a> / <a href="search.php?search=' . $strIPIngameArray['m_szName'] . '&character">' . $strIPIngameArray['m_szName'] . '</a> über ' . $strIPIngameArray['TotalPlayTime'] . ' Sekunden</li>
				';
		}
		echo '</ul>
		</div>';

		echo '
		<div style="float: left; width: 50%;">
		    <h2>Websitelogins</h2>
		    <ul>';
		while($strIPWebsiteArray =  odbc_fetch_array($resIPWebsite)) {
			echo '
				<li style="padding: 5px 0;">' . date('d.m.Y, H:i', $strIPWebsiteArray['timestamp']) . ' Uhr:  <a href="search.php?search=' . $strIPWebsiteArray['account'] . '&account" rel="tooltip" title="Browser: ' . $strIPWebsiteArray['browser'] . '">' . $strIPWebsiteArray['account'] . '  / ' . $strIPWebsiteArray['host'] . '</a></li>
				';
		}
		echo '</ul>
		</div>';
	}
}
?>
</div>
<?php include('inc/footer.php'); ?>