<?php
if(isset($_SESSION['user']) && isset($_SESSION['pass'])){

?>
<?php

if($_GET['new_email'] && $_GET['new_email']!=NULL)
{
	$log = $_SESSION['user'];
	$new_email = replace($_GET['new_email']);
	$cod = replace($_GET['cod']);
	if($new_email !=NULL && $cod!=NULL)
	{
		$chk = mysql_query("Select * from account.account where new_email_change='$new_email' and new_email_change2='$cod'");	
		// Verific daca emailul si codul sunt bune
		if(mysql_num_rows($chk)==1)
		{
			$old = mysql_fetch_object($chk);
			mysql_query("Update account.account set email='$new_email',new_email_change2='1' where login='$log'");
			echo succes("
			
			
			
				  <div class='postui post-title'>
	<h2><span></span></h2>
</div>
<div class='postui post-con'>
	<div class='con-wrap'>
	Email schimbat cu succes.Noua ta adresa este ".$new_email."
			</div>		
</div> 							
<div class='postui post-end'></div>
	"); 
		}
		else
		{
			echo error("<div class='postui post-title'>
	<h2><span></span></h2>
</div>
<div class='postui post-con'>
	<div class='con-wrap'>Link incorect.
				</div>		
</div> 							
<div class='postui post-end'></div>");	
		}
	}
}
else {
if($_GET['email']!=NULL && $_GET['cod'] !=NULL)
{
	
$log = $_SESSION['user'];
	$cod = replace($_GET['cod']);
	$email = replace($_GET['email']);
	if($cod != NULL && $email != NULL)
	{
		$cods = mysql_query("Select * from account.account where email='$email' AND emailchange_token='$cod'");
		if(mysql_num_rows($cods) == 0)
		{
			echo error("<div class='postui post-title'>
	<h2><span></span></h2>
</div>
<div class='postui post-con'>
	<div class='con-wrap'>Link incorect.			</div>		
</div> 							
<div class='postui post-end'></div>");	
		}
		else
		{
			$ch = mysql_fetch_object($cods);
			$cod = md5(rand(999,999999));
			mysql_query("Update account.account set emailchange_token='1',new_email_change2='$cod' where login='$log'") or die(mysql_error());
			$email = $ch->new_email_change;
			$to      = $email;
			$subject = 'Confirmare schimbare email!';
			$message = "Pentru a confirma schimbare emailului acceseaza linkul de mai jos ". "\r\n" ."http://metin2alaska.ro/index.php?page=schimbare-email&new_email=".$email."&cod=".$cod."";
			new mail($to, $subject, $message);
			echo succes("<div class='postui post-title'>
	<h2><span></span></h2>
</div>
<div class='postui post-con'>
	<div class='con-wrap'>Un email a fost trimis catre noua adresa.
	</div>		
</div> 							
<div class='postui post-end'></div>");	
		}
	}
}
else {


	$oldemail = replace($_POST['oldEmail']);
	$newemail = replace($_POST['newEmail']);
	$log = $_SESSION['user'];
	if($oldemail !=NULL && $newemail !=NULL)
	{
		if($oldemail!=$newemail)
		{
			$ch1 = mysql_query("Select * from account.account where login='$log' and email='$oldemail'");
			$ch2 = mysql_query("Select * from account.account where email='$newemail'");
			if(mysql_num_rows($ch2) == 0) // Verific daca emailul nou nu exista deja
			{
				if(mysql_num_rows($ch1) == 1) // Emailul vechi este al meu
				{
					$cod = md5(rand(999,999999));
					mysql_query("Update account.account set emailchange_token='$cod',new_email_change='$newemail' where login='$log'"); // Generez cod si inserez codul si adresa noua in db
					$email = $oldemail;
					$to      = $email;
					$subject = 'Confirmare schimbare email!';
					$message = "Pentru a confirma schimbare emailului acceseaza linkul de mai jos ". "\r\n" ."http://metin2alaska.ro/index.php?page=schimbare-email&email=".$oldemail."&cod=".$cod."
							";
							
		
							new mail($to, $subject, $message);
							echo succes("<div class='postui post-title'>
	<h2><span></span></h2>
</div>
<div class='postui post-con'>
	<div class='con-wrap'>Un mail pentru confirmare a fost trimis la adresa veche.Confirmati pentru a continua.	</div>		
</div> 							
<div class='postui post-end'></div>");// Trimit codul pe emailul vechi.
				}
				else
				{
					echo error("
					<div class='postui post-title'>
	<h2><span></span></h2>
</div>
<div class='postui post-con'>
	<div class='con-wrap'>Emailul vechi nu se potriveste.
	</div>		
</div> 							
<div class='postui post-end'></div>"); 	
				}
			}
			else
			{
				echo error("
									<div class='postui post-title'>
	<h2><span></span></h2>
</div>
<div class='postui post-con'>
Noul email exista deja in baza de date.Alegeti altul
	</div>		
</div> 							
<div class='postui post-end'></div>");
			}
		}
		else { echo error("					<div class='postui post-title'>
	<h2><span></span></h2>
</div>
<div class='postui post-con'>Emailurile sunt la fel.
			</div>		
</div> 							
<div class='postui post-end'></div>"); } 
	} } }
	  ?>
	  
	  
	  
	  <div class="postui post-title">
	<h2><span>ADMINISTRARE CONT - SCHIMBĂ E-MAIL</span></h2>
</div>
<div class="postui post-con">
	<div class="con-wrap">

<h4>Schimbare email :</h4>

<form name="emailChangeForm" id="emailChangeForm" method="POST" action="">
  <table width="100%" border="0">
    <tr>
    <td width="25%">Adresa veche : </td>
    <td width="75%"><input type="text" id="oldEmail" name="oldEmail" title="" value="" maxlength="64" class="fui txt"/></td>
  </tr>
  <tr>
    <td>Noua adresa : </td>
    <td><input type="text" id="newEmail" name="newEmail" title="" value="" maxlength="64" class="fui txt"/></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input id="submitBtn" type="submit" name="SubmitEmailChange" value="SCHIMBA" class="fui btn log"/></td>
  </tr>
</table></form>

		</div>		
</div> 							
<div class="postui post-end"></div>

<?php } else {echo "Acces restrictionat!";}?>  