<?php if (!defined('access')) {die("Die gesuchte Ressource wurde entfernt oder umbenannt, oder sie steht vorübergehend nicht zur Verfügung.");} ?>
<?php

if(!empty($_GET['key'])) {
	if(empty($_GET['salt'])) {
		$validateSelect = @odbc_exec($odbc_connect, '
			SELECT *
			FROM [' . $_CONFIG['db_databases']['web'] . '].[dbo].[VALIDATE]
			WHERE [key] = \'' . cleanSqlInput($_GET['key']) . '\'
		');
		
		if(odbc_num_rows($validateSelect) > 0) {
			$validateSelectArray = @odbc_fetch_array($validateSelect);
			
			if($validateSelectArray['type'] == 0) {
				echo '<p class="page_title">' . $_LANG['validation_registration_siteTitle'] . '</p>';

				$account = $validateSelectArray['account'];
				$email = $validateSelectArray['fieldAlfa'];
				$pass = $validateSelectArray['fieldBravo'];
				$birthday = $validateSelectArray['fieldDelta'];
				$secretQuestion = $validateSelectArray['fieldEcho'];
				$secretAnswer = $validateSelectArray['fieldCharlie'];
				$ip = $validateSelectArray['fieldFoxtrot'];
				
				$_SESSION['user'] = strtolower($account);
				$_SESSION['cash'] = 0;
				$_SESSION['votepoints'] = 0;
                $_SESSION['lang'] = $_CONFIG['web_default_lang'];
				
				@odbc_exec($odbc_connect, '
					EXEC [' . $_CONFIG['db_databases']['acc'] . '].[dbo].[usp_createAccount] @account=\'' . $account . '\', @pw=\''.$pass.'\', @email=\''.$email.'\', @gefra=\''.$secretQuestion.'\', @geant=\''.$secretAnswer.'\', @birthday=\''.$birthday.'\', @ip = \'' . $ip . '\', @lang=\'' . $_CONFIG['web_default_lang'] . '\'
					DELETE FROM [' . $_CONFIG['db_databases']['web'] . '].[dbo].[VALIDATE] WHERE [key] = \''. cleanSqlInput($_GET['key']) . '\'
				');
				echo createMessage($_LANG['success_registration'] . '<br />' . $_LANG['redirection_five_seconds'], 'success');
				print '<meta http-equiv="refresh" content="4; URL=index.php">';
			}
			elseif($validateSelectArray['type'] == 1) {
				echo '<p class="page_title">' . $_LANG['validation_changePassword_siteTitle'] . '</p>';
		
				$account = $validateSelectArray['account'];
				$newpw = $validateSelectArray['fieldAlfa'];
				
				@odbc_exec($odbc_connect, '
					UPDATE 	[' . $_CONFIG['db_databases']['acc'] . '].[dbo].[ACCOUNT_TBL]
					SET 	[id_no2] = [password],
							[id_no1] = \''. $newpw .'\',
							[password] = \''. $newpw .'\' 
					WHERE [account] = \''.$account.'\'
					
					UPDATE 	[' . $_CONFIG['db_databases']['acc'] . '].[dbo].[ACCOUNT_TBL_DETAIL]
					SET		[latestpassword] = \''. $newpw .'\'
					WHERE [account] = \''.$account.'\'
					
					DELETE FROM [' . $_CONFIG['db_databases']['web'] . '].[dbo].[VALIDATE] WHERE [key] = \''.cleanSqlInput($_GET['key']).'\'
				');

				echo createMessage($_LANG['success_changePassword'] . '<br />' . $_LANG['redirection_five_seconds'], 'success');
				print '<meta http-equiv="refresh" content="4; URL=index.php">';
				
			}
			elseif($validateSelectArray['type'] == 2) {
				echo '<p class="page_title">' . $_LANG['validation_changeMail_siteTitle'] . '</p>';

				$account = $validateSelectArray['account'];
				$newmail = $validateSelectArray['fieldAlfa'];
			
				@odbc_exec($odbc_connect, '
					UPDATE [' . $_CONFIG['db_databases']['acc'] . '].[dbo].[ACCOUNT_TBL_DETAIL] SET [email] = \''.$newmail.'\' WHERE [account] = \''.$account.'\'
					DELETE FROM [' . $_CONFIG['db_databases']['web'] . '].[dbo].[VALIDATE] WHERE [key] = \''.cleanSqlInput($_GET['key']).'\'
				');
				echo  createMessage($_LANG['success_changeMail'] . '<br />' . $_LANG['redirection_five_seconds'], 'success');
				print '<meta http-equiv="refresh" content="4; URL=index.php">';
			}
			else {
				echo '<p class="page_title">' . $_LANG['validation_error_siteTitle'] . '</p>';
				echo createMessage($_LANG['error_validation_wrong_type'], 'fail');
			}
		}
		else {
			echo '<p class="page_title">' . $_LANG['validation_error_siteTitle'] . '</p>';
			echo createMessage($_LANG['error_validation_key_not_found'], 'fail');
		}
	}
	else {
		if($_GET['salt'] == $_CONFIG['donate_adminsalt'] AND in_array(strtolower($_SESSION['user']), $_CONFIG['allg_svr_admins'])) {
			echo '<p class="page_title">' . $_LANG['validation_donate_siteTitle'] . '</p>';

			$key = cleanSqlInput($_GET['key']);
			$checkDonationsArray = @odbc_fetch_array(@odbc_exec($odbc_connect, '
				SELECT 	[account],
						[points],
						[state] 
				FROM [' . $_CONFIG['db_databases']['web'] . '].[dbo].[LOG_DONATIONS]
				WHERE [key] = \'' . $key . '\'
			'));

			if(isset($checkDonationsArray['account'])) {
				if($checkDonationsArray['state'] == 0) {
					if(isset($_GET['denied'])) {
                        $strAccountToBan = @odbc_result(@odbc_exec($odbc_connect, 'SELECT [account] FROM [' . $_CONFIG['db_databases']['web'] . '].[dbo].[LOG_DONATIONS] WHERE [key] = \'' . $key . '\''), 'account');
                        $blockTimeBeforeNow = strtotime(odbc_result(odbc_exec($odbc_connect, 'SELECT [BlockTime] FROM [' . $_CONFIG['db_databases']['acc'] . '].dbo.[ACCOUNT_TBL_DETAIL] WHERE [account] = \'' . $strAccountToBan . '\''), 'BlockTime'));

                        @odbc_exec($odbc_connect, 'UPDATE [' . $_CONFIG['db_databases']['web'] . '].[dbo].[LOG_DONATIONS] set [state] = 2 WHERE [key] = \'' . $key . '\'');
						@odbc_exec($odbc_connect, 'UPDATE [' . $_CONFIG['db_databases']['acc'] . '].[dbo].[ACCOUNT_TBL_DETAIL] SET [BlockTime] = ' . date('Ymd', mktime(00,00,00,date('m'),date('d')+3,date('Y'))) .' WHERE [account] = \'' . $strAccountToBan . '\'');
                        @odbc_exec($odbc_connect, '
						    INSERT INTO [' . $_CONFIG['db_databases']['web'] . '].[dbo].[LOG_BANS] (
                                [account],
                                [before],
                                [now],
                                [byadmin],
                                [datetime],
                                [reason]
						    ) VALUES (
                                \'' . strtolower($strAccountToBan) . '\',
                                ' . $blockTimeBeforeNow . ',
                                ' . mktime(00,00,00,date('m'),date('d')+3,date('Y')) . ',
                                \'marvin\',
                                \'' . date('d.m.Y H:i:s') . '\',
                                \'Missbrauch des Spendensystems\'
						    )');
						echo createMessage($_LANG['error_donatepoints_denied'], 'hint');
					}
					else {
                        $resCheckForExtraReward = @odbc_exec($odbc_connect, 'SELECT [type], [worth], [account] FROM [' . $_CONFIG['db_databases']['web'] . '].[dbo].[LOG_DONATIONS] WHERE [key] = \'' . $key . '\'');
                        $intWorthOfDonation = intval(@odbc_result($resCheckForExtraReward, 'worth'));
                        $strMostActiveUnbannedCharacter = @odbc_result(odbc_exec($odbc_connect, 'SELECT TOP 1 [m_idPlayer] FROM [' . $_CONFIG['db_databases']['chr'] . '].[dbo].[CHARACTER_TBL] WHERE [account] = \'' . @odbc_result($resCheckForExtraReward, 'account') . '\'  AND [isblock] = \'F\' ORDER BY [TotalPlayTime] DESC'), 'm_idPlayer');

                        if($_CONFIG['donate_paypal_reward_enabled'] AND @odbc_result($resCheckForExtraReward, 'type') == 1 AND array_key_exists($intWorthOfDonation, $_CONFIG['donate_paypal_reward_count']) AND !empty($strMostActiveUnbannedCharacter)) {
                            if(send_item($strMostActiveUnbannedCharacter, $_CONFIG['donate_paypal_reward_item'], $_CONFIG['donate_paypal_reward_count'][$intWorthOfDonation])) {
								echo createMessage($_LANG['success_send_paypal_item'], 'success');
							}
                        }

						@odbc_exec($odbc_connect, '
							UPDATE [' . $_CONFIG['db_databases']['acc'] . '].[dbo].[ACCOUNT_TBL] set [cash] = [cash]+'.$checkDonationsArray['points'].' WHERE [account] = \''.$checkDonationsArray['account'].'\'
							UPDATE [' . $_CONFIG['db_databases']['web'] . '].[dbo].[LOG_DONATIONS] set [state] = 1 WHERE [key] = \'' . $key . '\'');
						
						echo createMessage($_LANG['success_dontepoints_added'], 'success');
					}
				}
				else {
					echo createMessage($_LANG['error_validation_donation_not_new'], 'fail');
				}
			}
			else {
				echo createMessage($_LANG['error_validation_donation_key_not_found'], 'fail');
			}
		}
		else {
			echo createMessage($_LANG['error_validation_donation_login_required'], 'fail');
		}
	}
}