<?php
if($__TOKEN = "hardcodeshitbykernstudios"){
	if(isset($_SESSION['nEMID'])){
		echo '<h2 class="bg-h2-1 bot">Password Change</h2>';
	$conn = sqlsrv_connect($__CONFIG['SQLHost'], array("Database"=>$__CONFIG['SQLDB'], "UID"=>$__CONFIG['SQLUID'], "PWD"=>$__CONFIG['SQLPWD']));
	if(!$conn){
					echo print_r(sqlsrv_errors(), true);
					exit();
				}
		$checkAccount = sqlsrv_query($conn, "SELECT nEMID, sUsername, sEmail, dDate, nAGPoints FROM tAccounts WHERE nEMID = ?;", array($_SESSION['nEMID']), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
		$fetchAccount = sqlsrv_fetch_array($checkAccount);
		$checkCharacter = sqlsrv_query($conn, "SELECT nCharNo, sID, nLevel, nMoney FROM w00_Character..tCharacter WHERE nUserNo = ? AND bDeleted = 0;", array($_SESSION['nEMID']));
		
		If (!$fetchAccount['nEMID'] == $_SESSION['nEMID']){
			
			echo '<center>Game Account not activated!!!</center>';
			echo '<center>
			<form method="post">
			<input type="submit" name="submit" value="Activate Account" class="myButton">
			</form>
			</center>';
			if (isset($_POST['submit']))
			{
				$insert = sqlsrv_query($conn, "INSERT INTO tAccounts (nEMID, sUsername, sUserPass, sUserPassSalt, sEmail, sIP, nAuthID) VALUES (?, ?, ?, ?, ?, ?, ?);", array($_SESSION['nEMID'], $_SESSION['UserID'], 'CheckForum', 'Forum', 'CheckForum', getRemoteIP(), 1));
				showMessage('success', '<strong>Welcome</strong> to Noctis Gaming! Successfully activated.', 3, 'http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]);
				if(!$insert){
					echo ('Insert Error!!!');
				}
			}
			
		}elseif($fetchAccount['nEMID'] == $_SESSION['nEMID']){
		
		if(isset($_POST['changepw'])){
			$oldpassword = mssql_escape_string($_POST['oldpassword']);
			$newpassword = mssql_escape_string($_POST['password']);
			
			if($newpassword == $_POST['secpassword']){
				$checkAccount = sqlsrv_query($conn, "SELECT * FROM tAccounts WHERE nEMID = ? AND sUserPass = ?;", array($_SESSION['nEMID'], md5($oldpassword)), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));
				if(sqlsrv_num_rows($checkAccount) == 1){
					$updatePassword = sqlsrv_query($conn, "UPDATE tAccounts SET sUserPass = ? WHERE nEMID = ?;", array(md5($newpassword), $_SESSION['nEMID']));
					if($updatePassword){
						showMessage('success', 'Password has been updated!');
					}else{
						showMessage('error', 'sqlsrv error: '.print_r(sqlsrv_errors, true));
					}
				}else{
					showMessage('error', 'Old Password is wrong!', 5, '?module=changepw');
				}
			}else{
				showMessage('error', 'New Password does not match!', 5, '?module=changepw');
			}
		}else{
			echo '<center><form method="post" width="200px" style="display: block;"><input type="password" name="oldpassword" placeholder="Password" required><br><input type="password" name="password" placeholder="New password" required><br><input type="password" name="secpassword" placeholder="Repeat new password" required><br><br><input type="submit" name="changepw" value="changepw" class="myButton"></form><center>';
		}
		}
	}
}
?>