<?PHP
/********************************************************************************+\
** -+- LeetWEB Last Edition ~ The way to run a private server
*+*******************************************************************************
** -+- Copyright (C) 2011 Nilz. All rechten worden voorbehouden
** -+- http://www.leethotel.com
**
** -+- Parts Copyright (C) 2011 Joopie. All rechten worden voorbehouden
** -+- http://www.jospiek.nl
\+*******************************************************************************/
$must_login = true;
include("Includes/Global.php");

$pagename = "Mijn gegevens";
$pageid = "profile";

	if(isset($_GET['type']) && $_GET['type'] == '1')
	{
		$currentpw = $_POST['password'];
		$newpw = $_POST['npassword'];
		$newpwr = $_POST['npassworda'];

	if (LeetHash($currentpw) != mysql_result(mysql_query('SELECT `password` FROM `users` WHERE `username` = "'.$username.'" LIMIT 1'), 0))
	{
		$error = 'Your current password is not current.';
	}

	if ($newpw != $newpwr || strlen($newpw) <= 0 || strlen($newpwr) <= 0)
	{
		$error = 'Your password is not current.';
	}

	if (!isset($error))
	{
		$newpw = LeetHash($newpw);
		mysql_query('UPDATE `users` SET `password` = "'.$newpw.'" WHERE `username` = "'.$username.'" LIMIT 1') or die(mysql_error());
		$succes = true;
	
		session_destroy();
	}
}

	if(isset($_GET['type']) && $_GET['type'] == '2')
	{
		$currentmail = Filter($_POST['email']);
		$newmail = Filter($_POST['nemail']);
		$newmaila = Filter($_POST['nemaila']);

	if ($currentmail != mysql_result(mysql_query('SELECT `mail` FROM `users` WHERE `username` = "'.$username.'" LIMIT 1'), 0))
	{
		$errore = 'Invalid email address.';
	}

	if ($newmail != $newmaila || strlen($newmail) <= 0 || strlen($newmaila) <= 0)
	{
		$errore = 'Invalid email address.';
	}

	if (!isset($errore))
	{
		mysql_query('UPDATE `users` SET `mail` = "'.$newmail.'" WHERE `username` = "'.$username.'" LIMIT 1') or die(mysql_error());
		$succese = true;
	}
}
?>
<!doctype html>
<html>
	<head>
		<meta http-equiv="content-type" content="text/html; charset=UTF-8"> 
		<title><?PHP echo $shortname; ?>: <?PHP echo $pagename; ?></title>
		
		<link href="<?PHP echo $path; ?>/Web/style/global.css" type="text/css" rel="stylesheet">
		<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
		<link rel="icon" type="image/x-icon" href="<?PHP echo $path; ?>/Web/logo.ico" /> 
	</head>
	<body>
		<div class="begin static center">
			<div class="top">
				<div class="logo"><a href="/"><img src="<?PHP echo $path; ?>/Web/leet.png" alt=""></a></div>
				
				<div class="balk">
					
					<ul>
						<li class="title">Welcome to <?PHP echo $sitename; ?>!</li>
						<li class="menuitem" onclick="location.href = '<?PHP echo $path; ?>/logout.php';"><p class="icon ilogout"></p>Log out</li>
						<li class="menuitem" onclick="location.href = '<?PHP echo $path; ?>/settings.php';"><p class="icon isettings"></p>Settings</li>
						<li class="menuitem" onclick="location.href = '<?PHP echo $path; ?>/nieuws.php';"><p class="icon inews"></p>News</li>
						<li class="menuitem" onclick="location.href = '<?PHP echo $path; ?>/me.php';"><p class="icon ihome"></p>Home</li>
					</ul>
					
				</div>
			</div>
			
			<div class="page">
				
				<div class="box static">
					<div class="title">Settings</div>
					<div class="content settings">

						<p><b>Here you can change your settings for example when a new email address or if you want a new password.</b></p>
						
						<div class="center">
							<input type="button" id="button0" value="Change password">
							<input type="button" id="button1" value="Change email address">
						</div>
						
						<script type="text/javascript">
							var last_open = '<?php echo (isset($_GET['type'])) ? (($_GET['type'] == '1') ? 'change_password' : 'change_email') : 'default'; ?>';
							$(function(){
								$("#button0").click(function(){
									$("#"+last_open).slideUp("fast", function(){
										$("#change_password").slideDown("fast");
										last_open = 'change_password';
									});	
								});
								$("#button1").click(function(){
									$("#"+last_open).slideUp("fast", function(){
										$("#change_email").slideDown("fast");
										last_open = 'change_email';
									});	
								});
								$("#button2").click(function(){
									$("#"+last_open).slideUp("fast", function(){
										$("#settings").slideDown("fast");
										last_open = 'settings';
									});	
								});
							});
						</script>
					</div>
				</div>
				
				<div class="box static">
					<div class="content settings">
						<div id="change_password" <?php echo (isset($_GET['type']) && $_GET['type'] == '1') ?'style="display: block;"' : ''; ?>>
							<h3><b>Change password</b></h3>

<?PHP
		if(isset($error))
		{
			echo "<div class=\"errorbox\"><font color=white>".$error."</font></div>";
		}
		if(isset($succes))
		{
			echo "<div class=\"succesbox\"><font color=white>Your password has been changed. Please login again!</font></div>";
		}
?>

							<form action="<?PHP echo $path; ?>/settings.php?type=1" method="post">
								<p><label>Current password:</label><br>
								<input type="password" name="password" value=""></p>
								<br>
								
								<p><label>New password:</label><br>
								<input type="password" name="npassword" value=""></p>
								<p><label>New password again:</label><br>
								<input type="password" name="npassworda" value=""></p>
								
								<p><input type="submit" name="submit" value="Change"></p>
							</form>
						</div>
						
						<div id="change_email" <?php echo (isset($_GET['type']) && $_GET['type'] == '2') ? 'style="display: block;"' : ''; ?>>
							<h3><b>Change email address</b></h3>

<?PHP
	if(isset($errore))
	{
		echo "<div class=\"errorbox\"><font color=white>".$errore."</font></div>";
	}
	if(isset($succese))
	{
		echo "<div class=\"succesbox\"><font color=white>Your email has been changed.</font></div>";
	}
?>
							<form action="<?PHP echo $path; ?>/settings.php?type=2" method="post">
								<p><label>Current email:</label><br>
								<input type="text" name="email" value=""></p>
								<br>
								
								<p><label>New email:</label><br>
								<input type="text" name="nemail" value=""></p>
								<p><label>New email again:</label><br>
								<input type="text" name="nemaila" value=""></p>
								
								<p><input type="submit" name="submit" value="Change"></p>
							</form>
						</div>
						
						<div id="default">
						
						</div>
						
					</div>
				</div>
								
			</div>
		</div>
		
		<div class="copyright static center">
		<?PHP include("Includes/Footer.php"); ?>
		</div>
	</body>
</html>